」 feat: __to_redir and __to_redir_sign work

This commit is contained in:
2024-05-25 14:06:42 +02:00
parent 6a8a6382bd
commit f88e6380c0
6 changed files with 41 additions and 29 deletions

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/18 20:14:15 by adjoly #+# #+# */ /* Created: 2024/05/18 20:14:15 by adjoly #+# #+# */
/* Updated: 2024/05/23 12:11:42 by adjoly ### ########.fr */ /* Updated: 2024/05/24 15:00:52 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -35,11 +35,28 @@ typedef struct s_token
t_list *redirection; t_list *redirection;
} t_token; } t_token;
/**
* @brief Take a string and tell what type of redirect it is
*
* @param A string that contain a redirection sign
*
* @return (t_redirection_sign) The sign of the redirecition
*/
t_redirection_sign __to_redir_sign(char *redir_sign);
/**
* @brief Take a string and split the filename and the redirect sign
* @param redir_s A string that contain a redirection
*
* @return (t_redirection) The splited redirection
*/
t_redirection *__to_redir(char *redir_s);
/** /**
* @brief Convert the raw command into a t_token that contains * @brief Convert the raw command into a t_token that contains
* the argv of the command an a linked list of redirection * the argv of the command an a linked list of redirection
* *
* @param cmd A string that containt the command to tokenize * @param cmd A string that contain the command to tokenize
* *
* @return (t_token *) The tokenized version of the command * @return (t_token *) The tokenized version of the command
* *

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/24 11:18:04 by adjoly #+# #+# */ /* Created: 2024/04/24 11:18:04 by adjoly #+# #+# */
/* Updated: 2024/05/23 20:06:16 by adjoly ### ########.fr */ /* Updated: 2024/05/24 15:01:05 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -49,7 +49,7 @@ int main(int ac, char **av, char **env)
char *prompt; char *prompt;
char **lll; char **lll;
t_list *piped; t_list *piped;
t_token *token; //t_token *token;
(void)ac; (void)ac;
(void)av; (void)av;
@ -66,9 +66,8 @@ int main(int ac, char **av, char **env)
if (is_str(test, "exit")) if (is_str(test, "exit"))
break; break;
piped = __split_pipe(test); piped = __split_pipe(test);
token = __to_token(piped->content); print_redir(__to_redir(piped->content));
print_token(token); //free(token);
free(token);
free(test); free(test);
ft_lstclear(&piped, &free); ft_lstclear(&piped, &free);
} }

View File

@ -27,7 +27,8 @@ void print_redir_sign(t_redirection_sign redir_sign)
void print_redir(t_redirection *redir) void print_redir(t_redirection *redir)
{ {
ft_putstr_fd("file_name : ", STDOUT_FILENO); ft_putstr_fd("file_name : ", STDOUT_FILENO);
ft_putendl_fd(redir->file_name, STDOUT_FILENO); if (redir->file_name)
ft_putendl_fd(redir->file_name, STDOUT_FILENO);
ft_putstr_fd("", STDOUT_FILENO); ft_putstr_fd("", STDOUT_FILENO);
print_redir_sign(redir->sign); print_redir_sign(redir->sign);
} }

View File

@ -6,11 +6,12 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/20 21:05:04 by adjoly #+# #+# */ /* Created: 2024/05/20 21:05:04 by adjoly #+# #+# */
/* Updated: 2024/05/23 20:12:03 by adjoly ### ########.fr */ /* Updated: 2024/05/25 14:05:02 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "tokenizer.h" #include "tokenizer.h"
#include "parsing.h"
t_redirection_sign __to_redir_sign(char *redir_sign) t_redirection_sign __to_redir_sign(char *redir_sign)
{ {
@ -18,41 +19,35 @@ t_redirection_sign __to_redir_sign(char *redir_sign)
return (INFILE); return (INFILE);
else if (redir_sign[0] == '<' && redir_sign[1] == '<') else if (redir_sign[0] == '<' && redir_sign[1] == '<')
return (HEREDOC); return (HEREDOC);
else if (redir_sign[0] == '>' && redir_sign[1] != '>') else if (redir_sign[0] == '>' && redir_sign[1] == '>')
return (OUT_APPEND); return (OUT_APPEND);
else if (redir_sign[0] == '>' && redir_sign[1] != '>') else if (redir_sign[0] == '>' && redir_sign[1] != '>')
return (OUTFILE); return (OUTFILE);
return (0); return (0);
} }
t_redirection *__to_redir(char *redir_s) t_redirection *__to_redir(char *redir_s)
{ {
char *tmp;
t_redirection *redir; t_redirection *redir;
char *start_of_filename; char *tmp;
char *filename;
redir = ft_calloc(sizeof(t_redirection), 1); redir = ft_calloc(sizeof(t_redirection), 1);
redir->sign = __to_redir_sign(redir_s); redir->sign = __to_redir_sign(redir_s);
tmp = redir_s; if (redir->sign == HEREDOC || redir->sign == OUT_APPEND)
if (!redir->sign) redir_s += 2;
return (NULL);
if (redir->sign % 2)
tmp++;
else else
tmp += 2; redir_s++;
while (*tmp && *tmp != ' ') while (*redir_s && *redir_s == ' ')
redir_s++;
tmp = redir_s;
while (*tmp && ft_isalnum(*tmp))
tmp++; tmp++;
start_of_filename = tmp; redir->file_name = ft_calloc(tmp - redir_s + 1, sizeof(char));
while (*tmp && !ft_isalnum(*tmp)) ft_strlcpy(redir->file_name, redir_s, tmp - redir_s + 1);
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); return (redir);
} }
t_token *__to_token(char *cmd) /*t_token *__to_token(char *cmd)
{ {
char *tmp; char *tmp;
t_token *token; t_token *token;
@ -74,4 +69,4 @@ t_token *__to_token(char *cmd)
tmp++; tmp++;
} }
return (token); return (token);
} }*/

BIN
vgcore.421022 Normal file

Binary file not shown.

BIN
vgcore.421116 Normal file

Binary file not shown.