1 /* Parse command line arguments for Bison.
2 
3    Copyright (C) 1984, 1986, 1989, 1992, 2000-2015, 2018-2021 Free
4    Software Foundation, Inc.
5 
6    This file is part of Bison, the GNU Compiler Compiler.
7 
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.
12 
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.
17 
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
20 
21 #include <config.h>
22 #include "getargs.h"
23 
24 #include "system.h"
25 
26 #include <argmatch.h>
27 #include <c-strcase.h>
28 #include <configmake.h>
29 #include <error.h>
30 #include <getopt.h>
31 #include <progname.h>
32 #include <quote.h>
33 #include <textstyle.h>
34 
35 #include "complain.h"
36 #include "files.h"
37 #include "muscle-tab.h"
38 #include "output.h"
39 #include "uniqstr.h"
40 
41 bool defines_flag = false;
42 bool graph_flag = false;
43 bool xml_flag = false;
44 bool no_lines_flag = false;
45 bool token_table_flag = false;
46 location yacc_loc = EMPTY_LOCATION_INIT;
47 bool update_flag = false; /* for -u */
48 bool color_debug = false;
49 
50 bool nondeterministic_parser = false;
51 bool glr_parser = false;
52 
53 int feature_flag = feature_caret;
54 int report_flag = report_none;
55 int trace_flag = trace_none;
56 
57 static struct bison_language const valid_languages[] = {
58   /* lang,  skeleton,       ext,     hdr,     add_tab */
59   { "c",    "c-skel.m4",    ".c",    ".h",    true },
60   { "c++",  "c++-skel.m4",  ".cc",   ".hh",   true },
61   { "d",    "d-skel.m4",    ".d",    ".d",    false },
62   { "java", "java-skel.m4", ".java", ".java", false },
63   { "", "", "", "", false }
64 };
65 
66 int skeleton_prio = default_prio;
67 const char *skeleton = NULL;
68 int language_prio = default_prio;
69 struct bison_language const *language = &valid_languages[0];
70 
71 typedef int* (xargmatch_fn) (const char *context, const char *arg);
72 
73 /** Decode an option's key.
74  *
75  *  \param opt        option being decoded.
76  *  \param xargmatch  matching function.
77  *  \param all        the value of the argument 'all'.
78  *  \param flags      the flags to update
79  *  \param arg        the subarguments to decode.
80  *                    If null, then activate all the flags.
81  *  \param no         length of the potential "no-" prefix.
82  *                    Can be 0 or 3. If 3, negate the action of the subargument.
83  *
84  *  If VALUE != 0 then KEY sets flags and no-KEY clears them.
85  *  If VALUE == 0 then KEY clears all flags from \c all and no-KEY sets all
86  *  flags from \c all.  Thus no-none = all and no-all = none.
87  */
88 static void
flag_argmatch(const char * opt,xargmatch_fn xargmatch,int all,int * flags,char * arg,size_t no)89 flag_argmatch (const char *opt, xargmatch_fn xargmatch,
90                int all, int *flags, char *arg, size_t no)
91 {
92   int value = *xargmatch (opt, arg + no);
93 
94   /* -rnone == -rno-all, and -rno-none == -rall.  */
95   if (!value)
96     {
97       value = all;
98       no = !no;
99     }
100 
101   if (no)
102     *flags &= ~value;
103   else
104     *flags |= value;
105 }
106 
107 typedef void (usage_fn) (FILE *out);
108 
109 /** Decode an option's set of keys.
110  *
111  *  \param opt        option being decoded (e.g., --report).
112  *  \param xargmatch  matching function.
113  *  \param usage      function that implement --help for this option.
114  *  \param all        the value of the argument 'all'.
115  *  \param flags      the flags to update
116  *  \param args       comma separated list of effective subarguments to decode.
117  *                    If 0, then activate all the flags.
118  */
119 static void
flags_argmatch(const char * opt,xargmatch_fn xargmatch,usage_fn usage,int all,int * flags,char * args)120 flags_argmatch (const char *opt,
121                 xargmatch_fn xargmatch,
122                 usage_fn usage,
123                 int all, int *flags, char *args)
124 {
125   if (!args)
126     *flags |= all;
127   else if (STREQ (args, "help"))
128     {
129       usage (stdout);
130       exit (EXIT_SUCCESS);
131     }
132   else
133     for (args = strtok (args, ","); args; args = strtok (NULL, ","))
134       {
135         size_t no = STRPREFIX_LIT ("no-", args) ? 3 : 0;
136         flag_argmatch (opt, xargmatch,
137                        all, flags, args, no);
138       }
139 }
140 
141 
142 /** Decode a set of sub arguments.
143  *
144  *  \param FlagName  the flag family to update.
145  *  \param Args      the effective sub arguments to decode.
146  *  \param All       the "all" value.
147  *
148  *  \arg FlagName_args   the list of keys.
149  *  \arg FlagName_types  the list of values.
150  *  \arg FlagName_flag   the flag to update.
151  */
152 #define FLAGS_ARGMATCH(FlagName, Args, All)                             \
153   flags_argmatch ("--" #FlagName,                                       \
154                   (xargmatch_fn*) argmatch_## FlagName ## _value,       \
155                   argmatch_ ## FlagName ## _usage,                      \
156                   All, &FlagName ## _flag, Args)
157 
158 /*---------------------.
159 | --color's handling.  |
160 `---------------------*/
161 
162 enum color
163   {
164     color_always,
165     color_never,
166     color_auto
167   };
168 
169 ARGMATCH_DEFINE_GROUP (color, enum color)
170 
171 static const argmatch_color_doc argmatch_color_docs[] =
172 {
173   { "always",     N_("colorize the output") },
174   { "never",      N_("don't colorize the output") },
175   { "auto",       N_("colorize if the output device is a tty") },
176   { NULL, NULL },
177 };
178 
179 static const argmatch_color_arg argmatch_color_args[] =
180 {
181   { "always",   color_always },
182   { "yes",      color_always },
183   { "never",    color_never },
184   { "no",       color_never },
185   { "auto",     color_auto },
186   { "tty",      color_auto },
187   { NULL, color_always },
188 };
189 
190 const argmatch_color_group_type argmatch_color_group =
191 {
192   argmatch_color_args,
193   argmatch_color_docs,
194   /* TRANSLATORS: Use the same translation for WHEN as in the
195      --color=WHEN help message.  */
196   N_("WHEN can be one of the following:"),
197   NULL
198 };
199 
200 
201 /*----------------------.
202 | --report's handling.  |
203 `----------------------*/
204 
205 ARGMATCH_DEFINE_GROUP (report, enum report)
206 
207 static const argmatch_report_doc argmatch_report_docs[] =
208 {
209   { "states",          N_("describe the states") },
210   { "itemsets",        N_("complete the core item sets with their closure") },
211   { "lookaheads",      N_("explicitly associate lookahead tokens to items") },
212   { "solved",          N_("describe shift/reduce conflicts solving") },
213   { "counterexamples", N_("generate conflict counterexamples") },
214   { "all",             N_("include all the above information") },
215   { "none",            N_("disable the report") },
216   { NULL, NULL },
217 };
218 
219 static const argmatch_report_arg argmatch_report_args[] =
220 {
221   { "none",            report_none },
222   { "states",          report_states },
223   { "itemsets",        report_states | report_itemsets },
224   { "lookaheads",      report_states | report_lookaheads },
225   { "solved",          report_states | report_solved_conflicts },
226   { "counterexamples", report_cex },
227   { "cex",             report_cex },
228   { "all",             report_all },
229   { NULL, report_none },
230 };
231 
232 const argmatch_report_group_type argmatch_report_group =
233 {
234   argmatch_report_args,
235   argmatch_report_docs,
236   /* TRANSLATORS: Use the same translation for THINGS as in the
237      --report=THINGS help message.  */
238   N_("THINGS is a list of comma separated words that can include:"),
239   NULL
240 };
241 
242 /*---------------------.
243 | --trace's handling.  |
244 `---------------------*/
245 
246 ARGMATCH_DEFINE_GROUP (trace, enum trace)
247 
248 static const argmatch_trace_doc argmatch_trace_docs[] =
249 {
250   /* Meant for developers only, don't translate them.  */
251   { "none",       "no traces" },
252   { "locations",  "full display of the locations" },
253   { "scan",       "grammar scanner traces" },
254   { "parse",      "grammar parser traces" },
255   { "automaton",  "construction of the automaton" },
256   { "bitsets",    "use of bitsets" },
257   { "closure",    "input/output of closure" },
258   { "grammar",    "reading, reducing the grammar" },
259   { "resource",   "memory consumption (where available)" },
260   { "sets",       "grammar sets: firsts, nullable etc." },
261   { "muscles",    "m4 definitions passed to the skeleton" },
262   { "tools",      "m4 invocation" },
263   { "m4-early",   "m4 traces starting from the start" },
264   { "m4",         "m4 traces starting from the skeleton evaluation" },
265   { "skeleton",   "skeleton postprocessing" },
266   { "time",       "time consumption" },
267   { "ielr",       "IELR conversion" },
268   { "cex",        "counterexample generation"},
269   { "all",        "all of the above" },
270   { NULL, NULL},
271 };
272 
273 static const argmatch_trace_arg argmatch_trace_args[] =
274 {
275   { "none",      trace_none },
276   { "locations", trace_locations },
277   { "scan",      trace_scan },
278   { "parse",     trace_parse },
279   { "automaton", trace_automaton },
280   { "bitsets",   trace_bitsets },
281   { "closure",   trace_closure },
282   { "grammar",   trace_grammar },
283   { "resource",  trace_resource },
284   { "sets",      trace_sets },
285   { "muscles",   trace_muscles },
286   { "tools",     trace_tools },
287   { "m4-early",  trace_m4_early },
288   { "m4",        trace_m4 },
289   { "skeleton",  trace_skeleton },
290   { "time",      trace_time },
291   { "ielr",      trace_ielr },
292   { "cex",       trace_cex },
293   { "all",       trace_all },
294   { NULL,        trace_none},
295 };
296 
297 const argmatch_trace_group_type argmatch_trace_group =
298 {
299   argmatch_trace_args,
300   argmatch_trace_docs,
301   N_("TRACES is a list of comma separated words that can include:"),
302   NULL
303 };
304 
305 /*-----------------------.
306 | --feature's handling.  |
307 `-----------------------*/
308 
309 ARGMATCH_DEFINE_GROUP (feature, enum feature)
310 
311 static const argmatch_feature_doc argmatch_feature_docs[] =
312 {
313   { "caret",       N_("show errors with carets") },
314   { "fixit",       N_("show machine-readable fixes") },
315   { "syntax-only", N_("do not generate any file") },
316   { "all",         N_("all of the above") },
317   { "none",        N_("disable all of the above") },
318   { NULL, NULL }
319 };
320 
321 static const argmatch_feature_arg argmatch_feature_args[] =
322 {
323   { "none",                          feature_none },
324   { "caret",                         feature_caret },
325   { "diagnostics-show-caret",        feature_caret },
326   { "fixit",                         feature_fixit },
327   { "diagnostics-parseable-fixits",  feature_fixit },
328   { "syntax-only",                   feature_syntax_only },
329   { "all",                           feature_all },
330   { NULL, feature_none}
331 };
332 
333 const argmatch_feature_group_type argmatch_feature_group =
334 {
335   argmatch_feature_args,
336   argmatch_feature_docs,
337   /* TRANSLATORS: Use the same translation for FEATURES as in the
338      --feature=FEATURES help message.  */
339   N_("FEATURES is a list of comma separated words that can include:"),
340   NULL
341 };
342 
343 /*-------------------------------------------.
344 | Display the help message and exit STATUS.  |
345 `-------------------------------------------*/
346 
347  _Noreturn
348 static void usage (int);
349 
350 static void
usage(int status)351 usage (int status)
352 {
353   if (status != 0)
354     fprintf (stderr, _("Try '%s --help' for more information.\n"),
355              program_name);
356   else
357     {
358       /* For ../build-aux/cross-options.pl to work, use the format:
359                 ^  -S, --long[=ARGS] (whitespace)
360          A --long option is required.
361          Otherwise, add exceptions to ../build-aux/cross-options.pl.  */
362 
363       printf (_("Usage: %s [OPTION]... FILE\n"), program_name);
364       fputs (_("\
365 Generate a deterministic LR or generalized LR (GLR) parser employing\n\
366 LALR(1), IELR(1), or canonical LR(1) parser tables.\n\
367 \n\
368 "), stdout);
369 
370       fputs (_("\
371 Mandatory arguments to long options are mandatory for short options too.\n\
372 "), stdout);
373       fputs (_("\
374 The same is true for optional arguments.\n\
375 "), stdout);
376       putc ('\n', stdout);
377 
378       fputs (_("\
379 Operation Modes:\n\
380   -h, --help                 display this help and exit\n\
381   -V, --version              output version information and exit\n\
382       --print-localedir      output directory containing locale-dependent data\n\
383                              and exit\n\
384       --print-datadir        output directory containing skeletons and XSLT\n\
385                              and exit\n\
386   -u, --update               apply fixes to the source grammar file and exit\n\
387   -f, --feature[=FEATURES]   activate miscellaneous features\n\
388 \n\
389 "), stdout);
390 
391       argmatch_feature_usage (stdout);
392       putc ('\n', stdout);
393 
394       fputs (_("\
395 Diagnostics:\n\
396   -W, --warnings[=CATEGORY]  report the warnings falling in CATEGORY\n\
397       --color[=WHEN]         whether to colorize the diagnostics\n\
398       --style=FILE           specify the CSS FILE for colorizer diagnostics\n\
399 \n\
400 "), stdout);
401 
402       warning_usage (stdout);
403       putc ('\n', stdout);
404 
405       argmatch_color_usage (stdout);
406       putc ('\n', stdout);
407 
408       fputs (_("\
409 Tuning the Parser:\n\
410   -L, --language=LANGUAGE          specify the output programming language\n\
411   -S, --skeleton=FILE              specify the skeleton to use\n\
412   -t, --debug                      instrument the parser for tracing\n\
413                                    same as '-Dparse.trace'\n\
414       --locations                  enable location support\n\
415   -D, --define=NAME[=VALUE]        similar to '%define NAME VALUE'\n\
416   -F, --force-define=NAME[=VALUE]  override '%define NAME VALUE'\n\
417   -p, --name-prefix=PREFIX         prepend PREFIX to the external symbols\n\
418                                    deprecated by '-Dapi.prefix={PREFIX}'\n\
419   -l, --no-lines                   don't generate '#line' directives\n\
420   -k, --token-table                include a table of token names\n\
421   -y, --yacc                       emulate POSIX Yacc\n\
422 "), stdout);
423       putc ('\n', stdout);
424 
425       /* Keep -d and --defines separate so that ../build-aux/cross-options.pl
426        * won't assume that -d also takes an argument.  */
427       fputs (_("\
428 Output Files:\n\
429       --defines[=FILE]          also produce a header file\n\
430   -d                            likewise but cannot specify FILE (for POSIX Yacc)\n\
431   -r, --report=THINGS           also produce details on the automaton\n\
432       --report-file=FILE        write report to FILE\n\
433   -v, --verbose                 same as '--report=state'\n\
434   -b, --file-prefix=PREFIX      specify a PREFIX for output files\n\
435   -o, --output=FILE             leave output to FILE\n\
436   -g, --graph[=FILE]            also output a graph of the automaton\n\
437   -x, --xml[=FILE]              also output an XML report of the automaton\n\
438   -M, --file-prefix-map=OLD=NEW replace prefix OLD with NEW when writing file paths\n\
439                                 in output files\n\
440 "), stdout);
441       putc ('\n', stdout);
442 
443       argmatch_report_usage (stdout);
444       putc ('\n', stdout);
445 
446       printf (_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
447       printf (_("%s home page: <%s>.\n"), PACKAGE_NAME, PACKAGE_URL);
448       fputs (_("General help using GNU software: "
449                "<https://www.gnu.org/gethelp/>.\n"),
450              stdout);
451 
452 #if (defined __GLIBC__ && __GLIBC__ >= 2) && !defined __UCLIBC__
453       /* Don't output this redundant message for English locales.
454          Note we still output for 'C' so that it gets included in the
455          man page.  */
456       const char *lc_messages = setlocale (LC_MESSAGES, NULL);
457       if (lc_messages && !STREQ (lc_messages, "en_"))
458         /* TRANSLATORS: Replace LANG_CODE in this URL with your language code to
459            form one of the URLs at https://translationproject.org/team/.
460            Otherwise, replace the entire URL with your translation team's
461            email address.  */
462         fputs (_("Report translation bugs to "
463                  "<https://translationproject.org/team/>.\n"), stdout);
464 #endif
465       fputs (_("For complete documentation, run: info bison.\n"), stdout);
466     }
467 
468   exit (status);
469 }
470 
471 
472 /*------------------------------.
473 | Display the version message.  |
474 `------------------------------*/
475 
476 static void
version(void)477 version (void)
478 {
479   /* Some efforts were made to ease the translators' task, please
480      continue.  */
481   printf (_("bison (GNU Bison) %s"), VERSION);
482   putc ('\n', stdout);
483   fputs (_("Written by Robert Corbett and Richard Stallman.\n"), stdout);
484   putc ('\n', stdout);
485 
486   fprintf (stdout,
487            _("Copyright (C) %d Free Software Foundation, Inc.\n"),
488            PACKAGE_COPYRIGHT_YEAR);
489 
490   fputs (_("\
491 This is free software; see the source for copying conditions.  There is NO\n\
492 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
493 "),
494          stdout);
495 }
496 
497 
498 /*-------------------------------------.
499 | --skeleton and --language handling.  |
500 `--------------------------------------*/
501 
502 void
skeleton_arg(char const * arg,int prio,location loc)503 skeleton_arg (char const *arg, int prio, location loc)
504 {
505   if (prio < skeleton_prio)
506     {
507       skeleton_prio = prio;
508       skeleton = arg;
509     }
510   else if (prio == skeleton_prio)
511     complain (&loc, complaint,
512               _("multiple skeleton declarations are invalid"));
513 }
514 
515 void
language_argmatch(char const * arg,int prio,location loc)516 language_argmatch (char const *arg, int prio, location loc)
517 {
518   char const *msg = NULL;
519 
520   if (prio < language_prio)
521     {
522       for (int i = 0; valid_languages[i].language[0]; ++i)
523         if (c_strcasecmp (arg, valid_languages[i].language) == 0)
524           {
525             language_prio = prio;
526             language = &valid_languages[i];
527             return;
528           }
529       msg = _("%s: invalid language");
530     }
531   else if (language_prio == prio)
532     msg = _("multiple language declarations are invalid");
533 
534   if (msg)
535     complain (&loc, complaint, msg, quotearg_colon (arg));
536 }
537 
538 /*----------------------.
539 | Process the options.  |
540 `----------------------*/
541 
542 /* Shorts options.
543    Should be computed from long_options.  */
544 static char const short_options[] =
545   "D:"
546   "F:"
547   "L:"
548   "S:"
549   "T::"
550   "V"
551   "W::"
552   "b:"
553   "d"
554   "f::"
555   "g::"
556   "h"
557   "k"
558   "l"
559   "M:"
560   "o:"
561   "p:"
562   "r:"
563   "t"
564   "u"   /* --update */
565   "v"
566   "x::"
567   "y"
568   ;
569 
570 /* Values for long options that do not have single-letter equivalents.  */
571 enum
572 {
573   COLOR_OPTION = CHAR_MAX + 1,
574   FIXED_OUTPUT_FILES_OPTION,
575   LOCATIONS_OPTION,
576   PRINT_DATADIR_OPTION,
577   PRINT_LOCALEDIR_OPTION,
578   REPORT_FILE_OPTION,
579   STYLE_OPTION
580 };
581 
582 /* In the same order as in usage(), and in the documentation.  */
583 static struct option const long_options[] =
584 {
585   /* Operation modes. */
586   { "help",            no_argument,       0,   'h' },
587   { "version",         no_argument,       0,   'V' },
588   { "print-localedir", no_argument,       0,   PRINT_LOCALEDIR_OPTION },
589   { "print-datadir",   no_argument,       0,   PRINT_DATADIR_OPTION   },
590   { "update",          no_argument,       0,   'u' },
591   { "feature",         optional_argument, 0,   'f' },
592 
593   /* Diagnostics.  */
594   { "warnings",        optional_argument,  0, 'W' },
595   { "color",           optional_argument,  0,  COLOR_OPTION },
596   { "style",           optional_argument,  0,  STYLE_OPTION },
597 
598   /* Tuning the Parser. */
599   { "language",       required_argument,   0, 'L' },
600   { "skeleton",       required_argument,   0, 'S' },
601   { "debug",          no_argument,         0, 't' },
602   { "locations",      no_argument,         0, LOCATIONS_OPTION },
603   { "define",         required_argument,   0, 'D' },
604   { "force-define",   required_argument,   0, 'F' },
605   { "name-prefix",    required_argument,   0, 'p' },
606   { "no-lines",       no_argument,         0, 'l' },
607   { "token-table",    no_argument,         0, 'k' },
608   { "yacc",           no_argument,         0, 'y' },
609 
610   /* Output Files. */
611   { "defines",         optional_argument,   0,   'd' },
612   { "report",          required_argument,   0,   'r' },
613   { "report-file",     required_argument,   0,   REPORT_FILE_OPTION },
614   { "verbose",         no_argument,         0,   'v' },
615   { "file-prefix",     required_argument,   0,   'b' },
616   { "output",          required_argument,   0,   'o' },
617   { "graph",           optional_argument,   0,   'g' },
618   { "xml",             optional_argument,   0,   'x' },
619   { "file-prefix-map", required_argument,   0,   'M' },
620 
621   /* Hidden. */
622   { "fixed-output-files", no_argument,       0,  FIXED_OUTPUT_FILES_OPTION },
623   { "output-file",        required_argument, 0,  'o' },
624   { "trace",              optional_argument, 0,  'T' },
625 
626   {0, 0, 0, 0}
627 };
628 
629 /* Build a location for the current command line argument. */
630 static
631 location
command_line_location(void)632 command_line_location (void)
633 {
634   location res;
635   /* "<command line>" is used in GCC's messages about -D. */
636   boundary_set (&res.start, uniqstr_new ("<command line>"), optind - 1, -1, -1);
637   res.end = res.start;
638   return res;
639 }
640 
641 
642 /* Handle the command line options for color support.  Do it early, so
643    that error messages from getargs be also colored as per the user's
644    request.  This is consistent with the way GCC and Clang behave.  */
645 
646 static void
getargs_colors(int argc,char * argv[])647 getargs_colors (int argc, char *argv[])
648 {
649   for (int i = 1; i < argc; i++)
650     {
651       const char *arg = argv[i];
652       if (STRPREFIX_LIT ("--color=", arg))
653         {
654           const char *color = arg + strlen ("--color=");
655           if (STREQ (color, "debug"))
656             color_debug = true;
657           else
658             handle_color_option (color);
659         }
660       else if (STREQ ("--color", arg))
661         handle_color_option (NULL);
662       else if (STRPREFIX_LIT ("--style=", arg))
663         {
664           const char *style = arg + strlen ("--style=");
665           handle_style_option (style);
666         }
667     }
668   complain_init_color ();
669 }
670 
671 
672 void
getargs(int argc,char * argv[])673 getargs (int argc, char *argv[])
674 {
675   getargs_colors (argc, argv);
676 
677   int c;
678   while ((c = getopt_long (argc, argv, short_options, long_options, NULL))
679          != -1)
680   {
681     location loc = command_line_location ();
682     switch (c)
683       {
684         /* ASCII Sorting for short options (i.e., upper case then
685            lower case), and then long-only options.  */
686 
687       case 0:
688         /* Certain long options cause getopt_long to return 0.  */
689         break;
690 
691       case 'D': /* -DNAME[=(VALUE|"VALUE"|{VALUE})]. */
692       case 'F': /* -FNAME[=(VALUE|"VALUE"|{VALUE})]. */
693         {
694           char *name = optarg;
695           char *value = strchr (optarg, '=');
696           muscle_kind kind = muscle_keyword;
697           if (value)
698             {
699               char *end = value + strlen (value) - 1;
700               *value++ = 0;
701               if (*value == '{' && *end == '}')
702                 {
703                   kind = muscle_code;
704                   ++value;
705                   *end = 0;
706                 }
707               else if (*value == '"' && *end == '"')
708                 {
709                   kind = muscle_string;
710                   ++value;
711                   *end = 0;
712                 }
713             }
714           muscle_percent_define_insert (name, loc,
715                                         kind, value ? value : "",
716                                         c == 'D' ? MUSCLE_PERCENT_DEFINE_D
717                                                  : MUSCLE_PERCENT_DEFINE_F);
718         }
719         break;
720 
721       case 'L':
722         language_argmatch (optarg, command_line_prio, loc);
723         break;
724 
725       case 'M': // -MOLDPREFIX=NEWPREFIX
726         {
727           char *newprefix = strchr (optarg, '=');
728           if (newprefix)
729             {
730               *newprefix = '\0';
731               add_prefix_map (optarg, newprefix + 1);
732             }
733           else
734             {
735               complain (&loc, complaint, _("invalid argument for %s: %s"),
736                         quote ("--file-prefix-map"), quotearg_n (1, optarg));
737             }
738         }
739         break;
740 
741       case 'S':
742         skeleton_arg (optarg, command_line_prio, loc);
743         break;
744 
745       case 'T':
746         FLAGS_ARGMATCH (trace, optarg, trace_all);
747         break;
748 
749       case 'V':
750         version ();
751         exit (EXIT_SUCCESS);
752 
753       case 'f':
754         FLAGS_ARGMATCH (feature, optarg, feature_all);
755         break;
756 
757       case 'W':
758         warnings_argmatch (optarg);
759         break;
760 
761       case 'b':
762         spec_file_prefix = optarg;
763         break;
764 
765       case 'd':
766         /* Here, the -d and --defines options are differentiated.  */
767         defines_flag = true;
768         if (optarg)
769           {
770             free (spec_header_file);
771             spec_header_file = xstrdup (optarg);
772           }
773         break;
774 
775       case 'g':
776         graph_flag = true;
777         if (optarg)
778           {
779             free (spec_graph_file);
780             spec_graph_file = xstrdup (optarg);
781           }
782         break;
783 
784       case 'h':
785         usage (EXIT_SUCCESS);
786 
787       case 'k':
788         token_table_flag = true;
789         break;
790 
791       case 'l':
792         no_lines_flag = true;
793         break;
794 
795       case 'o':
796         spec_outfile = optarg;
797         break;
798 
799       case 'p':
800         spec_name_prefix = optarg;
801         break;
802 
803       case 'r':
804         FLAGS_ARGMATCH (report, optarg, report_all);
805         break;
806 
807       case 't':
808         muscle_percent_define_insert ("parse.trace",
809                                       loc,
810                                       muscle_keyword, "",
811                                       MUSCLE_PERCENT_DEFINE_D);
812         break;
813 
814       case 'u':
815         update_flag = true;
816         feature_flag |= feature_syntax_only;
817         break;
818 
819       case 'v':
820         report_flag |= report_states;
821         break;
822 
823       case 'x':
824         xml_flag = true;
825         if (optarg)
826           {
827             free (spec_xml_file);
828             spec_xml_file = xstrdup (optarg);
829           }
830         break;
831 
832       case 'y':
833         warning_argmatch ("yacc", 0, 0);
834         yacc_loc = loc;
835         break;
836 
837       case COLOR_OPTION:
838         /* Handled in getargs_colors. */
839         break;
840 
841       case FIXED_OUTPUT_FILES_OPTION:
842         complain (&loc, Wdeprecated,
843                   _("deprecated option: %s, use %s"),
844                   quote ("--fixed-output-files"), quote_n (1, "-o y.tab.c"));
845         spec_outfile = "y.tab.c";
846         break;
847 
848       case LOCATIONS_OPTION:
849         muscle_percent_define_ensure ("locations", loc, true);
850         break;
851 
852       case PRINT_LOCALEDIR_OPTION:
853         printf ("%s\n", LOCALEDIR);
854         exit (EXIT_SUCCESS);
855 
856       case PRINT_DATADIR_OPTION:
857         printf ("%s\n", pkgdatadir ());
858         exit (EXIT_SUCCESS);
859 
860       case REPORT_FILE_OPTION:
861         free (spec_verbose_file);
862         spec_verbose_file = xstrdup (optarg);
863         break;
864 
865       case STYLE_OPTION:
866         /* Handled in getargs_colors. */
867         break;
868 
869       default:
870         usage (EXIT_FAILURE);
871       }
872   }
873 
874   if (argc - optind != 1)
875     {
876       if (argc - optind < 1)
877         error (0, 0, _("missing operand"));
878       else
879         error (0, 0, _("extra operand %s"), quote (argv[optind + 1]));
880       usage (EXIT_FAILURE);
881     }
882 
883   grammar_file = uniqstr_new (argv[optind]);
884   MUSCLE_INSERT_C_STRING ("file_name", grammar_file);
885 }
886 
887 void
tr(char * s,char from,char to)888 tr (char *s, char from, char to)
889 {
890   for (; *s; ++s)
891     if (*s == from)
892       *s = to;
893 }
894