start of excution implementation

This commit is contained in:
starnakin 2023-02-09 18:47:05 +01:00
parent 4258c1da62
commit 1a04f73b49
48 changed files with 312 additions and 51 deletions

BIN
a.out Executable file

Binary file not shown.

24
d
View File

@ -0,0 +1,24 @@
a.out
argprinter
d
execution.c
file.c
file.o
ft_split_quoted.c
ft_split_quoted.o
heredoc.c
heredoc.o
infile.c
infile.o
libftx
main.c
main.o
Makefile
minishell
minishell.h
outfile.c
outfile.o
syntatics.c
syntatics.o
t
utils

39
execution.c Normal file
View File

@ -0,0 +1,39 @@
#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"))
return (ft_strjoin("/usr/bin/bo", const char *s2))
}
int ft_excutor(t_cmd *cmd, )
{
char cmd[13] = "/usr/bin/ls";
char *args[3] = {NULL, "cat", NULL};
int pid;
int fd_in;
int fd_out;
if (ac != 3)
return (1);
fd_out = open(av[2], O_WRONLY | O_CREAT | O_TRUNC);
fd_in = open(av[1], O_RDONLY);
pid = fork();
if (pid == -1)
return (1);
if (pid == 0)
{
dup2(fd_out, 1);
dup2(fd_in, 0);
execve(cmd, args, env);
}
else
waitpid(pid);
}

6
file.c
View File

@ -1,6 +1,6 @@
#include "minishell.h" #include "minishell.h"
int ft_file_is_readable(char *path) int ft_file_is_readable(const char *path)
{ {
int readable; int readable;
int fd; int fd;
@ -21,7 +21,7 @@ int ft_file_is_readable(char *path)
return (1); return (1);
} }
int ft_file_is_writeable(char *path) int ft_file_is_writeable(const char *path)
{ {
int writeable; int writeable;
int fd; int fd;
@ -42,7 +42,7 @@ int ft_file_is_writeable(char *path)
return (1); return (1);
} }
char *ft_get_file_path(char *infile) char *ft_get_file_path(const char *infile)
{ {
size_t i; size_t i;
size_t n; size_t n;

BIN
file.o

Binary file not shown.

Binary file not shown.

BIN
heredoc.o

Binary file not shown.

BIN
infile.o

Binary file not shown.

View File

@ -12,7 +12,7 @@
CC = clang 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) OBJS = $(SRCS:.c=.o)

Binary file not shown.

View File

@ -28,9 +28,13 @@ char *ft_strfjoin(char *s1, char *s2);
char *ft_strmerger(size_t arg_len, ...); char *ft_strmerger(size_t arg_len, ...);
int ft_is_in(char *str, char c); int ft_is_in(char *str, char c);
char **ft_tabrealloc(char **tab, size_t current_size, size_t new_size); 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); ssize_t ft_strchri(char *str, char c);
int ft_contain_only_str(char *str, char *to_find); int ft_contain_only_str(char *str, char *to_find);
int ft_contain_only(char *str, char c); int ft_contain_only(char *str, char c);
int ft_strcmp(char *s1, char *s2); 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 #endif

View File

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

Binary file not shown.

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

@ -0,0 +1,28 @@
#include "utils.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;
}

BIN
libftx/extra/ft_swap.o Normal file

Binary file not shown.

View File

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

Binary file not shown.

Binary file not shown.

BIN
libftx/libft/ft_lstclear.o Normal file

Binary file not shown.

BIN
libftx/libft/ft_lstdelone.o Normal file

Binary file not shown.

BIN
libftx/libft/ft_lstiter.o Normal file

Binary file not shown.

BIN
libftx/libft/ft_lstlast.o Normal file

Binary file not shown.

BIN
libftx/libft/ft_lstmap.o Normal file

Binary file not shown.

BIN
libftx/libft/ft_lstnew.o Normal file

Binary file not shown.

BIN
libftx/libft/ft_lstsize.o Normal file

Binary file not shown.

View File

