static void parseArguments(parsed p, char *input) { char **argv; char *arg; char *line; char *s; int i; int segments; segments = countArgs(input); if (segments > 0) { // create an array of char pointers argv = malloc(sizeof(char *) * segments); p->argv = argv; // create malloc'd strings line = strdup(input); s = strdup(input); s = strsep(&line, " "); i = 0; while (s != NULL) { if (strlen(s) > 0) { arg = strdup(s); p->argv[i] = arg; // if last argument is "&" if (i+1 == segments && checkBackground(arg)) { // don't add as an argument // change p->background -> TRUE // printf("adding as background..\n"); p->background = TRUE; free(arg); } else if (checkRedirection(arg)) { // don't add as an argument // change p->relevantStream -> new redirection // printf("adding as redirection..\n"); free(arg); } else { // add arg as an argument // increment arg counter in struct // printf("adding as new argv[%d]: #%s#\n", i, arg); // argv[i] = arg; // these two lines are equivalent. // arg = strsep(&arg, "\n"); // remove \n p->argv[i] = arg; p->argc += 1; i += 1; } } s = strsep(&line, " "); } free(line); free(s); } }
void executeCommand(char **command, char **path) { /* Searches for command present in one of the search folders On finding the command in a folder makes a child process and executes the command with parameters * */ int i=0; char *temp=malloc(2*MAXLINELENGTH); while(path[i]!=NULL) { temp=strcat(strcat(strdup(path[i]),"/"),&command[0][0]); struct stat *buf=malloc(sizeof(struct stat)); //Searches for commands in paths if(stat(temp,buf)==0&&strcmp(command[0],"pwd")!=0)//because pwd is new command { int rc=fork(); if(rc==0) { checkRedirection(temp,command); } else if(rc>0){int cpid=(int)wait(NULL);} else {callErrorFn();} //if fork fails return;//success } i++; } //section below for built in commands if(strcmp(&command[0][0],"exit")==0){exit(0);return;} if(strcmp(&command[0][0],"path")==0) { //incomplete // printf("**my implementation**\n"); int i=1; path[0]=NULL; while(command[i]!=NULL) { path[i-1]=strdup(command[i]); //printf("%s\n",path[i-1]); i++; } return; } if(strcmp(&command[0][0],"pwd")==0) { char buff[PATH_MAX+1]; char *cwd=getcwd(buff,PATH_MAX+1); printf("%s\n",cwd); return; } if(strcmp(&command[0][0],"cd")==0) { char buff[PATH_MAX+1]; if(command[1]==NULL) { char *cwd=strdup(getenv("HOME")); if(chdir(cwd)==-1){callErrorFn();} } else if(command[2]==NULL) { //char *cwd=strcat(strcat(getcwd(buff,PATH_MAX+1),"/"),command[1]); char *cwd=strdup(command[1]); if(chdir(cwd)==-1){callErrorFn();} } else{callErrorFn();exit(1);}//when more than one argument for cd return; } }
command_t makeCommandStreamUtil(int (*get_next_byte) (void *), void *get_next_byte_argument, STATE *state) { char **tokenPTR = checked_malloc(sizeof(char**)); char *token = NULL; int len = 0; TOKENTYPE type; command_t command = NULL; char *input = NULL, *output = NULL; type = readNextToken(tokenPTR, &len, get_next_byte, get_next_byte_argument); if (type == NOT_DEFINED) { free(tokenPTR); return NULL; } else if (type == O_PAR) { token = "("; } else { token = *tokenPTR; } command = AllocateCommand(); if (!command) { return NULL; } if (!strncmp(token, "then", 4)) { if (!(pop() == IF)) { printErr(); } push(THEN); *state = THEN; goto ret_null; } else if (!strncmp(token, "done", 4)) { if (!(pop() == DO)) { printErr(); } *state = DONE; CScount--; goto ret_null; } else if (!strncmp(token, "do", 4)) { STATE tmp = pop(); if (!((tmp == WHILE) || (tmp == UNTIL))) { printErr(); } push(DO); *state = DO; goto ret_null; } else if (!strncmp(token, "else", 4)) { if (!(pop() == THEN)) { printErr(); } push(ELSE); *state = ELSE; goto ret_null; } else if (!strncmp(token, "fi", 4)) { STATE tmp = pop(); if (!((tmp == THEN) || (tmp == ELSE))) { printErr(); } CScount--; *state = FI; goto ret_null; } else if (!strncmp(token, ")", 1)) { CScount--; *state = CLOSE_PAR; goto ret_null; } else if (!strncmp(token, "if", 2)) { push(IF); CScount++; command = makeCommand(command, NULL, IF_COMMAND, input, output); free(tokenPTR); command->u.command[0] = makeCommandStreamUtil(get_next_byte, get_next_byte_argument, state); while (*state != THEN) { if (!makeCommandStreamUtil(get_next_byte, get_next_byte_argument, state)) { type = NOT_DEFINED; break; } } if (type == NOT_DEFINED && *state != THEN) { return NULL; } command->u.command[1] = makeCommandStreamUtil(get_next_byte, get_next_byte_argument, state); if (*state != ELSE && *state != FI) { // HANDLE error; ; } else if (*state == ELSE || (*state == FI && CScount)) { command->u.command[2] = makeCommandStreamUtil(get_next_byte, get_next_byte_argument, state); } else { command->u.command[2] = NULL; } } else if (!strncmp(token, "while", 5)) { push(WHILE); (CScount)++; command = makeCommand(command, NULL, WHILE_COMMAND, input, output); free(tokenPTR); command->u.command[0] = makeCommandStreamUtil(get_next_byte, get_next_byte_argument, state); if (*state != DO) { // Handle Error ; } command->u.command[1] = makeCommandStreamUtil(get_next_byte, get_next_byte_argument, state); if (*state != DONE) { // HANDLE error; ; } else if (*state == DONE) { if (checkRedirection(get_next_byte, get_next_byte_argument)) { fillRedirectionOperands(&command->input, &command->output, get_next_byte, get_next_byte_argument); } command->u.command[2] = makeCommandStreamUtil(get_next_byte, get_next_byte_argument, state); /* if (command->u.command[2]) { */ /* command_t newCommand = makeCommand(NULL, NULL, SEQUENCE_COMMAND, */ /* NULL, NULL); */ /* newCommand->u.command[0] = command->u.command[1]; */ /* newCommand->u.command[1] = command->u.command[2]; */ /* command->u.command[1] = newCommand; */ /* command->u.command[2] = NULL; */ /* } */ } else { command->u.command[2] = NULL; } } else if (!strncmp(token, "until", 5)) { push(UNTIL); (CScount)++; command = makeCommand(command, NULL, UNTIL_COMMAND, input, output); free(tokenPTR); command->u.command[0] = makeCommandStreamUtil(get_next_byte, get_next_byte_argument, state); if (*state != DO) { // Handle Error ; } command->u.command[1] = makeCommandStreamUtil(get_next_byte, get_next_byte_argument, state); if (*state != DONE) { // HANDLE error; ; } else if (*state == DONE) { if (checkRedirection(get_next_byte, get_next_byte_argument)) { fillRedirectionOperands(&command->input, &command->output, get_next_byte, get_next_byte_argument); } command->u.command[2] = makeCommandStreamUtil(get_next_byte, get_next_byte_argument, state); /* if (command->u.command[2]) { */ /* command_t newCommand = makeCommand(NULL, NULL, SEQUENCE_COMMAND, */ /* NULL, NULL); */ /* newCommand->u.command[0] = command->u.command[1]; */ /* newCommand->u.command[1] = command->u.command[2]; */ /* command->u.command[1] = newCommand; */ /* command->u.command[2] = NULL; */ /* } */ } else { command->u.command[2] = NULL; } } else if (!strncmp(token, "(", 1)) { CScount++; command = makeCommand(command, NULL, SUBSHELL_COMMAND, input, output); free(tokenPTR); command->u.command[0] = makeCommandStreamUtil(get_next_byte, get_next_byte_argument, state); if (*state != CLOSE_PAR) { // Handle Error } else if (*state == CLOSE_PAR && CScount) { command->u.command[0] = makeCommandStreamUtil(get_next_byte, get_next_byte_argument, state); } } else { // SIMPLE_COMMAND while (1) { STATE prevState = *state; if (isKeyWordUpdate(token, state) && (prevState == COMMAND)) { removeWhiteSpace(token); command = makeSimpleCommand(command, tokenPTR, input, output); break; } if (type == REDIRECTION1 || type == REDIRECTION2) { type = fillRedirectionOperands(&input, &output, get_next_byte, get_next_byte_argument); // command = makeSimpleCommand(command, tokenPTR, input, output); // break; // type = readNextToken(tokenPTR, &len, get_next_byte, get_next_byte_argument); } else if (type == SPACE) { appendChar(token, ' '); type = readNextToken(tokenPTR, &len, get_next_byte, get_next_byte_argument); } else if (type == NEWLINE && !CScount) { command = makeSimpleCommand(command, tokenPTR, input, output); break; } else if (type == PIPE || type == SEMICOLON || type == NEWLINE) { removeWhiteSpace(token); if (((type == PIPE) || (type == SEMICOLON)) && !strlen(token)) { printErr(); } command = makeCommand(command, tokenPTR, type == PIPE ? PIPE_COMMAND : SEQUENCE_COMMAND, input, output); command->u.command[1] = makeCommandStreamUtil(get_next_byte, get_next_byte_argument, state); if (!command->u.command[1]) { command = convertToSimple(command); } break; } *state = COMMAND; } } return command; ret_null: free(command); free(tokenPTR); return NULL; }