// i can do it better .. int switch_cmd(FILE *fd, struct opt options) { int ret; switch(options.act) { case CUSTOM: ret = custom_cmd(fd, options.custom_cmd); break; case READ: ret = rw_cmd(fd, options); break; case WRITE: ret = rw_cmd(fd, options); break; case SEARCH: ret = search_cmd(fd, options); break; case LIST: ret = list_cmd(fd); break; case INFO: ret = info_cmd(fd); break; } return ret; }
void set_cmd(t_app *app) { if (app->cur_cmd->first->command[0] == '.' || app->cur_cmd->first->command[0] == '/') app->cur_cmd->cmd = check_cmd(app); else app->cur_cmd->cmd = search_cmd(app); }
int main(void){ char cmd[MAX_CMD_LENGTH]=""; //Command entered by the user including arguments,pipes,redirections,etc. char *tokens[MAX_CMD_LENGTH]={NULL}; //Stores the parsed command(Eg: token[0]=ls, token[1]=-l) char *commands[PATH_MAX]={NULL}; //Pathname of the actual commands. Commands may be more than one due to piping. char *target_cmd; int cpid,child_err_code; //Initialize PS1 content init_ps1(); while(1){ printf("%s",PS1); //Display prompt(PS1) if(!fgets(cmd,MAX_CMD_LENGTH,stdin)){ //Get input perror("fgets"); exit(EXIT_FAILURE); } if(strcmp(cmd,"\n") == 0) //If Empty command then new prompt again continue; cmd[strlen(cmd)-1] = '\0'; //Replace '\n' with a '\0' if(strcmp(cmd,"exit") == 0) //Exit code exit(0); //Tokenize the command if(tokenize_cmd(cmd,tokens) == -1){ fprintf(stderr,"Shell Error: Too may tokens in command.\n"); continue; } //Extract command names if(parse_cmd(tokens,commands) == -1){ fprintf(stderr,"Shell Error: Unable to parse command.\n"); continue; } target_cmd = search_cmd(commands[0]); //TODO....Modify this code for every commands //This is the core function of a shell. Rest are features. //Execute command by invoking the respective program for it using exec() //TODO.... cpid = fork(); if(cpid == -1){ perror("fork"); exit(EXIT_FAILURE); } if(cpid == 0){ //Child process if(execv(target_cmd,tokens) == -1){ //TODO...Extract command pathname and argument list perror("execv"); exit(EXIT_FAILURE); //Child exits if failed } } wait(&child_err_code); } return 0; }
void terminal_process(void) { uint8_t idx_start, idx_end, c; idx_start = 0; while(term_in[idx_start]== ' ' || term_in[idx_start]== '\t') idx_start++; idx_end = idx_start; while(term_in[idx_end] != ' ' && term_in[idx_end] != '\t' && term_in[idx_end] != '\0') idx_end++; term_in[idx_end] = '\0'; c = search_cmd(&term_in[idx_start]); cmds[c].cmd_func(&term_in[++idx_end]); }
functype_cmd search_cmd(const command_str *cmdlist,char *inputcmd,int *argpos) { int ilen = 0; int i = 0; int a; // 入力されたコマンドの文字列長を数える while(inputcmd[ilen] && (inputcmd[ilen] != ' ')) ilen++; // 引数の先頭箇所を探す a = ilen; while(inputcmd[a] && (inputcmd[a] == ' ')) a++; if(argpos) *argpos += a; // コマンドを探す while(cmdlist[i].func || cmdlist[i].sub) { printf("[%s]-[%s]\n",cmdlist[i].name,inputcmd); if(!strncmp(cmdlist[i].name,inputcmd,ilen)) // 名前が一致? { if(strlen(cmdlist[i].name) == ilen) // 長さが一致? { lastcmd = (command_str *)cmdlist; strncat(helphelp,inputcmd,a); if(cmdlist[i].sub) // サブコマンドあり? { // strncat(helphelp,inputcmd,a); return search_cmd(cmdlist[i].sub,&inputcmd[a],argpos); } return cmdlist[i].func; } } i++; } return NULL; }