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