From 4feaa64e082316ff13acd325832e75e8dd1db536 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Wed, 7 Dec 2016 18:27:49 +0100 Subject: [PATCH] exitstatus function, so i can set ? variable in one line with not memory handling. No more leaks, sexy debug macro DG(), valgrind suppression, gitignore --- 42sh/.gitignore | 2 ++ 42sh/.valgrind.supp | 10 +++++++++ 42sh/Makefile | 2 +- 42sh/includes/exec.h | 5 ++++- 42sh/includes/lexer.h | 2 +- 42sh/includes/line_editing.h | 2 +- 42sh/includes/minishell.h | 5 +++-- 42sh/libft | 2 +- 42sh/src/builtin/builtin.c | 2 +- 42sh/src/builtin/builtin_echo.c | 2 +- 42sh/src/builtin/builtin_env.c | 3 +++ 42sh/src/builtin/builtin_setenv.c | 8 ++++++- 42sh/src/exec/ast_free.c | 7 ++++-- 42sh/src/exec/exec_command.c | 2 +- 42sh/src/exec/exec_or_if.c | 2 +- 42sh/src/exec/exec_pipe.c | 4 ++-- 42sh/src/exec/ft_cmd.c | 19 ++++++++-------- 42sh/src/exec/ft_exec.c | 2 +- .../{main/lib_path.c => exec/ft_findexec.c} | 21 ++++++++++++------ 42sh/src/exec/set_exitstatus.c | 22 +++++++++++++++++++ 42sh/src/lexer/ft_tokenize.c | 6 +++-- 42sh/src/lexer/lexer_default.c | 2 +- 42sh/src/lexer/token_cmp_type.c | 2 +- 42sh/src/lexer/token_free.c | 2 +- 42sh/src/lexer/token_init.c | 2 +- 42sh/src/lexer/token_print.c | 4 ++-- 42sh/src/line-editing/ft_history_add.c | 2 +- 42sh/src/line-editing/ft_history_up.c | 2 +- 42sh/src/line-editing/ft_interactive_sh.c | 2 +- 42sh/src/line-editing/ft_key_ctrl_d.c | 3 ++- 42sh/src/line-editing/ft_key_default.c | 2 +- 42sh/src/line-editing/ft_key_enter.c | 3 +-- 42sh/src/line-editing/ft_set_termios.c | 8 ++----- 42sh/src/line-editing/input_init.c | 2 +- 42sh/src/main/data_exit.c | 21 ++++++++++++++++++ 42sh/src/main/data_init.c | 9 +++++++- 42sh/src/main/ft_cleanup.c | 9 +++++++- 42sh/src/main/ft_putast.c | 5 ++++- 42sh/src/main/ft_putast2.c | 2 +- 42sh/src/main/main.c | 7 +++--- 42sh/src/parser/ft_parse.c | 2 +- 42sh/src/parser/parse_word.c | 3 +-- 42 files changed, 158 insertions(+), 66 deletions(-) create mode 100644 42sh/.valgrind.supp rename 42sh/src/{main/lib_path.c => exec/ft_findexec.c} (70%) create mode 100644 42sh/src/exec/set_exitstatus.c create mode 100644 42sh/src/main/data_exit.c diff --git a/42sh/.gitignore b/42sh/.gitignore index bbff3e82..43b2b5b2 100644 --- a/42sh/.gitignore +++ b/42sh/.gitignore @@ -1,3 +1,5 @@ minishell 21sh 42sh +out +*.dSYM diff --git a/42sh/.valgrind.supp b/42sh/.valgrind.supp new file mode 100644 index 00000000..bbfa8f2b --- /dev/null +++ b/42sh/.valgrind.supp @@ -0,0 +1,10 @@ +{ + tgetent_1 + Memcheck:Leak + obj:*ncurses* +} +{ + valgrind_1 + Memcheck:Leak + obj:*valgrind* +} diff --git a/42sh/Makefile b/42sh/Makefile index 45ff43da..488e22b2 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -20,7 +20,7 @@ D_LIB = ft ncurses O_LIB = $(addprefix -l, $(D_LIB)) W_FLAGS = -Wall -Wextra -Werror -D_FLAGS = +D_FLAGS = -g MKDIR = mkdir -p RM = /bin/rm -rf diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index 28568d41..15aac6df 100644 --- a/42sh/includes/exec.h +++ b/42sh/includes/exec.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 20:29:56 by jhalford #+# #+# */ -/* Updated: 2016/12/05 12:22:40 by jhalford ### ########.fr */ +/* Updated: 2016/12/06 18:23:29 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -44,7 +44,10 @@ void fd_reset(t_data *data); int ft_cmd_process(char **argv, t_data *data); int ft_cmd_exec(char *execpath, char **argv, t_data *data); +char *ft_findexec(char *path, char *file); void ast_free(void *data, size_t content_size); +void set_exitstatus(t_data *data, int status); + #endif diff --git a/42sh/includes/lexer.h b/42sh/includes/lexer.h index 891d756d..97ba7c4a 100644 --- a/42sh/includes/lexer.h +++ b/42sh/includes/lexer.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */ -/* Updated: 2016/12/05 13:14:50 by jhalford ### ########.fr */ +/* Updated: 2016/12/05 14:11:52 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/includes/line_editing.h b/42sh/includes/line_editing.h index b81dfff8..435c9444 100644 --- a/42sh/includes/line_editing.h +++ b/42sh/includes/line_editing.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 13:38:21 by jhalford #+# #+# */ -/* Updated: 2016/12/03 13:44:24 by jhalford ### ########.fr */ +/* Updated: 2016/12/07 16:57:40 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index 9d20c3f1..729590a7 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/05 12:32:11 by jhalford ### ########.fr */ +/* Updated: 2016/12/07 18:09:27 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -69,6 +69,7 @@ extern pid_t g_pid; void sig_handler(int signo); int data_init(t_data *data); +void data_exit(t_data *data); void ft_cleanup(void); int ft_builtin(char **av, t_data *data); @@ -80,7 +81,7 @@ int builtin_unsetenv(char **av, t_data *data); int builtin_env(char **av, t_data *data); void ft_expand_dollar(char **av, char **env); -char *ft_findexec(char **path, char *file); +char *ft_findexec(char *path, char *file); char *ft_putast(void *node); void ft_putast2(void *node); diff --git a/42sh/libft b/42sh/libft index 1012e7bc..97bc4fed 160000 --- a/42sh/libft +++ b/42sh/libft @@ -1 +1 @@ -Subproject commit 1012e7bc51bb9b80cf9e62cacec4e61a6a37507d +Subproject commit 97bc4fed552dce523e2de8fb744ba46ec877f8eb diff --git a/42sh/src/builtin/builtin.c b/42sh/src/builtin/builtin.c index 21efa5c3..1966a63e 100644 --- a/42sh/src/builtin/builtin.c +++ b/42sh/src/builtin/builtin.c @@ -43,7 +43,7 @@ int ft_builtin(char **av, t_data *data) else { ret = (g_builtin[i].f)(av, data); - builtin_setenv((char*[3]){"?", ft_itoa(ret)}, data); + set_exitstatus(data, ret); } return (1); } diff --git a/42sh/src/builtin/builtin_echo.c b/42sh/src/builtin/builtin_echo.c index ef8e64a4..f559ebea 100644 --- a/42sh/src/builtin/builtin_echo.c +++ b/42sh/src/builtin/builtin_echo.c @@ -21,7 +21,7 @@ int builtin_echo(char **av, t_data *data) ft_printf("%s", *av); av++; if (*av) - ft_putstr(" "); + ft_putchar(' '); } ft_putchar('\n'); return (0); diff --git a/42sh/src/builtin/builtin_env.c b/42sh/src/builtin/builtin_env.c index 972b9303..90859d01 100644 --- a/42sh/src/builtin/builtin_env.c +++ b/42sh/src/builtin/builtin_env.c @@ -20,7 +20,10 @@ int builtin_env(char **av, t_data *data) i = 1; env = NULL; if (!av[1]) + { ft_sstrprint(data->env, '\n'); + ft_putchar('\n'); + } else { while (av[i] && ft_strchr(av[i], '=')) diff --git a/42sh/src/builtin/builtin_setenv.c b/42sh/src/builtin/builtin_setenv.c index f8ef44cf..0d547745 100644 --- a/42sh/src/builtin/builtin_setenv.c +++ b/42sh/src/builtin/builtin_setenv.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:25:17 by jhalford #+# #+# */ -/* Updated: 2016/11/28 14:29:13 by jhalford ### ########.fr */ +/* Updated: 2016/12/07 16:29:11 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,10 +18,14 @@ int builtin_setenv(char **av, t_data *data) char **env; env = data->env; + DG("doing setenv now"); if (ft_strcmp(av[0], "setenv") == 0) av++; if (!av[0]) + { ft_sstrprint(data->env, '\n'); + ft_putchar('\n'); + } else { str = ft_str3join(av[0], "=", av[1]); @@ -29,12 +33,14 @@ int builtin_setenv(char **av, t_data *data) { if (ft_strcmp(*env, av[0]) == '=') { + ft_strdel(env); *env = str; return (0); } env++; } data->env = ft_sstradd(data->env, str); + ft_strdel(&str); } return (0); } diff --git a/42sh/src/exec/ast_free.c b/42sh/src/exec/ast_free.c index 47babd94..f7b0943a 100644 --- a/42sh/src/exec/ast_free.c +++ b/42sh/src/exec/ast_free.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/05 11:50:51 by jhalford #+# #+# */ -/* Updated: 2016/12/05 12:07:38 by jhalford ### ########.fr */ +/* Updated: 2016/12/09 21:28:29 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,14 +18,17 @@ void ast_free(void *data, size_t content_size) (void)content_size; node = data; + DG("gonna free a node"); if (node->type == TK_COMMAND) { + DG("ast_free TK_COMMAND"); if (node->data.sstr) ft_sstrfree(node->data.sstr); } else if (node->type == TK_LESS || node->type == TK_GREAT || node->type == TK_DGREAT) { - ft_printf("gonna del word of redirection at %p\n", node->data.redir.word.word); + DG("ast_free TK_REDIR %p", node->data.redir.word.word); ft_strdel(&node->data.redir.word.word); } + DG("ast_free done"); } diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index 119b6159..82eb4e52 100644 --- a/42sh/src/exec/exec_command.c +++ b/42sh/src/exec/exec_command.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 17:28:14 by jhalford #+# #+# */ -/* Updated: 2016/12/05 12:12:31 by jhalford ### ########.fr */ +/* Updated: 2016/12/05 14:06:34 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_or_if.c b/42sh/src/exec/exec_or_if.c index 4410039c..1b60dbc3 100644 --- a/42sh/src/exec/exec_or_if.c +++ b/42sh/src/exec/exec_or_if.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/30 21:06:17 by jhalford #+# #+# */ -/* Updated: 2016/12/05 12:17:55 by jhalford ### ########.fr */ +/* Updated: 2016/12/09 21:50:19 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_pipe.c b/42sh/src/exec/exec_pipe.c index 31e03ef1..0d0c65ae 100644 --- a/42sh/src/exec/exec_pipe.c +++ b/42sh/src/exec/exec_pipe.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 21:13:23 by jhalford #+# #+# */ -/* Updated: 2016/12/05 12:14:13 by jhalford ### ########.fr */ +/* Updated: 2016/12/06 20:26:55 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,7 @@ int exec_pipe(t_btree **ast, t_data *data) int fds[2]; pipe(fds); - ft_dprintf(2, "pipe %i->%i\n", fds[PIPE_WRITE], fds[PIPE_READ]); + DG("pipe %i->%i", fds[PIPE_WRITE], fds[PIPE_READ]); data->exec.fdout = fds[PIPE_WRITE]; ft_exec(&(*ast)->left, data); if (data->exec.fdout != STDOUT) diff --git a/42sh/src/exec/ft_cmd.c b/42sh/src/exec/ft_cmd.c index a05d9e2b..adb9d6db 100644 --- a/42sh/src/exec/ft_cmd.c +++ b/42sh/src/exec/ft_cmd.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 21:13:18 by jhalford #+# #+# */ -/* Updated: 2016/12/03 15:24:31 by jhalford ### ########.fr */ +/* Updated: 2016/12/09 21:50:26 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,19 +16,17 @@ extern pid_t g_pid; int ft_cmd_process(char **argv, t_data *data) { - char **path; char *execpath; - path = ft_strsplit(ft_getenv(data->env, "PATH"), ':'); ft_expand_dollar(argv, data->env); if (ft_builtin(argv, data)) return (0); else if (ft_strchr(argv[0], '/')) execpath = argv[0]; - else if (!(execpath = ft_findexec(path, argv[0]))) + else if (!(execpath = ft_findexec(ft_getenv(data->env, "PATH"), argv[0]))) { ft_dprintf(2, "%s: command not found: %s\n", SHELL_NAME, argv[0]); - builtin_setenv((char*[3]){"?", "127"}, data); + set_exitstatus(data, 127); return (-1); } return (ft_cmd_exec(execpath, argv, data)); @@ -38,11 +36,11 @@ int ft_cmd_exec(char *execpath, char **argv, t_data *data) { pid_t pid; int status; - char **environ; if (access(execpath, X_OK) == -1) { ft_dprintf(2, "%s: permission denied: %s\n", SHELL_NAME, argv[0]); + ft_strdel(&execpath); return (-1); } if ((pid = fork()) == -1) @@ -50,15 +48,16 @@ int ft_cmd_exec(char *execpath, char **argv, t_data *data) else if (pid == 0) { fd_redirect(data); - environ = ft_sstrdup(data->env); - execve(execpath, argv, environ); + execve(execpath, argv, data->env); } - else if ((g_pid = pid)) + else { + ft_strdel(&execpath); + g_pid = pid; if (data->exec.fdout == STDOUT) { waitpid(pid, &status, 0); - builtin_setenv((char*[3]){"?", ft_itoa(status)}, data); + set_exitstatus(data, status); } g_pid = 0; } diff --git a/42sh/src/exec/ft_exec.c b/42sh/src/exec/ft_exec.c index c67eaf9a..0992b7aa 100644 --- a/42sh/src/exec/ft_exec.c +++ b/42sh/src/exec/ft_exec.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 20:30:32 by jhalford #+# #+# */ -/* Updated: 2016/12/05 12:18:45 by jhalford ### ########.fr */ +/* Updated: 2016/12/05 13:37:46 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/lib_path.c b/42sh/src/exec/ft_findexec.c similarity index 70% rename from 42sh/src/main/lib_path.c rename to 42sh/src/exec/ft_findexec.c index acd4c70d..10f67a95 100644 --- a/42sh/src/main/lib_path.c +++ b/42sh/src/exec/ft_findexec.c @@ -6,33 +6,40 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 13:37:49 by jhalford #+# #+# */ -/* Updated: 2016/12/03 13:37:49 by jhalford ### ########.fr */ +/* Updated: 2016/12/06 18:05:46 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -char *ft_findexec(char **path, char *file) +char *ft_findexec(char *path, char *file) { int i; DIR *dir; char *execpath; + char **spath; struct dirent *dirent; i = -1; - while (path && path[++i]) + spath = ft_strsplit(path, ':'); + while (spath && spath[++i]) { - if (!(dir = opendir(path[i]))) + if (!(dir = opendir(spath[i]))) continue ; while ((dirent = readdir(dir))) { if (ft_strcmp(dirent->d_name, file)) continue ; - if (path[i][ft_strlen(path[i])] != '/') - ft_strcat(path[i], "/"); - execpath = ft_strjoin(path[i], dirent->d_name); + if (spath[i][ft_strlen(spath[i])] != '/') + execpath = ft_str3join(spath[i], "/", dirent->d_name); + else + execpath = ft_strjoin(spath[i], dirent->d_name); + ft_sstrfree(spath); + closedir(dir); return (execpath); } + closedir(dir); } + ft_sstrfree(spath); return (NULL); } diff --git a/42sh/src/exec/set_exitstatus.c b/42sh/src/exec/set_exitstatus.c new file mode 100644 index 00000000..3a7dfd13 --- /dev/null +++ b/42sh/src/exec/set_exitstatus.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* set_exitstatus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/28 14:25:17 by jhalford #+# #+# */ +/* Updated: 2016/12/07 16:29:11 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void set_exitstatus(t_data *data, int status) +{ + char *astatus; + + astatus = ft_itoa(status); + builtin_setenv((char*[3]){"?", astatus}, data); + ft_strdel(&astatus); +} diff --git a/42sh/src/lexer/ft_tokenize.c b/42sh/src/lexer/ft_tokenize.c index 4353dff2..11b2f4bf 100644 --- a/42sh/src/lexer/ft_tokenize.c +++ b/42sh/src/lexer/ft_tokenize.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 13:37:11 by jhalford #+# #+# */ -/* Updated: 2016/12/03 13:49:13 by jhalford ### ########.fr */ +/* Updated: 2016/12/05 14:15:23 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,9 +39,11 @@ int ft_tokenize(t_list **alst, char *str, t_lexstate state) if (!*str) return (0); - token = token_init(); if (!*alst) + { + token = token_init(); *alst = ft_lstnew(token, sizeof(*token)); + } if (ft_is_delim(*str)) state = DELIM; else if (*str == '&' || *str == ';' || *str == '|') diff --git a/42sh/src/lexer/lexer_default.c b/42sh/src/lexer/lexer_default.c index 45e3996e..da00baee 100644 --- a/42sh/src/lexer/lexer_default.c +++ b/42sh/src/lexer/lexer_default.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 18:36:21 by jhalford #+# #+# */ -/* Updated: 2016/11/28 18:36:40 by jhalford ### ########.fr */ +/* Updated: 2016/12/05 14:15:26 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/token_cmp_type.c b/42sh/src/lexer/token_cmp_type.c index b7e204f3..08f3ab87 100644 --- a/42sh/src/lexer/token_cmp_type.c +++ b/42sh/src/lexer/token_cmp_type.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/30 17:08:55 by jhalford #+# #+# */ -/* Updated: 2016/12/05 12:29:50 by jhalford ### ########.fr */ +/* Updated: 2016/12/05 13:35:53 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/token_free.c b/42sh/src/lexer/token_free.c index 05ecb6e4..08cebb6a 100644 --- a/42sh/src/lexer/token_free.c +++ b/42sh/src/lexer/token_free.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:07:30 by jhalford #+# #+# */ -/* Updated: 2016/12/05 11:53:04 by jhalford ### ########.fr */ +/* Updated: 2016/12/05 13:17:56 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/token_init.c b/42sh/src/lexer/token_init.c index d9c474fd..e762eb1a 100644 --- a/42sh/src/lexer/token_init.c +++ b/42sh/src/lexer/token_init.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 15:30:25 by jhalford #+# #+# */ -/* Updated: 2016/12/03 12:07:46 by jhalford ### ########.fr */ +/* Updated: 2016/12/05 13:41:34 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/token_print.c b/42sh/src/lexer/token_print.c index c62f83d5..9a8b27c1 100644 --- a/42sh/src/lexer/token_print.c +++ b/42sh/src/lexer/token_print.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:39:01 by jhalford #+# #+# */ -/* Updated: 2016/12/05 12:50:03 by jhalford ### ########.fr */ +/* Updated: 2016/12/07 15:23:03 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,7 +25,7 @@ void token_print(t_list *lst) type = token->type; while (type >> (i++ + 2)) ; - ft_dprintf(2, "%02i '%s'\n", i, token->data); + DG("%02i '%s'", i, token->data); lst = lst->next; } } diff --git a/42sh/src/line-editing/ft_history_add.c b/42sh/src/line-editing/ft_history_add.c index aa796add..95490c18 100644 --- a/42sh/src/line-editing/ft_history_add.c +++ b/42sh/src/line-editing/ft_history_add.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 13:43:40 by jhalford #+# #+# */ -/* Updated: 2016/12/03 15:27:54 by jhalford ### ########.fr */ +/* Updated: 2016/12/07 16:52:48 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/line-editing/ft_history_up.c b/42sh/src/line-editing/ft_history_up.c index 04d4aa41..ccc01d64 100644 --- a/42sh/src/line-editing/ft_history_up.c +++ b/42sh/src/line-editing/ft_history_up.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 13:44:15 by jhalford #+# #+# */ -/* Updated: 2016/12/03 15:28:24 by jhalford ### ########.fr */ +/* Updated: 2016/12/07 16:52:28 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/line-editing/ft_interactive_sh.c b/42sh/src/line-editing/ft_interactive_sh.c index b9a8c453..ece0bda1 100644 --- a/42sh/src/line-editing/ft_interactive_sh.c +++ b/42sh/src/line-editing/ft_interactive_sh.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 12:14:12 by jhalford #+# #+# */ -/* Updated: 2016/12/03 17:03:55 by jhalford ### ########.fr */ +/* Updated: 2016/12/07 17:27:25 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/line-editing/ft_key_ctrl_d.c b/42sh/src/line-editing/ft_key_ctrl_d.c index c4f09da6..c882cc99 100644 --- a/42sh/src/line-editing/ft_key_ctrl_d.c +++ b/42sh/src/line-editing/ft_key_ctrl_d.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 13:44:24 by jhalford #+# #+# */ -/* Updated: 2016/11/10 13:44:24 by jhalford ### ########.fr */ +/* Updated: 2016/12/07 18:12:29 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,7 @@ 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/ft_key_default.c b/42sh/src/line-editing/ft_key_default.c index f440adbb..4cb66621 100644 --- a/42sh/src/line-editing/ft_key_default.c +++ b/42sh/src/line-editing/ft_key_default.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 18:45:23 by jhalford #+# #+# */ -/* Updated: 2016/12/03 15:37:25 by jhalford ### ########.fr */ +/* Updated: 2016/12/09 14:48:01 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/line-editing/ft_key_enter.c b/42sh/src/line-editing/ft_key_enter.c index 60acb82d..715b48a6 100644 --- a/42sh/src/line-editing/ft_key_enter.c +++ b/42sh/src/line-editing/ft_key_enter.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/29 15:56:36 by jhalford #+# #+# */ -/* Updated: 2016/12/03 15:30:46 by jhalford ### ########.fr */ +/* Updated: 2016/12/07 17:35:06 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,6 @@ int ft_key_enter(t_data *data, char *buf) if (*(t_qstate*)data->line.qstack->content == Q_NONE) { ft_putchar('\n'); - ft_history_add(data); return (2); } ft_key_default(data, buf); diff --git a/42sh/src/line-editing/ft_set_termios.c b/42sh/src/line-editing/ft_set_termios.c index 24343268..f51cb9de 100644 --- a/42sh/src/line-editing/ft_set_termios.c +++ b/42sh/src/line-editing/ft_set_termios.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 12:14:09 by jhalford #+# #+# */ -/* Updated: 2016/12/01 16:48:27 by jhalford ### ########.fr */ +/* Updated: 2016/12/07 14:20:59 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,12 +15,8 @@ int ft_set_termios(t_data *data, int input_mode) { struct termios term; - char *term_name; - if ((term_name = ft_getenv(data->env, "TERM")) == NULL) - return (-1); - if (tgetent(NULL, term_name) != 1) - return (-1); + (void)data; if (tcgetattr(0, &term) == -1) return (-1); if (input_mode) diff --git a/42sh/src/line-editing/input_init.c b/42sh/src/line-editing/input_init.c index 254c1133..bea6a347 100644 --- a/42sh/src/line-editing/input_init.c +++ b/42sh/src/line-editing/input_init.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 13:35:03 by jhalford #+# #+# */ -/* Updated: 2016/12/03 15:34:29 by jhalford ### ########.fr */ +/* Updated: 2016/12/07 16:30:40 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/data_exit.c b/42sh/src/main/data_exit.c new file mode 100644 index 00000000..cd912115 --- /dev/null +++ b/42sh/src/main/data_exit.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* data_exit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/07 18:07:50 by jhalford #+# #+# */ +/* Updated: 2016/12/07 18:12:34 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void data_exit(t_data *data) +{ + ft_strdel(&data->line.input); + ft_dlstdel(&data->line.history, &ft_lst_bfree); + ft_lstdel(&data->line.qstack, &ft_lst_cfree); + ft_sstrfree(data->env); +} diff --git a/42sh/src/main/data_init.c b/42sh/src/main/data_init.c index ed6cd000..6fc5a928 100644 --- a/42sh/src/main/data_init.c +++ b/42sh/src/main/data_init.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 19:26:32 by jhalford #+# #+# */ -/* Updated: 2016/12/03 15:32:25 by jhalford ### ########.fr */ +/* Updated: 2016/12/09 19:15:12 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,7 +16,10 @@ extern char **environ; int data_init(t_data *data) { + char *term_name; + atexit(&ft_cleanup); + data->line.input = NULL; data->env = ft_sstrdup(environ); data->line.history = NULL; data->exec.fdin = STDIN; @@ -25,5 +28,9 @@ int data_init(t_data *data) data->exec.aol_search = 0; if (!(data->line.history = ft_dlstnew(NULL, 0))) return (-1); + if ((term_name = ft_getenv(data->env, "TERM")) == NULL) + return (-1); + if (tgetent(NULL, term_name) != 1) + return (-1); return (0); } diff --git a/42sh/src/main/ft_cleanup.c b/42sh/src/main/ft_cleanup.c index d06d7254..7cf6aa3b 100644 --- a/42sh/src/main/ft_cleanup.c +++ b/42sh/src/main/ft_cleanup.c @@ -6,16 +6,23 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 14:42:42 by jhalford #+# #+# */ -/* Updated: 2016/12/03 15:19:41 by jhalford ### ########.fr */ +/* Updated: 2016/12/09 21:50:38 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" +extern char **environ; +extern char PC; +extern char *UP; +extern char *BC; + void ft_cleanup(void) { struct termios term; + DG("cleanup. char * UP at %p", UP); + DG("cleanup. char * BC at %p", BC); if (tcgetattr(0, &term) == -1) return ; term.c_lflag |= ICANON | ISIG | ECHO; diff --git a/42sh/src/main/ft_putast.c b/42sh/src/main/ft_putast.c index e1435c62..03607793 100644 --- a/42sh/src/main/ft_putast.c +++ b/42sh/src/main/ft_putast.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 18:18:04 by jhalford #+# #+# */ -/* Updated: 2016/12/03 13:44:22 by jhalford ### ########.fr */ +/* Updated: 2016/12/06 20:09:27 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -59,5 +59,8 @@ char *ft_putast(void *nodein) else if (node->type == TK_LESSAND) return (" <& "); else + { + ft_printf("type=%02i\n", node->type); return ("OTHER"); + } } diff --git a/42sh/src/main/ft_putast2.c b/42sh/src/main/ft_putast2.c index b73e0e55..5947a315 100644 --- a/42sh/src/main/ft_putast2.c +++ b/42sh/src/main/ft_putast2.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 18:18:04 by jhalford #+# #+# */ -/* Updated: 2016/12/05 12:35:35 by jhalford ### ########.fr */ +/* Updated: 2016/12/05 13:17:50 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 7579feab..b5eeb81c 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -5,8 +5,8 @@ /* +:+ +:+ +:+ */ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2016/11/27 21:13:34 by jhalford #+# #+# */ -/* Updated: 2016/12/05 12:11:27 by jhalford ### ########.fr */ +/* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */ +/* Updated: 2016/12/09 22:15:07 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,11 +24,12 @@ int main(void) return (1); if (signal(SIGINT, sig_handler) == SIG_ERR) ft_dprintf(STDERR, "\ncan't catch SIGINT\n"); + DG("{inv}{bol}{gre}start of shell"); while (1) { if (ft_interactive_sh(&data)) return (1); - /* ft_dprintf(STDERR, "command='%s'\n", data.input); */ + DG("{inv}{mag}got command '%s'", data.line.input); token = NULL; if (ft_tokenize(&token, data.line.input, DEFAULT)) return (1); diff --git a/42sh/src/parser/ft_parse.c b/42sh/src/parser/ft_parse.c index c2645ed7..99565769 100644 --- a/42sh/src/parser/ft_parse.c +++ b/42sh/src/parser/ft_parse.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/30 17:14:58 by jhalford #+# #+# */ -/* Updated: 2016/12/05 12:33:01 by jhalford ### ########.fr */ +/* Updated: 2016/12/07 17:37:25 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/parse_word.c b/42sh/src/parser/parse_word.c index 979a4667..9593f3ba 100644 --- a/42sh/src/parser/parse_word.c +++ b/42sh/src/parser/parse_word.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */ -/* Updated: 2016/12/01 16:39:24 by jhalford ### ########.fr */ +/* Updated: 2016/12/07 17:37:08 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,7 +23,6 @@ int parse_word(t_btree **ast, t_list **start, t_list **lst) node->type = TK_COMMAND; node->data.sstr = ft_sstradd(node->data.sstr, token->data); ft_parse(ast, &(*lst)->next); - ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free); return (0); }