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