diff --git a/Makefile b/Makefile index 30038f2..19fb75c 100644 --- a/Makefile +++ b/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 \ diff --git a/src/executing/here_doc/here_doc.c b/src/executing/here_doc/here_doc.c index 78ba367..21718b1 100644 --- a/src/executing/here_doc/here_doc.c +++ b/src/executing/here_doc/here_doc.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/05 11:03:08 by kcolin #+# #+# */ -/* Updated: 2025/05/05 11:40:04 by kcolin ### ########.fr */ +/* Updated: 2025/05/05 12:03:42 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,13 +18,14 @@ #include "../../ft_errno.h" #include "../../sig/sig.h" #include "here_doc_utils.h" +#include "tcattr.h" #include #include char *expand_line(char *line, t_minishell *app); char *failed_to_open_tmp_file(void); -static char *tcgetatr_fail(void) +static char *tcgetattr_fail(void) { return (ft_errno(FT_EHEREDOC_FAILED), perror("minishell: tcgetattr"), NULL); @@ -44,7 +45,7 @@ static char *setup_term_and_signal(int outfd, char *filename, int infd, set_here_doc_mode_sig_handling(); term_new = term_save; term_new.c_lflag = term_new.c_lflag & ~ECHOCTL; - if (tcsetattr(infd, TCSANOW, &term_new) < 0) + if (tcset(infd, TCSANOW, &term_new) < 0) return (perror("minishell: tcsetattr"), interupted(outfd, filename, infd, &term_save)); return (filename); @@ -57,8 +58,8 @@ char *here_doc(t_worddesc *marker, int infd, t_minishell *app) char *line; struct termios term_save; - if (tcgetattr(infd, &term_save) < 0) - return (tcgetatr_fail()); + 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()); diff --git a/src/executing/here_doc/here_doc_utils.c b/src/executing/here_doc/here_doc_utils.c index dcaa37c..279ac79 100644 --- a/src/executing/here_doc/here_doc_utils.c +++ b/src/executing/here_doc/here_doc_utils.c @@ -16,6 +16,7 @@ #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) { @@ -44,7 +45,7 @@ char *finalize(int outfd, char *filename, int infd, struct termios *term_save) { close(outfd); - if (tcsetattr(infd, TCSADRAIN, term_save) < 0) + if (tcset(infd, TCSADRAIN, term_save) < 0) { perror("minishell: tcsetattr"); ft_errno(FT_EHEREDOC_FAILED); @@ -62,7 +63,7 @@ char *interupted(int outfd, char *filename, int infd, unlink(filename); free(filename); g_signum = 0; - if (tcsetattr(infd, TCSADRAIN, term_save) < 0) + if (tcset(infd, TCSADRAIN, term_save) < 0) { perror("minishell: tcsetattr"); free(filename); diff --git a/src/executing/here_doc/tcattr.c b/src/executing/here_doc/tcattr.c new file mode 100644 index 0000000..85c31fb --- /dev/null +++ b/src/executing/here_doc/tcattr.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tcattr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kcolin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/05/05 11:58:14 by kcolin #+# #+# */ +/* Updated: 2025/05/05 12:08:40 by kcolin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "tcattr.h" +#include + +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); +} diff --git a/src/executing/here_doc/tcattr.h b/src/executing/here_doc/tcattr.h new file mode 100644 index 0000000..abae605 --- /dev/null +++ b/src/executing/here_doc/tcattr.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tcattr.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kcolin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 + +int tcset(int infd, int optional_actions, const struct termios *termios_p); +int tcget(int infd, struct termios *termios_p); + +#endif // TCATTR_H