From 61b193013e0a9952be438ecab8caf14b775eedcd Mon Sep 17 00:00:00 2001 From: M600 Date: Thu, 16 Feb 2017 22:19:56 +0100 Subject: [PATCH] Refreshed documentation on autocompletion module --- 42sh/Makefile | 3 +- 42sh/includes/completion.h | 48 +++++++++++++++++++++++---- 42sh/src/completion/c_binary.c | 4 +-- 42sh/src/completion/c_clear.c | 3 +- 42sh/src/completion/c_files.c | 6 ++-- 42sh/src/completion/c_init.c | 52 ++++++++++++++++-------------- 42sh/src/completion/c_matching.c | 16 ++++++--- 42sh/src/completion/c_misc.c | 52 ++++++++++++++++++++++++++++++ 42sh/src/completion/c_output.c | 20 ++++++++---- 42sh/src/completion/c_parser.c | 2 +- 42sh/src/completion/c_pathsolver.c | 49 ++-------------------------- 42sh/src/completion/c_printer.c | 26 ++++++--------- 42sh/src/completion/c_rematch.c | 3 +- 42sh/src/completion/c_sizing.c | 2 +- 42sh/src/completion/c_terminal.c | 2 +- 42sh/src/completion/completion.c | 4 +-- 16 files changed, 175 insertions(+), 117 deletions(-) create mode 100644 42sh/src/completion/c_misc.c diff --git a/42sh/Makefile b/42sh/Makefile index 1b558a0a..9c204824 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -6,7 +6,7 @@ # By: wescande +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2016/08/29 21:32:58 by wescande #+# #+# # -# Updated: 2017/02/15 20:00:50 by alao ### ########.fr # +# Updated: 2017/02/16 22:19:03 by alao ### ########.fr # # # # **************************************************************************** # @@ -48,6 +48,7 @@ completion/c_clear.c\ completion/c_files.c\ completion/c_init.c\ completion/c_matching.c\ +completion/c_misc.c\ completion/c_output.c\ completion/c_parser.c\ completion/c_pathsolver.c\ diff --git a/42sh/includes/completion.h b/42sh/includes/completion.h index 70951fdf..b9d1874b 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/02/16 17:09:17 by alao ### ########.fr */ +/* Updated: 2017/02/16 21:02:51 by alao ### ########.fr */ /* */ /* ************************************************************************** */ @@ -45,7 +45,10 @@ typedef struct s_clst ** match : Part of the command to match when searching. ** home : Path to home for the ~ solving. ** pwd : Current folder to solve local path. -** trail : The rest of the command after the position. +** start : See below. +** between : See below. +** trail : See below. +** cutpoint : See below. ** prompt : Size of the prompt. ** c_sx : Size of the longest word from the list. ** c_sy : Size of the list in number of item. @@ -55,6 +58,18 @@ typedef struct s_clst ** key : The keypressed lastly. ** isfolder : If the match is a folder. boolean. ** lst : List of the item corresponding to the completion. +** +** The complete command is cutted as follow using the command as exemple: +** +** Exemple: [ ls / ; cd (tab) ; pwd ] +** +** (int)cutpoint +** | +** | +** Become: [ls / ;] [ ] [cd ] [ ; pwd] +** | | | | +** | | | | +** (char *)start (char *)between (char *)rcmd (char *)trail */ typedef struct s_comp @@ -81,7 +96,14 @@ typedef struct s_comp } t_comp; /* -** Main autocompletion engine +** Main autocompletion engine: +** completion : Main function. +** c_init : Initialization. +** c_matching : Dispatcher for binary or local files. +** c_seek_binary : Search binary using env PATH. +** c_seek_files : Solve path and search. +** c_parser : Parser. +** c_sizing : Determine the size of the column/line. */ int completion(long int key); @@ -93,7 +115,11 @@ int c_parser(t_comp *c, char *path, char *name); int c_sizing(t_comp *c); /* -** Output functions. +** Output functions: +** +** c_updater : Output the result to struct data. +** c_gtfo : Keypress handling. +** c_rematch : Restart on keypress. */ int c_updater(t_comp *c, char *select); @@ -101,7 +127,12 @@ int c_gtfo(t_comp *c, long int keypress); int c_rematch(t_comp *c, long int keypress); /* -** Terminal related function (moving and printing the list) +** Terminal functions: +** +** c_term_mv_down : Make space for the list. +** c_term_mv_back : Reset the cursor position. +** c_term_clear : Delete the list from the terminal. +** c_printer : Printer of the list. */ void c_term_mv_down(t_comp *c); @@ -110,12 +141,15 @@ void c_term_clear(t_comp *c); void c_printer(t_comp *c); /* -** Support functions +** Support functions: +** +** c_clear : Memory clearing. +** c_clear_lst : List clearing. +** path_solver : Solve abstract path to absolute. */ int c_clear(t_data *s); int c_clear_lst(t_comp *c); char *path_solver(t_comp *c, char *cmd, char *cwd); -int test(t_comp *c); #endif diff --git a/42sh/src/completion/c_binary.c b/42sh/src/completion/c_binary.c index 4bbc824d..bccc73d8 100644 --- a/42sh/src/completion/c_binary.c +++ b/42sh/src/completion/c_binary.c @@ -6,7 +6,7 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/09 13:30:57 by alao #+# #+# */ -/* Updated: 2017/02/16 15:18:14 by alao ### ########.fr */ +/* Updated: 2017/02/16 21:03:18 by alao ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,7 @@ /* ** Retrieve the path from the env and create a char ** from the PATH pattern. ** The function will cycle through it to find the matching element using the -** c_parser() function +** c_parser() function. */ int c_seek_binary(t_data *s, t_comp *c) diff --git a/42sh/src/completion/c_clear.c b/42sh/src/completion/c_clear.c index fac24088..815d12b5 100644 --- a/42sh/src/completion/c_clear.c +++ b/42sh/src/completion/c_clear.c @@ -6,7 +6,7 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/10 11:37:43 by alao #+# #+# */ -/* Updated: 2017/02/16 17:43:57 by alao ### ########.fr */ +/* Updated: 2017/02/16 22:03:41 by alao ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,6 +35,7 @@ int c_clear_lst(t_comp *c) } c_lst->name ? ft_memdel((void *)&c_lst->name) : (0); c_lst ? ft_memdel((void *)&c_lst) : (0); + c->lst = NULL; return (0); } diff --git a/42sh/src/completion/c_files.c b/42sh/src/completion/c_files.c index 5df12b3a..dba7bc4f 100644 --- a/42sh/src/completion/c_files.c +++ b/42sh/src/completion/c_files.c @@ -6,7 +6,7 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/09 13:31:21 by alao #+# #+# */ -/* Updated: 2017/02/16 18:00:04 by alao ### ########.fr */ +/* Updated: 2017/02/16 22:14:51 by alao ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,8 +15,9 @@ /* ** If the parsing for local file fail. The function is called to check if the ** match is actually a folder. If so, the command is updated with a trailing -** slash (/). +** slash (/) using c_updater. ** Exemple: cd folder[tab] to cd folder/ +** Returning 1 if success (trigger an update) or 0. */ static int c_exclusion_folder(t_comp *c) @@ -30,6 +31,7 @@ static int c_exclusion_folder(t_comp *c) { tmp2 = ft_strjoin(c->match, "/"); c_updater(c, tmp2); + tmp ? ft_memdel((void *)&tmp) : (0); tmp2 ? ft_memdel((void *)&tmp2) : (0); return (1); } diff --git a/42sh/src/completion/c_init.c b/42sh/src/completion/c_init.c index b9978300..d9701694 100644 --- a/42sh/src/completion/c_init.c +++ b/42sh/src/completion/c_init.c @@ -6,7 +6,7 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/09 11:21:16 by alao #+# #+# */ -/* Updated: 2017/02/16 17:45:54 by alao ### ########.fr */ +/* Updated: 2017/02/16 22:08:25 by alao ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,7 @@ /* ** Trim if there's many commands in a raw separed with a semi colon. +** The cutpoint is saved and also between char **. */ static char *c_trimmer(char *cmd, int st, int nd) @@ -44,6 +45,26 @@ static char *c_trimmer(char *cmd, int st, int nd) return (tmp); } +/* +** Norme function for c_init(). +*/ + +static void c_init_base(t_comp *c) +{ + struct winsize win; + + ioctl(0, TIOCGWINSZ, &win); + c->win_x = win.ws_col; + c->cutpoint = 0; + c->between = NULL; + c->isfolder = 0; + c->match = NULL; + c->cpath = NULL; + c->lst = NULL; + c->trail = NULL; + c->start = NULL; +} + /* ** Init the completion structure by populating the default value from the main ** structure data and call for the c_matching() function. @@ -51,39 +72,22 @@ static char *c_trimmer(char *cmd, int st, int nd) void c_init(t_data *s, long int input) { - struct winsize win; + int len_trail; if (!(s->comp = (t_comp *)malloc((sizeof(t_comp))))) return ; - s->comp->cutpoint = 0; - s->comp->between = NULL; + c_init_base(s->comp); s->comp->rcmd = c_trimmer(s->line.input, s->line.pos, s->line.pos); len_trail = ft_strlen(s->line.input) - s->line.pos; - if (ft_strlen(s->line.input) > s->line.pos) - s->comp->trail = ft_strsub(s->line.input, s->line.pos, len_trail); - else - s->comp->trail = NULL; - if (s->comp->cutpoint) s->comp->start = ft_strsub(s->line.input, 0, s->comp->cutpoint); - else - s->comp->start = NULL; + if (ft_strlen(s->line.input) > s->line.pos) + s->comp->trail = ft_strsub(s->line.input, s->line.pos, len_trail); s->comp->ircmd = s->line.pos; - s->comp->match = NULL; - s->comp->cpath = NULL; - s->comp->lst = NULL; - ioctl(0, TIOCGWINSZ, &win); - s->comp->win_x = win.ws_col; + s->comp->key = input; + s->comp->prompt = s->line.prompt_size; s->comp->home = ft_strdup(ft_getenv(s->env, "HOME")); s->comp->pwd = ft_strdup(ft_getenv(s->env, "PWD")); - s->comp->key = input; - s->comp->isfolder = 0; - s->comp->prompt = s->line.prompt_size; -// DG("Init end as:"); -// DG("Start [%s]", s->comp->start); -// DG("Between [%s]", s->comp->between); -// DG("RCMD [%s]", s->comp->rcmd); -// DG("Trail [%s]", s->comp->trail); c_matching(s, s->comp); } diff --git a/42sh/src/completion/c_matching.c b/42sh/src/completion/c_matching.c index a698ad66..761f6ef3 100644 --- a/42sh/src/completion/c_matching.c +++ b/42sh/src/completion/c_matching.c @@ -6,17 +6,23 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/10/15 13:27:14 by alao #+# #+# */ -/* Updated: 2017/02/16 17:57:51 by alao ### ########.fr */ +/* Updated: 2017/02/16 21:57:47 by alao ### ########.fr */ /* */ /* ************************************************************************** */ #include "completion.h" /* -** Start the parsing for the autocompletion. If a space is found in the command -** it will call c_seek_files() function. If not, it assume it will be a binary -** so the function c_seek_binary() is called instead. -** Once that done, the printing function should occur. +** Start the parsing for the autocompletion. +** Check the first char of the c->rcmd for a . or /. to see if it's a local +** path to search even if there's no space in the command. +** If a space is found in the command. The function will assume it's a binary +** completion that is needed. Else the c_seek_files() is called to search for +** local path along with the binary part separated. +** If any of the other part successfully created a list, the c_sizing is called +** and 1 is returned. The last condition also check is the structure still +** exist. It's only when the c_rematch() called it that it can be the case. +** Else 0 is returned. */ int c_matching(t_data *s, t_comp *c) diff --git a/42sh/src/completion/c_misc.c b/42sh/src/completion/c_misc.c new file mode 100644 index 00000000..fbdd425a --- /dev/null +++ b/42sh/src/completion/c_misc.c @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* c_misc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: alao +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/16 22:17:10 by alao #+# #+# */ +/* Updated: 2017/02/16 22:18:43 by alao ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "completion.h" + +/* +** Support: Return the size of a char**. +*/ + +int ft_sstrlen(char **s) +{ + int i; + + i = 0; + while (s[i]) + i++; + return (i); +} + +/* +** Support: Tranform a char** as char* with char*sep in between. +*/ + +char *ft_sstrtostr(char **s, char *sep) +{ + char *tmp; + char *tmp2; + int i; + + tmp = NULL; + tmp2 = NULL; + tmp = ft_strjoin(s[0], sep); + i = 1; + while (s[i]) + { + tmp2 = ft_strjoin(tmp, s[i]); + ft_memdel((void *)&tmp); + tmp = ft_strjoin(tmp2, sep); + ft_memdel((void *)&tmp2); + i++; + } + return (tmp); +} diff --git a/42sh/src/completion/c_output.c b/42sh/src/completion/c_output.c index e70f006c..6b9f280d 100644 --- a/42sh/src/completion/c_output.c +++ b/42sh/src/completion/c_output.c @@ -6,12 +6,18 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/03 13:10:38 by alao #+# #+# */ -/* Updated: 2017/02/16 18:02:47 by alao ### ########.fr */ +/* Updated: 2017/02/16 22:00:38 by alao ### ########.fr */ /* */ /* ************************************************************************** */ #include "completion.h" +/* +** Update of the struct data. +** The broken out command is recomposed as one unique including the choice and +** put back in the main structure. The new cursor position is also updated. +*/ + static int c_updater_rcmd(t_comp *c) { char *tmp; @@ -22,12 +28,15 @@ static int c_updater_rcmd(t_comp *c) tmp2 = ft_strjoin(tmp, c->rcmd); c->rcmd ? ft_memdel((void *)&c->rcmd) : (0); c->rcmd = ft_strjoin(tmp2, c->trail); - tmp ? ft_memdel((void *)&tmp) : (0); - tmp2 ? ft_memdel((void *)&tmp2) : (0); - data_singleton()->line.input ? ft_memdel((void *)&data_singleton()->line.input) : (0); - data_singleton()->line.input = ft_strdup(c->rcmd); + if (data_singleton()->line.input) + { + ft_memdel((void *)&data_singleton()->line.input); + data_singleton()->line.input = ft_strdup(c->rcmd); + } new_pos = ft_strlen(c->start) + ft_strlen(c->between) + ft_strlen(c->rcmd); data_singleton()->line.pos = new_pos; + tmp ? ft_memdel((void *)&tmp) : (0); + tmp2 ? ft_memdel((void *)&tmp2) : (0); return (1); } @@ -58,7 +67,6 @@ int c_updater(t_comp *c, char *select) c_updater_rcmd(c); rt ? ft_memdel((void *)&rt) : (0); c_clear(data_singleton()); -// DG("Module complete commands [%s] with pos [%d]", data_singleton()->line.input, data_singleton()->line.pos); return (1); } diff --git a/42sh/src/completion/c_parser.c b/42sh/src/completion/c_parser.c index bca42a13..f611f4df 100644 --- a/42sh/src/completion/c_parser.c +++ b/42sh/src/completion/c_parser.c @@ -6,7 +6,7 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/09 13:52:07 by alao #+# #+# */ -/* Updated: 2017/02/16 16:32:44 by alao ### ########.fr */ +/* Updated: 2017/02/16 22:00:49 by alao ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/completion/c_pathsolver.c b/42sh/src/completion/c_pathsolver.c index 22c5573b..752d874d 100644 --- a/42sh/src/completion/c_pathsolver.c +++ b/42sh/src/completion/c_pathsolver.c @@ -6,55 +6,12 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/10/25 18:21:54 by alao #+# #+# */ -/* Updated: 2017/02/16 12:18:45 by alao ### ########.fr */ +/* Updated: 2017/02/16 22:17:53 by alao ### ########.fr */ /* */ /* ************************************************************************** */ #include "completion.h" -void ft_sstrdelete(char **s) -{ - int i; - - i = 0; - while (s[i]) - { - ft_memdel((void *)&s[i++]); - } - ft_memdel((void *)&s); -} - -int ft_sstrlen(char **s) -{ - int i; - - i = 0; - while (s[i]) - i++; - return (i); -} - -char *ft_sstrtostr(char **s, char *sep) -{ - char *tmp; - char *tmp2; - int i; - - tmp = NULL; - tmp2 = NULL; - tmp = ft_strjoin(s[0], sep); - i = 1; - while (s[i]) - { - tmp2 = ft_strjoin(tmp, s[i]); - ft_memdel((void *)&tmp); - tmp = ft_strjoin(tmp2, sep); - ft_memdel((void *)&tmp2); - i++; - } - return (tmp); -} - /* ** Solve the tilde pattern in the path */ @@ -136,8 +93,8 @@ static char *dots(char *cmd, char *cwd, int i) scmd = ft_strsplit(cmd, '/'); scwd = ft_strsplit(cwd, '/'); tmp = dots_purge(scwd, scmd, 0); - scmd ? ft_sstrdelete(scmd) : (0); - scwd ? ft_sstrdelete(scwd) : (0); + scmd ? ft_sstrfree(scmd) : (0); + scwd ? ft_sstrfree(scwd) : (0); } if (!cwd) tmp = ft_strdup("/"); diff --git a/42sh/src/completion/c_printer.c b/42sh/src/completion/c_printer.c index 6dc92063..f43163de 100644 --- a/42sh/src/completion/c_printer.c +++ b/42sh/src/completion/c_printer.c @@ -6,7 +6,7 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/10 12:55:39 by alao #+# #+# */ -/* Updated: 2017/02/16 12:20:20 by alao ### ########.fr */ +/* Updated: 2017/02/16 22:11:48 by alao ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,9 +15,10 @@ /* ** Print the name with or without an underline and colored upon file type ** as follow: -** - (4) Folder: Red "\e[1;31m" -** - (10) Symlink: Cyan "\e[96m" +** - (4) Folder: Red "\e[1;31m" and a trailing / is added. +** - (10) Symlink: Cyan "\e[96m" and a trailing @ is added. ** If the element is under selection, the video mode is inverted instead. +** The rest of the placeholder is filled with space to align the list. */ static void c_printer_node(t_clst *lst, int c_sx) @@ -25,20 +26,13 @@ static void c_printer_node(t_clst *lst, int c_sx) int i; i = lst->len; - if (lst->cursor) - ft_putstr_fd("\e[7m", 2); - else - { - lst->type == 4 ? ft_putstr_fd("\e[1;31m", 2) : (0); - lst->type == 10 ? ft_putstr_fd("\e[1;96m", 2) : (0); - } + lst->type == 4 ? ft_putstr_fd("\e[1;31m", 2) : (0); + lst->type == 10 ? ft_putstr_fd("\e[1;96m", 2) : (0); + lst->cursor ? ft_putstr_fd("\e[0;7m", 2) : (0); ft_putstr_fd(lst->name, 2); - if (lst->type == 4) - ft_putstr_fd("/", 2); - else if (lst->type == 10) - ft_putstr_fd("@", 2); - else - ft_putstr_fd(" ", 2); + lst->type == 4 ? ft_putstr_fd("/", 2) : (0); + lst->type == 10 ? ft_putstr_fd("@", 2) : (0); + lst->type != 4 && lst->type != 10 ? ft_putstr_fd(" ", 2) : (0); while (i++ < (c_sx)) ft_putstr(" "); ft_putstr_fd("\e[00m", 2); diff --git a/42sh/src/completion/c_rematch.c b/42sh/src/completion/c_rematch.c index d7d33ba1..13421f70 100644 --- a/42sh/src/completion/c_rematch.c +++ b/42sh/src/completion/c_rematch.c @@ -6,7 +6,7 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/15 12:03:30 by alao #+# #+# */ -/* Updated: 2017/02/16 18:04:15 by alao ### ########.fr */ +/* Updated: 2017/02/16 22:03:42 by alao ### ########.fr */ /* */ /* ************************************************************************** */ @@ -53,7 +53,6 @@ int c_rematch(t_comp *c, long int keypress) c_term_clear(c); c_refresh_match(c, keypress); c_clear_lst(c); - c->lst = NULL; c_matching(data_singleton(), c); if (c->lst == NULL) { diff --git a/42sh/src/completion/c_sizing.c b/42sh/src/completion/c_sizing.c index 76e616dc..2d7adda9 100644 --- a/42sh/src/completion/c_sizing.c +++ b/42sh/src/completion/c_sizing.c @@ -6,7 +6,7 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/10 08:50:26 by alao #+# #+# */ -/* Updated: 2017/01/19 15:45:35 by alao ### ########.fr */ +/* Updated: 2017/02/16 22:01:47 by alao ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/completion/c_terminal.c b/42sh/src/completion/c_terminal.c index b11c754c..1b5d28a2 100644 --- a/42sh/src/completion/c_terminal.c +++ b/42sh/src/completion/c_terminal.c @@ -6,7 +6,7 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/10/11 10:44:40 by alao #+# #+# */ -/* Updated: 2017/02/16 16:58:53 by alao ### ########.fr */ +/* Updated: 2017/02/16 22:01:37 by alao ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/completion/completion.c b/42sh/src/completion/completion.c index f1732087..42705b56 100644 --- a/42sh/src/completion/completion.c +++ b/42sh/src/completion/completion.c @@ -6,14 +6,14 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/09/20 14:50:33 by alao #+# #+# */ -/* Updated: 2017/02/16 17:59:23 by alao ### ########.fr */ +/* Updated: 2017/02/16 22:01:18 by alao ### ########.fr */ /* */ /* ************************************************************************** */ #include "completion.h" /* -** Function to select the next item in the list if it has already been Created +** Function to select the next item in the list if it has already been created ** and if the keypressed is tab. */