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