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 "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) */
116 /* Prepare the output file handles in GlobalState. */
118 init_default_global_state(void)
120 GlobalState.outputfile = stdout;
121 GlobalState.logfile = stderr;
122 set_output_line_length(MAX_LINE_LENGTH);
126 main(int argc, char *argv[])
129 /* Prepare global state. */
130 init_default_global_state();
131 /* Prepare the Game_Header. */
133 /* Prepare the tag lists for -t/-T matching. */
135 /* Prepare the hash tables for transposition detection. */
137 /* Initialise the lexical analyser's tables. */
139 /* Allow for some arguments. */
140 for(argnum = 1; argnum < argc; ){
141 const char *argument = argv[argnum];
142 if(argument[0] == '-'){
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], "");
162 /* Argument rewritten as a different one. */
163 case ALTERNATIVE_HELP_ARGUMENT:
164 process_argument(HELP_ARGUMENT, "");
168 /* Arguments where an additional component is required.
169 * It must be adjacent to the argument and not separated from it.
171 case TAG_EXTRACTION_ARGUMENT:
172 process_argument(argument[1], &(argument[2]));
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.
180 case USE_ECO_FILE_ARGUMENT:
181 case OUTPUT_FORMAT_ARGUMENT:
183 process_argument(argument[1], &(argument[2]));
187 /* Long form arguments. */
188 case LONG_FORM_ARGUMENT:
190 /* How many args (1 or 2) are processed. */
192 /* This argument might need the following argument
193 * as an associated value.
195 const char *possible_associated_value = "";
196 if(argnum + 1 < argc) {
197 possible_associated_value = argv[argnum+1];
199 /* Find out how many arguments were consumed
203 process_long_form_argument(&argument[2],
204 possible_associated_value);
205 argnum += args_processed;
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. */
227 filename = argv[argnum];
230 /* Make sure the associated_value does not look
231 * like the next argument.
233 if((*filename == '\0') || (*filename == '-')){
234 fprintf(GlobalState.logfile,
235 "Usage: -%c filename\n",
243 process_argument(argument[1], filename);
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. */
259 associated_value = argv[argnum];
262 /* Make sure the associated_value does not look
263 * like the next argument.
265 if((*associated_value == '\0') ||
266 (*associated_value == '-')){
267 fprintf(GlobalState.logfile,
268 "Usage: -%c value\n",
276 process_argument(argument[1], associated_value);
280 /* Argument that require different treatment because they
281 * are present on the command line rather than an argsfile.
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.
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. */
298 filename = argv[argnum];
301 /* Make sure the filename does not look
302 * like the next argument.
304 if((*filename == '\0') || (*filename == '-')){
305 fprintf(GlobalState.logfile,
306 "Usage: -%cfilename or -%c filename\n",
307 argument_letter,argument_letter);
314 process_argument(argument_letter,filename);
318 fprintf(GlobalState.logfile,
319 "Unknown flag %s. Use -%c for usage details.\n",
320 argument,HELP_ARGUMENT);
326 /* Should be a file name containing games. */
327 add_filename_to_source_list(argument,NORMALFILE);
331 /* Prepare the hash tables for duplicate detection. */
332 init_duplicate_hash_table();
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;
341 GlobalState.parsing_ECO_file = FALSE;
344 fprintf(GlobalState.logfile,"Unable to open the ECO file %s.\n",
345 GlobalState.eco_file);
350 /* Open up the first file to act as Lex's source of input. */
351 if(!open_first_file()){
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);
363 if((GlobalState.logfile != stderr) && (GlobalState.logfile != NULL)){
364 (void) fclose(GlobalState.logfile);