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