here_doc script_fd fix
+ line_len in here_doc + error message SIGQUIT
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/24 10:52:40 by lperrey #+# #+# */
|
||||
/* Updated: 2021/12/20 16:22:13 by hulamy ### ########.fr */
|
||||
/* Updated: 2021/12/22 13:53:19 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
|
||||
void save_redirections_words(t_token *t);
|
||||
|
||||
t_cmd **parsing(t_token *token_list)
|
||||
t_cmd **parsing(t_token *token_list, int script_fd)
|
||||
{
|
||||
t_cmd **pipeline;
|
||||
|
||||
@@ -26,7 +26,7 @@ t_cmd **parsing(t_token *token_list)
|
||||
return (NULL);
|
||||
if (!expansions(token_list, pipeline))
|
||||
return (ft_retp_free(NULL, &pipeline, (t_free_f)free_pipeline));
|
||||
if (!redirections(token_list, pipeline))
|
||||
if (!redirections(token_list, pipeline, script_fd))
|
||||
return (ft_retp_free(NULL, &pipeline, (t_free_f)free_pipeline));
|
||||
if (!pipeline_fill_argv(token_list, pipeline))
|
||||
return (ft_retp_free(NULL, &pipeline, (t_free_f)free_pipeline));
|
||||
|
||||
@@ -6,21 +6,21 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/11/11 18:46:43 by lperrey #+# #+# */
|
||||
/* Updated: 2021/12/20 14:24:51 by hulamy ### ########.fr */
|
||||
/* Updated: 2021/12/22 14:16:45 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
static int here_doc_write(char *delimiter, int doc_fd);
|
||||
static int here_doc_write(char *delimiter, int doc_fd, int script_fd);
|
||||
static int here_doc_write_interactive(char *delimiter, int doc_fd);
|
||||
static int here_doc_write_script(char *delimiter, int doc_fd);
|
||||
static int here_doc_write_script(char *delimiter, int doc_fd, int script_fd);
|
||||
static int event_hook_empty(void);
|
||||
|
||||
#define TMP_HERE_DOC "/tmp/minishell_here_doc"
|
||||
#define WARNING_EOF "warning: here-document delimited by end-of-file (wanted `"
|
||||
|
||||
int here_doc(char *delimiter)
|
||||
int here_doc(char *delimiter, int script_fd)
|
||||
{
|
||||
int here_doc;
|
||||
int ret;
|
||||
@@ -31,7 +31,7 @@ int here_doc(char *delimiter)
|
||||
here_doc = open(TMP_HERE_DOC, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU);
|
||||
if (here_doc == -1)
|
||||
return (ft_reti_perror_io(-1, "open() ", TMP_HERE_DOC));
|
||||
ret = here_doc_write(delimiter, here_doc);
|
||||
ret = here_doc_write(delimiter, here_doc, script_fd);
|
||||
free(delimiter);
|
||||
if (close(here_doc) == -1)
|
||||
shell_perror(TMP_HERE_DOC, ": ", "", 0);
|
||||
@@ -49,12 +49,16 @@ int here_doc(char *delimiter)
|
||||
return (here_doc);
|
||||
}
|
||||
|
||||
static int here_doc_write(char *delimiter, int doc_fd)
|
||||
static int here_doc_write(char *delimiter, int doc_fd, int script_fd)
|
||||
{
|
||||
int ret;
|
||||
struct sigaction signal_action;
|
||||
|
||||
if (isatty(STDIN_FILENO))
|
||||
if (script_fd || !isatty(STDIN_FILENO))
|
||||
{
|
||||
ret = here_doc_write_script(delimiter, doc_fd, script_fd);
|
||||
}
|
||||
else
|
||||
{
|
||||
ft_bzero(&signal_action, sizeof(signal_action));
|
||||
signal_action.sa_handler = sigint_handler_heredoc;
|
||||
@@ -65,17 +69,14 @@ static int here_doc_write(char *delimiter, int doc_fd)
|
||||
signal_action.sa_handler = SIG_IGN;
|
||||
sigaction(SIGINT, &signal_action, NULL);
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = here_doc_write_script(delimiter, doc_fd);
|
||||
}
|
||||
return (ret);
|
||||
}
|
||||
|
||||
static int here_doc_write_interactive(char *delimiter, int doc_fd)
|
||||
{
|
||||
char *line;
|
||||
|
||||
size_t line_len;
|
||||
|
||||
line = NULL;
|
||||
g_switch_heredoc_sigint = 0;
|
||||
while (1)
|
||||
@@ -85,9 +86,10 @@ static int here_doc_write_interactive(char *delimiter, int doc_fd)
|
||||
return (set_last_exit_status(EXIT_SIGNAL + SIGINT));
|
||||
if (!line)
|
||||
return (shell_error(WARNING_EOF, delimiter, "')", 0));
|
||||
if (ft_strncmp(line, delimiter, ft_strlen(line) + 1) == 0)
|
||||
line_len = ft_strlen(line);
|
||||
if (ft_strncmp(line, delimiter, line_len + 1) == 0)
|
||||
break ;
|
||||
if (write(doc_fd, line, ft_strlen(line)) == -1)
|
||||
if (write(doc_fd, line, line_len) == -1)
|
||||
return (ft_reti_perror_free(-1, line, free, "write "TMP_HERE_DOC));
|
||||
if (write(doc_fd, "\n", 1) == -1)
|
||||
return (ft_reti_perror_free(-1, line, free, "write "TMP_HERE_DOC));
|
||||
@@ -97,21 +99,23 @@ static int here_doc_write_interactive(char *delimiter, int doc_fd)
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int here_doc_write_script(char *delimiter, int doc_fd)
|
||||
static int here_doc_write_script(char *delimiter, int doc_fd, int script_fd)
|
||||
{
|
||||
char *line;
|
||||
int ret;
|
||||
size_t line_len;
|
||||
|
||||
line = NULL;
|
||||
ret = 1;
|
||||
while (ret)
|
||||
{
|
||||
ret = gnl(STDIN_FILENO, &line, 0);
|
||||
ret = gnl(script_fd, &line, 0);
|
||||
if (ret == -1)
|
||||
return (ft_reti_perror_free(-1, line, free, "gnl() STDIN"));
|
||||
if (ft_strncmp(line, delimiter, ft_strlen(line) + 1) == 0)
|
||||
return (ft_reti_perror_free(-1, line, free, "gnl() script_fd"));
|
||||
line_len = ft_strlen(line);
|
||||
if (ft_strncmp(line, delimiter, line_len + 1) == 0)
|
||||
break ;
|
||||
if (write(doc_fd, line, ft_strlen(line)) == -1)
|
||||
if (write(doc_fd, line, line_len) == -1)
|
||||
return (ft_reti_perror_free(-1, line, free, "write "TMP_HERE_DOC));
|
||||
if ((ret || line[0]) && write(doc_fd, "\n", 1) == -1)
|
||||
return (ft_reti_perror_free(-1, line, free, "write "TMP_HERE_DOC));
|
||||
|
||||
@@ -6,20 +6,20 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/11/11 18:46:43 by lperrey #+# #+# */
|
||||
/* Updated: 2021/12/20 15:25:16 by hulamy ### ########.fr */
|
||||
/* Updated: 2021/12/22 14:00:33 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
int here_doc(char *delimiter);
|
||||
int here_doc(char *delimiter, int script_fd);
|
||||
|
||||
static int redirect_cmd_input(t_token *t, t_cmd *cmd);
|
||||
static int redirect_cmd_heredoc(t_token *t, t_cmd *cmd);
|
||||
static int redirect_cmd_heredoc(t_token *t, t_cmd *cmd, int script_fd);
|
||||
static int redirect_cmd_output(t_token *t, t_cmd *cmd);
|
||||
static int expand_redirection(t_token *t);
|
||||
|
||||
int redirections(t_token *t, t_cmd **pipeline)
|
||||
int redirections(t_token *t, t_cmd **pipeline, int script_fd)
|
||||
{
|
||||
int i;
|
||||
|
||||
@@ -32,13 +32,12 @@ int redirections(t_token *t, t_cmd **pipeline)
|
||||
{
|
||||
if (t->id == '<' && !redirect_cmd_input(t, pipeline[i]))
|
||||
return (0);
|
||||
else if (t->id == T_DLESS && !redirect_cmd_heredoc(t, pipeline[i]))
|
||||
else if (t->id == T_DLESS
|
||||
&& !redirect_cmd_heredoc(t, pipeline[i], script_fd))
|
||||
return (0);
|
||||
else if (t->id == '>' || t->id == T_DGREAT)
|
||||
{
|
||||
if (!redirect_cmd_output(t, pipeline[i]))
|
||||
return (0);
|
||||
}
|
||||
}
|
||||
t = t->next;
|
||||
}
|
||||
@@ -64,12 +63,12 @@ static int redirect_cmd_input(t_token *t, t_cmd *cmd)
|
||||
return (1);
|
||||
}
|
||||
|
||||
static int redirect_cmd_heredoc(t_token *t, t_cmd *cmd)
|
||||
static int redirect_cmd_heredoc(t_token *t, t_cmd *cmd, int script_fd)
|
||||
{
|
||||
if (cmd->fd_in != STDIN_FILENO)
|
||||
if (close(cmd->fd_in) == -1)
|
||||
perror("close()");
|
||||
cmd->fd_in = here_doc(t->next->content);
|
||||
cmd->fd_in = here_doc(t->next->content, script_fd);
|
||||
if (cmd->fd_in == -1)
|
||||
{
|
||||
shell_error("heredoc error", ": ", "", 0);
|
||||
|
||||
Reference in New Issue
Block a user