diff --git a/42sh/Makefile b/42sh/Makefile index 61011aa3..14073819 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -25,10 +25,13 @@ D_FLAGS = MKDIR = mkdir -p RM = /bin/rm -rf -.PHONY: all clean fclean re tags test libft +.PHONY: all clean fclean re tags test all: $(NAME) +$(NAME): $(DF_OBJ) libft + $(CC) $(O_INC) $(O_SER) $(O_LIB) $(W_FLAGS) $(DF_OBJ) -o $@ $(D_FLAGS) + $(D_OBJ)/%.o: $(D_SRC)/main/%.c includes/minishell.h @$(MKDIR) $(D_OBJ) @$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS) @@ -62,9 +65,6 @@ $(D_OBJ)/%.o: $(D_SRC)/exec/%.c includes/exec.h libft: @$(MAKE) -C libft/ 2>/dev/null -$(NAME): $(DF_OBJ) libft - $(CC) $(O_INC) $(O_SER) $(O_LIB) $(W_FLAGS) $(DF_OBJ) -o $@ $(D_FLAGS) - clean: $(RM) $(D_OBJ) @@ -72,3 +72,4 @@ fclean: clean $(RM) $(NAME) re: fclean all + @$(MAKE) re -C libft/ 2>/dev/null diff --git a/42sh/includes/line_editing.h b/42sh/includes/line_editing.h index 2e959251..463a7eed 100644 --- a/42sh/includes/line_editing.h +++ b/42sh/includes/line_editing.h @@ -50,6 +50,13 @@ struct s_data char **env; t_dlist *history; t_dlist *input_mem; + + char *input; + int input_pos; + t_quote state_now; + t_quote state_last; + char quoted; + char backslash; }; extern t_stof g_keys[]; @@ -58,9 +65,9 @@ int ft_set_termios(t_data *data, int input_mode); int ft_interactive_sh(t_data *data); int ft_prompt(void); int ft_input_is_escaped(t_dlist *input_chain); -int ft_history_add(t_data *data, t_dlist *input_chain); +int ft_history_add(t_data *data); -typedef int key_press(t_data *data, t_dlist **input_chain, char *buf); +typedef int key_press(t_data *data, char *buf); key_press ft_clear_line; key_press ft_line_up; key_press ft_line_down; @@ -74,8 +81,8 @@ key_press ft_word_left; key_press ft_word_right; key_press ft_key_del; key_press ft_key_enter; -key_press ft_key_basic; key_press ft_key_ctrl_d; key_press ft_key_ctrl_c; +key_press ft_key_default; #endif diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index e1d721fe..55c4ed72 100644 --- a/42sh/includes/minishell.h +++ b/42sh/includes/minishell.h @@ -26,6 +26,7 @@ extern t_stof g_builtins[]; extern pid_t g_pid; void sig_handler(int signo); +int data_init(t_data *data); int ft_cmd_process(char **argv, char ***env_p); int ft_cmd_exec(char *execpath, char **argv, char ***env_p); diff --git a/42sh/includes/quote.h b/42sh/includes/quote.h new file mode 100644 index 00000000..91d62be9 --- /dev/null +++ b/42sh/includes/quote.h @@ -0,0 +1,16 @@ +#ifndef LINE_EDITING_H +# define LINE_EDITING_H + +typedef enum e_quote t_quote; + +enum e_quote +{ + NONE, + QUOTE, + DQUOTE, + BACKSLASH, +}; + +void quote_update(t_data *data, char c); + +#endif diff --git a/42sh/libft b/42sh/libft index a3f7d30e..202926ba 160000 --- a/42sh/libft +++ b/42sh/libft @@ -1 +1 @@ -Subproject commit a3f7d30e3fc482af179207682b48ae052d422bdf +Subproject commit 202926ba40a54535c54650272db67a60a9598e1d diff --git a/42sh/src/lexer-parser/ft_tokenize.c b/42sh/src/lexer-parser/ft_tokenize.c index 1c905ed5..acd22aae 100644 --- a/42sh/src/lexer-parser/ft_tokenize.c +++ b/42sh/src/lexer-parser/ft_tokenize.c @@ -18,11 +18,13 @@ int ft_tokenize(t_list **alst, char *str) char *cmd; int pos; + pos = 0; cmd = ft_strdup(str); while ((token = token_getnext(&pos, cmd))) { *alst = ft_lstnew(token, sizeof(*token)); alst = &(*alst)->next; + free(token); } ft_strdel(&cmd); return (0); diff --git a/42sh/src/line-editing/ft_clear_line.c b/42sh/src/line-editing/ft_clear_line.c index 46a2f275..24648a70 100644 --- a/42sh/src/line-editing/ft_clear_line.c +++ b/42sh/src/line-editing/ft_clear_line.c @@ -12,10 +12,10 @@ #include "line_editing.h" -int ft_clear_line(t_data *data, t_dlist **input_chain, char *buf) +int ft_clear_line(t_data *data, char *buf) { - ft_line_end(data, input_chain, buf); - while (ft_key_del(data, input_chain, buf)) + ft_line_end(data, buf); + while (ft_key_del(data, buf)) ; return (1); } diff --git a/42sh/src/line-editing/ft_cursor_left.c b/42sh/src/line-editing/ft_cursor_left.c index b4a38c05..a6d7b6b1 100644 --- a/42sh/src/line-editing/ft_cursor_left.c +++ b/42sh/src/line-editing/ft_cursor_left.c @@ -12,19 +12,18 @@ #include "line_editing.h" -int ft_cursor_left(t_data *data, t_dlist **input_chain, char *buf) +int ft_cursor_left(t_data *data, char *buf) { char *res; res = NULL; (void)buf; - (void)data; - if (*(char*)(*input_chain)->content == '\n') + if (!data->input_pos) return (0); - if (*(char*)(*input_chain)->content == '\0') + if (data->input[data->input_pos - 1] == '\n') return (0); - *input_chain = (*input_chain)->prev; - if ((res = tgetstr("le", NULL)) == NULL) + data->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/ft_cursor_right.c b/42sh/src/line-editing/ft_cursor_right.c index 0ab9995e..71785cab 100644 --- a/42sh/src/line-editing/ft_cursor_right.c +++ b/42sh/src/line-editing/ft_cursor_right.c @@ -12,18 +12,17 @@ #include "line_editing.h" -int ft_cursor_right(t_data *data, t_dlist **input_chain, char *buf) +int ft_cursor_right(t_data *data, char *buf) { char *res; res = NULL; (void)buf; - (void)data; - if (*(char*)(*input_chain)->content == '\n') + if (!data->input[data->input_pos]) return (0); - if (!(*input_chain)->next) + if (data->input[data->input_pos] == '\n') return (0); - *input_chain = (*input_chain)->next; + data->input_pos++; if ((res = tgetstr("nd", NULL)) == NULL) return (-1); tputs(tgoto(res, 0, 0), 0, &ft_putchar); diff --git a/42sh/src/line-editing/ft_history_add.c b/42sh/src/line-editing/ft_history_add.c index f9aae448..15f83f09 100644 --- a/42sh/src/line-editing/ft_history_add.c +++ b/42sh/src/line-editing/ft_history_add.c @@ -12,16 +12,15 @@ #include "line_editing.h" -int ft_history_add(t_data *data, t_dlist *input_chain) +int ft_history_add(t_data *data) { t_dlist *new; char *str; - str = ft_dlsttostr(input_chain); + str = data->input; if (data->history) while (data->history->next) data->history = data->history->next; - str = ft_strcut(str, "\\\n"); if (!data->history->prev || ft_strcmp(str, (char *)data->history->prev->content)) { new = ft_dlstnew(str, sizeof(char) * (ft_strlen(str) + 1)); diff --git a/42sh/src/line-editing/ft_history_down.c b/42sh/src/line-editing/ft_history_down.c index e70882fd..6f15dee6 100644 --- a/42sh/src/line-editing/ft_history_down.c +++ b/42sh/src/line-editing/ft_history_down.c @@ -12,7 +12,7 @@ #include "line_editing.h" -int ft_history_down(t_data *data, t_dlist **input_chain, char *buf) +int ft_history_down(t_data *data, char *buf) { int i; char *str; @@ -24,11 +24,11 @@ int ft_history_down(t_data *data, t_dlist **input_chain, char *buf) return (0); data->history = data->history->next; str = data->history->content; - *input_chain = ft_dlstlast(*input_chain); + /* *input_chain = ft_dlstlast(*input_chain); */ /* ft_clear_input(input_chain); */ i = 0; - while (str[i]) - ft_key_basic(data, input_chain, str + i++); + /* while (str[i]) */ + /* ft_key_basic(data, input_chain, str + i++); */ if (!data->history->next) ft_strdel(&str); return (0); diff --git a/42sh/src/line-editing/ft_history_up.c b/42sh/src/line-editing/ft_history_up.c index 7dae6bc4..dccbfc93 100644 --- a/42sh/src/line-editing/ft_history_up.c +++ b/42sh/src/line-editing/ft_history_up.c @@ -12,7 +12,7 @@ #include "line_editing.h" -int ft_history_up(t_data *data, t_dlist **input_chain, char *buf) +int ft_history_up(t_data *data, char *buf) { int i; char *str; @@ -22,15 +22,15 @@ int ft_history_up(t_data *data, t_dlist **input_chain, char *buf) return (0); if (!data->history->prev) return (0); - if (!data->history->next) - data->history->content = ft_dlsttostr(*input_chain); + /* if (!data->history->next) */ + /* data->history->content = ft_dlsttostr(*input_chain); */ data->history = data->history->prev; str = data->history->content; /* *input_chain = ft_dlst_last(*input_chain); */ - ft_clear_line(data, input_chain, buf); + /* ft_clear_line(data, input_chain, buf); */ i = 0; - while (str[i]) - ft_key_basic(data, input_chain, str + i++); + /* while (str[i]) */ + /* ft_key_basic(data, input_chain, str + i++); */ /* if (!data->history->next) */ /* ft_strdel(&str); */ return (0); diff --git a/42sh/src/line-editing/ft_interactive_sh.c b/42sh/src/line-editing/ft_interactive_sh.c index ee350707..ebb13386 100644 --- a/42sh/src/line-editing/ft_interactive_sh.c +++ b/42sh/src/line-editing/ft_interactive_sh.c @@ -39,27 +39,29 @@ t_stof g_keys[] = { {FT_KEY_C_D, &ft_key_ctrl_d}, {FT_KEY_C_C, &ft_key_ctrl_c}, {FT_KEY_C_Z, NULL}, - {NULL, &ft_key_basic}, + {NULL, &ft_key_default}, }; int ft_interactive_sh(t_data *data) { - char buf[20]; - t_dlist *input_chain; + char buf[4]; char null; int ret; int i; null = '\0'; - if (!data->history) - data->history = ft_dlstnew(NULL, 0); - input_chain = ft_dlstnew(&null, sizeof(char)); + + ft_strdel(&data->input); + data->input = ft_memalloc(10); + data->input_pos = 0; + data->quoting = 0; + data->backslach = 0; ft_set_termios(data, 1); ft_prompt(); while (1) { - ft_bzero(buf, 20); - ret = read(0, buf, 20); + ft_bzero(buf, 4); + ret = read(0, buf, 4); /* ft_printf("read=%i: %#x,%#x,%#x\n", ret, buf[0], buf[1], buf[2]); */ /* continue ; */ i = 0; @@ -67,8 +69,7 @@ int ft_interactive_sh(t_data *data) i++; if (!g_keys[i].f) continue ; - ret = (*g_keys[i].f)(data, &input_chain, buf); - if (ret < 0) + if ((ret = (*g_keys[i].f)(data, buf)) < 0) return (-1); else if (ret == 2) { diff --git a/42sh/src/line-editing/ft_key_ctrl_c.c b/42sh/src/line-editing/ft_key_ctrl_c.c index 2f97a4ab..bf34a8b8 100644 --- a/42sh/src/line-editing/ft_key_ctrl_c.c +++ b/42sh/src/line-editing/ft_key_ctrl_c.c @@ -12,15 +12,12 @@ #include "line_editing.h" -int ft_key_ctrl_c(t_data *data, t_dlist **input_chain, char *buf) +int ft_key_ctrl_c(t_data *data, char *buf) { - (void)data; (void)buf; - char null; - null = '\0'; - ft_dlstdel(input_chain, ft_lst_cfree); - *input_chain = ft_dlstnew(&null, sizeof(char)); + ft_strdel(&data->input); + data->input = ft_memalloc(10); ft_putendl(""); ft_prompt(); return (0); diff --git a/42sh/src/line-editing/ft_key_ctrl_d.c b/42sh/src/line-editing/ft_key_ctrl_d.c index c30a5cd3..c4f09da6 100644 --- a/42sh/src/line-editing/ft_key_ctrl_d.c +++ b/42sh/src/line-editing/ft_key_ctrl_d.c @@ -12,10 +12,9 @@ #include "line_editing.h" -int ft_key_ctrl_d(t_data *data, t_dlist **input_chain, char *buf) +int ft_key_ctrl_d(t_data *data, char *buf) { (void)data; - (void)input_chain; (void)buf; ft_putendl("exit"); exit(0); diff --git a/42sh/src/line-editing/ft_key_basic.c b/42sh/src/line-editing/ft_key_default.c similarity index 79% rename from 42sh/src/line-editing/ft_key_basic.c rename to 42sh/src/line-editing/ft_key_default.c index 7031f673..f429878c 100644 --- a/42sh/src/line-editing/ft_key_basic.c +++ b/42sh/src/line-editing/ft_key_default.c @@ -12,20 +12,22 @@ #include "line_editing.h" -int ft_key_basic(t_data *data, t_dlist **input_chain, char *buf) +int ft_key_default(t_data *data, char *buf) { - t_dlist *new; char *res; + char *tmp; - (void)data; - new = ft_dlstnew(&buf[0], sizeof(char)); - ft_dlstadd_after(input_chain, new); - if ((res = tgetstr("IC", NULL)) != NULL) + tmp = data->input; + data->input = ft_strinsert(data->input, *buf, data->input_pos); + data->input_pos++; + ft_strdel(&tmp); + quote_update(data, *buf); + if ((res = tgetstr("IC", NULL))) { tputs(tgoto(res, 0, 0), 1, &ft_putchar); ft_putchar(buf[0]); return (0); } - ft_putchar(buf[0]); - return (0); + else + return (-1); } diff --git a/42sh/src/line-editing/ft_key_del.c b/42sh/src/line-editing/ft_key_del.c index f24b42b8..8fd4d81a 100644 --- a/42sh/src/line-editing/ft_key_del.c +++ b/42sh/src/line-editing/ft_key_del.c @@ -12,17 +12,19 @@ #include "line_editing.h" -int ft_key_del(t_data *data, t_dlist **input_chain, char *buf) +int ft_key_del(t_data *data, char *buf) { char *res; (void)data; (void)buf; - if (!(*input_chain)->prev) + + if (!data->input_pos) return (0); - if (*(char*)(*input_chain)->content == '\n') + if (data->input[data->input_pos - 1] == '\n') return (0); - ft_dlstdelone(input_chain, &ft_lst_cfree); + ft_strsqueeze(data->input, data->input_pos - 1); + data->input_pos--; if ((res = tgetstr("le", NULL)) == NULL) { ft_printf("le error\n"); diff --git a/42sh/src/line-editing/ft_key_enter.c b/42sh/src/line-editing/ft_key_enter.c index d9aae469..2510021f 100644 --- a/42sh/src/line-editing/ft_key_enter.c +++ b/42sh/src/line-editing/ft_key_enter.c @@ -12,16 +12,16 @@ #include "line_editing.h" -int ft_key_enter(t_data *data, t_dlist **input_chain, char *buf) +int ft_key_enter(t_data *data, char *buf) { - if (ft_input_is_escaped(*input_chain)) + (void)buf; + if (data->quoting || data->backslash) { - ft_key_basic(data, input_chain, buf); + ft_key_basic(data, buf); ft_printf("> "); return (0); } ft_putchar('\n'); - ft_history_add(data, *input_chain); - ft_dlstdel(input_chain, ft_lst_cfree); + ft_history_add(data); return (2); } diff --git a/42sh/src/line-editing/ft_line_end.c b/42sh/src/line-editing/ft_line_end.c index 180fc04a..df7d2395 100644 --- a/42sh/src/line-editing/ft_line_end.c +++ b/42sh/src/line-editing/ft_line_end.c @@ -12,9 +12,9 @@ #include "line_editing.h" -int ft_line_end(t_data *data, t_dlist **input_chain, char *buf) +int ft_line_end(t_data *data, char *buf) { - while (ft_cursor_right(data, input_chain, buf)) + while (ft_cursor_right(data, buf)) ; return (0); } diff --git a/42sh/src/line-editing/ft_line_start.c b/42sh/src/line-editing/ft_line_start.c index e30d9f7d..354b4e74 100644 --- a/42sh/src/line-editing/ft_line_start.c +++ b/42sh/src/line-editing/ft_line_start.c @@ -12,9 +12,9 @@ #include "line_editing.h" -int ft_line_start(t_data *data, t_dlist **input_chain, char *buf) +int ft_line_start(t_data *data, char *buf) { - while (ft_cursor_left(data, input_chain, buf)) + while (ft_cursor_left(data, buf)) ; return (0); } diff --git a/42sh/src/line-editing/ft_word_left.c b/42sh/src/line-editing/ft_word_left.c index 0626d931..e5efc817 100644 --- a/42sh/src/line-editing/ft_word_left.c +++ b/42sh/src/line-editing/ft_word_left.c @@ -12,17 +12,17 @@ #include "line_editing.h" -int ft_word_left(t_data *data, t_dlist **input_chain, char *buf) +int ft_word_left(t_data *data, char *buf) { - ft_cursor_left(data, input_chain, buf); - while (FT_WS(*(char *)(*input_chain)->content)) + ft_cursor_left(data, buf); + while (FT_WS(data->input[data->input_pos - 1])) { - if (!ft_cursor_left(data, input_chain, buf)) + if (!ft_cursor_left(data, buf)) return (0) ; } - while (!FT_WS(*(char *)(*input_chain)->content)) + while (!FT_WS(data->input[data->input_pos - 1])) { - if (!ft_cursor_left(data, input_chain, buf)) + if (!ft_cursor_left(data, buf)) return (0); } return (0); diff --git a/42sh/src/line-editing/ft_word_right.c b/42sh/src/line-editing/ft_word_right.c index 1c6e2a4f..6d1a6012 100644 --- a/42sh/src/line-editing/ft_word_right.c +++ b/42sh/src/line-editing/ft_word_right.c @@ -12,19 +12,17 @@ #include "line_editing.h" -int ft_word_right(t_data *data, t_dlist **input_chain, char *buf) +int ft_word_right(t_data *data, char *buf) { - ft_cursor_right(data, input_chain, buf); - while (!FT_WS(*(char *)(*input_chain)->content)) + while (!FT_WS(data->input[data->input_pos])) { - if (!ft_cursor_right(data, input_chain, buf)) + if (!ft_cursor_right(data, buf)) return (0); } - while (FT_WS(*(char *)(*input_chain)->content)) + while (FT_WS(data->input[data->input_pos])) { - if (!ft_cursor_right(data, input_chain, buf)) + if (!ft_cursor_right(data, buf)) return (0) ; } - ft_cursor_left(data, input_chain, buf); return (0); } diff --git a/42sh/src/main/data_init.c b/42sh/src/main/data_init.c new file mode 100644 index 00000000..1f52a346 --- /dev/null +++ b/42sh/src/main/data_init.c @@ -0,0 +1,12 @@ +#include "minishell.h" + +extern char **environ; + +int data_init(t_data *data) +{ + data->env = ft_sstrdup(environ); + data->history = NULL; + if (!(data->history = ft_dlstnew(NULL, 0))) + return (-1); + return (0); +} diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index bf79520a..20c6bcea 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -12,8 +12,6 @@ #include "minishell.h" -extern char **environ; - int main(void) { t_data data; @@ -21,23 +19,25 @@ int main(void) t_btree *ast; ast = NULL; - data.env = ft_sstrdup(environ); + if (data_init(&data)) + return (1); if (signal(SIGINT, sig_handler) == SIG_ERR) ft_printf("\ncan't catch SIGINT\n"); while (1) { if (ft_interactive_sh(&data)) return (1); - ft_printf("got command:'%s'\n", data.history->prev->content); - if (ft_tokenize(&token, data.history->prev->content)) - return (1); - token_print(token); + ft_printf("got command:'%s'\n", data.input); + /* if (ft_tokenize(&token, data.history->prev->content)) */ + /* return (1); */ + /* token_print(token); */ /* (void)ast; */ - if (ft_parse(&ast, token)) - return (1); - btree_print(ast, &tree_type); - ft_printf("root: %i\n", ((t_astnode*)ast->item)->type); - ft_lstdel(&token, &token_free); + /* if (ft_parse(&ast, token)) */ + /* return (1); */ + /* btree_print(ast, &tree_type); */ + /* ft_printf("root: %i\n", ((t_astnode*)ast->item)->type); */ + /* ft_lstdel(&token, &token_free); */ + token = NULL; /* if (ft_exec(ast)) */ /* return (1); */ diff --git a/42sh/src/quote/quote_update.c b/42sh/src/quote/quote_update.c new file mode 100644 index 00000000..8b7b65f5 --- /dev/null +++ b/42sh/src/quote/quote_update.c @@ -0,0 +1,24 @@ +#include "libft.h" + +void quote_state_update(t_data *data, char c) +{ + t_quote now; + t_quote last; + + now = data->state_now; + last = data->state_last; + if (c == '\\' && now != DQUOTE) + { + if (now == BACKSLASH) + now = last; + else + now = BACKSLASH; + } + if (quote != BACKSLASH) + { + if (c == '\'') + quoted = now == QUOTE ? 0 : '\''; + if (c == '\"' ) + data->quoted = now == DQUOTE ? 0 : '\"'; + } +}