env ./42sh still making problems
This commit is contained in:
parent
06b2ad21ba
commit
b1f730a833
16 changed files with 40 additions and 65 deletions
|
|
@ -131,7 +131,7 @@ struct s_job
|
||||||
struct s_exec
|
struct s_exec
|
||||||
{
|
{
|
||||||
t_job job;
|
t_job job;
|
||||||
int fd_save[3];
|
int fd_save[10];
|
||||||
t_flag attrs;
|
t_flag attrs;
|
||||||
int fdin;
|
int fdin;
|
||||||
t_list *op_stack;
|
t_list *op_stack;
|
||||||
|
|
|
||||||
|
|
@ -67,7 +67,7 @@ int data_init(int ac, char **av);
|
||||||
void data_exit(void);
|
void data_exit(void);
|
||||||
int get_c_arg(char ***av, t_data *data);
|
int get_c_arg(char ***av, t_data *data);
|
||||||
|
|
||||||
void content_free(void *data, size_t content_size);
|
/* void content_free(void *data, size_t content_size); */
|
||||||
|
|
||||||
char *ft_putast(void *node);
|
char *ft_putast(void *node);
|
||||||
void ft_putast2(void *node);
|
void ft_putast2(void *node);
|
||||||
|
|
|
||||||
|
|
@ -81,14 +81,14 @@ int builtin_cd(const char *path,
|
||||||
opts = CDOPT_L;
|
opts = CDOPT_L;
|
||||||
i = builtin_cd_opts(av, &opts);
|
i = builtin_cd_opts(av, &opts);
|
||||||
if (!(target = builtin_cd_special(av + i, envp)))
|
if (!(target = builtin_cd_special(av + i, envp)))
|
||||||
return (builtin_return_status(0, 1));
|
return (1);
|
||||||
cwd = getcwd(NULL, 0);
|
cwd = getcwd(NULL, 0);
|
||||||
builtin_setenv(path, (char*[4]){"setenv", "OLDPWD", cwd, NULL}, envp);
|
builtin_setenv(path, (char*[4]){"setenv", "OLDPWD", cwd, NULL}, envp);
|
||||||
free(cwd);
|
free(cwd);
|
||||||
if (chdir(target))
|
if (chdir(target))
|
||||||
{
|
{
|
||||||
SH_ERR(CDERR_1, target);
|
SH_ERR(CDERR_1, target);
|
||||||
return (builtin_return_status(0, 1));
|
return (1);
|
||||||
}
|
}
|
||||||
else if (target != av[i])
|
else if (target != av[i])
|
||||||
ft_printf("%s\n", target);
|
ft_printf("%s\n", target);
|
||||||
|
|
@ -97,5 +97,5 @@ int builtin_cd(const char *path,
|
||||||
free(cwd);
|
free(cwd);
|
||||||
if (!ft_strcmp(*(av + i), "-"))
|
if (!ft_strcmp(*(av + i), "-"))
|
||||||
free(target);
|
free(target);
|
||||||
return (builtin_return_status(0, 0));
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -25,5 +25,5 @@ int builtin_echo(const char *path, char *const av[], char *const envp[])
|
||||||
ft_putchar(' ');
|
ft_putchar(' ');
|
||||||
}
|
}
|
||||||
ft_putchar('\n');
|
ft_putchar('\n');
|
||||||
return (builtin_return_status(0, 0));
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,7 @@ int builtin_exit(const char *path, char *const av[], char *const envp[])
|
||||||
tcsetattr(STDIN, TCSANOW, &data_singleton()->jobc.shell_tmodes);
|
tcsetattr(STDIN, TCSANOW, &data_singleton()->jobc.shell_tmodes);
|
||||||
job_hup_all();
|
job_hup_all();
|
||||||
data_exit();
|
data_exit();
|
||||||
|
DG("EXITING NOW");
|
||||||
exit(status);
|
exit(status);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -52,6 +52,6 @@ int exec_leaf(t_btree **ast)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
DG("WHY HERE?");
|
DG("WHY HERE? --> because no fork means no job control");
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,13 +12,6 @@
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
static int print_error(char *std)
|
|
||||||
{
|
|
||||||
ft_dprintf(2, "{red}%s: internal fcntl %s error errno=%i{eoc}\n",
|
|
||||||
SHELL_NAME, std, errno);
|
|
||||||
return (errno);
|
|
||||||
}
|
|
||||||
|
|
||||||
int exec_reset_job(t_job *job)
|
int exec_reset_job(t_job *job)
|
||||||
{
|
{
|
||||||
job->id = 0;
|
job->id = 0;
|
||||||
|
|
@ -32,21 +25,13 @@ int exec_reset(void)
|
||||||
{
|
{
|
||||||
t_exec *exec;
|
t_exec *exec;
|
||||||
t_jobc *jobc;
|
t_jobc *jobc;
|
||||||
|
int i;
|
||||||
|
|
||||||
exec = &data_singleton()->exec;
|
exec = &data_singleton()->exec;
|
||||||
jobc = &data_singleton()->jobc;
|
jobc = &data_singleton()->jobc;
|
||||||
if (errno != EBADF)
|
i = -1;
|
||||||
{
|
while (++i < 10)
|
||||||
if ((exec->fd_save[0] = fcntl(STDIN, F_DUPFD_CLOEXEC, 10)) == -1
|
exec->fd_save[i] = fcntl(i, F_DUPFD_CLOEXEC, 10);
|
||||||
&& errno != EBADF)
|
|
||||||
return (print_error("STDIN"));
|
|
||||||
if ((exec->fd_save[1] = fcntl(STDOUT, F_DUPFD_CLOEXEC, 10)) == -1
|
|
||||||
&& errno != EBADF)
|
|
||||||
return (print_error("STDOUT"));
|
|
||||||
if ((exec->fd_save[2] = fcntl(STDERR, F_DUPFD_CLOEXEC, 10)) == -1
|
|
||||||
&& errno != EBADF)
|
|
||||||
return (print_error("STDERR"));
|
|
||||||
}
|
|
||||||
exec->op_stack = NULL;
|
exec->op_stack = NULL;
|
||||||
exec->fdin = STDIN;
|
exec->fdin = STDIN;
|
||||||
exec->attrs = 0;
|
exec->attrs = 0;
|
||||||
|
|
|
||||||
|
|
@ -14,8 +14,8 @@
|
||||||
|
|
||||||
int plaunch_builtin(t_process *p)
|
int plaunch_builtin(t_process *p)
|
||||||
{
|
{
|
||||||
return ((*p->data.cmd.execf)(
|
return (*p->data.cmd.execf)(
|
||||||
p->data.cmd.path,
|
p->data.cmd.path,
|
||||||
p->data.cmd.av,
|
p->data.cmd.av,
|
||||||
data_singleton()->env));
|
data_singleton()->env);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@
|
||||||
|
|
||||||
static void error_launch(char *error_ori, char *error_type, int error_code)
|
static void error_launch(char *error_ori, char *error_type, int error_code)
|
||||||
{
|
{
|
||||||
ft_dprintf(2, "{red}%s: %s%s{eoc}\n", SHELL_NAME, error_ori, error_type);
|
SH_ERR("%s: %s%s", SHELL_NAME, error_ori, error_type);
|
||||||
exit(error_code);
|
exit(error_code);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
int do_the_muther_forker(t_process *p)
|
int process_fork(t_process *p)
|
||||||
{
|
{
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
|
|
||||||
|
|
@ -35,8 +35,9 @@ int do_the_muther_forker(t_process *p)
|
||||||
exit(p->map.launch(p));
|
exit(p->map.launch(p));
|
||||||
}
|
}
|
||||||
|
|
||||||
static int do_the_fork_if_i_have_to(t_process *p)
|
int process_launch(t_process *p)
|
||||||
{
|
{
|
||||||
|
p->state = PROCESS_RUNNING;
|
||||||
if (IS_PIPESINGLE(*p)
|
if (IS_PIPESINGLE(*p)
|
||||||
&& p->type != PROCESS_FILE && p->type != PROCESS_SUBSHELL)
|
&& p->type != PROCESS_FILE && p->type != PROCESS_SUBSHELL)
|
||||||
{
|
{
|
||||||
|
|
@ -45,27 +46,13 @@ static int do_the_fork_if_i_have_to(t_process *p)
|
||||||
set_exitstatus(1, 1);
|
set_exitstatus(1, 1);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
return (p->map.launch(p));
|
set_exitstatus(p->map.launch(p), 1);
|
||||||
/* set_exitstatus(p->map.launch(p), 1); */
|
|
||||||
/* return (0); */
|
|
||||||
}
|
|
||||||
return (do_the_muther_forker(p));
|
|
||||||
}
|
|
||||||
|
|
||||||
int process_launch(t_process *p)
|
|
||||||
{
|
|
||||||
pid_t pid;
|
|
||||||
|
|
||||||
p->state = PROCESS_RUNNING;
|
|
||||||
if (!(pid = do_the_fork_if_i_have_to(p)))
|
|
||||||
{
|
|
||||||
process_resetfds(p);
|
process_resetfds(p);
|
||||||
process_free(p, 0);
|
process_free(p, 0);
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
DG("FORK");
|
p->pid = process_fork(p):
|
||||||
p->pid = pid;
|
process_setgroup(p, p->pid);
|
||||||
process_setgroup(p, pid);
|
|
||||||
if (p->fdin != STDIN)
|
if (p->fdin != STDIN)
|
||||||
close(p->fdin);
|
close(p->fdin);
|
||||||
if (p->fdout != STDOUT)
|
if (p->fdout != STDOUT)
|
||||||
|
|
|
||||||
|
|
@ -19,20 +19,13 @@ void process_resetfds(t_process *p)
|
||||||
|
|
||||||
(void)p;
|
(void)p;
|
||||||
exec = &data_singleton()->exec;
|
exec = &data_singleton()->exec;
|
||||||
i = 0;
|
i = -1;
|
||||||
while (i < 10)
|
while (++i < 10)
|
||||||
{
|
{
|
||||||
/* if (i!=3) //JACK SOME PB HERE on close la sortie debug en forcant le close sur tous les fd... ne risque-t-on pas autre chose ??!!! */
|
if (exec->fd_save[i] != -1)
|
||||||
/* { */
|
{
|
||||||
close(i++);
|
DG("dup2(%i, %i)", exec->fd_save[i], i);
|
||||||
/* } */
|
dup2(exec->fd_save[i], i);
|
||||||
/* else */
|
}
|
||||||
/* i++; */
|
|
||||||
}
|
}
|
||||||
if (exec->fd_save[0] != -1)
|
|
||||||
dup2(exec->fd_save[0], STDIN);
|
|
||||||
if (exec->fd_save[1] != -1)
|
|
||||||
dup2(exec->fd_save[1], STDOUT);
|
|
||||||
if (exec->fd_save[2] != -1)
|
|
||||||
dup2(exec->fd_save[2], STDERR);
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,7 @@ int job_wait(int id)
|
||||||
&& errno != ECHILD)
|
&& errno != ECHILD)
|
||||||
ft_dprintf(2, "{red}%s: waitpid error errno=%i{eoc}\n",
|
ft_dprintf(2, "{red}%s: waitpid error errno=%i{eoc}\n",
|
||||||
SHELL_NAME, errno);
|
SHELL_NAME, errno);
|
||||||
|
DG("wait trigger pid=%i", pid);
|
||||||
if (pid <= 1 || mark_process_status(pid, status)
|
if (pid <= 1 || mark_process_status(pid, status)
|
||||||
|| job_is_stopped(j)
|
|| job_is_stopped(j)
|
||||||
|| job_is_completed(j))
|
|| job_is_completed(j))
|
||||||
|
|
|
||||||
|
|
@ -19,8 +19,9 @@ int lexer_word(t_list **alst, t_lexer *lexer)
|
||||||
|
|
||||||
token = (*alst)->content;
|
token = (*alst)->content;
|
||||||
token->type = token->type ? token->type : TK_WORD;
|
token->type = token->type ? token->type : TK_WORD;
|
||||||
if ((state = get_state_global(lexer))
|
if (lexer->str[lexer->pos] != '!'
|
||||||
|| (state = get_state_redir(lexer)))
|
&& ((state = get_state_global(lexer))
|
||||||
|
|| (state = get_state_redir(lexer))))
|
||||||
lexer->state = state;
|
lexer->state = state;
|
||||||
else if (lexer->str[lexer->pos] == '='
|
else if (lexer->str[lexer->pos] == '='
|
||||||
&& word_is_assignment((char *[]){token->data, (char *)token->esc}))
|
&& word_is_assignment((char *[]){token->data, (char *)token->esc}))
|
||||||
|
|
|
||||||
|
|
@ -58,8 +58,11 @@ static int read_stdin(int *ret, int *j)
|
||||||
{
|
{
|
||||||
*j = 0;
|
*j = 0;
|
||||||
*ret = 0;
|
*ret = 0;
|
||||||
if (read(0, ret, sizeof(int)) < 0)
|
if (read(STDIN, ret, sizeof(int)) < 0)
|
||||||
|
{
|
||||||
|
DG("read problem: %s", strerror(errno));
|
||||||
return (-1);
|
return (-1);
|
||||||
|
}
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,11 +17,14 @@ extern char **environ;
|
||||||
static int localenv_init(void)
|
static int localenv_init(void)
|
||||||
{
|
{
|
||||||
t_data *data;
|
t_data *data;
|
||||||
|
char *pid;
|
||||||
|
|
||||||
|
pid = ft_itoa(getpid());
|
||||||
data = data_singleton();
|
data = data_singleton();
|
||||||
data->local_var = NULL;
|
data->local_var = NULL;
|
||||||
builtin_setenv(NULL, (char *[]){"local", "IFS", " \t\n", 0}, NULL);
|
builtin_setenv(NULL, (char *[]){"local", "IFS", " \t\n", 0}, NULL);
|
||||||
builtin_setenv(NULL, (char *[]){"local", "PS2", " >", 0}, NULL);
|
builtin_setenv(NULL, (char *[]){"local", "PS2", " >", 0}, NULL);
|
||||||
|
ft_strdel(&pid);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -43,6 +43,7 @@ static int handle_instruction(t_list **token, t_btree **ast)
|
||||||
return (ret);
|
return (ret);
|
||||||
if (do_lexer_routine(token, stream) > 0)
|
if (do_lexer_routine(token, stream) > 0)
|
||||||
continue ;
|
continue ;
|
||||||
|
/* token_print(*token); */
|
||||||
if ((ret = do_parser_routine(token, ast)) == 1
|
if ((ret = do_parser_routine(token, ast)) == 1
|
||||||
&& SH_NO_INTERACTIVE(data->opts))
|
&& SH_NO_INTERACTIVE(data->opts))
|
||||||
return (ret);
|
return (ret);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue