fixed argv open().
previously wrongly dup2() to STDIN.
This commit is contained in:
2
Makefile
2
Makefile
@@ -31,7 +31,7 @@ LIBFT = $(LIBFT_D)/libft.a
|
|||||||
|
|
||||||
SRCS = main.c \
|
SRCS = main.c \
|
||||||
shell_loop.c shell_script.c \
|
shell_loop.c shell_script.c \
|
||||||
init.c init_prompt.c init_readline.c init_shlvl.c handle_argv.c \
|
init.c init_prompt.c init_readline.c init_shlvl.c open_script_file.c \
|
||||||
retrieve_path.c \
|
retrieve_path.c \
|
||||||
free.c \
|
free.c \
|
||||||
signals.c error_wrappers.c last_exit_status.c \
|
signals.c error_wrappers.c last_exit_status.c \
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/10/08 02:59:58 by lperrey #+# #+# */
|
/* Created: 2021/10/08 02:59:58 by lperrey #+# #+# */
|
||||||
/* Updated: 2021/12/20 18:06:36 by lperrey ### ########.fr */
|
/* Updated: 2021/12/20 21:51:07 by lperrey ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -18,13 +18,13 @@ extern char **environ;
|
|||||||
|
|
||||||
// Init
|
// Init
|
||||||
int init(t_all *c, char *argv[]);
|
int init(t_all *c, char *argv[]);
|
||||||
char *init_prompt(char *prompt_base);
|
char *update_prompt(char *prompt_base);
|
||||||
char **retrieve_path(void);
|
char **retrieve_path(void);
|
||||||
void set_signals_behaviour(void);
|
void set_signals_behaviour(void);
|
||||||
|
|
||||||
// Shell modes
|
// Shell modes
|
||||||
void shell_loop(t_all *c);
|
void shell_loop(t_all *c);
|
||||||
void shell_script(t_all *c);
|
void shell_script(t_all *c, int script_fd);
|
||||||
|
|
||||||
// Lexer
|
// Lexer
|
||||||
t_token *lexing(char *input);
|
t_token *lexing(char *input);
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/10/08 02:35:52 by lperrey #+# #+# */
|
/* Created: 2021/10/08 02:35:52 by lperrey #+# #+# */
|
||||||
/* Updated: 2021/12/20 17:24:45 by lperrey ### ########.fr */
|
/* Updated: 2021/12/20 22:13:45 by lperrey ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -42,6 +42,7 @@ typedef struct s_all
|
|||||||
char *prompt_base;
|
char *prompt_base;
|
||||||
char *prompt;
|
char *prompt;
|
||||||
t_token *token_list;
|
t_token *token_list;
|
||||||
|
int script_fd;
|
||||||
} t_all;
|
} t_all;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/12/04 19:31:19 by lperrey #+# #+# */
|
/* 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;
|
char *tmp;
|
||||||
|
|
||||||
tmp = init_prompt(c->prompt_base);
|
tmp = update_prompt(c->prompt_base);
|
||||||
if (!tmp)
|
if (!tmp)
|
||||||
return (-1);
|
return (-1);
|
||||||
free(c->prompt);
|
free(c->prompt);
|
||||||
|
|||||||
@@ -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);
|
|
||||||
}
|
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/10/08 09:22:12 by lperrey #+# #+# */
|
/* 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);
|
void init_readline(void);
|
||||||
int init_shlvl(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[])
|
int init(t_all *c, char *argv[])
|
||||||
{
|
{
|
||||||
@@ -27,7 +28,8 @@ int init(t_all *c, char *argv[])
|
|||||||
c->path = retrieve_path();
|
c->path = retrieve_path();
|
||||||
if (!init_shlvl())
|
if (!init_shlvl())
|
||||||
return (ft_reti_perror(0, "init_shlvl()"));
|
return (ft_reti_perror(0, "init_shlvl()"));
|
||||||
if (!handle_argv(c, argv))
|
open_script_file(c, argv);
|
||||||
return (0);
|
if (!init_prompt(c, c->script_fd))
|
||||||
|
return (ft_reti_perror(0, "init_prompt()"));
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,13 +6,29 @@
|
|||||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/10/08 09:22:12 by lperrey #+# #+# */
|
/* 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"
|
#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 *prompt_base;
|
||||||
char *tmp;
|
char *tmp;
|
||||||
@@ -40,7 +56,7 @@ char *init_prompt_base(void)
|
|||||||
return (prompt_base);
|
return (prompt_base);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *init_prompt(char *prompt_base)
|
char *update_prompt(char *prompt_base)
|
||||||
{
|
{
|
||||||
char *prompt;
|
char *prompt;
|
||||||
|
|
||||||
|
|||||||
26
srcs/init/open_script_file.c
Normal file
26
srcs/init/open_script_file.c
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */
|
/* 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;
|
(void)argc;
|
||||||
if (!init(&c, argv))
|
if (!init(&c, argv))
|
||||||
exit_free(&c, EXIT_FAILURE);
|
exit_free(&c, EXIT_FAILURE);
|
||||||
if (!isatty(STDIN_FILENO))
|
if (c.script_fd)
|
||||||
shell_script(&c);
|
shell_script(&c, c.script_fd);
|
||||||
|
else if (!isatty(STDIN_FILENO))
|
||||||
|
shell_script(&c, STDIN_FILENO);
|
||||||
else
|
else
|
||||||
shell_loop(&c);
|
shell_loop(&c);
|
||||||
return (0);
|
return (0);
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/10/10 23:53:17 by lperrey #+# #+# */
|
/* 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(environ);
|
||||||
ft_free_2d_arr(c->path);
|
ft_free_2d_arr(c->path);
|
||||||
free_pipeline(&c->pipeline);
|
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);
|
gnl(STDIN_FILENO, NULL, 1);
|
||||||
else
|
else
|
||||||
rl_clear_history();
|
rl_clear_history();
|
||||||
|
|||||||
@@ -6,16 +6,16 @@
|
|||||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/10/26 23:47:44 by lperrey #+# #+# */
|
/* 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"
|
#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);
|
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;
|
char *line_input;
|
||||||
|
|
||||||
@@ -23,7 +23,7 @@ void shell_script(t_all *c)
|
|||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
free(line_input);
|
free(line_input);
|
||||||
line_input = read_input_script(c);
|
line_input = read_input_script(c, script_fd);
|
||||||
if (!line_input)
|
if (!line_input)
|
||||||
break ;
|
break ;
|
||||||
if (line_input && *line_input)
|
if (line_input && *line_input)
|
||||||
@@ -44,7 +44,7 @@ void shell_script(t_all *c)
|
|||||||
exit_free(c, get_last_exit_status());
|
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;
|
char *line_input;
|
||||||
struct sigaction signal_behaviour;
|
struct sigaction signal_behaviour;
|
||||||
@@ -54,7 +54,7 @@ static char *read_input_script(t_all *c)
|
|||||||
signal_behaviour.sa_handler = SIG_IGN;
|
signal_behaviour.sa_handler = SIG_IGN;
|
||||||
sigaction(SIGINT, &signal_behaviour, NULL);
|
sigaction(SIGINT, &signal_behaviour, NULL);
|
||||||
line_input = NULL;
|
line_input = NULL;
|
||||||
ret = gnl(STDIN_FILENO, &line_input, 0);
|
ret = gnl(script_fd, &line_input, 0);
|
||||||
if (ret == -1)
|
if (ret == -1)
|
||||||
{
|
{
|
||||||
free(line_input);
|
free(line_input);
|
||||||
|
|||||||
Reference in New Issue
Block a user