int main(int argc, char *argv[]) { if (argc == 2) findcommand(argv[1]); return 0; }
/* execute a command with optional wait * ALL FOREGROUND version */ int runcommand(char **cline, int argno, int ground) { pid_t pid, status; /* process info */ int ix; char cpath[MAXBUF]; /* command path */ char *command = cline[0]; /* try to find the command executable on system PATH */ if ( !(findcommand(cpath, command)) ) { fprintf(stderr, "Executable %s not found on system PATH.", command); return 0; } /* pre-exec info */ printf("\n==> ToRun: %s : %s ", cpath, command); for (ix=1; ix<argno; ix++) printf(": %s ", cline[ix]); printf("\n"); /* Different cases according to pid(parent or child), * take different actions */ switch (pid = fork()) { case -1: fprintf(stderr, "Error forking process.\n"); return -1; case 0: /* * ** execute command in child process ** */ if (execv(cpath, cline) == -1) fprintf(stderr, "Error executing: %s\n", *cline); exit(1); } /* parent wait */ if (waitpid(pid, &status, 0) == -1) { fprintf(stderr, "Error waiting for PID: %d\n", pid); return -1; } else { /* post-exec info */ if (!postexec()) { fprintf(stderr, "Error in post-exec function."); return -1; } return status; } }
int parseline(char *buf) { int argc; char *argv[30]; char *buf2; command *cmd; buf2 = strdup(buf); argc = linetoarray(buf, argv, 1, 30); // if (strcmp(argv[1], "372")) // printlog("<< %s", buf2); free(buf2); if (!strcmp(argv[0], "PING")) { irc_write(QUEUE_SERVER, "PONG %s", argv[1]); return 1; } if ((cmd = findcommand(argv[1], CTYPE_SERVER)) && cmd->srvhandler) cmd->srvhandler(argv[0], argc - 2, argv + 2); return 0; }
int extractcommand(char *cmdlist[], char *st, char **argstart, char **argend) { char *p,*cp; char cmdstr[MAXCMDLEN+1]; int cmd, i, pcount; i=0; p=st; cp=cmdstr; while((*p!='{') && (*p!=0) && (!isspace(*p)) && (i<MAXCMDLEN)) { *cp=*p; cp++; p++; i++; } *cp=0; if((cmd=findcommand(cmdlist,cmdstr))!=-1) { if(*p=='{') { pcount=1; p++; *argstart=p; while(((*p!='}') || (pcount!=1)) && (*p!='\n') && (*p!=0)) { if(*p=='{') pcount++; else if(*p=='}') pcount--; p++; } *argend=p; } else { *argstart=p; *argend=p; } } return cmd; }
int parsecommandline( uint8_t* buffer ) { char *str_component = 0; char *str_command = 0; char *str_args = 0; // Component starts at offset 0 str_component = (char*)buffer; // Skip ahead until we find a space while( *buffer != ' ' ) buffer++; // Replace the space with \0 to terminate the component string and increase pointer *buffer++ = 0; // Pointer is now at beginning of command str_command = (char*)buffer; // Skip until we find =, CR, LF or EOS while( *buffer && *buffer != '=' && *buffer != '\r' && *buffer != '\n' ) buffer++; // Did we find =? if( *buffer == '=' ) { // Yes: terminate command string *buffer++ = 0; // Set args to next char str_args = (char*)buffer; // And skip ahead until we find \n, \r or EOS while( *buffer != '\r' && *buffer != '\n' && buffer ) buffer++; // Terminate args string *buffer = 0; } else { // Didn't find = so just terminate string here. str_args will be 0. *buffer = 0; } // See if we can match the component const Component *component = findcomponent( str_component, components ); /* sniprintf( response_buffer, OUTPUT_BUFFER_SIZE, "Component: %s, ptr: %p, ptr->name: %s\r\n", str_component, component, component->component_name ); USB_CDC_print( response_buffer ); return 0; */ // If we didn't find any, return if( !component ) return 1; // We got the component; see if we can find the command const Command *command = findcommand( str_command, component->command_table ); // If we didn't find any, return if( !command ) return 2; // Check permissions // TODO: check permissions according to permissions flag and sender // Invoke function command->handler( str_args, response_buffer ); // And output result USB_CDC_print( response_buffer ); return 0; }