]>
git.saurik.com Git - bison.git/blob - src/getargs.c
8af6528a1233cde704fb77c19a659a6b246a8fb0
   1 /* Parse command line arguments for Bison. 
   3    Copyright (C) 1984, 1986, 1989, 1992, 2000, 2001, 2002, 2003, 2004, 
   4    2005, 2006, 2007, 2008 Free Software Foundation, Inc. 
   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> 
  30 /* Hack to get <getopt.h> to declare getopt with a prototype.  */ 
  31 #if lint && ! defined __GNU_LIBRARY__ 
  32 # define __GNU_LIBRARY__ 
  33 # define HACK_FOR___GNU_LIBRARY___PROTOTYPE 1 
  38 #ifdef HACK_FOR___GNU_LIBRARY___PROTOTYPE 
  39 # undef __GNU_LIBRARY__ 
  40 # undef HACK_FOR___GNU_LIBRARY___PROTOTYPE 
  54 bool token_table_flag
; 
  55 bool yacc_flag
; /* for -y */ 
  57 bool error_verbose 
= false; 
  59 bool nondeterministic_parser 
= false; 
  60 bool glr_parser 
= false; 
  62 int report_flag 
= report_none
; 
  63 int trace_flag 
= trace_none
; 
  64 int warnings_flag 
= warnings_none
; 
  66 static struct bison_language 
