File: cli.c

package info (click to toggle)
pgn2web 0.4-3.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 2,116 kB
  • sloc: ansic: 1,938; cpp: 419; makefile: 39
file content (186 lines) | stat: -rw-r--r-- 4,086 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
  pgn2web - Converts PGN files to interactive web pages

  Copyright (C) 2004, 2005 William Hoggarth <email: whoggarth@users.sourceforge.net>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <unistd.h>
#include <stdio.h> 
#include <stdlib.h>
#include <string.h>

#include <sys/stat.h>

#include "bool.h"
#include "pgn2web.h"

/* default installation path */
#ifndef INSTALL_PATH
#define INSTALL_PATH "/usr/share/pgn2web/"
#endif

char usage[] = "usage: pgn2web\n"
               "       pgn2web [-c yes|no] [-p <pieces>] [-s frameset|linked|individual] pgn-filename html-filename\n";

/* main function */
int main(int argc, char *argv[])
{
  bool valid = true;
  bool in_options = true;
  int arg;
  bool credit_set = false;
  bool layout_set = false;
  int pgn_filename = 0;
  int html_filename = 0;
  int pieces = 0;

  char *path;
  struct stat stat_buf;

  /* default options */
  bool credit = true;
  STRUCTURE layout = FRAMESET;

  /* if no arguments provided then launch gui */
  if(argc == 1) {
    execvp("p2wgui", 0);
  }

  /* parse the arguments */
  arg = 1;
  while(arg < argc) {
    
    /* check for option and process it, else assume filenames */
    if(in_options && argv[arg][0] == '-') {

      if(!credit_set && !strcmp("-c", argv[arg])) {
	
	if(!strcmp("yes", argv[arg + 1])) {
	  credit = true;
	  credit_set = true;
	  arg +=2;
	  continue;
	}

	if(!strcmp("no", argv[arg + 1])) {
	  credit = false;
	  credit_set = true;
	  arg += 2;
	  continue;
	}

	valid = false;
	break;
      }

      if(!pieces && !strcmp("-p", argv[arg])) {

	/* check the piece set is valid */
	path = (char*)calloc(strlen(INSTALL_PATH) + strlen("/images/") + strlen(argv[arg + 1]) + 1, sizeof(char));
	strcpy(path, INSTALL_PATH);
	strcat(path, "/images/");
	strcat(path, argv[arg + 1]);

	if(!stat(path, &stat_buf)) {
	  free((void*)path);

	  pieces = arg + 1;
	  arg += 2;
	  continue;
	}
	else {
	  free((void*)path);
	  valid = false;
	  break;
	}
      }
	
      if(!layout_set && !strcmp("-s", argv[arg])) {

	if(!strcmp("frameset", argv[arg + 1])) {
	  layout_set = true;
	  layout = FRAMESET;
	  arg += 2;
	  continue;
	} 

	if(!strcmp("linked", argv[arg + 1])) {
	  layout_set = true;
	  layout = LINKED;
	  arg += 2;
	  continue;
	}

	if(!strcmp("individual", argv[arg + 1])) {
	  layout_set = true;
	  layout = INDIVIDUAL;
	  arg += 2;
	  continue;
	}

	valid = false;
	break;
      }

      /* invalid option as there is no match */
      valid = false;
      break;
    } 
    else {

      /* we now expect filename and not options */
      in_options = false;

      /* test for an option in the wrong place */
      if(argv[arg][0] == '-') {
	valid = false;
	break;
      }
      
      /* record which argument holds the filename */
      if(pgn_filename == 0) {
	pgn_filename = arg;
      }
      else if (html_filename == 0) {
	html_filename = arg;
      }
      else {
	/* we already have both filenames, so there must be an error */
	valid = false;
	break;
      }
    }

    /* move on to the next argument */
    arg++;
  }

  /* make sure that we have both filenames */
  if(!pgn_filename || !html_filename) {
    valid = false;
  }

  /* either execute or print error message */
  if(valid) {
    return pgn2web(INSTALL_PATH, argv[pgn_filename], argv[html_filename], credit,
		   pieces ? argv[pieces] : "merida", layout, NULL, NULL);
  }
  else {
    printf("%s", usage);
    return 1;
  }
}