Browse Source

fixed: libft-isspace, utils fonctions. rework: recursive parser

master
narnaud 3 years ago
parent
commit
fa52574ed3
  1. 10
      Makefile
  2. 11
      README.md
  3. 17
      lexer.c
  4. 2
      libft/Makefile
  5. 4
      libft/is/ft_isspace.c
  6. 3
      libft/libft.h
  7. 64
      minishell.c
  8. 44
      minishell.h
  9. 108
      utils.c

10
Makefile

@ -1,6 +1,6 @@
NAME = minishell NAME = minishell
LIBFT = 42-libft/libft.a LIBFT = libft.a
SRCS = minishell.c lexer.c parser.c caller.c built-in.c utils.c SRCS = minishell.c lexer.c parser.c utils.c
OBJS = ${SRCS:.c=.o} OBJS = ${SRCS:.c=.o}
READLINE_INC = ~/.brew/opt/readline/include READLINE_INC = ~/.brew/opt/readline/include
@ -10,8 +10,8 @@ $(NAME): $(OBJS)
gcc ${OBJS} ${LIB} -o ${NAME} gcc ${OBJS} ${LIB} -o ${NAME}
$(LIBFT): $(LIBFT):
${MAKE} -C ./42-libft ${MAKE} -C ./libft
cp ./42-libft/libft.a . cp ./libft/libft.a .
all: $(LIBFT) $(NAME) all: $(LIBFT) $(NAME)
@ -22,6 +22,8 @@ clean:
rm -rf ${OBJS} rm -rf ${OBJS}
fclean: clean fclean: clean
${MAKE} -C ./libft fclean
rm libft.a
rm -rf ${NAME} rm -rf ${NAME}
re: fclean all re: fclean all

11
README.md

@ -1,2 +1,13 @@
# Minishell # Minishell
## TODO :
## Notes :
## Issues :

