int main(int argc, char* argv[]) { mreal::initPrecision(); mreal::initRand(); CommandParser commandParser; initCommands(&commandParser); CommandFactory commandFactory; try { int commandCode = UNKNOWN_COMMAND; CommandParams commandParams; commandParser.getCommandCodeAndParams(argc, argv, commandCode, commandParams); ContextPtr pContext = Context::setup(DRY_MODE, ".3app"); CommandPtr pCommand = commandFactory.getCommand(commandCode, commandParams); pCommand->execute(); _destroy(pCommand); delete pContext; } catch(std::exception const& e) { std::cout << e.what() << std::endl; printSyntaxMessage(); } }
void Executor::run() { Lock xx(mutex); while(true) { while(head.get()==NULL) { xx.unlock(); moreWork.wait(); xx.lock(); } CommandPtr command = head; if(command.get()==NULL) continue; if(command.get()==shutdown.get()) break; xx.unlock(); try { command->command(); }catch(std::exception& e){ //TODO: feed into logging mechanism fprintf(stderr, "Executor: Unhandled exception: %s",e.what()); }catch(...){ fprintf(stderr, "Executor: Unhandled exception"); } xx.lock(); } stopped.signal(); }
void CommandQueue::execute (CommandPtr command_ptr) { if (!command_ptr) return; command_ptr->execute(); if (command_ptr->hasUndo()) { command_deque_.push_back(command_ptr); enforceDequeLimit(); } }
void runMain(GlobalEvrionment &g) { g.funcs["main"]->Call(g, std::vector<ValuePtr>()); for (;;) { Stack& s = g.stack; if (s.pcs.empty()) break; int pc = s.pcs.back(); CommandFunction* func = static_cast<CommandFunction*>(s.funcs.back().get()); if (pc == func->cmds.size()) { s.pcs.pop_back(); s.contexts.pop_back(); s.funcs.pop_back(); continue; } CommandPtr cmd = func->cmds[s.pcs.back()++]; cmd->apply(g); } }
int main (int argc, char ** argv) { setupSignals (); Factory f; if (argc < 2) { return displayHelp (argv[0], f); } string command = argv[1]; if (command == "help" || command == "-H" || command == "--help") { if (argc >= 3) { runManPage (argv[2]); } else { runManPage (); } return displayHelp (argv[0], f); } if (command == "-V" || command == "--version") { displayVersion (); return 0; } try { std::vector<char *> origArguments (argv + 1, argv + argc); origArguments.push_back (0); CommandPtr cmd = f.get (command); Cmdline cl (argc, argv, cmd.get ()); if (cl.help) { runManPage (command, cl.profile); // does not return, but may throw } // version and invalidOpt might be implemented // differently for external command if (dynamic_cast<ExternalCommand *> (cmd.get ())) { tryExternalCommand (&origArguments[0]); // does not return, but may throw } if (cl.version) { displayVersion (); return 0; } if (cl.invalidOpt) { cerr << cl << endl; return 1; } try { return cmd->execute (cl); } catch (std::invalid_argument const & ia) { cerr << "Sorry, I could not process the arguments: " << ia.what () << endl << endl; cerr << cl << endl; return 2; } } catch (CommandException const & ce) { std::cerr << "The command " << getErrorColor (ANSI_COLOR::BOLD) << argv[0] << " " << command << getErrorColor (ANSI_COLOR::RESET) << " terminated " << getErrorColor (ANSI_COLOR::RED) << "unsuccessfully" << getErrorColor (ANSI_COLOR::RESET) << " with the info:\n" << ce.what () << std::endl; if (ce.errorCode () != 3 && (ce.errorCode () < 11 || ce.errorCode () > 20)) { std::cerr << "Command used invalid return value (" << ce.errorCode () << "), please report the issue at https://issues.libelektra.org/" << std::endl; return 3; } return ce.errorCode (); } catch (UnknownCommand const & uc) { std::cerr << "The command " << getErrorColor (ANSI_COLOR::BOLD) << argv[0] << " " << command << getErrorColor (ANSI_COLOR::RESET) << " is " << getErrorColor (ANSI_COLOR::RED) << "not known" << getErrorColor (ANSI_COLOR::RESET) << std::endl; displayHelp (argv[0], f); return 4; } catch (kdb::KDBException const & ce) { std::cerr << "The command " << getErrorColor (ANSI_COLOR::BOLD) << argv[0] << " " << command << getErrorColor (ANSI_COLOR::RESET) << getErrorColor (ANSI_COLOR::RED) << " failed" << getErrorColor (ANSI_COLOR::RESET) << " while accessing the key database with the info:\n" << ce.what () << std::endl; return 5; } catch (std::exception const & ce) { std::cerr << "The command " << getErrorColor (ANSI_COLOR::BOLD) << argv[0] << " " << command << getErrorColor (ANSI_COLOR::RESET) << " terminated " << getErrorColor (ANSI_COLOR::RED) << "unsuccessfully" << getErrorColor (ANSI_COLOR::RESET) << " with the info:" << endl << ce.what () << endl << "Please report the issue at https://issues.libelektra.org/" << std::endl; return 7; } catch (...) { std::cerr << "The command " << getErrorColor (ANSI_COLOR::BOLD) << argv[0] << " " << command << getErrorColor (ANSI_COLOR::RESET) << " terminated with an " << getErrorColor (ANSI_COLOR::RED) << "unknown error" << getErrorColor (ANSI_COLOR::RESET) << endl << "Please report the issue at https://issues.libelektra.org/" << std::endl; displayHelp (argv[0], f); return 7; } }
int main(int argc, char**argv) { Factory f; if (argc < 2 ) { displayHelp(argv[0], f.getCommands()); return 0; } string command = argv[1]; if (command == "help" || command == "-H" || command == "--help") { if (argc == 3) { runManPage(argv[2]); } displayHelp(argv[0], f.getCommands()); return 0; } if (command == "-V" || command == "--version") { displayVersion(); return 0; } try { CommandPtr cmd = f.get(command); if (!cmd.get()) { tryExternalCommand(argv+1); // does not return, but may throw } Cmdline cl (argc, argv, cmd.get()); if (cl.version) { displayVersion(); return 0; } if (cl.help) { runManPage(command); // does not return, but may throw } if (cl.invalidOpt) { cerr << "Invalid options passed\n" << endl; cerr << cl << endl; return 1; } try { return cmd->execute (cl); } catch (std::invalid_argument const& ia) { cerr << "Invalid arguments passed: " << ia.what() << endl << endl; cerr << cl << endl; return 2; } } catch (CommandException const& ce) { std::cerr << "The command " << command << " terminated unsuccessfully with the info: " << ce.what() << std::endl; return 3; } catch (UnknownCommand const& uc) { std::cerr << "The command " << command << " is not known" << std::endl; displayHelp(argv[0], f.getCommands()); return 4; } catch (kdb::KDBException const& ce) { std::cerr << "The command " << command << " failed while accessing the key database with the info:" << std::endl << ce.what() << std::endl; return 5; } catch (std::exception const& ce) { std::cerr << "The command " << command << " terminated unsuccessfully with the info:" << std::endl << ce.what() << std::endl; return 6; } catch (...) { std::cerr << "Unknown error" << std::endl; displayHelp(argv[0], f.getCommands()); return 7; } }
bool DefaultProtocol::RegisterCommand(const CommandPtr &command) { s_cmds[command->Name()] = command; return true; }
void ConfigManager::init (IBindingSpaceListener *bindingSpaceListener, int argc, char **argv) { _persister = new ConfigPersisterSQL(System.getDatabaseDirectory() + Singleton<Application>::getInstance().getName() + ".sqlite"); Log::info(LOG_COMMON, "init configmanager"); _persister->init(); _bindingSpaceListener = bindingSpaceListener; LUA lua; Log::info(LOG_COMMON, "load config lua file"); const bool success = lua.load("config.lua"); if (!success) { Log::error(LOG_COMMON, "could not load config"); } if (success) { Log::info(LOG_COMMON, "load config values"); getKeyValueMap(lua, _configVarMap, "settings"); Log::info(LOG_COMMON, "load keybindings"); getBindingMap(lua, _keybindings, KEY_CONFIG_KEYBINDINGS, KEYBOARD); Log::info(LOG_COMMON, "load controller bindings"); getBindingMap(lua, _controllerBindings, KEY_CONFIG_CONTROLLERBINDINGS, CONTROLLER); } _language = getConfigValue(_configVarMap, "language", System.getLanguage()); _gameController = getConfigValue(_configVarMap, "gamecontroller"); _gameControllerTriggerAxis = getConfigValue(_configVarMap, "gamecontrollertriggeraxis"); _showFPS = getConfigValue(_configVarMap, "showfps"); _width = getConfigValue(_configVarMap, "width", "-1"); _height = getConfigValue(_configVarMap, "height", "-1"); _fullscreen = getConfigValue(_configVarMap, "fullscreen", "true"); _soundEnabled = getConfigValue(_configVarMap, "sound"); _port = getConfigValue(_configVarMap, "port", "4567"); _debug = getConfigValue(_configVarMap, "debug", "false"); #ifdef NONETWORK _network = getConfigValue(_configVarMap, "network", "false"); #else _network = getConfigValue(_configVarMap, "network", "true"); #endif _vsync = getConfigValue(_configVarMap, "vsync", "true"); _textureSize = getConfigValue(_configVarMap, "texturesize", "auto"); _grabMouse = getConfigValue(_configVarMap, "grabmouse", "true"); _soundEngine = getConfigValue(_configVarMap, "soundengine", "sdl"); _particles = getConfigValue(_configVarMap, "particles", "0"); _renderToTexture = getConfigValue(_configVarMap, "rendertotexture", "1"); getConfigValue(_configVarMap, "red", "8"); getConfigValue(_configVarMap, "green", "8"); getConfigValue(_configVarMap, "blue", "8"); _serverName = getConfigVar("servername", System.getCurrentUser()); _name = getConfigVar("name", System.getCurrentUser()); _debugui = getConfigVar("debugui", "false"); _debugEntity = getConfigVar("debugentity", "false", true); getConfigVar("alreadyrated", "false", true); _mode = getConfigValue(_configVarMap, "mode", ""); #if 0 // disabled because the vars must be created on access - to set the flags properly for (KeyValueMap::iterator i = _configVarMap.begin(); i != _configVarMap.end(); ++i) { getConfigVar(i->first, i->second, true); } std::vector<std::string> vars; _persister->getVars(vars); for (std::vector<std::string>::const_iterator i = vars.begin(); i != vars.end(); ++i) { getConfigVar(*i); } for (auto entry : _configVars) { Log::info(LOG_COMMON, "'%s' with value '%s'", entry.first.c_str(), entry.second->getValue().c_str()); } #endif memset(&_debugRendererData, 0, sizeof(_debugRendererData)); Log::info(LOG_COMMON, "mouse grab enabled: %s", _grabMouse->getValue().c_str()); Log::info(LOG_COMMON, "controller enabled: %s", _gameController->getValue().c_str()); Log::info(LOG_COMMON, " sound enabled: %s", _soundEnabled->getValue().c_str()); Log::info(LOG_COMMON, " debug enabled: %s", _debug->getValue().c_str()); Commands.registerCommand("loglevel", bindFunction(ConfigManager::setLogLevel)); CommandPtr cmd = Commands.registerCommand(CMD_SETVAR, bindFunction(ConfigManager::setConfig)); cmd->setCompleter([&] (const std::string& input, std::vector<std::string>& matches) { for (auto entry : _configVars) { if (!string::startsWith(entry.first, input)) continue; matches.push_back(entry.first); } }); Commands.registerCommand(CMD_LISTVARS, bindFunction(ConfigManager::listConfigVariables)); for (int i = 0; i < argc; i++) { if (argv[i][0] != '-') continue; const std::string command = &argv[i][1]; if (command == CMD_SETVAR) { if (i + 2 >= argc) continue; const std::string var = argv[i + 1]; const std::string val = argv[i + 2]; ConfigVarPtr p = getConfigVar(var); p->setValue(val); *argv[i] = *argv[i + 1] = *argv[i + 2] = '\0'; i += 2; } else if (command == "loglevel") { if (i + 1 >= argc) continue; ICommand::Args a; a.push_back(argv[i + 1]); setLogLevel(a); *argv[i] = *argv[i + 1] = '\0'; i += 1; } } increaseCounter("launchcount"); _persister->save(_configVars); }