diff --git a/42sh/.tags b/42sh/.tags index 82e41de5..05a93ff6 100644 --- a/42sh/.tags +++ b/42sh/.tags @@ -35,7 +35,14 @@ ft_path_access src/main/lib_path.c /^int ft_path_access(char *execpath, char *e ft_path_findexec src/main/lib_path.c /^char *ft_path_findexec(char **path, char *execname/ ft_prompt src/line-editing/ft_prompt.c /^int ft_prompt(void)$/ ft_tc_init src/line-editing/ft_tc_init.c /^int ft_tc_init(t_data *data)$/ -ft_tokenize src/token/ft_tokenize.c /^t_list *ft_tokenize(char *str)$/ +ft_tokenize src/token/ft_tokenize.c /^int ft_tokenize(t_list **alst, char *str)$/ ft_word_left src/line-editing/ft_word_left.c /^int ft_word_left(t_data *data, t_dlist **input_ch/ ft_word_right src/line-editing/ft_word_right.c /^int ft_word_right(t_data *data, t_dlist **input_c/ +is_separator src/token/token_getnext.c /^static int is_separator(char c)$/ +is_stop_char src/token/token_getnext.c /^static int is_stop_char(char c)$/ sig_handler src/main/sig_handler.c /^void sig_handler(int signo)$/ +token_append src/token/token_append.c /^int token_append(t_token *token, char c)$/ +token_free src/token/token_free.c /^void token_free(void *data, size_t size)$/ +token_getnext src/token/token_getnext.c /^t_token *token_getnext(int *pos, char *line)$/ +token_init src/token/token_init.c /^t_token *token_init()$/ +token_print src/token/token_print.c /^void token_print(t_list *lst)$/ diff --git a/42sh/Makefile b/42sh/Makefile index 287d1136..9296c46f 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -11,10 +11,10 @@ D_OBJ = obj F_OBJ = $(notdir $(F_SRC:.c=.o)) DF_OBJ := $(addprefix $(D_OBJ)/, $(F_OBJ)) -D_INC = includes libft/includes src/line-editing/includes +D_INC = includes libft/includes src/line-editing/includes src/token/includes O_INC = $(addprefix -I, $(D_INC)) -D_SER = libft +D_SER = libft/ O_SER = $(addprefix -L, $(D_SER)) D_LIB = ft ncurses @@ -28,22 +28,35 @@ RM = /bin/rm -rf .PHONY: all clean fclean re tags test libft -all: libft $(NAME) $(TAGFILE) +all: $(NAME) -$(TAGFILE): $(D_SRC)/*/*.c - @ctags -f $(TAGFILE) $(F_SRC) - @echo "Making tags..." - -$(D_OBJ)/%.o: $(D_SRC)/*/%.c $(D_INC) +$(D_OBJ)/%.o: $(D_SRC)/main/%.c includes/ @$(MKDIR) $(D_OBJ) @$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS) @echo "Compiling "$<"..." -libft: - @$(MAKE) -C libft/ 2>/dev/null +$(D_OBJ)/%.o: $(D_SRC)/line-editing/%.c src/line-editing/includes + @$(MKDIR) $(D_OBJ) + @$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS) + @echo "Compiling "$<"..." -line-editing: - @$(MAKE) -C line-editing 2>/dev/null +$(D_OBJ)/%.o: $(D_SRC)/builtin/%.c includes/ + @$(MKDIR) $(D_OBJ) + @$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS) + @echo "Compiling "$<"..." + +$(D_OBJ)/%.o: $(D_SRC)/token/%.c src/token/includes + @$(MKDIR) $(D_OBJ) + @$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS) + @echo "Compiling "$<"..." + +$(D_OBJ)/%.o: $(D_SRC)/minishell-exec/%.c includes/ + @$(MKDIR) $(D_OBJ) + @$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS) + @echo "Compiling "$<"..." + +libft/libft.a: + @$(MAKE) -C libft/ 2>/dev/null $(NAME): $(DF_OBJ) libft/libft.a $(CC) $(O_INC) $(O_SER) $(O_LIB) $(W_FLAGS) $(DF_OBJ) -o $@ $(D_FLAGS) diff --git a/42sh/Session.vim b/42sh/Session.vim index 556571b4..56312c09 100644 --- a/42sh/Session.vim +++ b/42sh/Session.vim @@ -3,12 +3,12 @@ if &cp | set nocp | endif let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0 let v:this_session=expand(":p") silent only -cd ~/minishell +cd ~/42/minishell if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == '' let s:wipebuf = bufnr('%') endif set shortmess=aoO -badd +38 includes/minishell.h +badd +23 includes/minishell.h badd +1 src/user-interaction/ft_word_left.c badd +11 src/user-interaction/ft_cursor_left.c badd +8 src/user-interaction/ft_interactive_sh.c @@ -17,7 +17,7 @@ badd +18 src/user-interaction/ft_cursor_right.c badd +7 src/user-interaction/ft_clear_line.c badd +1 src/user-interaction/ft_line_start.c badd +5 src/user-interaction/ft_line_end.c -badd +2 src/main/main.c +badd +40 src/main/main.c badd +12 src/builtin/builtin_exit.c badd +1 src/user-interaction/ft_cmd.c badd +8 src/line-editing/ft_cmd.c @@ -25,7 +25,7 @@ badd +2 src/main/ft_cmd.c badd +13 src/main/sig_handler.c badd +1 src/main/prompt.c badd +3 src/main/ft_prompt.c -badd +24 src/line-editing/ft_interactive_sh.c +badd +15 src/line-editing/ft_interactive_sh.c badd +7 src/line-editing/ft_key_ctrl_d.c badd +32 src/builtin/builtin_cd.c badd +4 src/main/lib_expansion.c @@ -37,45 +37,34 @@ badd +10 src/line-editing/ft_key_enter.c badd +16 src/line-editing/ft_history_add.c badd +9 src/line-editing/ft_history_up.c badd +18 src/line-editing/ft_history_down.c -badd +26 src/token/ft_tokenize.c +badd +62 src/token/ft_tokenize.c badd +30 src/line-editing/line-editing.h badd +1 libft/Makefile -badd +36 Makefile +badd +14 Makefile badd +18 line-editing/Makefile badd +14 line-editing/includes/line_editing.h -badd +15 src/line-editing/includes/line_editing.h +badd +25 src/line-editing/includes/line_editing.h badd +47 src/line-editing/src/ft_interactive_sh.c badd +20 src/line-editing/src/ft_key_enter.c badd +14 src/line-editing/ft_prompt.c badd +22 src/line-editing/ft_tc_init.c -badd +33 libft/includes/libft.h +badd +1 libft/includes/libft.h badd +5 libft/includes/get_next_line.h -badd +21 src/token/token_init.c -badd +32 src/token/includes/token.h +badd +15 src/token/token_init.c +badd +69 src/token/includes/token.h badd +15 src/token/token_recognition.c -badd +1 src -badd +14 src/token/token_append.c -badd +20 libft/src/mem/ft_realloc.c -badd +12 libft/src/mem/ft_memcpy.c -badd +23 libft/src/mem/ft_memalloc.c -badd +1 libft/src/mem/ft_memset.c -badd +23 libft/src/mem/ft_memmove.c -badd +14 libft/src/mem/ft_memchr.c -badd +13 src/token/token_delimit.c -badd +0 src/token/token_operator_match.c +badd +27 src/token/token_delimit.c +badd +11 src/token/token_operator_match.c +badd +7 src/token/token_next.c +badd +23 src/token/token_append.c +argglobal silent! argdel * -edit src/main/main.c +edit src/token/token_next.c set splitbelow splitright -wincmd _ | wincmd | -vsplit -1wincmd h -wincmd w set nosplitbelow set nosplitright wincmd t set winheight=1 winwidth=1 -exe 'vert 1resize ' . ((&columns * 91 + 90) / 180) -exe 'vert 2resize ' . ((&columns * 88 + 90) / 180) argglobal setlocal fdm=manual setlocal fde=0 @@ -86,189 +75,13 @@ setlocal fml=1 setlocal fdn=20 setlocal fen silent! normal! zE -let s:l = 35 - ((34 * winheight(0) + 37) / 75) -if s:l < 1 | let s:l = 1 | endif -exe s:l -normal! zt -35 -normal! 020l -wincmd w -argglobal -edit src/line-editing/includes/line_editing.h -setlocal fdm=manual -setlocal fde=0 -setlocal fmr={{{,}}} -setlocal fdi=# -setlocal fdl=0 -setlocal fml=1 -setlocal fdn=20 -setlocal fen -silent! normal! zE -let s:l = 2 - ((1 * winheight(0) + 37) / 75) -if s:l < 1 | let s:l = 1 | endif -exe s:l -normal! zt -2 -normal! 0 -wincmd w -exe 'vert 1resize ' . ((&columns * 91 + 90) / 180) -exe 'vert 2resize ' . ((&columns * 88 + 90) / 180) -tabedit src/token/ft_tokenize.c -set splitbelow splitright -wincmd _ | wincmd | -vsplit -1wincmd h -wincmd w -set nosplitbelow -set nosplitright -wincmd t -set winheight=1 winwidth=1 -exe 'vert 1resize ' . ((&columns * 90 + 90) / 180) -exe 'vert 2resize ' . ((&columns * 89 + 90) / 180) -argglobal -setlocal fdm=manual -setlocal fde=0 -setlocal fmr={{{,}}} -setlocal fdi=# -setlocal fdl=0 -setlocal fml=1 -setlocal fdn=20 -setlocal fen -silent! normal! zE -let s:l = 28 - ((27 * winheight(0) + 37) / 75) -if s:l < 1 | let s:l = 1 | endif -exe s:l -normal! zt -28 -normal! 0 -lcd ~/minishell -wincmd w -argglobal -edit ~/minishell/src/token/includes/token.h -setlocal fdm=manual -setlocal fde=0 -setlocal fmr={{{,}}} -setlocal fdi=# -setlocal fdl=0 -setlocal fml=1 -setlocal fdn=20 -setlocal fen -silent! normal! zE -let s:l = 48 - ((39 * winheight(0) + 37) / 75) -if s:l < 1 | let s:l = 1 | endif -exe s:l -normal! zt -48 -normal! 0 -wincmd w -2wincmd w -exe 'vert 1resize ' . ((&columns * 90 + 90) / 180) -exe 'vert 2resize ' . ((&columns * 89 + 90) / 180) -tabedit ~/minishell/src/token/token_delimit.c -set splitbelow splitright -wincmd _ | wincmd | -vsplit -1wincmd h -wincmd w -set nosplitbelow -set nosplitright -wincmd t -set winheight=1 winwidth=1 -exe 'vert 1resize ' . ((&columns * 90 + 90) / 180) -exe 'vert 2resize ' . ((&columns * 89 + 90) / 180) -argglobal -setlocal fdm=manual -setlocal fde=0 -setlocal fmr={{{,}}} -setlocal fdi=# -setlocal fdl=0 -setlocal fml=1 -setlocal fdn=20 -setlocal fen -silent! normal! zE -let s:l = 13 - ((12 * winheight(0) + 37) / 75) +let s:l = 13 - ((6 * winheight(0) + 20) / 41) if s:l < 1 | let s:l = 1 | endif exe s:l normal! zt 13 -normal! 017l -lcd ~/minishell -wincmd w -argglobal -edit ~/minishell/src/token/token_operator_match.c -setlocal fdm=manual -setlocal fde=0 -setlocal fmr={{{,}}} -setlocal fdi=# -setlocal fdl=0 -setlocal fml=1 -setlocal fdn=20 -setlocal fen -silent! normal! zE -let s:l = 16 - ((15 * winheight(0) + 37) / 75) -if s:l < 1 | let s:l = 1 | endif -exe s:l -normal! zt -16 normal! 0 -lcd ~/minishell -wincmd w -2wincmd w -exe 'vert 1resize ' . ((&columns * 90 + 90) / 180) -exe 'vert 2resize ' . ((&columns * 89 + 90) / 180) -tabedit ~/minishell/libft/src/mem/ft_realloc.c -set splitbelow splitright -wincmd _ | wincmd | -vsplit -1wincmd h -wincmd w -set nosplitbelow -set nosplitright -wincmd t -set winheight=1 winwidth=1 -exe 'vert 1resize ' . ((&columns * 90 + 90) / 180) -exe 'vert 2resize ' . ((&columns * 89 + 90) / 180) -argglobal -setlocal fdm=manual -setlocal fde=0 -setlocal fmr={{{,}}} -setlocal fdi=# -setlocal fdl=0 -setlocal fml=1 -setlocal fdn=20 -setlocal fen -silent! normal! zE -let s:l = 15 - ((14 * winheight(0) + 37) / 75) -if s:l < 1 | let s:l = 1 | endif -exe s:l -normal! zt -15 -normal! 014l -lcd ~/minishell -wincmd w -argglobal -edit ~/minishell/libft/includes/libft.h -setlocal fdm=manual -setlocal fde=0 -setlocal fmr={{{,}}} -setlocal fdi=# -setlocal fdl=0 -setlocal fml=1 -setlocal fdn=20 -setlocal fen -silent! normal! zE -let s:l = 131 - ((61 * winheight(0) + 37) / 75) -if s:l < 1 | let s:l = 1 | endif -exe s:l -normal! zt -131 -normal! 0 -lcd ~/minishell -wincmd w -2wincmd w -exe 'vert 1resize ' . ((&columns * 90 + 90) / 180) -exe 'vert 2resize ' . ((&columns * 89 + 90) / 180) -tabnext 2 +tabnext 1 if exists('s:wipebuf') silent exe 'bwipe ' . s:wipebuf endif diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index b0cfd6b1..a153492e 100644 --- a/42sh/includes/minishell.h +++ b/42sh/includes/minishell.h @@ -15,17 +15,14 @@ # include "libft.h" # include "line_editing.h" +# include "token.h" # include # include # include # include -# define MODE_INPUT 0b0001 -# define MODE_EXEC 0b0010 - extern t_stof g_builtins[]; extern pid_t g_pid; -extern int g_mode; void sig_handler(int signo); @@ -36,18 +33,14 @@ char **ft_cmd_getav(char *cmd); int ft_builtin(char **av, char ***env); int builtin_echo(char **av, char ***env); int builtin_cd(char **av, char ***env); -int builtin_cd_opts(char **av, int *opts); -char *builtin_cd_special(char **av, char **env); int builtin_exit(char **av, char ***env); int builtin_setenv(char **av, char ***env); int builtin_unsetenv(char **av, char ***env); int builtin_env(char **av, char ***env); void ft_expand_dollar(char **av, char **env); -char *ft_env_getval(char **env, char *key); -int ft_path_access(char *execpath, char *execname); -char *ft_path_findexec(char **path, char *execname); +char *ft_findexec(char **path, char *file); int ft_tokenize(t_list **alst, char *str); diff --git a/42sh/src/builtin/builtin.c b/42sh/src/builtin/builtin.c index 0f440dee..cc74c118 100644 --- a/42sh/src/builtin/builtin.c +++ b/42sh/src/builtin/builtin.c @@ -1,5 +1,6 @@ #include "minishell.h" + t_stof g_builtin[] = { {"echo", &builtin_echo}, {"cd", &builtin_cd}, diff --git a/42sh/src/builtin/builtin_cd.c b/42sh/src/builtin/builtin_cd.c index 28fb103b..5e4aee1f 100644 --- a/42sh/src/builtin/builtin_cd.c +++ b/42sh/src/builtin/builtin_cd.c @@ -5,7 +5,50 @@ #define HAS_CDOPT_L(x) (x & CD_OPT_L) #define CDERR_1 "cd: no such file or directory: %s\n" -int builtin_cd(char **av, char ***env_p) +static char *builtin_cd_special(char **av, char **env) +{ + char *target; + + if (!*av) + { + if (!(target = ft_getenv(env, "HOME"))) + return (NULL); + } + else if (ft_strcmp(*av, "-") == 0) + target = ft_getenv(env, "OLDPWD"); + else + target = *av; + return (target); +} + +static int builtin_cd_opts(char **av, int *opts) +{ + int i; + int j; + int tmp_opts; + + i = 1; + tmp_opts = 0; + if (av) + while (av[i] && av[i][0] == '-' && av[i][1]) + { + j = 0; + while (av[i][++j]) + { + if (av[i][j] == 'P') + tmp_opts |= CDOPT_P; + else if (av[i][j] == 'L') + tmp_opts |= CDOPT_L; + else + return (i); + } + *opts |= tmp_opts; + i++; + } + return (i); +} + +int builtin_cd(char **av, char ***env_p) { int i; int opts; @@ -32,46 +75,3 @@ int builtin_cd(char **av, char ***env_p) builtin_setenv(oldpwd, env_p); return (0); } - -char *builtin_cd_special(char **av, char **env) -{ - char *target; - - if (!*av) - { - if (!(target = ft_env_getval(env, "HOME"))) - return (NULL); - } - else if (ft_strcmp(*av, "-") == 0) - target = ft_env_getval(env, "OLDPWD"); - else - target = *av; - return (target); -} - -int builtin_cd_opts(char **av, int *opts) -{ - int i; - int j; - int tmp_opts; - - i = 1; - tmp_opts = 0; - if (av) - while (av[i] && av[i][0] == '-' && av[i][1]) - { - j = 0; - while (av[i][++j]) - { - if (av[i][j] == 'P') - tmp_opts |= CDOPT_P; - else if (av[i][j] == 'L') - tmp_opts |= CDOPT_L; - else - return (i); - } - *opts |= tmp_opts; - i++; - } - return (i); -} diff --git a/42sh/src/builtin/builtin_exit.c b/42sh/src/builtin/builtin_exit.c index 80156ff5..0b086272 100644 --- a/42sh/src/builtin/builtin_exit.c +++ b/42sh/src/builtin/builtin_exit.c @@ -8,7 +8,7 @@ int builtin_exit(char **av, char ***env_p) if (av[1]) status = ft_atoi(av[1]); else - status = ft_atoi(ft_env_getval(*env_p, "?")); + status = ft_atoi(ft_getenv(*env_p, "?")); exit(status); return (0); } diff --git a/42sh/src/line-editing/ft_history_add.c b/42sh/src/line-editing/ft_history_add.c index 313d31fe..f9aae448 100644 --- a/42sh/src/line-editing/ft_history_add.c +++ b/42sh/src/line-editing/ft_history_add.c @@ -24,8 +24,8 @@ int ft_history_add(t_data *data, t_dlist *input_chain) str = ft_strcut(str, "\\\n"); if (!data->history->prev || ft_strcmp(str, (char *)data->history->prev->content)) { - new = ft_dlst_new(str, sizeof(char) * (ft_strlen(str) + 1)); - ft_dlst_add_before(&data->history, new); + new = ft_dlstnew(str, sizeof(char) * (ft_strlen(str) + 1)); + ft_dlstadd_before(&data->history, new); data->history = data->history->next; ft_strdel((char **)&data->history->content); } diff --git a/42sh/src/line-editing/ft_history_down.c b/42sh/src/line-editing/ft_history_down.c index 6ebfaa57..e70882fd 100644 --- a/42sh/src/line-editing/ft_history_down.c +++ b/42sh/src/line-editing/ft_history_down.c @@ -24,7 +24,7 @@ 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_dlst_last(*input_chain); + *input_chain = ft_dlstlast(*input_chain); /* ft_clear_input(input_chain); */ i = 0; while (str[i]) diff --git a/42sh/src/line-editing/ft_interactive_sh.c b/42sh/src/line-editing/ft_interactive_sh.c index 0f4730d2..ee350707 100644 --- a/42sh/src/line-editing/ft_interactive_sh.c +++ b/42sh/src/line-editing/ft_interactive_sh.c @@ -37,6 +37,8 @@ t_stof g_keys[] = { {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_basic}, }; @@ -48,21 +50,20 @@ int ft_interactive_sh(t_data *data) int ret; int i; - (void)data; null = '\0'; if (!data->history) - data->history = ft_dlst_new(NULL, 0); - input_chain = ft_dlst_new(&null, sizeof(char)); - ft_tc_init(data); + data->history = ft_dlstnew(NULL, 0); + input_chain = ft_dlstnew(&null, sizeof(char)); + ft_set_termios(data, 1); ft_prompt(); while (1) { ft_bzero(buf, 20); - /* ret = read(0, buf, 20); */ + ret = read(0, buf, 20); /* ft_printf("read=%i: %#x,%#x,%#x\n", ret, buf[0], buf[1], buf[2]); */ - continue ; + /* continue ; */ i = 0; - while (g_keys[i].name && ft_strcmp(buf, g_keys[i].name) == 0) + while (g_keys[i].name && ft_strcmp(buf, g_keys[i].name)) i++; if (!g_keys[i].f) continue ; @@ -70,6 +71,9 @@ int ft_interactive_sh(t_data *data) if (ret < 0) return (-1); else if (ret == 2) + { + ft_set_termios(data, 0); return (0); + } } } diff --git a/42sh/src/line-editing/ft_key_basic.c b/42sh/src/line-editing/ft_key_basic.c index 17511927..7031f673 100644 --- a/42sh/src/line-editing/ft_key_basic.c +++ b/42sh/src/line-editing/ft_key_basic.c @@ -18,8 +18,8 @@ int ft_key_basic(t_data *data, t_dlist **input_chain, char *buf) char *res; (void)data; - new = ft_dlst_new(&buf[0], sizeof(char)); - ft_dlst_add_after(input_chain, new); + new = ft_dlstnew(&buf[0], sizeof(char)); + ft_dlstadd_after(input_chain, new); if ((res = tgetstr("IC", NULL)) != NULL) { tputs(tgoto(res, 0, 0), 1, &ft_putchar); diff --git a/42sh/src/token/token_recognition.c b/42sh/src/line-editing/ft_key_ctrl_c.c similarity index 60% rename from 42sh/src/token/token_recognition.c rename to 42sh/src/line-editing/ft_key_ctrl_c.c index 6b54de41..2f97a4ab 100644 --- a/42sh/src/token/token_recognition.c +++ b/42sh/src/line-editing/ft_key_ctrl_c.c @@ -1,18 +1,27 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* token_recognition.c :+: :+: :+: */ +/* ft_key_ctrl_c.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2016/11/10 15:37:22 by jhalford #+# #+# */ -/* Updated: 2016/11/10 15:48:45 by jhalford ### ########.fr */ +/* Created: 2016/11/10 13:44:24 by jhalford #+# #+# */ +/* Updated: 2016/11/10 13:44:24 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ -#include "token.h" +#include "line_editing.h" -int token_recognition(t_list *alst, char *str) +int ft_key_ctrl_c(t_data *data, t_dlist **input_chain, 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_putendl(""); + ft_prompt(); + return (0); } diff --git a/42sh/src/line-editing/ft_key_del.c b/42sh/src/line-editing/ft_key_del.c index 6fa530ef..f24b42b8 100644 --- a/42sh/src/line-editing/ft_key_del.c +++ b/42sh/src/line-editing/ft_key_del.c @@ -22,7 +22,7 @@ int ft_key_del(t_data *data, t_dlist **input_chain, char *buf) return (0); if (*(char*)(*input_chain)->content == '\n') return (0); - ft_dlst_delone(input_chain, &ft_lst_cfree); + ft_dlstdelone(input_chain, &ft_lst_cfree); 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 223dc262..6084075e 100644 --- a/42sh/src/line-editing/ft_key_enter.c +++ b/42sh/src/line-editing/ft_key_enter.c @@ -22,5 +22,6 @@ int ft_key_enter(t_data *data, t_dlist **input_chain, char *buf) } ft_putchar('\n'); ft_history_add(data, *input_chain); + ft_dlstdel(input_chain, ft_lst_cfree); return (2); } diff --git a/42sh/src/line-editing/ft_tc_init.c b/42sh/src/line-editing/ft_set_termios.c similarity index 88% rename from 42sh/src/line-editing/ft_tc_init.c rename to 42sh/src/line-editing/ft_set_termios.c index cfa2cb74..bf103511 100644 --- a/42sh/src/line-editing/ft_tc_init.c +++ b/42sh/src/line-editing/ft_set_termios.c @@ -12,10 +12,10 @@ #include "line_editing.h" -int ft_tc_init(t_data *data) +int ft_set_termios(t_data *data, int input_mode) { - char *term_name; struct termios term; + char *term_name; if ((term_name = ft_getenv(data->env, "TERM")) == NULL) return (-1); @@ -24,7 +24,10 @@ int ft_tc_init(t_data *data) if (tcgetattr(0, &term) == -1) return (-1); term.c_lflag &= ~(ICANON); // Met le terminal en mode canonique. - term.c_lflag &= ~(ECHO); // les touches tapées ne s'inscriront plus dans le terminal + if (input_mode == 1) + term.c_lflag &= ~(ISIG) & ~(ECHO); + else + term.c_lflag |= ISIG | ECHO; term.c_cc[VMIN] = 1; term.c_cc[VTIME] = 0; if (tcsetattr(0, TCSADRAIN, &term) == -1) diff --git a/42sh/src/line-editing/includes/line_editing.h b/42sh/src/line-editing/includes/line_editing.h index c8de2682..2e959251 100644 --- a/42sh/src/line-editing/includes/line_editing.h +++ b/42sh/src/line-editing/includes/line_editing.h @@ -20,6 +20,7 @@ # 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" @@ -27,6 +28,7 @@ # 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" @@ -52,7 +54,7 @@ struct s_data extern t_stof g_keys[]; -int ft_tc_init(t_data *data); +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); @@ -74,5 +76,6 @@ 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; #endif diff --git a/42sh/src/main/lib_env.c b/42sh/src/main/lib_env.c deleted file mode 100644 index 76d5f13e..00000000 --- a/42sh/src/main/lib_env.c +++ /dev/null @@ -1,15 +0,0 @@ -#include "minishell.h" - -char *ft_env_getval(char **env, char *key) -{ - if (!env) - return (NULL); - while (*env) - { - /* ft_printf("%s\n", env[i]); */ - if (ft_strcmp(*env, key) == '=') - return (*env + ft_strlen(key) + 1); - env++; - } - return (NULL); -} diff --git a/42sh/src/main/lib_expansion.c b/42sh/src/main/lib_expansion.c index 29b845f1..366bca87 100644 --- a/42sh/src/main/lib_expansion.c +++ b/42sh/src/main/lib_expansion.c @@ -9,7 +9,7 @@ void ft_expand_dollar(char **av, char **env) if ((dollar = ft_strchr(*av, '$'))) { *dollar = '\0'; - *av = ft_strjoin(*av, ft_env_getval(env, dollar + 1)); + *av = ft_strjoin(*av, ft_getenv(env, dollar + 1)); } av++; } diff --git a/42sh/src/main/lib_path.c b/42sh/src/main/lib_path.c index 24dfc404..ba899ea0 100644 --- a/42sh/src/main/lib_path.c +++ b/42sh/src/main/lib_path.c @@ -1,16 +1,6 @@ #include "minishell.h" -int ft_path_access(char *execpath, char *execname) -{ - if (access(execpath, X_OK) == -1) - { - ft_printf("minishell: permission denied: %s\n", execname); - return (1); - } - return (0); -} - -char *ft_path_findexec(char **path, char *execname) +char *ft_findexec(char **path, char *file) { int i; DIR *dir; @@ -24,7 +14,7 @@ char *ft_path_findexec(char **path, char *execname) continue ; while ((dirent = readdir(dir))) { - if (ft_strcmp(dirent->d_name, execname)) + if (ft_strcmp(dirent->d_name, file)) continue ; if (path[i][ft_strlen(path[i])] != '/') ft_strcat(path[i], "/"); @@ -32,6 +22,5 @@ char *ft_path_findexec(char **path, char *execname) return (execpath); } } - ft_printf("minishell: command not found: %s\n", execname); return (NULL); } diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 1dd38328..b517b9bb 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -13,39 +13,29 @@ #include "minishell.h" extern char **environ; -int g_mode; int main(void) { t_data data; - char *cmd; - char **av; t_list *token; data.env = ft_sstrdup(environ); - token = NULL; if (signal(SIGINT, sig_handler) == SIG_ERR) ft_printf("\ncan't catch SIGINT\n"); while (1) { - g_mode = MODE_INPUT; if (ft_interactive_sh(&data)) return (1); - cmd = ft_strdup(data.history->prev->content); - g_mode = MODE_EXEC; + /* ft_printf("got command:'%s'\n", data.history->prev->content); */ + if (ft_tokenize(&token, data.history->prev->content)) + return (1); + /* token_print(token); */ + /* t_btree *ast = ft_parse(&ast, token); */ + ft_lstdel(&token, &token_free); - ft_printf("got command:'%s'\n", cmd); - - /* (void)av; */ - /* ft_printf("got string:'%s'\n", cmd); */ - - /* av = ft_cmd_getav(cmd); */ + /* char **av = ft_cmd_getav(data.history->prev->content); */ /* if (av && av[0]) */ /* ft_cmd_process(av, &data.env); */ - - (void)av; - if (ft_tokenize(&token, cmd)) - return (1); } return (0); } diff --git a/42sh/src/main/sig_handler.c b/42sh/src/main/sig_handler.c index 67e12c98..31892118 100644 --- a/42sh/src/main/sig_handler.c +++ b/42sh/src/main/sig_handler.c @@ -1,18 +1,10 @@ #include "minishell.h" pid_t g_pid; -int g_mode; void sig_handler(int signo) { - if (signo != SIGINT) - return ; - if (g_mode == MODE_INPUT) + if (signo == SIGINT) { - ft_printf("input mode SIGINT"); - } - else if (g_mode == MODE_EXEC) - { - ft_putendl("^C"); if (g_pid) kill(g_pid, SIGINT); } diff --git a/42sh/src/minishell-exec/ft_cmd.c b/42sh/src/minishell-exec/ft_cmd.c index bdddebde..372fafab 100644 --- a/42sh/src/minishell-exec/ft_cmd.c +++ b/42sh/src/minishell-exec/ft_cmd.c @@ -6,14 +6,17 @@ int ft_cmd_process(char **argv, char ***env_p) char **path; char *execpath; - path = ft_strsplit(ft_env_getval(*env_p, "PATH"), ':'); + path = ft_strsplit(ft_getenv(*env_p, "PATH"), ':'); ft_expand_dollar(argv, *env_p); if (ft_builtin(argv, env_p)) return (0); else if (ft_strchr(argv[0], '/')) execpath = argv[0]; - else if (!(execpath = ft_path_findexec(path, argv[0]))) + else if (!(execpath = ft_findexec(path, argv[0]))) + { + ft_printf("minishell: command not found: %s\n", argv[0]); return (-1); + } return (ft_cmd_exec(execpath, argv, env_p)); } @@ -25,8 +28,11 @@ int ft_cmd_exec(char *execpath, char **argv, char ***env_p) char **sstr; sstr = NULL; - if (ft_path_access(execpath, argv[0])) + if (access(execpath, X_OK) == -1) + { + ft_printf("minishell: permission denied: %s\n", argv[0]); return (-1); + } if ((pid = fork()) == -1) return (-1); else if (pid == 0) diff --git a/42sh/src/token/ft_tokenize.c b/42sh/src/token/ft_tokenize.c index 7c3ea39d..2508b4e6 100644 --- a/42sh/src/token/ft_tokenize.c +++ b/42sh/src/token/ft_tokenize.c @@ -10,75 +10,20 @@ /* */ /* ************************************************************************** */ -#include "minishell.h" - -t_operator g_ops[] = -{ - {TK_LESS, "<", NUM_BEFORE | WORD_AFTER}, - {TK_GREAT, ">", NUM_BEFORE | WORD_AFTER}, - {TK_DGREAT, ">>", NUM_BEFORE | WORD_AFTER}, - - {TK_LESSAND, "<&", NUM_BEFORE | WORD_AFTER | MIN_END}, - {TK_GREATAND, ">&", NUM_BEFORE | WORD_AFTER | MIN_END}, - - {TK_DLESS, "<<", NUM_BEFORE | WORD_AFTER}, - - {TK_SCOLON, ";", 0}, - {TK_PIPE, "|", 0}, -} +#include "token.h" int ft_tokenize(t_list **alst, char *str) { - static int quoted = 0; - t_token *token; + t_token *token; + char *cmd; + int pos; - if (!*alst) - alst = ft_lstnew(token_init(), sizeof(t_token)); - else - token = (*alst)->content; - if (!*str) + cmd = ft_strdup(str); + while ((token = token_getnext(&pos, cmd))) { - token_delimit(token); - return (0); + *alst = ft_lstnew(token, sizeof(*token)); + alst = &(*alst)->next; } - else if ((token->type | TK_OPERATOR) && !quoted) - { - if (token_operator_match(token, *str)) - token_append(token, *str); - else - { - token_delimit(token); - ft_tokenize(&(*alst)->next, str); - } - } - else if (ft_strchr(OPERATOR_START, *str) && !quoted) - { - token_delimit(token); - (*alst)->next = ft_lstnew(token_init(), sizeof(t_token)); - (*alst) = (*alst)->next; - token = (*alst)->content; - token->type = TK_UNKNOWN; - token_append(token, *str); - ft_tokenize(alst, str + 1); - } - else if (*str == ' ' && !quoted) - { - token_delimit(token); - ft_tokenize(&(*alst)->next, str + 1); - } - else if (token->type == TOKEN_WORD) - token_append(token, *str); - else if (*str = '#') - { - while (*str && *str != '\n') - str++; - ft_tokenize(alst, str); - } - else - { - (*alst)->next = ft_lstnew(token_init(), sizeof(t_token)); - token->type = TK_WORD; - token_append(token, *str); - } - ft_tokenize(&(*alst)->next, str + 1); + ft_strdel(&cmd); + return (0); } diff --git a/42sh/src/token/includes/token.h b/42sh/src/token/includes/token.h index fbec699f..8c765369 100644 --- a/42sh/src/token/includes/token.h +++ b/42sh/src/token/includes/token.h @@ -13,60 +13,33 @@ #ifndef TOKEN_H # define TOKEN_H -typedef long long t_type; +# include "libft.h" -# define TK_WORD 0x0001 -# define TK_NEWLINE 0x0002 -# define TK_IOHERE 0x0004 -# define TK_LESS 0x0010 -# define TK_GREAT 0x0020 -# define TK_DLESS 0x0040 -# define TK_DGREAT 0x0080 -# define TK_LESSAND 0x0100 -# define TK_GREATAND 0x0200 -# define TK_SCOLON 0x0400 -# define TK_PIPE 0x0800 -# define TK_UNKNOWN 0x1000 +# define TK_LESS 0x0001 +# define TK_GREAT 0x0002 +# define TK_DLESS 0x0004 +# define TK_DGREAT 0x0008 +# define TK_LESSAND 0x0010 +# define TK_GREATAND 0x0020 +# define TK_SEMI 0x0040 +# define TK_PIPE 0x0080 +# define TK_WORD 0x0100 -# define TK_OPERATOR 1 & (~TK_WORD | ~TK_NEWLINE | ~TK_IOHERE) -# define OPERATOR_ST "<>;|0123456789" - -# define NUM_BEFORE 0x0001 -# define WORD_AFTER 0x0002 -# define MIN_END 0x0004 - - -typedef struct s_operator t_operator; +typedef long long t_type; typedef struct s_token t_token; -typedef struct s_redir t_redir; - -struct s_operator -{ - t_type type; - char symbol[3]; - t_type format; -}; - -struct s_redir -{ - t_type type; - int fd; - char *filename; -}; struct s_token { t_type type; - char *content; - char *size; + char *data; + int size; }; -extern t_operator g_ops[]; - t_token *token_init(); +t_token *token_getnext(int *pos,char *line); int ft_tokenize(t_list **alst, char *str); -int token_recognition(t_list *alst, char *str); -int token_append(t_token *token, char str); -int token_delimit(t_token *token); +int token_append(t_token *token, char c); +void token_free(void *data, size_t size); +void token_print(t_list *lst); #endif diff --git a/42sh/src/token/token_append.c b/42sh/src/token/token_append.c index e73d06f0..80296669 100644 --- a/42sh/src/token/token_append.c +++ b/42sh/src/token/token_append.c @@ -12,12 +12,13 @@ #include "token.h" -int token_append(t_token *token, char str) +int token_append(t_token *token, char c) { - if (ft_strlen(token->data) <= token->size) + if ((int)ft_strlen(token->data) >= token->size) { - token->data = (char *)ft_realloc(token->size + 10); + token->data = (char *)ft_realloc(token->data, token->size + 10); token->size += 10; } - ft_strcat(token->data, &str); + ft_strcat(token->data, (char[2]){c, '\0'}); + return (0); } diff --git a/42sh/src/token/token_delimit.c b/42sh/src/token/token_delimit.c deleted file mode 100644 index 4e1c44aa..00000000 --- a/42sh/src/token/token_delimit.c +++ /dev/null @@ -1,18 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* token_delimit.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/11/11 17:47:31 by jhalford #+# #+# */ -/* Updated: 2016/11/11 20:25:08 by jhalford ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "token.h" - -int token_delimit(t_token *token) -{ - -} diff --git a/42sh/src/token/token_free.c b/42sh/src/token/token_free.c new file mode 100644 index 00000000..52ddc369 --- /dev/null +++ b/42sh/src/token/token_free.c @@ -0,0 +1,11 @@ +#include "token.h" + +void token_free(void *data, size_t size) +{ + t_token *token; + + (void)size; + token = data; + ft_strdel(&token->data); + free(token); +} diff --git a/42sh/src/token/token_getnext.c b/42sh/src/token/token_getnext.c new file mode 100644 index 00000000..5bd5cdcf --- /dev/null +++ b/42sh/src/token/token_getnext.c @@ -0,0 +1,62 @@ +#include "token.h" + +static int is_separator(char c) +{ + return (c==' ' || c=='\t' || c=='\n' || c=='\r'); +} + + +static int is_stop_char(char c) +{ + return (c=='\0' || is_separator(c) || + c=='|' || c=='&' || c==';' || c=='<' || c=='>'); +} + +t_token *token_getnext(int *pos, char *line) +{ + int i; + t_token *token; + + i = 0; + if (!line[*pos]) + return (NULL); + token = token_init(); + while (is_separator(line[*pos])) + (*pos)++; + if (ft_strchr("|;", line[*pos])) + { + if (line[*pos] == ';') + token->type = TK_SEMI; + else if (line[*pos] == '|') + token->type = TK_PIPE; + token_append(token, line[(*pos)++]); + return (token); + } + while (ft_isdigit(line[*pos])) + token_append(token, line[(*pos)++]); + if (ft_strchr("<>", line[(*pos)])) + { + token->type = (*pos == '>' ? TK_GREAT : TK_LESS); + token_append(token, line[(*pos)++]); + if (line[(*pos)] == *pos - 1) + token->type = (token->type == TK_GREAT ? TK_DGREAT : TK_DLESS); + else if (line[(*pos)] == '&') + { + token->type = (token->type == TK_GREAT ? TK_GREATAND : TK_LESSAND); + token_append(token, line[(*pos)++]); + while (ft_isdigit(line[*pos])) + token_append(token, line[(*pos)++]); + if (line[*pos] == '-') + token_append(token, line[(*pos)++]); + } + return (token); + } + else + { + token->type = TK_WORD; + while (!is_stop_char(line[*pos])) + token_append(token, line[(*pos)++]); + return (token); + } + return (token); +} diff --git a/42sh/src/token/token_init.c b/42sh/src/token/token_init.c index a47d152c..100bf26e 100644 --- a/42sh/src/token/token_init.c +++ b/42sh/src/token/token_init.c @@ -10,13 +10,13 @@ /* */ /* ************************************************************************** */ -#include "minishell.h" +#include "token.h" -t_token *token_init(t_list **alst) +t_token *token_init() { t_token *token; - token = (t_token *)malloc(sizeof(t_token) + 1); + token = (t_token *)malloc(sizeof(t_token)); token->type = 0; token->size = 10; token->data = ft_strnew(token->size); diff --git a/42sh/src/token/token_operator_match.c b/42sh/src/token/token_operator_match.c deleted file mode 100644 index ab2dbbad..00000000 --- a/42sh/src/token/token_operator_match.c +++ /dev/null @@ -1,18 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* token_operator_match.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/11/11 20:12:52 by jhalford #+# #+# */ -/* Updated: 2016/11/11 20:13:36 by jhalford ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "token.h" - -int token_operator_match(t_token *token) -{ - -} diff --git a/42sh/src/token/token_print.c b/42sh/src/token/token_print.c new file mode 100644 index 00000000..15a67bd8 --- /dev/null +++ b/42sh/src/token/token_print.c @@ -0,0 +1,13 @@ +#include "token.h" + +void token_print(t_list *lst) +{ + t_token *token; + + while (lst) + { + token = lst->content; + ft_printf("%#06llx: '%s'\n", token->type, token->data); + lst = lst->next; + } +}