]>
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 Free Software Foundation, Inc.
6 This file is part of Bison, the GNU Compiler Compiler.
8 Bison is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 Bison is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Bison; see the file COPYING. If not, write to the Free
20 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
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
54 bool token_table_flag
;
55 bool yacc_flag
; /* for -y */
57 bool error_verbose
= false;
59 bool nondeterministic_parser
= false;
60 bool glr_parser
= false;
61 bool pure_parser
= false;
63 int report_flag
= report_none
;
64 int trace_flag
= trace_none
;
65 int warnings_flag
= warnings_none
;
67 const char *skeleton
= NULL
;
68 const char *include
= NULL
;
70 extern char *program_name
;
73 /** Decode an option's set of keys.
75 * \param option option being decoded.
76 * \paran keys array of valid subarguments.
77 * \param values array of corresponding (int) values.
78 * \param flag the flags to update
79 * \param args colon separated list of effective subarguments to decode.
80 * If 0, then activate all the flags.
82 * The special value 0 resets the flags to 0.
85 flags_argmatch (const char *option
,
86 const char * const keys
[], const int values
[],
87 int *flags
, char *args
)
91 args
= strtok (args
, ",");
94 int value
= XARGMATCH (option
, args
, keys
, values
);
100 while ((args
= strtok (NULL
, ",")));
106 /** Decode a set of sub arguments.
108 * \param FlagName the flag familly to update.
109 * \param args the effective sub arguments to decode.
111 * \arg FlagName_args the list of keys.
112 * \arg FlagName_types the list of values.
113 * \arg FlagName_flag the flag to update.
115 #define FLAGS_ARGMATCH(FlagName, Args) \
116 flags_argmatch ("--" #FlagName, FlagName ## _args, FlagName ## _types, \
117 &FlagName ## _flag, Args)
120 /*----------------------.
121 | --report's handling. |
122 `----------------------*/
124 static const char * const report_args
[] =
126 /* In a series of synonyms, present the most meaningful first, so
127 that argmatch_valid be more readable. */
130 "itemset", "itemsets",
131 "lookahead", "lookaheads", "look-ahead",
137 static const int report_types
[] =
140 report_states
, report_states
,
141 report_states
| report_itemsets
, report_states
| report_itemsets
,
142 report_states
| report_lookahead_tokens
,
143 report_states
| report_lookahead_tokens
,
144 report_states
| report_lookahead_tokens
,
145 report_states
| report_solved_conflicts
,
149 ARGMATCH_VERIFY (report_args
, report_types
);
152 /*---------------------.
153 | --trace's handling. |
154 `---------------------*/
156 static const char * const trace_args
[] =
158 /* In a series of synonyms, present the most meaningful first, so
159 that argmatch_valid be more readable. */
161 "scan - grammar scanner traces",
162 "parse - grammar parser traces",
163 "automaton - construction of the automaton",
164 "bitsets - use of bitsets",
165 "grammar - reading, reducing the grammar",
166 "resource - memory consumption (where available)",
167 "sets - grammar sets: firsts, nullable etc.",
168 "tools - m4 invocation",
170 "skeleton - skeleton postprocessing",
171 "time - time consumption",
172 "all - all of the above",
176 static const int trace_types
[] =
193 ARGMATCH_VERIFY (trace_args
, trace_types
);
196 /*------------------------.
197 | --warnings's handling. |
198 `------------------------*/
200 static const char * const warnings_args
[] =
202 /* In a series of synonyms, present the most meaningful first, so
203 that argmatch_valid be more readable. */
204 "none - no warnings",
205 "error - warnings are errors",
206 "yacc - incompatibilities with POSIX YACC",
207 "all - all of the above",
211 static const int warnings_types
[] =
219 ARGMATCH_VERIFY (warnings_args
, warnings_types
);
222 /*-------------------------------------------.
223 | Display the help message and exit STATUS. |
224 `-------------------------------------------*/
226 static void usage (int) ATTRIBUTE_NORETURN
;
232 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
236 /* Some efforts were made to ease the translators' task, please
239 GNU bison generates LALR(1) and GLR parsers.\n"), stdout
);
242 fprintf (stdout
, _("\
243 Usage: %s [OPTION]... FILE\n"), program_name
);
247 If a long option shows an argument as mandatory, then it is mandatory\n\
248 for the equivalent short option also. Similarly for optional arguments.\n"),
254 -h, --help display this help and exit\n\
255 -V, --version output version information and exit\n\
256 --print-localedir output directory containing locale-dependent data\n\
257 -y, --yacc emulate POSIX Yacc\n"), stdout
);
262 -S, --skeleton=FILE specify the skeleton to use\n\
263 -t, --debug instrument the parser for debugging\n\
264 --locations enable locations computation\n\
265 -p, --name-prefix=PREFIX prepend PREFIX to the external symbols\n\
266 -l, --no-lines don't generate `#line' directives\n\
267 -n, --no-parser generate the tables only\n\
268 -k, --token-table include a table of token names\n\
274 -d, --defines also produce a header file\n\
275 -r, --report=THINGS also produce details on the automaton\n\
276 -v, --verbose same as `--report=state'\n\
277 -b, --file-prefix=PREFIX specify a PREFIX for output files\n\
278 -o, --output=FILE leave output to FILE\n\
279 -g, --graph also produce a VCG description of the automaton\n\
284 THINGS is a list of comma separated words that can include:\n\
285 `state' describe the states\n\
286 `itemset' complete the core item sets with their closure\n\
287 `lookahead' explicitly associate lookahead tokens to items\n\
288 `solved' describe shift/reduce conflicts solving\n\
289 `all' include all the above information\n\
290 `none' disable the report\n\
295 Report bugs to <" PACKAGE_BUGREPORT
">.\n"), stdout
);
302 /*------------------------------.
303 | Display the version message. |
304 `------------------------------*/
309 /* Some efforts were made to ease the translators' task, please
311 printf (_("bison (GNU Bison) %s"), VERSION
);
313 printf ("%s", revision
);
314 fputs (_("Written by Robert Corbett and Richard Stallman.\n"), stdout
);
318 _("Copyright (C) %d Free Software Foundation, Inc.\n"), 2006);
321 This is free software; see the source for copying conditions. There is NO\n\
322 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
328 /*----------------------.
329 | Process the options. |
330 `----------------------*/
332 /* Shorts options. */
333 static char const short_options
[] = "yvegdhr:ltknVo:b:p:S:T::W";
335 /* Values for long options that do not have single-letter equivalents. */
338 LOCATIONS_OPTION
= CHAR_MAX
+ 1,
339 PRINT_LOCALEDIR_OPTION
342 static struct option
const long_options
[] =
344 /* Operation modes. */
345 { "help", no_argument
, 0, 'h' },
346 { "version", no_argument
, 0, 'V' },
347 { "print-localedir", no_argument
, 0, PRINT_LOCALEDIR_OPTION
},
348 { "warnings", optional_argument
, 0, 'W' },
351 { "name-prefix", required_argument
, 0, 'p' },
352 { "include", required_argument
, 0, 'I' },
355 { "file-prefix", required_argument
, 0, 'b' },
356 { "output", required_argument
, 0, 'o' },
357 { "output-file", required_argument
, 0, 'o' },
358 { "graph", optional_argument
, 0, 'g' },
359 { "report", required_argument
, 0, 'r' },
360 { "verbose", no_argument
, 0, 'v' },
363 { "trace", optional_argument
, 0, 'T' },
366 { "defines", optional_argument
, 0, 'd' },
368 /* Operation modes. */
369 { "fixed-output-files", no_argument
, 0, 'y' },
370 { "yacc", no_argument
, 0, 'y' },
373 { "debug", no_argument
, 0, 't' },
374 { "locations", no_argument
, 0, LOCATIONS_OPTION
},
375 { "no-lines", no_argument
, 0, 'l' },
376 { "no-parser", no_argument
, 0, 'n' },
377 { "raw", no_argument
, 0, 0 },
378 { "skeleton", required_argument
, 0, 'S' },
379 { "token-table", no_argument
, 0, 'k' },
384 /* Under DOS, there is no difference on the case. This can be
385 troublesome when looking for `.tab' etc. */
387 # define AS_FILE_NAME(File) (strlwr (File), (File))
389 # define AS_FILE_NAME(File) (File)
393 getargs (int argc
, char *argv
[])
397 while ((c
= getopt_long (argc
, argv
, short_options
, long_options
, NULL
))
402 /* Certain long options cause getopt_long to return 0. */
406 spec_file_prefix
= AS_FILE_NAME (optarg
);
410 /* Here, the -g and --graph=FILE options are differentiated. */
413 spec_graph_file
= AS_FILE_NAME (optarg
);
417 usage (EXIT_SUCCESS
);
420 skeleton
= AS_FILE_NAME (optarg
);
424 include
= AS_FILE_NAME (optarg
);
428 /* Here, the -d and --defines options are differentiated. */
431 spec_defines_file
= AS_FILE_NAME (optarg
);
435 token_table_flag
= true;
439 no_lines_flag
= true;
443 no_parser_flag
= true;
447 spec_outfile
= AS_FILE_NAME (optarg
);
451 spec_name_prefix
= optarg
;
455 FLAGS_ARGMATCH (report
, optarg
);
459 FLAGS_ARGMATCH (trace
, optarg
);
471 report_flag
|= report_states
;
479 FLAGS_ARGMATCH (warnings
, optarg
);
482 case LOCATIONS_OPTION
:
483 locations_flag
= true;
486 case PRINT_LOCALEDIR_OPTION
:
487 printf ("%s\n", LOCALEDIR
);
491 usage (EXIT_FAILURE
);
494 if (argc
- optind
!= 1)
496 if (argc
- optind
< 1)
497 error (0, 0, _("missing operand after `%s'"), argv
[argc
- 1]);
499 error (0, 0, _("extra operand `%s'"), argv
[optind
+ 1]);
500 usage (EXIT_FAILURE
);
503 current_file
= grammar_file
= uniqstr_new (argv
[optind
]);