1 /********************* */
2 /*! \file options_template.cpp
4 ** Original author: Morgan Deters
5 ** Major contributors: none
6 ** Minor contributors (to current version): none
7 ** This file is part of the CVC4 project.
8 ** Copyright (c) 2009-2013 New York University and The University of Iowa
9 ** See the file COPYING in the top-level source directory for licensing
10 ** information.\endverbatim
12 ** \brief Contains code for handling command-line options.
14 ** Contains code for handling command-line options
17 #if !defined(_BSD_SOURCE) && defined(__MINGW32__) && !defined(__MINGW64__)
18 // force use of optreset; mingw32 croaks on argv-switching otherwise
19 # include "cvc4autoconfig.h"
21 # undef HAVE_DECL_OPTRESET
22 # define HAVE_DECL_OPTRESET 1
23 # define CVC4_IS_NOT_REALLY_BSD
24 #endif /* !_BSD_SOURCE && __MINGW32__ && !__MINGW64__ */
28 #endif /* __MINGW64__ */
33 #ifdef CVC4_IS_NOT_REALLY_BSD
35 #endif /* CVC4_IS_NOT_REALLY_BSD */
48 #include "expr/expr.h"
49 #include "util/configuration.h"
50 #include "util/exception.h"
51 #include "util/language.h"
54 $
{include_all_option_headers
}
56 #line 57 "${template}"
58 #include "util/output.h"
59 #include "options/options_holder.h"
60 #include "cvc4autoconfig.h"
61 #include "options/base_options_handlers.h"
63 $
{option_handler_includes
}
65 #line 66 "${template}"
68 using namespace CVC4::options
;
72 CVC4_THREADLOCAL(Options
*) Options::s_current
= NULL
;
75 * This is a default handler for options of built-in C++ type. This
76 * template is really just a helper for the handleOption() template,
77 * below. Variants of this template handle numeric and non-numeric,
78 * integral and non-integral, signed and unsigned C++ types.
79 * handleOption() makes sure to instantiate the right one.
81 * This implements default behavior when e.g. an option is
82 * unsigned but the user specifies a negative argument; etc.
84 template <class T
, bool is_numeric
, bool is_integer
>
85 struct OptionHandler
{
86 static T
handle(std::string option
, std::string optionarg
);
87 };/* struct OptionHandler<> */
89 /** Variant for integral C++ types */
91 struct OptionHandler
<T
, true, true> {
92 static T
handle(std::string option
, std::string optionarg
) {
94 Integer
i(optionarg
, 10);
96 if(! std::numeric_limits
<T
>::is_signed
&& i
< 0) {
97 // unsigned type but user gave negative argument
98 throw OptionException(option
+ " requires a nonnegative argument");
99 } else if(i
< std::numeric_limits
<T
>::min()) {
100 // negative overflow for type
101 std::stringstream ss
;
102 ss
<< option
<< " requires an argument >= " << std::numeric_limits
<T
>::min();
103 throw OptionException(ss
.str());
104 } else if(i
> std::numeric_limits
<T
>::max()) {
105 // positive overflow for type
106 std::stringstream ss
;
107 ss
<< option
<< " requires an argument <= " << std::numeric_limits
<T
>::max();
108 throw OptionException(ss
.str());
111 if(std::numeric_limits
<T
>::is_signed
) {
112 return T(i
.getLong());
114 return T(i
.getUnsignedLong());
116 } catch(std::invalid_argument
&) {
117 // user gave something other than an integer
118 throw OptionException(option
+ " requires an integer argument");
121 };/* struct OptionHandler<T, true, true> */
123 /** Variant for numeric but non-integral C++ types */
125 struct OptionHandler
<T
, true, false> {
126 static T
handle(std::string option
, std::string optionarg
) {
127 std::stringstream
in(optionarg
);
131 // we didn't consume the whole string (junk at end)
132 throw OptionException(option
+ " requires a numeric argument");
135 if(! std::numeric_limits
<T
>::is_signed
&& r
< 0.0) {
136 // unsigned type but user gave negative value
137 throw OptionException(option
+ " requires a nonnegative argument");
138 } else if(r
< -std::numeric_limits
<T
>::max()) {
139 // negative overflow for type
140 std::stringstream ss
;
141 ss
<< option
<< " requires an argument >= " << -std::numeric_limits
<T
>::max();
142 throw OptionException(ss
.str());
143 } else if(r
> std::numeric_limits
<T
>::max()) {
144 // positive overflow for type
145 std::stringstream ss
;
146 ss
<< option
<< " requires an argument <= " << std::numeric_limits
<T
>::max();
147 throw OptionException(ss
.str());
152 };/* struct OptionHandler<T, true, false> */
154 /** Variant for non-numeric C++ types */
156 struct OptionHandler
<T
, false, false> {
157 static T
handle(std::string option
, std::string optionarg
) {
158 T::unsupported_handleOption_call___please_write_me
;
159 // The above line causes a compiler error if this version of the template
160 // is ever instantiated (meaning that a specialization is missing). So
161 // don't worry about the segfault in the next line, the "return" is only
162 // there to keep the compiler from giving additional, distracting errors
166 };/* struct OptionHandler<T, false, false> */
168 /** Handle an option of type T in the default way. */
170 T
handleOption(std::string option
, std::string optionarg
) {
171 return OptionHandler
<T
, std::numeric_limits
<T
>::is_specialized
, std::numeric_limits
<T
>::is_integer
>::handle(option
, optionarg
);
174 /** Handle an option of type std::string in the default way. */
176 std::string handleOption
<std::string
>(std::string option
, std::string optionarg
) {
181 * Run handler, and any user-given predicates, for option T.
182 * If a user specifies a :handler or :predicates, it overrides this.
185 typename
T::type
runHandlerAndPredicates(T
, std::string option
, std::string optionarg
, SmtEngine
* smt
) {
186 // By default, parse the option argument in a way appropriate for its type.
187 // E.g., for "unsigned int" options, ensure that the provided argument is
188 // a nonnegative integer that fits in the unsigned int type.
190 return handleOption
<typename
T::type
>(option
, optionarg
);
194 void runBoolPredicates(T
, std::string option
, bool b
, SmtEngine
* smt
) {
195 // By default, nothing to do for bool. Users add things with
196 // :predicate in options files to provide custom checking routines
197 // that can throw exceptions.
200 $
{all_custom_handlers
}
202 #line 203 "${template}"
205 # define USE_EARLY_TYPE_CHECKING_BY_DEFAULT true
206 #else /* CVC4_DEBUG */
207 # define USE_EARLY_TYPE_CHECKING_BY_DEFAULT false
208 #endif /* CVC4_DEBUG */
210 #if defined(CVC4_MUZZLED) || defined(CVC4_COMPETITION_MODE)
211 # define DO_SEMANTIC_CHECKS_BY_DEFAULT false
212 #else /* CVC4_MUZZLED || CVC4_COMPETITION_MODE */
213 # define DO_SEMANTIC_CHECKS_BY_DEFAULT true
214 #endif /* CVC4_MUZZLED || CVC4_COMPETITION_MODE */
217 d_holder(new options::OptionsHolder()) {
220 Options::Options(const Options
& options
) :
221 d_holder(new options::OptionsHolder(*options
.d_holder
)) {
224 Options::~Options() {
228 options::OptionsHolder::OptionsHolder() : $
{all_modules_defaults
}
232 #line 233 "${template}"
234 static const std::string mostCommonOptionsDescription
= "\
235 Most commonly-used CVC4 options:${common_documentation}";
237 #line 238 "${template}"
239 static const std::string optionsDescription
= mostCommonOptionsDescription
+ "\n\
241 Additional CVC4 options:${remaining_documentation}";
243 #line 244 "${template}"
245 static const std::string optionsFootnote
= "\n\
246 [*] Each of these options has a --no-OPTIONNAME variant, which reverses the\n\
247 sense of the option.\n\
250 static const std::string languageDescription
= "\
251 Languages currently supported as arguments to the -L / --lang option:\n\
252 auto attempt to automatically determine language\n\
253 cvc4 | presentation | pl CVC4 presentation language\n\
254 smt1 | smtlib1 SMT-LIB format 1.2\n\
255 smt | smtlib | smt2 | smtlib2 SMT-LIB format 2.0\n\
256 tptp TPTP format (cnf and fof)\n\
258 Languages currently supported as arguments to the --output-lang option:\n\
259 auto match output language to input language\n\
260 cvc4 | presentation | pl CVC4 presentation language\n\
261 smt1 | smtlib1 SMT-LIB format 1.2\n\
262 smt | smtlib | smt2 | smtlib2 SMT-LIB format 2.0\n\
264 ast internal format (simple syntax trees)\n\
267 std::string
Options::getDescription() const {
268 return optionsDescription
;
271 void Options::printUsage(const std::string msg
, std::ostream
& out
) {
272 out
<< msg
<< optionsDescription
<< std::endl
273 << optionsFootnote
<< std::endl
<< std::flush
;
276 void Options::printShortUsage(const std::string msg
, std::ostream
& out
) {
277 out
<< msg
<< mostCommonOptionsDescription
<< std::endl
278 << optionsFootnote
<< std::endl
279 << "For full usage, please use --help." << std::endl
<< std::endl
<< std::flush
;
282 void Options::printLanguageHelp(std::ostream
& out
) {
283 out
<< languageDescription
<< std::flush
;
287 * This is a table of long options. By policy, each short option
288 * should have an equivalent long option (but the reverse isn't the
289 * case), so this table should thus contain all command-line options.
291 * Each option in this array has four elements:
293 * 1. the long option string
294 * 2. argument behavior for the option:
295 * no_argument - no argument permitted
296 * required_argument - an argument is expected
297 * optional_argument - an argument is permitted but not required
298 * 3. this is a pointer to an int which is set to the 4th entry of the
299 * array if the option is present; or NULL, in which case
300 * getopt_long() returns the 4th entry
301 * 4. the return value for getopt_long() when this long option (or the
302 * value to set the 3rd entry to; see #3)
304 * If you add something here, you should add it in src/main/usage.h
305 * also, to document it.
307 * If you add something that has a short option equivalent, you should
308 * add it to the getopt_long() call in parseOptions().
310 static struct option cmdlineOptions
[] = {$
{all_modules_long_options
}
311 { NULL
, no_argument
, NULL
, '\0' }
312 };/* cmdlineOptions */
314 #line 315 "${template}"
316 static void preemptGetopt(int& argc
, char**& argv
, const char* opt
) {
317 const size_t maxoptlen
= 128;
319 Debug("preemptGetopt") << "preempting getopt() with " << opt
<< std::endl
;
321 AlwaysAssert(opt
!= NULL
&& *opt
!= '\0');
322 AlwaysAssert(strlen(opt
) <= maxoptlen
);
326 while(argv
[i
] != NULL
&& argv
[i
][0] != '\0') {
330 if(argv
[i
] == NULL
) {
331 argv
= (char**) realloc(argv
, (i
+ 6) * sizeof(char*));
332 for(unsigned j
= i
; j
< i
+ 5; ++j
) {
333 argv
[j
] = (char*) malloc(sizeof(char) * maxoptlen
);
339 strncpy(argv
[i
], opt
, maxoptlen
- 1);
340 argv
[i
][maxoptlen
- 1] = '\0'; // ensure NUL-termination even on overflow
345 /** Set a given Options* as "current" just for a particular scope. */
347 CVC4_THREADLOCAL_TYPE(Options
*)* d_field
;
350 OptionsGuard(CVC4_THREADLOCAL_TYPE(Options
*)* field
, Options
* opts
) :
358 };/* class OptionsGuard */
360 }/* CVC4::options namespace */
363 * Parse argc/argv and put the result into a CVC4::Options.
364 * The return value is what's left of the command line (that is, the
365 * non-option arguments).
367 std::vector
<std::string
> Options::parseOptions(int argc
, char* main_argv
[]) throw(OptionException
) {
368 options::OptionsGuard
guard(&s_current
, this);
370 const char *progName
= main_argv
[0];
371 SmtEngine
* const smt
= NULL
;
373 Debug("options") << "main_argv == " << main_argv
<< std::endl
;
375 // Reset getopt(), in the case of multiple calls to parseOptions().
376 // This can be = 1 in newer GNU getopt, but older (< 2007) require = 0.
378 #if HAVE_DECL_OPTRESET
379 optreset
= 1; // on BSD getopt() (e.g. Mac OS), might need this
380 #endif /* HAVE_DECL_OPTRESET */
382 // find the base name of the program
383 const char *x
= strrchr(progName
, '/');
387 d_holder
->binary_name
= std::string(progName
);
390 char **extra_argv
= (char**) malloc(2 * sizeof(char*));
391 extra_argv
[0] = NULL
;
392 extra_argv
[1] = NULL
;
394 int extra_optind
= 0, main_optind
= 0;
396 int *optind_ref
= &main_optind
;
398 char** argv
= main_argv
;
400 std::vector
<std::string
> nonOptions
;
405 std::string option
, optionarg
;
406 Debug("preemptGetopt") << "top of loop, extra_optind == " << extra_optind
<< ", extra_argc == " << extra_argc
<< std::endl
;
407 if((extra_optind
== 0 ? 1 : extra_optind
) < extra_argc
) {
408 #if HAVE_DECL_OPTRESET
409 if(optind_ref
!= &extra_optind
) {
410 optreset
= 1; // on BSD getopt() (e.g. Mac OS), might need this
412 #endif /* HAVE_DECL_OPTRESET */
413 old_optind
= optind
= extra_optind
;
414 optind_ref
= &extra_optind
;
416 Debug("preemptGetopt") << "in preempt code, next arg is " << extra_argv
[optind
== 0 ? 1 : optind
] << std::endl
;
417 if(extra_argv
[extra_optind
== 0 ? 1 : extra_optind
][0] != '-') {
418 InternalError("preempted args cannot give non-options command-line args (found `%s')", extra_argv
[extra_optind
== 0 ? 1 : extra_optind
]);
420 c
= getopt_long(extra_argc
, extra_argv
,
421 "+:${all_modules_short_options}",
422 cmdlineOptions
, NULL
);
423 Debug("preemptGetopt") << "in preempt code, c == " << c
<< " (`" << char(c
) << "') optind == " << optind
<< std::endl
;
425 ( optopt
>= $
{long_option_value_begin
} && optopt
<= $
{long_option_value_end
} )) {
427 option
= argv
[old_optind
== 0 ? 1 : old_optind
];
428 optionarg
= (optarg
== NULL
) ? "" : optarg
;
431 option
= std::string("-") + char(optopt
);
432 optionarg
= (optarg
== NULL
) ? "" : optarg
;
434 if(optind
>= extra_argc
) {
435 Debug("preemptGetopt") << "-- no more preempt args" << std::endl
;
437 while(extra_argv
[i
] != NULL
&& extra_argv
[i
][0] != '\0') {
438 extra_argv
[i
][0] = '\0';
444 Debug("preemptGetopt") << "-- more preempt args" << std::endl
;
445 extra_optind
= optind
;
449 #if HAVE_DECL_OPTRESET
450 if(optind_ref
!= &main_optind
) {
451 optreset
= 1; // on BSD getopt() (e.g. Mac OS), might need this
453 #endif /* HAVE_DECL_OPTRESET */
454 old_optind
= optind
= main_optind
;
455 optind_ref
= &main_optind
;
457 if(main_optind
< argc
&& main_argv
[main_optind
][0] != '-') {
459 if(main_optind
!= 0) {
460 nonOptions
.push_back(main_argv
[main_optind
]);
463 } while(main_optind
< argc
&& main_argv
[main_optind
][0] != '-');
466 Debug("options") << "[ before, optind == " << optind
<< " ]" << std::endl
;
467 #if defined(__MINGW32__) || defined(__MINGW64__)
468 if(optreset
== 1 && optind
> 1) {
469 // on mingw, optreset will reset the optind, so we have to
470 // manually advance argc, argv
471 main_argv
[optind
- 1] = main_argv
[0];
472 argv
= main_argv
+= optind
- 1;
474 old_optind
= optind
= main_optind
= 1;
476 Debug("options") << "looking at : " << argv
[0] << std::endl
;
478 /*c = getopt_long(argc, main_argv,
479 "+:${all_modules_short_options}",
480 cmdlineOptions, NULL);
481 Debug("options") << "pre-emptory c is " << c << " (" << char(c) << ")" << std::endl;
482 Debug("options") << "optind was reset to " << optind << std::endl;
483 optind = main_optind;
484 Debug("options") << "I restored optind to " << optind << std::endl;*/
486 #endif /* __MINGW32__ || __MINGW64__ */
487 Debug("options") << "[ argc == " << argc
<< ", main_argv == " << main_argv
<< " ]" << std::endl
;
488 c
= getopt_long(argc
, main_argv
,
489 "+:${all_modules_short_options}",
490 cmdlineOptions
, NULL
);
491 main_optind
= optind
;
492 Debug("options") << "[ got " << int(c
) << " (" << char(c
) << ") ]" << std::endl
;
493 Debug("options") << "[ next option will be at pos: " << optind
<< " ]" << std::endl
;
495 Debug("options") << "done with option parsing" << std::endl
;
498 option
= argv
[old_optind
== 0 ? 1 : old_optind
];
499 optionarg
= (optarg
== NULL
) ? "" : optarg
;
502 Debug("preemptGetopt") << "processing option " << c
<< " (`" << char(c
) << "'), " << option
<< std::endl
;
505 $
{all_modules_option_handlers
}
507 #line 508 "${template}"
510 // This can be a long or short option, and the way to get at the
511 // name of it is different.
512 throw OptionException(std::string("option `") + option
+ "' missing its required argument");
516 if( ( optopt
== 0 || ( optopt
>= $
{long_option_value_begin
} && optopt
<= $
{long_option_value_end
} ) ) &&
517 !strncmp(argv
[optind
- 1], "--thread", 8) &&
518 strlen(argv
[optind
- 1]) > 8 ) {
519 if(! isdigit(argv
[optind
- 1][8])) {
520 throw OptionException(std::string("can't understand option `") + option
+ "': expected something like --threadN=\"--option1 --option2\", where N is a nonnegative integer");
522 std::vector
<std::string
>& threadArgv
= d_holder
->threadArgv
;
524 long tnum
= strtol(argv
[optind
- 1] + 8, &end
, 10);
525 if(tnum
< 0 || (*end
!= '\0' && *end
!= '=')) {
526 throw OptionException(std::string("can't understand option `") + option
+ "': expected something like --threadN=\"--option1 --option2\", where N is a nonnegative integer");
528 if(threadArgv
.size() <= size_t(tnum
)) {
529 threadArgv
.resize(tnum
+ 1);
531 if(threadArgv
[tnum
] != "") {
532 threadArgv
[tnum
] += " ";
534 if(*end
== '\0') { // e.g., we have --thread0 "foo"
536 throw OptionException(std::string("option `") + option
+ "' missing its required argument");
538 Debug("options") << "thread " << tnum
<< " gets option " << argv
[optind
] << std::endl
;
539 threadArgv
[tnum
] += argv
[(*optind_ref
)++];
540 } else { // e.g., we have --thread0="foo"
542 throw OptionException(std::string("option `") + option
+ "' missing its required argument");
544 Debug("options") << "thread " << tnum
<< " gets option " << (end
+ 1) << std::endl
;
545 threadArgv
[tnum
] += end
+ 1;
547 Debug("options") << "thread " << tnum
<< " now has " << threadArgv
[tnum
] << std::endl
;
551 throw OptionException(std::string("can't understand option `") + option
+ "'");
555 Debug("options") << "returning " << nonOptions
.size() << " non-option arguments." << std::endl
;
560 std::vector
<std::string
> Options::suggestCommandLineOptions(const std::string
& optionName
) throw() {
561 std::vector
<std::string
> suggestions
;
564 for(size_t i
= 0; (opt
= cmdlineOptions
[i
].name
) != NULL
; ++i
) {
565 if(std::strstr(opt
, optionName
.c_str()) != NULL
) {
566 suggestions
.push_back(opt
);
573 static const char* smtOptions
[] = {
574 $
{all_modules_smt_options
},
575 #line 576 "${template}"
579 std::vector
<std::string
> Options::suggestSmtOptions(const std::string
& optionName
) throw() {
580 std::vector
<std::string
> suggestions
;
583 for(size_t i
= 0; (opt
= smtOptions
[i
]) != NULL
; ++i
) {
584 if(std::strstr(opt
, optionName
.c_str()) != NULL
) {
585 suggestions
.push_back(opt
);
592 SExpr
Options::getOptions() const throw() {
593 std::vector
<SExpr
> opts
;
595 $
{all_modules_get_options
}
597 #line 598 "${template}"
602 #undef USE_EARLY_TYPE_CHECKING_BY_DEFAULT
603 #undef DO_SEMANTIC_CHECKS_BY_DEFAULT
605 }/* CVC4 namespace */