/* Arguements: -(struct node*): The List of Commands Returns (void) */ void run_commands(struct node* commands) { while (commands) { char **array = 0; struct node* list = 0; // Redirect append if ( strstr( commands->data, ">>" ) ) { array = split_on_append(commands->data); redirect_append(array); } // Redirect stdout else if ( strstr( commands->data, ">" ) ) { array = split_on_stdout(commands->data); redirect_stdout(array); } // Redirect stdin else if ( strstr( commands->data, "<" ) ) { array = split_on_stdin(commands->data); redirect_stdin(array); } // Pipe else if ( strstr( commands->data, "|" ) ) { list = split_on_pipe(commands->data); redirect_pipe(list); } // Normal Execution else { array = arguementify(commands->data); execute_command(array); } // Cleanup free_list(list); free_string_array(array); // Continue commands = commands->next; } }
void main(int argc, char* argv[]) { if(argc > 2) { fprint(2, "usage: %s [file]\n", argv[0]); exits("usage"); } // Run script from file given as parameter instead of stdin if necesary if(argc == 2) redirect_stdin(argv[1]); // Allocate buffer for standard input Biobuf bin; if(Binit(&bin, 0, OREAD)) sysfatal("%s: standard input: %r", argv0); tHeredoc heredoc; heredoc.mode = 0; // Main loop char* line; while((line = Brdstr(&bin, '\n', 0))) { // Process heredoc line = heredoc_process(&heredoc, line); if(line) { // Process script script_process(line); // Free procesed line free(line); } } // Free stdin buffer Bterm(&bin); // Exit sucesfully exits(nil); }
void redirect_m(int numb_of_params, char* params[]) { char * commands[COMMAND_COUNT]; PARAMS=params; N_params=numb_of_params; int pars_res = parse(numb_of_params, params, commands); if (pars_res != 0) { printf("error parsing 'redirect' comand.\n"); return; } // --------------------------------------------------------- if (strcmp(commands[1], ">") == 0 || strcmp(commands[1], ">>") == 0) { redirect_stdout(commands[0], commands[1], commands[2]); } else { if (strcmp(commands[1], "<") == 0 && strcmp(commands[3], "\0") == 0) { redirect_stdin(commands[0], commands[1], commands[2]); } else if (strcmp(commands[1], "<") == 0 && (strcmp(commands[3], ">") == 0 || strcmp(commands[3], ">>") == 0)) { redirect_stdin_stdout(commands[0], commands[1], commands[2], commands[3], commands[4]); } } }
int background(char* line) // Check and exec on background all the commands ended with '&' on the command // line and return the last, foreground command { // Split pipeline in independent commands char* array[10]; int numCommands = gettokens(line, array, 10, "&"); // run script commands int i; for(i = 0; i < numCommands-1; ++i) { switch(fork()) { case -1: return -1; case 0: // child { // redirect stdin to /dev/null redirect_stdin("/dev/null"); // process pipeline pipeline_process(array[i]); exitError(); } // parent loop to exec in background the next command (if any) } } // collapse line to let only the last (not background) command on it int len = strlen(array[numCommands-1]); memmove(line, array[numCommands-1], len); *(line+len) = '\0'; return 0; }
bool redirect() { return redirect_stdout(out_pipe_) && redirect_stdin(in_pipe_); }
//! Called in the child process. Returns -1 on error, and errno is set. //! This doesn't throw because the caller needs to work out what exception //! to throw. bool redirect() { return redirect_stdin(pipe_) != -1; }