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