This commit is contained in:
Etienne Rey-bethbeder 2023-02-14 13:55:02 +01:00
commit b20d4e0ddc
36 changed files with 737 additions and 186 deletions

Binary file not shown.

View File

@ -1,13 +1,13 @@
UTILS_SRC = utils/ft_is_in_quote.c utils/ft_strncpy.c utils/ft_strreplace.c utils/ft_strnchr.c utils/ft_getstr.c
SRCS = env.c ${UTILS_SRC}
SRCS = ${UTILS_SRC} main.c file.c infile.c outfile.c heredoc.c syntatics.c ft_split_quoted.c env.c
OBJS = ${SRCS:.c=.o}
NAME = minishell
CC = clang
CC = gcc
CFLAGS = -g -Wall -Werror -Wextra
CFLAGS = -Wall -Werror -Wextra -g
LIBS = libftx/libftx.a
@ -18,7 +18,7 @@ all: ${NAME}
${NAME}: ${OBJS}
make -C libftx all
${CC} ${OBJS} -o ${NAME} ${LIBS}
${CC} ${OBJS} -o ${NAME} ${LIBS} -lreadline
clean:
make -C libftx clean

BIN
argprinter Executable file

Binary file not shown.

34
execution.c Normal file
View File

@ -0,0 +1,34 @@
#include "libftx/libftx.h"
#include "minishell.h"
#include <unistd.h>
int main(int ac, char **av, char **env)
{
}
/* char *ft_get_executable_path(t_data *data, char *executable) */
/* { */
/* if (ft_strcmp(executable, "env") == 0) */
/* return (ft_strjoin("", executable)); */
/* else */
/* return */
/* } */
int ft_excutor(t_cmd *cmd)
{
int pid;
pid = fork();
if (pid == -1)
return (1);
if (pid == 0)
{
dup2(cmd->fd_out, 1);
dup2(cmd->fd_in, 0);
//TODO ADD ENV VARIABLES
execve(cmd->executable, cmd->args, NULL);
}
else
waitpid(pid);
}

66
file.c Normal file
View File

@ -0,0 +1,66 @@
#include "minishell.h"
int ft_file_is_readable(const char *path)
{
int readable;
int fd;
fd = open(path, O_RDONLY);
if (fd == -1)
{
ft_eprintf("minishell: %s: No such file or directory\n", path);
return (0);
}
readable = read(fd, "", 0);
if (readable == -1)
{
ft_eprintf("minishell: %s: Permission denied\n", path);
return (0);
}
close(fd);
return (1);
}
int ft_file_is_writeable(const char *path)
{
int writeable;
int fd;
fd = open(path, O_WRONLY | O_CREAT, 0644);
if (fd == -1)
{
ft_eprintf("minishell: %s: Permission denied\n", path);
return (0);
}
writeable = write(fd, "", 0);
if (writeable == -1)
{
ft_eprintf("minishell: %s: Permission denied\n", path);
return (0);
}
close(fd);
return (1);
}
char *ft_get_file_path(const char *infile)
{
size_t i;
size_t n;
n = 1;
while (infile[-n] == infile[-1])
n++;
i = 0;
while (infile[i] == ' ')
i++;
if (infile[i] == '\0' || infile[i] == '>' || infile[i] == '<')
{
ft_eprintf("minishell: syntax error near ");
if ((infile[0] == '<' && n > 3) || (infile[0] == '>' && n > 2))
ft_eprintf("unexpected token `%c`\n", infile[i - 1]);
else
ft_eprintf("unexpected token `newline`\n");
return (NULL);
}
return (ft_getstr(infile, i + 1));
}

75
ft_split_quoted.c Normal file
View File

