diff options
author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-09-28 19:34:00 +0200 |
---|---|---|
committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-09-28 19:34:00 +0200 |
commit | 4df7b992eac08144283cfc589c2a8bc6f4c136ee (patch) | |
tree | 291bbce77b0f5b609ced0108757b27b3483471da | |
parent | New libft funcs (diff) | |
download | 42-minishell-4df7b992eac08144283cfc589c2a8bc6f4c136ee.tar.gz 42-minishell-4df7b992eac08144283cfc589c2a8bc6f4c136ee.tar.bz2 42-minishell-4df7b992eac08144283cfc589c2a8bc6f4c136ee.tar.xz 42-minishell-4df7b992eac08144283cfc589c2a8bc6f4c136ee.tar.zst 42-minishell-4df7b992eac08144283cfc589c2a8bc6f4c136ee.zip |
Fix redirs done
Diffstat (limited to '')
-rw-r--r-- | TODO.org | 5 | ||||
-rw-r--r-- | src/d_define.h | 1 | ||||
-rw-r--r-- | src/e_redirs.c | 2 | ||||
-rw-r--r-- | src/p_redirs.c | 19 | ||||
-rw-r--r-- | src/p_redirs_heredoc.c | 34 | ||||
-rw-r--r-- | src/p_redirs_heredoc.h | 4 |
6 files changed, 45 insertions, 20 deletions
@@ -62,9 +62,10 @@ + [ ] elif + [ ] while - do - done + [ ] for -** TODO [#C] << [1/2] +** DONE [#C] << [2/2] + CLOSED: [2020-09-28 Mon 19:30] + [X] base - + [ ] <<- to skip tabs + + [X] <<- to skip tabs ** TODO [#C] Termcaps ** TODO [#C] -i option (alias in scripts, etc) ** TODO [#C] Simple arithmetics $((a + 1)) diff --git a/src/d_define.h b/src/d_define.h index 65d4567..cc64a83 100644 --- a/src/d_define.h +++ b/src/d_define.h @@ -68,6 +68,7 @@ #define C_NUL 0x00 #define C_LF 0x0a +#define C_HT 0x09 #define C_SUB 0x1a #define C_ESC 0x1b #define C_DQUOTE 0x22 diff --git a/src/e_redirs.c b/src/e_redirs.c index 6bb600f..246f2c7 100644 --- a/src/e_redirs.c +++ b/src/e_redirs.c @@ -82,7 +82,7 @@ void e_dup_redirs(const t_com *com, t_msh *msh) ptr = com->rdr; while (ptr != NULL) { - if (ptr->redir == -2) + if (ptr->redir == -2 || ptr->redir == -3) { e_redir_minus_two(ptr); } diff --git a/src/p_redirs.c b/src/p_redirs.c index eeaefea..cbc9a10 100644 --- a/src/p_redirs.c +++ b/src/p_redirs.c @@ -43,9 +43,9 @@ static void p_append_redir(const char path[], return ; } new->heredoc = NULL; - if (redir == -2) + if (redir == -2 || redir == -3) { - new->heredoc = p_get_heredoc(path, msh); + new->heredoc = p_get_heredoc(path, redir, msh); } s_lredir_add_back(&com->rdr, new); } @@ -56,17 +56,17 @@ static size_t p_get_path(char path[], { char h[PATH_MAX]; size_t pos[2]; - size_t len[2]; + size_t hlen; + size_t len; - len[1] = 0; + hlen = 0; tmp.ptr += ft_abs(tmp.redir); - tmp.ptr += (tmp.redir == -2 && *tmp.ptr == '-') ? (1) : (0); while (*tmp.ptr != C_NUL && ft_iswhitespace(*tmp.ptr) == TRUE) tmp.ptr++; if (*tmp.ptr == C_TILDE && u_get_var_value(h, "$HOME", PATH_MAX, msh) == 0) { ft_strlcpy(path, h, PATH_MAX); - len[1] = ft_strlen(h); + hlen = ft_strlen(h); tmp.ptr++; } pos[0] = (tmp.ptr - tmp.word); @@ -74,9 +74,9 @@ static size_t p_get_path(char path[], && u_is_not_escaped(tmp.word, tmp.ptr) == TRUE) tmp.ptr++; pos[1] = (tmp.ptr - tmp.word); - len[0] = (pos[1] - pos[0]); - len[0] = ((len[1] + len[0] + 1) > PATH_MAX) ? (PATH_MAX - 1) : (len[0]); - ft_strlcpy(path + len[1], tmp.word + pos[0], len[0] + 1); + len = (pos[1] - pos[0]); + len = ((hlen + len + 1) > PATH_MAX) ? (PATH_MAX - 1) : (len); + ft_strlcpy(path + hlen, tmp.word + pos[0], len + 1); return (pos[1]); } @@ -116,6 +116,7 @@ static void p_get_redir(char word[], char *ptr, t_com *com, t_msh *msh) fd_rdr[RDR] = (*ptr == '>') ? (1) : (-1); fd_rdr[RDR] = (fd_rdr[RDR] == 1 && *(ptr + 1) == '>') ? (2) : (fd_rdr[1]); fd_rdr[RDR] = (fd_rdr[RDR] == -1 && *(ptr + 1) == '<') ? (-2) : (fd_rdr[1]); + fd_rdr[RDR] = (fd_rdr[RDR] == -2 && *(ptr + 2) == '-') ? (-3) : (fd_rdr[1]); if (fd_rdr[FD] == STDOUT_FILENO) pos[0] = (ptr - word); else diff --git a/src/p_redirs_heredoc.c b/src/p_redirs_heredoc.c index 6b939fe..6ba06f1 100644 --- a/src/p_redirs_heredoc.c +++ b/src/p_redirs_heredoc.c @@ -18,30 +18,50 @@ #include "m_prompt.h" #include "s_struct.h" -char *p_get_heredoc(const char path[], t_msh *msh) +static void p_skip_tabs(char *line[], const int8_t redir) +{ + char *ptr; + + if (redir != -3) + return ; + ptr = *line; + while (ptr[0] != C_NUL && ptr[0] == C_HT) + { + (void)ft_memmove(ptr, ptr + 1, (ft_strlen(ptr) + 1) * sizeof(char)); + } +} + +static void p_append_line(char *heredoc[], const char line[]) +{ + char *ptr; + + ptr = *heredoc; + ft_strlcpy(ptr + ft_strlen(ptr), line, ft_strlen(line) + 1); + ptr[ft_strlen(ptr) + 1] = C_NUL; + ptr[ft_strlen(ptr)] = C_LF; +} + +char *p_get_heredoc(const char path[], const int8_t redir, t_msh *msh) { char *heredoc; char *line; int8_t gnl; - line = NULL; - heredoc = NULL; - gnl = 1; if ((heredoc = ft_strdup("")) == NULL) return (NULL); + gnl = 1; while (gnl > 0) { if (msh->fd == STDIN_FILENO) m_prompt_psx(2, msh); gnl = get_next_line(msh->fd, &line); + p_skip_tabs(&line, redir); if (ft_strncmp(path, line, ft_strlen(path) + 1) == 0) break ; if ((heredoc = ft_nrealloc(heredoc, ft_strlen(heredoc) + 1, ft_strlen(heredoc) + ft_strlen(line) + 2)) == NULL) break ; - ft_strlcpy(heredoc + ft_strlen(heredoc), line, ft_strlen(line) + 1); - heredoc[ft_strlen(heredoc) + 1] = C_NUL; - heredoc[ft_strlen(heredoc)] = C_LF; + p_append_line(&heredoc, line); ft_memdel((void*)&line); } diff --git a/src/p_redirs_heredoc.h b/src/p_redirs_heredoc.h index 15d6e70..91803ac 100644 --- a/src/p_redirs_heredoc.h +++ b/src/p_redirs_heredoc.h @@ -13,8 +13,10 @@ #ifndef P_REDIRS_HEREDOC_H #define P_REDIRS_HEREDOC_H +#include <stdint.h> + #include "s_struct.h" -char *p_get_heredoc(const char path[], t_msh *msh); +char *p_get_heredoc(const char path[], const int8_t redir, t_msh *msh); #endif |