init/destroy methods for shell_fds and jobc. (#210, #214)

This commit is contained in:
Jack Halford 2017-03-27 16:05:38 +02:00
parent f3aa0ec096
commit f17e232003
20 changed files with 142 additions and 111 deletions

View file

@ -78,8 +78,6 @@ exec/exec_init.c\
exec/exec_leaf.c\
exec/exec_or_if.c\
exec/exec_pipe.c\
exec/exec_popfds.c\
exec/exec_pushfds.c\
exec/exec_semi.c\
exec/exec_var.c\
exec/fd_is_valid.c\
@ -197,6 +195,7 @@ job_control/job_update_id.c\
job_control/job_update_status.c\
job_control/job_wait.c\
job_control/jobc_destroy.c\
job_control/jobc_init.c\
job_control/mark_job_as_running.c\
job_control/pprint_brace.c\
job_control/pprint_case.c\
@ -286,8 +285,9 @@ main/data_init.c\
main/data_singleton.c\
main/ft_putast.c\
main/main.c\
main/shell_fds.c\
main/shell_init.c\
main/shell_reset.c\
main/shell_sig.c\
parser/add_bang.c\
parser/add_case.c\
parser/add_cmd.c\

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 20:29:56 by jhalford #+# #+# */
/* Updated: 2017/03/26 21:39:33 by jhalford ### ########.fr */
/* Updated: 2017/03/27 15:46:34 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -131,7 +131,6 @@ struct s_job
struct s_exec
{
t_job job;
t_list *fd_save[10];
t_flag attrs;
int fdin;
t_list *op_stack;
@ -141,8 +140,6 @@ struct s_exec
int exec_init(t_exec *exec);
int exec_destroy(t_exec *exec);
int exec_pushfds();
int exec_popfds();
int process_fork(t_process *p);
int process_setgroup(t_process *p, pid_t pid);
void process_setsig(void);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/10 16:55:09 by jhalford #+# #+# */
/* Updated: 2017/03/27 03:11:08 by jhalford ### ########.fr */
/* Updated: 2017/03/27 16:01:12 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -35,6 +35,7 @@ struct s_jobc
struct termios shell_tmodes;
};
int jobc_init(t_jobc *jobc);
int jobc_destroy(t_jobc *jobc);
t_list *job_getprocess(pid_t pid);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */
/* Updated: 2017/03/25 15:44:08 by ariard ### ########.fr */
/* Updated: 2017/03/27 15:55:04 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -48,6 +48,7 @@ struct s_data
char **argv;
t_line line;
t_lexer lexer;
t_list *fd_save[10];
t_parser parser;
t_comp *comp;
t_exec exec;
@ -64,8 +65,14 @@ int data_init(int ac, char **av, char **env);
void data_exit(void);
int get_c_arg(char *opt_arg, t_data *data);
void shell_resetfds(void);
void shell_resetsig(void);
void shell_sig_reset(void);
void shell_fds_init(void);
void shell_fds_destroy(void);
int shell_fds_pop(void);
int shell_fds_push(void);
void shell_fds_reset(void);
void shell_fds_destroy(void);
char *ft_putast(void *node);
void ft_putast2(void *node);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/13 13:09:57 by jhalford #+# #+# */
/* Updated: 2017/03/25 16:36:55 by ariard ### ########.fr */
/* Updated: 2017/03/27 16:03:10 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/27 03:09:58 by jhalford #+# #+# */
/* Updated: 2017/03/27 03:10:37 by jhalford ### ########.fr */
/* Updated: 2017/03/27 15:56:47 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,7 +15,9 @@
int exec_destroy(t_exec *exec)
{
if (!exec)
return (0);
return (1);
ft_lstdel(&exec->op_stack, ft_lst_cfree);
exec->attrs = 0;
exec->fdin = STDIN;
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/26 21:28:09 by jhalford #+# #+# */
/* Updated: 2017/03/26 22:13:04 by jhalford ### ########.fr */
/* Updated: 2017/03/27 15:57:19 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -24,19 +24,9 @@ int job_init(t_job *job)
int exec_init(t_exec *exec)
{
t_jobc *jobc;
int i;
jobc = &data_singleton()->jobc;
job_init(&exec->job);
i = -1;
while (++i < 10)
exec->fd_save[i] = NULL;
exec_pushfds();
exec->attrs = 0;
exec->fdin = STDIN;
exec->op_stack = NULL;
jobc->first_job = NULL;
jobc->current_id = 1;
return (0);
}

View file

@ -1,29 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exec_pushfds.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/26 21:31:46 by jhalford #+# #+# */
/* Updated: 2017/03/27 00:58:25 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int exec_pushfds(void)
{
int i;
int fd;
t_exec *exec;
exec = &data_singleton()->exec;
i = -1;
while (++i < 10)
{
fd = fcntl(i, F_DUPFD_CLOEXEC, 10);
push(&(exec->fd_save[i]), fd);
}
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/07 15:48:24 by jhalford #+# #+# */
/* Updated: 2017/03/24 14:56:11 by wescande ### ########.fr */
/* Updated: 2017/03/27 16:03:21 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/07 14:53:31 by jhalford #+# #+# */
/* Updated: 2017/03/22 21:32:36 by wescande ### ########.fr */
/* Updated: 2017/03/27 16:02:52 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -28,7 +28,7 @@ int plaunch_file(t_process *p)
error_launch(p->data.cmd.av[0], ": is a directory", 126);
else if (access(p->data.cmd.path, X_OK) == -1)
error_launch("permission denied: ", p->data.cmd.av[0], 126);
(*p->data.cmd.execf)(
execve(
p->data.cmd.path,
p->data.cmd.av,
data_singleton()->env);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/13 22:21:19 by jhalford #+# #+# */
/* Updated: 2017/03/27 01:01:27 by jhalford ### ########.fr */
/* Updated: 2017/03/27 16:04:22 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -27,10 +27,16 @@ int process_fork(t_process *p)
process_setgroup(p, 0);
process_setsig();
exec_destroy(&data_singleton()->exec);
exec_init(&data_singleton()->exec);
jobc_destroy(&data_singleton()->jobc);
if (p->type == PROCESS_FILE)
shell_fds_destroy();
else
shell_fds_push();
data_singleton()->opts &= ~SH_INTERACTIVE;
data_singleton()->opts &= ~SH_OPTS_JOBC;
exit(p->map.launch(p));
pid = p->map.launch(p);
shell_fds_destroy();
exit(pid);
}
int process_launch(t_process *p)
@ -52,12 +58,12 @@ int process_launch(t_process *p)
set_exitstatus(1, 1);
else
{
exec_pushfds();
shell_fds_push();
p->map.launch(p);
exec_popfds();
shell_resetfds();
shell_resetsig();
shell_fds_pop();
}
shell_fds_reset();
shell_sig_reset();
process_free(p, 0);
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/15 13:01:19 by jhalford #+# #+# */
/* Updated: 2017/03/21 14:14:45 by jhalford ### ########.fr */
/* Updated: 2017/03/27 15:58:33 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/27 03:09:41 by jhalford #+# #+# */
/* Updated: 2017/03/27 03:10:55 by jhalford ### ########.fr */
/* Updated: 2017/03/27 15:59:29 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -1,30 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exec_popfds.c :+: :+: :+: */
/* jobc_init.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/26 21:31:05 by jhalford #+# #+# */
/* Updated: 2017/03/27 00:58:34 by jhalford ### ########.fr */
/* Created: 2017/03/27 15:59:03 by jhalford #+# #+# */
/* Updated: 2017/03/27 16:01:41 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int exec_popfds(void)
int jobc_init(t_jobc *jobc)
{
int i;
int fd;
t_exec *exec;
exec = &data_singleton()->exec;
i = -1;
while (++i < 10)
{
fd = pop(&exec->fd_save[i]);
if (fd != -1)
close(fd);
}
jobc->first_job = NULL;
jobc->current_id = 1;
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/07 18:07:50 by jhalford #+# #+# */
/* Updated: 2017/03/27 03:11:57 by jhalford ### ########.fr */
/* Updated: 2017/03/27 16:04:32 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,7 +15,6 @@
void data_exit(void)
{
t_data *data;
int i;
data = data_singleton();
/* ft_strdel(&data->line.input); */
@ -27,9 +26,7 @@ void data_exit(void)
parser_destroy(&data->parser);
exec_destroy(&data->exec);
jobc_destroy(&data->jobc);
i = -1;
while (++i < 10)
ft_lstdel(&data->exec.fd_save[i], ft_lst_cfree);
shell_fds_destroy();
ft_lstdel(&data->lst_func, &tree_func_free);
ft_save_termios(-1);
ft_free_hash_table();

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 19:26:32 by jhalford #+# #+# */
/* Updated: 2017/03/26 21:54:19 by jhalford ### ########.fr */
/* Updated: 2017/03/27 16:00:19 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -86,6 +86,7 @@ int data_init(int ac, char **av, char **env)
lexer_init(&data->lexer);
parser_init(&data->parser);
exec_init(&data->exec);
jobc_init(&data->jobc);
if ((term_name = ft_getenv(data->env, "TERM")) == NULL)
term_name = "dumb";
if (tgetent(NULL, term_name) != 1)

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/20 14:45:40 by gwojda #+# #+# */
/* Updated: 2017/03/26 21:57:37 by jhalford ### ########.fr */
/* Updated: 2017/03/27 15:58:12 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

84
42sh/src/main/shell_fds.c Normal file
View file

@ -0,0 +1,84 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* shell_fds.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/27 15:32:13 by jhalford #+# #+# */
/* Updated: 2017/03/27 15:57:21 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void shell_fds_init(void)
{
t_data *data;
int i;
data = data_singleton();
i = -1;
while (++i < 10)
data->fd_save[i] = NULL;
}
int shell_fds_push(void)
{
t_data *data;
int i;
int fd;
data = data_singleton();
i = -1;
while (++i < 10)
{
fd = fcntl(i, F_DUPFD_CLOEXEC, 10);
push(&(data->fd_save[i]), fd);
}
return (0);
}
int shell_fds_pop(void)
{
t_data *data;
int i;
int fd;
data = data_singleton();
i = -1;
while (++i < 10)
{
fd = pop(&data->fd_save[i]);
if (fd != -1)
close(fd);
}
return (0);
}
void shell_fds_reset(void)
{
t_data *data;
int i;
int fd;
data = data_singleton();
i = -1;
while (++i < 10)
{
fd = top(data->fd_save[i]);
if (fd != -1)
dup2(fd, i);
}
}
void shell_fds_destroy(void)
{
t_data *data;
int i;
data = data_singleton();
i = -1;
while (++i < 10)
ft_lstdel(&data->fd_save[i], ft_lst_cfree);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/12 17:23:59 by jhalford #+# #+# */
/* Updated: 2017/03/25 01:41:36 by wescande ### ########.fr */
/* Updated: 2017/03/27 16:04:45 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -70,7 +70,7 @@ static int interactive_settings(void)
while (tcgetpgrp(STDIN) != (*shell_pgid = getpgrp()))
kill(-*shell_pgid, SIGTTIN);
*shell_pgid = getpid();
shell_resetsig();
shell_sig_reset();
if (setpgid(*shell_pgid, *shell_pgid))
{
SH_ERR("setpgid(): %s", strerror(errno));
@ -99,5 +99,6 @@ int shell_init(int ac, char **av, char **env)
return (-1);
if (SH_IS_INTERACTIVE(data->opts) && interactive_settings() < 0)
return (-1);
shell_fds_init();
return (0);
}

View file

@ -1,18 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* shell_reset.c :+: :+: :+: */
/* shell_sig_reset.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/22 16:07:14 by jhalford #+# #+# */
/* Updated: 2017/03/24 20:09:29 by jhalford ### ########.fr */
/* Created: 2017/03/27 15:35:01 by jhalford #+# #+# */
/* Updated: 2017/03/27 15:35:01 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void shell_resetsig(void)
void shell_sig_reset(void)
{
signal(SIGINT, SIG_IGN);
signal(SIGQUIT, SIG_IGN);
@ -21,19 +21,3 @@ void shell_resetsig(void)
signal(SIGTTOU, SIG_IGN);
signal(SIGCHLD, SIG_DFL);
}
void shell_resetfds(void)
{
t_exec *exec;
int i;
int fd;
exec = &data_singleton()->exec;
i = -1;
while (++i < 10)
{
fd = top(exec->fd_save[i]);
if (fd != -1)
dup2(fd, i);
}
}