debut du parsing :(

This commit is contained in:
Tom Moron
2024-02-06 22:23:32 +01:00
parent c13f32bf42
commit 9b80cc758f
11 changed files with 314 additions and 66 deletions

View File

@ -6,13 +6,14 @@
# By: tomoron <marvin@42.fr> +#+ +:+ +#+ # # By: tomoron <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2023/07/28 00:35:01 by tomoron #+# #+# # # Created: 2023/07/28 00:35:01 by tomoron #+# #+# #
# Updated: 2024/02/02 22:12:25 by tomoron ### ########.fr # # Updated: 2024/02/06 22:07:47 by tomoron ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
CC = cc CC = cc
SRCS = main.c\ SRCS = main.c\
ft_lst_cmd.c
OBJS = $(SRCS:.c=.o) OBJS = $(SRCS:.c=.o)
@ -25,7 +26,7 @@ NAME = minishell
all: $(NAME) all: $(NAME)
$(NAME) : $(LIBFT) $(OBJS) $(NAME) : $(LIBFT) $(OBJS)
$(CC) $(FLAGS) $(OBJS) $(LIBFT) -o $(NAME) $(CC) $(FLAGS) $(OBJS) $(LIBFT) -lreadline -o $(NAME)
$(LIBFT) : $(LIBFT) :
make --no-print-directory -j -C ./libft make --no-print-directory -j -C ./libft
@ -34,11 +35,11 @@ $(LIBFT) :
$(CC) $(FLAGS) -c $< -o $@ $(CC) $(FLAGS) -c $< -o $@
clean: clean:
rm -f $(OBJS_PS) $(OBJS_CHECKER) rm -f $(OBJS)
make --no-print-directory -C ./libft fclean make --no-print-directory -C ./libft fclean
fclean: clean fclean: clean
rm -f push_swap checker rm -f $(NAME)
re: fclean all re: fclean all

31
ft_lst_cmd.c Normal file
View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lst_cmd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/06 20:46:19 by tomoron #+# #+# */
/* Updated: 2024/02/06 22:09:09 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
t_cmd *ft_cmd_add_back(t_cmd *cmd, char *token)
{
t_cmd *res;
t_cmd *current;
res = ft_calloc(1, sizeof(t_cmd));
if (!res)
return (cmd);
res->token = token;
if (!cmd)
return (res);
current = cmd;
while (current->next)
current = current->next;
current->next = res;
return (cmd);
}

View File

@ -6,7 +6,7 @@
# By: tomoron <marvin@42.fr> +#+ +:+ +#+ # # By: tomoron <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2023/07/28 00:35:01 by tomoron #+# #+# # # Created: 2023/07/28 00:35:01 by tomoron #+# #+# #
# Updated: 2024/02/02 22:04:52 by tomoron ### ########.fr # # Updated: 2024/02/05 01:10:13 by tomoron ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -50,7 +50,8 @@ SRCS = ft_atoi.c\
ft_toupper.c\ ft_toupper.c\
ft_split_set.c\ ft_split_set.c\
ft_free_str_arr.c\ ft_free_str_arr.c\
ft_set_color.c ft_set_color.c\
ft_isspace.c
SRCS_BONUS = ft_lstnew.c\ SRCS_BONUS = ft_lstnew.c\
ft_lstadd_front.c\ ft_lstadd_front.c\
@ -65,7 +66,7 @@ SRCS_BONUS = ft_lstnew.c\
OBJS = $(SRCS:.c=.o) OBJS = $(SRCS:.c=.o)
OBJS_BONUS = $(SRCS_BONUS:.c=.o) OBJS_BONUS = $(SRCS_BONUS:.c=.o)
FLAGS = -Wall -Wextra -Werror FLAGS = -Wall -Wextra -Werror -g
all: $(NAME) all: $(NAME)

17
libft/ft_isspace.c Normal file
View File

@ -0,0 +1,17 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isspace.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/05 00:58:08 by tomoron #+# #+# */
/* Updated: 2024/02/06 22:17:28 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
int ft_isspace(int c)
{
return (c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v'
|| c == ' ');
}

View File

@ -6,7 +6,7 @@
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */ /* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/10 10:56:11 by tomoron #+# #+# */ /* Created: 2024/01/10 10:56:11 by tomoron #+# #+# */
/* Updated: 2024/01/10 14:10:42 by tomoron ### ########.fr */ /* Updated: 2024/02/04 02:00:50 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,6 +17,19 @@ void ft_set_color(int r, int g, int b)
ft_printf("\033[38;2;%d;%d;%dm", r, g, b); ft_printf("\033[38;2;%d;%d;%dm", r, g, b);
} }
char *ft_get_color(int r, int g, int b)
{
char *res;
res = ft_strjoin_free("\033[38;2;", ft_itoa(r), 2);
res = ft_strjoin_free(res, ";", 1);
res = ft_strjoin_free(res, ft_itoa(g), 3);
res = ft_strjoin_free(res, ";", 1);
res = ft_strjoin_free(res, ft_itoa(b), 3);
res = ft_strjoin_free(res, "m", 1);
return (res);
}
void ft_reset_color(void) void ft_reset_color(void)
{ {
ft_printf("\033[0m"); ft_printf("\033[0m");

View File

@ -6,7 +6,7 @@
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */ /* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/24 18:06:14 by tomoron #+# #+# */ /* Created: 2023/07/24 18:06:14 by tomoron #+# #+# */
/* Updated: 2023/11/02 10:26:09 by tomoron ### ########.fr */ /* Updated: 2024/02/03 22:47:27 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -61,3 +61,15 @@ char *ft_strjoin(char const *s1, char const *s2)
res[j] = 0; res[j] = 0;
return (res); return (res);
} }
char *ft_strjoin_free(char *s1, char *s2, int free_flags)
{
char *res;
res = ft_strjoin(s1, s2);
if (free_flags == 1 || free_flags == 3)
free(s1);
if (free_flags == 2 || free_flags == 3)
free(s2);
return (res);
}

View File

@ -6,7 +6,7 @@
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */ /* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/30 16:55:48 by tomoron #+# #+# */ /* Created: 2023/10/30 16:55:48 by tomoron #+# #+# */
/* Updated: 2024/01/10 14:10:51 by tomoron ### ########.fr */ /* Updated: 2024/02/06 22:15:58 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -24,54 +24,57 @@ typedef struct s_list
struct s_list *next; struct s_list *next;
} t_list; } t_list;
int ft_atoi(const char *str); t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *));
char *ft_strnstr(const char *str, const char *to_find, size_t len);
char *ft_substr(char const *s, unsigned int start, size_t len);
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
void ft_striteri(char *s, void (*f)(unsigned int, char*));
char *ft_strjoin_free(char *s1, char *s2, int free_flags);
size_t ft_strlcpy(char *dest, const char *src, size_t size);
size_t ft_strlcat(char *dest, const char *src, size_t size);
int ft_strncmp(const char *s1, const char *s2, size_t n);
int ft_memcmp(const void *s1, const void *s2, size_t n);
void *ft_memmove(void *dest, const void *src, size_t n);
void *ft_memcpy(void *dest, const void *src, size_t n);
void ft_lstdelone(t_list *lst, void (*del)(void*));
void ft_lstclear(t_list **lst, void (*del)(void*));
char *ft_strtrim(char const *s1, char const *set);
char *ft_strjoin(char const *s1, char const *s2);
void ft_lstiter(t_list *lst, void (*f)(void *));
void *ft_memchr(const void *s, int c, size_t n);
void ft_lstadd_front(t_list **lst, t_list *new);
char **ft_split(char const *str, char charset);
void ft_lstadd_back(t_list **lst, t_list *new);
char **ft_split_set(char *str, char *charset);
void *ft_memset(void *s, int c, size_t n);
char *ft_get_color(int r, int g, int b);
void *ft_calloc(size_t nb, size_t size);
char *ft_strrchr(const char *s, int c);
void ft_set_color(int r, int g, int b);
char *ft_strchr(const char *s, int c);
void ft_putendl_fd(char *s, int fd);
void ft_putchar_fd(char c, int fd);
void ft_putstr_fd(char *s, int fd);
int ft_strcmp(char *s1, char *s2);
char *ft_strdup(const char *src);
void ft_bzero(void *s, size_t n); void ft_bzero(void *s, size_t n);
void ft_putnbr_fd(int n, int fd);
void ft_free_str_arr(char **arr);
size_t ft_strlen(const char *str);
void ft_swap(int *n1, int *n2);
t_list *ft_lstnew(void *content);
int ft_atoi(const char *str);
t_list *ft_lstlast(t_list *lst);
int ft_lstsize(t_list *lst);
void ft_reset_color(void);
int ft_isspace(char c);
int ft_isalnum(int c); int ft_isalnum(int c);
int ft_isalpha(int c); int ft_isalpha(int c);
int ft_isascii(int c); int ft_isascii(int c);
int ft_isdigit(int c); int ft_isdigit(int c);
int ft_isprint(int c); int ft_isprint(int c);
void *ft_memchr(const void *s, int c, size_t n);
int ft_memcmp(const void *s1, const void *s2, size_t n);
void *ft_memcpy(void *dest, const void *src, size_t n);
void *ft_memmove(void *dest, const void *src, size_t n);
void *ft_memset(void *s, int c, size_t n);
char *ft_strchr(const char *s, int c);
size_t ft_strlcat(char *dest, const char *src, size_t size);
size_t ft_strlcpy(char *dest, const char *src, size_t size);
size_t ft_strlen(const char *str);
int ft_strncmp(const char *s1, const char *s2, size_t n);
char *ft_strnstr(const char *str, const char *to_find, size_t len);
char *ft_strrchr(const char *s, int c);
int ft_tolower(int c); int ft_tolower(int c);
int ft_toupper(int c); int ft_toupper(int c);
void *ft_calloc(size_t nb, size_t size);
char *ft_strdup(const char *src);
char *ft_strjoin(char const *s1, char const *s2);
char *ft_strtrim(char const *s1, char const *set);
char *ft_substr(char const *s, unsigned int start, size_t len);
char **ft_split(char const *str, char charset);
char *ft_itoa(int n); char *ft_itoa(int n);
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
void ft_striteri(char *s, void (*f)(unsigned int, char*));
void ft_putchar_fd(char c, int fd);
void ft_putstr_fd(char *s, int fd);
void ft_putendl_fd(char *s, int fd);
void ft_putnbr_fd(int n, int fd);
t_list *ft_lstnew(void *content);
void ft_lstadd_front(t_list **lst, t_list *new);
int ft_lstsize(t_list *lst);
t_list *ft_lstlast(t_list *lst);
void ft_lstadd_back(t_list **lst, t_list *new);
void ft_lstdelone(t_list *lst, void (*del)(void*));
void ft_lstclear(t_list **lst, void (*del)(void*));
void ft_lstiter(t_list *lst, void (*f)(void *));
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *));
char **ft_split_set(char *str, char *charset);
int ft_strcmp(char *s1, char *s2);
void ft_free_str_arr(char **arr);
void ft_swap(int *n1, int *n2);
void ft_set_color(int r, int g, int b);
void ft_reset_color(void);
#endif #endif

119
main.c
View File

@ -6,22 +6,123 @@
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */ /* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/02 21:59:20 by tomoron #+# #+# */ /* Created: 2024/02/02 21:59:20 by tomoron #+# #+# */
/* Updated: 2024/02/03 19:08:04 by tomoron ### ########.fr */ /* Updated: 2024/02/06 22:21:44 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include <stdio.h> #include "minishell.h"
#include <readline/readline.h>
#include <readline/history.h>
int main(void) int g_return_code = 0;
char *get_prompt(void)
{ {
char *res; char *res;
res = (char *)1; res = ft_strjoin_free("\001",ft_get_color(0,255,0),2);
while(res) res = ft_strjoin_free(res,"\002", 1);
res = ft_strjoin_free(res, getenv("USER"),1);
res = ft_strjoin_free(res, "@",1);
res = ft_strjoin_free(res, "minishell \001\033[0m\002$>",1);
return(res);
}
int ft_get_token_len(char *command)
{ {
res = readline("minishell $>"); int in_quote;
printf("%s\n",res); int in_dquote;
int res;
in_quote = 0;
in_dquote = 0;
res = 0;
while(*command && (!isspace(*command) || in_quote || in_dquote))
{
if(*command == '"' && !in_quote)
in_dquote = !in_dquote;
if(*command == '\'' && !in_dquote)
in_quote = !in_quote;
if(*command != '\'' && *command != '"')
res++;
if((*command == '\'' && in_dquote) || (*command == '"' && in_quote))
res++;
command++;
}
return(res);
}
void ft_skip_spaces(char **command)
{
while (ft_isspace(**command))
(*command)++;
}
char *ft_get_token(char **command)
{
int in_quote;
int in_dquote;
char *res;
int i;
in_quote = 0;
in_dquote = 0;
i = 0;
while (ft_isspace(**command))
(*command)++;
res = malloc(ft_get_token_len(*command));
while(res && **command && (!isspace(**command) || in_quote || in_dquote))
{
if(**command == '"' && !in_quote)
in_dquote = !in_dquote;
if(**command == '\'' && !in_dquote)
in_quote = !in_quote;
if(((**command == '\'' && in_dquote) || (**command == '"' && in_quote))
|| (**command != '\'' && **command != '"'))
res[i++] = **command;
(*command)++;
}
return(res);
}
t_cmd *ft_parse_command(char *command)
{
t_cmd *res;
char *token;
res = 0;
while(*command)
{
token = ft_get_token(&command);
res = ft_cmd_add_back(res, token);
}
return(res);
}
int main(void)
{
char *command;
char *prompt;
command = (char *)1;
while(command)
{
prompt = get_prompt();
if(!prompt)
exit(0);
command = readline(prompt);
add_history(command);
free(prompt);
t_cmd *test;
test = ft_parse_command(command);
printf("%s\n",test->token);
printf("%s\n",command);
if(command && !ft_strcmp(command,"exit"))
{
rl_clear_history();
free(command);
printf("au revoir :,(\n");
exit(0);
}
rl_clear_history();
free(command);
} }
} }

29
minishell.h Normal file
View File

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* minishell.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */
/* Updated: 2024/02/06 22:09:30 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MINISHELL_H
# define MINISHELL_H
# include <readline/readline.h>
# include <readline/history.h>
# include <stdio.h>//debug
# include "libft/libft.h"
typedef struct s_cmd
{
char *token;
struct s_cmd *next;
} t_cmd;
t_cmd *ft_cmd_add_back(t_cmd *res, char *token);
#endif

25
test.c Normal file
View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/04 17:30:08 by tomoron #+# #+# */
/* Updated: 2024/02/06 12:41:57 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argv)
{
int i = 0;
while(i < argc)
{
printf("%s\n",argv[i]);
i++;
}
return(0);
}

15
valgrind.supp Normal file
View File

@ -0,0 +1,15 @@
{
readline
Memcheck:Leak
...
fun:readline
...
}
{
readline
Memcheck:Leak
...
fun:add_history
...
}