diff --git a/42sh/Makefile b/42sh/Makefile index 7d39d7e5..f8a561a6 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -6,7 +6,7 @@ # By: wescande +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2016/08/29 21:32:58 by wescande #+# #+# # -# Updated: 2017/03/03 18:05:32 by jhalford ### ########.fr # +# Updated: 2017/03/03 20:06:37 by ariard ### ########.fr # # # # **************************************************************************** # @@ -59,6 +59,7 @@ completion/c_sizing.c\ completion/c_terminal.c\ completion/completion.c\ exec/ast_free.c\ +exec/redir_free.c\ exec/bad_fd.c\ exec/exec_ampersand.c\ exec/exec_and_if.c\ @@ -81,6 +82,7 @@ exec/fd_is_valid.c\ exec/ft_exec.c\ exec/ft_findexec.c\ exec/launch_process.c\ +exec/mark_process_status.c\ exec/process_redirect.c\ exec/process_reset.c\ exec/process_setexec.c\ @@ -159,7 +161,6 @@ job-control/mark_job_as_running.c\ job-control/process_cmp_pid.c\ job-control/process_format.c\ job-control/process_free.c\ -job-control/process_mark_status.c\ job-control/put_job_in_background.c\ job-control/put_job_in_foreground.c\ job-control/sigchld_handler.c\ @@ -234,6 +235,7 @@ parser/add_loop.c\ parser/add_redir.c\ parser/add_sep.c\ parser/add_subshell.c\ +parser/add_var.c\ parser/aggregate_sym.c\ parser/build_tree.c\ parser/error_syntax.c\ diff --git a/42sh/file1 b/42sh/file1 deleted file mode 100644 index e69de29b..00000000 diff --git a/42sh/file2 b/42sh/file2 deleted file mode 100644 index 37e50d1b..00000000 --- a/42sh/file2 +++ /dev/null @@ -1,14 +0,0 @@ -total 576 --rwxr-xr-x 1 ariard 2016_paris 690 Feb 21 15:30 update_makefile.sh -drwxr-xr-x 13 ariard 2016_paris 442 Feb 21 15:30 src -drwxr-xr-x 28 ariard 2016_paris 952 Feb 25 18:21 sample -drwxr-xr-x 6 ariard 2016_paris 204 Feb 21 15:30 pdf -drwxr-xr-x 13 ariard 2016_paris 442 Mar 2 20:15 objs -drwxr-xr-x 12 ariard 2016_paris 408 Mar 2 20:15 libft -drwxr-xr-x 19 ariard 2016_paris 646 Mar 2 20:36 includes --rw-r--r-- 1 ariard 2016_paris 0 Mar 2 20:40 file2 --rw-r--r-- 1 ariard 2016_paris 0 Mar 2 20:40 file1 --rw-r--r-- 1 ariard 2016_paris 4396 Feb 21 15:40 donovan_segaults_06-02 --rw-r--r-- 1 ariard 2016_paris 21985 Mar 2 20:39 STDBUG --rw-r--r-- 1 ariard 2016_paris 8051 Mar 1 16:42 Makefile --rwxr-xr-x 1 ariard 2016_paris 246884 Mar 2 20:38 42sh diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index bace5d56..4784ce77 100644 --- a/42sh/includes/exec.h +++ b/42sh/includes/exec.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 20:29:56 by jhalford #+# #+# */ -/* Updated: 2017/03/03 16:39:06 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 20:07:01 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -141,6 +141,7 @@ char *ft_findexec(char *path, char *file); void set_exitstatus(int status, int override); void ast_free(void *data, size_t content_size); +void redir_free(void *data, size_t content_size); char **token_to_argv(t_ld *ld); diff --git a/42sh/includes/job_control.h b/42sh/includes/job_control.h index 0437435c..b8f04fa4 100644 --- a/42sh/includes/job_control.h +++ b/42sh/includes/job_control.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/10 16:55:09 by jhalford #+# #+# */ -/* Updated: 2017/03/03 16:38:51 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:35:49 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -58,7 +58,7 @@ void job_format_head(t_job *j); void job_update_status(void); void mark_job_as_running (t_job *j); -int process_mark_status(pid_t pid, int status); +int mark_process_status(pid_t pid, int status); int job_is_stopped(int id); int job_is_completed(int id); diff --git a/42sh/includes/parser.h b/42sh/includes/parser.h index 5005c2eb..c5257a09 100644 --- a/42sh/includes/parser.h +++ b/42sh/includes/parser.h @@ -5,7 +5,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 12:15:54 by jhalford #+# #+# */ -/* Updated: 2017/03/02 17:10:21 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:18:14 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -124,22 +124,21 @@ int add_func_cmd(t_btree **ast, t_list **lst); int add_func_sep(t_btree **ast, t_list **lst); int add_one_func(t_btree **ast, t_list **lst); int add_pipe(t_btree **ast, t_list **lst); +int add_var(t_btree **ast, t_list **lst); + int isloop(t_btree **ast, t_list **lst); int iscase(t_btree **ast, t_list **lst); int iscondition(t_btree **ast, t_list **lst); int issubshell(t_btree **ast, t_list **lst); int isfunc(t_btree **ast, t_list **lst); -int join_ast(t_btree **ast, t_btree **new_node); -int gen_node(t_btree **ast); int isdir(t_btree **ast); int iscondition(t_btree **ast, t_list **list); int isdir_sep(t_btree **ast, t_list **list); int isdir_word(t_btree **ast, t_list **list); +int isvar(t_btree **ast, t_list **list); -/* - * Build AST - * -*/ +int join_ast(t_btree **ast, t_btree **new_node); +int gen_node(t_btree **ast); union u_word { diff --git a/42sh/includes/types.h b/42sh/includes/types.h index 893007b4..29e02f83 100644 --- a/42sh/includes/types.h +++ b/42sh/includes/types.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 17:11:48 by jhalford #+# #+# */ -/* Updated: 2017/03/03 17:30:47 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 20:07:30 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/sample/ls b/42sh/sample/ls new file mode 100644 index 00000000..9e2740c6 --- /dev/null +++ b/42sh/sample/ls @@ -0,0 +1 @@ +ls diff --git a/42sh/src/addls b/42sh/src/addls new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/42sh/src/addls @@ -0,0 +1 @@ + diff --git a/42sh/src/exec/ast_free.c b/42sh/src/exec/ast_free.c index de356f4f..487cf063 100644 --- a/42sh/src/exec/ast_free.c +++ b/42sh/src/exec/ast_free.c @@ -6,34 +6,32 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/05 11:50:51 by jhalford #+# #+# */ -/* Updated: 2017/03/02 21:40:54 by ariard ### ########.fr */ +/* Updated: 2017/03/03 19:59:33 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ #include "exec.h" +void read_redir(void *data) +{ + t_redir *redir; + + redir = data; + DG("file : [%s]", redir->word.word); +} + void ast_free(void *data, size_t content_size) { t_astnode *node; - (void)node; - (void)data; - (void)content_size; - - return ; -/* (void)content_size; node = data; - if (node->type == TK_COMMAND) + if (node->type == CMD) + { ft_ld_clear(&node->data.cmd.token, &ft_tabdel); - else if (node->type == TK_SUBSHELL) - { - ft_sstrfree(node->data.sstr); - node->data.sstr = NULL; + ft_lstdel(&node->data.cmd.redir, &redir_free); } - else if (node->type == TK_LESS || node->type == TK_GREAT || node->type == TK_DGREAT) - { - ft_strdel(&node->data.cmd.redir.word.word); - } -*/ +// if (node->type == WORDLIST) +// do clear + } diff --git a/42sh/src/exec/exec_ampersand.c b/42sh/src/exec/exec_ampersand.c index d32ad177..4480fc2e 100644 --- a/42sh/src/exec/exec_ampersand.c +++ b/42sh/src/exec/exec_ampersand.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/10 16:01:30 by jhalford #+# #+# */ -/* Updated: 2017/03/03 16:05:30 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 19:35:21 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,12 +16,13 @@ int exec_ampersand(t_btree **ast) { t_exec *exec; + if (!SH_HAS_JOBC(data_singleton()->opts)) + return (exec_semi(ast)); exec = &data_singleton()->exec; push(&exec->op_stack, TK_AMP); ft_exec(&(*ast)->left); exec->attrs &= ~EXEC_BG; ft_exec(&(*ast)->right); - /* if (SH_HAS_JOBC(data_singleton()->opts)) */ /* data_singleton()->exec.job.attributes |= JOB_BG; */ /* ft_exec(&(*ast)->left); */ diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index e3133e1f..b4acaccf 100644 --- a/42sh/src/exec/exec_command.c +++ b/42sh/src/exec/exec_command.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 17:28:14 by jhalford #+# #+# */ -/* Updated: 2017/03/03 18:44:43 by wescande ### ########.fr */ +/* Updated: 2017/03/03 19:46:11 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,7 +49,6 @@ int exec_cmd(t_btree **ast) job = &data_singleton()->exec.job; process_reset(&p); op = pop(&exec->op_stack); - DG("op=%i", op); fds[PIPE_WRITE] = STDOUT; fds[PIPE_READ] = STDIN; if (op == TK_AMP) @@ -57,7 +56,9 @@ int exec_cmd(t_btree **ast) else if (op == TK_PIPE) pipe(fds); p.fdin = exec->fdin; + p.to_close = fds[PIPE_READ]; p.fdout = fds[PIPE_WRITE]; + p.redirs = cmd->redir; exec->fdin = fds[PIPE_READ]; if (IS_PIPESTART(p)) { @@ -69,7 +70,18 @@ int exec_cmd(t_btree **ast) process_setexec(&p); if (!(launch_process(&p))) ft_lstadd(&job->first_process, ft_lstnew(&p, sizeof(p))); + if (fds[PIPE_WRITE] != STDOUT) + close(fds[PIPE_WRITE]); if (IS_PIPEEND(p)) + { add_new_job(job); + if (JOB_IS_FG(job->attrs)) + put_job_in_foreground(job, 0); + else + { + job_notify_new(job); + put_job_in_background(job, 0); + } + } return (0); } diff --git a/42sh/src/exec/exec_semi.c b/42sh/src/exec/exec_semi.c index 58013f17..f664b25e 100644 --- a/42sh/src/exec/exec_semi.c +++ b/42sh/src/exec/exec_semi.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/30 20:52:05 by jhalford #+# #+# */ -/* Updated: 2017/03/03 18:01:38 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:10:34 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,11 +17,8 @@ int exec_semi(t_btree **ast) t_exec *exec; exec = &data_singleton()->exec; - DG(); push(&exec->op_stack, TK_SEMI); - DG(); ft_exec(&(*ast)->left); - DG(); exec->attrs &= ~EXEC_AOL_MASK; ft_exec(&(*ast)->right); // btree_delone(ast, &ast_free); diff --git a/42sh/src/exec/ft_exec.c b/42sh/src/exec/ft_exec.c index 5d3b8090..b4b3c578 100644 --- a/42sh/src/exec/ft_exec.c +++ b/42sh/src/exec/ft_exec.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 20:30:32 by jhalford #+# #+# */ -/* Updated: 2017/03/03 18:02:55 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 19:03:46 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ t_execmap g_execmap[] = {TK_AMP, &exec_ampersand}, {TK_AND_IF, &exec_and_if}, {TK_OR_IF, &exec_or_if}, - /* {TK_PIPE, &exec_pipe}, */ + {TK_PIPE, &exec_pipe}, {TK_WHILE, &exec_while}, {TK_IF, &exec_if}, {TK_ELIF, &exec_elif}, @@ -36,7 +36,6 @@ int ft_exec(t_btree **ast) int i; i = 0; - DG(); if (!*ast) return (0); item = (*ast)->item; diff --git a/42sh/src/exec/launch_process.c b/42sh/src/exec/launch_process.c index 76fbd434..52919e69 100644 --- a/42sh/src/exec/launch_process.c +++ b/42sh/src/exec/launch_process.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 14:20:45 by jhalford #+# #+# */ -/* Updated: 2017/03/03 16:29:12 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 19:53:26 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,11 +18,15 @@ int launch_process(t_process *p) int pid; exec = &data_singleton()->exec; + DG("gonna launch [%s]", p->av[0]); + DG("fdin=[%i]", p->fdin); + DG("fdout=[%i]", p->fdout); if (p->attributes & PROCESS_BUILTIN && IS_PIPESINGLE(*p)) { if (process_redirect(p)) return (1); set_exitstatus((*p->execf)(p->path, p->av, data_singleton()->env), 1); + return (0); } else { diff --git a/42sh/src/job-control/process_mark_status.c b/42sh/src/exec/mark_process_status.c similarity index 90% rename from 42sh/src/job-control/process_mark_status.c rename to 42sh/src/exec/mark_process_status.c index fd285c0b..32679242 100644 --- a/42sh/src/job-control/process_mark_status.c +++ b/42sh/src/exec/mark_process_status.c @@ -6,19 +6,20 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 12:41:11 by jhalford #+# #+# */ -/* Updated: 2017/02/03 15:50:29 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 19:02:54 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "job_control.h" -int process_mark_status(pid_t pid, int status) +int mark_process_status(pid_t pid, int status) { t_list *plist; t_process *p; if (pid > 1) { + DG("MPS pid=%i,s=%i", pid, status); if ((plist = job_getprocess(pid))) { p = plist->content; diff --git a/42sh/src/exec/process_redirect.c b/42sh/src/exec/process_redirect.c index d62970e6..c13cf802 100644 --- a/42sh/src/exec/process_redirect.c +++ b/42sh/src/exec/process_redirect.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/29 16:04:18 by jhalford #+# #+# */ -/* Updated: 2017/03/02 19:44:42 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:49:27 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,10 +48,10 @@ int process_redirect(t_process *p) } redirs = redirs->next; } - if (p->to_close != 0) + if (p->to_close != STDIN) close(p->to_close); if (p->fdin != STDIN) - dup2_close(p->fdout, STDOUT); + dup2_close(p->fdin, STDIN); if (p->fdout != STDOUT) dup2_close(p->fdout, STDOUT); return (0); diff --git a/42sh/src/exec/redir_free.c b/42sh/src/exec/redir_free.c new file mode 100644 index 00000000..9e0a1b06 --- /dev/null +++ b/42sh/src/exec/redir_free.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* redir_free.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/03 18:12:57 by ariard #+# #+# */ +/* Updated: 2017/03/03 20:02:24 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "exec.h" + +void redir_free(void *data, size_t content_size) +{ + t_redir *redir; + + (void)content_size; + redir = data; + if (redir->type == TK_GREAT || redir->type == TK_LESS || redir->type == TK_DGREAT) + ft_strdel(&redir->word.word); + else + redir->word.fd = 0; + redir->type = 0; + redir->n = 0; + redir->close = 1; + free(redir); +} diff --git a/42sh/src/job-control/add_new_job.c b/42sh/src/job-control/add_new_job.c index 3838a178..3e866fac 100644 --- a/42sh/src/job-control/add_new_job.c +++ b/42sh/src/job-control/add_new_job.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/02 20:44:21 by jhalford #+# #+# */ -/* Updated: 2017/03/03 18:44:42 by wescande ### ########.fr */ +/* Updated: 2017/03/03 19:33:29 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,7 @@ int add_new_job(t_job *job) { t_jobc *jobc; + DG("adding new job"); if (!job->first_process) return (1); jobc = &data_singleton()->jobc; @@ -23,12 +24,5 @@ int add_new_job(t_job *job) job->id = jobc->current_id; job->pgid = ((t_process*)job->first_process->content)->pid; ft_lstadd(&jobc->first_job, ft_lstnew(job, sizeof(*job))); - if (JOB_IS_FG(job->attrs)) - put_job_in_foreground(job, 0); - else - { - job_notify_new(job); - put_job_in_background(job, 0); - } return (0); } diff --git a/42sh/src/job-control/job_getprocess.c b/42sh/src/job-control/job_getprocess.c index a5d421c3..3ad82217 100644 --- a/42sh/src/job-control/job_getprocess.c +++ b/42sh/src/job-control/job_getprocess.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/31 15:07:30 by jhalford #+# #+# */ -/* Updated: 2017/01/31 15:07:41 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 19:42:19 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/job_remove.c b/42sh/src/job-control/job_remove.c index 9c94db11..2ffe9c8a 100644 --- a/42sh/src/job-control/job_remove.c +++ b/42sh/src/job-control/job_remove.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 12:51:08 by jhalford #+# #+# */ -/* Updated: 2017/01/31 13:44:26 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:53:32 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/job_update_id.c b/42sh/src/job-control/job_update_id.c index 1fba6f36..8b423e52 100644 --- a/42sh/src/job-control/job_update_id.c +++ b/42sh/src/job-control/job_update_id.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 13:33:08 by jhalford #+# #+# */ -/* Updated: 2017/03/02 20:59:46 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:18:29 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/job_update_status.c b/42sh/src/job-control/job_update_status.c index 1fa30cd5..5e314792 100644 --- a/42sh/src/job-control/job_update_status.c +++ b/42sh/src/job-control/job_update_status.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 12:56:11 by jhalford #+# #+# */ -/* Updated: 2017/02/03 15:50:30 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 18:56:57 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,7 @@ void job_update_status(void) int status; pid_t pid; - pid = waitpid(WAIT_ANY, &status, WUNTRACED | WNOHANG); - while (!process_mark_status(pid, status)) - pid = waitpid(WAIT_ANY, &status, WUNTRACED | WNOHANG); + do + pid = waitpid (WAIT_ANY, &status, WUNTRACED|WNOHANG); + while (!mark_process_status (pid, status)); } diff --git a/42sh/src/job-control/job_wait.c b/42sh/src/job-control/job_wait.c index 62f5ebef..38f18f8f 100644 --- a/42sh/src/job-control/job_wait.c +++ b/42sh/src/job-control/job_wait.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 11:49:05 by jhalford #+# #+# */ -/* Updated: 2017/01/31 13:44:17 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 19:42:12 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,15 +17,11 @@ int job_wait(int id) pid_t pid; int status; - if (job_is_stopped(id)) - return (0); - job_update_status(); - pid = waitpid(WAIT_ANY, &status, WUNTRACED); - while (!process_mark_status(pid, status) - && !job_is_completed(id) - && !job_is_stopped(id)) - { + DG("job wait [%i]", id); + do pid = waitpid(WAIT_ANY, &status, WUNTRACED); - } + while (!mark_process_status(pid, status) + && !job_is_stopped(id) + && !job_is_completed(id)); return (0); } diff --git a/42sh/src/job-control/put_job_in_foreground.c b/42sh/src/job-control/put_job_in_foreground.c index 636853ed..c5b75d0b 100644 --- a/42sh/src/job-control/put_job_in_foreground.c +++ b/42sh/src/job-control/put_job_in_foreground.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 14:58:36 by jhalford #+# #+# */ -/* Updated: 2017/03/03 18:44:44 by wescande ### ########.fr */ +/* Updated: 2017/03/03 19:46:03 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,20 +17,27 @@ int put_job_in_foreground(t_job *j, int cont) t_jobc *jobc; jobc = &data_singleton()->jobc; - tcsetpgrp(STDIN, j->pgid); - tcsetattr(STDIN, TCSADRAIN, &jobc->shell_tmodes); - if (cont) + if (SH_HAS_JOBC(data_singleton()->opts)) { - tcsetattr(STDIN, TCSADRAIN, &j->tmodes); - if (kill(-j->pgid, SIGCONT) < 0) - DG("kill(SIGCONT) failed"); + tcsetpgrp(STDIN, j->pgid); + if (cont) + { + tcsetattr(STDIN, TCSADRAIN, &j->tmodes); + if (kill(-j->pgid, SIGCONT) < 0) + DG("kill(SIGCONT) failed"); + } + job_wait(j->id); + job_remove(j->id); + + tcsetpgrp(STDIN, jobc->shell_pgid); + + tcgetattr(STDIN, &j->tmodes); + tcsetattr(STDIN, TCSADRAIN, &jobc->shell_tmodes); + } + else + { + job_wait(j->id); + job_remove(j->id); } - job_wait(j->id); - job_remove(j->id); - - tcsetpgrp(STDIN, jobc->shell_pgid); - - tcgetattr(STDIN, &j->tmodes); - tcsetattr(STDIN, TCSADRAIN, &jobc->shell_tmodes); return (0); } diff --git a/42sh/src/lexer/token_free.c b/42sh/src/lexer/token_free.c index 09fb728f..d88b0e1b 100644 --- a/42sh/src/lexer/token_free.c +++ b/42sh/src/lexer/token_free.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:07:30 by jhalford #+# #+# */ -/* Updated: 2017/02/20 20:55:25 by ariard ### ########.fr */ +/* Updated: 2017/03/03 18:57:46 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/data_init.c b/42sh/src/main/data_init.c index 1aa6c777..78d413f7 100644 --- a/42sh/src/main/data_init.c +++ b/42sh/src/main/data_init.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 19:26:32 by jhalford #+# #+# */ -/* Updated: 2017/03/03 17:39:30 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 19:45:05 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,7 +22,7 @@ int data_init(void) data = data_singleton(); data->env = ft_sstrdup(environ); data->comp = NULL; - data->opts = SH_OPTS_JOBC; + data->opts = 0; /* data->exec.process.path = NULL; */ /* data->exec.process.av = NULL; */ /* data->exec.process.to_close = 0; */ diff --git a/42sh/src/main/ft_putast.c b/42sh/src/main/ft_putast.c index 5c76dbe0..d2f3fd6e 100644 --- a/42sh/src/main/ft_putast.c +++ b/42sh/src/main/ft_putast.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 18:18:04 by jhalford #+# #+# */ -/* Updated: 2017/03/03 17:32:06 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 20:05:54 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 9f6b966d..fddbc046 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */ -/* Updated: 2017/03/03 18:33:40 by wescande ### ########.fr */ +/* Updated: 2017/03/03 20:05:06 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -63,14 +63,10 @@ int handle_instruction(int fd) } DG("Before execution:"); btree_print(STDBUG, ast, &ft_putast); - DG(); if (ft_exec(&ast)) return (1); - DG(); btree_del(&ast, &ast_free); - DG(); ft_add_str_in_history(lexer.str); - DG(); return (0); } @@ -97,7 +93,11 @@ int get_input_fd() return (fd); } else if ((file = shell_get_avdata())) - return (open(file, O_RDONLY)); + { + if ((fd = open(file, O_RDONLY | O_CLOEXEC)) < 0) + return (-1); + return (fd); + } else return (STDIN); } @@ -108,13 +108,13 @@ int main(int ac, char **av) setlocale(LC_ALL, ""); shell_init(ac, av); - DG("{inv}{bol}{gre}start of shell{eoc} JOBC is %s", - SH_HAS_JOBC(data_singleton()->opts)?"ON":"OFF"); if ((fd = get_input_fd()) < 0) { - ft_printf("{red}%s: No such file or directory\n{eoc}", SHELL_NAME); + ft_printf("{red}%s: %s: No such file or directory\n{eoc}", SHELL_NAME, shell_get_avdata()); return (1); } + DG("{inv}{bol}{gre}start of shell{eoc} JOBC is %s, fd=[%i]", + SH_HAS_JOBC(data_singleton()->opts)?"ON":"OFF", fd); while (handle_instruction(fd) == 0) { // lexer_clean; diff --git a/42sh/src/main/shell_get_opts.c b/42sh/src/main/shell_get_opts.c index 215f2145..98742c4b 100644 --- a/42sh/src/main/shell_get_opts.c +++ b/42sh/src/main/shell_get_opts.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/11 14:04:48 by jhalford #+# #+# */ -/* Updated: 2017/02/21 15:37:22 by ariard ### ########.fr */ +/* Updated: 2017/03/03 19:53:11 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,7 +41,10 @@ void shell_get_opts(int ac, char **av) i = 1; if (isatty(STDIN) && !av[1]) + { data_singleton()->opts |= SH_INTERACTIVE; + data_singleton()->opts |= SH_OPTS_JOBC; + } while (i < ac && av[i][0] == '-') { if (ft_strcmp(av[i], "--") == 0) diff --git a/42sh/src/main/shell_init.c b/42sh/src/main/shell_init.c index 7ad9da23..272440d7 100644 --- a/42sh/src/main/shell_init.c +++ b/42sh/src/main/shell_init.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 17:23:59 by jhalford #+# #+# */ -/* Updated: 2017/03/02 20:38:23 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 19:45:52 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/add_cmd.c b/42sh/src/parser/add_cmd.c index f0898a76..32368c49 100644 --- a/42sh/src/parser/add_cmd.c +++ b/42sh/src/parser/add_cmd.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/15 20:49:15 by ariard #+# #+# */ -/* Updated: 2017/03/03 17:33:28 by jhalford ### ########.fr */ +/* Updated: 2017/03/03 20:04:08 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,6 +26,8 @@ int add_cmd(t_btree **ast, t_list **lst) gen_node(ast); else if (isdir_word(ast, lst)) return (add_redir_word(ast, lst)); + else if (isvar(ast, lst)) + return (add_var(ast, lst)); else if (isloop(ast, lst) == 3) return (add_loop_condition(ast, lst)); else if (isloop(ast, lst)) diff --git a/42sh/src/parser/add_loop.c b/42sh/src/parser/add_loop.c index fa17904c..a26d2755 100644 --- a/42sh/src/parser/add_loop.c +++ b/42sh/src/parser/add_loop.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/17 22:17:14 by ariard #+# #+# */ -/* Updated: 2017/03/03 14:27:27 by ariard ### ########.fr */ +/* Updated: 2017/03/03 18:01:55 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/add_redir.c b/42sh/src/parser/add_redir.c index f3cc03bd..7d88688e 100644 --- a/42sh/src/parser/add_redir.c +++ b/42sh/src/parser/add_redir.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/17 16:39:05 by ariard #+# #+# */ -/* Updated: 2017/03/03 14:35:14 by ariard ### ########.fr */ +/* Updated: 2017/03/03 20:02:22 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -69,7 +69,7 @@ int add_redir_word(t_btree **ast, t_list **lst) else if (ft_stris((char *)token->data, &ft_isdigit)) redir->word.fd = ft_atoi(token->data); else - redir->word.word = token->data; + redir->word.word = ft_strdup(token->data); } return (0); } @@ -78,7 +78,7 @@ int add_redir_type(t_btree **ast, t_list **lst) { t_astnode *node; t_token *token; - t_redir *redir; + t_redir redir; DG("add redir"); if (!*ast) @@ -86,8 +86,7 @@ int add_redir_type(t_btree **ast, t_list **lst) token = (*lst)->content; node = (*ast)->item; node->type = REDIR; - redir = ft_memalloc(sizeof(redir)); - redir->type = token->type; - ft_lsteadd(&node->data.cmd.redir, ft_lstnew(redir, sizeof(redir))); + redir.type = token->type; + ft_lsteadd(&node->data.cmd.redir, ft_lstnew(&redir, sizeof(redir))); return (0); } diff --git a/42sh/src/parser/add_sep.c b/42sh/src/parser/add_sep.c index 5aa2d8f5..04a6fde0 100644 --- a/42sh/src/parser/add_sep.c +++ b/42sh/src/parser/add_sep.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/15 19:12:07 by ariard #+# #+# */ -/* Updated: 2017/03/03 14:27:40 by ariard ### ########.fr */ +/* Updated: 2017/03/03 16:05:24 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/add_var.c b/42sh/src/parser/add_var.c new file mode 100644 index 00000000..ad11bf54 --- /dev/null +++ b/42sh/src/parser/add_var.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* add_var.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/03 15:08:16 by ariard #+# #+# */ +/* Updated: 2017/03/03 16:17:27 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parser.h" + +int isvar(t_btree **ast, t_list **lst) +{ + t_astnode *node; + t_token *token; + + node = NULL; + token = (*lst)->content; + if (*ast) + { + node = (*ast)->item; + if (node->type != TK_ASSIGNEMENT_WORD && token->type == TK_ASSIGNEMENT_WORD) + return (1); + } + return (0); +} + +int add_var(t_btree **ast, t_list **lst) +{ + t_astnode *node; + t_btree *new_node; + + if (!*ast) + return (0); + new_node = NULL; + gen_node(&new_node); + join_ast(ast, &new_node); + node = (new_node)->item; + node->type = TK_SEMI; + add_cmd(&new_node, lst); + return (0); +} diff --git a/42sh/src/parser/aggregate_sym.c b/42sh/src/parser/aggregate_sym.c index 113d812c..0a47c660 100644 --- a/42sh/src/parser/aggregate_sym.c +++ b/42sh/src/parser/aggregate_sym.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 17:39:18 by ariard #+# #+# */ -/* Updated: 2017/03/03 14:27:46 by ariard ### ########.fr */ +/* Updated: 2017/03/03 16:35:04 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,6 +25,7 @@ t_aggrematch g_aggrematch[] = {TK_PAREN_OPEN, CMD_SUPERIOR, FUNC_NAME, CMD_SUPERIOR}, {TK_ASSIGNEMENT_WORD, CMD_PREFIX,CMD_PREFIX, 0}, {TK_PIPE, CMD_SUPERIOR, SIMPLE_COMMAND, CMD_SUPERIOR}, + {TK_PIPE, PIPE_SEMI_SEQUENCE, PIPE_SEMI_SEQUENCE, PIPE_SEMI_SEQUENCE}, {TK_FI, ELSE_PART, IF_CLAUSE, TK_IF}, {TK_FI, COMPOUND_LIST, IF_CLAUSE, COMPLETE_CONDITION}, {TK_FI, COMPLETE_CONDITION, IF_CLAUSE, COMPLETE_CONDITION}, @@ -306,20 +307,20 @@ int aggregate_sym(t_sym **stack, t_sym *new_sym, t_parstate *state) int i; i = 0; -// DG("aggregate head %s && sym %s", -// read_state(**stack), read_state(*new_sym)); + DG("aggregate head %s && sym %s", + read_state(**stack), read_state(*new_sym)); while (g_aggrematch[i].top) { if (*new_sym == g_aggrematch[i].top && MATCH_STACK(**stack, g_aggrematch[i].under)) { -// DG("MATCH : %s", read_state(g_aggrematch[i].new_sym)); + DG("MATCH : %s", read_state(g_aggrematch[i].new_sym)); *new_sym = g_aggrematch[i].new_sym; if (g_aggrematch[i].erase_sym) { pop_stack(stack, g_aggrematch[i].erase_sym); -// DG("stack after pop: %s", read_state(**stack)); + DG("stack after pop: %s", read_state(**stack)); } if (eval_sym(**stack, *new_sym)) return ((*state = ERROR)); diff --git a/42sh/src/parser/build_tree.c b/42sh/src/parser/build_tree.c index a2929cb6..246cd41d 100644 --- a/42sh/src/parser/build_tree.c +++ b/42sh/src/parser/build_tree.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/15 18:32:59 by ariard #+# #+# */ -/* Updated: 2017/03/03 14:35:40 by ariard ### ########.fr */ +/* Updated: 2017/03/03 15:08:55 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/eval_sym.c b/42sh/src/parser/eval_sym.c index cae28a05..8add5de6 100644 --- a/42sh/src/parser/eval_sym.c +++ b/42sh/src/parser/eval_sym.c @@ -393,6 +393,7 @@ t_stackmatch g_stackmatch[] = {TK_AMP, TERM}, {TK_PIPE, PATTERN}, {TK_PIPE, CMD_SUPERIOR}, + {TK_PIPE, PIPE_SEMI_SEQUENCE}, {PATTERN_CASE, TK_IN}, {PATTERN_CASE, CASE_LIST_NS}, {TK_PAREN_OPEN, COMPLETE_COMMANDS}, @@ -1009,7 +1010,7 @@ int eval_sym(t_sym stack, t_sym new_sym) { int i; -// DG("eval head %s && sym %s", read_state(stack), read_state(new_sym)); + DG("eval head %s && sym %s", read_state(stack), read_state(new_sym)); i = 0; while (g_stackmatch[i].top) { diff --git a/42sh/src/parser/ft_parse.c b/42sh/src/parser/ft_parse.c index ef80af07..8672cfc8 100644 --- a/42sh/src/parser/ft_parse.c +++ b/42sh/src/parser/ft_parse.c @@ -32,8 +32,8 @@ int ft_parse(t_btree **ast, t_list **token, t_parser *parser) while (*token) { produce_sym(*parser->stack, parser->new_sym, token); - //DG("new sym %s", read_state(*parser->new_sym)); - + DG("new sym %s", read_state(*parser->new_sym)); + DG("number of token to treat: %d", ft_lstsize(*token)); if (eval_sym(*parser->stack, *parser->new_sym)) return ((parser->state = ERROR)); else diff --git a/42sh/src/parser/produce_sym.c b/42sh/src/parser/produce_sym.c index c21fec05..f7e07c32 100644 --- a/42sh/src/parser/produce_sym.c +++ b/42sh/src/parser/produce_sym.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 17:58:34 by ariard #+# #+# */ -/* Updated: 2017/03/03 14:28:05 by ariard ### ########.fr */ +/* Updated: 2017/03/03 18:43:13 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -88,13 +88,11 @@ t_prodmatch g_prodmatch[] = {TK_NEWLINE, FOR_WORDLIST, NEWLINE_LIST}, {TK_NEWLINE, SEQUENTIAL_SEP, NEWLINE_LIST}, {TK_SEMI, CMD_SUPERIOR, SEPARATOR_OP}, - {TK_SEMI, CMD_SUPERIOR, SEPARATOR_OP}, - {TK_SEMI, TERM, SEPARATOR_OP}, {TK_SEMI, LIST, SEPARATOR_OP}, {TK_SEMI, PIPE_SEMI_SEQUENCE, SEPARATOR_OP}, {TK_AMP, CMD_SUPERIOR, SEPARATOR_OP}, - {TK_AMP, TERM, SEPARATOR_OP}, {TK_AMP, LIST, SEPARATOR_OP}, + {TK_AMP, PIPE_SEMI_SEQUENCE, SEPARATOR_OP}, {0, 0, 0}, }; @@ -105,8 +103,8 @@ int produce_sym(t_sym stack, t_sym *new_sym, t_list **lst) int i; token = (*lst)->content; -// DG("produce stack : %s && token : %s", read_state(stack), -// read_state(token->type)); + DG("produce stack : %s && token : %s", read_state(stack), + read_state(token->type)); i = 0; *new_sym = 0; while (g_prodmatch[i].new_sym) @@ -114,7 +112,7 @@ int produce_sym(t_sym stack, t_sym *new_sym, t_list **lst) if (token->type == g_prodmatch[i].token && stack == g_prodmatch[i].stack) { -// DG("MATCH : %s", read_state(g_prodmatch[i].new_sym)); + DG("MATCH : %s", read_state(g_prodmatch[i].new_sym)); *new_sym = g_prodmatch[i].new_sym; } i++; diff --git a/42sh/src/parser/read_stack.c b/42sh/src/parser/read_stack.c index 38e51155..03af345b 100644 --- a/42sh/src/parser/read_stack.c +++ b/42sh/src/parser/read_stack.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 15:32:10 by ariard #+# #+# */ -/* Updated: 2017/03/03 14:28:12 by ariard ### ########.fr */ +/* Updated: 2017/03/03 20:03:24 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -194,6 +194,8 @@ char *read_state(t_sym current) return ("ALL"); if (current == NEWLINE_LIST) return ("NEWLINE_LIST"); + if (current == CMD) + return ("CMD"); if (current != 0) return ("NON-DEFINED"); if (current == 0)