cleanup after merge (few segfaults remained because of auto merge done the wrong way around. process_reset added

This commit is contained in:
Jack Halford 2017-01-02 21:31:20 +01:00
parent 4c63c30b04
commit 9e8939d588
11 changed files with 55 additions and 40 deletions

View file

@ -25,8 +25,8 @@
# define PROCESS_PIPESTART (1 << 6) # define PROCESS_PIPESTART (1 << 6)
# define PROCESS_PIPEEND (1 << 7) # define PROCESS_PIPEEND (1 << 7)
# define IS_PIPESTART(p) (p & (PROCESS_PIPESTART)) # define IS_PIPESTART(a) (a & PROCESS_PIPESTART)
# define IS_PIPEEND(p) (p & (PROCESS_PIPEEND)) # define IS_PIPEEND(a) (a & PROCESS_PIPEEND)
# include "libft.h" # include "libft.h"
# include "types.h" # include "types.h"
@ -80,6 +80,7 @@ int process_setexec(t_process *p);
int process_setgroup(t_process *p); int process_setgroup(t_process *p);
int process_redirect(t_process *p); int process_redirect(t_process *p);
void process_free(void *content, size_t content_size); void process_free(void *content, size_t content_size);
void process_reset(void);
void fd_redirect(void); void fd_redirect(void);
void fd_reset(void); void fd_reset(void);

View file

@ -35,10 +35,13 @@ int builtin_setenv(const char *path, char *const av[], char *const envp[])
str = ft_str3join(av[0], "=", av[1]); str = ft_str3join(av[0], "=", av[1]);
while ((*env)[i]) while ((*env)[i])
{ {
/* DG("check 2: i=%i, (*env)[i]=%p",i, (*env)[i]); */
/* DG("content=%s", (*env)[i]); */
if (ft_strcmp((*env)[i], av[0]) == '=') if (ft_strcmp((*env)[i], av[0]) == '=')
{ {
ft_strdel(&(*env)[i]); ft_strdel(&(*env)[i]);
(*env)[i] = str; (*env)[i] = str;
DG("done setenv");
return (0); return (0);
} }
i++; i++;
@ -46,5 +49,6 @@ int builtin_setenv(const char *path, char *const av[], char *const envp[])
*env = ft_sstradd(*env, str); *env = ft_sstradd(*env, str);
ft_strdel(&str); ft_strdel(&str);
} }
DG("done setenv");
return (0); return (0);
} }

View file

@ -29,7 +29,7 @@ t_execf *is_builtin(t_process *p)
i = -1; i = -1;
while (g_builtin[++i].name) while (g_builtin[++i].name)
if (ft_strcmp(g_builtin[i].name, p->argv[0]) == 0) if (ft_strcmp(g_builtin[i].name, p->av[0]) == 0)
return (g_builtin[i].f); return (g_builtin[i].f);
return (NULL); return (NULL);
} }

View file

@ -25,25 +25,23 @@ int exec_command(t_btree **ast)
if (process_setexec(p)) if (process_setexec(p))
{ {
ft_dprintf(2, "{red}%s: command not found: %s{eoc}\n", SHELL_NAME, p->av[0]); ft_dprintf(2, "{red}%s: command not found: %s{eoc}\n", SHELL_NAME, p->av[0]);
p->attributes = 0; set_exitstatus(127);
p->path = NULL;
p->argv = NULL;
btree_delone(ast, &ast_free);
return (0);
} }
DG("gonna launch_process now"); else
launch_process(p);
job_addprocess(p);
if (IS_PIPEEND(p->attributes))
{ {
if (JOB_IS_FG(job->attributes)) DG("gonna launch_process now");
put_job_in_foreground(job, 0); launch_process(p);
else DG("launch_process done");
put_job_in_background(job, 0); job_addprocess(p);
DG("job_addprocess done");
if (IS_PIPEEND(p->attributes))
{
JOB_IS_FG(job->attributes) ?
put_job_in_foreground(job, 0):
put_job_in_background(job, 0);
}
} }
process_reset();
btree_delone(ast, &ast_free); btree_delone(ast, &ast_free);
p->attributes = 0;
p->path = NULL;
p->argv = NULL;
return (0); return (0);
} }

View file

@ -15,6 +15,7 @@
int exec_pipe(t_btree **ast) int exec_pipe(t_btree **ast)
{ {
int fds[2]; int fds[2];
int start;
t_data *data; t_data *data;
t_process *p; t_process *p;
@ -23,27 +24,30 @@ int exec_pipe(t_btree **ast)
pipe(fds); pipe(fds);
DG("pipe %i->%i", fds[PIPE_WRITE], fds[PIPE_READ]); DG("pipe %i->%i", fds[PIPE_WRITE], fds[PIPE_READ]);
p->fdout = fds[PIPE_WRITE]; p->fdout = fds[PIPE_WRITE];
if (!IS_PIPEEND(p->attributes)) start = 0;
if (!IS_PIPESTART(p->attributes))
{
p->attributes |= PROCESS_PIPESTART; p->attributes |= PROCESS_PIPESTART;
else start = 1;
p->attributes &= ~PROCESS_PIPESTART; }
p->attributes &= ~PROCESS_PIPEEND; p->attributes &= ~PROCESS_PIPESTART;
p->attributes &= ~PROCESS_PIPEEND;
ft_exec(&(*ast)->left); ft_exec(&(*ast)->left);
p->attributes &= ~PROCESS_PIPESTART;
if (p->fdout != STDOUT) if (p->fdout != STDOUT)
close(p->fdout); close(p->fdout);
p->fdout = STDOUT; p->fdout = STDOUT;
p->fdin = fds[PIPE_READ]; p->fdin = fds[PIPE_READ];
p->attributes &= ~PROCESS_PIPESTART;
p->attributes |= PROCESS_PIPEEND;
p->attributes |= PROCESS_PIPEEND;
ft_exec(&(*ast)->right); ft_exec(&(*ast)->right);
if (start)
p->attributes |= PROCESS_PIPESTART;
/* close(fds[PIPE_WRITE]); */ /* close(fds[PIPE_WRITE]); */
/* close(fds[PIPE_READ]); */ /* close(fds[PIPE_READ]); */
p->fdin = STDIN; p->fdin = STDIN;
p->fdout = STDOUT; p->fdout = STDOUT;
p->attributes |= PROCESS_PIPESTART;
p->attributes &= ~PROCESS_PIPEEND;
btree_delone(ast, &ast_free); btree_delone(ast, &ast_free);
return (0); return (0);
} }

