]>
git.saurik.com Git - bison.git/blob - src/getargs.c
1 /* Parse command line arguments for Bison.
3 Copyright (C) 1984, 1986, 1989, 1992, 2000, 2001, 2002, 2003, 2004,
4 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
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>
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
39 #ifdef HACK_FOR___GNU_LIBRARY___PROTOTYPE
40 # undef __GNU_LIBRARY__
41 # undef HACK_FOR___GNU_LIBRARY___PROTOTYPE
47 #include "muscle_tab.h"
55 bool token_table_flag
;
56 bool yacc_flag
; /* for -y */
58 bool nondeterministic_parser
= false;
59 bool glr_parser
= false;
61 int report_flag
= report_none
;
62 int trace_flag
= trace_none
;
63 int warnings_flag
= warnings_none
;
65 static struct bison_language
const valid_languages
[] = {
66 { "c", "c-skel.m4", ".c", ".h", true },
67 { "c++", "c++-skel.m4", ".cc", ".hh", true },
68 { "java", "java-skel.m4", ".java", ".java", false },
69 { "", "", "", "", false }
72 int skeleton_prio
= default_prio
;
73 const char *skeleton
= NULL
;
74 int language_prio
= default_prio
;
75 struct bison_language
const *language
= &valid_languages
[0];
76 const char *include
= NULL
;
81 /** Decode an option's set of keys.
83 * \param option option being decoded.
84 * \param keys array of valid subarguments.
85 * \param values array of corresponding (int) values.
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.
90 * If VALUE != 0 then KEY sets flags and no-KEY clears them.
91 * If VALUE == 0 then KEY clears all flags and no-KEY sets all flags.
92 * Thus no-none = all and no-all = none.
95 flags_argmatch (const char *option
,
96 const char * const keys
[], const int values
[],
97 int *flags
, char *args
)
101 args
= strtok (args
, ",");
104 int no
= strncmp (args
, "no-", 3) == 0 ? 3 : 0;
105 int value
= XARGMATCH (option
, args
+ no
, keys
, values
);
116 args
= strtok (NULL
, ",");
123 /** Decode a set of sub arguments.
125 * \param FlagName the flag familly to update.
126 * \param Args the effective sub arguments to decode.
128 * \arg FlagName_args the list of keys.
129 * \arg FlagName_types the list of values.
130 * \arg FlagName_flag the flag to update.
132 #define FLAGS_ARGMATCH(FlagName, Args) \
133 flags_argmatch ("--" #FlagName, FlagName ## _args, FlagName ## _types, \
134 &FlagName ## _flag, Args)
137 /*----------------------.
138 | --report's handling. |
139 `----------------------*/
141 static const char * const report_args
[] =
143 /* In a series of synonyms, present the most meaningful first, so
144 that argmatch_valid be more readable. */
147 "itemset", "itemsets",
148 "lookahead", "lookaheads", "look-ahead",
154 static const int report_types
[] =
157 report_states
, report_states
,
158 report_states
| report_itemsets
, report_states
| report_itemsets
,
159 report_states
| report_lookahead_tokens
,
160 report_states
| report_lookahead_tokens
,
161 report_states
| report_lookahead_tokens
,
162 report_states
| report_solved_conflicts
,
166 ARGMATCH_VERIFY (report_args
, report_types
);
169 /*---------------------.
170 | --trace's handling. |
171 `---------------------*/
173 static const char * const trace_args
[] =
175 /* In a series of synonyms, present the most meaningful first, so
176 that argmatch_valid be more readable. */
178 "scan - grammar scanner traces",
179 "parse - grammar parser traces",
180 "automaton - construction of the automaton",
181 "bitsets - use of bitsets",
182 "grammar - reading, reducing the grammar",
183 "resource - memory consumption (where available)",
184 "sets - grammar sets: firsts, nullable etc.",
185 "muscles - m4 definitions passed to the skeleton",
186 "tools - m4 invocation",
188 "skeleton - skeleton postprocessing",
189 "time - time consumption",
190 "all - all of the above",
194 static const int trace_types
[] =
212 ARGMATCH_VERIFY (trace_args
, trace_types
);
215 /*------------------------.
216 | --warnings's handling. |
217 `------------------------*/
219 static const char * const warnings_args
[] =
221 /* In a series of synonyms, present the most meaningful first, so
222 that argmatch_valid be more readable. */
223 "none - no warnings",
224 "midrule-values - unset or unused midrule values",
225 "yacc - incompatibilities with POSIX YACC",
226 "all - all of the above",
227 "error - warnings are errors",
231 static const int warnings_types
[] =
234 warnings_midrule_values
,
240 ARGMATCH_VERIFY (warnings_args
, warnings_types
);
243 /*-------------------------------------------.
244 | Display the help message and exit STATUS. |
245 `-------------------------------------------*/
247 static void usage (int) ATTRIBUTE_NORETURN
;
253 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
257 /* For ../build-aux/cross-options.pl to work, use the format:
258 ^ -S, --long[=ARGS] (whitespace)
259 A --long option is required.
260 Otherwise, add exceptions to ../build-aux/cross-options.pl. */
262 printf (_("Usage: %s [OPTION]... FILE\n"), program_name
);
264 Generate LALR(1) and GLR parsers.\n\
269 Mandatory arguments to long options are mandatory for short options too.\n\
272 The same is true for optional arguments.\n\
278 -h, --help display this help and exit\n\
279 -V, --version output version information and exit\n\
280 --print-localedir output directory containing locale-dependent data\n\
281 --print-datadir output directory containing skeletons and XSLT\n\
282 -y, --yacc emulate POSIX Yacc\n\
283 -W, --warnings[=CATEGORY] report the warnings falling in CATEGORY\n\
289 -L, --language=LANGUAGE specify the output programming language\n\
290 (this is an experimental feature)\n\
291 -S, --skeleton=FILE specify the skeleton to use\n\
292 -t, --debug instrument the parser for debugging\n\
293 --locations enable location support\n\
294 -D, --define=NAME[=VALUE] same as `%define NAME \"VALUE\"'\n\
295 -p, --name-prefix=PREFIX prepend PREFIX to the external symbols\n\
296 -l, --no-lines don't generate `#line' directives\n\
297 -k, --token-table include a table of token names\n\
301 /* Keep -d and --defines separate so that ../build-aux/cross-options.pl
302 * won't assume that -d also takes an argument. */
305 --defines[=FILE] also produce a header file\n\
306 -d likewise but cannot specify FILE (for POSIX Yacc)\n\
307 -r, --report=THINGS also produce details on the automaton\n\
308 --report-file=FILE write report to FILE\n\
309 -v, --verbose same as `--report=state'\n\
310 -b, --file-prefix=PREFIX specify a PREFIX for output files\n\
311 -o, --output=FILE leave output to FILE\n\
312 -g, --graph[=FILE] also output a graph of the automaton\n\
313 -x, --xml[=FILE] also output an XML report of the automaton\n\
314 (the XML schema is experimental)\n\
319 Warning categories include:\n\
320 `midrule-values' unset or unused midrule values\n\
321 `yacc' incompatibilities with POSIX YACC\n\
322 `all' all the warnings\n\
323 `no-CATEGORY' turn off warnings in CATEGORY\n\
324 `none' turn off all the warnings\n\
325 `error' treat warnings as errors\n\
330 THINGS is a list of comma separated words that can include:\n\
331 `state' describe the states\n\
332 `itemset' complete the core item sets with their closure\n\
333 `lookahead' explicitly associate lookahead tokens to items\n\
334 `solved' describe shift/reduce conflicts solving\n\
335 `all' include all the above information\n\
336 `none' disable the report\n\
339 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
346 /*------------------------------.
347 | Display the version message. |
348 `------------------------------*/
353 /* Some efforts were made to ease the translators' task, please
355 printf (_("bison (GNU Bison) %s"), VERSION
);
357 fputs (_("Written by Robert Corbett and Richard Stallman.\n"), stdout
);
361 _("Copyright (C) %d Free Software Foundation, Inc.\n"),
362 PACKAGE_COPYRIGHT_YEAR
);
365 This is free software; see the source for copying conditions. There is NO\n\
366 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
372 /*-------------------------------------.
373 | --skeleton and --language handling. |
374 `--------------------------------------*/
377 skeleton_arg (char const *arg
, int prio
, location loc
)
379 if (prio
< skeleton_prio
)
381 skeleton_prio
= prio
;
384 else if (prio
== skeleton_prio
)
387 _("multiple skeleton declarations are invalid");
388 complain_at (loc
, msg
);
393 language_argmatch (char const *arg
, int prio
, location loc
)
397 if (prio
< language_prio
)
400 for (i
= 0; valid_languages
[i
].language
[0]; i
++)
401 if (c_strcasecmp (arg
, valid_languages
[i
].language
) == 0)
403 language_prio
= prio
;
404 language
= &valid_languages
[i
];
407 msg
= _("invalid language `%s'");
409 else if (language_prio
== prio
)
410 msg
= _("multiple language declarations are invalid");
414 complain_at (loc
, msg
, arg
);
417 /*----------------------.
418 | Process the options. |
419 `----------------------*/
422 Should be computed from long_options. */
423 static char const short_options
[] =
447 /* Values for long options that do not have single-letter equivalents. */
450 LOCATIONS_OPTION
= CHAR_MAX
+ 1,
451 PRINT_LOCALEDIR_OPTION
,
452 PRINT_DATADIR_OPTION
,
456 static struct option
const long_options
[] =
458 /* Operation modes. */
459 { "help", no_argument
, 0, 'h' },
460 { "version", no_argument
, 0, 'V' },
461 { "print-localedir", no_argument
, 0, PRINT_LOCALEDIR_OPTION
},
462 { "print-datadir", no_argument
, 0, PRINT_DATADIR_OPTION
},
463 { "warnings", optional_argument
, 0, 'W' },
466 { "name-prefix", required_argument
, 0, 'p' },
467 { "include", required_argument
, 0, 'I' },
470 { "file-prefix", required_argument
, 0, 'b' },
471 { "output", required_argument
, 0, 'o' },
472 { "output-file", required_argument
, 0, 'o' },
473 { "graph", optional_argument
, 0, 'g' },
474 { "xml", optional_argument
, 0, 'x' },
475 { "report", required_argument
, 0, 'r' },
476 { "report-file", required_argument
, 0, REPORT_FILE_OPTION
},
477 { "verbose", no_argument
, 0, 'v' },
480 { "trace", optional_argument
, 0, 'T' },
483 { "defines", optional_argument
, 0, 'd' },
485 /* Operation modes. */
486 { "fixed-output-files", no_argument
, 0, 'y' },
487 { "yacc", no_argument
, 0, 'y' },
490 { "debug", no_argument
, 0, 't' },
491 { "define", required_argument
, 0, 'D' },
492 { "locations", no_argument
, 0, LOCATIONS_OPTION
},
493 { "no-lines", no_argument
, 0, 'l' },
494 { "raw", no_argument
, 0, 0 },
495 { "skeleton", required_argument
, 0, 'S' },
496 { "language", required_argument
, 0, 'L' },
497 { "token-table", no_argument
, 0, 'k' },
502 /* Under DOS, there is no difference on the case. This can be
503 troublesome when looking for `.tab' etc. */
505 # define AS_FILE_NAME(File) (strlwr (File), (File))
507 # define AS_FILE_NAME(File) (File)
510 /* Build a location for the current command line argument. */
513 command_line_location (void)
516 /* "<command line>" is used in GCC's messages about -D. */
517 boundary_set (&res
.start
, uniqstr_new ("<command line>"), optind
, -1);
524 getargs (int argc
, char *argv
[])
528 while ((c
= getopt_long (argc
, argv
, short_options
, long_options
, NULL
))
532 /* ASCII Sorting for short options (i.e., upper case then
533 lower case), and then long-only options. */
536 /* Certain long options cause getopt_long to return 0. */
539 case 'D': /* -DNAME[=VALUE]. */
542 char* value
= strchr (optarg
, '=');
545 muscle_percent_define_insert (name
, command_line_location (),
551 include
= AS_FILE_NAME (optarg
);
555 language_argmatch (optarg
, command_line_prio
,
556 command_line_location ());
560 skeleton_arg (AS_FILE_NAME (optarg
), command_line_prio
,
561 command_line_location ());
565 FLAGS_ARGMATCH (trace
, optarg
);
574 FLAGS_ARGMATCH (warnings
, optarg
);
576 warnings_flag
|= warnings_all
;
580 spec_file_prefix
= AS_FILE_NAME (optarg
);
584 /* Here, the -d and --defines options are differentiated. */
587 spec_defines_file
= xstrdup (AS_FILE_NAME (optarg
));
593 spec_graph_file
= xstrdup (AS_FILE_NAME (optarg
));
597 usage (EXIT_SUCCESS
);
600 token_table_flag
= true;
604 no_lines_flag
= true;
608 spec_outfile
= AS_FILE_NAME (optarg
);
612 spec_name_prefix
= optarg
;
616 FLAGS_ARGMATCH (report
, optarg
);
620 muscle_percent_define_insert ("debug", command_line_location (), "");
624 report_flag
|= report_states
;
630 spec_xml_file
= xstrdup (AS_FILE_NAME (optarg
));
637 case LOCATIONS_OPTION
:
638 locations_flag
= true;
641 case PRINT_LOCALEDIR_OPTION
:
642 printf ("%s\n", LOCALEDIR
);
645 case PRINT_DATADIR_OPTION
:
646 printf ("%s\n", compute_pkgdatadir ());
649 case REPORT_FILE_OPTION
:
650 spec_verbose_file
= xstrdup (AS_FILE_NAME (optarg
));
654 usage (EXIT_FAILURE
);
657 if (argc
- optind
!= 1)
659 if (argc
- optind
< 1)
660 error (0, 0, _("missing operand after `%s'"), argv
[argc
- 1]);
662 error (0, 0, _("extra operand `%s'"), argv
[optind
+ 1]);
663 usage (EXIT_FAILURE
);
666 current_file
= grammar_file
= uniqstr_new (argv
[optind
]);
667 MUSCLE_INSERT_C_STRING ("file_name", grammar_file
);