diff options
-rw-r--r-- | TODO.org | 1 | ||||
-rw-r--r-- | src/b_alias.c | 81 | ||||
-rw-r--r-- | src/b_alias.h | 2 | ||||
-rw-r--r-- | src/d_define.h | 1 | ||||
-rw-r--r-- | src/p_lblock_next.c | 108 | ||||
-rw-r--r-- | src/p_lblock_next.h | 5 | ||||
-rw-r--r-- | src/s_com.c | 3 | ||||
-rw-r--r-- | src/u_alias.c | 2 |
8 files changed, 155 insertions, 48 deletions
@@ -26,6 +26,7 @@ ** DONE [#C] Handle comments better # ** TODO [#C] msh ~> ./qwe.sh <---- without shebang (maybe works fine already) ** TODO [#C] Fix ~ only first pos of arg +** TODO [#C] Fix $? * Stuff to add ** DONE [#A] builtins to pipes diff --git a/src/b_alias.c b/src/b_alias.c index 08f40ed..8aff531 100644 --- a/src/b_alias.c +++ b/src/b_alias.c @@ -10,9 +10,14 @@ /* */ /* ************************************************************************** */ +#include <libft.h> #include <stdint.h> +#include <unistd.h> +#include <limits.h> +#include "d_define.h" #include "s_struct.h" +#include "u_alias.h" #include "u_utils.h" static void @@ -28,10 +33,64 @@ static void } } -static void +static uint8_t b_print_arg(const char arg[], t_msh *msh) { char *ptr; + char val[ARG_MAX]; + t_bool invalid; + + ptr = (char*)arg; + invalid = FALSE; + while (*ptr != C_NUL && *ptr != C_EQUALS) + { + if (ft_iswhitespace(*ptr) == TRUE) + invalid = TRUE; + ptr++; + } + if (*ptr == C_NUL) + { + if (u_get_alias_value(val, arg, ARG_MAX, msh) == 0) + { + ft_printf("alias %s='%s'", arg, val); + return (0); + } + else + { + ft_dprintf(STDERR_FILENO, "minishell: alias: %s: not found\n", arg); + return (1); + } + } + else if (*ptr == C_EQUALS && invalid == TRUE) + { + ft_strlcpy(val, arg, ptr - arg); + ft_dprintf(STDERR_FILENO, + "minishell: alias: `%s': invalid alias name\n", + val); + return (1); + } + return (0); +} + +static void + b_register_arg(const char arg[], t_msh *msh) +{ + char *ptr; + char name[255]; + char value[ARG_MAX]; + + ptr = (char*)arg; + while (*ptr != C_NUL && *ptr != C_EQUALS) + { + ptr++; + } + if (*ptr == C_EQUALS) + { + ft_strlcpy(name, arg, (ptr - arg < 255) ? ((ptr - arg) + 1) : (255)); + ptr += 1; + ft_strlcpy(value, ptr, ARG_MAX); + u_set_alias_value(name, value, msh); + } } uint8_t @@ -39,7 +98,9 @@ uint8_t { const uint64_t argc = u_builtins_get_argc((const char**)args); int32_t i; + uint8_t ret; + ret = 0; if (argc == 0) { b_print_alias_list(msh); @@ -49,21 +110,29 @@ uint8_t i = 0; while (args[i] != NULL) { - b_print_arg(args[i], msh); + if (b_print_arg(args[i], msh) != 0) + { + ret = 1; + } i++; } } - return (0); + return (ret); } -uint8_t +void b_alias_mute(char *args[], t_msh *msh) { const uint64_t argc = u_builtins_get_argc((const char**)args); + int32_t i; - (void)msh; if (argc > 0) { + i = 0; + while (args[i] != NULL) + { + b_register_arg(args[i], msh); + i++; + } } - return (0); } diff --git a/src/b_alias.h b/src/b_alias.h index 2c80ffc..83b1878 100644 --- a/src/b_alias.h +++ b/src/b_alias.h @@ -18,6 +18,6 @@ #include "s_struct.h" uint8_t b_alias(char *args[], t_msh *msh); -uint8_t b_alias_mute(char *args[], t_msh *msh); +void b_alias_mute(char *args[], t_msh *msh); #endif diff --git a/src/d_define.h b/src/d_define.h index 897b720..1709bea 100644 --- a/src/d_define.h +++ b/src/d_define.h @@ -75,6 +75,7 @@ #define C_SQUOTE 0x27 #define C_AMP 0x26 #define C_SEMIC 0x3b +#define C_EQUALS 0x3d #define C_BACKS 0x5c #define C_PIPE 0x7c diff --git a/src/p_lblock_next.c b/src/p_lblock_next.c index a5314f2..adfa043 100644 --- a/src/p_lblock_next.c +++ b/src/p_lblock_next.c @@ -19,6 +19,7 @@ #include "s_destroy.h" #include "f_fail.h" #include "s_struct.h" +#include "u_alias.h" #include "u_parse.h" #include "u_utils.h" #include "u_vars.h" @@ -66,8 +67,8 @@ static void *(p) = word + (i + ft_strlen(varval) - 1); } -char - *p_subst_vars(char word[], t_msh *msh) +void + p_subst_vars(char word[], t_msh *msh) { char *ptr; t_quote_mode mode; @@ -87,7 +88,25 @@ char } ptr++; } - return (word); +} + +void + p_subst_alias(char word[], t_msh *msh) +{ + /* char subst[ARG_MAX]; */ + char *ptr; + + ptr = word; + (void)msh; + ft_printf("[%s]\n", ptr); + /* while (*ptr != C_NUL) */ + /* { */ + /* ptr++; */ + /* } */ + /* if (u_get_alias_value(subst, ?, ARG_MAX, msh) == 0) */ + /* { */ + /* /\* copy *\/ */ + /* } */ } char @@ -200,42 +219,57 @@ static void msh->env_fork_tmp[j][0] = '\0'; } -char - **p_check_args_equals(char *words[], t_msh *msh) +void + p_check_args_equals(char word[], t_msh *msh) { - char *ptr; - t_bool reg; - t_bool isvar; - int64_t i; + char *ptr; + t_quote_mode mode; - i = 0; - reg = FALSE; - isvar = FALSE; - while (words[i]) + mode = Q_NONE; + ptr = word; + while (*ptr != C_NUL) { - ptr = words[i]; - while (*ptr != '\0' && *ptr != '=') - ptr++; - if (*ptr == '=') - { - reg = TRUE; - isvar = TRUE; - } - if (*ptr == '\0' || words[i][0] == '=' || - ft_isdigit(words[i][0]) == TRUE) - { - reg = FALSE; - if (i == 0) - isvar = FALSE; - if (isvar == TRUE) - p_add_to_env_fork(i, words, msh); - else - msh->env_fork_tmp[0][0] = '\0'; - break ; - } - i++; + if (*ptr == C_DQUOTE) + mode = u_meet_dquote(word, ptr, mode); + else if (*ptr == C_SQUOTE) + mode = u_meet_squote(word, ptr, mode); + else if (mode == Q_NONE && *ptr == C_EQUALS) + u_meet_equals(); + ptr++; } - if (isvar == TRUE) - return (p_add_to_variables_and_delete(words, reg, i, msh)); - return (words); + /* char *ptr; */ + /* t_bool reg; */ + /* t_bool isvar; */ + /* int64_t i; */ + + /* i = 0; */ + /* reg = FALSE; */ + /* isvar = FALSE; */ + /* while (words[i]) */ + /* { */ + /* ptr = words[i]; */ + /* while (*ptr != '\0' && *ptr != '=') */ + /* ptr++; */ + /* if (*ptr == '=') */ + /* { */ + /* reg = TRUE; */ + /* isvar = TRUE; */ + /* } */ + /* if (*ptr == '\0' || words[i][0] == '=' || */ + /* ft_isdigit(words[i][0]) == TRUE) */ + /* { */ + /* reg = FALSE; */ + /* if (i == 0) */ + /* isvar = FALSE; */ + /* if (isvar == TRUE) */ + /* p_add_to_env_fork(i, words, msh); */ + /* else */ + /* msh->env_fork_tmp[0][0] = '\0'; */ + /* break ; */ + /* } */ + /* i++; */ + /* } */ + /* if (isvar == TRUE) */ + /* return (p_add_to_variables_and_delete(words, reg, i, msh)); */ + /* return (words); */ } diff --git a/src/p_lblock_next.h b/src/p_lblock_next.h index 5fa9f45..916055e 100644 --- a/src/p_lblock_next.h +++ b/src/p_lblock_next.h @@ -17,9 +17,10 @@ #include "s_struct.h" -char *p_subst_vars(char word[], t_msh *msh); +void p_subst_vars(char word[], t_msh *msh); +void p_subst_alias(char word[], t_msh *msh); char **p_subst_args(const char word[], int8_t redir); char **p_subst_home(char *word[], t_msh *msh); -char **p_check_args_equals(char *words[], t_msh *msh); +void p_check_args_equals(char word[], t_msh *msh); #endif diff --git a/src/s_com.c b/src/s_com.c index 277d996..51b2500 100644 --- a/src/s_com.c +++ b/src/s_com.c @@ -117,11 +117,12 @@ t_com if (p_get_redir(nword, &com) != 0) return (NULL); p_subst_vars(nword, msh); + p_check_args_equals(nword, msh); + p_subst_alias(nword, msh); if ((words = p_split_args(nword, com->redir)) == NULL) return (NULL); if ((words = p_subst_home(words, msh)) == NULL) return (NULL); - words = p_check_args_equals(words, msh); if (msh->env_fork_tmp[0][0] != '\0') s_com_cpy_env_fork(&com, msh); if (s_fill_com(words, &com) < 0) diff --git a/src/u_alias.c b/src/u_alias.c index f273a5c..92bd498 100644 --- a/src/u_alias.c +++ b/src/u_alias.c @@ -48,7 +48,7 @@ void { t_lalias *new; - if (u_get_alias_value(NULL, name, 0, msh) != 0) + if (u_get_alias_value(NULL, name, 0, msh) == 0) { s_lalias_rebind(&msh->alias, name, value); } |