]> git.saurik.com Git - bison.git/blame - src/getargs.c
Fix typo: "&" should be "&&". Problem reported
[bison.git] / src / getargs.c
CommitLineData
08721544
PE
1/* Parse command line arguments for Bison.
2
e2a21b6f
PE
3 Copyright (C) 1984, 1986, 1989, 1992, 2000, 2001, 2002, 2003, 2004,
4 2005, 2006 Free Software Foundation, Inc.
3d8fc6ca 5
9f306f2a 6 This file is part of Bison, the GNU Compiler Compiler.
3d8fc6ca 7
9f306f2a
AD
8 Bison is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
3d8fc6ca 12
9f306f2a
AD
13 Bison is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
3d8fc6ca 17
9f306f2a
AD
18 You should have received a copy of the GNU General Public License
19 along with Bison; see the file COPYING. If not, write to the Free
0fb669f9
PE
20 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
3d8fc6ca 22
2cec9080 23#include <config.h>
3d8fc6ca 24#include "system.h"
7891a7c4 25#include "revision.h"
d38a11a6
PE
26
27#include <argmatch.h>
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;
d0829076
PE
51bool locations_flag;
52bool no_lines_flag;
53bool no_parser_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;
61bool pure_parser = false;
62
4e83ea15
AD
63int report_flag = report_none;
64int trace_flag = trace_none;
7b42569e 65int warnings_flag = warnings_none;
4e83ea15 66
b0ce6046 67const char *skeleton = NULL;
f6bd5427 68const char *include = NULL;
b0ce6046 69
cbd8ffc5 70extern char *program_name;
3d8fc6ca 71
273a74fa 72
7b42569e
AD
73/** Decode an option's set of keys.
74 *
75 * \param option option being decoded.
76 * \paran keys array of valid subarguments.
77 * \param values array of corresponding (int) values.
78 * \param flag the flags to update
79 * \param args colon separated list of effective subarguments to decode.
80 * If 0, then activate all the flags.
81 *
82 * The special value 0 resets the flags to 0.
83 */
80ac75bc
PE
84static void
85flags_argmatch (const char *option,
7b42569e
AD
86 const char * const keys[], const int values[],
87 int *flags, char *args)
88{
89 if (args)
90 {
91 args = strtok (args, ",");
92 do
93 {
94 int value = XARGMATCH (option, args, keys, values);
95 if (value == 0)
96 *flags = 0;
97 else
98 *flags |= value;
99 }
100 while ((args = strtok (NULL, ",")));
101 }
102 else
103 *flags = ~0;
104}
105
80ac75bc 106/** Decode a set of sub arguments.
7b42569e
AD
107 *
108 * \param FlagName the flag familly to update.
109 * \param args the effective sub arguments to decode.
110 *
111 * \arg FlagName_args the list of keys.
112 * \arg FlagName_types the list of values.
113 * \arg FlagName_flag the flag to update.
114 */
115#define FLAGS_ARGMATCH(FlagName, Args) \
116 flags_argmatch ("--" #FlagName, FlagName ## _args, FlagName ## _types, \
117 &FlagName ## _flag, Args)
118
119
b8a41559
AD
120/*----------------------.
121| --report's handling. |
122`----------------------*/
123
124static const char * const report_args[] =
125{
126 /* In a series of synonyms, present the most meaningful first, so
127 that argmatch_valid be more readable. */
128 "none",
129 "state", "states",
130 "itemset", "itemsets",
131 "lookahead", "lookaheads", "look-ahead",
132 "solved",
133 "all",
134 0
135};
136
137static const int report_types[] =
138{
139 report_none,
140 report_states, report_states,
141 report_states | report_itemsets, report_states | report_itemsets,
142 report_states | report_lookahead_tokens,
143 report_states | report_lookahead_tokens,
144 report_states | report_lookahead_tokens,
145 report_states | report_solved_conflicts,
146 report_all
147};
148
149ARGMATCH_VERIFY (report_args, report_types);
150
b8a41559 151
273a74fa
AD
152/*---------------------.
153| --trace's handling. |
154`---------------------*/
155
156static const char * const trace_args[] =
157{
158 /* In a series of synonyms, present the most meaningful first, so
159 that argmatch_valid be more readable. */
b8a41559 160 "none - no traces",
c5e3e510
AD
161 "scan - grammar scanner traces",
162 "parse - grammar parser traces",
b8a41559 163 "automaton - construction of the automaton",
273a74fa 164 "bitsets - use of bitsets",
b8a41559 165 "grammar - reading, reducing the grammar",
1509d42f 166 "resource - memory consumption (where available)",
273a74fa 167 "sets - grammar sets: firsts, nullable etc.",
327afc7c
AD
168 "tools - m4 invocation",
169 "m4 - m4 traces",
c5e3e510
AD
170 "skeleton - skeleton postprocessing",
171 "time - time consumption",
273a74fa
AD
172 "all - all of the above",
173 0
174};
175
176static const int trace_types[] =
177{
178 trace_none,
473d0a75
AD
179 trace_scan,
180 trace_parse,
273a74fa
AD
181 trace_automaton,
182 trace_bitsets,
183 trace_grammar,
184 trace_resource,
185 trace_sets,
186 trace_tools,
327afc7c 187 trace_m4,
c5e3e510
AD
188 trace_skeleton,
189 trace_time,
273a74fa
AD
190 trace_all
191};
192
8a6f72f3 193ARGMATCH_VERIFY (trace_args, trace_types);
273a74fa 194
7b42569e
AD
195
196/*------------------------.
197| --warnings's handling. |
198`------------------------*/
199
200static const char * const warnings_args[] =
273a74fa 201{
7b42569e
AD
202 /* In a series of synonyms, present the most meaningful first, so
203 that argmatch_valid be more readable. */
204 "none - no warnings",
205 "error - warnings are errors",
206 "yacc - incompatibilities with POSIX YACC",
207 "all - all of the above",
208 0
209};
210
211static const int warnings_types[] =
212{
213 warnings_none,
214 warnings_error,
215 warnings_yacc,
216 warnings_all
217};
218
219ARGMATCH_VERIFY (warnings_args, warnings_types);
273a74fa
AD
220
221
0e575721
AD
222/*-------------------------------------------.
223| Display the help message and exit STATUS. |
224`-------------------------------------------*/
e79137ac 225
0df27e8b
PE
226static void usage (int) ATTRIBUTE_NORETURN;
227
4a120d45 228static void
0e575721 229usage (int status)
cbd8ffc5 230{
0e575721
AD
231 if (status != 0)
232 fprintf (stderr, _("Try `%s --help' for more information.\n"),
233 program_name);
234 else
235 {
236 /* Some efforts were made to ease the translators' task, please
237 continue. */
238 fputs (_("\
184e42f0 239GNU bison generates LALR(1) and GLR parsers.\n"), stdout);
0e575721 240 putc ('\n', stdout);
9f306f2a 241
0e575721 242 fprintf (stdout, _("\
9f306f2a 243Usage: %s [OPTION]... FILE\n"), program_name);
0e575721 244 putc ('\n', stdout);
9f306f2a 245
0e575721 246 fputs (_("\
9f306f2a
AD
247If a long option shows an argument as mandatory, then it is mandatory\n\
248for the equivalent short option also. Similarly for optional arguments.\n"),
0e575721
AD
249 stdout);
250 putc ('\n', stdout);
9f306f2a 251
0e575721 252 fputs (_("\
9f306f2a 253Operation modes:\n\
f7ab6a50
PE
254 -h, --help display this help and exit\n\
255 -V, --version output version information and exit\n\
256 --print-localedir output directory containing locale-dependent data\n\
b931235e 257 -y, --yacc emulate POSIX Yacc\n"), stdout);
0e575721 258 putc ('\n', stdout);
9f306f2a 259
0e575721 260 fputs (_("\
9f306f2a 261Parser:\n\
cd5bd6ac 262 -S, --skeleton=FILE specify the skeleton to use\n\
9f306f2a 263 -t, --debug instrument the parser for debugging\n\
89cab50d 264 --locations enable locations computation\n\
9f306f2a
AD
265 -p, --name-prefix=PREFIX prepend PREFIX to the external symbols\n\
266 -l, --no-lines don't generate `#line' directives\n\
267 -n, --no-parser generate the tables only\n\
9f306f2a 268 -k, --token-table include a table of token names\n\
0e575721
AD
269"), stdout);
270 putc ('\n', stdout);
9f306f2a 271
0e575721 272 fputs (_("\
9f306f2a
AD
273Output:\n\
274 -d, --defines also produce a header file\n\
ec3bc396
AD
275 -r, --report=THINGS also produce details on the automaton\n\
276 -v, --verbose same as `--report=state'\n\
9f306f2a 277 -b, --file-prefix=PREFIX specify a PREFIX for output files\n\
951366c1
AD
278 -o, --output=FILE leave output to FILE\n\
279 -g, --graph also produce a VCG description of the automaton\n\
0e575721
AD
280"), stdout);
281 putc ('\n', stdout);
86eff183 282
0e575721 283 fputs (_("\
ec3bc396
AD
284THINGS is a list of comma separated words that can include:\n\
285 `state' describe the states\n\
286 `itemset' complete the core item sets with their closure\n\
742e4900 287 `lookahead' explicitly associate lookahead tokens to items\n\
b408954b 288 `solved' describe shift/reduce conflicts solving\n\
ec3bc396
AD
289 `all' include all the above information\n\
290 `none' disable the report\n\
0e575721
AD
291"), stdout);
292 putc ('\n', stdout);
9f306f2a 293
0e575721 294 fputs (_("\
7b42569e 295Report bugs to <" PACKAGE_BUGREPORT ">.\n"), stdout);
0e575721
AD
296 }
297
298 exit (status);
cbd8ffc5
DM
299}
300
e79137ac
AD
301
302/*------------------------------.
303| Display the version message. |
304`------------------------------*/
305
306static void
0e575721 307version (void)
e79137ac
AD
308{
309 /* Some efforts were made to ease the translators' task, please
310 continue. */
0e575721
AD
311 printf (_("bison (GNU Bison) %s"), VERSION);
312 putc ('\n', stdout);
7891a7c4 313 printf ("%s", revision);
0e575721
AD
314 fputs (_("Written by Robert Corbett and Richard Stallman.\n"), stdout);
315 putc ('\n', stdout);
e79137ac 316
0e575721 317 fprintf (stdout,
e2a21b6f 318 _("Copyright (C) %d Free Software Foundation, Inc.\n"), 2006);
e79137ac
AD
319
320 fputs (_("\
321This is free software; see the source for copying conditions. There is NO\n\
322warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
323"),
0e575721 324 stdout);
e79137ac
AD
325}
326
327
328/*----------------------.
329| Process the options. |
330`----------------------*/
331
e2aaf4c4 332/* Shorts options. */
7b42569e 333static char const short_options[] = "yvegdhr:ltknVo:b:p:S:T::W";
e2aaf4c4 334
d0829076
PE
335/* Values for long options that do not have single-letter equivalents. */
336enum
337{
f7ab6a50
PE
338 LOCATIONS_OPTION = CHAR_MAX + 1,
339 PRINT_LOCALEDIR_OPTION
d0829076
PE
340};
341
e2aaf4c4
AD
342static struct option const long_options[] =
343{
344 /* Operation modes. */
7b42569e
AD
345 { "help", no_argument, 0, 'h' },
346 { "version", no_argument, 0, 'V' },
347 { "print-localedir", no_argument, 0, PRINT_LOCALEDIR_OPTION },
348 { "warnings", optional_argument, 0, 'W' },
e2aaf4c4
AD
349
350 /* Parser. */
351 { "name-prefix", required_argument, 0, 'p' },
352 { "include", required_argument, 0, 'I' },
353
354 /* Output. */
355 { "file-prefix", required_argument, 0, 'b' },
356 { "output", required_argument, 0, 'o' },
357 { "output-file", required_argument, 0, 'o' },
358 { "graph", optional_argument, 0, 'g' },
359 { "report", required_argument, 0, 'r' },
360 { "verbose", no_argument, 0, 'v' },
361
362 /* Hidden. */
273a74fa 363 { "trace", optional_argument, 0, 'T' },
e2aaf4c4 364
e2aaf4c4
AD
365 /* Output. */
366 { "defines", optional_argument, 0, 'd' },
367
368 /* Operation modes. */
369 { "fixed-output-files", no_argument, 0, 'y' },
370 { "yacc", no_argument, 0, 'y' },
371
372 /* Parser. */
373 { "debug", no_argument, 0, 't' },
d0829076 374 { "locations", no_argument, 0, LOCATIONS_OPTION },
e2aaf4c4
AD
375 { "no-lines", no_argument, 0, 'l' },
376 { "no-parser", no_argument, 0, 'n' },
377 { "raw", no_argument, 0, 0 },
378 { "skeleton", required_argument, 0, 'S' },
379 { "token-table", no_argument, 0, 'k' },
380
381 {0, 0, 0, 0}
382};
383
ae404801
AD
384/* Under DOS, there is no difference on the case. This can be
385 troublesome when looking for `.tab' etc. */
386#ifdef MSDOS
387# define AS_FILE_NAME(File) (strlwr (File), (File))
388#else
389# define AS_FILE_NAME(File) (File)
390#endif
391
3d8fc6ca 392void
d2729d44 393getargs (int argc, char *argv[])
3d8fc6ca 394{
1916f98e 395 int c;
3d8fc6ca 396
08721544
PE
397 while ((c = getopt_long (argc, argv, short_options, long_options, NULL))
398 != -1)
cd5bd6ac
AD
399 switch (c)
400 {
401 case 0:
402 /* Certain long options cause getopt_long to return 0. */
403 break;
404
7b42569e
AD
405 case 'b':
406 spec_file_prefix = AS_FILE_NAME (optarg);
cd5bd6ac
AD
407 break;
408
22c2cbc0 409 case 'g':
342b8b6e 410 /* Here, the -g and --graph=FILE options are differentiated. */
d0829076 411 graph_flag = true;
fcbfa6b0
PE
412 if (optarg)
413 spec_graph_file = AS_FILE_NAME (optarg);
22c2cbc0
AD
414 break;
415
7b42569e
AD
416 case 'h':
417 usage (EXIT_SUCCESS);
cd5bd6ac
AD
418
419 case 'S':
ae404801 420 skeleton = AS_FILE_NAME (optarg);
cd5bd6ac
AD
421 break;
422
f6bd5427 423 case 'I':
ae404801 424 include = AS_FILE_NAME (optarg);
f6bd5427
MA
425 break;
426
cd5bd6ac 427 case 'd':
342b8b6e 428 /* Here, the -d and --defines options are differentiated. */
d0829076 429 defines_flag = true;
ae404801
AD
430 if (optarg)
431 spec_defines_file = AS_FILE_NAME (optarg);
cd5bd6ac
AD
432 break;
433
7b42569e
AD
434 case 'k':
435 token_table_flag = true;
436 break;
437
cd5bd6ac 438 case 'l':
d0829076
PE
439 no_lines_flag = true;
440 break;
441
7b42569e
AD
442 case 'n':
443 no_parser_flag = true;
cd5bd6ac
AD
444 break;
445
7b42569e
AD
446 case 'o':
447 spec_outfile = AS_FILE_NAME (optarg);
cd5bd6ac
AD
448 break;
449
7b42569e
AD
450 case 'p':
451 spec_name_prefix = optarg;
452 break;
453
454 case 'r':
455 FLAGS_ARGMATCH (report, optarg);
456 break;
457
458 case 'T':
459 FLAGS_ARGMATCH (trace, optarg);
cd5bd6ac
AD
460 break;
461
462 case 't':
d0829076 463 debug_flag = true;
cd5bd6ac
AD
464 break;
465
7b42569e
AD
466 case 'V':
467 version ();
468 exit (EXIT_SUCCESS);
cd5bd6ac 469
7b42569e
AD
470 case 'v':
471 report_flag |= report_states;
cd5bd6ac
AD
472 break;
473
7b42569e
AD
474 case 'y':
475 yacc_flag = true;
cd5bd6ac
AD
476 break;
477
7b42569e
AD
478 case 'W':
479 FLAGS_ARGMATCH (warnings, optarg);
ec3bc396
AD
480 break;
481
7b42569e
AD
482 case LOCATIONS_OPTION:
483 locations_flag = true;
273a74fa
AD
484 break;
485
7b42569e
AD
486 case PRINT_LOCALEDIR_OPTION:
487 printf ("%s\n", LOCALEDIR);
488 exit (EXIT_SUCCESS);
489
cd5bd6ac 490 default:
0df27e8b 491 usage (EXIT_FAILURE);
cd5bd6ac 492 }
3d8fc6ca 493
a4b6efd4 494 if (argc - optind != 1)
3d8fc6ca 495 {
a4b6efd4
PE
496 if (argc - optind < 1)
497 error (0, 0, _("missing operand after `%s'"), argv[argc - 1]);
498 else
499 error (0, 0, _("extra operand `%s'"), argv[optind + 1]);
0e575721 500 usage (EXIT_FAILURE);
3d8fc6ca 501 }
3d8fc6ca 502
d38a11a6 503 current_file = grammar_file = uniqstr_new (argv[optind]);
3d8fc6ca 504}