]>
git.saurik.com Git - bison.git/blob - src/getargs.c
599cbbad3e2dd7d35cfdf77c7ba8c0f3a5238e26
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 report_flag
= report_none
;
67 int trace_flag
= trace_none
;
68 int warnings_flag
= warnings_conflicts_sr
| warnings_conflicts_rr
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 }
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
;
85 /** Decode an option's set of keys.
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.
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.
100 flags_argmatch (const char *option
,
101 const char * const keys
[], const int values
[],
102 int all
, int *flags
, char *args
)
106 args
= strtok (args
, ",");
109 int no
= strncmp (args
, "no-", 3) == 0 ? 3 : 0;
110 int value
= XARGMATCH (option
, args
+ no
, keys
, values
);
125 args
= strtok (NULL
, ",");
132 /** Decode a set of sub arguments.
134 * \param FlagName the flag familly to update.
135 * \param Args the effective sub arguments to decode.
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.
142 #define FLAGS_ARGMATCH(FlagName, Args) \
143 flags_argmatch ("--" #FlagName, FlagName ## _args, FlagName ## _types, \
144 FlagName ## _all, &FlagName ## _flag, Args)
147 /*----------------------.
148 | --report's handling. |
149 `----------------------*/
151 static const char * const report_args
[] =
153 /* In a series of synonyms, present the most meaningful first, so
154 that argmatch_valid be more readable. */
157 "itemset", "itemsets",
158 "lookahead", "lookaheads", "look-ahead",
164 static const int report_types
[] =
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
,
176 ARGMATCH_VERIFY (report_args
, report_types
);
179 /*---------------------.
180 | --trace's handling. |
181 `---------------------*/
183 static const char * const trace_args
[] =
185 /* In a series of synonyms, present the most meaningful first, so
186 that argmatch_valid be more readable. */
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",
198 "skeleton - skeleton postprocessing",
199 "time - time consumption",
200 "ielr - IELR conversion",
201 "all - all of the above",
205 static const int trace_types
[] =
224 ARGMATCH_VERIFY (trace_args
, trace_types
);
227 /*------------------------.
228 | --warnings's handling. |
229 `------------------------*/
231 static const char * const warnings_args
[] =
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",
246 static const int warnings_types
[] =
249 warnings_midrule_values
,
251 warnings_conflicts_sr
,
252 warnings_conflicts_rr
,
258 ARGMATCH_VERIFY (warnings_args
, warnings_types
);
261 /*-------------------------------------------.
262 | Display the help message and exit STATUS. |
263 `-------------------------------------------*/
265 static void usage (int) ATTRIBUTE_NORETURN
;
271 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
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. */
280 printf (_("Usage: %s [OPTION]... FILE\n"), program_name
);
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\
289 Mandatory arguments to long options are mandatory for short options too.\n\
292 The same is true for optional arguments.\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\
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\
323 /* Keep -d and --defines separate so that ../build-aux/cross-options.pl
324 * won't assume that -d also takes an argument. */
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\
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 `other' all other warnings (enabled by default)\n\
347 `all' all the warnings\n\
348 `no-CATEGORY' turn off warnings in CATEGORY\n\
349 `none' turn off all the warnings\n\
350 `error' treat warnings as errors\n\
355 THINGS is a list of comma separated words that can include:\n\
356 `state' describe the states\n\
357 `itemset' complete the core item sets with their closure\n\
358 `lookahead' explicitly associate lookahead tokens to items\n\
359 `solved' describe shift/reduce conflicts solving\n\
360 `all' include all the above information\n\
361 `none' disable the report\n\
364 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
371 /*------------------------------.
372 | Display the version message. |
373 `------------------------------*/
378 /* Some efforts were made to ease the translators' task, please
380 printf (_("bison (GNU Bison) %s"), VERSION
);
382 fputs (_("Written by Robert Corbett and Richard Stallman.\n"), stdout
);
386 _("Copyright (C) %d Free Software Foundation, Inc.\n"),
387 PACKAGE_COPYRIGHT_YEAR
);
390 This is free software; see the source for copying conditions. There is NO\n\
391 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
397 /*-------------------------------------.
398 | --skeleton and --language handling. |
399 `--------------------------------------*/
402 skeleton_arg (char const *arg
, int prio
, location loc
)
404 if (prio
< skeleton_prio
)
406 skeleton_prio
= prio
;
409 else if (prio
== skeleton_prio
)
410 complain_at (loc
, _("multiple skeleton declarations are invalid"));
414 language_argmatch (char const *arg
, int prio
, location loc
)
418 if (prio
< language_prio
)
421 for (i
= 0; valid_languages
[i
].language
[0]; i
++)
422 if (c_strcasecmp (arg
, valid_languages
[i
].language
) == 0)
424 language_prio
= prio
;
425 language
= &valid_languages
[i
];
428 msg
= _("%s: invalid language");
430 else if (language_prio
== prio
)
431 msg
= _("multiple language declarations are invalid");
435 complain_at (loc
, msg
, quotearg_colon (arg
));
438 /*----------------------.
439 | Process the options. |
440 `----------------------*/
443 Should be computed from long_options. */
444 static char const short_options
[] =
469 /* Values for long options that do not have single-letter equivalents. */
472 LOCATIONS_OPTION
= CHAR_MAX
+ 1,
473 PRINT_LOCALEDIR_OPTION
,
474 PRINT_DATADIR_OPTION
,
478 static struct option
const long_options
[] =
480 /* Operation modes. */
481 { "help", no_argument
, 0, 'h' },
482 { "version", no_argument
, 0, 'V' },
483 { "print-localedir", no_argument
, 0, PRINT_LOCALEDIR_OPTION
},
484 { "print-datadir", no_argument
, 0, PRINT_DATADIR_OPTION
},
485 { "warnings", optional_argument
, 0, 'W' },
488 { "name-prefix", required_argument
, 0, 'p' },
489 { "include", required_argument
, 0, 'I' },
492 { "file-prefix", required_argument
, 0, 'b' },
493 { "output", required_argument
, 0, 'o' },
494 { "output-file", required_argument
, 0, 'o' },
495 { "graph", optional_argument
, 0, 'g' },
496 { "xml", optional_argument
, 0, 'x' },
497 { "report", required_argument
, 0, 'r' },
498 { "report-file", required_argument
, 0, REPORT_FILE_OPTION
},
499 { "verbose", no_argument
, 0, 'v' },
502 { "trace", optional_argument
, 0, 'T' },
505 { "defines", optional_argument
, 0, 'd' },
507 /* Operation modes. */
508 { "fixed-output-files", no_argument
, 0, 'y' },
509 { "yacc", no_argument
, 0, 'y' },
512 { "debug", no_argument
, 0, 't' },
513 { "define", required_argument
, 0, 'D' },
514 { "force-define", required_argument
, 0, 'F' },
515 { "locations", no_argument
, 0, LOCATIONS_OPTION
},
516 { "no-lines", no_argument
, 0, 'l' },
517 { "raw", no_argument
, 0, 0 },
518 { "skeleton", required_argument
, 0, 'S' },
519 { "language", required_argument
, 0, 'L' },
520 { "token-table", no_argument
, 0, 'k' },
525 /* Under DOS, there is no difference on the case. This can be
526 troublesome when looking for `.tab' etc. */
528 # define AS_FILE_NAME(File) (strlwr (File), (File))
530 # define AS_FILE_NAME(File) (File)
533 /* Build a location for the current command line argument. */
536 command_line_location (void)
539 /* "<command line>" is used in GCC's messages about -D. */
540 boundary_set (&res
.start
, uniqstr_new ("<command line>"), optind
, -1);
547 getargs (int argc
, char *argv
[])
551 while ((c
= getopt_long (argc
, argv
, short_options
, long_options
, NULL
))
555 /* ASCII Sorting for short options (i.e., upper case then
556 lower case), and then long-only options. */
559 /* Certain long options cause getopt_long to return 0. */
562 case 'D': /* -DNAME[=VALUE]. */
563 case 'F': /* -FNAME[=VALUE]. */
566 char* value
= mbschr (optarg
, '=');
569 muscle_percent_define_insert (name
, command_line_location (),
571 c
== 'D' ? MUSCLE_PERCENT_DEFINE_D
572 : MUSCLE_PERCENT_DEFINE_F
);
577 include
= AS_FILE_NAME (optarg
);
581 language_argmatch (optarg
, command_line_prio
,
582 command_line_location ());
586 skeleton_arg (AS_FILE_NAME (optarg
), command_line_prio
,
587 command_line_location ());
591 FLAGS_ARGMATCH (trace
, optarg
);
599 FLAGS_ARGMATCH (warnings
, optarg
);
603 spec_file_prefix
= AS_FILE_NAME (optarg
);
607 /* Here, the -d and --defines options are differentiated. */
611 free (spec_defines_file
);
612 spec_defines_file
= xstrdup (AS_FILE_NAME (optarg
));
620 free (spec_graph_file
);
621 spec_graph_file
= xstrdup (AS_FILE_NAME (optarg
));
626 usage (EXIT_SUCCESS
);
629 token_table_flag
= true;
633 no_lines_flag
= true;
637 spec_outfile
= AS_FILE_NAME (optarg
);
641 spec_name_prefix
= optarg
;
645 FLAGS_ARGMATCH (report
, optarg
);
653 report_flag
|= report_states
;
660 free (spec_xml_file
);
661 spec_xml_file
= xstrdup (AS_FILE_NAME (optarg
));
669 case LOCATIONS_OPTION
:
670 locations_flag
= true;
673 case PRINT_LOCALEDIR_OPTION
:
674 printf ("%s\n", LOCALEDIR
);
677 case PRINT_DATADIR_OPTION
:
678 printf ("%s\n", compute_pkgdatadir ());
681 case REPORT_FILE_OPTION
:
682 free (spec_verbose_file
);
683 spec_verbose_file
= xstrdup (AS_FILE_NAME (optarg
));
687 usage (EXIT_FAILURE
);
690 if (argc
- optind
!= 1)
692 if (argc
- optind
< 1)
693 error (0, 0, _("%s: missing operand"), quotearg_colon (argv
[argc
- 1]));
695 error (0, 0, _("extra operand %s"), quote (argv
[optind
+ 1]));
696 usage (EXIT_FAILURE
);
699 current_file
= grammar_file
= uniqstr_new (argv
[optind
]);
700 MUSCLE_INSERT_C_STRING ("file_name", grammar_file
);
704 tr (char *s
, char from
, char to
)