signals and termios WIP

+ shell script placeholder
+ generics functions
+ valgrind add_history() supp
+ misc
This commit is contained in:
LuckyLaszlo
2021-10-30 16:39:24 +02:00
parent 58ef5a4a03
commit ab2aa509df
16 changed files with 302 additions and 73 deletions

View File

@@ -2,7 +2,7 @@ NAME = minishell
CC = clang
CFLAGS = -Wall -Wextra -Werror $(INCLUDES) -g
CFLAGS = -Wall -Wextra $(INCLUDES) -g # add -Werror, del -g
VPATH = $(DIR_SRCS)
DIR_SRCS = srcs srcs/builtins srcs/parsing
@@ -22,7 +22,8 @@ LIBFT_D = ./libft
LIBFT = $(LIBFT_D)/libft.a
SRCS = main.c init.c free.c generic.c \
shell_loop.c \
signals.c terminal.c \
shell_loop.c shell_script.c \
lexing.c \
parsing.c \
valid_syntax.c valid_pipeline.c valid_command.c valid_io_redirect.c \

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/03 19:14:46 by lperrey #+# #+# */
/* Updated: 2021/10/13 16:09:32 by lperrey ### ########.fr */
/* Updated: 2021/10/26 14:53:32 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,6 +15,7 @@
# include "libft.h"
# include <fcntl.h>
# include <unistd.h>
# include <sys/ioctl.h>
# include <stdlib.h>
# include <string.h>
# include <errno.h>
@@ -52,8 +53,8 @@
** <fcntl.h>: open()
** <unistd.h>: read(), write(), close(), fork(), getcwd(), chdir(),
** stat(), lstat(), fstat(), execve(), dup(), dup2(), pipe(),
** isatty(), ttyname(), ttyslot(), ioctl(),
** tcsetattr(), tcgetattr()
** isatty(), ttyname(), ttyslot(), tcsetattr(), tcgetattr()
** <sys/ioctl.h>: ioctl()
** <stdlib.h>: malloc(), free(), exit(), getenv()
** <string.h>: strerror(), define NULL, define size_t
** <errno.h>: define errno

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 02:59:58 by lperrey #+# #+# */
/* Updated: 2021/10/24 19:20:09 by lperrey ### ########.fr */
/* Updated: 2021/10/30 14:16:25 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,9 +15,15 @@
// Init
int init(t_all *c, char *envp[]);
int set_signals_handling(struct sigaction *ori_signal_behaviour,
struct sigaction *signal_behaviour);
int set_terminal_attributes(struct termios *ori_termios,
struct termios *interactive_termios,
int *termios_changed);
// Shell loop
// Shell modes
void shell_loop(t_all *c);
void shell_script(t_all *c);
// Lexer
t_token *input_to_tokens(char *input);
@@ -43,7 +49,8 @@ char *ft_strjoinfree_s2(const char *s1, char *s2);
void ft_lstprint(t_list *lst, int fd);
int ft_isinset_str(char *str, char *set);
size_t ft_2d_arrlen(void *ptr); // Replace ft_arrlen()
void *ft_dup_2d_arr(void *ptr);
char **ft_dup_2d_char_arr(char **ptr);
void *ft_resize_2d_arr(void *ptr, size_t add_nbr);
t_list *ft_lstbeforelast(t_list *lst);
#endif

View File

@@ -6,13 +6,16 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 02:35:52 by lperrey #+# #+# */
/* Updated: 2021/10/24 19:18:28 by lperrey ### ########.fr */
/* Updated: 2021/10/30 13:31:07 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MINISHELL_STRUCTS_H
# define MINISHELL_STRUCTS_H
struct s_all;
struct s_all *g_all;
enum e_token_id
{
T_TOKEN = 0,
@@ -31,7 +34,6 @@ typedef struct s_token
enum e_token_id id;
} t_token;
struct s_all;
typedef int (*t_builtin_ptr)(int,char **,struct s_all *);
typedef struct s_cmd
@@ -45,12 +47,17 @@ typedef struct s_cmd
typedef struct s_all
{
t_cmd **cmd_arr;
char **envp;
char *prompt_base;
char *prompt;
t_token *token_list;
int last_exit_status;
t_cmd **cmd_arr;
char **envp;
char *prompt_base;
char *prompt;
t_token *token_list;
int last_exit_status;
struct termios ori_termios;
struct termios interactive_termios;
int termios_changed;
struct sigaction ori_signal_behaviour;
struct sigaction signal_behaviour;
} t_all;
#endif

View File

@@ -15,8 +15,8 @@ https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18
------------------------------------------------------- */
%start program
%%
pipeline : command
| pipe_sequence '|' command
pipeline : command
| pipeline '|' command
;
command : cmd_prefix cmd_name cmd_suffix
| cmd_prefix cmd_name

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/10 23:53:17 by lperrey #+# #+# */
/* Updated: 2021/10/22 13:35:33 by lperrey ### ########.fr */
/* Updated: 2021/10/30 14:18:09 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -17,6 +17,9 @@ int free_exit(t_all *c, int exit_status)
free(c->prompt_base);
free(c->prompt);
ft_lstclear((t_list **)&c->token_list, free); // a voir avec Hugo, il y a un truc qui me semble superflu dans la fonction
exit(exit_status);
ft_free_2d_arr(c->envp);
if (c->termios_changed)
tcsetattr(STDIN_FILENO, TCSANOW, &c->ori_termios);
rl_clear_history();
exit(exit_status);
}

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 09:25:35 by lperrey #+# #+# */
/* Updated: 2021/10/23 15:19:07 by lperrey ### ########.fr */
/* Updated: 2021/10/30 12:30:10 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -85,7 +85,7 @@ size_t ft_2d_arrlen(void *ptr) // Replace ft_arrlen()
return (len);
}
void *ft_dup_2d_arr(void *ptr)
char **ft_dup_2d_char_arr(char **ptr)
{
unsigned int i;
char **arr;
@@ -98,7 +98,9 @@ void *ft_dup_2d_arr(void *ptr)
i = 0;
while (arr[i])
{
new_arr[i] = arr[i];
new_arr[i] = ft_strdup(arr[i]);
if (!new_arr[i])
return (ft_retp_free(NULL, new_arr, ft_free_2d_arr));
i++;
}
new_arr[i] = NULL;
@@ -122,3 +124,12 @@ void *ft_resize_2d_arr(void *ptr, size_t add_nbr)
free(arr);
return (new_arr);
}
t_list *ft_lstbeforelast(t_list *lst)
{
if (!lst || !lst->next)
return (NULL);
while (lst->next->next)
lst = lst->next;
return (lst);
}

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 09:22:12 by lperrey #+# #+# */
/* Updated: 2021/10/23 16:04:03 by lperrey ### ########.fr */
/* Updated: 2021/10/30 14:17:41 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -17,16 +17,24 @@ static char *init_prompt(char *prompt_base);
int init(t_all *c, char *envp[])
{
g_all = c;
ft_bzero(c, sizeof *c);
c->envp = ft_dup_2d_arr(envp);
c->envp = ft_dup_2d_char_arr(envp);
if (!c->envp)
return (ft_reti_perror(0, "ft_dup_2d_arr(envp) error"));
return (ft_reti_perror(0, "ft_dup_2d_char_arr(envp) error"));
c->prompt_base = init_prompt_base();
if (!c->prompt_base)
return (ft_reti_perror(0, "init_prompt_base() error"));
c->prompt = init_prompt(c->prompt_base);
if (!c->prompt)
return (ft_reti_perror(0, "init_prompt() error"));
set_signals_handling(&c->ori_signal_behaviour, &c->signal_behaviour);
if (isatty(STDIN_FILENO))
{
if (!set_terminal_attributes(&c->ori_termios, &c->interactive_termios,
&c->termios_changed))
return (ft_reti_perror(0, "set_terminal_attributes() error"));
}
return (1);
}

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/19 08:38:55 by lperrey #+# #+# */
/* Updated: 2021/10/30 12:43:05 by lperrey ### ########.fr */
/* Updated: 2021/10/30 14:19:42 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -29,15 +29,6 @@ enum e_fill_token_return
DELIMITE_TOKEN
};
t_list *ft_lstbeforelast(t_list *lst) // tmp
{
if (!lst || !lst->next)
return (NULL);
while (lst->next->next)
lst = lst->next;
return (lst);
}
t_token *input_to_tokens(char *input)
{
t_token *t_head;
@@ -50,7 +41,7 @@ t_token *input_to_tokens(char *input)
return (ft_retp_perror(NULL, "alloc_token() error"));
if (!tokenize_input(t_head, input, input_len))
return (ft_lstclear((t_list **)&t_head, free));
t_tmp = ft_lstbeforelast((t_list *)t_head);
t_tmp = (t_token *)ft_lstbeforelast((t_list *)t_head);
if (t_tmp && !t_tmp->next->id)
{
ft_lstdelone((t_list *)t_tmp->next, free);

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */
/* Updated: 2021/10/22 10:33:00 by lperrey ### ########.fr */
/* Updated: 2021/10/29 01:59:04 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -19,7 +19,10 @@ int main(int argc, char *argv[], char *envp[])
(void)argc;
(void)argv;
if (!init(&c, envp))
exit(EXIT_FAILURE);
shell_loop(&c);
free_exit(&c, EXIT_FAILURE);
if (isatty(STDIN_FILENO))
shell_loop(&c);
else
shell_script(&c);
return (0);
}

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/24 10:52:40 by lperrey #+# #+# */
/* Updated: 2021/10/24 19:19:08 by lperrey ### ########.fr */
/* Updated: 2021/10/30 15:33:27 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -68,7 +68,7 @@ t_cmd **parsing(t_token *token_list)
%start program
%%
pipeline : command
| pipe_sequence '|' command
| pipeline '|' command
;
command : cmd_prefix cmd_name cmd_suffix
| cmd_prefix cmd_name

View File

@@ -6,13 +6,15 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */
/* Updated: 2021/10/24 19:17:03 by lperrey ### ########.fr */
/* Updated: 2021/10/30 15:32:44 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static char **tokens_list_to_argv(t_token *t); // temp test
//static char **tokens_list_to_argv(t_token *t); // temp test
void sigint_handler(int signum); //tmp
void sigquit_aka_eof_handler(int signum); //tmp
void shell_loop(t_all *c)
{
@@ -29,10 +31,42 @@ void shell_loop(t_all *c)
add_history(line_input);
c->token_list = input_to_tokens(line_input);
// TEMP
// A faire aprés être sortie du mode interactif
// - Ignorer tout les signaux
// - Remettre ori_termios
c->signal_behaviour.sa_handler = SIG_IGN;
sigaction(SIGINT, &c->signal_behaviour, NULL);
sigaction(SIGQUIT, &c->signal_behaviour, NULL);
tcsetattr(STDIN_FILENO, TCSANOW, &c->ori_termios);
if (!fork())
{
char *arg_test[3];
arg_test[0] = ft_strdup("sleep");
arg_test[1] = ft_strdup("3");
arg_test[2] = NULL;
sigaction(SIGQUIT, &c->ori_signal_behaviour, NULL);
sigaction(SIGINT, &c->ori_signal_behaviour, NULL);
execve("/bin/sleep", arg_test, c->envp);
}
else
{
int wait_test;
wait(&wait_test);
c->signal_behaviour.sa_handler = sigint_handler;
sigaction(SIGINT, &c->signal_behaviour, NULL);
c->signal_behaviour.sa_handler = SIG_IGN;
sigaction(SIGQUIT, &c->signal_behaviour, NULL);
tcsetattr(STDIN_FILENO, TCSANOW, &c->interactive_termios);
}
// EXEC_PIPES_AND_CO()
// temp placeholder
if (ft_strncmp(c->token_list->content, "env", 4) == 0)
/* if (ft_strncmp(c->token_list->content, "env", 4) == 0)
builtin_env(0, NULL, c);
else if (ft_strncmp(c->token_list->content, "exit", 5) == 0)
builtin_exit(0, NULL, c);
@@ -48,12 +82,14 @@ void shell_loop(t_all *c)
ft_putstr_fd("TOKENS LIST :\n-----------\n", 1);
ft_lstprint((t_list *)c->token_list, 1);
ft_lstclear((t_list **)&c->token_list, free);
}
} */
}
else if (!line_input)
write(1, "\n", 1);
}
}
static char **tokens_list_to_argv(t_token *t) // temp test
/* static char **tokens_list_to_argv(t_token *t) // temp test
{
size_t i;
char **argv;
@@ -68,29 +104,4 @@ static char **tokens_list_to_argv(t_token *t) // temp test
t = t->next;
}
return (argv);
}
void wip_test()
{
char term_desc[2048];
char *term_type;
int term_width;
int term_height;
int ret;
term_type = getenv("TERM");
if (term_type == 0)
ft_putstr_fd("Specify a terminal type with `setenv TERM <yourtype>'.\n", 2);
ret = tgetent(term_desc, term_type);
if (ret < 0)
ft_putstr_fd("Could not access the termcap data base.\n", 2);
if (ret == 0)
ft_putstr_fd("Terminal type `%s' is not defined.\n", 2);
term_height = tgetnum ("li");
term_width = tgetnum ("co");
/* Extract information that termcap functions use. */
/* temp = tgetstr ("pc", BUFFADDR);
PC = temp ? *temp : 0;
BC = tgetstr ("le", BUFFADDR);
UP = tgetstr ("up", BUFFADDR); */
}
} */

