mirror of
https://github.com/KeyZox71/ft_minipowershell.git
synced 2025-03-15 03:16:51 +01:00
「🔨」 fix: parsing reworked
This commit is contained in:
69
Makefile
69
Makefile
@ -6,7 +6,7 @@
|
||||
# By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2024/04/24 10:49:52 by adjoly #+# #+# #
|
||||
# Updated: 2024/07/29 19:26:36 by adjoly ### ########.fr #
|
||||
# Updated: 2024/08/06 15:31:02 by adjoly ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
@ -18,70 +18,7 @@ CC = cc
|
||||
|
||||
OBJSDIR = obj/
|
||||
|
||||
SRC = src/utils/free_list.c \
|
||||
src/utils/sig.c \
|
||||
src/utils/ft_arrlen.c \
|
||||
src/utils/get.c \
|
||||
src/utils/ret_cwd.c \
|
||||
src/utils/get_to_free.c \
|
||||
src/utils/is_str.c \
|
||||
src/builtins/ft_echo.c \
|
||||
src/builtins/free_exit.c \
|
||||
src/builtins/ft_export_utils2.c \
|
||||
src/builtins/ft_cd.c \
|
||||
src/builtins/ft_unset.c \
|
||||
src/builtins/ft_env.c \
|
||||
src/builtins/ft_pwd.c \
|
||||
src/builtins/ft_exit.c \
|
||||
src/builtins/ft_export.c \
|
||||
src/builtins/ft_export_utils.c \
|
||||
src/env/env_cmd.c \
|
||||
src/env/env_list/ft_envnew.c \
|
||||
src/env/env_list/ft_envprint.c \
|
||||
src/env/env_list/ft_envadd_back.c \
|
||||
src/env/env_list/ft_envsize.c \
|
||||
src/env/env_list/ft_envdelone.c \
|
||||
src/env/env_list/ft_envadd_front.c \
|
||||
src/env/env_list/ft_envlast.c \
|
||||
src/env/env_list/ft_envclear.c \
|
||||
src/env/env_setters.c \
|
||||
src/main.c \
|
||||
src/prompt/get_pwd.c \
|
||||
src/prompt/get_prompt.c \
|
||||
src/prompt/get_hostname.c \
|
||||
src/exec/sig.c \
|
||||
src/exec/format_quotes.c \
|
||||
src/exec/heredoc_utils.c \
|
||||
src/exec/free_exec.c \
|
||||
src/exec/exec_cmd.c \
|
||||
src/exec/exec_split_cmd.c \
|
||||
src/exec/utils_exec_2.c \
|
||||
src/exec/utils_exec.c \
|
||||
src/exec/heredoc.c \
|
||||
src/exec/get_path.c \
|
||||
src/parsing/is_inquote.c \
|
||||
src/parsing/get_redir_fd.c \
|
||||
src/parsing/split_cmd.c \
|
||||
src/parsing/is_quote.c \
|
||||
src/parsing/open_redir.c \
|
||||
src/parsing/tokenizer/__to_redir_sign.c \
|
||||
src/parsing/tokenizer/__split_pipe.c \
|
||||
src/parsing/tokenizer/__to_token.c \
|
||||
src/parsing/tokenizer/__to_redir.c \
|
||||
src/parsing/tokenizer/tokenizer.c \
|
||||
src/parsing/check_error/run_checks.c \
|
||||
src/parsing/check_error/check_redir.c \
|
||||
src/parsing/check_error/check_wspace.c \
|
||||
src/parsing/check_error/check_argv.c \
|
||||
src/parsing/check_error/check_syntax.c \
|
||||
src/parsing/check_error/send_error.c \
|
||||
src/parsing/check_error/check_pipe.c \
|
||||
src/parsing/check_error/check_quote.c \
|
||||
src/parsing/get_cmd_list.c \
|
||||
src/parsing/env_var/env_var_replace.c \
|
||||
src/parsing/env_var/strlen_till_char.c \
|
||||
src/parsing/env_var/get_size_with_env.c \
|
||||
src/parsing/split_argv.c
|
||||
SRC = $(shell find src -name '*.c')
|
||||
|
||||
I_DIR = include/
|
||||
|
||||
@ -91,7 +28,7 @@ INCLUDE = -I $(I_DIR) -I $(LIBFT_DIR)/$(I_DIR)
|
||||
|
||||
OBJS = $(addprefix $(OBJSDIR), $(SRC:.c=.o))
|
||||
|
||||
FLAGS = -Werror -Wall -Wextra -g
|
||||
FLAGS = -Wall -Werror -Wextra -g
|
||||
|
||||
LIB = libft/libft.a
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/04/29 13:20:22 by mmoussou #+# #+# */
|
||||
/* Updated: 2024/08/01 16:39:11 by mmoussou ### ########.fr */
|
||||
/* Updated: 2024/08/06 17:00:05 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -16,7 +16,6 @@
|
||||
# include "env.h"
|
||||
# include "libft.h"
|
||||
# include "parsing.h"
|
||||
# include "tokenizer.h"
|
||||
|
||||
typedef struct s_exec
|
||||
{
|
||||
@ -26,7 +25,7 @@ typedef struct s_exec
|
||||
int pipe_fd[2];
|
||||
} t_exec;
|
||||
|
||||
int format_quotes(t_list *list_cmd);
|
||||
void format_quotes(char **content);
|
||||
|
||||
int exec_single_cmd_execution(t_cmd *cmd, char **env,
|
||||
t_env *env_t, t_exec exec);
|
||||
@ -52,7 +51,7 @@ void __fork_single_cmd(t_cmd *cmd, char **env, t_env *env_t, t_exec exec);
|
||||
*
|
||||
* @return (int) fd of a file containing the user's input, or -1 on error
|
||||
*/
|
||||
int ft_heredoc(char *delimiter, t_cmd *cmd);
|
||||
int ft_heredoc(char *delimiter);
|
||||
int __open_fd_here(char *path, int mode);
|
||||
void ft_lstclear_till_nxt(t_list **lst, void (*del)(void *));
|
||||
int get_fd_heredoc(int in);
|
||||
|
@ -29,10 +29,10 @@
|
||||
# include <stdbool.h>
|
||||
# include "libft.h"
|
||||
|
||||
# include "parsing.h"
|
||||
# include "error_msg.h"
|
||||
# include "env.h"
|
||||
# include "prompt.h"
|
||||
# include "parsing.h"
|
||||
# include "execution.h"
|
||||
|
||||
void free_redir(void *redir_v);
|
||||
@ -41,7 +41,7 @@ void free_cmd(void *content);
|
||||
|
||||
t_env **get_env(t_env **env);
|
||||
t_list **get_list(t_list **list);
|
||||
t_list **get_list2(t_list **list);
|
||||
void *get_void(void *in);
|
||||
int get_exit_code(int in);
|
||||
size_t ft_arrlen(char **a);
|
||||
char **get_rl(char **rl);
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/04/25 12:20:26 by adjoly #+# #+# */
|
||||
/* Updated: 2024/08/02 17:17:39 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/08/06 15:14:32 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -14,9 +14,23 @@
|
||||
# define PARSING_H
|
||||
|
||||
# include "libft.h"
|
||||
# include "tokenizer.h"
|
||||
# include "env.h"
|
||||
|
||||
typedef enum s_redir_sign
|
||||
{
|
||||
ERROR = -1,
|
||||
INFILE,
|
||||
HEREDOC,
|
||||
OUTFILE,
|
||||
OUT_APPEND,
|
||||
} t_redir_sign;
|
||||
|
||||
typedef struct s_redir
|
||||
{
|
||||
int fd;
|
||||
t_redir_sign sign;
|
||||
} t_redir;
|
||||
|
||||
typedef struct s_cmd
|
||||
{
|
||||
char *cmd;
|
||||
@ -33,10 +47,6 @@ typedef enum s_quote
|
||||
DOUBLE = 34
|
||||
} t_quote;
|
||||
|
||||
t_cmd *get_redir_fd(void *content, t_list *tmp);
|
||||
t_list *get_cmd_list(t_list *list);
|
||||
void open_redir(t_redirection *redir, t_cmd *cmd, t_redir_sign sign[2]);
|
||||
|
||||
/**
|
||||
* @brief Take a string and a character and return the lengh
|
||||
* until the given character
|
||||
@ -46,13 +56,13 @@ void open_redir(t_redirection *redir, t_cmd *cmd, t_redir_sign sign[2]);
|
||||
*
|
||||
* @return (size_t) The lengh until the given character
|
||||
*/
|
||||
size_t strlen_till_char(char *s, int c);
|
||||
size_t strlen_till_char(char *s, int c);
|
||||
|
||||
bool check_quote(char *readline);
|
||||
bool check_syntax(char *readline);
|
||||
bool check_redir(t_list *redir);
|
||||
bool check_argv(t_list *token);
|
||||
bool check_wspace(char *readline);
|
||||
bool check_quote(char *readline);
|
||||
bool check_syntax(char *readline);
|
||||
bool check_redir(t_list *redir);
|
||||
bool check_argv(t_list *token);
|
||||
bool check_wspace(char *readline);
|
||||
/**
|
||||
* @brief Take the readline output and check if all the pipe
|
||||
* a command after them
|
||||
@ -61,7 +71,7 @@ bool check_wspace(char *readline);
|
||||
*
|
||||
* @return (bool) A boolean of whether or not there is an error
|
||||
*/
|
||||
bool check_pipe(char *readline);
|
||||
bool check_pipe(char *readline);
|
||||
|
||||
/**
|
||||
* @brief Take the readline output and split it into an argv
|
||||
@ -71,7 +81,7 @@ bool check_pipe(char *readline);
|
||||
*
|
||||
* @return (char **) The argv of the command
|
||||
*/
|
||||
char **split_argv(char *readline);
|
||||
char **split_argv(char *readline);
|
||||
|
||||
/**
|
||||
* @brief Take the argv of a command a split the argv and the
|
||||
@ -81,7 +91,7 @@ char **split_argv(char *readline);
|
||||
*
|
||||
* @return (t_cmd *) cmd and argv splited into a struct
|
||||
*/
|
||||
t_cmd *split_cmd(char *cmd_av, t_cmd *cmd);
|
||||
t_cmd *split_cmd(char *cmd_av, t_cmd *cmd);
|
||||
|
||||
/**
|
||||
* @brief Take a string and an index and check if the character
|
||||
@ -93,7 +103,7 @@ t_cmd *split_cmd(char *cmd_av, t_cmd *cmd);
|
||||
* @return (t_quote) The type of quote if between, if not return FALSE or
|
||||
* NOT_CLOSED if the quote is not closed
|
||||
*/
|
||||
t_quote is_inquote(char *s, size_t i);
|
||||
t_quote is_inquote(char *s, size_t i);
|
||||
|
||||
/**
|
||||
* @brief Take a character and check if it is a quote and return
|
||||
@ -103,7 +113,7 @@ t_quote is_inquote(char *s, size_t i);
|
||||
*
|
||||
* @return (t_quote) The type of quote or FALSE
|
||||
*/
|
||||
t_quote __is_quote(char c);
|
||||
t_quote __is_quote(char c);
|
||||
|
||||
/**
|
||||
* @brief Take a string and a quote type and return the pointer to
|
||||
@ -114,7 +124,7 @@ t_quote __is_quote(char c);
|
||||
*
|
||||
* @return (char *) A pointer to the next quote
|
||||
*/
|
||||
char *search_for_next_quote(char *s, t_quote quote_type);
|
||||
char *search_for_next_quote(char *s, t_quote quote_type);
|
||||
|
||||
/**
|
||||
* @brief Take the readline output and the env and replace all the
|
||||
@ -125,8 +135,36 @@ char *search_for_next_quote(char *s, t_quote quote_type);
|
||||
*
|
||||
* @return (char *) The curated string
|
||||
*/
|
||||
char *env_var_replace(char *readline, t_env *env);
|
||||
size_t get_size_with_env(char *readline, t_env *env);
|
||||
size_t strlen_till_notalnum(char *s);
|
||||
char *env_var_replace(char *readline, t_env *env);
|
||||
size_t get_size_with_env(char *readline, t_env *env);
|
||||
size_t strlen_till_notalnum(char *s);
|
||||
|
||||
t_cmd *__to_cmd(char **content);
|
||||
|
||||
/**
|
||||
* @brief Split a readline output into an argv
|
||||
*
|
||||
* @param The readline output
|
||||
*
|
||||
* @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)
|
||||
*
|
||||
* @param readline The readline output
|
||||
*
|
||||
* @return (t_list *) Linked list of t_token
|
||||
*
|
||||
*/
|
||||
t_list *tokenizer(char *readline, t_env *env);
|
||||
|
||||
t_redir_sign __to_redir_sign(char *redir_sign);
|
||||
t_redir *__open_heredoc(char *filename);
|
||||
t_redir *__to_redir(char *filename, t_redir_sign sign);
|
||||
void __get_fd(t_list *list, t_cmd *cmd);
|
||||
char **__get_argv(char **content);
|
||||
#endif
|
||||
|
@ -1,89 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* tokenizer.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/18 20:14:15 by adjoly #+# #+# */
|
||||
/* Updated: 2024/07/20 16:52:51 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef TOKENIZER_H
|
||||
# define TOKENIZER_H
|
||||
|
||||
# include "libft.h"
|
||||
|
||||
typedef enum s_redir_sign
|
||||
{
|
||||
INFILE,
|
||||
HEREDOC,
|
||||
OUTFILE,
|
||||
OUT_APPEND,
|
||||
} t_redir_sign;
|
||||
|
||||
typedef struct s_redirection
|
||||
{
|
||||
char *file_name;
|
||||
t_redir_sign sign;
|
||||
} t_redirection;
|
||||
|
||||
typedef struct s_token
|
||||
{
|
||||
char *argv;
|
||||
t_list *redirection;
|
||||
} 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_redir_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
|
||||
* the argv of the command an a linked list of redirection
|
||||
*
|
||||
* @param cmd A string that contain the command to tokenize
|
||||
*
|
||||
* @return (t_token *) The tokenized version of the command
|
||||
*
|
||||
*/
|
||||
t_token *__to_token(char *cmd);
|
||||
|
||||
/**
|
||||
* @brief
|
||||
*
|
||||
* @param The readline output
|
||||
*
|
||||
* @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)
|
||||
*
|
||||
* @param readline The readline output
|
||||
*
|
||||
* @return (t_list *) Linked list of t_token
|
||||
*
|
||||
*/
|
||||
t_list *tokenizer(char *readline);
|
||||
|
||||
#endif
|
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/03 10:50:52 by mmoussou #+# #+# */
|
||||
/* Updated: 2024/07/24 18:53:31 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/08/06 17:02:58 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -54,31 +54,14 @@ char *format_quotes_string(char *cmd)
|
||||
return (cmd);
|
||||
}
|
||||
|
||||
int format_quotes_cmd(t_cmd *cmd)
|
||||
void format_quotes(char **content)
|
||||
{
|
||||
uint i;
|
||||
char **argv;
|
||||
|
||||
if (!cmd || !cmd->cmd)
|
||||
return (0);
|
||||
cmd->cmd = format_quotes_string(cmd->cmd);
|
||||
i = 0;
|
||||
while (cmd->argv[i])
|
||||
argv = content;
|
||||
while (argv && *argv)
|
||||
{
|
||||
cmd->argv[i] = format_quotes_string(cmd->argv[i]);
|
||||
i++;
|
||||
*argv = format_quotes_string(*argv);
|
||||
argv++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int format_quotes(t_list *list_cmd)
|
||||
{
|
||||
if (!list_cmd)
|
||||
return (0);
|
||||
while (list_cmd)
|
||||
{
|
||||
if (format_quotes_cmd(list_cmd->content))
|
||||
return (-1);
|
||||
list_cmd = list_cmd->next;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/20 09:19:39 by mmoussou #+# #+# */
|
||||
/* Updated: 2024/08/04 16:57:45 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/08/06 15:18:02 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -71,20 +71,18 @@ static int get_input(char *delimiter, int fd)
|
||||
return (-(status == -1));
|
||||
}
|
||||
|
||||
void __forked(char *delimiter, int fd, t_cmd *cmd)
|
||||
void __forked(char *delimiter, int fd)
|
||||
{
|
||||
signal(SIGINT, &__heredoc_sig);
|
||||
free(cmd);
|
||||
get_input(delimiter, fd);
|
||||
ft_envclear(get_env(NULL), free);
|
||||
ft_lstclear_till_nxt(get_list2(NULL), &free_cmd);
|
||||
ft_lstclear(get_list(NULL), &free_token);
|
||||
free(*get_rl(NULL));
|
||||
ft_lstclear(get_list(NULL), free);
|
||||
ft_free("a", (char ***)get_void(NULL));
|
||||
rl_clear_history();
|
||||
close(fd);
|
||||
}
|
||||
|
||||
int __heredoc(char *delimiter, t_cmd *cmd)
|
||||
int __heredoc(char *delimiter)
|
||||
{
|
||||
int fork_pid;
|
||||
int fd;
|
||||
@ -105,7 +103,7 @@ int __heredoc(char *delimiter, t_cmd *cmd)
|
||||
}
|
||||
if (!fork_pid)
|
||||
{
|
||||
__forked(delimiter, fd, cmd);
|
||||
__forked(delimiter, fd);
|
||||
exit(0);
|
||||
}
|
||||
else
|
||||
@ -113,10 +111,10 @@ int __heredoc(char *delimiter, t_cmd *cmd)
|
||||
return (check_error(heredoc_ret, fd));
|
||||
}
|
||||
|
||||
int ft_heredoc(char *delimiter, t_cmd *cmd)
|
||||
int ft_heredoc(char *delimiter)
|
||||
{
|
||||
int fd;
|
||||
|
||||
fd = __heredoc(delimiter, cmd);
|
||||
fd = __heredoc(delimiter);
|
||||
return (fd);
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/10 01:14:10 by adjoly #+# #+# */
|
||||
/* Updated: 2024/08/01 17:58:58 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/08/06 15:18:19 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -66,10 +66,10 @@ void __heredoc_sig(int code)
|
||||
{
|
||||
(void)code;
|
||||
ft_envclear(get_env(NULL), free);
|
||||
ft_lstclear_till_nxt(get_list2(NULL), &free_cmd);
|
||||
ft_lstclear(get_list(NULL), &free_token);
|
||||
ft_lstclear(get_list(NULL), free);
|
||||
ft_free("a", (char ***)get_void(NULL));
|
||||
close(get_fd_heredoc(-1));
|
||||
close(get_fd_heredoc(-1));
|
||||
rl_clear_history();
|
||||
free(*get_rl(NULL));
|
||||
exit(130);
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/16 14:38:11 by adjoly #+# #+# */
|
||||
/* Updated: 2024/07/29 19:16:15 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/08/05 16:22:38 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
42
src/main.c
42
src/main.c
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/04/24 11:18:04 by adjoly #+# #+# */
|
||||
/* Updated: 2024/08/04 12:43:53 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/08/06 17:08:09 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -23,44 +23,16 @@ int free_end(char *rl, t_env *env_l, int exit_code, bool print)
|
||||
return (get_exit_code(exit_code));
|
||||
}
|
||||
|
||||
void __exec(t_list *cmd_list, t_env *env_l)
|
||||
{
|
||||
if (!cmd_list)
|
||||
return ;
|
||||
if (format_quotes(cmd_list))
|
||||
{
|
||||
ft_lstclear(get_list(NULL), &free_cmd);
|
||||
return ;
|
||||
}
|
||||
get_list(&cmd_list);
|
||||
if (check_redir(cmd_list))
|
||||
{
|
||||
ft_lstclear(get_list(NULL), &free_cmd);
|
||||
return ;
|
||||
}
|
||||
exec_split_cmd(cmd_list, env_l);
|
||||
ft_lstclear(&cmd_list, &free_cmd);
|
||||
}
|
||||
|
||||
void __parse(char *rl, t_env *env_l)
|
||||
{
|
||||
t_list *cmd_list;
|
||||
t_list *piped;
|
||||
t_list *token;
|
||||
|
||||
rl = env_var_replace(rl, env_l);
|
||||
get_rl(&rl);
|
||||
piped = tokenizer(rl);
|
||||
get_list(&piped);
|
||||
if (check_argv(piped))
|
||||
{
|
||||
free(rl);
|
||||
ft_lstclear(&piped, &free_token);
|
||||
token = tokenizer(rl, env_l);
|
||||
if (!token)
|
||||
return ;
|
||||
}
|
||||
cmd_list = get_cmd_list(piped);
|
||||
free(rl);
|
||||
ft_lstclear(&piped, &free_token);
|
||||
__exec(cmd_list, env_l);
|
||||
exec_split_cmd(token, env_l);
|
||||
ft_lstclear(&token, free_cmd);
|
||||
return ;
|
||||
}
|
||||
|
||||
char *__rl(t_env *env_l)
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/03 16:34:19 by adjoly #+# #+# */
|
||||
/* Updated: 2024/07/18 17:15:01 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/08/05 16:34:01 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -14,27 +14,27 @@
|
||||
#include "parsing.h"
|
||||
#include "error_msg.h"
|
||||
|
||||
bool check_file_name(t_list *redir)
|
||||
{
|
||||
while (redir)
|
||||
{
|
||||
if (!((t_redirection *)redir->content)->file_name)
|
||||
return (true);
|
||||
redir = redir->next;
|
||||
}
|
||||
return (false);
|
||||
}
|
||||
//bool check_file_name(t_list *redir)
|
||||
//{
|
||||
// while (redir)
|
||||
// {
|
||||
// if (!((t_redirection *)redir->content)->file_name)
|
||||
// return (true);
|
||||
// redir = redir->next;
|
||||
// }
|
||||
// return (false);
|
||||
//}
|
||||
|
||||
bool check_argv(t_list *token)
|
||||
{
|
||||
t_list *tmp;
|
||||
|
||||
tmp = token;
|
||||
while (tmp)
|
||||
{
|
||||
if (check_file_name(((t_token *)tmp->content)->redirection))
|
||||
return (send_error_parsing(ERROR_NO_FILE));
|
||||
tmp = tmp->next;
|
||||
}
|
||||
return (false);
|
||||
}
|
||||
//bool check_argv(t_list *token)
|
||||
//{
|
||||
// t_list *tmp;
|
||||
//
|
||||
// tmp = token;
|
||||
// while (tmp)
|
||||
// {
|
||||
// if (check_file_name(((t_token *)tmp->content)->redirection))
|
||||
// return (send_error_parsing(ERROR_NO_FILE));
|
||||
// tmp = tmp->next;
|
||||
// }
|
||||
// return (false);
|
||||
//}
|
||||
|
@ -6,13 +6,12 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/21 11:59:34 by adjoly #+# #+# */
|
||||
/* Updated: 2024/07/18 13:01:49 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/08/05 16:28:43 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "parsing.h"
|
||||
#include "error_msg.h"
|
||||
#include "tokenizer.h"
|
||||
#include <stdio.h>
|
||||
|
||||
char *go_to_nxt_quote(char *rl, t_quote quote_type)
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/09 16:32:21 by adjoly #+# #+# */
|
||||
/* Updated: 2024/07/09 23:09:41 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/08/05 22:02:00 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -17,13 +17,13 @@
|
||||
bool check_redir(t_list *list)
|
||||
{
|
||||
t_list *tmp;
|
||||
t_cmd *cmd;
|
||||
t_redir *redir;
|
||||
|
||||
tmp = list;
|
||||
while (tmp)
|
||||
{
|
||||
cmd = (t_cmd *)tmp->content;
|
||||
if (cmd->infile == -1 || cmd->outfile == -1)
|
||||
redir = (t_redir *)tmp->content;
|
||||
if (redir->fd < 0)
|
||||
return (send_error_parsing(ERROR_NO_FILE));
|
||||
tmp = tmp->next;
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/28 17:40:13 by adjoly #+# #+# */
|
||||
/* Updated: 2024/06/30 13:54:53 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/08/06 16:51:48 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -40,7 +40,7 @@ bool check_if_file(char *readline)
|
||||
{
|
||||
while (*tmp && is_chevron(*tmp))
|
||||
tmp++;
|
||||
while (*tmp && !ft_isalnum(*tmp))
|
||||
while (*tmp && ft_isspace(*tmp))
|
||||
tmp++;
|
||||
if (!*tmp)
|
||||
return (true);
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/05 21:14:04 by adjoly #+# #+# */
|
||||
/* Updated: 2024/07/29 12:06:11 by mmoussou ### ########.fr */
|
||||
/* Updated: 2024/08/06 17:18:02 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -78,6 +78,10 @@ size_t __add_dollar(t_env *env, char **rl_dollared, char *tmp)
|
||||
free(exit_code);
|
||||
return (2);
|
||||
}
|
||||
else if ((*tmp) == '$')
|
||||
return (2);
|
||||
else if (!(*tmp) || ft_isspace(*tmp))
|
||||
return (1);
|
||||
dollar_size = strlen_till_notalnum(tmp);
|
||||
__cpy_dollar(tmp, dollar_size, env, rl_dollared);
|
||||
return (dollar_size + 1);
|
||||
@ -102,6 +106,5 @@ char *env_var_replace(char *readline, t_env *env)
|
||||
tmp++;
|
||||
}
|
||||
}
|
||||
free(readline);
|
||||
return (rl_dollared);
|
||||
}
|
||||
|
@ -1,56 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_cmd_list.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/31 12:47:13 by adjoly #+# #+# */
|
||||
/* Updated: 2024/08/01 19:07:28 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
#include "parsing.h"
|
||||
#include "minishell.h"
|
||||
|
||||
t_list *make_lst(t_list *tmp)
|
||||
{
|
||||
t_list *cmd_list;
|
||||
|
||||
cmd_list = malloc(sizeof(t_list));
|
||||
get_list2(&cmd_list);
|
||||
cmd_list->next = NULL;
|
||||
cmd_list->content = get_redir_fd(tmp->content, \
|
||||
((t_token *)tmp->content)->redirection);
|
||||
return (cmd_list);
|
||||
}
|
||||
|
||||
t_list *get_cmd_list(t_list *list)
|
||||
{
|
||||
t_list *tmp;
|
||||
t_list *cmd_list;
|
||||
|
||||
tmp = list;
|
||||
cmd_list = make_lst(tmp);
|
||||
if (!cmd_list->content)
|
||||
{
|
||||
free(cmd_list);
|
||||
return (NULL);
|
||||
}
|
||||
tmp = tmp->next;
|
||||
while (tmp)
|
||||
{
|
||||
ft_lstadd_back(&cmd_list, ft_lstnew(get_redir_fd(tmp->content, \
|
||||
((t_token *)tmp->content)->redirection)));
|
||||
if (!ft_lstlast(cmd_list)->content)
|
||||
{
|
||||
ft_lstclear(&cmd_list, &free_cmd);
|
||||
if (cmd_list)
|
||||
free(cmd_list);
|
||||
return (NULL);
|
||||
}
|
||||
tmp = tmp->next;
|
||||
}
|
||||
return (cmd_list);
|
||||
}
|
@ -1,68 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_redir_fd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/30 10:48:41 by adjoly #+# #+# */
|
||||
/* Updated: 2024/08/03 18:43:13 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "parsing.h"
|
||||
#include "tokenizer.h"
|
||||
#include "execution.h"
|
||||
#include <fcntl.h>
|
||||
#include "minishell.h"
|
||||
#include <stdio.h>
|
||||
#include "libft.h"
|
||||
|
||||
void parsing_msg(t_cmd *cmd)
|
||||
{
|
||||
if (cmd->infile == -1 || cmd->outfile == -1)
|
||||
send_error_parsing(ERROR_NO_FILE);
|
||||
}
|
||||
|
||||
t_cmd *init_cmd(void)
|
||||
{
|
||||
t_cmd *cmd;
|
||||
|
||||
cmd = ft_calloc(sizeof(t_cmd), 1);
|
||||
if (!cmd)
|
||||
return (NULL);
|
||||
cmd->infile = -2;
|
||||
cmd->outfile = -2;
|
||||
cmd->argv = NULL;
|
||||
cmd->cmd = NULL;
|
||||
return (cmd);
|
||||
}
|
||||
|
||||
t_cmd *get_redir_fd(void *content, t_list *tmp)
|
||||
{
|
||||
t_redir_sign sign[2];
|
||||
t_cmd *cmd;
|
||||
|
||||
sign[0] = INFILE;
|
||||
sign[1] = OUTFILE;
|
||||
cmd = init_cmd();
|
||||
if (!cmd)
|
||||
return (NULL);
|
||||
while (tmp)
|
||||
{
|
||||
open_redir((t_redirection *)tmp->content, cmd, sign);
|
||||
if (cmd->infile <= -1 || cmd->outfile == -1)
|
||||
{
|
||||
parsing_msg(cmd);
|
||||
free(cmd);
|
||||
return (NULL);
|
||||
}
|
||||
tmp = tmp->next;
|
||||
}
|
||||
if (sign[0] == INFILE || cmd->outfile == -1)
|
||||
cmd->outfile = STDOUT_FILENO;
|
||||
if (sign[1] == OUTFILE || cmd->infile == -1)
|
||||
cmd->infile = STDIN_FILENO;
|
||||
cmd = split_cmd(((t_token *)content)->argv, cmd);
|
||||
return (cmd);
|
||||
}
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/20 20:06:13 by adjoly #+# #+# */
|
||||
/* Updated: 2024/07/17 16:20:46 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/08/05 16:28:20 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -1,48 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* open_redir.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/30 15:56:53 by adjoly #+# #+# */
|
||||
/* Updated: 2024/08/03 18:44:03 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "tokenizer.h"
|
||||
#include <fcntl.h>
|
||||
#include "parsing.h"
|
||||
#include "execution.h"
|
||||
#include <stdio.h>
|
||||
|
||||
t_redir_sign __close_unused_fd(t_cmd *cmd, t_redir_sign sign)
|
||||
{
|
||||
if (cmd->outfile > 1 && sign >= 2)
|
||||
close(cmd->outfile);
|
||||
else if (cmd->infile > 1 && sign < 2)
|
||||
close(cmd->infile);
|
||||
return (sign);
|
||||
}
|
||||
|
||||
void open_redir(t_redirection *redir, t_cmd *cmd, t_redir_sign sign[2])
|
||||
{
|
||||
if (redir->sign == OUTFILE || redir->sign == OUT_APPEND)
|
||||
sign[0] = __close_unused_fd(cmd, redir->sign);
|
||||
else if (redir->sign == INFILE || redir->sign == HEREDOC)
|
||||
sign[1] = __close_unused_fd(cmd, redir->sign);
|
||||
if (redir->sign == HEREDOC)
|
||||
{
|
||||
cmd->infile = ft_heredoc(redir->file_name, cmd);
|
||||
if (cmd->infile == -2)
|
||||
return ;
|
||||
}
|
||||
else if (redir->sign == INFILE)
|
||||
cmd->infile = open(redir->file_name, O_RDONLY);
|
||||
else if (redir->sign == OUTFILE)
|
||||
cmd->outfile = open(redir->file_name, \
|
||||
O_CREAT | O_TRUNC | O_WRONLY, 0644);
|
||||
else if (redir->sign == OUT_APPEND)
|
||||
cmd->outfile = open(redir->file_name, \
|
||||
O_CREAT | O_APPEND | O_WRONLY, 0644);
|
||||
}
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/05 13:48:57 by adjoly #+# #+# */
|
||||
/* Updated: 2024/07/30 19:10:17 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/08/06 17:23:12 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -14,14 +14,26 @@
|
||||
#include "parsing.h"
|
||||
#include <stdio.h>
|
||||
|
||||
int ft_ischevron(int c)
|
||||
{
|
||||
return (c == '<' || c == '>');
|
||||
}
|
||||
|
||||
size_t __get_len_arg(char *s)
|
||||
{
|
||||
char *tmp;
|
||||
|
||||
tmp = s;
|
||||
while (*tmp)
|
||||
if (ft_ischevron(*tmp))
|
||||
{
|
||||
if (ft_isspace(*tmp) && is_inquote(s, tmp - s) == FALSE)
|
||||
while (*tmp && ft_ischevron(*tmp))
|
||||
tmp++;
|
||||
return (tmp - s);
|
||||
}
|
||||
while (tmp && *tmp)
|
||||
{
|
||||
if ((ft_isspace(*tmp) || ft_ischevron(*tmp)) \
|
||||
&& is_inquote(s, tmp - s) == FALSE)
|
||||
break ;
|
||||
tmp++;
|
||||
}
|
||||
@ -64,8 +76,13 @@ char **split_argv(char *readline)
|
||||
char *tmp;
|
||||
|
||||
tmp = readline;
|
||||
if (!readline || !*readline)
|
||||
if (!readline)
|
||||
return (NULL);
|
||||
if (!*readline)
|
||||
{
|
||||
free(readline);
|
||||
return (NULL);
|
||||
}
|
||||
argv = ft_calloc(__count_args(readline) + 1, sizeof(char *));
|
||||
tmp_av = argv;
|
||||
while (*tmp)
|
||||
@ -80,5 +97,6 @@ char **split_argv(char *readline)
|
||||
}
|
||||
}
|
||||
*tmp_av = NULL;
|
||||
free(readline);
|
||||
return (argv);
|
||||
}
|
||||
|
@ -1,34 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* split_cmd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/04 15:00:32 by adjoly #+# #+# */
|
||||
/* Updated: 2024/08/01 17:39:20 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "parsing.h"
|
||||
#include "libft.h"
|
||||
|
||||
t_cmd *split_cmd(char *cmd_av, t_cmd *cmd)
|
||||
{
|
||||
char **split;
|
||||
|
||||
cmd->argv = NULL;
|
||||
cmd->cmd = NULL;
|
||||
split = split_argv(cmd_av);
|
||||
if (!split)
|
||||
{
|
||||
cmd->argv = NULL;
|
||||
cmd->cmd = NULL;
|
||||
return (cmd);
|
||||
}
|
||||
cmd->argv = split;
|
||||
cmd->cmd = ft_strdup(*split);
|
||||
if (!cmd->cmd)
|
||||
return (cmd);
|
||||
return (cmd);
|
||||
}
|
57
src/parsing/tokenizer/__get_argv.c
Normal file
57
src/parsing/tokenizer/__get_argv.c
Normal file
@ -0,0 +1,57 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* __get_argv.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/08/06 14:30:31 by adjoly #+# #+# */
|
||||
/* Updated: 2024/08/06 15:24:51 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "parsing.h"
|
||||
|
||||
size_t __get_size_argv(char **content)
|
||||
{
|
||||
size_t size;
|
||||
|
||||
size = 0;
|
||||
while (content && *content)
|
||||
{
|
||||
if (__to_redir_sign(*content) != ERROR)
|
||||
content++;
|
||||
else
|
||||
size++;
|
||||
content++;
|
||||
}
|
||||
return (size);
|
||||
}
|
||||
|
||||
char **__get_argv(char **content)
|
||||
{
|
||||
char **argv;
|
||||
char **start;
|
||||
|
||||
if (!content || !*content)
|
||||
return (NULL);
|
||||
argv = ft_calloc(sizeof(char *), __get_size_argv(content) + 1);
|
||||
start = argv;
|
||||
while (content && *content)
|
||||
{
|
||||
if (__to_redir_sign(*content) != ERROR)
|
||||
{
|
||||
content += 2;
|
||||
free(*(content - 1));
|
||||
free(*(content - 2));
|
||||
}
|
||||
else
|
||||
{
|
||||
*argv = *content;
|
||||
argv++;
|
||||
content++;
|
||||
}
|
||||
}
|
||||
argv = NULL;
|
||||
return (start);
|
||||
}
|
@ -1,26 +1,41 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* __to_redir_sign.c :+: :+: :+: */
|
||||
/* __get_fd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/25 15:06:40 by adjoly #+# #+# */
|
||||
/* Updated: 2024/06/30 17:27:57 by adjoly ### ########.fr */
|
||||
/* Created: 2024/08/05 23:34:03 by adjoly #+# #+# */
|
||||
/* Updated: 2024/08/06 14:15:02 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "tokenizer.h"
|
||||
#include "parsing.h"
|
||||
#include <stdio.h>
|
||||
|
||||
t_redir_sign __to_redir_sign(char *redir_sign)
|
||||
void __get_fd(t_list *list, t_cmd *cmd)
|
||||
{
|
||||
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_redir *redir;
|
||||
|
||||
cmd->infile = STDIN_FILENO;
|
||||
cmd->outfile = STDOUT_FILENO;
|
||||
if (!list)
|
||||
return ;
|
||||
while (list)
|
||||
{
|
||||
redir = list->content;
|
||||
if (redir->sign < 2)
|
||||
{
|
||||
if (cmd->infile > 1)
|
||||
close (cmd->infile);
|
||||
cmd->infile = redir->fd;
|
||||
}
|
||||
else if (redir->sign >= 2)
|
||||
{
|
||||
if (cmd->outfile > 1)
|
||||
close (cmd->outfile);
|
||||
cmd->outfile = redir->fd;
|
||||
}
|
||||
list = list->next;
|
||||
}
|
||||
}
|
83
src/parsing/tokenizer/__open_redir.c
Normal file
83
src/parsing/tokenizer/__open_redir.c
Normal file
@ -0,0 +1,83 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* __open_redir.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/08/05 23:31:09 by adjoly #+# #+# */
|
||||
/* Updated: 2024/08/06 15:29:13 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "parsing.h"
|
||||
#include "execution.h"
|
||||
#include <fcntl.h>
|
||||
|
||||
t_redir_sign __to_redir_sign(char *redir_sign)
|
||||
{
|
||||
if (!redir_sign || !*redir_sign)
|
||||
return (ERROR);
|
||||
else if (!(*redir_sign == '<' || *redir_sign == '>'))
|
||||
return (ERROR);
|
||||
else if (*redir_sign == '<' && !*(redir_sign + 1))
|
||||
return (INFILE);
|
||||
else if (*redir_sign == '<' && *(redir_sign + 1) == '<' \
|
||||
&& !*(redir_sign + 2))
|
||||
return (HEREDOC);
|
||||
else if (*redir_sign == '>' && !*(redir_sign + 1))
|
||||
return (OUTFILE);
|
||||
else if (*redir_sign == '>' && *(redir_sign + 1) == '>' \
|
||||
&& !*(redir_sign + 2))
|
||||
return (OUT_APPEND);
|
||||
return (ERROR);
|
||||
}
|
||||
|
||||
t_redir *__open_heredoc(char *filename)
|
||||
{
|
||||
int fd;
|
||||
t_redir *redir;
|
||||
|
||||
if (!filename && !*filename)
|
||||
return (NULL);
|
||||
fd = ft_heredoc(filename);
|
||||
redir = ft_calloc(sizeof(t_redir), 1);
|
||||
if (!redir)
|
||||
{
|
||||
if (fd != -1)
|
||||
close(fd);
|
||||
return (NULL);
|
||||
}
|
||||
redir->fd = fd;
|
||||
if (fd == -2)
|
||||
redir->sign = ERROR;
|
||||
else
|
||||
redir->sign = HEREDOC;
|
||||
return (redir);
|
||||
}
|
||||
|
||||
t_redir *__to_redir(char *filename, t_redir_sign sign)
|
||||
{
|
||||
t_redir *redir;
|
||||
int fd;
|
||||
|
||||
if (!filename && !*filename)
|
||||
return (NULL);
|
||||
fd = -1;
|
||||
if (sign == INFILE)
|
||||
fd = open(filename, O_RDONLY);
|
||||
else if (sign == OUTFILE)
|
||||
fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0644);
|
||||
else if (sign == OUT_APPEND)
|
||||
fd = open(filename, O_CREAT | O_APPEND | O_WRONLY, 0644);
|
||||
redir = ft_calloc(sizeof(t_redir), 1);
|
||||
if (!redir)
|
||||
{
|
||||
if (fd != 1)
|
||||
close(fd);
|
||||
return (NULL);
|
||||
}
|
||||
redir->fd = fd;
|
||||
redir->sign = sign;
|
||||
return (redir);
|
||||
}
|
88
src/parsing/tokenizer/__to_cmd.c
Normal file
88
src/parsing/tokenizer/__to_cmd.c
Normal file
@ -0,0 +1,88 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* __to_cmd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/08/05 14:19:35 by adjoly #+# #+# */
|
||||
/* Updated: 2024/08/06 17:25:14 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
void printcmd(t_cmd *cmd){
|
||||
printf("in = %d\n", cmd->infile);
|
||||
printf("out = %d\n", cmd->outfile);
|
||||
printf("cmd = %s\n", cmd->cmd);
|
||||
char **tmp = cmd->argv;
|
||||
while (tmp && *tmp)
|
||||
{
|
||||
printf("argv = %s\n", *tmp);
|
||||
tmp++;
|
||||
}
|
||||
}
|
||||
|
||||
t_list *__get_redir(char **content)
|
||||
{
|
||||
t_list *list;
|
||||
t_redir_sign sign;
|
||||
t_list *tmp;
|
||||
|
||||
list = NULL;
|
||||
while (content && *content)
|
||||
{
|
||||
sign = __to_redir_sign(*content);
|
||||
if (sign == HEREDOC)
|
||||
{
|
||||
tmp = ft_lstnew(__open_heredoc(*(content + 1)));
|
||||
ft_lstadd_back(&list, tmp);
|
||||
if (((t_redir *)(tmp->content))->sign == ERROR)
|
||||
{
|
||||
ft_lstclear(&list, free_redir);
|
||||
return (NULL);
|
||||
}
|
||||
}
|
||||
else if (sign != ERROR)
|
||||
ft_lstadd_back(&list, ft_lstnew(__to_redir(*(content + 1), sign)));
|
||||
content++;
|
||||
}
|
||||
return (list);
|
||||
}
|
||||
|
||||
void *clear_all_cmd(char **content, t_list *redir)
|
||||
{
|
||||
ft_lstclear(&redir, free_redir);
|
||||
ft_free("a", &content);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
t_cmd *__to_cmd(char **content)
|
||||
{
|
||||
t_list *redir;
|
||||
t_cmd *cmd;
|
||||
|
||||
if (!content)
|
||||
return (NULL);
|
||||
if (!*content)
|
||||
{
|
||||
free(content);
|
||||
return (NULL);
|
||||
}
|
||||
format_quotes(content);
|
||||
get_void(&content);
|
||||
redir = __get_redir(content);
|
||||
if (check_redir(redir))
|
||||
return (clear_all_cmd(content, redir));
|
||||
cmd = ft_calloc(sizeof(t_cmd), 1);
|
||||
if (!cmd)
|
||||
return (clear_all_cmd(content, redir));
|
||||
__get_fd(redir, cmd);
|
||||
cmd->argv = __get_argv(content);
|
||||
free(content);
|
||||
cmd->cmd = ft_strdup(cmd->argv[0]);
|
||||
ft_lstclear(&redir, free);
|
||||
printcmd(cmd);
|
||||
return (cmd);
|
||||
}
|
@ -1,50 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* __to_redir.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/25 15:06:15 by adjoly #+# #+# */
|
||||
/* Updated: 2024/08/04 12:53:17 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "tokenizer.h"
|
||||
#include <stdio.h>
|
||||
#include "parsing.h"
|
||||
|
||||
size_t __cpy_arg(char *dst, char *src);
|
||||
size_t __get_len_arg(char *s);
|
||||
char *format_quotes_string(char *cmd);
|
||||
|
||||
char *__redir_filename_cpy(char *redir_s)
|
||||
{
|
||||
char *tmp;
|
||||
|
||||
tmp = ft_calloc(__get_len_arg(redir_s) + 1, sizeof(char));
|
||||
if (!tmp)
|
||||
return (NULL);
|
||||
__cpy_arg(tmp, redir_s);
|
||||
tmp = format_quotes_string(tmp);
|
||||
return (tmp);
|
||||
}
|
||||
|
||||
t_redirection *__to_redir(char *redir_s)
|
||||
{
|
||||
t_redirection *redir;
|
||||
|
||||
redir = ft_calloc(sizeof(t_redirection), 1);
|
||||
if (!redir)
|
||||
return (NULL);
|
||||
redir->sign = __to_redir_sign(redir_s);
|
||||
redir->file_name = NULL;
|
||||
if (redir->sign == OUT_APPEND || redir->sign == HEREDOC)
|
||||
redir_s += 2;
|
||||
else
|
||||
redir_s++;
|
||||
while (*redir_s && ft_isspace(*redir_s))
|
||||
redir_s++;
|
||||
redir->file_name = __redir_filename_cpy(redir_s);
|
||||
return (redir);
|
||||
}
|
@ -1,81 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* __to_token.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/20 21:05:04 by adjoly #+# #+# */
|
||||
/* Updated: 2024/08/04 12:52:25 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "tokenizer.h"
|
||||
#include "libft.h"
|
||||
#include "parsing.h"
|
||||
#include <stdio.h>
|
||||
|
||||
size_t __get_len_arg(char *s);
|
||||
|
||||
bool check_argvcontent(char *argv)
|
||||
{
|
||||
if (!argv)
|
||||
return (false);
|
||||
while (*argv && ft_isspace(*argv))
|
||||
argv++;
|
||||
if (!*argv)
|
||||
return (true);
|
||||
return (false);
|
||||
}
|
||||
|
||||
size_t __get_sizeof_redir(char *redir_s, t_redirection *redir)
|
||||
{
|
||||
size_t i;
|
||||
char *tmp;
|
||||
|
||||
if (!redir_s || !redir)
|
||||
return (0);
|
||||
else if (redir->sign == OUT_APPEND || redir->sign == HEREDOC)
|
||||
i = 1;
|
||||
else
|
||||
i = 0;
|
||||
if (!redir->file_name)
|
||||
return (i);
|
||||
tmp = redir_s + i;
|
||||
while (*++tmp && ft_isspace(*tmp))
|
||||
i++;
|
||||
i += __get_len_arg(tmp);
|
||||
return (i);
|
||||
}
|
||||
|
||||
t_token *__to_token(char *cmd)
|
||||
{
|
||||
t_token *token;
|
||||
t_redirection *tmp_redir;
|
||||
char *tmp;
|
||||
|
||||
token = ft_calloc(sizeof(t_token), 1);
|
||||
token->argv = NULL;
|
||||
tmp = cmd;
|
||||
while (*tmp)
|
||||
{
|
||||
if ((*tmp == '<' || *tmp == '>') && is_inquote(cmd, tmp - cmd) == FALSE)
|
||||
{
|
||||
tmp_redir = __to_redir(tmp);
|
||||
ft_lstadd_back(&(token->redirection), ft_lstnew((void *)tmp_redir));
|
||||
tmp += __get_sizeof_redir(tmp, tmp_redir);
|
||||
}
|
||||
else
|
||||
{
|
||||
token->argv = ft_strjoin_free(token->argv, ft_substr(tmp, 0, 1));
|
||||
if (check_argvcontent(token->argv))
|
||||
{
|
||||
free(token->argv);
|
||||
token->argv = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
tmp++;
|
||||
}
|
||||
return (token);
|
||||
}
|
@ -6,28 +6,42 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/18 20:13:50 by adjoly #+# #+# */
|
||||
/* Updated: 2024/08/04 16:16:59 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/08/06 17:05:44 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "tokenizer.h"
|
||||
#include "parsing.h"
|
||||
#include "minishell.h"
|
||||
#include <stdio.h>
|
||||
|
||||
t_list *tokenizer(char *readline)
|
||||
void *clear_all(t_list *cmd, t_list *piped)
|
||||
{
|
||||
t_list *token;
|
||||
ft_lstclear(&cmd, free);
|
||||
ft_lstclear(&piped, free);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
t_list *tokenizer(char *readline, t_env *env)
|
||||
{
|
||||
t_list *cmd;
|
||||
t_list *piped;
|
||||
t_list *tmp;
|
||||
|
||||
(void)(env);
|
||||
piped = __split_pipe(readline);
|
||||
token = NULL;
|
||||
free(readline);
|
||||
cmd = NULL;
|
||||
tmp = piped;
|
||||
get_list(&piped);
|
||||
while (tmp)
|
||||
{
|
||||
ft_lstadd_back(&token, ft_lstnew((void *)__to_token(tmp->content)));
|
||||
ft_lstadd_back(&cmd,
|
||||
ft_lstnew(__to_cmd(split_argv(env_var_replace(tmp->content,
|
||||
env)))));
|
||||
if (!ft_lstlast(cmd)->content)
|
||||
return (clear_all(cmd, piped));
|
||||
tmp = tmp->next;
|
||||
}
|
||||
ft_lstclear(&piped, free);
|
||||
return (token);
|
||||
return (cmd);
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/04 20:10:35 by adjoly #+# #+# */
|
||||
/* Updated: 2024/08/04 16:58:07 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/08/05 23:27:57 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -15,26 +15,14 @@
|
||||
|
||||
void free_redir(void *redir_v)
|
||||
{
|
||||
t_redirection *redir;
|
||||
t_redir *redir;
|
||||
|
||||
redir = redir_v;
|
||||
free(redir->file_name);
|
||||
if (redir->fd > 1)
|
||||
close (redir->fd);
|
||||
free(redir);
|
||||
}
|
||||
|
||||
void free_token(void *token_v)
|
||||
{
|
||||
t_token *token;
|
||||
|
||||
token = token_v;
|
||||
if (token->argv)
|
||||
free(token->argv);
|
||||
if (token->redirection)
|
||||
ft_lstclear(&(token->redirection), free_redir);
|
||||
if (token)
|
||||
free(token);
|
||||
}
|
||||
|
||||
void free_cmd(void *content)
|
||||
{
|
||||
t_cmd *cmd;
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/09 17:24:15 by adjoly #+# #+# */
|
||||
/* Updated: 2024/07/18 14:15:18 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/08/05 23:11:27 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -31,12 +31,12 @@ t_list **get_list(t_list **list)
|
||||
return (ret);
|
||||
}
|
||||
|
||||
t_list **get_list2(t_list **list)
|
||||
void *get_void(void *in)
|
||||
{
|
||||
static t_list **ret;
|
||||
static void *ret;
|
||||
|
||||
if (list)
|
||||
ret = list;
|
||||
if (in)
|
||||
ret = in;
|
||||
return (ret);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user