]> git.saurik.com Git - bison.git/blob - src/getargs.c
Use git-version-gen for version names between releases.
[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, 2007, 2008 Free Software Foundation, Inc.
5
6 This file is part of Bison, the GNU Compiler Compiler.
7
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.
12
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.
17
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/>. */
20
21 #include <config.h>
22 #include "system.h"
23 #include "output.h"
24
25 #include <argmatch.h>
26 #include <c-strcase.h>
27 #include <configmake.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 xml_flag;
52 bool locations_flag;
53 bool no_lines_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
62 int report_flag = report_none;
63 int trace_flag = trace_none;
64 int warnings_flag = warnings_none;
65
66 static struct bison_language const valid_languages[] = {
67 { "c", "c-skel.m4", ".c", ".h", true },
68 { "c++", "c++-skel.m4", ".cc", ".hh", true },
69 { "java", "java-skel.m4", ".java", ".java", false },
70 { "", "", "", "", false }
71 };
72
73 static int skeleton_prio = 2;
74 const char *skeleton = NULL;
75 static int language_prio = 2;
76 struct bison_language const *language = &valid_languages[0];
77 const char *include = NULL;
78
79 char *program_name;
80
81
82 /** Decode an option's set of keys.
83 *
84 * \param option option being decoded.
85 * \param keys array of valid subarguments.
86 * \param values array of corresponding (int) values.
87 * \param flags the flags to update
88 * \param args colon separated list of effective subarguments to decode.
89 * If 0, then activate all the flags.
90 *
91 * The special value 0 resets the flags to 0.
92 */
93 static void
94 flags_argmatch (const char *option,
95 const char * const keys[], const int values[],
96 int *flags, char *args)
97 {
98 if (args)
99 {
100 args = strtok (args, ",");
101 while (args)
102 {
103 int value = XARGMATCH (option, args, keys, values);
104 if (value == 0)
105 *flags = 0;
106 else
107 *flags |= value;
108 args = strtok (NULL, ",");
109 }
110 }
111 else
112 *flags = ~0;
113 }
114
115 /** Decode a set of sub arguments.
116 *
117 * \param FlagName the flag familly to update.
118 * \param Args the effective sub arguments to decode.
119 *
120 * \arg FlagName_args the list of keys.
121 * \arg FlagName_types the list of values.
122 * \arg FlagName_flag the flag to update.
123 */
124 #define FLAGS_ARGMATCH(FlagName, Args) \
125 flags_argmatch ("--" #FlagName, FlagName ## _args, FlagName ## _types, \
126 &FlagName ## _flag, Args)
127
128
129 /*----------------------.
130 | --report's handling. |
131 `----------------------*/
132
133 static const char * const report_args[] =
134 {
135 /* In a series of synonyms, present the most meaningful first, so
136 that argmatch_valid be more readable. */
137 "none",
138 "state", "states",
139 "itemset", "itemsets",
140 "lookahead", "lookaheads", "look-ahead",
141 "solved",
142 "all",
143 0
144 };
145
146 static const int report_types[] =
147 {
148 report_none,
149 report_states, report_states,
150 report_states | report_itemsets, report_states | report_itemsets,
151 report_states | report_lookahead_tokens,
152 report_states | report_lookahead_tokens,
153 report_states | report_lookahead_tokens,
154 report_states | report_solved_conflicts,
155 report_all
156 };
157
158 ARGMATCH_VERIFY (report_args, report_types);
159
160
161 /*---------------------.
162 | --trace's handling. |
163 `---------------------*/
164
165 static const char * const trace_args[] =
166 {
167 /* In a series of synonyms, present the most meaningful first, so
168 that argmatch_valid be more readable. */
169 "none - no traces",
170 "scan - grammar scanner traces",
171 "parse - grammar parser traces",
172 "automaton - construction of the automaton",
173 "bitsets - use of bitsets",
174 "grammar - reading, reducing the grammar",
175 "resource - memory consumption (where available)",
176 "sets - grammar sets: firsts, nullable etc.",
177 "tools - m4 invocation",
178 "m4 - m4 traces",
179 "skeleton - skeleton postprocessing",
180 "time - time consumption",
181 "all - all of the above",
182 0
183 };
184
185 static const int trace_types[] =
186 {
187 trace_none,
188 trace_scan,
189 trace_parse,
190 trace_automaton,
191 trace_bitsets,
192 trace_grammar,
193 trace_resource,
194 trace_sets,
195 trace_tools,
196 trace_m4,
197 trace_skeleton,
198 trace_time,
199 trace_all
200 };
201
202 ARGMATCH_VERIFY (trace_args, trace_types);
203
204
205 /*------------------------.
206 | --warnings's handling. |
207 `------------------------*/
208
209 static const char * const warnings_args[] =
210 {
211 /* In a series of synonyms, present the most meaningful first, so
212 that argmatch_valid be more readable. */
213 "none - no warnings",
214 "midrule-values - unset or unused midrule values",
215 "yacc - incompatibilities with POSIX YACC",
216 "all - all of the above",
217 "error - warnings are errors",
218 0
219 };
220
221 static const int warnings_types[] =
222 {
223 warnings_none,
224 warnings_midrule_values,
225 warnings_yacc,
226 warnings_all,
227 warnings_error
228 };
229
230 ARGMATCH_VERIFY (warnings_args, warnings_types);
231
232
233 /*-------------------------------------------.
234 | Display the help message and exit STATUS. |
235 `-------------------------------------------*/
236
237 static void usage (int) ATTRIBUTE_NORETURN;
238
239 static void
240 usage (int status)
241 {
242 if (status != 0)
243 fprintf (stderr, _("Try `%s --help' for more information.\n"),
244 program_name);
245 else
246 {
247 printf (_("Usage: %s [OPTION]... FILE\n"), program_name);
248 fputs (_("\
249 Generate LALR(1) and GLR parsers.\n\
250 \n\
251 "), stdout);
252
253 fputs (_("\
254 Mandatory arguments to long options are mandatory for short options too.\n\
255 "), stdout);
256 fputs (_("\
257 The same is true for optional arguments.\n\
258 "), stdout);
259
260 fputs (_("\
261 \n\
262 Operation modes:\n\
263 -h, --help display this help and exit\n\
264 -V, --version output version information and exit\n\
265 --print-localedir output directory containing locale-dependent data\n\
266 --print-datadir output directory containing skeletons and XSLT\n\
267 -y, --yacc emulate POSIX Yacc\n\
268 -W, --warnings=[CATEGORY] report the warnings falling in CATEGORY\n\
269 \n\
270 "), stdout);
271
272 fputs (_("\
273 Parser:\n\
274 -L, --language=LANGUAGE specify the output programming language\n\
275 -S, --skeleton=FILE specify the skeleton to use\n\
276 -t, --debug instrument the parser for debugging\n\
277 --locations enable locations computation\n\
278 -p, --name-prefix=PREFIX prepend PREFIX to the external symbols\n\
279 -l, --no-lines don't generate `#line' directives\n\
280 -k, --token-table include a table of token names\n\
281 \n\
282 "), stdout);
283
284 /* Keep -d and --defines separate so that ../build-aux/cross-options.pl
285 * won't assume that -d also takes an argument. */
286 fputs (_("\
287 Output:\n\
288 --defines[=FILE] also produce a header file\n\
289 -d likewise but cannot specify FILE (for POSIX Yacc)\n\
290 -r, --report=THINGS also produce details on the automaton\n\
291 --report-file=FILE write report to FILE\n\
292 -v, --verbose same as `--report=state'\n\
293 -b, --file-prefix=PREFIX specify a PREFIX for output files\n\
294 -o, --output=FILE leave output to FILE\n\
295 -g, --graph[=FILE] also output a graph of the automaton\n\
296 -x, --xml[=FILE] also output an XML report of the automaton\n\
297 (the XML schema is experimental)\n\
298 \n\
299 "), stdout);
300
301 fputs (_("\
302 Warning categories include:\n\
303 `midrule-values' unset or unused midrule values\n\
304 `yacc' incompatibilities with POSIX YACC\n\
305 `all' all the warnings\n\
306 `no-CATEGORY' turn off warnings in CATEGORY\n\
307 `none' turn off all the warnings\n\
308 `error' treat warnings as errors\n\
309 \n\
310 "), stdout);
311
312 fputs (_("\
313 THINGS is a list of comma separated words that can include:\n\
314 `state' describe the states\n\
315 `itemset' complete the core item sets with their closure\n\
316 `lookahead' explicitly associate lookahead tokens to items\n\
317 `solved' describe shift/reduce conflicts solving\n\
318 `all' include all the above information\n\
319 `none' disable the report\n\
320 "), stdout);
321
322 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
323 }
324
325 exit (status);
326 }
327
328
329 /*------------------------------.
330 | Display the version message. |
331 `------------------------------*/
332
333 static void
334 version (void)
335 {
336 /* Some efforts were made to ease the translators' task, please
337 continue. */
338 printf (_("bison (GNU Bison) %s"), VERSION);
339 putc ('\n', stdout);
340 fputs (_("Written by Robert Corbett and Richard Stallman.\n"), stdout);
341 putc ('\n', stdout);
342
343 fprintf (stdout,
344 _("Copyright (C) %d Free Software Foundation, Inc.\n"),
345 PACKAGE_COPYRIGHT_YEAR);
346
347 fputs (_("\
348 This is free software; see the source for copying conditions. There is NO\n\
349 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
350 "),
351 stdout);
352 }
353
354
355 /*-------------------------------------.
356 | --skeleton and --language handling. |
357 `--------------------------------------*/
358
359 void
360 skeleton_arg (char const *arg, int prio, location const *loc)
361 {
362 if (prio < skeleton_prio)
363 {
364 skeleton_prio = prio;
365 skeleton = arg;
366 }
367 else if (prio == skeleton_prio)
368 {
369 char const *msg =
370 _("multiple skeleton declarations are invalid");
371 if (loc)
372 complain_at (*loc, msg);
373 else
374 complain (msg);
375 }
376 }
377
378 void
379 language_argmatch (char const *arg, int prio, location const *loc)
380 {
381 char const *msg;
382
383 if (prio < language_prio)
384 {
385 int i;
386 for (i = 0; valid_languages[i].language[0]; i++)
387 if (c_strcasecmp (arg, valid_languages[i].language) == 0)
388 {
389 language_prio = prio;
390 language = &valid_languages[i];
391 return;
392 }
393 msg = _("invalid language `%s'");
394 }
395 else if (language_prio == prio)
396 msg = _("multiple language declarations are invalid");
397 else
398 return;
399
400 if (loc)
401 complain_at (*loc, msg, arg);
402 else
403 complain (msg, arg);
404 }
405
406 /*----------------------.
407 | Process the options. |
408 `----------------------*/
409
410 /* Shorts options.
411 Should be computed from long_options. */
412 static char const short_options[] =
413 "L:"
414 "S:"
415 "T::"
416 "V"
417 "W::"
418 "b:"
419 "d"
420 "e"
421 "g::"
422 "h"
423 "k"
424 "l"
425 "n"
426 "o:"
427 "p:"
428 "r:"
429 "t"
430 "v"
431 "x::"
432 "y"
433 ;
434
435 /* Values for long options that do not have single-letter equivalents. */
436 enum
437 {
438 LOCATIONS_OPTION = CHAR_MAX + 1,
439 PRINT_LOCALEDIR_OPTION,
440 PRINT_DATADIR_OPTION,
441 REPORT_FILE_OPTION
442 };
443
444 static struct option const long_options[] =
445 {
446 /* Operation modes. */
447 { "help", no_argument, 0, 'h' },
448 { "version", no_argument, 0, 'V' },
449 { "print-localedir", no_argument, 0, PRINT_LOCALEDIR_OPTION },
450 { "print-datadir", no_argument, 0, PRINT_DATADIR_OPTION },
451 { "warnings", optional_argument, 0, 'W' },
452
453 /* Parser. */
454 { "name-prefix", required_argument, 0, 'p' },
455 { "include", required_argument, 0, 'I' },
456
457 /* Output. */
458 { "file-prefix", required_argument, 0, 'b' },
459 { "output", required_argument, 0, 'o' },
460 { "output-file", required_argument, 0, 'o' },
461 { "graph", optional_argument, 0, 'g' },
462 { "xml", optional_argument, 0, 'x' },
463 { "report", required_argument, 0, 'r' },
464 { "report-file", required_argument, 0, REPORT_FILE_OPTION },
465 { "verbose", no_argument, 0, 'v' },
466
467 /* Hidden. */
468 { "trace", optional_argument, 0, 'T' },
469
470 /* Output. */
471 { "defines", optional_argument, 0, 'd' },
472
473 /* Operation modes. */
474 { "fixed-output-files", no_argument, 0, 'y' },
475 { "yacc", no_argument, 0, 'y' },
476
477 /* Parser. */
478 { "debug", no_argument, 0, 't' },
479 { "locations", no_argument, 0, LOCATIONS_OPTION },
480 { "no-lines", no_argument, 0, 'l' },
481 { "raw", no_argument, 0, 0 },
482 { "skeleton", required_argument, 0, 'S' },
483 { "language", required_argument, 0, 'L' },
484 { "token-table", no_argument, 0, 'k' },
485
486 {0, 0, 0, 0}
487 };
488
489 /* Under DOS, there is no difference on the case. This can be
490 troublesome when looking for `.tab' etc. */
491 #ifdef MSDOS
492 # define AS_FILE_NAME(File) (strlwr (File), (File))
493 #else
494 # define AS_FILE_NAME(File) (File)
495 #endif
496
497 void
498 getargs (int argc, char *argv[])
499 {
500 int c;
501
502 while ((c = getopt_long (argc, argv, short_options, long_options, NULL))
503 != -1)
504 switch (c)
505 {
506 case 0:
507 /* Certain long options cause getopt_long to return 0. */
508 break;
509
510 case 'd':
511 /* Here, the -d and --defines options are differentiated. */
512 defines_flag = true;
513 if (optarg)
514 spec_defines_file = xstrdup (AS_FILE_NAME (optarg));
515 break;
516
517 case 'I':
518 include = AS_FILE_NAME (optarg);
519 break;
520
521 case 'L':
522 language_argmatch (optarg, 0, NULL);
523 break;
524
525 case 'S':
526 skeleton_arg (AS_FILE_NAME (optarg), 0, NULL);
527 break;
528
529 case 'T':
530 FLAGS_ARGMATCH (trace, optarg);
531 break;
532
533 case 'V':
534 version ();
535 exit (EXIT_SUCCESS);
536
537 case 'W':
538 if (optarg)
539 FLAGS_ARGMATCH (warnings, optarg);
540 else
541 warnings_flag |= warnings_all;
542 break;
543
544 case 'b':
545 spec_file_prefix = AS_FILE_NAME (optarg);
546 break;
547
548 case 'g':
549 graph_flag = true;
550 if (optarg)
551 spec_graph_file = xstrdup (AS_FILE_NAME (optarg));
552 break;
553
554 case 'h':
555 usage (EXIT_SUCCESS);
556
557 case 'k':
558 token_table_flag = true;
559 break;
560
561 case 'l':
562 no_lines_flag = true;
563 break;
564
565 case 'o':
566 spec_outfile = AS_FILE_NAME (optarg);
567 break;
568
569 case 'p':
570 spec_name_prefix = optarg;
571 break;
572
573 case 'r':
574 FLAGS_ARGMATCH (report, optarg);
575 break;
576
577 case 't':
578 debug_flag = true;
579 break;
580
581 case 'v':
582 report_flag |= report_states;
583 break;
584
585 case 'x':
586 xml_flag = true;
587 if (optarg)
588 spec_xml_file = xstrdup (AS_FILE_NAME (optarg));
589 break;
590
591 case 'y':
592 yacc_flag = true;
593 break;
594
595 case LOCATIONS_OPTION:
596 locations_flag = true;
597 break;
598
599 case PRINT_LOCALEDIR_OPTION:
600 printf ("%s\n", LOCALEDIR);
601 exit (EXIT_SUCCESS);
602
603 case PRINT_DATADIR_OPTION:
604 printf ("%s\n", compute_pkgdatadir ());
605 exit (EXIT_SUCCESS);
606
607 case REPORT_FILE_OPTION:
608 spec_verbose_file = xstrdup (AS_FILE_NAME (optarg));
609 break;
610
611 default:
612 usage (EXIT_FAILURE);
613 }
614
615 if (argc - optind != 1)
616 {
617 if (argc - optind < 1)
618 error (0, 0, _("missing operand after `%s'"), argv[argc - 1]);
619 else
620 error (0, 0, _("extra operand `%s'"), argv[optind + 1]);
621 usage (EXIT_FAILURE);
622 }
623
624 current_file = grammar_file = uniqstr_new (argv[optind]);
625 }