#include "queue.h" #include "dynamic_array.h" #include #include #include #include #include #include #include #include enum modes { word_separation, whole_word }; enum { new_line = 10, whitespace = ' ', tab = 9, backslash = '\\', double_quotes = '"' }; /* two-letter tokens */ enum { append = '>' + 1, and = '&' + 1, or = '|' + 1 }; /* storing file names to redirect standard input/output streams */ struct io_type { char *input_stream; char *output_stream; char *output_stream_to_append; }; struct param_type { int is_word; int escape_sequences; unsigned int double_quotes_counter; char stored_symbol; int empty_word_flag; int tokens; int wrong_command; struct io_type streams; int last_execution_status; }; static void show_invitation() { printf("> "); } static void print_error() { fprintf(stderr, "Error: unmatched quotes\n"); } /* 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; *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 queue *word_chain, struct dynamic_array *tmp_word) { queue_clear(word_chain); dynarr_drop_word(tmp_word); clear_filename(params); init_params(params, current_mode); } static int is_double_quotes_pair(struct param_type params) { return !(params.double_quotes_counter % 2); } static int check_separation(int ch, struct param_type params) { return (ch == whitespace || ch == tab) && params.is_word && !params.escape_sequences; } static 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 int filename_waiting(const struct param_type *params) { return params->tokens == '<' || params->tokens == '>' || params->tokens == append; } static 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_drop_word(tmp_word); params->is_word = 0; } static void add_word(struct 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); queue_push(word_chain, word); dynarr_drop_word(tmp_word); params->is_word = 0; } static int is_double_token(struct param_type *params) { return params->tokens == and || params->tokens == or || params->tokens == append; } static int is_redirect_token(int ch, int next_ch) { return ch == '<' || ch == '>' || (ch == '>' && next_ch == '>'); } static int validate_redirections(int ch, int next_ch, struct param_type *params) { return (ch == '<' && params->streams.input_stream == NULL) || ((ch == '>' || (ch == '>' && next_ch == '>')) && params->streams.output_stream == NULL && params->streams.output_stream_to_append == NULL); } static int stream_redirect_tokens(struct queue *word_chain, struct dynamic_array *tmp_word, int ch, struct param_type *params) { int next_ch; next_ch = getchar(); ungetc(next_ch, stdin); if(is_redirect_token(ch, next_ch)) { /* filenames */ if(filename_waiting(params)) if(params->is_word) add_filename(tmp_word, params); else { fprintf(stderr, "syntax error\n"); params->wrong_command = 1; return 0; } /* execute command */ else if(params->is_word) { add_word(word_chain, tmp_word, params); params->is_word = 0; } if(validate_redirections(ch, next_ch, params)) { params->tokens = (ch == '>' && next_ch == '>') ? append : ch; if(is_double_token(params)) getchar(); return 1; } else { fprintf(stderr, "syntax error\n"); params->wrong_command = 1; } } return 0; } static void clean_input_buffer() { int ch; while((ch = getchar()) != new_line) {} } static int is_special_token(int ch) { return ch == and || ch == or || ch == '&' || ch == ';' || ch == '|'; } static int special_tokens_allowed(int ch, struct param_type *params) { return (!filename_waiting(params) || (filename_waiting(params) && params->is_word)); } static int special_tokens(struct dynamic_array *tmp_word, int ch, struct param_type *params) { int next_ch; next_ch = getchar(); ungetc(next_ch, stdin); if(is_special_token(ch)) { if(special_tokens_allowed(ch, params)) { if(filename_waiting(params) && params->is_word) { add_filename(tmp_word, params); params->is_word = 0; } if(ch == '|') params->tokens = '|'; else if(ch == '|' && next_ch == '|') params->tokens = or; else if(ch == '&' && next_ch == '&') params->tokens = and; else if(ch == '&') { while((ch = getchar()) != new_line) { if(ch != whitespace && ch != tab) { fprintf(stderr, "incorrect command\n"); params->wrong_command = 1; return 0; } } params->tokens = '&'; } if(is_double_token(params)) getchar(); return 1; } else { params->wrong_command = 1; fprintf(stderr, "filename expected\n"); } } return 0; } static int special_token_handling(struct queue *word_chain, struct dynamic_array *tmp_word, int ch, struct param_type *params) { return stream_redirect_tokens(word_chain, tmp_word, ch, params) ? 1 : special_tokens(tmp_word, ch, params); } static int ignore_spaces(int ch, struct param_type params) { return (ch == whitespace || ch == tab) && !params.escape_sequences; } static int change_mode(int ch, struct param_type params) { return ch == '"' && !params.escape_sequences; } static int start_escape_sequence(int ch, struct param_type params) { return ch == backslash && !params.escape_sequences; } /* name of file for opening stream already exists */ static int excessive_words(int ch, const struct param_type *params) { int next_ch; if(filename_waiting(params)) { if(ch == new_line) return 0; while((next_ch = getchar()) != new_line) { if(next_ch == ' ') continue; if(!is_special_token(next_ch) && next_ch != '<' && next_ch != '>') return 1; else break; } ungetc(next_ch, stdin); } return 0; } static char** create_cmdline(const struct queue *word_chain, int word_counter) { char **cmdline = malloc((word_counter + 1) * sizeof(char*)); queue_copy_words_to_args(word_chain, cmdline); return cmdline; } 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 clean_up_memory(struct queue *word_chain, char **cmdline, struct param_type *params) { queue_clear(word_chain); free(cmdline); clear_filename(params); } 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 void change_streams(int input_fd, int output_fd) { if(input_fd) { dup2(input_fd, 0); close(input_fd); } if(output_fd) { dup2(output_fd, 1); close(output_fd); } } static void close_files(int input_fd, int output_fd) { if(input_fd) close(input_fd); if(output_fd) close(output_fd); } static void run_external_program(struct queue *word_chain, struct param_type *params) { int pid, wait_pid, result, input_fd, output_fd; input_fd = 0; output_fd = 0; if(word_chain->first == NULL) { fprintf(stderr, "empty command\n"); return; } 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; } char **cmdline = create_cmdline(word_chain, queue_get_word_count(word_chain)); if(is_cd_command(cmdline[0])) { change_directory(cmdline); clean_up_memory(word_chain, cmdline, params); } else { pid = fork(); if(pid == -1) { perror("fork error"); exit(1); } 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); /* child process */ if(pid == 0) { if(is_stream_redirection_set(params)) change_streams(input_fd, output_fd); execvp(cmdline[0], cmdline); params->last_execution_status = 1; perror(cmdline[0]); exit(1); } /* parent process */ close_files(input_fd, output_fd); clean_up_memory(word_chain, cmdline, params); /* waiting for forground process by pid*/ if(params->tokens != '&') do { wait_pid = wait(&result); } while(wait_pid != pid); /* return of background process zombie cleanup */ signal(SIGCHLD, handler); } } static void command_processing(struct param_type *params, enum modes *current_mode, struct queue *word_chain, struct dynamic_array *tmp_word, int ch) { /* and odd number of double quotes */ if(!is_double_quotes_pair(*params)) { print_error(); reset_params(params, current_mode, word_chain, tmp_word); show_invitation(); return; } if(params->empty_word_flag || params->is_word) { if(filename_waiting(params)) if(excessive_words(ch, params)) { fprintf(stderr, "too many args\n"); goto clean; } else add_filename(tmp_word, params); else add_word(word_chain, tmp_word, params); } else if(filename_waiting(params)) { fprintf(stderr, "filename expected\n"); goto clean; } if((params->tokens == and || params->tokens == or) && params->last_execution_status == 1) fprintf(stderr, "cannot be performed\n"); else run_external_program(word_chain, params); clean: if(params->tokens == '&' || params->tokens == 0 || ch == new_line) show_invitation(); reset_params(params, current_mode, word_chain, tmp_word); } static int is_empty_word(int ch, struct param_type params) { return (ch == whitespace || ch == tab) && !params.is_word && params.empty_word_flag; } static void word_separation_processing(int ch, struct param_type *params, enum modes *current_mode, struct queue *word_chain, struct dynamic_array *tmp_word) { /* 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, tmp_word, ch, params)) { if(!filename_waiting(params)) command_processing(params, current_mode, word_chain, tmp_word, ch); return; } if(params->wrong_command) { clean_input_buffer(); reset_params(params, current_mode, word_chain, tmp_word); show_invitation(); return; } if(check_separation(ch, *params)) { if(excessive_words(ch, params)) { clean_input_buffer(); reset_params(params, current_mode, word_chain, tmp_word); fprintf(stderr, "too many args\n"); 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; dynarr_push_back(tmp_word, ch); params->is_word = 1; params->escape_sequences = 0; } static int escape_double_quotes_or_backslash(int ch, struct param_type params) { return params.escape_sequences && (ch == double_quotes || ch == backslash); } static int double_quotes_again(int ch, struct param_type params) { return ch == double_quotes && !params.is_word && params.stored_symbol == '"'; } static void whole_word_processing(int ch, struct param_type *params, enum modes *current_mode, struct 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); dynarr_push_back(tmp_word, ch); params->is_word = 1; params->escape_sequences = 0; } int main() { int ch; struct param_type params; struct queue word_chain; struct dynamic_array tmp_word; enum modes current_mode = word_separation; queue_init(&word_chain); dynarr_create_array(&tmp_word); init_params(¶ms, ¤t_mode); show_invitation(); while((ch = getchar()) != EOF) { if(ch == new_line) command_processing(¶ms, ¤t_mode, &word_chain, &tmp_word, ch); else if(current_mode == word_separation) word_separation_processing(ch, ¶ms, ¤t_mode, &word_chain, &tmp_word); else if(current_mode == whole_word) whole_word_processing(ch, ¶ms, ¤t_mode, &word_chain, &tmp_word); } putchar(new_line); dynarr_clear(&tmp_word); return 0; }