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