merge conflicts

This commit is contained in:
hugogogo
2021-12-19 12:12:43 +01:00
7 changed files with 126 additions and 66 deletions

View File

@@ -6,25 +6,22 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/11 18:46:43 by lperrey #+# #+# */
/* Updated: 2021/12/17 22:09:51 by hulamy ### ########.fr */
/* Updated: 2021/12/19 12:12:27 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
#define TMP_HERE_DOC "/tmp/minishell_here_doc"
static int here_doc_write(char *delimiter, int doc_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 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)
{
// https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_07_04
// https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_02
/* If any part of word is quoted,
the delimiter shall be formed by performing quote removal on word,
and the here-document lines shall not be expanded.
Otherwise, the delimiter shall be the word itself. */
// TODO : A voir si on fait les expansions de variables dans le here_doc.
int here_doc;
int ret;
@@ -52,64 +49,113 @@ int here_doc(char *delimiter)
return (here_doc);
}
int void_func_return_readline(void)
{
return (0);
}
// rl_done
// Setting this to a non-zero value causes Readline to return the current line
// immediately
// rl_event_hook
// If non-zero, this is the address of a function to call periodically when Readline
// is waiting for terminal input. By default, this will be called at most ten times
// a second if there is no keyboard input
// from stackoverflow (https://stackoverflow.com/questions/53165704/readline-c-force-return-of-certain-text-in-readline)
// "rl_done is only checked in the event loop. When you give it a null event hook
// function, it checks the rl_done and exits"
//
// rl_signal_event_hook
// If non-zero, this is the address of a function to call if a read system call is
// interrupted when Readline is reading terminal input.
static int here_doc_write(char *delimiter, int doc_fd)
{
char *line;
size_t line_count;
int ret;
struct sigaction signal_action;
ft_bzero(&signal_action, sizeof(signal_action));
signal_action.sa_handler = sigint_handler_heredoc;
sigaction(SIGINT, &signal_action, NULL);
if (isatty(STDIN_FILENO))
{
ft_bzero(&signal_action, sizeof(signal_action));
signal_action.sa_handler = sigint_handler_heredoc;
sigaction(SIGINT, &signal_action, NULL);
rl_event_hook = event_hook_empty;
ret = here_doc_write_interactive(delimiter, doc_fd);
rl_event_hook = NULL;
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;
line = NULL;
g_switch_heredoc_sigint = 0;
rl_event_hook = void_func_return_readline;
line_count = 0;
while (1)
{
line_count++;
line = NULL;
line = readline("> ");
if (g_switch_heredoc_sigint == 1)
return (set_last_exit_status(EXIT_SIGNAL + SIGINT));
if (!line)
{ // TODO : error print wrapper
ft_putstr_fd("minishell: ", 2);
ft_putstr_fd("warning: here-document at line ", 2);
ft_putnbr_fd(line_count, 2);
ft_putstr_fd(" delimited by end-of-file (wanted `", 2);
ft_putstr_fd(delimiter, 2);
ft_putstr_fd("')\n", 2);
return (0);
}
return (shell_error(WARNING_EOF, delimiter, "')", 0));
if (ft_strncmp(line, delimiter, ft_strlen(line) + 1) == 0) // Ou ft_strlen(delimiter) + 1 ? Ça devrais être identique et ça peux se calculer une seul fois.
break ;
if (write(doc_fd, line, ft_strlen(line)) == -1)
return (ft_reti_perror_free(-1, line, free, "write() "TMP_HERE_DOC));
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));
free(line);
return (ft_reti_perror_free(-1, line, free, "write "TMP_HERE_DOC));
ft_free_null(&line);
}
free(line);
signal_action.sa_handler = SIG_IGN;
sigaction(SIGINT, &signal_action, NULL);
return (0);
}
static int here_doc_write_script(char *delimiter, int doc_fd)
{
char *line;
int ret;
line = NULL;
ret = 1;
while (ret)
{
ret = gnl(STDIN_FILENO, &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) // Ou ft_strlen(delimiter) + 1 ? Ça devrais être identique et ça peux se calculer une seul fois.
break ;
if (write(doc_fd, line, ft_strlen(line)) == -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));
ft_free_null(&line);
}
if (!line && ret == 0)
shell_error(WARNING_EOF, delimiter, "')", 0);
free(line);
return (0);
}
static int event_hook_empty(void)
{
return (0);
}
/*
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
#tag_18_07_04
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
#tag_18_02
*/
/* If any part of word is quoted,
the delimiter shall be formed by performing quote removal on word,
and the here-document lines shall not be expanded.
Otherwise, the delimiter shall be the word itself. */
// TODO Bonus: expansions in here_doc.
/*
** rl_done
** Setting this to a non-zero value causes Readline
** to return the current line immediately.
** rl_event_hook
** If non-zero, this is the address of a function to call periodically
** when Readline is waiting for terminal input.
** By default, this will be called at most
** ten times a second if there is no keyboard input.
**
** https://stackoverflow.com/questions/53165704/
** readline-c-force-return-of-certain-text-in-readline
** "rl_done is only checked in the event loop. When you give it
** a null event hook function, it checks the rl_done and exits"
**
** rl_signal_event_hook
** If non-zero, this is the address of a function to call if a read
** system call is interrupted when Readline is reading terminal input.
*/