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