Merge branch 'master' of https://github.com/CVC4/CVC4
[cvc5.git] / src / options / options_template.cpp
1 /********************* */
2 /*! \file options_template.cpp
3 ** \verbatim
4 ** Top contributors (to current version):
5 ** Tim King, Morgan Deters, Kshitij Bansal
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2016 by the authors listed in the file AUTHORS
8 ** in the top-level source directory) and their institutional affiliations.
9 ** All rights reserved. See the file COPYING in the top-level source
10 ** directory for licensing information.\endverbatim
11 **
12 ** \brief Contains code for handling command-line options.
13 **
14 ** Contains code for handling command-line options
15 **/
16
17 #if !defined(_BSD_SOURCE) && defined(__MINGW32__) && !defined(__MINGW64__)
18 // force use of optreset; mingw32 croaks on argv-switching otherwise
19 # include "cvc4autoconfig.h"
20 # define _BSD_SOURCE
21 # undef HAVE_DECL_OPTRESET
22 # define HAVE_DECL_OPTRESET 1
23 # define CVC4_IS_NOT_REALLY_BSD
24 #endif /* !_BSD_SOURCE && __MINGW32__ && !__MINGW64__ */
25
26 #ifdef __MINGW64__
27 extern int optreset;
28 #endif /* __MINGW64__ */
29
30 #include <getopt.h>
31
32 // clean up
33 #ifdef CVC4_IS_NOT_REALLY_BSD
34 # undef _BSD_SOURCE
35 #endif /* CVC4_IS_NOT_REALLY_BSD */
36
37 #include <unistd.h>
38 #include <string.h>
39 #include <stdint.h>
40 #include <time.h>
41
42 #include <cstdio>
43 #include <cstdlib>
44 #include <cstring>
45 #include <iomanip>
46 #include <new>
47 #include <string>
48 #include <sstream>
49 #include <limits>
50
51 #include "base/cvc4_assert.h"
52 #include "base/exception.h"
53 #include "base/output.h"
54 #include "base/tls.h"
55 #include "options/argument_extender.h"
56 #include "options/argument_extender_implementation.h"
57 #include "options/didyoumean.h"
58 #include "options/language.h"
59 #include "options/options_handler.h"
60
61 ${include_all_option_headers}
62
63 #line 64 "${template}"
64
65 #include "options/options_holder.h"
66 #include "cvc4autoconfig.h"
67 #include "options/base_handlers.h"
68
69 ${option_handler_includes}
70
71 #line 72 "${template}"
72
73 using namespace CVC4;
74 using namespace CVC4::options;
75
76 namespace CVC4 {
77
78 CVC4_THREADLOCAL(Options*) Options::s_current = NULL;
79
80
81
82 /**
83 * This is a default handler for options of built-in C++ type. This
84 * template is really just a helper for the handleOption() template,
85 * below. Variants of this template handle numeric and non-numeric,
86 * integral and non-integral, signed and unsigned C++ types.
87 * handleOption() makes sure to instantiate the right one.
88 *
89 * This implements default behavior when e.g. an option is
90 * unsigned but the user specifies a negative argument; etc.
91 */
92 template <class T, bool is_numeric, bool is_integer>
93 struct OptionHandler {
94 static T handle(std::string option, std::string optionarg);
95 };/* struct OptionHandler<> */
96
97 /** Variant for integral C++ types */
98 template <class T>
99 struct OptionHandler<T, true, true> {
100 static bool stringToInt(T& t, const std::string& str) {
101 std::istringstream ss(str);
102 ss >> t;
103 char tmp;
104 return !(ss.fail() || ss.get(tmp));
105 }
106
107 static bool containsMinus(const std::string& str) {
108 return str.find('-') != std::string::npos;
109 }
110
111 static T handle(const std::string& option, const std::string& optionarg) {
112 try {
113 T i;
114 bool success = stringToInt(i, optionarg);
115
116 if(!success){
117 throw OptionException(option + ": failed to parse "+ optionarg +
118 " as an integer of the appropraite type.");
119 }
120
121 // Depending in the platform unsigned numbers with '-' signs may parse.
122 // Reject these by looking for any minus if it is not signed.
123 if( (! std::numeric_limits<T>::is_signed) && containsMinus(optionarg) ) {
124 // unsigned type but user gave negative argument
125 throw OptionException(option + " requires a nonnegative argument");
126 } else if(i < std::numeric_limits<T>::min()) {
127 // negative overflow for type
128 std::stringstream ss;
129 ss << option << " requires an argument >= "
130 << std::numeric_limits<T>::min();
131 throw OptionException(ss.str());
132 } else if(i > std::numeric_limits<T>::max()) {
133 // positive overflow for type
134 std::stringstream ss;
135 ss << option << " requires an argument <= "
136 << std::numeric_limits<T>::max();
137 throw OptionException(ss.str());
138 }
139
140 return i;
141
142 // if(std::numeric_limits<T>::is_signed) {
143 // return T(i.getLong());
144 // } else {
145 // return T(i.getUnsignedLong());
146 // }
147 } catch(std::invalid_argument&) {
148 // user gave something other than an integer
149 throw OptionException(option + " requires an integer argument");
150 }
151 }
152 };/* struct OptionHandler<T, true, true> */
153
154 /** Variant for numeric but non-integral C++ types */
155 template <class T>
156 struct OptionHandler<T, true, false> {
157 static T handle(std::string option, std::string optionarg) {
158 std::stringstream in(optionarg);
159 long double r;
160 in >> r;
161 if(! in.eof()) {
162 // we didn't consume the whole string (junk at end)
163 throw OptionException(option + " requires a numeric argument");
164 }
165
166 if(! std::numeric_limits<T>::is_signed && r < 0.0) {
167 // unsigned type but user gave negative value
168 throw OptionException(option + " requires a nonnegative argument");
169 } else if(r < -std::numeric_limits<T>::max()) {
170 // negative overflow for type
171 std::stringstream ss;
172 ss << option << " requires an argument >= "
173 << -std::numeric_limits<T>::max();
174 throw OptionException(ss.str());
175 } else if(r > std::numeric_limits<T>::max()) {
176 // positive overflow for type
177 std::stringstream ss;
178 ss << option << " requires an argument <= "
179 << std::numeric_limits<T>::max();
180 throw OptionException(ss.str());
181 }
182
183 return T(r);
184 }
185 };/* struct OptionHandler<T, true, false> */
186
187 /** Variant for non-numeric C++ types */
188 template <class T>
189 struct OptionHandler<T, false, false> {
190 static T handle(std::string option, std::string optionarg) {
191 T::unsupported_handleOption_call___please_write_me;
192 // The above line causes a compiler error if this version of the template
193 // is ever instantiated (meaning that a specialization is missing). So
194 // don't worry about the segfault in the next line, the "return" is only
195 // there to keep the compiler from giving additional, distracting errors
196 // and warnings.
197 return *(T*)0;
198 }
199 };/* struct OptionHandler<T, false, false> */
200
201 /** Handle an option of type T in the default way. */
202 template <class T>
203 T handleOption(std::string option, std::string optionarg) {
204 return OptionHandler<T, std::numeric_limits<T>::is_specialized, std::numeric_limits<T>::is_integer>::handle(option, optionarg);
205 }
206
207 /** Handle an option of type std::string in the default way. */
208 template <>
209 std::string handleOption<std::string>(std::string option, std::string optionarg) {
210 return optionarg;
211 }
212
213 /**
214 * Run handler, and any user-given predicates, for option T.
215 * If a user specifies a :handler or :predicates, it overrides this.
216 */
217 template <class T>
218 typename T::type runHandlerAndPredicates(T, std::string option, std::string optionarg, options::OptionsHandler* handler) {
219 // By default, parse the option argument in a way appropriate for its type.
220 // E.g., for "unsigned int" options, ensure that the provided argument is
221 // a nonnegative integer that fits in the unsigned int type.
222
223 return handleOption<typename T::type>(option, optionarg);
224 }
225
226 template <class T>
227 void runBoolPredicates(T, std::string option, bool b, options::OptionsHandler* handler) {
228 // By default, nothing to do for bool. Users add things with
229 // :predicate in options files to provide custom checking routines
230 // that can throw exceptions.
231 }
232
233
234 Options::Options()
235 : d_holder(new options::OptionsHolder())
236 , d_handler(new options::OptionsHandler(this))
237 , d_forceLogicListeners()
238 , d_beforeSearchListeners()
239 , d_tlimitListeners()
240 , d_tlimitPerListeners()
241 , d_rlimitListeners()
242 , d_rlimitPerListeners()
243 {}
244
245 Options::~Options() {
246 delete d_handler;
247 delete d_holder;
248 }
249
250 void Options::copyValues(const Options& options){
251 if(this != &options) {
252 delete d_holder;
253 d_holder = new options::OptionsHolder(*options.d_holder);
254 }
255 }
256
257 std::string Options::formatThreadOptionException(const std::string& option) {
258 std::stringstream ss;
259 ss << "can't understand option `" << option
260 << "': expected something like --threadN=\"--option1 --option2\","
261 << " where N is a nonnegative integer";
262 return ss.str();
263 }
264
265 ListenerCollection::Registration* Options::registerAndNotify(
266 ListenerCollection& collection, Listener* listener, bool notify)
267 {
268 ListenerCollection::Registration* registration =
269 collection.registerListener(listener);
270 if(notify) {
271 listener->notify();
272 }
273 return registration;
274 }
275
276 ListenerCollection::Registration* Options::registerForceLogicListener(
277 Listener* listener, bool notifyIfSet)
278 {
279 bool notify = notifyIfSet && wasSetByUser(options::forceLogicString);
280 return registerAndNotify(d_forceLogicListeners, listener, notify);
281 }
282
283 ListenerCollection::Registration* Options::registerBeforeSearchListener(
284 Listener* listener)
285 {
286 return d_beforeSearchListeners.registerListener(listener);
287 }
288
289 ListenerCollection::Registration* Options::registerTlimitListener(
290 Listener* listener, bool notifyIfSet)
291 {
292 bool notify = notifyIfSet &&
293 wasSetByUser(options::cumulativeMillisecondLimit);
294 return registerAndNotify(d_tlimitListeners, listener, notify);
295 }
296
297 ListenerCollection::Registration* Options::registerTlimitPerListener(
298 Listener* listener, bool notifyIfSet)
299 {
300 bool notify = notifyIfSet && wasSetByUser(options::perCallMillisecondLimit);
301 return registerAndNotify(d_tlimitPerListeners, listener, notify);
302 }
303
304 ListenerCollection::Registration* Options::registerRlimitListener(
305 Listener* listener, bool notifyIfSet)
306 {
307 bool notify = notifyIfSet && wasSetByUser(options::cumulativeResourceLimit);
308 return registerAndNotify(d_rlimitListeners, listener, notify);
309 }
310
311 ListenerCollection::Registration* Options::registerRlimitPerListener(
312 Listener* listener, bool notifyIfSet)
313 {
314 bool notify = notifyIfSet && wasSetByUser(options::perCallResourceLimit);
315 return registerAndNotify(d_rlimitPerListeners, listener, notify);
316 }
317
318 ListenerCollection::Registration* Options::registerUseTheoryListListener(
319 Listener* listener, bool notifyIfSet)
320 {
321 bool notify = notifyIfSet && wasSetByUser(options::useTheoryList);
322 return registerAndNotify(d_useTheoryListListeners, listener, notify);
323 }
324
325 ListenerCollection::Registration* Options::registerSetDefaultExprDepthListener(
326 Listener* listener, bool notifyIfSet)
327 {
328 bool notify = notifyIfSet && wasSetByUser(options::defaultExprDepth);
329 return registerAndNotify(d_setDefaultExprDepthListeners, listener, notify);
330 }
331
332 ListenerCollection::Registration* Options::registerSetDefaultExprDagListener(
333 Listener* listener, bool notifyIfSet)
334 {
335 bool notify = notifyIfSet && wasSetByUser(options::defaultDagThresh);
336 return registerAndNotify(d_setDefaultDagThreshListeners, listener, notify);
337 }
338
339 ListenerCollection::Registration* Options::registerSetPrintExprTypesListener(
340 Listener* listener, bool notifyIfSet)
341 {
342 bool notify = notifyIfSet && wasSetByUser(options::printExprTypes);
343 return registerAndNotify(d_setPrintExprTypesListeners, listener, notify);
344 }
345
346 ListenerCollection::Registration* Options::registerSetDumpModeListener(
347 Listener* listener, bool notifyIfSet)
348 {
349 bool notify = notifyIfSet && wasSetByUser(options::dumpModeString);
350 return registerAndNotify(d_setDumpModeListeners, listener, notify);
351 }
352
353 ListenerCollection::Registration* Options::registerSetPrintSuccessListener(
354 Listener* listener, bool notifyIfSet)
355 {
356 bool notify = notifyIfSet && wasSetByUser(options::printSuccess);
357 return registerAndNotify(d_setPrintSuccessListeners, listener, notify);
358 }
359
360 ListenerCollection::Registration* Options::registerDumpToFileNameListener(
361 Listener* listener, bool notifyIfSet)
362 {
363 bool notify = notifyIfSet && wasSetByUser(options::dumpToFileName);
364 return registerAndNotify(d_dumpToFileListeners, listener, notify);
365 }
366
367 ListenerCollection::Registration*
368 Options::registerSetRegularOutputChannelListener(
369 Listener* listener, bool notifyIfSet)
370 {
371 bool notify = notifyIfSet && wasSetByUser(options::regularChannelName);
372 return registerAndNotify(d_setRegularChannelListeners, listener, notify);
373 }
374
375 ListenerCollection::Registration*
376 Options::registerSetDiagnosticOutputChannelListener(
377 Listener* listener, bool notifyIfSet)
378 {
379 bool notify = notifyIfSet && wasSetByUser(options::diagnosticChannelName);
380 return registerAndNotify(d_setDiagnosticChannelListeners, listener, notify);
381 }
382
383 ListenerCollection::Registration*
384 Options::registerSetReplayLogFilename(
385 Listener* listener, bool notifyIfSet)
386 {
387 bool notify = notifyIfSet && wasSetByUser(options::replayLogFilename);
388 return registerAndNotify(d_setReplayFilenameListeners, listener, notify);
389 }
390
391 ${all_custom_handlers}
392
393 #line 394 "${template}"
394
395 #ifdef CVC4_DEBUG
396 # define USE_EARLY_TYPE_CHECKING_BY_DEFAULT true
397 #else /* CVC4_DEBUG */
398 # define USE_EARLY_TYPE_CHECKING_BY_DEFAULT false
399 #endif /* CVC4_DEBUG */
400
401 #if defined(CVC4_MUZZLED) || defined(CVC4_COMPETITION_MODE)
402 # define DO_SEMANTIC_CHECKS_BY_DEFAULT false
403 #else /* CVC4_MUZZLED || CVC4_COMPETITION_MODE */
404 # define DO_SEMANTIC_CHECKS_BY_DEFAULT true
405 #endif /* CVC4_MUZZLED || CVC4_COMPETITION_MODE */
406
407 options::OptionsHolder::OptionsHolder() : ${all_modules_defaults}
408 {
409 }
410
411 #line 412 "${template}"
412
413 static const std::string mostCommonOptionsDescription = "\
414 Most commonly-used CVC4 options:${common_documentation}";
415
416 #line 417 "${template}"
417
418 static const std::string optionsDescription = mostCommonOptionsDescription + "\n\
419 \n\
420 Additional CVC4 options:${remaining_documentation}";
421
422 #line 423 "${template}"
423
424 static const std::string optionsFootnote = "\n\
425 [*] Each of these options has a --no-OPTIONNAME variant, which reverses the\n\
426 sense of the option.\n\
427 ";
428
429 static const std::string languageDescription = "\
430 Languages currently supported as arguments to the -L / --lang option:\n\
431 auto attempt to automatically determine language\n\
432 cvc4 | presentation | pl CVC4 presentation language\n\
433 smt1 | smtlib1 SMT-LIB format 1.2\n\
434 smt | smtlib | smt2 |\n\
435 smt2.0 | smtlib2 | smtlib2.0 SMT-LIB format 2.0\n\
436 smt2.5 | smtlib2.5 SMT-LIB format 2.5\n\
437 tptp TPTP format (cnf and fof)\n\
438 sygus SyGuS format\n\
439 \n\
440 Languages currently supported as arguments to the --output-lang option:\n\
441 auto match output language to input language\n\
442 cvc4 | presentation | pl CVC4 presentation language\n\
443 cvc3 CVC3 presentation language\n\
444 smt1 | smtlib1 SMT-LIB format 1.2\n\
445 smt | smtlib | smt2 |\n\
446 smt2.0 | smtlib2.0 | smtlib2 SMT-LIB format 2.0\n\
447 smt2.5 | smtlib2.5 SMT-LIB format 2.5\n\
448 tptp TPTP format\n\
449 z3str SMT-LIB 2.0 with Z3-str string constraints\n\
450 ast internal format (simple syntax trees)\n\
451 ";
452
453 std::string Options::getDescription() const {
454 return optionsDescription;
455 }
456
457 void Options::printUsage(const std::string msg, std::ostream& out) {
458 out << msg << optionsDescription << std::endl
459 << optionsFootnote << std::endl << std::flush;
460 }
461
462 void Options::printShortUsage(const std::string msg, std::ostream& out) {
463 out << msg << mostCommonOptionsDescription << std::endl
464 << optionsFootnote << std::endl
465 << "For full usage, please use --help."
466 << std::endl << std::endl << std::flush;
467 }
468
469 void Options::printLanguageHelp(std::ostream& out) {
470 out << languageDescription << std::flush;
471 }
472
473 /**
474 * This is a table of long options. By policy, each short option
475 * should have an equivalent long option (but the reverse isn't the
476 * case), so this table should thus contain all command-line options.
477 *
478 * Each option in this array has four elements:
479 *
480 * 1. the long option string
481 * 2. argument behavior for the option:
482 * no_argument - no argument permitted
483 * required_argument - an argument is expected
484 * optional_argument - an argument is permitted but not required
485 * 3. this is a pointer to an int which is set to the 4th entry of the
486 * array if the option is present; or NULL, in which case
487 * getopt_long() returns the 4th entry
488 * 4. the return value for getopt_long() when this long option (or the
489 * value to set the 3rd entry to; see #3)
490 *
491 * If you add something here, you should add it in src/main/usage.h
492 * also, to document it.
493 *
494 * If you add something that has a short option equivalent, you should
495 * add it to the getopt_long() call in parseOptions().
496 */
497 static struct option cmdlineOptions[] = {${all_modules_long_options}
498 { NULL, no_argument, NULL, '\0' }
499 };/* cmdlineOptions */
500
501 #line 502 "${template}"
502
503 // static void preemptGetopt(int& argc, char**& argv, const char* opt) {
504
505 // Debug("preemptGetopt") << "preempting getopt() with " << opt << std::endl;
506
507 // AlwaysAssert(opt != NULL && *opt != '\0');
508 // AlwaysAssert(strlen(opt) <= maxoptlen);
509
510 // ++argc;
511 // unsigned i = 1;
512 // while(argv[i] != NULL && argv[i][0] != '\0') {
513 // ++i;
514 // }
515
516 // if(argv[i] == NULL) {
517 // argv = (char**) realloc(argv, (i + 6) * sizeof(char*));
518 // for(unsigned j = i; j < i + 5; ++j) {
519 // argv[j] = (char*) malloc(sizeof(char) * maxoptlen);
520 // argv[j][0] = '\0';
521 // }
522 // argv[i + 5] = NULL;
523 // }
524
525 // strncpy(argv[i], opt, maxoptlen - 1);
526 // argv[i][maxoptlen - 1] = '\0'; // ensure NUL-termination even on overflow
527 // }
528
529 namespace options {
530
531 /** Set a given Options* as "current" just for a particular scope. */
532 class OptionsGuard {
533 CVC4_THREADLOCAL_TYPE(Options*)* d_field;
534 Options* d_old;
535 public:
536 OptionsGuard(CVC4_THREADLOCAL_TYPE(Options*)* field, Options* opts) :
537 d_field(field),
538 d_old(*field) {
539 *field = opts;
540 }
541 ~OptionsGuard() {
542 *d_field = d_old;
543 }
544 };/* class OptionsGuard */
545
546 }/* CVC4::options namespace */
547
548 /**
549 * Parse argc/argv and put the result into a CVC4::Options.
550 * The return value is what's left of the command line (that is, the
551 * non-option arguments).
552 */
553 std::vector<std::string> Options::parseOptions(Options* options,
554 int argc, char* argv[])
555 throw(OptionException) {
556
557 Assert(options != NULL);
558 Assert(argv != NULL);
559
560 options::OptionsGuard guard(&s_current, options);
561
562 const char *progName = argv[0];
563
564 // To debug options parsing, you may prefer to simply uncomment this
565 // and recompile. Debug flags have not been parsed yet so these have
566 // not been set.
567 //DebugChannel.on("options");
568
569 Debug("options") << "Options::parseOptions == " << options << std::endl;
570 Debug("options") << "argv == " << argv << std::endl;
571
572 // Find the base name of the program.
573 const char *x = strrchr(progName, '/');
574 if(x != NULL) {
575 progName = x + 1;
576 }
577 options->d_holder->binary_name = std::string(progName);
578
579 ArgumentExtender* argumentExtender = new ArgumentExtenderImplementation();
580 for(int position = 1; position < argc; position++) {
581 argumentExtender->pushBackArgument(argv[position]);
582 }
583
584 std::vector<std::string> nonoptions;
585 parseOptionsRecursive(options, argumentExtender, &nonoptions);
586 if(Debug.isOn("options")){
587 for(std::vector<std::string>::const_iterator i = nonoptions.begin(),
588 iend = nonoptions.end(); i != iend; ++i){
589 Debug("options") << "nonoptions " << *i << std::endl;
590 }
591 }
592
593 delete argumentExtender;
594 return nonoptions;
595 }
596
597 void Options::parseOptionsRecursive(Options* options,
598 ArgumentExtender* extender,
599 std::vector<std::string>* nonoptions)
600 throw(OptionException) {
601
602 int argc;
603 char** argv;
604
605 extender->movePreemptionsToArguments();
606 extender->pushFrontArgument("");
607 extender->getArguments(&argc, &argv);
608
609 if(Debug.isOn("options")) {
610 Debug("options") << "starting a new parseOptionsRecursive with "
611 << argc << " arguments" << std::endl;
612 for( int i = 0; i < argc ; i++ ){
613 Assert(argv[i] != NULL);
614 Debug("options") << " argv[" << i << "] = " << argv[i] << std::endl;
615 }
616 }
617
618 // Having this synonym simplifies the generation code in mkoptions.
619 options::OptionsHandler* handler = options->d_handler;
620 options::OptionsHolder* holder = options->d_holder;
621
622 // Reset getopt(), in the case of multiple calls to parseOptions().
623 // This can be = 1 in newer GNU getopt, but older (< 2007) require = 0.
624 optind = 0;
625 #if HAVE_DECL_OPTRESET
626 optreset = 1; // on BSD getopt() (e.g. Mac OS), might need this
627 #endif /* HAVE_DECL_OPTRESET */
628
629
630 int main_optind = 0;
631 int old_optind;
632
633
634 while(true) { // Repeat Forever
635
636 if(extender->hasPreemptions()){
637 // Stop this round of parsing. We now parse recursively
638 // to start on a new character array for argv.
639 parseOptionsRecursive(options, extender, nonoptions);
640 break;
641 }
642
643 optopt = 0;
644 std::string option, optionarg;
645
646 optind = main_optind;
647 old_optind = main_optind;
648 //optind_ref = &main_optind;
649 //argv = main_argv;
650
651 // If we encounter an element that is not at zero and does not start
652 // with a "-", this is a non-option. We consume this element as a
653 // non-option.
654 if (main_optind > 0 && main_optind < argc &&
655 argv[main_optind][0] != '-') {
656 Debug("options") << "enqueueing " << argv[main_optind]
657 << " as a non-option." << std::endl;
658 nonoptions->push_back(argv[main_optind]);
659 ++main_optind;
660 extender->popFrontArgument();
661 continue;
662 }
663
664
665 Debug("options") << "[ before, main_optind == " << main_optind << " ]"
666 << std::endl;
667 Debug("options") << "[ before, optind == " << optind << " ]" << std::endl;
668 Debug("options") << "[ argc == " << argc << ", argv == " << argv << " ]"
669 << std::endl;
670 int c = getopt_long(argc, argv,
671 "+:${all_modules_short_options}",
672 cmdlineOptions, NULL);
673
674 while(main_optind < optind) {
675 main_optind++;
676 extender->popFrontArgument();
677 }
678
679 Debug("options") << "[ got " << int(c) << " (" << char(c) << ") ]"
680 << "[ next option will be at pos: " << optind << " ]"
681 << std::endl;
682
683 // The initial getopt_long call should always determine that argv[0]
684 // is not an option and returns -1. We always manually advance beyond
685 // this element.
686 //
687 // We have to reinitialize optind to 0 instead of 1 as we need to support
688 // changing the argv array passed to getopt.
689 // This is needed as are using GNU extensions.
690 // From: http://man7.org/linux/man-pages/man3/getopt.3.html
691 // A program that scans multiple argument vectors, or rescans the same
692 // vector more than once, and wants to make use of GNU extensions such
693 // as '+' and '-' at the start of optstring, or changes the value of
694 // POSIXLY_CORRECT between scans, must reinitialize getopt() by
695 // resetting optind to 0, rather than the traditional value of 1.
696 // (Resetting to 0 forces the invocation of an internal initialization
697 // routine that rechecks POSIXLY_CORRECT and checks for GNU extensions
698 // in optstring.)
699 if ( old_optind == 0 && c == -1 ) {
700 Assert(main_optind > 0);
701 continue;
702 }
703
704 if ( c == -1 ) {
705 if(Debug.isOn("options")) {
706 Debug("options") << "done with option parsing" << std::endl;
707 for(int index = optind; index < argc; ++index) {
708 Debug("options") << "remaining " << argv[index] << std::endl;
709 }
710 }
711 break;
712 }
713
714 option = argv[old_optind == 0 ? 1 : old_optind];
715 optionarg = (optarg == NULL) ? "" : optarg;
716
717 Debug("preemptGetopt") << "processing option " << c
718 << " (`" << char(c) << "'), " << option << std::endl;
719
720 switch(c) {
721 ${all_modules_option_handlers}
722
723 #line 724 "${template}"
724
725 case ':':
726 // This can be a long or short option, and the way to get at the
727 // name of it is different.
728 throw OptionException(std::string("option `") + option +
729 "' missing its required argument");
730
731 case '?':
732 default:
733 if( ( optopt == 0 ||
734 ( optopt >= ${long_option_value_begin} &&
735 optopt <= ${long_option_value_end} )
736 ) && !strncmp(argv[optind - 1], "--thread", 8) &&
737 strlen(argv[optind - 1]) > 8 )
738 {
739 if(! isdigit(argv[optind - 1][8])) {
740 throw OptionException(formatThreadOptionException(option));
741 }
742 std::vector<std::string>& threadArgv = holder->threadArgv;
743 char *end;
744 long tnum = strtol(argv[optind - 1] + 8, &end, 10);
745 if(tnum < 0 || (*end != '\0' && *end != '=')) {
746 throw OptionException(formatThreadOptionException(option));
747 }
748 if(threadArgv.size() <= size_t(tnum)) {
749 threadArgv.resize(tnum + 1);
750 }
751 if(threadArgv[tnum] != "") {
752 threadArgv[tnum] += " ";
753 }
754 if(*end == '\0') { // e.g., we have --thread0 "foo"
755 if(argc <= optind) {
756 throw OptionException(std::string("option `") + option +
757 "' missing its required argument");
758 }
759 Debug("options") << "thread " << tnum << " gets option "
760 << argv[optind] << std::endl;
761 threadArgv[tnum] += argv[main_optind];
762 main_optind++;
763 } else { // e.g., we have --thread0="foo"
764 if(end[1] == '\0') {
765 throw OptionException(std::string("option `") + option +
766 "' missing its required argument");
767 }
768 Debug("options") << "thread " << tnum << " gets option "
769 << (end + 1) << std::endl;
770 threadArgv[tnum] += end + 1;
771 }
772 Debug("options") << "thread " << tnum << " now has "
773 << threadArgv[tnum] << std::endl;
774 break;
775 }
776
777 throw OptionException(std::string("can't understand option `") + option +
778 "'" + suggestCommandLineOptions(option));
779 }
780 }
781
782 Debug("options") << "got " << nonoptions->size()
783 << " non-option arguments." << std::endl;
784
785 free(argv);
786 }
787
788 std::string Options::suggestCommandLineOptions(const std::string& optionName) throw() {
789 DidYouMean didYouMean;
790
791 const char* opt;
792 for(size_t i = 0; (opt = cmdlineOptions[i].name) != NULL; ++i) {
793 didYouMean.addWord(std::string("--") + cmdlineOptions[i].name);
794 }
795
796 return didYouMean.getMatchAsString(optionName.substr(0, optionName.find('=')));
797 }
798
799 static const char* smtOptions[] = {
800 ${all_modules_smt_options},
801 #line 802 "${template}"
802 NULL
803 };/* smtOptions[] */
804
805 std::vector<std::string> Options::suggestSmtOptions(const std::string& optionName) throw() {
806 std::vector<std::string> suggestions;
807
808 const char* opt;
809 for(size_t i = 0; (opt = smtOptions[i]) != NULL; ++i) {
810 if(std::strstr(opt, optionName.c_str()) != NULL) {
811 suggestions.push_back(opt);
812 }
813 }
814
815 return suggestions;
816 }
817
818 std::vector< std::vector<std::string> > Options::getOptions() const throw() {
819 std::vector< std::vector<std::string> > opts;
820
821 ${all_modules_get_options}
822
823 #line 824 "${template}"
824
825 return opts;
826 }
827
828
829 #undef USE_EARLY_TYPE_CHECKING_BY_DEFAULT
830 #undef DO_SEMANTIC_CHECKS_BY_DEFAULT
831
832 }/* CVC4 namespace */