]> git.saurik.com Git - bison.git/blob - src/getargs.c
90b2655f7627a1db073f1e5ed26ce28181d4b861
[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 = all;
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 if (no)
103 *flags |= all;
104 else
105 *flags &= ~all;
106 }
107 else
108 {
109 if (no)
110 *flags &= ~value;
111 else
112 {
113 if (err)
114 warnings_flag |= value;
115 *flags |= value;
116 }
117 }
118 flags = save_flags;
119 args = strtok (NULL, ",");
120 }
121 }
122 else
123 *flags |= all;
124 }
125
126 /** Decode a set of sub arguments.
127 *
128 * \param FlagName the flag familly to update.
129 * \param Args the effective sub arguments to decode.
130 * \param All the "all" value.
131 *
132 * \arg FlagName_args the list of keys.
133 * \arg FlagName_types the list of values.
134 * \arg FlagName_flag the flag to update.
135 */
136 #define FLAGS_ARGMATCH(FlagName, Args, All) \
137 flags_argmatch ("--" #FlagName, FlagName ## _args, FlagName ## _types, \
138 All, &FlagName ## _flag, Args)
139
140
141 /*----------------------.
142 | --report's handling. |
143 `----------------------*/
144
145 static const char * const report_args[] =
146 {
147 /* In a series of synonyms, present the most meaningful first, so
148 that argmatch_valid be more readable. */
149 "none",
150 "state", "states",
151 "itemset", "itemsets",
152 "lookahead", "lookaheads", "look-ahead",
153 "solved",
154 "all",
155 0
156 };
157
158 static const int report_types[] =
159 {
160 report_none,
161 report_states, report_states,
162 report_states | report_itemsets, report_states | report_itemsets,
163 report_states | report_lookahead_tokens,
164 report_states | report_lookahead_tokens,
165 report_states | report_lookahead_tokens,
166 report_states | report_solved_conflicts,
167 report_all
168 };
169
170 ARGMATCH_VERIFY (report_args, report_types);
171
172
173 /*---------------------.
174 | --trace's handling. |
175 `---------------------*/
176
177 static const char * const trace_args[] =
178 {
179 /* In a series of synonyms, present the most meaningful first, so
180 that argmatch_valid be more readable. */
181 "none - no traces",
182 "scan - grammar scanner traces",
183 "parse - grammar parser traces",
184 "automaton - construction of the automaton",
185 "bitsets - use of bitsets",
186 "grammar - reading, reducing the grammar",
187 "resource - memory consumption (where available)",
188 "sets - grammar sets: firsts, nullable etc.",
189 "muscles - m4 definitions passed to the skeleton",
190 "tools - m4 invocation",
191 "m4 - m4 traces",
192 "skeleton - skeleton postprocessing",
193 "time - time consumption",
194 "ielr - IELR conversion",
195 "all - all of the above",
196 0
197 };
198
199 static const int trace_types[] =
200 {
201 trace_none,
202 trace_scan,
203 trace_parse,
204 trace_automaton,
205 trace_bitsets,
206 trace_grammar,
207 trace_resource,
208 trace_sets,
209 trace_muscles,
210 trace_tools,
211 trace_m4,
212 trace_skeleton,
213 trace_time,
214 trace_ielr,
215 trace_all
216 };
217
218 ARGMATCH_VERIFY (trace_args, trace_types);
219
220
221 /*------------------------.
222 | --warnings's handling. |
223 `------------------------*/
224
225 static const char * const warnings_args[] =
226 {
227 /* In a series of synonyms, present the most meaningful first, so
228 that argmatch_valid be more readable. */
229 "none - no warnings",
230 "midrule-values - unset or unused midrule values",
231 "yacc - incompatibilities with POSIX Yacc",
232 "conflicts-sr - S/R conflicts",
233 "conflicts-rr - R/R conflicts",
234 "deprecated - obsolete constructs",
235 "other - all other warnings",
236 "all - all of the above",
237 "error - warnings are errors",
238 0
239 };
240
241 static const int warnings_types[] =
242 {
243 Wnone,
244 Wmidrule_values,
245 Wyacc,
246 Wconflicts_sr,
247 Wconflicts_rr,
248 Wdeprecated,
249 Wother,
250 Wall,
251 Werror
252 };
253
254 ARGMATCH_VERIFY (warnings_args, warnings_types);
255
256 /*-------------------------------------------.
257 | Display the help message and exit STATUS. |
258 `-------------------------------------------*/
259
260 static void usage (int) ATTRIBUTE_NORETURN;
261
262 static void
263 usage (int status)
264 {
265 if (status != 0)
266 fprintf (stderr, _("Try `%s --help' for more information.\n"),
267 program_name);
268 else
269 {
270 /* For ../build-aux/cross-options.pl to work, use the format:
271 ^ -S, --long[=ARGS] (whitespace)
272 A --long option is required.
273 Otherwise, add exceptions to ../build-aux/cross-options.pl. */
274
275 printf (_("Usage: %s [OPTION]... FILE\n"), program_name);
276 fputs (_("\
277 Generate a deterministic LR or generalized LR (GLR) parser employing\n\
278 LALR(1), IELR(1), or canonical LR(1) parser tables. IELR(1) and\n\
279 canonical LR(1) support is experimental.\n\
280 \n\
281 "), stdout);
282
283 fputs (_("\
284 Mandatory arguments to long options are mandatory for short options too.\n\
285 "), stdout);
286 fputs (_("\
287 The same is true for optional arguments.\n\
288 "), stdout);
289
290 fputs (_("\
291 \n\
292 Operation modes:\n\
293 -h, --help display this help and exit\n\
294 -V, --version output version information and exit\n\
295 --print-localedir output directory containing locale-dependent data\n\
296 --print-datadir output directory containing skeletons and XSLT\n\
297 -y, --yacc emulate POSIX Yacc\n\
298 -W, --warnings[=CATEGORY] report the warnings falling in CATEGORY\n\
299 \n\
300 "), stdout);
301
302 fputs (_("\
303 Parser:\n\
304 -L, --language=LANGUAGE specify the output programming language\n\
305 (this is an experimental feature)\n\
306 -S, --skeleton=FILE specify the skeleton to use\n\
307 -t, --debug instrument the parser for tracing\n\
308 same as `-Dparse.trace'\n\
309 --locations enable location support\n\
310 -D, --define=NAME[=VALUE] similar to '%define NAME \"VALUE\"'\n\
311 -F, --force-define=NAME[=VALUE] override '%define NAME \"VALUE\"'\n\
312 -p, --name-prefix=PREFIX prepend PREFIX to the external symbols\n\
313 deprecated by '-Dapi.prefix=PREFIX'\n\
314 -l, --no-lines don't generate '#line' directives\n\
315 -k, --token-table include a table of token names\n\
316 \n\
317 "), stdout);
318
319 /* Keep -d and --defines separate so that ../build-aux/cross-options.pl
320 * won't assume that -d also takes an argument. */
321 fputs (_("\
322 Output:\n\
323 --defines[=FILE] also produce a header file\n\
324 -d likewise but cannot specify FILE (for POSIX Yacc)\n\
325 -r, --report=THINGS also produce details on the automaton\n\
326 --report-file=FILE write report to FILE\n\
327 -v, --verbose same as `--report=state'\n\
328 -b, --file-prefix=PREFIX specify a PREFIX for output files\n\
329 -o, --output=FILE leave output to FILE\n\
330 -g, --graph[=FILE] also output a graph of the automaton\n\
331 -x, --xml[=FILE] also output an XML report of the automaton\n\
332 (the XML schema is experimental)\n\
333 \n\
334 "), stdout);
335
336 fputs (_("\
337 Warning categories include:\n\
338 `midrule-values' unset or unused midrule values\n\
339 `yacc' incompatibilities with POSIX Yacc\n\
340 `conflicts-sr' S/R conflicts (enabled by default)\n\
341 `conflicts-rr' R/R conflicts (enabled by default)\n\
342 `deprecated' obsolete constructs\n\
343 `other' all other warnings (enabled by default)\n\
344 `all' all the warnings\n\
345 `no-CATEGORY' turn off warnings in CATEGORY\n\
346 `none' turn off all the warnings\n\
347 `error[=CATEGORY]' treat warnings as errors\n\
348 "), stdout);
349 putc ('\n', stdout);
350
351 fputs (_("\
352 THINGS is a list of comma separated words that can include:\n\
353 `state' describe the states\n\
354 `itemset' complete the core item sets with their closure\n\
355 `lookahead' explicitly associate lookahead tokens to items\n\
356 `solved' describe shift/reduce conflicts solving\n\
357 `all' include all the above information\n\
358 `none' disable the report\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_at (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_at (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 "e"
471 "g::"
472 "h"
473 "k"
474 "l"
475 "n"
476 "o:"
477 "p:"
478 "r:"
479 "t"
480 "v"
481 "x::"
482 "y"
483 ;
484
485 /* Values for long options that do not have single-letter equivalents. */
486 enum
487 {
488 LOCATIONS_OPTION = CHAR_MAX + 1,
489 PRINT_LOCALEDIR_OPTION,
490 PRINT_DATADIR_OPTION,
491 REPORT_FILE_OPTION
492 };
493
494 static struct option const long_options[] =
495 {
496 /* Operation modes. */
497 { "help", no_argument, 0, 'h' },
498 { "version", no_argument, 0, 'V' },
499 { "print-localedir", no_argument, 0, PRINT_LOCALEDIR_OPTION },
500 { "print-datadir", no_argument, 0, PRINT_DATADIR_OPTION },
501 { "warnings", optional_argument, 0, 'W' },
502
503 /* Parser. */
504 { "name-prefix", required_argument, 0, 'p' },
505 { "include", required_argument, 0, 'I' },
506
507 /* Output. */
508 { "file-prefix", required_argument, 0, 'b' },
509 { "output", required_argument, 0, 'o' },
510 { "output-file", required_argument, 0, 'o' },
511 { "graph", optional_argument, 0, 'g' },
512 { "xml", optional_argument, 0, 'x' },
513 { "report", required_argument, 0, 'r' },
514 { "report-file", required_argument, 0, REPORT_FILE_OPTION },
515 { "verbose", no_argument, 0, 'v' },
516
517 /* Hidden. */
518 { "trace", optional_argument, 0, 'T' },
519
520 /* Output. */
521 { "defines", optional_argument, 0, 'd' },
522
523 /* Operation modes. */
524 { "fixed-output-files", no_argument, 0, 'y' },
525 { "yacc", no_argument, 0, 'y' },
526
527 /* Parser. */
528 { "debug", no_argument, 0, 't' },
529 { "define", required_argument, 0, 'D' },
530 { "force-define", required_argument, 0, 'F' },
531 { "locations", no_argument, 0, LOCATIONS_OPTION },
532 { "no-lines", no_argument, 0, 'l' },
533 { "raw", no_argument, 0, 0 },
534 { "skeleton", required_argument, 0, 'S' },
535 { "language", required_argument, 0, 'L' },
536 { "token-table", no_argument, 0, 'k' },
537
538 {0, 0, 0, 0}
539 };
540
541 /* Under DOS, there is no difference on the case. This can be
542 troublesome when looking for `.tab' etc. */
543 #ifdef MSDOS
544 # define AS_FILE_NAME(File) (strlwr (File), (File))
545 #else
546 # define AS_FILE_NAME(File) (File)
547 #endif
548
549 /* Build a location for the current command line argument. */
550 static
551 location
552 command_line_location (void)
553 {
554 location res;
555 /* "<command line>" is used in GCC's messages about -D. */
556 boundary_set (&res.start, uniqstr_new ("<command line>"), optind, -1);
557 res.end = res.start;
558 return res;
559 }
560
561
562 void
563 getargs (int argc, char *argv[])
564 {
565 int c;
566
567 while ((c = getopt_long (argc, argv, short_options, long_options, NULL))
568 != -1)
569 switch (c)
570 {
571 /* ASCII Sorting for short options (i.e., upper case then
572 lower case), and then long-only options. */
573
574 case 0:
575 /* Certain long options cause getopt_long to return 0. */
576 break;
577
578 case 'D': /* -DNAME[=VALUE]. */
579 case 'F': /* -FNAME[=VALUE]. */
580 {
581 char* name = optarg;
582 char* value = strchr (optarg, '=');
583 if (value)
584 *value++ = 0;
585 muscle_percent_define_insert (name, command_line_location (),
586 value ? value : "",
587 c == 'D' ? MUSCLE_PERCENT_DEFINE_D
588 : MUSCLE_PERCENT_DEFINE_F);
589 }
590 break;
591
592 case 'I':
593 include = AS_FILE_NAME (optarg);
594 break;
595
596 case 'L':
597 language_argmatch (optarg, command_line_prio,
598 command_line_location ());
599 break;
600
601 case 'S':
602 skeleton_arg (AS_FILE_NAME (optarg), command_line_prio,
603 command_line_location ());
604 break;
605
606 case 'T':
607 FLAGS_ARGMATCH (trace, optarg, trace_all);
608 break;
609
610 case 'V':
611 version ();
612 exit (EXIT_SUCCESS);
613
614 case 'W':
615 FLAGS_ARGMATCH (warnings, optarg, Wall);
616 break;
617
618 case 'b':
619 spec_file_prefix = AS_FILE_NAME (optarg);
620 break;
621
622 case 'd':
623 /* Here, the -d and --defines options are differentiated. */
624 defines_flag = true;
625 if (optarg)
626 {
627 free (spec_defines_file);
628 spec_defines_file = xstrdup (AS_FILE_NAME (optarg));
629 }
630 break;
631
632 case 'g':
633 graph_flag = true;
634 if (optarg)
635 {
636 free (spec_graph_file);
637 spec_graph_file = xstrdup (AS_FILE_NAME (optarg));
638 }
639 break;
640
641 case 'h':
642 usage (EXIT_SUCCESS);
643
644 case 'k':
645 token_table_flag = true;
646 break;
647
648 case 'l':
649 no_lines_flag = true;
650 break;
651
652 case 'o':
653 spec_outfile = AS_FILE_NAME (optarg);
654 break;
655
656 case 'p':
657 spec_name_prefix = optarg;
658 break;
659
660 case 'r':
661 FLAGS_ARGMATCH (report, optarg, report_all);
662 break;
663
664 case 't':
665 muscle_percent_define_insert ("parse.trace",
666 command_line_location (), "",
667 MUSCLE_PERCENT_DEFINE_D);
668 break;
669
670 case 'v':
671 report_flag |= report_states;
672 break;
673
674 case 'x':
675 xml_flag = true;
676 if (optarg)
677 {
678 free (spec_xml_file);
679 spec_xml_file = xstrdup (AS_FILE_NAME (optarg));
680 }
681 break;
682
683 case 'y':
684 yacc_flag = true;
685 break;
686
687 case LOCATIONS_OPTION:
688 muscle_percent_define_ensure ("locations",
689 command_line_location (), true);
690 break;
691
692 case PRINT_LOCALEDIR_OPTION:
693 printf ("%s\n", LOCALEDIR);
694 exit (EXIT_SUCCESS);
695
696 case PRINT_DATADIR_OPTION:
697 printf ("%s\n", pkgdatadir ());
698 exit (EXIT_SUCCESS);
699
700 case REPORT_FILE_OPTION:
701 free (spec_verbose_file);
702 spec_verbose_file = xstrdup (AS_FILE_NAME (optarg));
703 break;
704
705 default:
706 usage (EXIT_FAILURE);
707 }
708
709 if (argc - optind != 1)
710 {
711 if (argc - optind < 1)
712 error (0, 0, _("%s: missing operand"), quotearg_colon (argv[argc - 1]));
713 else
714 error (0, 0, _("extra operand %s"), quote (argv[optind + 1]));
715 usage (EXIT_FAILURE);
716 }
717
718 current_file = grammar_file = uniqstr_new (argv[optind]);
719 MUSCLE_INSERT_C_STRING ("file_name", grammar_file);
720 }
721
722 void
723 tr (char *s, char from, char to)
724 {
725 for (; *s; s++)
726 if (*s == from)
727 *s = to;
728 }