merge conflict with master
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -13,3 +13,5 @@ memo
|
||||
memo.txt
|
||||
minishell
|
||||
*.zip
|
||||
arg_test
|
||||
osef_temp
|
||||
|
||||
8
Makefile
8
Makefile
@@ -5,7 +5,7 @@ CC = clang
|
||||
CFLAGS = -Wall -Wextra -Werror $(INCLUDES) -g
|
||||
|
||||
VPATH = $(DIR_SRCS)
|
||||
DIR_SRCS = srcs srcs/builtins
|
||||
DIR_SRCS = srcs srcs/builtins srcs/parsing
|
||||
|
||||
INCLUDES = -I$(HEADERS_D) -I$(LIBFT_D)
|
||||
|
||||
@@ -22,7 +22,11 @@ LIBFT_D = ./libft
|
||||
LIBFT = $(LIBFT_D)/libft.a
|
||||
|
||||
SRCS = main.c init.c free.c generic.c \
|
||||
env.c exit.c
|
||||
shell_loop.c \
|
||||
lexing.c \
|
||||
parsing.c \
|
||||
valid_syntax.c valid_pipeline.c valid_command.c valid_io_redirect.c \
|
||||
env.c exit.c echo.c
|
||||
|
||||
DIR_OBJS = builds
|
||||
OBJS = $(SRCS:%.c=$(DIR_OBJS)/%.o)
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/08 02:59:58 by lperrey #+# #+# */
|
||||
/* Updated: 2021/10/10 23:55:14 by lperrey ### ########.fr */
|
||||
/* Updated: 2021/10/24 19:20:09 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -16,9 +16,22 @@
|
||||
// Init
|
||||
int init(t_all *c, char *envp[]);
|
||||
|
||||
// Shell loop
|
||||
void shell_loop(t_all *c);
|
||||
|
||||
// Lexer
|
||||
t_token *input_to_tokens(char *input);
|
||||
|
||||
// Parser
|
||||
t_cmd **parsing(t_token *token_list);
|
||||
int valid_syntax(t_token *token_list);
|
||||
int valid_token(t_token **token_list, enum e_token_id token_id);
|
||||
int valid_command_separator(const t_token *token_list);
|
||||
|
||||
// Builtins
|
||||
int builtin_env(int argc, char *argv[], t_all *c);
|
||||
int builtin_exit(int argc, char *argv[], t_all *c);
|
||||
int builtin_echo(int argc, char *argv[], t_all *c);
|
||||
|
||||
// Free
|
||||
int free_exit(t_all *c, int exit_status);
|
||||
@@ -27,5 +40,10 @@ int free_exit(t_all *c, int exit_status);
|
||||
char *ft_strjoinfree(char *s1, char *s2);
|
||||
char *ft_strjoinfree_s1(char *s1, const char *s2);
|
||||
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);
|
||||
void *ft_resize_2d_arr(void *ptr, size_t add_nbr);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -6,18 +6,51 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/08 02:35:52 by lperrey #+# #+# */
|
||||
/* Updated: 2021/10/10 05:39:09 by lperrey ### ########.fr */
|
||||
/* Updated: 2021/10/24 19:18:28 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef MINISHELL_STRUCTS_H
|
||||
# define MINISHELL_STRUCTS_H
|
||||
|
||||
enum e_token_id
|
||||
{
|
||||
T_TOKEN = 0,
|
||||
T_LESS = '<',
|
||||
T_GREAT = '>',
|
||||
T_PIPE = '|',
|
||||
T_DLESS, //'<<'
|
||||
T_DGREAT, //'>>'
|
||||
T_WORD
|
||||
};
|
||||
|
||||
typedef struct s_token
|
||||
{
|
||||
char *content;
|
||||
struct s_token *next;
|
||||
enum e_token_id id;
|
||||
} t_token;
|
||||
|
||||
struct s_all;
|
||||
typedef int (*t_builtin_ptr)(int,char **,struct s_all *);
|
||||
|
||||
typedef struct s_cmd
|
||||
{
|
||||
char **argv;
|
||||
pid_t pid;
|
||||
t_builtin_ptr *builtin_command;
|
||||
int fd_in;
|
||||
int fd_out;
|
||||
} t_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_all;
|
||||
|
||||
#endif
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/10 03:36:37 by lperrey #+# #+# */
|
||||
/* Updated: 2021/10/10 20:58:28 by lperrey ### ########.fr */
|
||||
/* Updated: 2021/10/19 20:30:34 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
|
||||
# include "minishell_macro.h"
|
||||
|
||||
# define U_PROMPT_END PROMPT_EURO
|
||||
# define U_PROMPT_END PROMPT_CHEVRON
|
||||
# define U_DEFAULT_USER "NoUser"
|
||||
# define U_DEFAULT_NAME "NoName"
|
||||
|
||||
|
||||
102
parsing.txt
Normal file
102
parsing.txt
Normal file
@@ -0,0 +1,102 @@
|
||||
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_10_02
|
||||
|
||||
/* -------------------------------------------------------
|
||||
The grammar symbols
|
||||
------------------------------------------------------- */
|
||||
%token WORD
|
||||
%token LESS // '<'
|
||||
%token GREAT // '>'
|
||||
%token DLESS // '<<'
|
||||
%token DGREAT // '>>'
|
||||
%token PIPE // '|'
|
||||
|
||||
/* -------------------------------------------------------
|
||||
The Simplified Grammar
|
||||
------------------------------------------------------- */
|
||||
%start program
|
||||
%%
|
||||
pipeline : command
|
||||
| pipe_sequence '|' command
|
||||
;
|
||||
command : cmd_prefix cmd_name cmd_suffix
|
||||
| cmd_prefix cmd_name
|
||||
| cmd_name cmd_suffix
|
||||
| cmd_name
|
||||
;
|
||||
cmd_name : WORD // Apply rule 7a
|
||||
;
|
||||
cmd_prefix : io_redirect
|
||||
| cmd_prefix io_redirect
|
||||
;
|
||||
cmd_suffix : io_redirect
|
||||
| cmd_suffix io_redirect
|
||||
| WORD
|
||||
| cmd_suffix WORD
|
||||
;
|
||||
io_redirect : io_file
|
||||
| io_here
|
||||
;
|
||||
io_file : '<' filename
|
||||
| LESSAND filename
|
||||
| '>' filename
|
||||
| GREATAND filename
|
||||
| DGREAT filename
|
||||
| LESSGREAT filename
|
||||
| CLOBBER filename
|
||||
;
|
||||
filename : WORD // Apply rule 2
|
||||
;
|
||||
io_here : DLESS here_end
|
||||
;
|
||||
here_end : WORD // Apply rule 3
|
||||
;
|
||||
|
||||
|
||||
|
||||
/* -------------------------------------------------------
|
||||
The Grammar
|
||||
------------------------------------------------------- */
|
||||
%start program
|
||||
%%
|
||||
pipeline : pipe_sequence
|
||||
;
|
||||
pipe_sequence : command
|
||||
| pipe_sequence '|' command
|
||||
;
|
||||
command : simple_command
|
||||
;
|
||||
simple_command : cmd_prefix cmd_word cmd_suffix
|
||||
| cmd_prefix cmd_word
|
||||
| cmd_prefix
|
||||
| cmd_name cmd_suffix
|
||||
| cmd_name
|
||||
;
|
||||
cmd_name : WORD /* Apply rule 7a */
|
||||
;
|
||||
cmd_word : WORD /* Apply rule 7b */
|
||||
;
|
||||
cmd_prefix : io_redirect
|
||||
| cmd_prefix io_redirect
|
||||
;
|
||||
cmd_suffix : io_redirect
|
||||
| cmd_suffix io_redirect
|
||||
| WORD
|
||||
| cmd_suffix WORD
|
||||
;
|
||||
io_redirect : io_file
|
||||
| io_here
|
||||
;
|
||||
io_file : '<' filename
|
||||
| LESSAND filename
|
||||
| '>' filename
|
||||
| GREATAND filename
|
||||
| DGREAT filename
|
||||
| LESSGREAT filename
|
||||
| CLOBBER filename
|
||||
;
|
||||
filename : WORD /* Apply rule 2 */
|
||||
;
|
||||
io_here : DLESS here_end
|
||||
;
|
||||
here_end : WORD /* Apply rule 3 */
|
||||
;
|
||||
47
srcs/builtins/echo.c
Normal file
47
srcs/builtins/echo.c
Normal file
@@ -0,0 +1,47 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* echo.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/21 16:37:44 by lperrey #+# #+# */
|
||||
/* Updated: 2021/10/21 20:22:22 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
enum e_echo_options
|
||||
{
|
||||
BIT_N = 01,
|
||||
};
|
||||
|
||||
int builtin_echo(int argc, char *argv[], t_all *c)
|
||||
{
|
||||
int i;
|
||||
int options;
|
||||
|
||||
(void)argc;
|
||||
(void)c;
|
||||
options = 0;
|
||||
i = 1;
|
||||
while (argv[i] && argv[i][0] == '-')
|
||||
{
|
||||
if (ft_isinset_str(&argv[i][1], "n"))
|
||||
options = options | BIT_N;
|
||||
else
|
||||
break ;
|
||||
i++;
|
||||
}
|
||||
while (argv[i])
|
||||
{
|
||||
ft_putstr_fd(argv[i], 1);
|
||||
i++;
|
||||
if (argv[i])
|
||||
write(1, " ", 1);
|
||||
}
|
||||
if ((options & BIT_N) == 0)
|
||||
write(1, "\n", 1);
|
||||
return (0);
|
||||
}
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/10 05:01:22 by lperrey #+# #+# */
|
||||
/* Updated: 2021/10/11 01:50:53 by lperrey ### ########.fr */
|
||||
/* Updated: 2021/10/22 14:49:01 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -17,7 +17,7 @@ int builtin_exit(int argc, char *argv[], t_all *c) // WIP
|
||||
unsigned char status;
|
||||
int i;
|
||||
|
||||
status = 0;
|
||||
status = c->last_exit_status;
|
||||
if (argc > 2)
|
||||
return (ft_reti_print(1, "exit: too many arguments\n", 2));
|
||||
if (argc == 2)
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/10 23:53:17 by lperrey #+# #+# */
|
||||
/* Updated: 2021/10/10 23:59:25 by lperrey ### ########.fr */
|
||||
/* Updated: 2021/10/22 13:35:33 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -16,5 +16,7 @@ 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);
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/08 09:25:35 by lperrey #+# #+# */
|
||||
/* Updated: 2021/10/08 09:28:49 by lperrey ### ########.fr */
|
||||
/* Updated: 2021/10/23 15:19:07 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -39,3 +39,86 @@ char *ft_strjoinfree_s2(const char *s1, char *s2)
|
||||
free(s2);
|
||||
return (str);
|
||||
}
|
||||
|
||||
void ft_lstprint(t_list *lst, int fd)
|
||||
{
|
||||
while (lst)
|
||||
{
|
||||
ft_putendl_fd(lst->content, fd);
|
||||
lst = lst->next;
|
||||
}
|
||||
}
|
||||
|
||||
int ft_isinset_str(char *str, char *set)
|
||||
{
|
||||
size_t i;
|
||||
size_t i_set;
|
||||
int valid;
|
||||
|
||||
i = 0;
|
||||
while (str[i])
|
||||
{
|
||||
valid = 0;
|
||||
i_set = 0;
|
||||
while (set[i_set] && !valid)
|
||||
{
|
||||
if (str[i] == set[i_set])
|
||||
valid = 1;
|
||||
i_set++;
|
||||
}
|
||||
if (!valid)
|
||||
return (0);
|
||||
i++;
|
||||
}
|
||||
return (i);
|
||||
}
|
||||
|
||||
size_t ft_2d_arrlen(void *ptr) // Replace ft_arrlen()
|
||||
{
|
||||
size_t len;
|
||||
char **arr;
|
||||
|
||||
arr = (char **)ptr;
|
||||
len = 0;
|
||||
while (arr[len] != NULL)
|
||||
len++;
|
||||
return (len);
|
||||
}
|
||||
|
||||
void *ft_dup_2d_arr(void *ptr)
|
||||
{
|
||||
unsigned int i;
|
||||
char **arr;
|
||||
char **new_arr;
|
||||
|
||||
new_arr = malloc((ft_2d_arrlen(ptr) + 1) * sizeof (void *));
|
||||
if (!new_arr)
|
||||
return (NULL);
|
||||
arr = (char **)ptr;
|
||||
i = 0;
|
||||
while (arr[i])
|
||||
{
|
||||
new_arr[i] = arr[i];
|
||||
i++;
|
||||
}
|
||||
new_arr[i] = NULL;
|
||||
return (new_arr);
|
||||
}
|
||||
|
||||
void *ft_resize_2d_arr(void *ptr, size_t add_nbr)
|
||||
{
|
||||
unsigned int i;
|
||||
char **arr;
|
||||
char **new_arr;
|
||||
|
||||
new_arr = ft_calloc(ft_2d_arrlen(ptr) + add_nbr + 1, sizeof (void *));
|
||||
arr = (char **)ptr;
|
||||
i = 0;
|
||||
while (arr[i])
|
||||
{
|
||||
new_arr[i] = arr[i];
|
||||
i++;
|
||||
}
|
||||
free(arr);
|
||||
return (new_arr);
|
||||
}
|
||||
|
||||
10
srcs/init.c
10
srcs/init.c
@@ -6,7 +6,7 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/08 09:22:12 by lperrey #+# #+# */
|
||||
/* Updated: 2021/10/10 21:56:33 by lperrey ### ########.fr */
|
||||
/* Updated: 2021/10/23 16:04:03 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -18,13 +18,15 @@ static char *init_prompt(char *prompt_base);
|
||||
int init(t_all *c, char *envp[])
|
||||
{
|
||||
ft_bzero(c, sizeof *c);
|
||||
c->envp = envp;
|
||||
c->envp = ft_dup_2d_arr(envp);
|
||||
if (!c->envp)
|
||||
return (ft_reti_perror(0, "ft_dup_2d_arr(envp) error"));
|
||||
c->prompt_base = init_prompt_base();
|
||||
if (!c->prompt_base)
|
||||
return (ft_reti_perror(0, "init_prompt_base() fail"));
|
||||
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() fail"));
|
||||
return (ft_reti_perror(0, "init_prompt() error"));
|
||||
return (1);
|
||||
}
|
||||
|
||||
|
||||
193
srcs/lexing.c
Normal file
193
srcs/lexing.c
Normal file
@@ -0,0 +1,193 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* lexing.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/19 08:38:55 by lperrey #+# #+# */
|
||||
/* Updated: 2021/10/24 19:53:40 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
static t_token *alloc_token(size_t content_len);
|
||||
static int tokenize_input(t_token *t, char *input, size_t input_len);
|
||||
static int fill_token(t_token *t, char *input, int *i, int *t_i);
|
||||
static int check_operators(t_token *t, char *input, int *i, int *t_i);
|
||||
|
||||
enum e_in_quote_state
|
||||
{
|
||||
NOT_IN = 0,
|
||||
IN_QUOTES = '\'',
|
||||
IN_DQUOTES = '\"'
|
||||
};
|
||||
|
||||
enum e_fill_token_return
|
||||
{
|
||||
CONTINUE_TOKEN = 1,
|
||||
DELIMITE_TOKEN
|
||||
};
|
||||
|
||||
t_token *input_to_tokens(char *input)
|
||||
{
|
||||
t_token *t_head;
|
||||
size_t input_len;
|
||||
|
||||
input_len = ft_strlen(input);
|
||||
t_head = alloc_token(input_len);
|
||||
if (!t_head)
|
||||
return (ft_retp_perror(NULL, "alloc_token() error"));
|
||||
if (!tokenize_input(t_head, input, input_len))
|
||||
return (ft_lstclear((t_list **)&t_head, free));
|
||||
return (t_head);
|
||||
}
|
||||
|
||||
// TODO : Fix final space saved after a pipe like in "cmd | "
|
||||
// "cmd | " should behave like "cmd |"
|
||||
static int tokenize_input(t_token *t, char *input, size_t input_len)
|
||||
{
|
||||
int i;
|
||||
int t_i;
|
||||
|
||||
i = 0;
|
||||
t_i = 0;
|
||||
while (input[i])
|
||||
{
|
||||
if (fill_token(t, input, &i, &t_i) == DELIMITE_TOKEN && input[i] && t_i)
|
||||
{
|
||||
if (!t->id)
|
||||
t->id = T_WORD;
|
||||
t->next = alloc_token(input_len - i);
|
||||
if (!t->next)
|
||||
return (ft_reti_perror(0, "alloc_token() error"));
|
||||
t = t->next;
|
||||
t_i = 0;
|
||||
}
|
||||
}
|
||||
if (!t->id && t_i) // Fix parser syntax, but last elem must still be free
|
||||
t->id = T_WORD;
|
||||
/* if (!t->id)
|
||||
t->id = T_WORD; */
|
||||
return (1);
|
||||
}
|
||||
|
||||
static int fill_token(t_token *t, char *input, int *i, int *t_i)
|
||||
{
|
||||
static int in_quotes = 0;
|
||||
|
||||
// operators
|
||||
if (!in_quotes)
|
||||
{
|
||||
if (check_operators(t, input, i, t_i) == DELIMITE_TOKEN)
|
||||
return (DELIMITE_TOKEN);
|
||||
}
|
||||
// quoting
|
||||
if (input[*i] == '\'' && in_quotes != IN_DQUOTES)
|
||||
{
|
||||
t->content[(*t_i)++] = input[(*i)++];
|
||||
if (in_quotes == IN_QUOTES)
|
||||
in_quotes = 0;
|
||||
else if (ft_strchr(&input[*i], '\'')) // if closed quotes
|
||||
in_quotes = IN_QUOTES;
|
||||
return (CONTINUE_TOKEN);
|
||||
}
|
||||
else if (input[*i] == '\"' && in_quotes != IN_QUOTES)
|
||||
{
|
||||
t->content[(*t_i)++] = input[(*i)++];
|
||||
if (in_quotes == IN_DQUOTES)
|
||||
in_quotes = 0;
|
||||
else if (ft_strchr(&input[*i], '\"')) // if closed dquotes
|
||||
in_quotes = IN_DQUOTES;
|
||||
return (CONTINUE_TOKEN);
|
||||
}
|
||||
// blanks
|
||||
if (!in_quotes && (input[*i] == ' ' || input[*i] == '\t'))
|
||||
{
|
||||
while (input[*i] == ' ' || input[*i] == '\t')
|
||||
(*i)++;
|
||||
return (DELIMITE_TOKEN);
|
||||
}
|
||||
else
|
||||
t->content[(*t_i)++] = input[(*i)++];
|
||||
return (CONTINUE_TOKEN);
|
||||
}
|
||||
|
||||
static int check_operators(t_token *t, char *input, int *i, int *t_i)
|
||||
{
|
||||
if (*t_i != 0 && (input[*i] == '|' || input[*i] == '<' || input[*i] == '>'))
|
||||
return (DELIMITE_TOKEN);
|
||||
if (input[*i] == '|')
|
||||
{
|
||||
t->content[(*t_i)++] = input[(*i)++];
|
||||
t->id = T_PIPE;
|
||||
return (DELIMITE_TOKEN);
|
||||
}
|
||||
else if (input[*i] == '<')
|
||||
{
|
||||
t->content[(*t_i)++] = input[(*i)++];
|
||||
t->id = T_LESS;
|
||||
if (input[*i] == '<')
|
||||
{
|
||||
t->content[(*t_i)++] = input[(*i)++];
|
||||
t->id = T_DLESS;
|
||||
}
|
||||
return (DELIMITE_TOKEN);
|
||||
}
|
||||
else if (input[*i] == '>')
|
||||
{
|
||||
t->content[(*t_i)++] = input[(*i)++];
|
||||
t->id = T_GREAT;
|
||||
if (input[*i] == '>')
|
||||
{
|
||||
t->content[(*t_i)++] = input[(*i)++];
|
||||
t->id = T_DGREAT;
|
||||
}
|
||||
return (DELIMITE_TOKEN);
|
||||
}
|
||||
return (CONTINUE_TOKEN);
|
||||
}
|
||||
|
||||
static t_token *alloc_token(size_t content_len)
|
||||
{
|
||||
t_token *token;
|
||||
|
||||
token = ft_calloc(1, sizeof *token);
|
||||
if (!token)
|
||||
return (NULL);
|
||||
token->content = ft_calloc(content_len + 1, 1);
|
||||
if (!token->content)
|
||||
return (ft_retp_free(NULL, token, free));
|
||||
return (token);
|
||||
}
|
||||
|
||||
/*
|
||||
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_03
|
||||
1 - OK
|
||||
2 - OK
|
||||
3 - OK
|
||||
4 - OK
|
||||
5 - OK / SEMI-OSEF
|
||||
6 - OK
|
||||
7 - OK
|
||||
8 - OK
|
||||
9 - OSEF
|
||||
10 - OK
|
||||
|
||||
*/
|
||||
|
||||
// Doublon avec ft_lstclear()
|
||||
/* void *free_tokens(t_token *t)
|
||||
{
|
||||
void *tmp;
|
||||
|
||||
while (t)
|
||||
{
|
||||
if (t->content)
|
||||
free (t->content);
|
||||
tmp = t;
|
||||
t = t->next;
|
||||
free(tmp);
|
||||
}
|
||||
return (NULL);
|
||||
} */
|
||||
49
srcs/main.c
49
srcs/main.c
@@ -6,59 +6,12 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */
|
||||
/* Updated: 2021/10/10 15:13:26 by hulamy ### ########.fr */
|
||||
/* Updated: 2021/10/27 14:48:03 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
void shell_loop(t_all *c)
|
||||
{
|
||||
char *line_input;
|
||||
|
||||
line_input = NULL;
|
||||
while (1)
|
||||
{
|
||||
if (line_input)
|
||||
free(line_input);
|
||||
line_input = readline(c->prompt);
|
||||
if (line_input && *line_input)
|
||||
{
|
||||
if (!ft_strncmp(line_input, "env", 4)) // temp placeholder
|
||||
builtin_env(0, NULL, c);
|
||||
else if (!ft_strncmp(line_input, "exit", 5)) // temp placeholder
|
||||
builtin_exit(0, NULL, c);
|
||||
else
|
||||
printf("echo: %s\n", line_input);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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); */
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[], char *envp[])
|
||||
{
|
||||
t_all c;
|
||||
|
||||
101
srcs/parsing/parsing.c
Normal file
101
srcs/parsing/parsing.c
Normal file
@@ -0,0 +1,101 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* parsing.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
// A quoi bon un arbre binaire ? Je ne vois plus l'utilité.
|
||||
|
||||
/* typedef struct s_binary_tree
|
||||
{
|
||||
char *content;
|
||||
struct s_binary_tree *sub;
|
||||
struct s_binary_tree *sibling;
|
||||
enum e_token_id id;
|
||||
} t_binary_tree; */
|
||||
|
||||
t_cmd **parsing(t_token *token_list)
|
||||
{
|
||||
t_cmd **cmd_arr;
|
||||
size_t cmd_nbr;
|
||||
|
||||
(void)cmd_arr;
|
||||
(void)cmd_nbr;
|
||||
/* t_binary_tree *syntax_tree;
|
||||
|
||||
syntax_tree = ft_calloc(1, sizeof *syntax_tree);
|
||||
if (!syntax_tree)
|
||||
return (0); //WIP ERROR */
|
||||
|
||||
if (!valid_syntax(token_list))
|
||||
return (NULL);
|
||||
// Pipes count (determine cmd_nbr)
|
||||
|
||||
// Struct CMD alloc/fill
|
||||
|
||||
// 2.9.1 - 2) Expansion
|
||||
|
||||
// 2.9.1 - 3) Redirection
|
||||
|
||||
//return (cmd_arr);
|
||||
return ((t_cmd **)1); //temp test
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------
|
||||
The grammar symbols
|
||||
------------------------------------------------------- */
|
||||
/*
|
||||
%token WORD
|
||||
%token LESS // '<'
|
||||
%token GREAT // '>'
|
||||
%token DLESS // '<<'
|
||||
%token DGREAT // '>>'
|
||||
%token PIPE // '|'
|
||||
*/
|
||||
|
||||
/* -------------------------------------------------------
|
||||
The Simplified Grammar
|
||||
------------------------------------------------------- */
|
||||
/*
|
||||
%start program
|
||||
%%
|
||||
pipeline : command
|
||||
| pipe_sequence '|' command
|
||||
;
|
||||
command : cmd_prefix cmd_name cmd_suffix
|
||||
| cmd_prefix cmd_name
|
||||
| cmd_name cmd_suffix
|
||||
| cmd_name
|
||||
;
|
||||
cmd_name : WORD // Apply rule 7a
|
||||
;
|
||||
cmd_prefix : io_redirect
|
||||
| cmd_prefix io_redirect
|
||||
;
|
||||
cmd_suffix : io_redirect
|
||||
| cmd_suffix io_redirect
|
||||
| WORD
|
||||
| cmd_suffix WORD
|
||||
;
|
||||
io_redirect : io_file
|
||||
| io_here
|
||||
;
|
||||
io_file : '<' filename
|
||||
| '>' filename
|
||||
| DGREAT filename
|
||||
;
|
||||
filename : WORD // Apply rule 2
|
||||
;
|
||||
io_here : DLESS here_end
|
||||
;
|
||||
here_end : WORD // Apply rule 3
|
||||
;
|
||||
*/
|
||||
95
srcs/parsing/valid_command.c
Normal file
95
srcs/parsing/valid_command.c
Normal file
@@ -0,0 +1,95 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* valid_command.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/24 18:52:05 by lperrey #+# #+# */
|
||||
/* Updated: 2021/10/24 19:21:28 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
int valid_io_redirect(t_token **token_list);
|
||||
|
||||
static int valid_command_rule1(t_token **token_list);
|
||||
static int valid_command_rule2(t_token **token_list);
|
||||
static int valid_command_rule3(t_token **token_list);
|
||||
static int valid_command_rule4(t_token **token_list);
|
||||
|
||||
int valid_command(t_token **token_list)
|
||||
{
|
||||
t_token *cmd_start;
|
||||
|
||||
cmd_start = *token_list;
|
||||
if (valid_command_rule1(token_list))
|
||||
return (1);
|
||||
*token_list = cmd_start;
|
||||
if (valid_command_rule2(token_list))
|
||||
return (1);
|
||||
*token_list = cmd_start;
|
||||
if (valid_command_rule3(token_list))
|
||||
return (1);
|
||||
*token_list = cmd_start;
|
||||
if (valid_command_rule4(token_list))
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
// cmd_prefix cmd_name cmd_suffix
|
||||
static int valid_command_rule1(t_token **token_list)
|
||||
{
|
||||
while (valid_io_redirect(token_list))
|
||||
{
|
||||
if (valid_token(token_list, T_WORD))
|
||||
{
|
||||
while (valid_token(token_list, T_WORD)
|
||||
|| valid_io_redirect(token_list))
|
||||
{
|
||||
if (valid_command_separator(*token_list))
|
||||
return (1);
|
||||
}
|
||||
}
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
// cmd_prefix cmd_name
|
||||
static int valid_command_rule2(t_token **token_list)
|
||||
{
|
||||
while (valid_io_redirect(token_list))
|
||||
{
|
||||
if (valid_token(token_list, T_WORD))
|
||||
{
|
||||
if (valid_command_separator(*token_list))
|
||||
return (1);
|
||||
}
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
// cmd_name cmd_suffix
|
||||
static int valid_command_rule3(t_token **token_list)
|
||||
{
|
||||
if (valid_token(token_list, T_WORD))
|
||||
{
|
||||
while (valid_token(token_list, T_WORD) || valid_io_redirect(token_list))
|
||||
{
|
||||
if (valid_command_separator(*token_list))
|
||||
return (1);
|
||||
}
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
// cmd_name
|
||||
static int valid_command_rule4(t_token **token_list)
|
||||
{
|
||||
if (valid_token(token_list, T_WORD))
|
||||
{
|
||||
if (valid_command_separator(*token_list))
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
44
srcs/parsing/valid_io_redirect.c
Normal file
44
srcs/parsing/valid_io_redirect.c
Normal file
@@ -0,0 +1,44 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* valid_io_redirect.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/24 18:52:42 by lperrey #+# #+# */
|
||||
/* Updated: 2021/10/24 19:21:35 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
static int valid_io_file(t_token **token_list);
|
||||
static int valid_io_here(t_token **token_list);
|
||||
|
||||
int valid_io_redirect(t_token **token_list)
|
||||
{
|
||||
if (valid_io_file(token_list) || valid_io_here(token_list))
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int valid_io_file(t_token **token_list)
|
||||
{
|
||||
if (valid_token(token_list, '<') || valid_token(token_list, '>')
|
||||
|| valid_token(token_list, T_DGREAT))
|
||||
{
|
||||
if (valid_token(token_list, T_WORD))
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int valid_io_here(t_token **token_list)
|
||||
{
|
||||
if (valid_token(token_list, T_DLESS))
|
||||
{
|
||||
if (valid_token(token_list, T_WORD))
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
27
srcs/parsing/valid_pipeline.c
Normal file
27
srcs/parsing/valid_pipeline.c
Normal file
@@ -0,0 +1,27 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* valid_pipeline.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/24 18:51:35 by lperrey #+# #+# */
|
||||
/* Updated: 2021/10/24 19:35:16 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
int valid_command(t_token **token_list);
|
||||
|
||||
int valid_pipeline(t_token **token_list)
|
||||
{
|
||||
while (valid_command(token_list))
|
||||
{
|
||||
if (*token_list == NULL)
|
||||
return (1);
|
||||
else if ((*token_list)->id != '|' || (*token_list)->next == NULL)
|
||||
return (0);
|
||||
valid_token(token_list, '|');
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
44
srcs/parsing/valid_syntax.c
Normal file
44
srcs/parsing/valid_syntax.c
Normal file
@@ -0,0 +1,44 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* valid_syntax.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/24 13:01:10 by lperrey #+# #+# */
|
||||
/* Updated: 2021/10/24 19:40:23 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
int valid_pipeline(t_token **token_list);
|
||||
|
||||
int valid_syntax(t_token *token_list)
|
||||
{
|
||||
if (valid_pipeline(&token_list))
|
||||
return (1);
|
||||
else
|
||||
{ // WIP ERROR
|
||||
ft_putstr_fd("minishell: syntax error near unexpected token \'", 2);
|
||||
ft_putstr_fd(token_list->content, 2);
|
||||
ft_putstr_fd("\'\n", 2);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int valid_token(t_token **token_list, enum e_token_id token_id)
|
||||
{
|
||||
if (*token_list != NULL && (*token_list)->id == token_id)
|
||||
{
|
||||
*token_list = (*token_list)->next;
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int valid_command_separator(const t_token *token_list)
|
||||
{
|
||||
if (token_list == NULL || token_list->id == '|')
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
96
srcs/shell_loop.c
Normal file
96
srcs/shell_loop.c
Normal file
@@ -0,0 +1,96 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* shell_loop.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
static char **tokens_list_to_argv(t_token *t); // temp test
|
||||
|
||||
void shell_loop(t_all *c)
|
||||
{
|
||||
char *line_input;
|
||||
|
||||
line_input = NULL;
|
||||
while (1)
|
||||
{
|
||||
if (line_input)
|
||||
free(line_input);
|
||||
line_input = readline(c->prompt);
|
||||
if (line_input && *line_input)
|
||||
{
|
||||
add_history(line_input);
|
||||
c->token_list = input_to_tokens(line_input);
|
||||
|
||||
// EXEC_PIPES_AND_CO()
|
||||
|
||||
// temp placeholder
|
||||
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);
|
||||
else if (ft_strncmp(c->token_list->content, "echo", 5) == 0)
|
||||
builtin_echo(ft_lstsize((t_list *)c->token_list) + 1, tokens_list_to_argv(c->token_list), c);
|
||||
else
|
||||
{
|
||||
if (parsing(c->token_list))
|
||||
ft_putstr_fd("Syntax OK:\n-----------\n", 1);
|
||||
else
|
||||
ft_putstr_fd("Syntax KO:\n-----------\n", 1);
|
||||
|
||||
ft_putstr_fd("TOKENS LIST :\n-----------\n", 1);
|
||||
ft_lstprint((t_list *)c->token_list, 1);
|
||||
ft_lstclear((t_list **)&c->token_list, free);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static char **tokens_list_to_argv(t_token *t) // temp test
|
||||
{
|
||||
size_t i;
|
||||
char **argv;
|
||||
|
||||
i = ft_lstsize((t_list *)t);
|
||||
argv = ft_calloc(i + 1, sizeof(char*));
|
||||
i = 0;
|
||||
while (t)
|
||||
{
|
||||
argv[i] = t->content;
|
||||
i++;
|
||||
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); */
|
||||
}
|
||||
7
tests/trop_de_pipes.sh
Normal file
7
tests/trop_de_pipes.sh
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user