exitstatus function, so i can set ? variable in one line with not memory handling. No more leaks, sexy debug macro DG(), valgrind suppression, gitignore

This commit is contained in:
Jack Halford 2016-12-07 18:27:49 +01:00
parent d24633c9e6
commit 4feaa64e08
42 changed files with 158 additions and 66 deletions

2
42sh/.gitignore vendored
View file

@ -1,3 +1,5 @@
minishell minishell
21sh 21sh
42sh 42sh
out
*.dSYM

10
42sh/.valgrind.supp Normal file
View file

@ -0,0 +1,10 @@
{
tgetent_1
Memcheck:Leak
obj:*ncurses*
}
{
valgrind_1
Memcheck:Leak
obj:*valgrind*
}

View file

@ -20,7 +20,7 @@ D_LIB = ft ncurses
O_LIB = $(addprefix -l, $(D_LIB)) O_LIB = $(addprefix -l, $(D_LIB))
W_FLAGS = -Wall -Wextra -Werror W_FLAGS = -Wall -Wextra -Werror
D_FLAGS = D_FLAGS = -g
MKDIR = mkdir -p MKDIR = mkdir -p
RM = /bin/rm -rf RM = /bin/rm -rf

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 20:29:56 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_process(char **argv, t_data *data);
int ft_cmd_exec(char *execpath, 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 ast_free(void *data, size_t content_size);
void set_exitstatus(t_data *data, int status);
#endif #endif

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:15:50 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 13:38:21 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 13:07:44 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); void sig_handler(int signo);
int data_init(t_data *data); int data_init(t_data *data);
void data_exit(t_data *data);
void ft_cleanup(void); void ft_cleanup(void);
int ft_builtin(char **av, t_data *data); 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); int builtin_env(char **av, t_data *data);
void ft_expand_dollar(char **av, char **env); 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); char *ft_putast(void *node);
void ft_putast2(void *node); void ft_putast2(void *node);

@ -1 +1 @@
Subproject commit 1012e7bc51bb9b80cf9e62cacec4e61a6a37507d Subproject commit 97bc4fed552dce523e2de8fb744ba46ec877f8eb

View file

@ -43,7 +43,7 @@ int ft_builtin(char **av, t_data *data)
else else
{ {
ret = (g_builtin[i].f)(av, data); ret = (g_builtin[i].f)(av, data);
builtin_setenv((char*[3]){"?", ft_itoa(ret)}, data); set_exitstatus(data, ret);
} }
return (1); return (1);
} }

View file

@ -21,7 +21,7 @@ int builtin_echo(char **av, t_data *data)
ft_printf("%s", *av); ft_printf("%s", *av);
av++; av++;
if (*av) if (*av)
ft_putstr(" "); ft_putchar(' ');
} }
ft_putchar('\n'); ft_putchar('\n');
return (0); return (0);

View file

