echo builtin

+ WIP grammar rules in parsing.txt
+ test trop_de_pipes.sh
This commit is contained in:
LuckyLaszlo
2021-10-22 10:53:06 +02:00
parent 1054f3d6ff
commit 39de10e001
10 changed files with 241 additions and 59 deletions

2
.gitignore vendored
View File

@@ -13,3 +13,5 @@ memo
memo.txt
minishell
*.zip
arg_test
osef_temp

View File

@@ -22,7 +22,9 @@ LIBFT_D = ./libft
LIBFT = $(LIBFT_D)/libft.a
SRCS = main.c init.c free.c generic.c \
env.c exit.c lexing.c
shell_loop.c \
lexing.c \
env.c exit.c echo.c
DIR_OBJS = builds
OBJS = $(SRCS:%.c=$(DIR_OBJS)/%.o)

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 02:59:58 by lperrey #+# #+# */
/* Updated: 2021/10/19 19:55:08 by lperrey ### ########.fr */
/* Updated: 2021/10/22 10:41:34 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -19,9 +19,13 @@ int init(t_all *c, char *envp[]);
// Lexer
t_token *input_to_tokens(char *input);
// Shell loop
void shell_loop(t_all *c);
// 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);
@@ -31,5 +35,6 @@ 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);
#endif

View File

@@ -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"

57
parsing.txt Normal file
View File

@@ -0,0 +1,57 @@
/* -------------------------------------------------------
The grammar symbols
------------------------------------------------------- */
%token WORD
%token LESS // '<'
%token GREAT // '>'
%token DLESS // '<<'
%token DGREAT // '>>'
%token PIPE // '|'
/* -------------------------------------------------------
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
View 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);
}

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 09:25:35 by lperrey #+# #+# */
/* Updated: 2021/10/19 19:54:59 by lperrey ### ########.fr */
/* Updated: 2021/10/21 18:23:04 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -47,4 +47,28 @@ void ft_lstprint(t_list *lst, int fd)
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);
}

View File

@@ -6,64 +6,12 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */
/* Updated: 2021/10/19 20:23:19 by lperrey ### ########.fr */
/* Updated: 2021/10/22 10:33:00 by lperrey ### ########.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 // temp placeholder
{
c->token_list = input_to_tokens(line_input);
ft_lstprint((t_list *)c->token_list, 1);
ft_lstclear((t_list **)&c->token_list, free);
//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;

90
srcs/shell_loop.c Normal file
View File

@@ -0,0 +1,90 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* shell_loop.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */
/* Updated: 2021/10/22 10:48:07 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);
c->token_list = input_to_tokens(line_input);
// temp placeholder
if (!ft_strncmp(c->token_list->content, "env", 4))
builtin_env(0, NULL, c);
else if (!ft_strncmp(c->token_list->content, "exit", 5))
builtin_exit(0, NULL, c);
else if (!ft_strncmp(c->token_list->content, "echo", 5))
builtin_echo(ft_lstsize((t_list *)c->token_list) + 1, tokens_list_to_argv(c->token_list), c);
else
{
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

File diff suppressed because one or more lines are too long