diff --git a/42sh/Makefile b/42sh/Makefile index 67a83bca..00876ba3 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -6,14 +6,14 @@ # By: wescande +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2016/08/29 21:32:58 by wescande #+# #+# # -# Updated: 2017/03/20 12:04:31 by wescande ### ########.fr # +# Updated: 2017/03/20 21:07:34 by jhalford ### ########.fr # # # # **************************************************************************** # NAME = 42sh CC = gcc -FLAGS = -Wall -Wextra -Werror -fvisibility=hidden +FLAGS = -Wall -Wextra -Werror -fvisibility=hidden -fsanitize=address D_FLAGS = -g DELTA = $$(echo "$$(tput cols)-47"|bc) diff --git a/42sh/README.md b/42sh/README.md new file mode 100644 index 00000000..da7ed695 --- /dev/null +++ b/42sh/README.md @@ -0,0 +1,58 @@ +# 42sh + +42sh school project. + +Mandatory part: +- Prompt without line edition. +- Builtins `cd`, `echo`, `exit`, `env`, `setenv`, `unsetenv` with there options. +- Executing simple commands with there parameters using `PATH`. +- Support for redirection `>`, `>>`, `<`and `|`. +- Logical operand `&&`and `||`. +- Separator `;`. + +Optional feature (five of theses are mandatory to validate the project): +- Inhibitors `"`, `'`and `\`. +- Advanced redirections: aggregation of file output and heredoc `<<`. +- Globbing: `*`, `?`, `[]`, `{}`, etc. +- Backquotes \`. +- Subshell with operand `()`. +- Local variable and builtin `unset` and `export`. +- History with builtin `history`and `!` with options. +- Advanced line edition. +- File descriptors and builtin `read` with options. +- Dynamical autocompletion. + +Optional feature highly appreciated: +- Job Control and builtins `job`, `fg`, `bg`and operand `&`. +- Shell Scripting. + +## shell / subshell + +## line editing + +Keys|Functions| +:-:|:-- +Opt+C
Opt+X
Opt+V|Copy
Cut
Paste +Opt+<
Opt+>|Move per words. +Opt+^
Opt+v|Move per line. +Ctrl+L|Clear screen. +Ctrl+C|Terminate/Kill current foreground process. +Ctrl+Z|Suspend/Stop current foreground process. + +## history + +Keys|Functions| +:-:|:-- +^
v|Browse the history. +Ctrl+R|Research function. +`!!`|Retype the last command. +`!n`|Retype the `n`(numerical value) command from the begin of history. +`!-n`|Retype the `-n`(numerical value) command from the last command. +`!name`|Search for a command beginning with `name`. +`!?name`|Search for a command which contain `name`. + +## autocompletion +## globbing +## hash table +## job control +## scripting diff --git a/42sh/libft/src/btree/btree_print.c b/42sh/libft/src/btree/btree_print.c index a23e21f3..bc1961dc 100644 --- a/42sh/libft/src/btree/btree_print.c +++ b/42sh/libft/src/btree/btree_print.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 18:06:24 by jhalford #+# #+# */ -/* Updated: 2017/03/18 17:33:49 by ariard ### ########.fr */ +/* Updated: 2017/03/20 21:06:28 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/libft/src/get_next_line/get_next_line.c b/42sh/libft/src/get_next_line/get_next_line.c index 4a65cb55..ba080bde 100644 --- a/42sh/libft/src/get_next_line/get_next_line.c +++ b/42sh/libft/src/get_next_line/get_next_line.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/15 13:12:06 by jhalford #+# #+# */ -/* Updated: 2017/03/14 17:47:51 by jhalford ### ########.fr */ +/* Updated: 2017/03/20 21:18:01 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,6 +43,7 @@ static int ft_loop_read(int fd, char **line, char *save) while ((ret = read(fd, buf, BUFF_SIZE)) > 0) { + DG("boucle"); buf[ret] = 0; tmp = *line; if ((pos = ft_strchr(buf, '\n'))) @@ -58,6 +59,7 @@ static int ft_loop_read(int fd, char **line, char *save) } if (ret < 0) return (-1); + DG("GNL ret %i, fd=%i", **line ? 1 : 0, fd); return (**line ? 1 : 0); } diff --git a/42sh/src/builtin/builtin_env.c b/42sh/src/builtin/builtin_env.c index 400e0022..a30da58a 100644 --- a/42sh/src/builtin/builtin_env.c +++ b/42sh/src/builtin/builtin_env.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:14:20 by jhalford #+# #+# */ -/* Updated: 2017/03/20 15:02:31 by wescande ### ########.fr */ +/* Updated: 2017/03/20 20:51:55 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -87,7 +87,7 @@ static int env_treat_flag(char ***custom_env, char *const *arg[]) return (0); } -int builtin_env(const char *path, +int builtin_env(const char *path, char *const argv[], char *const envp[]) { char **env; diff --git a/42sh/src/builtin/builtin_exit.c b/42sh/src/builtin/builtin_exit.c index 2e56a7d5..d9e01d6a 100644 --- a/42sh/src/builtin/builtin_exit.c +++ b/42sh/src/builtin/builtin_exit.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:28:41 by jhalford #+# #+# */ -/* Updated: 2017/03/20 14:17:40 by gwojda ### ########.fr */ +/* Updated: 2017/03/20 21:23:03 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_leaf.c b/42sh/src/exec/exec_leaf.c index 4e71d269..4468ef57 100644 --- a/42sh/src/exec/exec_leaf.c +++ b/42sh/src/exec/exec_leaf.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 15:47:30 by wescande #+# #+# */ -/* Updated: 2017/03/20 12:47:34 by jhalford ### ########.fr */ +/* Updated: 2017/03/20 20:41:34 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,6 +40,7 @@ int exec_leaf(t_btree **ast) p.map = g_process_map[p.type]; if (!(process_launch(&p))) { + DG("check"); job_addprocess(&p); if (IS_PIPEEND(p)) { diff --git a/42sh/src/exec/exec_reset.c b/42sh/src/exec/exec_reset.c index 161bf6a0..ed1892a3 100644 --- a/42sh/src/exec/exec_reset.c +++ b/42sh/src/exec/exec_reset.c @@ -6,16 +6,26 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/08 14:31:42 by jhalford #+# #+# */ -/* Updated: 2017/03/20 16:09:02 by gwojda ### ########.fr */ +/* Updated: 2017/03/20 20:40:43 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -static void print_error(char *std) +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) +{ + job->id = 0; + job->pgid = 0; + job->attrs = JOB_NOTIFIED; + job->first_process = NULL; + return (0); } int exec_reset(void) @@ -27,20 +37,20 @@ int exec_reset(void) jobc = &data_singleton()->jobc; if (errno != EBADF) { - if ((exec->fd_save[0] = fcntl(STDIN, F_DUPFD_CLOEXEC, 10)) == -1) - print_error("STDIN"); - if ((exec->fd_save[1] = fcntl(STDOUT, F_DUPFD_CLOEXEC, 10)) == -1) - print_error("STDOUT"); - if ((exec->fd_save[2] = fcntl(STDERR, F_DUPFD_CLOEXEC, 10)) == -1) - print_error("STDERR"); + if ((exec->fd_save[0] = fcntl(STDIN, F_DUPFD_CLOEXEC, 10)) == -1 + && 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->fdin = STDIN; exec->attrs = 0; - exec->job.id = 0; - exec->job.pgid = 0; - exec->job.attrs = JOB_NOTIFIED; - exec->job.first_process = NULL; + exec_reset_job(&exec->job); tcgetattr(STDIN, &exec->job.tmodes); jobc->first_job = NULL; jobc->current_id = 1; diff --git a/42sh/src/exec/fd_is_valid.c b/42sh/src/exec/fd_is_valid.c index 41114621..ebf60e6d 100644 --- a/42sh/src/exec/fd_is_valid.c +++ b/42sh/src/exec/fd_is_valid.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/03 13:46:40 by jhalford #+# #+# */ -/* Updated: 2017/03/15 17:48:21 by jhalford ### ########.fr */ +/* Updated: 2017/03/20 21:13:57 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/process_launch.c b/42sh/src/exec/process_launch.c index 4566bd1b..5ee8f200 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/20 18:41:25 by wescande ### ########.fr */ +/* Updated: 2017/03/20 20:52:03 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -45,7 +45,8 @@ static int do_the_fork_if_i_have_to(t_process *p) set_exitstatus(1, 1); return (0); } - return (p->map.launch(p)); + set_exitstatus(p->map.launch(p), 1); + return (0); } return (do_the_muther_forker(p)); } diff --git a/42sh/src/exec/process_resetfds.c b/42sh/src/exec/process_resetfds.c index 4045c943..aae6ee4c 100644 --- a/42sh/src/exec/process_resetfds.c +++ b/42sh/src/exec/process_resetfds.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/09 14:51:23 by jhalford #+# #+# */ -/* Updated: 2017/03/19 19:36:11 by wescande ### ########.fr */ +/* Updated: 2017/03/20 20:25:06 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,7 +29,10 @@ void process_resetfds(t_process *p) /* else */ /* i++; */ } - dup2(exec->fd_save[0], STDIN); - dup2(exec->fd_save[1], STDOUT); - dup2(exec->fd_save[2], STDERR); + 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); } diff --git a/42sh/src/exec/process_set.c b/42sh/src/exec/process_set.c index 2c12279c..29c64f87 100644 --- a/42sh/src/exec/process_set.c +++ b/42sh/src/exec/process_set.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/05 14:54:45 by jhalford #+# #+# */ -/* Updated: 2017/03/20 18:50:44 by wescande ### ########.fr */ +/* Updated: 2017/03/20 20:41:48 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -65,6 +65,7 @@ int process_set(t_process *p, t_btree *ast) p->fdin = exec->fdin; p->to_close = fds[PIPE_READ]; p->fdout = fds[PIPE_WRITE]; + p->pid = 0; exec->fdin = fds[PIPE_READ]; if (!ast) return (0); diff --git a/42sh/src/history/list_toolz.c b/42sh/src/history/list_toolz.c index 1c2008fa..71b6e9be 100644 --- a/42sh/src/history/list_toolz.c +++ b/42sh/src/history/list_toolz.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/17 11:37:47 by gwojda #+# #+# */ -/* Updated: 2017/03/20 18:16:45 by gwojda ### ########.fr */ +/* Updated: 2017/03/20 21:25:22 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,9 +16,9 @@ void free_history_list(t_list_history *head) { t_list_history *prev; - if (!head) + if (!SH_IS_INTERACTIVE(data_singleton()->opts)) return ; - if (head->next) + if (head && head->next) free(head->next); while (head) { diff --git a/42sh/src/job_control/job_addprocess.c b/42sh/src/job_control/job_addprocess.c index 4a4356d4..7a860da3 100644 --- a/42sh/src/job_control/job_addprocess.c +++ b/42sh/src/job_control/job_addprocess.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 13:54:51 by jhalford #+# #+# */ -/* Updated: 2017/03/20 14:16:48 by jhalford ### ########.fr */ +/* Updated: 2017/03/20 20:51:59 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,7 +24,7 @@ int job_addprocess(t_process *p) job_update_id(); job->id = jobc->current_id; job->pgid = SH_IS_INTERACTIVE(data_singleton()->opts) ? - p->pid : getpgid(0); + p->pid : data_singleton()->jobc.shell_pgid; ft_lstadd(&jobc->first_job, ft_lstnew(job, sizeof(*job))); } job = jobc->first_job->content; diff --git a/42sh/src/job_control/job_hup_all.c b/42sh/src/job_control/job_hup_all.c index 0a4e238c..37944153 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/20 11:37:53 by jhalford ### ########.fr */ +/* Updated: 2017/03/20 20:51:54 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,7 +23,8 @@ void job_hup_all(void) while (jlist) { job = jlist->content; - kill(-job->pgid, SIGHUP); + if (job->pgid != 1) + kill(-job->pgid, SIGHUP); jlist = jlist->next; } } diff --git a/42sh/src/job_control/job_remove.c b/42sh/src/job_control/job_remove.c index 00cbb7dc..b8ffc169 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/03/20 14:31:22 by jhalford ### ########.fr */ +/* Updated: 2017/03/20 20:19:55 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,12 +25,10 @@ void job_remove(int id) j = jlist->content; if (job_is_completed(j)) { - DG(); p = ft_lstlast(j->first_process)->content; set_exitstatus(p->status, 0); if (id < data_singleton()->jobc.current_id) data_singleton()->jobc.current_id = id; ft_lst_delif(&jobc->first_job, &id, job_cmp_id, job_free); - DG(); } } diff --git a/42sh/src/line_editing/readline.c b/42sh/src/line_editing/readline.c index cbbc9438..04cbdc62 100644 --- a/42sh/src/line_editing/readline.c +++ b/42sh/src/line_editing/readline.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 14:19:48 by gwojda #+# #+# */ -/* Updated: 2017/03/20 14:49:03 by gwojda ### ########.fr */ +/* Updated: 2017/03/20 21:19:20 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,7 +21,10 @@ int readline(int has_prompt, char **input) if (!SH_IS_INTERACTIVE(data_singleton()->opts)) { if ((ret = get_next_line(data_singleton()->fd, input)) >= 0) + { + DG("returning %i", !ret); return (!ret); + } return (ret); } readline_init(has_prompt); diff --git a/42sh/src/main/data_exit.c b/42sh/src/main/data_exit.c index d62a3ddb..51f33403 100644 --- a/42sh/src/main/data_exit.c +++ b/42sh/src/main/data_exit.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/07 18:07:50 by jhalford #+# #+# */ -/* Updated: 2017/03/20 14:44:51 by gwojda ### ########.fr */ +/* Updated: 2017/03/20 21:25:25 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index f321c414..7f326eb5 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/20 20:31:21 by ariard ### ########.fr */ +/* Updated: 2017/03/20 23:12:42 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,6 +52,7 @@ static int handle_instruction(t_list **token, t_btree **ast) set_exitstatus(1, 1); if (SH_IS_INTERACTIVE(data->opts) && data->lexer.str) ft_add_str_in_history(data->lexer.str); + /* exit(0); */ return (0); } diff --git a/42sh/src/parser/do_parser_routine.c b/42sh/src/parser/do_parser_routine.c index cf0160d2..2f9671b9 100644 --- a/42sh/src/parser/do_parser_routine.c +++ b/42sh/src/parser/do_parser_routine.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/19 14:24:14 by wescande #+# #+# */ -/* Updated: 2017/03/20 15:47:03 by ariard ### ########.fr */ +/* Updated: 2017/03/20 21:11:21 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */