From 0c61957b6c638f77d949bf8859122495ec265610 Mon Sep 17 00:00:00 2001 From: m4s Date: Sat, 10 Dec 2016 10:45:38 +0100 Subject: [PATCH] sbenning: Replace all line-editing files --- 42sh/includes/ft_curs.h | 83 ++++++++ 42sh/includes/ft_input.h | 49 +++++ 42sh/includes/ft_readline.h | 185 ++++++++++++++++++ 42sh/includes/line_editing.h | 79 +------- 42sh/includes/line_editing.h.old | 89 +++++++++ 42sh/includes/minishell.h | 6 +- 42sh/src/line-editing/OLD/ft_clear_line.c | 21 ++ 42sh/src/line-editing/OLD/ft_cursor_left.c | 30 +++ 42sh/src/line-editing/OLD/ft_cursor_right.c | 30 +++ 42sh/src/line-editing/OLD/ft_history_add.c | 33 ++++ 42sh/src/line-editing/OLD/ft_history_down.c | 31 +++ 42sh/src/line-editing/OLD/ft_history_up.c | 29 +++ .../line-editing/OLD/ft_input_is_escaped.c | 21 ++ 42sh/src/line-editing/OLD/ft_interactive_sh.c | 68 +++++++ 42sh/src/line-editing/OLD/ft_key_ctrl_c.c | 23 +++ 42sh/src/line-editing/OLD/ft_key_ctrl_d.c | 22 +++ 42sh/src/line-editing/OLD/ft_key_default.c | 33 ++++ 42sh/src/line-editing/OLD/ft_key_del.c | 42 ++++ 42sh/src/line-editing/OLD/ft_key_enter.c | 26 +++ 42sh/src/line-editing/OLD/ft_line_down.c | 24 +++ 42sh/src/line-editing/OLD/ft_line_end.c | 20 ++ 42sh/src/line-editing/OLD/ft_line_start.c | 20 ++ 42sh/src/line-editing/OLD/ft_line_up.c | 24 +++ 42sh/src/line-editing/OLD/ft_prompt.c | 19 ++ 42sh/src/line-editing/OLD/ft_set_termios.c | 31 +++ 42sh/src/line-editing/OLD/ft_word_left.c | 25 +++ 42sh/src/line-editing/OLD/ft_word_right.c | 24 +++ 42sh/src/line-editing/OLD/input_init.c | 29 +++ 42sh/src/line-editing/OLD/qstate_dquote.c | 21 ++ 42sh/src/line-editing/OLD/qstate_none.c | 23 +++ 42sh/src/line-editing/OLD/qstate_quote.c | 19 ++ 42sh/src/line-editing/curs_ask.c | 57 ++++++ 42sh/src/line-editing/curs_clear.c | 34 ++++ 42sh/src/line-editing/curs_goto.c | 31 +++ 42sh/src/line-editing/curs_move.c | 45 +++++ 42sh/src/line-editing/curs_setup.c | 82 ++++++++ 42sh/src/line-editing/curs_single.c | 26 +++ 42sh/src/line-editing/curs_term_setup.c | 59 ++++++ 42sh/src/line-editing/curs_write.c | 63 ++++++ 42sh/src/line-editing/ft_readline.c | 146 ++++++++++++++ 42sh/src/line-editing/input_buffer.c | 77 ++++++++ 42sh/src/line-editing/interactive_sh.c | 26 +++ 42sh/src/line-editing/rl_clear_function.c | 32 +++ 42sh/src/line-editing/rl_clipboard.c | 95 +++++++++ 42sh/src/line-editing/rl_copy_function.c | 23 +++ 42sh/src/line-editing/rl_cut_function.c | 24 +++ 42sh/src/line-editing/rl_default_function.c | 35 ++++ 42sh/src/line-editing/rl_display.c | 61 ++++++ 42sh/src/line-editing/rl_dquote_function.c | 25 +++ 42sh/src/line-editing/rl_end_function.c | 25 +++ 42sh/src/line-editing/rl_esc_function.c | 20 ++ 42sh/src/line-editing/rl_home_function.c | 25 +++ 42sh/src/line-editing/rl_insert_function.c | 23 +++ 42sh/src/line-editing/rl_left_function.c | 25 +++ 42sh/src/line-editing/rl_merge_line.c | 40 ++++ 42sh/src/line-editing/rl_nl_function.c | 24 +++ 42sh/src/line-editing/rl_pagedown_function.c | 30 +++ 42sh/src/line-editing/rl_pageup_function.c | 28 +++ 42sh/src/line-editing/rl_paste_function.c | 41 ++++ 42sh/src/line-editing/rl_prompt.c | 18 ++ 42sh/src/line-editing/rl_quote_function.c | 25 +++ 42sh/src/line-editing/rl_retarr_function.c | 27 +++ 42sh/src/line-editing/rl_right_function.c | 25 +++ .../src/line-editing/rl_select_end_function.c | 27 +++ .../line-editing/rl_select_home_function.c | 27 +++ .../line-editing/rl_select_left_function.c | 27 +++ .../rl_select_pagedown_function.c | 28 +++ .../line-editing/rl_select_pageup_function.c | 27 +++ .../line-editing/rl_select_right_function.c | 28 +++ .../line-editing/rl_select_wleft_function.c | 27 +++ .../line-editing/rl_select_wright_function.c | 28 +++ 42sh/src/line-editing/rl_setup.c | 40 ++++ 42sh/src/line-editing/rl_stack_line.c | 28 +++ 42sh/src/line-editing/rl_suppr_function.c | 25 +++ 42sh/src/line-editing/rl_wleft_function.c | 45 +++++ 42sh/src/line-editing/rl_wright_function.c | 44 +++++ 76 files changed, 2822 insertions(+), 75 deletions(-) create mode 100644 42sh/includes/ft_curs.h create mode 100644 42sh/includes/ft_input.h create mode 100644 42sh/includes/ft_readline.h create mode 100644 42sh/includes/line_editing.h.old create mode 100644 42sh/src/line-editing/OLD/ft_clear_line.c create mode 100644 42sh/src/line-editing/OLD/ft_cursor_left.c create mode 100644 42sh/src/line-editing/OLD/ft_cursor_right.c create mode 100644 42sh/src/line-editing/OLD/ft_history_add.c create mode 100644 42sh/src/line-editing/OLD/ft_history_down.c create mode 100644 42sh/src/line-editing/OLD/ft_history_up.c create mode 100644 42sh/src/line-editing/OLD/ft_input_is_escaped.c create mode 100644 42sh/src/line-editing/OLD/ft_interactive_sh.c create mode 100644 42sh/src/line-editing/OLD/ft_key_ctrl_c.c create mode 100644 42sh/src/line-editing/OLD/ft_key_ctrl_d.c create mode 100644 42sh/src/line-editing/OLD/ft_key_default.c create mode 100644 42sh/src/line-editing/OLD/ft_key_del.c create mode 100644 42sh/src/line-editing/OLD/ft_key_enter.c create mode 100644 42sh/src/line-editing/OLD/ft_line_down.c create mode 100644 42sh/src/line-editing/OLD/ft_line_end.c create mode 100644 42sh/src/line-editing/OLD/ft_line_start.c create mode 100644 42sh/src/line-editing/OLD/ft_line_up.c create mode 100644 42sh/src/line-editing/OLD/ft_prompt.c create mode 100644 42sh/src/line-editing/OLD/ft_set_termios.c create mode 100644 42sh/src/line-editing/OLD/ft_word_left.c create mode 100644 42sh/src/line-editing/OLD/ft_word_right.c create mode 100644 42sh/src/line-editing/OLD/input_init.c create mode 100644 42sh/src/line-editing/OLD/qstate_dquote.c create mode 100644 42sh/src/line-editing/OLD/qstate_none.c create mode 100644 42sh/src/line-editing/OLD/qstate_quote.c create mode 100644 42sh/src/line-editing/curs_ask.c create mode 100644 42sh/src/line-editing/curs_clear.c create mode 100644 42sh/src/line-editing/curs_goto.c create mode 100644 42sh/src/line-editing/curs_move.c create mode 100644 42sh/src/line-editing/curs_setup.c create mode 100644 42sh/src/line-editing/curs_single.c create mode 100644 42sh/src/line-editing/curs_term_setup.c create mode 100644 42sh/src/line-editing/curs_write.c create mode 100644 42sh/src/line-editing/ft_readline.c create mode 100644 42sh/src/line-editing/input_buffer.c create mode 100644 42sh/src/line-editing/interactive_sh.c create mode 100644 42sh/src/line-editing/rl_clear_function.c create mode 100644 42sh/src/line-editing/rl_clipboard.c create mode 100644 42sh/src/line-editing/rl_copy_function.c create mode 100644 42sh/src/line-editing/rl_cut_function.c create mode 100644 42sh/src/line-editing/rl_default_function.c create mode 100644 42sh/src/line-editing/rl_display.c create mode 100644 42sh/src/line-editing/rl_dquote_function.c create mode 100644 42sh/src/line-editing/rl_end_function.c create mode 100644 42sh/src/line-editing/rl_esc_function.c create mode 100644 42sh/src/line-editing/rl_home_function.c create mode 100644 42sh/src/line-editing/rl_insert_function.c create mode 100644 42sh/src/line-editing/rl_left_function.c create mode 100644 42sh/src/line-editing/rl_merge_line.c create mode 100644 42sh/src/line-editing/rl_nl_function.c create mode 100644 42sh/src/line-editing/rl_pagedown_function.c create mode 100644 42sh/src/line-editing/rl_pageup_function.c create mode 100644 42sh/src/line-editing/rl_paste_function.c create mode 100644 42sh/src/line-editing/rl_prompt.c create mode 100644 42sh/src/line-editing/rl_quote_function.c create mode 100644 42sh/src/line-editing/rl_retarr_function.c create mode 100644 42sh/src/line-editing/rl_right_function.c create mode 100644 42sh/src/line-editing/rl_select_end_function.c create mode 100644 42sh/src/line-editing/rl_select_home_function.c create mode 100644 42sh/src/line-editing/rl_select_left_function.c create mode 100644 42sh/src/line-editing/rl_select_pagedown_function.c create mode 100644 42sh/src/line-editing/rl_select_pageup_function.c create mode 100644 42sh/src/line-editing/rl_select_right_function.c create mode 100644 42sh/src/line-editing/rl_select_wleft_function.c create mode 100644 42sh/src/line-editing/rl_select_wright_function.c create mode 100644 42sh/src/line-editing/rl_setup.c create mode 100644 42sh/src/line-editing/rl_stack_line.c create mode 100644 42sh/src/line-editing/rl_suppr_function.c create mode 100644 42sh/src/line-editing/rl_wleft_function.c create mode 100644 42sh/src/line-editing/rl_wright_function.c diff --git a/42sh/includes/ft_curs.h b/42sh/includes/ft_curs.h new file mode 100644 index 00000000..02f3f314 --- /dev/null +++ b/42sh/includes/ft_curs.h @@ -0,0 +1,83 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_curs.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/08 16:34:51 by sbenning #+# #+# */ +/* Updated: 2016/12/09 17:08:52 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_CURS_H +# define FT_CURS_H + +# include "libft.h" + +# include +# include +# include +# include +# include + +typedef struct s_curs t_curs; + +/* + * Cursor data : cursor's coordonate and windows size +*/ + +struct s_curs +{ + int co; + int li; + int win_co; + int win_li; +}; + +/* + * Cursor reference singleton +*/ + +t_curs *curs_single(t_curs *curs, int set); + +/* + * Cursor setup +*/ + +int curs_li_ask(void); +int curs_co_ask(void); + +int curs_term_setup(void); +void curs_win_setup(t_curs *curs); +int curs_coo_setup(t_curs *curs); +int curs_setup(t_curs *curs); + +/* + * Cursor cleanup +*/ + +int curs_term_cleanup(void); +int curs_cleanup(t_curs *curs); + +/* + * Cursor's data reset +*/ + +void curs_reset(int sig); + +/* + * Cursor manipulation function +*/ + +void curs_clear(t_curs *curs); +void curs_clear_post(t_curs *curs); +void curs_goto(t_curs *curs, int li, int co); +void curs_forward(t_curs *curs, int offset); +void curs_backward(t_curs *curs, int offset); +void curs_write(t_curs *curs, char *str, size_t len); +void curs_write_static(t_curs *curs, char *str, size_t len); +void curs_video_write(t_curs *curs, char *str, size_t len); +void curs_video_write_static(t_curs *curs, char *str, size_t len); + +#endif diff --git a/42sh/includes/ft_input.h b/42sh/includes/ft_input.h new file mode 100644 index 00000000..6b07de96 --- /dev/null +++ b/42sh/includes/ft_input.h @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_input.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/09 02:05:22 by sbenning #+# #+# */ +/* Updated: 2016/12/09 18:27:29 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_INPUT_H +# define FT_INPUT_H + +/* + * Input code mapping (!!!!LINUX!!!!) + * TODO Same for MACOSX +*/ + +# define RL_INSERT_CODE 0x7e325b1b +# define RL_CLEAR_CODE 0xc +# define RL_NL_CODE 0xa +# define RL_LEFT_CODE 0x445b1b +# define RL_RIGHT_CODE 0x435b1b +# define RL_WLEFT_CODE 0x44323b315b1b +# define RL_WRIGHT_CODE 0x43323b315b1b +# define RL_HOME_CODE 0x485b1b +# define RL_END_CODE 0x465b1b +# define RL_PAGEUP_CODE 0x7e355b1b +# define RL_PAGEDOWN_CODE 0x7e365b1b +# define RL_SELECT_RIGHT_CODE 0x43333b315b1b +# define RL_SELECT_LEFT_CODE 0x44333b315b1b +# define RL_SELECT_PAGEUP_CODE 0x7e333b355b1b +# define RL_SELECT_PAGEDOWN_CODE 0x7e333b365b1b +# define RL_SELECT_HOME_CODE 0x48333b315b1b +# define RL_SELECT_END_CODE 0x46333b315b1b +# define RL_SELECT_WRIGHT_CODE 0x43343b315b1b +# define RL_SELECT_WLEFT_CODE 0x44343b315b1b +# define RL_RETARR_CODE 0x7f +# define RL_SUPPR_CODE 0x7e335b1b +# define RL_ESC_CODE 0x5c +# define RL_QUOTE_CODE 0x27 +# define RL_DQUOTE_CODE 0x22 +# define RL_COPY_CODE 0x631b +# define RL_CUT_CODE 0x781b +# define RL_PASTE_CODE 0x761b + +#endif diff --git a/42sh/includes/ft_readline.h b/42sh/includes/ft_readline.h new file mode 100644 index 00000000..a1760ef5 --- /dev/null +++ b/42sh/includes/ft_readline.h @@ -0,0 +1,185 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_readline.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/08 18:02:25 by sbenning #+# #+# */ +/* Updated: 2016/12/09 18:26:46 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_READLINE_H +# define FT_READLINE_H + +# include "libft.h" +# include "ft_curs.h" +# include "ft_input.h" + +/* + * Default offset for dynamic allocation +*/ + +# define RL_OFFSET 1024 + +/* + * Bitset manipulation : X is the bitset and Y is the bit to manipulate + * IS : Is Y set in X + * SET : Set Y in X + * UNSET : Unset Y from X +*/ + +# define RL_IS(X, Y) (X & Y) +# define RL_SET(X, Y) (X |= Y) +# define RL_UNSET(X, Y) (X &= ~Y) + +/* + * Possible bit for t_line.bitset + * + * FINISH : bit to terminate and flush the readline + * STACK : bit to stack the current line and begin another one + * INSERT : bit to toogle insert-mode/replace-mode + * SELECT : bit to toogle extend-video-area-mode/clear-video-area-mode + * ESC : bit to escape quoting and new line + * QUOTE : bit to toogle quote-terminated-mode/quote-not-terminated-mode + * DQUOTE : bit to toogle dquote-terminated-mode/dquote-not-terminated-mode + * QUOTING : Regroup ESC, QUOTE and DQUOTE +*/ + +# define RL_FINISH 0x1 +# define RL_STACK 0x2 +# define RL_INSERT 0x4 +# define RL_SELECT 0x8 +# define RL_ESC 0x10 +# define RL_QUOTE 0x20 +# define RL_DQUOTE 0x40 +# define RL_QUOTING (RL_ESC|RL_QUOTE|RL_DQUOTE) + +typedef struct s_line t_line; +typedef struct s_input t_input; +typedef int (*t_input_function)(t_line *, long int); + +/* + * Interactive line data: + * + * bitset : global/local state and settings for readline + * prompt : current prompt + * input : destination buffer + * pos : cursor position in the destination buffer + * size : allocated size of the destination buffer + * used : actual used size in the destination buffer + * select : start position of the select area in the destination buffer + * clipboard : duplication of the copied/cuted part of the destination buffer + * stack : list of stacked line. (lines are stacked when a quoted new line appear) +*/ + +struct s_line +{ + int bitset; + char *prompt; + char *input; + int pos; + int size; + int used; + int select; + char *clipboard; + t_curs curs; + t_list *stack; +}; + +/* + * Input data: + * code : input identifier + * function : input handler +*/ + +struct s_input +{ + long int code; + t_input_function function; +}; + +/* + * Readline setup/cleanup/teardown +*/ + +int rl_setup(t_line *line); +int rl_cleanup(t_line *line); +void rl_teardown(t_line *line); + +/* + * Dynamic allocated buffer manipulation +*/ + +int input_maj(t_line *line, char *str, int size); +int input_move(t_line *line, char *str, int size); +void input_remove(t_line *line, int size); + +/* + * Readline internal function +*/ + +void rl_put_prompt(t_line *line); +int rl_clipboard_new(t_line *line); +int rl_clipboard_new_cut(t_line *line); +int rl_stack_line(t_line *line); +int rl_merge_line(t_line *line); + +/* + * Realine display functions: + * + * reset_display : Redraw the interactive buffer and replace the cursor + * based on t_line.input, t_line.pos and t_line.select . + * reset_display_ante : Redraw before the cursor position. + * reset_display_post : Redraw after the cursor position. +*/ + +void rl_reset_display(t_line *line); +void rl_reset_display_ante(t_line *line); +void rl_reset_display_post(t_line *line); + +/* + * input handler functions + * + * An handler use curs_ to manipulate the cursor position + * and recalc t_line.pos based on the cursor movement. + * + * An handler can also modify the content of the destination buffer. + * If it do so, it must reset all display that appear + * after the first index of that modification. + * If t_line.pos is that index, rl_reset_display_post can simply do the work. +*/ + +int rl_default_function(t_line *line, long int input); /* Not handled input */ +int rl_esc_function(t_line *line, long int input); /* Backslash */ +int rl_quote_function(t_line *line, long int input); /* Simple quote */ +int rl_dquote_function(t_line *line, long int input); /* Double quote */ +int rl_nl_function(t_line *line, long int input); /* New line */ +int rl_left_function(t_line *line, long int input); /* Left move */ +int rl_right_function(t_line *line, long int input); /* Right move */ +int rl_wleft_function(t_line *line, long int input); /* Word left move */ +int rl_wright_function(t_line *line, long int input); /* Word right move */ +int rl_home_function(t_line *line, long int input); /* Home move */ +int rl_end_function(t_line *line, long int input); /* End move */ +int rl_pageup_function(t_line *line, long int input); /* Pageup move */ +int rl_pagedown_function(t_line *line, long int input); /* Pagedown move*/ +int rl_select_left_function(t_line *line, long int input); /* Left extend video area */ +int rl_select_right_function(t_line *line, long int input); /* Right extend video area */ +int rl_select_wleft_function(t_line *line, long int input); /* Word left extend video area */ +int rl_select_wright_function(t_line *line, long int input); /* Word right extend video area */ +int rl_select_home_function(t_line *line, long int input); /* Home extend video area */ +int rl_select_end_function(t_line *line, long int input); /* End extend video area */ +int rl_select_pageup_function(t_line *line, long int input); /* Pageup extend video area */ +int rl_select_pagedown_function(t_line *line, long int input); /* Pagedown extend video area */ +int rl_retarr_function(t_line *line, long int input); /* Remove before the cursor */ +int rl_suppr_function(t_line *line, long int input); /* Remove after the cursor */ +int rl_clear_function(t_line *line, long int input); /* Clear screen */ +int rl_copy_function(t_line *line, long int input); /* Copy selected area or current cursor line */ +int rl_cut_function(t_line *line, long int input); /* Cut selected area or current cursor line */ +int rl_paste_function(t_line *line, long int input); /* Paste copied/cuted area */ +int rl_insert_function(t_line *line, long int input); /* Toogle insert-mode/replacement-mode */ + +int ft_readline(t_line *line); + +#endif diff --git a/42sh/includes/line_editing.h b/42sh/includes/line_editing.h index 435c9444..19c01177 100644 --- a/42sh/includes/line_editing.h +++ b/42sh/includes/line_editing.h @@ -1,89 +1,24 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* line-editing.h :+: :+: :+: */ +/* line_editing.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ +/* By: sbenning +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2016/11/10 13:38:21 by jhalford #+# #+# */ -/* Updated: 2016/12/07 16:57:40 by jhalford ### ########.fr */ +/* Created: 2016/12/10 09:41:50 by sbenning #+# #+# */ +/* Updated: 2016/12/10 10:24:12 by sbenning ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef LINE_EDITING_H # define LINE_EDITING_H +# include "ft_readline.h" # include "minishell.h" -# include -# include - -# define SHELL_PROMPT "$> " -# define BUFF_SIZE 32 -# define READ_BUF 32 -# define FT_KEY_C_C "\x3" -# define FT_KEY_C_D "\x4" -# define FT_KEY_C_H "\x8" -# define FT_KEY_TAB "\x9" -# define FT_KEY_ENTER "\xa" -# define FT_KEY_C_K "\xb" -# define FT_KEY_C_L "\xc" -# define FT_KEY_C_U "\x15" -# define FT_KEY_C_Z "\x1a" -# define FT_KEY_ESC "\x1b" -# define FT_KEY_UP "\x1b\x5b\x41" -# define FT_KEY_DOWN "\x1b\x5b\x42" -# define FT_KEY_RIGHT "\x1b\x5b\x43" -# define FT_KEY_LEFT "\x1b\x5b\x44" -# define FT_KEY_C_UP "\x1b\x4f\x41" -# define FT_KEY_C_DOWN "\x1b\x4f\x42" -# define FT_KEY_C_RIGHT "\x1b\x4f\x43" -# define FT_KEY_C_LEFT "\x1b\x4f\x44" -# define FT_KEY_SQUOTE "\x22" -# define FT_KEY_DQUOTE "\x27" -# define FT_KEY_BSLASH "\x5c" -# define FT_KEY_DEL "\x7f" - -enum e_qstate -{ - Q_NONE, - Q_QUOTE, - Q_DQUOTE, - Q_BACKSLASH, -}; typedef struct s_data t_data; -typedef enum e_qstate t_qstate; -extern t_stof g_keys[]; - -int ft_interactive_sh(t_data *data); -int input_init(t_data *data); -int ft_set_termios(t_data *data, int input_mode); -int ft_prompt(void); - -int ft_history_add(t_data *data); - -int ft_clear_line(t_data *data, char *buf); -int ft_line_up(t_data *data, char *buf); -int ft_line_down(t_data *data, char *buf); -int ft_line_start(t_data *data, char *buf); -int ft_line_end(t_data *data, char *buf); - -int ft_key_del(t_data *data, char *buf); -int ft_key_enter(t_data *data, char *buf); -int ft_key_ctrl_d(t_data *data, char *buf); -int ft_key_ctrl_c(t_data *data, char *buf); -int ft_key_default(t_data *data, char *buf); - -int ft_history_up(t_data *data, char *buf); -int ft_history_down(t_data *data, char *buf); -int ft_cursor_left(t_data *data, char *buf); -int ft_cursor_right(t_data *data, char *buf); -int ft_word_left(t_data *data, char *buf); -int ft_word_right(t_data *data, char *buf); - -void qstate_none(t_qstate *new, char c); -void qstate_quote(t_qstate *new, char c); -void qstate_dquote(t_qstate *new, char c); +t_data **data_singleton(void); +int ft_interactive_sh(t_data *data); #endif diff --git a/42sh/includes/line_editing.h.old b/42sh/includes/line_editing.h.old new file mode 100644 index 00000000..435c9444 --- /dev/null +++ b/42sh/includes/line_editing.h.old @@ -0,0 +1,89 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* line-editing.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/10 13:38:21 by jhalford #+# #+# */ +/* Updated: 2016/12/07 16:57:40 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LINE_EDITING_H +# define LINE_EDITING_H + +# include "minishell.h" +# include +# include + +# define SHELL_PROMPT "$> " +# define BUFF_SIZE 32 +# define READ_BUF 32 +# define FT_KEY_C_C "\x3" +# define FT_KEY_C_D "\x4" +# define FT_KEY_C_H "\x8" +# define FT_KEY_TAB "\x9" +# define FT_KEY_ENTER "\xa" +# define FT_KEY_C_K "\xb" +# define FT_KEY_C_L "\xc" +# define FT_KEY_C_U "\x15" +# define FT_KEY_C_Z "\x1a" +# define FT_KEY_ESC "\x1b" +# define FT_KEY_UP "\x1b\x5b\x41" +# define FT_KEY_DOWN "\x1b\x5b\x42" +# define FT_KEY_RIGHT "\x1b\x5b\x43" +# define FT_KEY_LEFT "\x1b\x5b\x44" +# define FT_KEY_C_UP "\x1b\x4f\x41" +# define FT_KEY_C_DOWN "\x1b\x4f\x42" +# define FT_KEY_C_RIGHT "\x1b\x4f\x43" +# define FT_KEY_C_LEFT "\x1b\x4f\x44" +# define FT_KEY_SQUOTE "\x22" +# define FT_KEY_DQUOTE "\x27" +# define FT_KEY_BSLASH "\x5c" +# define FT_KEY_DEL "\x7f" + +enum e_qstate +{ + Q_NONE, + Q_QUOTE, + Q_DQUOTE, + Q_BACKSLASH, +}; + +typedef struct s_data t_data; +typedef enum e_qstate t_qstate; + +extern t_stof g_keys[]; + +int ft_interactive_sh(t_data *data); +int input_init(t_data *data); +int ft_set_termios(t_data *data, int input_mode); +int ft_prompt(void); + +int ft_history_add(t_data *data); + +int ft_clear_line(t_data *data, char *buf); +int ft_line_up(t_data *data, char *buf); +int ft_line_down(t_data *data, char *buf); +int ft_line_start(t_data *data, char *buf); +int ft_line_end(t_data *data, char *buf); + +int ft_key_del(t_data *data, char *buf); +int ft_key_enter(t_data *data, char *buf); +int ft_key_ctrl_d(t_data *data, char *buf); +int ft_key_ctrl_c(t_data *data, char *buf); +int ft_key_default(t_data *data, char *buf); + +int ft_history_up(t_data *data, char *buf); +int ft_history_down(t_data *data, char *buf); +int ft_cursor_left(t_data *data, char *buf); +int ft_cursor_right(t_data *data, char *buf); +int ft_word_left(t_data *data, char *buf); +int ft_word_right(t_data *data, char *buf); + +void qstate_none(t_qstate *new, char c); +void qstate_quote(t_qstate *new, char c); +void qstate_dquote(t_qstate *new, char c); + +#endif diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index 729590a7..1c48ca09 100644 --- a/42sh/includes/minishell.h +++ b/42sh/includes/minishell.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */ -/* Updated: 2016/12/07 18:09:27 by jhalford ### ########.fr */ +/* Updated: 2016/12/10 10:28:20 by sbenning ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,7 +31,7 @@ typedef long long t_type; typedef struct s_line t_line; typedef struct s_comp t_comp; typedef struct s_exec t_exec; - +/* struct s_line { t_dlist *history; @@ -39,7 +39,7 @@ struct s_line t_list *qstack; char *input; }; - +*/ struct s_comp { int a; diff --git a/42sh/src/line-editing/OLD/ft_clear_line.c b/42sh/src/line-editing/OLD/ft_clear_line.c new file mode 100644 index 00000000..24648a70 --- /dev/null +++ b/42sh/src/line-editing/OLD/ft_clear_line.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_clear_line.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/10 13:42:45 by jhalford #+# #+# */ +/* Updated: 2016/11/10 13:42:59 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "line_editing.h" + +int ft_clear_line(t_data *data, char *buf) +{ + ft_line_end(data, buf); + while (ft_key_del(data, buf)) + ; + return (1); +} diff --git a/42sh/src/line-editing/OLD/ft_cursor_left.c b/42sh/src/line-editing/OLD/ft_cursor_left.c new file mode 100644 index 00000000..62ba6d74 --- /dev/null +++ b/42sh/src/line-editing/OLD/ft_cursor_left.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_cursor_left.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/10 13:43:10 by jhalford #+# #+# */ +/* Updated: 2016/12/03 15:26:16 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "line_editing.h" + +int ft_cursor_left(t_data *data, char *buf) +{ + char *res; + + res = NULL; + (void)buf; + if (!data->line.input_pos) + return (0); + if (data->line.input[data->line.input_pos - 1] == '\n') + return (0); + data->line.input_pos--; + if (!(res = tgetstr("le", NULL))) + return (-1); + tputs(tgoto(res, 0, 0), 0, &ft_putchar); + return (1); +} diff --git a/42sh/src/line-editing/OLD/ft_cursor_right.c b/42sh/src/line-editing/OLD/ft_cursor_right.c new file mode 100644 index 00000000..60ddedc4 --- /dev/null +++ b/42sh/src/line-editing/OLD/ft_cursor_right.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_cursor_right.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/10 13:43:31 by jhalford #+# #+# */ +/* Updated: 2016/12/03 15:26:46 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "line_editing.h" + +int ft_cursor_right(t_data *data, char *buf) +{ + char *res; + + res = NULL; + (void)buf; + if (!data->line.input[data->line.input_pos]) + return (0); + if (data->line.input[data->line.input_pos] == '\n') + return (0); + data->line.input_pos++; + if ((res = tgetstr("nd", NULL)) == NULL) + return (-1); + tputs(tgoto(res, 0, 0), 0, &ft_putchar); + return (1); +} diff --git a/42sh/src/line-editing/OLD/ft_history_add.c b/42sh/src/line-editing/OLD/ft_history_add.c new file mode 100644 index 00000000..95490c18 --- /dev/null +++ b/42sh/src/line-editing/OLD/ft_history_add.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_history_add.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/10 13:43:40 by jhalford #+# #+# */ +/* Updated: 2016/12/07 16:52:48 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "line_editing.h" + +int ft_history_add(t_data *data) +{ + t_dlist *new; + char *str; + + str = data->line.input; + if (data->line.history) + while (data->line.history->next) + data->line.history = data->line.history->next; + if (!data->line.history->prev + || ft_strcmp(str, (char *)data->line.history->prev->content)) + { + new = ft_dlstnew(str, sizeof(char) * (ft_strlen(str) + 1)); + ft_dlstadd_before(&data->line.history, new); + data->line.history = data->line.history->next; + ft_strdel((char **)&data->line.history->content); + } + return (0); +} diff --git a/42sh/src/line-editing/OLD/ft_history_down.c b/42sh/src/line-editing/OLD/ft_history_down.c new file mode 100644 index 00000000..989786a9 --- /dev/null +++ b/42sh/src/line-editing/OLD/ft_history_down.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_history_down.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/10 13:44:06 by jhalford #+# #+# */ +/* Updated: 2016/12/03 15:27:08 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "line_editing.h" + +int ft_history_down(t_data *data, char *buf) +{ + int i; + char *str; + + (void)buf; + if (!data->line.history) + return (0); + if (!data->line.history->next) + return (0); + data->line.history = data->line.history->next; + str = data->line.history->content; + i = 0; + if (!data->line.history->next) + ft_strdel(&str); + return (0); +} diff --git a/42sh/src/line-editing/OLD/ft_history_up.c b/42sh/src/line-editing/OLD/ft_history_up.c new file mode 100644 index 00000000..ccc01d64 --- /dev/null +++ b/42sh/src/line-editing/OLD/ft_history_up.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_history_up.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/10 13:44:15 by jhalford #+# #+# */ +/* Updated: 2016/12/07 16:52:28 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "line_editing.h" + +int ft_history_up(t_data *data, char *buf) +{ + int i; + char *str; + + (void)buf; + if (!data->line.history) + return (0); + if (!data->line.history->prev) + return (0); + data->line.history = data->line.history->prev; + str = data->line.history->content; + i = 0; + return (0); +} diff --git a/42sh/src/line-editing/OLD/ft_input_is_escaped.c b/42sh/src/line-editing/OLD/ft_input_is_escaped.c new file mode 100644 index 00000000..069a9647 --- /dev/null +++ b/42sh/src/line-editing/OLD/ft_input_is_escaped.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_input_is_escaped.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/10 13:44:17 by jhalford #+# #+# */ +/* Updated: 2016/11/10 13:44:17 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "line_editing.h" + +int ft_input_is_escaped(t_dlist *input_chain) +{ + if (*(char *)input_chain->content == '\\') + return (1); + else + return (0); +} diff --git a/42sh/src/line-editing/OLD/ft_interactive_sh.c b/42sh/src/line-editing/OLD/ft_interactive_sh.c new file mode 100644 index 00000000..ece0bda1 --- /dev/null +++ b/42sh/src/line-editing/OLD/ft_interactive_sh.c @@ -0,0 +1,68 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_interactive_sh.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/01 12:14:12 by jhalford #+# #+# */ +/* Updated: 2016/12/07 17:27:25 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "line_editing.h" + +t_stof g_keys[] = { + {FT_KEY_C_K, NULL}, + {FT_KEY_TAB, &ft_key_default}, + {FT_KEY_C_H, &ft_line_start}, + {FT_KEY_C_L, &ft_line_end}, + {FT_KEY_C_U, &ft_clear_line}, + {FT_KEY_ESC, NULL}, + /* {FT_KEY_UP, &ft_history_up}, */ + /* {FT_KEY_DOWN, &ft_history_down}, */ + {FT_KEY_UP, NULL}, + {FT_KEY_DOWN, NULL}, + /* {FT_KEY_C_UP, &ft_line_up}, */ + /* {FT_KEY_C_DOWN, &ft_line_down}, */ + {FT_KEY_C_UP, NULL}, + {FT_KEY_C_DOWN, NULL}, + {FT_KEY_RIGHT, &ft_cursor_right}, + {FT_KEY_LEFT, &ft_cursor_left}, + {FT_KEY_C_RIGHT, &ft_word_right}, + {FT_KEY_C_LEFT, &ft_word_left}, + {FT_KEY_ENTER, &ft_key_enter}, + {FT_KEY_DEL, &ft_key_del}, + {FT_KEY_C_D, &ft_key_ctrl_d}, + {FT_KEY_C_C, &ft_key_ctrl_c}, + {FT_KEY_C_Z, NULL}, + {NULL, &ft_key_default}, +}; + +int ft_interactive_sh(t_data *data) +{ + char buf[4]; + int ret; + int i; + + if ((input_init(data))) + return (-1); + while (1) + { + ft_bzero(buf, 4); + ret = read(0, buf, 4); + i = 0; + while (g_keys[i].name && ft_strcmp(buf, g_keys[i].name)) + i++; + if (!g_keys[i].f) + continue ; + if ((ret = (*g_keys[i].f)(data, buf)) < 0) + return (-1); + else if (ret == 2) + { + ft_lstdel(&data->line.qstack, &ft_lst_cfree); + ft_set_termios(data, 0); + return (0); + } + } +} diff --git a/42sh/src/line-editing/OLD/ft_key_ctrl_c.c b/42sh/src/line-editing/OLD/ft_key_ctrl_c.c new file mode 100644 index 00000000..eebb7d02 --- /dev/null +++ b/42sh/src/line-editing/OLD/ft_key_ctrl_c.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_key_ctrl_c.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/10 13:44:24 by jhalford #+# #+# */ +/* Updated: 2016/12/03 15:29:09 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "line_editing.h" + +int ft_key_ctrl_c(t_data *data, char *buf) +{ + (void)buf; + ft_strdel(&data->line.input); + data->line.input = ft_memalloc(10); + ft_putendl(""); + ft_prompt(); + return (0); +} diff --git a/42sh/src/line-editing/OLD/ft_key_ctrl_d.c b/42sh/src/line-editing/OLD/ft_key_ctrl_d.c new file mode 100644 index 00000000..c882cc99 --- /dev/null +++ b/42sh/src/line-editing/OLD/ft_key_ctrl_d.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_key_ctrl_d.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/10 13:44:24 by jhalford #+# #+# */ +/* Updated: 2016/12/07 18:12:29 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "line_editing.h" + +int ft_key_ctrl_d(t_data *data, char *buf) +{ + (void)data; + (void)buf; + data_exit(data); + ft_putendl("exit"); + exit(0); +} diff --git a/42sh/src/line-editing/OLD/ft_key_default.c b/42sh/src/line-editing/OLD/ft_key_default.c new file mode 100644 index 00000000..4cb66621 --- /dev/null +++ b/42sh/src/line-editing/OLD/ft_key_default.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_key_default.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/28 18:45:23 by jhalford #+# #+# */ +/* Updated: 2016/12/09 14:48:01 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "line_editing.h" + +int ft_key_default(t_data *data, char *buf) +{ + char *res; + char *tmp; + + tmp = data->line.input; + data->line.input = ft_strinsert(data->line.input, *buf, data->line.input_pos); + data->line.input_pos++; + ft_strdel(&tmp); + qstate_update(data, *buf); + if ((res = tgetstr("IC", NULL))) + { + tputs(tgoto(res, 0, 0), 1, &ft_putchar); + ft_putchar(buf[0]); + return (0); + } + else + return (-1); +} diff --git a/42sh/src/line-editing/OLD/ft_key_del.c b/42sh/src/line-editing/OLD/ft_key_del.c new file mode 100644 index 00000000..4ce4e6fd --- /dev/null +++ b/42sh/src/line-editing/OLD/ft_key_del.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_key_del.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/29 15:57:43 by jhalford #+# #+# */ +/* Updated: 2016/12/03 15:30:02 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "line_editing.h" + +int ft_key_del(t_data *data, char *buf) +{ + char *res; + + (void)data; + (void)buf; + if (!data->line.input_pos) + return (0); + if (data->line.input[data->line.input_pos - 1] == '\n') + return (0); + qstate_update(data, -1); + ft_strcpy(data->line.input + data->line.input_pos - 1, + data->line.input + data->line.input_pos); + data->line.input_pos--; + if ((res = tgetstr("le", NULL)) == NULL) + { + ft_printf("le error\n"); + return (-1); + } + tputs(tgoto(res, 0, 0), 0, &ft_putchar); + if ((res = tgetstr("DC", NULL)) == NULL) + { + ft_printf("DC error\n"); + return (-1); + } + tputs(tgoto(res, 0, 0), 0, &ft_putchar); + return (1); +} diff --git a/42sh/src/line-editing/OLD/ft_key_enter.c b/42sh/src/line-editing/OLD/ft_key_enter.c new file mode 100644 index 00000000..715b48a6 --- /dev/null +++ b/42sh/src/line-editing/OLD/ft_key_enter.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_key_enter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/29 15:56:36 by jhalford #+# #+# */ +/* Updated: 2016/12/07 17:35:06 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "line_editing.h" + +int ft_key_enter(t_data *data, char *buf) +{ + (void)buf; + if (*(t_qstate*)data->line.qstack->content == Q_NONE) + { + ft_putchar('\n'); + return (2); + } + ft_key_default(data, buf); + ft_printf("> "); + return (0); +} diff --git a/42sh/src/line-editing/OLD/ft_line_down.c b/42sh/src/line-editing/OLD/ft_line_down.c new file mode 100644 index 00000000..b04e747d --- /dev/null +++ b/42sh/src/line-editing/OLD/ft_line_down.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_line_down.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/10 13:44:31 by jhalford #+# #+# */ +/* Updated: 2016/11/10 13:44:31 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "line_editing.h" + +int ft_line_go_down(t_dlist **input_chain) +{ + char *res; + + (void)input_chain; + if ((res = tgetstr("up", NULL)) == NULL) + return (-1); + tputs(tgoto(res, 0, 0), 0, &ft_putchar); + return (0); +} diff --git a/42sh/src/line-editing/OLD/ft_line_end.c b/42sh/src/line-editing/OLD/ft_line_end.c new file mode 100644 index 00000000..df7d2395 --- /dev/null +++ b/42sh/src/line-editing/OLD/ft_line_end.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_line_end.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/10 13:44:33 by jhalford #+# #+# */ +/* Updated: 2016/11/10 13:44:33 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "line_editing.h" + +int ft_line_end(t_data *data, char *buf) +{ + while (ft_cursor_right(data, buf)) + ; + return (0); +} diff --git a/42sh/src/line-editing/OLD/ft_line_start.c b/42sh/src/line-editing/OLD/ft_line_start.c new file mode 100644 index 00000000..354b4e74 --- /dev/null +++ b/42sh/src/line-editing/OLD/ft_line_start.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_line_start.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/10 13:44:34 by jhalford #+# #+# */ +/* Updated: 2016/11/10 13:44:34 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "line_editing.h" + +int ft_line_start(t_data *data, char *buf) +{ + while (ft_cursor_left(data, buf)) + ; + return (0); +} diff --git a/42sh/src/line-editing/OLD/ft_line_up.c b/42sh/src/line-editing/OLD/ft_line_up.c new file mode 100644 index 00000000..15804028 --- /dev/null +++ b/42sh/src/line-editing/OLD/ft_line_up.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_line_up.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/10 13:44:35 by jhalford #+# #+# */ +/* Updated: 2016/11/10 13:44:35 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "line_editing.h" + +int ft_line_go_up(t_dlist **input_chain) +{ + char *res; + + (void)input_chain; + if ((res = tgetstr("up", NULL)) == NULL) + return (-1); + tputs(tgoto(res, 0, 0), 0, &ft_putchar); + return (0); +} diff --git a/42sh/src/line-editing/OLD/ft_prompt.c b/42sh/src/line-editing/OLD/ft_prompt.c new file mode 100644 index 00000000..32aa615e --- /dev/null +++ b/42sh/src/line-editing/OLD/ft_prompt.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_prompt.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/10 14:22:34 by jhalford #+# #+# */ +/* Updated: 2016/11/28 15:16:30 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "line_editing.h" + +int ft_prompt(void) +{ + ft_putstr(SHELL_PROMPT); + return (0); +} diff --git a/42sh/src/line-editing/OLD/ft_set_termios.c b/42sh/src/line-editing/OLD/ft_set_termios.c new file mode 100644 index 00000000..f51cb9de --- /dev/null +++ b/42sh/src/line-editing/OLD/ft_set_termios.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_set_termios.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/01 12:14:09 by jhalford #+# #+# */ +/* Updated: 2016/12/07 14:20:59 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "line_editing.h" + +int ft_set_termios(t_data *data, int input_mode) +{ + struct termios term; + + (void)data; + if (tcgetattr(0, &term) == -1) + return (-1); + if (input_mode) + term.c_lflag &= ~(ICANON) & ~(ISIG) & ~(ECHO); + else + term.c_lflag |= ICANON | ISIG | ECHO; + term.c_cc[VMIN] = 1; + term.c_cc[VTIME] = 0; + if (tcsetattr(0, TCSADRAIN, &term) == -1) + return (-1); + return (0); +} diff --git a/42sh/src/line-editing/OLD/ft_word_left.c b/42sh/src/line-editing/OLD/ft_word_left.c new file mode 100644 index 00000000..6689c508 --- /dev/null +++ b/42sh/src/line-editing/OLD/ft_word_left.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_word_left.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/10 13:44:36 by jhalford #+# #+# */ +/* Updated: 2016/12/03 15:31:10 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "line_editing.h" + +int ft_word_left(t_data *data, char *buf) +{ + ft_cursor_left(data, buf); + while (FT_WS(data->line.input[data->line.input_pos - 1])) + if (!ft_cursor_left(data, buf)) + return (0); + while (!FT_WS(data->line.input[data->line.input_pos - 1])) + if (!ft_cursor_left(data, buf)) + return (0); + return (0); +} diff --git a/42sh/src/line-editing/OLD/ft_word_right.c b/42sh/src/line-editing/OLD/ft_word_right.c new file mode 100644 index 00000000..7999d2df --- /dev/null +++ b/42sh/src/line-editing/OLD/ft_word_right.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_word_right.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/10 13:44:37 by jhalford #+# #+# */ +/* Updated: 2016/12/03 15:31:25 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "line_editing.h" + +int ft_word_right(t_data *data, char *buf) +{ + while (!FT_WS(data->line.input[data->line.input_pos])) + if (!ft_cursor_right(data, buf)) + return (0); + while (FT_WS(data->line.input[data->line.input_pos])) + if (!ft_cursor_right(data, buf)) + return (0); + return (0); +} diff --git a/42sh/src/line-editing/OLD/input_init.c b/42sh/src/line-editing/OLD/input_init.c new file mode 100644 index 00000000..bea6a347 --- /dev/null +++ b/42sh/src/line-editing/OLD/input_init.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* input_init.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/03 13:35:03 by jhalford #+# #+# */ +/* Updated: 2016/12/07 16:30:40 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int input_init(t_data *data) +{ + char null; + + null = '\0'; + ft_strdel(&data->line.input); + data->line.input = ft_memalloc(10); + data->line.input_pos = 0; + data->line.qstack = ft_lstnew(NULL, sizeof(t_qstate)); + *((t_qstate*)data->line.qstack->content) = Q_NONE; + if (ft_set_termios(data, 1)) + return (-1); + ft_prompt(); + return (0); +} diff --git a/42sh/src/line-editing/OLD/qstate_dquote.c b/42sh/src/line-editing/OLD/qstate_dquote.c new file mode 100644 index 00000000..15cd7f93 --- /dev/null +++ b/42sh/src/line-editing/OLD/qstate_dquote.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* qstate_dquote.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/03 12:10:09 by jhalford #+# #+# */ +/* Updated: 2016/12/03 12:26:09 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "line_editing.h" + +void qstate_dquote(t_qstate *new, char c) +{ + if (c == '\\') + *new = Q_BACKSLASH; + else if (c == '\"') + *new = Q_NONE; +} diff --git a/42sh/src/line-editing/OLD/qstate_none.c b/42sh/src/line-editing/OLD/qstate_none.c new file mode 100644 index 00000000..f1f2e8c4 --- /dev/null +++ b/42sh/src/line-editing/OLD/qstate_none.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* qstate_none.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/03 12:08:48 by jhalford #+# #+# */ +/* Updated: 2016/12/03 12:51:29 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "line_editing.h" + +void qstate_none(t_qstate *new, char c) +{ + if (c == '\\') + *new = Q_BACKSLASH; + else if (c == '\"') + *new = Q_DQUOTE; + else if (c == '\'') + *new = Q_QUOTE; +} diff --git a/42sh/src/line-editing/OLD/qstate_quote.c b/42sh/src/line-editing/OLD/qstate_quote.c new file mode 100644 index 00000000..3e76a128 --- /dev/null +++ b/42sh/src/line-editing/OLD/qstate_quote.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* qstate_quote.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/03 12:10:09 by jhalford #+# #+# */ +/* Updated: 2016/12/03 12:26:06 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "line_editing.h" + +void qstate_quote(t_qstate *new, char c) +{ + if (c == '\'') + *new = Q_NONE; +} diff --git a/42sh/src/line-editing/curs_ask.c b/42sh/src/line-editing/curs_ask.c new file mode 100644 index 00000000..e465b8c7 --- /dev/null +++ b/42sh/src/line-editing/curs_ask.c @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* curs_ask.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/08 17:03:49 by sbenning #+# #+# */ +/* Updated: 2016/12/09 16:47:13 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_curs.h" + +/* + * Read the line number for cursor's coordonate +*/ + +int curs_li_ask(void) +{ + char buff; + int li; + + li = 0; + buff = 0; + while (buff != ';') + { + buff = 0; + if (read(0, &buff, 1) < 0) + return (-1); + if (ft_isdigit(buff)) + li = li * 10 + (buff - '0'); + } + return (li); +} + +/* + * Read the colomn number for cursor's coordonate +*/ + +int curs_co_ask(void) +{ + char buff; + int co; + + co = 0; + buff = 0; + while (buff != 'R') + { + buff = 0; + if (read(0, &buff, 1) < 0) + return (-1); + if (ft_isdigit(buff)) + co = co * 10 + (buff - '0'); + } + return (co); +} diff --git a/42sh/src/line-editing/curs_clear.c b/42sh/src/line-editing/curs_clear.c new file mode 100644 index 00000000..af2f6b03 --- /dev/null +++ b/42sh/src/line-editing/curs_clear.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* curs_clear.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/08 17:28:50 by sbenning #+# #+# */ +/* Updated: 2016/12/09 16:51:33 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_curs.h" + +/* + * Clear screen and keep track of cursoe's coordonate +*/ + +void curs_clear(t_curs *curs) +{ + ft_putstr(tgetstr("cl", NULL)); + curs->li = 0; + curs->co = 0; +} + +/* + * Clear screen after cursor position without moving cursor +*/ + +void curs_clear_post(t_curs *curs) +{ + ft_putstr(tgetstr("cd", NULL)); + (void)curs; +} diff --git a/42sh/src/line-editing/curs_goto.c b/42sh/src/line-editing/curs_goto.c new file mode 100644 index 00000000..389c982d --- /dev/null +++ b/42sh/src/line-editing/curs_goto.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* curs_goto.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/08 17:10:22 by sbenning #+# #+# */ +/* Updated: 2016/12/09 16:58:26 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_curs.h" + +/* + * Move the cursor to the given coordonate // scroll screen if line is unreachable +*/ + +void curs_goto(t_curs *curs, int li, int co) +{ + if (co >= curs->win_co) + co = curs->win_co - 1; + if (li >= curs->win_li) + { + ft_putstr(tgetstr("sf", NULL)); + li = curs->win_li - 1; + } + ft_putstr(tgoto(tgetstr("cm", NULL), co, li)); + curs->li = li; + curs->co = co; +} diff --git a/42sh/src/line-editing/curs_move.c b/42sh/src/line-editing/curs_move.c new file mode 100644 index 00000000..d96d0ec1 --- /dev/null +++ b/42sh/src/line-editing/curs_move.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* curs_forward.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/08 17:21:08 by sbenning #+# #+# */ +/* Updated: 2016/12/09 16:56:41 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_curs.h" + +/* + * Move the cursor forward (right/down) +*/ + +void curs_forward(t_curs *curs, int offset) +{ + int size; + int div; + int mod; + + size = (curs->li * curs->win_co + curs->co) + offset; + div = size / curs->win_co; + mod = size % curs->win_co; + curs_goto(curs, div, mod); +} + +/* + * Move the cursor backward (left/up) +*/ + +void curs_backward(t_curs *curs, int offset) +{ + int size; + int div; + int mod; + + size = (curs->li * curs->win_co + curs->co) - offset; + div = size / curs->win_co; + mod = size % curs->win_co; + curs_goto(curs, div, mod); +} diff --git a/42sh/src/line-editing/curs_setup.c b/42sh/src/line-editing/curs_setup.c new file mode 100644 index 00000000..411b9abf --- /dev/null +++ b/42sh/src/line-editing/curs_setup.c @@ -0,0 +1,82 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* curs_setup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/08 16:36:43 by sbenning #+# #+# */ +/* Updated: 2016/12/09 16:46:01 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_curs.h" + +/* + * Set coordonate cursor data + * "\033[6n" ask for terminal to write on standard entry the cursor's coordonate +*/ + +int curs_coo_setup(t_curs *curs) +{ + ft_putstr("\033[6n"); + curs->li = curs_li_ask() - 1; + curs->co = curs_co_ask() - 1; + return ((curs->li < 0 || curs->co < 0 ? -1 : 0)); +} + +/* + * Set windows cursor data +*/ + +void curs_win_setup(t_curs *curs) +{ + struct winsize ws; + + ioctl(0, TIOCGWINSZ, &ws); + curs->win_li = ws.ws_row; + curs->win_co = ws.ws_col; +} + +/* + * Reset saved cursor data +*/ + +void curs_reset(int sig) +{ + t_curs *curs; + + curs = curs_single(NULL, 0); + curs_win_setup(curs); + curs_coo_setup(curs); + return ; + (void)sig; +} + +/* + * Save the cursor reference, init termcap and set cursor data +*/ + +int curs_setup(t_curs *curs) +{ + curs_single(curs, 1); + signal(SIGWINCH, curs_reset); + if (tgetent(NULL, "xterm") != 1) + return (-1); + if (curs_term_setup() < 0) + return (-1); + curs_win_setup(curs); + if (curs_coo_setup(curs) < 0) + return (-1); + return (0); +} + +/* + * Erase cursor data and reset terminal +*/ + +int curs_cleanup(t_curs *curs) +{ + ft_bzero(curs, sizeof(t_curs)); + return (curs_term_cleanup()); +} diff --git a/42sh/src/line-editing/curs_single.c b/42sh/src/line-editing/curs_single.c new file mode 100644 index 00000000..a7c883a9 --- /dev/null +++ b/42sh/src/line-editing/curs_single.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* curs_single.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/08 17:04:53 by sbenning #+# #+# */ +/* Updated: 2016/12/09 16:40:41 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_curs.h" + +/* + * Save cursor reference or return a previous saved reference +*/ + +t_curs *curs_single(t_curs *curs, int set) +{ + static t_curs *single; + + if (set) + single = curs; + return (single); +} diff --git a/42sh/src/line-editing/curs_term_setup.c b/42sh/src/line-editing/curs_term_setup.c new file mode 100644 index 00000000..bf9a1a87 --- /dev/null +++ b/42sh/src/line-editing/curs_term_setup.c @@ -0,0 +1,59 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* curs_term_setup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/08 16:50:26 by sbenning #+# #+# */ +/* Updated: 2016/12/09 16:50:10 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_curs.h" + +/* + * Return a static termios structure reference +*/ + +static struct termios *curs_term(void) +{ + static struct termios term; + + return (&term); +} + +/* + * Save the current termios state and set termios to RAW +*/ + +int curs_term_setup(void) +{ + struct termios *single; + struct termios term; + + single = curs_term(); + if (tcgetattr(0, single) < 0) + return (-1); + term = *single; + term.c_lflag &= ~(ECHO | ICANON); + term.c_cc[VTIME] = 0; + term.c_cc[VMIN] = 1; + if (tcsetattr(0, TCSADRAIN, &term) < 0) + return (-1); + return (0); +} + +/* + * Set termios to the saved state +*/ + +int curs_term_cleanup(void) +{ + struct termios *single; + + single = curs_term(); + if (tcsetattr(0, TCSADRAIN, single) < 0) + return (-1); + return (0); +} diff --git a/42sh/src/line-editing/curs_write.c b/42sh/src/line-editing/curs_write.c new file mode 100644 index 00000000..efe19c72 --- /dev/null +++ b/42sh/src/line-editing/curs_write.c @@ -0,0 +1,63 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* curs_write.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/08 17:52:20 by sbenning #+# #+# */ +/* Updated: 2016/12/09 17:03:41 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_curs.h" + +/* + * Write some buffer to stdout and recalc coordonate (go to next line and scroll if necessary) +*/ + +void curs_write(t_curs *curs, char *str, size_t len) +{ + int size; + int div; + int mod; + + size = (curs->li * curs->win_co + curs->co) + len; + div = size / curs->win_co; + mod = size % curs->win_co; + write(1, str, len); + curs_goto(curs, div, mod); +} + +/* + * curs_write in video mode +*/ + +void curs_video_write(t_curs *curs, char *str, size_t len) +{ + ft_putstr(tgetstr("so", NULL)); + curs_write(curs, str, len); + ft_putstr(tgetstr("me", NULL)); +} + +/* + * curs_write in video mode without moving the cursor +*/ + +void curs_video_write_static(t_curs *curs, char *str, size_t len) +{ + ft_putstr(tgetstr("so", NULL)); + curs_write(curs, str, len); + ft_putstr(tgetstr("me", NULL)); + curs_backward(curs, len); +} + +/* + * curs_write without moving the cursor +*/ + +void curs_write_static(t_curs *curs, char *str, size_t len) +{ + curs_write(curs, str, len); + curs_backward(curs, len); +} diff --git a/42sh/src/line-editing/ft_readline.c b/42sh/src/line-editing/ft_readline.c new file mode 100644 index 00000000..4f496620 --- /dev/null +++ b/42sh/src/line-editing/ft_readline.c @@ -0,0 +1,146 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_readline.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/08 18:03:48 by sbenning #+# #+# */ +/* Updated: 2016/12/09 18:27:49 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_readline.h" + +static t_input g_input[] = {\ + {RL_NL_CODE, rl_nl_function},\ + {RL_INSERT_CODE, rl_insert_function},\ + {RL_CLEAR_CODE, rl_clear_function},\ + {RL_RETARR_CODE, rl_retarr_function},\ + {RL_SUPPR_CODE, rl_suppr_function},\ + {RL_ESC_CODE, rl_esc_function},\ + {RL_QUOTE_CODE, rl_quote_function},\ + {RL_DQUOTE_CODE, rl_dquote_function},\ + {RL_LEFT_CODE, rl_left_function},\ + {RL_SELECT_LEFT_CODE, rl_select_left_function},\ + {RL_RIGHT_CODE, rl_right_function},\ + {RL_SELECT_RIGHT_CODE, rl_select_right_function},\ + {RL_WLEFT_CODE, rl_wleft_function},\ + {RL_SELECT_WLEFT_CODE, rl_select_wleft_function},\ + {RL_WRIGHT_CODE, rl_wright_function},\ + {RL_SELECT_WRIGHT_CODE, rl_select_wright_function},\ + {RL_HOME_CODE, rl_home_function},\ + {RL_SELECT_HOME_CODE, rl_select_home_function},\ + {RL_END_CODE, rl_end_function},\ + {RL_SELECT_END_CODE, rl_select_end_function},\ + {RL_PAGEUP_CODE, rl_pageup_function},\ + {RL_SELECT_PAGEUP_CODE, rl_select_pageup_function},\ + {RL_PAGEDOWN_CODE, rl_pagedown_function},\ + {RL_SELECT_PAGEDOWN_CODE, rl_select_pagedown_function},\ + {RL_COPY_CODE, rl_copy_function},\ + {RL_CUT_CODE, rl_cut_function},\ + {RL_PASTE_CODE, rl_paste_function},\ + {0x0, rl_default_function}\ +}; + +static void rl_toogle_bitset(t_line *line, int set) +{ + static int esc; + static int select; + + if (!set) + { + esc = RL_IS(line->bitset, RL_ESC); + select = RL_IS(line->bitset, RL_SELECT); + } + else + { + if (esc) + { + RL_UNSET(line->bitset, RL_ESC); + esc = 0; + } + if ((select && !RL_IS(line->bitset, RL_SELECT)) || (!select && RL_IS(line->bitset, RL_SELECT))) + { + rl_reset_display(line); + } + } +} + +static int rl_finish(t_line *line) +{ + if (RL_IS(line->bitset, RL_STACK)) + { + rl_end_function(line, 0); + write(1, "\n", 1); + if (rl_stack_line(line) < 0) + return (-1); + if (!RL_IS(line->bitset, RL_FINISH)) + { + if (curs_coo_setup(&line->curs) < 0) + return (-1); + line->prompt = (RL_IS(line->bitset, RL_ESC) ? "> " : "quote> "); + rl_put_prompt(line); + } + RL_UNSET(line->bitset, RL_STACK); + } + return (RL_IS(line->bitset, RL_FINISH)); +} + +static t_input_function rl_get_function(long int input) +{ + int i; + + i = -1; + while (g_input[++i].code) + { + if (input == g_input[i].code) + break ; + } + return (g_input[i].function); +} + +static long int rl_read(void) +{ + long int input; + + ft_bzero(&input, sizeof(long int)); + if (read(0, &input, sizeof(long int)) < 0) + return (0); + return (input); +} + +static int rl_loop(t_line *line) +{ + long int input; + int ret; + + rl_put_prompt(line); + while (42) + { + input = rl_read(); + if (input == 0) + continue ; + rl_toogle_bitset(line, 0); + if (rl_get_function(input)(line, input) < 0) + return (-1); + if ((ret = rl_finish(line))) + break ; + rl_toogle_bitset(line, 1); + } + return ((ret < 0 ? -1 : 0)); +} + +int ft_readline(t_line *line) +{ + if (rl_setup(line) < 0) + return (-1); + if (rl_loop(line) < 0) + { + rl_teardown(line); + return (-1); + } + if (rl_cleanup(line) < 0) + return (-1); + return (0); +} diff --git a/42sh/src/line-editing/input_buffer.c b/42sh/src/line-editing/input_buffer.c new file mode 100644 index 00000000..6498cbc7 --- /dev/null +++ b/42sh/src/line-editing/input_buffer.c @@ -0,0 +1,77 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* input_buffer.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/08 18:42:31 by sbenning #+# #+# */ +/* Updated: 2016/12/09 16:30:35 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_readline.h" + +int input_extend(t_line *line, int size) +{ + char *tmp; + + size = (RL_OFFSET > size ? RL_OFFSET : size); + if ((tmp = ft_strnew(line->size + size)) == NULL) + return (-1); + if (line->input) + { + ft_memcpy(tmp, line->input, line->size); + free(line->input); + } + line->input = tmp; + line->size += size; + return (0); +} + +int input_maj(t_line *line, char *str, int size) +{ + if (line->used + size >= line->size) + { + if (input_extend(line, size) < 0) + return (-1); + } + ft_memcpy(line->input + line->pos, str, size); + if (line->pos + size > line->used) + line->used = line->pos + size; + return (0); +} + +int input_move(t_line *line, char *str, int size) +{ + char *dup; + + if (line->used + size >= line->size) + { + if (input_extend(line, size) < 0) + return (-1); + } + if ((dup = ft_strdup(line->input + line->pos)) == NULL) + return (-1); + ft_memcpy(line->input + line->pos, str, size); + ft_memcpy(line->input + line->pos + size, dup, ft_strlen(dup)); + free(dup); + line->used += size; + return (0); +} + +void input_remove(t_line *line, int size) +{ + char *start; + + if (size < 0) + { + start = line->input + line->pos; + size *= -1; + } + else + start = (line->input + line->pos) - size; + ft_strcpy(start, start + size); + line->used -= size; + ft_bzero(line->input + line->used, line->size - line->used); +} diff --git a/42sh/src/line-editing/interactive_sh.c b/42sh/src/line-editing/interactive_sh.c new file mode 100644 index 00000000..5110f44b --- /dev/null +++ b/42sh/src/line-editing/interactive_sh.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* interactive_sh.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/10 09:44:43 by sbenning #+# #+# */ +/* Updated: 2016/12/10 10:24:12 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "line_editing.h" + +t_data **data_singleton(void) +{ + static t_data *data; + + return (&data); +} + +int ft_interactive_sh(t_data *data) +{ + *data_singleton() = data; + return (ft_readline(&data->line)); +} diff --git a/42sh/src/line-editing/rl_clear_function.c b/42sh/src/line-editing/rl_clear_function.c new file mode 100644 index 00000000..190b05c1 --- /dev/null +++ b/42sh/src/line-editing/rl_clear_function.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rl_clear_function.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/09 13:34:29 by sbenning #+# #+# */ +/* Updated: 2016/12/09 13:50:20 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_readline.h" + +int rl_clear_function(t_line *line, long int input) +{ + int pos; + + if (RL_IS(line->bitset, RL_SELECT)) + RL_UNSET(line->bitset, RL_SELECT); + curs_clear(&line->curs); + rl_put_prompt(line); + pos = line->pos; + line->pos = 0; + if (line->used) + rl_reset_display_post(line); + if (pos) + curs_forward(&line->curs, pos); + line->pos = pos; + return (0); + (void)input; +} diff --git a/42sh/src/line-editing/rl_clipboard.c b/42sh/src/line-editing/rl_clipboard.c new file mode 100644 index 00000000..86b1987e --- /dev/null +++ b/42sh/src/line-editing/rl_clipboard.c @@ -0,0 +1,95 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rl_clipboard.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/09 14:04:33 by sbenning #+# #+# */ +/* Updated: 2016/12/09 15:57:47 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_readline.h" + +int rl_clipboard_new_selected(t_line *line) +{ + int start; + int size; + + RL_UNSET(line->bitset, RL_SELECT); + start = (line->select > line->pos) ? line->pos : line->select; + size = (line->select > line->pos ? line->select - line->pos : line->pos - line->select); + if ((line->clipboard = ft_strsub(line->input, start, (size_t)size)) == NULL) + return (-1); + return (0); +} + +int rl_clipboard_new_selected_cut(t_line *line) +{ + int start; + int size; + + RL_UNSET(line->bitset, RL_SELECT); + start = (line->select > line->pos) ? line->pos : line->select; + size = (line->select > line->pos ? line->select - line->pos : line->pos - line->select); + if ((line->clipboard = ft_strsub(line->input, start, (size_t)size)) == NULL) + return (-1); + if (line->pos != start) + { + curs_backward(&line->curs, line->pos - start); + line->pos = line->pos - (line->pos - start); + } + input_remove(line, -size); + return (0); +} + +int rl_clipboard_new(t_line *line) +{ + int start; + int real_pos; + int size; + + if (RL_IS(line->bitset, RL_SELECT)) + return (rl_clipboard_new_selected(line)); + size = line->curs.win_co; + RL_UNSET(line->bitset, RL_SELECT); + real_pos = line->pos + ft_strlen(line->prompt); + start = (real_pos / line->curs.win_co) * line->curs.win_co; + if (start) + start -= ft_strlen(line->prompt); + else + size -= ft_strlen(line->prompt); + if ((line->clipboard = ft_strsub(line->input, start, size)) == NULL) + return (-1); + return (0); +} + +int rl_clipboard_new_cut(t_line *line) +{ + int start; + int real_pos; + int size; + + if (RL_IS(line->bitset, RL_SELECT)) + return (rl_clipboard_new_selected_cut(line)); + size = line->curs.win_co; + RL_UNSET(line->bitset, RL_SELECT); + real_pos = line->pos + ft_strlen(line->prompt); + start = (real_pos / line->curs.win_co) * line->curs.win_co; + if (start) + start -= ft_strlen(line->prompt); + else + size -= ft_strlen(line->prompt); + if ((line->clipboard = ft_strsub(line->input, start, size)) == NULL) + return (-1); + if (line->pos != start) + { + curs_backward(&line->curs, line->pos - start); + line->pos = line->pos - (line->pos - start); + } + if (size > line->used - start) + size = line->used - start; + input_remove(line, -size); + return (0); +} diff --git a/42sh/src/line-editing/rl_copy_function.c b/42sh/src/line-editing/rl_copy_function.c new file mode 100644 index 00000000..abdc5005 --- /dev/null +++ b/42sh/src/line-editing/rl_copy_function.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rl_copy_function.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/09 14:01:31 by sbenning #+# #+# */ +/* Updated: 2016/12/09 18:22:38 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_readline.h" + +int rl_copy_function(t_line *line, long int input) +{ + if (line->clipboard) + ft_memdel((void **)&line->clipboard); + if (rl_clipboard_new(line) < 0) + return (-1); + return (0); + (void)input; +} diff --git a/42sh/src/line-editing/rl_cut_function.c b/42sh/src/line-editing/rl_cut_function.c new file mode 100644 index 00000000..0319ea0b --- /dev/null +++ b/42sh/src/line-editing/rl_cut_function.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rl_cut_function.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/09 14:19:20 by sbenning #+# #+# */ +/* Updated: 2016/12/09 16:24:12 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_readline.h" + +int rl_cut_function(t_line *line, long int input) +{ + if (line->clipboard) + ft_memdel((void **)&line->clipboard); + if (rl_clipboard_new_cut(line) < 0) + return (-1); + rl_reset_display_post(line); + return (0); + (void)input; +} diff --git a/42sh/src/line-editing/rl_default_function.c b/42sh/src/line-editing/rl_default_function.c new file mode 100644 index 00000000..41b11d27 --- /dev/null +++ b/42sh/src/line-editing/rl_default_function.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rl_default_function.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/08 18:30:38 by sbenning #+# #+# */ +/* Updated: 2016/12/09 12:04:50 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_readline.h" + +int rl_default_function(t_line *line, long int input) +{ + if (!ft_isprint(input)) + return (0); + if (RL_IS(line->bitset, RL_SELECT)) + RL_UNSET(line->bitset, RL_SELECT); + if (RL_IS(line->bitset, RL_INSERT)) + { + if (input_move(line, (char *)&input, sizeof(char)) < 0) + return (-1); + } + else + { + if (input_maj(line, (char *)&input, sizeof(char)) < 0) + return (-1); + } + curs_write(&line->curs, (char *)&input, sizeof(char)); + line->pos += 1; + rl_reset_display_post(line); + return (0); +} diff --git a/42sh/src/line-editing/rl_display.c b/42sh/src/line-editing/rl_display.c new file mode 100644 index 00000000..0261bd7e --- /dev/null +++ b/42sh/src/line-editing/rl_display.c @@ -0,0 +1,61 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rl_display.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/08 18:38:39 by sbenning #+# #+# */ +/* Updated: 2016/12/09 13:16:11 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_readline.h" + +void rl_reset_display_post(t_line *line) +{ + size_t len; + + len = ft_strlen(line->input + line->pos); + curs_clear_post(&line->curs); + if (len == 0) + return ; + if (!RL_IS(line->bitset, RL_SELECT) || (RL_IS(line->bitset, RL_SELECT) && line->select < line->pos)) + { + curs_write_static(&line->curs, line->input + line->pos, len); + return; + } + if (line->select == line->used) + { + curs_video_write_static(&line->curs, line->input + line->pos, len); + return ; + } + curs_video_write(&line->curs, line->input + line->pos, line->select - line->pos); + curs_write(&line->curs, line->input + line->select, line->used - line->select); + curs_backward(&line->curs, line->used - line->pos); +} + +void rl_reset_display_ante(t_line *line) +{ + if (!line->pos) + return ; + curs_backward(&line->curs, line->pos); + if (!RL_IS(line->bitset, RL_SELECT) || (RL_IS(line->bitset, RL_SELECT) && line->select > line->pos)) + { + curs_write(&line->curs, line->input, line->pos); + return ; + } + if (line->select == 0) + { + curs_video_write(&line->curs, line->input, line->pos); + return ; + } + curs_write(&line->curs, line->input, line->select); + curs_video_write(&line->curs, line->input + line->select, line->pos - line->select); +} + +void rl_reset_display(t_line *line) +{ + rl_reset_display_ante(line); + rl_reset_display_post(line); +} diff --git a/42sh/src/line-editing/rl_dquote_function.c b/42sh/src/line-editing/rl_dquote_function.c new file mode 100644 index 00000000..2737131c --- /dev/null +++ b/42sh/src/line-editing/rl_dquote_function.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rl_dquote_function.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/09 00:46:36 by sbenning #+# #+# */ +/* Updated: 2016/12/09 02:20:10 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_readline.h" + +int rl_dquote_function(t_line *line, long int input) +{ + if (!RL_IS(line->bitset, RL_ESC)) + { + if (RL_IS(line->bitset, RL_DQUOTE)) + RL_UNSET(line->bitset, RL_DQUOTE); + else + RL_SET(line->bitset, RL_DQUOTE); + } + return (rl_default_function(line, input)); +} diff --git a/42sh/src/line-editing/rl_end_function.c b/42sh/src/line-editing/rl_end_function.c new file mode 100644 index 00000000..7e66bafe --- /dev/null +++ b/42sh/src/line-editing/rl_end_function.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rl_end_function.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/08 19:29:15 by sbenning #+# #+# */ +/* Updated: 2016/12/09 12:04:48 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_readline.h" + +int rl_end_function(t_line *line, long int input) +{ + if (RL_IS(line->bitset, RL_SELECT)) + RL_UNSET(line->bitset, RL_SELECT); + if (line->pos == line->used) + return (0); + curs_forward(&line->curs, line->used - line->pos); + line->pos = line->used; + return (0); + (void)input; +} diff --git a/42sh/src/line-editing/rl_esc_function.c b/42sh/src/line-editing/rl_esc_function.c new file mode 100644 index 00000000..99e700c0 --- /dev/null +++ b/42sh/src/line-editing/rl_esc_function.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rl_esc_function.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/09 00:40:22 by sbenning #+# #+# */ +/* Updated: 2016/12/09 02:18:11 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_readline.h" + +int rl_esc_function(t_line *line, long int input) +{ + if (!RL_IS(line->bitset, RL_ESC)) + RL_SET(line->bitset, RL_ESC); + return (rl_default_function(line, input)); +} diff --git a/42sh/src/line-editing/rl_home_function.c b/42sh/src/line-editing/rl_home_function.c new file mode 100644 index 00000000..7e76f263 --- /dev/null +++ b/42sh/src/line-editing/rl_home_function.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rl_home_function.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/08 19:28:10 by sbenning #+# #+# */ +/* Updated: 2016/12/09 12:04:47 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_readline.h" + +int rl_home_function(t_line *line, long int input) +{ + if (RL_IS(line->bitset, RL_SELECT)) + RL_UNSET(line->bitset, RL_SELECT); + if (!line->pos) + return (0); + curs_backward(&line->curs, line->pos); + line->pos = 0; + return (0); + (void)input; +} diff --git a/42sh/src/line-editing/rl_insert_function.c b/42sh/src/line-editing/rl_insert_function.c new file mode 100644 index 00000000..cf986e29 --- /dev/null +++ b/42sh/src/line-editing/rl_insert_function.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rl_insert_function.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/09 18:28:01 by sbenning #+# #+# */ +/* Updated: 2016/12/09 18:29:33 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_readline.h" + +int rl_insert_function(t_line *line, long int input) +{ + if (RL_IS(line->bitset, RL_INSERT)) + RL_UNSET(line->bitset, RL_INSERT); + else + RL_SET(line->bitset, RL_INSERT); + return (0); + (void)input; +} diff --git a/42sh/src/line-editing/rl_left_function.c b/42sh/src/line-editing/rl_left_function.c new file mode 100644 index 00000000..f11b35f1 --- /dev/null +++ b/42sh/src/line-editing/rl_left_function.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rl_left_function.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/08 19:17:13 by sbenning #+# #+# */ +/* Updated: 2016/12/09 12:37:48 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_readline.h" + +int rl_left_function(t_line *line, long int input) +{ + if (RL_IS(line->bitset, RL_SELECT)) + RL_UNSET(line->bitset, RL_SELECT); + if (!line->pos) + return (0); + curs_backward(&line->curs, 1); + line->pos -= 1; + return (0); + (void)input; +} diff --git a/42sh/src/line-editing/rl_merge_line.c b/42sh/src/line-editing/rl_merge_line.c new file mode 100644 index 00000000..38a7f490 --- /dev/null +++ b/42sh/src/line-editing/rl_merge_line.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rl_merge_line.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/09 01:25:03 by sbenning #+# #+# */ +/* Updated: 2016/12/09 01:58:15 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_readline.h" + +int rl_merge_line(t_line *line) +{ + t_list *next; + char *chunk; + size_t size; + + while (line->stack) + { + next = line->stack->next; + chunk = (char *)line->stack->content; + size = line->stack->content_size; + if (input_maj(line, chunk, size) < 0) + return (-1); + line->pos += size; + if (next) + { + if (input_maj(line, "\n", 1) < 0) + return (-1); + line->pos += 1; + } + free(chunk); + free(line->stack); + line->stack = next; + } + return (0); +} diff --git a/42sh/src/line-editing/rl_nl_function.c b/42sh/src/line-editing/rl_nl_function.c new file mode 100644 index 00000000..2450d72f --- /dev/null +++ b/42sh/src/line-editing/rl_nl_function.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rl_nl_function.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/08 19:03:22 by sbenning #+# #+# */ +/* Updated: 2016/12/09 13:02:00 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_readline.h" + +int rl_nl_function(t_line *line, long int input) +{ + if (RL_IS(line->bitset, RL_SELECT)) + RL_UNSET(line->bitset, RL_SELECT); + if (!RL_IS(line->bitset, RL_QUOTING)) + RL_SET(line->bitset, RL_FINISH); + RL_SET(line->bitset, RL_STACK); + return (0); + (void)input; +} diff --git a/42sh/src/line-editing/rl_pagedown_function.c b/42sh/src/line-editing/rl_pagedown_function.c new file mode 100644 index 00000000..bd746dd4 --- /dev/null +++ b/42sh/src/line-editing/rl_pagedown_function.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rl_pagedown_function.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/08 19:32:26 by sbenning #+# #+# */ +/* Updated: 2016/12/09 12:04:19 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_readline.h" + +int rl_pagedown_function(t_line *line, long int input) +{ + int len; + + if (RL_IS(line->bitset, RL_SELECT)) + RL_UNSET(line->bitset, RL_SELECT); + if (line->pos == line->used) + return (0); + len = line->used - line->pos; + len = (line->curs.win_co > len ? len : line->curs.win_co); + curs_forward(&line->curs, len); + line->pos += len; + return (0); + (void)input; +} + diff --git a/42sh/src/line-editing/rl_pageup_function.c b/42sh/src/line-editing/rl_pageup_function.c new file mode 100644 index 00000000..c1402d41 --- /dev/null +++ b/42sh/src/line-editing/rl_pageup_function.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rl_pageup_function.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/08 19:32:16 by sbenning #+# #+# */ +/* Updated: 2016/12/09 12:04:07 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_readline.h" + +int rl_pageup_function(t_line *line, long int input) +{ + int len; + + if (RL_IS(line->bitset, RL_SELECT)) + RL_UNSET(line->bitset, RL_SELECT); + if (!line->pos) + return (0); + len = (line->curs.win_co > line->pos ? line->pos : line->curs.win_co); + curs_backward(&line->curs, len); + line->pos -= len; + return (0); + (void)input; +} diff --git a/42sh/src/line-editing/rl_paste_function.c b/42sh/src/line-editing/rl_paste_function.c new file mode 100644 index 00000000..42a2fa79 --- /dev/null +++ b/42sh/src/line-editing/rl_paste_function.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rl_paste_function.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/09 14:40:45 by sbenning #+# #+# */ +/* Updated: 2016/12/09 18:22:38 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_readline.h" + +int rl_paste_function(t_line *line, long int input) +{ + int len; + + if (RL_IS(line->bitset, RL_SELECT)) + RL_UNSET(line->bitset, RL_SELECT); + if (!line->clipboard) + return (0); + len = ft_strlen(line->clipboard); + if (!len) + return (0); + if (RL_IS(line->bitset, RL_INSERT)) + { + if (input_move(line, line->clipboard, len) < 0) + return (-1); + } + else + { + if (input_maj(line, line->clipboard, len) < 0) + return (-1); + } + rl_reset_display_post(line); + curs_forward(&line->curs, len); + line->pos += len; + return (0); + (void)input; +} diff --git a/42sh/src/line-editing/rl_prompt.c b/42sh/src/line-editing/rl_prompt.c new file mode 100644 index 00000000..baff188c --- /dev/null +++ b/42sh/src/line-editing/rl_prompt.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rl_prompt.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/09 01:45:09 by sbenning #+# #+# */ +/* Updated: 2016/12/09 13:38:07 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_readline.h" + +void rl_put_prompt(t_line *line) +{ + curs_write(&line->curs, line->prompt, ft_strlen(line->prompt)); +} diff --git a/42sh/src/line-editing/rl_quote_function.c b/42sh/src/line-editing/rl_quote_function.c new file mode 100644 index 00000000..7a1a8024 --- /dev/null +++ b/42sh/src/line-editing/rl_quote_function.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rl_quote_function.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/09 00:45:34 by sbenning #+# #+# */ +/* Updated: 2016/12/09 02:20:20 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_readline.h" + +int rl_quote_function(t_line *line, long int input) +{ + if (!RL_IS(line->bitset, RL_ESC)) + { + if (RL_IS(line->bitset, RL_QUOTE)) + RL_UNSET(line->bitset, RL_QUOTE); + else + RL_SET(line->bitset, RL_QUOTE); + } + return (rl_default_function(line, input)); +} diff --git a/42sh/src/line-editing/rl_retarr_function.c b/42sh/src/line-editing/rl_retarr_function.c new file mode 100644 index 00000000..ad92bb15 --- /dev/null +++ b/42sh/src/line-editing/rl_retarr_function.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rl_retarr_function.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/08 20:06:50 by sbenning #+# #+# */ +/* Updated: 2016/12/09 12:04:31 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_readline.h" + +int rl_retarr_function(t_line *line, long int input) +{ + if (RL_IS(line->bitset, RL_SELECT)) + RL_UNSET(line->bitset, RL_SELECT); + if (!line->pos) + return (0); + input_remove(line, 1); + curs_backward(&line->curs, 1); + line->pos -= 1; + rl_reset_display_post(line); + return (0); + (void)input; +} diff --git a/42sh/src/line-editing/rl_right_function.c b/42sh/src/line-editing/rl_right_function.c new file mode 100644 index 00000000..e4172a86 --- /dev/null +++ b/42sh/src/line-editing/rl_right_function.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rl_right_function.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/08 19:20:15 by sbenning #+# #+# */ +/* Updated: 2016/12/09 12:03:24 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_readline.h" + +int rl_right_function(t_line *line, long int input) +{ + if (RL_IS(line->bitset, RL_SELECT)) + RL_UNSET(line->bitset, RL_SELECT); + if (line->pos == line->used) + return (0); + curs_forward(&line->curs, 1); + line->pos += 1; + return (0); + (void)input; +} diff --git a/42sh/src/line-editing/rl_select_end_function.c b/42sh/src/line-editing/rl_select_end_function.c new file mode 100644 index 00000000..f79862eb --- /dev/null +++ b/42sh/src/line-editing/rl_select_end_function.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rl_select_end_function.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/09 13:19:35 by sbenning #+# #+# */ +/* Updated: 2016/12/09 13:20:31 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_readline.h" + +int rl_select_end_function(t_line *line, long int input) +{ + int ret; + + if (line->pos == line->used) + return (0); + if (!RL_IS(line->bitset, RL_SELECT)) + line->select = line->pos; + ret = rl_end_function(line, input); + RL_SET(line->bitset, RL_SELECT); + rl_reset_display_ante(line); + return (ret); +} diff --git a/42sh/src/line-editing/rl_select_home_function.c b/42sh/src/line-editing/rl_select_home_function.c new file mode 100644 index 00000000..6bdb5692 --- /dev/null +++ b/42sh/src/line-editing/rl_select_home_function.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rl_select_home_function.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/09 13:18:43 by sbenning #+# #+# */ +/* Updated: 2016/12/09 13:20:31 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_readline.h" + +int rl_select_home_function(t_line *line, long int input) +{ + int ret; + + if (!line->pos) + return (0); + if (!RL_IS(line->bitset, RL_SELECT)) + line->select = line->pos; + ret = rl_home_function(line, input); + RL_SET(line->bitset, RL_SELECT); + rl_reset_display_post(line); + return (ret); +} diff --git a/42sh/src/line-editing/rl_select_left_function.c b/42sh/src/line-editing/rl_select_left_function.c new file mode 100644 index 00000000..9e91cdfc --- /dev/null +++ b/42sh/src/line-editing/rl_select_left_function.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rl_select_left_function.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/09 03:06:59 by sbenning #+# #+# */ +/* Updated: 2016/12/09 12:37:28 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_readline.h" + +int rl_select_left_function(t_line *line, long int input) +{ + int ret; + + if (!line->pos) + return (0); + if (!RL_IS(line->bitset, RL_SELECT)) + line->select = line->pos; + ret = rl_left_function(line, input); + RL_SET(line->bitset, RL_SELECT); + rl_reset_display_post(line); + return (ret); +} diff --git a/42sh/src/line-editing/rl_select_pagedown_function.c b/42sh/src/line-editing/rl_select_pagedown_function.c new file mode 100644 index 00000000..9dd81a5b --- /dev/null +++ b/42sh/src/line-editing/rl_select_pagedown_function.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rl_select_right_function.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/09 03:16:57 by sbenning #+# #+# */ +/* Updated: 2016/12/09 13:29:15 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + + +#include "ft_readline.h" + +int rl_select_pagedown_function(t_line *line, long int input) +{ + int ret; + + if (line->pos == line->used) + return (0); + if (!RL_IS(line->bitset, RL_SELECT)) + line->select = line->pos; + ret = rl_pagedown_function(line, input); + RL_SET(line->bitset, RL_SELECT); + rl_reset_display_ante(line); + return (ret); +} diff --git a/42sh/src/line-editing/rl_select_pageup_function.c b/42sh/src/line-editing/rl_select_pageup_function.c new file mode 100644 index 00000000..acceff3c --- /dev/null +++ b/42sh/src/line-editing/rl_select_pageup_function.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rl_select_left_function.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/09 03:06:59 by sbenning #+# #+# */ +/* Updated: 2016/12/09 13:29:30 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_readline.h" + +int rl_select_pageup_function(t_line *line, long int input) +{ + int ret; + + if (!line->pos) + return (0); + if (!RL_IS(line->bitset, RL_SELECT)) + line->select = line->pos; + ret = rl_pageup_function(line, input); + RL_SET(line->bitset, RL_SELECT); + rl_reset_display_post(line); + return (ret); +} diff --git a/42sh/src/line-editing/rl_select_right_function.c b/42sh/src/line-editing/rl_select_right_function.c new file mode 100644 index 00000000..28495e7f --- /dev/null +++ b/42sh/src/line-editing/rl_select_right_function.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rl_select_right_function.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/09 03:16:57 by sbenning #+# #+# */ +/* Updated: 2016/12/09 12:37:31 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + + +#include "ft_readline.h" + +int rl_select_right_function(t_line *line, long int input) +{ + int ret; + + if (line->pos == line->used) + return (0); + if (!RL_IS(line->bitset, RL_SELECT)) + line->select = line->pos; + ret = rl_right_function(line, input); + RL_SET(line->bitset, RL_SELECT); + rl_reset_display_ante(line); + return (ret); +} diff --git a/42sh/src/line-editing/rl_select_wleft_function.c b/42sh/src/line-editing/rl_select_wleft_function.c new file mode 100644 index 00000000..9c4314d9 --- /dev/null +++ b/42sh/src/line-editing/rl_select_wleft_function.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rl_select_left_function.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/09 03:06:59 by sbenning #+# #+# */ +/* Updated: 2016/12/09 13:28:20 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_readline.h" + +int rl_select_wleft_function(t_line *line, long int input) +{ + int ret; + + if (!line->pos) + return (0); + if (!RL_IS(line->bitset, RL_SELECT)) + line->select = line->pos; + ret = rl_wleft_function(line, input); + RL_SET(line->bitset, RL_SELECT); + rl_reset_display_post(line); + return (ret); +} diff --git a/42sh/src/line-editing/rl_select_wright_function.c b/42sh/src/line-editing/rl_select_wright_function.c new file mode 100644 index 00000000..af75dd2a --- /dev/null +++ b/42sh/src/line-editing/rl_select_wright_function.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rl_select_right_function.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/09 03:16:57 by sbenning #+# #+# */ +/* Updated: 2016/12/09 13:28:37 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + + +#include "ft_readline.h" + +int rl_select_wright_function(t_line *line, long int input) +{ + int ret; + + if (line->pos == line->used) + return (0); + if (!RL_IS(line->bitset, RL_SELECT)) + line->select = line->pos; + ret = rl_wright_function(line, input); + RL_SET(line->bitset, RL_SELECT); + rl_reset_display_ante(line); + return (ret); +} diff --git a/42sh/src/line-editing/rl_setup.c b/42sh/src/line-editing/rl_setup.c new file mode 100644 index 00000000..5114c7e7 --- /dev/null +++ b/42sh/src/line-editing/rl_setup.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rl_setup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/08 18:07:47 by sbenning #+# #+# */ +/* Updated: 2016/12/09 13:39:22 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_readline.h" + +int rl_setup(t_line *line) +{ + ft_bzero(line, sizeof(t_line)); + if (curs_setup(&line->curs) < 0) + return (-1); + RL_SET(line->bitset, RL_INSERT); + line->prompt = "$> "; + return (0); +} + +int rl_cleanup(t_line *line) +{ + if (curs_cleanup(&line->curs) < 0) + return (-1); + if (rl_merge_line(line) < 0) + return (-1); + return (0); +} + +void rl_teardown(t_line *line) +{ + rl_cleanup(line); + if (line->input) + ft_memdel((void **)&line->input); + line->pos = 0; +} diff --git a/42sh/src/line-editing/rl_stack_line.c b/42sh/src/line-editing/rl_stack_line.c new file mode 100644 index 00000000..b632b99c --- /dev/null +++ b/42sh/src/line-editing/rl_stack_line.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rl_stack_line.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/09 01:06:54 by sbenning #+# #+# */ +/* Updated: 2016/12/10 09:54:10 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_readline.h" + +int rl_stack_line(t_line *line) +{ + t_list *elem; + + elem = ft_lstnew(line->input, line->used); + if (elem == NULL) + return (-1); + ft_lsteadd(&line->stack, elem); + ft_memdel((void **)&line->input); + line->pos = 0; + line->size = 0; + line->used = 0; + return (0); +} diff --git a/42sh/src/line-editing/rl_suppr_function.c b/42sh/src/line-editing/rl_suppr_function.c new file mode 100644 index 00000000..73e5fccf --- /dev/null +++ b/42sh/src/line-editing/rl_suppr_function.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rl_suppr_function.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/09 02:23:56 by sbenning #+# #+# */ +/* Updated: 2016/12/09 12:04:43 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_readline.h" + +int rl_suppr_function(t_line *line, long int input) +{ + if (RL_IS(line->bitset, RL_SELECT)) + RL_UNSET(line->bitset, RL_SELECT); + if (line->pos == line->used) + return (0); + input_remove(line, -1); + rl_reset_display_post(line); + return (0); + (void)input; +} diff --git a/42sh/src/line-editing/rl_wleft_function.c b/42sh/src/line-editing/rl_wleft_function.c new file mode 100644 index 00000000..b869f74b --- /dev/null +++ b/42sh/src/line-editing/rl_wleft_function.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rl_left_function.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/08 19:17:13 by sbenning #+# #+# */ +/* Updated: 2016/12/09 12:03:42 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_readline.h" + +static int rl_previous_word(t_line *line) +{ + int i; + int len; + + if (RL_IS(line->bitset, RL_SELECT)) + RL_UNSET(line->bitset, RL_SELECT); + i = 0; + while (line->input[i] && i < line->pos) + { + if (!i && line->input[i] != ' ') + len = i; + if (i && line->input[i - 1] == ' ' && line->input[i] != ' ') + len = i; + i++; + } + return (line->pos - len); +} + +int rl_wleft_function(t_line *line, long int input) +{ + size_t len; + + if (!line->pos) + return (0); + len = rl_previous_word(line); + curs_backward(&line->curs, len); + line->pos -= len; + return (0); + (void)input; +} diff --git a/42sh/src/line-editing/rl_wright_function.c b/42sh/src/line-editing/rl_wright_function.c new file mode 100644 index 00000000..6ead739d --- /dev/null +++ b/42sh/src/line-editing/rl_wright_function.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rl_right_function.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sbenning +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/08 19:20:15 by sbenning #+# #+# */ +/* Updated: 2016/12/09 12:03:54 by sbenning ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_readline.h" + +static int rl_next_word(t_line *line) +{ + int i; + int flag; + + if (RL_IS(line->bitset, RL_SELECT)) + RL_UNSET(line->bitset, RL_SELECT); + i = line->pos; + flag = 0; + while (line->input[i] && (line->input[i] == ' ' || !flag)) + { + if (line->input[i] == ' ') + flag = 1; + i++; + } + return (i - line->pos); +} + +int rl_wright_function(t_line *line, long int input) +{ + size_t len; + + if (line->pos == line->used) + return (0); + len = rl_next_word(line); + curs_forward(&line->curs, len); + line->pos += len; + return (0); + (void)input; +}