mirror of
https://github.com/KeyZox71/ft_minipowershell.git
synced 2025-05-13 16:08:45 +02:00
「🏗️」 wip: __to_token nearly done
This commit is contained in:
40
src/main.c
40
src/main.c
@ -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
46
src/parsing/debug_print.c
Normal 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
66
src/parsing/is_inquote.c
Normal 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);
|
||||
}
|
77
src/parsing/tokenizer/__to_token.c
Normal file
77
src/parsing/tokenizer/__to_token.c
Normal 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);
|
||||
}
|
@ -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);
|
||||
}
|
||||
}*/
|
Reference in New Issue
Block a user