mirror of
https://github.com/KeyZox71/ft_minipowershell.git
synced 2025-03-15 03:16:51 +01:00
「✨」 feat: Parsing of command kinda working
This commit is contained in:
@ -6,14 +6,19 @@
|
||||
/* 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 */
|
||||
/* Updated: 2024/05/30 12:53:09 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef ERROR_MSG_H
|
||||
# define ERROR_MSG_H
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
@ -32,5 +32,6 @@
|
||||
|
||||
char set_env(char **env, const char *name, char *content);
|
||||
bool is_str(char *src, char *dst);
|
||||
void print_cmd(t_cmd *cmd);
|
||||
|
||||
#endif
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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
|
||||
{
|
||||
char *cmd;
|
||||
char **argv;
|
||||
char *argv;
|
||||
int infile;
|
||||
int outfile;
|
||||
} t_cmd;
|
||||
@ -32,6 +32,10 @@ typedef enum s_quote
|
||||
DOUBLE
|
||||
} 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
|
||||
* command it self
|
||||
|
44
src/main.c
44
src/main.c
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 "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)
|
||||
{
|
||||
ft_putendl_fd(cmd.cmd, 1);
|
||||
@ -51,31 +68,38 @@ int main(int ac, char **av, char **env)
|
||||
char **lll;
|
||||
t_list *piped;
|
||||
t_env env_l;
|
||||
t_cmd *cmd;
|
||||
//t_token *token;
|
||||
|
||||
(void)ac;
|
||||
(void)av;
|
||||
(void)env;
|
||||
if (!env_init(env, &env_l))
|
||||
{
|
||||
|
||||
}
|
||||
piped = NULL;
|
||||
if (env_init(env, &env_l))
|
||||
return (EXIT_FAILURE);
|
||||
while (1)
|
||||
{
|
||||
prompt = get_prompt(env_l);
|
||||
test = readline(prompt);
|
||||
free(prompt);
|
||||
add_history(test);
|
||||
check_syntax(test, av);
|
||||
lll = ft_split(test, ' ');
|
||||
if (!*lll)
|
||||
continue ;
|
||||
if (is_str(test, "exit"))
|
||||
break;
|
||||
piped = __split_pipe(test);
|
||||
print_redir(__to_redir(piped->content));
|
||||
//free(token);
|
||||
break ;
|
||||
piped = tokenizer(test);
|
||||
// check_redir(((t_token *)(piped->content))->redirection, av);
|
||||
/* while (piped)
|
||||
{
|
||||
print_token(piped->content);
|
||||
piped = piped->next;
|
||||
}*/
|
||||
cmd = get_redir_fd(piped->content);
|
||||
print_cmd(cmd);
|
||||
free(test);
|
||||
ft_lstclear(&piped, &free);
|
||||
ft_lstclear(&piped, free_token);
|
||||
ft_free("a", &lll);
|
||||
}
|
||||
ft_free("a", &lll);
|
||||
|
33
src/parsing/check_error/check_redir.c
Normal file
33
src/parsing/check_error/check_redir.c
Normal 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;
|
||||
}
|
||||
}
|
45
src/parsing/check_error/check_syntax.c
Normal file
45
src/parsing/check_error/check_syntax.c
Normal 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++;
|
||||
}
|
||||
}
|
20
src/parsing/check_error/send_error.c
Normal file
20
src/parsing/check_error/send_error.c
Normal 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);
|
||||
}
|
@ -45,3 +45,17 @@ void print_token(t_token *token)
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
70
src/parsing/get_redir_fd.c
Normal file
70
src/parsing/get_redir_fd.c
Normal 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);
|
||||
}
|
@ -6,14 +6,14 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 "libft.h"
|
||||
|
||||
t_cmd *split_cmd(char *cmd_av)
|
||||
/*t_cmd *split_cmd(char *cmd_av)
|
||||
{
|
||||
char **split;
|
||||
char **tmp_split;
|
||||
@ -25,4 +25,4 @@ t_cmd *split_cmd(char *cmd_av)
|
||||
cmd->cmd = ft_strdup(*tmp_split);
|
||||
cmd->argv = tmp_split;
|
||||
return (cmd);
|
||||
}
|
||||
}*/
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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));
|
||||
if (!tmp_pipe)
|
||||
{
|
||||
ft_lstclear(&pipe, free);
|
||||
return (NULL);
|
||||
}
|
||||
return (ft_lstclear(&pipe, free), NULL);
|
||||
ft_strlcpy(tmp_pipe, start_of_pipe, (tmp - start_of_pipe) + 1);
|
||||
ft_lstadd_back(&pipe, ft_lstnew((void *)(tmp_pipe)));
|
||||
start_of_pipe = tmp + 1;
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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->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;
|
||||
else
|
||||
redir_s++;
|
||||
while (*redir_s && *redir_s == ' ')
|
||||
redir_s++;
|
||||
tmp = redir_s;
|
||||
if (!ft_isalnum(*tmp))
|
||||
return (redir);
|
||||
while (*tmp && ft_isalnum(*tmp))
|
||||
tmp++;
|
||||
redir->file_name = ft_calloc(tmp - redir_s + 1, sizeof(char));
|
||||
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);
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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)
|
||||
return (0);
|
||||
if (redir->sign == HEREDOC)
|
||||
return (1);
|
||||
else if (redir->sign == OUT_APPEND)
|
||||
else if (redir->sign == OUT_APPEND || redir->sign == HEREDOC)
|
||||
i = 1;
|
||||
else
|
||||
i = 0;
|
||||
@ -33,6 +31,8 @@ size_t __get_sizeof_redir(char *redir_s, t_redirection *redir)
|
||||
while (*++tmp && *tmp == ' ')
|
||||
i++;
|
||||
i += ft_strlen(redir->file_name);
|
||||
//if (redir->sign == OUT_APPEND || redir->sign == HEREDOC)
|
||||
//i++;
|
||||
return (i);
|
||||
}
|
||||
|
||||
@ -50,7 +50,7 @@ t_token *__to_token(char *cmd)
|
||||
if (*tmp == '<' || *tmp == '>')
|
||||
{
|
||||
tmp_redir = __to_redir(tmp);
|
||||
ft_lstadd_back(&(token->redirection), ft_lstnew((void*)tmp_redir));
|
||||
ft_lstadd_back(&(token->redirection), ft_lstnew((void *)tmp_redir));
|
||||
tmp += __get_sizeof_redir(tmp, tmp_redir);
|
||||
}
|
||||
else
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -17,13 +17,13 @@ t_list *tokenizer(char *readline)
|
||||
t_list *token;
|
||||
t_list *piped;
|
||||
t_list *tmp;
|
||||
|
||||
|
||||
piped = __split_pipe(readline);
|
||||
token = NULL;
|
||||
tmp = piped;
|
||||
while (tmp)
|
||||
{
|
||||
ft_lstadd_back(&token, ft_lstnew((void*)__to_token(tmp->content)));
|
||||
ft_lstadd_back(&token, ft_lstnew((void *)__to_token(tmp->content)));
|
||||
tmp = tmp->next;
|
||||
}
|
||||
ft_lstclear(&piped, free);
|
||||
|
Reference in New Issue
Block a user