mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-05 23:18:08 +01:00
Compare commits
4 commits
083a2bb2a2
...
dd94aba23b
| Author | SHA1 | Date | |
|---|---|---|---|
| dd94aba23b | |||
| 4e7c9f4da9 | |||
|
|
7cd8d981e2 | ||
| b7686df5a8 |
13 changed files with 158 additions and 34 deletions
1
Makefile
1
Makefile
|
|
@ -39,6 +39,7 @@ srcs = \
|
|||
src/executing/here_doc/here_doc_utils.c \
|
||||
src/executing/here_doc/random_filename.c \
|
||||
src/executing/here_doc/strip_newline.c \
|
||||
src/executing/here_doc/tcattr.c \
|
||||
src/executing/simple_cmd/builtin_cd.c \
|
||||
src/executing/simple_cmd/builtin_echo.c \
|
||||
src/executing/simple_cmd/builtin_env.c \
|
||||
|
|
|
|||
|
|
@ -3,10 +3,10 @@
|
|||
/* ::: :::::::: */
|
||||
/* here_doc.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
|
||||
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/07 11:42:29 by kcolin #+# #+# */
|
||||
/* Updated: 2025/05/02 12:47:33 by jguelen ### ########.fr */
|
||||
/* Created: 2025/05/05 11:03:08 by kcolin #+# #+# */
|
||||
/* Updated: 2025/05/05 12:03:42 by kcolin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -18,33 +18,64 @@
|
|||
#include "../../ft_errno.h"
|
||||
#include "../../sig/sig.h"
|
||||
#include "here_doc_utils.h"
|
||||
#include "tcattr.h"
|
||||
#include <termios.h>
|
||||
#include <stdio.h>
|
||||
|
||||
char *expand_line(char *line, t_minishell *app);
|
||||
void failed_to_open_tmp_file(void);
|
||||
char *failed_to_open_tmp_file(void);
|
||||
|
||||
static char *tcgetattr_fail(void)
|
||||
{
|
||||
return (ft_errno(FT_EHEREDOC_FAILED),
|
||||
perror("minishell: tcgetattr"), NULL);
|
||||
}
|
||||
|
||||
static void eof_warning(char *marker)
|
||||
{
|
||||
ft_dprintf(STDERR_FILENO, "minishell: warning: here-document delimited by \
|
||||
end-of-file (wanted `%s')\n", marker);
|
||||
}
|
||||
|
||||
static char *setup_term_and_signal(int outfd, char *filename, int infd,
|
||||
struct termios term_save)
|
||||
{
|
||||
struct termios term_new;
|
||||
|
||||
set_here_doc_mode_sig_handling();
|
||||
term_new = term_save;
|
||||
term_new.c_lflag = term_new.c_lflag & ~ECHOCTL;
|
||||
if (tcset(infd, TCSANOW, &term_new) < 0)
|
||||
return (perror("minishell: tcsetattr"),
|
||||
interupted(outfd, filename, infd, &term_save));
|
||||
return (filename);
|
||||
}
|
||||
|
||||
char *here_doc(t_worddesc *marker, int infd, t_minishell *app)
|
||||
{
|
||||
int outfd;
|
||||
char *filename;
|
||||
char *line;
|
||||
int outfd;
|
||||
char *filename;
|
||||
char *line;
|
||||
struct termios term_save;
|
||||
|
||||
if (tcget(infd, &term_save) < 0)
|
||||
return (tcgetattr_fail());
|
||||
outfd = setup_here_doc(marker, infd, &filename);
|
||||
if (outfd < 0)
|
||||
return (ft_errno(FT_EHEREDOC_FAILED), failed_to_open_tmp_file(), NULL);
|
||||
set_here_doc_mode_sig_handling();
|
||||
return (ft_errno(FT_EHEREDOC_FAILED), failed_to_open_tmp_file());
|
||||
if (setup_term_and_signal(outfd, filename, infd, term_save) == NULL)
|
||||
return (NULL);
|
||||
line = get_next_line(infd);
|
||||
while (line != NULL)
|
||||
{
|
||||
if (is_marker(line, marker))
|
||||
return (finalize(outfd, filename));
|
||||
return (finalize(outfd, filename, infd, &term_save));
|
||||
if (!(marker->flags & (W_QUOTED | W_DQUOTE)))
|
||||
line = expand_line(line, app);
|
||||
line = output_line_and_next(infd, outfd, line);
|
||||
}
|
||||
if (g_signum != 0)
|
||||
return (ft_errno(FT_EHEREDOC_FAILED), interupted(outfd, filename),
|
||||
NULL);
|
||||
ft_dprintf(STDERR_FILENO, "minishell: warning: here-document delimited by \
|
||||
end-of-file (wanted `%s')\n", marker->word);
|
||||
return (finalize(outfd, filename));
|
||||
return (interupted(outfd, filename, infd, &term_save));
|
||||
eof_warning(marker->word);
|
||||
return (finalize(outfd, filename, infd, &term_save));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/28 18:29:44 by kcolin #+# #+# */
|
||||
/* Updated: 2025/03/28 19:03:45 by kcolin ### ########.fr */
|
||||
/* Updated: 2025/05/05 11:24:45 by kcolin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -14,7 +14,8 @@
|
|||
#include "libft.h"
|
||||
#include "../../ft_errno.h"
|
||||
|
||||
void failed_to_open_tmp_file(void)
|
||||
char *failed_to_open_tmp_file(void)
|
||||
{
|
||||
ft_perror("minishell: cannot create temp file for here-document");
|
||||
return (NULL);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,18 +3,20 @@
|
|||
/* ::: :::::::: */
|
||||
/* here_doc_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
||||
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/05/02 14:36:33 by kcolin #+# #+# */
|
||||
/* Updated: 2025/05/02 14:36:33 by kcolin ### ########.fr */
|
||||
/* Updated: 2025/05/05 12:04:34 by jguelen ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "here_doc_utils.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../../ft_errno.h"
|
||||
#include "here_doc.h"
|
||||
#include "../../sig/sig.h"
|
||||
#include "tcattr.h"
|
||||
|
||||
int setup_here_doc(t_worddesc *marker, int infd, char **filename)
|
||||
{
|
||||
|
|
@ -39,21 +41,37 @@ char *output_line_and_next(int infd, int outfd, char *line)
|
|||
return (line);
|
||||
}
|
||||
|
||||
char *finalize(int outfd, char *filename)
|
||||
char *finalize(int outfd, char *filename, int infd,
|
||||
struct termios *term_save)
|
||||
{
|
||||
close(outfd);
|
||||
if (tcset(infd, TCSADRAIN, term_save) < 0)
|
||||
{
|
||||
perror("minishell: tcsetattr");
|
||||
ft_errno(FT_EHEREDOC_FAILED);
|
||||
free(filename);
|
||||
filename = NULL;
|
||||
}
|
||||
set_interactive_mode_sig_handling();
|
||||
return (filename);
|
||||
}
|
||||
|
||||
int interupted(int outfd, char *filename)
|
||||
char *interupted(int outfd, char *filename, int infd,
|
||||
struct termios *term_save)
|
||||
{
|
||||
close(outfd);
|
||||
unlink(filename);
|
||||
free(filename);
|
||||
g_signum = 0;
|
||||
if (tcset(infd, TCSADRAIN, term_save) < 0)
|
||||
{
|
||||
perror("minishell: tcsetattr");
|
||||
free(filename);
|
||||
filename = NULL;
|
||||
}
|
||||
ft_errno(FT_EHEREDOC_FAILED);
|
||||
set_interactive_mode_sig_handling();
|
||||
return (-1);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
bool is_marker(char *line, t_worddesc *marker)
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/17 11:49:52 by kcolin #+# #+# */
|
||||
/* Updated: 2025/04/29 16:37:56 by kcolin ### ########.fr */
|
||||
/* Updated: 2025/05/05 11:24:11 by kcolin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -14,11 +14,14 @@
|
|||
# define HERE_DOC_UTILS_H
|
||||
|
||||
# include "../../parser/worddesc/worddesc.h"
|
||||
# include <termios.h>
|
||||
|
||||
int setup_here_doc(t_worddesc *marker, int infd, char **filename);
|
||||
char *output_line_and_next(int infd, int outfd, char *line);
|
||||
char *finalize(int outfd, char *filename);
|
||||
int interupted(int outfd, char *filename);
|
||||
char *finalize(int outfd, char *filename, int infd,
|
||||
struct termios *term_save);
|
||||
char *interupted(int outfd, char *filename, int infd,
|
||||
struct termios *term_save);
|
||||
bool is_marker(char *line, t_worddesc *marker);
|
||||
|
||||
#endif // HERE_DOC_UTILS_H
|
||||
|
|
|
|||
28
src/executing/here_doc/tcattr.c
Normal file
28
src/executing/here_doc/tcattr.c
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* tcattr.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/05/05 11:58:14 by kcolin #+# #+# */
|
||||
/* Updated: 2025/05/05 12:08:40 by kcolin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "tcattr.h"
|
||||
#include <unistd.h>
|
||||
|
||||
int tcset(int infd, int optional_actions, const struct termios *termios_p)
|
||||
{
|
||||
if (isatty(infd))
|
||||
return (tcsetattr(infd, optional_actions, termios_p));
|
||||
return (0);
|
||||
}
|
||||
|
||||
int tcget(int infd, struct termios *termios_p)
|
||||
{
|
||||
if (isatty(infd))
|
||||
return (tcgetattr(infd, termios_p));
|
||||
return (1);
|
||||
}
|
||||
21
src/executing/here_doc/tcattr.h
Normal file
21
src/executing/here_doc/tcattr.h
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* tcattr.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/05/05 11:57:56 by kcolin #+# #+# */
|
||||
/* Updated: 2025/05/05 12:08:35 by kcolin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef TCATTR_H
|
||||
# define TCATTR_H
|
||||
|
||||
# include <termios.h>
|
||||
|
||||
int tcset(int infd, int optional_actions, const struct termios *termios_p);
|
||||
int tcget(int infd, struct termios *termios_p);
|
||||
|
||||
#endif // TCATTR_H
|
||||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/31 14:21:52 by kcolin #+# #+# */
|
||||
/* Updated: 2025/04/29 15:42:51 by kcolin ### ########.fr */
|
||||
/* Updated: 2025/05/05 15:28:40 by kcolin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -27,9 +27,9 @@ static char *get_current_dir(void)
|
|||
return (ft_errno(FT_EERRNO), NULL);
|
||||
while (getcwd(path, size) == NULL)
|
||||
{
|
||||
free(path);
|
||||
if (errno != ERANGE)
|
||||
return (ft_errno(FT_EERRNO), NULL);
|
||||
free(path);
|
||||
size *= 2;
|
||||
path = ft_calloc(size, sizeof(char));
|
||||
if (path == NULL)
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/05/02 11:53:53 by kcolin #+# #+# */
|
||||
/* Updated: 2025/05/02 13:10:24 by jguelen ### ########.fr */
|
||||
/* Updated: 2025/05/02 17:38:40 by jguelen ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/09 16:53:02 by kcolin #+# #+# */
|
||||
/* Updated: 2025/05/02 14:35:54 by kcolin ### ########.fr */
|
||||
/* Updated: 2025/05/05 11:22:40 by kcolin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -23,7 +23,8 @@ void redirect_destroy(t_redirect *redirect)
|
|||
{
|
||||
next = redirect->next;
|
||||
free(redirect->here_doc_eof);
|
||||
if (redirect->type == FT_HEREDOC && redirect->redirectee.filename != NULL
|
||||
if (redirect->type == FT_HEREDOC
|
||||
&& redirect->redirectee.filename != NULL
|
||||
&& redirect->redirectee.filename->word != NULL)
|
||||
unlink(redirect->redirectee.filename->word);
|
||||
worddesc_destroy(redirect->redirectee.filename);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/05/02 12:16:37 by kcolin #+# #+# */
|
||||
/* Updated: 2025/05/02 13:01:42 by jguelen ### ########.fr */
|
||||
/* Updated: 2025/05/05 12:03:56 by jguelen ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -57,7 +57,7 @@ int set_here_doc_mode_sig_handling(void)
|
|||
ft_bzero(&sig_act, sizeof(struct sigaction));
|
||||
if (sigemptyset(&sig_act.sa_mask) == -1)
|
||||
return (-1);
|
||||
sig_act.sa_sigaction = &sig_cmd;
|
||||
sig_act.sa_sigaction = &sig_heredoc;
|
||||
sig_act.sa_flags |= SA_SIGINFO;
|
||||
if (sigaction(SIGINT, &sig_act, NULL) == -1)
|
||||
return (-1);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/19 18:21:55 by jguelen #+# #+# */
|
||||
/* Updated: 2025/05/02 13:01:49 by jguelen ### ########.fr */
|
||||
/* Updated: 2025/05/05 12:03:51 by jguelen ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -28,5 +28,6 @@ extern int g_signum;
|
|||
int set_interactive_mode_sig_handling(void);
|
||||
int set_exec_mode_sig_handling(void);
|
||||
int set_here_doc_mode_sig_handling(void);
|
||||
void sig_heredoc(int signum, siginfo_t *siginfo, void *context);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/17 12:01:39 by kcolin #+# #+# */
|
||||
/* Updated: 2025/05/02 13:01:15 by jguelen ### ########.fr */
|
||||
/* Updated: 2025/05/05 12:03:03 by jguelen ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -52,3 +52,22 @@ void sig_cmd(int signum, siginfo_t *siginfo, void *context)
|
|||
}
|
||||
g_signum = signum;
|
||||
}
|
||||
|
||||
void sig_heredoc(int signum, siginfo_t *siginfo, void *context)
|
||||
{
|
||||
(void)context;
|
||||
(void)siginfo;
|
||||
if (signum == SIGQUIT)
|
||||
{
|
||||
ft_dprintf(STDERR_FILENO, "Quit (core dumped)\n");
|
||||
}
|
||||
else if (signum == SIGINT)
|
||||
{
|
||||
ft_dprintf(STDOUT_FILENO, "^C\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
ft_printf("\n");
|
||||
}
|
||||
g_signum = signum;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue