From 46ccf477559ad5eb86d332b2008446af74aba88b Mon Sep 17 00:00:00 2001 From: "ariard@student.42.fr" Date: Mon, 6 Feb 2017 22:04:21 +0100 Subject: [PATCH] loop read again if script modifierd --- 42sh/a.out | Bin 0 -> 8432 bytes 42sh/file1 | 1 + 42sh/includes/minishell.h | 5 +++-- 42sh/infinite.sh | 3 +++ 42sh/sample/2lines.sh | 4 +++- 42sh/sample/infinite.sh | 1 + 42sh/src/exec/exec_while.c | 2 +- 42sh/src/exec/ft_exec.c | 2 +- 42sh/src/main/read_script.c | 24 +++++++++++++--------- 42sh/src/main/shell_script.c | 38 +++++++++++++++++++++-------------- 42sh/test.c | 12 +++++++++++ 11 files changed, 63 insertions(+), 29 deletions(-) create mode 100755 42sh/a.out create mode 100644 42sh/infinite.sh create mode 100644 42sh/sample/infinite.sh create mode 100644 42sh/test.c diff --git a/42sh/a.out b/42sh/a.out new file mode 100755 index 0000000000000000000000000000000000000000..2dc8cdd28381c33fe07cde3ab6fc6298d5e101cc GIT binary patch literal 8432 zcmeHMJ!lj`6rM|>9vaN15r3kr!H8NU5iD$!oMg!bgC-`51j3k0F5b%CUAWsLxk3m= zAt@s1v=$4?l$HU7fQ?pSX`@6`NMR8~Jil*u=5Ftzb{4`sc>Ctfd-KiAH?y!i^ZNVO zpL>LeH4CxOB!p;#uC)m9K-ee=aR{1(N;#YuaK@c$S6N#xv3mEn9&z3wQOYsr=2(47 z(B7)A6UxWf#4e@GkkU`j`z(dhh}VY+5HF$P^f8^9w1|b8L%gD2N)@YBjd=GI-foql zecd3g@N_)HlbN(Pn=T-!5pPN19aaGJ$$M3HjBl#w7u?)!mC%UyMB%lnIQ^*9VIv@A zDL3clrlp&k$p?P@daNltt~rH%BSshrpIp~XsFeMQF-!TPILw zK}GA{K+kBOb&yk-FWd1wj0*eVJQ(Acg?NkU#cF+{|Iu>MPiMO?bz>ZEDo7G1Vc6d- zJLK=Q^2v`8!+>GHFkl!k3>XFs1BL;^z`thTdUE+ga`B6mT)8lczBZg6>~X`{j=`*n z&0{c&cell|^R4YZe>OH>RVtNbXY1^Ga`B}#@nW^owS?-Sm7`^t#Av0?O+TNwfu(WH}*MLD*sa{Eb~9672nntR7tg;hBX#(}1lWJ@8&J>W8qXL=%>6+z{^^gAB%?$Gc-jX!i>^3&PO= literal 0 HcmV?d00001 diff --git a/42sh/file1 b/42sh/file1 index e69de29b..ce09b016 100644 --- a/42sh/file1 +++ b/42sh/file1 @@ -0,0 +1 @@ +/Users/ariard/Projects/42sh diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index f5831140..8095048c 100644 --- a/42sh/includes/minishell.h +++ b/42sh/includes/minishell.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */ -/* Updated: 2017/02/06 18:33:10 by ariard ### ########.fr */ +/* Updated: 2017/02/06 21:23:44 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -51,6 +51,7 @@ struct s_comp struct s_script { char *buffer; + int fd; int size; int lc; }; @@ -84,7 +85,7 @@ int shell_single_command(char *command); int read_script(char *file); int shell_script(void); -t_list **shell_get_ast(char *command); +int get_script_content(t_script *script); void ft_expand_dollar(char **av, char **env); char *ft_findexec(char *path, char *file); diff --git a/42sh/infinite.sh b/42sh/infinite.sh new file mode 100644 index 00000000..cac2d64c --- /dev/null +++ b/42sh/infinite.sh @@ -0,0 +1,3 @@ +echo hello >> infinite.sh +hello +hello diff --git a/42sh/sample/2lines.sh b/42sh/sample/2lines.sh index cf36d0bf..22372171 100644 --- a/42sh/sample/2lines.sh +++ b/42sh/sample/2lines.sh @@ -1 +1,3 @@ -ls ; pwd > file1 ; ls | cat +ls ; pwd > file1 +ls | cat +echo hello world diff --git a/42sh/sample/infinite.sh b/42sh/sample/infinite.sh new file mode 100644 index 00000000..4e3d731d --- /dev/null +++ b/42sh/sample/infinite.sh @@ -0,0 +1 @@ +echo "echo hello" >> sample/infinite.sh diff --git a/42sh/src/exec/exec_while.c b/42sh/src/exec/exec_while.c index 68fa713b..20883c67 100644 --- a/42sh/src/exec/exec_while.c +++ b/42sh/src/exec/exec_while.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/30 17:33:53 by ariard #+# #+# */ -/* Updated: 2017/02/06 18:12:10 by ariard ### ########.fr */ +/* Updated: 2017/02/06 22:03:38 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/ft_exec.c b/42sh/src/exec/ft_exec.c index a97029b3..9bf82ba0 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/02/06 20:38:07 by ariard ### ########.fr */ +/* Updated: 2017/02/06 21:03:47 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/read_script.c b/42sh/src/main/read_script.c index a8f05050..9a45defd 100644 --- a/42sh/src/main/read_script.c +++ b/42sh/src/main/read_script.c @@ -6,29 +6,34 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/21 22:49:31 by ariard #+# #+# */ -/* Updated: 2017/02/03 19:55:38 by ariard ### ########.fr */ +/* Updated: 2017/02/06 21:58:44 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -static int rs_loop(char *file, t_script *script) +int get_script_content(t_script *script) { - int fd; char *line; line = NULL; - if ((fd = open(file, O_RDONLY)) == -1) - return (0); - while ((get_next_line(fd, &line)) > 0) + while (((get_next_line(script->fd, &line))) > 0) { ft_strappend(&script->buffer, line); ft_strappend(&script->buffer, "\n"); ft_strdel(&line); script->size += ft_strlen(script->buffer); } - script->buffer[ft_strlen(script->buffer) - 1] = 0; - close(fd); + if (script->size) + script->buffer[ft_strlen(script->buffer) - 1] = 0; + return (script->size); +} + +static int rs_read(char *file, t_script *script) +{ + if ((script->fd = open(file, O_RDONLY)) == -1) + return (0); + get_script_content(script); if (script->size > 0) { data_singleton()->opts &= ~SHELL_MODE_MASK; @@ -42,6 +47,7 @@ static int rs_setup(t_script *script) ft_bzero(script, sizeof(script)); script->buffer = NULL; script->size = 0; + script->fd = -1; return (0); } @@ -54,7 +60,7 @@ int read_script(char *file) return (0); if (rs_setup(script) < 0) return (-1); - if (rs_loop(file, script) < 0) + if (rs_read(file, script) < 0) return (-1); DG("{inv}{bol}{gre}read of script{eoc} script detected"); return (1); diff --git a/42sh/src/main/shell_script.c b/42sh/src/main/shell_script.c index a04baf04..fae45eeb 100644 --- a/42sh/src/main/shell_script.c +++ b/42sh/src/main/shell_script.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/22 23:06:34 by ariard #+# #+# */ -/* Updated: 2017/02/06 20:58:54 by ariard ### ########.fr */ +/* Updated: 2017/02/06 22:03:30 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,26 +14,34 @@ int shell_script() { - t_list *token; - t_btree *ast; - t_list *list_ast; + t_list *token; + t_btree *ast; + t_list *list_ast; + t_script *script; token = NULL; ast = NULL; list_ast = NULL; - if (ft_lexer(&token, &data_singleton()->script.buffer) || !token) - return (1); - DG("after post_tokenize"); - token_print(token); - - while (token) + script = &data_singleton()->script; + while (script->size) { - if (parse(&ast, &token)) + if (ft_lexer(&token, &data_singleton()->script.buffer) || !token) return (1); - btree_print(STDBUG, ast, &ft_putast); - if (ft_exec(&ast)) - return (1); - ast = NULL; + DG("after post_tokenize"); + token_print(token); + + while (token) + { + if (parse(&ast, &token)) + return (1); + btree_print(STDBUG, ast, &ft_putast); + if (ft_exec(&ast)) + return (1); + ast = NULL; + } + script->size = 0; + get_script_content(script); } + close(script->fd); return (0); } diff --git a/42sh/test.c b/42sh/test.c new file mode 100644 index 00000000..01d83581 --- /dev/null +++ b/42sh/test.c @@ -0,0 +1,12 @@ +#include + +int main(void) +{ + int loop; + + loop = 1; + printf("hello world"); + if (loop) + main(); + return (0); +}