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