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/
43 /* The maximum length of an output line. This is conservatively
44 * slightly smaller than the PGN export standard of 80.
46 #define MAX_LINE_LENGTH 75
48 /* Define a file name relative to the current directory representing
49 * a file of ECO classificiations.
51 #ifndef DEFAULT_ECO_FILE
52 #define DEFAULT_ECO_FILE "eco.pgn"
55 /* This structure holds details of the program state
56 * available to all parts of the program.
57 * This goes against the grain of good structured programming
58 * principles, but most of these fields are set from the program's
59 * arguments and are read-only thereafter. If I had done this in
60 * C++ there would have been a cleaner interface!
62 StateInfo GlobalState = {
63 FALSE, /* skipping_current_game */
64 FALSE, /* check_only (-r) */
65 TRUE, /* verbose (-s) */
66 TRUE, /* keep_NAGs (-N) */
67 TRUE, /* keep_comments (-C) */
68 TRUE, /* keep_variations (-V) */
69 ALL_TAGS, /* tag_output_form (-7, --notags) */
70 TRUE, /* match_permutations (-v) */
71 FALSE, /* positional_variations (-x) */
72 FALSE, /* use_soundex (-S) */
73 FALSE, /* suppress_duplicates (-D) */
74 FALSE, /* suppress_originals (-U) */
75 FALSE, /* fuzzy_match_duplicates (--fuzzy) */
76 0, /* fuzzy_match_depth (--fuzzy) */
77 FALSE, /* check_tags */
78 FALSE, /* add_ECO (-e) */
79 FALSE, /* parsing_ECO_file (-e) */
80 DONT_DIVIDE, /* ECO_level (-E) */
81 SAN, /* output_format (-W) */
82 MAX_LINE_LENGTH, /* max_line_length (-w) */
83 FALSE, /* use_virtual_hash_table (-Z) */
84 FALSE, /* check_move_bounds (-b) */
85 FALSE, /* match_only_checkmate (-M) */
86 FALSE, /* match_only_stalemate (--stalemate) */
87 TRUE, /* keep_move_numbers (--nomovenumbers) */
88 TRUE, /* keep_results (--noresults) */
89 TRUE, /* keep_checks (--nochecks) */
90 FALSE, /* output_evaluation (--evaluation) */
91 FALSE, /* keep_broken_games (--keepbroken) */
92 0, /* depth_of_positional_search */
93 0, /* num_games_processed */
94 0, /* num_games_matched */
95 0, /* games_per_file (-#) */
96 1, /* next_file_number */
97 0, /* lower_move_bound */
98 10000, /* upper_move_bound */
99 -1, /* output_ply_limit (--plylimit) */
100 0, /* matching_game_number */
101 FALSE, /* output_FEN_string */
102 FALSE, /* add_FEN_comments (--fencomments) */
103 FALSE, /* add_position_match_comments (--markmatches) */
104 FALSE, /* output_total_plycount (--totalplycount) */
105 FALSE, /* add_hashcode_tag (--addhashcode) */
106 FALSE, /* dump-eco (--dumpeco) */
107 "MATCH", /* position_match_comment (--markpositionmatches) */
108 (char *)NULL, /* current_input_file */
109 NORMALFILE, /* current_file_type */
110 0, /* start_position */
111 LONG_MAX, /* end_position */
112 DEFAULT_ECO_FILE, /* eco_file (-e) */
113 (FILE *)NULL, /* outputfile (-o, -a). Default is stdout */
114 (char *)NULL, /* output_filename (-o, -a) */
115 (FILE *)NULL, /* logfile (-l). Default is stderr */
116 (FILE *)NULL, /* duplicate_file (-d) */
117 (FILE *)NULL, /* non_matching_file (-n) */
120 /* Prepare the output file handles in GlobalState. */
122 init_default_global_state(void)
124 GlobalState.outputfile = stdout;
125 GlobalState.logfile = stderr;
126 set_output_line_length(MAX_LINE_LENGTH);
130 main(int argc, char *argv[])
133 /* Prepare global state. */
134 init_default_global_state();
135 /* Prepare the Game_Header. */
137 /* Prepare the tag lists for -t/-T matching. */
139 /* Prepare the hash tables for transposition detection. */
141 /* Initialise the lexical analyser's tables. */
143 /* Allow for some arguments. */
144 for(argnum = 1; argnum < argc; ){
145 const char *argument = argv[argnum];
146 if(argument[0] == '-'){
148 /* Arguments with no additional component. */
149 case SEVEN_TAG_ROSTER_ARGUMENT:
150 case DONT_KEEP_COMMENTS_ARGUMENT:
151 case DONT_KEEP_DUPLICATES_ARGUMENT:
152 case DONT_KEEP_VARIATIONS_ARGUMENT:
153 case DONT_KEEP_NAGS_ARGUMENT:
154 case DONT_MATCH_PERMUTATIONS_ARGUMENT:
155 case CHECK_ONLY_ARGUMENT:
156 case KEEP_SILENT_ARGUMENT:
157 case USE_SOUNDEX_ARGUMENT:
158 case MATCH_CHECKMATE_ARGUMENT:
159 case SUPPRESS_ORIGINALS_ARGUMENT:
160 case OUTPUT_FEN_STRING_ARGUMENT:
161 case USE_VIRTUAL_HASH_TABLE_ARGUMENT:
162 process_argument(argument[1], "");
166 /* Argument rewritten as a different one. */
167 case ALTERNATIVE_HELP_ARGUMENT:
168 process_argument(HELP_ARGUMENT, "");
172 /* Arguments where an additional component is required.
173 * It must be adjacent to the argument and not separated from it.
175 case TAG_EXTRACTION_ARGUMENT:
176 process_argument(argument[1], &(argument[2]));
180 /* Arguments where an additional component is optional.
181 * If it is present, it must be adjacent to the argument
182 * letter and not separated from it.
184 case USE_ECO_FILE_ARGUMENT:
185 case OUTPUT_FORMAT_ARGUMENT:
187 process_argument(argument[1], &(argument[2]));
191 /* Long form arguments. */
192 case LONG_FORM_ARGUMENT:
194 /* How many args (1 or 2) are processed. */
196 /* This argument might need the following argument
197 * as an associated value.
199 const char *possible_associated_value = "";
200 if(argnum + 1 < argc) {
201 possible_associated_value = argv[argnum+1];
203 /* Find out how many arguments were consumed
207 process_long_form_argument(&argument[2],
208 possible_associated_value);
209 argnum += args_processed;
213 /* Arguments with a required filename component. */
214 case FILE_OF_ARGUMENTS_ARGUMENT:
215 case APPEND_TO_OUTPUT_FILE_ARGUMENT:
216 case CHECK_FILE_ARGUMENT:
217 case DUPLICATES_FILE_ARGUMENT:
218 case FILE_OF_FILES_ARGUMENT:
219 case WRITE_TO_LOG_FILE_ARGUMENT:
220 case APPEND_TO_LOG_FILE_ARGUMENT:
221 case NON_MATCHING_GAMES_ARGUMENT:
222 case WRITE_TO_OUTPUT_FILE_ARGUMENT:
223 case TAG_ROSTER_ARGUMENT:
224 { /* We require an associated file argument. */
225 const char argument_letter = argument[1];
226 const char *filename = &(argument[2]);
227 if(*filename == '\0'){
228 /* Try to pick it up from the next argument. */
231 filename = argv[argnum];
234 /* Make sure the associated_value does not look
235 * like the next argument.
237 if((*filename == '\0') || (*filename == '-')){
238 fprintf(GlobalState.logfile,
239 "Usage: -%c filename\n",
247 process_argument(argument[1], filename);
251 /* Arguments with a required following value. */
252 case BOUNDS_ARGUMENT:
253 case ECO_OUTPUT_LEVEL_ARGUMENT:
254 case LINE_WIDTH_ARGUMENT:
255 case GAMES_PER_FILE_ARGUMENT:
256 { /* We require an associated file argument. */
257 const char argument_letter = argument[1];
258 const char *associated_value = &(argument[2]);
259 if(*associated_value == '\0'){
260 /* Try to pick it up from the next argument. */
263 associated_value = argv[argnum];
266 /* Make sure the associated_value does not look
267 * like the next argument.
269 if((*associated_value == '\0') ||
270 (*associated_value == '-')){
271 fprintf(GlobalState.logfile,
272 "Usage: -%c value\n",
280 process_argument(argument[1], associated_value);
284 /* Argument that require different treatment because they
285 * are present on the command line rather than an argsfile.
289 case POSITIONS_ARGUMENT:
290 case ENDINGS_ARGUMENT:
291 { /* From the command line, we require an
292 * associated file argument.
293 * Check this here, as it is not the case
294 * when reading arguments from an argument file.
296 const char *filename = &(argument[2]);
297 const char argument_letter = argument[1];
298 if(*filename == '\0'){
299 /* Try to pick it up from the next argument. */
302 filename = argv[argnum];
305 /* Make sure the filename does not look
306 * like the next argument.
308 if((*filename == '\0') || (*filename == '-')){
309 fprintf(GlobalState.logfile,
310 "Usage: -%cfilename or -%c filename\n",
311 argument_letter,argument_letter);
318 process_argument(argument_letter,filename);
322 fprintf(GlobalState.logfile,
323 "Unknown flag %s. Use -%c for usage details.\n",
324 argument,HELP_ARGUMENT);
330 /* Should be a file name containing games. */
331 add_filename_to_source_list(argument,NORMALFILE);
335 /* Prepare the hash tables for duplicate detection. */
336 init_duplicate_hash_table();
338 if(GlobalState.add_ECO){
339 /* Read in a list of ECO lines in order to classify the games. */
340 if(open_eco_file(GlobalState.eco_file)){
341 /* Indicate that the ECO file is currently being parsed. */
342 GlobalState.parsing_ECO_file = TRUE;
345 GlobalState.parsing_ECO_file = FALSE;
348 fprintf(GlobalState.logfile,"Unable to open the ECO file %s.\n",
349 GlobalState.eco_file);
354 /* Open up the first file to act as Lex's source of input. */
355 if(!open_first_file()){
358 yyparse(GlobalState.current_file_type);
359 /* Remove any temporary files. */
360 clear_duplicate_hash_table();
361 if(GlobalState.verbose){
362 fprintf(GlobalState.logfile,"%lu game%s matched out of %lu.\n",
363 GlobalState.num_games_matched,
364 GlobalState.num_games_matched == 1?"":"s",
365 GlobalState.num_games_processed);
367 if((GlobalState.logfile != stderr) && (GlobalState.logfile != NULL)){
368 (void) fclose(GlobalState.logfile);