From fecf54d47030ecc570b6d3b58e0ca78af448ccf9 Mon Sep 17 00:00:00 2001 From: narnaud Date: Fri, 6 May 2022 18:10:46 +0200 Subject: [PATCH] some more norminage --- Makefile | 2 +- lexer.c | 74 ++++++------------------------------------- minishell.h | 20 +++++++----- errors.c => utils_2.c | 53 ++++++++++++++++++++++++++++++- 4 files changed, 75 insertions(+), 74 deletions(-) rename errors.c => utils_2.c (50%) diff --git a/Makefile b/Makefile index 72e1f82..7c3b6ff 100755 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ NAME = minishell LIBFT = libft.a -SRCS = minishell.c lexer.c parser.c utils.c caller.c built-in.c env.c errors.c +SRCS = minishell.c lexer.c parser.c caller.c built-in.c env.c utils.c utils_2.c OBJS = ${SRCS:.c=.o} diff --git a/lexer.c b/lexer.c index 5e7a484..998c03a 100755 --- a/lexer.c +++ b/lexer.c @@ -15,6 +15,7 @@ int create_token(t_lexer *lex, char str[]) { t_token *tok; + t_token *tmp; t_type type; type = lex->type; @@ -23,22 +24,18 @@ int create_token(t_lexer *lex, char str[]) else lex->type = WORD; lex->next_type = WORD; - //if (!str[0]) - // return (0); - tok = lex->tokens; - if (!lex->tokens) - tok = ft_calloc(1, sizeof *tok); - else - { - while (tok->next) - tok = tok->next; - tok->next = ft_calloc(1, sizeof *tok); - tok = tok->next; - } + if (!str[0]) + return (0); + tok = ft_calloc(1, sizeof *tok); tok->type = type; tok->value = ft_strdup(str); - if (!lex->tokens) + tmp = lex->tokens; + while (tmp && tmp->next) + tmp = tmp->next; + if (!tmp) lex->tokens = tok; + else + tmp->next = tok; ft_bzero(str, 1024); return (1); } @@ -92,57 +89,6 @@ int check_register(t_lexer *lex, char **line, char *tmp) return (create_token(lex, tmp)); } -char *get_var_value(t_datas *datas, char **line, int *name_len) -{ - int i; - char **env; - char *name; - char *value; - - i = 1; - while (ft_isalpha((*line)[i]) || ft_isdigit((*line)[i]) || (*line)[i] == '_') - i++; - name = ft_substr(*line, 1, i - 1); - i = 0; - *name_len = ft_strlen(name); - value = NULL; - while (datas->envp[i] && !value) - { - env = ft_split(datas->envp[i], '='); - if (!ft_strncmp(name, env[0], *name_len + 1)) - value = (free(env[0]), env[1]); - else if (++i) - ft_free_split(env); - } - if (!datas->envp[i]) - value = strdup(""); - else - free(env); - return (value); -} - -int replace_var(t_datas *datas, char **line, char *tmp, int tmp_i) -{ - int name_len; - int i; - char *value; - - if ((*line)[1] == '?') - { - value = ft_itoa(datas->exit_code); - name_len = 2; - } - else - value = get_var_value(datas, line, &name_len); - i = 0; - while (value[i]) - tmp[tmp_i++] = value[i++]; - tmp[tmp_i] = 0; - *line += name_len + 1; - free(value); - return (tmp_i); -} - int check_state(t_lexer *lex, char **line) { t_state new; diff --git a/minishell.h b/minishell.h index 78a168c..6246368 100755 --- a/minishell.h +++ b/minishell.h @@ -46,16 +46,18 @@ typedef struct s_datas char **envp; int exit_code; } t_datas; -// ----------------------------------Errors.c -int file_error(char *cmd, char *file, char *msg); // ----------------------------------Utils.c int is_empty(char *line); -void halt(int ret_code); -void sigs_handler(int sig_num); -void nothing(int sig_num); void handle_status(t_datas *datas, int status); +void nothing(int sig_num); +void sigs_handler(int sig_num); +void halt(int ret_code); +// ----------------------------------Utils_2.c +int replace_var(t_datas *datas, char **line, \ + char *tmp, int tmp_i); +int file_error(char *cmd, char *file, char *msg); // ----------------------------------Builtins.c int close_minishell(int exit_code); int ft_echo(t_command *cmd); @@ -92,10 +94,13 @@ typedef struct s_token t_type type; struct s_token *next; } t_token; + char *expend_str(t_datas *datas, char *line); size_t count_arguments(t_token *tok); -void update_redir(t_datas *datas, t_command *cmd, t_token *tok); -t_command *parser(t_datas *datas, t_token *tok, t_command *prev); +void update_redir(t_datas *datas, t_command *cmd,\ + t_token *tok); +t_command *parser(t_datas *datas, t_token *tok,\ + t_command *prev); // ----------------------------------Lexer.c typedef enum e_state @@ -117,7 +122,6 @@ typedef struct s_lex int create_token(t_lexer *lex, char str[]); int check_register(t_lexer *lex, char **line, char *tmp); -int replace_var(t_datas *datas, char **line, char *tmp, int tmp_i); int check_state(t_lexer *lex, char **line); t_token *lexer(t_datas *datas, char *line); diff --git a/errors.c b/utils_2.c similarity index 50% rename from errors.c rename to utils_2.c index 0453235..734f8e9 100644 --- a/errors.c +++ b/utils_2.c @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* errors.c :+: :+: :+: */ +/* utils_2.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -23,3 +23,54 @@ int file_error(char *cmd, char *file, char *msg) ft_putchar_fd('\n', 2); return (0); } + +char *get_var_value(t_datas *datas, char **line, int *name_len) +{ + int i; + char **env; + char *name; + char *value; + + i = 1; + while (ft_isalpha((*line)[i]) || ft_isdigit((*line)[i]) || (*line)[i] == '_') + i++; + name = ft_substr(*line, 1, i - 1); + i = 0; + *name_len = ft_strlen(name); + value = NULL; + while (datas->envp[i] && !value) + { + env = ft_split(datas->envp[i], '='); + if (!ft_strncmp(name, env[0], *name_len + 1)) + value = (free(env[0]), env[1]); + else if (++i) + ft_free_split(env); + } + if (!datas->envp[i]) + value = strdup(""); + else + free(env); + return (value); +} + +int replace_var(t_datas *datas, char **line, char *tmp, int tmp_i) +{ + int name_len; + int i; + char *value; + + if ((*line)[1] == '?') + { + value = ft_itoa(datas->exit_code); + name_len = 2; + } + else + value = get_var_value(datas, line, &name_len); + i = 0; + while (value[i]) + tmp[tmp_i++] = value[i++]; + tmp[tmp_i] = 0; + *line += name_len + 1; + free(value); + return (tmp_i); +}