input is now char* not dlist, started to write FSM for quoting git add --all
This commit is contained in:
parent
e3725aa547
commit
74f9916047
25 changed files with 158 additions and 99 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
16
42sh/includes/quote.h
Normal file
16
42sh/includes/quote.h
Normal file
|
|
@ -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
|
||||
|
|
@ -1 +1 @@
|
|||
Subproject commit a3f7d30e3fc482af179207682b48ae052d422bdf
|
||||
Subproject commit 202926ba40a54535c54650272db67a60a9598e1d
|
||||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
@ -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");
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
12
42sh/src/main/data_init.c
Normal file
12
42sh/src/main/data_init.c
Normal file
|
|
@ -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);
|
||||
}
|
||||
|
|
@ -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); */
|
||||
|
||||
|
|
|
|||
24
42sh/src/quote/quote_update.c
Normal file
24
42sh/src/quote/quote_update.c
Normal file
|
|
@ -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 : '\"';
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue