diff --git a/Makefile b/Makefile index 1834a75..69233b9 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,11 @@ 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 = ${UTILS_SRC} main.c file.c infile.c outfile.c heredoc.c +SRCS = ${UTILS_SRC} main.c file.c infile.c outfile.c heredoc.c syntatics.c ft_split_quoted.c OBJS = ${SRCS:.c=.o} NAME = minishell -CC = clang +CC = gcc CFLAGS = -Wall -Werror -Wextra -g diff --git a/d b/d index 9daeafb..e69de29 100755 --- a/d +++ b/d @@ -1 +0,0 @@ -test diff --git a/file.o b/file.o index 8c378ac..5561861 100644 Binary files a/file.o and b/file.o differ diff --git a/ft_split_quoted.c b/ft_split_quoted.c new file mode 100644 index 0000000..a9335fb --- /dev/null +++ b/ft_split_quoted.c @@ -0,0 +1,75 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cchauvet + +void ft_lstdel(void *ptr) +{ + t_list *element; + t_cmd *content; + + element = (t_list *) ptr; + content = (t_cmd *) element->content; + if (content->executable != NULL) + free(content->executable); + if (content->args != NULL) + free(content->args); + free(content); + free(element); +} + +int ft_cmds_init(t_list **cmds, size_t len) +{ + t_cmd *content; + t_list *current; + size_t i; + + current = malloc(sizeof(t_list)); + if (current == NULL) + return (1); + i = 0; + while (i < len) + { + content = malloc(sizeof(t_cmd *)); + if (content == NULL) + ft_lstclear(cmds, ft_lstdel); + content->args = NULL; + content->executable = NULL; + content->fd_in = -1; + content->fd_out = -1; + current->next = malloc(sizeof(t_list *)); + if (current->next == NULL) + ft_lstclear(cmds, ft_lstdel); + current = current->next; + } +} + +int ft_cmds_prep(t_list **cmds, const char *line, int infile, int outfile) +{ + size_t i; + size_t len; + t_cmd *cmd; + t_list *current; + + len = ft_seglen_quoted(line, '|'); + if (len == 0) + return (0); + cmds = malloc(sizeof(t_list *)); + if (ft_cmds_init(cmds, ft_seglen_quoted(line, '|'))) + return (1); + cmd = (t_cmd *)(*cmds)->content; + cmd->fd_in = infile; + current = *cmds; + while (current->next != NULL) + current = current->next; + cmd = (t_cmd *) current->content; + cmd->fd_out = outfile; + return (0); +} + +int ft_cmds_fill(t_list **cmds, const char *line) +{ + //TODO + //remplir les executables; + // les args; +} t_list **ft_parse_cmd(char *line) { - char infile; - char outfile; + int infile; + int outfile; t_list **cmds; - size_t i; - (void) outfile; - (void) cmds; - (void) infile; - i = 0; - ft_outfile(line); - ft_infile(line); + cmds = NULL; + outfile = ft_outfile(line); + infile = ft_infile(line); + if (ft_syntatic_verif(line) == 1) + return (NULL); + ft_cmds_prep(cmds, line, infile, outfile); ft_printf("%s\n", line); return (NULL); } diff --git a/main.o b/main.o index 57adb25..7a6a47d 100644 Binary files a/main.o and b/main.o differ diff --git a/minishell b/minishell index aa5b96f..167b363 100755 Binary files a/minishell and b/minishell differ diff --git a/minishell.h b/minishell.h index 4e0055c..f0ef128 100644 --- a/minishell.h +++ b/minishell.h @@ -9,19 +9,22 @@ #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); +int ft_syntatic_verif(const char *str); +int ft_file_is_readable(const char *path); +int ft_file_is_writeable(const char *path); +char *ft_get_file_path(const char *infile); +int ft_infile(char *line); +int ft_outfile(char *line); +int ft_heredoc(char *stop); +size_t ft_seglen_quoted(const char *str, char c); +char **ft_split_quoted(const char *s, char c); -typedef struct cmd +typedef struct s_cmd { int fd_in; int fd_out; - char *cmd; + char *executable; char *args; -} cmd; +} t_cmd; #endif diff --git a/outfile.o b/outfile.o index 43c9022..9f922f0 100644 Binary files a/outfile.o and b/outfile.o differ diff --git a/syntatics.c b/syntatics.c new file mode 100644 index 0000000..475e244 --- /dev/null +++ b/syntatics.c @@ -0,0 +1,41 @@ +#include "libftx/libftx.h" +#include "minishell.h" + +static int ft_quote_verif(char *str) +{ + if (ft_is_in_quote(str, ft_strlen(str))) + { + ft_eprintf("minishell: Quote is note closed"); + return (1); + } + else + return (0); +} + +static int ft_multipipe(char *str) +{ + size_t i; + size_t y; + + i = 0; + while (str[i] != '\0') + { + y = 0; + while (str[i + y] == '|' && ft_is_in_quote(str, i)) + { + y++; + if (y > 1) + { + ft_eprintf("minishell: Multiple pipes is not supported\n"); + return (1); + } + } + i++; + } + return (0); +} + +int ft_syntatic_verif(char *str) +{ + return (ft_quote_verif(str) || ft_multipipe(str)); +} diff --git a/syntatics.o b/syntatics.o new file mode 100644 index 0000000..e7a1364 Binary files /dev/null and b/syntatics.o differ diff --git a/t b/t new file mode 100644 index 0000000..e69de29 diff --git a/utils/ft_getstr.o b/utils/ft_getstr.o index fa8b630..34d708b 100644 Binary files a/utils/ft_getstr.o and b/utils/ft_getstr.o differ diff --git a/utils/ft_is_in_quote.c b/utils/ft_is_in_quote.c index 3b697d1..c645ad3 100644 --- a/utils/ft_is_in_quote.c +++ b/utils/ft_is_in_quote.c @@ -1,6 +1,6 @@ #include "utils.h" -int ft_is_in_quote(char *str, size_t n) +int ft_is_in_quote(const char *str, size_t n) { size_t double_quoted; size_t simple_quoted; diff --git a/utils/ft_is_in_quote.o b/utils/ft_is_in_quote.o index 1177bf1..4552d07 100644 Binary files a/utils/ft_is_in_quote.o and b/utils/ft_is_in_quote.o differ diff --git a/utils/ft_strnchr.o b/utils/ft_strnchr.o index c1cc6b1..4acfc36 100644 Binary files a/utils/ft_strnchr.o and b/utils/ft_strnchr.o differ diff --git a/utils/ft_strncpy.o b/utils/ft_strncpy.o index f84950c..f777e2b 100644 Binary files a/utils/ft_strncpy.o and b/utils/ft_strncpy.o differ diff --git a/utils/ft_strreplace.o b/utils/ft_strreplace.o index afdaa9c..3dbc090 100644 Binary files a/utils/ft_strreplace.o and b/utils/ft_strreplace.o differ diff --git a/utils/utils.h b/utils/utils.h index 0f24105..23df973 100644 --- a/utils/utils.h +++ b/utils/utils.h @@ -3,10 +3,10 @@ # include # include "../libftx/libftx.h" -size_t ft_strncpy(char *dst, char *src, size_t n); -int ft_is_in_quote(char *str, size_t n); -char *ft_strreplace(char *str, char *fill, size_t start, size_t stop); -ssize_t ft_strnchr(char *str, char c); -char *ft_getstr(char *str, size_t n); +size_t ft_strncpy(char *dst, const char *src, size_t n); +int ft_is_in_quote(const char *str, size_t n); +char *ft_strreplace(char *str, const char *fill, size_t start, size_t stop); +ssize_t ft_strnchr(const char *str, char c); +char *ft_getstr(const char *str, size_t n); #endif