diff --git a/.heredoc.c.swp b/.heredoc.c.swp new file mode 100644 index 0000000..476a495 Binary files /dev/null and b/.heredoc.c.swp differ diff --git a/.infile.c.swp b/.infile.c.swp new file mode 100644 index 0000000..c2bfbc1 Binary files /dev/null and b/.infile.c.swp differ diff --git a/.main.c.swp b/.main.c.swp deleted file mode 100644 index 35e105d..0000000 Binary files a/.main.c.swp and /dev/null differ diff --git a/Makefile b/Makefile index d33b5a0..1db8453 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 utils/ft_printn.c -SRCS = ${UTILS_SRC} main.c file.c infile.c outfile.c heredoc.c syntatics.c ft_split_quoted.c env.c execution.c +UTILS_SRC = utils/ft_is_in_quote.c utils/ft_strncpy.c utils/ft_strreplace.c utils/ft_strnchr.c utils/ft_split_quoted.c utils/ft_strshift.c utils/ft_quote_remover.c +SRCS = ${UTILS_SRC} main.c file.c infile.c outfile.c heredoc.c syntatics.c cmd.c cmds.c env.c execution.c spacer.c OBJS = ${SRCS:.c=.o} diff --git a/bozo b/bozo new file mode 100644 index 0000000..89da72d --- /dev/null +++ b/bozo @@ -0,0 +1,5 @@ + +d +d +d +test diff --git a/bozobozo b/bozobozo new file mode 100644 index 0000000..e69de29 diff --git a/cmd.c b/cmd.c new file mode 100644 index 0000000..e6581ef --- /dev/null +++ b/cmd.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cmd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cchauvet args != NULL) + ft_freer_tab_ultimate(1, content->args); + if (content->executable != NULL) + free(content->executable); + free(content); +} + +int ft_cmd_filler(t_list *element, char **args) +{ + t_cmd *content; + size_t i; + + if (args == NULL) + return (1); + content = (t_cmd *)element->content; + i = 0; + while (args[i] != NULL) + { + ft_quote_remover(args[i]); + i++; + } + content->args = args; + content->executable = args[0]; + return (0); +} diff --git a/cmds.c b/cmds.c new file mode 100644 index 0000000..65ee907 --- /dev/null +++ b/cmds.c @@ -0,0 +1,121 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cmds.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cchauvet args = NULL; + content->executable = NULL; + content->fd_in = -1; + content->fd_out = -1; + current->content = content; + if (!((i + 1) < len)) + { + current->next = NULL; + return (0); + } + current->next = malloc(sizeof(t_list)); + if (current->next == NULL) + ft_lstclear(cmds, ft_cmddel); + current = current->next; + i++; + } + return (0); +} + +int ft_cmds_prep(t_list **cmds, const char *line, int infile, int outfile) +{ + size_t len; + t_cmd *cmd; + t_list *current; + + len = ft_seglen_quoted(line, '|'); + if (len == 0) + return (0); + if (ft_cmds_init(cmds, ft_seglen_quoted(line, '|'))) + { + free(cmds); + 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) +{ + char **tab; + char **args; + t_list *current; + size_t i; + + tab = ft_split_quoted(line, '|'); + if (tab == NULL) + return (1); + i = 0; + current = *cmds; + while (tab[i] != NULL) + { + args = ft_split_quoted(tab[i], ' '); + if (ft_cmd_filler(current, args) == 1) + { + ft_lstclear(cmds, ft_cmddel); + ft_freer_tab_ultimate(2, args, tab); + return (1); + } + current = current->next; + i++; + } + ft_freer_tab_ultimate(1, tab); + return (0); +} + +t_list **ft_parse_cmds(char *line) +{ + int infile; + int outfile; + t_list **cmds; + + cmds = malloc(sizeof(t_list *)); + outfile = ft_outfile(line); + infile = ft_infile(line); + if (infile == -2) + return (NULL); + if (ft_syntatic_verif(line) == 1) + return (NULL); + if (ft_cmds_prep(cmds, line, infile, outfile) == 1) + return (NULL); + if (ft_cmds_fill(cmds, line) == 1) + return (NULL); + return (cmds); +} diff --git a/env.c b/env.c index 9b79576..ee5bb27 100644 --- a/env.c +++ b/env.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/02/02 14:39:56 by erey-bet #+# #+# */ -/* Updated: 2023/02/14 15:21:28 by cchauvet ### ########.fr */ +/* Updated: 2023/02/15 19:58:19 by cchauvet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -113,7 +113,7 @@ char *get_value_by_key(char *key, t_list **head) return (NULL); } -int set_value_by_key(t_list **env, char *key, char *value) +int set_value_key(t_list **env, char *key, char *value) { t_list *current; char *temp; diff --git a/env.o b/env.o deleted file mode 100644 index c59381d..0000000 Binary files a/env.o and /dev/null differ diff --git a/execution.c b/execution.c index eb6a2ed..6516a74 100644 --- a/execution.c +++ b/execution.c @@ -26,7 +26,14 @@ static char *ft_get_executable_path(char *executable_name, char **env) path = NULL; if (executable_name[0] == '.' || executable_name[0] == '/') + { path = ft_strdup(executable_name); + if (path == NULL) + { + ft_eprintf("minishell: malloc failed\n"); + return (NULL); + } + } else { tab = ft_split(ft_get_variable(env, "PATH"), ':'); @@ -64,6 +71,8 @@ static int ft_excutor(t_cmd *cmd, char **env) int pid; int return_value; + if (cmd->fd_in == -1 || cmd->fd_out == -1) + return (1); pid = fork(); if (pid == -1) return (1); diff --git a/execution.o b/execution.o deleted file mode 100644 index 6a78a99..0000000 Binary files a/execution.o and /dev/null differ diff --git a/file.c b/file.c index d03757d..d2b5d31 100644 --- a/file.c +++ b/file.c @@ -1,4 +1,17 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* file.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cchauvet int ft_file_is_readable(const char *path) { @@ -9,58 +22,58 @@ int ft_file_is_readable(const char *path) if (fd == -1) { ft_eprintf("minishell: %s: No such file or directory\n", path); - return (0); + return (-1); } readable = read(fd, "", 0); if (readable == -1) { ft_eprintf("minishell: %s: Permission denied\n", path); - return (0); + return (-1); } - close(fd); - return (1); + return (fd); } -int ft_file_is_writeable(const char *path) +int ft_file_is_writable(const char *path) { int writeable; int fd; - fd = open(path, O_WRONLY | O_CREAT, 0644); + fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644); if (fd == -1) { ft_eprintf("minishell: %s: Permission denied\n", path); - return (0); + return (-1); } writeable = write(fd, "", 0); if (writeable == -1) { ft_eprintf("minishell: %s: Permission denied\n", path); - return (0); + return (-1); } - close(fd); - return (1); + return (fd); } -char *ft_get_file_path(const char *infile) +int ft_file_is_appendable(const char *path) { - size_t i; - size_t n; + int writeable; + int fd; - n = 1; - while (infile[-n] == infile[-1]) - n++; - i = 0; - while (infile[i] == ' ') - i++; - if (infile[i] == '\0' || infile[i] == '>' || infile[i] == '<') + fd = open(path, O_WRONLY | O_CREAT | O_APPEND, 0644); + if (fd == -1) { - ft_eprintf("minishell: syntax error near "); - if ((infile[0] == '<' && n > 3) || (infile[0] == '>' && n > 2)) - ft_eprintf("unexpected token `%c`\n", infile[i - 1]); - else - ft_eprintf("unexpected token `newline`\n"); - return (NULL); + ft_eprintf("minishell: %s: Permission denied\n", path); + return (-1); } - return (ft_getstr(infile, i + 1)); + writeable = write(fd, "", 0); + if (writeable == -1) + { + ft_eprintf("minishell: %s: Permission denied\n", path); + return (-1); + } + return (fd); +} + +int ft_file_is_executable(const char *path) +{ + return (access(path, X_OK) == 0); } diff --git a/file.o b/file.o deleted file mode 100644 index fdeafb9..0000000 Binary files a/file.o and /dev/null differ diff --git a/ft_split_quoted.o b/ft_split_quoted.o deleted file mode 100644 index 377f37d..0000000 Binary files a/ft_split_quoted.o and /dev/null differ diff --git a/heredoc.o b/heredoc.o deleted file mode 100644 index 985bf76..0000000 Binary files a/heredoc.o and /dev/null differ diff --git a/infile.c b/infile.c index 279fa17..95b922a 100644 --- a/infile.c +++ b/infile.c @@ -1,76 +1,95 @@ -#include "minishell.h" +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* infile.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cchauvet 0) - close(fd); - return (-1); - } + if (fd == -2) + return (-2); + ft_remove_infile(line); return (fd); } diff --git a/infile.o b/infile.o deleted file mode 100644 index 939685e..0000000 Binary files a/infile.o and /dev/null differ diff --git a/libftx/extra/extra.a b/libftx/extra/extra.a deleted file mode 100644 index b05f88c..0000000 Binary files a/libftx/extra/extra.a and /dev/null differ diff --git a/libftx/extra/ft_contain_only.o b/libftx/extra/ft_contain_only.o deleted file mode 100644 index 4b885eb..0000000 Binary files a/libftx/extra/ft_contain_only.o and /dev/null differ diff --git a/libftx/extra/ft_freer.o b/libftx/extra/ft_freer.o deleted file mode 100644 index cf50d16..0000000 Binary files a/libftx/extra/ft_freer.o and /dev/null differ diff --git a/libftx/extra/ft_is_in.o b/libftx/extra/ft_is_in.o deleted file mode 100644 index 9ac5767..0000000 Binary files a/libftx/extra/ft_is_in.o and /dev/null differ diff --git a/libftx/extra/ft_random_generator.o b/libftx/extra/ft_random_generator.o deleted file mode 100644 index a64dbb0..0000000 Binary files a/libftx/extra/ft_random_generator.o and /dev/null differ diff --git a/libftx/extra/ft_strchri.o b/libftx/extra/ft_strchri.o deleted file mode 100644 index 230ddca..0000000 Binary files a/libftx/extra/ft_strchri.o and /dev/null differ diff --git a/libftx/extra/ft_strcmp.o b/libftx/extra/ft_strcmp.o deleted file mode 100644 index 0ed8391..0000000 Binary files a/libftx/extra/ft_strcmp.o and /dev/null differ diff --git a/libftx/extra/ft_strfjoin.o b/libftx/extra/ft_strfjoin.o deleted file mode 100644 index 1a4743d..0000000 Binary files a/libftx/extra/ft_strfjoin.o and /dev/null differ diff --git a/libftx/extra/ft_strgen.o b/libftx/extra/ft_strgen.o deleted file mode 100644 index 261ec45..0000000 Binary files a/libftx/extra/ft_strgen.o and /dev/null differ diff --git a/libftx/extra/ft_strmerger.o b/libftx/extra/ft_strmerger.o deleted file mode 100644 index 3526a30..0000000 Binary files a/libftx/extra/ft_strmerger.o and /dev/null differ diff --git a/libftx/extra/ft_strndup.o b/libftx/extra/ft_strndup.o deleted file mode 100644 index e773a90..0000000 Binary files a/libftx/extra/ft_strndup.o and /dev/null differ diff --git a/libftx/extra/ft_swap.o b/libftx/extra/ft_swap.o deleted file mode 100644 index bc573bd..0000000 Binary files a/libftx/extra/ft_swap.o and /dev/null differ diff --git a/libftx/extra/ft_tabrealloc.o b/libftx/extra/ft_tabrealloc.o deleted file mode 100644 index 973478b..0000000 Binary files a/libftx/extra/ft_tabrealloc.o and /dev/null differ diff --git a/libftx/extra/ft_ultoa_base.o b/libftx/extra/ft_ultoa_base.o deleted file mode 100644 index 155c917..0000000 Binary files a/libftx/extra/ft_ultoa_base.o and /dev/null differ diff --git a/libftx/gnl/get_next_line.a b/libftx/gnl/get_next_line.a deleted file mode 100644 index 7d3ebe9..0000000 Binary files a/libftx/gnl/get_next_line.a and /dev/null differ diff --git a/libftx/gnl/get_next_line.o b/libftx/gnl/get_next_line.o deleted file mode 100644 index 46741e0..0000000 Binary files a/libftx/gnl/get_next_line.o and /dev/null differ diff --git a/libftx/libft/ft_atoi.o b/libftx/libft/ft_atoi.o deleted file mode 100644 index 749693b..0000000 Binary files a/libftx/libft/ft_atoi.o and /dev/null differ diff --git a/libftx/libft/ft_bzero.o b/libftx/libft/ft_bzero.o deleted file mode 100644 index 77fec06..0000000 Binary files a/libftx/libft/ft_bzero.o and /dev/null differ diff --git a/libftx/libft/ft_calloc.o b/libftx/libft/ft_calloc.o deleted file mode 100644 index a17b032..0000000 Binary files a/libftx/libft/ft_calloc.o and /dev/null differ diff --git a/libftx/libft/ft_isalnum.o b/libftx/libft/ft_isalnum.o deleted file mode 100644 index 717e49b..0000000 Binary files a/libftx/libft/ft_isalnum.o and /dev/null differ diff --git a/libftx/libft/ft_isalpha.o b/libftx/libft/ft_isalpha.o deleted file mode 100644 index fbdbbb4..0000000 Binary files a/libftx/libft/ft_isalpha.o and /dev/null differ diff --git a/libftx/libft/ft_isascii.o b/libftx/libft/ft_isascii.o deleted file mode 100644 index 67137be..0000000 Binary files a/libftx/libft/ft_isascii.o and /dev/null differ diff --git a/libftx/libft/ft_isdigit.o b/libftx/libft/ft_isdigit.o deleted file mode 100644 index 6a91375..0000000 Binary files a/libftx/libft/ft_isdigit.o and /dev/null differ diff --git a/libftx/libft/ft_isprint.o b/libftx/libft/ft_isprint.o deleted file mode 100644 index bde8775..0000000 Binary files a/libftx/libft/ft_isprint.o and /dev/null differ diff --git a/libftx/libft/ft_itoa.o b/libftx/libft/ft_itoa.o deleted file mode 100644 index c75cfcb..0000000 Binary files a/libftx/libft/ft_itoa.o and /dev/null differ diff --git a/libftx/libft/ft_lstadd_back.o b/libftx/libft/ft_lstadd_back.o deleted file mode 100644 index 1cf3242..0000000 Binary files a/libftx/libft/ft_lstadd_back.o and /dev/null differ diff --git a/libftx/libft/ft_lstadd_front.o b/libftx/libft/ft_lstadd_front.o deleted file mode 100644 index 8f727c5..0000000 Binary files a/libftx/libft/ft_lstadd_front.o and /dev/null differ diff --git a/libftx/libft/ft_lstclear.o b/libftx/libft/ft_lstclear.o deleted file mode 100644 index 236b952..0000000 Binary files a/libftx/libft/ft_lstclear.o and /dev/null differ diff --git a/libftx/libft/ft_lstdelone.o b/libftx/libft/ft_lstdelone.o deleted file mode 100644 index cd26ada..0000000 Binary files a/libftx/libft/ft_lstdelone.o and /dev/null differ diff --git a/libftx/libft/ft_lstiter.o b/libftx/libft/ft_lstiter.o deleted file mode 100644 index 266717a..0000000 Binary files a/libftx/libft/ft_lstiter.o and /dev/null differ diff --git a/libftx/libft/ft_lstlast.o b/libftx/libft/ft_lstlast.o deleted file mode 100644 index 3d210a2..0000000 Binary files a/libftx/libft/ft_lstlast.o and /dev/null differ diff --git a/libftx/libft/ft_lstmap.o b/libftx/libft/ft_lstmap.o deleted file mode 100644 index 3122efb..0000000 Binary files a/libftx/libft/ft_lstmap.o and /dev/null differ diff --git a/libftx/libft/ft_lstnew.o b/libftx/libft/ft_lstnew.o deleted file mode 100644 index 825ded9..0000000 Binary files a/libftx/libft/ft_lstnew.o and /dev/null differ diff --git a/libftx/libft/ft_lstsize.o b/libftx/libft/ft_lstsize.o deleted file mode 100644 index dc114d8..0000000 Binary files a/libftx/libft/ft_lstsize.o and /dev/null differ diff --git a/libftx/libft/ft_memchr.o b/libftx/libft/ft_memchr.o deleted file mode 100644 index f1c3326..0000000 Binary files a/libftx/libft/ft_memchr.o and /dev/null differ diff --git a/libftx/libft/ft_memcmp.o b/libftx/libft/ft_memcmp.o deleted file mode 100644 index 6b0da03..0000000 Binary files a/libftx/libft/ft_memcmp.o and /dev/null differ diff --git a/libftx/libft/ft_memcpy.o b/libftx/libft/ft_memcpy.o deleted file mode 100644 index 98e41b8..0000000 Binary files a/libftx/libft/ft_memcpy.o and /dev/null differ diff --git a/libftx/libft/ft_memmove.o b/libftx/libft/ft_memmove.o deleted file mode 100644 index 71f160b..0000000 Binary files a/libftx/libft/ft_memmove.o and /dev/null differ diff --git a/libftx/libft/ft_memset.o b/libftx/libft/ft_memset.o deleted file mode 100644 index 2737808..0000000 Binary files a/libftx/libft/ft_memset.o and /dev/null differ diff --git a/libftx/libft/ft_putchar_fd.o b/libftx/libft/ft_putchar_fd.o deleted file mode 100644 index 58df18e..0000000 Binary files a/libftx/libft/ft_putchar_fd.o and /dev/null differ diff --git a/libftx/libft/ft_putendl_fd.o b/libftx/libft/ft_putendl_fd.o deleted file mode 100644 index 50cfa1e..0000000 Binary files a/libftx/libft/ft_putendl_fd.o and /dev/null differ diff --git a/libftx/libft/ft_putnbr_fd.o b/libftx/libft/ft_putnbr_fd.o deleted file mode 100644 index 17d4db5..0000000 Binary files a/libftx/libft/ft_putnbr_fd.o and /dev/null differ diff --git a/libftx/libft/ft_putstr_fd.o b/libftx/libft/ft_putstr_fd.o deleted file mode 100644 index f795861..0000000 Binary files a/libftx/libft/ft_putstr_fd.o and /dev/null differ diff --git a/libftx/libft/ft_split.o b/libftx/libft/ft_split.o deleted file mode 100644 index dbc3910..0000000 Binary files a/libftx/libft/ft_split.o and /dev/null differ diff --git a/libftx/libft/ft_strchr.o b/libftx/libft/ft_strchr.o deleted file mode 100644 index dcad448..0000000 Binary files a/libftx/libft/ft_strchr.o and /dev/null differ diff --git a/libftx/libft/ft_strdup.o b/libftx/libft/ft_strdup.o deleted file mode 100644 index 82b9840..0000000 Binary files a/libftx/libft/ft_strdup.o and /dev/null differ diff --git a/libftx/libft/ft_striteri.o b/libftx/libft/ft_striteri.o deleted file mode 100644 index 8ecd945..0000000 Binary files a/libftx/libft/ft_striteri.o and /dev/null differ diff --git a/libftx/libft/ft_strjoin.o b/libftx/libft/ft_strjoin.o deleted file mode 100644 index ec8e4ff..0000000 Binary files a/libftx/libft/ft_strjoin.o and /dev/null differ diff --git a/libftx/libft/ft_strlcat.o b/libftx/libft/ft_strlcat.o deleted file mode 100644 index 01a3aa5..0000000 Binary files a/libftx/libft/ft_strlcat.o and /dev/null differ diff --git a/libftx/libft/ft_strlcpy.o b/libftx/libft/ft_strlcpy.o deleted file mode 100644 index f3e39eb..0000000 Binary files a/libftx/libft/ft_strlcpy.o and /dev/null differ diff --git a/libftx/libft/ft_strlen.o b/libftx/libft/ft_strlen.o deleted file mode 100644 index 3c92240..0000000 Binary files a/libftx/libft/ft_strlen.o and /dev/null differ diff --git a/libftx/libft/ft_strmapi.o b/libftx/libft/ft_strmapi.o deleted file mode 100644 index c36b41b..0000000 Binary files a/libftx/libft/ft_strmapi.o and /dev/null differ diff --git a/libftx/libft/ft_strncmp.o b/libftx/libft/ft_strncmp.o deleted file mode 100644 index 5af8cfb..0000000 Binary files a/libftx/libft/ft_strncmp.o and /dev/null differ diff --git a/libftx/libft/ft_strnstr.o b/libftx/libft/ft_strnstr.o deleted file mode 100644 index 80ab0e1..0000000 Binary files a/libftx/libft/ft_strnstr.o and /dev/null differ diff --git a/libftx/libft/ft_strrchr.o b/libftx/libft/ft_strrchr.o deleted file mode 100644 index 4b6be03..0000000 Binary files a/libftx/libft/ft_strrchr.o and /dev/null differ diff --git a/libftx/libft/ft_strtrim.o b/libftx/libft/ft_strtrim.o deleted file mode 100644 index b08e9b2..0000000 Binary files a/libftx/libft/ft_strtrim.o and /dev/null differ diff --git a/libftx/libft/ft_substr.o b/libftx/libft/ft_substr.o deleted file mode 100644 index 982ffda..0000000 Binary files a/libftx/libft/ft_substr.o and /dev/null differ diff --git a/libftx/libft/ft_tolower.o b/libftx/libft/ft_tolower.o deleted file mode 100644 index a1107d9..0000000 Binary files a/libftx/libft/ft_tolower.o and /dev/null differ diff --git a/libftx/libft/ft_toupper.o b/libftx/libft/ft_toupper.o deleted file mode 100644 index a7c9dbf..0000000 Binary files a/libftx/libft/ft_toupper.o and /dev/null differ diff --git a/libftx/libft/libft.a b/libftx/libft/libft.a deleted file mode 100644 index c0094b0..0000000 Binary files a/libftx/libft/libft.a and /dev/null differ diff --git a/libftx/libftx.a b/libftx/libftx.a deleted file mode 100644 index 0afbcc0..0000000 Binary files a/libftx/libftx.a and /dev/null differ diff --git a/libftx/printf/ft_dprintX.o b/libftx/printf/ft_dprintX.o deleted file mode 100644 index f17c096..0000000 Binary files a/libftx/printf/ft_dprintX.o and /dev/null differ diff --git a/libftx/printf/ft_dprintarg.o b/libftx/printf/ft_dprintarg.o deleted file mode 100644 index 2bbcb2d..0000000 Binary files a/libftx/printf/ft_dprintarg.o and /dev/null differ diff --git a/libftx/printf/ft_dprintflag.o b/libftx/printf/ft_dprintflag.o deleted file mode 100644 index 920afb5..0000000 Binary files a/libftx/printf/ft_dprintflag.o and /dev/null differ diff --git a/libftx/printf/ft_dprintl_base.o b/libftx/printf/ft_dprintl_base.o deleted file mode 100644 index 0f75c8d..0000000 Binary files a/libftx/printf/ft_dprintl_base.o and /dev/null differ diff --git a/libftx/printf/ft_dprintptr.o b/libftx/printf/ft_dprintptr.o deleted file mode 100644 index 5b13694..0000000 Binary files a/libftx/printf/ft_dprintptr.o and /dev/null differ diff --git a/libftx/printf/ft_dprintstrtab.o b/libftx/printf/ft_dprintstrtab.o deleted file mode 100644 index b2635fb..0000000 Binary files a/libftx/printf/ft_dprintstrtab.o and /dev/null differ diff --git a/libftx/printf/ft_dprintul.o b/libftx/printf/ft_dprintul.o deleted file mode 100644 index 85d8866..0000000 Binary files a/libftx/printf/ft_dprintul.o and /dev/null differ diff --git a/libftx/printf/ft_dprintul_base.o b/libftx/printf/ft_dprintul_base.o deleted file mode 100644 index ca970dc..0000000 Binary files a/libftx/printf/ft_dprintul_base.o and /dev/null differ diff --git a/libftx/printf/ft_dprintx.o b/libftx/printf/ft_dprintx.o deleted file mode 100644 index 6bcdbb9..0000000 Binary files a/libftx/printf/ft_dprintx.o and /dev/null differ diff --git a/libftx/printf/ft_eprintf.o b/libftx/printf/ft_eprintf.o deleted file mode 100644 index 2b649ef..0000000 Binary files a/libftx/printf/ft_eprintf.o and /dev/null differ diff --git a/libftx/printf/ft_isarg.o b/libftx/printf/ft_isarg.o deleted file mode 100644 index 28e9b52..0000000 Binary files a/libftx/printf/ft_isarg.o and /dev/null differ diff --git a/libftx/printf/ft_isdigit.o b/libftx/printf/ft_isdigit.o deleted file mode 100644 index e6094e7..0000000 Binary files a/libftx/printf/ft_isdigit.o and /dev/null differ diff --git a/libftx/printf/ft_printf.a b/libftx/printf/ft_printf.a deleted file mode 100644 index cbf2819..0000000 Binary files a/libftx/printf/ft_printf.a and /dev/null differ diff --git a/libftx/printf/ft_printf.o b/libftx/printf/ft_printf.o deleted file mode 100644 index 0a01be8..0000000 Binary files a/libftx/printf/ft_printf.o and /dev/null differ diff --git a/libftx/printf/ft_putchar_fd.o b/libftx/printf/ft_putchar_fd.o deleted file mode 100644 index 5554463..0000000 Binary files a/libftx/printf/ft_putchar_fd.o and /dev/null differ diff --git a/libftx/printf/ft_putstr_fd.o b/libftx/printf/ft_putstr_fd.o deleted file mode 100644 index 918d6ac..0000000 Binary files a/libftx/printf/ft_putstr_fd.o and /dev/null differ diff --git a/libftx/printf/ft_skipflag.o b/libftx/printf/ft_skipflag.o deleted file mode 100644 index 6af9017..0000000 Binary files a/libftx/printf/ft_skipflag.o and /dev/null differ diff --git a/libftx/printf/ft_strlen.o b/libftx/printf/ft_strlen.o deleted file mode 100644 index f576670..0000000 Binary files a/libftx/printf/ft_strlen.o and /dev/null differ diff --git a/libftx/printf/ft_vdprintf.o b/libftx/printf/ft_vdprintf.o deleted file mode 100644 index b75be4f..0000000 Binary files a/libftx/printf/ft_vdprintf.o and /dev/null differ diff --git a/main.c b/main.c index c6837d0..866178a 100644 --- a/main.c +++ b/main.c @@ -1,210 +1,53 @@ #include "libftx/libftx.h" #include "minishell.h" +#include -void ft_lstdel(void *ptr) -{ - t_cmd *content; - - content = (t_cmd *) ptr; - if (content->args != NULL) - ft_freer_tab_ultimate(1, content->args); - if (content->executable != NULL) - free(content->executable); - free(content); -} - -int ft_cmds_init(t_list **cmds, size_t len) -{ - t_cmd *content; - t_list *current; - size_t i; - - *cmds = malloc(sizeof(t_list)); - current = *cmds; - i = 0; - while (i < len) - { - content = malloc(sizeof(t_cmd)); - if (content == NULL) - { - ft_lstclear(cmds, ft_lstdel); - return (1); - } - content->args = NULL; - content->executable = NULL; - content->fd_in = -1; - content->fd_out = -1; - current->content = content; - if (!((i + 1) < len)) - { - current->next = NULL; - return (0); - } - current->next = malloc(sizeof(t_list)); - if (current->next == NULL) - ft_lstclear(cmds, ft_lstdel); - current = current->next; - i++; - } - return (0); -} - -int ft_cmds_prep(t_list **cmds, const char *line, int infile, int outfile) -{ - size_t len; - t_cmd *cmd; - t_list *current; - - len = ft_seglen_quoted(line, '|'); - if (len == 0) - return (0); - if (ft_cmds_init(cmds, ft_seglen_quoted(line, '|'))) - { - free(cmds); - 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); -} - -void ft_strshift(char *str, int shift) -{ - size_t i; - - if (shift > 0) - return ; - i = 0; - while (str[i - shift] != '\0') - { - str[i] = str[i - shift]; - i++; - } - str[i + shift] = '\0'; -} - -void ft_quote_remover(char *str) -{ - size_t i; - ssize_t start; - ssize_t stop; - - start = -1; - i = 0; - while (str[i] != '\0') - { - if ((str[i] == '\"' || str[i] == '\'')) - { - if (start == -1) - start = i; - else if (str[i] == str[start]) - { - stop = i; - break ; - } - } - i++; - } - if (start != -1) - { - ft_strshift(str, -1); - str[stop] = '\0'; - } -} - -int ft_cmd_filler(t_list *element, char **args) -{ - t_cmd *content; - size_t i; - - if (args == NULL) - return (1); - content = (t_cmd *)element->content; - i = 0; - while (args[i] != NULL) - { - ft_quote_remover(args[i]); - i++; - } - content->args = args; - //pas free:10 - content->executable = args[0]; - return (0); -} - -int ft_cmds_fill(t_list **cmds, const char *line) -{ - char **tab; - char **args; - t_list *current; - size_t i; - - tab = ft_split_quoted(line, '|'); - if (tab == NULL) - return (1); - i = 0; - current = *cmds; - while (tab[i] != NULL) - { - args = ft_split_quoted(tab[i], ' '); - if (ft_cmd_filler(current, args) == 1) - { - ft_lstclear(cmds, ft_lstdel); - ft_freer_tab_ultimate(2, args, tab); - return (1); - } - current = current->next; - i++; - } - ft_freer_tab_ultimate(1, tab); - return (0); -} - -t_list **ft_parse_cmds(char *line) -{ - int infile; - int outfile; - t_list **cmds; - - cmds = malloc(sizeof(t_list *)); - outfile = ft_outfile(line); - infile = ft_infile(line); - if (ft_syntatic_verif(line) == 1) - return (NULL); - if (ft_cmds_prep(cmds, line, infile, outfile) == 1) - return (NULL); - if (ft_cmds_fill(cmds, line) == 1) - return (NULL); - return (cmds); -} +/* int main(int ac, char **av) */ +/* { */ +/* char *line; */ +/* int fd; */ +/* */ +/* if (ac != 2) */ +/* return (1); */ +/* fd = open(av[1], O_RDONLY); */ +/* line = get_next_line(fd); */ +/* while (line != NULL) */ +/* { */ +/* ft_printf(line); */ +/* line = get_next_line(fd); */ +/* free(line); */ +/* } */ +/* free(line); */ +/* return (0); */ +/* } */ int main(int ac, char **av, char **env) { t_list **cmds; + char *line; t_data data; if (ac == 1) return (1); + line = ft_normalizer(av[1]); + ft_printf("%s\n", line); data.env = init_env(env); if (data.env == NULL) return (1); - cmds = ft_parse_cmds(av[1]); + cmds = ft_parse_cmds(line); if (cmds == NULL) return (1); + ft_printf("%s\n", line); if (ft_cmds_executor(cmds, env) == 1) { ft_lstclear(data.env, env_del); - ft_lstclear(cmds, ft_lstdel); + ft_lstclear(cmds, ft_cmddel); return (1); } ft_lstclear(data.env, env_del); - ft_lstclear(cmds, ft_lstdel); + ft_lstclear(cmds, ft_cmddel); free(cmds); free(data.env); + free(line); return (0); } diff --git a/main.o b/main.o deleted file mode 100644 index 24722c1..0000000 Binary files a/main.o and /dev/null differ diff --git a/minishell b/minishell deleted file mode 100755 index bd888d3..0000000 Binary files a/minishell and /dev/null differ diff --git a/minishell.h b/minishell.h index 6bf32c9..99fa3fc 100644 --- a/minishell.h +++ b/minishell.h @@ -6,7 +6,7 @@ /* By: cchauvet ') + { + ft_eprintf("minishell: %s: must be followed by an infile\n", tab[i]); + return (0); + } + ft_freer_tab_ultimate(1, tab); + return (1); +} + +static int ft_get_outfile(const char *line) { size_t i; - int append; - char *path; + int fd; + char **tab; - path = NULL; - append = 0; - i = 0; - while (line[i] != '\0') + tab = ft_split_quoted(line, ' '); + if (tab == NULL) { - if (line[i] == '>' && ft_is_in_quote(line, i) == 0) - { - i++; - if (line[i] == '>') - { - i++; - append = 1; - } - else - append = 0; - if (path != NULL) - free(path); - path = ft_get_file_path(line + i); - if (path == NULL || ft_file_is_writeable(path) == 0) - { - free(path); - return (-1); - } - } + ft_eprintf("minishell: malloc failed\n"); + return (-2); + } + fd = 0; + i = 0; + while (tab[i + 1] != NULL) + { + if (ft_strcmp(">", tab[i]) == 0) + fd = ft_file_is_writable(ft_quote_remover(tab[i + 1])); + else if (ft_strcmp(">>", tab[i]) == 0) + fd = ft_file_is_appendable(ft_quote_remover(tab[i + 1])); i++; } - if (path == NULL) - return (1); - if (append == 1) - i = open(path, O_APPEND | O_CREAT, 0664); - else - i = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0664); - free(path); - return (i); + ft_freer_tab_ultimate(1, tab); + return (fd); } static int ft_remove_outfile(char *line) { size_t i; - int separator; + size_t y; + char **tab; - i = 0; - while (line[i] != '\0') + tab = ft_split_quoted(line, ' '); + if (tab == NULL) { - if (line[i] == '>' && ft_is_in_quote(line, i) == 0) + ft_eprintf("minishell: malloc failed\n"); + return (1); + } + i = 0; + y = 0; + while (tab[i] != NULL) + { + if (tab[i][0] == '>') { - while (line[i] == '>') - line[i++] = ' '; - while (line[i] == ' ') - i++; - separator = ft_is_in_quote(line, i); - if (separator == 0) - separator = ' '; - else if (separator == 1) - separator = '\''; - else - separator = '\"'; - while (line[i] != separator && line[i] != '\0') - line[i++] = ' '; - if (line[i] != '\0' - && (separator == '\'' || separator == '\"')) - line[i++] = ' '; + ft_strshift(line + y, -1 * (ft_strlen(tab[i]) + 1)); + i++; + ft_strshift(line + y, -1 * (ft_strlen(tab[i]) + 1)); } + else + y = y + ft_strlen(tab[i]); i++; } + ft_freer_tab_ultimate(1, tab); return (0); } @@ -78,12 +96,11 @@ int ft_outfile(char *line) { int fd; + if (ft_outfile_is_valid(line) == 0) + return (-2); fd = ft_get_outfile(line); - if (ft_remove_outfile(line)) - { - if (fd > 0) - close(fd); - return (-1); - } + if (fd == -2) + return (-2); + ft_remove_outfile(line); return (fd); } diff --git a/outfile.o b/outfile.o deleted file mode 100644 index 74a3568..0000000 Binary files a/outfile.o and /dev/null differ diff --git a/spacer.c b/spacer.c new file mode 100644 index 0000000..98cd434 --- /dev/null +++ b/spacer.c @@ -0,0 +1,115 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* spacer.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cchauvet <|", out[i - 1])) + { + while (str[i] == out[i - 1]) + i++; + temp = ft_strreplace(out, " ", i, i); + free(out); + out = temp; + if (out == NULL) + return (NULL); + } + i++; + } + return (out); +} + +static char *ft_spacer_before(const char *str) +{ + char *out; + char *temp; + size_t i; + + out = ft_strdup(str); + if (out == NULL) + return (NULL); + i = 0; + while (out[i + 1] != '\0') + { + if (ft_is_in_quote(out, i)) + continue ; + if (ft_is_in("><|", out[i + 1])) + { + while (out[i] == ' ') + i++; + while (out[i] == out[i + 1]) + i++; + temp = ft_strreplace(out, " ", i + 1, i + 1); + free(out); + out = temp; + if (out == NULL) + return (NULL); + } + i++; + } + return (out); +} + +static void ft_space_simplifier(char *str) +{ + size_t i; + size_t y; + + i = 0; + while (str[i] != '\0') + { + y = 0; + while (str[y + i] == ' ') + y++; + if (ft_is_in_quote(str, i + y)) + { + i = i + y; + continue ; + } + if (y > 1) + { + ft_strshift(str + i, -y + 1); + y--; + } + i++; + } +} + +char *ft_normalizer(char *str) +{ + char *out; + char *temp; + + temp = ft_spacer_after(str); + if (temp == NULL) + return (NULL); + out = ft_spacer_before(temp); + free(temp); + if (out == NULL) + return (NULL); + ft_space_simplifier(out); + return (out); +} diff --git a/syntatics.o b/syntatics.o deleted file mode 100644 index b15b208..0000000 Binary files a/syntatics.o and /dev/null differ diff --git a/t b/t index 7f8f011..e69de29 100644 --- a/t +++ b/t @@ -1 +0,0 @@ -7 diff --git a/utils/ft_getstr.c b/utils/ft_getstr.c deleted file mode 100644 index b7f88ca..0000000 --- a/utils/ft_getstr.c +++ /dev/null @@ -1,27 +0,0 @@ -#include "utils.h" - -char *ft_getstr(const char *str, size_t n) -{ - size_t start; - size_t stop; - char c; - int quote; - - start = n; - stop = n; - quote = ft_is_in_quote(str, n); - if (quote == 0) - c = ' '; - else - { - if (quote == 1) - c = '\''; - else - c = '"'; - } - while (str[start - 1] != c && start > 0) - start--; - while (str[stop] != c && str[stop] != '\0') - stop++; - return (ft_strndup(str + start, stop - start)); -} diff --git a/utils/ft_is_in_quote.o b/utils/ft_is_in_quote.o deleted file mode 100644 index 74e6223..0000000 Binary files a/utils/ft_is_in_quote.o and /dev/null differ diff --git a/utils/ft_quote_remover.c b/utils/ft_quote_remover.c new file mode 100644 index 0000000..923c976 --- /dev/null +++ b/utils/ft_quote_remover.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_quote_remover.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cchauvet 0) + return ; + else + { + i = 0; + while (str[i - shift - 1] != '\0') + { + str[i] = str[i - shift]; + i++; + } + } +} diff --git a/utils/utils.h b/utils/utils.h index f1d80b8..57c9c3d 100644 --- a/utils/utils.h +++ b/utils/utils.h @@ -6,7 +6,7 @@ /* By: cchauvet