]> git.saurik.com Git - bison.git/blame - src/getargs.c
bench: More use of the verbosity level.
[bison.git] / src / getargs.c
CommitLineData
08721544
PE
1/* Parse command line arguments for Bison.
2
e2a21b6f 3 Copyright (C) 1984, 1986, 1989, 1992, 2000, 2001, 2002, 2003, 2004,
59da312b 4 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
3d8fc6ca 5
9f306f2a 6 This file is part of Bison, the GNU Compiler Compiler.
3d8fc6ca 7
f16b0819
PE
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
3d8fc6ca 12
f16b0819
PE
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
3d8fc6ca 17
9f306f2a 18 You should have received a copy of the GNU General Public License
f16b0819 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
3d8fc6ca 20
2cec9080 21#include <config.h>
3d8fc6ca 22#include "system.h"
d4bd2295 23#include "output.h"
d38a11a6
PE
24
25#include <argmatch.h>
d7e0a1a7 26#include <c-strcase.h>
3b2942e6 27#include <configmake.h>
d38a11a6 28#include <error.h>
f47dbf6b
PE
29
30/* Hack to get <getopt.h> to declare getopt with a prototype. */
31#if lint && ! defined __GNU_LIBRARY__
32# define __GNU_LIBRARY__
33# define HACK_FOR___GNU_LIBRARY___PROTOTYPE 1
34#endif
35
d38a11a6
PE
36#include <getopt.h>
37
f47dbf6b
PE
38#ifdef HACK_FOR___GNU_LIBRARY___PROTOTYPE
39# undef __GNU_LIBRARY__
40# undef HACK_FOR___GNU_LIBRARY___PROTOTYPE
41#endif
42
b0ce6046 43#include "complain.h"
ec3bc396 44#include "files.h"
d38a11a6
PE
45#include "getargs.h"
46#include "uniqstr.h"
3d8fc6ca 47
d0829076
PE
48bool debug_flag;
49bool defines_flag;
4e83ea15 50bool graph_flag;
41d7a5f2 51bool xml_flag;
d0829076
PE
52bool locations_flag;
53bool no_lines_flag;
d0829076
PE
54bool token_table_flag;
55bool yacc_flag; /* for -y */
4e83ea15
AD
56
57bool error_verbose = false;
d2729d44 58
916708d5
AD
59bool nondeterministic_parser = false;
60bool glr_parser = false;
916708d5 61
4e83ea15
AD
62int report_flag = report_none;
63int trace_flag = trace_none;
7b42569e 64int warnings_flag = warnings_none;
4e83ea15 65
0e021770
PE
66static struct bison_language const valid_languages[] = {
67 { "c", "c-skel.m4", ".c", ".h", true },
68 { "c++", "c++-skel.m4", ".cc", ".hh", true },
8405b70c 69 { "java", "java-skel.m4", ".java", ".java", false },
0e021770
PE
70 { "", "", "", "", false }
71};
72
73static int skeleton_prio = 2;
b0ce6046 74const char *skeleton = NULL;
0e021770
PE
75static int language_prio = 2;
76struct bison_language const *language = &valid_languages[0];
f6bd5427 77const char *include = NULL;
b0ce6046 78
0e021770 79char *program_name;
3d8fc6ca 80
273a74fa 81
7b42569e
AD
82/** Decode an option's set of keys.
83 *
84 * \param option option being decoded.
07c39ae9 85 * \param keys array of valid subarguments.
7b42569e 86 * \param values array of corresponding (int) values.
07c39ae9 87 * \param flags the flags to update
7b42569e
AD
88 * \param args colon separated list of effective subarguments to decode.
89 * If 0, then activate all the flags.
90 *
91 * The special value 0 resets the flags to 0.
92 */
80ac75bc
PE
93static void
94flags_argmatch (const char *option,
7b42569e
AD
95 const char * const keys[], const int values[],
96 int *flags, char *args)
97{
98 if (args)
99 {
100 args = strtok (args, ",");
ba7560e2 101 while (args)
7b42569e
AD
102 {
103 int value = XARGMATCH (option, args, keys, values);
104 if (value == 0)
105 *flags = 0;
106 else
107 *flags |= value;
eb095650 108 args = strtok (NULL, ",");
7b42569e 109 }
7b42569e
AD
110 }
111 else
112 *flags = ~0;
113}
114
80ac75bc 115/** Decode a set of sub arguments.
7b42569e
AD
116 *
117 * \param FlagName the flag familly to update.
07c39ae9 118 * \param Args the effective sub arguments to decode.
7b42569e
AD
119 *
120 * \arg FlagName_args the list of keys.
121 * \arg FlagName_types the list of values.
122 * \arg FlagName_flag the flag to update.
123 */
124#define FLAGS_ARGMATCH(FlagName, Args) \
125 flags_argmatch ("--" #FlagName, FlagName ## _args, FlagName ## _types, \
126 &FlagName ## _flag, Args)
127
128
b8a41559
AD
129/*----------------------.
130| --report's handling. |
131`----------------------*/
132
133static const char * const report_args[] =
134{
135 /* In a series of synonyms, present the most meaningful first, so
136 that argmatch_valid be more readable. */
137 "none",
138 "state", "states",
139 "itemset", "itemsets",
140 "lookahead", "lookaheads", "look-ahead",
141 "solved",
142 "all",
143 0
144};
145
146static const int report_types[] =
147{
148 report_none,
149 report_states, report_states,
150 report_states | report_itemsets, report_states | report_itemsets,
151 report_states | report_lookahead_tokens,
152 report_states | report_lookahead_tokens,
153 report_states | report_lookahead_tokens,
154 report_states | report_solved_conflicts,
155 report_all
156};
157
158ARGMATCH_VERIFY (report_args, report_types);
159
b8a41559 160
273a74fa
AD
161/*---------------------.
162| --trace's handling. |
163`---------------------*/
164
165static const char * const trace_args[] =
166{
167 /* In a series of synonyms, present the most meaningful first, so
168 that argmatch_valid be more readable. */
b8a41559 169 "none - no traces",
c5e3e510
AD
170 "scan - grammar scanner traces",
171 "parse - grammar parser traces",
b8a41559 172 "automaton - construction of the automaton",
273a74fa 173 "bitsets - use of bitsets",
b8a41559 174 "grammar - reading, reducing the grammar",
1509d42f 175 "resource - memory consumption (where available)",
273a74fa 176 "sets - grammar sets: firsts, nullable etc.",
327afc7c
AD
177 "tools - m4 invocation",
178 "m4 - m4 traces",
c5e3e510
AD
179 "skeleton - skeleton postprocessing",
180 "time - time consumption",
273a74fa
AD
181 "all - all of the above",
182 0
183};
184
185static const int trace_types[] =
186{
187 trace_none,
473d0a75
AD
188 trace_scan,
189 trace_parse,
273a74fa
AD
190 trace_automaton,
191 trace_bitsets,
192 trace_grammar,
193 trace_resource,
194 trace_sets,
195 trace_tools,
327afc7c 196 trace_m4,
c5e3e510
AD
197 trace_skeleton,
198 trace_time,
273a74fa
AD
199 trace_all
200};
201
8a6f72f3 202ARGMATCH_VERIFY (trace_args, trace_types);
273a74fa 203
7b42569e
AD
204
205/*------------------------.
206| --warnings's handling. |
207`------------------------*/
208
209static const char * const warnings_args[] =
273a74fa 210{
7b42569e
AD
211 /* In a series of synonyms, present the most meaningful first, so
212 that argmatch_valid be more readable. */
17bd8a73
JD
213 "none - no warnings",
214 "midrule-values - unset or unused midrule values",
215 "yacc - incompatibilities with POSIX YACC",
216 "all - all of the above",
217 "error - warnings are errors",
7b42569e
AD
218 0
219};
220
221static const int warnings_types[] =
222{
223 warnings_none,
17bd8a73 224 warnings_midrule_values,
7b42569e 225 warnings_yacc,
89eb3c76
JD
226 warnings_all,
227 warnings_error
7b42569e
AD
228};
229
230ARGMATCH_VERIFY (warnings_args, warnings_types);
273a74fa
AD
231
232
0e575721
AD
233/*-------------------------------------------.
234| Display the help message and exit STATUS. |
235`-------------------------------------------*/
e79137ac 236
0df27e8b
PE
237static void usage (int) ATTRIBUTE_NORETURN;
238
4a120d45 239static void
0e575721 240usage (int status)
cbd8ffc5 241{
0e575721
AD
242 if (status != 0)
243 fprintf (stderr, _("Try `%s --help' for more information.\n"),
244 program_name);
245 else
246 {
a92be413 247 printf (_("Usage: %s [OPTION]... FILE\n"), program_name);
0e575721 248 fputs (_("\
a92be413
PE
249Generate LALR(1) and GLR parsers.\n\
250\n\
251"), stdout);
9f306f2a 252
0e575721 253 fputs (_("\
a92be413 254Mandatory arguments to long options are mandatory for short options too.\n\
8e55b3aa
JD
255"), stdout);
256 fputs (_("\
257The same is true for optional arguments.\n\
a92be413 258"), stdout);
9f306f2a 259
0e575721 260 fputs (_("\
a92be413 261\n\
9f306f2a 262Operation modes:\n\
f7ab6a50
PE
263 -h, --help display this help and exit\n\
264 -V, --version output version information and exit\n\
265 --print-localedir output directory containing locale-dependent data\n\
d4bd2295 266 --print-datadir output directory containing skeletons and XSLT\n\
a92be413 267 -y, --yacc emulate POSIX Yacc\n\
6aeb9c57 268 -W, --warnings=[CATEGORY] report the warnings falling in CATEGORY\n\
a92be413
PE
269\n\
270"), stdout);
9f306f2a 271
0e575721 272 fputs (_("\
9f306f2a 273Parser:\n\
59da312b 274 -L, --language=LANGUAGE specify the output programming language\n\
ed4d67dc 275 (this is an experimental feature)\n\
cd5bd6ac 276 -S, --skeleton=FILE specify the skeleton to use\n\
9f306f2a 277 -t, --debug instrument the parser for debugging\n\
89cab50d 278 --locations enable locations computation\n\
9f306f2a
AD
279 -p, --name-prefix=PREFIX prepend PREFIX to the external symbols\n\
280 -l, --no-lines don't generate `#line' directives\n\
9f306f2a 281 -k, --token-table include a table of token names\n\
a92be413 282\n\
0e575721 283"), stdout);
9f306f2a 284
8e55b3aa
JD
285 /* Keep -d and --defines separate so that ../build-aux/cross-options.pl
286 * won't assume that -d also takes an argument. */
0e575721 287 fputs (_("\
9f306f2a 288Output:\n\
8e55b3aa
JD
289 --defines[=FILE] also produce a header file\n\
290 -d likewise but cannot specify FILE (for POSIX Yacc)\n\
ec3bc396 291 -r, --report=THINGS also produce details on the automaton\n\
1bb2bd75 292 --report-file=FILE write report to FILE\n\
ec3bc396 293 -v, --verbose same as `--report=state'\n\
9f306f2a 294 -b, --file-prefix=PREFIX specify a PREFIX for output files\n\
951366c1 295 -o, --output=FILE leave output to FILE\n\
6aeb9c57
AD
296 -g, --graph[=FILE] also output a graph of the automaton\n\
297 -x, --xml[=FILE] also output an XML report of the automaton\n\
59da312b 298 (the XML schema is experimental)\n\
a92be413 299\n\
0e575721 300"), stdout);
86eff183 301
6aeb9c57
AD
302 fputs (_("\
303Warning categories include:\n\
304 `midrule-values' unset or unused midrule values\n\
305 `yacc' incompatibilities with POSIX YACC\n\
306 `all' all the warnings\n\
307 `no-CATEGORY' turn off warnings in CATEGORY\n\
308 `none' turn off all the warnings\n\
309 `error' treat warnings as errors\n\
66f0441d 310\n\
6aeb9c57
AD
311"), stdout);
312
0e575721 313 fputs (_("\
ec3bc396
AD
314THINGS is a list of comma separated words that can include:\n\
315 `state' describe the states\n\
316 `itemset' complete the core item sets with their closure\n\
742e4900 317 `lookahead' explicitly associate lookahead tokens to items\n\
b408954b 318 `solved' describe shift/reduce conflicts solving\n\
ec3bc396
AD
319 `all' include all the above information\n\
320 `none' disable the report\n\
0e575721 321"), stdout);
9f306f2a 322
a92be413 323 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
0e575721
AD
324 }
325
326 exit (status);
cbd8ffc5
DM
327}
328
e79137ac
AD
329
330/*------------------------------.
331| Display the version message. |
332`------------------------------*/
333
334static void
0e575721 335version (void)
e79137ac
AD
336{
337 /* Some efforts were made to ease the translators' task, please
338 continue. */
0e575721
AD
339 printf (_("bison (GNU Bison) %s"), VERSION);
340 putc ('\n', stdout);
341 fputs (_("Written by Robert Corbett and Richard Stallman.\n"), stdout);
342 putc ('\n', stdout);
e79137ac 343
0e575721 344 fprintf (stdout,
a005a9c4
AD
345 _("Copyright (C) %d Free Software Foundation, Inc.\n"),
346 PACKAGE_COPYRIGHT_YEAR);
e79137ac
AD
347
348 fputs (_("\
349This is free software; see the source for copying conditions. There is NO\n\
350warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
351"),
0e575721 352 stdout);
e79137ac
AD
353}
354
355
0e021770
PE
356/*-------------------------------------.
357| --skeleton and --language handling. |
358`--------------------------------------*/
359
360void
361skeleton_arg (char const *arg, int prio, location const *loc)
362{
363 if (prio < skeleton_prio)
364 {
365 skeleton_prio = prio;
366 skeleton = arg;
367 }
368 else if (prio == skeleton_prio)
369 {
370 char const *msg =
371 _("multiple skeleton declarations are invalid");
372 if (loc)
373 complain_at (*loc, msg);
374 else
375 complain (msg);
376 }
377}
378
379void
380language_argmatch (char const *arg, int prio, location const *loc)
381{
382 char const *msg;
383
384 if (prio < language_prio)
385 {
386 int i;
387 for (i = 0; valid_languages[i].language[0]; i++)
d7e0a1a7 388 if (c_strcasecmp (arg, valid_languages[i].language) == 0)
0e021770
PE
389 {
390 language_prio = prio;
391 language = &valid_languages[i];
392 return;
393 }
394 msg = _("invalid language `%s'");
395 }
396 else if (language_prio == prio)
397 msg = _("multiple language declarations are invalid");
398 else
399 return;
400
401 if (loc)
402 complain_at (*loc, msg, arg);
403 else
404 complain (msg, arg);
405}
406
e79137ac
AD
407/*----------------------.
408| Process the options. |
409`----------------------*/
410
7020f1e9
AD
411/* Shorts options.
412 Should be computed from long_options. */
413static char const short_options[] =
414 "L:"
415 "S:"
416 "T::"
417 "V"
8e55b3aa 418 "W::"
7020f1e9
AD
419 "b:"
420 "d"
421 "e"
422 "g::"
423 "h"
424 "k"
425 "l"
426 "n"
427 "o:"
428 "p:"
429 "r:"
430 "t"
431 "v"
432 "x::"
433 "y"
434 ;
e2aaf4c4 435
d0829076
PE
436/* Values for long options that do not have single-letter equivalents. */
437enum
438{
f7ab6a50 439 LOCATIONS_OPTION = CHAR_MAX + 1,
d4bd2295 440 PRINT_LOCALEDIR_OPTION,
1bb2bd75
JD
441 PRINT_DATADIR_OPTION,
442 REPORT_FILE_OPTION
d0829076
PE
443};
444
e2aaf4c4
AD
445static struct option const long_options[] =
446{
447 /* Operation modes. */
7b42569e
AD
448 { "help", no_argument, 0, 'h' },
449 { "version", no_argument, 0, 'V' },
450 { "print-localedir", no_argument, 0, PRINT_LOCALEDIR_OPTION },
d4bd2295 451 { "print-datadir", no_argument, 0, PRINT_DATADIR_OPTION },
7b42569e 452 { "warnings", optional_argument, 0, 'W' },
e2aaf4c4
AD
453
454 /* Parser. */
455 { "name-prefix", required_argument, 0, 'p' },
456 { "include", required_argument, 0, 'I' },
457
458 /* Output. */
459 { "file-prefix", required_argument, 0, 'b' },
460 { "output", required_argument, 0, 'o' },
461 { "output-file", required_argument, 0, 'o' },
462 { "graph", optional_argument, 0, 'g' },
41d7a5f2 463 { "xml", optional_argument, 0, 'x' },
e2aaf4c4 464 { "report", required_argument, 0, 'r' },
1bb2bd75 465 { "report-file", required_argument, 0, REPORT_FILE_OPTION },
e2aaf4c4
AD
466 { "verbose", no_argument, 0, 'v' },
467
468 /* Hidden. */
273a74fa 469 { "trace", optional_argument, 0, 'T' },
e2aaf4c4 470
e2aaf4c4
AD
471 /* Output. */
472 { "defines", optional_argument, 0, 'd' },
473
474 /* Operation modes. */
475 { "fixed-output-files", no_argument, 0, 'y' },
476 { "yacc", no_argument, 0, 'y' },
477
478 /* Parser. */
479 { "debug", no_argument, 0, 't' },
d0829076 480 { "locations", no_argument, 0, LOCATIONS_OPTION },
e2aaf4c4 481 { "no-lines", no_argument, 0, 'l' },
e2aaf4c4
AD
482 { "raw", no_argument, 0, 0 },
483 { "skeleton", required_argument, 0, 'S' },
0e021770 484 { "language", required_argument, 0, 'L' },
e2aaf4c4
AD
485 { "token-table", no_argument, 0, 'k' },
486
487 {0, 0, 0, 0}
488};
489
ae404801
AD
490/* Under DOS, there is no difference on the case. This can be
491 troublesome when looking for `.tab' etc. */
492#ifdef MSDOS
493# define AS_FILE_NAME(File) (strlwr (File), (File))
494#else
495# define AS_FILE_NAME(File) (File)
496#endif
497
3d8fc6ca 498void
d2729d44 499getargs (int argc, char *argv[])
3d8fc6ca 500{
1916f98e 501 int c;
3d8fc6ca 502
08721544
PE
503 while ((c = getopt_long (argc, argv, short_options, long_options, NULL))
504 != -1)
cd5bd6ac
AD
505 switch (c)
506 {
507 case 0:
508 /* Certain long options cause getopt_long to return 0. */
509 break;
510
8e55b3aa
JD
511 case 'd':
512 /* Here, the -d and --defines options are differentiated. */
513 defines_flag = true;
fcbfa6b0 514 if (optarg)
8e55b3aa 515 spec_defines_file = xstrdup (AS_FILE_NAME (optarg));
22c2cbc0
AD
516 break;
517
8e55b3aa
JD
518 case 'I':
519 include = AS_FILE_NAME (optarg);
41d7a5f2
PE
520 break;
521
0e021770
PE
522 case 'L':
523 language_argmatch (optarg, 0, NULL);
524 break;
525
cd5bd6ac 526 case 'S':
0e021770 527 skeleton_arg (AS_FILE_NAME (optarg), 0, NULL);
cd5bd6ac
AD
528 break;
529
8e55b3aa
JD
530 case 'T':
531 FLAGS_ARGMATCH (trace, optarg);
f6bd5427
MA
532 break;
533
8e55b3aa
JD
534 case 'V':
535 version ();
536 exit (EXIT_SUCCESS);
537
538 case 'W':
ae404801 539 if (optarg)
8e55b3aa
JD
540 FLAGS_ARGMATCH (warnings, optarg);
541 else
542 warnings_flag |= warnings_all;
543 break;
544
545 case 'b':
546 spec_file_prefix = AS_FILE_NAME (optarg);
547 break;
548
549 case 'g':
550 graph_flag = true;
551 if (optarg)
552 spec_graph_file = xstrdup (AS_FILE_NAME (optarg));
cd5bd6ac
AD
553 break;
554
8e55b3aa
JD
555 case 'h':
556 usage (EXIT_SUCCESS);
557
7b42569e
AD
558 case 'k':
559 token_table_flag = true;
560 break;
561
cd5bd6ac 562 case 'l':
d0829076
PE
563 no_lines_flag = true;
564 break;
565
7b42569e
AD
566 case 'o':
567 spec_outfile = AS_FILE_NAME (optarg);
cd5bd6ac
AD
568 break;
569
7b42569e
AD
570 case 'p':
571 spec_name_prefix = optarg;
572 break;
573
574 case 'r':
575 FLAGS_ARGMATCH (report, optarg);
576 break;
577
cd5bd6ac 578 case 't':
d0829076 579 debug_flag = true;
cd5bd6ac
AD
580 break;
581
7b42569e
AD
582 case 'v':
583 report_flag |= report_states;
cd5bd6ac
AD
584 break;
585
8e55b3aa
JD
586 case 'x':
587 xml_flag = true;
588 if (optarg)
589 spec_xml_file = xstrdup (AS_FILE_NAME (optarg));
cd5bd6ac
AD
590 break;
591
8e55b3aa
JD
592 case 'y':
593 yacc_flag = true;
ec3bc396
AD
594 break;
595
7b42569e
AD
596 case LOCATIONS_OPTION:
597 locations_flag = true;
273a74fa
AD
598 break;
599
7b42569e
AD
600 case PRINT_LOCALEDIR_OPTION:
601 printf ("%s\n", LOCALEDIR);
602 exit (EXIT_SUCCESS);
603
d4bd2295
JD
604 case PRINT_DATADIR_OPTION:
605 printf ("%s\n", compute_pkgdatadir ());
606 exit (EXIT_SUCCESS);
607
8e55b3aa
JD
608 case REPORT_FILE_OPTION:
609 spec_verbose_file = xstrdup (AS_FILE_NAME (optarg));
610 break;
611
cd5bd6ac 612 default:
0df27e8b 613 usage (EXIT_FAILURE);
cd5bd6ac 614 }
3d8fc6ca 615
a4b6efd4 616 if (argc - optind != 1)
3d8fc6ca 617 {
a4b6efd4
PE
618 if (argc - optind < 1)
619 error (0, 0, _("missing operand after `%s'"), argv[argc - 1]);
620 else
621 error (0, 0, _("extra operand `%s'"), argv[optind + 1]);
0e575721 622 usage (EXIT_FAILURE);
3d8fc6ca 623 }
3d8fc6ca 624
d38a11a6 625 current_file = grammar_file = uniqstr_new (argv[optind]);
3d8fc6ca 626}