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