]>
git.saurik.com Git - bison.git/blob - src/getargs.c
1 /* Parse command line arguments for Bison.
3 Copyright (C) 1984, 1986, 1989, 1992, 2000-2012 Free Software
6 This file is part of Bison, the GNU Compiler Compiler.
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26 #include <c-strcase.h>
27 #include <configmake.h>
35 #include "muscle-tab.h"
43 bool token_table_flag
;
44 bool yacc_flag
; /* for -y */
46 bool nondeterministic_parser
= false;
47 bool glr_parser
= false;
49 int report_flag
= report_none
;
50 int trace_flag
= trace_none
;
52 static struct bison_language
const valid_languages
[] = {
53 { "c", "c-skel.m4", ".c", ".h", true },
54 { "c++", "c++-skel.m4", ".cc", ".hh", true },
55 { "java", "java-skel.m4", ".java", ".java", false },
56 { "", "", "", "", false }
59 int skeleton_prio
= default_prio
;
60 const char *skeleton
= NULL
;
61 int language_prio
= default_prio
;
62 struct bison_language
const *language
= &valid_languages
[0];
63 const char *include
= NULL
;
65 /** Decode an option's key.
67 * \param option option being decoded.
68 * \param keys array of valid subarguments.
69 * \param values array of corresponding (int) values.
70 * \param all the all value.
71 * \param flags the flags to update
72 * \param arg the subarguments to decode.
73 * If null, then activate all the flags.
74 * \param no length of the potential "no-" prefix.
75 * Can be 0 or 3. If 3, negate the action of the subargument.
76 * \param err length of a potential "error=".
77 * Can be 0 or 6. If 6, treat the subargument as a CATEGORY
79 * If VALUE != 0 then KEY sets flags and no-KEY clears them.
80 * If VALUE == 0 then KEY clears all flags from \c all and no-KEY sets all
81 * flags from \c all. Thus no-none = all and no-all = none.
84 flag_argmatch (const char *option
,
85 const char * const keys
[], const int values
[],
86 int all
, int *flags
, char *arg
, size_t no
, size_t err
)
89 if (!err
|| arg
[no
+ err
++] != '\0')
90 value
= XARGMATCH (option
, arg
+ no
+ err
, keys
, values
);
99 warnings_flag
|= value
;
105 /* With a simpler 'if (no)' version, -Werror means -Werror=all
106 (or rather, -Werror=no-none, but that syntax is invalid).
108 - Werror activates all errors, but not the warnings
109 - Werror=all activates errors, and all warnings */
116 /** Decode an option's set of keys.
118 * \param option option being decoded.
119 * \param keys array of valid subarguments.
120 * \param values array of corresponding (int) values.
121 * \param all the all value.
122 * \param flags the flags to update
123 * \param args comma separated list of effective subarguments to decode.
124 * If 0, then activate all the flags.
127 flags_argmatch (const char *option
,
128 const char * const keys
[], const int values
[],
129 int all
, int *flags
, char *args
)
132 for (args
= strtok (args
, ","); args
; args
= strtok (NULL
, ","))
134 size_t no
= STRPREFIX_LIT ("no-", args
) ? 3 : 0;
135 size_t err
= STRPREFIX_LIT ("error", args
+ no
) ? 5 : 0;
137 flag_argmatch (option
, keys
,
138 values
, all
, err
? &errors_flag
: flags
,
145 /** Decode a set of sub arguments.
147 * \param FlagName the flag familly to update.
148 * \param Args the effective sub arguments to decode.
149 * \param All the "all" value.
151 * \arg FlagName_args the list of keys.
152 * \arg FlagName_types the list of values.
153 * \arg FlagName_flag the flag to update.
155 #define FLAGS_ARGMATCH(FlagName, Args, All) \
156 flags_argmatch ("--" #FlagName, FlagName ## _args, FlagName ## _types, \
157 All, &FlagName ## _flag, Args)
160 /*----------------------.
161 | --report's handling. |
162 `----------------------*/
164 static const char * const report_args
[] =
166 /* In a series of synonyms, present the most meaningful first, so
167 that argmatch_valid be more readable. */
170 "itemset", "itemsets",
171 "lookahead", "lookaheads", "look-ahead",
177 static const int report_types
[] =
180 report_states
, report_states
,
181 report_states
| report_itemsets
, report_states
| report_itemsets
,
182 report_states
| report_lookahead_tokens
,
183 report_states
| report_lookahead_tokens
,
184 report_states
| report_lookahead_tokens
,
185 report_states
| report_solved_conflicts
,
189 ARGMATCH_VERIFY (report_args
, report_types
);
192 /*---------------------.
193 | --trace's handling. |
194 `---------------------*/
196 static const char * const trace_args
[] =
198 /* In a series of synonyms, present the most meaningful first, so
199 that argmatch_valid be more readable. */
201 "scan - grammar scanner traces",
202 "parse - grammar parser traces",
203 "automaton - construction of the automaton",
204 "bitsets - use of bitsets",
205 "grammar - reading, reducing the grammar",
206 "resource - memory consumption (where available)",
207 "sets - grammar sets: firsts, nullable etc.",
208 "muscles - m4 definitions passed to the skeleton",
209 "tools - m4 invocation",
211 "skeleton - skeleton postprocessing",
212 "time - time consumption",
213 "ielr - IELR conversion",
214 "all - all of the above",
218 static const int trace_types
[] =
237 ARGMATCH_VERIFY (trace_args
, trace_types
);
240 /*------------------------.
241 | --warnings's handling. |
242 `------------------------*/
244 static const char * const warnings_args
[] =
246 /* In a series of synonyms, present the most meaningful first, so
247 that argmatch_valid be more readable. */
248 "none - no warnings",
249 "midrule-values - unset or unused midrule values",
250 "yacc - incompatibilities with POSIX Yacc",
251 "conflicts-sr - S/R conflicts",
252 "conflicts-rr - R/R conflicts",
253 "deprecated - obsolete constructs",
254 "other - all other warnings",
255 "all - all of the above",
256 "error - warnings are errors",
260 static const int warnings_types
[] =
273 ARGMATCH_VERIFY (warnings_args
, warnings_types
);
275 /*-------------------------------------------.
276 | Display the help message and exit STATUS. |
277 `-------------------------------------------*/
279 static void usage (int) ATTRIBUTE_NORETURN
;
285 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
289 /* For ../build-aux/cross-options.pl to work, use the format:
290 ^ -S, --long[=ARGS] (whitespace)
291 A --long option is required.
292 Otherwise, add exceptions to ../build-aux/cross-options.pl. */
294 printf (_("Usage: %s [OPTION]... FILE\n"), program_name
);
296 Generate a deterministic LR or generalized LR (GLR) parser employing\n\
297 LALR(1), IELR(1), or canonical LR(1) parser tables. IELR(1) and\n\
298 canonical LR(1) support is experimental.\n\
303 Mandatory arguments to long options are mandatory for short options too.\n\
306 The same is true for optional arguments.\n\
312 -h, --help display this help and exit\n\
313 -V, --version output version information and exit\n\
314 --print-localedir output directory containing locale-dependent data\n\
315 --print-datadir output directory containing skeletons and XSLT\n\
316 -y, --yacc emulate POSIX Yacc\n\
317 -W, --warnings[=CATEGORY] report the warnings falling in CATEGORY\n\
323 -L, --language=LANGUAGE specify the output programming language\n\
324 (this is an experimental feature)\n\
325 -S, --skeleton=FILE specify the skeleton to use\n\
326 -t, --debug instrument the parser for tracing\n\
327 same as `-Dparse.trace'\n\
328 --locations enable location support\n\
329 -D, --define=NAME[=VALUE] similar to '%define NAME \"VALUE\"'\n\
330 -F, --force-define=NAME[=VALUE] override '%define NAME \"VALUE\"'\n\
331 -p, --name-prefix=PREFIX prepend PREFIX to the external symbols\n\
332 deprecated by '-Dapi.prefix=PREFIX'\n\
333 -l, --no-lines don't generate '#line' directives\n\
334 -k, --token-table include a table of token names\n\
338 /* Keep -d and --defines separate so that ../build-aux/cross-options.pl
339 * won't assume that -d also takes an argument. */
342 --defines[=FILE] also produce a header file\n\
343 -d likewise but cannot specify FILE (for POSIX Yacc)\n\
344 -r, --report=THINGS also produce details on the automaton\n\
345 --report-file=FILE write report to FILE\n\
346 -v, --verbose same as `--report=state'\n\
347 -b, --file-prefix=PREFIX specify a PREFIX for output files\n\
348 -o, --output=FILE leave output to FILE\n\
349 -g, --graph[=FILE] also output a graph of the automaton\n\
350 -x, --xml[=FILE] also output an XML report of the automaton\n\
351 (the XML schema is experimental)\n\
356 Warning categories include:\n\
357 `midrule-values' unset or unused midrule values\n\
358 `yacc' incompatibilities with POSIX Yacc\n\
359 `conflicts-sr' S/R conflicts (enabled by default)\n\
360 `conflicts-rr' R/R conflicts (enabled by default)\n\
361 `deprecated' obsolete constructs\n\
362 `other' all other warnings (enabled by default)\n\
363 `all' all the warnings\n\
364 `no-CATEGORY' turn off warnings in CATEGORY\n\
365 `none' turn off all the warnings\n\
366 `error[=CATEGORY]' treat warnings as errors\n\
371 THINGS is a list of comma separated words that can include:\n\
372 `state' describe the states\n\
373 `itemset' complete the core item sets with their closure\n\
374 `lookahead' explicitly associate lookahead tokens to items\n\
375 `solved' describe shift/reduce conflicts solving\n\
376 `all' include all the above information\n\
377 `none' disable the report\n\
381 printf (_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
382 printf (_("%s home page: <%s>.\n"), PACKAGE_NAME
, PACKAGE_URL
);
383 fputs (_("General help using GNU software: "
384 "<http://www.gnu.org/gethelp/>.\n"),
386 /* Don't output this redundant message for English locales.
387 Note we still output for 'C' so that it gets included in the
389 const char *lc_messages
= setlocale (LC_MESSAGES
, NULL
);
390 if (lc_messages
&& !STREQ (lc_messages
, "en_"))
391 /* TRANSLATORS: Replace LANG_CODE in this URL with your language
392 code <http://translationproject.org/team/LANG_CODE.html> to
393 form one of the URLs at http://translationproject.org/team/.
394 Otherwise, replace the entire URL with your translation team's
396 fputs (_("Report translation bugs to "
397 "<http://translationproject.org/team/>.\n"), stdout
);
398 fputs (_("For complete documentation, run: info bison.\n"), stdout
);
405 /*------------------------------.
406 | Display the version message. |
407 `------------------------------*/
412 /* Some efforts were made to ease the translators' task, please
414 printf (_("bison (GNU Bison) %s"), VERSION
);
416 fputs (_("Written by Robert Corbett and Richard Stallman.\n"), stdout
);
420 _("Copyright (C) %d Free Software Foundation, Inc.\n"),
421 PACKAGE_COPYRIGHT_YEAR
);
424 This is free software; see the source for copying conditions. There is NO\n\
425 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
431 /*-------------------------------------.
432 | --skeleton and --language handling. |
433 `--------------------------------------*/
436 skeleton_arg (char const *arg
, int prio
, location loc
)
438 if (prio
< skeleton_prio
)
440 skeleton_prio
= prio
;
443 else if (prio
== skeleton_prio
)
444 complain (&loc
, complaint
,
445 _("multiple skeleton declarations are invalid"));
449 language_argmatch (char const *arg
, int prio
, location loc
)
453 if (prio
< language_prio
)
456 for (i
= 0; valid_languages
[i
].language
[0]; i
++)
457 if (c_strcasecmp (arg
, valid_languages
[i
].language
) == 0)
459 language_prio
= prio
;
460 language
= &valid_languages
[i
];
463 msg
= _("%s: invalid language");
465 else if (language_prio
== prio
)
466 msg
= _("multiple language declarations are invalid");
470 complain (&loc
, complaint
, msg
, quotearg_colon (arg
));
473 /*----------------------.
474 | Process the options. |
475 `----------------------*/
478 Should be computed from long_options. */
479 static char const short_options
[] =
504 /* Values for long options that do not have single-letter equivalents. */
507 LOCATIONS_OPTION
= CHAR_MAX
+ 1,
508 PRINT_LOCALEDIR_OPTION
,
509 PRINT_DATADIR_OPTION
,
513 static struct option
const long_options
[] =
515 /* Operation modes. */
516 { "help", no_argument
, 0, 'h' },
517 { "version", no_argument
, 0, 'V' },
518 { "print-localedir", no_argument
, 0, PRINT_LOCALEDIR_OPTION
},
519 { "print-datadir", no_argument
, 0, PRINT_DATADIR_OPTION
},
520 { "warnings", optional_argument
, 0, 'W' },
523 { "name-prefix", required_argument
, 0, 'p' },
524 { "include", required_argument
, 0, 'I' },
527 { "file-prefix", required_argument
, 0, 'b' },
528 { "output", required_argument
, 0, 'o' },
529 { "output-file", required_argument
, 0, 'o' },
530 { "graph", optional_argument
, 0, 'g' },
531 { "xml", optional_argument
, 0, 'x' },
532 { "report", required_argument
, 0, 'r' },
533 { "report-file", required_argument
, 0, REPORT_FILE_OPTION
},
534 { "verbose", no_argument
, 0, 'v' },
537 { "trace", optional_argument
, 0, 'T' },
540 { "defines", optional_argument
, 0, 'd' },
542 /* Operation modes. */
543 { "fixed-output-files", no_argument
, 0, 'y' },
544 { "yacc", no_argument
, 0, 'y' },
547 { "debug", no_argument
, 0, 't' },
548 { "define", required_argument
, 0, 'D' },
549 { "force-define", required_argument
, 0, 'F' },
550 { "locations", no_argument
, 0, LOCATIONS_OPTION
},
551 { "no-lines", no_argument
, 0, 'l' },
552 { "raw", no_argument
, 0, 0 },
553 { "skeleton", required_argument
, 0, 'S' },
554 { "language", required_argument
, 0, 'L' },
555 { "token-table", no_argument
, 0, 'k' },
560 /* Under DOS, there is no difference on the case. This can be
561 troublesome when looking for `.tab' etc. */
563 # define AS_FILE_NAME(File) (strlwr (File), (File))
565 # define AS_FILE_NAME(File) (File)
568 /* Build a location for the current command line argument. */
571 command_line_location (void)
574 /* "<command line>" is used in GCC's messages about -D. */
575 boundary_set (&res
.start
, uniqstr_new ("<command line>"), optind
, -1);
582 getargs (int argc
, char *argv
[])
586 while ((c
= getopt_long (argc
, argv
, short_options
, long_options
, NULL
))
590 /* ASCII Sorting for short options (i.e., upper case then
591 lower case), and then long-only options. */
594 /* Certain long options cause getopt_long to return 0. */
597 case 'D': /* -DNAME[=VALUE]. */
598 case 'F': /* -FNAME[=VALUE]. */
601 char* value
= strchr (optarg
, '=');
604 muscle_percent_define_insert (name
, command_line_location (),
606 c
== 'D' ? MUSCLE_PERCENT_DEFINE_D
607 : MUSCLE_PERCENT_DEFINE_F
);
612 include
= AS_FILE_NAME (optarg
);
616 language_argmatch (optarg
, command_line_prio
,
617 command_line_location ());
621 skeleton_arg (AS_FILE_NAME (optarg
), command_line_prio
,
622 command_line_location ());
626 FLAGS_ARGMATCH (trace
, optarg
, trace_all
);
634 FLAGS_ARGMATCH (warnings
, optarg
, Wall
);
638 spec_file_prefix
= AS_FILE_NAME (optarg
);
642 /* Here, the -d and --defines options are differentiated. */
646 free (spec_defines_file
);
647 spec_defines_file
= xstrdup (AS_FILE_NAME (optarg
));
655 free (spec_graph_file
);
656 spec_graph_file
= xstrdup (AS_FILE_NAME (optarg
));
661 usage (EXIT_SUCCESS
);
664 token_table_flag
= true;
668 no_lines_flag
= true;
672 spec_outfile
= AS_FILE_NAME (optarg
);
676 spec_name_prefix
= optarg
;
680 FLAGS_ARGMATCH (report
, optarg
, report_all
);
684 muscle_percent_define_insert ("parse.trace",
685 command_line_location (), "",
686 MUSCLE_PERCENT_DEFINE_D
);
690 report_flag
|= report_states
;
697 free (spec_xml_file
);
698 spec_xml_file
= xstrdup (AS_FILE_NAME (optarg
));
703 warnings_flag
|= Wyacc
;
704 errors_flag
|= Wyacc
;
708 case LOCATIONS_OPTION
:
709 muscle_percent_define_ensure ("locations",
710 command_line_location (), true);
713 case PRINT_LOCALEDIR_OPTION
:
714 printf ("%s\n", LOCALEDIR
);
717 case PRINT_DATADIR_OPTION
:
718 printf ("%s\n", pkgdatadir ());
721 case REPORT_FILE_OPTION
:
722 free (spec_verbose_file
);
723 spec_verbose_file
= xstrdup (AS_FILE_NAME (optarg
));
727 usage (EXIT_FAILURE
);
730 if (argc
- optind
!= 1)
732 if (argc
- optind
< 1)
733 error (0, 0, _("%s: missing operand"), quotearg_colon (argv
[argc
- 1]));
735 error (0, 0, _("extra operand %s"), quote (argv
[optind
+ 1]));
736 usage (EXIT_FAILURE
);
739 current_file
= grammar_file
= uniqstr_new (argv
[optind
]);
740 MUSCLE_INSERT_C_STRING ("file_name", grammar_file
);
744 tr (char *s
, char from
, char to
)