🏗️」 wip: __to_token nearly done

This commit is contained in:
2024-05-23 20:53:07 +02:00
parent ce0ef1d897
commit 6a8a6382bd
12 changed files with 305 additions and 60 deletions

View File

@ -6,7 +6,7 @@
# By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/04/24 10:49:52 by adjoly #+# #+# #
# Updated: 2024/05/09 16:05:08 by adjoly ### ########.fr #
# Updated: 2024/05/21 20:56:16 by adjoly ### ########.fr #
# #
# **************************************************************************** #

19
include/error_msg.h Normal file
View File

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* error_msg.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/20 20:25:06 by adjoly #+# #+# */
/* Updated: 2024/05/20 21:00:45 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ERROR_MSG_H
# define ERROR_MSG_H
/**
* Here we define all the error message
*/
#endif

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/25 12:20:26 by adjoly #+# #+# */
/* Updated: 2024/05/19 13:45:01 by adjoly ### ########.fr */
/* Updated: 2024/05/23 19:56:20 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -24,18 +24,52 @@ typedef struct s_cmd
int outfile;
} t_cmd;
typedef enum s_quote
{
NOT_CLOSED = -1,
FALSE,
SINGLE,
DOUBLE
} t_quote;
/**
* @brief Take the argv of a command a split the argv and the command it self
* @brief Take the argv of a command a split the argv and the
* command it self
*
* @param The full argv of the command
* @param cmd_av The full argv of the command
*
* @return (t_cmd *) cmd and argv splited into a struct
*/
t_cmd *split_cmd(char *cmd_av);
/*
* @deprecated
/**
* @brief Take a string and an index and check if the character
* at the index is in quote
*
* @param s The string to search in
* @param i The index of the character to check
*
* @return (t_quote) The type of quote if between, if not return FALSE or
* NOT_CLOSED if the quote is not closed
*/
t_list *split_pipe(char *readline);
t_quote is_inquote(char *s, size_t i);
/**
* @brief Take a character and check if it is a quote and return the
* type of quote
*
* @param c The character to check
*
* @return (t_quote) The type of quote or FALSE
*/
t_quote __is_quote(char c);
/*
* ONLY FOR DEBUG TO BE REMOVED
*/
void print_quote_type(t_quote type);
void print_redir_sign(t_redirection_sign redir_sign);
void print_token(t_token *token);
void print_redir(t_redirection *redir);
#endif

View File