@ -68,7 +68,7 @@ static char **ft_segsplitter(char **tab, size_t len, const char *s, char c)
let_index++; let_index++;
tab[tab_index] = ft_substr(s, start, let_index - start); tab[tab_index] = ft_substr(s, start, let_index - start);
if (tab[tab_index] == NULL) if (tab[tab_index] == NULL)
return (ft_cancel(tab, tab_index)); return (ft_cancel((void **)tab, tab_index));
tab_index++; tab_index++;
} }
return (tab); return (tab);

Binary file not shown.

Binary file not shown.

View File

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

Binary file not shown.

View File

@ -32,7 +32,10 @@ char *ft_strfjoin(char *s1, char *s2);
char *ft_strmerger(size_t arg_len, ...); char *ft_strmerger(size_t arg_len, ...);
int ft_strcmp(char *s1, char *s2); int ft_strcmp(char *s1, char *s2);
ssize_t ft_strchri(char *str, char c); 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(void **tab, size_t len); void *ft_cancel(void **tab, size_t len);
int ft_atoi(const char *nptr); int ft_atoi(const char *nptr);

165
main.c
View File

@ -1,6 +1,5 @@
#include "libftx/libftx.h" #include "libftx/libftx.h"
#include "minishell.h" #include "minishell.h"
#include <stdlib.h>
void ft_lstdel(void *ptr) void ft_lstdel(void *ptr)
{ {
@ -12,7 +11,7 @@ void ft_lstdel(void *ptr)
if (content->executable != NULL) if (content->executable != NULL)
free(content->executable); free(content->executable);
if (content->args != NULL) if (content->args != NULL)
free(content->args); ft_freer_tab_ultimate(1, content->args);
free(content); free(content);
free(element); free(element);
} }
@ -23,29 +22,38 @@ int ft_cmds_init(t_list **cmds, size_t len)
t_list *current; t_list *current;
size_t i; size_t i;
current = malloc(sizeof(t_list)); *cmds = malloc(sizeof(t_list));
if (current == NULL) current = *cmds;
return (1);
i = 0; i = 0;
while (i < len) while (i < len)
{ {
content = malloc(sizeof(t_cmd *)); content = malloc(sizeof(t_cmd));
if (content == NULL) if (content == NULL)
{
ft_lstclear(cmds, ft_lstdel); ft_lstclear(cmds, ft_lstdel);
return (1);
}
content->args = NULL; content->args = NULL;
content->executable = NULL; content->executable = NULL;
content->fd_in = -1; content->fd_in = -1;
content->fd_out = -1; content->fd_out = -1;
current->next = malloc(sizeof(t_list *)); current->content = content;
if (!((i + 1) < len))
{
current->next = NULL;
return (0);
}
current->next = malloc(sizeof(t_list));
if (current->next == NULL) if (current->next == NULL)
ft_lstclear(cmds, ft_lstdel); ft_lstclear(cmds, ft_lstdel);
current = current->next; current = current->next;
i++;
} }
return (0);
} }
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 i;
size_t len; size_t len;
t_cmd *cmd; t_cmd *cmd;
t_list *current; t_list *current;
@ -53,9 +61,11 @@ int ft_cmds_prep(t_list **cmds, const char *line, int infile, int outfile)
len = ft_seglen_quoted(line, '|'); len = ft_seglen_quoted(line, '|');
if (len == 0) if (len == 0)
return (0); return (0);
cmds = malloc(sizeof(t_list *));
if (ft_cmds_init(cmds, ft_seglen_quoted(line, '|'))) if (ft_cmds_init(cmds, ft_seglen_quoted(line, '|')))
{
free(cmds);
return (1); return (1);
}
cmd = (t_cmd *)(*cmds)->content; cmd = (t_cmd *)(*cmds)->content;
cmd->fd_in = infile; cmd->fd_in = infile;
current = *cmds; current = *cmds;
@ -66,33 +76,152 @@ int ft_cmds_prep(t_list **cmds, const char *line, int infile, int outfile)
return (0); return (0);
} }
int ft_cmds_fill(t_list **cmds, const char *line) void ft_strshift(char *str, int shift)
{ {
//TODO size_t i;
//remplir les executables;
// les args; if (shift > 0)
return ;
i = 0;
while (str[i - shift] != '\0')
{
str[i] = str[i - shift];
i++;
}
str[i + shift] = '\0';
} }
t_list **ft_parse_cmd(char *line) 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;
(void) cmds;
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(1, args);
return (1);
}
current = current->next;
i++;
}
return (0);
}
t_list **ft_parse_cmds(char *line)
{ {
int infile; int infile;
int outfile; int outfile;
t_list **cmds; t_list **cmds;
cmds = NULL; cmds = malloc(sizeof(t_list *));
outfile = ft_outfile(line); outfile = ft_outfile(line);
infile = ft_infile(line); infile = ft_infile(line);
if (ft_syntatic_verif(line) == 1) if (ft_syntatic_verif(line) == 1)
return (NULL); return (NULL);
ft_cmds_prep(cmds, line, infile, outfile); if (ft_cmds_prep(cmds, line, infile, outfile) == 1)
ft_printf("%s\n", line);
return (NULL); 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 main(int ac, char **av)
{ {
t_list **cmds;
if (ac == 1) if (ac == 1)
return (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)
return (1);
ft_lstclear(cmds);
return (1); return (1);
} }

BIN
main.o

Binary file not shown.

BIN
minishell

Binary file not shown.

View File

@ -24,7 +24,7 @@ typedef struct s_cmd
int fd_in; int fd_in;
int fd_out; int fd_out;
char *executable; char *executable;
char *args; char **args;
} t_cmd; } t_cmd;
#endif #endif