19
srcs/shell_script.c Normal file
View File

@@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* shell_script.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/26 23:47:44 by lperrey #+# #+# */
/* Updated: 2021/10/30 15:01:13 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void shell_script(t_all *c) // WIP
{
ft_putstr_fd("Shell Script Placeholder\n", 1);
free_exit(c, EXIT_SUCCESS);
}

84
srcs/signals.c Normal file
View File

@@ -0,0 +1,84 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* signals.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/23 18:56:53 by lperrey #+# #+# */
/* Updated: 2021/10/30 14:28:08 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void sigint_handler(int signum)
{
// Comment virer LE ^D De l'affichage ? Il ne fait pas partie de "rl_line_buffer".
if (rl_line_buffer && *rl_line_buffer)
{
return ;
}
else
{
free_exit(g_all, g_all->last_exit_status);
}
return ;
}
/* void sigquit_aka_eof_handler(int signum)
{
//ft_putstr_fd("TESTS\n", 1);
ft_putstr_fd("\n", 1);
rl_replace_line("", 1);
rl_on_new_line();
rl_redisplay();
return ;
} */
int set_signals_handling(struct sigaction *ori_signal_behaviour,
struct sigaction *signal_behaviour)
{
ori_signal_behaviour->sa_handler = SIG_DFL;
/* ctrl-D exit the shell.
eof = ^D; */
signal_behaviour->sa_handler = sigint_handler;
sigaction(SIGINT, signal_behaviour, NULL);
/* ctrl-\ do nothing.
quit = ^\; */
signal_behaviour->sa_handler = SIG_IGN;
//signal_behaviour->sa_handler = sigquit_aka_eof_handler;
sigaction(SIGQUIT, signal_behaviour, NULL);
/*
** remap (^D to ^C) and (^C to ^D) in terminal
** ^D is now "SIGINT" (handle here)
** ^C is now EOF (handle in shell_loop())
*/
return (1);
}
/*
ctrl-C print a new prompt on a newline.
intr = ^C;
ctrl-D exit the shell.
eof = ^D;
ctrl-\ do nothing.
quit = ^\;
*/
/*
speed 38400 baud; rows 22; columns 90; line = 0;
erase = ^?; kill = ^U;
eol = M-^?; eol2 = M-^?;
swtch = M-^?; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V;
discard = ^O; min = 1; time = 0;
-parenb -parodd -cmspar cs8 hupcl -cstopb cread -clocal -crtscts
-ignbrk brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc ixany
imaxbel iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke
-flusho -extproc
*/