@ -6,20 +6,19 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/18 20:14:15 by adjoly #+# #+# */
/* Updated: 2024/05/19 14:02:58 by adjoly ### ########.fr */
/* Updated: 2024/05/23 12:11:42 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef TOKENIZER_H
#ifndef TOKENIZER_H
# define TOKENIZER_H
# include "libft.h"
# include <stdbool.h>
typedef enum s_redirection_sign
typedef enum s_redirection_sign
{
HEREDOC,
INFILE,
HEREDOC,
OUTFILE,
OUT_APPEND,
} t_redirection_sign;
@ -37,7 +36,8 @@ typedef struct s_token
} t_token;
/**
* @brief Convert the raw command into a t_token that contains the argv of the command an a linked list of redirection
* @brief Convert the raw command into a t_token that contains
* the argv of the command an a linked list of redirection
*
* @param cmd A string that containt the command to tokenize
*
@ -47,16 +47,18 @@ typedef struct s_token
t_token *__to_token(char *cmd);
/**
* @brief **
* @brief
*
* @param
* @param The readline output
*
* @return ()
* @return (t_list *) A linked lst of all the command splited
*
*/
t_list *__split_pipe(char *readline);
/**
* @brief Convert the readline output, split all command and put it in linked list of t_token (given by t_token function)
* @brief Convert the readline output, split all command and put
* it in linked list of t_token (given by t_token function)
*
* @param readline The readline output
*

BIN
libft/include/libft.h.gch Normal file

Binary file not shown.

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/08 21:44:04 by mmoussou #+# #+# */
/* Updated: 2023/11/13 19:31:49 by mmoussou ### ########.fr */
/* Updated: 2024/05/22 11:35:58 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -18,7 +18,7 @@ void ft_lstclear(t_list **lst, void (*del)(void *))
if (!lst || !del)
return ;
while (*lst)
while (*lst && lst && del)
{
tmp = (*lst)->next;
ft_lstdelone(*lst, del);

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/03 19:35:01 by mmoussou #+# #+# */
/* Updated: 2024/05/16 17:36:58 by adjoly ### ########.fr */
/* Updated: 2024/05/23 20:01:55 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -41,7 +41,7 @@ char *ft_strjoin_free_s1(char *s1, const char *s2)
return (NULL);
ft_strlcpy(fstr, s1, ft_strlen(s1) + 1);
ft_strlcat(fstr, s2, ft_strlen(s1) + ft_strlen(s2) + 1);
if (s1)
if (*s1)
free(s1);
return (fstr);
}
@ -59,7 +59,7 @@ char *ft_strjoin_free_s2(const char *s1, char *s2)
return (NULL);
ft_strlcpy(fstr, s1, ft_strlen(s1) + 1);
ft_strlcat(fstr, s2, ft_strlen(s1) + ft_strlen(s2) + 1);
if (s2)
if (*s2)
free(s2);
return (fstr);
}
@ -77,9 +77,9 @@ char *ft_strjoin_free(char *s1, char *s2)
return (NULL);
ft_strlcpy(fstr, s1, ft_strlen(s1) + 1);
ft_strlcat(fstr, s2, ft_strlen(s1) + ft_strlen(s2) + 1);
if (s1)
if (*s1)
free(s1);
if (s2)
if (*s2)
free(s2);
return (fstr);
}

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/24 11:18:04 by adjoly #+# #+# */
/* Updated: 2024/05/13 17:26:21 by adjoly ### ########.fr */
/* Updated: 2024/05/23 20:06:16 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,8 +19,9 @@
#include "libft.h"
#include "minishell.h"
#include "parsing.h"
#include "prompt.h"
void print_cmd(t_cmd cmd)
/*void print_cmd(t_cmd cmd)
{
ft_putendl_fd(cmd.cmd, 1);
while (*(cmd.argv))
@ -28,21 +29,16 @@ void print_cmd(t_cmd cmd)
ft_putendl_fd(*(cmd.argv), 1);
(cmd.argv)++;
}
}
}*/
void print_pipe(t_list *pipe)
{
t_list *tmp;
tmp = pipe;
if (!pipe->next)
while (tmp)
{
print_cmd(*(t_cmd*)tmp->content);
return;
}
while (tmp)
{
print_cmd(*(t_cmd*)tmp->content);
ft_putendl_fd(tmp->content, STDOUT_FILENO);
tmp = tmp->next;
}
}
@ -50,9 +46,10 @@ void print_pipe(t_list *pipe)
int main(int ac, char **av, char **env)
{
char *test;
char **lll;
char *prompt;
t_list *cmd;
char **lll;
t_list *piped;
t_token *token;
(void)ac;
(void)av;
@ -68,10 +65,23 @@ int main(int ac, char **av, char **env)
continue;
if (is_str(test, "exit"))
break;
cmd = split_pipe(test);
print_pipe(cmd);
ft_free("a", &lll);
piped = __split_pipe(test);
token = __to_token(piped->content);
print_token(token);
free(token);
free(test);
ft_lstclear(&piped, &free);
}
ft_free("a", &lll);
return (0);
}
/*int main()
{
char *ll = "asdf\"xf\"asfffd";
t_quote d;
d = is_inquote(ll, 6);
ft_printf("%c\n", *(ll+6));
print_quote_type(d);
}*/

46
src/parsing/debug_print.c Normal file
View File

@ -0,0 +1,46 @@
#include "parsing.h"
void print_quote_type(t_quote type)
{
if (type == SINGLE)
ft_putendl_fd("SINGLE", STDOUT_FILENO);
else if (type == DOUBLE)
ft_putendl_fd("DOUBLE", STDOUT_FILENO);
else if (type == FALSE)
ft_putendl_fd("FALSE", STDOUT_FILENO);
else if (type == NOT_CLOSED)
ft_putendl_fd("NOT_CLOSED", STDOUT_FILENO);
}
void print_redir_sign(t_redirection_sign redir_sign)
{
if (redir_sign == HEREDOC)
ft_putendl_fd("HEREDOC", STDOUT_FILENO);
else if (redir_sign == INFILE)
ft_putendl_fd("INFILE", STDOUT_FILENO);
else if (redir_sign == OUTFILE)
ft_putendl_fd("OUTFILE", STDOUT_FILENO);
else if (redir_sign == OUT_APPEND)
ft_putendl_fd("OUT_APPEND", STDOUT_FILENO);
}
void print_redir(t_redirection *redir)
{
ft_putstr_fd("file_name : ", STDOUT_FILENO);
ft_putendl_fd(redir->file_name, STDOUT_FILENO);
ft_putstr_fd("", STDOUT_FILENO);
print_redir_sign(redir->sign);
}
void print_token(t_token *token)
{
t_list *tmp;
tmp = token->redirection;
while (tmp)
{
print_redir((t_redirection*)tmp->content);
tmp = tmp->next;
}
ft_putendl_fd(token->argv, STDOUT_FILENO);
}