BIN
outfile.o

Binary file not shown.

View File

@ -1,7 +1,7 @@
#include "libftx/libftx.h" #include "libftx/libftx.h"
#include "minishell.h" #include "minishell.h"
static int ft_quote_verif(char *str) static int ft_quote_verif(const char *str)
{ {
if (ft_is_in_quote(str, ft_strlen(str))) if (ft_is_in_quote(str, ft_strlen(str)))
{ {
@ -12,7 +12,7 @@ static int ft_quote_verif(char *str)
return (0); return (0);
} }
static int ft_multipipe(char *str) static int ft_multipipe(const char *str)
{ {
size_t i; size_t i;
size_t y; size_t y;
@ -21,21 +21,36 @@ static int ft_multipipe(char *str)
while (str[i] != '\0') while (str[i] != '\0')
{ {
y = 0; y = 0;
while (str[i + y] == '|' && ft_is_in_quote(str, i)) while (str[i + y] == '|' && !ft_is_in_quote(str, i))
{ {
y++; if (y > 0)
if (y > 1)
{ {
ft_eprintf("minishell: Multiple pipes is not supported\n"); ft_eprintf("minishell: Multiple pipes is not supported\n");
return (1); return (1);
} }
y++;
} }
i++; i++;
} }
return (0); return (0);
} }
int ft_syntatic_verif(char *str) static int ft_pipe_is_alone(const char *str)
{ {
return (ft_quote_verif(str) || ft_multipipe(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));
} }

Binary file not shown.

24
t
View File

@ -0,0 +1,24 @@
a.out
argprinter
d
execution.c
file.c
file.o
ft_split_quoted.c
ft_split_quoted.o
heredoc.c
heredoc.o
infile.c
infile.o
libftx
main.c
main.o
Makefile
minishell
minishell.h
outfile.c
outfile.o
syntatics.c
syntatics.o
t
utils

View File

@ -1,6 +1,6 @@
#include "utils.h" #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 start;
size_t stop; size_t stop;

Binary file not shown.

View File

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

Binary file not shown.

View File

@ -1,6 +1,6 @@
#include "utils.h" #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; size_t i;

Binary file not shown.

View File

@ -1,11 +1,12 @@
#include "utils.h" #include "utils.h"
char *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)
{ {
char *out; char *out;
size_t sum; 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) if (out == NULL)
return (NULL); return (NULL);
ft_strncpy(out, str, start); ft_strncpy(out, str, start);

Binary file not shown.