@ -20,7 +20,10 @@ int builtin_env(char **av, t_data *data)
i = 1; i = 1;
env = NULL; env = NULL;
if (!av[1]) if (!av[1])
{
ft_sstrprint(data->env, '\n'); ft_sstrprint(data->env, '\n');
ft_putchar('\n');
}
else else
{ {
while (av[i] && ft_strchr(av[i], '=')) while (av[i] && ft_strchr(av[i], '='))

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:25:17 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; char **env;
env = data->env; env = data->env;
DG("doing setenv now");
if (ft_strcmp(av[0], "setenv") == 0) if (ft_strcmp(av[0], "setenv") == 0)
av++; av++;
if (!av[0]) if (!av[0])
{
ft_sstrprint(data->env, '\n'); ft_sstrprint(data->env, '\n');
ft_putchar('\n');
}
else else
{ {
str = ft_str3join(av[0], "=", av[1]); 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]) == '=') if (ft_strcmp(*env, av[0]) == '=')
{ {
ft_strdel(env);
*env = str; *env = str;
return (0); return (0);
} }
env++; env++;
} }
data->env = ft_sstradd(data->env, str); data->env = ft_sstradd(data->env, str);
ft_strdel(&str);
} }
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/05 11:50:51 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; (void)content_size;
node = data; node = data;
DG("gonna free a node");
if (node->type == TK_COMMAND) if (node->type == TK_COMMAND)
{ {
DG("ast_free TK_COMMAND");
if (node->data.sstr) if (node->data.sstr)
ft_sstrfree(node->data.sstr); ft_sstrfree(node->data.sstr);
} }
else if (node->type == TK_LESS || node->type == TK_GREAT || node->type == TK_DGREAT) 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); ft_strdel(&node->data.redir.word.word);
} }
DG("ast_free done");
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 17:28:14 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/30 21:06:17 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 21:13:23 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]; int fds[2];
pipe(fds); 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]; data->exec.fdout = fds[PIPE_WRITE];
ft_exec(&(*ast)->left, data); ft_exec(&(*ast)->left, data);
if (data->exec.fdout != STDOUT) if (data->exec.fdout != STDOUT)

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 21:13:18 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) int ft_cmd_process(char **argv, t_data *data)
{ {
char **path;
char *execpath; char *execpath;
path = ft_strsplit(ft_getenv(data->env, "PATH"), ':');
ft_expand_dollar(argv, data->env); ft_expand_dollar(argv, data->env);
if (ft_builtin(argv, data)) if (ft_builtin(argv, data))
return (0); return (0);
else if (ft_strchr(argv[0], '/')) else if (ft_strchr(argv[0], '/'))
execpath = 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]); 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 (-1);
} }
return (ft_cmd_exec(execpath, argv, data)); 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; pid_t pid;
int status; int status;
char **environ;
if (access(execpath, X_OK) == -1) if (access(execpath, X_OK) == -1)
{ {
ft_dprintf(2, "%s: permission denied: %s\n", SHELL_NAME, argv[0]); ft_dprintf(2, "%s: permission denied: %s\n", SHELL_NAME, argv[0]);
ft_strdel(&execpath);
return (-1); return (-1);
} }
if ((pid = fork()) == -1) if ((pid = fork()) == -1)
@ -50,15 +48,16 @@ int ft_cmd_exec(char *execpath, char **argv, t_data *data)
else if (pid == 0) else if (pid == 0)
{ {
fd_redirect(data); fd_redirect(data);
environ = ft_sstrdup(data->env); execve(execpath, argv, data->env);
execve(execpath, argv, environ);
} }
else if ((g_pid = pid)) else
{ {
ft_strdel(&execpath);
g_pid = pid;
if (data->exec.fdout == STDOUT) if (data->exec.fdout == STDOUT)
{ {
waitpid(pid, &status, 0); waitpid(pid, &status, 0);
builtin_setenv((char*[3]){"?", ft_itoa(status)}, data); set_exitstatus(data, status);
} }
g_pid = 0; g_pid = 0;
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 20:30:32 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,33 +6,40 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/03 13:37:49 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" #include "minishell.h"
char *ft_findexec(char **path, char *file) char *ft_findexec(char *path, char *file)
{ {
int i; int i;
DIR *dir; DIR *dir;
char *execpath; char *execpath;
char **spath;
struct dirent *dirent; struct dirent *dirent;
i = -1; 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 ; continue ;
while ((dirent = readdir(dir))) while ((dirent = readdir(dir)))
{ {
if (ft_strcmp(dirent->d_name, file)) if (ft_strcmp(dirent->d_name, file))
continue ; continue ;
if (path[i][ft_strlen(path[i])] != '/') if (spath[i][ft_strlen(spath[i])] != '/')
ft_strcat(path[i], "/"); execpath = ft_str3join(spath[i], "/", dirent->d_name);
execpath = ft_strjoin(path[i], dirent->d_name); else
execpath = ft_strjoin(spath[i], dirent->d_name);
ft_sstrfree(spath);
closedir(dir);
return (execpath); return (execpath);
} }
closedir(dir);
} }
ft_sstrfree(spath);
return (NULL); return (NULL);
} }

