without print

This commit is contained in:
Antoine Riard 2017-03-20 23:13:00 +01:00
commit c1c19bc52d
20 changed files with 121 additions and 42 deletions

View file

@ -6,14 +6,14 @@
# By: wescande <wescande@student.42.fr> +#+ +:+ +#+ # # By: wescande <wescande@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2016/08/29 21:32:58 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 NAME = 42sh
CC = gcc CC = gcc
FLAGS = -Wall -Wextra -Werror -fvisibility=hidden FLAGS = -Wall -Wextra -Werror -fvisibility=hidden -fsanitize=address
D_FLAGS = -g D_FLAGS = -g
DELTA = $$(echo "$$(tput cols)-47"|bc) DELTA = $$(echo "$$(tput cols)-47"|bc)

58
42sh/README.md Normal file
View file

@ -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|
:-:|:--
<kbd>Opt</kbd>+<kbd>C</kbd><br><kbd>Opt</kbd>+<kbd>X</kbd><br><kbd>Opt</kbd>+<kbd>V</kbd>|Copy<br>Cut<br>Paste
<kbd>Opt</kbd>+<kbd><</kbd><br><kbd>Opt</kbd>+<kbd>></kbd>|Move per words.
<kbd>Opt</kbd>+<kbd>^</kbd><br><kbd>Opt</kbd>+<kbd>v</kbd>|Move per line.
<kbd>Ctrl</kbd>+<kbd>L</kbd>|Clear screen.
<kbd>Ctrl</kbd>+<kbd>C</kbd>|Terminate/Kill current foreground process.
<kbd>Ctrl</kbd>+<kbd>Z</kbd>|Suspend/Stop current foreground process.
## history
Keys|Functions|
:-:|:--
<kbd>^</kbd><br><kbd>v</kbd>|Browse the history.
<kbd>Ctrl</kbd>+<kbd>R</kbd>|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

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 18:06:24 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/15 13:12:06 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) while ((ret = read(fd, buf, BUFF_SIZE)) > 0)
{ {
DG("boucle");
buf[ret] = 0; buf[ret] = 0;
tmp = *line; tmp = *line;
if ((pos = ft_strchr(buf, '\n'))) if ((pos = ft_strchr(buf, '\n')))
@ -58,6 +59,7 @@ static int ft_loop_read(int fd, char **line, char *save)
} }
if (ret < 0) if (ret < 0)
return (-1); return (-1);
DG("GNL ret %i, fd=%i", **line ? 1 : 0, fd);
return (**line ? 1 : 0); return (**line ? 1 : 0);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:14:20 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:28:41 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */ /* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/07 15:47:30 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]; p.map = g_process_map[p.type];
if (!(process_launch(&p))) if (!(process_launch(&p)))
{ {
DG("check");
job_addprocess(&p); job_addprocess(&p);
if (IS_PIPEEND(p)) if (IS_PIPEEND(p))
{ {

View file

@ -6,16 +6,26 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/08 14:31:42 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" #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", ft_dprintf(2, "{red}%s: internal fcntl %s error errno=%i{eoc}\n",
SHELL_NAME, std, errno); 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) int exec_reset(void)
@ -27,20 +37,20 @@ int exec_reset(void)
jobc = &data_singleton()->jobc; jobc = &data_singleton()->jobc;
if (errno != EBADF) if (errno != EBADF)
{ {
if ((exec->fd_save[0] = fcntl(STDIN, F_DUPFD_CLOEXEC, 10)) == -1) if ((exec->fd_save[0] = fcntl(STDIN, F_DUPFD_CLOEXEC, 10)) == -1
print_error("STDIN"); && errno != EBADF)
if ((exec->fd_save[1] = fcntl(STDOUT, F_DUPFD_CLOEXEC, 10)) == -1) return (print_error("STDIN"));
print_error("STDOUT"); if ((exec->fd_save[1] = fcntl(STDOUT, F_DUPFD_CLOEXEC, 10)) == -1
if ((exec->fd_save[2] = fcntl(STDERR, F_DUPFD_CLOEXEC, 10)) == -1) && errno != EBADF)
print_error("STDERR"); 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;
exec->job.id = 0; exec_reset_job(&exec->job);
exec->job.pgid = 0;
exec->job.attrs = JOB_NOTIFIED;
exec->job.first_process = NULL;
tcgetattr(STDIN, &exec->job.tmodes); tcgetattr(STDIN, &exec->job.tmodes);
jobc->first_job = NULL; jobc->first_job = NULL;
jobc->current_id = 1; jobc->current_id = 1;

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/03 13:46:40 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/13 22:21:19 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); set_exitstatus(1, 1);
return (0); return (0);
} }
return (p->map.launch(p)); set_exitstatus(p->map.launch(p), 1);
return (0);
} }
return (do_the_muther_forker(p)); return (do_the_muther_forker(p));
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/09 14:51:23 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 */ /* else */
/* i++; */ /* i++; */
} }
if (exec->fd_save[0] != -1)
dup2(exec->fd_save[0], STDIN); dup2(exec->fd_save[0], STDIN);
if (exec->fd_save[1] != -1)
dup2(exec->fd_save[1], STDOUT); dup2(exec->fd_save[1], STDOUT);
if (exec->fd_save[2] != -1)
dup2(exec->fd_save[2], STDERR); dup2(exec->fd_save[2], STDERR);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/05 14:54:45 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->fdin = exec->fdin;
p->to_close = fds[PIPE_READ]; p->to_close = fds[PIPE_READ];
p->fdout = fds[PIPE_WRITE]; p->fdout = fds[PIPE_WRITE];
p->pid = 0;
exec->fdin = fds[PIPE_READ]; exec->fdin = fds[PIPE_READ];
if (!ast) if (!ast)
return (0); return (0);

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/17 11:37:47 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; t_list_history *prev;
if (!head) if (!SH_IS_INTERACTIVE(data_singleton()->opts))
return ; return ;
if (head->next) if (head && head->next)
free(head->next); free(head->next);
while (head) while (head)
{ {

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/13 13:54:51 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_update_id();
job->id = jobc->current_id; job->id = jobc->current_id;
job->pgid = SH_IS_INTERACTIVE(data_singleton()->opts) ? 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))); ft_lstadd(&jobc->first_job, ft_lstnew(job, sizeof(*job)));
} }
job = jobc->first_job->content; job = jobc->first_job->content;

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/20 11:37:40 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,6 +23,7 @@ void job_hup_all(void)
while (jlist) while (jlist)
{ {
job = jlist->content; job = jlist->content;
if (job->pgid != 1)
kill(-job->pgid, SIGHUP); kill(-job->pgid, SIGHUP);
jlist = jlist->next; jlist = jlist->next;
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/15 12:51:08 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; j = jlist->content;
if (job_is_completed(j)) if (job_is_completed(j))
{ {
DG();
p = ft_lstlast(j->first_process)->content; p = ft_lstlast(j->first_process)->content;
set_exitstatus(p->status, 0); set_exitstatus(p->status, 0);
if (id < data_singleton()->jobc.current_id) if (id < data_singleton()->jobc.current_id)
data_singleton()->jobc.current_id = id; data_singleton()->jobc.current_id = id;
ft_lst_delif(&jobc->first_job, &id, job_cmp_id, job_free); ft_lst_delif(&jobc->first_job, &id, job_cmp_id, job_free);
DG();
} }
} }

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/15 14:19:48 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 (!SH_IS_INTERACTIVE(data_singleton()->opts))
{ {
if ((ret = get_next_line(data_singleton()->fd, input)) >= 0) if ((ret = get_next_line(data_singleton()->fd, input)) >= 0)
{
DG("returning %i", !ret);
return (!ret); return (!ret);
}
return (ret); return (ret);
} }
readline_init(has_prompt); readline_init(has_prompt);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/07 18:07:50 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/20 14:45:40 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); set_exitstatus(1, 1);
if (SH_IS_INTERACTIVE(data->opts) && data->lexer.str) if (SH_IS_INTERACTIVE(data->opts) && data->lexer.str)
ft_add_str_in_history(data->lexer.str); ft_add_str_in_history(data->lexer.str);
/* exit(0); */
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */ /* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/19 14:24:14 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */