」 feat: Parsing of command kinda working

This commit is contained in:
2024-05-30 17:59:49 +02:00
parent d84749d009
commit 6bbdef9d7c
14 changed files with 248 additions and 33 deletions

View File

@ -6,14 +6,19 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/20 20:25:06 by adjoly #+# #+# */ /* Created: 2024/05/20 20:25:06 by adjoly #+# #+# */
/* Updated: 2024/05/20 21:00:45 by adjoly ### ########.fr */ /* Updated: 2024/05/30 12:53:09 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef ERROR_MSG_H #ifndef ERROR_MSG_H
# define ERROR_MSG_H # define ERROR_MSG_H
/** /**
* Here we define all the error message * Here we define all the error message
*/ */
#define ERROR_SYNTAX ": syntax error"
#define ERROR_NO_REDIR ": need redirection file"
#define ERROR_NO_EOF ": need delimiter to heredoc"
#endif #endif

View File

@ -32,5 +32,6 @@
char set_env(char **env, const char *name, char *content); char set_env(char **env, const char *name, char *content);
bool is_str(char *src, char *dst); bool is_str(char *src, char *dst);
void print_cmd(t_cmd *cmd);
#endif #endif

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/25 12:20:26 by adjoly #+# #+# */ /* Created: 2024/04/25 12:20:26 by adjoly #+# #+# */
/* Updated: 2024/05/23 19:56:20 by adjoly ### ########.fr */ /* Updated: 2024/05/30 16:31:48 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,7 +19,7 @@
typedef struct s_cmd typedef struct s_cmd
{ {
char *cmd; char *cmd;
char **argv; char *argv;
int infile; int infile;
int outfile; int outfile;
} t_cmd; } t_cmd;
@ -32,6 +32,10 @@ typedef enum s_quote
DOUBLE DOUBLE
} t_quote; } t_quote;
void check_syntax(char *readline, char **argv);
void send_error(char *msg, char **argv);
void check_redir(t_list *redir, char **argv);
t_cmd *get_redir_fd(void *content);
/** /**
* @brief Take the argv of a command a split the argv and the * @brief Take the argv of a command a split the argv and the
* command it self * command it self

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/24 11:18:04 by adjoly #+# #+# */ /* Created: 2024/04/24 11:18:04 by adjoly #+# #+# */
/* Updated: 2024/05/27 18:58:13 by adjoly ### ########.fr */ /* Updated: 2024/05/30 16:37:57 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,6 +21,23 @@
#include "parsing.h" #include "parsing.h"
#include "prompt.h" #include "prompt.h"
void free_redir(void *redir_v)
{
t_redirection *redir;
redir = redir_v;
free(redir->file_name);
}
void free_token(void *token_v)
{
t_token *token;
token = token_v;
free(token->argv);
ft_lstclear(&(token->redirection), free_redir);
}
/*void print_cmd(t_cmd cmd) /*void print_cmd(t_cmd cmd)
{ {
ft_putendl_fd(cmd.cmd, 1); ft_putendl_fd(cmd.cmd, 1);
@ -51,31 +68,38 @@ int main(int ac, char **av, char **env)
char **lll; char **lll;
t_list *piped; t_list *piped;
t_env env_l; t_env env_l;
t_cmd *cmd;
//t_token *token; //t_token *token;
(void)ac; (void)ac;
(void)av; (void)av;
(void)env; (void)env;
if (!env_init(env, &env_l)) piped = NULL;
{ if (env_init(env, &env_l))
return (EXIT_FAILURE);
}
while (1) while (1)
{ {
prompt = get_prompt(env_l); prompt = get_prompt(env_l);
test = readline(prompt); test = readline(prompt);
free(prompt); free(prompt);
add_history(test); add_history(test);
check_syntax(test, av);
lll = ft_split(test, ' '); lll = ft_split(test, ' ');
if (!*lll) if (!*lll)
continue ; continue ;
if (is_str(test, "exit")) if (is_str(test, "exit"))
break ; break ;
piped = __split_pipe(test); piped = tokenizer(test);
print_redir(__to_redir(piped->content)); // check_redir(((t_token *)(piped->content))->redirection, av);
//free(token); /* while (piped)
{
print_token(piped->content);
piped = piped->next;
}*/
cmd = get_redir_fd(piped->content);
print_cmd(cmd);
free(test); free(test);
ft_lstclear(&piped, &free); ft_lstclear(&piped, free_token);
ft_free("a", &lll); ft_free("a", &lll);
} }
ft_free("a", &lll); ft_free("a", &lll);

