diff --git a/.file.c.swp b/.file.c.swp new file mode 100644 index 0000000..8d73039 Binary files /dev/null and b/.file.c.swp differ diff --git a/>> b/>> deleted file mode 100644 index e69de29..0000000 diff --git a/>>> b/>>> deleted file mode 100644 index e69de29..0000000 diff --git a/Makefile b/Makefile index 70ce727..1834a75 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ UTILS_SRC = utils/ft_is_in_quote.c utils/ft_strncpy.c utils/ft_strreplace.c utils/ft_strnchr.c utils/ft_getstr.c -SRCS = main.c file.c infile.c outfile.c ${UTILS_SRC} +SRCS = ${UTILS_SRC} main.c file.c infile.c outfile.c heredoc.c OBJS = ${SRCS:.c=.o} @@ -18,7 +18,7 @@ all: ${NAME} ${NAME}: ${OBJS} make -C libftx all - ${CC} ${OBJS} -o ${NAME} ${LIBS} + ${CC} ${OBJS} -o ${NAME} ${LIBS} -lreadline clean: make -C libftx clean diff --git a/d b/d new file mode 100755 index 0000000..9daeafb --- /dev/null +++ b/d @@ -0,0 +1 @@ +test diff --git a/file.c b/file.c index e567526..da9b9c8 100644 --- a/file.c +++ b/file.c @@ -47,21 +47,20 @@ char *ft_get_file_path(char *infile) size_t i; size_t n; - n = 0; - while (infile[-n] == infile[0]) + n = 1; + while (infile[-n] == infile[-1]) n++; - i = 1; + i = 0; while (infile[i] == ' ') i++; - if (infile[i] == '\0') + if (infile[i] == '\0' || infile[i] == '>' || infile[i] == '<') { ft_eprintf("minishell: syntax error near "); - if ((infile[0] == '<' && n < 3) || (infile[0] == '>' && n < 4)) - ft_eprintf("unexpected token `newline`\n"); + if ((infile[0] == '<' && n > 3) || (infile[0] == '>' && n > 2)) + ft_eprintf("unexpected token `%c`\n", infile[i - 1]); else - ft_eprintf("unexpected token `%c`\n", infile[i]); + ft_eprintf("unexpected token `newline`\n"); return (NULL); } return (ft_getstr(infile, i + 1)); - //j'aime jackie et michel } diff --git a/file.o b/file.o index 4a8dab1..8c378ac 100644 Binary files a/file.o and b/file.o differ diff --git a/heredoc.c b/heredoc.c new file mode 100644 index 0000000..df182a4 --- /dev/null +++ b/heredoc.c @@ -0,0 +1,25 @@ +#include "minishell.h" +#include + +int ft_heredoc(char *stop) +{ + int fds[2]; + char *line; + + pipe(fds); + line = readline("> "); + while (line != NULL) + { + if (ft_strcmp(line, stop) == 0) + { + free(line); + break ; + } + ft_putstr_fd(line, fds[1]); + add_history(line); + free(line); + line = readline("> "); + } + close(fds[1]); + return (fds[0]); +} diff --git a/heredoc.o b/heredoc.o new file mode 100644 index 0000000..1f428b1 Binary files /dev/null and b/heredoc.o differ diff --git a/infile.c b/infile.c index 76d3230..279fa17 100644 --- a/infile.c +++ b/infile.c @@ -3,30 +3,43 @@ static int ft_get_infile(char *line) { size_t i; + int fd; char *path; - path = NULL; + fd = 0; i = 0; while (line[i] != '\0') { if (line[i] == '<' && ft_is_in_quote(line, i) == 0) { - if (path != NULL) - free(path); - path = ft_get_file_path(line + i); - if (path == NULL || ft_file_is_readable(path) == 0) + i++; + if (fd != 0) + close(fd); + if (line[i] == '<') { - free(path); - return (-1); + i++; + path = ft_get_file_path(line + i); + if (path == NULL) + return (-1); + fd = ft_heredoc(path); } + else + { + path = ft_get_file_path(line + i); + if (path == NULL) + return (-1); + if (ft_file_is_readable(path) == 0) + { + free(path); + return (-1); + } + fd = open(path, O_RDONLY); + } + free(path); } i++; } - if (path == NULL) - return (-2); - i = open(path, O_RDONLY); - free(path); - return (i); + return (fd); } static int ft_remove_infile(char *line) @@ -39,7 +52,8 @@ static int ft_remove_infile(char *line) { if (line[i] == '<' && ft_is_in_quote(line, i) == 0) { - line[i++] = ' '; + while (line[i] == '<') + line[i++] = ' '; while (line[i] == ' ') i++; separator = ft_is_in_quote(line, i); diff --git a/infile.o b/infile.o index cc20d3e..4d01b27 100644 Binary files a/infile.o and b/infile.o differ diff --git a/libftx/libftx.a b/libftx/libftx.a index 2346697..4958b95 100644 Binary files a/libftx/libftx.a and b/libftx/libftx.a differ diff --git a/libftx/libftx.h b/libftx/libftx.h index 3f74279..7c02636 100644 --- a/libftx/libftx.h +++ b/libftx/libftx.h @@ -6,7 +6,7 @@ /* By: cchauvet # include # include +#include +#include +#include int ft_file_is_readable(char *path); int ft_file_is_writeable(char *path); char *ft_get_file_path(char *infile); int ft_infile(char *line); int ft_outfile(char *line); +int ft_heredoc(char *stop); typedef struct cmd { diff --git a/outfile.c b/outfile.c index 8939ef3..2779606 100644 --- a/outfile.c +++ b/outfile.c @@ -13,7 +13,8 @@ static int ft_get_outfile(char *line) { if (line[i] == '>' && ft_is_in_quote(line, i) == 0) { - if (line[i + 1] == '>') + i++; + if (line[i] == '>') { i++; append = 1; diff --git a/outfile.o b/outfile.o index 13f900d..43c9022 100644 Binary files a/outfile.o and b/outfile.o differ