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