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:
5
Makefile
5
Makefile
@@ -6,6 +6,7 @@ CFLAGS = -Wall -Wextra $(INCLUDES) -g # add -Werror, del -g
|
|||||||
|
|
||||||
VPATH = $(DIR_SRCS)
|
VPATH = $(DIR_SRCS)
|
||||||
DIR_SRCS = srcs \
|
DIR_SRCS = srcs \
|
||||||
|
srcs/init \
|
||||||
srcs/builtins \
|
srcs/builtins \
|
||||||
srcs/lexing \
|
srcs/lexing \
|
||||||
srcs/parsing srcs/parsing/valid_syntax \
|
srcs/parsing srcs/parsing/valid_syntax \
|
||||||
@@ -30,7 +31,9 @@ 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 retrieve_path.c free.c \
|
init.c init_prompt.c init_readline.c init_shlvl.c handle_argv.c \
|
||||||
|
retrieve_path.c \
|
||||||
|
free.c \
|
||||||
signals.c error_wrappers.c last_exit_status.c \
|
signals.c error_wrappers.c last_exit_status.c \
|
||||||
lexing.c fill_token.c check_operators.c \
|
lexing.c fill_token.c check_operators.c \
|
||||||
parsing.c create_pipeline.c \
|
parsing.c create_pipeline.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/16 06:35:04 by lperrey ### ########.fr */
|
/* Updated: 2021/12/17 21:16:53 by lperrey ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -17,7 +17,7 @@ int g_switch_heredoc_sigint;
|
|||||||
extern char **environ;
|
extern char **environ;
|
||||||
|
|
||||||
// Init
|
// Init
|
||||||
int init(t_all *c);
|
int init(t_all *c, char *argv[]);
|
||||||
char *init_prompt(char *prompt_base);
|
char *init_prompt(char *prompt_base);
|
||||||
char **retrieve_path(void);
|
char **retrieve_path(void);
|
||||||
void set_signals_behaviour(void);
|
void set_signals_behaviour(void);
|
||||||
|
|||||||
Submodule minishell_tests updated: 427cec8cb4...21c56c3985
62
srcs/init/handle_argv.c
Normal file
62
srcs/init/handle_argv.c
Normal 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);
|
||||||
|
}
|
||||||
33
srcs/init/init.c
Normal file
33
srcs/init/init.c
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* init.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2021/10/08 09:22:12 by lperrey #+# #+# */
|
||||||
|
/* Updated: 2021/12/18 04:53:11 by lperrey ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
void init_readline(void);
|
||||||
|
int init_shlvl(void);
|
||||||
|
int handle_argv(t_all *c, char *argv[]);
|
||||||
|
|
||||||
|
int init(t_all *c, char *argv[])
|
||||||
|
{
|
||||||
|
ft_bzero(c, sizeof (*c));
|
||||||
|
init_readline();
|
||||||
|
environ = ft_dup_2d_arr(environ, (t_dup_f)ft_strdup);
|
||||||
|
if (!environ)
|
||||||
|
return (ft_reti_perror(0, "ft_dup_2d_arr(environ)"));
|
||||||
|
c->path = retrieve_path();
|
||||||
|
if (!init_shlvl())
|
||||||
|
return (ft_reti_perror(0, "init_shlvl()"));
|
||||||
|
if (!handle_argv(c, argv))
|
||||||
|
return (0);
|
||||||
|
set_signals_behaviour();
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
@@ -1,69 +1,18 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* init.c :+: :+: :+: */
|
/* init_prompt.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* 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/08 21:54:36 by lperrey ### ########.fr */
|
/* Updated: 2021/12/18 04:28:46 by lperrey ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
static char *init_prompt_base(void);
|
char *init_prompt_base(void)
|
||||||
static int init_readline_hook(void);
|
|
||||||
static int shlvl_init(void);
|
|
||||||
|
|
||||||
int init(t_all *c)
|
|
||||||
{
|
|
||||||
ft_bzero(c, sizeof (*c));
|
|
||||||
rl_outstream = stderr;
|
|
||||||
rl_startup_hook = init_readline_hook;
|
|
||||||
readline(NULL);
|
|
||||||
rl_startup_hook = NULL;
|
|
||||||
environ = ft_dup_2d_arr(environ, (t_dup_f)ft_strdup);
|
|
||||||
if (!environ)
|
|
||||||
return (ft_reti_perror(0, "ft_dup_2d_arr(environ)"));
|
|
||||||
c->path = retrieve_path();
|
|
||||||
if (!shlvl_init())
|
|
||||||
return (ft_reti_perror(0, "shlvl_init()"));
|
|
||||||
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()"));
|
|
||||||
set_signals_behaviour();
|
|
||||||
return (1);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int shlvl_init(void)
|
|
||||||
{
|
|
||||||
char *tmp;
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
tmp = getenv("SHLVL");
|
|
||||||
if (tmp && ft_isdigit_str(tmp))
|
|
||||||
{
|
|
||||||
tmp = ft_itoa(ft_atoi(tmp) + 1);
|
|
||||||
if (!tmp)
|
|
||||||
return (0);
|
|
||||||
tmp = ft_strjoinfree_s2("SHLVL=", tmp);
|
|
||||||
if (!tmp)
|
|
||||||
return (0);
|
|
||||||
ret = export_var(tmp);
|
|
||||||
free(tmp);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
ret = export_var("SHLVL=1");
|
|
||||||
if (ret == -1)
|
|
||||||
return (0);
|
|
||||||
return (1);
|
|
||||||
}
|
|
||||||
|
|
||||||
static char *init_prompt_base(void)
|
|
||||||
{
|
{
|
||||||
char *prompt_base;
|
char *prompt_base;
|
||||||
char *tmp;
|
char *tmp;
|
||||||
@@ -103,13 +52,3 @@ char *init_prompt(char *prompt_base)
|
|||||||
return (NULL);
|
return (NULL);
|
||||||
return (prompt);
|
return (prompt);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
** set rl_startup_hook with this, for init COLUMNS and LINES variables
|
|
||||||
** and prevent leak/double_free with **environ
|
|
||||||
*/
|
|
||||||
static int init_readline_hook(void)
|
|
||||||
{
|
|
||||||
rl_done = 1;
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
33
srcs/init/init_readline.c
Normal file
33
srcs/init/init_readline.c
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* init_readline.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2021/10/08 09:22:12 by lperrey #+# #+# */
|
||||||
|
/* Updated: 2021/12/18 04:31:59 by lperrey ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
static int init_readline_hook(void);
|
||||||
|
|
||||||
|
void init_readline(void)
|
||||||
|
{
|
||||||
|
rl_outstream = stderr;
|
||||||
|
rl_startup_hook = init_readline_hook;
|
||||||
|
readline(NULL);
|
||||||
|
rl_startup_hook = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** set rl_startup_hook with this, for init COLUMNS and LINES variables
|
||||||
|
** and prevent leak/double_free with **environ
|
||||||
|
*/
|
||||||
|
static int init_readline_hook(void)
|
||||||
|
{
|
||||||
|
rl_done = 1;
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
37
srcs/init/init_shlvl.c
Normal file
37
srcs/init/init_shlvl.c
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* init_shlvl.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2021/10/08 09:22:12 by lperrey #+# #+# */
|
||||||
|
/* Updated: 2021/12/18 04:35:33 by lperrey ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
int init_shlvl(void)
|
||||||
|
{
|
||||||
|
char *tmp;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
tmp = getenv("SHLVL");
|
||||||
|
if (tmp && ft_isdigit_str(tmp))
|
||||||
|
{
|
||||||
|
tmp = ft_itoa(ft_atoi(tmp) + 1);
|
||||||
|
if (!tmp)
|
||||||
|
return (0);
|
||||||
|
tmp = ft_strjoinfree_s2("SHLVL=", tmp);
|
||||||
|
if (!tmp)
|
||||||
|
return (0);
|
||||||
|
ret = export_var(tmp);
|
||||||
|
free(tmp);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
ret = export_var("SHLVL=1");
|
||||||
|
if (ret == -1)
|
||||||
|
return (0);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
12
srcs/main.c
12
srcs/main.c
@@ -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/11/29 12:43:34 by lperrey ### ########.fr */
|
/* Updated: 2021/12/18 05:08:48 by lperrey ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -17,13 +17,11 @@ int main(int argc, char *argv[])
|
|||||||
t_all c;
|
t_all c;
|
||||||
|
|
||||||
(void)argc;
|
(void)argc;
|
||||||
(void)argv;
|
if (!init(&c, argv))
|
||||||
if (!init(&c))
|
|
||||||
exit_free(&c, EXIT_FAILURE);
|
exit_free(&c, EXIT_FAILURE);
|
||||||
//putenv("VAR=W1 W2 W3"); // TEMP TEST
|
if (!isatty(STDIN_FILENO))
|
||||||
// if (argv[1] || !isatty(STDIN_FILENO))
|
shell_script(&c);
|
||||||
// shell_script(&c);
|
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/04 05:59:26 by lperrey #+# #+# */
|
/* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */
|
||||||
/* Updated: 2021/12/01 14:45:04 by lperrey ### ########.fr */
|
/* Updated: 2021/12/18 03:24:54 by lperrey ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -21,8 +21,7 @@ void shell_loop(t_all *c)
|
|||||||
line_input = NULL;
|
line_input = NULL;
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
if (line_input)
|
free(line_input);
|
||||||
free(line_input);
|
|
||||||
line_input = read_input(c->prompt, c);
|
line_input = read_input(c->prompt, c);
|
||||||
if (line_input && *line_input)
|
if (line_input && *line_input)
|
||||||
{
|
{
|
||||||
@@ -48,6 +47,7 @@ static char *read_input(char *prompt, t_all *c)
|
|||||||
ft_bzero(&signal_behaviour, sizeof signal_behaviour);
|
ft_bzero(&signal_behaviour, sizeof signal_behaviour);
|
||||||
signal_behaviour.sa_handler = sigint_handler_interactive;
|
signal_behaviour.sa_handler = sigint_handler_interactive;
|
||||||
sigaction(SIGINT, &signal_behaviour, NULL);
|
sigaction(SIGINT, &signal_behaviour, NULL);
|
||||||
|
rl_event_hook = NULL;
|
||||||
line_input = readline(prompt);
|
line_input = readline(prompt);
|
||||||
signal_behaviour.sa_handler = SIG_IGN;
|
signal_behaviour.sa_handler = SIG_IGN;
|
||||||
sigaction(SIGINT, &signal_behaviour, NULL);
|
sigaction(SIGINT, &signal_behaviour, NULL);
|
||||||
|
|||||||
@@ -6,14 +6,53 @@
|
|||||||
/* 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/11/29 12:43:44 by lperrey ### ########.fr */
|
/* Updated: 2021/12/18 05:29:27 by lperrey ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
void shell_script(t_all *c) // WIP
|
static char *read_input_script(t_all *c);
|
||||||
|
|
||||||
|
void shell_script(t_all *c)
|
||||||
{
|
{
|
||||||
ft_putstr_fd("Shell Script Placeholder\n", 1);
|
char *line_input;
|
||||||
exit_free(c, EXIT_SUCCESS);
|
|
||||||
|
line_input = NULL;
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
free(line_input);
|
||||||
|
line_input = read_input_script(c);
|
||||||
|
if (line_input && *line_input)
|
||||||
|
{
|
||||||
|
c->token_list = lexing(line_input);
|
||||||
|
ft_free_null(&line_input);
|
||||||
|
if (!c->token_list)
|
||||||
|
break ;
|
||||||
|
c->pipeline = parsing(c->token_list);
|
||||||
|
ft_lstclear((t_list **)&c->token_list, free);
|
||||||
|
if (!c->pipeline)
|
||||||
|
break ;
|
||||||
|
exec_cmd_line(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
free(line_input);
|
||||||
|
exit_free(c, get_last_exit_status());
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *read_input_script(t_all *c)
|
||||||
|
{
|
||||||
|
char *line_input;
|
||||||
|
struct sigaction signal_behaviour;
|
||||||
|
|
||||||
|
ft_bzero(&signal_behaviour, sizeof signal_behaviour);
|
||||||
|
signal_behaviour.sa_handler = sigint_handler_interactive;
|
||||||
|
sigaction(SIGINT, &signal_behaviour, NULL);
|
||||||
|
rl_event_hook = NULL;
|
||||||
|
line_input = readline(NULL);
|
||||||
|
signal_behaviour.sa_handler = SIG_IGN;
|
||||||
|
sigaction(SIGINT, &signal_behaviour, NULL);
|
||||||
|
if (!line_input)
|
||||||
|
exit_free(c, get_last_exit_status());
|
||||||
|
return (line_input);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user