fix quote je pense ca marche

This commit is contained in:
Camille Chauvet 2023-02-15 20:52:27 +01:00
parent c5467769d9
commit 99fdd578e9
117 changed files with 613 additions and 375 deletions

BIN
.heredoc.c.swp Normal file

Binary file not shown.

BIN
.infile.c.swp Normal file

Binary file not shown.

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_getstr.c utils/ft_printn.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 ft_split_quoted.c env.c execution.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} OBJS = ${SRCS:.c=.o}

5
bozo Normal file
View File

@ -0,0 +1,5 @@
d
d
d
test

0
bozobozo Normal file
View File

44
cmd.c Normal file
View File

@ -0,0 +1,44 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cmd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/15 14:18:21 by cchauvet #+# #+# */
/* Updated: 2023/02/15 14:18:38 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void ft_cmddel(void *ptr)
{
t_cmd *content;
content = (t_cmd *) ptr;
if (content->args != NULL)
ft_freer_tab_ultimate(1, content->args);
if (content->executable != NULL)
free(content->executable);
free(content);
}
int ft_cmd_filler(t_list *element, char **args)
{
t_cmd *content;
size_t i;
if (args == NULL)
return (1);
content = (t_cmd *)element->content;
i = 0;
while (args[i] != NULL)
{
ft_quote_remover(args[i]);
i++;
}
content->args = args;
content->executable = args[0];
return (0);
}

121
cmds.c Normal file
View File

@ -0,0 +1,121 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cmds.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/15 14:17:26 by cchauvet #+# #+# */
/* Updated: 2023/02/15 17:53:59 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int ft_cmds_init(t_list **cmds, size_t len)
{
t_cmd *content;
t_list *current;
size_t i;
*cmds = malloc(sizeof(t_list));
current = *cmds;
i = 0;
while (i < len)
{
content = malloc(sizeof(t_cmd));
if (content == NULL)
{
ft_lstclear(cmds, ft_cmddel);
return (1);
}
content->args = NULL;
content->executable = NULL;
content->fd_in = -1;
content->fd_out = -1;
current->content = content;
if (!((i + 1) < len))
{
current->next = NULL;
return (0);
}
current->next = malloc(sizeof(t_list));
if (current->next == NULL)
ft_lstclear(cmds, ft_cmddel);
current = current->next;
i++;
}
return (0);
}
int ft_cmds_prep(t_list **cmds, const char *line, int infile, int outfile)
{
size_t len;
t_cmd *cmd;
t_list *current;
len = ft_seglen_quoted(line, '|');
if (len == 0)
return (0);
if (ft_cmds_init(cmds, ft_seglen_quoted(line, '|')))
{
free(cmds);
return (1);
}
cmd = (t_cmd *)(*cmds)->content;
cmd->fd_in = infile;
current = *cmds;
while (current->next != NULL)
current = current->next;
cmd = (t_cmd *) current->content;
cmd->fd_out = outfile;
return (0);
}
int ft_cmds_fill(t_list **cmds, const char *line)
{
char **tab;
char **args;
t_list *current;
size_t i;
tab = ft_split_quoted(line, '|');
if (tab == NULL)
return (1);
i = 0;
current = *cmds;
while (tab[i] != NULL)
{
args = ft_split_quoted(tab[i], ' ');
if (ft_cmd_filler(current, args) == 1)
{
ft_lstclear(cmds, ft_cmddel);
ft_freer_tab_ultimate(2, args, tab);
return (1);
}
current = current->next;
i++;
}
ft_freer_tab_ultimate(1, tab);
return (0);
}
t_list **ft_parse_cmds(char *line)
{
int infile;
int outfile;
t_list **cmds;
cmds = malloc(sizeof(t_list *));
outfile = ft_outfile(line);
infile = ft_infile(line);
if (infile == -2)
return (NULL);
if (ft_syntatic_verif(line) == 1)
return (NULL);
if (ft_cmds_prep(cmds, line, infile, outfile) == 1)
return (NULL);
if (ft_cmds_fill(cmds, line) == 1)
return (NULL);
return (cmds);
}

4
env.c
View File

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/02 14:39:56 by erey-bet #+# #+# */ /* Created: 2023/02/02 14:39:56 by erey-bet #+# #+# */
/* Updated: 2023/02/14 15:21:28 by cchauvet ### ########.fr */ /* Updated: 2023/02/15 19:58:19 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -113,7 +113,7 @@ char *get_value_by_key(char *key, t_list **head)
return (NULL); return (NULL);
} }
int set_value_by_key(t_list **env, char *key, char *value) int set_value_key(t_list **env, char *key, char *value)
{ {
t_list *current; t_list *current;
char *temp; char *temp;

BIN
env.o

Binary file not shown.

View File

@ -26,7 +26,14 @@ static char *ft_get_executable_path(char *executable_name, char **env)
path = NULL; path = NULL;
if (executable_name[0] == '.' || executable_name[0] == '/') if (executable_name[0] == '.' || executable_name[0] == '/')
{
path = ft_strdup(executable_name); path = ft_strdup(executable_name);
if (path == NULL)
{
ft_eprintf("minishell: malloc failed\n");
return (NULL);
}
}
else else
{ {
tab = ft_split(ft_get_variable(env, "PATH"), ':'); tab = ft_split(ft_get_variable(env, "PATH"), ':');
@ -64,6 +71,8 @@ static int ft_excutor(t_cmd *cmd, char **env)
int pid; int pid;
int return_value; int return_value;
if (cmd->fd_in == -1 || cmd->fd_out == -1)
return (1);
pid = fork(); pid = fork();
if (pid == -1) if (pid == -1)
return (1); return (1);

Binary file not shown.

67
file.c
View File

@ -1,4 +1,17 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* file.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/15 17:36:11 by cchauvet #+# #+# */
/* Updated: 2023/02/15 17:41:13 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
#include <unistd.h>
int ft_file_is_readable(const char *path) int ft_file_is_readable(const char *path)
{ {
@ -9,58 +22,58 @@ int ft_file_is_readable(const char *path)
if (fd == -1) if (fd == -1)
{ {
ft_eprintf("minishell: %s: No such file or directory\n", path); ft_eprintf("minishell: %s: No such file or directory\n", path);
return (0); return (-1);
} }
readable = read(fd, "", 0); readable = read(fd, "", 0);
if (readable == -1) if (readable == -1)
{ {
ft_eprintf("minishell: %s: Permission denied\n", path); ft_eprintf("minishell: %s: Permission denied\n", path);
return (0); return (-1);
} }
close(fd); return (fd);
return (1);
} }
int ft_file_is_writeable(const char *path) int ft_file_is_writable(const char *path)
{ {
int writeable; int writeable;
int fd; int fd;
fd = open(path, O_WRONLY | O_CREAT, 0644); fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd == -1) if (fd == -1)
{ {
ft_eprintf("minishell: %s: Permission denied\n", path); ft_eprintf("minishell: %s: Permission denied\n", path);
return (0); return (-1);
} }
writeable = write(fd, "", 0); writeable = write(fd, "", 0);
if (writeable == -1) if (writeable == -1)
{ {
ft_eprintf("minishell: %s: Permission denied\n", path); ft_eprintf("minishell: %s: Permission denied\n", path);
return (0); return (-1);
} }
close(fd); return (fd);
return (1);
} }
char *ft_get_file_path(const char *infile) int ft_file_is_appendable(const char *path)
{ {
size_t i; int writeable;
size_t n; int fd;
n = 1; fd = open(path, O_WRONLY | O_CREAT | O_APPEND, 0644);
while (infile[-n] == infile[-1]) if (fd == -1)
n++;
i = 0;
while (infile[i] == ' ')
i++;
if (infile[i] == '\0' || infile[i] == '>' || infile[i] == '<')
{ {
ft_eprintf("minishell: syntax error near "); ft_eprintf("minishell: %s: Permission denied\n", path);
if ((infile[0] == '<' && n > 3) || (infile[0] == '>' && n > 2)) return (-1);
ft_eprintf("unexpected token `%c`\n", infile[i - 1]);
else
ft_eprintf("unexpected token `newline`\n");
return (NULL);
} }
return (ft_getstr(infile, i + 1)); writeable = write(fd, "", 0);
if (writeable == -1)
{
ft_eprintf("minishell: %s: Permission denied\n", path);
return (-1);
}
return (fd);
}
int ft_file_is_executable(const char *path)
{
return (access(path, X_OK) == 0);
} }

BIN
file.o

Binary file not shown.

Binary file not shown.

BIN
heredoc.o

Binary file not shown.

132
infile.c
View File

@ -1,76 +1,95 @@
#include "minishell.h" /* ************************************************************************** */
/* */
/* ::: :::::::: */
/* infile.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/15 17:52:10 by cchauvet #+# #+# */
/* Updated: 2023/02/15 20:47:19 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
static int ft_get_infile(char *line) #include "libftx/libftx.h"
#include "minishell.h"
#include "utils/utils.h"
static int ft_infile_is_valid(const char *line)
{
char **tab;
size_t i;
tab = ft_split_quoted(line, ' ');
if (tab == NULL)
{
ft_eprintf("minishell: malloc failed\n");
return (0);
}
i = 0;
while (tab[i + 1] != NULL)
i++;
if (tab[i][0] == '<')
{
ft_eprintf("minishell: %s: must be followed by an infile\n", tab[i]);
return (0);
}
ft_freer_tab_ultimate(1, tab);
return (1);
}
static int ft_get_infile(const char *line)
{ {
size_t i; size_t i;
int fd; int fd;
char *path; char **tab;
tab = ft_split_quoted(line, ' ');
if (tab == NULL)
{
ft_eprintf("minishell: malloc failed\n");
return (-2);
}
fd = 0; fd = 0;
i = 0; i = 0;
while (line[i] != '\0') while (tab[i + 1] != NULL)
{ {
if (line[i] == '<' && ft_is_in_quote(line, i) == 0) if (ft_strcmp("<", tab[i]) == 0)
{ fd = ft_file_is_readable(ft_quote_remover(tab[i + 1]));
i++; else if (ft_strcmp("<<", tab[i]) == 0)
if (fd != 0) fd = ft_heredoc(tab[i + 1]);
close(fd);
if (line[i] == '<')
{
i++;
path = ft_get_file_path(line + i);
if (path == NULL)
return (-1);
fd = ft_heredoc(path);
}
else
{
path = ft_get_file_path(line + i);
if (path == NULL)
return (-1);
if (ft_file_is_readable(path) == 0)
{
free(path);
return (-1);
}
fd = open(path, O_RDONLY);
}
free(path);
}
i++; i++;
} }
ft_freer_tab_ultimate(1, tab);
return (fd); return (fd);
} }
static int ft_remove_infile(char *line) static int ft_remove_infile(char *line)
{ {
size_t i; size_t i;
int separator; size_t y;
char **tab;
i = 0; tab = ft_split_quoted(line, ' ');
while (line[i] != '\0') if (tab == NULL)
{ {
if (line[i] == '<' && ft_is_in_quote(line, i) == 0) ft_eprintf("minishell: malloc failed\n");
return (1);
}
i = 0;
y = 0;
while (tab[i] != NULL)
{
if (tab[i][0] == '<')
{ {
while (line[i] == '<') ft_strshift(line + y, -1 * (ft_strlen(tab[i]) + 1));
line[i++] = ' '; i++;
while (line[i] == ' ') ft_strshift(line + y, -1 * (ft_strlen(tab[i]) + 1));
i++;
separator = ft_is_in_quote(line, i);
if (separator == 0)
separator = ' ';
else if (separator == 1)
separator = '\'';
else
separator = '\"';
while (line[i] != separator && line[i] != '\0')
line[i++] = ' ';
if (line[i] != '\0'
&& (separator == '\'' || separator == '\"'))
line[i++] = ' ';
} }
else
y = y + ft_strlen(tab[i]);
i++; i++;
} }
ft_freer_tab_ultimate(1, tab);
return (0); return (0);
} }
@ -78,12 +97,11 @@ int ft_infile(char *line)
{ {
int fd; int fd;
if (ft_infile_is_valid(line) == 0)
return (-2);
fd = ft_get_infile(line); fd = ft_get_infile(line);
if (ft_remove_infile(line)) if (fd == -2)
{ return (-2);
if (fd > 0) ft_remove_infile(line);
close(fd);
return (-1);
}
return (fd); return (fd);
} }

BIN
infile.o

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

211
main.c
View File

@ -1,210 +1,53 @@
#include "libftx/libftx.h" #include "libftx/libftx.h"
#include "minishell.h" #include "minishell.h"
#include <fcntl.h>
void ft_lstdel(void *ptr) /* int main(int ac, char **av) */
{ /* { */
t_cmd *content; /* char *line; */
/* int fd; */
content = (t_cmd *) ptr; /* */
if (content->args != NULL) /* if (ac != 2) */
ft_freer_tab_ultimate(1, content->args); /* return (1); */
if (content->executable != NULL) /* fd = open(av[1], O_RDONLY); */
free(content->executable); /* line = get_next_line(fd); */
free(content); /* while (line != NULL) */
} /* { */
/* ft_printf(line); */
int ft_cmds_init(t_list **cmds, size_t len) /* line = get_next_line(fd); */
{ /* free(line); */
t_cmd *content; /* } */
t_list *current; /* free(line); */
size_t i; /* return (0); */
/* } */
*cmds = malloc(sizeof(t_list));
current = *cmds;
i = 0;
while (i < len)
{
content = malloc(sizeof(t_cmd));
if (content == NULL)
{
ft_lstclear(cmds, ft_lstdel);
return (1);
}
content->args = NULL;
content->executable = NULL;
content->fd_in = -1;
content->fd_out = -1;
current->content = content;
if (!((i + 1) < len))
{
current->next = NULL;
return (0);
}
current->next = malloc(sizeof(t_list));
if (current->next == NULL)
ft_lstclear(cmds, ft_lstdel);
current = current->next;
i++;
}
return (0);
}
int ft_cmds_prep(t_list **cmds, const char *line, int infile, int outfile)
{
size_t len;
t_cmd *cmd;
t_list *current;
len = ft_seglen_quoted(line, '|');
if (len == 0)
return (0);
if (ft_cmds_init(cmds, ft_seglen_quoted(line, '|')))
{
free(cmds);
return (1);
}
cmd = (t_cmd *)(*cmds)->content;
cmd->fd_in = infile;
current = *cmds;
while (current->next != NULL)
current = current->next;
cmd = (t_cmd *) current->content;
cmd->fd_out = outfile;
return (0);
}
void ft_strshift(char *str, int shift)
{
size_t i;
if (shift > 0)
return ;
i = 0;
while (str[i - shift] != '\0')
{
str[i] = str[i - shift];
i++;
}
str[i + shift] = '\0';
}
void ft_quote_remover(char *str)
{
size_t i;
ssize_t start;
ssize_t stop;
start = -1;
i = 0;
while (str[i] != '\0')
{
if ((str[i] == '\"' || str[i] == '\''))
{
if (start == -1)
start = i;
else if (str[i] == str[start])
{
stop = i;
break ;
}
}
i++;
}
if (start != -1)
{
ft_strshift(str, -1);
str[stop] = '\0';
}
}
int ft_cmd_filler(t_list *element, char **args)
{
t_cmd *content;
size_t i;
if (args == NULL)
return (1);
content = (t_cmd *)element->content;
i = 0;
while (args[i] != NULL)
{
ft_quote_remover(args[i]);
i++;
}
content->args = args;
//pas free:10
content->executable = args[0];
return (0);
}
int ft_cmds_fill(t_list **cmds, const char *line)
{
char **tab;
char **args;
t_list *current;
size_t i;
tab = ft_split_quoted(line, '|');
if (tab == NULL)
return (1);
i = 0;
current = *cmds;
while (tab[i] != NULL)
{
args = ft_split_quoted(tab[i], ' ');
if (ft_cmd_filler(current, args) == 1)
{
ft_lstclear(cmds, ft_lstdel);
ft_freer_tab_ultimate(2, args, tab);
return (1);
}
current = current->next;
i++;
}
ft_freer_tab_ultimate(1, tab);
return (0);
}
t_list **ft_parse_cmds(char *line)
{
int infile;
int outfile;
t_list **cmds;
cmds = malloc(sizeof(t_list *));
outfile = ft_outfile(line);
infile = ft_infile(line);
if (ft_syntatic_verif(line) == 1)
return (NULL);
if (ft_cmds_prep(cmds, line, infile, outfile) == 1)
return (NULL);
if (ft_cmds_fill(cmds, line) == 1)
return (NULL);
return (cmds);
}
int main(int ac, char **av, char **env) int main(int ac, char **av, char **env)
{ {
t_list **cmds; t_list **cmds;
char *line;
t_data data; t_data data;
if (ac == 1) if (ac == 1)
return (1); return (1);
line = ft_normalizer(av[1]);
ft_printf("%s\n", line);
data.env = init_env(env); data.env = init_env(env);
if (data.env == NULL) if (data.env == NULL)
return (1); return (1);
cmds = ft_parse_cmds(av[1]); cmds = ft_parse_cmds(line);
if (cmds == NULL) if (cmds == NULL)
return (1); return (1);
ft_printf("%s\n", line);
if (ft_cmds_executor(cmds, env) == 1) if (ft_cmds_executor(cmds, env) == 1)
{ {
ft_lstclear(data.env, env_del); ft_lstclear(data.env, env_del);
ft_lstclear(cmds, ft_lstdel); ft_lstclear(cmds, ft_cmddel);
return (1); return (1);
} }
ft_lstclear(data.env, env_del); ft_lstclear(data.env, env_del);
ft_lstclear(cmds, ft_lstdel); ft_lstclear(cmds, ft_cmddel);
free(cmds); free(cmds);
free(data.env); free(data.env);
free(line);
return (0); return (0);
} }

BIN
main.o

Binary file not shown.

Some files were not shown because too many files have changed in this diff Show More