diff --git a/42sh/includes/glob.h b/42sh/includes/glob.h index 6b8d2475..943ab15a 100644 --- a/42sh/includes/glob.h +++ b/42sh/includes/glob.h @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/04 16:31:18 by wescande #+# #+# */ -/* Updated: 2017/01/24 21:22:58 by wescande ### ########.fr */ +/* Updated: 2017/01/27 19:13:15 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,14 +22,23 @@ typedef struct s_ld struct s_ld *prev; } t_ld; -char **glob(const char *str, char **env); -t_ld *expand_brace(const char *pat); +typedef struct s_glob +{ + const char *pat; + const char *esc; + t_ld *match; + t_ld *m_pat; +} t_glob; + +char **glob(const char *str, const char *esc, char **env); +void expand_brace(t_glob *tglob); void glob_print(t_list *token, t_data *data); -int match_pattern(const char *pat, char *str, - char *full_word, t_ld **match); -void dir_research(const char *pat, char *path, t_ld **match); -void dir_research_recursive(const char *pat, char *p, t_ld **match); -bool is_directory(const char *path); +int match_pattern(t_glob *tglob, char *str, char *full_word); +void dir_research(t_glob *tglob, char *path); +void dir_research_recursive(t_glob *tglob, char *p); +int is_directory(const char *path); + +int is_char_esc(const char *esc, const char *ini_str, const char *str_pos); /* ** LIST D: diff --git a/42sh/includes/lexer.h b/42sh/includes/lexer.h index 03276719..e1dcf24f 100644 --- a/42sh/includes/lexer.h +++ b/42sh/includes/lexer.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */ -/* Updated: 2017/01/12 14:57:41 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 15:52:11 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -63,6 +63,7 @@ struct s_token { t_type type; char *data; + char *esc; int size; }; @@ -75,7 +76,7 @@ extern int (*g_lexer[])(t_list **alst, char *str); t_token *token_init(); int ft_tokenize(t_list **alst, char *str, t_lexstate state); int ft_post_tokenize(t_list **alst, char *str); -int token_append(t_token *token, char c); +int token_append(t_token *token, char c, short int esc); void token_free(void *data, size_t size); int token_cmp_type(t_token *token, t_type *ref); void token_print(t_list *lst); diff --git a/42sh/src/glob/dir_glob.c b/42sh/src/glob/dir_glob.c index c4bdb128..804c9465 100644 --- a/42sh/src/glob/dir_glob.c +++ b/42sh/src/glob/dir_glob.c @@ -6,13 +6,13 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/04 16:29:54 by wescande #+# #+# */ -/* Updated: 2017/01/24 21:16:42 by wescande ### ########.fr */ +/* Updated: 2017/01/27 18:32:54 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ #include "glob.h" -bool is_directory(const char *path) +int is_directory(const char *path) { struct stat path_stat; @@ -20,7 +20,7 @@ bool is_directory(const char *path) return (S_ISDIR(path_stat.st_mode)); } -void dir_research(const char *pat, char *p, t_ld **match) +void dir_research(t_glob *gl, char *p) { DIR *dir; struct dirent *in; @@ -38,8 +38,8 @@ void dir_research(const char *pat, char *p, t_ld **match) path_tmp = ft_strjoin(p, in->d_name); else path_tmp = ft_strjoinf(ft_strjoin(p, "/"), in->d_name, 1); - if (match_pattern(pat, in->d_name, path_tmp, match)) - ft_ld_pushfront(match, ft_strdup(path_tmp + 2 * + if (match_pattern(gl, in->d_name, path_tmp)) + ft_ld_pushfront(&gl->match, ft_strdup(path_tmp + 2 * (path_tmp[0] == '.' && path_tmp[1] == '/'))); ft_strdel(&path_tmp); } @@ -47,7 +47,7 @@ void dir_research(const char *pat, char *p, t_ld **match) } } -void dir_research_recursive(const char *pat, char *p, t_ld **match) +void dir_research_recursive(t_glob *gl, char *p) { DIR *dir; struct dirent *in; @@ -66,9 +66,9 @@ void dir_research_recursive(const char *pat, char *p, t_ld **match) else path_tmp = ft_strjoinf(ft_strjoin(p, "/"), in->d_name, 1); if (is_directory(path_tmp)) - dir_research_recursive(pat, path_tmp, match); - if (match_pattern(pat, in->d_name, path_tmp, match)) - ft_ld_pushfront(match, ft_strdup(path_tmp + 2 * + dir_research_recursive(gl, path_tmp); + if (match_pattern(gl, in->d_name, path_tmp)) + ft_ld_pushfront(&gl->match, ft_strdup(path_tmp + 2 * (path_tmp[0] == '.' && path_tmp[1] == '/'))); ft_strdel(&path_tmp); } diff --git a/42sh/src/glob/expand_brace.c b/42sh/src/glob/expand_brace.c index 501b7ac9..8db42a27 100644 --- a/42sh/src/glob/expand_brace.c +++ b/42sh/src/glob/expand_brace.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/12 19:00:29 by wescande #+# #+# */ -/* Updated: 2017/01/24 19:15:35 by wescande ### ########.fr */ +/* Updated: 2017/01/27 20:12:12 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,43 @@ ** -char *pat -> pattern string to be looking for expand */ -static int search_brace(t_ld **wk, char *str, int index) +static char **generate_tab(const char *pat, const char *esc, int dup) +{ + char **my_tab; + + if (!(my_tab = (char **)malloc(sizeof(char*) * 3))) + return (NULL); + if (dup) + { + my_tab[0] = ft_strdup(pat); + my_tab[1] = ft_strdup(esc); + } + else + { + my_tab[0] = (char *)pat; + my_tab[1] = (char *)esc; + } + my_tab[2] = NULL; + return (my_tab); +} + +static char *calc_expand_esc(const char *esc, + int nb_start, int nb_middle, int nb_end) +{ + char *new_esc; + int index; + + new_esc = ft_memalloc(sizeof(char) * (nb_start + nb_middle + nb_end) / 8); + index = -1; + while (++index < nb_start) + new_esc[index / 8] |= (esc[index / 8] >> index % 8) & 1 << index % 8; + //copy the nb_start first bit of esc. + //append nb_middle bit at status 0 + //append nb_end last bit from end of esc. + return (new_esc); +} + +static int search_brace(t_ld **wk, char *str, char *esc, int index) { char *start; char *s1; @@ -29,17 +65,17 @@ static int search_brace(t_ld **wk, char *str, int index) start = NULL; while (*str) { - if (*str == '{') - start = str; - else if (*str == '}' && start) + start = *str == '{' ? str : start; + if (*str == '}' && start) { s1 = ft_strsub(start, 1, str - start - 1); split = ft_strsplit(s1, ','); ft_strdel(&s1); s1 = ft_strsub((*wk)->content, 0, start - (char *)(*wk)->content); while (split[++index]) - ft_ld_pushfront(wk, ft_strjoinf(ft_strjoin(s1, split[index]), - str + 1, 1)); + ft_ld_pushfront(wk, generate_tab(ft_strjoinf(ft_strjoin(s1, + split[index]), str + 1, 1), calc_expand_esc(esc, + ft_strlen(s1), ft_strlen(split[index]), ft_strlen(str +1)), 0)); ft_strdel(&s1); ft_tabdel(&split); return (1); @@ -49,29 +85,27 @@ static int search_brace(t_ld **wk, char *str, int index) return (0); } -t_ld *expand_brace(const char *pat) +void expand_brace(t_glob *gl) { - t_ld *ret; t_ld *tmp; int do_it; - ret = NULL; - ft_ld_pushfront(&ret, ft_strdup("")); - ft_ld_pushfront(&ret, ft_strdup(pat)); + ft_ld_pushfront(&gl->m_pat, generate_tab("", "", 1)); + ft_ld_pushfront(&gl->m_pat, generate_tab(gl->pat, gl->esc, 1)); do_it = 1; while (do_it) { do_it = 0; - while (ret->next) + while (gl->m_pat->next) { - if ((tmp = ret) && search_brace(&ret, ret->content, -1)) + if ((tmp = gl->m_pat) && search_brace(&gl->m_pat, + ((char **)gl->m_pat->content)[0],((char **)gl->m_pat->content)[1], -1)) { - ft_ld_del(&tmp, &ft_strdel); + ft_ld_del(&tmp, &ft_tabdel); do_it = 1; } - ret = ret->next; + gl->m_pat = gl->m_pat->next; } - ret = ft_ld_front(ret); + gl->m_pat = ft_ld_front(gl->m_pat); } - return (ret); } diff --git a/42sh/src/glob/glob.c b/42sh/src/glob/glob.c index 320b15eb..de095b22 100644 --- a/42sh/src/glob/glob.c +++ b/42sh/src/glob/glob.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/04 16:29:54 by wescande #+# #+# */ -/* Updated: 2017/01/26 17:18:20 by wescande ### ########.fr */ +/* Updated: 2017/01/27 19:05:38 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,13 +21,13 @@ ** to just expanse in local directory and not in path */ -static void path_research(const char *pat, char **path, t_ld **match) +static void path_research(t_glob *tglob, char **path) { int i; i = -1; while (path[++i]) - dir_research(pat, path[i], match); + dir_research(tglob, path[i]); } static char **treat_glob(t_ld **match) @@ -42,30 +42,54 @@ static char **treat_glob(t_ld **match) return (gl); } -char **glob(const char *pat, char **env) +static void add_simple_pat(t_glob *gl) { - t_ld *match; - char **path; - t_ld *mul_pat; + char *str; + int start; - match = NULL; - mul_pat = expand_brace(pat); - while (mul_pat->next) + str = ((char **)gl->m_pat->content)[0]; + start = 0; + while (*str) { - if (!(((char *)mul_pat->content)[0] == '/' - || (((char *)mul_pat->content)[0] == '.' - && ((char *)mul_pat->content)[1] == '/')) + if (!is_char_esc(((char **)gl->m_pat->content)[1], + ((char **)gl->m_pat->content)[0], str)) + { + if (*str == '[') + start = 1; + else if (*str == ']' && start == 1) + return ; + else if (*str == '*' || *str == '?') + return ; + } + ++str; + } + ft_ld_pushfront(&gl->match, ((char **)gl->m_pat->content)[0]); +} + +char **glob(const char *pat, const char *esc, char **env) +{ + t_glob tglob; + char **path; + + tglob = (t_glob){pat, esc, NULL, NULL}; + expand_brace(&tglob, tglob.pat); + while (tglob.m_pat->next) + { + add_simple_pat(&tglob); + if (!(((char **)tglob.m_pat->content)[0][0] == '/' + || (((char **)tglob.m_pat->content)[0][0] == '.' + && ((char **)tglob.m_pat->content)[0][1] == '/')) && env && (path = ft_strsplit(ft_getenv(env, "PATH"), ':'))) { - path_research(mul_pat->content, path, &match); + path_research(&tglob, path); ft_tabdel(&path); } - if (((char *)mul_pat->content)[0] != '/') - dir_research(mul_pat->content, ".", &match); + if (((char **)tglob.m_pat->content)[0][0] != '/') + dir_research(&tglob, "."); else - dir_research(mul_pat->content + 1, "/", &match); - mul_pat = mul_pat->next; + dir_research(&tglob, "/"); + tglob.m_pat = tglob.m_pat->next; } - ft_ld_clear(&mul_pat, &ft_strdel); - return (treat_glob(&match)); + ft_ld_clear(&tglob.m_pat, &ft_strdel); + return (treat_glob(&tglob.match)); } diff --git a/42sh/src/glob/glob_print.c b/42sh/src/glob/glob_print.c index 38a8bdc0..ce0cd349 100644 --- a/42sh/src/glob/glob_print.c +++ b/42sh/src/glob/glob_print.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/05 16:09:40 by wescande #+# #+# */ -/* Updated: 2017/01/26 17:14:32 by wescande ### ########.fr */ +/* Updated: 2017/01/27 18:29:32 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,6 +26,20 @@ void ft_tabprint_fd(char **mytab, int fd) } } +void print_esc(t_token *token) +{ + char *str = token->data; + while (*str) + { + if (is_char_esc(token->esc, token->data, str)) + ft_dprintf(3, "\\%c", *str); + else + ft_dprintf(3, "%c", *str); + ++str; + } + ft_dprintf(3, "\n"); +} + void glob_print(t_list *lst, t_data *data) { t_token *token; @@ -40,7 +54,8 @@ void glob_print(t_list *lst, t_data *data) type = token->type; while (type >> (i++ + 2)) ; - glob_ret = glob(token->data, data->env); + glob_ret = glob(token->data, token->esc, data->env); + print_esc(token); DG("%02i '%s'", i, token->data); ft_tabprint_fd(glob_ret, 3); lst = lst->next; diff --git a/42sh/src/glob/is_char_esc.c b/42sh/src/glob/is_char_esc.c new file mode 100644 index 00000000..ba85450c --- /dev/null +++ b/42sh/src/glob/is_char_esc.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* is_char_esc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/27 18:19:55 by wescande #+# #+# */ +/* Updated: 2017/01/27 18:23:22 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +int is_char_esc(const char *esc, const char *ini_str, const char *str_pos) +{ + int pos; + + pos = str_pos - ini_str; + if ((esc[pos / 8] >> pos % 8) & 1) + return (1); + return (0); +} diff --git a/42sh/src/lexer/ft_tokenize.c b/42sh/src/lexer/ft_tokenize.c index 3955993f..de620b65 100644 --- a/42sh/src/lexer/ft_tokenize.c +++ b/42sh/src/lexer/ft_tokenize.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 13:37:11 by jhalford #+# #+# */ -/* Updated: 2017/01/11 16:11:02 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 15:43:46 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_backslash.c b/42sh/src/lexer/lexer_backslash.c index 9a6f2438..3a0aefff 100644 --- a/42sh/src/lexer/lexer_backslash.c +++ b/42sh/src/lexer/lexer_backslash.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 11:56:49 by jhalford #+# #+# */ -/* Updated: 2016/12/03 12:35:13 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 15:52:46 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,6 +18,6 @@ int lexer_backslash(t_list **alst, char *str) token = (*alst)->content; token->type = TK_WORD; - token_append(token, str[1]); + token_append(token, str[1], 1); return (ft_tokenize(alst, str + 2, WORD)); } diff --git a/42sh/src/lexer/lexer_default.c b/42sh/src/lexer/lexer_default.c index 04c87fca..08ccabcf 100644 --- a/42sh/src/lexer/lexer_default.c +++ b/42sh/src/lexer/lexer_default.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 18:36:21 by jhalford #+# #+# */ -/* Updated: 2017/01/11 16:10:53 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 15:53:10 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,7 +28,7 @@ int lexer_default(t_list **alst, char *str) else state = WORD; token = (*alst)->content; - token_append(token, *str); + token_append(token, *str, 0); token->type = TK_N_WORD; return (ft_tokenize(alst, str + 1, state)); } diff --git a/42sh/src/lexer/lexer_dquote.c b/42sh/src/lexer/lexer_dquote.c index d5c1b2e3..56a49ab2 100644 --- a/42sh/src/lexer/lexer_dquote.c +++ b/42sh/src/lexer/lexer_dquote.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 18:36:58 by jhalford #+# #+# */ -/* Updated: 2017/01/10 15:15:24 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 15:53:31 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,9 +23,9 @@ int lexer_dquote(t_list **alst, char *str) return (ft_tokenize(alst, str + 1, DEFAULT)); if (*str == '\\') { - token_append(token, *(str + 1)); + token_append(token, *(str + 1), 1); return (lexer_dquote(alst, str + 1)); } - token_append(token, *str); + token_append(token, *str, 1); return (lexer_dquote(alst, str)); } diff --git a/42sh/src/lexer/lexer_great.c b/42sh/src/lexer/lexer_great.c index d02a183b..82b9e4b4 100644 --- a/42sh/src/lexer/lexer_great.c +++ b/42sh/src/lexer/lexer_great.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:06:35 by jhalford #+# #+# */ -/* Updated: 2017/01/10 14:48:49 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 15:54:53 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,17 +17,17 @@ int lexer_great(t_list **alst, char *str) t_token *token; token = (*alst)->content; - token_append(token, str[0]); + token_append(token, str[0], 0); if (*(str + 1) == '&') { token->type = TK_GREATAND; - token_append(token, str[1]); + token_append(token, str[1], 0); return (lexer_greatand(alst, str + 2)); } else if (*(str + 1) == '>') { token->type = TK_DGREAT; - token_append(token, str[1]); + token_append(token, str[1], 0); return (ft_tokenize(&(*alst)->next, str + 2, DEFAULT)); } else diff --git a/42sh/src/lexer/lexer_greatand.c b/42sh/src/lexer/lexer_greatand.c index 7a32ef24..b803f0f1 100644 --- a/42sh/src/lexer/lexer_greatand.c +++ b/42sh/src/lexer/lexer_greatand.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 11:56:58 by jhalford #+# #+# */ -/* Updated: 2016/12/03 11:57:09 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 15:55:04 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,12 +20,12 @@ int lexer_greatand(t_list **alst, char *str) token->type = TK_GREATAND; if (ft_isdigit(*str)) { - token_append(token, *str); + token_append(token, *str, 0); return (lexer_greatand(alst, str + 1)); } else if (*str == '-') { - token_append(token, *str); + token_append(token, *str, 0); return (ft_tokenize(&(*alst)->next, str + 1, DEFAULT)); } return (ft_tokenize(alst, str, DEFAULT)); diff --git a/42sh/src/lexer/lexer_less.c b/42sh/src/lexer/lexer_less.c index bc3fa958..ea7c8398 100644 --- a/42sh/src/lexer/lexer_less.c +++ b/42sh/src/lexer/lexer_less.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:06:53 by jhalford #+# #+# */ -/* Updated: 2017/01/10 14:27:51 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 15:55:16 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,17 +17,17 @@ int lexer_less(t_list **alst, char *str) t_token *token; token = (*alst)->content; - token_append(token, str[0]); + token_append(token, str[0], 0); if (*(str + 1) == '&') { token->type = TK_LESSAND; - token_append(token, str[1]); + token_append(token, str[1], 0); return (lexer_lessand(alst, str + 2)); } else if (*(str + 1) == '<') { token->type = TK_DLESS; - token_append(token, str[1]); + token_append(token, str[1], 0); return (ft_tokenize(&(*alst)->next, str + 2, DEFAULT)); } else diff --git a/42sh/src/lexer/lexer_lessand.c b/42sh/src/lexer/lexer_lessand.c index 089c78bd..fc6eef56 100644 --- a/42sh/src/lexer/lexer_lessand.c +++ b/42sh/src/lexer/lexer_lessand.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 11:58:51 by jhalford #+# #+# */ -/* Updated: 2016/12/03 11:58:52 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 15:55:27 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,12 +20,12 @@ int lexer_lessand(t_list **alst, char *str) token->type = TK_LESSAND; if (ft_isdigit(*str)) { - token_append(token, *str); + token_append(token, *str, 0); return (lexer_lessand(alst, str + 1)); } else if (*str == '-') { - token_append(token, *str); + token_append(token, *str, 0); return (ft_tokenize(&(*alst)->next, str + 1, DEFAULT)); } return (ft_tokenize(alst, str, DEFAULT)); diff --git a/42sh/src/lexer/lexer_number.c b/42sh/src/lexer/lexer_number.c index b5cffe2d..6488442b 100644 --- a/42sh/src/lexer/lexer_number.c +++ b/42sh/src/lexer/lexer_number.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:06:45 by jhalford #+# #+# */ -/* Updated: 2017/01/10 14:29:46 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 15:52:58 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,7 +27,7 @@ int lexer_number(t_list **alst, char *str) return (ft_tokenize(alst, str, LESS)); else if (ft_isdigit(*str)) { - token_append(token, *str); + token_append(token, *str, 0); return (lexer_number(alst, str + 1)); } return (ft_tokenize(alst, str, DEFAULT)); diff --git a/42sh/src/lexer/lexer_quote.c b/42sh/src/lexer/lexer_quote.c index 401fd07f..4b99e6fe 100644 --- a/42sh/src/lexer/lexer_quote.c +++ b/42sh/src/lexer/lexer_quote.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:07:08 by jhalford #+# #+# */ -/* Updated: 2017/01/10 15:13:06 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 15:52:21 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,6 +21,6 @@ int lexer_quote(t_list **alst, char *str) str++; if (*str == '\'') return (ft_tokenize(alst, str + 1, WORD)); - token_append(token, *str); + token_append(token, *str, 1); return (lexer_quote(alst, str)); } diff --git a/42sh/src/lexer/lexer_var.c b/42sh/src/lexer/lexer_var.c index 61011338..ab6e0b79 100644 --- a/42sh/src/lexer/lexer_var.c +++ b/42sh/src/lexer/lexer_var.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/10 14:54:57 by jhalford #+# #+# */ -/* Updated: 2017/01/10 16:36:15 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 15:55:54 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,7 +21,7 @@ int lexer_var(t_list **alst, char *str) token->type = TK_N_WORD; str++; if (!ft_strchr(token->data, '$')) - token_append(token, '$'); + token_append(token, '$', 0); if (!*str) { token_expand_var(token); @@ -32,6 +32,6 @@ int lexer_var(t_list **alst, char *str) token_expand_var(token); return (ft_tokenize(alst, str, state)); } - token_append(token, *str); + token_append(token, *str, 0); return (lexer_var(alst, str)); } diff --git a/42sh/src/lexer/lexer_word.c b/42sh/src/lexer/lexer_word.c index 53ed4746..f24a303f 100644 --- a/42sh/src/lexer/lexer_word.c +++ b/42sh/src/lexer/lexer_word.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:07:11 by jhalford #+# #+# */ -/* Updated: 2017/01/11 15:38:03 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 15:56:02 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,6 +25,6 @@ int lexer_word(t_list **alst, char *str) return (ft_tokenize(&(*alst)->next, str, GREAT)); else if (*str == '<') return (ft_tokenize(&(*alst)->next, str, LESS)); - token_append(token, *str); + token_append(token, *str, 0); return (ft_tokenize(alst, str + 1, WORD)); } diff --git a/42sh/src/lexer/token_append.c b/42sh/src/lexer/token_append.c index c08e5c5a..649f0901 100644 --- a/42sh/src/lexer/token_append.c +++ b/42sh/src/lexer/token_append.c @@ -6,19 +6,26 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/11 17:18:42 by jhalford #+# #+# */ -/* Updated: 2016/11/11 17:47:15 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 20:08:12 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ #include "lexer.h" -int token_append(t_token *token, char c) +int token_append(t_token *token, char c, short int esc) { - if ((int)ft_strlen(token->data) >= token->size) + int len; + + len = ft_strlen(token->data); + if (len >= token->size) { - token->data = (char *)ft_realloc(token->data, token->size + 10); - token->size += 10; + token->size += 8; + token->data = (char *)ft_realloc(token->data, token->size + 1); + token->esc = (char *)ft_realloc(token->esc, token->size / 8); + token->esc[token->size / 8 - 1] = 0; } ft_strcat(token->data, (char[2]){c, '\0'}); + if (esc) + token->esc[len / 8] |= 1 << len % 8; return (0); } diff --git a/42sh/src/lexer/token_expand_var.c b/42sh/src/lexer/token_expand_var.c index b729611c..9743d062 100644 --- a/42sh/src/lexer/token_expand_var.c +++ b/42sh/src/lexer/token_expand_var.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/10 14:57:53 by jhalford #+# #+# */ -/* Updated: 2017/01/10 16:37:33 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 15:56:40 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,5 +24,5 @@ void token_expand_var(t_token *token) *dollar = 0; if (val) while (*val) - token_append(token, *val++); + token_append(token, *val++, 1); } diff --git a/42sh/src/lexer/token_free.c b/42sh/src/lexer/token_free.c index 50ef0d0f..fc42c83a 100644 --- a/42sh/src/lexer/token_free.c +++ b/42sh/src/lexer/token_free.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:07:30 by jhalford #+# #+# */ -/* Updated: 2017/01/12 13:18:46 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 15:35:37 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,6 +19,9 @@ void token_free(void *data, size_t size) (void)size; token = data; if (!(token->type & TK_NON_FREEABLE)) + { ft_strdel(&token->data); + ft_strdel(&token->esc); + } free(token); } diff --git a/42sh/src/lexer/token_init.c b/42sh/src/lexer/token_init.c index cf9fe1b7..8d113343 100644 --- a/42sh/src/lexer/token_init.c +++ b/42sh/src/lexer/token_init.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 15:30:25 by jhalford #+# #+# */ -/* Updated: 2017/01/11 15:45:10 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 15:35:04 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,8 @@ t_token *token_init(void) token = (t_token *)malloc(sizeof(t_token)); token->type = 0; - token->size = 10; - token->data = ft_strnew(token->size); + token->size = 8; + token->data = ft_strnew(token->size + 1); + token->esc = ft_strnew(token->size / 8); return (token); } diff --git a/42sh/src/lexer/token_print.c b/42sh/src/lexer/token_print.c index 97876ddb..3edd1759 100644 --- a/42sh/src/lexer/token_print.c +++ b/42sh/src/lexer/token_print.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:39:01 by jhalford #+# #+# */ -/* Updated: 2017/01/12 14:48:33 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 16:08:16 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,7 @@ void token_print(t_list *lst) t_token *token; int i; t_type type; + int index; while (lst) { @@ -26,6 +27,10 @@ void token_print(t_list *lst) while (type >> (i++ + 2)) ; DG("%02i '%s'", i, token->data); + index = -1; + while (++index < token->size / 8) + ft_dprintf(3, "|%b vs %x vs %c| ", token->esc[index], token->esc[index], token->esc[index]); + ft_dprintf(3, "\n"); lst = lst->next; } }