File: main.c

package info (click to toggle)
pgn-extract 16.0-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 912 kB
  • ctags: 737
  • sloc: ansic: 8,857; makefile: 103
file content (332 lines) | stat: -rw-r--r-- 12,722 bytes parent folder | download
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
/*
 *  Program: pgn-extract: a Portable Game Notation (PGN) extractor.
 *  Copyright (C) 1994-2005 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) */
    FALSE,                /* seven_tag_roster (-7) */
    TRUE,                 /* match_permutations (-v) */
    FALSE,                /* positional_variations (-x) */
    FALSE,                /* use_soundex (-S) */
    FALSE,                /* suppress_duplicates (-D) */
    FALSE,                /* suppress_originals (-U) */
    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) */
    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 */
    FALSE,		  /* output_FEN_string */
    (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(GlobalState.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; argnum++){
        const char *argument = argv[argnum];
        if(argument[0] == '-'){
            switch(argument[1]){
                case SEVEN_TAG_ROSTER_ARGUMENT:
		    process_argument(argument[1],"");
                    break;
                case TAG_ROSTER_ARGUMENT:
		    process_argument(argument[1],&argument[2]);
                    break;
                case FILE_OF_ARGUMENTS_ARGUMENT:
                   {  /* We expect a file name containing arguments. */
                      const char *filename = &(argument[2]);
		      process_argument(argument[1],filename);
                    }
                    break;
                case APPEND_TO_OUTPUT_FILE_ARGUMENT:
                    { /* We expect an output file to which to append. */
                      const char *filename = &(argument[2]);
		      process_argument(argument[1],filename);
		    }
                    break;
                case BOUNDS_ARGUMENT:
                    { const char *bound = &(argument[2]);
		      process_argument(argument[1],bound);
                  }
                  break;
                case CHECK_FILE_ARGUMENT:
                    { /* We expect a list of files as the game sources.
                       * These files are CHECKFILEs, to act as checks
                       * for duplicates in any NORMALFILEs.
                       */
                      const char *filename = &(argument[2]);
		      process_argument(argument[1],filename);
                    }
                    break;
                case DONT_KEEP_COMMENTS_ARGUMENT:
		    process_argument(argument[1],"");
                    break;
                case DUPLICATES_FILE_ARGUMENT:
                    { /* We expect an output file for duplicate games. */
                      const char *filename = &(argument[2]);
		      process_argument(argument[1],filename);
                    }
                    break;
                case DONT_KEEP_DUPLICATES_ARGUMENT:
		    process_argument(argument[1],"");
                    break;
                case USE_ECO_FILE_ARGUMENT:
                    { /* A filename of ECO lines is optional. */
                      const char *filename = &(argument[2]);
		      process_argument(argument[1],filename);
                    }
                    break;
                case ECO_OUTPUT_LEVEL_ARGUMENT:
                    /* Set an ECO output level. */
		    process_argument(argument[1],&argument[2]);
                    break;
                case FILE_OF_FILES_ARGUMENT:
                    { /* We expect a list of files as the game sources. */
                      const char *filename = &(argument[2]);
		      process_argument(argument[1],filename);
                    }
                    break;
		case OUTPUT_FEN_STRING_ARGUMENT:
		    process_argument(argument[1],"");
		    break;
                case HELP_ARGUMENT:
		    process_argument(argument[1],&argument[2]);
                    break;
                case ALTERNATIVE_HELP_ARGUMENT:
		    process_argument(HELP_ARGUMENT,"");
                    break;
                case WRITE_TO_LOG_FILE_ARGUMENT:
		    /* Create new log file. */
                case APPEND_TO_LOG_FILE_ARGUMENT:
		    /* Append to log file. */
		    process_argument(argument[1],&argument[2]);
                    break;
                case NON_MATCHING_GAMES_ARGUMENT:
                    { /* We expect an output file for unmatched games. */
                      const char *filename = &(argument[2]);
		      process_argument(argument[1],filename);
                    }
                    break;
                case DONT_KEEP_NAGS_ARGUMENT:
		    process_argument(argument[1],"");
                    break;
                case WRITE_TO_OUTPUT_FILE_ARGUMENT:
                    { /* We expect an output file to write to. */
                      const char *filename = &(argument[2]);
		      process_argument(argument[1],filename);
                    }
                    break;
                case DONT_MATCH_PERMUTATIONS_ARGUMENT:
		    process_argument(argument[1],"");
                    break;
                case CHECK_ONLY_ARGUMENT:
		    process_argument(argument[1],"");
                    break;
                case KEEP_SILENT_ARGUMENT:
		    process_argument(argument[1],"");
                    break;
                case USE_SOUNDEX_ARGUMENT:
                    /* Use soundex matches for player tags. */
		    process_argument(argument[1],"");
                    break;
                case TAG_EXTRACTION_ARGUMENT:
                    /* A single tag extraction criterion. */
		    process_argument(argument[1],&argument[2]);
                    break;
                case SUPPRESS_ORIGINALS_ARGUMENT:
		    process_argument(argument[1],"");
                    break;
                case DONT_KEEP_VARIATIONS_ARGUMENT:
		    process_argument(argument[1],"");
                    break;
                case LINE_WIDTH_ARGUMENT:
		    process_argument(argument[1],&argument[2]);
                    break;
                case OUTPUT_FORMAT_ARGUMENT:
		    process_argument(argument[1],&argument[2]);
                    break;
                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. */
			      if((argnum+1) < argc){
				  ++argnum;
				  filename = argv[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{
			    process_argument(argument_letter,filename);
                        }
                    }
                    break;
                case USE_VIRTUAL_HASH_TABLE_ARGUMENT:
		    process_argument(argument[1],"");
                    break;
                case GAMES_PER_FILE_ARGUMENT:
		    process_argument(argument[1],&argument[2]);
                    break;
                default:
                    fprintf(GlobalState.logfile,
			    "Unknown flag %s. Use -%c for usage details.\n",
			    argument,HELP_ARGUMENT);
                    break;
            }
        }
        else{
            /* Should be a file name containing games. */
            add_filename_to_source_list(argument,NORMALFILE);
        }
    }
    /* 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);
            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;
}