loop read again if script modifierd

This commit is contained in:
ariard@student.42.fr 2017-02-06 22:04:21 +01:00
parent c0977b20ff
commit 46ccf47755
11 changed files with 63 additions and 29 deletions

BIN
42sh/a.out Executable file

Binary file not shown.

View file

@ -0,0 +1 @@
/Users/ariard/Projects/42sh

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 13:07:44 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 struct s_script
{ {
char *buffer; char *buffer;
int fd;
int size; int size;
int lc; int lc;
}; };
@ -84,7 +85,7 @@ int shell_single_command(char *command);
int read_script(char *file); int read_script(char *file);
int shell_script(void); 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); void ft_expand_dollar(char **av, char **env);
char *ft_findexec(char *path, char *file); char *ft_findexec(char *path, char *file);

3
42sh/infinite.sh Normal file
View file

@ -0,0 +1,3 @@
echo hello >> infinite.sh
hello
hello

View file

@ -1 +1,3 @@
ls ; pwd > file1 ; ls | cat ls ; pwd > file1
ls | cat
echo hello world

1
42sh/sample/infinite.sh Normal file
View file

@ -0,0 +1 @@
echo "echo hello" >> sample/infinite.sh

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/30 17:33:53 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 20:30:32 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,29 +6,34 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/21 22:49:31 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" #include "minishell.h"
static int rs_loop(char *file, t_script *script) int get_script_content(t_script *script)
{ {
int fd;
char *line; char *line;
line = NULL; line = NULL;
if ((fd = open(file, O_RDONLY)) == -1) while (((get_next_line(script->fd, &line))) > 0)
return (0);
while ((get_next_line(fd, &line)) > 0)
{ {
ft_strappend(&script->buffer, line); ft_strappend(&script->buffer, line);
ft_strappend(&script->buffer, "\n"); ft_strappend(&script->buffer, "\n");
ft_strdel(&line); ft_strdel(&line);
script->size += ft_strlen(script->buffer); script->size += ft_strlen(script->buffer);
} }
if (script->size)
script->buffer[ft_strlen(script->buffer) - 1] = 0; script->buffer[ft_strlen(script->buffer) - 1] = 0;
close(fd); 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) if (script->size > 0)
{ {
data_singleton()->opts &= ~SHELL_MODE_MASK; data_singleton()->opts &= ~SHELL_MODE_MASK;
@ -42,6 +47,7 @@ static int rs_setup(t_script *script)
ft_bzero(script, sizeof(script)); ft_bzero(script, sizeof(script));
script->buffer = NULL; script->buffer = NULL;
script->size = 0; script->size = 0;
script->fd = -1;
return (0); return (0);
} }
@ -54,7 +60,7 @@ int read_script(char *file)
return (0); return (0);
if (rs_setup(script) < 0) if (rs_setup(script) < 0)
return (-1); return (-1);
if (rs_loop(file, script) < 0) if (rs_read(file, script) < 0)
return (-1); return (-1);
DG("{inv}{bol}{gre}read of script{eoc} script detected"); DG("{inv}{bol}{gre}read of script{eoc} script detected");
return (1); return (1);

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/22 23:06:34 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,10 +17,14 @@ int shell_script()
t_list *token; t_list *token;
t_btree *ast; t_btree *ast;
t_list *list_ast; t_list *list_ast;
t_script *script;
token = NULL; token = NULL;
ast = NULL; ast = NULL;
list_ast = NULL; list_ast = NULL;
script = &data_singleton()->script;
while (script->size)
{
if (ft_lexer(&token, &data_singleton()->script.buffer) || !token) if (ft_lexer(&token, &data_singleton()->script.buffer) || !token)
return (1); return (1);
DG("after post_tokenize"); DG("after post_tokenize");
@ -35,5 +39,9 @@ int shell_script()
return (1); return (1);
ast = NULL; ast = NULL;
} }
script->size = 0;
get_script_content(script);
}
close(script->fd);
return (0); return (0);
} }

12
42sh/test.c Normal file
View file

@ -0,0 +1,12 @@
#include <stdio.h>
int main(void)
{
int loop;
loop = 1;
printf("hello world");
if (loop)
main();
return (0);
}