]> git.saurik.com Git - bison.git/blob - src/getargs.c
Instead of having skeletons declare all valid %define variables and
[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 <configmake.h>
29 #include <error.h>
30
31 /* Hack to get <getopt.h> to declare getopt with a prototype. */
32 #if lint && ! defined __GNU_LIBRARY__
33 # define __GNU_LIBRARY__
34 # define HACK_FOR___GNU_LIBRARY___PROTOTYPE 1
35 #endif
36
37 #include <getopt.h>
38
39 #ifdef HACK_FOR___GNU_LIBRARY___PROTOTYPE
40 # undef __GNU_LIBRARY__
41 # undef HACK_FOR___GNU_LIBRARY___PROTOTYPE
42 #endif
43
44 #include "complain.h"
45 #include "files.h"
46 #include "getargs.h"
47 #include "uniqstr.h"
48
49 bool debug_flag;
50 bool defines_flag;
51 bool graph_flag;
52 bool locations_flag;
53 bool no_lines_flag;
54 bool no_parser_flag;
55 bool token_table_flag;
56 bool yacc_flag; /* for -y */
57
58 bool error_verbose = false;
59
60 bool nondeterministic_parser = false;
61 bool glr_parser = false;
62 bool pull_parser = true;
63 bool pure_parser = false;
64 bool push_parser = false;
65
66 int report_flag = report_none;
67 int trace_flag = trace_none;
68 int warnings_flag = warnings_none;
69
70 static struct bison_language const valid_languages[] = {
71 { "c", "c-skel.m4", ".c", ".h", true },
72 { "c++", "c++-skel.m4", ".cc", ".hh", true },
73 { "", "", "", "", false }
74 };
75
76 static int skeleton_prio = 2;
77 const char *skeleton = NULL;
78 static int language_prio = 2;
79 struct bison_language const *language = &valid_languages[0];
80 const char *include = NULL;
81
82 char *program_name;
83
84
85 /** Decode an option's set of keys.
86 *
87 * \param option option being decoded.
88 * \param keys array of valid subarguments.
89 * \param values array of corresponding (int) values.
90 * \param flags the flags to update
91 * \param args colon separated list of effective subarguments to decode.
92 * If 0, then activate all the flags.
93 *
94 * The special value 0 resets the flags to 0.
95 */
96 static void
97 flags_argmatch (const char *option,
98 const char * const keys[], const int values[],
99 int *flags, char *args)
100 {
101 if (args)
102 {
103 args = strtok (args, ",");
104 while (args)
105 {
106 int value = XARGMATCH (option, args, keys, values);
107 if (value == 0)
108 *flags = 0;
109 else
110 *flags |= value;
111 args = strtok (NULL, ",");
112 }
113 }
114 else
115 *flags = ~0;
116 }
117
118 /** Decode a set of sub arguments.
119 *
120 * \param FlagName the flag familly to update.
121 * \param Args the effective sub arguments to decode.
122 *
123 * \arg FlagName_args the list of keys.
124 * \arg FlagName_types the list of values.
125 * \arg FlagName_flag the flag to update.
126 */
127 #define FLAGS_ARGMATCH(FlagName, Args) \
128 flags_argmatch ("--" #FlagName, FlagName ## _args, FlagName ## _types, \
129 &FlagName ## _flag, Args)
130
131
132 /*----------------------.
133 | --report's handling. |
134 `----------------------*/
135
136 static const char * const report_args[] =
137 {
138 /* In a series of synonyms, present the most meaningful first, so
139 that argmatch_valid be more readable. */
140 "none",
141 "state", "states",
142 "itemset", "itemsets",
143 "lookahead", "lookaheads", "look-ahead",
144 "solved",
145 "all",
146 0
147 };
148
149 static const int report_types[] =
150 {
151 report_none,
152 report_states, report_states,
153 report_states | report_itemsets, report_states | report_itemsets,
154 report_states | report_lookahead_tokens,
155 report_states | report_lookahead_tokens,
156 report_states | report_lookahead_tokens,
157 report_states | report_solved_conflicts,
158 report_all
159 };
160
161 ARGMATCH_VERIFY (report_args, report_types);
162
163
164 /*---------------------.
165 | --trace's handling. |
166 `---------------------*/
167
168 static const char * const trace_args[] =
169 {
170 /* In a series of synonyms, present the most meaningful first, so
171 that argmatch_valid be more readable. */
172 "none - no traces",
173 "scan - grammar scanner traces",
174 "parse - grammar parser traces",
175 "automaton - construction of the automaton",
176 "bitsets - use of bitsets",
177 "grammar - reading, reducing the grammar",
178 "resource - memory consumption (where available)",
179 "sets - grammar sets: firsts, nullable etc.",
180 "tools - m4 invocation",
181 "m4 - m4 traces",
182 "skeleton - skeleton postprocessing",
183 "time - time consumption",
184 "all - all of the above",
185 0
186 };
187
188 static const int trace_types[] =
189 {
190 trace_none,
191 trace_scan,
192 trace_parse,
193 trace_automaton,
194 trace_bitsets,
195 trace_grammar,
196 trace_resource,
197 trace_sets,
198 trace_tools,
199 trace_m4,
200 trace_skeleton,
201 trace_time,
202 trace_all
203 };
204
205 ARGMATCH_VERIFY (trace_args, trace_types);
206
207
208 /*------------------------.
209 | --warnings's handling. |
210 `------------------------*/
211
212 static const char * const warnings_args[] =
213 {
214 /* In a series of synonyms, present the most meaningful first, so
215 that argmatch_valid be more readable. */
216 "none - no warnings",
217 "midrule-values - unset or unused midrule values",
218 "yacc - incompatibilities with POSIX YACC",
219 "all - all of the above",
220 "error - warnings are errors",
221 0
222 };
223
224 static const int warnings_types[] =
225 {
226 warnings_none,
227 warnings_midrule_values,
228 warnings_yacc,
229 warnings_all,
230 warnings_error
231 };
232
233 ARGMATCH_VERIFY (warnings_args, warnings_types);
234
235
236 /*-------------------------------------------.
237 | Display the help message and exit STATUS. |
238 `-------------------------------------------*/
239
240 static void usage (int) ATTRIBUTE_NORETURN;
241
242 static void
243 usage (int status)
244 {
245 if (status != 0)
246 fprintf (stderr, _("Try `%s --help' for more information.\n"),
247 program_name);
248 else
249 {
250 printf (_("Usage: %s [OPTION]... FILE\n"), program_name);
251 fputs (_("\
252 Generate LALR(1) and GLR parsers.\n\
253 \n\
254 "), stdout);
255
256 fputs (_("\
257 Mandatory arguments to long options are mandatory for short options too.\n\
258 "), stdout);
259
260 fputs (_("\
261 \n\
262 Operation modes:\n\
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\
266 -y, --yacc emulate POSIX Yacc\n\
267 \n\
268 "), stdout);
269
270 fputs (_("\
271 Parser:\n\
272 -S, --skeleton=FILE specify the skeleton to use\n\
273 -t, --debug instrument the parser for debugging\n\
274 --locations enable locations computation\n\
275 -p, --name-prefix=PREFIX prepend PREFIX to the external symbols\n\
276 -l, --no-lines don't generate `#line' directives\n\
277 -n, --no-parser generate the tables only\n\
278 -k, --token-table include a table of token names\n\
279 \n\
280 "), stdout);
281
282 fputs (_("\
283 Output:\n\
284 -d, --defines also produce a header file\n\
285 -r, --report=THINGS also produce details on the automaton\n\
286 -v, --verbose same as `--report=state'\n\
287 -b, --file-prefix=PREFIX specify a PREFIX for output files\n\
288 -o, --output=FILE leave output to FILE\n\
289 -g, --graph also output a graph of the automaton\n\
290 \n\
291 "), stdout);
292
293 fputs (_("\
294 THINGS is a list of comma separated words that can include:\n\
295 `state' describe the states\n\
296 `itemset' complete the core item sets with their closure\n\
297 `lookahead' explicitly associate lookahead tokens to items\n\
298 `solved' describe shift/reduce conflicts solving\n\
299 `all' include all the above information\n\
300 `none' disable the report\n\
301 "), stdout);
302
303 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
304 }
305
306 exit (status);
307 }
308
309
310 /*------------------------------.
311 | Display the version message. |
312 `------------------------------*/
313
314 static void
315 version (void)
316 {
317 /* Some efforts were made to ease the translators' task, please
318 continue. */
319 printf (_("bison (GNU Bison) %s"), VERSION);
320 putc ('\n', stdout);
321 printf ("%s", revision);
322 fputs (_("Written by Robert Corbett and Richard Stallman.\n"), stdout);
323 putc ('\n', stdout);
324
325 fprintf (stdout,
326 _("Copyright (C) %d Free Software Foundation, Inc.\n"), 2006);
327
328 fputs (_("\
329 This is free software; see the source for copying conditions. There is NO\n\
330 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
331 "),
332 stdout);
333 }
334
335
336 /*-------------------------------------.
337 | --skeleton and --language handling. |
338 `--------------------------------------*/
339
340 void
341 skeleton_arg (char const *arg, int prio, location const *loc)
342 {
343 if (prio < skeleton_prio)
344 {
345 skeleton_prio = prio;
346 skeleton = arg;
347 }
348 else if (prio == skeleton_prio)
349 {
350 char const *msg =
351 _("multiple skeleton declarations are invalid");
352 if (loc)
353 complain_at (*loc, msg);
354 else
355 complain (msg);
356 }
357 }
358
359 void
360 language_argmatch (char const *arg, int prio, location const *loc)
361 {
362 char const *msg;
363
364 if (prio < language_prio)
365 {
366 int i;
367 for (i = 0; valid_languages[i].language[0]; i++)
368 if (strcasecmp (arg, valid_languages[i].language) == 0)
369 {
370 language_prio = prio;
371 language = &valid_languages[i];
372 return;
373 }
374 msg = _("invalid language `%s'");
375 }
376 else if (language_prio == prio)
377 msg = _("multiple language declarations are invalid");
378 else
379 return;
380
381 if (loc)
382 complain_at (*loc, msg, arg);
383 else
384 complain (msg, arg);
385 }
386
387 /*----------------------.
388 | Process the options. |
389 `----------------------*/
390
391 /* Shorts options. */
392 static char const short_options[] = "yvegdhr:L:ltknVo:b:p:S:T::W";
393
394 /* Values for long options that do not have single-letter equivalents. */
395 enum
396 {
397 LOCATIONS_OPTION = CHAR_MAX + 1,
398 PRINT_LOCALEDIR_OPTION
399 };
400
401 static struct option const long_options[] =
402 {
403 /* Operation modes. */
404 { "help", no_argument, 0, 'h' },
405 { "version", no_argument, 0, 'V' },
406 { "print-localedir", no_argument, 0, PRINT_LOCALEDIR_OPTION },
407 { "warnings", optional_argument, 0, 'W' },
408
409 /* Parser. */
410 { "name-prefix", required_argument, 0, 'p' },
411 { "include", required_argument, 0, 'I' },
412
413 /* Output. */
414 { "file-prefix", required_argument, 0, 'b' },
415 { "output", required_argument, 0, 'o' },
416 { "output-file", required_argument, 0, 'o' },
417 { "graph", optional_argument, 0, 'g' },
418 { "report", required_argument, 0, 'r' },
419 { "verbose", no_argument, 0, 'v' },
420
421 /* Hidden. */
422 { "trace", optional_argument, 0, 'T' },
423
424 /* Output. */
425 { "defines", optional_argument, 0, 'd' },
426
427 /* Operation modes. */
428 { "fixed-output-files", no_argument, 0, 'y' },
429 { "yacc", no_argument, 0, 'y' },
430
431 /* Parser. */
432 { "debug", no_argument, 0, 't' },
433 { "locations", no_argument, 0, LOCATIONS_OPTION },
434 { "no-lines", no_argument, 0, 'l' },
435 { "no-parser", no_argument, 0, 'n' },
436 { "raw", no_argument, 0, 0 },
437 { "skeleton", required_argument, 0, 'S' },
438 { "language", required_argument, 0, 'L' },
439 { "token-table", no_argument, 0, 'k' },
440
441 {0, 0, 0, 0}
442 };
443
444 /* Under DOS, there is no difference on the case. This can be
445 troublesome when looking for `.tab' etc. */
446 #ifdef MSDOS
447 # define AS_FILE_NAME(File) (strlwr (File), (File))
448 #else
449 # define AS_FILE_NAME(File) (File)
450 #endif
451
452 void
453 getargs (int argc, char *argv[])
454 {
455 int c;
456
457 while ((c = getopt_long (argc, argv, short_options, long_options, NULL))
458 != -1)
459 switch (c)
460 {
461 case 0:
462 /* Certain long options cause getopt_long to return 0. */
463 break;
464
465 case 'b':
466 spec_file_prefix = AS_FILE_NAME (optarg);
467 break;
468
469 case 'g':
470 /* Here, the -g and --graph=FILE options are differentiated. */
471 graph_flag = true;
472 if (optarg)
473 spec_graph_file = xstrdup (AS_FILE_NAME (optarg));
474 break;
475
476 case 'h':
477 usage (EXIT_SUCCESS);
478
479 case 'L':
480 language_argmatch (optarg, 0, NULL);
481 break;
482
483 case 'S':
484 skeleton_arg (AS_FILE_NAME (optarg), 0, NULL);
485 break;
486
487 case 'I':
488 include = AS_FILE_NAME (optarg);
489 break;
490
491 case 'd':
492 /* Here, the -d and --defines options are differentiated. */
493 defines_flag = true;
494 if (optarg)
495 spec_defines_file = xstrdup (AS_FILE_NAME (optarg));
496 break;
497
498 case 'k':
499 token_table_flag = true;
500 break;
501
502 case 'l':
503 no_lines_flag = true;
504 break;
505
506 case 'n':
507 no_parser_flag = true;
508 break;
509
510 case 'o':
511 spec_outfile = AS_FILE_NAME (optarg);
512 break;
513
514 case 'p':
515 spec_name_prefix = optarg;
516 break;
517
518 case 'r':
519 FLAGS_ARGMATCH (report, optarg);
520 break;
521
522 case 'T':
523 FLAGS_ARGMATCH (trace, optarg);
524 break;
525
526 case 't':
527 debug_flag = true;
528 break;
529
530 case 'V':
531 version ();
532 exit (EXIT_SUCCESS);
533
534 case 'v':
535 report_flag |= report_states;
536 break;
537
538 case 'y':
539 yacc_flag = true;
540 break;
541
542 case 'W':
543 if (optarg)
544 FLAGS_ARGMATCH (warnings, optarg);
545 else
546 warnings_flag |= warnings_all;
547 break;
548
549 case LOCATIONS_OPTION:
550 locations_flag = true;
551 break;
552
553 case PRINT_LOCALEDIR_OPTION:
554 printf ("%s\n", LOCALEDIR);
555 exit (EXIT_SUCCESS);
556
557 default:
558 usage (EXIT_FAILURE);
559 }
560
561 if (argc - optind != 1)
562 {
563 if (argc - optind < 1)
564 error (0, 0, _("missing operand after `%s'"), argv[argc - 1]);
565 else
566 error (0, 0, _("extra operand `%s'"), argv[optind + 1]);
567 usage (EXIT_FAILURE);
568 }
569
570 current_file = grammar_file = uniqstr_new (argv[optind]);
571 }