shell_script()

+ reset "rl_event_hook" to NULL after here_doc()
+ split init.c in multiple files
+ submodule minishell_tests
This commit is contained in:
LuckyLaszlo
2021-12-18 05:42:08 +01:00
parent fa71189132
commit afbb1cd2e0
11 changed files with 226 additions and 82 deletions

62
srcs/init/handle_argv.c Normal file
View File

@@ -0,0 +1,62 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* handle_argv.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 09:22:12 by lperrey #+# #+# */
/* Updated: 2021/12/18 04:46:05 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int remap_stdin_to_script_file(char *script_file);
char *init_prompt_base(void);
int handle_argv(t_all *c, char *argv[])
{
int ret;
if (argv[1])
{
ret = remap_stdin_to_script_file(argv[1]);
if (ret)
exit_free(c, ret);
}
else if (isatty(STDIN_FILENO))
{
c->prompt_base = init_prompt_base();
if (!c->prompt_base)
return (ft_reti_perror(0, "init_prompt_base()"));
c->prompt = init_prompt(c->prompt_base);
if (!c->prompt)
return (ft_reti_perror(0, "init_prompt()"));
}
return (1);
}
int remap_stdin_to_script_file(char *script_file)
{
int fd;
int ret;
int tmp;
fd = open(script_file, O_RDONLY);
if (fd == -1)
{
shell_perror(script_file, ": ", "", 0);
return (EXIT_CMD_NOT_FOUND);
}
ret = dup2(fd, STDIN_FILENO);
tmp = errno;
if (close(fd) == -1)
perror("close()");
if (ret == -1)
{
errno = tmp;
return (ft_reti_perror(EXIT_FAILURE, "dup2()"));
}
return (0);
}