File: main.c

package info (click to toggle)
pgn-extract 19.04-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 2,492 kB
  • sloc: ansic: 13,372; makefile: 483; sh: 76; xml: 50
file content (433 lines) | stat: -rw-r--r-- 18,216 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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
 *  This file is part of pgn-extract: a Portable Game Notation (PGN) extractor.
 *  Copyright (C) 1994-2019 David J. Barnes
 *
 *  pgn-extract 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 3 of the License, or
 *  (at your option) any later version.
 *
 *  pgn-extract 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 pgn-extract. If not, see <http://www.gnu.org/licenses/>.
 *
 *  David J. Barnes may be contacted as d.j.barnes@kent.ac.uk
 *  https://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,              /* skipping_current_game */
    FALSE,              /* check_only (-r) */
    2,                  /* verbosity level (-s and --quiet) */
    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) */
    FALSE,              /* keep_broken_games (--keepbroken) */
    FALSE,              /* suppress_redundant_ep_info (--nofauxep) */
    FALSE,              /* json_format (--json) */
    FALSE,              /* check_for_repetition (--repetition) */
    FALSE,              /* check_for_fifty_move_rule (--fifty) */
    FALSE,              /* tag_match_anywhere (--tagsubstr) */
    FALSE,              /* match_underpromotion (--underpromotion) */
    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) */
    0,                  /* stability_threshold (--stable) */
    0,                  /* maximum_matches */
    0,                  /* drop_ply_number (--dropply) */
    FALSE,              /* output_FEN_string */
    FALSE,              /* add_FEN_comments (--fencomments) */
    FALSE,              /* add_hashcode_comments (--hashcomments) */
    FALSE,              /* add_position_match_comments (--markmatches) */
    FALSE,              /* output_plycount (--plycount) */
    FALSE,              /* output_total_plycount (--totalplycount) */
    FALSE,              /* add_hashcode_tag (--addhashcode) */
    FALSE,              /* fix_result_tags (--fixresulttags) */
    FALSE,              /* separate_comment_lines (--commentlines) */
    FALSE,              /* split_variants (--separatevariants) */
    FALSE,              /* reject_inconsistent_results (--nobadresults) */
    FALSE,              /* allow_null_moves (--allownullmoves) */
    FALSE,              /* allow_nested_comments (--nestedcomments) */
    FALSE,              /* add_match_tag (--addmatchtag) */
    FALSE,              /* add_matchlabel_tag (--addlabeltag) */
    FALSE,              /* only_output_wanted_tags (--xroster) */
    0,                  /* split_depth_limit */
    NORMALFILE,         /* current_file_type */
    SETUP_TAG_OK,       /* setup_status */
    "MATCH",            /* position_match_comment (--markpositionmatches) */
    (char *) NULL,      /* FEN_comment_pattern (-Fpattern) */
    (char *) NULL,      /* drop_comment_pattern (--dropbefore) */
    (char *) NULL,      /* current_input_file */
    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) */
    NULL,               /* matching_game_numbers */
    NULL,               /* next_game_number_to_output */
    NULL,               /* skip_game_numbers */
    NULL,               /* next_game_number_to_skip */
};

/* 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 Game_Header. */
    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 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 HELP_ARGUMENT:
                case OUTPUT_FORMAT_ARGUMENT:
                case USE_ECO_FILE_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 ECO_OUTPUT_LEVEL_ARGUMENT:
                case GAMES_PER_FILE_ARGUMENT:
                case LINE_WIDTH_ARGUMENT:
                case MOVE_BOUNDS_ARGUMENT:
                case PLY_BOUNDS_ARGUMENT:
                { /* We require an associated 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;

                case OUTPUT_FEN_STRING_ARGUMENT:
                    /* May be following by an optional argument immediately after
                     * the argument letter.
                     */
                    process_argument(argument[1], &argument[2]);
                    argnum++;
                    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:
                case ENDINGS_COLOURED_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;
                case HASHCODE_MATCH_ARGUMENT:
                    process_argument(argument[1], &argument[2]);
                    argnum++;
                    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++;
        }
    }

    /* Make some adjustments to other settings if JSON output is required. */
    if (GlobalState.json_format) {
        if (GlobalState.output_format != EPD &&
                GlobalState.output_format != CM &&
                GlobalState.ECO_level == DONT_DIVIDE) {
            GlobalState.keep_comments = FALSE;
            GlobalState.keep_variations = FALSE;
            GlobalState.keep_results = FALSE;
        }
        else {
            fprintf(GlobalState.logfile, "JSON output is not currently supported with -E, -Wepd or -Wcm\n");
            GlobalState.json_format = FALSE;
        }
    }

    /* 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 as the source of input. */
    if (!open_first_file()) {
        exit(1);
    }

    yyparse(GlobalState.current_file_type);

    /* @@@ I would prefer this to be somewhere else. */
    if (GlobalState.json_format &&
            !GlobalState.check_only &&
            GlobalState.num_games_matched > 0) {
        fputs("\n]\n", GlobalState.outputfile);
    }

    /* Remove any temporary files. */
    clear_duplicate_hash_table();
    if (GlobalState.verbosity > 1) {
        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;
}