]> git.sesse.net Git - pgn-extract/blob - main.c
Add support for outputting positions in my own bit-packed FEN format.
[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     "MATCH",              /* position_match_comment (--markpositionmatches) */
106     (char *)NULL,         /* current_input_file */
107     NORMALFILE,           /* current_file_type */
108     DEFAULT_ECO_FILE,     /* eco_file (-e) */
109     (FILE *)NULL,         /* outputfile (-o, -a). Default is stdout */
110     (char *)NULL,         /* output_filename (-o, -a) */
111     (FILE *)NULL,         /* logfile (-l). Default is stderr */
112     (FILE *)NULL,         /* duplicate_file (-d) */
113     (FILE *)NULL,         /* non_matching_file (-n) */
114 };
115
116          /* Prepare the output file handles in GlobalState. */
117 static void
118 init_default_global_state(void)
119 {
120     GlobalState.outputfile = stdout;
121     GlobalState.logfile = stderr;
122     set_output_line_length(MAX_LINE_LENGTH);
123 }
124
125 int
126 main(int argc, char *argv[])
127 {   int argnum;
128
129     /* Prepare global state. */
130     init_default_global_state();
131     /* Prepare the Game_Header. */
132     init_game_header();
133     /* Prepare the tag lists for -t/-T matching. */
134     init_tag_lists();
135     /* Prepare the hash tables for transposition detection. */
136     init_hashtab();
137     /* Initialise the lexical analyser's tables. */
138     init_lex_tables();
139     /* Allow for some arguments. */
140     for(argnum = 1; argnum < argc; ){
141         const char *argument = argv[argnum];
142         if(argument[0] == '-'){
143             switch(argument[1]){
144                 /* Arguments with no additional component. */
145                 case SEVEN_TAG_ROSTER_ARGUMENT:
146                 case DONT_KEEP_COMMENTS_ARGUMENT:
147                 case DONT_KEEP_DUPLICATES_ARGUMENT:
148                 case DONT_KEEP_VARIATIONS_ARGUMENT:
149                 case DONT_KEEP_NAGS_ARGUMENT:
150                 case DONT_MATCH_PERMUTATIONS_ARGUMENT:
151                 case CHECK_ONLY_ARGUMENT:
152                 case KEEP_SILENT_ARGUMENT:
153                 case USE_SOUNDEX_ARGUMENT:
154                 case MATCH_CHECKMATE_ARGUMENT:
155                 case SUPPRESS_ORIGINALS_ARGUMENT:
156                 case OUTPUT_FEN_STRING_ARGUMENT:
157                 case USE_VIRTUAL_HASH_TABLE_ARGUMENT:
158                     process_argument(argument[1], "");
159                     argnum++;
160                     break;
161
162                 /* Argument rewritten as a different one. */
163                 case ALTERNATIVE_HELP_ARGUMENT:
164                     process_argument(HELP_ARGUMENT, "");
165                     argnum++;
166                     break;
167
168                 /* Arguments where an additional component is required.
169                  * It must be adjacent to the argument and not separated from it.
170                  */
171                 case TAG_EXTRACTION_ARGUMENT:
172                     process_argument(argument[1], &(argument[2]));
173                     argnum++;
174                     break;
175
176                 /* Arguments where an additional component is optional.
177                  * If it is present, it must be adjacent to the argument
178                  * letter and not separated from it.
179                  */
180                 case USE_ECO_FILE_ARGUMENT:
181                 case OUTPUT_FORMAT_ARGUMENT:
182                 case HELP_ARGUMENT:
183                     process_argument(argument[1], &(argument[2]));
184                     argnum++;
185                     break;
186
187                 /* Long form arguments. */
188                 case LONG_FORM_ARGUMENT:
189                     {  
190                       /* How many args (1 or 2) are processed. */
191                       int args_processed;
192                       /* This argument might need the following argument
193                        * as an associated value.
194                        */
195                       const char *possible_associated_value = "";
196                       if(argnum + 1 < argc) {
197                           possible_associated_value = argv[argnum+1];
198                       }
199                       /* Find out how many arguments were consumed
200                        * (1 or 2).
201                        */
202                       args_processed =
203                           process_long_form_argument(&argument[2],
204                                                      possible_associated_value);
205                       argnum += args_processed;
206                     }
207                     break;
208
209                 /* Arguments with a required filename component. */
210                 case FILE_OF_ARGUMENTS_ARGUMENT:
211                 case APPEND_TO_OUTPUT_FILE_ARGUMENT:
212                 case CHECK_FILE_ARGUMENT:
213                 case DUPLICATES_FILE_ARGUMENT:
214                 case FILE_OF_FILES_ARGUMENT:
215                 case WRITE_TO_LOG_FILE_ARGUMENT:
216                 case APPEND_TO_LOG_FILE_ARGUMENT:
217                 case NON_MATCHING_GAMES_ARGUMENT:
218                 case WRITE_TO_OUTPUT_FILE_ARGUMENT:
219                 case TAG_ROSTER_ARGUMENT:
220                     {   /* We require an associated file argument. */
221                         const char argument_letter = argument[1];
222                         const char *filename = &(argument[2]);
223                         if(*filename == '\0'){
224                              /* Try to pick it up from the next argument. */
225                              argnum++;
226                              if(argnum < argc){
227                                  filename = argv[argnum];
228                                  argnum++;
229                              }
230                             /* Make sure the associated_value does not look
231                              * like the next argument.
232                              */
233                             if((*filename == '\0') || (*filename == '-')){
234                                 fprintf(GlobalState.logfile,
235                                         "Usage: -%c filename\n",
236                                         argument_letter);
237                                 exit(1);
238                             }
239                         }
240                         else {
241                             argnum++;
242                         }
243                         process_argument(argument[1], filename);
244                     }
245                     break;
246
247                 /* Arguments with a required following value. */
248                 case BOUNDS_ARGUMENT:
249                 case ECO_OUTPUT_LEVEL_ARGUMENT:
250                 case LINE_WIDTH_ARGUMENT:
251                 case GAMES_PER_FILE_ARGUMENT:
252                     {   /* We require an associated file argument. */
253                         const char argument_letter = argument[1];
254                         const char *associated_value = &(argument[2]);
255                         if(*associated_value == '\0'){
256                              /* Try to pick it up from the next argument. */
257                              argnum++;
258                              if(argnum < argc){
259                                  associated_value = argv[argnum];
260                                  argnum++;
261                              }
262                             /* Make sure the associated_value does not look
263                              * like the next argument.
264                              */
265                             if((*associated_value == '\0') ||
266                                        (*associated_value == '-')){
267                                 fprintf(GlobalState.logfile,
268                                         "Usage: -%c value\n",
269                                         argument_letter);
270                                 exit(1);
271                             }
272                         }
273                         else {
274                             argnum++;
275                         }
276                         process_argument(argument[1], associated_value);
277                     }
278                     break;
279
280                 /* Argument that require different treatment because they
281                  * are present on the command line rather than an argsfile.
282                  */
283                 case TAGS_ARGUMENT:
284                 case MOVES_ARGUMENT:
285                 case POSITIONS_ARGUMENT:
286                 case ENDINGS_ARGUMENT:
287                     {   /* From the command line, we require an
288                          * associated file argument.
289                          * Check this here, as it is not the case
290                          * when reading arguments from an argument file.
291                          */
292                         const char *filename =  &(argument[2]);
293                         const char argument_letter = argument[1];
294                         if(*filename == '\0'){
295                              /* Try to pick it up from the next argument. */
296                              argnum++;
297                              if(argnum < argc){
298                                  filename = argv[argnum];
299                                  argnum++;
300                              }
301                             /* Make sure the filename does not look
302                              * like the next argument.
303                              */
304                             if((*filename == '\0') || (*filename == '-')){
305                                 fprintf(GlobalState.logfile,
306                                         "Usage: -%cfilename or -%c filename\n",
307                                         argument_letter,argument_letter);
308                                 exit(1);
309                             }
310                         }
311                         else{
312                             argnum++;
313                         }
314                         process_argument(argument_letter,filename);
315                     }
316                     break;
317                 default:
318                     fprintf(GlobalState.logfile,
319                             "Unknown flag %s. Use -%c for usage details.\n",
320                             argument,HELP_ARGUMENT);
321                     exit(1);
322                     break;
323             }
324         }
325         else{
326             /* Should be a file name containing games. */
327             add_filename_to_source_list(argument,NORMALFILE);
328             argnum++;
329         }
330     }
331     /* Prepare the hash tables for duplicate detection. */
332     init_duplicate_hash_table();
333
334     if(GlobalState.add_ECO){
335         /* Read in a list of ECO lines in order to classify the games. */
336         if(open_eco_file(GlobalState.eco_file)){
337             /* Indicate that the ECO file is currently being parsed. */
338             GlobalState.parsing_ECO_file = TRUE;
339             yyparse(ECOFILE);
340             reset_line_number();
341             GlobalState.parsing_ECO_file = FALSE;
342         }
343         else{
344             fprintf(GlobalState.logfile,"Unable to open the ECO file %s.\n",
345                         GlobalState.eco_file);
346             exit(1);
347         }
348     }
349
350     /* Open up the first file to act as Lex's source of input. */
351     if(!open_first_file()){
352         exit(1);
353     }
354     yyparse(GlobalState.current_file_type);
355     /* Remove any temporary files. */
356     clear_duplicate_hash_table();
357     if(GlobalState.verbose){
358         fprintf(GlobalState.logfile,"%lu game%s matched out of %lu.\n",
359                         GlobalState.num_games_matched,
360                         GlobalState.num_games_matched == 1?"":"s",
361                         GlobalState.num_games_processed);
362     }
363     if((GlobalState.logfile != stderr) && (GlobalState.logfile != NULL)){
364         (void) fclose(GlobalState.logfile);
365     }
366     return 0;
367 }