]> git.saurik.com Git - bison.git/blob - src/getargs.c
Use some of gnulib's new modules, taken from coreutils.
[bison.git] / src / getargs.c
1 /* Parse command line arguments for Bison.
2
3 Copyright (C) 1984, 1986, 1989, 1992, 2000, 2001, 2002, 2003, 2004,
4 2005, 2006 Free Software Foundation, Inc.
5
6 This file is part of Bison, the GNU Compiler Compiler.
7
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)
11 any later version.
12
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.
17
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
21 02110-1301, USA. */
22
23 #include <config.h>
24 #include "system.h"
25 #include "revision.h"
26
27 #include <argmatch.h>
28 #include <configmake.h>
29 #include <error.h>
30
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
35 #endif
36
37 #include <getopt.h>
38
39 #ifdef HACK_FOR___GNU_LIBRARY___PROTOTYPE
40 # undef __GNU_LIBRARY__
41 # undef HACK_FOR___GNU_LIBRARY___PROTOTYPE
42 #endif
43
44 #include "complain.h"
45 #include "files.h"
46 #include "getargs.h"
47 #include "uniqstr.h"
48
49 bool debug_flag;
50 bool defines_flag;
51 bool graph_flag;
52 bool locations_flag;
53 bool no_lines_flag;
54 bool no_parser_flag;
55 bool token_table_flag;
56 bool yacc_flag; /* for -y */
57
58 bool error_verbose = false;
59
60 bool nondeterministic_parser = false;
61 bool glr_parser = false;
62 bool pure_parser = false;
63 bool push_parser = false;
64
65 int report_flag = report_none;
66 int trace_flag = trace_none;
67 int warnings_flag = warnings_none;
68
69 const char *skeleton = NULL;
70 const char *include = NULL;
71
72 extern char *program_name;
73
74
75 /** Decode an option's set of keys.
76 *
77 * \param option option being decoded.
78 * \paran keys array of valid subarguments.
79 * \param values array of corresponding (int) values.
80 * \param flag the flags to update
81 * \param args colon separated list of effective subarguments to decode.
82 * If 0, then activate all the flags.
83 *
84 * The special value 0 resets the flags to 0.
85 */
86 static void
87 flags_argmatch (const char *option,
88 const char * const keys[], const int values[],
89 int *flags, char *args)
90 {
91 if (args)
92 {
93 args = strtok (args, ",");
94 do
95 {
96 int value = XARGMATCH (option, args, keys, values);
97 if (value == 0)
98 *flags = 0;
99 else
100 *flags |= value;
101 }
102 while ((args = strtok (NULL, ",")));
103 }
104 else
105 *flags = ~0;
106 }
107
108 /** Decode a set of sub arguments.
109 *
110 * \param FlagName the flag familly to update.
111 * \param args the effective sub arguments to decode.
112 *
113 * \arg FlagName_args the list of keys.
114 * \arg FlagName_types the list of values.
115 * \arg FlagName_flag the flag to update.
116 */
117 #define FLAGS_ARGMATCH(FlagName, Args) \
118 flags_argmatch ("--" #FlagName, FlagName ## _args, FlagName ## _types, \
119 &FlagName ## _flag, Args)
120
121
122 /*----------------------.
123 | --report's handling. |
124 `----------------------*/
125
126 static const char * const report_args[] =
127 {
128 /* In a series of synonyms, present the most meaningful first, so
129 that argmatch_valid be more readable. */
130 "none",
131 "state", "states",
132 "itemset", "itemsets",
133 "lookahead", "lookaheads", "look-ahead",
134 "solved",
135 "all",
136 0
137 };
138
139 static const int report_types[] =
140 {
141 report_none,
142 report_states, report_states,
143 report_states | report_itemsets, report_states | report_itemsets,
144 report_states | report_lookahead_tokens,
145 report_states | report_lookahead_tokens,
146 report_states | report_lookahead_tokens,
147 report_states | report_solved_conflicts,
148 report_all
149 };
150
151 ARGMATCH_VERIFY (report_args, report_types);
152
153
154 /*---------------------.
155 | --trace's handling. |
156 `---------------------*/
157
158 static const char * const trace_args[] =
159 {
160 /* In a series of synonyms, present the most meaningful first, so
161 that argmatch_valid be more readable. */
162 "none - no traces",
163 "scan - grammar scanner traces",
164 "parse - grammar parser traces",
165 "automaton - construction of the automaton",
166 "bitsets - use of bitsets",
167 "grammar - reading, reducing the grammar",
168 "resource - memory consumption (where available)",
169 "sets - grammar sets: firsts, nullable etc.",
170 "tools - m4 invocation",
171 "m4 - m4 traces",
172 "skeleton - skeleton postprocessing",
173 "time - time consumption",
174 "all - all of the above",
175 0
176 };
177
178 static const int trace_types[] =
179 {
180 trace_none,
181 trace_scan,
182 trace_parse,
183 trace_automaton,
184 trace_bitsets,
185 trace_grammar,
186 trace_resource,
187 trace_sets,
188 trace_tools,
189 trace_m4,
190 trace_skeleton,
191 trace_time,
192 trace_all
193 };
194
195 ARGMATCH_VERIFY (trace_args, trace_types);
196
197
198 /*------------------------.
199 | --warnings's handling. |
200 `------------------------*/
201
202 static const char * const warnings_args[] =
203 {
204 /* In a series of synonyms, present the most meaningful first, so
205 that argmatch_valid be more readable. */
206 "none - no warnings",
207 "error - warnings are errors",
208 "yacc - incompatibilities with POSIX YACC",
209 "all - all of the above",
210 0
211 };
212
213 static const int warnings_types[] =
214 {
215 warnings_none,
216 warnings_error,
217 warnings_yacc,
218 warnings_all
219 };
220
221 ARGMATCH_VERIFY (warnings_args, warnings_types);
222
223
224 /*-------------------------------------------.
225 | Display the help message and exit STATUS. |
226 `-------------------------------------------*/
227
228 static void usage (int) ATTRIBUTE_NORETURN;
229
230 static void
231 usage (int status)
232 {
233 if (status != 0)
234 fprintf (stderr, _("Try `%s --help' for more information.\n"),
235 program_name);
236 else
237 {
238 /* Some efforts were made to ease the translators' task, please
239 continue. */
240 fputs (_("\
241 GNU bison generates LALR(1) and GLR parsers.\n"), stdout);
242 putc ('\n', stdout);
243
244 fprintf (stdout, _("\
245 Usage: %s [OPTION]... FILE\n"), program_name);
246 putc ('\n', stdout);
247
248 fputs (_("\
249 If a long option shows an argument as mandatory, then it is mandatory\n\
250 for the equivalent short option also. Similarly for optional arguments.\n"),
251 stdout);
252 putc ('\n', stdout);
253
254 fputs (_("\
255 Operation modes:\n\
256 -h, --help display this help and exit\n\
257 -V, --version output version information and exit\n\
258 --print-localedir output directory containing locale-dependent data\n\
259 -y, --yacc emulate POSIX Yacc\n"), stdout);
260 putc ('\n', stdout);
261
262 fputs (_("\
263 Parser:\n\
264 -S, --skeleton=FILE specify the skeleton to use\n\
265 -t, --debug instrument the parser for debugging\n\
266 --locations enable locations computation\n\
267 -p, --name-prefix=PREFIX prepend PREFIX to the external symbols\n\
268 -l, --no-lines don't generate `#line' directives\n\
269 -n, --no-parser generate the tables only\n\
270 -k, --token-table include a table of token names\n\
271 "), stdout);
272 putc ('\n', stdout);
273
274 fputs (_("\
275 Output:\n\
276 -d, --defines also produce a header file\n\
277 -r, --report=THINGS also produce details on the automaton\n\
278 -v, --verbose same as `--report=state'\n\
279 -b, --file-prefix=PREFIX specify a PREFIX for output files\n\
280 -o, --output=FILE leave output to FILE\n\
281 -g, --graph also produce a VCG description of the automaton\n\
282 "), stdout);
283 putc ('\n', stdout);
284
285 fputs (_("\
286 THINGS is a list of comma separated words that can include:\n\
287 `state' describe the states\n\
288 `itemset' complete the core item sets with their closure\n\
289 `lookahead' explicitly associate lookahead tokens to items\n\
290 `solved' describe shift/reduce conflicts solving\n\
291 `all' include all the above information\n\
292 `none' disable the report\n\
293 "), stdout);
294 putc ('\n', stdout);
295
296 fputs (_("\
297 Report bugs to <" PACKAGE_BUGREPORT ">.\n"), stdout);
298 }
299
300 exit (status);
301 }
302
303
304 /*------------------------------.
305 | Display the version message. |
306 `------------------------------*/
307
308 static void
309 version (void)
310 {
311 /* Some efforts were made to ease the translators' task, please
312 continue. */
313 printf (_("bison (GNU Bison) %s"), VERSION);
314 putc ('\n', stdout);
315 printf ("%s", revision);
316 fputs (_("Written by Robert Corbett and Richard Stallman.\n"), stdout);
317 putc ('\n', stdout);
318
319 fprintf (stdout,
320 _("Copyright (C) %d Free Software Foundation, Inc.\n"), 2006);
321
322 fputs (_("\
323 This is free software; see the source for copying conditions. There is NO\n\
324 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
325 "),
326 stdout);
327 }
328
329
330 /*----------------------.
331 | Process the options. |
332 `----------------------*/
333
334 /* Shorts options. */
335 static char const short_options[] = "yvegdhr:ltknVo:b:p:S:T::W";
336
337 /* Values for long options that do not have single-letter equivalents. */
338 enum
339 {
340 LOCATIONS_OPTION = CHAR_MAX + 1,
341 PRINT_LOCALEDIR_OPTION
342 };
343
344 static struct option const long_options[] =
345 {
346 /* Operation modes. */
347 { "help", no_argument, 0, 'h' },
348 { "version", no_argument, 0, 'V' },
349 { "print-localedir", no_argument, 0, PRINT_LOCALEDIR_OPTION },
350 { "warnings", optional_argument, 0, 'W' },
351
352 /* Parser. */
353 { "name-prefix", required_argument, 0, 'p' },
354 { "include", required_argument, 0, 'I' },
355
356 /* Output. */
357 { "file-prefix", required_argument, 0, 'b' },
358 { "output", required_argument, 0, 'o' },
359 { "output-file", required_argument, 0, 'o' },
360 { "graph", optional_argument, 0, 'g' },
361 { "report", required_argument, 0, 'r' },
362 { "verbose", no_argument, 0, 'v' },
363
364 /* Hidden. */
365 { "trace", optional_argument, 0, 'T' },
366
367 /* Output. */
368 { "defines", optional_argument, 0, 'd' },
369
370 /* Operation modes. */
371 { "fixed-output-files", no_argument, 0, 'y' },
372 { "yacc", no_argument, 0, 'y' },
373
374 /* Parser. */
375 { "debug", no_argument, 0, 't' },
376 { "locations", no_argument, 0, LOCATIONS_OPTION },
377 { "no-lines", no_argument, 0, 'l' },
378 { "no-parser", no_argument, 0, 'n' },
379 { "raw", no_argument, 0, 0 },
380 { "skeleton", required_argument, 0, 'S' },
381 { "token-table", no_argument, 0, 'k' },
382
383 {0, 0, 0, 0}
384 };
385
386 /* Under DOS, there is no difference on the case. This can be
387 troublesome when looking for `.tab' etc. */
388 #ifdef MSDOS
389 # define AS_FILE_NAME(File) (strlwr (File), (File))
390 #else
391 # define AS_FILE_NAME(File) (File)
392 #endif
393
394 void
395 getargs (int argc, char *argv[])
396 {
397 int c;
398
399 while ((c = getopt_long (argc, argv, short_options, long_options, NULL))
400 != -1)
401 switch (c)
402 {
403 case 0:
404 /* Certain long options cause getopt_long to return 0. */
405 break;
406
407 case 'b':
408 spec_file_prefix = AS_FILE_NAME (optarg);
409 break;
410
411 case 'g':
412 /* Here, the -g and --graph=FILE options are differentiated. */
413 graph_flag = true;
414 if (optarg)
415 spec_graph_file = AS_FILE_NAME (optarg);
416 break;
417
418 case 'h':
419 usage (EXIT_SUCCESS);
420
421 case 'S':
422 skeleton = AS_FILE_NAME (optarg);
423 break;
424
425 case 'I':
426 include = AS_FILE_NAME (optarg);
427 break;
428
429 case 'd':
430 /* Here, the -d and --defines options are differentiated. */
431 defines_flag = true;
432 if (optarg)
433 spec_defines_file = AS_FILE_NAME (optarg);
434 break;
435
436 case 'k':
437 token_table_flag = true;
438 break;
439
440 case 'l':
441 no_lines_flag = true;
442 break;
443
444 case 'n':
445 no_parser_flag = true;
446 break;
447
448 case 'o':
449 spec_outfile = AS_FILE_NAME (optarg);
450 break;
451
452 case 'p':
453 spec_name_prefix = optarg;
454 break;
455
456 case 'r':
457 FLAGS_ARGMATCH (report, optarg);
458 break;
459
460 case 'T':
461 FLAGS_ARGMATCH (trace, optarg);
462 break;
463
464 case 't':
465 debug_flag = true;
466 break;
467
468 case 'V':
469 version ();
470 exit (EXIT_SUCCESS);
471
472 case 'v':
473 report_flag |= report_states;
474 break;
475
476 case 'y':
477 yacc_flag = true;
478 break;
479
480 case 'W':
481 FLAGS_ARGMATCH (warnings, optarg);
482 break;
483
484 case LOCATIONS_OPTION:
485 locations_flag = true;
486 break;
487
488 case PRINT_LOCALEDIR_OPTION:
489 printf ("%s\n", LOCALEDIR);
490 exit (EXIT_SUCCESS);
491
492 default:
493 usage (EXIT_FAILURE);
494 }
495
496 if (argc - optind != 1)
497 {
498 if (argc - optind < 1)
499 error (0, 0, _("missing operand after `%s'"), argv[argc - 1]);
500 else
501 error (0, 0, _("extra operand `%s'"), argv[optind + 1]);
502 usage (EXIT_FAILURE);
503 }
504
505 current_file = grammar_file = uniqstr_new (argv[optind]);
506 }