diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/.DS_Store | bin | 6148 -> 0 bytes | |||
| -rw-r--r-- | src/env.c | 6 | ||||
| -rw-r--r-- | src/lexer/lexer_len_element_and_mall.c | 1 | ||||
| -rw-r--r-- | src/lexer/token.c | 29 | ||||
| -rw-r--r-- | src/main.c | 24 | ||||
| -rw-r--r-- | src/ms_glob.c (renamed from src/glob.c) | 6 | ||||
| -rw-r--r-- | src/parse/parse.c | 12 | ||||
| -rw-r--r-- | src/preprocess.c | 96 | ||||
| -rw-r--r-- | src/utils.c | 4 |
9 files changed, 143 insertions, 35 deletions
diff --git a/src/.DS_Store b/src/.DS_Store Binary files differdeleted file mode 100644 index cd95961..0000000 --- a/src/.DS_Store +++ /dev/null @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 09:21:24 by cacharle #+# #+# */ -/* Updated: 2020/05/08 13:48:40 by charles ### ########.fr */ +/* Updated: 2020/06/09 16:15:47 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -75,8 +75,10 @@ char *env_search_first_match(t_env env, const char *haystack) len = 0; while (ft_isalnum(haystack[len]) || haystack[len] == '_') len++; + if (len == 0) + return ("$"); i = 0; - while (i < env->size) + while (i < env->size - 1) { if (ft_strncmp((char*)env->data[i], haystack, len) == 0) return (ft_strchr((char*)env->data[i], '=') + 1); diff --git a/src/lexer/lexer_len_element_and_mall.c b/src/lexer/lexer_len_element_and_mall.c index 30ebc7b..e0e1132 100644 --- a/src/lexer/lexer_len_element_and_mall.c +++ b/src/lexer/lexer_len_element_and_mall.c @@ -38,6 +38,7 @@ char **lexer_malloc_len_elem(char *input, int i, char **out) int k = 0; int temp = 0; + (void)i; j += lexer_count_len_element(&input[j], 0); k = j; out[temp] = malloc(sizeof(char) * k); diff --git a/src/lexer/token.c b/src/lexer/token.c new file mode 100644 index 0000000..bf534a4 --- /dev/null +++ b/src/lexer/token.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* token.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/06/09 13:38:08 by charles #+# #+# */ +/* Updated: 2020/06/09 13:39:27 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "lexer.h" + +t_token *token_new(enum e_token_tag tag, char *content) +{ + t_token *token; + + if (content == NULL + || (token = malloc(sizeof(t_token))) == NULL) + return (NULL); + if ((token->content = ft_strdup(content)) == NULL) + { + free(token); + return (NULL); + } + token->tag = tag; + return token; +} @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 11:45:44 by cacharle #+# #+# */ -/* Updated: 2020/04/05 12:15:57 by charles ### ########.fr */ +/* Updated: 2020/06/09 16:53:45 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,6 +19,7 @@ #include "ast.h" #include "eval.h" #include "ms_glob.h" +#include "lexer.h" /* ** \brief Program entrypoint @@ -88,9 +89,24 @@ int main(int argc, char **argv, char **envp) /* free(line); */ /* ft_htdestroy(path, free); */ /* ms_glob("src#<{(|"); */ - char *j = ms_glob("*/*.c"); - printf("%s\n", j); - free(j); + + /* char *j = ms_glob("|)}>#*.c"); */ + /* printf("%s\n", j); */ + /* free(j); */ + + t_ftvec *v = ft_vecnew(32); + /* ft_vecpush(v, token_new(LTAG_STR, "$TERM$LFS$TERM$TERM.")); */ + /* ft_vecpush(v, token_new(LTAG_STR, "$$LFS$TERM$TERM.")); */ + ft_vecpush(v, token_new(LTAG_STR, "*/*.c$TERM")); + /* ft_vecpush(v, token_new(LTAG_STR, "src#<{(|.c include#<{(|.h")); */ + /* ft_vecpush(v, token_new(LTAG_STR, "$A$B")); */ + char **as = preprocess_argv(v, env); + char **tmp = as; + + while (*as != NULL) + puts(*as++); + + ft_split_destroy(tmp); ft_vecdestroy(env, free); return (0); } diff --git a/src/glob.c b/src/ms_glob.c index 2d544da..2ab62a3 100644 --- a/src/glob.c +++ b/src/ms_glob.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* glob.c :+: :+: :+: */ +/* ms_glob.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/05 11:44:07 by charles #+# #+# */ -/* Updated: 2020/04/05 13:21:25 by charles ### ########.fr */ +/* Updated: 2020/06/09 16:20:22 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -147,7 +147,7 @@ char *ms_glob(char *pattern) return (NULL); ft_vecsort(matches, ft_compar_str); if (ft_vecpush(matches, NULL) == NULL || - (join = ft_strsjoin((char**)matches->data, "\n")) == NULL) + (join = ft_strsjoin((char**)matches->data, " ")) == NULL) { ft_vecdestroy(matches, free); return (NULL); diff --git a/src/parse/parse.c b/src/parse/parse.c index 4c8d279..a0da5d9 100644 --- a/src/parse/parse.c +++ b/src/parse/parse.c @@ -7,10 +7,12 @@ t_ast *parse(char **input) { - int i = 0; + /* int i = 0; */ - while (input[i] != '\0') - { - - } + (void)input; + /* while (input[i] != '\0') */ + /* { */ + /* */ + /* } */ + return NULL; } diff --git a/src/preprocess.c b/src/preprocess.c index c30bb70..0d72a2c 100644 --- a/src/preprocess.c +++ b/src/preprocess.c @@ -6,12 +6,13 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/03 08:58:49 by charles #+# #+# */ -/* Updated: 2020/04/05 15:04:06 by charles ### ########.fr */ +/* Updated: 2020/06/09 17:03:37 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" #include "ms_glob.h" +#include "lexer.h" static char *iterpolate(char *str, t_env env) { @@ -28,11 +29,18 @@ static char *iterpolate(char *str, t_env env) if (dstr->str[i] == '$') { if ((match = env_search_first_match(env, dstr->str + i + 1)) == NULL) + { ft_dstrerase(dstr, i, utils_var_end(dstr->str + i + 1)); + i--; + } else { if (ft_dstrsubstitute(dstr, match, i, utils_var_end(dstr->str + i + 1)) == NULL) + { + ft_dstrdestroy(dstr); return (NULL); + } + i += ft_strlen(match) - 1; } } i++; @@ -40,30 +48,80 @@ static char *iterpolate(char *str, t_env env) return (ft_dstrunwrap(dstr)); } -static char *preprocess_arg(char *arg, t_env env) +static char *iterpolate_globs(char *str) +{ + char **strs; + int i; + + if ((strs = ft_split(str, ' ')) == NULL) + return (NULL); + i = 0; + while (strs[i] != NULL) + { + if (ft_strchr(strs[i], '*') != NULL + && (strs[i] = ms_glob(strs[i])) == NULL) + { + ft_split_destroy(strs); + return (NULL); + } + i++; + } + return (ft_strsjoinf(strs, " ")); +} + +static int splat_arg(t_ftvec *argv, int i) { - if (*arg == '\'') - return (ft_strsubf(arg, 1, ft_strlen(arg) - 1)); - if (*arg == '"') + t_token *splated; + char **strs; + int j; + + if ((splated = ft_vectake(argv, i)) == NULL + || (strs = ft_split(splated->content, ' ')) == NULL) + return (-1); + j = 0; + while (strs[j] != NULL) { - if (ft_strchr(arg, '$') != NULL) - arg = iterpolate(arg, env); - return (ft_strsubf(arg, 1, ft_strlen(arg) - 1)); + if (ft_vecinsert_safe(argv, i + j, token_new(LTAG_STR, strs[j])) == NULL) + { + ft_split_destroy(strs); + return (-1); + } + j++; } - if (ft_strchr(arg, '$') != NULL) - return (iterpolate(arg, env)); - if (ft_strchr(arg, '*') != NULL) - return (ms_glob(arg)); - return (arg); + ft_split_destroy(strs); + return i + j - 1; } -char **preprocess_argv(char **argv, t_env env) +void iter_func_unwrap_token(void **addr) { - int i; + char *content; + + content = (*(t_token**)addr)->content; + free(*(t_token**)addr); + *(char**)addr = content; +} + +char **preprocess_argv(t_ftvec *argv, t_env env) +{ + size_t i; + t_token *token; i = -1; - while (argv[++i] != NULL) - if ((argv[i] = preprocess_arg(argv[i], env)) == NULL) - return (NULL); - return (argv); + while (++i < argv->size) + { + token = argv->data[i]; + if (token->tag == LTAG_STR_SINGLE) + continue ; + token->content = iterpolate(token->content, env); + if (token->tag == LTAG_STR) + { + if (ft_strchr(token->content, '*') != NULL) + token->content = iterpolate_globs(token->content); + if ((i = splat_arg(argv, i)) == (size_t)-1) + return (NULL); + } + } + ft_veciter_addr(argv, iter_func_unwrap_token); + ft_vecpush(argv, NULL); + return ((char**)ft_vecunwrap(argv)); } diff --git a/src/utils.c b/src/utils.c index dd89457..3449c25 100644 --- a/src/utils.c +++ b/src/utils.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 11:56:31 by cacharle #+# #+# */ -/* Updated: 2020/04/05 14:51:52 by charles ### ########.fr */ +/* Updated: 2020/06/09 15:49:39 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -59,5 +59,5 @@ size_t utils_var_end(char *name) i = 0; while (ft_isalnum(name[i]) || name[i] == '_') i++; - return (i); + return (i + 1); } |
