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