void execute(cmdList list, cmdLine *pCmdLine){ int **pipes; int *currPipe; cmdLine *cmdline = pCmdLine; int i = 0; int count = countCmds(cmdline); pipes = createPipes(count -1); while(i<count){ pid_t pid = fork(); if(pid == -1 ){ perror("fork() "); return; } if(pid == 0 ){ if(count>1){ if(cmdline->idx != 0){ currPipe = leftPipe(pipes,cmdline); close(0); dup2(currPipe[0],0); close(currPipe[0]); } if(cmdline->next != NULL){ currPipe = rightPipe(pipes,cmdline); close(1); dup2(currPipe[1],1); close(currPipe[1]); } } if(cmdline->inputRedirect != NULL){ close(0); fopen(cmdline->inputRedirect, "r"); } if(cmdline->outputRedirect != NULL){ close(1); fopen(cmdline->outputRedirect, "w"); } execvp(cmdline->arguments[0], cmdline->arguments); perror("Error: "); freeCmdLines(pCmdLine); freeCmdList(list); releasePipes(pipes,count - 1); _exit(errno); } else{ currPipe = leftPipe(pipes,cmdline); if(currPipe != NULL) close(currPipe[0]); currPipe = rightPipe(pipes,cmdline); if(currPipe != NULL) close(currPipe[1]); if((cmdline->blocking)==1){ int status; waitpid(pid,&status,0); } } cmdline=cmdline->next; i++; } releasePipes(pipes,count-1); }
/*This function execute the parsed line with the execv system_call */ void execute(cmdLine *pCmdLine) { /*executing the program*/ if(execvp(pCmdLine->arguments[0],pCmdLine->arguments) == -1) perror(""); /*Releasing allocated memory*/ freeCmdLines(pCmdLine); }
int main(int argc, char **argv){ cmdList list = make_cmdList(); while(1){ char buf[PATH_MAX]; char s[linelen]; getcwd(buf,PATH_MAX); printf("current path: %s >\n",buf); fgets(s,linelen,stdin); cmdLine *cmdline; cmdline = parseCmdLines(s); if((cmdline)!= NULL){ /*testFunc(cmdline);*/ if(strcmp(cmdline->arguments[0],"quit")==0){ freeCmdLines(cmdline); break; } if (strcmp(cmdline->arguments[0],"cd")==0){ changeDir(cmdline); } else { if (s[0]=='!'){ invoke(list,s); } else{ add_cmdLine(list, s); if(strcmp(cmdline->arguments[0],"history") == 0){ history(list); } else { execute(list,cmdline); } } } } freeCmdLines(cmdline); } freeCmdList(list); return 0; }
void invoke(cmdList list,char *s){ int index =atoi(s+1); if(list->size <= index) printf("Error: no such index in history!\n"); else{ cmdLine *redo; redo = parseCmdLines(list->cmds[index]); add_cmdLine(list,list->cmds[index]); if(strcmp(redo->arguments[0],"history") == 0) history(list); else execute(list,redo); freeCmdLines(redo); } }
void freeCmdLines(cmdLine *pCmdLine) { int i; if (!pCmdLine) return; FREE(pCmdLine->inputRedirect); FREE(pCmdLine->outputRedirect); for (i=0; i<pCmdLine->argCount; ++i) FREE(pCmdLine->arguments[i]); if (pCmdLine->next) freeCmdLines(pCmdLine->next); FREE(pCmdLine); }
int main (int argc , char* argv[]){ char dir[PATH_MAX]; char BUFFER[LINE_LENGTH]; int commands_counter = 0; linked_list *head = 0; var_list *var_head = 0; cmdLine *command; while(1){ getcwd(dir, PATH_MAX); printf("The current dir is: %s \n", dir); fgets(BUFFER, LINE_LENGTH, stdin); command = parseCmdLines(BUFFER); if(strncmp(command->arguments[0], "!", 1) == 0){ int place = atoi(&command->arguments[0][1]); if(place < 0 || place > commands_counter){ perror("Error, out of bounds"); }else{ freeCmdLines(command); char *repeat = getCommand(head, place); command = parseCmdLines(repeat); strcpy(BUFFER, repeat); } } int i; for(i = 0; i<command->argCount; i++){ if(strncmp(command->arguments[i], "$", 1) == 0){ char *change = &command->arguments[i][1]; char *fut_change = getArgu(var_head, change); replaceCmdArg(command, i, fut_change); } } if(strcmp(command->arguments[0], "set") == 0){ var_list *newVar = malloc(sizeof(var_list)); char *str1 = calloc(strlen(command->arguments[1])+1, sizeof(char)); char *str2 = calloc(strlen(command->arguments[2])+1, sizeof(char)); strcpy(str1, command->arguments[1]); strcpy(str2, command->arguments[2]); newVar->name = str1; newVar->value = str2; newVar->next = 0; var_head = setValue(var_head, newVar); } else if(strcmp(command->arguments[0], "delete") == 0){ var_head = delete_var(var_head, command->arguments[1]); } else if((choose_action(command, head, var_head)) == 1){ freeCmdLines(command); break; } linked_list *newNode = malloc(sizeof(linked_list)); char *str = calloc(strlen(BUFFER)+1, sizeof(char)); strcpy(str, BUFFER); newNode->command = str; newNode->next = 0; head = list_append(head, newNode); freeCmdLines(command); commands_counter++; } list_free(head); var_free(var_head); return 0; }
List* execString(cmdLog* cLog, List* env, char* incoming) { cmdLine* line; cmdLine* firstLine; cmdLine* currLine; int pipeCounter = 0; int * pid; int pid2; int i; int j; int **pipefd; line = parseCmdLines(incoming); currLine = line; firstLine = line; if (line==NULL || !strcmp(line->arguments[0], "quit")) { freeList(env); freeCmdLines(line); return (List*)-1; } line= cmdEnvReplace(env, line); if (!strcmp(line->arguments[0], "assign")) { env= cmdAssign(env, line); } else if (!strcmp(line->arguments[0], "unassign")) { env= cmdUnassign(env, line); } else if (!strcmp(line->arguments[0], "cd")) { cmdCD(cLog, line, incoming); } else if (line->arguments[0][0]=='!') { env= cmdReadLog(cLog, env, line); } else { j = 0; pushArray(cLog, incoming); if(line->next) { while(currLine != NULL) { currLine = currLine->next; pipeCounter++; } pipefd= createPipes(pipeCounter-1); pid = (int *)malloc(pipeCounter * sizeof(int)); if (!(pid[j] =fork())) { /* first son*/ close(STDOUT_FILENO); /* Close unused write end */ dup(rightPipe(pipefd, line)[1]); close(rightPipe(pipefd, line)[1]); coreExec(cLog, env, line); } close(rightPipe(pipefd, line)[1]); j++; line = line->next; for (i=0; i<pipeCounter-2; i++) { if (!(pid[j] =fork())) { /* first son*/ close(STDOUT_FILENO); /* Close unused write end */ dup(rightPipe(pipefd, line)[1]); close(rightPipe(pipefd, line)[1]); close(STDIN_FILENO); dup(leftPipe(pipefd, line)[0]); close(leftPipe(pipefd, line)[0]); coreExec(cLog, env, line); } close(leftPipe(pipefd, line)[0]); close(rightPipe(pipefd, line)[1]); j++; line = line->next; } if (!(pid[j] =fork())) { /* first son*/ close(STDIN_FILENO); /* Close unused write end */ dup(leftPipe(pipefd, line)[0]); close(leftPipe(pipefd, line)[0]); coreExec(cLog, env, line); } close(leftPipe(pipefd, line)[0]); for(i=0; i<=j; i++) { if(line->blocking!=0) { waitpid(pid[i],0,0); } } releasePipes(pipefd, pipeCounter-1); } else { if (!(pid2=fork())) { coreExec(cLog, env, line); } } if(line->blocking!=0) { waitpid(pid2,0,0); } } freeCmdLines(firstLine); free(pid); return env; }