View file

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* set_exitstatus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 13:37:11 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) if (!*str)
return (0); return (0);
token = token_init();
if (!*alst) if (!*alst)
{
token = token_init();
*alst = ft_lstnew(token, sizeof(*token)); *alst = ft_lstnew(token, sizeof(*token));
}
if (ft_is_delim(*str)) if (ft_is_delim(*str))
state = DELIM; state = DELIM;
else if (*str == '&' || *str == ';' || *str == '|') else if (*str == '&' || *str == ';' || *str == '|')

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 18:36:21 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/30 17:08:55 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/03 12:07:30 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 15:30:25 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:39:01 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; type = token->type;
while (type >> (i++ + 2)) while (type >> (i++ + 2))
; ;
ft_dprintf(2, "%02i '%s'\n", i, token->data); DG("%02i '%s'", i, token->data);
lst = lst->next; lst = lst->next;
} }
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 13:43:40 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 13:44:15 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:14:12 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 13:44:24 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)data;
(void)buf; (void)buf;
data_exit(data);
ft_putendl("exit"); ft_putendl("exit");
exit(0); exit(0);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 18:45:23 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/29 15:56:36 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) if (*(t_qstate*)data->line.qstack->content == Q_NONE)
{ {
ft_putchar('\n'); ft_putchar('\n');
ft_history_add(data);
return (2); return (2);
} }
ft_key_default(data, buf); ft_key_default(data, buf);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:14:09 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) int ft_set_termios(t_data *data, int input_mode)
{ {
struct termios term; struct termios term;
char *term_name;
if ((term_name = ft_getenv(data->env, "TERM")) == NULL) (void)data;
return (-1);
if (tgetent(NULL, term_name) != 1)
return (-1);
if (tcgetattr(0, &term) == -1) if (tcgetattr(0, &term) == -1)
return (-1); return (-1);
if (input_mode) if (input_mode)

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/03 13:35:03 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

21
42sh/src/main/data_exit.c Normal file
View file

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* data_exit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 19:26:32 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) int data_init(t_data *data)
{ {
char *term_name;
atexit(&ft_cleanup); atexit(&ft_cleanup);
data->line.input = NULL;
data->env = ft_sstrdup(environ); data->env = ft_sstrdup(environ);
data->line.history = NULL; data->line.history = NULL;
data->exec.fdin = STDIN; data->exec.fdin = STDIN;
@ -25,5 +28,9 @@ int data_init(t_data *data)
data->exec.aol_search = 0; data->exec.aol_search = 0;
if (!(data->line.history = ft_dlstnew(NULL, 0))) if (!(data->line.history = ft_dlstnew(NULL, 0)))
return (-1); return (-1);
if ((term_name = ft_getenv(data->env, "TERM")) == NULL)
return (-1);
if (tgetent(NULL, term_name) != 1)
return (-1);
return (0); return (0);
} }

View file

@ -6,16 +6,23 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 14:42:42 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" #include "minishell.h"
extern char **environ;
extern char PC;
extern char *UP;
extern char *BC;
void ft_cleanup(void) void ft_cleanup(void)
{ {
struct termios term; struct termios term;
DG("cleanup. char * UP at %p", UP);
DG("cleanup. char * BC at %p", BC);
if (tcgetattr(0, &term) == -1) if (tcgetattr(0, &term) == -1)
return ; return ;
term.c_lflag |= ICANON | ISIG | ECHO; term.c_lflag |= ICANON | ISIG | ECHO;

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 18:18:04 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) else if (node->type == TK_LESSAND)
return (" <& "); return (" <& ");
else else
{
ft_printf("type=%02i\n", node->type);
return ("OTHER"); return ("OTHER");
}
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 18:18:04 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -5,8 +5,8 @@
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 21:13:34 by jhalford #+# #+# */ /* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */
/* Updated: 2016/12/05 12:11:27 by jhalford ### ########.fr */ /* Updated: 2016/12/09 22:15:07 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -24,11 +24,12 @@ int main(void)
return (1); return (1);
if (signal(SIGINT, sig_handler) == SIG_ERR) if (signal(SIGINT, sig_handler) == SIG_ERR)
ft_dprintf(STDERR, "\ncan't catch SIGINT\n"); ft_dprintf(STDERR, "\ncan't catch SIGINT\n");
DG("{inv}{bol}{gre}start of shell");
while (1) while (1)
{ {
if (ft_interactive_sh(&data)) if (ft_interactive_sh(&data))
return (1); return (1);
/* ft_dprintf(STDERR, "command='%s'\n", data.input); */ DG("{inv}{mag}got command '%s'", data.line.input);
token = NULL; token = NULL;
if (ft_tokenize(&token, data.line.input, DEFAULT)) if (ft_tokenize(&token, data.line.input, DEFAULT))
return (1); return (1);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/30 17:14:58 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 12:49:45 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->type = TK_COMMAND;
node->data.sstr = ft_sstradd(node->data.sstr, token->data); node->data.sstr = ft_sstradd(node->data.sstr, token->data);
ft_parse(ast, &(*lst)->next); ft_parse(ast, &(*lst)->next);
ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free); ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free);
return (0); return (0);
} }