fixed argv open().

previously wrongly dup2() to STDIN.
This commit is contained in:
lperrey
2021-12-20 22:52:41 +01:00
parent 6a9b7bae1b
commit 8ed97346f7
11 changed files with 78 additions and 87 deletions

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/04 19:31:19 by lperrey #+# #+# */
/* Updated: 2021/12/08 22:33:56 by lperrey ### ########.fr */
/* Updated: 2021/12/20 21:51:00 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -44,7 +44,7 @@ static int change_prompt(t_all *c)
{
char *tmp;
tmp = init_prompt(c->prompt_base);
tmp = update_prompt(c->prompt_base);
if (!tmp)
return (-1);
free(c->prompt);

View File

@@ -1,62 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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);
}

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 09:22:12 by lperrey #+# #+# */
/* Updated: 2021/12/18 14:22:20 by lperrey ### ########.fr */
/* Updated: 2021/12/20 22:26:05 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,7 +14,8 @@
void init_readline(void);
int init_shlvl(void);
int handle_argv(t_all *c, char *argv[]);
void open_script_file(t_all *c, char *argv[]);
int init_prompt(t_all *c, int script_fd);
int init(t_all *c, char *argv[])
{
@@ -27,7 +28,8 @@ int init(t_all *c, char *argv[])
c->path = retrieve_path();
if (!init_shlvl())
return (ft_reti_perror(0, "init_shlvl()"));
if (!handle_argv(c, argv))
return (0);
open_script_file(c, argv);
if (!init_prompt(c, c->script_fd))
return (ft_reti_perror(0, "init_prompt()"));
return (1);
}

View File

@@ -6,13 +6,29 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 09:22:12 by lperrey #+# #+# */
/* Updated: 2021/12/18 04:28:46 by lperrey ### ########.fr */
/* Updated: 2021/12/20 22:25:46 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
char *init_prompt_base(void)
static char *init_prompt_base(void);
int init_prompt(t_all *c, int script_fd)
{
if (!script_fd && isatty(STDIN_FILENO))
{
c->prompt_base = init_prompt_base();
if (!c->prompt_base)
return (0);
c->prompt = update_prompt(c->prompt_base);
if (!c->prompt)
return (0);
}
return (1);
}
static char *init_prompt_base(void)
{
char *prompt_base;
char *tmp;
@@ -40,7 +56,7 @@ char *init_prompt_base(void)
return (prompt_base);
}
char *init_prompt(char *prompt_base)
char *update_prompt(char *prompt_base)
{
char *prompt;

View File

@@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* open_script_file.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/20 22:22:29 by lperrey #+# #+# */
/* Updated: 2021/12/20 22:27:14 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void open_script_file(t_all *c, char *argv[])
{
if (argv[1])
{
c->script_fd = open(argv[1], O_RDONLY);
if (c->script_fd == -1)
{
shell_perror(argv[1], ": ", "", 0);
exit_free(c, EXIT_CMD_NOT_FOUND);
}
}
}

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */
/* Updated: 2021/12/20 15:07:31 by hulamy ### ########.fr */
/* Updated: 2021/12/20 22:26:14 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -21,8 +21,10 @@ int main(int argc, char *argv[])
(void)argc;
if (!init(&c, argv))
exit_free(&c, EXIT_FAILURE);
if (!isatty(STDIN_FILENO))
shell_script(&c);
if (c.script_fd)
shell_script(&c, c.script_fd);
else if (!isatty(STDIN_FILENO))
shell_script(&c, STDIN_FILENO);
else
shell_loop(&c);
return (0);

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/10 23:53:17 by lperrey #+# #+# */
/* Updated: 2021/12/20 18:06:15 by lperrey ### ########.fr */
/* Updated: 2021/12/20 22:47:26 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -20,7 +20,13 @@ int exit_free(t_all *c, int exit_status)
ft_free_2d_arr(environ);
ft_free_2d_arr(c->path);
free_pipeline(&c->pipeline);
if (!isatty(STDIN_FILENO))
if (c->script_fd)
{
gnl(c->script_fd, NULL, 1);
if (close(c->script_fd) == -1)
perror("close()");
}
else if (!isatty(STDIN_FILENO))
gnl(STDIN_FILENO, NULL, 1);
else
rl_clear_history();

View File

@@ -6,16 +6,16 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/26 23:47:44 by lperrey #+# #+# */
/* Updated: 2021/12/18 15:15:30 by lperrey ### ########.fr */
/* Updated: 2021/12/20 22:26:57 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static char *read_input_script(t_all *c);
static char *read_input_script(t_all *c, int script_fd);
static void exit_signal(t_all *c);
void shell_script(t_all *c)
void shell_script(t_all *c, int script_fd)
{
char *line_input;
@@ -23,7 +23,7 @@ void shell_script(t_all *c)
while (1)
{
free(line_input);
line_input = read_input_script(c);
line_input = read_input_script(c, script_fd);
if (!line_input)
break ;
if (line_input && *line_input)
@@ -44,7 +44,7 @@ void shell_script(t_all *c)
exit_free(c, get_last_exit_status());
}
static char *read_input_script(t_all *c)
static char *read_input_script(t_all *c, int script_fd)
{
char *line_input;
struct sigaction signal_behaviour;
@@ -54,7 +54,7 @@ static char *read_input_script(t_all *c)
signal_behaviour.sa_handler = SIG_IGN;
sigaction(SIGINT, &signal_behaviour, NULL);
line_input = NULL;
ret = gnl(STDIN_FILENO, &line_input, 0);
ret = gnl(script_fd, &line_input, 0);
if (ret == -1)
{
free(line_input);