]>
git.saurik.com Git - bison.git/blob - src/getargs.c
1b7c6f1bd1be3f8a64ad6c0ea25369dd43d46c18
1 /* Parse command line arguments for Bison.
3 Copyright (C) 1984, 1986, 1989, 1992, 2000-2012 Free Software
6 This file is part of Bison, the GNU Compiler Compiler.
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.
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.
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/>. */
26 #include <c-strcase.h>
27 #include <configmake.h>
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
38 #ifdef HACK_FOR___GNU_LIBRARY___PROTOTYPE
39 # undef __GNU_LIBRARY__
40 # undef HACK_FOR___GNU_LIBRARY___PROTOTYPE
48 #include "muscle-tab.h"
58 bool token_table_flag
;
59 bool yacc_flag
; /* for -y */
61 bool error_verbose
= false;
63 bool nondeterministic_parser
= false;
64 bool glr_parser
= false;
66 int feature_flag
= feature_none
;
67 int report_flag
= report_none
;
68 int trace_flag
= trace_none
;
69 int warnings_flag
= warnings_conflicts_sr
| warnings_conflicts_rr
72 static struct bison_language
const valid_languages
[] = {
73 { "c", "c-skel.m4", ".c", ".h", true },
74 { "c++", "c++-skel.m4", ".cc", ".hh", true },
75 { "java", "java-skel.m4", ".java", ".java", false },
76 { "", "", "", "", false }
79 int skeleton_prio
= default_prio
;
80 const char *skeleton
= NULL
;
81 int language_prio
= default_prio
;
82 struct bison_language
const *language
= &valid_languages
[0];
83 const char *include
= NULL
;
86 /** Decode an option's set of keys.
88 * \param option option being decoded.
89 * \param keys array of valid subarguments.
90 * \param values array of corresponding (int) values.
91 * \param all the all value.
92 * \param flags the flags to update
93 * \param args comma separated list of effective subarguments to decode.
94 * If 0, then activate all the flags.
96 * If VALUE != 0 then KEY sets flags and no-KEY clears them.
97 * If VALUE == 0 then KEY clears all flags from \c all and no-KEY sets all
98 * flags from \c all. Thus no-none = all and no-all = none.
101 flags_argmatch (const char *option
,
102 const char * const keys
[], const int values
[],
103 int all
, int *flags
, char *args
)
107 args
= strtok (args
, ",");
110 int no
= strncmp (args
, "no-", 3) == 0 ? 3 : 0;
111 int value
= XARGMATCH (option
, args
+ no
, keys
, values
);
126 args
= strtok (NULL
, ",");
133 /** Decode a set of sub arguments.
135 * \param FlagName the flag familly to update.
136 * \param Args the effective sub arguments to decode.
138 * \arg FlagName_args the list of keys.
139 * \arg FlagName_types the list of values.
140 * \arg FlagName_all the all value.
141 * \arg FlagName_flag the flag to update.
143 #define FLAGS_ARGMATCH(FlagName, Args) \
144 flags_argmatch ("--" #FlagName, FlagName ## _args, FlagName ## _types, \
145 FlagName ## _all, &FlagName ## _flag, Args)
148 /*----------------------.
149 | --report's handling. |
150 `----------------------*/
152 static const char * const report_args
[] =
154 /* In a series of synonyms, present the most meaningful first, so
155 that argmatch_valid be more readable. */
158 "itemset", "itemsets",
159 "lookahead", "lookaheads", "look-ahead",
165 static const int report_types
[] =
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
,
177 ARGMATCH_VERIFY (report_args
, report_types
);
180 /*---------------------.
181 | --trace's handling. |
182 `---------------------*/
184 static const char * const trace_args
[] =
186 /* In a series of synonyms, present the most meaningful first, so
187 that argmatch_valid be more readable. */
189 "scan - grammar scanner traces",
190 "parse - grammar parser traces",
191 "automaton - construction of the automaton",
192 "bitsets - use of bitsets",
193 "grammar - reading, reducing the grammar",
194 "resource - memory consumption (where available)",
195 "sets - grammar sets: firsts, nullable etc.",
196 "muscles - m4 definitions passed to the skeleton",
197 "tools - m4 invocation",
199 "skeleton - skeleton postprocessing",
200 "time - time consumption",
201 "ielr - IELR conversion",
202 "all - all of the above",
206 static const int trace_types
[] =
225 ARGMATCH_VERIFY (trace_args
, trace_types
);
228 /*------------------------.
229 | --warnings's handling. |
230 `------------------------*/
232 static const char * const warnings_args
[] =
234 /* In a series of synonyms, present the most meaningful first, so
235 that argmatch_valid be more readable. */
236 "none - no warnings",
237 "midrule-values - unset or unused midrule values",
238 "yacc - incompatibilities with POSIX Yacc",
239 "conflicts-sr - S/R conflicts",
240 "conflicts-rr - R/R conflicts",
241 "other - all other warnings",
242 "all - all of the above",
243 "error - warnings are errors",
247 static const int warnings_types
[] =
250 warnings_midrule_values
,
252 warnings_conflicts_sr
,
253 warnings_conflicts_rr
,
259 ARGMATCH_VERIFY (warnings_args
, warnings_types
);
261 /*-----------------------.
262 | --feature's handling. |
263 `-----------------------*/
265 static const char * const feature_args
[] =
268 "caret", "diagnostics-show-caret",
273 static const int feature_types
[] =
276 feature_caret
, feature_caret
,
280 ARGMATCH_VERIFY (feature_args
, feature_types
);
282 /*-------------------------------------------.
283 | Display the help message and exit STATUS. |
284 `-------------------------------------------*/
286 static void usage (int) ATTRIBUTE_NORETURN
;
292 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
296 /* For ../build-aux/cross-options.pl to work, use the format:
297 ^ -S, --long[=ARGS] (whitespace)
298 A --long option is required.
299 Otherwise, add exceptions to ../build-aux/cross-options.pl. */
301 printf (_("Usage: %s [OPTION]... FILE\n"), program_name
);
303 Generate a deterministic LR or generalized LR (GLR) parser employing\n\
304 LALR(1), IELR(1), or canonical LR(1) parser tables. IELR(1) and\n\
305 canonical LR(1) support is experimental.\n\
310 Mandatory arguments to long options are mandatory for short options too.\n\
313 The same is true for optional arguments.\n\
319 -h, --help display this help and exit\n\
320 -V, --version output version information and exit\n\
321 --print-localedir output directory containing locale-dependent data\n\
322 --print-datadir output directory containing skeletons and XSLT\n\
323 -y, --yacc emulate POSIX Yacc\n\
324 -W, --warnings[=CATEGORY] report the warnings falling in CATEGORY\n\
325 -f, --feature[=FEATURE] activate miscellaneous features\n\
331 -L, --language=LANGUAGE specify the output programming language\n\
332 -S, --skeleton=FILE specify the skeleton to use\n\
333 -t, --debug instrument the parser for debugging\n\
334 --locations enable location support\n\
335 -D, --define=NAME[=VALUE] similar to '%define NAME \"VALUE\"'\n\
336 -F, --force-define=NAME[=VALUE] override '%define NAME \"VALUE\"'\n\
337 -p, --name-prefix=PREFIX prepend PREFIX to the external symbols\n\
338 deprecated by '-Dapi.prefix=PREFIX'\n\
339 -l, --no-lines don't generate '#line' directives\n\
340 -k, --token-table include a table of token names\n\
344 /* Keep -d and --defines separate so that ../build-aux/cross-options.pl
345 * won't assume that -d also takes an argument. */
348 --defines[=FILE] also produce a header file\n\
349 -d likewise but cannot specify FILE (for POSIX Yacc)\n\
350 -r, --report=THINGS also produce details on the automaton\n\
351 --report-file=FILE write report to FILE\n\
352 -v, --verbose same as `--report=state'\n\
353 -b, --file-prefix=PREFIX specify a PREFIX for output files\n\
354 -o, --output=FILE leave output to FILE\n\
355 -g, --graph[=FILE] also output a graph of the automaton\n\
356 -x, --xml[=FILE] also output an XML report of the automaton\n\
357 (the XML schema is experimental)\n\
362 Warning categories include:\n\
363 `midrule-values' unset or unused midrule values\n\
364 `yacc' incompatibilities with POSIX Yacc\n\
365 `conflicts-sr' S/R conflicts (enabled by default)\n\
366 `conflicts-rr' R/R conflicts (enabled by default)\n\
367 `deprecated' obsolete constructs\n\
368 `other' all other warnings (enabled by default)\n\
369 `all' all the warnings\n\
370 `no-CATEGORY' turn off warnings in CATEGORY\n\
371 `none' turn off all the warnings\n\
372 `error' treat warnings as errors\n\
377 THINGS is a list of comma separated words that can include:\n\
378 `state' describe the states\n\
379 `itemset' complete the core item sets with their closure\n\
380 `lookahead' explicitly associate lookahead tokens to items\n\
381 `solved' describe shift/reduce conflicts solving\n\
382 `all' include all the above information\n\
383 `none' disable the report\n\
388 FEATURE is a list of comma separated words that can include:\n\
389 `caret' show errors with carets\n\
390 `all' all of the above\n\
391 `none' disable all of the above\n\
395 printf (_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
396 printf (_("%s home page: <%s>.\n"), PACKAGE_NAME
, PACKAGE_URL
);
397 fputs (_("General help using GNU software: "
398 "<http://www.gnu.org/gethelp/>.\n"),
400 /* Don't output this redundant message for English locales.
401 Note we still output for 'C' so that it gets included in the
403 const char *lc_messages
= setlocale (LC_MESSAGES
, NULL
);
404 if (lc_messages
&& strcmp (lc_messages
, "en_"))
405 /* TRANSLATORS: Replace LANG_CODE in this URL with your language
406 code <http://translationproject.org/team/LANG_CODE.html> to
407 form one of the URLs at http://translationproject.org/team/.
408 Otherwise, replace the entire URL with your translation team's
410 fputs (_("Report translation bugs to "
411 "<http://translationproject.org/team/>.\n"), stdout
);
412 fputs (_("For complete documentation, run: info bison.\n"), stdout
);
419 /*------------------------------.
420 | Display the version message. |
421 `------------------------------*/
426 /* Some efforts were made to ease the translators' task, please
428 printf (_("bison (GNU Bison) %s"), VERSION
);
430 fputs (_("Written by Robert Corbett and Richard Stallman.\n"), stdout
);
434 _("Copyright (C) %d Free Software Foundation, Inc.\n"),
435 PACKAGE_COPYRIGHT_YEAR
);
438 This is free software; see the source for copying conditions. There is NO\n\
439 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
445 /*-------------------------------------.
446 | --skeleton and --language handling. |
447 `--------------------------------------*/
450 skeleton_arg (char const *arg
, int prio
, location loc
)
452 if (prio
< skeleton_prio
)
454 skeleton_prio
= prio
;
457 else if (prio
== skeleton_prio
)
458 complain_at (loc
, _("multiple skeleton declarations are invalid"));
462 language_argmatch (char const *arg
, int prio
, location loc
)
466 if (prio
< language_prio
)
469 for (i
= 0; valid_languages
[i
].language
[0]; i
++)
470 if (c_strcasecmp (arg
, valid_languages
[i
].language
) == 0)
472 language_prio
= prio
;
473 language
= &valid_languages
[i
];
476 msg
= _("%s: invalid language");
478 else if (language_prio
== prio
)
479 msg
= _("multiple language declarations are invalid");
483 complain_at (loc
, msg
, quotearg_colon (arg
));
486 /*----------------------.
487 | Process the options. |
488 `----------------------*/
491 Should be computed from long_options. */
492 static char const short_options
[] =
518 /* Values for long options that do not have single-letter equivalents. */
521 LOCATIONS_OPTION
= CHAR_MAX
+ 1,
522 PRINT_LOCALEDIR_OPTION
,
523 PRINT_DATADIR_OPTION
,
527 static struct option
const long_options
[] =
529 /* Operation modes. */
530 { "help", no_argument
, 0, 'h' },
531 { "version", no_argument
, 0, 'V' },
532 { "print-localedir", no_argument
, 0, PRINT_LOCALEDIR_OPTION
},
533 { "print-datadir", no_argument
, 0, PRINT_DATADIR_OPTION
},
534 { "warnings", optional_argument
, 0, 'W' },
537 { "name-prefix", required_argument
, 0, 'p' },
538 { "include", required_argument
, 0, 'I' },
541 { "file-prefix", required_argument
, 0, 'b' },
542 { "output", required_argument
, 0, 'o' },
543 { "output-file", required_argument
, 0, 'o' },
544 { "graph", optional_argument
, 0, 'g' },
545 { "xml", optional_argument
, 0, 'x' },
546 { "report", required_argument
, 0, 'r' },
547 { "report-file", required_argument
, 0, REPORT_FILE_OPTION
},
548 { "verbose", no_argument
, 0, 'v' },
551 { "trace", optional_argument
, 0, 'T' },
554 { "defines", optional_argument
, 0, 'd' },
555 { "flag", optional_argument
, 0, 'f' },
557 /* Operation modes. */
558 { "fixed-output-files", no_argument
, 0, 'y' },
559 { "yacc", no_argument
, 0, 'y' },
562 { "debug", no_argument
, 0, 't' },
563 { "define", required_argument
, 0, 'D' },
564 { "force-define", required_argument
, 0, 'F' },
565 { "locations", no_argument
, 0, LOCATIONS_OPTION
},
566 { "no-lines", no_argument
, 0, 'l' },
567 { "raw", no_argument
, 0, 0 },
568 { "skeleton", required_argument
, 0, 'S' },
569 { "language", required_argument
, 0, 'L' },
570 { "token-table", no_argument
, 0, 'k' },
575 /* Under DOS, there is no difference on the case. This can be
576 troublesome when looking for `.tab' etc. */
578 # define AS_FILE_NAME(File) (strlwr (File), (File))
580 # define AS_FILE_NAME(File) (File)
583 /* Build a location for the current command line argument. */
586 command_line_location (void)
589 /* "<command line>" is used in GCC's messages about -D. */
590 boundary_set (&res
.start
, uniqstr_new ("<command line>"), optind
- 1, -1);
597 getargs (int argc
, char *argv
[])
601 while ((c
= getopt_long (argc
, argv
, short_options
, long_options
, NULL
))
605 /* ASCII Sorting for short options (i.e., upper case then
606 lower case), and then long-only options. */
609 /* Certain long options cause getopt_long to return 0. */
612 case 'D': /* -DNAME[=VALUE]. */
613 case 'F': /* -FNAME[=VALUE]. */
616 char* value
= mbschr (optarg
, '=');
619 muscle_percent_define_insert (name
, command_line_location (),
621 c
== 'D' ? MUSCLE_PERCENT_DEFINE_D
622 : MUSCLE_PERCENT_DEFINE_F
);
627 include
= AS_FILE_NAME (optarg
);
631 language_argmatch (optarg
, command_line_prio
,
632 command_line_location ());
636 skeleton_arg (AS_FILE_NAME (optarg
), command_line_prio
,
637 command_line_location ());
641 FLAGS_ARGMATCH (trace
, optarg
);
649 FLAGS_ARGMATCH (flag
, optarg
);
653 FLAGS_ARGMATCH (warnings
, optarg
);
657 spec_file_prefix
= AS_FILE_NAME (optarg
);
661 /* Here, the -d and --defines options are differentiated. */
665 free (spec_defines_file
);
666 spec_defines_file
= xstrdup (AS_FILE_NAME (optarg
));
674 free (spec_graph_file
);
675 spec_graph_file
= xstrdup (AS_FILE_NAME (optarg
));
680 usage (EXIT_SUCCESS
);
683 token_table_flag
= true;
687 no_lines_flag
= true;
691 spec_outfile
= AS_FILE_NAME (optarg
);
695 spec_name_prefix
= optarg
;
699 FLAGS_ARGMATCH (report
, optarg
);
707 report_flag
|= report_states
;
714 free (spec_xml_file
);
715 spec_xml_file
= xstrdup (AS_FILE_NAME (optarg
));
723 case LOCATIONS_OPTION
:
724 locations_flag
= true;
727 case PRINT_LOCALEDIR_OPTION
:
728 printf ("%s\n", LOCALEDIR
);
731 case PRINT_DATADIR_OPTION
:
732 printf ("%s\n", compute_pkgdatadir ());
735 case REPORT_FILE_OPTION
:
736 free (spec_verbose_file
);
737 spec_verbose_file
= xstrdup (AS_FILE_NAME (optarg
));
741 usage (EXIT_FAILURE
);
744 if (argc
- optind
!= 1)
746 if (argc
- optind
< 1)
747 error (0, 0, _("%s: missing operand"), quotearg_colon (argv
[argc
- 1]));
749 error (0, 0, _("extra operand %s"), quote (argv
[optind
+ 1]));
750 usage (EXIT_FAILURE
);
753 current_file
= grammar_file
= uniqstr_new (argv
[optind
]);
754 MUSCLE_INSERT_C_STRING ("file_name", grammar_file
);
758 tr (char *s
, char from
, char to
)