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