diff --git a/42sh/src/completion/c_match.c b/42sh/src/completion/c_match.c index 0fc2d8eb..07391a64 100644 --- a/42sh/src/completion/c_match.c +++ b/42sh/src/completion/c_match.c @@ -6,33 +6,59 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/10/15 13:27:14 by alao #+# #+# */ -/* Updated: 2017/03/22 15:13:12 by gwojda ### ########.fr */ +/* Updated: 2017/03/22 15:49:09 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" +static int c_is_delim(char c) +{ + if (c == ' ' || c == '<' || c == '>' || c == '\n' || c == ';') + return (1); + return (0); +} + +static char *c_strdupi(char *s, int (*f)(char)) +{ + int i; + char *str; + + i = 0; + while (s[i] && !(*f)(s[i])) + i++; + str = (char *)ft_malloc(sizeof(char) * (i + 1)); + if (str) + { + str[i--] = '\0'; + while (i >= 0) + { + str[i] = s[i]; + i--; + } + } + return (str); +} + + /* ** Seek the current word. */ -static char *c_current_words(t_comp *c) +static char *c_current_words(void) { size_t pos; char *str; - (void)c; pos = data_singleton()->line.pos; str = data_singleton()->line.input; - DG("pos = \"%zu\"", pos); - while (pos && str[pos] != ' ' && str[pos] != '<' - && str[pos] != '>' && str[pos] != '\n' && str[pos] != ';') + if (pos && c_is_delim(str[pos])) --pos; - if (str[pos] == ' ' || str[pos] == '<' - || str[pos] == '>' || str[pos] == '\n' || str[pos] == ';') + while (pos && !c_is_delim(str[pos])) + --pos; + if (c_is_delim(str[pos])) ++pos; - DG("pos = \"%zu\"", pos); - return (str + pos); + return (c_strdupi(str + pos, &c_is_delim)); } /* @@ -43,9 +69,7 @@ int c_matching(t_data *s, t_comp *c) { char *current_word; - DG("c->rcmd = \"%s\"", c->rcmd); - current_word = c_current_words(c); - DG("current_word = \"%s\"", current_word); + current_word = c_current_words(); if (ft_strchr(c->rcmd, '/')) c_seek_abs_path(c, current_word); else if (ft_strchr(c->rcmd, '$')) @@ -54,6 +78,7 @@ int c_matching(t_data *s, t_comp *c) c_seek_binary(s, c); else c_seek_files(s, c, current_word); + ft_strdel(¤t_word); if (s->comp && s->comp->lst) { c_sizing(c);