#include "shell.h" #include "queue.h" #include "dynamic_array.h" #include "lexical_analysis.h" #include "file_suggestions.h" #include "readline.h" #include #include #include #include #include #include #include #include #include MAKE_QUEUE_INIT(w_queue) MAKE_QUEUE_INIT(p_queue) MAKE_QUEUE_PUSH(w_queue, word, char*) MAKE_QUEUE_PUSH(c_queue, cmdline, char**) MAKE_QUEUE_PUSH(p_queue, pid, int) enum modes { word_separation, whole_word }; static void show_invitation() { printf("> "); fflush(stdout); } /* remove background zombie processes */ static void handler(int signal) { int save_errno = errno; int pid; if(signal == SIGCHLD) { do { pid = wait4(-1, NULL, WNOHANG, NULL); } while(pid > 0); } errno = save_errno; } static void init_params(struct param_type *params, enum modes *current_mode) { params->is_word = 0; params->escape_sequences = 0; params->double_quotes_counter = 0; params->stored_symbol = ' '; params->empty_word_flag = 0; params->tokens = 0; params->wrong_command = 0; params->streams.input_stream = NULL; params->streams.output_stream = NULL; params->streams.output_stream_to_append = NULL; /* 0 - success, 1 - error */ params->last_execution_status = 0; params->pipeline = 0; params->new_readline = 1; *current_mode = word_separation; } static void clear_filename(struct param_type *params) { if(params->streams.input_stream) free(params->streams.input_stream); if(params->streams.output_stream) free(params->streams.output_stream); if(params->streams.output_stream_to_append) free(params->streams.output_stream_to_append); params->streams.input_stream = NULL; params->streams.output_stream = NULL; params->streams.output_stream_to_append = NULL; } static void reset_params(struct param_type *params, enum modes *current_mode, struct w_queue *word_chain, struct c_queue *cmdlines, struct dynamic_array *tmp_word, struct readline_type *readline) { w_queue_clear(word_chain); c_queue_clear(cmdlines); dynarr_reset_array(tmp_word); readline_reset_array(readline); clear_filename(params); init_params(params, current_mode); } static void add_letter(int ch, struct dynamic_array *tmp_word, struct param_type *params) { dynarr_push_back(tmp_word, ch); params->is_word = 1; params->escape_sequences = 0; } static void clean_input_buffer() { int ch; while((ch = getchar()) != new_line) {} } static void print_error_msg(const char *error_msg) { if(error_msg) fprintf(stderr, "%s\n", error_msg); } static const char* error_code_to_token(int error_code) { switch(error_code) { case err_filename_expected: return "expected file name"; case err_redirect_stream_again: return "stream redirected again"; case err_redirect_stream_in_pipeline: return "bad stream redirect in pipe"; case err_bg_process: return "extra characters after &"; case err_empty_command: return "empty command"; case err_extra_chars_after_filename: return "extra chars after filename"; case err_odd_double_quotes: return "odd number of double quotes"; } return NULL; } void error_identification(const struct param_type *params) { print_error_msg(error_code_to_token(params->wrong_command)); } static int special_token_handling(struct w_queue *word_chain, struct c_queue *cmdlines, struct dynamic_array *tmp_word, int ch, struct param_type *params, struct readline_type *readline) { return stream_redirect_tokens(word_chain, tmp_word, ch, params, readline) ? 1 : special_tokens(word_chain, cmdlines, tmp_word, ch, params, readline); } static void wait_for_process_to_complete(struct p_queue *pid_store) { int total_process_counter, current_process_counter, wait_pid; total_process_counter = p_queue_get_process_quantity(pid_store); current_process_counter = 0; do { wait_pid = wait(NULL); if(p_queue_find_pid(pid_store, wait_pid)) ++current_process_counter; } while(total_process_counter != current_process_counter); /* return of background process zombie cleanup */ signal(SIGCHLD, handler); } static void clean_up_memory(struct w_queue *word_chain, struct c_queue *cmdlines, struct param_type *params) { w_queue_clear(word_chain); c_queue_clear(cmdlines); clear_filename(params); } static void close_files(int input_fd, int output_fd) { if(input_fd) close(input_fd); if(output_fd) close(output_fd); } static void postprocessing(struct w_queue *word_chain, struct c_queue *cmdlines, struct p_queue *pid_store, struct param_type *params, int input_fd, int output_fd) { clean_up_memory(word_chain, cmdlines, params); close_files(input_fd, output_fd); if(params->tokens != '&') wait_for_process_to_complete(pid_store); p_queue_clear(pid_store); } static void set_signal_disposition(struct param_type *params) { if(params->tokens == '&') /* zombie process termination on signal */ signal(SIGCHLD, handler); else /* the parent process will wait for the process to complete; default signal disposition */ signal(SIGCHLD, SIG_DFL); } static void change_streams(int input_fd, int output_fd, int is_pipeline, int is_begin_pipeline) { if((!is_pipeline && input_fd) || (is_pipeline && is_begin_pipeline && input_fd)) { dup2(input_fd, 0); close(input_fd); } if((!is_pipeline && output_fd) || (is_pipeline && !is_begin_pipeline && output_fd)) { dup2(output_fd, 1); close(output_fd); } } int is_stream_redirection_set(const struct param_type *params) { return params->streams.input_stream || params->streams.output_stream || params->streams.output_stream_to_append; } static void make_pipeline(struct w_queue *word_chain, struct c_queue *cmdlines, struct param_type *params, int input_fd, int output_fd) { struct p_queue pid_store; p_queue_init(&pid_store); char **cmdline = NULL; int fd[2]; int save_read_fd, pid; pipe(fd); cmdline = c_queue_pop(cmdlines); set_signal_disposition(params); pid = fork(); if(pid == 0) { close(fd[0]); dup2(fd[1], 1); close(fd[1]); if(is_stream_redirection_set(params)) change_streams(input_fd, output_fd, 1, 1); execvp(cmdline[0], cmdline); perror(cmdline[0]); exit(1); } close(fd[1]); p_queue_push(&pid_store, pid); while(!c_queue_is_empty(cmdlines)) { cmdline = c_queue_pop(cmdlines); /* if not last process in pipeline*/ if(!c_queue_is_empty(cmdlines)) { save_read_fd = fd[0]; pipe(fd); } pid = fork(); if(pid == 0) { if(!c_queue_is_empty(cmdlines)) { dup2(save_read_fd, 0); close(save_read_fd); close(fd[0]); dup2(fd[1], 1); close(fd[1]); } else { dup2(fd[0], 0); close(fd[0]); } /* for last process in pipeline */ if(c_queue_is_empty(cmdlines)) if(is_stream_redirection_set(params)) change_streams(input_fd, output_fd, 1, 0); execvp(cmdline[0], cmdline); perror(cmdline[0]); exit(1); } if(!c_queue_is_empty(cmdlines)) { close(save_read_fd); close(fd[1]); } else close(fd[0]); p_queue_push(&pid_store, pid); } postprocessing(word_chain, cmdlines, &pid_store, params, input_fd, output_fd); } static void open_files(const struct param_type *params, int *input_fd, int *output_fd) { if(params->streams.input_stream) *input_fd = open(params->streams.input_stream, O_RDONLY); if(params->streams.output_stream) *output_fd = open(params->streams.output_stream, O_WRONLY | O_CREAT | O_TRUNC, 0666); if(params->streams.output_stream_to_append) *output_fd = open(params->streams.output_stream_to_append, O_WRONLY | O_APPEND, 0666); } static int is_cd_command(const char *arg) { return !strcmp(arg, "cd"); } static void change_directory(char **cmdline) { int result; char *first_arg = cmdline[1]; char *path = NULL; /* change to user home directory */ if(first_arg == NULL) { path = getenv("HOME"); if(!path) { perror("I don't know where's your home..."); return; } } else { if(cmdline[2]) { perror("cd: too many arguments"); return; } path = first_arg; } result = chdir(path); if(result == -1) perror(path); } static void run_external_program(struct w_queue *word_chain, struct c_queue *cmdlines, struct param_type *params) { int pid, input_fd, output_fd; struct p_queue pid_store; p_queue_init(&pid_store); char **cmdline = NULL; input_fd = 0; output_fd = 0; if(is_stream_redirection_set(params)) open_files(params, &input_fd, &output_fd); if(input_fd == -1 || output_fd == -1) { perror("can't open file"); return; } if(params->pipeline) { make_pipeline(word_chain, cmdlines, params, input_fd, output_fd); return; } cmdline = c_queue_pop(cmdlines); if(is_cd_command(cmdline[0])) change_directory(cmdline); else { set_signal_disposition(params); pid = fork(); if(pid == -1) { perror("fork error"); exit(1); } /* child process */ if(pid == 0) { if(is_stream_redirection_set(params)) change_streams(input_fd, output_fd, 0, 0); execvp(cmdline[0], cmdline); params->last_execution_status = 1; perror(cmdline[0]); exit(1); } /* parent process */ p_queue_push(&pid_store, pid); postprocessing(word_chain, cmdlines, &pid_store, params, input_fd, output_fd); } } int filename_waiting(struct param_type *params) { if(params->tokens == '<' || params->tokens == '>' || params->tokens == append) { if(!params->is_word) params->wrong_command = err_filename_expected; return 1; } else return 0; } void add_filename(struct dynamic_array *tmp_word, struct param_type *params) { dynarr_push_back(tmp_word, '\0'); switch(params->tokens) { case '<': params->streams.input_stream = malloc(tmp_word->last_element_index+1); dynarr_copy_array(tmp_word, params->streams.input_stream); break; case '>': params->streams.output_stream = malloc(tmp_word->last_element_index+1); dynarr_copy_array(tmp_word, params->streams.output_stream); break; case append: params->streams.output_stream_to_append = malloc(tmp_word->last_element_index+1); dynarr_copy_array(tmp_word, params->streams.output_stream_to_append); } dynarr_reset_array(tmp_word); params->is_word = 0; } void add_word(struct w_queue *word_chain, struct dynamic_array *tmp_word, struct param_type *params) { dynarr_push_back(tmp_word, 0); char *word = malloc(tmp_word->last_element_index+1); dynarr_copy_array(tmp_word, word); w_queue_push(word_chain, word); dynarr_reset_array(tmp_word); params->is_word = 0; } char** create_cmdline(const struct w_queue *word_chain, int word_counter) { char **cmdline = malloc((word_counter + 1) * sizeof(char*)); w_queue_copy_words_to_args(word_chain, cmdline); return cmdline; } static int readline_termination_condition(int ch, struct readline_type *readline) { return ch == new_line || (ch == end_of_file && readline->last_element_index == -1); } static void echo_characters(int ch) { write(1, &ch, 1); } static void generate_readline(struct readline_type *readline) { enum keys found_key = none; int match_result; char ch; while((ch = getchar()) && !readline_termination_condition(ch, readline)) { if(ch == tab) { if(suggestions_for_filename(readline)) { match_result = get_filename_match(readline, get_start_filename_idx(readline)); if(!match_result) echo_characters(new_line); show_invitation(); if(readline->last_element_index != -1) readline_print(readline); /* program name is not empty */ } else if(readline->last_element_index != -1) { match_result = get_program_name_match(readline); if(!match_result) echo_characters(new_line); show_invitation(); if(readline->last_element_index != -1) readline_print(readline); } continue; } if(ch == end_of_file) continue; /* moving */ found_key = readline_detect_arrow_keys(ch); if(found_key != none && found_key != unknown_key) { readline_move_along(readline, found_key); continue; } if(found_key == unknown_key) continue; /* deletion */ if(ch == backspace) { readline_character_deletion(readline); continue; } echo_characters(ch); readline_add_char(readline, ch); } /* moving cursor to end */ if(ch == new_line) readline->cursor_pos = readline->last_element_index + 1; /* adding '\n' to readline */ readline_add_char(readline, ch); /* print '\n' to end of readline */ if(ch == new_line) echo_characters(ch); } static void command_processing(struct param_type *params, enum modes *current_mode, struct w_queue *word_chain, struct c_queue *cmdlines, struct dynamic_array *tmp_word, struct readline_type *readline, int ch) { char **cmdline = NULL; int last_token = 0; /* odd number of double quotes */ if(!is_double_quotes_pair(*params)) { params->wrong_command = err_odd_double_quotes; goto clean; return; } if(filename_waiting(params)) { if(params->wrong_command) goto clean; if(params->pipeline) { if(params->tokens == '>' || params->tokens == append) add_filename(tmp_word, params); else if(params->tokens == '<') { params->wrong_command = err_redirect_stream_in_pipeline; goto clean; } } else add_filename(tmp_word, params); } else if(params->empty_word_flag || params->is_word) add_word(word_chain, tmp_word, params); if((params->tokens == and || params->tokens == or) && params->last_execution_status == 1) { /* ??? */ fprintf(stderr, "cannot be performed\n"); return; } if(word_chain->first == NULL) { params->wrong_command = err_empty_command; goto clean; } cmdline = create_cmdline(word_chain, w_queue_get_word_count(word_chain)); c_queue_push(cmdlines, cmdline); run_external_program(word_chain, cmdlines, params); clean: error_identification(params); last_token = params->tokens; reset_params(params, current_mode, word_chain, cmdlines, tmp_word, readline); if(last_token == '&' || last_token == 0 || ch == new_line) { show_invitation(); generate_readline(readline); } } static void word_separation_processing(int ch, struct param_type *params, enum modes *current_mode, struct w_queue *word_chain, struct c_queue *cmdlines, struct dynamic_array *tmp_word, struct readline_type *readline) { /* could be a marker for the beginning of a blank word */ params->stored_symbol = ch; if(is_empty_word(ch, *params)) { dynarr_push_back(tmp_word, '\0'); add_word(word_chain, tmp_word, params); params->empty_word_flag = 0; return; } params->empty_word_flag = 0; if(change_mode(ch, *params)) { ++params->double_quotes_counter; *current_mode = whole_word; return; } if(start_escape_sequence(ch, *params)) { params->escape_sequences = 1; return; } if(special_token_handling(word_chain, cmdlines, tmp_word, ch, params, readline)) { if(command_execution_condition(params)) command_processing(params, current_mode, word_chain, cmdlines, tmp_word, readline, ch); return; } if(params->wrong_command) { error_identification(params); clean_input_buffer(); reset_params(params, current_mode, word_chain, cmdlines, tmp_word, readline); show_invitation(); return; } if(check_separation(ch, *params)) { if(excessive_words(ch, params)) { error_identification(params); clean_input_buffer(); reset_params(params, current_mode, word_chain, cmdlines, tmp_word, readline); show_invitation(); return; } if(!filename_waiting(params)) { add_word(word_chain, tmp_word, params); params->is_word = 0; } return; } if(ignore_spaces(ch, *params)) return; add_letter(ch, tmp_word, params); } static void whole_word_processing(int ch, struct param_type *params, enum modes *current_mode, struct w_queue *word_chain, struct dynamic_array *tmp_word) { if(double_quotes_again(ch, *params)) { params->stored_symbol = '\0'; params->empty_word_flag = 1; ++params->double_quotes_counter; *current_mode = word_separation; return; } params->stored_symbol = '\0'; if(change_mode(ch, *params)) { ++params->double_quotes_counter; *current_mode = word_separation; return; } if(escape_double_quotes_or_backslash(ch, *params)) { dynarr_push_back(tmp_word, ch); params->escape_sequences = 0; params->is_word = 1; return; } if(start_escape_sequence(ch, *params)) { params->escape_sequences = 1; return; } /* backslash recovery */ if(params->escape_sequences) dynarr_push_back(tmp_word, backslash); add_letter(ch, tmp_word, params); } static void change_terminal_settings(struct termios *cur_terminal_settings, struct termios *save_terminal_settings) { tcgetattr(0, save_terminal_settings); memcpy(cur_terminal_settings, save_terminal_settings, sizeof(*save_terminal_settings)); cur_terminal_settings->c_lflag &= ~(ICANON | ECHO); tcsetattr(0, TCSANOW, cur_terminal_settings); } static void restore_terminal_settings(struct termios *cur_terminal_settings, struct termios *save_terminal_settings) { tcsetattr(0, TCSANOW, save_terminal_settings); } int main() { char ch; int i; struct param_type params; struct termios cur_terminal_settings, save_terminal_settings; struct w_queue word_chain; struct c_queue cmdlines; struct dynamic_array tmp_word; struct readline_type readline; enum modes current_mode = word_separation; change_terminal_settings(&cur_terminal_settings, &save_terminal_settings); w_queue_init(&word_chain); c_queue_init(&cmdlines); dynarr_create_array(&tmp_word); readline_create_array(&readline); init_params(¶ms, ¤t_mode); show_invitation(); generate_readline(&readline); for(i = 0; readline.arr[i] != end_of_file; ) { ch = readline.arr[i]; readline.considered_index = i; if(params.new_readline) params.new_readline = 0; if(ch == new_line) command_processing(¶ms, ¤t_mode, &word_chain, &cmdlines, &tmp_word, &readline, ch); else if(current_mode == word_separation) { word_separation_processing(ch, ¶ms, ¤t_mode, &word_chain, &cmdlines, &tmp_word, &readline); /* double token was found */ if(readline.considered_index != i) ++i; } else if(current_mode == whole_word) whole_word_processing(ch, ¶ms, ¤t_mode, &word_chain, &tmp_word); i = params.new_readline ? 0 : i + 1; } restore_terminal_settings(&cur_terminal_settings, &save_terminal_settings); putchar(new_line); dynarr_clear(&tmp_word); readline_clear(&readline); return 0; }