AutonomousSequence::AutonomousSequence(){ startPosition = leftBack; puts("constructed without parameter"); DoSequence(); }
void DoSubshell(command_t command, int pipe_write, int pipe_read) { mode_t fd_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; // user: r/w, group: r, other: r pid_t pid; int status; // Return status of execute command int new_fd_input; // input file descriptor int new_fd_output; // output file descriptor int fd_output = -1; int fd_input = -1; int is_fd_output = 0; // flag of fd_output int is_fd_input = 0; // flag of fd_input //printf("In Subshell\n"); //printf("Type: %d\n", command->type); pid = fork(); if (pid == 0) { //printf("IN CHILD\n"); if (command->input != NULL) // I/O redirection has higher priority than pipe { fd_input = open(command->input, O_RDONLY, fd_mode); is_fd_input = 1; // flag of fd_input if (fd_input < 0) { switch (errno) { case ENOENT: case ENOTDIR: case EACCES: error(1, 0, "%s", strerror(errno)); exit(-1); break; default: error(1, 0, "some error happened in chdir"); exit(-1); } } new_fd_input = dup2(fd_input, STDIN_FILENO); //STDIN_FILENO = 0; if (new_fd_input != 0) { exit(-1); } close(fd_input); } else // No Input redirection, then check pipe input { if (pipe_read != -1) { new_fd_input = dup2(pipe_read, STDIN_FILENO); //STDIN_FILENO = 0; if (new_fd_input != 0) { exit(-1); } close(pipe_read); } } if (command->output != NULL) // I/O redirection has higher priority than pipe { fd_output = open(command->output, O_WRONLY|O_CREAT|O_TRUNC, fd_mode); // r/w, create if not exist, trunc to zero before write is_fd_output = 1; // flag of fd_output if (fd_output < 0) { switch (errno) { case ENOENT: case ENOTDIR: case EACCES: error(1, 0, "%s", strerror(errno)); exit(-1); break; default: error(1, 0, "some error happened in chdir"); exit(-1); } } new_fd_output = dup2(fd_output, STDOUT_FILENO); if (new_fd_output != 1) { exit(-1); } close(fd_output); } else // No output redirection, then check pipe output { if (pipe_write != -1) { new_fd_output = dup2(pipe_write, STDOUT_FILENO); if (new_fd_output != 1) { exit(-1); } close(pipe_write); } } DoSequence(command->u.subshell_command); exit(0); } else if (pid > 0) { //printf("IN FATHER\n"); if (pipe_read != -1) close(pipe_read); // Very important, must close the pipe fd! if (pipe_write != -1) close(pipe_write); if (is_fd_input) close(fd_input); if (is_fd_output) close(fd_output); wait(&status); command->status = status; //printf("OUT FATHER\n"); } else { error(1, 0, "%s", strerror(errno)); } }
AutonomousSequence::AutonomousSequence(StartPosition startPosition){ this->startPosition = startPosition; puts("constructed auto with config"); DoSequence(); }