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