17
lexer.c

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 13:44:57 by narnaud #+# #+# */ /* Created: 2022/05/02 13:44:57 by narnaud #+# #+# */
/* Updated: 2022/05/02 17:05:17 by narnaud ### ########.fr */ /* Updated: 2022/05/03 09:19:36 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -47,9 +47,9 @@ int check_register(t_lexer *lex, char **line, char *tmp)
if (lex->state != ROOT_ST) if (lex->state != ROOT_ST)
return (0); return (0);
spaces = 0; spaces = 0;
if (is_space(**line)) if (ft_isspace(**line))
{ {
while (is_space(**line)) while (ft_isspace(**line))
(*line)++; (*line)++;
spaces = 1; spaces = 1;
} }
@ -59,7 +59,7 @@ int check_register(t_lexer *lex, char **line, char *tmp)
lex->next_type = ADD; lex->next_type = ADD;
else else
lex->next_type = OUT; lex->next_type = OUT;
return (create_token(*lex, tmp)); return (create_token(lex, tmp));
} }
else if (**line == '<' && (*line)++) else if (**line == '<' && (*line)++)
{ {
@ -67,19 +67,18 @@ int check_register(t_lexer *lex, char **line, char *tmp)
lex->next_type = HD; lex->next_type = HD;
else else
lex->next_type = IN; lex->next_type = IN;
return (create_token(*lex, tmp)); return (create_token(lex, tmp));
} }
else if (**line == '|') else if (**line == '|')
{ {
(*line)++; (*line)++;
lex->next_type = PIPE; lex->next_type = PIPE;
create_token(*lex, tmp); create_token(lex, tmp);
return (create_token(*lex, "|")); return (create_token(lex, "|"));
} }
if (!spaces) if (!spaces)
return (0); return (0);
return (create_token(l-*ex, tmp)); return (create_token(lex, tmp));
} }
int replace_var(char **line, char *tmp, int tmp_i) int replace_var(char **line, char *tmp, int tmp_i)

2
libft/Makefile

@ -1,7 +1,7 @@
NAME = libft.a NAME = libft.a
SRCS = is/ft_isalpha.c is/ft_isdigit.c is/ft_isascii.c is/ft_isprint.c \ SRCS = is/ft_isalpha.c is/ft_isdigit.c is/ft_isascii.c is/ft_isprint.c \
is/ft_isalnum.c is/ft_isalnum.c is/ft_isspace.c
SRCS += str/ft_len.c str/ft_lcpy.c str/ft_lcat.c str/ft_chr.c \ SRCS += str/ft_len.c str/ft_lcpy.c str/ft_lcat.c str/ft_chr.c \
str/ft_rchr.c str/ft_ncmp.c str/ft_nstr.c str/ft_dup.c \ str/ft_rchr.c str/ft_ncmp.c str/ft_nstr.c str/ft_dup.c \

4
libft/is/ft_isspace.c

@ -6,13 +6,13 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/24 10:05:51 by narnaud #+# #+# */ /* Created: 2022/03/24 10:05:51 by narnaud #+# #+# */
/* Updated: 2022/03/24 10:06:15 by narnaud ### ########.fr */ /* Updated: 2022/05/03 09:47:28 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../libft.h" #include "../libft.h"
int ft_isspace(char c) int ft_isspace(int c)
{ {
if (c > 8 && c < 14) if (c > 8 && c < 14)
return (1); return (1);

3
libft/libft.h

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/19 14:42:45 by narnaud #+# #+# */ /* Created: 2021/10/19 14:42:45 by narnaud #+# #+# */
/* Updated: 2022/03/25 13:31:43 by narnaud ### ########.fr */ /* Updated: 2022/05/03 09:48:13 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -44,6 +44,7 @@ int ft_isascii(int ch);
int ft_isdigit(int ch); int ft_isdigit(int ch);
int ft_isprint(int ch); int ft_isprint(int ch);
int ft_isalnum(int ch); int ft_isalnum(int ch);
int ft_isspace(int ch);
void *ft_memchr(const void *b, int c, size_t n); void *ft_memchr(const void *b, int c, size_t n);
int ft_memcmp(const void *s1, const void *s2, size_t n); int ft_memcmp(const void *s1, const void *s2, size_t n);
void *ft_memcpy(void *dst, const void *src, size_t n); void *ft_memcpy(void *dst, const void *src, size_t n);

64
minishell.c

@ -6,46 +6,38 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 12:14:09 by narnaud #+# #+# */ /* Created: 2022/05/02 12:14:09 by narnaud #+# #+# */
/* Updated: 2022/05/02 17:43:27 by narnaud ### ########.fr */ /* Updated: 2022/05/03 10:39:02 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
# include "minishell.h" # include "minishell.h"
t_command *parser(t_token *tok) t_command *parser(t_token *tok, t_command *prev)
{ {
int i; int i;
t_command *cmd; t_command *cmd;
t_command *ret;
if (!tok)
return (NULL);
cmd = ft_calloc(1, sizeof(t_command)); cmd = ft_calloc(1, sizeof(t_command));
ret = cmd; if (prev)
while (tok->value) cmd->prev = prev;
i = 0;
cmd->argv = ft_calloc(count_arguments(tok) + 1, sizeof(char *));
while(tok && tok->type != PIPE)
{ {
i = 0; if (tok->type)
cmd->argv = ft_calloc(count_arguments(tok) + 1, sizeof(char *)); update_redir(cmd, tok);
while(tok->type != PIPE)
{
if (tok->type)
update_redir(cmd, tok);
else
cmd->argv[i++] = tok->value;
if (!tok->next)
break ;
tok = tok->next;
}
//printf("Input : %d\n", cmd->input);
//printf("Output : %d\n", cmd->output);
cmd->argv[i] = NULL;
if (tok->next)
tok = tok->next;
else else
break ; cmd->argv[i++] = tok->value;
cmd->next = ft_calloc(1, sizeof(t_command)); if (DEBUG)
cmd->next->prev = cmd; printf("token : %s, type: %d\n", tok->value, tok->type);
cmd = cmd->next; tok = tok->next;
} }
return (ret); cmd->argv[i] = NULL;
if (tok && tok->type == PIPE)
cmd->next = parser(tok->next, cmd);
return (cmd);
} }
t_token *lexer(char *line) t_token *lexer(char *line)
@ -54,19 +46,21 @@ t_token *lexer(char *line)
char *tmp; char *tmp;
int tmp_i; int tmp_i;
lex = ft_calloc(1, sizeof *ret); lex = ft_calloc(1, sizeof *lex);
lex->state = IN_ROOT; lex->state = ROOT_ST;
tmp = ft_calloc(1024, sizeof *tmp); tmp = ft_calloc(1024, sizeof *tmp);
tmp_i = 0; tmp_i = 0;
while (*line) while (*line)
{ {
if (check_state(lex, &line) == 0) if (DEBUG)
printf("%c\n", *line);
if (check_state(lex, &line))
continue; continue;
if (lex->state != S_QUOTE_ST && *line == '$') if (lex->state != S_QUOTE_ST && *line == '$')
tmp_i = replace_var(&line, tmp, tmp_i); tmp_i = replace_var(&line, tmp, tmp_i);
if (check_register(lex, &line, tmp)) if (check_register(lex, &line, tmp))
{ {
i = 0; tmp_i = 0;
ft_bzero(tmp, 1024); ft_bzero(tmp, 1024);
continue ; continue ;
} }
@ -81,16 +75,17 @@ t_token *lexer(char *line)
int launcher(t_env env) int launcher(t_env env)
{ {
char *line; char *line;
int ret;
(void)env;
// TODO: handle ret
line = readline("$ "); line = readline("$ ");
if (line == NULL) if (line == NULL)
halt(EXIT_FAILURE); halt(EXIT_FAILURE);
if (is_empty(line)) if (is_empty(line))
return (EXIT_FAILURE); return (EXIT_FAILURE);
ret = piper(parser(lexer(line))); parser(lexer(line), NULL);
add_history(line); add_history(line);
return (ret); return (0);
} }
int main(int argc, char **argv, char**envp) int main(int argc, char **argv, char**envp)
@ -99,6 +94,7 @@ int main(int argc, char **argv, char**envp)
(void)argv; (void)argv;
(void)envp; (void)envp;
t_env env; t_env env;
env.null = 0;
while (1) while (1)
launcher(env); launcher(env);
clear_history(); clear_history();

44
minishell.h

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 13:50:44 by narnaud #+# #+# */ /* Created: 2022/05/02 13:50:44 by narnaud #+# #+# */
/* Updated: 2022/05/02 16:45:28 by narnaud ### ########.fr */ /* Updated: 2022/05/03 09:34:27 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,6 +15,7 @@
# define PIPE_MAX_SIZE 8192 # define PIPE_MAX_SIZE 8192
# define PATHS_MAX_SIZE 126 # define PATHS_MAX_SIZE 126
# define PROMPT_PREFIX 0 # define PROMPT_PREFIX 0
# define DEBUG 1
# include "libft/libft.h" # include "libft/libft.h"
# include <fcntl.h> # include <fcntl.h>
@ -32,14 +33,26 @@
char **g_env; char **g_env;
// ----------------------------------Utils.c
void halt(int ret_code);
int is_empty(char *line);
int init_file(char *filename, int mode);
// ----------------------------------Piper.c // ----------------------------------Piper.c
typedef struct s_env
{
int null;
} t_env;
typedef struct s_command typedef struct s_command
{ {
char **argv; char **argv;
char **envp; char **envp;
int argc; int argc;
int fd[2]; int fd[2];
int input;
int output;
char *heredoc; char *heredoc;
struct s_command *next; struct s_command *next;
struct s_command *prev; struct s_command *prev;
@ -48,6 +61,16 @@ typedef struct s_command
// ----------------------------------Parser.c // ----------------------------------Parser.c
typedef enum e_type
{
WORD,
PIPE,
OUT,
ADD,
IN,
HD,
} t_type;
typedef struct s_token typedef struct s_token
{ {
char *value; char *value;
@ -55,8 +78,8 @@ typedef struct s_token
struct s_token *next; struct s_token *next;
} t_token; } t_token;
size_t count_arguments(t_token tok); size_t count_arguments(t_token *tok);
void update_redir(t_command *cmd, t_token tok); void update_redir(t_command *cmd, t_token *tok);
// ----------------------------------Lexer.c // ----------------------------------Lexer.c
@ -68,16 +91,6 @@ typedef enum e_state
D_QUOTE_ST, D_QUOTE_ST,
} t_state; } t_state;
typedef enum e_type
{
WORD,
PIPE,
OUT,
ADD,
IN,
HD,
} t_type;
typedef struct s_lex typedef struct s_lex
{ {
t_state state; t_state state;
@ -90,6 +103,9 @@ typedef struct s_lex
int create_token(t_lexer *lex, char str[]); int create_token(t_lexer *lex, char str[]);
int check_register(t_lexer *lex, char **line, char *tmp); int check_register(t_lexer *lex, char **line, char *tmp);
int replace_var(char **line, char *tmp, int tmp_i); int replace_var(char **line, char *tmp, int tmp_i);
int check_state(t_lexer *lex, char *line, int i); int check_state(t_lexer *lex, char **line);
#endif #endif

108
utils.c

@ -0,0 +1,108 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/03 08:57:53 by narnaud #+# #+# */
/* Updated: 2022/05/03 09:00:40 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void push_heredoc(char *str, int fd)
{
char *line;
while(1)
{
line = readline(">");
if (!ft_strncmp(line, str, ft_strlen(str) + 1))
break;
ft_putstr_fd(line, fd);
write(fd, "\n", 1);
}
}
char **get_commands(char *line)
{
char **split;
split = ft_split(line, '|');
return (split);
}
int is_empty(char *line)
{
while (*line)
{
if (*line <= 9 || (*line >= 13 && *line != 32))
return (0);
line++;
}
return (1);
}
int msg_n_ret(char *msg, int ret)
{
printf("%s", msg);
return (ret);
}
int init_file(char *filename, int mode)
{
return (open(filename, mode | O_CREAT, 0644));
}
int is_space(char ch)
{
if (ch == 32 || (ch > 8 && ch < 14))
return (1);
else
return (0);
}
void termios(int ctl)
{
struct termios termios_p;
int tty;
tty = ttyslot();
if (tcgetattr(tty, &termios_p) < 0)
{
perror("stdin");
exit (EXIT_FAILURE);
}
//termios_p.c_oflag &= CRDLY;
//termios_p.c_lflag &= ICANON;
if (ctl)
{
termios_p.c_lflag |= ECHOCTL;
termios_p.c_cc[VQUIT] = 28;
}
else
{
termios_p.c_lflag &= ~(ECHOCTL);
termios_p.c_cc[VQUIT] = 0;
}
tcsetattr(tty, TCSANOW, &termios_p);
}
void sigs_handler(int sig_num)
{
if (sig_num == SIGQUIT)
printf("QUIT: 3\n");
else if (sig_num == SIGINT)
printf("\n");
rl_on_new_line();
rl_replace_line("", 0);
rl_redisplay();
return ;
}
void halt(int ret_code)
{
printf("exit");
exit(ret_code);
}
Loading…
Cancel
Save