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