#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define MAX_JOBS 32 #define MAX_ARGS 1024 #define DEBUG_ENABLE false #define DEBUG(format, args...) \ if (DEBUG_ENABLE) \ printf(format, ##args) #include typedef struct { char* command; pid_t group_id; pid_t pipe_id; bool running; int8_t number; int8_t next; int8_t prev; bool occupied; bool main_exited; bool do_pipe; bool pipe_exited; } Job; static volatile Job jobs[MAX_JOBS]; static sigjmp_buf jump_buf; static volatile sig_atomic_t first_job = -1; static volatile sig_atomic_t recent_job = -1; static volatile sig_atomic_t foreground_job = -1; static void push_job(int j) { if (recent_job == -1) { first_job = j; } else { jobs[recent_job].next = (int8_t)j; } jobs[j].prev = (int8_t)recent_job; jobs[j].next = -1; recent_job = j; } static void remove_job(int j) { if (jobs[j].prev != -1) { jobs[jobs[j].prev].next = jobs[j].next; } else { assert(first_job == j); first_job = jobs[j].next; } if (jobs[j].next != -1) { jobs[jobs[j].next].prev = jobs[j].prev; } else { assert(recent_job == j); recent_job = jobs[j].prev; } } static void handle_finished_jobs() { DEBUG("going through finished stuff\n"); for (int i = 0; i < MAX_JOBS; ++i) { if (jobs[i].occupied && jobs[i].main_exited && jobs[i].pipe_exited) { printf("[%d]+ %-20s %s &\n", jobs[i].number, "Done", jobs[i].command); free(jobs[i].command); jobs[i].occupied = false; } } } static int max(int a, int b) { return a > b ? a : b; } static char* next_token(char** line_ptr) { char* line = *line_ptr; while (*line && isspace(*line)) { ++line; } if (!*line) { return NULL; } char* start = line; char c = *line; if (c == '|' || c == '<' || c == '>' || c == '&') { *line_ptr = line + 1; return strndup(start, 1); } else if (c == '2' && *(line + 1) == '>') { *line_ptr = line + 2; return strndup(start, 2); } do { c = *++line; } while (c && !isspace(c) && c != '|' && c != '<' && c != '>' && c != '&'); *line_ptr = line; return strndup(start, (size_t)(line - start)); } static char* main_args[MAX_ARGS]; static char* pipe_args[MAX_ARGS]; static int main_redir_in, main_redir_out, main_redir_err; static int pipe_redir_in, pipe_redir_out, pipe_redir_err; static pid_t main_pid; static pid_t pipe_pid; static bool launch_bg; static bool do_pipe; static bool parse_line(char* line, bool after_pipe) { do_pipe = false; launch_bg = false; size_t arg_num = 0; int redir_in = -1, redir_out = -1, redir_err = -1; char** args = after_pipe ? pipe_args : main_args; for (int i = 0; args[i]; ++i) { free(args[i]); } char* current; while ((current = next_token(&line))) { if (!strcmp("<", current)) { if (!(current = next_token(&line))) { DEBUG("No file passed for redirection\n"); return false; } if ((redir_in = open(current, O_RDONLY)) == -1) { printf("yash: %s: No such file or directory\n", current); return false; } } else if (!strcmp(">", current)) { if (!(current = next_token(&line))) { DEBUG("No file passed for redirection\n"); } (redir_out = open(current, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH)); } else if (!strcmp("2>", current)) { if (!(current = next_token(&line))) { DEBUG("No file passed for redirection\n"); return false; } redir_err = open(current, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH); } else if (!strcmp("|", current)) { if (!parse_line(line, true)) { DEBUG("Error parsing pipe command\n"); } do_pipe = true; break; } else if (!strcmp("&", current)) { launch_bg = true; break; } else { args[arg_num++] = current; } } if (after_pipe) { pipe_redir_err = redir_err; pipe_redir_in = redir_in; pipe_redir_out = redir_out; } else { main_redir_err = redir_err; main_redir_in = redir_in; main_redir_out = redir_out; } args[arg_num] = 0; return true; } static void line(char* orig) { char* line = orig; if (!line) { for (int i = first_job; i != -1; i = jobs[i].next) { if (jobs[i].occupied) { kill(-jobs[i].group_id, SIGKILL); } } exit(0); } add_history(line); char* command = next_token(&line); if (!command) { return; } else if (!strcmp("fg", command)) { if (recent_job != -1) { foreground_job = recent_job; puts(jobs[recent_job].command); kill(-jobs[recent_job].group_id, SIGCONT); remove_job(recent_job); } return; } else if (!strcmp("bg", command)) { int i = recent_job; while (i != -1 && jobs[i].running) { i = jobs[i].prev; } if (i != -1) { jobs[i].running = true; printf("[%d]+ %s &\n", jobs[i].number, jobs[i].command); DEBUG("Sending SIGCONT to %d\n", jobs[i].group_id); kill(-jobs[i].group_id, SIGCONT); } return; } else if (!strcmp("jobs", command)) { int i = first_job; while (i != -1) { printf("[%d]%c %-20s %s%s\n", jobs[i].number, i == recent_job ? '+' : '-', jobs[i].running ? "Running" : "Stopped", jobs[i].command, jobs[i].running ? " &" : ""); i = jobs[i].next; } return; } if (!parse_line(orig, false)) { return; } if (launch_bg) { // strip & and resulting trailing whitespace char* end = strchr(orig, '&'); if (end) { --end; while (isspace(*end)) { --end; } *++end = (char)'\0'; } } int pipe_ends[2]; if (do_pipe) { pipe(pipe_ends); } if ((main_pid = fork())) { if (do_pipe) { if (!(pipe_pid = fork())) { setpgid(0, main_pid); signal(SIGINT, SIG_DFL); signal(SIGTSTP, SIG_DFL); signal(SIGCHLD, SIG_DFL); dup2(pipe_ends[0], 0); close(pipe_ends[1]); if (pipe_redir_in != -1) { dup2(pipe_redir_in, 0); } if (pipe_redir_out != -1) { dup2(pipe_redir_out, 1); } if (pipe_redir_err != -1) { dup2(pipe_redir_err, 2); } if (execvp(pipe_args[0], pipe_args) == -1) { DEBUG("Error calling exec for pipe\n"); exit(1); } } close(pipe_ends[0]); close(pipe_ends[1]); } } else { setpgid(0, 0); signal(SIGINT, SIG_DFL); signal(SIGTSTP, SIG_DFL); signal(SIGCHLD, SIG_DFL); if (do_pipe) { dup2(pipe_ends[1], 1); close(pipe_ends[0]); } if (main_redir_in != -1) { dup2(main_redir_in, 0); } if (main_redir_out != -1) { dup2(main_redir_out, 1); } if (main_redir_err != -1) { dup2(main_redir_err, 2); } if (execvp(main_args[0], main_args) == -1) { DEBUG("Error calling exec\n"); exit(1); } } int i; for (i = 0; i < MAX_JOBS; ++i) { if (!jobs[i].occupied) { break; } } jobs[i].occupied = true; jobs[i].pipe_id = do_pipe ? pipe_pid : -1; jobs[i].group_id = main_pid; jobs[i].command = strdup(orig); jobs[i].running = true; jobs[i].running = true; jobs[i].main_exited = false; jobs[i].pipe_exited = !do_pipe; int8_t biggest = 0; int temp = first_job; while (temp != -1) { biggest = (int8_t)max(jobs[temp].number, biggest); temp = jobs[temp].next; } jobs[i].number = biggest + 1; if (launch_bg) { push_job(i); } else { foreground_job = i; } DEBUG("Launching %d (%d) as job #%d\n", main_pid, pipe_pid, jobs[i].number); } static void handle_sigint(int sig) { assert(sig == SIGINT); if (foreground_job != -1) { kill(-jobs[foreground_job].group_id, SIGINT); jobs[foreground_job].occupied = false; foreground_job = -1; DEBUG("\nexited\n"); } else { siglongjmp(jump_buf, 0); } } static void handle_sigtstp(int sig) { assert(sig == SIGTSTP); if (foreground_job != -1) { kill(-jobs[foreground_job].group_id, SIGTSTP); jobs[foreground_job].running = false; push_job(foreground_job); foreground_job = -1; return; } DEBUG("Attempted to stop shell process\n"); } static void handle_sigchld(int sig) { assert(sig == SIGCHLD); pid_t pid; int status; while ((pid = waitpid(-1, &status, WNOHANG | WUNTRACED)) != -1) { if (pid == 0) { return; } if (WIFEXITED(status) || WIFSIGNALED(status)) { DEBUG("Exited: %d(%d) %d(%d)\n", WIFEXITED(status), WEXITSTATUS(status), WIFSIGNALED(status), WTERMSIG(status)); if (jobs[foreground_job].group_id == pid) { jobs[foreground_job].main_exited = true; if (jobs[foreground_job].pipe_exited) { jobs[foreground_job].occupied = false; DEBUG("Removing foreground: %s\n", jobs[foreground_job].command); free(jobs[foreground_job].command); foreground_job = -1; } } else if (jobs[foreground_job].pipe_id == pid) { jobs[foreground_job].pipe_exited = true; if (jobs[foreground_job].main_exited) { jobs[foreground_job].occupied = false; DEBUG("Removing foreground: %s\n", jobs[foreground_job].command); free(jobs[foreground_job].command); foreground_job = -1; } } else { for (int i = first_job; i != -1; i = jobs[i].next) { if (jobs[i].group_id == pid) { jobs[i].main_exited = true; if (jobs[i].pipe_exited) { DEBUG("Removing %d\n", jobs[i].number); remove_job(i); } } else if (!jobs[i].pipe_exited && jobs[i].pipe_id == pid) { jobs[i].pipe_exited = true; if (jobs[i].main_exited) { DEBUG("Removing %d\n", jobs[i].number); remove_job(i); } } } } } else if (WIFSTOPPED(status)) { DEBUG("stopped\n"); } else { DEBUG("ERROR unexpected wait status\n"); } DEBUG("PID: %d, status: %d\n", pid, status); } } int main() { signal(SIGINT, handle_sigint); signal(SIGTSTP, handle_sigtstp); signal(SIGCHLD, handle_sigchld); for (int i = 0; i < MAX_JOBS; ++i) { jobs[i].occupied = false; } while (true) { // SIGINT during readline prints a new prompt while (sigsetjmp(jump_buf, true)) { putchar('\n'); } char* raw_input = readline("# "); handle_finished_jobs(); line(raw_input); free(raw_input); while (foreground_job != -1) { DEBUG("pausing...\n"); pause(); } } }