diff options
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | src/m_redirs.c | 69 | ||||
-rw-r--r-- | src/p_args.c | 44 | ||||
-rw-r--r-- | src/p_args.h | 2 | ||||
-rw-r--r-- | src/p_lblock.c | 74 | ||||
-rw-r--r-- | src/p_lblock.h | 1 | ||||
-rw-r--r-- | src/p_redirs.c | 44 | ||||
-rw-r--r-- | src/p_redirs.h | 22 | ||||
-rw-r--r-- | src/s_com.c | 13 | ||||
-rw-r--r-- | src/s_lredir.c | 1 | ||||
-rw-r--r-- | src/s_lredir.h | 8 |
11 files changed, 120 insertions, 159 deletions
@@ -73,6 +73,7 @@ SRCS_NAME += p_args_escape SRCS_NAME += p_line SRCS_NAME += p_lblock SRCS_NAME += p_lblock_next +SRCS_NAME += p_redirs SRCS_NAME += p_split SRCS_NAME += u_alias SRCS_NAME += u_parse diff --git a/src/m_redirs.c b/src/m_redirs.c index 50e416a..40a6646 100644 --- a/src/m_redirs.c +++ b/src/m_redirs.c @@ -22,39 +22,40 @@ void dup_redirs(const t_com *ptr, t_msh *msh) { - int32_t fd; + (void)ptr; + (void)msh; + /* int32_t fd; */ - if (ptr->redir == -1) - { - if ((fd = open(ptr->rdrpath, O_RDONLY)) == -1) - { - f_redir(ptr->rdrpath, msh); - return ; - } - ft_printf("[%s]", ptr->rdrpath); - dup2(fd, STDIN_FILENO); - close(fd); - } - else if (ptr->redir == 1) - { - if ((fd = open(ptr->rdrpath, - O_CREAT | O_TRUNC | O_WRONLY, 0644)) == -1) - { - f_redir(ptr->rdrpath, msh); - return ; - } - dup2(fd, ptr->rdrfd); - close(fd); - } - else if (ptr->redir == 2) - { - if ((fd = open(ptr->rdrpath, - O_CREAT | O_APPEND | O_WRONLY, 0644)) == -1) - { - f_redir(ptr->rdrpath, msh); - return ; - } - dup2(fd, ptr->rdrfd); - close(fd); - } + /* if (ptr->redir == -1) */ + /* { */ + /* if ((fd = open(ptr->rdrpath, O_RDONLY)) == -1) */ + /* { */ + /* f_redir(ptr->rdrpath, msh); */ + /* return ; */ + /* } */ + /* dup2(fd, STDIN_FILENO); */ + /* close(fd); */ + /* } */ + /* else if (ptr->redir == 1) */ + /* { */ + /* if ((fd = open(ptr->rdrpath, */ + /* O_CREAT | O_TRUNC | O_WRONLY, 0644)) == -1) */ + /* { */ + /* f_redir(ptr->rdrpath, msh); */ + /* return ; */ + /* } */ + /* dup2(fd, ptr->rdrfd); */ + /* close(fd); */ + /* } */ + /* else if (ptr->redir == 2) */ + /* { */ + /* if ((fd = open(ptr->rdrpath, */ + /* O_CREAT | O_APPEND | O_WRONLY, 0644)) == -1) */ + /* { */ + /* f_redir(ptr->rdrpath, msh); */ + /* return ; */ + /* } */ + /* dup2(fd, ptr->rdrfd); */ + /* close(fd); */ + /* } */ } diff --git a/src/p_args.c b/src/p_args.c index 54a95a7..fc676a1 100644 --- a/src/p_args.c +++ b/src/p_args.c @@ -88,7 +88,7 @@ static uint16_t } static char - **p_split_words_no_rdr(const char word[]) + **p_split_words(const char word[]) { char **words; size_t start[512]; @@ -109,50 +109,12 @@ static char } char - **p_split_args(char word[], int8_t redir) + **p_split_args(char word[]) { char **words; - size_t i; words = NULL; - if (redir == 0) - { - if ((words = p_split_words_no_rdr(word)) == NULL) - return (NULL); - return (words); - } - i = ft_strlen(word); - while (ft_ischarset("<>", word[i]) == FALSE) - i--; - i--; - while (redir > 0 && ft_isdigit(word[i]) == TRUE) - i--; - word[i] = C_NUL; - if ((words = p_split_words_no_rdr(word)) == NULL) + if ((words = p_split_words(word)) == NULL) return (NULL); return (words); - /* char **words; */ - /* char *subst; */ - /* size_t i; */ - - /* if (redir == 0) */ - /* { */ - /* if (!(words = ft_split(word, ' '))) */ - /* return (NULL); */ - /* return (words); */ - /* } */ - /* i = 0; */ - /* while (word[i] && ft_ischarset("<>", word[i]) == FALSE) */ - /* i++; */ - /* while (redir > 0 && ft_isdigit(word[i]) == TRUE) */ - /* i--; */ - /* if (!(subst = ft_substr(word, 0, i))) */ - /* return (NULL); */ - /* if (!(words = ft_split(subst, ' '))) */ - /* { */ - /* ft_memdel((void*)&subst); */ - /* return (NULL); */ - /* } */ - /* ft_memdel((void*)&subst); */ - /* return (words); */ } diff --git a/src/p_args.h b/src/p_args.h index fa61d92..42db6c0 100644 --- a/src/p_args.h +++ b/src/p_args.h @@ -15,6 +15,6 @@ #include <stdint.h> -char **p_split_args(char word[], int8_t redir); +char **p_split_args(char word[]); #endif diff --git a/src/p_lblock.c b/src/p_lblock.c index ba47028..a9b7144 100644 --- a/src/p_lblock.c +++ b/src/p_lblock.c @@ -24,79 +24,7 @@ #include "s_lpipes.h" #include "s_struct.h" -static void - rdr_err_check(char *ptr, t_com **com) -{ - if ((*com)->redir == -1 && ft_ischarset("><", *(ptr + 1)) == TRUE) - { - /* TODO: syntax err */ - } - else if ((*com)->redir == 1 && ft_ischarset("<", *(ptr + 1)) == TRUE) - { - /* TODO: syntax err */ - } - else if ((*com)->redir == 2 && ft_ischarset("<>", *(ptr + 1)) == TRUE) - { - /* TODO: syntax err */ - } -} - -static int8_t - get_rdrpath(char *ptr, t_com **com) -{ - char *p_rdrpath; - - ptr += ((*com)->redir == 2) ? (2) : (1); - if (!((*com)->rdrpath = - (char*)malloc((ft_strlen(ptr) + 1) * sizeof(char)))) - { - return (-1); - } - p_rdrpath = (*com)->rdrpath; - while (*ptr) - { - if (*ptr != ' ') - { - *p_rdrpath = *ptr; - p_rdrpath++; - } - ptr++; - } - *p_rdrpath = '\0'; - return (0); -} - -static void - get_rdrfd(const char *ptr, - t_com **com) -{ - while (ft_isdigit(*ptr) == TRUE) - { - ptr--; - } - if (*ptr != ' ') - (*com)->rdrfd = STDOUT_FILENO; - else - { - ptr += 1; - (*com)->rdrfd = ft_atoi(ptr); - } -} - -int8_t p_get_redirs(const char word[], t_com **com) -{ - char *ptr; - - ptr = (char *)word; - while (*ptr != C_NUL) - { - ptr++; - } - return (0); -} - -int8_t - p_line_block(const char line[], t_msh *msh) +int8_t p_line_block(const char line[], t_msh *msh) { /* TODO: norme */ uint64_t i; diff --git a/src/p_lblock.h b/src/p_lblock.h index 8247cfb..b5052f3 100644 --- a/src/p_lblock.h +++ b/src/p_lblock.h @@ -17,7 +17,6 @@ #include "s_struct.h" -int8_t p_get_redirs(const char word[], t_com **com); int8_t p_line_block(const char line[], t_msh *msh); #endif diff --git a/src/p_redirs.c b/src/p_redirs.c new file mode 100644 index 0000000..aec3ba6 --- /dev/null +++ b/src/p_redirs.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* p_redirs.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stdint.h> +#include <stddef.h> + +#include "d_define.h" +#include "s_struct.h" +#include "u_parse.h" + +int8_t p_redirs(char word[], t_com **com) +{ + struct s_com *com_ptr; + char *ptr; + t_quote_mode mode; + + mode = Q_NONE; + ptr = (char *)word; + while (*ptr != C_NUL) + { + if (*ptr == C_DQUOTE) + mode = u_meet_dquote(word, ptr, mode); + else if (*ptr == C_SQUOTE) + mode = u_meet_squote(word, ptr, mode); + else if (mode == Q_NONE && (*ptr == '<' || *ptr == '>') == 1) + { + if (p_get_redir(ptr, (ptr - word), com) != ) + ptr = word; + } + ptr++; + } + return (0); +} + diff --git a/src/p_redirs.h b/src/p_redirs.h new file mode 100644 index 0000000..493c44a --- /dev/null +++ b/src/p_redirs.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* p_redirs.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef P_REDIRS_H +#define P_REDIRS_H + +#include <stdint.h> + +#include "s_struct.h" + +int8_t p_redirs(char word[], t_com **com); + +#endif diff --git a/src/s_com.c b/src/s_com.c index 6dd4dc0..8c41040 100644 --- a/src/s_com.c +++ b/src/s_com.c @@ -19,6 +19,7 @@ #include "p_args.h" #include "p_lblock.h" #include "p_lblock_next.h" +#include "p_redirs.h" #include "s_lredir.h" #include "s_struct.h" @@ -82,8 +83,6 @@ void ft_memdel((void*)&ptr->bin); if (ptr->argv != NULL) ft_delwords(ptr->argv); - if (ptr->redir != 0) - ft_memdel((void*)&ptr->rdrpath); if (ptr->env_fork != NULL) ft_delwords(ptr->env_fork); if (ptr->rdr != NULL) @@ -102,13 +101,15 @@ t_com if ((com = (t_com*)malloc(sizeof(t_com))) == NULL) return (NULL); - com->redir = NULL; - com->bin = NULL; com->argv = NULL; com->env_fork = NULL; + com->bin = NULL; + com->rdr = NULL; nword[0] = C_NUL; + ft_printf("BEFORE: [%s]\n", nword); ft_strlcpy(nword, word, ARG_MAX); - if (p_get_redirs(nword, &com) != 0) + ft_printf("AFTER: [%s]\n", nword); + if (p_redirs(nword, &com) != 0) return (NULL); if (msh->alias != NULL) { @@ -117,7 +118,7 @@ t_com ret = p_subst_alias(nword, FALSE, msh); } p_subst_vars(nword, msh); - if ((words = p_split_args(nword, com->redir)) == NULL) + if ((words = p_split_args(nword)) == NULL) return (NULL); if ((words = p_subst_home(words, msh)) == NULL) return (NULL); diff --git a/src/s_lredir.c b/src/s_lredir.c index 353149d..1aae3be 100644 --- a/src/s_lredir.c +++ b/src/s_lredir.c @@ -10,6 +10,7 @@ /* */ /* ************************************************************************** */ +#include <libft.h> #include <stdint.h> #include <stdlib.h> #include <limits.h> diff --git a/src/s_lredir.h b/src/s_lredir.h index 3349329..4b82511 100644 --- a/src/s_lredir.h +++ b/src/s_lredir.h @@ -13,10 +13,12 @@ #ifndef S_LREDIR_H #define S_LREDIR_H +#include <stdint.h> + #include "s_struct.h" -void s_lredir_add_back(t_lredir **lredir, t_lredir *new) -void s_lredir_clear(struct s_lredir **lredir) -struct s_lredir *s_lredir_new(const char path[], int32_t fd, int8_t redir) +void s_lredir_add_back(t_lredir **lredir, t_lredir *new); +void s_lredir_clear(struct s_lredir **lredir); +struct s_lredir *s_lredir_new(const char path[], int32_t fd, int8_t redir); #endif |