Compare commits

..

No commits in common. "96da8e54c3a49e717bc1188f75e0fa0e986b7fe6" and "f20038e37bba79ed7f5c82a72ea8218e35893226" have entirely different histories.

15 changed files with 51 additions and 170 deletions

Binary file not shown.

View File

@ -1,5 +1,5 @@
UTILS_SRC = utils/ft_is_in_quote.c utils/ft_strncpy.c utils/ft_strreplace.c utils/ft_strnchr.c utils/ft_split_quoted.c utils/ft_strshift.c utils/ft_quote_remover.c utils/ft_str_is_empty.c
SRCS = ${UTILS_SRC} main.c file.c infile.c outfile.c heredoc.c syntatics.c cmd.c cmds.c env.c execution.c spacer.c env_fill.c
UTILS_SRC = utils/ft_is_in_quote.c utils/ft_strncpy.c utils/ft_strreplace.c utils/ft_strnchr.c utils/ft_split_quoted.c utils/ft_strshift.c utils/ft_quote_remover.c
SRCS = ${UTILS_SRC} main.c file.c infile.c outfile.c heredoc.c syntatics.c cmd.c cmds.c env.c execution.c spacer.c
OBJS = ${SRCS:.c=.o}

16
cmd.c
View File

@ -6,11 +6,10 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/15 14:18:21 by cchauvet #+# #+# */
/* Updated: 2023/02/16 18:25:14 by cchauvet ### ########.fr */
/* Updated: 2023/02/15 14:18:38 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftx/libftx.h"
#include "minishell.h"
void ft_cmddel(void *ptr)
@ -25,10 +24,9 @@ void ft_cmddel(void *ptr)
free(content);
}
int ft_cmd_filler(t_list *element, char **args, t_list **env)
int ft_cmd_filler(t_list *element, char **args)
{
t_cmd *content;
char *temp;
size_t i;
if (args == NULL)
@ -37,15 +35,7 @@ int ft_cmd_filler(t_list *element, char **args, t_list **env)
i = 0;
while (args[i] != NULL)
{
temp = ft_env_filler(env, args[i]);
if (temp == NULL)
{
ft_eprintf("minishell: malloc failed\n");
return (1);
}
free(args[i]);
args[i] = temp;
ft_quote_remover(temp);
ft_quote_remover(args[i]);
i++;
}
content->args = args;

15
cmds.c
View File

@ -6,14 +6,13 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/15 14:17:26 by cchauvet #+# #+# */
/* Updated: 2023/02/16 18:20:10 by cchauvet ### ########.fr */
/* Updated: 2023/02/16 14:42:08 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftx/libftx.h"
#include "minishell.h"
static int ft_cmds_init(t_list **cmds, size_t len)
int ft_cmds_init(t_list **cmds, size_t len)
{
t_cmd *content;
t_list *current;
@ -49,7 +48,7 @@ static int ft_cmds_init(t_list **cmds, size_t len)
return (0);
}
static int ft_cmds_prep(t_list **cmds, const char *line, int infile, int outfile)
int ft_cmds_prep(t_list **cmds, const char *line, int infile, int outfile)
{
size_t len;
t_cmd *cmd;
@ -73,7 +72,7 @@ static int ft_cmds_prep(t_list **cmds, const char *line, int infile, int outfile
return (0);
}
static int ft_cmds_fill(t_list **cmds, t_list **env, const char *line)
int ft_cmds_fill(t_list **cmds, const char *line)
{
char **tab;
char **args;
@ -88,7 +87,7 @@ static int ft_cmds_fill(t_list **cmds, t_list **env, const char *line)
while (tab[i] != NULL)
{
args = ft_split_quoted(tab[i], ' ');
if (ft_cmd_filler(current, args, env) == 1)
if (ft_cmd_filler(current, args) == 1)
{
ft_lstclear(cmds, ft_cmddel);
ft_freer_tab_ultimate(2, args, tab);
@ -101,7 +100,7 @@ static int ft_cmds_fill(t_list **cmds, t_list **env, const char *line)
return (0);
}
t_list **ft_parse_cmds(char *line, t_list **env)
t_list **ft_parse_cmds(char *line)
{
int infile;
int outfile;
@ -114,7 +113,7 @@ t_list **ft_parse_cmds(char *line, t_list **env)
return (NULL);
if (ft_cmds_prep(cmds, line, infile, outfile) == 1)
return (NULL);
if (ft_cmds_fill(cmds, env, line) == 1)
if (ft_cmds_fill(cmds, line) == 1)
return (NULL);
return (cmds);
}

3
env.c
View File

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/02 14:39:56 by erey-bet #+# #+# */
/* Updated: 2023/02/16 16:24:21 by cchauvet ### ########.fr */
/* Updated: 2023/02/16 15:18:48 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -113,7 +113,6 @@ void env_del(void *ptr)
content = ptr;
free(content->key);
free(content->value);
free(content);
}
char *get_key(char *str)

View File

@ -1,76 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* env_fill.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/16 16:29:08 by cchauvet #+# #+# */
/* Updated: 2023/02/16 17:49:03 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftx/libftx.h"
#include "minishell.h"
#include "utils/utils.h"
static char *ft_get_value(t_list **env, const char *str, size_t start,
size_t stop)
{
char *key;
char *value;
key = ft_strndup(str + start, stop);
if (key == NULL)
{
ft_eprintf("minishell: malloc failed\n");
return (NULL);
}
value = get_value_by_key(key, env);
if (value == NULL)
value = "";
free(key);
return (value);
}
char *ft_env_filler(t_list **env, const char *str)
{
size_t i;
size_t y;
char *out;
char *temp;
char *value;
out = ft_strdup(str);
if (out == NULL)
{
ft_eprintf("minishell: malloc failed\n");
return (NULL);
}
i = 0;
while (out[i] != '\0')
{
while (ft_is_in_quote(out, i) == 1)
i++;
while (out[i] == '$')
{
y = i + 1;
while (out[y] != '\0' && out[y] != '$' && out[y] != ' ')
y++;
value = ft_get_value(env, out, i + 1, y - i - 1);
if (value == NULL)
return (NULL);
temp = ft_strreplace(out, value, i, y);
i = i + ft_strlen(value) - 1;
free(out);
if (temp == NULL)
{
ft_eprintf("minishell: malloc failed\n");
return (NULL);
}
out = temp;
}
i++;
}
return (out);
}

View File

@ -3,7 +3,21 @@
#include "utils/utils.h"
#include <unistd.h>
static char *ft_get_executable_path(char *executable_name, t_list **env)
static char *ft_get_variable(char **env, char *variable)
{
size_t i;
i = 0;
while (env[i] != NULL)
{
if (ft_strncmp(variable, env[i], ft_strlen(variable)) == 0)
return (ft_strchr(env[i], '=') + 1);
i++;
}
return (NULL);
}
static char *ft_get_executable_path(char *executable_name, char **env)
{
char *path;
char *temp;
@ -19,15 +33,10 @@ static char *ft_get_executable_path(char *executable_name, t_list **env)
ft_eprintf("minishell: malloc failed\n");
return (NULL);
}
if (access(path, X_OK) == 0)
{
ft_eprintf("minishell: %s: permission denied\n", path);
return (NULL);
}
}
else
{
tab = ft_split(get_value_by_key("PATH", env), ':');
tab = ft_split(ft_get_variable(env, "PATH"), ':');
if (tab == NULL)
return (NULL);
i = 0;
@ -57,11 +66,10 @@ static char *ft_get_executable_path(char *executable_name, t_list **env)
return (path);
}
static int ft_excutor(t_cmd *cmd, t_list **env)
static int ft_excutor(t_cmd *cmd, char **env)
{
int pid;
int return_value;
char **tab;
if (cmd->fd_in == -1 || cmd->fd_out == -1)
return (1);
@ -70,21 +78,16 @@ static int ft_excutor(t_cmd *cmd, t_list **env)
return (1);
if (pid == 0)
{
//TODO DO THIS
/* tab = t_env2tab(env); */
/* if (tab == NULL) */
/* return (1); */
tab = NULL;
dup2(cmd->fd_out, 1);
dup2(cmd->fd_in, 0);
execve(cmd->executable, cmd->args, tab);
execve(cmd->executable, cmd->args, env);
}
else
waitpid(pid, &return_value, 0);
return (return_value);
}
int ft_cmds_executor(t_list **cmds, t_list **env)
int ft_cmds_executor(t_list **cmds, char **env)
{
t_cmd *content;
t_list *current;

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/15 17:52:10 by cchauvet #+# #+# */
/* Updated: 2023/02/16 17:52:29 by cchauvet ### ########.fr */
/* Updated: 2023/02/15 21:22:24 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -55,9 +55,6 @@ static int ft_get_infile(const char *line)
i = 0;
while (tab[i + 1] != NULL)
{
if (tab[i][0] == '<')
if (fd != 0)
close(fd);
if (ft_strcmp("<", tab[i]) == 0)
fd = ft_file_is_readable(ft_quote_remover(tab[i + 1]));
else if (ft_strcmp("<<", tab[i]) == 0)

6
main.c
View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/16 15:16:14 by cchauvet #+# #+# */
/* Updated: 2023/02/16 18:19:49 by cchauvet ### ########.fr */
/* Updated: 2023/02/16 15:16:45 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -31,10 +31,10 @@ int main(int ac, char **av, char **env)
data.env = init_env(env);
if (data.env == NULL)
return (1);
cmds = ft_parse_cmds(line, data.env);
cmds = ft_parse_cmds(line);
if (cmds == NULL)
return (1);
if (ft_cmds_executor(cmds, data.env) == 1)
if (ft_cmds_executor(cmds, env) == 1)
{
ft_lstclear(data.env, env_del);
ft_lstclear(cmds, ft_cmddel);

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/14 13:45:30 by cchauvet #+# #+# */
/* Updated: 2023/02/16 18:19:34 by cchauvet ### ########.fr */
/* Updated: 2023/02/16 15:17:07 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -23,8 +23,9 @@
# include <readline/history.h>
t_list **init_env(char **env);
int set_value_by_key(char *key, char *value, t_list **env);
char *get_value_by_key(char *key, t_list **head);
int set_value_key(t_list **env, char *key, char *value);
char *get_value_index(int index, t_list **head);
char *get_value_key(char *key, t_list **head);
int ft_syntatic_verif(const char *str);
int ft_file_is_readable(const char *path);
int ft_file_is_writable(const char *path);
@ -34,14 +35,13 @@ int ft_infile(char *line);
int ft_outfile(char *line);
int ft_heredoc(char *stop);
size_t ft_seglen_quoted(const char *str, char c);
int ft_cmds_executor(t_list **cmds, t_list **env);
int ft_cmds_executor(t_list **cmds, char **env);
char **ft_split_quoted(const char *s, char c);
void ft_cmddel(void *content);
void env_del(void *content);
t_list **ft_parse_cmds(char *line, t_list **env);
int ft_cmd_filler(t_list *current, char **args, t_list **env);
t_list **ft_parse_cmds(char *line);
int ft_cmd_filler(t_list *current, char **args);
char *ft_normalizer(char *str);
char *ft_env_filler(t_list **env, const char *str);
typedef struct s_cmd
{

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/15 18:01:07 by cchauvet #+# #+# */
/* Updated: 2023/02/16 17:53:28 by cchauvet ### ########.fr */
/* Updated: 2023/02/15 21:22:51 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -50,13 +50,10 @@ static int ft_get_outfile(const char *line)
ft_eprintf("minishell: malloc failed\n");
return (-2);
}
fd = 1;
fd = 0;
i = 0;
while (tab[i + 1] != NULL)
{
if (tab[i][0] == '>')
if (fd != 1)
close(fd);
if (ft_strcmp(">", tab[i]) == 0)
fd = ft_file_is_writable(ft_quote_remover(tab[i + 1]));
else if (ft_strcmp(">>", tab[i]) == 0)

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/15 13:35:50 by cchauvet #+# #+# */
/* Updated: 2023/02/16 16:26:15 by cchauvet ### ########.fr */
/* Updated: 2023/02/16 14:53:16 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -101,8 +101,6 @@ char *ft_normalizer(char *str)
char *out;
char *temp;
if (ft_str_is_empty(str))
return (ft_strdup(" "));
temp = ft_spacer_after(str);
if (temp == NULL)
return (NULL);
@ -111,7 +109,6 @@ char *ft_normalizer(char *str)
if (out == NULL)
return (NULL);
ft_space_simplifier(out);
if (out[ft_strlen(out) - 1] == ' ')
out[ft_strlen(out) - 1] = '\0';
out[ft_strlen(out) - 1] = '\0';
return (out);
}

View File

@ -24,9 +24,7 @@ static int ft_pipe_is_alone(const char *str)
{
while (str[i] == ' ')
i++;
if (str[i] == '\0')
break ;
if (str[i] != '|')
if (str[i] != '|' && str[i] != '\0')
check = 1;
else
{
@ -91,7 +89,7 @@ int ft_empty_verif(const char *str)
int ft_syntatic_verif(const char *str)
{
return (ft_quote_verif(str)
|| ft_empty_verif(str)
|| ft_pipe_is_alone(str)
|| ft_empty_verif(str)
|| ft_special_char_dub(str));
}

View File

@ -1,23 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_str_is_empty.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/16 16:12:46 by cchauvet #+# #+# */
/* Updated: 2023/02/16 16:13:07 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "utils.h"
int ft_str_is_empty(const char *str)
{
size_t i;
i = 0;
while (str[i] == ' ')
i++;
return (str[i] == '\0');
}

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/14 14:46:40 by cchauvet #+# #+# */
/* Updated: 2023/02/16 16:13:47 by cchauvet ### ########.fr */
/* Updated: 2023/02/16 15:15:05 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -21,7 +21,7 @@ char *ft_strreplace(const char *str, const char *fill,
size_t start, size_t stop);
ssize_t ft_strnchr(const char *str, char c);
char *ft_getstr(const char *str, size_t n);
int ft_str_is_empty(const char *str);
void ft_printn(const char *str, size_t n);
char **ft_split_quoted(const char *s, char c);
void ft_strshift(char *str, int shift);
char *ft_quote_remover(char *str);