diff --git a/42sh/Makefile b/42sh/Makefile index e77f118d..8e5c6248 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -44,21 +44,21 @@ builtin/builtin_setenv.c\ builtin/builtin_unset.c\ builtin/builtin_unsetenv.c\ builtin/is_builtin.c\ -completion/c_abs_path.c\ completion/c_arrow.c\ -completion/c_binary.c\ completion/c_clear.c\ -completion/c_files.c\ -completion/c_glob_matching.c\ +completion/c_find_abspath.c\ +completion/c_find_binary.c\ +completion/c_find_env.c\ +completion/c_find_files.c\ completion/c_init.c\ -completion/c_matching.c\ +completion/c_match.c\ +completion/c_match_glob.c\ +completion/c_match_update.c\ completion/c_misc.c\ completion/c_output.c\ completion/c_parser.c\ completion/c_pathsolver.c\ completion/c_printer.c\ -completion/c_rematch.c\ -completion/c_seek_env.c\ completion/c_sizing.c\ completion/c_terminal.c\ completion/completion.c\ diff --git a/42sh/includes/completion.h b/42sh/includes/completion.h index c174bee0..399d81cd 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/15 18:46:45 by jhalford ### ########.fr */ +/* Updated: 2017/03/16 09:14:30 by alao ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,11 @@ # define COMPLETION_H # include "minishell.h" -# define RETARDED_BEHAVIOR 0 + +# define KP_U 4283163 +# define KP_D 4348699 +# define KP_L 4479771 +# define KP_R 4414235 /* ** Autocompletion list for the valid candidates from the parser. @@ -27,16 +31,16 @@ ** prev : Pointer to the previous item of the list. */ -typedef struct s_clst +typedef struct s_clst { - int id; - char *name; - int type; - int len; - int cursor; - struct s_clst *next; - struct s_clst *prev; -} t_clst; + int id; + char *name; + int type; + int len; + int cursor; + struct s_clst *next; + struct s_clst *prev; +} t_clst; /* ** Autocompletion structure composed as follow: @@ -77,65 +81,76 @@ typedef struct s_clst ** (char *)start (char *)between (char *)rcmd (char *)trail */ -typedef struct s_comp +typedef struct s_comp { - char *rcmd; - int ircmd; - char *cpath; - char *match; - char *home; - char *pwd; - char *start; - char *between; - char *trail; - int cutpoint; - int prompt; - int c_sx; - int c_sy; - int c_pline; - int c_line; - int win_x; - int win_y; - int m_size; - int pos_x; - int pos_y; - int key; - int isfolder; - int isrematch; - t_clst *lst; -} t_comp; + char *rcmd; + int ircmd; + char *cpath; + char *match; + char *home; + char *pwd; + char *start; + char *between; + char *trail; + int cutpoint; + int prompt; + int c_sx; + int c_sy; + int c_pline; + int c_line; + int win_x; + int win_y; + int m_size; + int pos_x; + int pos_y; + int key; + int isfolder; + int isrematch; + t_clst *lst; +} t_comp; /* ** Main autocompletion engine: ** completion : Main function. ** c_init : Initialization. ** c_matching : Dispatcher for binary or local files. +** c_rematch : Restart on keypress. +** c_glob_matching : Call for globbing completion. ** c_seek_binary : Search binary using env PATH. ** c_seek_files : Solve path and search. +** c_seek_env : Search in env for $(var) +** c_seek_abs_path : Determiner for absolute path ** c_parser : Parser. +** c_add_to_lst : Add new node to the list. ** c_sizing : Determine the size of the column/line. */ -int completion(long int key); -void c_init(t_data *s, long int input); -int c_matching(t_data *s, t_comp *c); -int c_seek_binary(t_data *s, t_comp *c); -int c_seek_files( - t_data *s, t_comp *c, char *current_word); -int c_parser(t_comp *c, char *path, char *name); -int c_sizing(t_comp *c); +int completion(long int key); +void c_init(t_data *s, long int input); +int c_matching(t_data *s, t_comp *c); +int c_rematch(t_comp *c, long int keypress); +int c_glob_matching(void); +int c_seek_binary(t_data *s, t_comp *c); +int c_seek_files(t_data *s, t_comp *c, char *current_word); +int c_seek_env(t_comp *c, char *current_word); +void c_seek_abs_path(t_comp *c, char *current_word); +int c_parser(t_comp *c, char *path, char *name); +void c_add_to_lst(t_comp *c, t_clst *node); +int c_sizing(t_comp *c); /* ** Output functions: ** +** c_dispatcher : Choose between clear, print or update from to the list ** c_updater : Output the result to struct data. -** c_gtfo : Keypress handling. -** c_rematch : Restart on keypress. +** c_keypress : Keypress handling. +** c_arrow : Arrow support. */ -int c_updater(t_comp *c, char *select); -int c_gtfo(t_comp *c, long int keypress); -int c_rematch(t_comp *c, long int keypress); +int c_dispatcher(t_data *s); +int c_updater(t_comp *c, char *select); +int c_keypress(t_comp *c, long int key); +void c_arrow(t_comp *c, long int keypress); /* ** Terminal functions: @@ -147,11 +162,11 @@ int c_rematch(t_comp *c, long int keypress); ** c_printer : Printer of the list. */ -void c_term_mv_down(t_comp *c); -void c_term_mv_back(t_comp *c); -void c_term_clear(t_comp *c); -int c_term_resize(t_comp *c); -void c_printer(t_comp *c); +void c_term_mv_down(t_comp *c); +void c_term_mv_back(t_comp *c); +void c_term_clear(t_comp *c); +int c_term_resize(t_comp *c); +void c_printer(t_comp *c); /* ** Support functions: @@ -159,26 +174,16 @@ void c_printer(t_comp *c); ** c_clear : Memory clearing. ** c_clear_lst : List clearing. ** path_solver : Solve abstract path to absolute. +** c_exclusion_foldr : Check for match folder. +** ft_sstrlen : Return size of char **. +** ft_sstrtostr : Create char * from char ** with char *sep between. */ -int c_clear(t_data *s); -int c_clear_lst(t_comp *c); -char *path_solver(t_comp *c, char *cmd, char *cwd); -int c_spacing_escape(t_clst *node, int x, int o); -int c_spacing_clear(t_comp *c); -int ft_sstrlen(char **s); -char *ft_sstrtostr(char **s, char *sep); - -/* -** j'ajoute a la va vite^^ -*/ - -int c_glob_matching(void); -void c_add_to_lst(t_comp *c, t_clst *node); -int c_seek_env(t_comp *c, char *current_word); -void c_seek_abs_path(t_comp *c, char *current_word); -void c_arrow(t_comp *c, long int keypress); -int c_chevron(t_comp *c); -int c_dispatcher(t_data *s); +int c_clear(t_data *s); +int c_clear_lst(t_comp *c); +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); #endif diff --git a/42sh/src/completion/c_arrow.c b/42sh/src/completion/c_arrow.c index b06481ec..d0246fdd 100644 --- a/42sh/src/completion/c_arrow.c +++ b/42sh/src/completion/c_arrow.c @@ -6,43 +6,51 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/10 09:04:42 by alao #+# #+# */ -/* Updated: 2017/03/15 14:26:11 by alao ### ########.fr */ +/* Updated: 2017/03/16 08:17:12 by alao ### ########.fr */ /* */ /* ************************************************************************** */ #include "completion.h" /* +** Calculate the offset id value from the current one relative to the list size. +** lst_modulo is the number in element in the last column of the list that is +** not perfectly in the same size. ** */ static int c_idsolver(t_comp *c, int ids, int ids_bk) { + int lst_modulo; + + lst_modulo = c->lst->prev->id % c->c_line; + if (lst_modulo == 0) + lst_modulo = c->c_line; if (ids > c->lst->prev->id) { ids -= c->lst->prev->id; - if (ids_bk < ((c->lst->prev->id - (c->lst->prev->id % c->c_line)) + 1)) - ids = ids + (c->lst->prev->id % c->c_line); + if (ids_bk < ((c->lst->prev->id - lst_modulo) + 1)) + ids = ids + lst_modulo; else - ids = ids - (c->c_line - (c->lst->prev->id % c->c_line)); + ids = ids - (c->c_line - lst_modulo); } else if (ids < c->lst->id) { - if (ids_bk <= (c->lst->prev->id % c->c_line)) - ids = (c->lst->prev->id - (c->lst->prev->id % c->c_line)) + ids_bk; + if (ids_bk <= lst_modulo) + ids = (c->lst->prev->id - lst_modulo) + ids_bk; else - ids = c->lst->prev->id - (ids * -1) - (c->lst->prev->id % c->c_line); + ids = c->lst->prev->id - (ids * -1) - lst_modulo; if (ids < c->lst->id) - { - DG("Failsafe %d", ids); ids = 1; - } } return (ids); } /* -** +** Determine the movement in the list by retrieving the current id selected in +** the list and add/substract the value depending on the keypress (left/right). +** Call for c_idsolver() to adjust this value and cycle in the list to reassign +** the correct element to cursor position. */ static void c_arrow_multi(t_comp *c, long int keypress) @@ -52,7 +60,6 @@ static void c_arrow_multi(t_comp *c, long int keypress) int ids_bk; ids = 0; - ptr = c->lst; while (!ptr->cursor) ptr = ptr->next; @@ -69,7 +76,10 @@ static void c_arrow_multi(t_comp *c, long int keypress) } /* -** +** Arrow support for moving inside the list of the completion. +** If the keypress is left/right, c_arrow_multi() is called. Else the list is +** just read until the cursor is found and the next/prev is reassigned as +** selected. */ void c_arrow(t_comp *c, long int keypress) diff --git a/42sh/src/completion/c_clear.c b/42sh/src/completion/c_clear.c index 7ab7f837..2e1dc33b 100644 --- a/42sh/src/completion/c_clear.c +++ b/42sh/src/completion/c_clear.c @@ -6,20 +6,20 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/10 11:37:43 by alao #+# #+# */ -/* Updated: 2017/02/17 14:57:09 by alao ### ########.fr */ +/* Updated: 2017/03/16 08:27:50 by alao ### ########.fr */ /* */ /* ************************************************************************** */ -#include "minishell.h" +#include "completion.h" /* ** Clear the list from the memory */ -int c_clear_lst(t_comp *c) +int c_clear_lst(t_comp *c) { - t_clst *c_lst; - t_clst *p_lst; + t_clst *c_lst; + t_clst *p_lst; if (c->lst == NULL) return (0); @@ -46,9 +46,9 @@ int c_clear_lst(t_comp *c) ** function */ -int c_clear(t_data *s) +int c_clear(t_data *s) { - t_comp *ptr; + t_comp *ptr; if (s->comp == NULL) return (0); diff --git a/42sh/src/completion/c_abs_path.c b/42sh/src/completion/c_find_abspath.c similarity index 59% rename from 42sh/src/completion/c_abs_path.c rename to 42sh/src/completion/c_find_abspath.c index 7706dbd8..faa5588c 100644 --- a/42sh/src/completion/c_abs_path.c +++ b/42sh/src/completion/c_find_abspath.c @@ -1,45 +1,28 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* c_abs_path.c :+: :+: :+: */ +/* c_find_abspath.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/09 16:54:59 by gwojda #+# #+# */ -/* Updated: 2017/03/15 06:57:53 by alao ### ########.fr */ +/* Updated: 2017/03/16 08:27:55 by alao ### ########.fr */ /* */ /* ************************************************************************** */ #include "completion.h" -static int c_exclusion_folder(t_comp *c) -{ - DIR *rep; - char *tmp; - char *tmp2; +/* +** Process the absolute path for the completion. +*/ - tmp = ft_strjoin(c->cpath, c->match); - tmp2 = NULL; - if (tmp[ft_strlen(tmp) - 1] == '/') - return (0); - if ((rep = opendir(tmp)) && (!closedir(rep))) - { - tmp ? ft_memdel((void *)&tmp) : (0); - tmp2 = ft_strjoin(c->match, "/"); - c_updater(c, tmp2); - tmp2 ? ft_memdel((void *)&tmp2) : (0); - return (1); - } - tmp ? ft_memdel((void *)&tmp) : (0); - tmp ? ft_memdel((void *)&tmp) : (0); - return (0); -} - -void c_seek_abs_path(t_comp *c, char *current_word) +void c_seek_abs_path(t_comp *c, char *current_word) { char *tmp; + int len; - c->cpath = ft_strndup(current_word, ft_strrchr(c->rcmd, '/') - current_word + 1); + len = ft_strrchr(c->rcmd, '/') - current_word + 1; + c->cpath = ft_strndup(current_word, len); if (current_word[0] == '~') { tmp = c->cpath; diff --git a/42sh/src/completion/c_binary.c b/42sh/src/completion/c_find_binary.c similarity index 84% rename from 42sh/src/completion/c_binary.c rename to 42sh/src/completion/c_find_binary.c index 0a3dbb33..61c6efd5 100644 --- a/42sh/src/completion/c_binary.c +++ b/42sh/src/completion/c_find_binary.c @@ -1,16 +1,16 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* c_binary.c :+: :+: :+: */ +/* c_find_binary.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/09 13:30:57 by alao #+# #+# */ -/* Updated: 2017/02/27 13:17:27 by alao ### ########.fr */ +/* Updated: 2017/03/16 08:28:03 by alao ### ########.fr */ /* */ /* ************************************************************************** */ -#include "minishell.h" +#include "completion.h" /* ** Retrieve the path from the env and create a char ** from the PATH pattern. @@ -18,11 +18,11 @@ ** c_parser() function. */ -int c_seek_binary(t_data *s, t_comp *c) +int c_seek_binary(t_data *s, t_comp *c) { - char *tmp; - char **paths; - int i; + char *tmp; + char **paths; + int i; i = 0; tmp = NULL; diff --git a/42sh/src/completion/c_seek_env.c b/42sh/src/completion/c_find_env.c similarity index 72% rename from 42sh/src/completion/c_seek_env.c rename to 42sh/src/completion/c_find_env.c index e63e1135..02c1d296 100644 --- a/42sh/src/completion/c_seek_env.c +++ b/42sh/src/completion/c_find_env.c @@ -1,20 +1,24 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* c_seek_env.c :+: :+: :+: */ +/* c_find_env.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/09 15:50:24 by gwojda #+# #+# */ -/* Updated: 2017/03/15 11:50:15 by gwojda ### ########.fr */ +/* Updated: 2017/03/16 08:28:19 by alao ### ########.fr */ /* */ /* ************************************************************************** */ -#include "minishell.h" +#include "completion.h" -static int c_storing(t_comp *c, char *value) +/* +** Create a new list node and add it by calling c_add_to_lst. +*/ + +static int c_addnode(t_comp *c, char *value) { - t_clst *tmp; + t_clst *tmp; if (!(tmp = (t_clst *)malloc(sizeof(t_clst)))) return (-1); @@ -26,6 +30,10 @@ static int c_storing(t_comp *c, char *value) return (0); } +/* +** Search in the env for matching element and add them to the list if matched. +*/ + int c_seek_env(t_comp *c, char *current_word) { char **env; @@ -37,8 +45,8 @@ int c_seek_env(t_comp *c, char *current_word) while (env[i]) { if (!ft_strncmp(c->match, env[i], ft_strlen(c->match)) && - env[i][ft_strlen(c->match)] != '=') - c_storing(c, ft_strndup(env[i], ft_strchr(env[i], '=') - env[i])); + env[i][ft_strlen(c->match)] != '=') + c_addnode(c, ft_strndup(env[i], ft_strchr(env[i], '=') - env[i])); ++i; } return (0); diff --git a/42sh/src/completion/c_files.c b/42sh/src/completion/c_find_files.c similarity index 87% rename from 42sh/src/completion/c_files.c rename to 42sh/src/completion/c_find_files.c index b4fd8fed..4fb754b5 100644 --- a/42sh/src/completion/c_files.c +++ b/42sh/src/completion/c_find_files.c @@ -1,16 +1,16 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* c_files.c :+: :+: :+: */ +/* c_find_files.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/09 13:31:21 by alao #+# #+# */ -/* Updated: 2017/03/15 11:47:40 by gwojda ### ########.fr */ +/* Updated: 2017/03/16 08:29:21 by alao ### ########.fr */ /* */ /* ************************************************************************** */ -#include "minishell.h" +#include "completion.h" /* ** If the parsing for local file fail. The function is called to check if the @@ -20,11 +20,11 @@ ** Returning 1 if success (trigger an update) or 0. */ -static int c_exclusion_folder(t_comp *c) +int c_exclusion_folder(t_comp *c) { - DIR *rep; - char *tmp; - char *tmp2; + DIR *rep; + char *tmp; + char *tmp2; tmp = ft_strjoin(c->cpath, c->match); tmp2 = NULL; @@ -45,16 +45,17 @@ static int c_exclusion_folder(t_comp *c) tmp ? ft_memdel((void *)&tmp) : (0); return (0); } + /* ** Clear the binary from c->rcmd ans save the result in c->match. Return the ** path part of it if exist or NULL. */ -static char *c_slicer(t_comp *c) +static char *c_slicer(t_comp *c) { - char *tmp; - char *rt; - int i; + char *tmp; + char *rt; + int i; i = 0; tmp = NULL; @@ -78,7 +79,7 @@ static char *c_slicer(t_comp *c) ** Files searching */ -int c_seek_files(t_data *s, t_comp *c, char *current_word) +int c_seek_files(t_data *s, t_comp *c, char *current_word) { char *path; diff --git a/42sh/src/completion/c_init.c b/42sh/src/completion/c_init.c index e625e035..7840296e 100644 --- a/42sh/src/completion/c_init.c +++ b/42sh/src/completion/c_init.c @@ -6,22 +6,22 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/09 11:21:16 by alao #+# #+# */ -/* Updated: 2017/03/10 12:43:57 by alao ### ########.fr */ +/* Updated: 2017/03/16 08:30:52 by alao ### ########.fr */ /* */ /* ************************************************************************** */ -#include "minishell.h" +#include "completion.h" /* ** 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) +static char *c_trimmer(char *cmd, int st, int nd) { - char *rt; - char *tmp; - int len; + char *rt; + char *tmp; + int len; rt = NULL; tmp = NULL; @@ -49,9 +49,9 @@ static char *c_trimmer(char *cmd, int st, int nd) ** Norme function for c_init(). */ -static void c_init_base(t_comp *c) +static void c_init_base(t_comp *c) { - struct winsize win; + struct winsize win; ioctl(0, TIOCGWINSZ, &win); c->win_x = win.ws_col; @@ -80,7 +80,7 @@ static void c_init_base(t_comp *c) void c_init(t_data *s, long int input) { - int len_trail; + int len_trail; if (!(s->comp = (t_comp *)malloc((sizeof(t_comp))))) return ; diff --git a/42sh/src/completion/c_matching.c b/42sh/src/completion/c_match.c similarity index 68% rename from 42sh/src/completion/c_matching.c rename to 42sh/src/completion/c_match.c index ca3d6192..7d85f162 100644 --- a/42sh/src/completion/c_matching.c +++ b/42sh/src/completion/c_match.c @@ -1,22 +1,22 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* c_matching.c :+: :+: :+: */ +/* c_match.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/10/15 13:27:14 by alao #+# #+# */ -/* Updated: 2017/03/14 17:22:10 by gwojda ### ########.fr */ +/* Updated: 2017/03/16 09:12:40 by alao ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" /* -** chevron y es-tu ??? +** Failsafe by checking if the nearby char are not a < or > for aggregation. */ -int c_chevron(t_comp *c) +static int c_chevron(t_comp *c) { size_t pos; @@ -33,16 +33,7 @@ int c_chevron(t_comp *c) } /* -** 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. +** Seek the current word. */ static char *c_current_words(t_comp *c) @@ -57,6 +48,10 @@ static char *c_current_words(t_comp *c) return (c->rcmd + pos); } +/* +** Start the parsing for the autocompletion. +*/ + int c_matching(t_data *s, t_comp *c) { char *current_word; diff --git a/42sh/src/completion/c_glob_matching.c b/42sh/src/completion/c_match_glob.c similarity index 71% rename from 42sh/src/completion/c_glob_matching.c rename to 42sh/src/completion/c_match_glob.c index 4ff64652..d7720fce 100644 --- a/42sh/src/completion/c_glob_matching.c +++ b/42sh/src/completion/c_match_glob.c @@ -1,24 +1,28 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* c_glob_matching.c :+: :+: :+: */ +/* c_match_glob.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/08 17:20:01 by gwojda #+# #+# */ -/* Updated: 2017/03/14 14:00:30 by gwojda ### ########.fr */ +/* Updated: 2017/03/16 08:39:09 by alao ### ########.fr */ /* */ /* ************************************************************************** */ -#include "minishell.h" +#include "completion.h" -static void c_replace_globbing(char **glob, size_t start) +/* +** Recreate the command from the globbing module responds. +*/ + +static void c_replace_globbing(char **glob, size_t start) { - char *ref_next; - char *ref_mid; - char *ref_prev; - char *str; - size_t pos; + char *ref_next; + char *ref_mid; + char *ref_prev; + char *str; + size_t pos; str = data_singleton()->line.input; pos = data_singleton()->line.pos; @@ -36,7 +40,30 @@ static void c_replace_globbing(char **glob, size_t start) free(str); } -int c_glob_matching(void) +/* +** 42 norme. Readjust the position. +*/ + +static int c_glob_pos(char *str, size_t pos) +{ + str = data_singleton()->line.input; + pos = data_singleton()->line.pos; + if (!str) + return (-1); + if (pos && str[pos] == ' ') + --pos; + while (pos && str[pos] != ' ') + --pos; + if (str[pos] == ' ') + ++pos; + return (pos); +} + +/* +** Match for globbing feature. +*/ + +int c_glob_matching(void) { char *current_word; char **ss_glob; @@ -45,15 +72,8 @@ int c_glob_matching(void) size_t pos; str = data_singleton()->line.input; - pos = data_singleton()->line.pos; - if (!str) + if ((pos = c_glob_pos(NULL, 0)) == -1) return (0); - if (pos && str[pos] == ' ') - --pos; - while (pos && str[pos] != ' ') - --pos; - if (str[pos] == ' ') - ++pos; current_word = ft_strdupi_w(str + pos); if (current_word[0] == '$') { @@ -64,7 +84,8 @@ int c_glob_matching(void) ft_bzero(glob_echap, ft_strlen(str) >> 3); ss_glob = glob(current_word, glob_echap, glob_echap, 1); free(current_word); - if (!*ss_glob || !**ss_glob || !ft_strncmp(str + pos, *ss_glob, ft_strlen(*ss_glob))) + if (!*ss_glob || !**ss_glob || + !ft_strncmp(str + pos, *ss_glob, ft_strlen(*ss_glob))) return (0); c_replace_globbing(ss_glob, pos); return (1); diff --git a/42sh/src/completion/c_rematch.c b/42sh/src/completion/c_match_update.c similarity index 93% rename from 42sh/src/completion/c_rematch.c rename to 42sh/src/completion/c_match_update.c index edea661e..5d382df5 100644 --- a/42sh/src/completion/c_rematch.c +++ b/42sh/src/completion/c_match_update.c @@ -1,16 +1,16 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* c_rematch.c :+: :+: :+: */ +/* c_match_update.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/15 12:03:30 by alao #+# #+# */ -/* Updated: 2017/03/10 17:27:06 by gwojda ### ########.fr */ +/* Updated: 2017/03/16 08:08:11 by alao ### ########.fr */ /* */ /* ************************************************************************** */ -#include "minishell.h" +#include "completion.h" /* ** Recreate a c->match value by adding the new key pressed to it. diff --git a/42sh/src/completion/c_misc.c b/42sh/src/completion/c_misc.c index eb740791..34ee487e 100644 --- a/42sh/src/completion/c_misc.c +++ b/42sh/src/completion/c_misc.c @@ -6,11 +6,11 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/16 22:17:10 by alao #+# #+# */ -/* Updated: 2017/02/16 22:18:43 by alao ### ########.fr */ +/* Updated: 2017/03/16 08:08:19 by alao ### ########.fr */ /* */ /* ************************************************************************** */ -#include "minishell.h" +#include "completion.h" /* ** Support: Return the size of a char**. diff --git a/42sh/src/completion/c_output.c b/42sh/src/completion/c_output.c index af1ed37d..cd73c1f8 100644 --- a/42sh/src/completion/c_output.c +++ b/42sh/src/completion/c_output.c @@ -6,11 +6,11 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/03 13:10:38 by alao #+# #+# */ -/* Updated: 2017/03/10 09:05:23 by alao ### ########.fr */ +/* Updated: 2017/03/16 08:48:05 by alao ### ########.fr */ /* */ /* ************************************************************************** */ -#include "minishell.h" +#include "completion.h" /* ** Update of the struct data. @@ -87,26 +87,31 @@ int c_updater(t_comp *c, char *select) ** return 0. Else it will return 1. ** ** Keypress values that cancel the module: -** 27: Escape -** 127: Backspace -** 2117294875: Delete +** 27 : Escape +** 127 : Backspace +** 2117294875 : Delete ** ** Keypress values that validate the choice: -** 10: Enter -** 32: Space +** 10 : Enter +** 32 : Space +** Keypress values for arrows +** 4283163 : Up +** 4348699 : Down +** 4479771 : Left +** 4414235 : Right */ -int c_gtfo(t_comp *c, long int keypress) +int c_keypress(t_comp *c, long int key) { t_clst *ptr; - if (keypress == 27 || keypress == 127 || keypress == 2117294875) + if (key == 27 || key == 127 || key == 2117294875) { c_term_clear(c); c_clear(data_singleton()); return (0); } - if (keypress == 10 || keypress == 32) + if (key == 10 || key == 32) { ptr = c->lst; while (!ptr->cursor) @@ -114,11 +119,10 @@ int c_gtfo(t_comp *c, long int keypress) c_updater(c, ptr->name); return (1); } - if (keypress == 4283163 || keypress == 4348699 - || keypress == 4479771 || keypress == 4414235) + if (key == KP_U || key == KP_D || key == KP_L || key == KP_R) { - c_arrow(c, keypress); + c_arrow(c, key); return (0); } - return ((c_rematch(c, keypress)) ? (0) : (1)); + return ((c_rematch(c, key)) ? (0) : (1)); } diff --git a/42sh/src/completion/c_parser.c b/42sh/src/completion/c_parser.c index 92358007..166716ae 100644 --- a/42sh/src/completion/c_parser.c +++ b/42sh/src/completion/c_parser.c @@ -6,11 +6,11 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/09 13:52:07 by alao #+# #+# */ -/* Updated: 2017/03/15 06:53:34 by alao ### ########.fr */ +/* Updated: 2017/03/16 08:49:22 by alao ### ########.fr */ /* */ /* ************************************************************************** */ -#include "minishell.h" +#include "completion.h" /* ** Add the matching element to the list @@ -18,7 +18,6 @@ void c_add_to_lst(t_comp *c, t_clst *node) { - DG("\tADD %s", node->name); if (c->lst == NULL) { c->lst = node; diff --git a/42sh/src/completion/c_pathsolver.c b/42sh/src/completion/c_pathsolver.c index 6ab88f40..a8f2eff8 100644 --- a/42sh/src/completion/c_pathsolver.c +++ b/42sh/src/completion/c_pathsolver.c @@ -6,11 +6,11 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/10/25 18:21:54 by alao #+# #+# */ -/* Updated: 2017/02/16 22:17:53 by alao ### ########.fr */ +/* Updated: 2017/03/16 08:50:38 by alao ### ########.fr */ /* */ /* ************************************************************************** */ -#include "minishell.h" +#include "completion.h" /* ** Solve the tilde pattern in the path @@ -18,10 +18,10 @@ static char *tilde(t_comp *c, char *cmd) { - char *t_home; - char *t_sub; - char *rt; - int l_cmd; + char *t_home; + char *t_sub; + char *rt; + int l_cmd; t_home = NULL; t_sub = NULL; @@ -42,9 +42,9 @@ static char *tilde(t_comp *c, char *cmd) static char *dots_purge(char **scwd, char **scmd, int i) { - char *tmp; - char *rt; - int j; + char *tmp; + char *rt; + int j; tmp = NULL; rt = NULL; @@ -75,9 +75,9 @@ static char *dots_purge(char **scwd, char **scmd, int i) static char *dots(char *cmd, char *cwd, int i) { - char *tmp; - char **scmd; - char **scwd; + char *tmp; + char **scmd; + char **scwd; tmp = NULL; if (!(ft_strcmp(cmd, ".")) || !(ft_strcmp(cmd, "./"))) @@ -107,9 +107,9 @@ static char *dots(char *cmd, char *cwd, int i) char *path_solver(t_comp *c, char *cmd, char *cwd) { - char *dir; - char *tmp; - char *ncmd; + char *dir; + char *tmp; + char *ncmd; dir = NULL; tmp = NULL; diff --git a/42sh/src/completion/c_printer.c b/42sh/src/completion/c_printer.c index 85bd5adf..07f3c2b0 100644 --- a/42sh/src/completion/c_printer.c +++ b/42sh/src/completion/c_printer.c @@ -6,11 +6,11 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/10 12:55:39 by alao #+# #+# */ -/* Updated: 2017/03/15 14:30:37 by gwojda ### ########.fr */ +/* Updated: 2017/03/16 09:01:22 by alao ### ########.fr */ /* */ /* ************************************************************************** */ -#include "minishell.h" +#include "completion.h" /* ** Print the name with or without an underline and colored upon file type @@ -21,9 +21,9 @@ ** The rest of the placeholder is filled with space to align the list. */ -static void c_printer_node(t_clst *lst, int c_sx) +static void c_printer_node(t_clst *lst, int c_sx) { - int i; + int i; i = lst->len; lst->type == 4 ? ft_putstr_fd("\e[1;31m", 2) : (0); @@ -45,9 +45,9 @@ static void c_printer_node(t_clst *lst, int c_sx) ** trailing / for folder and a space in between. */ -static int c_printer_line(t_comp *c, t_clst *lst, int loop, int i) +static int c_printer_line(t_comp *c, t_clst *lst, int loop, int i) { - t_clst *ptr; + t_clst *ptr; ptr = lst->next; c_printer_node(lst, c->c_sx); @@ -72,6 +72,25 @@ static int c_printer_line(t_comp *c, t_clst *lst, int loop, int i) return (0); } +/* +** Return the right pointer in the list from the offset value calculated below. +*/ + +static t_clst *c_rolling_ptr(t_comp *c, int id) +{ + t_clst *ptr; + int i; + + i = (id - 1) * (c->m_size - 1); + ptr = c->lst; + while (i) + { + ptr = ptr->next; + i--; + } + return (ptr); +} + /* ** Controlling the offset value for the rolling list if the space in the ** terminal is too small to display the whole list. @@ -90,7 +109,6 @@ static t_clst *c_rolling(t_comp *c) ptr = c->lst; while (!ptr->cursor) ptr = ptr->next; - x = 1; while ((x * c->c_line) < ptr->id) x++; @@ -101,16 +119,7 @@ static t_clst *c_rolling(t_comp *c) c->pos_x = id; c->pos_y = y; if (y > 1) - { - x = (y - 1) * (c->m_size - 1); - ptr = c->lst; - while (x) - { - ptr = ptr->next; - x--; - } - return (ptr); - } + return (c_rolling_ptr(c, y)); return (c->lst); } @@ -118,12 +127,11 @@ static t_clst *c_rolling(t_comp *c) ** Control the number of time it cycle for LINE */ -void c_printer(t_comp *c) +void c_printer(t_comp *c) { - t_clst *ptr; - int loop; - int max_line; - //int offset; + t_clst *ptr; + int loop; + int max_line; loop = c->c_line; if (!c->c_line) @@ -134,7 +142,6 @@ void c_printer(t_comp *c) max_line = (c->c_line % (c->m_size - 1)); else max_line = c->m_size - 1; -// offset = (c->m_size - 1) - max_line; while (loop && max_line) { c_printer_line(c, ptr, c->c_pline, 1); diff --git a/42sh/src/completion/c_sizing.c b/42sh/src/completion/c_sizing.c index 10da2078..fcdb32be 100644 --- a/42sh/src/completion/c_sizing.c +++ b/42sh/src/completion/c_sizing.c @@ -6,11 +6,11 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/10 08:50:26 by alao #+# #+# */ -/* Updated: 2017/03/15 14:25:54 by gwojda ### ########.fr */ +/* Updated: 2017/03/16 08:09:00 by alao ### ########.fr */ /* */ /* ************************************************************************** */ -#include "minishell.h" +#include "completion.h" /* ** Get the max length from the list diff --git a/42sh/src/completion/c_terminal.c b/42sh/src/completion/c_terminal.c index e441d5a5..ffc880e2 100644 --- a/42sh/src/completion/c_terminal.c +++ b/42sh/src/completion/c_terminal.c @@ -6,20 +6,20 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/10/11 10:44:40 by alao #+# #+# */ -/* Updated: 2017/03/15 14:36:43 by gwojda ### ########.fr */ +/* Updated: 2017/03/16 09:02:14 by alao ### ########.fr */ /* */ /* ************************************************************************** */ -#include "minishell.h" +#include "completion.h" /* ** Clear the previous list from the screen and restore the same position. */ -void c_term_clear(t_comp *c) +void c_term_clear(t_comp *c) { - int i; - int lcmd; + int i; + int lcmd; ft_putstr(tgetstr("do", NULL)); ft_putstr(tgetstr("cd", NULL)); @@ -39,11 +39,11 @@ void c_term_clear(t_comp *c) ** the original position. */ -void c_term_mv_back(t_comp *c) +void c_term_mv_back(t_comp *c) { - int i; - int lcmd; - int value; + int i; + int lcmd; + int value; i = 0; if (c->c_line > c->win_y) @@ -71,10 +71,10 @@ void c_term_mv_back(t_comp *c) ** the first line under the prompt */ -void c_term_mv_down(t_comp *c) +void c_term_mv_down(t_comp *c) { - int i; - int value; + int i; + int value; i = 0; if (c->c_line > c->win_y) @@ -104,7 +104,7 @@ void c_term_mv_down(t_comp *c) ** and clear the previous print list. */ -int c_term_resize(t_comp *c) +int c_term_resize(t_comp *c) { struct winsize win; diff --git a/42sh/src/completion/completion.c b/42sh/src/completion/completion.c index 1eba71eb..3996ccff 100644 --- a/42sh/src/completion/completion.c +++ b/42sh/src/completion/completion.c @@ -6,11 +6,11 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/09/20 14:50:33 by alao #+# #+# */ -/* Updated: 2017/03/15 14:37:34 by gwojda ### ########.fr */ +/* Updated: 2017/03/16 09:17:11 by alao ### ########.fr */ /* */ /* ************************************************************************** */ -#include "minishell.h" +#include "completion.h" /* ** Function to select the next item in the list if it has already been created @@ -36,7 +36,7 @@ static void c_next_item(t_comp *c) ** of the cursor is restored. */ -int c_dispatcher(t_data *s) +int c_dispatcher(t_data *s) { if (s->comp && s->comp->lst == NULL) { @@ -85,10 +85,8 @@ int completion(long int keypress) return (1); if (s->comp == NULL) { - if (s->line.pos == 0) - return (0); - if (s->line.input[s->line.pos] != ' ' && - s->line.input[s->line.pos] != '\0') + if ((s->line.pos == 0) || (s->line.input[s->line.pos] != ' ' && + s->line.input[s->line.pos] != '\0')) return (0); c_init(s, keypress); if (s->comp == NULL) @@ -99,11 +97,8 @@ int completion(long int keypress) c_term_resize(s->comp); if (keypress == TOUCHE_TAB) c_next_item(s->comp); - else - { - if (c_gtfo(s->comp, keypress)) - return (1); - } + else if (c_keypress(s->comp, keypress)) + return (1); } return (c_dispatcher(s)); }