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