Update copyright headers.
[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-2017 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 smt2.6 | smtlib2.6 SMT-LIB format 2.6\n\
438 tptp TPTP format (cnf and fof)\n\
439 sygus SyGuS format\n\
440 \n\
441 Languages currently supported as arguments to the --output-lang option:\n\
442 auto match output language to input language\n\
443 cvc4 | presentation | pl CVC4 presentation language\n\
444 cvc3 CVC3 presentation language\n\
445 smt1 | smtlib1 SMT-LIB format 1.2\n\
446 smt | smtlib | smt2 |\n\
447 smt2.0 | smtlib2.0 | smtlib2 SMT-LIB format 2.0\n\
448 smt2.5 | smtlib2.5 SMT-LIB format 2.5\n\
449 smt2.6 | smtlib2.6 SMT-LIB format 2.6\n\
450 tptp TPTP format\n\
451 z3str SMT-LIB 2.0 with Z3-str string constraints\n\
452 ast internal format (simple syntax trees)\n\
453 ";
454
455 std::string Options::getDescription() const {
456 return optionsDescription;
457 }
458
459 void Options::printUsage(const std::string msg, std::ostream& out) {
460 out << msg << optionsDescription << std::endl
461 << optionsFootnote << std::endl << std::flush;
462 }
463
464 void Options::printShortUsage(const std::string msg, std::ostream& out) {
465 out << msg << mostCommonOptionsDescription << std::endl
466 << optionsFootnote << std::endl
467 << "For full usage, please use --help."
468 << std::endl << std::endl << std::flush;
469 }
470
471 void Options::printLanguageHelp(std::ostream& out) {
472 out << languageDescription << std::flush;
473 }
474
475 /**
476 * This is a table of long options. By policy, each short option
477 * should have an equivalent long option (but the reverse isn't the
478 * case), so this table should thus contain all command-line options.
479 *
480 * Each option in this array has four elements:
481 *
482 * 1. the long option string
483 * 2. argument behavior for the option:
484 * no_argument - no argument permitted
485 * required_argument - an argument is expected
486 * optional_argument - an argument is permitted but not required
487 * 3. this is a pointer to an int which is set to the 4th entry of the
488 * array if the option is present; or NULL, in which case
489 * getopt_long() returns the 4th entry
490 * 4. the return value for getopt_long() when this long option (or the
491 * value to set the 3rd entry to; see #3)
492 *
493 * If you add something here, you should add it in src/main/usage.h
494 * also, to document it.
495 *
496 * If you add something that has a short option equivalent, you should
497 * add it to the getopt_long() call in parseOptions().
498 */
499 static struct option cmdlineOptions[] = {${all_modules_long_options}
500 { NULL, no_argument, NULL, '\0' }
501 };/* cmdlineOptions */
502
503 #line 502 "${template}"
504
505 // static void preemptGetopt(int& argc, char**& argv, const char* opt) {
506
507 // Debug("preemptGetopt") << "preempting getopt() with " << opt << std::endl;
508
509 // AlwaysAssert(opt != NULL && *opt != '\0');
510 // AlwaysAssert(strlen(opt) <= maxoptlen);
511
512 // ++argc;
513 // unsigned i = 1;
514 // while(argv[i] != NULL && argv[i][0] != '\0') {
515 // ++i;
516 // }
517
518 // if(argv[i] == NULL) {
519 // argv = (char**) realloc(argv, (i + 6) * sizeof(char*));
520 // for(unsigned j = i; j < i + 5; ++j) {
521 // argv[j] = (char*) malloc(sizeof(char) * maxoptlen);
522 // argv[j][0] = '\0';
523 // }
524 // argv[i + 5] = NULL;
525 // }
526
527 // strncpy(argv[i], opt, maxoptlen - 1);
528 // argv[i][maxoptlen - 1] = '\0'; // ensure NUL-termination even on overflow
529 // }
530
531 namespace options {
532
533 /** Set a given Options* as "current" just for a particular scope. */
534 class OptionsGuard {
535 CVC4_THREADLOCAL_TYPE(Options*)* d_field;
536 Options* d_old;
537 public:
538 OptionsGuard(CVC4_THREADLOCAL_TYPE(Options*)* field, Options* opts) :
539 d_field(field),
540 d_old(*field) {
541 *field = opts;
542 }
543 ~OptionsGuard() {
544 *d_field = d_old;
545 }
546 };/* class OptionsGuard */
547
548 }/* CVC4::options namespace */
549
550 /**
551 * Parse argc/argv and put the result into a CVC4::Options.
552 * The return value is what's left of the command line (that is, the
553 * non-option arguments).
554 */
555 std::vector<std::string> Options::parseOptions(Options* options,
556 int argc, char* argv[])
557 throw(OptionException) {
558
559 Assert(options != NULL);
560 Assert(argv != NULL);
561
562 options::OptionsGuard guard(&s_current, options);
563
564 const char *progName = argv[0];
565
566 // To debug options parsing, you may prefer to simply uncomment this
567 // and recompile. Debug flags have not been parsed yet so these have
568 // not been set.
569 //DebugChannel.on("options");
570
571 Debug("options") << "Options::parseOptions == " << options << std::endl;
572 Debug("options") << "argv == " << argv << std::endl;
573
574 // Find the base name of the program.
575 const char *x = strrchr(progName, '/');
576 if(x != NULL) {
577 progName = x + 1;
578 }
579 options->d_holder->binary_name = std::string(progName);
580
581 ArgumentExtender* argumentExtender = new ArgumentExtenderImplementation();
582 for(int position = 1; position < argc; position++) {
583 argumentExtender->pushBackArgument(argv[position]);
584 }
585
586 std::vector<std::string> nonoptions;
587 parseOptionsRecursive(options, argumentExtender, &nonoptions);
588 if(Debug.isOn("options")){
589 for(std::vector<std::string>::const_iterator i = nonoptions.begin(),
590 iend = nonoptions.end(); i != iend; ++i){
591 Debug("options") << "nonoptions " << *i << std::endl;
592 }
593 }
594
595 delete argumentExtender;
596 return nonoptions;
597 }
598
599 void Options::parseOptionsRecursive(Options* options,
600 ArgumentExtender* extender,
601 std::vector<std::string>* nonoptions)
602 throw(OptionException) {
603
604 int argc;
605 char** argv;
606
607 extender->movePreemptionsToArguments();
608 extender->pushFrontArgument("");
609 extender->getArguments(&argc, &argv);
610
611 if(Debug.isOn("options")) {
612 Debug("options") << "starting a new parseOptionsRecursive with "
613 << argc << " arguments" << std::endl;
614 for( int i = 0; i < argc ; i++ ){
615 Assert(argv[i] != NULL);
616 Debug("options") << " argv[" << i << "] = " << argv[i] << std::endl;
617 }
618 }
619
620 // Having this synonym simplifies the generation code in mkoptions.
621 options::OptionsHandler* handler = options->d_handler;
622 options::OptionsHolder* holder = options->d_holder;
623
624 // Reset getopt(), in the case of multiple calls to parseOptions().
625 // This can be = 1 in newer GNU getopt, but older (< 2007) require = 0.
626 optind = 0;
627 #if HAVE_DECL_OPTRESET
628 optreset = 1; // on BSD getopt() (e.g. Mac OS), might need this
629 #endif /* HAVE_DECL_OPTRESET */
630
631
632 int main_optind = 0;
633 int old_optind;
634
635
636 while(true) { // Repeat Forever
637
638 if(extender->hasPreemptions()){
639 // Stop this round of parsing. We now parse recursively
640 // to start on a new character array for argv.
641 parseOptionsRecursive(options, extender, nonoptions);
642 break;
643 }
644
645 optopt = 0;
646 std::string option, optionarg;
647
648 optind = main_optind;
649 old_optind = main_optind;
650 //optind_ref = &main_optind;
651 //argv = main_argv;
652
653 // If we encounter an element that is not at zero and does not start
654 // with a "-", this is a non-option. We consume this element as a
655 // non-option.
656 if (main_optind > 0 && main_optind < argc &&
657 argv[main_optind][0] != '-') {
658 Debug("options") << "enqueueing " << argv[main_optind]
659 << " as a non-option." << std::endl;
660 nonoptions->push_back(argv[main_optind]);
661 ++main_optind;
662 extender->popFrontArgument();
663 continue;
664 }
665
666
667 Debug("options") << "[ before, main_optind == " << main_optind << " ]"
668 << std::endl;
669 Debug("options") << "[ before, optind == " << optind << " ]" << std::endl;
670 Debug("options") << "[ argc == " << argc << ", argv == " << argv << " ]"
671 << std::endl;
672 int c = getopt_long(argc, argv,
673 "+:${all_modules_short_options}",
674 cmdlineOptions, NULL);
675
676 while(main_optind < optind) {
677 main_optind++;
678 extender->popFrontArgument();
679 }
680
681 Debug("options") << "[ got " << int(c) << " (" << char(c) << ") ]"
682 << "[ next option will be at pos: " << optind << " ]"
683 << std::endl;
684
685 // The initial getopt_long call should always determine that argv[0]
686 // is not an option and returns -1. We always manually advance beyond
687 // this element.
688 //
689 // We have to reinitialize optind to 0 instead of 1 as we need to support
690 // changing the argv array passed to getopt.
691 // This is needed as are using GNU extensions.
692 // From: http://man7.org/linux/man-pages/man3/getopt.3.html
693 // A program that scans multiple argument vectors, or rescans the same
694 // vector more than once, and wants to make use of GNU extensions such
695 // as '+' and '-' at the start of optstring, or changes the value of
696 // POSIXLY_CORRECT between scans, must reinitialize getopt() by
697 // resetting optind to 0, rather than the traditional value of 1.
698 // (Resetting to 0 forces the invocation of an internal initialization
699 // routine that rechecks POSIXLY_CORRECT and checks for GNU extensions
700 // in optstring.)
701 if ( old_optind == 0 && c == -1 ) {
702 Assert(main_optind > 0);
703 continue;
704 }
705
706 if ( c == -1 ) {
707 if(Debug.isOn("options")) {
708 Debug("options") << "done with option parsing" << std::endl;
709 for(int index = optind; index < argc; ++index) {
710 Debug("options") << "remaining " << argv[index] << std::endl;
711 }
712 }
713 break;
714 }
715
716 option = argv[old_optind == 0 ? 1 : old_optind];
717 optionarg = (optarg == NULL) ? "" : optarg;
718
719 Debug("preemptGetopt") << "processing option " << c
720 << " (`" << char(c) << "'), " << option << std::endl;
721
722 switch(c) {
723 ${all_modules_option_handlers}
724
725 #line 724 "${template}"
726
727 case ':':
728 // This can be a long or short option, and the way to get at the
729 // name of it is different.
730 throw OptionException(std::string("option `") + option +
731 "' missing its required argument");
732
733 case '?':
734 default:
735 if( ( optopt == 0 ||
736 ( optopt >= ${long_option_value_begin} &&
737 optopt <= ${long_option_value_end} )
738 ) && !strncmp(argv[optind - 1], "--thread", 8) &&
739 strlen(argv[optind - 1]) > 8 )
740 {
741 if(! isdigit(argv[optind - 1][8])) {
742 throw OptionException(formatThreadOptionException(option));
743 }
744 std::vector<std::string>& threadArgv = holder->threadArgv;
745 char *end;
746 long tnum = strtol(argv[optind - 1] + 8, &end, 10);
747 if(tnum < 0 || (*end != '\0' && *end != '=')) {
748 throw OptionException(formatThreadOptionException(option));
749 }
750 if(threadArgv.size() <= size_t(tnum)) {
751 threadArgv.resize(tnum + 1);
752 }
753 if(threadArgv[tnum] != "") {
754 threadArgv[tnum] += " ";
755 }
756 if(*end == '\0') { // e.g., we have --thread0 "foo"
757 if(argc <= optind) {
758 throw OptionException(std::string("option `") + option +
759 "' missing its required argument");
760 }
761 Debug("options") << "thread " << tnum << " gets option "
762 << argv[optind] << std::endl;
763 threadArgv[tnum] += argv[main_optind];
764 main_optind++;
765 } else { // e.g., we have --thread0="foo"
766 if(end[1] == '\0') {
767 throw OptionException(std::string("option `") + option +
768 "' missing its required argument");
769 }
770 Debug("options") << "thread " << tnum << " gets option "
771 << (end + 1) << std::endl;
772 threadArgv[tnum] += end + 1;
773 }
774 Debug("options") << "thread " << tnum << " now has "
775 << threadArgv[tnum] << std::endl;
776 break;
777 }
778
779 throw OptionException(std::string("can't understand option `") + option +
780 "'" + suggestCommandLineOptions(option));
781 }
782 }
783
784 Debug("options") << "got " << nonoptions->size()
785 << " non-option arguments." << std::endl;
786
787 free(argv);
788 }
789
790 std::string Options::suggestCommandLineOptions(const std::string& optionName) throw() {
791 DidYouMean didYouMean;
792
793 const char* opt;
794 for(size_t i = 0; (opt = cmdlineOptions[i].name) != NULL; ++i) {
795 didYouMean.addWord(std::string("--") + cmdlineOptions[i].name);
796 }
797
798 return didYouMean.getMatchAsString(optionName.substr(0, optionName.find('=')));
799 }
800
801 static const char* smtOptions[] = {
802 ${all_modules_smt_options},
803 #line 802 "${template}"
804 NULL
805 };/* smtOptions[] */
806
807 std::vector<std::string> Options::suggestSmtOptions(const std::string& optionName) throw() {
808 std::vector<std::string> suggestions;
809
810 const char* opt;
811 for(size_t i = 0; (opt = smtOptions[i]) != NULL; ++i) {
812 if(std::strstr(opt, optionName.c_str()) != NULL) {
813 suggestions.push_back(opt);
814 }
815 }
816
817 return suggestions;
818 }
819
820 std::vector< std::vector<std::string> > Options::getOptions() const throw() {
821 std::vector< std::vector<std::string> > opts;
822
823 ${all_modules_get_options}
824
825 #line 824 "${template}"
826
827 return opts;
828 }
829
830
831 #undef USE_EARLY_TYPE_CHECKING_BY_DEFAULT
832 #undef DO_SEMANTIC_CHECKS_BY_DEFAULT
833
834 }/* CVC4 namespace */