From 9b6487f994f4e0fd77c7e99001ac4f0477686306 Mon Sep 17 00:00:00 2001 From: william Date: Fri, 24 Mar 2017 17:55:31 +0100 Subject: [PATCH 1/4] rectif leaks on noninteractif --- 42sh/src/main/data_exit.c | 1 + 42sh/src/main/main.c | 3 +++ 2 files changed, 4 insertions(+) diff --git a/42sh/src/main/data_exit.c b/42sh/src/main/data_exit.c index dba5a2ba..08ceca6e 100644 --- a/42sh/src/main/data_exit.c +++ b/42sh/src/main/data_exit.c @@ -17,6 +17,7 @@ void data_exit(void) t_data *data; data = data_singleton(); + ft_strdel(&data->line.input); ft_strdel(&data->binary); ft_sstrfree(data->env); ft_sstrfree(data->local_var); diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 396dc632..8d74f850 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -23,7 +23,10 @@ static int do_readline_routine(char **stream) || data->parser.state == UNDEFINED || data->lexer.state == HEREDOC); ret = readline(has_prompt, stream); if (ret == -1) + { + ft_strdel(stream); exit(1); + } if (ret == 1 && data->parser.state == UNDEFINED) error_eof(); return (ret); From deb53a6b9605101b31e12d2f3d80f82ffc16f370 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Fri, 24 Mar 2017 18:24:23 +0100 Subject: [PATCH 2/4] new env --- 42sh/libft/Makefile | 1 + 42sh/libft/includes/sstr.h | 3 +- 42sh/libft/src/sstr/ft_sstrmerge.c | 28 +++++++++++++++++ 42sh/src/builtin/builtin_env.c | 32 +++++++++++++++----- 42sh/src/exec/exec_reset.c | 6 ++-- 42sh/src/exec/mark_process_status.c | 10 ++++-- 42sh/src/exec/process_launch.c | 5 ++- 42sh/src/exec/redirect_great.c | 4 +-- 42sh/src/job_control/builtin_jobs.c | 9 ++---- 42sh/src/job_control/job_format_head.c | 3 +- 42sh/src/job_control/job_hup_all.c | 5 +-- 42sh/src/job_control/put_job_in_background.c | 4 +-- 42sh/src/main/main.c | 4 ++- 13 files changed, 81 insertions(+), 33 deletions(-) create mode 100644 42sh/libft/src/sstr/ft_sstrmerge.c diff --git a/42sh/libft/Makefile b/42sh/libft/Makefile index c0902fc6..48456c09 100644 --- a/42sh/libft/Makefile +++ b/42sh/libft/Makefile @@ -141,6 +141,7 @@ sstr/ft_sstrcat.c\ sstr/ft_sstrdel.c\ sstr/ft_sstrdup.c\ sstr/ft_sstrfree.c\ +sstr/ft_sstrmerge.c\ sstr/ft_sstrprint.c\ sstr/ft_sstrprint_fd.c\ sstr/ft_sstrsort.c\ diff --git a/42sh/libft/includes/sstr.h b/42sh/libft/includes/sstr.h index ac65cd02..b1f10bc2 100644 --- a/42sh/libft/includes/sstr.h +++ b/42sh/libft/includes/sstr.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/18 13:16:25 by jhalford #+# #+# */ -/* Updated: 2017/03/07 11:35:11 by ariard ### ########.fr */ +/* Updated: 2017/03/24 17:46:36 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,6 +18,7 @@ void ft_sstrsort(char **list, int (*cmp)()); void ft_sstrprint(char **list, char sep); void ft_sstrprint_fd(int fd, char **list, char sep); char **ft_sstrdup(char **list); +char **ft_sstrmerge(char **s1, char **s2); void ft_sstrdel(char **sstr, int index); void ft_sstrfree(char **sstr); char *ft_sstrcat(char **sstr, char sep); diff --git a/42sh/libft/src/sstr/ft_sstrmerge.c b/42sh/libft/src/sstr/ft_sstrmerge.c new file mode 100644 index 00000000..2705a285 --- /dev/null +++ b/42sh/libft/src/sstr/ft_sstrmerge.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sstrmerge.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/24 17:40:50 by jhalford #+# #+# */ +/* Updated: 2017/03/24 18:05:08 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char **ft_sstrmerge(char **s1, char **s2) +{ + char **out; + + out = ft_sstrdup(s1); + if (!s2) + return (out); + while (*s2) + { + out = ft_sstradd(out, *s2); + s2++; + } + return (out); +} diff --git a/42sh/src/builtin/builtin_env.c b/42sh/src/builtin/builtin_env.c index 838a9c24..51e5936d 100644 --- a/42sh/src/builtin/builtin_env.c +++ b/42sh/src/builtin/builtin_env.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/22 16:20:31 by gwojda #+# #+# */ -/* Updated: 2017/03/24 15:11:42 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 17:59:00 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,11 +18,11 @@ t_cliopts g_env_opts[] = { - {'i', NULL, BT_ENV_LI, 0, bt_env_geti}, + {'i', NULL, BT_ENV_LI, 0, NULL}, {0, 0, 0, 0, 0}, }; -int bt_env_geti(char ***av, t_env_data *data) +int bt_env_getcustom(char ***av, t_env_data *data) { if (!av || !*av || !data) return (1); @@ -31,7 +31,6 @@ int bt_env_geti(char ***av, t_env_data *data) data->custom_env = ft_sstradd(data->custom_env, **av); ++(*av); } - --(*av); return (0); } @@ -39,9 +38,19 @@ static int bt_env_parse(t_env_data *data, char **av) { data->flag = 0; data->av_data = NULL; - data->custom_env = NULL; + DG(); if (cliopts_get(av, g_env_opts, data)) return (1); + DG(); + data->custom_env = NULL; + bt_env_getcustom(&data->av_data, data); + DG(); + if (!(data->flag & BT_ENV_LI)) + { + DG("no -i"); + data->custom_env = ft_sstrmerge(data_singleton()->env, data->custom_env); + } + DG(); return (0); } @@ -55,11 +64,18 @@ int builtin_env(const char *path, (void)envp; if (bt_env_parse(&data, (char**)argv)) - return (ft_perror("env") && SH_ERR("usage: %s", ENV_USAGE) ? 0 : 0); - else if (!*data.av_data) - return (builtin_setenv(NULL, (char*[]){"setenv", 0}, NULL)); + return (ft_perror("env") && SH_ERR("usage: %s", ENV_USAGE)); + DG(); + if (!*data.av_data) + { + DG(); + ft_sstrprint(data.custom_env, '\n'); + ft_putchar('\n'); + return (0); + } else if ((pid = fork()) == 0) { + DG(); if (!(path = ft_strchr(data.av_data[0], '/') ? ft_strdup(data.av_data[0]) : ft_hash(data.av_data[0])) || access(path, F_OK) != 0) diff --git a/42sh/src/exec/exec_reset.c b/42sh/src/exec/exec_reset.c index e1865178..f2fd8909 100644 --- a/42sh/src/exec/exec_reset.c +++ b/42sh/src/exec/exec_reset.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/08 14:31:42 by jhalford #+# #+# */ -/* Updated: 2017/03/24 14:06:48 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 18:23:49 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,6 +18,7 @@ int exec_reset_job(t_job *job) job->pgid = 0; job->attrs = 0; job->first_process = NULL; + tcgetattr(STDIN, &job->tmodes); return (0); } @@ -31,12 +32,11 @@ int exec_reset(void) jobc = &data_singleton()->jobc; i = -1; while (++i < 10) - exec->fd_save[i] = fcntl(i, F_DUPFD_CLOEXEC, 10); + exec->fd_save[i] = fcntl(i, F_DUPFD, 10); exec->op_stack = NULL; exec->fdin = STDIN; exec->attrs = 0; exec_reset_job(&exec->job); - tcgetattr(STDIN, &exec->job.tmodes); jobc->first_job = NULL; jobc->current_id = 1; return (0); diff --git a/42sh/src/exec/mark_process_status.c b/42sh/src/exec/mark_process_status.c index 7c6af2b2..15f1f223 100644 --- a/42sh/src/exec/mark_process_status.c +++ b/42sh/src/exec/mark_process_status.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 12:41:11 by jhalford #+# #+# */ -/* Updated: 2017/03/21 14:05:28 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 17:28:45 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,7 @@ int mark_process_status(pid_t pid, int status) { t_list *plist; t_process *p; + int signo; if ((plist = job_getprocess(pid))) { @@ -26,8 +27,11 @@ int mark_process_status(pid_t pid, int status) else { p->state = PROCESS_COMPLETED; - if (WIFSIGNALED(status) && WTERMSIG(status) != SIGPIPE) - ft_dprintf(2, "%s\n", strsignal((WTERMSIG(status)))); + if (WIFSIGNALED(status) && (signo = WTERMSIG(status))) + { + if (signo != SIGPIPE && signo != SIGINT) + ft_dprintf(2, "%s\n", strsignal(signo)); + } } return (0); } diff --git a/42sh/src/exec/process_launch.c b/42sh/src/exec/process_launch.c index b044bdbd..eb01f277 100644 --- a/42sh/src/exec/process_launch.c +++ b/42sh/src/exec/process_launch.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/13 22:21:19 by jhalford #+# #+# */ -/* Updated: 2017/03/24 14:56:32 by wescande ### ########.fr */ +/* Updated: 2017/03/24 18:22:16 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,7 +41,10 @@ int process_launch(t_process *p) if (process_redirect(p)) set_exitstatus(1, 1); else + { + exec_reset(); p->map.launch(p); + } shell_resetfds(); shell_resetsig(); process_free(p, 0); diff --git a/42sh/src/exec/redirect_great.c b/42sh/src/exec/redirect_great.c index 49f59d8e..4bca5724 100644 --- a/42sh/src/exec/redirect_great.c +++ b/42sh/src/exec/redirect_great.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 22:03:53 by jhalford #+# #+# */ -/* Updated: 2017/03/20 18:15:43 by gwojda ### ########.fr */ +/* Updated: 2017/03/24 16:58:17 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,6 +21,6 @@ int redirect_great(t_redir *redir) if ((fdold = open(redir->word, O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0) exit(1); - dup2(fdold, fdnew); + dup2_close(fdold, fdnew); return (0); } diff --git a/42sh/src/job_control/builtin_jobs.c b/42sh/src/job_control/builtin_jobs.c index 22ef92a6..68ae3c86 100644 --- a/42sh/src/job_control/builtin_jobs.c +++ b/42sh/src/job_control/builtin_jobs.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 17:43:01 by jhalford #+# #+# */ -/* Updated: 2017/03/24 15:39:48 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 18:17:02 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -66,12 +66,9 @@ int builtin_jobs(const char *path, char *const av[], char *const envp[]) (void)path; (void)envp; - do_job_notification(); if (!SH_HAS_JOBC(data_singleton()->opts)) - { - SH_ERR("jobs: %s", SH_MSG_NOJOBC); - return (1); - } + return (SH_ERR("jobs: %s", SH_MSG_NOJOBC)); + do_job_notification(); ft_bzero(&data, sizeof(t_data_template)); if (cliopts_get((char**)av, g_jobs_opts, &data)) return (ft_perror("jobs")); diff --git a/42sh/src/job_control/job_format_head.c b/42sh/src/job_control/job_format_head.c index 450fcdeb..f12526cb 100644 --- a/42sh/src/job_control/job_format_head.c +++ b/42sh/src/job_control/job_format_head.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/09 13:10:38 by jhalford #+# #+# */ -/* Updated: 2017/03/21 14:55:16 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 16:55:55 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,6 +20,5 @@ void job_format_head(t_job *j) job_getrank(&rank); crank = j->id == rank[0] ? '+' : ' '; crank = j->id == rank[1] ? '-' : crank; - DG("raks [%i:%i]", rank[0], rank[1]); ft_printf("{mag}[%i]%c {eoc}", j->id, crank); } diff --git a/42sh/src/job_control/job_hup_all.c b/42sh/src/job_control/job_hup_all.c index 0933048b..9e38a8c0 100644 --- a/42sh/src/job_control/job_hup_all.c +++ b/42sh/src/job_control/job_hup_all.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/20 11:37:40 by jhalford #+# #+# */ -/* Updated: 2017/03/24 15:40:55 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 15:54:19 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,11 +25,8 @@ void job_hup_all(void) while (jlist) { job = jlist->content; - DG("gonna SIGHUP %i", job->pgid); - builtin_jobs(NULL, NULL, NULL); if (job->pgid != 1) kill(-job->pgid, SIGHUP); - DG("after SIGHUP %i", job->pgid); jlist = jlist->next; } } diff --git a/42sh/src/job_control/put_job_in_background.c b/42sh/src/job_control/put_job_in_background.c index b0302223..ae75068d 100644 --- a/42sh/src/job_control/put_job_in_background.c +++ b/42sh/src/job_control/put_job_in_background.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 15:03:29 by jhalford #+# #+# */ -/* Updated: 2017/03/10 15:08:14 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 17:24:46 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,6 @@ int put_job_in_background(t_job *j, int cont) { if (cont) if (kill(-j->pgid, SIGCONT) < 0) - DG("kill(SIGCONT) failed"); + SH_ERR("kill(): %s", strerror(errno)); return (0); } diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 4f32fbba..e63fdc2c 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/20 14:45:40 by gwojda #+# #+# */ -/* Updated: 2017/03/24 15:30:51 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 18:23:39 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,6 +49,8 @@ static int handle_instruction(t_list **token, t_btree **ast) else if (ret > 0) break ; } + /* btree_print(3, *ast, ft_putast); */ + /* exit(1); */ if (data->parser.state == SUCCESS && ft_exec(ast) < 0) exit(1); else if (data->parser.state != SUCCESS) From 6c29b493e1f6f61edd8a010ca83d59725a4fec03 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Fri, 24 Mar 2017 18:25:52 +0100 Subject: [PATCH 3/4] ca compile --- 42sh/src/exec/redirect_great.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/42sh/src/exec/redirect_great.c b/42sh/src/exec/redirect_great.c index ed4cca69..5c39be5e 100644 --- a/42sh/src/exec/redirect_great.c +++ b/42sh/src/exec/redirect_great.c @@ -6,11 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 22:03:53 by jhalford #+# #+# */ -<<<<<<< HEAD -/* Updated: 2017/03/24 16:58:17 by jhalford ### ########.fr */ -======= -/* Updated: 2017/03/24 17:22:28 by gwojda ### ########.fr */ ->>>>>>> db98f2f1bcbfc8738908b02fd327e30e18e37f88 +/* Updated: 2017/03/24 18:25:41 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ From c4852a0303b718df429200991ceed19cf2e365be Mon Sep 17 00:00:00 2001 From: wescande Date: Fri, 24 Mar 2017 18:47:03 +0100 Subject: [PATCH 4/4] rectif multi_expand_var --- 42sh/includes/glob.h | 4 ++-- 42sh/src/glob/esc_print.c | 10 +++++----- 42sh/src/glob/expand_var.c | 6 +++--- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/42sh/includes/glob.h b/42sh/includes/glob.h index 77236481..223aff21 100644 --- a/42sh/includes/glob.h +++ b/42sh/includes/glob.h @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/04 16:31:18 by wescande #+# #+# */ -/* Updated: 2017/03/23 03:18:41 by wescande ### ########.fr */ +/* Updated: 2017/03/24 18:38:35 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -62,7 +62,7 @@ typedef struct s_bquote */ char **glob(char *str, unsigned char *esc, unsigned char *dbl_esc, int do_match); -void esc_print(char *str, unsigned char *esc); +void esc_print(int fd, char *str, unsigned char *esc); int word_is_assignment(char **content); void *tab_esc_copy(void *content); char *get_output(char *command); diff --git a/42sh/src/glob/esc_print.c b/42sh/src/glob/esc_print.c index a91afb64..c7362f81 100644 --- a/42sh/src/glob/esc_print.c +++ b/42sh/src/glob/esc_print.c @@ -6,13 +6,13 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/17 15:38:14 by jhalford #+# #+# */ -/* Updated: 2017/03/15 18:12:49 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 18:39:17 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -void esc_print(char *str, unsigned char *esc) +void esc_print(int fd, char *str, unsigned char *esc) { char *cur; @@ -20,10 +20,10 @@ void esc_print(char *str, unsigned char *esc) while (*cur) { if (is_char_esc(esc, str, cur)) - ft_printf("\\%c", *cur); + ft_dprintf(fd, "\\%c", *cur); else - ft_printf("%c", *cur); + ft_dprintf(fd, "%c", *cur); ++cur; } - ft_printf("\n"); + ft_dprintf(fd, "\n"); } diff --git a/42sh/src/glob/expand_var.c b/42sh/src/glob/expand_var.c index 1de56e57..2bc248af 100644 --- a/42sh/src/glob/expand_var.c +++ b/42sh/src/glob/expand_var.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 16:09:27 by wescande #+# #+# */ -/* Updated: 2017/03/07 12:07:53 by wescande ### ########.fr */ +/* Updated: 2017/03/24 18:42:15 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,12 +32,12 @@ static void insert_var(t_glob *gl, char *pos, char *name, char *content) gl->pat = ft_strjoinf(ft_strjoin(s1, content), s2, 1); new_esc = calc_expand_esc(gl->esc, ft_strlen(s1), (int[2]){ft_strlen(content), 1}, - (int[2]){delta, ft_strlen(s2)}); + (int[2]){delta - 1, ft_strlen(s2)}); ft_memdel((void **)&gl->esc); gl->esc = new_esc; new_esc = calc_expand_esc(gl->esc2, ft_strlen(s1), (int[2]){ft_strlen(content), 1}, - (int[2]){delta, ft_strlen(s2)}); + (int[2]){delta - 1, ft_strlen(s2)}); ft_memdel((void **)&gl->esc2); gl->esc2 = new_esc; ft_strdel(&s1);