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