@ -0,0 +1,75 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split_quoted.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/05 19:04:34 by cchauvet #+# #+# */
/* Updated: 2023/01/06 19:33:51 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
size_t ft_seglen_quoted(const char *str, char c)
{
size_t len;
size_t i;
len = 0;
i = 0;
while (str[i] != 0)
{
while ((str[i] == c || ft_is_in_quote(str, i)) && str[i] != 0)
i++;
if (str[i] != 0)
len++;
while ((str[i] != c || ft_is_in_quote(str, i)) && str[i] != 0)
i++;
}
return (len);
}
static char **ft_segsplitter(char **tab, size_t len, const char *s, char c)
{
size_t tab_index;
size_t let_index;
size_t start;
tab_index = 0;
let_index = 0;
start = 0;
if (tab == NULL || s == NULL)
return (NULL);
while (tab_index < len)
{
while ((s[let_index] == c || ft_is_in_quote(s, let_index)) && s[let_index] != 0)
let_index++;
start = let_index;
while ((s[let_index] != c || ft_is_in_quote(s, let_index)) && s[let_index] != 0)
let_index++;
tab[tab_index] = ft_substr(s, start, let_index - start);
if (tab[tab_index] == NULL)
return (ft_cancel((void *)tab, tab_index));
tab_index++;
}
return (tab);
}
char **ft_split_quoted(const char *s, char c)
{
size_t len;
char **tab;
if (s == NULL)
return (NULL);
len = ft_seglen_quoted(s, c);
tab = malloc((len + 1) * sizeof(char *));
if (tab == NULL)
return (NULL);
tab[len] = NULL;
if (ft_segsplitter(tab, len, s, c) == NULL)
return (NULL);
return (tab);
}

25
heredoc.c Normal file
View File

@ -0,0 +1,25 @@
#include "minishell.h"
#include <readline/history.h>
int ft_heredoc(char *stop)
{
int fds[2];
char *line;
pipe(fds);
line = readline("> ");
while (line != NULL)
{
if (ft_strcmp(line, stop) == 0)
{
free(line);
break ;
}
ft_putstr_fd(line, fds[1]);
add_history(line);
free(line);
line = readline("> ");
}
close(fds[1]);
return (fds[0]);
}

89
infile.c Normal file
View File

@ -0,0 +1,89 @@
#include "minishell.h"
static int ft_get_infile(char *line)
{
size_t i;
int fd;
char *path;
fd = 0;
i = 0;
while (line[i] != '\0')
{
if (line[i] == '<' && ft_is_in_quote(line, i) == 0)
{
i++;
if (fd != 0)
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++;
}
return (fd);
}
static int ft_remove_infile(char *line)
{
size_t i;
int separator;
i = 0;
while (line[i] != '\0')
{
if (line[i] == '<' && ft_is_in_quote(line, i) == 0)
{
while (line[i] == '<')
line[i++] = ' ';
while (line[i] == ' ')
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++] = ' ';
}
i++;
}
return (0);
}
int ft_infile(char *line)
{
int fd;
fd = ft_get_infile(line);
if (ft_remove_infile(line))
{
if (fd > 0)
close(fd);
return (-1);
}
return (fd);
}

View File

@ -12,7 +12,7 @@
CC = clang
SRCS = ft_contain_only.c ft_freer.c ft_is_in.c ft_random_generator.c ft_strchri.c ft_strcmp.c ft_strfjoin.c ft_strgen.c ft_strmerger.c ft_strndup.c ft_tabrealloc.c ft_ultoa_base.c
SRCS = ft_contain_only.c ft_freer.c ft_is_in.c ft_random_generator.c ft_strchri.c ft_strcmp.c ft_strfjoin.c ft_strgen.c ft_strmerger.c ft_strndup.c ft_tabrealloc.c ft_ultoa_base.c ft_swap.c
OBJS = $(SRCS:.c=.o)

View File

@ -28,9 +28,13 @@ char *ft_strfjoin(char *s1, char *s2);
char *ft_strmerger(size_t arg_len, ...);
int ft_is_in(char *str, char c);
char **ft_tabrealloc(char **tab, size_t current_size, size_t new_size);
char *ft_strndup(char *src, size_t n);
char *ft_strndup(const char *src, size_t n);
ssize_t ft_strchri(char *str, char c);
int ft_contain_only_str(char *str, char *to_find);
int ft_contain_only(char *str, char c);
int ft_strcmp(char *s1, char *s2);
void ft_swap(void *a, void *b);
void ft_swap_int(int *a, int *b);
void ft_swap_char(char *a, char *b);
#endif

View File

@ -12,7 +12,7 @@
#include "extra.h"
char *ft_strndup(char *src, size_t n)
char *ft_strndup(const char *src, size_t n)
{
char *out;
size_t i;

28
libftx/extra/ft_swap.c Normal file
View File

@ -0,0 +1,28 @@
#include "extra.h"
void ft_swap(void *a, void *b)
{
void *c;
c = a;
a = b;
b = c;
}
void ft_swap_char(char *a, char *b)
{
char c;
c = *a;
*a = *b;
*b = c;
}
void ft_swap_int(int *a, int *b)
{
int c;
c = *a;
*a = *b;
*b = c;
}

View File

@ -45,9 +45,8 @@ SRCS = ft_isalpha.c \
ft_putchar_fd.c \
ft_putstr_fd.c \
ft_putendl_fd.c \
ft_putnbr_fd.c
BSRCS = ft_lstnew.c \
ft_putnbr_fd.c \
ft_lstnew.c \
ft_lstadd_front.c \
ft_lstsize.c \
ft_lstlast.c \
@ -59,8 +58,6 @@ BSRCS = ft_lstnew.c \
OBJS = $(SRCS:.c=.o)
BOBJS = $(BSRCS:.c=.o)
NAME = libft.a
CFLAGS = -Wall -Werror -Wextra -g
@ -73,15 +70,12 @@ all: $(NAME)
$(NAME): $(OBJS)
ar -rc $(NAME) $(OBJS)
bonus: $(OBJS) $(BOBJS)
ar -rc $(NAME) $(OBJS) $(BOBJS)
clean:
rm -f $(OBJS) $(BOBJS)
rm -f $(OBJS)
fclean: clean
rm -f $(NAME)
re: fclean all
.PHONY: all bonus clean fclean re
.PHONY: all clean fclean re

View File

@ -12,7 +12,7 @@
#include "libft.h"
void *ft_cancel(char **tab, size_t len)
void *ft_cancel(void **tab, size_t len)
{
size_t i;
@ -68,7 +68,7 @@ static char **ft_segsplitter(char **tab, size_t len, const char *s, char c)
let_index++;
tab[tab_index] = ft_substr(s, start, let_index - start);
if (tab[tab_index] == NULL)
return (ft_cancel(tab, tab_index));
return (ft_cancel((void **)tab, tab_index));
tab_index++;
}
return (tab);

View File

@ -15,7 +15,7 @@
# include <stdlib.h>
# include <unistd.h>
void *ft_cancel(char **tab, size_t len);
void *ft_cancel(void **tab, size_t len);
int ft_atoi(const char *nptr);
void ft_bzero(void *s, size_t n);
void *ft_calloc(size_t nmemb, size_t size);

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/26 14:47:54 by cchauvet #+# #+# */
/* Updated: 2023/02/03 12:34:10 by erey-bet ### ########.fr */
/* Updated: 2023/02/03 12:42:13 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -32,9 +32,12 @@ char *ft_strfjoin(char *s1, char *s2);
char *ft_strmerger(size_t arg_len, ...);
int ft_strcmp(char *s1, char *s2);
ssize_t ft_strchri(char *str, char c);
char *ft_strndup(char *src, size_t n);
char *ft_strndup(const char *src, size_t n);
void ft_swap(void *a, void *b);
void ft_swap_int(int *a, int *b);
void ft_swap_char(char *a, char *b);
void *ft_cancel(char **tab, size_t len);
void *ft_cancel(void **tab, size_t len);
int ft_atoi(const char *nptr);
void ft_bzero(void *s, size_t n);
void *ft_calloc(size_t nmemb, size_t size);
@ -68,7 +71,6 @@ char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
void ft_striteri(char *s, void (*f)(unsigned int, char*));
void ft_putchar_fd(int fd, char c);
void ft_putstr_fd(char *s, int fd);
void ft_putendl_fd(int fd, char *s);
void ft_putnbr_fd(int fd, int n);

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/23 18:08:31 by cchauvet #+# #+# */
/* Updated: 2023/01/05 17:37:12 by cchauvet ### ########.fr */
/* Updated: 2023/02/03 12:52:38 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -27,7 +27,7 @@ int ft_dprintarg(int fd, int arg, va_list args)
if (arg == 'S')
return (ft_dprintstrtab(fd, va_arg(args, char **)));
if (arg == 's')
return (ft_putstr_fd(fd, va_arg(args, char *)));
return (ft_putstr_fd_p(fd, va_arg(args, char *)));
if (arg == '%')
return (ft_putchar_fd(fd, '%'));
if (arg == 'p')

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/06 17:41:04 by cchauvet #+# #+# */
/* Updated: 2022/10/12 15:22:40 by cchauvet ### ########.fr */
/* Updated: 2023/02/03 12:53:45 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -18,10 +18,10 @@ int ft_dprintptr(int fd, void *ptr)
int i;
if (ptr == NULL)
return (ft_putstr_fd(fd, "(nil)"));
return (ft_putstr_fd_p(fd, "(nil)"));
address = (unsigned long) ptr;
i = 2;
ft_putstr_fd(fd, "0x");
ft_putstr_fd_p(fd, "0x");
i += ft_dprintul_base(fd, address, "0123456789abcdef");
return (i);
}

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/05 17:26:55 by cchauvet #+# #+# */
/* Updated: 2023/01/05 18:52:56 by cchauvet ### ########.fr */
/* Updated: 2023/02/03 12:54:44 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -21,7 +21,7 @@ int ft_dprintstrtab(int fd, char **tab)
index = 0;
while (tab[index] != NULL)
{
i += ft_putstr_fd(fd, tab[index]) + 1;
i += ft_putstr_fd_p(fd, tab[index]) + 1;
ft_putchar_fd(fd, '\n');
index++;
}

View File

@ -12,7 +12,7 @@
#include "ft_printf.h"
int ft_printf(const char *format, ...)
int ft_eprintf(const char *format, ...)
{
va_list va;
int i;

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/26 14:47:54 by cchauvet #+# #+# */
/* Updated: 2023/02/01 16:19:38 by cchauvet ### ########.fr */
/* Updated: 2023/02/03 12:51:07 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -37,6 +37,6 @@ int ft_eprintf(const char *format, ...);
int ft_vdprintf(int fd, const char *format, va_list va);
int ft_putchar_fd(int fd, char c);
int ft_putstr_fd(int fd, char *str);
int ft_putstr_fd_p(int fd, char *str);
#endif

View File

@ -6,13 +6,13 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 22:25:08 by cchauvet #+# #+# */
/* Updated: 2022/10/12 16:51:51 by cchauvet ### ########.fr */
/* Updated: 2023/02/03 12:49:18 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_putstr_fd(int fd, char *str)
int ft_putstr_fd_p(int fd, char *str)
{
int i;

240
main.c
View File

@ -1,56 +1,228 @@
#include "libftx/libftx.h"
#include "minishell.h"
int ft_get_infile(char **line)
void ft_lstdel(void *ptr)
{
size_t i;
char *path;
t_cmd *content;
path = NULL;
i = 0;
while ((*line)[i] != '\0')
{
if ((*line)[i] == '<' && ft_is_in_quote(*line, i) == 0)
{
if (path != NULL)
free(path);
path = ft_getstr(*line, i);
if (path == NULL)
return (-1);
if (ft_file_is_readable(path) == 0)
{
free(path);
return (-1);
}
}
i++;
}
return (open(path, O_RDONLY));
content = (t_cmd *) ptr;
if (content->executable != NULL)
free(content->executable);
if (content->args != NULL)
ft_freer_tab_ultimate(1, content->args);
free(content);
}
t_list **ft_parse_cmd(char *line)
int ft_cmds_init(t_list **cmds, size_t len)
{
char infile;
char outfile;
t_list **cmds;
t_cmd *content;
t_list *current;
size_t i;
(void) outfile;
(void) cmds;
*cmds = malloc(sizeof(t_list));
current = *cmds;
i = 0;
while (line[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;
//TODO check if executable exist
//TODO change it by env value
//TODO add switch to bultin
content->executable = ft_strjoin("/usr/bin/", args[0]);
if (content->executable == NULL)
return (1);
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);
ft_printf("%s\n", line);
return (cmds);
}
int ft_cmds_excutor(t_list **cmds)
{
t_cmd *content;
t_list *current;
size_t i;
i = 0;
current = *cmds;
while (current != NULL)
{
content = current->content;
ft_printf("--- COMMAND %d\n", i);
ft_printf("excutable: %s\n", content->executable);
ft_printf("args:\n%S", content->args);
current = current->next;
i++;
}
return (0);
}
int main(int ac, char **av)
{
int fd;
int i;
t_list **cmds;
if (ac == 1)
return (1);
ft_parse_cmd(av[1]);
cmds = ft_parse_cmds(av[1]);
if (cmds == NULL)
return (1);
if (ft_cmds_excutor(cmds) == 1)
{
ft_lstclear(cmds, ft_lstdel);
return (1);
}
ft_lstclear(cmds, ft_lstdel);
free(cmds);
return (1);
}

BIN
main.o

Binary file not shown.

View File

@ -1,24 +1,36 @@
#ifndef FT_MINISHELL
# define FT_MINISHELL
#ifndef MINISHELL_H
# define MINISHELL_H
# include "libftx/libftx.h"
# include "utils/utils.h"
# include <sys/types.h>
# include <sys/stat.h>
# include <fcntl.h>
# include <sys/wait.h>
# include <stdio.h>
# include <readline/readline.h>
# include <readline/history.h>
int ft_file_is_readable(char *path);
int ft_file_is_writeable(char *path);
t_list **init_env(char **env);
char *get_value_index(int index, t_list **head);
char *get_value_key(char *key, t_list **head);
typedef struct cmd
int ft_syntatic_verif(const char *str);
int ft_file_is_readable(const char *path);
int ft_file_is_writeable(const char *path);
char *ft_get_file_path(const char *infile);
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);
char **ft_split_quoted(const char *s, char c);
typedef struct s_cmd
{
int fd_in;
int fd_out;
char *cmd;
char *args;
} cmd;
char *executable;
char **args;
} t_cmd;
typedef struct s_data
{

89
outfile.c Normal file
View File

@ -0,0 +1,89 @@
#include "minishell.h"
static int ft_get_outfile(char *line)
{
size_t i;
int append;
char *path;
path = NULL;
append = 0;
i = 0;
while (line[i] != '\0')
{
if (line[i] == '>' && ft_is_in_quote(line, i) == 0)
{
i++;
if (line[i] == '>')
{
i++;
append = 1;
}
else
append = 0;
if (path != NULL)
free(path);
path = ft_get_file_path(line + i);
if (path == NULL || ft_file_is_writeable(path) == 0)
{
free(path);
return (-1);
}
}
i++;
}
if (path == NULL)
return (-2);
if (append == 1)
i = open(path, O_APPEND | O_CREAT, 0664);
else
i = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0664);
free(path);
return (i);
}
static int ft_remove_outfile(char *line)
{
size_t i;
int separator;
i = 0;
while (line[i] != '\0')
{
if (line[i] == '>' && ft_is_in_quote(line, i) == 0)
{
while (line[i] == '>')
line[i++] = ' ';
while (line[i] == ' ')
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++] = ' ';
}
i++;
}
return (0);
}
int ft_outfile(char *line)
{
int fd;
fd = ft_get_outfile(line);
if (ft_remove_outfile(line))
{
if (fd > 0)
close(fd);
return (-1);
}
return (fd);
}

56
syntatics.c Normal file
View File

@ -0,0 +1,56 @@
#include "libftx/libftx.h"
#include "minishell.h"
static int ft_quote_verif(const char *str)
{
if (ft_is_in_quote(str, ft_strlen(str)))
{
ft_eprintf("minishell: Quote is note closed");
return (1);
}
else
return (0);
}
static int ft_multipipe(const char *str)
{
size_t i;
size_t y;
i = 0;
while (str[i] != '\0')
{
y = 0;
while (str[i + y] == '|' && !ft_is_in_quote(str, i))
{
if (y > 0)
{
ft_eprintf("minishell: Multiple pipes is not supported\n");
return (1);
}
y++;
}
i++;
}
return (0);
}
static int ft_pipe_is_alone(const char *str)
{
size_t i;
i = ft_strlen(str) - 1;
while (str[i] != '|' && i > 0)
{
if (str[i] != ' ')
return (0);
i--;
}
ft_eprintf("minishell: Pipe must be followed by a command or redirection\n");
return (1);
}
int ft_syntatic_verif(const char *str)
{
return (ft_quote_verif(str) || ft_multipipe(str) || ft_pipe_is_alone(str));
}

50
test1
View File

@ -1,50 +0,0 @@
COLORTERM=truecolor
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/101231/bus,guid=621c3599211e51a4e50420bf63dcee5f
DBUS_STARTER_ADDRESS=unix:path=/run/user/101231/bus,guid=621c3599211e51a4e50420bf63dcee5f
DBUS_STARTER_BUS_TYPE=session
DESKTOP_SESSION=ubuntu
DISPLAY=:0
DOCKER_HOST=unix:///run/user/101231/docker.sock
GDMSESSION=ubuntu
GDM_LANG=en
GNOME_DESKTOP_SESSION_ID=this-is-deprecated
GNOME_TERMINAL_SCREEN=/org/gnome/Terminal/screen/277eb9d7_4a54_437b_9556_e8825e1c7429
GNOME_TERMINAL_SERVICE=:1.89
GPG_AGENT_INFO=/run/user/101231/gnupg/S.gpg-agent:0:1
GTK_MODULES=gail:atk-bridge
HOME=/nfs/homes/erey-bet
IM_CONFIG_PHASE=1
INVOCATION_ID=9d85372853b94435bbce5d2edac38c19
JOURNAL_STREAM=9:2506910
KRB5CCNAME=FILE:/tmp/krb5cc_101231_mfTZPs
LANG=en_US.UTF-8
LANGUAGE=en
LOGNAME=erey-bet
MANAGERPID=763384
OLDPWD=/nfs/homes/erey-bet/travaux/Cursus42/minishell
PATH=/nfs/homes/erey-bet/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
PWD=/nfs/homes/erey-bet/travaux/Cursus42/minishell
QT_ACCESSIBILITY=1
QT_IM_MODULE=ibus
SESSION_MANAGER=local/1D2.42angouleme.fr:@/tmp/.ICE-unix/763641,unix/1D2.42angouleme.fr:/tmp/.ICE-unix/763641
SHELL=/bin/zsh
SHLVL=2
SSH_AGENT_PID=763603
SSH_AUTH_SOCK=/run/user/101231/keyring/ssh
TERM=xterm-256color
USER=erey-bet
VTE_VERSION=6003
XAUTHORITY=/nfs/homes/erey-bet/.Xauthority
XDG_CONFIG_DIRS=/etc/xdg/xdg-ubuntu:/etc/xdg
XDG_CURRENT_DESKTOP=ubuntu:GNOME
XDG_DATA_DIRS=/usr/share/gnome:/usr/share/ubuntu:/nfs/homes/erey-bet/.local/share/flatpak/exports/share:/var/lib/flatpak/exports/share:/usr/local/share:/usr/share:/var/lib/snapd/desktop
XDG_GREETER_DATA_DIR=/var/lib/lightdm-data/erey-bet
XDG_MENU_PREFIX=gnome-
XDG_RUNTIME_DIR=/run/user/101231
XDG_SEAT_PATH=/org/freedesktop/DisplayManager/Seat0
XDG_SESSION_CLASS=user
XDG_SESSION_DESKTOP=ubuntu
XDG_SESSION_PATH=/org/freedesktop/DisplayManager/Session3
XDG_SESSION_TYPE=x11
XMODIFIERS=@im=ibus
_=./minishell

50
test2
View File

@ -1,50 +0,0 @@
SHELL=/bin/zsh
SESSION_MANAGER=local/1D2.42angouleme.fr:@/tmp/.ICE-unix/763641,unix/1D2.42angouleme.fr:/tmp/.ICE-unix/763641
QT_ACCESSIBILITY=1
COLORTERM=truecolor
XDG_CONFIG_DIRS=/etc/xdg/xdg-ubuntu:/etc/xdg
XDG_SESSION_PATH=/org/freedesktop/DisplayManager/Session3
XDG_MENU_PREFIX=gnome-
GNOME_DESKTOP_SESSION_ID=this-is-deprecated
LANGUAGE=en
SSH_AUTH_SOCK=/run/user/101231/keyring/ssh
XMODIFIERS=@im=ibus
DESKTOP_SESSION=ubuntu
SSH_AGENT_PID=763603
GTK_MODULES=gail:atk-bridge
DBUS_STARTER_BUS_TYPE=session
KRB5CCNAME=FILE:/tmp/krb5cc_101231_mfTZPs
PWD=/nfs/homes/erey-bet/travaux/Cursus42/minishell
LOGNAME=erey-bet
XDG_SESSION_DESKTOP=ubuntu
XDG_SESSION_TYPE=x11
GPG_AGENT_INFO=/run/user/101231/gnupg/S.gpg-agent:0:1
XAUTHORITY=/nfs/homes/erey-bet/.Xauthority
XDG_GREETER_DATA_DIR=/var/lib/lightdm-data/erey-bet
GDM_LANG=en
HOME=/nfs/homes/erey-bet
IM_CONFIG_PHASE=1
LANG=en_US.UTF-8
XDG_CURRENT_DESKTOP=ubuntu:GNOME
VTE_VERSION=6003
XDG_SEAT_PATH=/org/freedesktop/DisplayManager/Seat0
GNOME_TERMINAL_SCREEN=/org/gnome/Terminal/screen/277eb9d7_4a54_437b_9556_e8825e1c7429
INVOCATION_ID=9d85372853b94435bbce5d2edac38c19
MANAGERPID=763384
XDG_SESSION_CLASS=user
TERM=xterm-256color
USER=erey-bet
GNOME_TERMINAL_SERVICE=:1.89
DISPLAY=:0
SHLVL=2
QT_IM_MODULE=ibus
DBUS_STARTER_ADDRESS=unix:path=/run/user/101231/bus,guid=621c3599211e51a4e50420bf63dcee5f
XDG_RUNTIME_DIR=/run/user/101231
DOCKER_HOST=unix:///run/user/101231/docker.sock
JOURNAL_STREAM=9:2506910
XDG_DATA_DIRS=/usr/share/gnome:/usr/share/ubuntu:/nfs/homes/erey-bet/.local/share/flatpak/exports/share:/var/lib/flatpak/exports/share:/usr/local/share:/usr/share:/var/lib/snapd/desktop
PATH=/nfs/homes/erey-bet/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
GDMSESSION=ubuntu
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/101231/bus,guid=621c3599211e51a4e50420bf63dcee5f
OLDPWD=/nfs/homes/erey-bet/travaux/Cursus42/minishell
_=/usr/bin/env

View File

@ -1,6 +1,6 @@
#include "utils.h"
char *ft_getstr(char *str, size_t n)
char *ft_getstr(const char *str, size_t n)
{
size_t start;
size_t stop;

View File

@ -1,6 +1,6 @@
#include "utils.h"
int ft_is_in_quote(char *str, size_t n)
int ft_is_in_quote(const char *str, size_t n)
{
size_t double_quoted;
size_t simple_quoted;

View File

@ -1,6 +1,6 @@
#include "utils.h"
ssize_t ft_strnchr(char *str, char c)
ssize_t ft_strnchr(const char *str, char c)
{
size_t i;

View File

@ -1,6 +1,6 @@
#include "utils.h"
size_t ft_strncpy(char *dst, char *src, size_t n)
size_t ft_strncpy(char *dst, const char *src, size_t n)
{
size_t i;

View File

@ -1,11 +1,16 @@
#include "utils.h"
<<<<<<< HEAD
int ft_strreplace(char **str, char *fill, size_t start, size_t stop)
=======
char *ft_strreplace(char *str, const char *fill, size_t start, size_t stop)
>>>>>>> 67fb6d0533f56a0d63ffeebd9fde7cdc75919eb2
{
char *out;
size_t sum;
out = malloc((ft_strlen(str) + ft_strlen(fill) - (stop - start) + 1 * sizeof(char)));
out = malloc((ft_strlen(str) + ft_strlen(fill) - (stop - start) + 1
* sizeof(char)));
if (out == NULL)
return (1);
ft_strncpy(out, str, start);

View File

@ -3,10 +3,10 @@
# include <stdlib.h>
# include "../libftx/libftx.h"
size_t ft_strncpy(char *dst, char *src, size_t n);
int ft_is_in_quote(char *str, size_t n);
char *ft_strreplace(char *str, char *fill, size_t start, size_t stop);
ssize_t ft_strnchr(char *str, char c);
char *ft_getstr(char *str, size_t n);
size_t ft_strncpy(char *dst, const char *src, size_t n);
int ft_is_in_quote(const char *str, size_t n);
char *ft_strreplace(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);
#endif