const valid_languages
[] = { 
  67   { "c", "c-skel.m4", ".c", ".h", true }, 
  68   { "c++", "c++-skel.m4", ".cc", ".hh", true }, 
  69   { "java", "java-skel.m4", ".java", ".java", false }, 
  70   { "", "", "", "", false } 
  73 static int skeleton_prio 
= 2; 
  74 const char *skeleton 
= NULL
; 
  75 static int language_prio 
= 2; 
  76 struct bison_language 
const *language 
= &valid_languages
[0]; 
  77 const char *include 
= NULL
; 
  82 /** Decode an option's set of keys. 
  84  *  \param option   option being decoded. 
  85  *  \param keys     array of valid subarguments. 
  86  *  \param values   array of corresponding (int) values. 
  87  *  \param flags    the flags to update 
  88  *  \param args     colon separated list of effective subarguments to decode. 
  89  *                  If 0, then activate all the flags. 
  91  *  The special value 0 resets the flags to 0. 
  94 flags_argmatch (const char *option
, 
  95                 const char * const keys
[], const int values
[], 
  96                 int *flags
, char *args
) 
 100       args 
= strtok (args
, ","); 
 103           int value 
= XARGMATCH (option
, args
, keys
, values
); 
 108           args 
= strtok (NULL
, ","); 
 115 /** Decode a set of sub arguments. 
 117  *  \param FlagName  the flag familly to update. 
 118  *  \param Args      the effective sub arguments to decode. 
 120  *  \arg FlagName_args   the list of keys. 
 121  *  \arg FlagName_types  the list of values. 
 122  *  \arg FlagName_flag   the flag to update. 
 124 #define FLAGS_ARGMATCH(FlagName, Args)                                  \ 
 125   flags_argmatch ("--" #FlagName, FlagName ## _args, FlagName ## _types, \ 
 126                   &FlagName ## _flag, Args) 
 129 /*----------------------. 
 130 | --report's handling.  | 
 131 `----------------------*/ 
 133 static const char * const report_args
[] = 
 135   /* In a series of synonyms, present the most meaningful first, so 
 136      that argmatch_valid be more readable.  */ 
 139   "itemset", "itemsets", 
 140   "lookahead", "lookaheads", "look-ahead", 
 146 static const int report_types
[] = 
 149   report_states
, report_states
, 
 150   report_states 
| report_itemsets
, report_states 
| report_itemsets
, 
 151   report_states 
| report_lookahead_tokens
, 
 152   report_states 
| report_lookahead_tokens
, 
 153   report_states 
| report_lookahead_tokens
, 
 154   report_states 
| report_solved_conflicts
, 
 158 ARGMATCH_VERIFY (report_args
, report_types
); 
 161 /*---------------------. 
 162 | --trace's handling.  | 
 163 `---------------------*/ 
 165 static const char * const trace_args
[] = 
 167   /* In a series of synonyms, present the most meaningful first, so 
 168      that argmatch_valid be more readable.  */ 
 170   "scan       - grammar scanner traces", 
 171   "parse      - grammar parser traces", 
 172   "automaton  - construction of the automaton", 
 173   "bitsets    - use of bitsets", 
 174   "grammar    - reading, reducing the grammar", 
 175   "resource   - memory consumption (where available)", 
 176   "sets       - grammar sets: firsts, nullable etc.", 
 177   "tools      - m4 invocation", 
 179   "skeleton   - skeleton postprocessing", 
 180   "time       - time consumption", 
 181   "all        - all of the above", 
 185 static const int trace_types
[] = 
 202 ARGMATCH_VERIFY (trace_args
, trace_types
); 
 205 /*------------------------. 
 206 | --warnings's handling.  | 
 207 `------------------------*/ 
 209 static const char * const warnings_args
[] = 
 211   /* In a series of synonyms, present the most meaningful first, so 
 212      that argmatch_valid be more readable.  */ 
 213   "none            - no warnings", 
 214   "midrule-values  - unset or unused midrule values", 
 215   "yacc            - incompatibilities with POSIX YACC", 
 216   "all             - all of the above", 
 217   "error           - warnings are errors", 
 221 static const int warnings_types
[] = 
 224   warnings_midrule_values
, 
 230 ARGMATCH_VERIFY (warnings_args
, warnings_types
); 
 233 /*-------------------------------------------. 
 234 | Display the help message and exit STATUS.  | 
 235 `-------------------------------------------*/ 
 237 static void usage (int) ATTRIBUTE_NORETURN
; 
 243     fprintf (stderr
, _("Try `%s --help' for more information.\n"), 
 247       printf (_("Usage: %s [OPTION]... FILE\n"), program_name
); 
 249 Generate LALR(1) and GLR parsers.\n\ 
 254 Mandatory arguments to long options are mandatory for short options too.\n\ 
 257 The same is true for optional arguments.\n\ 
 263   -h, --help                 display this help and exit\n\ 
 264   -V, --version              output version information and exit\n\ 
 265       --print-localedir      output directory containing locale-dependent data\n\ 
 266       --print-datadir        output directory containing skeletons and XSLT\n\ 
 267   -y, --yacc                 emulate POSIX Yacc\n\ 
 268   -W, --warnings[=CATEGORY]  report the warnings falling in CATEGORY\n\ 
 274   -L, --language=LANGUAGE    specify the output programming language\n\ 
 275                              (this is an experimental feature)\n\ 
 276   -S, --skeleton=FILE        specify the skeleton to use\n\ 
 277   -t, --debug                instrument the parser for debugging\n\ 
 278       --locations            enable locations computation\n\ 
 279   -p, --name-prefix=PREFIX   prepend PREFIX to the external symbols\n\ 
 280   -l, --no-lines             don't generate `#line' directives\n\ 
 281   -k, --token-table          include a table of token names\n\ 
 285       /* Keep -d and --defines separate so that ../build-aux/cross-options.pl 
 286        * won't assume that -d also takes an argument.  */ 
 289       --defines[=FILE]       also produce a header file\n\ 
 290   -d                         likewise but cannot specify FILE (for POSIX Yacc)\n\ 
 291   -r, --report=THINGS        also produce details on the automaton\n\ 
 292       --report-file=FILE     write report to FILE\n\ 
 293   -v, --verbose              same as `--report=state'\n\ 
 294   -b, --file-prefix=PREFIX   specify a PREFIX for output files\n\ 
 295   -o, --output=FILE          leave output to FILE\n\ 
 296   -g, --graph[=FILE]         also output a graph of the automaton\n\ 
 297   -x, --xml[=FILE]           also output an XML report of the automaton\n\ 
 298                              (the XML schema is experimental)\n\ 
 303 Warning categories include:\n\ 
 304   `midrule-values'  unset or unused midrule values\n\ 
 305   `yacc'            incompatibilities with POSIX YACC\n\ 
 306   `all'             all the warnings\n\ 
 307   `no-CATEGORY'     turn off warnings in CATEGORY\n\ 
 308   `none'            turn off all the warnings\n\ 
 309   `error'           treat warnings as errors\n\ 
 314 THINGS is a list of comma separated words that can include:\n\ 
 315   `state'        describe the states\n\ 
 316   `itemset'      complete the core item sets with their closure\n\ 
 317   `lookahead'    explicitly associate lookahead tokens to items\n\ 
 318   `solved'       describe shift/reduce conflicts solving\n\ 
 319   `all'          include all the above information\n\ 
 320   `none'         disable the report\n\ 
 323       printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
); 
 330 /*------------------------------. 
 331 | Display the version message.  | 
 332 `------------------------------*/ 
 337   /* Some efforts were made to ease the translators' task, please 
 339   printf (_("bison (GNU Bison) %s"), VERSION
); 
 341   fputs (_("Written by Robert Corbett and Richard Stallman.\n"), stdout
); 
 345            _("Copyright (C) %d Free Software Foundation, Inc.\n"), 
 346            PACKAGE_COPYRIGHT_YEAR
); 
 349 This is free software; see the source for copying conditions.  There is NO\n\ 
 350 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\ 
 356 /*-------------------------------------. 
 357 | --skeleton and --language handling.  | 
 358 `--------------------------------------*/ 
 361 skeleton_arg (char const *arg
, int prio
, location 
const *loc
) 
 363   if (prio 
< skeleton_prio
) 
 365       skeleton_prio 
= prio
; 
 368   else if (prio 
== skeleton_prio
) 
 371         _("multiple skeleton declarations are invalid"); 
 373         complain_at (*loc
, msg
); 
 380 language_argmatch (char const *arg
, int prio
, location 
const *loc
) 
 384   if (prio 
< language_prio
) 
 387       for (i 
= 0; valid_languages
[i
].language
[0]; i
++) 
 388         if (c_strcasecmp (arg
, valid_languages
[i
].language
) == 0) 
 390             language_prio 
= prio
; 
 391             language 
= &valid_languages
[i
]; 
 394       msg 
= _("invalid language `%s'"); 
 396   else if (language_prio 
== prio
) 
 397     msg 
= _("multiple language declarations are invalid"); 
 402     complain_at (*loc
, msg
, arg
); 
 407 /*----------------------. 
 408 | Process the options.  | 
 409 `----------------------*/ 
 412    Should be computed from long_options.  */ 
 413 static char const short_options
[] = 
 436 /* Values for long options that do not have single-letter equivalents.  */ 
 439   LOCATIONS_OPTION 
= CHAR_MAX 
+ 1, 
 440   PRINT_LOCALEDIR_OPTION
, 
 441   PRINT_DATADIR_OPTION
, 
 445 static struct option 
const long_options
[] = 
 447   /* Operation modes. */ 
 448   { "help",            no_argument
,       0,   'h' }, 
 449   { "version",         no_argument
,       0,   'V' }, 
 450   { "print-localedir", no_argument
,       0,   PRINT_LOCALEDIR_OPTION 
}, 
 451   { "print-datadir",   no_argument
,       0,   PRINT_DATADIR_OPTION   
}, 
 452   { "warnings",        optional_argument
, 0,   'W' }, 
 455   { "name-prefix",   required_argument
,   0,   'p' }, 
 456   { "include",       required_argument
,   0,   'I' }, 
 459   { "file-prefix", required_argument
,   0,   'b' }, 
 460   { "output",      required_argument
,   0,   'o' }, 
 461   { "output-file", required_argument
,   0,   'o' }, 
 462   { "graph",       optional_argument
,   0,   'g' }, 
 463   { "xml",         optional_argument
,   0,   'x' }, 
 464   { "report",      required_argument
,   0,   'r' }, 
 465   { "report-file", required_argument
,   0,   REPORT_FILE_OPTION 
}, 
 466   { "verbose",     no_argument
,         0,   'v' }, 
 469   { "trace",         optional_argument
,   0,     'T' }, 
 472   { "defines",     optional_argument
,   0,   'd' }, 
 474   /* Operation modes.  */ 
 475   { "fixed-output-files", no_argument
,  0,   'y' }, 
 476   { "yacc",               no_argument
,  0,   'y' }, 
 479   { "debug",          no_argument
,               0,   't' }, 
 480   { "locations",      no_argument
,               0, LOCATIONS_OPTION 
}, 
 481   { "no-lines",       no_argument
,               0,   'l' }, 
 482   { "raw",            no_argument
,               0,     0 }, 
 483   { "skeleton",       required_argument
,         0,   'S' }, 
 484   { "language",       required_argument
,         0,   'L' }, 
 485   { "token-table",    no_argument
,               0,   'k' }, 
 490 /* Under DOS, there is no difference on the case.  This can be 
 491    troublesome when looking for `.tab' etc.  */ 
 493 # define AS_FILE_NAME(File) (strlwr (File), (File)) 
 495 # define AS_FILE_NAME(File) (File) 
 499 getargs (int argc
, char *argv
[]) 
 503   while ((c 
= getopt_long (argc
, argv
, short_options
, long_options
, NULL
)) 
 508         /* Certain long options cause getopt_long to return 0.  */ 
 512         /* Here, the -d and --defines options are differentiated.  */ 
 515           spec_defines_file 
= xstrdup (AS_FILE_NAME (optarg
)); 
 519         include 
= AS_FILE_NAME (optarg
); 
 523         language_argmatch (optarg
, 0, NULL
); 
 527         skeleton_arg (AS_FILE_NAME (optarg
), 0, NULL
); 
 531         FLAGS_ARGMATCH (trace
, optarg
); 
 540           FLAGS_ARGMATCH (warnings
, optarg
); 
 542           warnings_flag 
|= warnings_all
; 
 546         spec_file_prefix 
= AS_FILE_NAME (optarg
); 
 552           spec_graph_file 
= xstrdup (AS_FILE_NAME (optarg
)); 
 556         usage (EXIT_SUCCESS
); 
 559         token_table_flag 
= true; 
 563         no_lines_flag 
= true; 
 567         spec_outfile 
= AS_FILE_NAME (optarg
); 
 571         spec_name_prefix 
= optarg
; 
 575         FLAGS_ARGMATCH (report
, optarg
); 
 583         report_flag 
|= report_states
; 
 589           spec_xml_file 
= xstrdup (AS_FILE_NAME (optarg
)); 
 596       case LOCATIONS_OPTION
: 
 597         locations_flag 
= true; 
 600       case PRINT_LOCALEDIR_OPTION
: 
 601         printf ("%s\n", LOCALEDIR
); 
 604       case PRINT_DATADIR_OPTION
: 
 605         printf ("%s\n", compute_pkgdatadir ()); 
 608       case REPORT_FILE_OPTION
: 
 609         spec_verbose_file 
= xstrdup (AS_FILE_NAME (optarg
)); 
 613         usage (EXIT_FAILURE
); 
 616   if (argc 
- optind 
!= 1) 
 618       if (argc 
- optind 
< 1) 
 619         error (0, 0, _("missing operand after `%s'"), argv
[argc 
- 1]); 
 621         error (0, 0, _("extra operand `%s'"), argv
[optind 
+ 1]); 
 622       usage (EXIT_FAILURE
); 
 625   current_file 
= grammar_file 
= uniqstr_new (argv
[optind
]);