76
srcs/terminal.c Normal file
View File

@@ -0,0 +1,76 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* terminal.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/27 00:10:04 by lperrey #+# #+# */
/* Updated: 2021/10/30 14:17:16 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
#define CTRL_C 03
#define CTRL_D 04
#define CTRL_BACKSLASH 034
int set_terminal_attributes(struct termios *ori_termios,
struct termios *interactive_termios,
int *termios_changed)
{
tcgetattr(STDIN_FILENO, ori_termios);
*interactive_termios = *ori_termios;
interactive_termios->c_cc[VINTR] = CTRL_D;
interactive_termios->c_cc[VEOF] = CTRL_C;
//interactive_termios->c_cc[VQUIT] = CTRL_C;
//interactive_termios->c_cc[VEOF] = CTRL_BACKSLASH;
//interactive_termios->c_cc[VEOL] = CTRL_C;
*termios_changed = 1;
tcsetattr(STDIN_FILENO, TCSANOW, interactive_termios);
return (1);
}
//printf("STDIN_FILENO = %s\n ", ttyname(STDIN_FILENO));
//printf("STDOUT_FILENO = %s\n ", ttyname(STDOUT_FILENO));
//printf("STDERR_FILENO = %s\n ", ttyname(STDERR_FILENO));
//ft_putendl_fd(ttyname(STDIN_FILENO), 1);
//ft_putendl_fd(ttyname(STDOUT_FILENO), 1);
//ft_putendl_fd(ttyname(STDERR_FILENO), 1);
// ft_printf("BEFORE\n");
// ft_printf("i_io.c_cc[VEOF] = %i\ni_termios.c_cc[VINTR] = %i\n", (*interactive_termios)->c_cc[VEOF], (*interactive_termios)->c_cc[VINTR]);
// ft_printf("o_io.c_cc[VEOF] = %i\no_termios.c_cc[VINTR] = %i\n", (*ori_termios)->c_cc[VEOF], (*ori_termios)->c_cc[VINTR]);
// ft_printf("AFTER\n");
// ft_printf("i_io.c_cc[VEOF] = %i\ni_termios.c_cc[VINTR] = %i\n", (*interactive_termios)->c_cc[VEOF], (*interactive_termios)->c_cc[VINTR]);
// ft_printf("o_io.c_cc[VEOF] = %i\no_termios.c_cc[VINTR] = %i\n", (*ori_termios)->c_cc[VEOF], (*ori_termios)->c_cc[VINTR]);
void wip_test()
{
char term_desc[2048];
char *term_type;
int term_width;
int term_height;
int ret;
term_type = getenv("TERM");
if (term_type == 0)
ft_putstr_fd("Specify a terminal type with `setenv TERM <yourtype>'.\n", 2);
ret = tgetent(term_desc, term_type);
if (ret < 0)
ft_putstr_fd("Could not access the termcap data base.\n", 2);
if (ret == 0)
ft_putstr_fd("Terminal type `%s' is not defined.\n", 2);
term_height = tgetnum ("li");
term_width = tgetnum ("co");
/* Extract information that termcap functions use. */
/* temp = tgetstr ("pc", BUFFADDR);
PC = temp ? *temp : 0;
BC = tgetstr ("le", BUFFADDR);
UP = tgetstr ("up", BUFFADDR); */
}

View File

@@ -4,3 +4,10 @@
...
fun:readline
}
{
<readline history>
Memcheck:Leak
...
fun:add_history
}