]> git.sesse.net Git - pgn-extract/blob - main.c
218793a0f7005bf5d352faa0ed85c7d80b4fb058
[pgn-extract] / main.c
1 /*
2  *  Program: pgn-extract: a Portable Game Notation (PGN) extractor.
3  *  Copyright (C) 1994-2012 David Barnes
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 1, or (at your option)
7  *  any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  *
18  *  David Barnes may be contacted as D.J.Barnes@kent.ac.uk
19  *  http://www.cs.kent.ac.uk/people/staff/djb/
20  *
21  */
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <ctype.h>
26 #include "bool.h"
27 #include "mymalloc.h"
28 #include "defs.h"
29 #include "typedef.h"
30 #include "tokens.h"
31 #include "taglist.h"
32 #include "lex.h"
33 #include "moves.h"
34 #include "map.h"
35 #include "lists.h"
36 #include "output.h"
37 #include "end.h"
38 #include "grammar.h"
39 #include "hashing.h"
40 #include "argsfile.h"
41
42 /* The maximum length of an output line.  This is conservatively
43  * slightly smaller than the PGN export standard of 80.
44  */
45 #define MAX_LINE_LENGTH 75
46
47 /* Define a file name relative to the current directory representing
48  * a file of ECO classificiations.
49  */
50 #ifndef DEFAULT_ECO_FILE
51 #define DEFAULT_ECO_FILE "eco.pgn"
52 #endif
53
54     /* This structure holds details of the program state
55      * available to all parts of the program.
56      * This goes against the grain of good structured programming
57      * principles, but most of these fields are set from the program's
58      * arguments and are read-only thereafter. If I had done this in
59      * C++ there would have been a cleaner interface!
60      */
61 StateInfo GlobalState = {
62     FALSE,                /* skipping_current_game */
63     FALSE,                /* check_only (-r) */
64     TRUE,                 /* verbose (-s) */
65     TRUE,                 /* keep_NAGs (-N) */
66     TRUE,                 /* keep_comments (-C) */
67     TRUE,                 /* keep_variations (-V) */
68     ALL_TAGS,             /* tag_output_form (-7, --notags) */
69     TRUE,                 /* match_permutations (-v) */
70     FALSE,                /* positional_variations (-x) */
71     FALSE,                /* use_soundex (-S) */
72     FALSE,                /* suppress_duplicates (-D) */
73     FALSE,                /* suppress_originals (-U) */
74     FALSE,                /* fuzzy_match_duplicates (--fuzzy) */
75     0,                    /* fuzzy_match_depth (--fuzzy) */
76     FALSE,                /* check_tags */
77     FALSE,                /* add_ECO (-e) */
78     FALSE,                /* parsing_ECO_file (-e) */
79     DONT_DIVIDE,          /* ECO_level (-E) */
80     SAN,                  /* output_format (-W) */
81     MAX_LINE_LENGTH,      /* max_line_length (-w) */
82     FALSE,                /* use_virtual_hash_table (-Z) */
83     FALSE,                /* check_move_bounds (-b) */
84     FALSE,                /* match_only_checkmate (-M) */
85     FALSE,                /* match_only_stalemate (--stalemate) */
86     TRUE,                 /* keep_move_numbers (--nomovenumbers) */
87     TRUE,                 /* keep_results (--noresults) */
88     TRUE,                 /* keep_checks (--nochecks) */
89     FALSE,                /* output_evaluation (--evaluation) */
90     FALSE,                /* keep_broken_games (--keepbroken) */
91     0,                    /* depth_of_positional_search */
92     0,                    /* num_games_processed */
93     0,                    /* num_games_matched */
94     0,                    /* games_per_file (-#) */
95     1,                    /* next_file_number */
96     0,                    /* lower_move_bound */
97     10000,                /* upper_move_bound */
98     -1,                   /* output_ply_limit (--plylimit) */
99     0,                    /* matching_game_number */
100     FALSE,                /* output_FEN_string */
101     FALSE,                /* add_FEN_comments (--fencomments) */
102     FALSE,                /* add_position_match_comments (--markmatches) */
103     FALSE,                /* output_total_plycount (--totalplycount) */
104     FALSE,                /* add_hashcode_tag (--addhashcode) */
105     FALSE,                /* dump-eco (--dumpeco) */
106     "MATCH",              /* position_match_comment (--markpositionmatches) */
107     (char *)NULL,         /* current_input_file */
108     NORMALFILE,           /* current_file_type */
109     DEFAULT_ECO_FILE,     /* eco_file (-e) */
110     (FILE *)NULL,         /* outputfile (-o, -a). Default is stdout */
111     (char *)NULL,         /* output_filename (-o, -a) */
112     (FILE *)NULL,         /* logfile (-l). Default is stderr */
113     (FILE *)NULL,         /* duplicate_file (-d) */
114     (FILE *)NULL,         /* non_matching_file (-n) */
115 };
116
117          /* Prepare the output file handles in GlobalState. */
118 static void
119 init_default_global_state(void)
120 {
121     GlobalState.outputfile = stdout;
122     GlobalState.logfile = stderr;
123     set_output_line_length(MAX_LINE_LENGTH);
124 }
125
126 int
127 main(int argc, char *argv[])
128 {   int argnum;
129
130     /* Prepare global state. */
131     init_default_global_state();
132     /* Prepare the Game_Header. */
133     init_game_header();
134     /* Prepare the tag lists for -t/-T matching. */
135     init_tag_lists();
136     /* Prepare the hash tables for transposition detection. */
137     init_hashtab();
138     /* Initialise the lexical analyser's tables. */
139     init_lex_tables();
140     /* Allow for some arguments. */
141     for(argnum = 1; argnum < argc; ){
142         const char *argument = argv[argnum];
143         if(argument[0] == '-'){
144             switch(argument[1]){
145                 /* Arguments with no additional component. */
146                 case SEVEN_TAG_ROSTER_ARGUMENT:
147                 case DONT_KEEP_COMMENTS_ARGUMENT:
148                 case DONT_KEEP_DUPLICATES_ARGUMENT:
149                 case DONT_KEEP_VARIATIONS_ARGUMENT:
150                 case DONT_KEEP_NAGS_ARGUMENT:
151                 case DONT_MATCH_PERMUTATIONS_ARGUMENT:
152                 case CHECK_ONLY_ARGUMENT:
153                 case KEEP_SILENT_ARGUMENT:
154                 case USE_SOUNDEX_ARGUMENT:
155                 case MATCH_CHECKMATE_ARGUMENT:
156                 case SUPPRESS_ORIGINALS_ARGUMENT:
157                 case OUTPUT_FEN_STRING_ARGUMENT:
158                 case USE_VIRTUAL_HASH_TABLE_ARGUMENT:
159                     process_argument(argument[1], "");
160                     argnum++;
161                     break;
162
163                 /* Argument rewritten as a different one. */
164                 case ALTERNATIVE_HELP_ARGUMENT:
165                     process_argument(HELP_ARGUMENT, "");
166                     argnum++;
167                     break;
168
169                 /* Arguments where an additional component is required.
170                  * It must be adjacent to the argument and not separated from it.
171                  */
172                 case TAG_EXTRACTION_ARGUMENT:
173                     process_argument(argument[1], &(argument[2]));
174                     argnum++;
175                     break;
176
177                 /* Arguments where an additional component is optional.
178                  * If it is present, it must be adjacent to the argument
179                  * letter and not separated from it.
180                  */
181                 case USE_ECO_FILE_ARGUMENT:
182                 case OUTPUT_FORMAT_ARGUMENT:
183                 case HELP_ARGUMENT:
184                     process_argument(argument[1], &(argument[2]));
185                     argnum++;
186                     break;
187
188                 /* Long form arguments. */
189                 case LONG_FORM_ARGUMENT:
190                     {  
191                       /* How many args (1 or 2) are processed. */
192                       int args_processed;
193                       /* This argument might need the following argument
194                        * as an associated value.
195                        */
196                       const char *possible_associated_value = "";
197                       if(argnum + 1 < argc) {
198                           possible_associated_value = argv[argnum+1];
199                       }
200                       /* Find out how many arguments were consumed
201                        * (1 or 2).
202                        */
203                       args_processed =
204                           process_long_form_argument(&argument[2],
205                                                      possible_associated_value);
206                       argnum += args_processed;
207                     }
208                     break;
209
210                 /* Arguments with a required filename component. */
211                 case FILE_OF_ARGUMENTS_ARGUMENT:
212                 case APPEND_TO_OUTPUT_FILE_ARGUMENT:
213                 case CHECK_FILE_ARGUMENT:
214                 case DUPLICATES_FILE_ARGUMENT:
215                 case FILE_OF_FILES_ARGUMENT:
216                 case WRITE_TO_LOG_FILE_ARGUMENT:
217                 case APPEND_TO_LOG_FILE_ARGUMENT:
218                 case NON_MATCHING_GAMES_ARGUMENT:
219                 case WRITE_TO_OUTPUT_FILE_ARGUMENT:
220                 case TAG_ROSTER_ARGUMENT:
221                     {   /* We require an associated file argument. */
222                         const char argument_letter = argument[1];
223                         const char *filename = &(argument[2]);
224                         if(*filename == '\0'){
225                              /* Try to pick it up from the next argument. */
226                              argnum++;
227                              if(argnum < argc){
228                                  filename = argv[argnum];
229                                  argnum++;
230                              }
231                             /* Make sure the associated_value does not look
232                              * like the next argument.
233                              */
234                             if((*filename == '\0') || (*filename == '-')){
235                                 fprintf(GlobalState.logfile,
236                                         "Usage: -%c filename\n",
237                                         argument_letter);
238                                 exit(1);
239                             }
240                         }
241                         else {
242                             argnum++;
243                         }
244                         process_argument(argument[1], filename);
245                     }
246                     break;
247
248                 /* Arguments with a required following value. */
249                 case BOUNDS_ARGUMENT:
250                 case ECO_OUTPUT_LEVEL_ARGUMENT:
251                 case LINE_WIDTH_ARGUMENT:
252                 case GAMES_PER_FILE_ARGUMENT:
253                     {   /* We require an associated file argument. */
254                         const char argument_letter = argument[1];
255                         const char *associated_value = &(argument[2]);
256                         if(*associated_value == '\0'){
257                              /* Try to pick it up from the next argument. */
258                              argnum++;
259                              if(argnum < argc){
260                                  associated_value = argv[argnum];
261                                  argnum++;
262                              }
263                             /* Make sure the associated_value does not look
264                              * like the next argument.
265                              */
266                             if((*associated_value == '\0') ||
267                                        (*associated_value == '-')){
268                                 fprintf(GlobalState.logfile,
269                                         "Usage: -%c value\n",
270                                         argument_letter);
271                                 exit(1);
272                             }
273                         }
274                         else {
275                             argnum++;
276                         }
277                         process_argument(argument[1], associated_value);
278                     }
279                     break;
280
281                 /* Argument that require different treatment because they
282                  * are present on the command line rather than an argsfile.
283                  */
284                 case TAGS_ARGUMENT:
285                 case MOVES_ARGUMENT:
286                 case POSITIONS_ARGUMENT:
287                 case ENDINGS_ARGUMENT:
288                     {   /* From the command line, we require an
289                          * associated file argument.
290                          * Check this here, as it is not the case
291                          * when reading arguments from an argument file.
292                          */
293                         const char *filename =  &(argument[2]);
294                         const char argument_letter = argument[1];
295                         if(*filename == '\0'){
296                              /* Try to pick it up from the next argument. */
297                              argnum++;
298                              if(argnum < argc){
299                                  filename = argv[argnum];
300                                  argnum++;
301                              }
302                             /* Make sure the filename does not look
303                              * like the next argument.
304                              */
305                             if((*filename == '\0') || (*filename == '-')){
306                                 fprintf(GlobalState.logfile,
307                                         "Usage: -%cfilename or -%c filename\n",
308                                         argument_letter,argument_letter);
309                                 exit(1);
310                             }
311                         }
312                         else{
313                             argnum++;
314                         }
315                         process_argument(argument_letter,filename);
316                     }
317                     break;
318                 default:
319                     fprintf(GlobalState.logfile,
320                             "Unknown flag %s. Use -%c for usage details.\n",
321                             argument,HELP_ARGUMENT);
322                     exit(1);
323                     break;
324             }
325         }
326         else{
327             /* Should be a file name containing games. */
328             add_filename_to_source_list(argument,NORMALFILE);
329             argnum++;
330         }
331     }
332     /* Prepare the hash tables for duplicate detection. */
333     init_duplicate_hash_table();
334
335     if(GlobalState.add_ECO){
336         /* Read in a list of ECO lines in order to classify the games. */
337         if(open_eco_file(GlobalState.eco_file)){
338             /* Indicate that the ECO file is currently being parsed. */
339             GlobalState.parsing_ECO_file = TRUE;
340             yyparse(ECOFILE);
341             reset_line_number();
342             GlobalState.parsing_ECO_file = FALSE;
343         }
344         else{
345             fprintf(GlobalState.logfile,"Unable to open the ECO file %s.\n",
346                         GlobalState.eco_file);
347             exit(1);
348         }
349     }
350
351     /* Open up the first file to act as Lex's source of input. */
352     if(!open_first_file()){
353         exit(1);
354     }
355     yyparse(GlobalState.current_file_type);
356     /* Remove any temporary files. */
357     clear_duplicate_hash_table();
358     if(GlobalState.verbose){
359         fprintf(GlobalState.logfile,"%lu game%s matched out of %lu.\n",
360                         GlobalState.num_games_matched,
361                         GlobalState.num_games_matched == 1?"":"s",
362                         GlobalState.num_games_processed);
363     }
364     if((GlobalState.logfile != stderr) && (GlobalState.logfile != NULL)){
365         (void) fclose(GlobalState.logfile);
366     }
367     return 0;
368 }