int formatArgs(char *command, char **argv) { char *lastArg = command; char *nextArg = splitCommand(lastArg); argv[0] = lastArg; int argNum = 1; while(nextArg != lastArg) { argv[argNum++] = nextArg; lastArg = nextArg; nextArg = splitCommand(lastArg); } return argNum; }
void processCommand(void){ splitCommand(getCommand()); setParameters(); if (ready()){ run(); } }
WekaClassifier::WekaClassifier(const std::vector<Feature> &features, bool caching, const std::string &opts) : Classifier(features,caching), dropFrac(0.0), trainAllowed(true) { classifierCount++; memSegName = "WEKA_BRIDGE_" + boost::lexical_cast<std::string>(getpid()) + "_" + boost::lexical_cast<std::string>(classifierCount); comm = boost::shared_ptr<Communicator>(new Communicator(memSegName,true,features.size(),numClasses)); // fork you pid = fork(); if (pid < 0) { // failed to fork std::cerr << "WekaClassifier: ERROR: failed in fork: " << pid << std::endl; exit(65); } else if (pid == 0) { //child std::string cmd = WEKA_CMD + " " + memSegName + " " + "data/dt/blank.arff" + " " + opts; char** cmdArr = splitCommand(cmd); execvp(cmdArr[0],cmdArr); freeCommand(cmdArr); exit(0); return; } else { //parent } *(comm->cmd) = '\0'; }
int tabComplete(char *command) { char *commands[NUMCOMMANDS] = {"q", "tr", "rv", "sw", "setTrack", "move", "randomizeSwitches", "init", "clear", "configureVelocities"}; int length = strlen(command); char *arg1 = splitCommand(command); if(arg1 != command) { *(arg1-1) = ' '; return length; } int maxMatched = -1; char *matchedIn = ""; int i; for(i=0; i<NUMCOMMANDS; i++) { int prefix = largestPrefix(command, commands[i]); if(maxMatched == -1 && prefix >= length) { maxMatched = strlen(commands[i]); matchedIn = commands[i]; }else if(prefix >= length) { maxMatched = largestPrefix(matchedIn, commands[i]); } } if(maxMatched == length) { outputEscape("[B[100D"); for(i=0; i<NUMCOMMANDS; i++) { int prefix = largestPrefix(command, commands[i]); if(maxMatched == prefix) { printf("%s\t", commands[i]); } } outputEscape("[B"); } if(maxMatched == -1) { return length; } for(i=0; i<maxMatched; i++) { command[i] = matchedIn[i]; } if(maxMatched == strlen(matchedIn)) { command[maxMatched] = ' '; maxMatched++; } command[maxMatched] = '\0'; return maxMatched; }
int main(int argc, char *argv[]){ global_program = 0; global_output = 0; int i,len; char** cmd; cmd = malloc(sizeof(char*)*(argc - 1)); printf("argc:\t%i\n", argc); for (i = 1; i < argc; i++){ printf("argv[%i]:\t%s\n", i, argv[i]); len = strlen(argv[i]); cmd[i-1] = malloc(sizeof(char)*len); cmd[i-1] = argv[i]; if (strcmp(cmd[i-1], "quit") == 0){ printf("Program terminates successfully by the user\n"); break; } splitCommand(cmd[i-1], len); } free(cmd); return 0; }
void handleClient(session* ses) { //write(socket, buf, BUF_SIZE); string response("220 Service ready for new user.\r\n"); write(ses->csck, response.c_str(), response.length()); // command loop int status; while (true) { char cmd[commandSize] = {0}; status = read(ses->csck, cmd, commandSize-1); // last character for null termination if (status == 0) { // connection closed return; } else { ERROR(status); list<string> args = splitCommand(cmd); execCmd(ses, args); } } }
int main() { int pipe_fd[2]; int status; pipe(pipe_fd); char *argv[MAXLINE]; pid_t child1, child2; while (1) { getUserNameLine(userLine); printf("[%s]", userLine); fgets(command, MAXLINE, stdin); if(splitCommand(command, argv)) { printf("split failed!\n"); } if ((child1 = fork())) { if ((child2) = fork() == 0) { close(pipe_fd[1]); close(fileno(stdin)); dup2(pipe_fd[0] ,fileno(stdin)); close(pipe_fd[0] ); execvp(argv[0], argv); } else { close(pipe_fd[0]); close(pipe_fd[1]); waitpid(child2, &status , 0); } waitpid(child1, &status, 0); } else { printf ("subshell 3cmd %d\n", getpid()); close (pipe_fd[0]); close (fileno(stdout)); dup2 (pipe_fd[1], fileno(stdout)); close(pipe_fd[1]); execvp(argv[0], argv); } } return 0; }
void Command::runCommand(std::string const &input) { static const struct { std::string name; void (Command::*ptr)(std::vector<std::string>&); } commands[] = { {"cat", &Command::cat}, {"cd", &Command::cd}, {"cp", &Command::cp}, {"dir", &Command::dir}, {"exit", &Command::exit}, {"fdisk", &Command::fdisk}, {"help", &Command::help} }; std::vector<std::string> tokens; splitCommand(input, tokens); if (tokens.size() == 0) return; size_t i = 0; for (; i < sizeof(commands) / sizeof(commands[0]); ++i) { if (tokens[0] == commands[i].name) { (this->*commands[i].ptr)(tokens); break; } } if (i == sizeof(commands) / sizeof(commands[0])) std::cerr << "Error: " << tokens[0] << ": unrecognized command (try help?)" << std::endl; }
int main(void) { char command[MAX_LINE]; char *args[MAX_LINE/2+1]; int should_run = 1; int top = 0; char stack[MAX_COMMAND][MAX_LINE]; signal(SIGCHLD, handler); while (should_run) { print_prompt(); fflush(stdout); if (fgets(command, MAX_LINE, stdin)==NULL) { perror("fgets"); return 1; } command[strlen(command)-1] = '\0'; if (strlen(command)==0) continue; else if (strcmp(command, "exit")==0) should_run = 0; else if (strcmp(command, "history")==0) { int i; for (i=0; i<10 && i<top; ++i) printf("\t%d %s\n", top-i, stack[top-i]); } else{ if (strcmp(command, "!!")==0) { if (top) strcpy(command, stack[top]); else{ printf("No commands in history.\n"); continue; } } else if (command[0]=='!') { int index = atoi(command+1); if (0<index && index<=top) strcpy(command, stack[index]); else{ printf("No such command in history.\n"); continue; } } strcpy(stack[++top], command); int background = splitCommand(command, args); if (strcmp(args[0], "cd")==0) { int ret = chdir(args[1]); if (ret==-1) perror("cd"); continue; } pid_t pid = fork(); if (!pid) { int p1 = 0, p2 = 0, p3 = 0, i = 0; while (args[++i]!=NULL) if (strcmp(args[i], "<")==0) p1 = i; else if (strcmp(args[i], ">")==0) p2 = i; else if (strcmp(args[i], ">>")==0) p3 = i; if (p1) freopen(args[p1+1], "r", stdin); if (p2) freopen(args[p2+1], "w", stdout); if (p3) freopen(args[p3+1], "a", stdout); if (p1) args[p1] = NULL; if (p2) args[p2] = NULL; if (p3) args[p3] = NULL; int ret = execvp(args[0], args); if (ret==-1) { perror("execvp"); return 1; } } if (!background) wait(NULL); } } return 0; }