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; mingw 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__) */
29 #ifdef CVC4_IS_NOT_REALLY_BSD
31 #endif /* CVC4_IS_NOT_REALLY_BSD */
44 #include "expr/expr.h"
45 #include "util/configuration.h"
46 #include "util/exception.h"
47 #include "util/language.h"
50 $
{include_all_option_headers
}
52 #line 57 "${template}"
54 #include "util/output.h"
55 #include "options/options_holder.h"
56 #include "cvc4autoconfig.h"
57 #include "options/base_options_handlers.h"
59 $
{option_handler_includes
}
61 #line 66 "${template}"
64 using namespace CVC4::options
;
68 CVC4_THREADLOCAL(Options
*) Options::s_current
= NULL
;
71 * This is a default handler for options of built-in C++ type. This
72 * template is really just a helper for the handleOption() template,
73 * below. Variants of this template handle numeric and non-numeric,
74 * integral and non-integral, signed and unsigned C++ types.
75 * handleOption() makes sure to instantiate the right one.
77 * This implements default behavior when e.g. an option is
78 * unsigned but the user specifies a negative argument; etc.
80 template <class T
, bool is_numeric
, bool is_integer
>
81 struct OptionHandler
{
82 static T
handle(std::string option
, std::string optionarg
);
83 };/* struct OptionHandler<> */
85 /** Variant for integral C++ types */
87 struct OptionHandler
<T
, true, true> {
88 static T
handle(std::string option
, std::string optionarg
) {
90 Integer
i(optionarg
, 10);
92 if(! std::numeric_limits
<T
>::is_signed
&& i
< 0) {
93 // unsigned type but user gave negative argument
94 throw OptionException(option
+ " requires a nonnegative argument");
95 } else if(i
< std::numeric_limits
<T
>::min()) {
96 // negative overflow for type
98 ss
<< option
<< " requires an argument >= " << std::numeric_limits
<T
>::min();
99 throw OptionException(ss
.str());
100 } else if(i
> std::numeric_limits
<T
>::max()) {
101 // positive overflow for type
102 std::stringstream ss
;
103 ss
<< option
<< " requires an argument <= " << std::numeric_limits
<T
>::max();
104 throw OptionException(ss
.str());
107 if(std::numeric_limits
<T
>::is_signed
) {
108 return T(i
.getLong());
110 return T(i
.getUnsignedLong());
112 } catch(std::invalid_argument
&) {
113 // user gave something other than an integer
114 throw OptionException(option
+ " requires an integer argument");
117 };/* struct OptionHandler<T, true, true> */
119 /** Variant for numeric but non-integral C++ types */
121 struct OptionHandler
<T
, true, false> {
122 static T
handle(std::string option
, std::string optionarg
) {
123 std::stringstream
in(optionarg
);
127 // we didn't consume the whole string (junk at end)
128 throw OptionException(option
+ " requires a numeric argument");
131 if(! std::numeric_limits
<T
>::is_signed
&& r
< 0.0) {
132 // unsigned type but user gave negative value
133 throw OptionException(option
+ " requires a nonnegative argument");
134 } else if(r
< -std::numeric_limits
<T
>::max()) {
135 // negative overflow for type
136 std::stringstream ss
;
137 ss
<< option
<< " requires an argument >= " << -std::numeric_limits
<T
>::max();
138 throw OptionException(ss
.str());
139 } else if(r
> std::numeric_limits
<T
>::max()) {
140 // positive overflow for type
141 std::stringstream ss
;
142 ss
<< option
<< " requires an argument <= " << std::numeric_limits
<T
>::max();
143 throw OptionException(ss
.str());
148 };/* struct OptionHandler<T, true, false> */
150 /** Variant for non-numeric C++ types */
152 struct OptionHandler
<T
, false, false> {
153 static T
handle(std::string option
, std::string optionarg
) {
154 T::unsupported_handleOption_call___please_write_me
;
155 // The above line causes a compiler error if this version of the template
156 // is ever instantiated (meaning that a specialization is missing). So
157 // don't worry about the segfault in the next line, the "return" is only
158 // there to keep the compiler from giving additional, distracting errors
162 };/* struct OptionHandler<T, false, false> */
164 /** Handle an option of type T in the default way. */
166 T
handleOption(std::string option
, std::string optionarg
) {
167 return OptionHandler
<T
, std::numeric_limits
<T
>::is_specialized
, std::numeric_limits
<T
>::is_integer
>::handle(option
, optionarg
);
170 /** Handle an option of type std::string in the default way. */
172 std::string handleOption
<std::string
>(std::string option
, std::string optionarg
) {
177 * Run handler, and any user-given predicates, for option T.
178 * If a user specifies a :handler or :predicates, it overrides this.
181 typename
T::type
runHandlerAndPredicates(T
, std::string option
, std::string optionarg
, SmtEngine
* smt
) {
182 // By default, parse the option argument in a way appropriate for its type.
183 // E.g., for "unsigned int" options, ensure that the provided argument is
184 // a nonnegative integer that fits in the unsigned int type.
186 return handleOption
<typename
T::type
>(option
, optionarg
);
190 void runBoolPredicates(T
, std::string option
, bool b
, SmtEngine
* smt
) {
191 // By default, nothing to do for bool. Users add things with
192 // :predicate in options files to provide custom checking routines
193 // that can throw exceptions.
196 $
{all_custom_handlers
}
198 #line 203 "${template}"
201 # define USE_EARLY_TYPE_CHECKING_BY_DEFAULT true
202 #else /* CVC4_DEBUG */
203 # define USE_EARLY_TYPE_CHECKING_BY_DEFAULT false
204 #endif /* CVC4_DEBUG */
206 #if defined(CVC4_MUZZLED) || defined(CVC4_COMPETITION_MODE)
207 # define DO_SEMANTIC_CHECKS_BY_DEFAULT false
208 #else /* CVC4_MUZZLED || CVC4_COMPETITION_MODE */
209 # define DO_SEMANTIC_CHECKS_BY_DEFAULT true
210 #endif /* CVC4_MUZZLED || CVC4_COMPETITION_MODE */
213 d_holder(new options::OptionsHolder()) {
216 Options::Options(const Options
& options
) :
217 d_holder(new options::OptionsHolder(*options
.d_holder
)) {
220 Options::~Options() {
224 options::OptionsHolder::OptionsHolder() : $
{all_modules_defaults
}
228 #line 233 "${template}"
230 static const std::string mostCommonOptionsDescription
= "\
231 Most commonly-used CVC4 options:${common_documentation}";
233 #line 238 "${template}"
235 static const std::string optionsDescription
= mostCommonOptionsDescription
+ "\n\
237 Additional CVC4 options:${remaining_documentation}";
239 #line 244 "${template}"
241 static const std::string optionsFootnote
= "\n\
242 [*] Each of these options has a --no-OPTIONNAME variant, which reverses the\n\
243 sense of the option.\n\
246 static const std::string languageDescription
= "\
247 Languages currently supported as arguments to the -L / --lang option:\n\
248 auto attempt to automatically determine language\n\
249 cvc4 | presentation | pl CVC4 presentation language\n\
250 smt1 | smtlib1 SMT-LIB format 1.2\n\
251 smt | smtlib | smt2 | smtlib2 SMT-LIB format 2.0\n\
252 tptp TPTP format (cnf and fof)\n\
254 Languages currently supported as arguments to the --output-lang option:\n\
255 auto match output language to input language\n\
256 cvc4 | presentation | pl CVC4 presentation language\n\
257 smt1 | smtlib1 SMT-LIB format 1.2\n\
258 smt | smtlib | smt2 | smtlib2 SMT-LIB format 2.0\n\
260 ast internal format (simple syntax trees)\n\
263 std::string
Options::getDescription() const {
264 return optionsDescription
;
267 void Options::printUsage(const std::string msg
, std::ostream
& out
) {
268 out
<< msg
<< optionsDescription
<< std::endl
269 << optionsFootnote
<< std::endl
<< std::flush
;
272 void Options::printShortUsage(const std::string msg
, std::ostream
& out
) {
273 out
<< msg
<< mostCommonOptionsDescription
<< std::endl
274 << optionsFootnote
<< std::endl
275 << "For full usage, please use --help." << std::endl
<< std::endl
<< std::flush
;
278 void Options::printLanguageHelp(std::ostream
& out
) {
279 out
<< languageDescription
<< std::flush
;
283 * This is a table of long options. By policy, each short option
284 * should have an equivalent long option (but the reverse isn't the
285 * case), so this table should thus contain all command-line options.
287 * Each option in this array has four elements:
289 * 1. the long option string
290 * 2. argument behavior for the option:
291 * no_argument - no argument permitted
292 * required_argument - an argument is expected
293 * optional_argument - an argument is permitted but not required
294 * 3. this is a pointer to an int which is set to the 4th entry of the
295 * array if the option is present; or NULL, in which case
296 * getopt_long() returns the 4th entry
297 * 4. the return value for getopt_long() when this long option (or the
298 * value to set the 3rd entry to; see #3)
300 * If you add something here, you should add it in src/main/usage.h
301 * also, to document it.
303 * If you add something that has a short option equivalent, you should
304 * add it to the getopt_long() call in parseOptions().
306 static struct option cmdlineOptions
[] = {$
{all_modules_long_options
}
307 { NULL
, no_argument
, NULL
, '\0' }
308 };/* cmdlineOptions */
310 #line 315 "${template}"
312 static void preemptGetopt(int& argc
, char**& argv
, const char* opt
) {
313 const size_t maxoptlen
= 128;
315 Debug("preemptGetopt") << "preempting getopt() with " << opt
<< std::endl
;
317 AlwaysAssert(opt
!= NULL
&& *opt
!= '\0');
318 AlwaysAssert(strlen(opt
) <= maxoptlen
);
322 while(argv
[i
] != NULL
&& argv
[i
][0] != '\0') {
326 if(argv
[i
] == NULL
) {
327 argv
= (char**) realloc(argv
, (i
+ 6) * sizeof(char*));
328 for(unsigned j
= i
; j
< i
+ 5; ++j
) {
329 argv
[j
] = (char*) malloc(sizeof(char) * maxoptlen
);
335 strncpy(argv
[i
], opt
, maxoptlen
- 1);
336 argv
[i
][maxoptlen
- 1] = '\0'; // ensure NUL-termination even on overflow
341 /** Set a given Options* as "current" just for a particular scope. */
343 CVC4_THREADLOCAL_TYPE(Options
*)* d_field
;
346 OptionsGuard(CVC4_THREADLOCAL_TYPE(Options
*)* field
, Options
* opts
) :
354 };/* class OptionsGuard */
356 }/* CVC4::options namespace */
359 * Parse argc/argv and put the result into a CVC4::Options.
360 * The return value is what's left of the command line (that is, the
361 * non-option arguments).
363 std::vector
<std::string
> Options::parseOptions(int argc
, char* main_argv
[]) throw(OptionException
) {
364 options::OptionsGuard
guard(&s_current
, this);
366 const char *progName
= main_argv
[0];
367 SmtEngine
* const smt
= NULL
;
369 Debug("options") << "main_argv == " << main_argv
<< std::endl
;
371 // Reset getopt(), in the case of multiple calls to parseOptions().
372 // This can be = 1 in newer GNU getopt, but older (< 2007) require = 0.
374 #if HAVE_DECL_OPTRESET
375 optreset
= 1; // on BSD getopt() (e.g. Mac OS), might need this
376 #endif /* HAVE_DECL_OPTRESET */
378 // find the base name of the program
379 const char *x
= strrchr(progName
, '/');
383 d_holder
->binary_name
= std::string(progName
);
386 char **extra_argv
= (char**) malloc(2 * sizeof(char*));
387 extra_argv
[0] = NULL
;
388 extra_argv
[1] = NULL
;
390 int extra_optind
= 0, main_optind
= 0;
392 int *optind_ref
= &main_optind
;
394 char** argv
= main_argv
;
396 std::vector
<std::string
> nonOptions
;
401 std::string option
, optionarg
;
402 Debug("preemptGetopt") << "top of loop, extra_optind == " << extra_optind
<< ", extra_argc == " << extra_argc
<< std::endl
;
403 if((extra_optind
== 0 ? 1 : extra_optind
) < extra_argc
) {
404 #if HAVE_DECL_OPTRESET
405 if(optind_ref
!= &extra_optind
) {
406 optreset
= 1; // on BSD getopt() (e.g. Mac OS), might need this
408 #endif /* HAVE_DECL_OPTRESET */
409 old_optind
= optind
= extra_optind
;
410 optind_ref
= &extra_optind
;
412 Debug("preemptGetopt") << "in preempt code, next arg is " << extra_argv
[optind
== 0 ? 1 : optind
] << std::endl
;
413 if(extra_argv
[extra_optind
== 0 ? 1 : extra_optind
][0] != '-') {
414 InternalError("preempted args cannot give non-options command-line args (found `%s')", extra_argv
[extra_optind
== 0 ? 1 : extra_optind
]);
416 c
= getopt_long(extra_argc
, extra_argv
,
417 "+:${all_modules_short_options}",
418 cmdlineOptions
, NULL
);
419 Debug("preemptGetopt") << "in preempt code, c == " << c
<< " (`" << char(c
) << "') optind == " << optind
<< std::endl
;
421 ( optopt
>= $
{long_option_value_begin
} && optopt
<= $
{long_option_value_end
} )) {
423 option
= argv
[old_optind
== 0 ? 1 : old_optind
];
424 optionarg
= (optarg
== NULL
) ? "" : optarg
;
427 option
= std::string("-") + char(optopt
);
428 optionarg
= (optarg
== NULL
) ? "" : optarg
;
430 if(optind
>= extra_argc
) {
431 Debug("preemptGetopt") << "-- no more preempt args" << std::endl
;
433 while(extra_argv
[i
] != NULL
&& extra_argv
[i
][0] != '\0') {
434 extra_argv
[i
][0] = '\0';
440 Debug("preemptGetopt") << "-- more preempt args" << std::endl
;
441 extra_optind
= optind
;
445 #if HAVE_DECL_OPTRESET
446 if(optind_ref
!= &main_optind
) {
447 optreset
= 1; // on BSD getopt() (e.g. Mac OS), might need this
449 #endif /* HAVE_DECL_OPTRESET */
450 old_optind
= optind
= main_optind
;
451 optind_ref
= &main_optind
;
453 if(main_optind
< argc
&& main_argv
[main_optind
][0] != '-') {
455 if(main_optind
!= 0) {
456 nonOptions
.push_back(main_argv
[main_optind
]);
459 } while(main_optind
< argc
&& main_argv
[main_optind
][0] != '-');
462 Debug("options") << "[ before, optind == " << optind
<< " ]" << std::endl
;
463 #if defined(__MINGW32__) || defined(__MINGW64__)
464 if(optreset
== 1 && optind
> 1) {
465 // on mingw, optreset will reset the optind, so we have to
466 // manually advance argc, argv
467 main_argv
[optind
- 1] = main_argv
[0];
468 argv
= main_argv
+= optind
- 1;
470 old_optind
= optind
= main_optind
= 1;
472 Debug("options") << "looking at : " << argv
[0] << std::endl
;
474 /*c = getopt_long(argc, main_argv,
475 "+:${all_modules_short_options}",
476 cmdlineOptions, NULL);
477 Debug("options") << "pre-emptory c is " << c << " (" << char(c) << ")" << std::endl;
478 Debug("options") << "optind was reset to " << optind << std::endl;
479 optind = main_optind;
480 Debug("options") << "I restored optind to " << optind << std::endl;*/
482 #endif /* __MINGW32__ || __MINGW64__ */
483 Debug("options") << "[ argc == " << argc
<< ", main_argv == " << main_argv
<< " ]" << std::endl
;
484 c
= getopt_long(argc
, main_argv
,
485 "+:${all_modules_short_options}",
486 cmdlineOptions
, NULL
);
487 main_optind
= optind
;
488 Debug("options") << "[ got " << int(c
) << " (" << char(c
) << ") ]" << std::endl
;
489 Debug("options") << "[ next option will be at pos: " << optind
<< " ]" << std::endl
;
491 Debug("options") << "done with option parsing" << std::endl
;
494 option
= argv
[old_optind
== 0 ? 1 : old_optind
];
495 optionarg
= (optarg
== NULL
) ? "" : optarg
;
498 Debug("preemptGetopt") << "processing option " << c
<< " (`" << char(c
) << "'), " << option
<< std::endl
;
501 $
{all_modules_option_handlers
}
503 #line 508 "${template}"
506 // This can be a long or short option, and the way to get at the
507 // name of it is different.
508 throw OptionException(std::string("option `") + option
+ "' missing its required argument");
512 if( ( optopt
== 0 || ( optopt
>= $
{long_option_value_begin
} && optopt
<= $
{long_option_value_end
} ) ) &&
513 !strncmp(argv
[optind
- 1], "--thread", 8) &&
514 strlen(argv
[optind
- 1]) > 8 ) {
515 if(! isdigit(argv
[optind
- 1][8])) {
516 throw OptionException(std::string("can't understand option `") + option
+ "': expected something like --threadN=\"--option1 --option2\", where N is a nonnegative integer");
518 std::vector
<std::string
>& threadArgv
= d_holder
->threadArgv
;
520 long tnum
= strtol(argv
[optind
- 1] + 8, &end
, 10);
521 if(tnum
< 0 || (*end
!= '\0' && *end
!= '=')) {
522 throw OptionException(std::string("can't understand option `") + option
+ "': expected something like --threadN=\"--option1 --option2\", where N is a nonnegative integer");
524 if(threadArgv
.size() <= size_t(tnum
)) {
525 threadArgv
.resize(tnum
+ 1);
527 if(threadArgv
[tnum
] != "") {
528 threadArgv
[tnum
] += " ";
530 if(*end
== '\0') { // e.g., we have --thread0 "foo"
532 throw OptionException(std::string("option `") + option
+ "' missing its required argument");
534 Debug("options") << "thread " << tnum
<< " gets option " << argv
[optind
] << std::endl
;
535 threadArgv
[tnum
] += argv
[(*optind_ref
)++];
536 } else { // e.g., we have --thread0="foo"
538 throw OptionException(std::string("option `") + option
+ "' missing its required argument");
540 Debug("options") << "thread " << tnum
<< " gets option " << (end
+ 1) << std::endl
;
541 threadArgv
[tnum
] += end
+ 1;
543 Debug("options") << "thread " << tnum
<< " now has " << threadArgv
[tnum
] << std::endl
;
547 throw OptionException(std::string("can't understand option `") + option
+ "'");
551 if((*this)[options::incrementalSolving
] && (*this)[options::proof
]) {
552 throw OptionException(std::string("The use of --incremental with --proof is not yet supported"));
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 590 "${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 612 "${template}"
602 #undef USE_EARLY_TYPE_CHECKING_BY_DEFAULT
603 #undef DO_SEMANTIC_CHECKS_BY_DEFAULT
605 }/* CVC4 namespace */