From 2d42874c482ae05363fbfbb365c48eb288b8902d Mon Sep 17 00:00:00 2001 From: M600 Date: Wed, 22 Mar 2017 18:24:21 +0100 Subject: [PATCH] Completion: Output spaced name with escape char --- 42sh/includes/completion.h | 3 +- 42sh/src/completion/c_misc.c | 51 +++++++++++++++++++++++++++++++++- 42sh/src/completion/c_output.c | 7 +++-- 3 files changed, 57 insertions(+), 4 deletions(-) diff --git a/42sh/includes/completion.h b/42sh/includes/completion.h index b2b62c12..7ce7db9e 100644 --- a/42sh/includes/completion.h +++ b/42sh/includes/completion.h @@ -6,7 +6,7 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/02/18 11:13:04 by alao #+# #+# */ -/* Updated: 2017/03/17 17:23:34 by gwojda ### ########.fr */ +/* Updated: 2017/03/22 18:19:43 by alao ### ########.fr */ /* */ /* ************************************************************************** */ @@ -185,5 +185,6 @@ char *path_solver(t_comp *c, char *cmd, char *cwd); int c_exclusion_folder(t_comp *c); int ft_sstrlen(char **s); char *ft_sstrtostr(char **s, char *sep); +char *ft_add_escape(char *str, char to_escape); #endif diff --git a/42sh/src/completion/c_misc.c b/42sh/src/completion/c_misc.c index 92dd2101..1a979b1d 100644 --- a/42sh/src/completion/c_misc.c +++ b/42sh/src/completion/c_misc.c @@ -6,12 +6,61 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/16 22:17:10 by alao #+# #+# */ -/* Updated: 2017/03/17 16:51:53 by gwojda ### ########.fr */ +/* Updated: 2017/03/22 18:23:04 by alao ### ########.fr */ /* */ /* ************************************************************************** */ #include "completion.h" +/* +** Count the number of time char c is in str. +*/ + +static size_t ft_strxchr(char *str, char c) +{ + size_t rt; + + rt = 0; + while(*str) + { + if (*str == c) + rt++; + str++; + } + return (rt); +} + +/* +** Add escape char \ for char to_escape. +*/ + +char *ft_add_escape(char *str, char to_escape) +{ + char *rt; + int i; + int j; + + if (!str) + return (NULL); + if (!ft_strxchr(str, ' ')) + return (ft_strdup(str)); + rt = ft_strnew(ft_strlen(str) + ft_strxchr(str, to_escape)); + i = 0; + j = 0; + while (str[i]) + { + if (str[i] == to_escape) + { + i++; + rt[j++] = '\\'; + rt[j++] = ' '; + } + else + rt[j++] = str[i++]; + } + return (rt); +} + /* ** Support: Return the size of a char**. */ diff --git a/42sh/src/completion/c_output.c b/42sh/src/completion/c_output.c index c4aead28..ed2a512d 100644 --- a/42sh/src/completion/c_output.c +++ b/42sh/src/completion/c_output.c @@ -6,7 +6,7 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/03 13:10:38 by alao #+# #+# */ -/* Updated: 2017/03/21 14:37:14 by gwojda ### ########.fr */ +/* Updated: 2017/03/22 18:20:29 by alao ### ########.fr */ /* */ /* ************************************************************************** */ @@ -53,19 +53,22 @@ int c_updater(t_comp *c, char *select) { char *tmp; char *rt; + char *alter; tmp = NULL; rt = NULL; + alter = ft_add_escape(select, ' '); if (c->match) tmp = ft_strsub(c->rcmd, 0, ft_strlen(c->rcmd) - ft_strlen(c->match)); else tmp = ft_strdup(c->rcmd); - rt = ft_strjoin(tmp, select); + rt = ft_strjoin(tmp, alter); tmp ? ft_memdel((void *)&tmp) : (0); c->rcmd ? ft_memdel((void *)&c->rcmd) : (0); c->rcmd = ft_strdup(rt); c_updater_rcmd(c); rt ? ft_memdel((void *)&rt) : (0); + alter ? ft_memdel((void *)&alter) : (0); c_clear(data_singleton()); return (1); }