static _TCHAR* findProgram(_TCHAR* argv[]) { _TCHAR * program; #ifdef _WIN32 /* windows, make sure we are looking for the .exe */ _TCHAR * ch; int length = _tcslen(argv[0]); ch = malloc( (length + 5) * sizeof(_TCHAR)); _tcscpy(ch, argv[0]); if (length <= 4 || _tcsicmp( &ch[ length - 4 ], _T_ECLIPSE(".exe") ) != 0) _tcscat(ch, _T_ECLIPSE(".exe")); program = findCommand(ch); if (ch != program) free(ch); #else program = findCommand( argv[0] ); #endif if (program == NULL) { #ifdef _WIN32 program = malloc( MAX_PATH_LENGTH + 1 ); GetModuleFileName( NULL, program, MAX_PATH_LENGTH ); argv[0] = program; #else program = malloc( (strlen( argv[0] ) + 1) * sizeof(_TCHAR) ); strcpy( program, argv[0] ); #endif } else if (_tcscmp(argv[0], program) != 0) { argv[0] = program; } return program; }
bool MgCmdManagerImpl::setCommand(const MgMotion* sender, const char* name, MgStorage* s) { if (strcmp(name, "@draw") == 0) { // 将 @draw 换成上一次绘图命令名 name = _drawcmd.empty() ? "splines" : _drawcmd.c_str(); } MgCommand* cmd = findCommand(name); if (!cmd) { cmd = sender->view->getCmdSubject()->createCommand(sender, name); if (cmd) { _cmds[name] = cmd; LOGD("createCommand %d: %s", (int)_cmds.size(), name); } } if (strcmp(name, "erase") == 0 && _cmdname == "select") { // 在选择命令中点橡皮擦 MgSelection *sel = getSelection(); if (sel && sel->deleteSelection(sender)) { // 直接删除选中的图形 return false; // 不切换到橡皮擦命令 } } cancel(sender); bool ret = false; std::string oldname(_cmdname); if (cmd) { _cmdname = cmd->getName(); ret = cmd->initialize(sender, s); if (!ret) { _cmdname = oldname; } else if (cmd->isDrawingCommand()) { _drawcmd = _cmdname; } } else { if (strcmp(name, "erasewnd") == 0) { eraseWnd(sender); } else { _cmdname = "select"; cmd = findCommand(_cmdname.c_str()); cmd->initialize(sender, s); } } if (MgBaseShape::minTol().equalPoint() < 1e-5) { MgBaseShape::minTol().setEqualPoint(sender->view->xform()->displayToModel(1.f, true)); } if (oldname != _cmdname) { sender->view->commandChanged(); } return ret; }
void CommandInterpreter::complete() { if (mFirstSpace == 0) { Possibilities possible; findCommand(mLine, mLineLen, possible); const char* useName = possible.string(); if (possible.count() == 1) { int useLen = strlen(useName); strncpy(mLine + mLineLen, useName + mLineLen, useLen - mLineLen); mLineLen = useLen; mFirstSpace = mLineLen - 1; printLine(); } else if (possible.count() > 1) { std::printf("\n%s\n", useName); printLine(); } } else { Possibilities possible; Command* cmd = findCommand(mLine, mFirstSpace, possible); if (cmd != nullptr) { unsigned int argc = findArguments(false); if (argc >= 2) { Argument arg; arg.name = cmd->argument(argc - 2); arg.value.s = nullptr; parseArgument(arg); if (arg.enhancedType == Argument::EnhancedType::Address) { unsigned int i = mLineLen - 1; bool hex = false; while (i > 0 && mLine[i] != ' ') { if (mLine[i] == 'x' || mLine[i] == 'X') { hex = true; break; } --i; } if (hex) { for (unsigned int j = mLineLen - i; j < 9; ++j) mLine[mLineLen++] = '0'; mLine[mLineLen++] = ' '; printLine(); } } } } } }
int main(int argc, char *argv[], char *envp[]) { if (argc < 2) { char **scan; for (scan=envp; *scan!=NULL; scan++) { printf("%s\n", *scan); }; return 0; }; if (findCommand(path, argv[1]) != 0) { fprintf(stderr, "%s: %s: command not found\n", argv[0], argv[1]); return 1; }; if (execv(path, &argv[1]) != 0) { perror(path); return 1; }; // never reached return 0; };
boolean isCommand(Char *command_) { Char command[256]; strcpy(command, command_); return (findCommand(command) != none); }
void shell() { Console_Write_String("\nEFOS>"); Console_Read_Line(); // Readline = 0; char * buf = Readline; buf = 0; Console_Write_String_Colored(buf,ColorYellow); int i = findCommand(Readline); if(i >= 0) { void (*command_function) (); command_function = CommandTable[i].function; command_function(); } else { Console_Write_String_Colored("\nCommand Not Found",ColorRed); //Console_Write_String_Colored(i,ColorRed); } // void (*command_run)(void); }
std::string SystemInfo::findExecutable_(std::string name) { std::string findCommand(pathToWhich_); findCommand += " "; findCommand += name; job::ostream in; job::istream out; job::istream err; try { job::service js(saga::url("localhost")); job::job j1 = js.run_job (findCommand, "localhost", in, out, err); job::state state = j1.get_state (); if(state != job::Running && state != job::Done) { //TODO - something went wrong } } catch (std::exception const & e) { std::cout << "error: " << e.what() << std::endl; } std::string path(""); while(true) { char buffer[255]; out.read (buffer, sizeof (buffer)); if (out.gcount() > 0) { path += std::string (buffer, out.gcount ()); } if(out.fail()) { break; } } return path; }
int main() { int command_code = 0; initRoot(); // Initialize the '/' root directory of the tree cwd = root; // Set cwd to root while(command_code != 10) { printf("Enter a command or enter menu: "); getInput(); command_code = findCommand(); breakPathname(); // printf("pathname = %s, command = %s, command code = %d, dirname = %s, basename = %s\n", pathname, command, command_code, dirname, basename); // For testing switch (command_code) { case 0: printMenu(); break; case 1: mkdir(); break; case 2: rmdir(); break; case 3: cd(); break; case 4: ls(); break; case 5: pwd(); break; case 6: creat(); break; case 7: rm(); break; case 8: save(); break; case 9: reload(); break; case 10: quit(); break; default: printf("Invalid command\n"); break; } } return 0; }
void setup(char inputBuffer[], char *args[], int *background) { emptyPointerArray(args, MAX_ARGS); int len = read(STDIN_FILENO, inputBuffer, MAX_LINE); //Remove EOL inputBuffer[--len] = '\0'; int nElements; splitStringBySpace(args, inputBuffer, &nElements); if (nElements > 0 && *(args[nElements - 1]) == '&') { args[nElements - 1] = NULL; *background = 1; } if (nElements > 0) { if (strcmp(args[0],"exit") == 0) { exit(0); } else if (strcmp(args[0],"r") == 0) { if (stackCounter > 0) { if (nElements > 1) { int i = findCommand(args[1]); printf("%s\n", history[i]); emptyPointerArray(args, MAX_ARGS); splitStringBySpace(args, history[i], &nElements); } else { printf("%s\n", history[stackCounter - 1]); splitStringBySpace(args, history[stackCounter - 1], &nElements); } } } else { pushToHistory(inputBuffer); } } }
/** * Parse one command line * @param context * @param data - complete command line * @param len - command line length * @return 1 if the last evaluated command was found */ int SCPI_Parse(scpi_t * context, const char * data, size_t len) { int result = 0; const char * cmdline_end = data + len; const char * cmdline_ptr = data; size_t cmd_len; size_t cmdline_len; if (context == NULL) { return -1; } while (cmdline_ptr < cmdline_end) { result = 0; cmd_len = cmdTerminatorPos(cmdline_ptr, cmdline_end - cmdline_ptr); cmdline_len = cmdlineSeparatorPos(cmdline_ptr, cmdline_end - cmdline_ptr); if (cmd_len > 0) { if(findCommand(context, cmdline_ptr, cmdline_len, cmd_len)) { processCommand(context); result = 1; } else { SCPI_ErrorPush(context, SCPI_ERROR_UNDEFINED_HEADER); } } cmdline_ptr = cmdlineNext(cmdline_ptr, cmdline_end - cmdline_ptr); } return result; }
/** * Parse one command line * @param context * @param data - complete command line * @param len - command line length * @return 1 if the last evaluated command was found */ int SCPI_Parse(scpi_t * context, char * data, size_t len) { int result = 0; const char * cmdline_end = data + len; char * cmdline_ptr = data; size_t cmd_len; size_t cmdline_len; char * cmdline_ptr_prev = NULL; size_t cmd_len_prev = 0; if (context == NULL) { return -1; } while (cmdline_ptr < cmdline_end) { result = 0; cmd_len = cmdTerminatorPos(cmdline_ptr, cmdline_end - cmdline_ptr); if (cmd_len > 0) { composeCompoundCommand(cmdline_ptr_prev, cmd_len_prev, &cmdline_ptr, &cmd_len); cmdline_len = cmdlineSeparatorPos(cmdline_ptr, cmdline_end - cmdline_ptr); if(findCommand(context, cmdline_ptr, cmdline_len, cmd_len)) { processCommand(context); result = 1; cmdline_ptr_prev = cmdline_ptr; cmd_len_prev = cmd_len; } else { SCPI_ErrorPush(context, SCPI_ERROR_UNDEFINED_HEADER); } } cmdline_ptr += skipCmdLine(cmdline_ptr, cmdline_end - cmdline_ptr); cmdline_ptr += skipWhitespace(cmdline_ptr, cmdline_end - cmdline_ptr); } return result; }
void packetManager(struct PlinkBuffer * commandBuffer) { if (deFormatBuffer(commandBuffer)) { DEBUG(LOG_DEBUG, "Packet received"); logBuffer(LOG_DEBUG,commandBuffer); int cmd_not_recognized=1; int i; for (i=0;i<Pmax_NBCOMMAND;i++) { if (findCommand(commandBuffer, &PmaxCommand[i])) { PmaxCommand[i].action(commandBuffer); cmd_not_recognized=0; break; } } if ( cmd_not_recognized==1 ) { DEBUG(LOG_INFO, "Packet not recognized"); logBuffer(LOG_INFO, commandBuffer); sendBuffer(&PowerlinkCommand[Pmax_ACK]); } } else { DEBUG(LOG_ERR, "Packet not correctly formated"); logBuffer(LOG_ERR,commandBuffer); } //command has been treated, reset the commandbuffer commandBuffer->size=0; DEBUG(LOG_DEBUG, "End of packet treatment"); }
pair<bool, std::string> Program::getCommandName(const vector<string> &command_line) { vector<string> positionals; po::options_description options(""); addGlobalOptions(options); options.add_options() ("positional_commands", po::value<vector<string>>(&positionals), "") ; po::positional_options_description positional_options; positional_options.add("positional_commands", -1); try{ auto parsed = po::command_line_parser(command_line).options(options).positional(positional_options).allow_unregistered().run(); po::variables_map vm; po::store(parsed, vm); po::notify(vm); if (m_version){ cout << program_name << " " << version::version << "\n"; return pair<bool, string>(false, ""); } for (size_t i = 0; i < positionals.size(); i++){ if (findCommand(positionals[i].c_str())){ return pair<bool, string>(true, positionals[i]); } } for (size_t i = 0; i < positionals.size(); i++){ return pair<bool, string>(true, positionals[i]); } return pair<bool, string>(true, ""); }catch(const exception &e){ return pair<bool, string>(true, ""); } }
/** * Remove command from the command list, effectively disabling it */ void CommandHandler::removeCommand(char command){ int commandSlot = findCommand(command); if(commandSlot >= 0){ _commandList[commandSlot].command = 0; _commandList[commandSlot].function = 0; } }
void NestedCommandLineApp::displayHelp( const po::variables_map& /* globalOptions */, const std::vector<std::string>& args) { if (args.empty()) { // General help printf( "Usage: %s [global_options...] <command> [command_options...] " "[command_args...]\n\n", programName_.c_str()); std::cout << globalOptions_; printf("\nAvailable commands:\n"); size_t maxLen = 0; for (auto& p : commands_) { maxLen = std::max(maxLen, p.first.size()); } for (auto& p : aliases_) { maxLen = std::max(maxLen, p.first.size()); } for (auto& p : commands_) { printf(" %-*s %s\n", int(maxLen), p.first.c_str(), p.second.shortHelp.c_str()); } if (!aliases_.empty()) { printf("\nAvailable aliases:\n"); for (auto& p : aliases_) { printf(" %-*s => %s\n", int(maxLen), p.first.c_str(), resolveAlias(p.second).c_str()); } } } else { // Help for a given command auto& p = findCommand(args.front()); if (p.first != args.front()) { printf("`%s' is an alias for `%s'; showing help for `%s'\n", args.front().c_str(), p.first.c_str(), p.first.c_str()); } auto& info = p.second; printf( "Usage: %s [global_options...] %s%s%s%s\n\n", programName_.c_str(), p.first.c_str(), info.options.options().empty() ? "" : " [command_options...]", info.argStr.empty() ? "" : " ", info.argStr.c_str()); std::cout << globalOptions_; if (!info.options.options().empty()) { printf("\n"); std::cout << info.options; } printf("\n%s\n", info.fullHelp.c_str()); } }
int Program::run(int argc, char **argv) { setLocale(); #ifdef WIN32 auto command_line = po::split_winmain(ucsToUtf8(wstring(GetCommandLine()))); command_line.erase(command_line.begin()); #else vector<string> command_line; command_line.resize(argc - 1); for (int i = 1; i < argc; i++) { command_line[i - 1] = argv[i]; } #endif auto command_name = getCommandName(command_line); if (!command_name.first) return EXIT_FAILURE; if (command_name.first && command_name.second == ""){ printHelp(); return EXIT_FAILURE; } auto command = findCommand(command_name.second.c_str()); if (!command){ cerr << program_name << ": " << "unknown command '" << command_name.second << "'" << "\n"; printHelp(); return EXIT_FAILURE; } try{ po::options_description all_options, global_options, options, hidden_options; po::positional_options_description positional_options; string command_confirm; addGlobalOptions(global_options); hidden_options.add_options() ("command", po::value<string>(&command_confirm), "") ; positional_options.add("command", 1); command->addOptions(options, hidden_options); command->addPositionalOptions(positional_options); all_options.add(global_options).add(options).add(hidden_options); auto parsed = po::command_line_parser(command_line).options(all_options).positional(positional_options).run(); po::variables_map vm; po::store(parsed, vm); po::notify(vm); if (vm.count("help")) { printHelp(*command); return EXIT_SUCCESS; } if (!command->checkOptions(vm)){ printHelp(*command); return EXIT_FAILURE; } return command->run(); }catch(const exception &e){ cerr << program_name << ": " << e.what() << "\n"; return EXIT_FAILURE; } }
Command::LockType Command::locktype(const string& name) { Command* c = findCommand(name); if (! c) { return WRITE; } return c->locktype(); }
/** * Upon /os reload refresh the count **/ static int reload_config(int argc, char **argv) { Command *c; if (argc >= 1 && !stricmp(argv[0], EVENT_START)) if ((c = findCommand(OPERSERV, "OPERNEWS"))) c->help_param1 = (char *) (long) NewsCount; return MOD_CONT; }
void runShell(char* command) { NODE* cmd; cmd = findCommand(command); if (cmd) cmd->cmd(); tcputs("$>> ", COLOR_GREEN); waitCmd(); }
void CommandInterpreter::execute() { mLine[mLineLen] = 0; addToHistory(mLine); // first we split our line into seperate strings (by replacing whitespace with binary 0) unsigned int argc = findArguments(true); if (mArguments[0].value.s[0] == 0) return; Possibilities possible; Command* cmd = findCommand(mArguments[0].value.s, strlen(mArguments[0].value.s), possible); if (cmd == nullptr) { printf("Unknown command: %s", mArguments[0].value.s); // const char* p = mArguments[0].value.s; // while (*p != 0) // { // printf(" %02x", *p++); // } printf(", try help.\n"); return; } unsigned int maxArgc = std::min(static_cast<unsigned int>(MAX_ARG_LEN), cmd->argumentCount() + 1); unsigned int minArgc = 1; bool failed = false; // If everything is fine so far we parse our arguments and check if they are valid. for (unsigned int i = 1; i < argc; ++i) { mArguments[i].name = cmd->argument(i - 1); //printf("Parsing %s/%s: ", argv[i].name, argv[i].value.s); if (!parseArgument(mArguments[i])) { failed = true; break; } //printf("%u\n", argv[i].value.u); if (!mArguments[i].optional) ++minArgc; } // If we fond a problem print a usage message. if (failed || argc < minArgc || argc > maxArgc) { if (argc > maxArgc || argc < minArgc) printf("Wrong argument count (%i != [%i, %i]).\n", argc - 1, minArgc - 1, maxArgc - 1); printUsage(cmd); return; } // Everything is fine, execute it. if (cmd != nullptr) { uint64_t start = System::instance()->ns(); cmd->execute(*this, argc, mArguments); if (mCommandTime) { uint64_t delta = System::instance()->ns() - start; printf("Command took %u.%06us\n", static_cast<unsigned int>(delta / 1000000000), static_cast<unsigned int>((delta / 1000) % 1000000)); } } }
/** * Adds a "command" and a handler function to the list of available commands. * This is used for matching a found token in the buffer, and gives the pointer * to the handler function to deal with it. */ void CommandHandler::addCommand(const char command, void (*function)()) { // Only write the command if it doesn't already exist if (findCommand(command) == NOT_FOUND){ int freeSlot = findFreeCommandSlot(); if (freeSlot >= 0 && command != EMPTY){ _commandList[freeSlot].command = command; _commandList[freeSlot].function = function; } } }
/* * Execute a command from the commandline * * Return true if it was a valid command */ bool Command::execCommand(const Command args[], uint8_t nr, const char * line) { int ix = findCommand(args, nr, line); if (ix < 0) { return false; } const Command *s = &args[ix]; if (s->exec_func) { s->exec_func(s, line + strlen(s->cmd_prefix)); } return true; }
int CommandProcessor::executeCommand(int argc, char* argv[]) { int returnStatus = COMMAND_FAILED; Command* command; returnStatus = findCommand(argv[0], &command); if(returnStatus == COMMAND_SUCCESS && command) { returnStatus = command->execute(argc, argv); } return(returnStatus); }
Static line_type doCommand(Char *line_) { line_type Result; Char line[256]; Char command[256]; command_type last_command; boolean starts_with_note; Char STR1[256], STR3[256]; strcpy(line, line_); if (line[0] == comment && !isAssertion(line)) return comment_line; starts_with_note = maybeMusicLine(line); GetNextWord(command, line, blank_, colon_); sprintf(STR1, "%c", colon_); if (endsWith(command, STR1)) { last_command = findCommand(command); Result = command_line; if (last_command == enable) doEnable(line, true); else if (last_command == disable) doEnable(line, false); else if (last_command == range) orig_range_line = line_no; if (last_command != none) { if (mustAppend(last_command) && redefined[(long)last_command]) sprintf(cline[(long)last_command] + strlen(cline[(long)last_command]), "\n%s", line); else { strcpy(cline[(long)last_command], line); if (warn_redefine && redefined[(long)last_command]) { sprintf(STR3, "You have redefined preamble command %s", command); warning(STR3, print); } } if (last_command == start) strcpy(start_line, line); redefined[(long)last_command] = true; return Result; } Result = colon_line; sprintf(STR3, "%s%c %s", command, colon_, line); addStyle(STR3); orig_style_line[known_styles-1] = line_no; return Result; } else if (starts_with_note) return plain_line; else return unknown; return Result; }
void ActionDialog::accept() { QVariant itemData = createCurrentItemData(); int i = findCommand(*ui->comboBoxCommands, itemData); if (i != -1) ui->comboBoxCommands->removeItem(i); const QString text = ui->commandEdit->command(); ui->comboBoxCommands->insertItem(1, commandToLabel(text), itemData); saveHistory(); QDialog::accept(); }
int main() { char *s, *buf, *cmd, **args; int i, pos; buf = malloc(STRING_SIZE * sizeof(char)); cmd = malloc(STRING_SIZE * sizeof(char)); if (buf == NULL || cmd == NULL) { printf("Impossivel alocar memoria.\n"); exit(EXIT_FAILURE); } agenda ag = createAgenda(); carregaContatos(ag); args = malloc(ARGS_SIZE * sizeof(char*)); for (i = 0; i < ARGS_SIZE; i++) { args[i] = malloc(STRING_SIZE * sizeof(char)); if (args[i] == NULL) { printf("Impossivel alocar memoria para argumentos.\n"); exit(EXIT_FAILURE); } } printf("Agenda Topicos de Programacao 2016\n"); while (1) { printf("> "); if (fgets(buf, STRING_SIZE, stdin) == NULL) normalexit(NULL, 0, NULL, ag); if (sscanf(buf, " %s %n", cmd, &pos) == EOF) { // evita que um comando seja chamado duas vezes caso uma string vazia seja passada cmd = ""; pos = 0; } s = buf + pos; i = 0; while (sscanf(s, "%s %n", args[i], &pos) != EOF) { i++; s = s + pos; } int (*foo)(char*, int, char**, agenda) = findCommand(cmd); (*foo)(cmd, i, args, ag); } free(buf); free(cmd); return EXIT_SUCCESS; }
// robosolver <size> <pos> move <from> <direction> // robosolver <size> <pos> to <from> <to> // robosolver <size> <pos> find <color> // robosolver <size> <pos> solve <to> int main(int argc, const char** argv) { N = atoi(argv[1]); field f = parse(strdup(argv[2])); if (strcmp("move", argv[3]) == 0) { moveCommand(f, atoi(argv[4]), atoi(argv[5])); } else if (strcmp("to", argv[3]) == 0) { toCommand(f, atoi(argv[4]), atoi(argv[5])); } else if (strcmp("find", argv[3]) == 0) { findCommand(f, atoi(argv[4])); } else if (strcmp("solve", argv[3]) == 0) { solveCommand(f, atoi(argv[4])); } return 0; }
void Console::verifyRegistration(const std::string &proc, const std::string &name, bool isNull) { if (name.empty()) { D6_THROW(ConsoleException, CON_Format(CON_Lang("{0} : cannot register empty name")) << proc); } if (findCommand(name) != nullptr || findVar(name) != nullptr || findAlias(name) != nullptr) { D6_THROW(ConsoleException, CON_Format(CON_Lang("{0} : name \"{1}\" is already registered")) << proc << name); } if (isNull) { D6_THROW(ConsoleException, CON_Format(CON_Lang("{0} : attempt to register \"{1}\" with null pointer")) << proc << name); } }
/** * Process the buffer to find commands */ void CommandHandler::processCommands(){ while (isCacheAvailable()){ char command = readCache(); int commandSlot = findCommand(command); // Command found; do the thing if (commandSlot >= 0){ _commandList[commandSlot].function(); } // Command not found; do the default thing else{ (*defaultHandler)(command); } } clearCache(); }
void NestedCommandLineApp::doRun(const std::vector<std::string>& args) { if (programName_.empty()) { programName_ = guessProgramName(); } auto parsed = parseNestedCommandLine(args, globalOptions_); po::variables_map vm; po::store(parsed.options, vm); if (vm.count("help")) { std::vector<std::string> helpArgs; if (parsed.command) { helpArgs.push_back(*parsed.command); } displayHelp(vm, helpArgs); return; } if (vm.count("version")) { printf("%s %s\n", programName_.c_str(), version_.c_str()); return; } if (!parsed.command) { throw ProgramExit( 1, folly::sformat("Command not specified. Run `{} help' for help.", programName_)); } auto& p = findCommand(*parsed.command); auto& cmd = p.first; auto& info = p.second; auto cmdOptions = po::command_line_parser(parsed.rest).options(info.options).run(); po::store(cmdOptions, vm); po::notify(vm); auto cmdArgs = po::collect_unrecognized(cmdOptions.options, po::include_positional); if (initFunction_) { initFunction_(cmd, vm, cmdArgs); } info.command(vm, cmdArgs); }