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