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)
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.
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.
18 * David Barnes may be contacted as D.J.Barnes@kent.ac.uk
19 * http://www.cs.kent.ac.uk/people/staff/djb/
42 /* The maximum length of an output line. This is conservatively
43 * slightly smaller than the PGN export standard of 80.
45 #define MAX_LINE_LENGTH 75
47 /* Define a file name relative to the current directory representing
48 * a file of ECO classificiations.
50 #ifndef DEFAULT_ECO_FILE
51 #define DEFAULT_ECO_FILE "eco.pgn"
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!
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) */
117 /* Prepare the output file handles in GlobalState. */
119 init_default_global_state(void)
121 GlobalState.outputfile = stdout;
122 GlobalState.logfile = stderr;
123 set_output_line_length(MAX_LINE_LENGTH);
127 main(int argc, char *argv[])
130 /* Prepare global state. */
131 init_default_global_state();
132 /* Prepare the Game_Header. */
134 /* Prepare the tag lists for -t/-T matching. */
136 /* Prepare the hash tables for transposition detection. */
138 /* Initialise the lexical analyser's tables. */
140 /* Allow for some arguments. */
141 for(argnum = 1; argnum < argc; ){
142 const char *argument = argv[argnum];
143 if(argument[0] == '-'){
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], "");
163 /* Argument rewritten as a different one. */
164 case ALTERNATIVE_HELP_ARGUMENT:
165 process_argument(HELP_ARGUMENT, "");
169 /* Arguments where an additional component is required.
170 * It must be adjacent to the argument and not separated from it.
172 case TAG_EXTRACTION_ARGUMENT:
173 process_argument(argument[1], &(argument[2]));
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.
181 case USE_ECO_FILE_ARGUMENT:
182 case OUTPUT_FORMAT_ARGUMENT:
184 process_argument(argument[1], &(argument[2]));
188 /* Long form arguments. */
189 case LONG_FORM_ARGUMENT:
191 /* How many args (1 or 2) are processed. */
193 /* This argument might need the following argument
194 * as an associated value.
196 const char *possible_associated_value = "";
197 if(argnum + 1 < argc) {
198 possible_associated_value = argv[argnum+1];
200 /* Find out how many arguments were consumed
204 process_long_form_argument(&argument[2],
205 possible_associated_value);
206 argnum += args_processed;
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. */
228 filename = argv[argnum];
231 /* Make sure the associated_value does not look
232 * like the next argument.
234 if((*filename == '\0') || (*filename == '-')){
235 fprintf(GlobalState.logfile,
236 "Usage: -%c filename\n",
244 process_argument(argument[1], filename);
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. */
260 associated_value = argv[argnum];
263 /* Make sure the associated_value does not look
264 * like the next argument.
266 if((*associated_value == '\0') ||
267 (*associated_value == '-')){
268 fprintf(GlobalState.logfile,
269 "Usage: -%c value\n",
277 process_argument(argument[1], associated_value);
281 /* Argument that require different treatment because they
282 * are present on the command line rather than an argsfile.
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.
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. */
299 filename = argv[argnum];
302 /* Make sure the filename does not look
303 * like the next argument.
305 if((*filename == '\0') || (*filename == '-')){
306 fprintf(GlobalState.logfile,
307 "Usage: -%cfilename or -%c filename\n",
308 argument_letter,argument_letter);
315 process_argument(argument_letter,filename);
319 fprintf(GlobalState.logfile,
320 "Unknown flag %s. Use -%c for usage details.\n",
321 argument,HELP_ARGUMENT);
327 /* Should be a file name containing games. */
328 add_filename_to_source_list(argument,NORMALFILE);
332 /* Prepare the hash tables for duplicate detection. */
333 init_duplicate_hash_table();
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;
342 GlobalState.parsing_ECO_file = FALSE;
345 fprintf(GlobalState.logfile,"Unable to open the ECO file %s.\n",
346 GlobalState.eco_file);
351 /* Open up the first file to act as Lex's source of input. */
352 if(!open_first_file()){
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);
364 if((GlobalState.logfile != stderr) && (GlobalState.logfile != NULL)){
365 (void) fclose(GlobalState.logfile);