/* * Function: exec_command() * ------------------------ * Checks the command string for pipes and builtin commands. * * @param command_str; All commands given as string. * @returns; void. */ void exec_command(char* command_str) { funcp_t builtin_func; /* Split the commands on pipes */ char** commands = split_command(command_str, "|"); /* Split the first command to check for builtins */ char** command_args = split_command(commands[0], " "); builtin_func = get_builtin_command(command_args[0]); /* If builtin the execute, else goto pipeline execution. */ if (builtin_func) { (*builtin_func)(command_args); } else { exec_pipeline(commands, STDIN_FILENO); } /* Free all splitted commands */ unsigned int i = 0; while (commands[i]) free(commands[i++]); i = 0; while (command_args[i]) free(command_args[i++]); free(commands); }
static void exec_arguments(int argc, char **argv) { /* Split the command line into sequences of arguments */ /* Break at pipe symbols as arguments on their own */ char **cmdv[argc / 2]; // Way too many char *args[argc + 1]; int cmdn = 0; int argn = 0; cmdv[cmdn++] = &args[argn]; for (int i = 1; i < argc; i++) { char *arg = argv[i]; if (strcmp(arg, "|") == 0) { if (i == 1) err_sysexit("Syntax error: pipe before any command"); if (args[argn - 1] == 0) err_sysexit("Syntax error: two pipes with no command between"); arg = 0; } args[argn++] = arg; if (arg == 0) cmdv[cmdn++] = &args[argn]; } if (args[argn - 1] == 0) err_sysexit("Syntax error: pipe with no command following"); args[argn] = 0; exec_pipeline(cmdn, cmdv); }
int exec_line(t_param *param, char *line) { t_pipeline pipeline; pipeline.line = line; pipeline.nb_pipe = get_nb_pipe(pipeline.line); if (!verify(&pipeline)) return (-42); if ((param->tty) && (param->debug)) my_fprintf(2, "%[6]Exec line `%S' (%d pipes)%[]\n", pipeline.line, pipeline.nb_pipe); pipeline.input = extract_input(pipeline.line); pipeline.output = extract_output(pipeline.line); if (verify_pipe_redirless(pipeline.line) == -1) return (-42); if ((param->tty) && (param->debug)) { if (pipeline.input.type != NONE) my_fprintf(2, "%[6]Input file : %S%[]\n", pipeline.input.file); if (pipeline.output.type != NONE) my_fprintf(2, "%[6]Output file : %S%[]\n", pipeline.output.file); } exec_pipeline(param, &pipeline); return (pipeline.status); }
/* * Function: exec_pipeline() * ------------------------ * Execute the commands given in a pipeline, where the given * file descriptor is the write side of the pipe precessor. * * @param commands; All commands due for executing in the pipeline * @param in_fd; file descriptor to read from. * @returns; void. */ void exec_pipeline(char** commands, int in_fd) { char** args = split_command(commands[0], " "); pid_t child; int fd[2]; pipe(fd); switch (child = fork()) { /* Something wrong */ case -1: perror("Fork:"); exit(EXIT_FAILURE); /* Child */ case 0: /* Reset to the default signal handler to kill processes */ signal(SIGINT, SIG_DFL); if (commands[1] != NULL) { /* Set the OUTPUT of child to WRITE f_desc. */ dup2(fd[1], STDOUT_FILENO); } /* Set the INPUT of child to the given output of the previes call. */ dup2(in_fd, STDIN_FILENO); /* Disobey parent. (stop reading from shell). */ close(fd[0]); execvp(args[0], args); perror("Error:"); _exit(EXIT_FAILURE); /* Parent */ default: /* Parent cannot write to child. */ close(fd[1]); /* If there are more commands, put them in pipeline. */ if (commands[1] != NULL) { exec_pipeline(++commands, fd[0]); } /* Else wait for the last child from the pipeline to finish. */ else { while (wait(NULL) != child); } } /* Free the splitted string */ unsigned int i = 0; while (args[i]) free(args[i++]); free(args); }
int main(int argc, char **argv) { err_setarg0(argv[0]); sigchld_status(); if (argc == 1) { /* Run the built in pipe-line */ exec_pipeline(ncmds, cmds); } else { /* Run command line specified by user */ exec_arguments(argc, argv); } corpse_collector(); return(0); }
int main(int argc, char **argv) { int opt; char *argv0 = argv[0]; setvbuf(stderr, 0, _IOLBF, BUFSIZ); err_setarg0(argv[0]); sigchld_status(); while ((opt = getopt(argc, argv, "v")) != -1) { switch (opt) { case 'v': vflag = 1; break; default: err_usage("[-v] [cmd1 | cmd2 ...]"); break; } } argv += optind - 1; argc -= optind - 1; argv[0] = argv0; fd_info(0); if (argc == 1) { /* Run the built in pipe-line */ exec_pipeline(ncmds, cmds); } else { /* Run command line specified by user */ exec_arguments(argc, argv); } corpse_collector(); return(0); }
static void execute(struct s_command *command, char *line) { parse(command, line); exec_pipeline(command); }