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