]> git.saurik.com Git - bison.git/blob - src/getargs.c
* data/Makefile.am (dist_pkgdata_DATA): Add push.c.
[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 <error.h>
29
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
34 #endif
35
36 #include <getopt.h>
37
38 #ifdef HACK_FOR___GNU_LIBRARY___PROTOTYPE
39 # undef __GNU_LIBRARY__
40 # undef HACK_FOR___GNU_LIBRARY___PROTOTYPE
41 #endif
42
43 #include "complain.h"
44 #include "files.h"
45 #include "getargs.h"
46 #include "uniqstr.h"
47
48 bool debug_flag;
49 bool defines_flag;
50 bool graph_flag;
51 bool locations_flag;
52 bool no_lines_flag;
53 bool no_parser_flag;
54 bool token_table_flag;
55 bool yacc_flag; /* for -y */
56
57 bool error_verbose = false;
58
59 bool nondeterministic_parser = false;
60 bool glr_parser = false;
61 bool pure_parser = false;
62 bool push_parser = false;
63
64 int report_flag = report_none;
65 int trace_flag = trace_none;
66 int warnings_flag = warnings_none;
67
68 const char *skeleton = NULL;
69 const char *include = NULL;
70
71 extern char *program_name;
72
73
74 /** Decode an option's set of keys.
75 *
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.
82 *
83 * The special value 0 resets the flags to 0.
84 */
85 static void
86 flags_argmatch (const char *option,
87 const char * const keys[], const int values[],
88 int *flags, char *args)
89 {
90 if (args)
91 {
92 args = strtok (args, ",");
93 do
94 {
95 int value = XARGMATCH (option, args, keys, values);
96 if (value == 0)
97 *flags = 0;
98 else
99 *flags |= value;
100 }
101 while ((args = strtok (NULL, ",")));
102 }
103 else
104 *flags = ~0;
105 }
106
107 /** Decode a set of sub arguments.
108 *
109 * \param FlagName the flag familly to update.
110 * \param args the effective sub arguments to decode.
111 *
112 * \arg FlagName_args the list of keys.
113 * \arg FlagName_types the list of values.
114 * \arg FlagName_flag the flag to update.
115 */
116 #define FLAGS_ARGMATCH(FlagName, Args) \
117 flags_argmatch ("--" #FlagName, FlagName ## _args, FlagName ## _types, \
118 &FlagName ## _flag, Args)
119
120
121 /*----------------------.
122 | --report's handling. |
123 `----------------------*/
124
125 static const char * const report_args[] =
126 {
127 /* In a series of synonyms, present the most meaningful first, so
128 that argmatch_valid be more readable. */
129 "none",
130 "state", "states",
131 "itemset", "itemsets",
132 "lookahead", "lookaheads", "look-ahead",
133 "solved",
134 "all",
135 0
136 };
137
138 static const int report_types[] =
139 {
140 report_none,
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,
147 report_all
148 };
149
150 ARGMATCH_VERIFY (report_args, report_types);
151
152
153 /*---------------------.
154 | --trace's handling. |
155 `---------------------*/
156
157 static const char * const trace_args[] =
158 {
159 /* In a series of synonyms, present the most meaningful first, so
160 that argmatch_valid be more readable. */
161 "none - no traces",
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",
170 "m4 - m4 traces",
171 "skeleton - skeleton postprocessing",
172 "time - time consumption",
173 "all - all of the above",
174 0
175 };
176
177 static const int trace_types[] =
178 {
179 trace_none,
180 trace_scan,
181 trace_parse,
182 trace_automaton,
183 trace_bitsets,
184 trace_grammar,
185 trace_resource,
186 trace_sets,
187 trace_tools,
188 trace_m4,
189 trace_skeleton,
190 trace_time,
191 trace_all
192 };
193
194 ARGMATCH_VERIFY (trace_args, trace_types);
195
196
197 /*------------------------.
198 | --warnings's handling. |
199 `------------------------*/
200
201 static const char * const warnings_args[] =
202 {
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",
209 0
210 };
211
212 static const int warnings_types[] =
213 {
214 warnings_none,
215 warnings_error,
216 warnings_yacc,
217 warnings_all
218 };
219
220 ARGMATCH_VERIFY (warnings_args, warnings_types);
221
222
223 /*-------------------------------------------.
224 | Display the help message and exit STATUS. |
225 `-------------------------------------------*/
226
227 static void usage (int) ATTRIBUTE_NORETURN;
228
229 static void
230 usage (int status)
231 {
232 if (status != 0)
233 fprintf (stderr, _("Try `%s --help' for more information.\n"),
234 program_name);
235 else
236 {
237 /* Some efforts were made to ease the translators' task, please
238 continue. */
239 fputs (_("\
240 GNU bison generates LALR(1) and GLR parsers.\n"), stdout);
241 putc ('\n', stdout);
242
243 fprintf (stdout, _("\
244 Usage: %s [OPTION]... FILE\n"), program_name);
245 putc ('\n', stdout);
246
247 fputs (_("\
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"),
250 stdout);
251 putc ('\n', stdout);
252
253 fputs (_("\
254 Operation modes:\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);
259 putc ('\n', stdout);
260
261 fputs (_("\
262 Parser:\n\
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\
270 "), stdout);
271 putc ('\n', stdout);
272
273 fputs (_("\
274 Output:\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\
281 "), stdout);
282 putc ('\n', stdout);
283
284 fputs (_("\
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\
292 "), stdout);
293 putc ('\n', stdout);
294
295 fputs (_("\
296 Report bugs to <" PACKAGE_BUGREPORT ">.\n"), stdout);
297 }
298
299 exit (status);
300 }
301
302
303 /*------------------------------.
304 | Display the version message. |
305 `------------------------------*/
306
307 static void
308 version (void)
309 {
310 /* Some efforts were made to ease the translators' task, please
311 continue. */
312 printf (_("bison (GNU Bison) %s"), VERSION);
313 putc ('\n', stdout);
314 printf ("%s", revision);
315 fputs (_("Written by Robert Corbett and Richard Stallman.\n"), stdout);
316 putc ('\n', stdout);
317
318 fprintf (stdout,
319 _("Copyright (C) %d Free Software Foundation, Inc.\n"), 2006);
320
321 fputs (_("\
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\
324 "),
325 stdout);
326 }
327
328
329 /*----------------------.
330 | Process the options. |
331 `----------------------*/
332
333 /* Shorts options. */
334 static char const short_options[] = "yvegdhr:ltknVo:b:p:S:T::W";
335
336 /* Values for long options that do not have single-letter equivalents. */
337 enum
338 {
339 LOCATIONS_OPTION = CHAR_MAX + 1,
340 PRINT_LOCALEDIR_OPTION
341 };
342
343 static struct option const long_options[] =
344 {
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' },
350
351 /* Parser. */
352 { "name-prefix", required_argument, 0, 'p' },
353 { "include", required_argument, 0, 'I' },
354
355 /* Output. */
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' },
362
363 /* Hidden. */
364 { "trace", optional_argument, 0, 'T' },
365
366 /* Output. */
367 { "defines", optional_argument, 0, 'd' },
368
369 /* Operation modes. */
370 { "fixed-output-files", no_argument, 0, 'y' },
371 { "yacc", no_argument, 0, 'y' },
372
373 /* Parser. */
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' },
381
382 {0, 0, 0, 0}
383 };
384
385 /* Under DOS, there is no difference on the case. This can be
386 troublesome when looking for `.tab' etc. */
387 #ifdef MSDOS
388 # define AS_FILE_NAME(File) (strlwr (File), (File))
389 #else
390 # define AS_FILE_NAME(File) (File)
391 #endif
392
393 void
394 getargs (int argc, char *argv[])
395 {
396 int c;
397
398 while ((c = getopt_long (argc, argv, short_options, long_options, NULL))
399 != -1)
400 switch (c)
401 {
402 case 0:
403 /* Certain long options cause getopt_long to return 0. */
404 break;
405
406 case 'b':
407 spec_file_prefix = AS_FILE_NAME (optarg);
408 break;
409
410 case 'g':
411 /* Here, the -g and --graph=FILE options are differentiated. */
412 graph_flag = true;
413 if (optarg)
414 spec_graph_file = AS_FILE_NAME (optarg);
415 break;
416
417 case 'h':
418 usage (EXIT_SUCCESS);
419
420 case 'S':
421 skeleton = AS_FILE_NAME (optarg);
422 break;
423
424 case 'I':
425 include = AS_FILE_NAME (optarg);
426 break;
427
428 case 'd':
429 /* Here, the -d and --defines options are differentiated. */
430 defines_flag = true;
431 if (optarg)
432 spec_defines_file = AS_FILE_NAME (optarg);
433 break;
434
435 case 'k':
436 token_table_flag = true;
437 break;
438
439 case 'l':
440 no_lines_flag = true;
441 break;
442
443 case 'n':
444 no_parser_flag = true;
445 break;
446
447 case 'o':
448 spec_outfile = AS_FILE_NAME (optarg);
449 break;
450
451 case 'p':
452 spec_name_prefix = optarg;
453 break;
454
455 case 'r':
456 FLAGS_ARGMATCH (report, optarg);
457 break;
458
459 case 'T':
460 FLAGS_ARGMATCH (trace, optarg);
461 break;
462
463 case 't':
464 debug_flag = true;
465 break;
466
467 case 'V':
468 version ();
469 exit (EXIT_SUCCESS);
470
471 case 'v':
472 report_flag |= report_states;
473 break;
474
475 case 'y':
476 yacc_flag = true;
477 break;
478
479 case 'W':
480 FLAGS_ARGMATCH (warnings, optarg);
481 break;
482
483 case LOCATIONS_OPTION:
484 locations_flag = true;
485 break;
486
487 case PRINT_LOCALEDIR_OPTION:
488 printf ("%s\n", LOCALEDIR);
489 exit (EXIT_SUCCESS);
490
491 default:
492 usage (EXIT_FAILURE);
493 }
494
495 if (argc - optind != 1)
496 {
497 if (argc - optind < 1)
498 error (0, 0, _("missing operand after `%s'"), argv[argc - 1]);
499 else
500 error (0, 0, _("extra operand `%s'"), argv[optind + 1]);
501 usage (EXIT_FAILURE);
502 }
503
504 current_file = grammar_file = uniqstr_new (argv[optind]);
505 }