66
src/parsing/is_inquote.c Normal file
View File

@ -0,0 +1,66 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* is_inquote.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/20 20:06:13 by adjoly #+# #+# */
/* Updated: 2024/05/21 20:34:14 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "parsing.h"
#include "libft.h"
t_quote __is_quote(char c)
{
if (c == 39)
return (SINGLE);
if (c == 34)
return (DOUBLE);
return (FALSE);
}
char *search_for_next_quote(char *s, t_quote quote_type)
{
char *tmp;
tmp = s;
while (*tmp)
{
if (__is_quote(*tmp) == quote_type)
break ;
tmp++;
}
return (tmp);
}
t_quote is_inquote(char *s, size_t i)
{
char *tmp;
size_t start_quote;
t_quote quote_type;
start_quote = 0;
tmp = s;
quote_type = FALSE;
while (*tmp)
{
if ((size_t)(tmp - s) > i)
break ;
if (__is_quote(*tmp) != FALSE)
{
start_quote = tmp - s;
quote_type = __is_quote(*tmp);
tmp = search_for_next_quote(tmp, quote_type);
tmp++;
if (*tmp && (start_quote < i && (size_t)(tmp - s) > i))
return (quote_type);
else if (!*tmp)
return (NOT_CLOSED);
}
tmp++;
}
return (FALSE);
}

View File

@ -0,0 +1,77 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* __to_token.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/20 21:05:04 by adjoly #+# #+# */
/* Updated: 2024/05/23 20:12:03 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "tokenizer.h"
t_redirection_sign __to_redir_sign(char *redir_sign)
{
if (redir_sign[0] == '<' && redir_sign[1] != '<')
return (INFILE);
else if (redir_sign[0] == '<' && redir_sign[1] == '<')
return (HEREDOC);
else if (redir_sign[0] == '>' && redir_sign[1] != '>')
return (OUT_APPEND);
else if (redir_sign[0] == '>' && redir_sign[1] != '>')
return (OUTFILE);
return (0);
}
t_redirection *__to_redir(char *redir_s)
{
char *tmp;
t_redirection *redir;
char *start_of_filename;
char *filename;
redir = ft_calloc(sizeof(t_redirection), 1);
redir->sign = __to_redir_sign(redir_s);
tmp = redir_s;
if (!redir->sign)
return (NULL);
if (redir->sign % 2)
tmp++;
else
tmp += 2;
while (*tmp && *tmp != ' ')
tmp++;
start_of_filename = tmp;
while (*tmp && !ft_isalnum(*tmp))
tmp++;
filename = ft_calloc(tmp - start_of_filename + 1, sizeof(char));
ft_strlcpy(filename, start_of_filename, (tmp - start_of_filename));
redir->file_name = filename;
return (redir);
}
t_token *__to_token(char *cmd)
{
char *tmp;
t_token *token;
t_redirection *tmp_redir;
char *argv;
tmp = cmd;
argv = NULL;
token = ft_calloc(sizeof(t_token), 1);
while (*tmp)
{
if (*tmp == '<' || *tmp == '>')
{
tmp_redir = __to_redir(tmp);
ft_lstadd_back(&(token->redirection), ft_lstnew((void *)&tmp_redir));
}
else
ft_strjoin_free_s1(argv, &(*tmp));
tmp++;
}
return (token);
}

View File

@ -1,34 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* split_pipe.c :+: :+: :+: */
/* tokenizer.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 13:26:40 by adjoly #+# #+# */
/* Updated: 2024/05/18 17:12:42 by adjoly ### ########.fr */
/* Created: 2024/05/18 20:13:50 by adjoly #+# #+# */
/* Updated: 2024/05/20 22:51:45 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include "parsing.h"
#include "tokenizer.h"
t_list *split_pipe(char *readline)
/*t_list *tokenizer(char *readline)
{
char **split;
char **tmp;
t_list *list;
split = ft_split(readline, '|');
tmp = split;
if (!*(split+1))
return (ft_lstnew((void *)readline));
list = NULL;
while (tmp && *tmp)
t_list *token;
t_list *piped;
piped = __split_pipe(readline);
while (piped && *piped)
{
ft_lstadd_back(&list, ft_lstnew((void *)(*tmp)));
tmp++;
}
free(split);
return (list);
}
}*/