From 8f4f87eabec13330a2b3a974975053c1e4632a11 Mon Sep 17 00:00:00 2001 From: scratko Date: Fri, 14 Jun 2024 15:15:41 +0300 Subject: Shell-V release Added pipeline. Formatting shell.c (created lexical_analysys.c). Error codes. Some functions of queue.c are created by preprocessor. --- lexical_analysis.c | 50 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 20 deletions(-) (limited to 'lexical_analysis.c') diff --git a/lexical_analysis.c b/lexical_analysis.c index f931118..4349a69 100644 --- a/lexical_analysis.c +++ b/lexical_analysis.c @@ -1,5 +1,6 @@ #include "lexical_analysis.h" -MAKE_QUEUE_PUSH(c_queue, cmdline, char**) + +#include int is_double_quotes_pair(struct param_type params) { @@ -48,18 +49,14 @@ int is_empty_word(int ch, struct param_type params) int command_execution_condition(struct param_type *params) { return - (!filename_waiting(params) && !params->pipeline) || + (params->tokens != '<' && params->tokens != '>' && + params->tokens != append && !params->pipeline) || (params->pipeline && params->tokens == '&'); } -int is_special_token(int ch) +int is_first_special_token_character(int ch) { - return ch == and || ch == or || ch == '&' || ch == ';' || ch == '|'; -} - -int is_redirect_token(int ch, int next_ch) -{ - return ch == '<' || ch == '>' || (ch == '>' && next_ch == '>'); + return ch == '<' || ch == '>' || ch == '&' || ch == '|' || ch == ';'; } int excessive_words(int ch, struct param_type *params) @@ -72,8 +69,10 @@ int excessive_words(int ch, struct param_type *params) while((next_ch = getchar()) != new_line) { if(next_ch == ' ') continue; - if(!is_special_token(next_ch) && next_ch != '<' && next_ch != '>') + if(!is_first_special_token_character(next_ch)) { + params->wrong_command = err_extra_chars_after_filename; return 1; + } else break; } @@ -88,7 +87,7 @@ void add_word_or_filename(struct w_queue *word_chain, { /* filenames */ if(filename_waiting(params) && !params->wrong_command) - add_filename(tmp_word, params); + add_filename(tmp_word, params); /* execute command */ else if(params->is_word) add_word(word_chain, tmp_word, params); @@ -108,6 +107,17 @@ int is_double_token(struct param_type *params) params->tokens == append; } +int is_special_token(int ch, int next_ch) +{ + return (ch == '&' && next_ch == '&') || (ch == '|' && ch == '|') || + ch == '&' || ch == ';' || ch == '|'; +} + +int is_redirect_token(int ch, int next_ch) +{ + return ch == '<' || ch == '>' || (ch == '>' && next_ch == '>'); +} + /* * redirection token verification */ @@ -130,14 +140,16 @@ int stream_redirect_tokens(struct w_queue *word_chain, if(is_double_token(params)) getchar(); return 1; - } else { - fprintf(stderr, "syntax error\n"); - params->wrong_command = 1; - } + } else + params->wrong_command = err_redirect_stream_again; } return 0; } +/* + * the first element of the pipeline outputs to a file or + * redirects to a file in the middle of the pipeline + */ int wrong_streams_redirection(struct param_type *params) { return @@ -156,8 +168,7 @@ int pipeline_token_processing(struct w_queue *word_chain, char **cmdline = NULL; if(is_stream_redirection_set(params) && wrong_streams_redirection(params)) { - /* TODO: add error codes */ - params->wrong_command = 1; + params->wrong_command = err_redirect_stream_in_pipeline; return 0; } params->tokens = '|'; @@ -182,7 +193,7 @@ int special_tokens(struct w_queue *word_chain, struct c_queue *cmdlines, next_ch = getchar(); ungetc(next_ch, stdin); - if(is_special_token(ch)) { + if(is_special_token(ch, next_ch)) { add_word_or_filename(word_chain, tmp_word, params); if(params->wrong_command) @@ -199,8 +210,7 @@ int special_tokens(struct w_queue *word_chain, struct c_queue *cmdlines, else if(ch == '&') { while((ch = getchar()) != new_line) { if(ch != whitespace && ch != tab) { - fprintf(stderr, "incorrect command\n"); - params->wrong_command = 1; + params->wrong_command = err_bg_process; return 0; } } -- cgit v1.2.3