View file

@ -20,7 +20,7 @@ int launch_process(t_process *p)
exec = &data_singleton()->exec; exec = &data_singleton()->exec;
if (p->attributes & PROCESS_UNKNOWN) if (p->attributes & PROCESS_UNKNOWN)
ft_dprintf(2, "%s: command not found: %s\n", SHELL_NAME, p->av[0]); ft_dprintf(2, "%s: command not found: %s\n", SHELL_NAME, p->av[0]);
else if (p->attributes & PROCESS_BUILTIN && p->fdout != STDOUT) else if (p->attributes & PROCESS_BUILTIN && p->fdout == STDOUT)
set_exitstatus((*p->execf)(p->path, p->av, data_singleton()->env)); set_exitstatus((*p->execf)(p->path, p->av, data_singleton()->env));
else else
{ {

View file

@ -0,0 +1,14 @@
#include "exec.h"
void process_reset(void)
{
t_data *data;
data = data_singleton();
data->exec.process.path = NULL;
data->exec.process.av = NULL;
data->exec.process.fdin = STDIN;
data->exec.process.fdout = STDOUT;
data->exec.process.pid = 0;
data->exec.process.attributes = PROCESS_PIPESTART | PROCESS_PIPEEND;
}

View file

@ -16,7 +16,7 @@ int process_setexec(t_process *p)
{ {
if ((p->execf = is_builtin(p))) if ((p->execf = is_builtin(p)))
{ {
DG("process is a builtin"); DG("process is a builtin, attr=%b", p->attributes);
p->attributes |= PROCESS_BUILTIN; p->attributes |= PROCESS_BUILTIN;
} }
else if (ft_strchr(p->av[0], '/')) else if (ft_strchr(p->av[0], '/'))

View file

@ -31,15 +31,11 @@ int builtin_jobs(const char *path, char *const av[], char *const envp[])
lg = 0; lg = 0;
if (ft_strcmp(av[1], "-l") == 0) if (ft_strcmp(av[1], "-l") == 0)
lg = 1; lg = 1;
rank = '+';
while (jlist) while (jlist)
{ {
firstp = 1; firstp = 1;
job = jlist->content; job = jlist->content;
rank = ' ';
if (job->id == data_singleton()->jobc.rank[0])
rank = '+';
else if (job->id == data_singleton()->jobc.rank[1])
rank = '-';
ft_printf("{mag}[%i] %c ", job->id, rank); ft_printf("{mag}[%i] %c ", job->id, rank);
if (lg) if (lg)
ft_printf("%i ", p->pid); ft_printf("%i ", p->pid);
@ -63,6 +59,7 @@ int builtin_jobs(const char *path, char *const av[], char *const envp[])
firstp = 0; firstp = 0;
} }
jlist = jlist->next; jlist = jlist->next;
rank = (rank == '+') ? '-' : ' ';
ft_printf("{eoc}\n"); ft_printf("{eoc}\n");
} }
return (0); return (0);

View file

@ -19,8 +19,10 @@ int job_addprocess(t_process *p)
jobc = &data_singleton()->jobc; jobc = &data_singleton()->jobc;
job = &data_singleton()->exec.job; job = &data_singleton()->exec.job;
DG("check; attr=%b", p->attributes);
if (IS_PIPESTART(p->attributes)) if (IS_PIPESTART(p->attributes))
{ {
DG("check");
job_update_id(); job_update_id();
job->id = jobc->current_id; job->id = jobc->current_id;
ft_lstadd(&jobc->first_job, ft_lstnew(job, sizeof(*job))); ft_lstadd(&jobc->first_job, ft_lstnew(job, sizeof(*job)));

View file

@ -23,10 +23,7 @@ int data_init(void)
data->line.input = NULL; data->line.input = NULL;
data->env = ft_sstrdup(environ); data->env = ft_sstrdup(environ);
data->line.history = NULL; data->line.history = NULL;
data->exec.process.fdin = STDIN; process_reset();
data->exec.process.fdout = STDOUT;
data->exec.process.pid = 0;
data->exec.process.attributes = PROCESS_PIPESTART;
data->exec.aol_status = NULL; data->exec.aol_status = NULL;
data->exec.aol_search = 0; data->exec.aol_search = 0;
data->exec.job.id = 0; data->exec.job.id = 0;
@ -35,8 +32,6 @@ int data_init(void)
data->exec.job.first_process = 0; data->exec.job.first_process = 0;
data->jobc.first_job = NULL; data->jobc.first_job = NULL;
data->jobc.current_id = 1; data->jobc.current_id = 1;
data->jobc.rank[0] = 0;
data->jobc.rank[1] = 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) if ((term_name = ft_getenv(data->env, "TERM")) == NULL)