diff --git a/a.out b/a.out new file mode 100755 index 0000000..9e84bec Binary files /dev/null and b/a.out differ diff --git a/d b/d index e69de29..d0b1d06 100755 --- a/d +++ b/d @@ -0,0 +1,24 @@ +a.out +argprinter +d +execution.c +file.c +file.o +ft_split_quoted.c +ft_split_quoted.o +heredoc.c +heredoc.o +infile.c +infile.o +libftx +main.c +main.o +Makefile +minishell +minishell.h +outfile.c +outfile.o +syntatics.c +syntatics.o +t +utils diff --git a/execution.c b/execution.c new file mode 100644 index 0000000..5ed3840 --- /dev/null +++ b/execution.c @@ -0,0 +1,39 @@ +#include "libftx/libftx.h" +#include "minishell.h" +#include + +int main(int ac, char **av, char **env) +{ + +} + +char *ft_get_executable_path(t_data *data, char *executable) +{ + if (ft_strcmp(executable, "env")) + return (ft_strjoin("/usr/bin/bo", const char *s2)) +} + +int ft_excutor(t_cmd *cmd, ) +{ + char cmd[13] = "/usr/bin/ls"; + char *args[3] = {NULL, "cat", NULL}; + int pid; + int fd_in; + int fd_out; + + if (ac != 3) + return (1); + fd_out = open(av[2], O_WRONLY | O_CREAT | O_TRUNC); + fd_in = open(av[1], O_RDONLY); + pid = fork(); + if (pid == -1) + return (1); + if (pid == 0) + { + dup2(fd_out, 1); + dup2(fd_in, 0); + execve(cmd, args, env); + } + else + waitpid(pid); +} diff --git a/file.c b/file.c index da9b9c8..d03757d 100644 --- a/file.c +++ b/file.c @@ -1,6 +1,6 @@ #include "minishell.h" -int ft_file_is_readable(char *path) +int ft_file_is_readable(const char *path) { int readable; int fd; @@ -21,7 +21,7 @@ int ft_file_is_readable(char *path) return (1); } -int ft_file_is_writeable(char *path) +int ft_file_is_writeable(const char *path) { int writeable; int fd; @@ -42,7 +42,7 @@ int ft_file_is_writeable(char *path) return (1); } -char *ft_get_file_path(char *infile) +char *ft_get_file_path(const char *infile) { size_t i; size_t n; diff --git a/file.o b/file.o index 5561861..f7d44cf 100644 Binary files a/file.o and b/file.o differ diff --git a/ft_split_quoted.o b/ft_split_quoted.o index ccc57e3..210c12f 100644 Binary files a/ft_split_quoted.o and b/ft_split_quoted.o differ diff --git a/heredoc.o b/heredoc.o index 9d6ea8f..1b9deb4 100644 Binary files a/heredoc.o and b/heredoc.o differ diff --git a/infile.o b/infile.o index c829348..69a9740 100644 Binary files a/infile.o and b/infile.o differ diff --git a/libftx/extra/Makefile b/libftx/extra/Makefile index 269a370..bc967dd 100644 --- a/libftx/extra/Makefile +++ b/libftx/extra/Makefile @@ -12,7 +12,7 @@ CC = clang -SRCS = ft_contain_only.c ft_freer.c ft_is_in.c ft_random_generator.c ft_strchri.c ft_strcmp.c ft_strfjoin.c ft_strgen.c ft_strmerger.c ft_strndup.c ft_tabrealloc.c ft_ultoa_base.c +SRCS = ft_contain_only.c ft_freer.c ft_is_in.c ft_random_generator.c ft_strchri.c ft_strcmp.c ft_strfjoin.c ft_strgen.c ft_strmerger.c ft_strndup.c ft_tabrealloc.c ft_ultoa_base.c ft_swap.c OBJS = $(SRCS:.c=.o) diff --git a/libftx/extra/extra.a b/libftx/extra/extra.a index f037fe7..309850b 100644 Binary files a/libftx/extra/extra.a and b/libftx/extra/extra.a differ diff --git a/libftx/extra/extra.h b/libftx/extra/extra.h index c5180fa..f745d3f 100644 --- a/libftx/extra/extra.h +++ b/libftx/extra/extra.h @@ -28,9 +28,13 @@ char *ft_strfjoin(char *s1, char *s2); char *ft_strmerger(size_t arg_len, ...); int ft_is_in(char *str, char c); char **ft_tabrealloc(char **tab, size_t current_size, size_t new_size); -char *ft_strndup(char *src, size_t n); +char *ft_strndup(const char *src, size_t n); ssize_t ft_strchri(char *str, char c); int ft_contain_only_str(char *str, char *to_find); int ft_contain_only(char *str, char c); int ft_strcmp(char *s1, char *s2); +void ft_swap(void *a, void *b); +void ft_swap_int(int *a, int *b); +void ft_swap_char(char *a, char *b); + #endif diff --git a/libftx/extra/ft_strndup.c b/libftx/extra/ft_strndup.c index c0fc2aa..c289878 100644 --- a/libftx/extra/ft_strndup.c +++ b/libftx/extra/ft_strndup.c @@ -12,7 +12,7 @@ #include "extra.h" -char *ft_strndup(char *src, size_t n) +char *ft_strndup(const char *src, size_t n) { char *out; size_t i; diff --git a/libftx/extra/ft_strndup.o b/libftx/extra/ft_strndup.o index dae7c88..585b50b 100644 Binary files a/libftx/extra/ft_strndup.o and b/libftx/extra/ft_strndup.o differ diff --git a/libftx/extra/ft_swap.c b/libftx/extra/ft_swap.c new file mode 100644 index 0000000..53fba9f --- /dev/null +++ b/libftx/extra/ft_swap.c @@ -0,0 +1,28 @@ +#include "utils.h" + +void ft_swap(void *a, void *b) +{ + void *c; + + c = a; + a = b; + b = c; +} + +void ft_swap_char(char *a, char *b) +{ + char c; + + c = *a; + *a = *b; + *b = c; +} + +void ft_swap_int(int *a, int *b) +{ + int c; + + c = *a; + *a = *b; + *b = c; +} diff --git a/libftx/extra/ft_swap.o b/libftx/extra/ft_swap.o new file mode 100644 index 0000000..f00ec7dd Binary files /dev/null and b/libftx/extra/ft_swap.o differ diff --git a/libftx/extra/ft_tabrealloc-5f29f43f.o.tmp b/libftx/extra/ft_tabrealloc-5f29f43f.o.tmp deleted file mode 100644 index e69de29..0000000 diff --git a/libftx/libft/Makefile b/libftx/libft/Makefile index 9a2c3b1..0102a19 100644 --- a/libftx/libft/Makefile +++ b/libftx/libft/Makefile @@ -45,9 +45,8 @@ SRCS = ft_isalpha.c \ ft_putchar_fd.c \ ft_putstr_fd.c \ ft_putendl_fd.c \ - ft_putnbr_fd.c - -BSRCS = ft_lstnew.c \ + ft_putnbr_fd.c \ + ft_lstnew.c \ ft_lstadd_front.c \ ft_lstsize.c \ ft_lstlast.c \ @@ -59,8 +58,6 @@ BSRCS = ft_lstnew.c \ OBJS = $(SRCS:.c=.o) -BOBJS = $(BSRCS:.c=.o) - NAME = libft.a CFLAGS = -Wall -Werror -Wextra -g @@ -73,15 +70,12 @@ all: $(NAME) $(NAME): $(OBJS) ar -rc $(NAME) $(OBJS) -bonus: $(OBJS) $(BOBJS) - ar -rc $(NAME) $(OBJS) $(BOBJS) - clean: - rm -f $(OBJS) $(BOBJS) + rm -f $(OBJS) fclean: clean rm -f $(NAME) re: fclean all -.PHONY: all bonus clean fclean re +.PHONY: all clean fclean re diff --git a/libftx/libft/ft_lstadd_back.o b/libftx/libft/ft_lstadd_back.o new file mode 100644 index 0000000..3dd90bc Binary files /dev/null and b/libftx/libft/ft_lstadd_back.o differ diff --git a/libftx/libft/ft_lstadd_front.o b/libftx/libft/ft_lstadd_front.o new file mode 100644 index 0000000..46cb135 Binary files /dev/null and b/libftx/libft/ft_lstadd_front.o differ diff --git a/libftx/libft/ft_lstclear.o b/libftx/libft/ft_lstclear.o new file mode 100644 index 0000000..563dd89 Binary files /dev/null and b/libftx/libft/ft_lstclear.o differ diff --git a/libftx/libft/ft_lstdelone.o b/libftx/libft/ft_lstdelone.o new file mode 100644 index 0000000..2182609 Binary files /dev/null and b/libftx/libft/ft_lstdelone.o differ diff --git a/libftx/libft/ft_lstiter.o b/libftx/libft/ft_lstiter.o new file mode 100644 index 0000000..d5d3d11 Binary files /dev/null and b/libftx/libft/ft_lstiter.o differ diff --git a/libftx/libft/ft_lstlast.o b/libftx/libft/ft_lstlast.o new file mode 100644 index 0000000..6262d1d Binary files /dev/null and b/libftx/libft/ft_lstlast.o differ diff --git a/libftx/libft/ft_lstmap.o b/libftx/libft/ft_lstmap.o new file mode 100644 index 0000000..adcdfdc Binary files /dev/null and b/libftx/libft/ft_lstmap.o differ diff --git a/libftx/libft/ft_lstnew.o b/libftx/libft/ft_lstnew.o new file mode 100644 index 0000000..8b02f8b Binary files /dev/null and b/libftx/libft/ft_lstnew.o differ diff --git a/libftx/libft/ft_lstsize.o b/libftx/libft/ft_lstsize.o new file mode 100644 index 0000000..a66c496 Binary files /dev/null and b/libftx/libft/ft_lstsize.o differ diff --git a/libftx/libft/ft_split.c b/libftx/libft/ft_split.c index 9924e7d..acd870d 100644 --- a/libftx/libft/ft_split.c +++ b/libftx/libft/ft_split.c @@ -68,7 +68,7 @@ static char **ft_segsplitter(char **tab, size_t len, const char *s, char c) let_index++; tab[tab_index] = ft_substr(s, start, let_index - start); if (tab[tab_index] == NULL) - return (ft_cancel(tab, tab_index)); + return (ft_cancel((void **)tab, tab_index)); tab_index++; } return (tab); diff --git a/libftx/libft/ft_split.o b/libftx/libft/ft_split.o index 198869a..3b9bb56 100644 Binary files a/libftx/libft/ft_split.o and b/libftx/libft/ft_split.o differ diff --git a/libftx/libft/libft.a b/libftx/libft/libft.a index 43a93d3..da9bcae 100644 Binary files a/libftx/libft/libft.a and b/libftx/libft/libft.a differ diff --git a/libftx/libft/libft.h b/libftx/libft/libft.h index 3c98b38..11cc2c4 100644 --- a/libftx/libft/libft.h +++ b/libftx/libft/libft.h @@ -15,7 +15,7 @@ # include # include -void *ft_cancel(char **tab, size_t len); +void *ft_cancel(void **tab, size_t len); int ft_atoi(const char *nptr); void ft_bzero(void *s, size_t n); void *ft_calloc(size_t nmemb, size_t size); diff --git a/libftx/libftx.a b/libftx/libftx.a index 4796721..b5d3abd 100644 Binary files a/libftx/libftx.a and b/libftx/libftx.a differ diff --git a/libftx/libftx.h b/libftx/libftx.h index 1ce04b2..5735aa1 100644 --- a/libftx/libftx.h +++ b/libftx/libftx.h @@ -32,7 +32,10 @@ char *ft_strfjoin(char *s1, char *s2); char *ft_strmerger(size_t arg_len, ...); int ft_strcmp(char *s1, char *s2); ssize_t ft_strchri(char *str, char c); -char *ft_strndup(char *src, size_t n); +char *ft_strndup(const char *src, size_t n); +void ft_swap(void *a, void *b); +void ft_swap_int(int *a, int *b); +void ft_swap_char(char *a, char *b); void *ft_cancel(void **tab, size_t len); int ft_atoi(const char *nptr); diff --git a/main.c b/main.c index 0d630fb..da0df79 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,5 @@ #include "libftx/libftx.h" #include "minishell.h" -#include void ft_lstdel(void *ptr) { @@ -12,7 +11,7 @@ void ft_lstdel(void *ptr) if (content->executable != NULL) free(content->executable); if (content->args != NULL) - free(content->args); + ft_freer_tab_ultimate(1, content->args); free(content); free(element); } @@ -23,29 +22,38 @@ int ft_cmds_init(t_list **cmds, size_t len) t_list *current; size_t i; - current = malloc(sizeof(t_list)); - if (current == NULL) - return (1); + *cmds = malloc(sizeof(t_list)); + current = *cmds; i = 0; while (i < len) { - content = malloc(sizeof(t_cmd *)); + 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->next = malloc(sizeof(t_list *)); + 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 i; size_t len; t_cmd *cmd; t_list *current; @@ -53,9 +61,11 @@ int ft_cmds_prep(t_list **cmds, const char *line, int infile, int outfile) len = ft_seglen_quoted(line, '|'); if (len == 0) return (0); - cmds = malloc(sizeof(t_list *)); if (ft_cmds_init(cmds, ft_seglen_quoted(line, '|'))) + { + free(cmds); return (1); + } cmd = (t_cmd *)(*cmds)->content; cmd->fd_in = infile; current = *cmds; @@ -66,33 +76,152 @@ int ft_cmds_prep(t_list **cmds, const char *line, int infile, int outfile) return (0); } -int ft_cmds_fill(t_list **cmds, const char *line) +void ft_strshift(char *str, int shift) { - //TODO - //remplir les executables; - // les args; + size_t i; + + if (shift > 0) + return ; + i = 0; + while (str[i - shift] != '\0') + { + str[i] = str[i - shift]; + i++; + } + str[i + shift] = '\0'; } -t_list **ft_parse_cmd(char *line) +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; + //TODO check if executable exist + //TODO change it by env value + //TODO add switch to bultin + content->executable = ft_strjoin("/usr/bin/", args[0]); + if (content->executable == NULL) + return (1); + return (0); +} + +int ft_cmds_fill(t_list **cmds, const char *line) +{ + char **tab; + char **args; + t_list *current; + size_t i; + + (void) cmds; + 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(1, args); + return (1); + } + current = current->next; + i++; + } + return (0); +} + +t_list **ft_parse_cmds(char *line) { int infile; int outfile; t_list **cmds; - cmds = NULL; + cmds = malloc(sizeof(t_list *)); outfile = ft_outfile(line); infile = ft_infile(line); if (ft_syntatic_verif(line) == 1) return (NULL); - ft_cmds_prep(cmds, line, infile, outfile); + if (ft_cmds_prep(cmds, line, infile, outfile) == 1) + return (NULL); + if (ft_cmds_fill(cmds, line) == 1) + return (NULL); ft_printf("%s\n", line); - return (NULL); + return (cmds); +} + +int ft_cmds_excutor(t_list **cmds) +{ + t_cmd *content; + t_list *current; + size_t i; + + i = 0; + current = *cmds; + while (current != NULL) + { + content = current->content; + ft_printf("--- COMMAND %d\n", i); + ft_printf("excutable: %s\n", content->executable); + ft_printf("args:\n%S", content->args); + current = current->next; + i++; + } + return (0); } int main(int ac, char **av) { + t_list **cmds; + if (ac == 1) return (1); - ft_parse_cmd(av[1]); + cmds = ft_parse_cmds(av[1]); + if (cmds == NULL) + return (1); + if (ft_cmds_excutor(cmds) == 1) + return (1); + ft_lstclear(cmds); return (1); } diff --git a/main.o b/main.o index 7a6a47d..babf8b8 100644 Binary files a/main.o and b/main.o differ diff --git a/minishell b/minishell index 167b363..4c2107d 100755 Binary files a/minishell and b/minishell differ diff --git a/minishell.h b/minishell.h index f0ef128..151e9e1 100644 --- a/minishell.h +++ b/minishell.h @@ -24,7 +24,7 @@ typedef struct s_cmd int fd_in; int fd_out; char *executable; - char *args; + char **args; } t_cmd; #endif diff --git a/outfile.o b/outfile.o index 9f922f0..0f40334 100644 Binary files a/outfile.o and b/outfile.o differ diff --git a/syntatics.c b/syntatics.c index 475e244..ab9cc7d 100644 --- a/syntatics.c +++ b/syntatics.c @@ -1,7 +1,7 @@ #include "libftx/libftx.h" #include "minishell.h" -static int ft_quote_verif(char *str) +static int ft_quote_verif(const char *str) { if (ft_is_in_quote(str, ft_strlen(str))) { @@ -12,7 +12,7 @@ static int ft_quote_verif(char *str) return (0); } -static int ft_multipipe(char *str) +static int ft_multipipe(const char *str) { size_t i; size_t y; @@ -21,21 +21,36 @@ static int ft_multipipe(char *str) while (str[i] != '\0') { y = 0; - while (str[i + y] == '|' && ft_is_in_quote(str, i)) + while (str[i + y] == '|' && !ft_is_in_quote(str, i)) { - y++; - if (y > 1) + if (y > 0) { ft_eprintf("minishell: Multiple pipes is not supported\n"); return (1); } + y++; } i++; } return (0); } -int ft_syntatic_verif(char *str) +static int ft_pipe_is_alone(const char *str) { - return (ft_quote_verif(str) || ft_multipipe(str)); + size_t i; + + i = ft_strlen(str) - 1; + while (str[i] != '|' && i > 0) + { + if (str[i] != ' ') + return (0); + i--; + } + ft_eprintf("minishell: Pipe must be followed by a command or redirection\n"); + return (1); +} + +int ft_syntatic_verif(const char *str) +{ + return (ft_quote_verif(str) || ft_multipipe(str) || ft_pipe_is_alone(str)); } diff --git a/syntatics.o b/syntatics.o index e7a1364..cf454fd 100644 Binary files a/syntatics.o and b/syntatics.o differ diff --git a/t b/t index e69de29..d0b1d06 100644 --- a/t +++ b/t @@ -0,0 +1,24 @@ +a.out +argprinter +d +execution.c +file.c +file.o +ft_split_quoted.c +ft_split_quoted.o +heredoc.c +heredoc.o +infile.c +infile.o +libftx +main.c +main.o +Makefile +minishell +minishell.h +outfile.c +outfile.o +syntatics.c +syntatics.o +t +utils diff --git a/utils/ft_getstr.c b/utils/ft_getstr.c index c55fed4..b7f88ca 100644 --- a/utils/ft_getstr.c +++ b/utils/ft_getstr.c @@ -1,11 +1,11 @@ #include "utils.h" -char *ft_getstr(char *str, size_t n) +char *ft_getstr(const char *str, size_t n) { size_t start; size_t stop; char c; - int quote; + int quote; start = n; stop = n; diff --git a/utils/ft_getstr.o b/utils/ft_getstr.o index 34d708b..abfa88a 100644 Binary files a/utils/ft_getstr.o and b/utils/ft_getstr.o differ diff --git a/utils/ft_strnchr.c b/utils/ft_strnchr.c index 6654db9..d385f48 100644 --- a/utils/ft_strnchr.c +++ b/utils/ft_strnchr.c @@ -1,6 +1,6 @@ #include "utils.h" -ssize_t ft_strnchr(char *str, char c) +ssize_t ft_strnchr(const char *str, char c) { size_t i; diff --git a/utils/ft_strnchr.o b/utils/ft_strnchr.o index 4acfc36..2069436 100644 Binary files a/utils/ft_strnchr.o and b/utils/ft_strnchr.o differ diff --git a/utils/ft_strncpy.c b/utils/ft_strncpy.c index 16de576..8eca3f1 100644 --- a/utils/ft_strncpy.c +++ b/utils/ft_strncpy.c @@ -1,6 +1,6 @@ #include "utils.h" -size_t ft_strncpy(char *dst, char *src, size_t n) +size_t ft_strncpy(char *dst, const char *src, size_t n) { size_t i; diff --git a/utils/ft_strncpy.o b/utils/ft_strncpy.o index f777e2b..07b66cd 100644 Binary files a/utils/ft_strncpy.o and b/utils/ft_strncpy.o differ diff --git a/utils/ft_strreplace.c b/utils/ft_strreplace.c index 8a2a2ba..cfe92bf 100644 --- a/utils/ft_strreplace.c +++ b/utils/ft_strreplace.c @@ -1,11 +1,12 @@ #include "utils.h" -char *ft_strreplace(char *str, char *fill, size_t start, size_t stop) +char *ft_strreplace(char *str, const char *fill, size_t start, size_t stop) { char *out; size_t sum; - out = malloc((ft_strlen(str) + ft_strlen(fill) - (stop - start) + 1 * sizeof(char))); + out = malloc((ft_strlen(str) + ft_strlen(fill) - (stop - start) + 1 + * sizeof(char))); if (out == NULL) return (NULL); ft_strncpy(out, str, start); diff --git a/utils/ft_strreplace.o b/utils/ft_strreplace.o index 3dbc090..ea3e08c 100644 Binary files a/utils/ft_strreplace.o and b/utils/ft_strreplace.o differ