View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_redir.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/28 18:17:26 by adjoly #+# #+# */
/* Updated: 2024/05/30 12:52:51 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "tokenizer.h"
#include "parsing.h"
#include "error_msg.h"
void check_redir(t_list *redir, char **argv)
{
t_list *tmp;
t_redirection *tmp_redir;
tmp = redir;
while(tmp)
{
tmp_redir = tmp->content;
if (tmp_redir->sign == HEREDOC && \
!((t_redirection *)(tmp->content))->file_name)
send_error(ERROR_NO_EOF, argv);
if (!((t_redirection *)(tmp->content))->file_name)
send_error(ERROR_NO_REDIR, argv);
tmp = tmp->next;
}
}

View File

@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_syntax.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/28 17:40:13 by adjoly #+# #+# */
/* Updated: 2024/05/29 11:37:06 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdbool.h>
#include <stdlib.h>
#include "libft.h"
#include "parsing.h"
#include "error_msg.h"
bool is_chevron(int c)
{
if (c == '<' || c == '>')
return (true);
return (false);
}
bool check_triple(char *chevron)
{
if (is_chevron(*chevron) && is_chevron(*(chevron + 1)) \
&& is_chevron(*(chevron + 2)))
return (true);
return (false);
}
void check_syntax(char *readline, char **argv)
{
char *tmp;
tmp = readline;
while (*tmp)
{
if (check_triple(tmp))
send_error(ERROR_SYNTAX, argv);
tmp++;
}
}

View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* send_error.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/28 18:09:49 by adjoly #+# #+# */
/* Updated: 2024/05/28 18:15:09 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void send_error(char *msg, char **argv)
{
ft_putstr_fd(argv[0], STDERR_FILENO);
ft_putendl_fd(msg, STDERR_FILENO);
exit(EXIT_FAILURE);
}

View File

@ -45,3 +45,17 @@ void print_token(t_token *token)
} }
ft_putendl_fd(token->argv, STDOUT_FILENO); ft_putendl_fd(token->argv, STDOUT_FILENO);
} }
void print_cmd(t_cmd *cmd)
{
ft_putstr_fd("INFILE fd : ", STDOUT_FILENO);
ft_putnbr_fd(cmd->infile, STDOUT_FILENO);
ft_putchar_fd('\n', STDOUT_FILENO);
ft_putstr_fd("OUTFILE fd : ", STDOUT_FILENO);
ft_putnbr_fd(cmd->outfile, STDOUT_FILENO);
ft_putchar_fd('\n', STDOUT_FILENO);
ft_putstr_fd("cmd : ", STDOUT_FILENO);
ft_putendl_fd(cmd->cmd, STDOUT_FILENO);
ft_putstr_fd("argv : ", STDOUT_FILENO);
ft_putendl_fd(cmd->argv, STDOUT_FILENO);
}

View File

@ -0,0 +1,70 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_redir_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/30 10:48:41 by adjoly #+# #+# */
/* Updated: 2024/05/30 17:47:43 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "parsing.h"
#include "tokenizer.h"
#include "execution.h"
#include <fcntl.h>
#include "libft.h"
t_cmd *get_redir_fd(void *content)
{
t_token *token;
t_list *tmp;
t_redirection *tmp_redir;
t_redirection out;
t_redirection in;
t_cmd *cmd;
token = (t_token *)content;
tmp = token->redirection;
cmd = NULL;
out.sign = INFILE;
in.sign = OUTFILE;
cmd = ft_calloc(sizeof(t_cmd), 1);
while (tmp)
{
tmp_redir = (t_redirection *)tmp->content;
if (tmp_redir->sign == (t_redirection_sign)HEREDOC)
{
in.file_name = NULL;
in.sign = HEREDOC;
close(cmd->infile);
cmd->infile = ft_heredoc(tmp_redir->file_name);
}
else if (tmp_redir->sign == INFILE)
{
in.sign = INFILE;
in.file_name = tmp_redir->file_name;
}
else
{
out.sign = tmp_redir->sign;
out.file_name = tmp_redir->file_name;
}
tmp = tmp->next;
}
if (in.sign == OUTFILE)
cmd->infile = STDIN_FILENO;
else if (in.sign == INFILE)
{
cmd->infile = open(in.file_name, O_RDONLY);
ft_putendl_fd(in.file_name, STDOUT_FILENO);
}
if (out.sign == INFILE)
cmd->outfile = STDOUT_FILENO;
else if (out.sign == OUTFILE)
cmd->outfile = open(out.file_name, O_CREAT | O_TRUNC | O_WRONLY);
else if (out.sign == OUT_APPEND)
cmd->outfile = open(out.file_name, O_CREAT | O_APPEND | O_WRONLY);
return (cmd);
}

View File

@ -6,14 +6,14 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/04 15:00:32 by adjoly #+# #+# */ /* Created: 2024/05/04 15:00:32 by adjoly #+# #+# */
/* Updated: 2024/05/07 13:56:57 by adjoly ### ########.fr */ /* Updated: 2024/05/30 16:31:04 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "parsing.h" #include "parsing.h"
#include "libft.h" #include "libft.h"
t_cmd *split_cmd(char *cmd_av) /*t_cmd *split_cmd(char *cmd_av)
{ {
char **split; char **split;
char **tmp_split; char **tmp_split;
@ -25,4 +25,4 @@ t_cmd *split_cmd(char *cmd_av)
cmd->cmd = ft_strdup(*tmp_split); cmd->cmd = ft_strdup(*tmp_split);
cmd->argv = tmp_split; cmd->argv = tmp_split;
return (cmd); return (cmd);
} }*/

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/20 20:01:25 by adjoly #+# #+# */ /* Created: 2024/05/20 20:01:25 by adjoly #+# #+# */
/* Updated: 2024/05/22 11:42:47 by adjoly ### ########.fr */ /* Updated: 2024/05/28 16:43:21 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -30,10 +30,7 @@ t_list *__split_pipe(char *readline)
{ {
tmp_pipe = ft_calloc(tmp - start_of_pipe + 1, sizeof(char)); tmp_pipe = ft_calloc(tmp - start_of_pipe + 1, sizeof(char));
if (!tmp_pipe) if (!tmp_pipe)
{ return (ft_lstclear(&pipe, free), NULL);
ft_lstclear(&pipe, free);
return (NULL);
}
ft_strlcpy(tmp_pipe, start_of_pipe, (tmp - start_of_pipe) + 1); ft_strlcpy(tmp_pipe, start_of_pipe, (tmp - start_of_pipe) + 1);
ft_lstadd_back(&pipe, ft_lstnew((void *)(tmp_pipe))); ft_lstadd_back(&pipe, ft_lstnew((void *)(tmp_pipe)));
start_of_pipe = tmp + 1; start_of_pipe = tmp + 1;

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/25 15:06:15 by adjoly #+# #+# */ /* Created: 2024/05/25 15:06:15 by adjoly #+# #+# */
/* Updated: 2024/05/26 16:39:27 by adjoly ### ########.fr */ /* Updated: 2024/05/30 15:47:19 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,18 +19,20 @@ t_redirection *__to_redir(char *redir_s)
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);
if (redir->sign == HEREDOC || redir->sign == OUT_APPEND) redir->file_name = NULL;
if (redir->sign == OUT_APPEND || redir->sign == HEREDOC)
redir_s += 2; redir_s += 2;
else else
redir_s++; redir_s++;
while (*redir_s && *redir_s == ' ') while (*redir_s && *redir_s == ' ')
redir_s++; redir_s++;
tmp = redir_s; tmp = redir_s;
if (!ft_isalnum(*tmp))
return (redir);
while (*tmp && ft_isalnum(*tmp)) while (*tmp && ft_isalnum(*tmp))
tmp++; tmp++;
redir->file_name = ft_calloc(tmp - redir_s + 1, sizeof(char)); redir->file_name = ft_calloc(tmp - redir_s + 1, sizeof(char));
ft_strlcpy(redir->file_name, redir_s, tmp - redir_s + 1); ft_strlcpy(redir->file_name, redir_s, tmp - redir_s + 1);
if (redir->sign != HEREDOC)
redir_s += (tmp - redir_s); redir_s += (tmp - redir_s);
return (redir); return (redir);
} }

View File

@ -6,7 +6,7 @@
/* 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/28 16:28:24 by adjoly ### ########.fr */ /* Updated: 2024/05/30 16:37:16 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,9 +21,7 @@ size_t __get_sizeof_redir(char *redir_s, t_redirection *redir)
if (!redir_s || !redir) if (!redir_s || !redir)
return (0); return (0);
if (redir->sign == HEREDOC) else if (redir->sign == OUT_APPEND || redir->sign == HEREDOC)
return (1);
else if (redir->sign == OUT_APPEND)
i = 1; i = 1;
else else
i = 0; i = 0;
@ -33,6 +31,8 @@ size_t __get_sizeof_redir(char *redir_s, t_redirection *redir)
while (*++tmp && *tmp == ' ') while (*++tmp && *tmp == ' ')
i++; i++;
i += ft_strlen(redir->file_name); i += ft_strlen(redir->file_name);
//if (redir->sign == OUT_APPEND || redir->sign == HEREDOC)
//i++;
return (i); return (i);
} }

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/18 20:13:50 by adjoly #+# #+# */ /* Created: 2024/05/18 20:13:50 by adjoly #+# #+# */
/* Updated: 2024/05/28 14:25:18 by adjoly ### ########.fr */ /* Updated: 2024/05/28 16:41:49 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */