From 3bb997212b3a0d140a34932f9deff52793673d49 Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Wed, 16 Sep 2020 16:40:22 +0200 Subject: Added g_state to store global variables, Refactoring tok_assign_str --- include/minishell.h | 14 +++++-- src/builtin/exit.c | 4 +- src/env.c | 4 +- src/error.c | 4 +- src/eval/cmd.c | 5 +-- src/eval/eval.c | 2 +- src/eval/operation.c | 2 +- src/lexer/lexer.c | 23 +---------- src/lexer/trim.c | 110 +++++++++++++++++++++++++-------------------------- src/lexer/utils.c | 87 +++++++++++++++++----------------------- src/main.c | 22 ++++++----- src/setup.c | 6 +-- src/signal.c | 6 +-- 13 files changed, 131 insertions(+), 158 deletions(-) diff --git a/include/minishell.h b/include/minishell.h index 58afe7f..2f64269 100644 --- a/include/minishell.h +++ b/include/minishell.h @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/26 15:33:51 by cacharle #+# #+# */ -/* Updated: 2020/09/16 16:07:36 by charles ### ########.fr */ +/* Updated: 2020/09/16 16:26:25 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -50,8 +50,14 @@ typedef t_ftvec* t_env; -extern int g_last_status; -extern char *g_progname; +typedef struct +{ + int last_status; + char *progname; + +} t_state; + +extern t_state g_state; /* ** path.c @@ -127,6 +133,6 @@ void print_prompt(void); ** setup.c */ -bool setup(char *first_arg, t_env env); +bool setup(char *first_arg, t_env env); #endif diff --git a/src/builtin/exit.c b/src/builtin/exit.c index 31acd11..aa09af7 100644 --- a/src/builtin/exit.c +++ b/src/builtin/exit.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:10:16 by charles #+# #+# */ -/* Updated: 2020/09/15 17:48:22 by charles ### ########.fr */ +/* Updated: 2020/09/16 16:26:08 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,7 +31,7 @@ int builtin_exit(char **argv, t_env env) (void)env; if (argv[1] == NULL) - status = g_last_status; + status = g_state.last_status; else { errno = 0; diff --git a/src/env.c b/src/env.c index 0f84d44..b463e9c 100644 --- a/src/env.c +++ b/src/env.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 09:21:24 by cacharle #+# #+# */ -/* Updated: 2020/09/15 17:44:21 by charles ### ########.fr */ +/* Updated: 2020/09/16 16:29:33 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -132,7 +132,7 @@ char *env_search_first_match(t_env env, const char *haystack) while (ft_isalnum(haystack[len]) || haystack[len] == '_') len++; if (haystack[0] == '?') - return (ft_itoa_cpy(g_status_buf, g_last_status)); + return (ft_itoa_cpy(g_status_buf, g_state.last_status)); if (len == 0) return (NULL); i = -1; diff --git a/src/error.c b/src/error.c index b5a422b..9fe66a6 100644 --- a/src/error.c +++ b/src/error.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/14 11:02:52 by charles #+# #+# */ -/* Updated: 2020/09/16 15:42:42 by charles ### ########.fr */ +/* Updated: 2020/09/16 16:28:10 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,7 +37,7 @@ void verrorf(const char *format, va_list ap) char *str; char c; - ft_putstr_fd(g_progname, STDERR_FILENO); + ft_putstr_fd(g_state.progname, STDERR_FILENO); ft_putstr_fd(": ", STDERR_FILENO); while (*format != '\0') { diff --git a/src/eval/cmd.c b/src/eval/cmd.c index 3e301b0..151d11d 100644 --- a/src/eval/cmd.c +++ b/src/eval/cmd.c @@ -6,14 +6,13 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/14 10:41:31 by charles #+# #+# */ -/* Updated: 2020/09/15 20:06:39 by charles ### ########.fr */ +/* Updated: 2020/09/16 16:27:59 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "eval.h" pid_t g_child_pid = -1; -int g_last_status = 0; int wrapped_cmd(t_fork_param_cmd *param) { @@ -58,6 +57,6 @@ int eval_cmd(int fds[2], t_env env, t_ast *ast, pid_t *child_pid, int fd_to_cl param.env = env; status = fork_wrap(fds, ¶m, (t_wrapped_func)wrapped_cmd, child_pid, fd_to_close); ft_split_destroy(argv); - g_last_status = status; + g_state.last_status = status; return (status); } diff --git a/src/eval/eval.c b/src/eval/eval.c index 899a880..2775f9e 100644 --- a/src/eval/eval.c +++ b/src/eval/eval.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/09/13 20:38:06 by charles #+# #+# */ -/* Updated: 2020/09/15 20:09:43 by charles ### ########.fr */ +/* Updated: 2020/09/16 16:29:20 by charles ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/eval/operation.c b/src/eval/operation.c index cd154b5..fbdf5bb 100644 --- a/src/eval/operation.c +++ b/src/eval/operation.c @@ -58,7 +58,7 @@ int eval_operation(int fds[2], t_env env, t_ast *ast) } if ((status = eval(left_fds, env, ast->op.left, NULL, FD_NONE)) == EVAL_FATAL) return (EVAL_FATAL); - g_last_status = status; + g_state.last_status = status; if ((ast->op.sep == TAG_AND && status != 0) || (ast->op.sep == TAG_PIPE && status != 0) || (ast->op.sep == TAG_OR && status == 0)) diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c index a1d7469..26355fe 100644 --- a/src/lexer/lexer.c +++ b/src/lexer/lexer.c @@ -6,7 +6,7 @@ /* By: nahaddac +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/07/16 08:18:25 by nahaddac #+# #+# */ -/* Updated: 2020/09/14 16:33:58 by charles ### ########.fr */ +/* Updated: 2020/09/16 16:37:49 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -53,7 +53,7 @@ int tok_len(char *input) } if (input[i] == '(' || input[i] == ')') return (i + 1); - if (lexer_sep(input[i])) // fucked on & alone + if (lexer_sep(input[i])) { if (input[i] == input[i + 1]) i++; @@ -114,22 +114,3 @@ int lexer(char *input, t_tok_lst **out) return status; } - -/* int check_input_out(char *input) */ -/* { */ -/* int i; */ -/* int j; */ -/* */ -/* i = 0; */ -/* while(input[i] != '\0') */ -/* { */ -/* j = 0; */ -/* j += len_until_sep(&input[i]); */ -/* if (j != 0) */ -/* return(j); */ -/* i += j; */ -/* j = check_input(&input[i]); */ -/* return(j); */ -/* } */ -/* return(0); */ -/* } */ diff --git a/src/lexer/trim.c b/src/lexer/trim.c index 0bdacac..34e162a 100644 --- a/src/lexer/trim.c +++ b/src/lexer/trim.c @@ -6,7 +6,7 @@ /* By: nahaddac +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/07/16 08:18:36 by nahaddac #+# #+# */ -/* Updated: 2020/09/15 18:31:56 by charles ### ########.fr */ +/* Updated: 2020/09/16 16:39:37 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,83 +16,83 @@ void del_space(char *str) { - int i; + int i; - i = ft_strlen(str); - if (ft_isblank(str[i - 1])) - { - i -= 1; - while (ft_isblank(str[i])) - { - if (str[i - 1] == '\\') - break ; - i--; - } + i = ft_strlen(str); + if (ft_isblank(str[i - 1])) + { + i -= 1; + while (ft_isblank(str[i])) + { + if (str[i - 1] == '\\') + break ; + i--; + } str[i + 1] = '\0'; - } + } } int del_quote(char *str) { - size_t i; - size_t quote_counter; + size_t i; + size_t quote_counter; - i = 0; - quote_counter = 1; - if (str[0] == '\'') + i = 0; + quote_counter = 1; + if (str[0] == '\'') { - while (str[i++] != '\0') - { - if (str[i] == '\'') - { - quote_counter++; - break ; - } - } + while (str[i++] != '\0') + { + if (str[i] == '\'') + { + quote_counter++; + break ; + } + } } - else if (str[0] == '"') + else if (str[0] == '"') { - while (str[i++] != '\0') - { - if (str[i] == '\\') - i += 2; - if (str[i] == '"') - { - quote_counter++; - break ; - } - } + while (str[i++] != '\0') + { + if (str[i] == '\\') + i += 2; + if (str[i] == '"') + { + quote_counter++; + break ; + } + } } - if (quote_counter % 2 == 1) + if (quote_counter % 2 == 1) { errorf("unexpected EOF while looking for matching `%c'\n", str[0]); - return (1); + return (1); } str[i] = '\0'; ft_memmove(str, str + 1, ft_strlen(str + 1) + 1); return (0); } -int lexer_trim(t_tok_lst *tokens) +int lexer_trim(t_tok_lst *tokens) { - int status; + int status; - while (tokens != NULL) - { - if (tokens->tag & (TAG_STR_DOUBLE | TAG_STR_SINGLE)) - { - if ((status = del_quote(tokens->content)) != 0) + while (tokens != NULL) + { + if (tokens->tag & (TAG_STR_DOUBLE | TAG_STR_SINGLE)) + { + if ((status = del_quote(tokens->content)) != 0) return (status); if (tokens->next == NULL) tokens->tag &= ~TAG_STICK; - } - else - { - del_space(tokens->content); - if (tokens->next == NULL) + } + else + { + del_space(tokens->content); + if (tokens->next == NULL) tokens->tag &= ~TAG_STICK; - } - tokens = tokens->next; - } - return (0); + } + tokens = tokens->next; + } + return (0); } diff --git a/src/lexer/utils.c b/src/lexer/utils.c index b4846d6..66b2af7 100644 --- a/src/lexer/utils.c +++ b/src/lexer/utils.c @@ -6,7 +6,7 @@ /* By: nahaddac +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/07/16 08:18:15 by nahaddac #+# #+# */ -/* Updated: 2020/09/14 15:16:35 by charles ### ########.fr */ +/* Updated: 2020/09/16 16:37:20 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,25 +15,25 @@ // return token tag corresponding to string id enum e_tok tok_assign_tag(char *content) { - if (content[0] == ';') - return (TAG_END); - if (ft_strncmp(content, "&&", 2) == 0) - return (TAG_AND); - if (ft_strncmp(content, "||", 2) == 0) - return (TAG_OR); - if(content[0] == '|') - return (TAG_PIPE); - if (content[0] == '>') - return (TAG_REDIR_OUT); - if (content[0] == '<') - return (TAG_REDIR_IN); - if (ft_strncmp(content, ">>", 2) == 0) - return (TAG_REDIR_APPEND); - if (content[0] == '(') - return (TAG_PARENT_OPEN); - if (content[0] == ')') - return (TAG_PARENT_CLOSE); - return (0); + if (content[0] == ';') + return (TAG_END); + if (ft_strncmp(content, "&&", 2) == 0) + return (TAG_AND); + if (ft_strncmp(content, "||", 2) == 0) + return (TAG_OR); + if(content[0] == '|') + return (TAG_PIPE); + if (content[0] == '>') + return (TAG_REDIR_OUT); + if (content[0] == '<') + return (TAG_REDIR_IN); + if (ft_strncmp(content, ">>", 2) == 0) + return (TAG_REDIR_APPEND); + if (content[0] == '(') + return (TAG_PARENT_OPEN); + if (content[0] == ')') + return (TAG_PARENT_CLOSE); + return (0); } enum e_tok tok_assign_stick(t_tok_lst *tok) @@ -49,35 +49,20 @@ enum e_tok tok_assign_stick(t_tok_lst *tok) enum e_tok tok_assign_str(t_tok_lst *tok) { - int i; + char *found; - // could use strchr to search ' or " - i = 0; - while (tok->content[i] != '\0') - { - if (tok->content[i] == '\'') - { - tok->tag = TAG_STR_SINGLE; - return (tok_assign_stick(tok)); - } - if (tok->content[i] == '"') - { - tok->tag = TAG_STR_DOUBLE; - return (tok_assign_stick(tok)); - } - else - { - tok->tag = TAG_STR; - return (tok_assign_stick(tok)); - } - i++; - } - return (0); + found = ft_strpbrk(tok->content, "'\""); + if (found == NULL) + tok->tag = TAG_STR; + else if (*found == '\'') + tok->tag = TAG_STR_SINGLE; + else if (*found == '"') + tok->tag = TAG_STR_DOUBLE; + return (tok_assign_stick(tok)); } // check is char is separator -// & alone could be considered a separator int lexer_sep(char c) { return (ft_strchr(";&|><()", c) != NULL); @@ -94,14 +79,14 @@ int quote_len(char *input, int i) char quote_type; quote_type = input[i]; - i++; - while (input[i] != quote_type && input[i] != '\0') - { - if (quote_type == '"' && input[i] == '\\') + i++; + while (input[i] != quote_type && input[i] != '\0') + { + if (quote_type == '"' && input[i] == '\\') i++; - i++; - } + i++; + } while (ft_isblank(input[i + 1])) i++; - return (i + 1); + return (i + 1); } diff --git a/src/main.c b/src/main.c index b99bb97..0c33591 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 11:45:44 by cacharle #+# #+# */ -/* Updated: 2020/09/16 16:14:21 by charles ### ########.fr */ +/* Updated: 2020/09/16 16:28:47 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,7 +31,9 @@ int debug_lexer(char *input); char *g_progname = "minishell"; -int execute(t_env env, char *input) +t_state g_state; + +int execute(t_env env, char *input) { t_tok_lst *lexer_out; int status; @@ -49,11 +51,11 @@ int execute(t_env env, char *input) status = eval(fds, env, parser_out->ast, NULL, FD_NONE); if (status == EVAL_FATAL) exit(1); - g_last_status = status; + g_state.last_status = status; return (status); } -int repl(t_env env) +int repl(t_env env) { int ret; char *line; @@ -77,7 +79,7 @@ int repl(t_env env) #ifndef MINISHELL_TEST -int main(int argc, char **argv, char **envp) +int main(int argc, char **argv, char **envp) { t_env env; @@ -89,29 +91,29 @@ int main(int argc, char **argv, char **envp) if ((env = env_from_array(envp)) == NULL) return (1); setup(argv[0], env); - g_last_status = 0; + g_state.child_pid = 0; repl(env); ft_vecdestroy(env, free); - return (g_last_status); + return (g_state.last_status); } #else -int main(int argc, char **argv, char **envp) +int main(int argc, char **argv, char **envp) { t_env env; if ((env = env_from_array(envp)) == NULL) return (1); setup(argv[0], env); - g_last_status = 0; + g_state.last_status = 0; if (argc == 3 && ft_strcmp(argv[1], "-l") == 0) return (debug_lexer(argv[2])); if (argc == 3 && ft_strcmp(argv[1], "-c") == 0) return (execute(env, argv[2])); repl(env); ft_vecdestroy(env, free); - return (g_last_status); + return (g_state.last_status); } #endif diff --git a/src/setup.c b/src/setup.c index a453e99..61d5ba5 100644 --- a/src/setup.c +++ b/src/setup.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/09/16 15:46:09 by charles #+# #+# */ -/* Updated: 2020/09/16 16:17:35 by charles ### ########.fr */ +/* Updated: 2020/09/16 16:24:28 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -68,9 +68,9 @@ void setup_progname(char *first_arg) return ; last_slash = ft_strrchr(first_arg, '/'); if (last_slash == NULL) - g_progname = first_arg; + g_state.progname = first_arg; else - g_progname = last_slash + 1; + g_state.progname = last_slash + 1; } bool setup(char *first_arg, t_env env) diff --git a/src/signal.c b/src/signal.c index 0dec80c..5d87d85 100644 --- a/src/signal.c +++ b/src/signal.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/07/16 09:16:16 by charles #+# #+# */ -/* Updated: 2020/09/09 14:10:40 by charles ### ########.fr */ +/* Updated: 2020/09/16 16:27:30 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,7 +16,7 @@ void signal_sigint(int signum) { (void)signum; - g_last_status = 130; + g_state.last_status = 130; if (g_child_pid != -1) { kill(g_child_pid, SIGINT); @@ -32,7 +32,7 @@ void signal_sigint(int signum) void signal_sigquit(int signum) { (void)signum; - g_last_status = 131; + g_state.last_status = 131; if (g_child_pid != -1) { kill(g_child_pid, SIGQUIT); -- cgit