here_doc script_fd fix

+ line_len in here_doc
+ error message SIGQUIT
This commit is contained in:
lperrey
2021-12-22 15:57:02 +01:00
parent b51535cdbc
commit 523f560eab
9 changed files with 46 additions and 152 deletions

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 02:59:58 by lperrey #+# #+# */
/* Updated: 2021/12/20 21:51:07 by lperrey ### ########.fr */
/* Updated: 2021/12/22 13:50:04 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -30,7 +30,7 @@ void shell_script(t_all *c, int script_fd);
t_token *lexing(char *input);
// Parser
t_cmd **parsing(t_token *token_list);
t_cmd **parsing(t_token *token_list, int script_fd);
int valid_syntax(t_token *token_list);
int valid_token(t_token **token_list, enum e_token_id token_id);
int valid_command_separator(const t_token *token_list);
@@ -39,7 +39,7 @@ t_cmd **pipeline_alloc(size_t cmd_nbr);
int pipeline_fill_argv(t_token *token_list, t_cmd **pipeline);
int expansions(t_token *token_list, t_cmd **pipeline);
int token_expansions(t_token *t);
int redirections(t_token *token_list, t_cmd **pipeline);
int redirections(t_token *token_list, t_cmd **pipeline, int script_fd);
// Exec
int exec_cmd_line(t_all *c);

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */
/* Updated: 2021/12/20 14:23:40 by hulamy ### ########.fr */
/* Updated: 2021/12/22 15:03:31 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -23,11 +23,6 @@ static int handle_access_error(char *file_name);
* #tag_18_08_02
*/
/*
* TODO : faire des test sur la valeur de errno selon les cas (if directory, if
* pathname invalid, ...)
*/
int cmd_find_access(t_cmd *cmd, char *path[])
{
if (ft_strchr(cmd->argv[0], '/'))

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */
/* Updated: 2021/12/21 17:29:39 by lperrey ### ########.fr */
/* Updated: 2021/12/22 15:38:01 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -48,7 +48,9 @@ static void handle_signal_status(int wstatus)
signum = WTERMSIG(wstatus);
set_last_exit_status(EXIT_SIGNAL + signum);
if (signum == SIGINT)
write(STDIN_FILENO, "\n", 1);
write(STDOUT_FILENO, "\n", 1);
if (signum == SIGQUIT)
ft_putstr_fd("Quit (core dumped)\n", STDERR_FILENO);
}
static int handle_wait_error(void)

View File

@@ -1,106 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split_MODIF.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/13 07:08:40 by lperrey #+# #+# */
/* Updated: 2021/11/13 20:24:33 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
// FT_SPLIT LEGEREMENT REMANIER.
// A REMPLACER DANS LA LIBFT
#include "libft.h"
static size_t count_word(char const *s, char c);
static char **alloc_words(char const *s, char c, char **str_arr,
size_t words_count);
static void fill_arr(char const *s, char c, char **str_arr);
char **ft_split(char const *s, char c)
{
char **str_arr;
size_t words_count;
if (s == NULL)
return (NULL);
words_count = count_word(s, c);
str_arr = ft_calloc(words_count + 1, sizeof(char *));
if (!str_arr)
return (NULL);
if (!(alloc_words(s, c, str_arr, words_count)))
{
ft_free_2d_arr(str_arr);
return (NULL);
}
fill_arr(s, c, str_arr);
return (str_arr);
}
static size_t count_word(char const *s, char c)
{
unsigned int i;
size_t count;
count = 0;
i = 0;
while (s[i])
{
while (s[i] == c)
i++;
if (s[i])
count++;
while (s[i] && s[i] != c)
i++;
}
return (count);
}
static char **alloc_words(char const *s, char c, char **str_arr,
size_t words_count)
{
unsigned int i;
size_t len;
unsigned int arr_i;
i = 0;
arr_i = 0;
while (arr_i < words_count)
{
len = 0;
while (s[i] == c)
i++;
while (s[i] && s[i] != c)
{
len++;
i++;
}
str_arr[arr_i] = malloc(len + 1);
if (!str_arr[arr_i])
return (NULL);
arr_i++;
}
return (str_arr);
}
static void fill_arr(char const *s, char c, char **str_arr)
{
unsigned int i;
unsigned int arr_i;
unsigned int char_i;
i = 0;
arr_i = 0;
while (str_arr[arr_i])
{
while (s[i] == c)
i++;
char_i = 0;
while (s[i] && s[i] != c)
str_arr[arr_i][char_i++] = s[i++];
str_arr[arr_i][char_i] = '\0';
arr_i++;
}
}

View File

@@ -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));

View File

@@ -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,16 +69,13 @@ 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;
@@ -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));

View File

@@ -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,14 +32,13 @@ 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;
}
return (1);
@@ -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);

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */
/* Updated: 2021/12/18 03:24:54 by lperrey ### ########.fr */
/* Updated: 2021/12/22 13:56:38 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -30,7 +30,7 @@ void shell_loop(t_all *c)
ft_free_null(&line_input);
if (!c->token_list)
continue ;
c->pipeline = parsing(c->token_list);
c->pipeline = parsing(c->token_list, 0);
ft_lstclear((t_list **)&c->token_list, free);
if (!c->pipeline)
continue ;

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/26 23:47:44 by lperrey #+# #+# */
/* Updated: 2021/12/20 22:26:57 by lperrey ### ########.fr */
/* Updated: 2021/12/22 14:01:52 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -32,7 +32,7 @@ void shell_script(t_all *c, int script_fd)
ft_free_null(&line_input);
if (!c->token_list)
break ;
c->pipeline = parsing(c->token_list);
c->pipeline = parsing(c->token_list, script_fd);
ft_lstclear((t_list **)&c->token_list, free);
if (!c->pipeline)
break ;