]> git.saurik.com Git - bison.git/blob - src/getargs.c
* data/glr.c (b4_shared_declarations): Put start-header first,
[bison.git] / src / getargs.c
1 /* Parse command line arguments for Bison.
2
3 Copyright (C) 1984, 1986, 1989, 1992, 2000, 2001, 2002, 2003, 2004,
4 2005, 2006 Free Software Foundation, Inc.
5
6 This file is part of Bison, the GNU Compiler Compiler.
7
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.
12
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.
17
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
20 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
22
23 #include <config.h>
24 #include "system.h"
25 #include "revision.h"
26
27 #include <argmatch.h>
28 #include <error.h>
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
36 #include <getopt.h>
37
38 #ifdef HACK_FOR___GNU_LIBRARY___PROTOTYPE
39 # undef __GNU_LIBRARY__
40 # undef HACK_FOR___GNU_LIBRARY___PROTOTYPE
41 #endif
42
43 #include "complain.h"
44 #include "files.h"
45 #include "getargs.h"
46 #include "uniqstr.h"
47
48 bool debug_flag;
49 bool defines_flag;
50 bool graph_flag;
51 bool locations_flag;
52 bool no_lines_flag;
53 bool no_parser_flag;
54 bool token_table_flag;
55 bool yacc_flag; /* for -y */
56
57 bool error_verbose = false;
58
59 bool nondeterministic_parser = false;
60 bool glr_parser = false;
61 bool pure_parser = false;
62
63 int report_flag = report_none;
64 int trace_flag = trace_none;
65 int warnings_flag = warnings_none;
66
67 const char *skeleton = NULL;
68 const char *include = NULL;
69
70 extern char *program_name;
71
72
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 */
84 static void
85 flags_argmatch (const char *option,
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
106 /** Decode a set of sub arguments.
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
120 /*----------------------.
121 | --report's handling. |
122 `----------------------*/
123
124 static 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
137 static 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
149 ARGMATCH_VERIFY (report_args, report_types);
150
151
152 /*---------------------.
153 | --trace's handling. |
154 `---------------------*/
155
156 static 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. */
160 "none - no traces",
161 "scan - grammar scanner traces",
162 "parse - grammar parser traces",
163 "automaton - construction of the automaton",
164 "bitsets - use of bitsets",
165 "grammar - reading, reducing the grammar",
166 "resource - memory consumption (where available)",
167 "sets - grammar sets: firsts, nullable etc.",
168 "tools - m4 invocation",
169 "m4 - m4 traces",
170 "skeleton - skeleton postprocessing",
171 "time - time consumption",
172 "all - all of the above",
173 0
174 };
175
176 static const int trace_types[] =
177 {
178 trace_none,
179 trace_scan,
180 trace_parse,
181 trace_automaton,
182 trace_bitsets,
183 trace_grammar,
184 trace_resource,
185 trace_sets,
186 trace_tools,
187 trace_m4,
188 trace_skeleton,
189 trace_time,
190 trace_all
191 };
192
193 ARGMATCH_VERIFY (trace_args, trace_types);
194
195
196 /*------------------------.
197 | --warnings's handling. |
198 `------------------------*/
199
200 static const char * const warnings_args[] =
201 {
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
211 static const int warnings_types[] =
212 {
213 warnings_none,
214 warnings_error,
215 warnings_yacc,
216 warnings_all
217 };
218
219 ARGMATCH_VERIFY (warnings_args, warnings_types);
220
221
222 /*-------------------------------------------.
223 | Display the help message and exit STATUS. |
224 `-------------------------------------------*/
225
226 static void usage (int) ATTRIBUTE_NORETURN;
227
228 static void
229 usage (int status)
230 {
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 (_("\
239 GNU bison generates LALR(1) and GLR parsers.\n"), stdout);
240 putc ('\n', stdout);
241
242 fprintf (stdout, _("\
243 Usage: %s [OPTION]... FILE\n"), program_name);
244 putc ('\n', stdout);
245
246 fputs (_("\
247 If a long option shows an argument as mandatory, then it is mandatory\n\
248 for the equivalent short option also. Similarly for optional arguments.\n"),
249 stdout);
250 putc ('\n', stdout);
251
252 fputs (_("\
253 Operation modes:\n\
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\
257 -y, --yacc emulate POSIX Yacc\n"), stdout);
258 putc ('\n', stdout);
259
260 fputs (_("\
261 Parser:\n\
262 -S, --skeleton=FILE specify the skeleton to use\n\
263 -t, --debug instrument the parser for debugging\n\
264 --locations enable locations computation\n\
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\
268 -k, --token-table include a table of token names\n\
269 "), stdout);
270 putc ('\n', stdout);
271
272 fputs (_("\
273 Output:\n\
274 -d, --defines also produce a header file\n\
275 -r, --report=THINGS also produce details on the automaton\n\
276 -v, --verbose same as `--report=state'\n\
277 -b, --file-prefix=PREFIX specify a PREFIX for output files\n\
278 -o, --output=FILE leave output to FILE\n\
279 -g, --graph also produce a VCG description of the automaton\n\
280 "), stdout);
281 putc ('\n', stdout);
282
283 fputs (_("\
284 THINGS 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\
287 `lookahead' explicitly associate lookahead tokens to items\n\
288 `solved' describe shift/reduce conflicts solving\n\
289 `all' include all the above information\n\
290 `none' disable the report\n\
291 "), stdout);
292 putc ('\n', stdout);
293
294 fputs (_("\
295 Report bugs to <" PACKAGE_BUGREPORT ">.\n"), stdout);
296 }
297
298 exit (status);
299 }
300
301
302 /*------------------------------.
303 | Display the version message. |
304 `------------------------------*/
305
306 static void
307 version (void)
308 {
309 /* Some efforts were made to ease the translators' task, please
310 continue. */
311 printf (_("bison (GNU Bison) %s"), VERSION);
312 putc ('\n', stdout);
313 printf ("%s", revision);
314 fputs (_("Written by Robert Corbett and Richard Stallman.\n"), stdout);
315 putc ('\n', stdout);
316
317 fprintf (stdout,
318 _("Copyright (C) %d Free Software Foundation, Inc.\n"), 2006);
319
320 fputs (_("\
321 This is free software; see the source for copying conditions. There is NO\n\
322 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
323 "),
324 stdout);
325 }
326
327
328 /*----------------------.
329 | Process the options. |
330 `----------------------*/
331
332 /* Shorts options. */
333 static char const short_options[] = "yvegdhr:ltknVo:b:p:S:T::W";
334
335 /* Values for long options that do not have single-letter equivalents. */
336 enum
337 {
338 LOCATIONS_OPTION = CHAR_MAX + 1,
339 PRINT_LOCALEDIR_OPTION
340 };
341
342 static struct option const long_options[] =
343 {
344 /* Operation modes. */
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' },
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. */
363 { "trace", optional_argument, 0, 'T' },
364
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' },
374 { "locations", no_argument, 0, LOCATIONS_OPTION },
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
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
392 void
393 getargs (int argc, char *argv[])
394 {
395 int c;
396
397 while ((c = getopt_long (argc, argv, short_options, long_options, NULL))
398 != -1)
399 switch (c)
400 {
401 case 0:
402 /* Certain long options cause getopt_long to return 0. */
403 break;
404
405 case 'b':
406 spec_file_prefix = AS_FILE_NAME (optarg);
407 break;
408
409 case 'g':
410 /* Here, the -g and --graph=FILE options are differentiated. */
411 graph_flag = true;
412 if (optarg)
413 spec_graph_file = AS_FILE_NAME (optarg);
414 break;
415
416 case 'h':
417 usage (EXIT_SUCCESS);
418
419 case 'S':
420 skeleton = AS_FILE_NAME (optarg);
421 break;
422
423 case 'I':
424 include = AS_FILE_NAME (optarg);
425 break;
426
427 case 'd':
428 /* Here, the -d and --defines options are differentiated. */
429 defines_flag = true;
430 if (optarg)
431 spec_defines_file = AS_FILE_NAME (optarg);
432 break;
433
434 case 'k':
435 token_table_flag = true;
436 break;
437
438 case 'l':
439 no_lines_flag = true;
440 break;
441
442 case 'n':
443 no_parser_flag = true;
444 break;
445
446 case 'o':
447 spec_outfile = AS_FILE_NAME (optarg);
448 break;
449
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);
460 break;
461
462 case 't':
463 debug_flag = true;
464 break;
465
466 case 'V':
467 version ();
468 exit (EXIT_SUCCESS);
469
470 case 'v':
471 report_flag |= report_states;
472 break;
473
474 case 'y':
475 yacc_flag = true;
476 break;
477
478 case 'W':
479 FLAGS_ARGMATCH (warnings, optarg);
480 break;
481
482 case LOCATIONS_OPTION:
483 locations_flag = true;
484 break;
485
486 case PRINT_LOCALEDIR_OPTION:
487 printf ("%s\n", LOCALEDIR);
488 exit (EXIT_SUCCESS);
489
490 default:
491 usage (EXIT_FAILURE);
492 }
493
494 if (argc - optind != 1)
495 {
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]);
500 usage (EXIT_FAILURE);
501 }
502
503 current_file = grammar_file = uniqstr_new (argv[optind]);
504 }