]>
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;
62 bool push_parser
= false;
64 int report_flag
= report_none
;
65 int trace_flag
= trace_none
;
66 int warnings_flag
= warnings_none
;
68 const char *skeleton
= NULL
;
69 const char *include
= NULL
;
71 extern char *program_name
;
74 /** Decode an option's set of keys.
76 * \param option option being decoded.
77 * \paran keys array of valid subarguments.
78 * \param values array of corresponding (int) values.
79 * \param flag the flags to update
80 * \param args colon separated list of effective subarguments to decode.
81 * If 0, then activate all the flags.
83 * The special value 0 resets the flags to 0.
86 flags_argmatch (const char *option
,
87 const char * const keys
[], const int values
[],
88 int *flags
, char *args
)
92 args
= strtok (args
, ",");
95 int value
= XARGMATCH (option
, args
, keys
, values
);
101 while ((args
= strtok (NULL
, ",")));
107 /** Decode a set of sub arguments.
109 * \param FlagName the flag familly to update.
110 * \param args the effective sub arguments to decode.
112 * \arg FlagName_args the list of keys.
113 * \arg FlagName_types the list of values.
114 * \arg FlagName_flag the flag to update.
116 #define FLAGS_ARGMATCH(FlagName, Args) \
117 flags_argmatch ("--" #FlagName, FlagName ## _args, FlagName ## _types, \
118 &FlagName ## _flag, Args)
121 /*----------------------.
122 | --report's handling. |
123 `----------------------*/
125 static const char * const report_args
[] =
127 /* In a series of synonyms, present the most meaningful first, so
128 that argmatch_valid be more readable. */
131 "itemset", "itemsets",
132 "lookahead", "lookaheads", "look-ahead",
138 static const int report_types
[] =
141 report_states
, report_states
,
142 report_states
| report_itemsets
, report_states
| report_itemsets
,
143 report_states
| report_lookahead_tokens
,
144 report_states
| report_lookahead_tokens
,
145 report_states
| report_lookahead_tokens
,
146 report_states
| report_solved_conflicts
,
150 ARGMATCH_VERIFY (report_args
, report_types
);
153 /*---------------------.
154 | --trace's handling. |
155 `---------------------*/
157 static const char * const trace_args
[] =
159 /* In a series of synonyms, present the most meaningful first, so
160 that argmatch_valid be more readable. */
162 "scan - grammar scanner traces",
163 "parse - grammar parser traces",
164 "automaton - construction of the automaton",
165 "bitsets - use of bitsets",
166 "grammar - reading, reducing the grammar",
167 "resource - memory consumption (where available)",
168 "sets - grammar sets: firsts, nullable etc.",
169 "tools - m4 invocation",
171 "skeleton - skeleton postprocessing",
172 "time - time consumption",
173 "all - all of the above",
177 static const int trace_types
[] =
194 ARGMATCH_VERIFY (trace_args
, trace_types
);
197 /*------------------------.
198 | --warnings's handling. |
199 `------------------------*/
201 static const char * const warnings_args
[] =
203 /* In a series of synonyms, present the most meaningful first, so
204 that argmatch_valid be more readable. */
205 "none - no warnings",
206 "error - warnings are errors",
207 "yacc - incompatibilities with POSIX YACC",
208 "all - all of the above",
212 static const int warnings_types
[] =
220 ARGMATCH_VERIFY (warnings_args
, warnings_types
);
223 /*-------------------------------------------.
224 | Display the help message and exit STATUS. |
225 `-------------------------------------------*/
227 static void usage (int) ATTRIBUTE_NORETURN
;
233 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
237 /* Some efforts were made to ease the translators' task, please
240 GNU bison generates LALR(1) and GLR parsers.\n"), stdout
);
243 fprintf (stdout
, _("\
244 Usage: %s [OPTION]... FILE\n"), program_name
);
248 If a long option shows an argument as mandatory, then it is mandatory\n\
249 for the equivalent short option also. Similarly for optional arguments.\n"),
255 -h, --help display this help and exit\n\
256 -V, --version output version information and exit\n\
257 --print-localedir output directory containing locale-dependent data\n\
258 -y, --yacc emulate POSIX Yacc\n"), stdout
);
263 -S, --skeleton=FILE specify the skeleton to use\n\
264 -t, --debug instrument the parser for debugging\n\
265 --locations enable locations computation\n\
266 -p, --name-prefix=PREFIX prepend PREFIX to the external symbols\n\
267 -l, --no-lines don't generate `#line' directives\n\
268 -n, --no-parser generate the tables only\n\
269 -k, --token-table include a table of token names\n\
275 -d, --defines also produce a header file\n\
276 -r, --report=THINGS also produce details on the automaton\n\
277 -v, --verbose same as `--report=state'\n\
278 -b, --file-prefix=PREFIX specify a PREFIX for output files\n\
279 -o, --output=FILE leave output to FILE\n\
280 -g, --graph also produce a VCG description of the automaton\n\
285 THINGS is a list of comma separated words that can include:\n\
286 `state' describe the states\n\
287 `itemset' complete the core item sets with their closure\n\
288 `lookahead' explicitly associate lookahead tokens to items\n\
289 `solved' describe shift/reduce conflicts solving\n\
290 `all' include all the above information\n\
291 `none' disable the report\n\
296 Report bugs to <" PACKAGE_BUGREPORT
">.\n"), stdout
);
303 /*------------------------------.
304 | Display the version message. |
305 `------------------------------*/
310 /* Some efforts were made to ease the translators' task, please
312 printf (_("bison (GNU Bison) %s"), VERSION
);
314 printf ("%s", revision
);
315 fputs (_("Written by Robert Corbett and Richard Stallman.\n"), stdout
);
319 _("Copyright (C) %d Free Software Foundation, Inc.\n"), 2006);
322 This is free software; see the source for copying conditions. There is NO\n\
323 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
329 /*----------------------.
330 | Process the options. |
331 `----------------------*/
333 /* Shorts options. */
334 static char const short_options
[] = "yvegdhr:ltknVo:b:p:S:T::W";
336 /* Values for long options that do not have single-letter equivalents. */
339 LOCATIONS_OPTION
= CHAR_MAX
+ 1,
340 PRINT_LOCALEDIR_OPTION
343 static struct option
const long_options
[] =
345 /* Operation modes. */
346 { "help", no_argument
, 0, 'h' },
347 { "version", no_argument
, 0, 'V' },
348 { "print-localedir", no_argument
, 0, PRINT_LOCALEDIR_OPTION
},
349 { "warnings", optional_argument
, 0, 'W' },
352 { "name-prefix", required_argument
, 0, 'p' },
353 { "include", required_argument
, 0, 'I' },
356 { "file-prefix", required_argument
, 0, 'b' },
357 { "output", required_argument
, 0, 'o' },
358 { "output-file", required_argument
, 0, 'o' },
359 { "graph", optional_argument
, 0, 'g' },
360 { "report", required_argument
, 0, 'r' },
361 { "verbose", no_argument
, 0, 'v' },
364 { "trace", optional_argument
, 0, 'T' },
367 { "defines", optional_argument
, 0, 'd' },
369 /* Operation modes. */
370 { "fixed-output-files", no_argument
, 0, 'y' },
371 { "yacc", no_argument
, 0, 'y' },
374 { "debug", no_argument
, 0, 't' },
375 { "locations", no_argument
, 0, LOCATIONS_OPTION
},
376 { "no-lines", no_argument
, 0, 'l' },
377 { "no-parser", no_argument
, 0, 'n' },
378 { "raw", no_argument
, 0, 0 },
379 { "skeleton", required_argument
, 0, 'S' },
380 { "token-table", no_argument
, 0, 'k' },
385 /* Under DOS, there is no difference on the case. This can be
386 troublesome when looking for `.tab' etc. */
388 # define AS_FILE_NAME(File) (strlwr (File), (File))
390 # define AS_FILE_NAME(File) (File)
394 getargs (int argc
, char *argv
[])
398 while ((c
= getopt_long (argc
, argv
, short_options
, long_options
, NULL
))
403 /* Certain long options cause getopt_long to return 0. */
407 spec_file_prefix
= AS_FILE_NAME (optarg
);
411 /* Here, the -g and --graph=FILE options are differentiated. */
414 spec_graph_file
= AS_FILE_NAME (optarg
);
418 usage (EXIT_SUCCESS
);
421 skeleton
= AS_FILE_NAME (optarg
);
425 include
= AS_FILE_NAME (optarg
);
429 /* Here, the -d and --defines options are differentiated. */
432 spec_defines_file
= AS_FILE_NAME (optarg
);
436 token_table_flag
= true;
440 no_lines_flag
= true;
444 no_parser_flag
= true;
448 spec_outfile
= AS_FILE_NAME (optarg
);
452 spec_name_prefix
= optarg
;
456 FLAGS_ARGMATCH (report
, optarg
);
460 FLAGS_ARGMATCH (trace
, optarg
);
472 report_flag
|= report_states
;
480 FLAGS_ARGMATCH (warnings
, optarg
);
483 case LOCATIONS_OPTION
:
484 locations_flag
= true;
487 case PRINT_LOCALEDIR_OPTION
:
488 printf ("%s\n", LOCALEDIR
);
492 usage (EXIT_FAILURE
);
495 if (argc
- optind
!= 1)
497 if (argc
- optind
< 1)
498 error (0, 0, _("missing operand after `%s'"), argv
[argc
- 1]);
500 error (0, 0, _("extra operand `%s'"), argv
[optind
+ 1]);
501 usage (EXIT_FAILURE
);
504 current_file
= grammar_file
= uniqstr_new (argv
[optind
]);