From dea3e1c7a8df55af437b7adb6ee7a108d1cda22b Mon Sep 17 00:00:00 2001 From: gwojda Date: Wed, 22 Mar 2017 15:46:42 +0100 Subject: [PATCH] fix bugs completion... --- 42sh/src/completion/c_match.c | 48 +++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/42sh/src/completion/c_match.c b/42sh/src/completion/c_match.c index 0fc2d8eb..7daa6f1d 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:46:22 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)); } /* @@ -44,7 +70,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); + current_word = c_current_words(); DG("current_word = \"%s\"", current_word); if (ft_strchr(c->rcmd, '/')) c_seek_abs_path(c, current_word);