diff options
| -rw-r--r-- | .DS_Store | bin | 6148 -> 0 bytes | |||
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | .travis.yml | 2 | ||||
| -rw-r--r-- | Makefile | 15 | ||||
| -rw-r--r-- | README.md | 2 | ||||
| -rw-r--r-- | include/ast.h | 10 | ||||
| -rw-r--r-- | include/lexer.h | 43 | ||||
| -rw-r--r-- | include/minishell.h | 4 | ||||
| -rw-r--r-- | include/ms_glob.h | 3 | ||||
| -rw-r--r-- | include/parse.h | 6 | ||||
| -rw-r--r-- | print_argv_env_main.c | 12 | ||||
| -rw-r--r-- | src/.DS_Store | bin | 6148 -> 0 bytes | |||
| -rw-r--r-- | src/env.c | 16 | ||||
| -rw-r--r-- | src/eval/eval.c | 5 | ||||
| -rw-r--r-- | src/lexer/.DS_Store | bin | 6148 -> 0 bytes | |||
| -rw-r--r-- | src/lexer/lexer.c | 37 | ||||
| -rw-r--r-- | src/lexer/lexer_count_nb_element.c | 31 | ||||
| -rw-r--r-- | src/lexer/lexer_len_element_and_mall.c | 58 | ||||
| -rw-r--r-- | src/lexer/lexer_utils.c | 10 | ||||
| -rw-r--r-- | src/lexer/main.c | 29 | ||||
| -rw-r--r-- | src/lexer/token.c | 35 | ||||
| -rw-r--r-- | src/main.c | 57 | ||||
| -rw-r--r-- | src/ms_glob.c (renamed from src/glob.c) | 19 | ||||
| -rw-r--r-- | src/parse/lexer.c | 26 | ||||
| -rw-r--r-- | src/parse/parse.c | 18 | ||||
| -rw-r--r-- | src/preprocess.c | 141 | ||||
| -rw-r--r-- | src/utils.c | 4 | ||||
| -rwxr-xr-x | test.sh | 14 |
28 files changed, 447 insertions, 152 deletions
diff --git a/.DS_Store b/.DS_Store Binary files differdeleted file mode 100644 index 0a19ec0..0000000 --- a/.DS_Store +++ /dev/null @@ -5,3 +5,5 @@ minishell tags doc/* tmp/* +.DS_Store +a.out diff --git a/.travis.yml b/.travis.yml index 039216a..466a76c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,3 @@ language: c -script: make all && make test +script: make all @@ -6,18 +6,17 @@ # By: cacharle <marvin@42.fr> +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/02/03 04:14:24 by cacharle #+# #+# # -# Updated: 2020/04/03 13:50:17 by charles ### ########.fr # +# Updated: 2020/06/12 09:17:28 by charles ### ########.fr # # # # **************************************************************************** # RM = rm -f -MAKE = make +MAKE = make --no-print-directory +JOBS = 4 DOXYGEN = doxygen DOXYGEN_FILE = Doxyfile DOC_DIR = doc -TESTEXEC = test.sh - LIBFTDIR = libft INCLUDEDIR = include SRCDIR = src @@ -37,11 +36,11 @@ LDFLAGS = -L$(LIBFTDIR) -lft NAME = minishell .PHONY: all -all: libft_all prebuild $(NAME) +all: libft_all prebuild + @$(MAKE) -j$(JOBS) allnopre -.PHONY: test -test: - @./$(TESTEXEC) +.PHONY: allnopre +allnopre: $(NAME) .PHONY: prebuild prebuild: @@ -1,4 +1,4 @@ -# minishell +# minishell [](https://travis-ci.com/HappyTramp/minishell) minishell project of school 42 diff --git a/include/ast.h b/include/ast.h index b725c8b..e63ab22 100644 --- a/include/ast.h +++ b/include/ast.h @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:05:38 by charles #+# #+# */ -/* Updated: 2020/05/04 11:59:43 by charles ### ########.fr */ +/* Updated: 2020/06/09 11:44:45 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -66,9 +66,9 @@ typedef struct s_line typedef struct s_cmd { - char **argv; - char *in; - char *out; + char **argv; // change to t_ftvec of t_token + char *in; // change to t_token + char *out; // change to t_token bool is_append; } t_cmd; @@ -98,7 +98,7 @@ typedef struct s_ast { t_line line; t_cmd cmd; - } ; + }; } t_ast; t_ast *ast_new(t_ast_tag tag, void *data); diff --git a/include/lexer.h b/include/lexer.h index 694b09d..90f9353 100644 --- a/include/lexer.h +++ b/include/lexer.h @@ -1,32 +1,32 @@ +#ifndef LEXER_H +# define LEXER_H -# include <stdio.h> # include <stdlib.h> # include "libft_lst.h" # include "libft_str.h" - +# include "minishell.h" enum e_token_tag { - LTAG_AND = 1 << 0, - LTAG_END = 1 << 1, - LTAG_OR = 1 << 2, - LTAG_PIPE = 1 << 3, - LTAG_REDIR_IN = 1 << 4, - LTAG_REDIR_OUT = 1 << 5, - LTAG_REDIR_APPEND = 1 << 6, - LTAG_PARENT_OPEN = 1 << 7, - LTAG_PARENT_CLOSE = 1 << 8, - LTAG_STR = 1 << 9, - LTAG_STR_DOUBLE = 1 << 10, - LTAG_STR_SINGLE = 1 << 11, - LTAG_STICK = 1 << 12, + LTAG_AND = 1 << 0, + LTAG_END = 1 << 1, + LTAG_OR = 1 << 2, + LTAG_PIPE = 1 << 3, + LTAG_REDIR_IN = 1 << 4, + LTAG_REDIR_OUT = 1 << 5, + LTAG_REDIR_APPEND = 1 << 6, + LTAG_PARENT_OPEN = 1 << 7, + LTAG_PARENT_CLOSE = 1 << 8, + LTAG_STR = 1 << 9, + LTAG_STR_DOUBLE = 1 << 10, + LTAG_STR_SINGLE = 1 << 11, + LTAG_STICK = 1 << 12, }; -typedef struct s_token +typedef struct { - enum e_token_tag token; - char *value; - + enum e_token_tag tag; + char *content; } t_token; t_ftlst *lexer(char *input); @@ -36,3 +36,8 @@ enum e_token_tag ret_token_sep_redir_append(char *input, int i); int lexer_sep(char input); int lexer_verif_entre_cote(char *input, int i); int lexe_space(char *input); + +t_token *token_new(enum e_token_tag tag, char *content); +void token_destroy(t_token *token); + +#endif diff --git a/include/minishell.h b/include/minishell.h index 9551c1f..2afbed9 100644 --- a/include/minishell.h +++ b/include/minishell.h @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/26 15:33:51 by cacharle #+# #+# */ -/* Updated: 2020/04/05 14:52:20 by charles ### ########.fr */ +/* Updated: 2020/06/12 11:57:36 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -109,6 +109,6 @@ int builtin_exit(char **argv, t_env env); ** preprocess.c */ -char *preprocess(char *input, t_env env); +char **preprocess(t_ftvec *argv, t_env env); #endif diff --git a/include/ms_glob.h b/include/ms_glob.h index a5c21bd..5b5c932 100644 --- a/include/ms_glob.h +++ b/include/ms_glob.h @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/05 11:45:11 by charles #+# #+# */ -/* Updated: 2020/04/05 13:05:10 by charles ### ########.fr */ +/* Updated: 2020/06/09 17:51:34 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,5 +29,6 @@ struct s_glob_param t_ftvec *glob_matches(char *pattern); char *ms_glob(char *pattern); +char *ms_globf(char *pattern); #endif diff --git a/include/parse.h b/include/parse.h index bc86230..ec95ef9 100644 --- a/include/parse.h +++ b/include/parse.h @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 09:00:00 by cacharle #+# #+# */ -/* Updated: 2020/04/01 17:49:45 by charles ### ########.fr */ +/* Updated: 2020/06/13 11:59:47 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,12 +37,12 @@ ** lexer.c */ -char **lexer(char *input); +// char **lexer(char *input); /* ** parse.c */ -t_ast *parse(char *input); +t_ast *parse(char **input); #endif diff --git a/print_argv_env_main.c b/print_argv_env_main.c new file mode 100644 index 0000000..dcc6e0e --- /dev/null +++ b/print_argv_env_main.c @@ -0,0 +1,12 @@ +#include <stdio.h> + +int main(int argc, char **argv, char **envp) +{ + printf("ARGV:\n"); + for (int i = 0; i < argc; i++) + printf("[%d] %s\n", i, argv[i]); + printf("\nENV:\n"); + for (int i = 0; envp[i] != NULL && i < 10; i++) + printf("[%d] %s\n", i, envp[i]); + return 0; +} diff --git a/src/.DS_Store b/src/.DS_Store Binary files differdeleted file mode 100644 index 68abd32..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/04/05 14:42:38 by charles ### ########.fr */ +/* Updated: 2020/06/12 10:51:10 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -67,19 +67,25 @@ char *env_search(t_env env, char *key) char *env_search_first_match(t_env env, const char *haystack) { - int len; + size_t len; size_t i; + size_t key_len; if (ft_isdigit(*haystack)) return (NULL); len = 0; while (ft_isalnum(haystack[len]) || haystack[len] == '_') len++; - while (i < env->size) + if (len == 0) + return (NULL); + i = -1; + while (++i < env->size - 1) { + key_len = ft_strchr(env->data[i], '=') - (char*)env->data[i]; + if (len != key_len) + continue ; if (ft_strncmp((char*)env->data[i], haystack, len) == 0) - return (ft_strchr((char*)env->data[i], '=') + 1); - i++; + return (ft_strchr(env->data[i], '=') + 1); } return (NULL); } diff --git a/src/eval/eval.c b/src/eval/eval.c index 0270024..c4df1c9 100644 --- a/src/eval/eval.c +++ b/src/eval/eval.c @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:05:21 by charles #+# #+# */ -/* Updated: 2020/05/04 12:00:38 by charles ### ########.fr */ +/* Updated: 2020/05/15 00:12:51 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -89,7 +89,7 @@ static int eval_cmd(int fd_in, int fd_out, t_eval_state *state, t_cmd *cmd) if (cmd->in != NULL && (fd_in = open(cmd->in, O_RDONLY)) == -1) return (-1); if (cmd->out != NULL && (fd_out = open(cmd->out, - (cmd->is_append ? O_APPEND : O_RDONLY) | O_CREAT)) == -1) + (cmd->is_append ? O_APPEND : O_WRONLY) | O_CREAT)) == -1) return (-1); param.argv = cmd->argv; param.envp = (char**)state->env->data; @@ -102,6 +102,7 @@ static int eval_cmd(int fd_in, int fd_out, t_eval_state *state, t_cmd *cmd) ** \param line Line to evaluate ** \return Last Executed command status or -1 on error */ + static int eval_line(void *param) { int status; diff --git a/src/lexer/.DS_Store b/src/lexer/.DS_Store Binary files differdeleted file mode 100644 index fcaf8ef..0000000 --- a/src/lexer/.DS_Store +++ /dev/null diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c index 185ff83..92cd34a 100644 --- a/src/lexer/lexer.c +++ b/src/lexer/lexer.c @@ -1,4 +1,3 @@ - #include "lexer.h" int len_is_not_sep(char *input) @@ -76,16 +75,16 @@ t_token *lexer_lst_token_str(char *input, int i, int j) if (!(lst_token = malloc(sizeof(t_token) * 1))) return (NULL); - lst_token->token = 0; - lst_token->value = NULL; - if (!(lst_token->value = malloc(sizeof(char) * j + 1))) + lst_token->tag = 0; + lst_token->content = NULL; + if (!(lst_token->content = malloc(sizeof(char) * j + 1))) return(0); - if (!(ft_strlcpy(lst_token->value, &input[i], j + 1))) + if (!(ft_strlcpy(lst_token->content, &input[i], j + 1))) { free(lst_token); return(0); } - //printf("%s-\n", lst_token->value); + //printf("%s-\n", lst_token->content); return (lst_token); } @@ -93,11 +92,11 @@ enum e_token_tag token_verif_stick(t_token *lst_token) { int i; - i = ft_strlen(lst_token->value); + i = ft_strlen(lst_token->content); - if (lst_token->value[i - 1] == ' ') - return(lst_token->token); - return(lst_token->token | LTAG_STICK); + if (lst_token->content[i - 1] == ' ') + return(lst_token->tag); + return(lst_token->tag | LTAG_STICK); } enum e_token_tag token_str_or_cote(t_token *lst_token) @@ -105,21 +104,21 @@ enum e_token_tag token_str_or_cote(t_token *lst_token) int i; i = 0; - while(lst_token->value[i] != '\0') + while(lst_token->content[i] != '\0') { - if(lst_token->value[i] == '\'') + if(lst_token->content[i] == '\'') { - lst_token->token = LTAG_STR_SINGLE; + lst_token->tag = LTAG_STR_SINGLE; return(token_verif_stick(lst_token)); } - if(lst_token->value[i] == '"') + if(lst_token->content[i] == '"') { - lst_token->token = LTAG_STR_DOUBLE; + lst_token->tag = LTAG_STR_DOUBLE; return(token_verif_stick(lst_token)); } else { - lst_token->token = LTAG_STR; + lst_token->tag = LTAG_STR; return(token_verif_stick(lst_token)); } i++; @@ -131,12 +130,12 @@ t_token *push_token_enum_and_trim(t_token *lst_token) { enum e_token_tag tk; - tk = ret_token(lst_token->value, 0); + tk = ret_token(lst_token->content, 0); if (tk == 0) { - lst_token->token = token_str_or_cote(lst_token); + lst_token->tag = token_str_or_cote(lst_token); } - printf("%s-, %d\n",lst_token->value, (int)lst_token->token); + /* printf("%s-, %d\n",lst_token->content, (int)lst_token->tag); */ return (lst_token); } diff --git a/src/lexer/lexer_count_nb_element.c b/src/lexer/lexer_count_nb_element.c new file mode 100644 index 0000000..1455d5e --- /dev/null +++ b/src/lexer/lexer_count_nb_element.c @@ -0,0 +1,31 @@ + +#include "lexer.h" + +int lexer_count_nb_element(char *input) +{ + int i; + int j; + + i = 0; + j = 1; + while(input[i] != '\0') + { + if (lexer_sep(input[i]) || input[i] == '"' || input[i] == '\'') + { + if (input[i] == '"' || input[i] == '\'') + { + i = lexer_verif_entre_cote(input,i); + j++; + } + if (lexer_sep(input[i])) + { + while (lexer_sep(input[i]) || input[i] == ' ') + ++i; + j++; + } + j++; + } + i++; + } + return (j); +} diff --git a/src/lexer/lexer_len_element_and_mall.c b/src/lexer/lexer_len_element_and_mall.c new file mode 100644 index 0000000..e0e1132 --- /dev/null +++ b/src/lexer/lexer_len_element_and_mall.c @@ -0,0 +1,58 @@ + +#include "lexer.h" + +int lexer_count_len_element(char *input, int i) +{ + int j = -1; + if (input[i] == '"' || input[i] == '\'') + { + return(j = lexer_verif_entre_cote(input,i)); + } + else if (lexer_sep(input[i]) || input[i] == ' ') + { + while(lexer_sep(input[i]) || input[i] == ' ') + { + if(input[j] == '\0') + return(j); + ++i; + ++j; + } + } + else + { + while (!lexer_sep(input[i]) && input[i] != ' ') + { + if(input[j] == '\0') + return(j); + ++i; + ++j; + } + } + ++j; + return(j); +} + +char **lexer_malloc_len_elem(char *input, int i, char **out) +{ + int j = 0; + int k = 0; + int temp = 0; + + (void)i; + j += lexer_count_len_element(&input[j], 0); + k = j; + out[temp] = malloc(sizeof(char) * k); + ft_strlcpy(out[temp], input, k + 1); + while (input[j] != '\0') + { + temp++; + j += lexer_count_len_element(&input[j], 0); + k -= j; + if (k < 0) + k *= -1; + out[temp] = malloc(sizeof(char) * j - k + 1); + ft_strlcpy(out[temp], &input[j - k], k + 1); + k = j; + } + return(out); +} diff --git a/src/lexer/lexer_utils.c b/src/lexer/lexer_utils.c index 72d8288..986db50 100644 --- a/src/lexer/lexer_utils.c +++ b/src/lexer/lexer_utils.c @@ -1,8 +1,5 @@ - #include "lexer.h" - - enum e_token_tag ret_token_sep_redir_append(char *input, int i) { if (input[i + 1] == '>') @@ -33,8 +30,7 @@ enum e_token_tag ret_token(char *input, int i) } - -int lexer_sep(char input) +int lexer_sep(char input) { char *sep; int i; @@ -50,7 +46,7 @@ int lexer_sep(char input) return (0); } -int lexe_space(char *input) +int lexe_space(char *input) { int i; @@ -75,7 +71,7 @@ static int lex_verif_simple_cote(char *input, int i) return(i + 1); } -int lexer_verif_entre_cote(char *input, int i) +int lexer_verif_entre_cote(char *input, int i) { if(input[i] == '\'') return(lex_verif_simple_cote(input, i)); diff --git a/src/lexer/main.c b/src/lexer/main.c deleted file mode 100644 index 753969d..0000000 --- a/src/lexer/main.c +++ /dev/null @@ -1,29 +0,0 @@ - -#include "lexer.h" - -/*int main(int argc, char **argv) -{ - char *input; - - if (!(input = malloc(sizeof(char) * ft_strlen(argv[1]) + 1))) - return(0); - ft_strlcpy(input, argv[1], ft_strlen(argv[1]) + 1); - - lexer(input); - free(input); - exit(0); - return (0); -}*/ -/* -#include <stdio.h> -int main(int argc, char **argv, char **envp) -{ - printf("ARGV:\n"); - for (int i = 0; i < argc; i++) - printf("[%d] %s\n", i, argv[i]); - printf("\nENV:\n"); - for (int i = 0; envp[i] != NULL && i < 10; i++) - printf("[%d] %s\n", i, envp[i]); - return 0; -} -*/ diff --git a/src/lexer/token.c b/src/lexer/token.c new file mode 100644 index 0000000..6c6a184 --- /dev/null +++ b/src/lexer/token.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* token.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/06/09 13:38:08 by charles #+# #+# */ +/* Updated: 2020/06/09 17:55:23 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "lexer.h" + +t_token *token_new(enum e_token_tag tag, char *content) +{ + t_token *token; + + if (content == NULL + || (token = (t_token*)malloc(sizeof(t_token))) == NULL) + return (NULL); + if ((token->content = ft_strdup(content)) == NULL) + { + free(token); + return (NULL); + } + token->tag = tag; + return token; +} + +void token_destroy(t_token *token) +{ + free(token->content); + free(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/13 11:56:27 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,57 @@ 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")); + + ft_vecpush(v, token_new(LTAG_STR, "\\$TERM")); + ft_vecpush(v, token_new(LTAG_STR, "$TER\\M")); + ft_vecpush(v, token_new(LTAG_STR, "\\\\")); + ft_vecpush(v, token_new(LTAG_STR_SINGLE, "''''$TEST\\TEST")); + ft_vecpush(v, token_new(LTAG_STR_DOUBLE, ",$TEST,$B,")); + + ft_vecpush(v, token_new(LTAG_STR_DOUBLE | LTAG_STICK, "$TEST")); + ft_vecpush(v, token_new(LTAG_STR_DOUBLE | LTAG_STICK, "$TEST")); + ft_vecpush(v, token_new(LTAG_STR_DOUBLE , "$TEST")); + ft_vecpush(v, token_new(LTAG_STR_DOUBLE | LTAG_STICK, "$TEST")); + ft_vecpush(v, token_new(LTAG_STR_SINGLE, "$TEST")); + + char **as = preprocess(v, env); + char **tmp = as; + + while (*as != NULL) + puts(*as++); + + ft_split_destroy(tmp); ft_vecdestroy(env, free); return (0); } + + +///////////////////////////////////////////////////////////////////////////////////////// +// lexer main +///////////////////////////////////////////////////////////////////////////////////////// + +#include "lexer.h" + +/* int main(void) */ +/* { */ +/* char **out; */ +/* int i = -1; */ +/* */ +/* out = lexer("\"echo\" \"* login_x\"<<AUTHORS; echo && ; salut;\"echo\"\"* login_x\""); */ +/* //out = lexer("echo * login_x << AUTHORS&&cd Desktop"); */ +/* while(out[++i]) */ +/* printf("%s\n", out[i]);; */ +/* exit(0); */ +/* return (0); */ +/* } */ diff --git a/src/glob.c b/src/ms_glob.c index d1d1bdf..39c537c 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/12 11:51:41 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -148,7 +148,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); @@ -161,3 +161,16 @@ char *ms_glob(char *pattern) } return (join); } + +/* +** \brief Wrapper around `ms_glob` which free `pattern` for convinience +*/ + +char *ms_globf(char *pattern) +{ + char *ret; + + ret = ms_glob(pattern); + free(pattern); + return (ret); +} diff --git a/src/parse/lexer.c b/src/parse/lexer.c new file mode 100644 index 0000000..2aa8a6f --- /dev/null +++ b/src/parse/lexer.c @@ -0,0 +1,26 @@ +/* +** \file lexer.c +** \brief Lexer +*/ + +#include "minishell.h" + +/* static char **lex_len(char *input) */ +/* { */ +/* int i; */ +/* */ +/* i = 0; */ +/* while(input[i] != '\0') */ +/* { */ +/* lex_comp_cmd(input); */ +/* i++; */ +/* } */ +/* } */ + +/* char **lexer(char *input) */ +/* { */ +/* if (!input) */ +/* return (NULL); */ +/* lex_len(input); */ +/* return (NULL); */ +/* } */ diff --git a/src/parse/parse.c b/src/parse/parse.c new file mode 100644 index 0000000..a0da5d9 --- /dev/null +++ b/src/parse/parse.c @@ -0,0 +1,18 @@ +/* +** \file parse.c +** \brief Parser +*/ + +#include "parse.h" + +t_ast *parse(char **input) +{ + /* int i = 0; */ + + (void)input; + /* while (input[i] != '\0') */ + /* { */ + /* */ + /* } */ + return NULL; +} diff --git a/src/preprocess.c b/src/preprocess.c index c30bb70..1034068 100644 --- a/src/preprocess.c +++ b/src/preprocess.c @@ -6,64 +6,151 @@ /* 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/12 11:57:17 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" #include "ms_glob.h" +#include "lexer.h" -static char *iterpolate(char *str, t_env env) +static bool st_escapable(char c, enum e_token_tag tag) +{ + if (tag & LTAG_STR) + return (true); + if ((tag & LTAG_STR_DOUBLE) && (c == '\\' || c == '"' || c == '$')) + return (true); + return (false); +} + +static char *st_iterpolate_env(char *str, enum e_token_tag tag, t_env env) { size_t i; t_ftdstr *dstr; char *match; - if ((dstr = ft_dstrnew(str)) == NULL) + if ((dstr = ft_dstrwrap(str)) == NULL) return (NULL); - free(str); - i = 0; - while (i < dstr->length) - { - if (dstr->str[i] == '$') + i = -1; + while (++i < dstr->length) + if (dstr->str[i] == '\\' && st_escapable(dstr->str[i + 1], tag)) + ft_dstrerase(dstr, i, 1); + else 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 |
