1 /********************* */
2 /*! \file driver_unified.cpp
4 ** Top contributors (to current version):
5 ** Morgan Deters, Liana Hadarean, Tim King
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2021 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
12 ** \brief Driver for CVC4 executable (cvc4)
25 #include "cvc4autoconfig.h"
27 #include "api/cvc4cpp.h"
28 #include "base/configuration.h"
29 #include "base/output.h"
30 #include "main/command_executor.h"
31 #include "main/interactive_shell.h"
32 #include "main/main.h"
33 #include "main/signal_handlers.h"
34 #include "main/time_limit.h"
35 #include "options/options.h"
36 #include "options/set_language.h"
37 #include "parser/parser.h"
38 #include "parser/parser_builder.h"
39 #include "parser/parser_exception.h"
40 #include "smt/command.h"
41 #include "util/result.h"
42 #include "util/statistics_registry.h"
46 using namespace CVC4::parser
;
47 using namespace CVC4::main
;
51 /** Global options variable */
52 thread_local Options
* pOptions
;
57 /** Just the basename component of argv[0] */
58 const std::string
*progName
;
60 /** A pointer to the CommandExecutor (the signal handlers need it) */
61 CVC4::main::CommandExecutor
* pExecutor
= nullptr;
63 /** A pointer to the totalTime driver stat (the signal handlers need it) */
64 CVC4::TimerStat
* pTotalTime
= nullptr;
66 }/* CVC4::main namespace */
70 void printUsage(Options
& opts
, bool full
) {
72 ss
<< "usage: " << opts
.getBinaryName() << " [options] [input-file]"
74 << "Without an input file, or with `-', CVC4 reads from standard input."
76 << "CVC4 options:" << endl
;
78 Options::printUsage( ss
.str(), *(opts
.getOut()) );
80 Options::printShortUsage( ss
.str(), *(opts
.getOut()) );
84 int runCvc4(int argc
, char* argv
[], Options
& opts
) {
87 pTotalTime
= new TimerStat("totalTime");
90 // For the signal handlers' benefit
93 // Initialize the signal handlers
94 signal_handlers::install();
99 vector
<string
> filenames
= Options::parseOptions(&opts
, argc
, argv
);
101 auto limit
= install_time_limit(opts
);
103 string progNameStr
= opts
.getBinaryName();
104 progName
= &progNameStr
;
106 if( opts
.getHelp() ) {
107 printUsage(opts
, true);
109 } else if( opts
.getLanguageHelp() ) {
110 Options::printLanguageHelp(*(opts
.getOut()));
112 } else if( opts
.getVersion() ) {
113 *(opts
.getOut()) << Configuration::about().c_str() << flush
;
117 segvSpin
= opts
.getSegvSpin();
119 // If in competition mode, set output stream option to flush immediately
120 #ifdef CVC4_COMPETITION_MODE
121 *(opts
.getOut()) << unitbuf
;
122 #endif /* CVC4_COMPETITION_MODE */
124 // We only accept one input file
125 if(filenames
.size() > 1) {
126 throw Exception("Too many input files specified.");
129 // If no file supplied we will read from standard input
130 const bool inputFromStdin
= filenames
.empty() || filenames
[0] == "-";
132 // if we're reading from stdin on a TTY, default to interactive mode
133 if(!opts
.wasSetByUserInteractive()) {
134 opts
.setInteractive(inputFromStdin
&& isatty(fileno(stdin
)));
137 // Auto-detect input language by filename extension
138 std::string
filenameStr("<stdin>");
139 if (!inputFromStdin
) {
140 // Use swap to avoid copying the string
141 // TODO: use std::move() when switching to c++11
142 filenameStr
.swap(filenames
[0]);
144 const char* filename
= filenameStr
.c_str();
146 if(opts
.getInputLanguage() == language::input::LANG_AUTO
) {
147 if( inputFromStdin
) {
148 // We can't do any fancy detection on stdin
149 opts
.setInputLanguage(language::input::LANG_CVC4
);
151 unsigned len
= filenameStr
.size();
152 if(len
>= 5 && !strcmp(".smt2", filename
+ len
- 5)) {
153 opts
.setInputLanguage(language::input::LANG_SMTLIB_V2_6
);
154 } else if((len
>= 2 && !strcmp(".p", filename
+ len
- 2))
155 || (len
>= 5 && !strcmp(".tptp", filename
+ len
- 5))) {
156 opts
.setInputLanguage(language::input::LANG_TPTP
);
157 } else if(( len
>= 4 && !strcmp(".cvc", filename
+ len
- 4) )
158 || ( len
>= 5 && !strcmp(".cvc4", filename
+ len
- 5) )) {
159 opts
.setInputLanguage(language::input::LANG_CVC4
);
160 } else if((len
>= 3 && !strcmp(".sy", filename
+ len
- 3))
161 || (len
>= 3 && !strcmp(".sl", filename
+ len
- 3))) {
162 // version 2 sygus is the default
163 opts
.setInputLanguage(language::input::LANG_SYGUS_V2
);
168 if(opts
.getOutputLanguage() == language::output::LANG_AUTO
) {
169 opts
.setOutputLanguage(language::toOutputLanguage(opts
.getInputLanguage()));
172 // Determine which messages to show based on smtcomp_mode and verbosity
173 if(Configuration::isMuzzledBuild()) {
174 DebugChannel
.setStream(&CVC4::null_os
);
175 TraceChannel
.setStream(&CVC4::null_os
);
176 NoticeChannel
.setStream(&CVC4::null_os
);
177 ChatChannel
.setStream(&CVC4::null_os
);
178 MessageChannel
.setStream(&CVC4::null_os
);
179 WarningChannel
.setStream(&CVC4::null_os
);
182 // important even for muzzled builds (to get result output right)
183 (*(opts
.getOut())) << language::SetLanguage(opts
.getOutputLanguage());
185 // Create the command executor to execute the parsed commands
186 pExecutor
= new CommandExecutor(opts
);
191 RegisterStatistic
statTotalTime(&pExecutor
->getStatisticsRegistry(),
194 // Filename statistics
195 ReferenceStat
<std::string
> s_statFilename("filename", filenameStr
);
196 RegisterStatistic
statFilenameReg(&pExecutor
->getStatisticsRegistry(),
198 // notify SmtEngine that we are starting to parse
199 pExecutor
->getSmtEngine()->notifyStartParsing(filenameStr
);
201 // Parse and execute commands until we are done
202 std::unique_ptr
<Command
> cmd
;
204 if(opts
.getInteractive() && inputFromStdin
) {
205 if(opts
.getTearDownIncremental() > 0) {
206 throw OptionException(
207 "--tear-down-incremental doesn't work in interactive mode");
209 if(!opts
.wasSetByUserIncrementalSolving()) {
210 cmd
.reset(new SetOptionCommand("incremental", "true"));
212 pExecutor
->doCommand(cmd
);
214 InteractiveShell
shell(pExecutor
->getSolver(),
215 pExecutor
->getSymbolManager());
216 if(opts
.getInteractivePrompt()) {
217 CVC4Message() << Configuration::getPackageName() << " "
218 << Configuration::getVersionString();
219 if(Configuration::isGitBuild()) {
220 CVC4Message() << " [" << Configuration::getGitId() << "]";
222 CVC4Message() << (Configuration::isDebugBuild() ? " DEBUG" : "")
224 << (Configuration::isAssertionBuild() ? "on" : "off")
227 CVC4Message() << Configuration::copyright() << endl
;
232 cmd
.reset(shell
.readCommand());
233 } catch(UnsafeInterruptException
& e
) {
234 (*opts
.getOut()) << CommandInterrupted();
239 status
= pExecutor
->doCommand(cmd
) && status
;
240 if (cmd
->interrupted()) {
244 } else if( opts
.getTearDownIncremental() > 0) {
245 if(!opts
.getIncrementalSolving() && opts
.getTearDownIncremental() > 1) {
246 // For tear-down-incremental values greater than 1, need incremental
248 cmd
.reset(new SetOptionCommand("incremental", "true"));
250 pExecutor
->doCommand(cmd
);
251 // if(opts.wasSetByUserIncrementalSolving()) {
252 // throw OptionException(
253 // "--tear-down-incremental incompatible with --incremental");
256 // cmd.reset(new SetOptionCommand("incremental", SExpr(false)));
257 // cmd->setMuted(true);
258 // pExecutor->doCommand(cmd);
261 ParserBuilder
parserBuilder(pExecutor
->getSolver(),
262 pExecutor
->getSymbolManager(),
266 if( inputFromStdin
) {
267 #if defined(CVC4_COMPETITION_MODE) && !defined(CVC4_SMTCOMP_APPLICATION_TRACK)
268 parserBuilder
.withStreamInput(cin
);
269 #else /* CVC4_COMPETITION_MODE && !CVC4_SMTCOMP_APPLICATION_TRACK */
270 parserBuilder
.withLineBufferedStreamInput(cin
);
271 #endif /* CVC4_COMPETITION_MODE && !CVC4_SMTCOMP_APPLICATION_TRACK */
274 vector
< vector
<Command
*> > allCommands
;
275 allCommands
.push_back(vector
<Command
*>());
276 std::unique_ptr
<Parser
> parser(parserBuilder
.build());
278 // true if one of the commands was interrupted
279 bool interrupted
= false;
283 (*opts
.getOut()) << CommandInterrupted();
288 cmd
.reset(parser
->nextCommand());
289 if (cmd
== nullptr) break;
290 } catch (UnsafeInterruptException
& e
) {
295 if(dynamic_cast<PushCommand
*>(cmd
.get()) != nullptr) {
296 if(needReset
>= opts
.getTearDownIncremental()) {
298 for(size_t i
= 0; i
< allCommands
.size() && !interrupted
; ++i
) {
299 if (interrupted
) break;
300 for(size_t j
= 0; j
< allCommands
[i
].size() && !interrupted
; ++j
)
302 Command
* ccmd
= allCommands
[i
][j
]->clone();
303 ccmd
->setMuted(true);
304 pExecutor
->doCommand(ccmd
);
305 if (ccmd
->interrupted())
314 allCommands
.push_back(vector
<Command
*>());
315 Command
* copy
= cmd
->clone();
316 allCommands
.back().push_back(copy
);
317 status
= pExecutor
->doCommand(cmd
);
318 if(cmd
->interrupted()) {
322 } else if(dynamic_cast<PopCommand
*>(cmd
.get()) != nullptr) {
323 allCommands
.pop_back(); // fixme leaks cmds here
324 if (needReset
>= opts
.getTearDownIncremental()) {
326 for(size_t i
= 0; i
< allCommands
.size() && !interrupted
; ++i
) {
327 for(size_t j
= 0; j
< allCommands
[i
].size() && !interrupted
; ++j
)
329 std::unique_ptr
<Command
> ccmd(allCommands
[i
][j
]->clone());
330 ccmd
->setMuted(true);
331 pExecutor
->doCommand(ccmd
);
332 if (ccmd
->interrupted())
338 if (interrupted
) continue;
339 (*opts
.getOut()) << CommandSuccess();
342 status
= pExecutor
->doCommand(cmd
);
343 if(cmd
->interrupted()) {
348 } else if(dynamic_cast<CheckSatCommand
*>(cmd
.get()) != nullptr ||
349 dynamic_cast<QueryCommand
*>(cmd
.get()) != nullptr) {
350 if(needReset
>= opts
.getTearDownIncremental()) {
352 for(size_t i
= 0; i
< allCommands
.size() && !interrupted
; ++i
) {
353 for(size_t j
= 0; j
< allCommands
[i
].size() && !interrupted
; ++j
)
355 Command
* ccmd
= allCommands
[i
][j
]->clone();
356 ccmd
->setMuted(true);
357 pExecutor
->doCommand(ccmd
);
358 if (ccmd
->interrupted())
373 status
= pExecutor
->doCommand(cmd
);
374 if(cmd
->interrupted()) {
378 } else if(dynamic_cast<ResetCommand
*>(cmd
.get()) != nullptr) {
379 pExecutor
->doCommand(cmd
);
381 allCommands
.push_back(vector
<Command
*>());
383 // We shouldn't copy certain commands, because they can cause
384 // an error on replay since there's no associated sat/unsat check
386 if(dynamic_cast<GetUnsatCoreCommand
*>(cmd
.get()) == nullptr &&
387 dynamic_cast<GetProofCommand
*>(cmd
.get()) == nullptr &&
388 dynamic_cast<GetValueCommand
*>(cmd
.get()) == nullptr &&
389 dynamic_cast<GetModelCommand
*>(cmd
.get()) == nullptr &&
390 dynamic_cast<GetAssignmentCommand
*>(cmd
.get()) == nullptr &&
391 dynamic_cast<GetInstantiationsCommand
*>(cmd
.get()) == nullptr &&
392 dynamic_cast<GetAssertionsCommand
*>(cmd
.get()) == nullptr &&
393 dynamic_cast<GetInfoCommand
*>(cmd
.get()) == nullptr &&
394 dynamic_cast<GetOptionCommand
*>(cmd
.get()) == nullptr &&
395 dynamic_cast<EchoCommand
*>(cmd
.get()) == nullptr) {
396 Command
* copy
= cmd
->clone();
397 allCommands
.back().push_back(copy
);
399 status
= pExecutor
->doCommand(cmd
);
400 if(cmd
->interrupted()) {
405 if(dynamic_cast<QuitCommand
*>(cmd
.get()) != nullptr) {
411 if(!opts
.wasSetByUserIncrementalSolving()) {
412 cmd
.reset(new SetOptionCommand("incremental", "false"));
414 pExecutor
->doCommand(cmd
);
417 ParserBuilder
parserBuilder(pExecutor
->getSolver(),
418 pExecutor
->getSymbolManager(),
422 if( inputFromStdin
) {
423 #if defined(CVC4_COMPETITION_MODE) && !defined(CVC4_SMTCOMP_APPLICATION_TRACK)
424 parserBuilder
.withStreamInput(cin
);
425 #else /* CVC4_COMPETITION_MODE && !CVC4_SMTCOMP_APPLICATION_TRACK */
426 parserBuilder
.withLineBufferedStreamInput(cin
);
427 #endif /* CVC4_COMPETITION_MODE && !CVC4_SMTCOMP_APPLICATION_TRACK */
430 std::unique_ptr
<Parser
> parser(parserBuilder
.build());
431 bool interrupted
= false;
435 (*opts
.getOut()) << CommandInterrupted();
440 cmd
.reset(parser
->nextCommand());
441 if (cmd
== nullptr) break;
442 } catch (UnsafeInterruptException
& e
) {
447 status
= pExecutor
->doCommand(cmd
);
448 if (cmd
->interrupted() && status
== 0) {
453 if(dynamic_cast<QuitCommand
*>(cmd
.get()) != nullptr) {
461 result
= pExecutor
->getResult();
464 // there was some kind of error
468 #ifdef CVC4_COMPETITION_MODE
470 // exit, don't return (don't want destructors to run)
471 // _exit() from unistd.h doesn't run global destructors
472 // or other on_exit/atexit stuff.
474 #endif /* CVC4_COMPETITION_MODE */
476 ReferenceStat
<api::Result
> s_statSatResult("sat/unsat", result
);
477 RegisterStatistic
statSatResultReg(&pExecutor
->getStatisticsRegistry(),
482 // Tim: I think that following comment is out of date?
483 // Set the global executor pointer to nullptr first. If we get a
484 // signal while dumping statistics, we don't want to try again.
485 pExecutor
->flushOutputStreams();
488 if(opts
.getEarlyExit() && opts
.wasSetByUserEarlyExit()) {
491 #else /* CVC4_DEBUG */
492 if(opts
.getEarlyExit()) {
495 #endif /* CVC4_DEBUG */
498 // On exceptional exit, these are leaked, but that's okay... they
499 // need to be around in that case for main() to print statistics.
503 pTotalTime
= nullptr;
506 signal_handlers::cleanup();