Compare commits
30 Commits
1423d42583
...
camille
Author | SHA1 | Date | |
---|---|---|---|
daaa9dea51 | |||
fa37938073 | |||
7bec120312 | |||
6090784c02 | |||
d4cead2b0f | |||
61cd1209ff | |||
6ecb66a60e | |||
642af26133 | |||
38e1b9834f | |||
e31fff5137 | |||
8b771d6615 | |||
96da8e54c3 | |||
e48ee8b693 | |||
f20038e37b | |||
edbd267c0d | |||
de8dfc41d4 | |||
82a0af80ad | |||
c44530728c | |||
1fb6e0a2c1 | |||
02f5815485 | |||
22b7a4feea | |||
8366b63821 | |||
0ba212410f | |||
56eead9241 | |||
99fdd578e9 | |||
c5467769d9 | |||
b20d4e0ddc | |||
afaf34f869 | |||
8283472d2e | |||
402b6e875e |
BIN
.execution.c.swp
BIN
.execution.c.swp
Binary file not shown.
9
Makefile
9
Makefile
@ -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_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 ft_split_quoted.c env.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
|
||||||
|
|
||||||
OBJS = ${SRCS:.c=.o}
|
OBJS = ${SRCS:.c=.o}
|
||||||
|
|
||||||
@ -7,7 +7,7 @@ NAME = minishell
|
|||||||
|
|
||||||
CC = gcc
|
CC = gcc
|
||||||
|
|
||||||
CFLAGS = -Wall -Werror -Wextra -g
|
CFLAGS = -Werror -Wextra -g
|
||||||
|
|
||||||
LIBS = libftx/libftx.a
|
LIBS = libftx/libftx.a
|
||||||
|
|
||||||
@ -28,7 +28,8 @@ fclean: clean
|
|||||||
make -C libftx fclean
|
make -C libftx fclean
|
||||||
rm -f ${NAME}
|
rm -f ${NAME}
|
||||||
|
|
||||||
re: fclean all
|
re: fclean
|
||||||
|
make all
|
||||||
|
|
||||||
.PHONY: all clean fclean re
|
.PHONY: all clean fclean re
|
||||||
|
|
||||||
|
BIN
argprinter
BIN
argprinter
Binary file not shown.
26
builtin/env.c
Normal file
26
builtin/env.c
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* env.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/02/14 14:56:02 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/02/14 14:58:40 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../minishell.h"
|
||||||
|
|
||||||
|
int main(char **env)
|
||||||
|
{
|
||||||
|
t_list **head;
|
||||||
|
t_list *current;
|
||||||
|
|
||||||
|
while (current != NULL)
|
||||||
|
{
|
||||||
|
ft_putendl_fd(1, current->content);
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
40
builtin/export.c
Normal file
40
builtin/export.c
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* export.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/02/14 14:27:08 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/02/14 14:52:50 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../minishell.h"
|
||||||
|
|
||||||
|
int main(char **env)
|
||||||
|
{
|
||||||
|
t_list **env_lst;
|
||||||
|
t_list *current;
|
||||||
|
char *key;
|
||||||
|
char *value;
|
||||||
|
|
||||||
|
env_lst = init_env(env);
|
||||||
|
current = *env_lst;
|
||||||
|
while (current != NULL)
|
||||||
|
{
|
||||||
|
value = ft_strchr(current->content, '=') + 1;
|
||||||
|
key = ft_strndup(current->content,
|
||||||
|
ft_strlen(current->content) - ft_strlen(value));
|
||||||
|
if (key == NULL)
|
||||||
|
{
|
||||||
|
ft_lstclear(env_lst, env_del);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
ft_printf("declare -x %s=\"%s\"\n", key, value);
|
||||||
|
free(key);
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
ft_lstclear(env_lst, env_del);
|
||||||
|
return (0);
|
||||||
|
}
|
54
cmd.c
Normal file
54
cmd.c
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* cmd.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/02/15 14:18:21 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/02/16 18:25:14 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libftx/libftx.h"
|
||||||
|
#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_list **env)
|
||||||
|
{
|
||||||
|
t_cmd *content;
|
||||||
|
char *temp;
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
if (args == NULL)
|
||||||
|
return (1);
|
||||||
|
content = (t_cmd *)element->content;
|
||||||
|
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);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
content->args = args;
|
||||||
|
content->executable = args[0];
|
||||||
|
return (0);
|
||||||
|
}
|
114
cmds.c
Normal file
114
cmds.c
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* cmds.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/02/15 14:17:26 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/02/17 15:20:29 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libftx/libftx.h"
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
static 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
static 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int ft_cmds_fill(t_list **cmds, t_list **env, 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, env) == 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, t_list **env, int infile, int outfile)
|
||||||
|
{
|
||||||
|
t_list **cmds;
|
||||||
|
|
||||||
|
cmds = malloc(sizeof(t_list *));
|
||||||
|
if (ft_cmds_prep(cmds, line, infile, outfile) == 1)
|
||||||
|
return (NULL);
|
||||||
|
if (ft_cmds_fill(cmds, env, line) == 1)
|
||||||
|
return (NULL);
|
||||||
|
return (cmds);
|
||||||
|
}
|
230
env.c
230
env.c
@ -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 07:32:43 by cchauvet ### ########.fr */
|
/* Updated: 2023/02/17 13:05:29 by erey-bet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -26,22 +26,16 @@ int get_index(char *s, char c)
|
|||||||
void print_export(t_list **head, int fd)
|
void print_export(t_list **head, int fd)
|
||||||
{
|
{
|
||||||
t_list *current;
|
t_list *current;
|
||||||
char *ctn;
|
|
||||||
int v;
|
|
||||||
|
|
||||||
current = *head;
|
current = *head;
|
||||||
while (current != NULL)
|
while (current->next != NULL)
|
||||||
{
|
{
|
||||||
ctn = current->content;
|
write(fd, "declare -x ", 11);
|
||||||
if (*(ft_strchr(ctn, '=') - 1) != '_')
|
ft_putstr_fd(((t_env*)(current->content))->key, fd);
|
||||||
{
|
ft_putstr_fd("=", fd);
|
||||||
v = get_index(ctn, '=');
|
write(fd, "\"", 1);
|
||||||
write(fd, "declare -x ", 11);
|
ft_putstr_fd(((t_env*)(current->content))->value, fd);
|
||||||
write(fd, ctn, v + 1);
|
write(fd, "\"\n", 2);
|
||||||
write(fd, "\"", 1);
|
|
||||||
ft_putstr_fd(ctn + v + 1, fd);
|
|
||||||
write(fd, "\"\n", 2);
|
|
||||||
}
|
|
||||||
current = current->next;
|
current = current->next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -51,9 +45,11 @@ void print_env(t_list **head, int fd)
|
|||||||
t_list *current;
|
t_list *current;
|
||||||
|
|
||||||
current = *head;
|
current = *head;
|
||||||
while (current != NULL)
|
while (current->next != NULL)
|
||||||
{
|
{
|
||||||
ft_putstr_fd(current->content, fd);
|
ft_putstr_fd(((t_env*)(current->content))->key, fd);
|
||||||
|
ft_putstr_fd("=", fd);
|
||||||
|
ft_putstr_fd(((t_env*)(current->content))->value, fd);
|
||||||
write(fd, "\n", 1);
|
write(fd, "\n", 1);
|
||||||
current = current->next;
|
current = current->next;
|
||||||
}
|
}
|
||||||
@ -77,16 +73,62 @@ int strcmp_alphabet(char *s1, char *s2)
|
|||||||
return (-1);
|
return (-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ft_double_swap(char **a, char **b)
|
void ft_double_swap(void *a, void *b)
|
||||||
{
|
{
|
||||||
void *c;
|
void *c;
|
||||||
|
|
||||||
c = *a;
|
c = a;
|
||||||
*a = *b;
|
a = b;
|
||||||
*b = c;
|
b = c;
|
||||||
}
|
}
|
||||||
|
|
||||||
void exchange(char **a, char **b, char **c)
|
void exchange(void *a, void *b, void *c)
|
||||||
|
{
|
||||||
|
void *d;
|
||||||
|
|
||||||
|
d = a;
|
||||||
|
a = b;
|
||||||
|
b = c;
|
||||||
|
c = d;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *get_value(char *str)
|
||||||
|
{
|
||||||
|
char *s;
|
||||||
|
int i;
|
||||||
|
int start;
|
||||||
|
|
||||||
|
s = ft_calloc(ft_strlen(str), sizeof(char));
|
||||||
|
start = get_index(str, '=');
|
||||||
|
i = start;
|
||||||
|
while (str[++i])
|
||||||
|
s[i - start - 1] = str[i];
|
||||||
|
return (s);
|
||||||
|
}
|
||||||
|
|
||||||
|
void env_del(void *ptr)
|
||||||
|
{
|
||||||
|
t_env *content;
|
||||||
|
|
||||||
|
content = ptr;
|
||||||
|
free(content->key);
|
||||||
|
free(content->value);
|
||||||
|
free(content);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *get_key(char *str)
|
||||||
|
{
|
||||||
|
char *s;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
s = ft_calloc(ft_strlen(str), sizeof(char));
|
||||||
|
i = -1;
|
||||||
|
while (str[++i] != '=')
|
||||||
|
s[i] = str[i];
|
||||||
|
return (s);
|
||||||
|
}
|
||||||
|
|
||||||
|
void swap_env_3(void **a, void **b, void **c)
|
||||||
{
|
{
|
||||||
void *d;
|
void *d;
|
||||||
|
|
||||||
@ -96,63 +138,37 @@ void exchange(char **a, char **b, char **c)
|
|||||||
*c = d;
|
*c = d;
|
||||||
}
|
}
|
||||||
|
|
||||||
void add_sort(t_list **head, char *str)
|
void swap_env(void **a, void **b)
|
||||||
|
{
|
||||||
|
void *c;
|
||||||
|
|
||||||
|
c = *a;
|
||||||
|
*a = *b;
|
||||||
|
*b = c;
|
||||||
|
}
|
||||||
|
|
||||||
|
void add_sort(t_list **head, t_env *var)
|
||||||
{
|
{
|
||||||
t_list *current;
|
t_list *current;
|
||||||
char *last;
|
t_env *last;
|
||||||
|
|
||||||
current = *head;
|
current = *head;
|
||||||
while (current->next != NULL && strcmp_alphabet(str, current->content) != 0)
|
while (current->next != NULL && strcmp_alphabet(var->key, ((t_env*)(current->content))->key) != 0)
|
||||||
current = current->next;
|
current = current->next;
|
||||||
if (strcmp_alphabet(str, current->content) == 1)
|
if (current->content == NULL)
|
||||||
last = str;
|
current->content = var;
|
||||||
else
|
else
|
||||||
exchange(&last, (char **)(¤t->content), &str);
|
|
||||||
while (current != NULL)
|
|
||||||
{
|
{
|
||||||
if (current->next == NULL)
|
last = NULL;
|
||||||
current->next = ft_calloc(1, sizeof(t_list));
|
swap_env_3((void **)&last, ¤t->content, (void **)&var);
|
||||||
if (current->next == NULL)
|
while (current->next != NULL)
|
||||||
return ;
|
|
||||||
current = current->next;
|
|
||||||
if (current->content == NULL)
|
|
||||||
{
|
{
|
||||||
current->content = last;
|
current = current->next;
|
||||||
return ;
|
swap_env(¤t->content, (void **)&last);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
ft_double_swap((char **)(¤t->content), &last);
|
|
||||||
}
|
}
|
||||||
}
|
if (current->next == NULL)
|
||||||
|
current->next = ft_calloc(1, sizeof(t_list));
|
||||||
char *get_value_by_index(int index, t_list **head)
|
|
||||||
{
|
|
||||||
t_list *current;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
current = *head;
|
|
||||||
i = -1;
|
|
||||||
while (current != NULL && ++i != index)
|
|
||||||
current = current->next;
|
|
||||||
if (i == index)
|
|
||||||
return (ft_strchr(current->content, '=') + 1);
|
|
||||||
return (NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
int is_start(char *big, char *little)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
while (big[i])
|
|
||||||
{
|
|
||||||
if (little[i] != big[i])
|
|
||||||
return (0);
|
|
||||||
i++;
|
|
||||||
if (!little[i])
|
|
||||||
return (1);
|
|
||||||
}
|
|
||||||
return (0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char *get_value_by_key(char *key, t_list **head)
|
char *get_value_by_key(char *key, t_list **head)
|
||||||
@ -160,35 +176,97 @@ char *get_value_by_key(char *key, t_list **head)
|
|||||||
t_list *current;
|
t_list *current;
|
||||||
|
|
||||||
current = *head;
|
current = *head;
|
||||||
while (current != NULL)
|
while (current->next != NULL)
|
||||||
{
|
{
|
||||||
if (is_start(current->content, key))
|
if (ft_strcmp(((t_env *)current->content)->key, key) == 0)
|
||||||
return (ft_strchr(current->content, '=') + 1);
|
return (((t_env *)current->content)->value);
|
||||||
current = current->next;
|
current = current->next;
|
||||||
}
|
}
|
||||||
return (NULL);
|
return (NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int set_value_by_key(char *key, char *value, t_list **head)
|
||||||
|
{
|
||||||
|
t_list *current;
|
||||||
|
|
||||||
|
current = *head;
|
||||||
|
while (current->next != NULL)
|
||||||
|
{
|
||||||
|
if (ft_strcmp(((t_env *)current->content)->key, key) == 0)
|
||||||
|
{
|
||||||
|
free(((t_env *)current->content)->value);
|
||||||
|
((t_env *)current->content)->value = value;
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int create_value_by_key(char *key, char *value, t_list **head)
|
||||||
|
{
|
||||||
|
t_env *content;
|
||||||
|
|
||||||
|
if (set_value_by_key(key, value, head) == 0)
|
||||||
|
return (0);
|
||||||
|
content = ft_calloc(1, sizeof(t_env));
|
||||||
|
if (content == NULL)
|
||||||
|
return (1);
|
||||||
|
content->key = key;
|
||||||
|
content->value = value;
|
||||||
|
add_sort(head, content);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
char **env_to_strs(t_list **head)
|
||||||
|
{
|
||||||
|
t_list *current;
|
||||||
|
t_env *content;
|
||||||
|
char **env;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
current = *head;
|
||||||
|
env = ft_calloc(ft_lstsize(*head), sizeof(char *));
|
||||||
|
i = 0;
|
||||||
|
while (current->content)
|
||||||
|
{
|
||||||
|
content = current->content;
|
||||||
|
env[i++] = ft_strmerger(3, content->key, "=", content->value);
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
return (env);
|
||||||
|
}
|
||||||
|
|
||||||
t_list **init_env(char **env)
|
t_list **init_env(char **env)
|
||||||
{
|
{
|
||||||
t_list **head;
|
t_list **head;
|
||||||
int i;
|
int i;
|
||||||
|
t_env *var;
|
||||||
|
|
||||||
head = ft_calloc(1, sizeof(t_list *));
|
head = ft_calloc(1, sizeof(t_list *));
|
||||||
|
if (head == NULL)
|
||||||
|
return (NULL);
|
||||||
*head = ft_calloc(1, sizeof(t_list));
|
*head = ft_calloc(1, sizeof(t_list));
|
||||||
if (*head == NULL)
|
if (*head == NULL)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
i = -1;
|
i = -1;
|
||||||
while (env[++i])
|
while (env[++i])
|
||||||
add_sort(head, env[i]);
|
{
|
||||||
|
var = ft_calloc(1, sizeof(t_env));
|
||||||
|
if (var == NULL)
|
||||||
|
return (NULL);
|
||||||
|
var->key = get_key(env[i]);
|
||||||
|
var->value = get_value(env[i]);
|
||||||
|
add_sort(head, var);
|
||||||
|
}
|
||||||
return (head);
|
return (head);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*int main(int argc, char *argv[], char **env)
|
/*int main(int argc, char *argv[], char **env)
|
||||||
{
|
{
|
||||||
(void)argc;
|
t_list **new;
|
||||||
(void)argv;
|
|
||||||
ft_putstr_fd(get_value_index(10, init_env(env)), 1);
|
new = init_env(env);
|
||||||
//print_env(init_env(env), 1);
|
ft_printf("%S", env_to_strs(new));
|
||||||
return (0);
|
return (0);
|
||||||
}*/
|
}*/
|
||||||
|
76
env_fill.c
Normal file
76
env_fill.c
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* env_fill.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/02/16 16:29:08 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/02/17 13:41:51 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);
|
||||||
|
free(out);
|
||||||
|
if (temp == NULL)
|
||||||
|
{
|
||||||
|
ft_eprintf("minishell: malloc failed\n");
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
out = temp;
|
||||||
|
}
|
||||||
|
if (out[i] != '\0')
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (out);
|
||||||
|
}
|
108
execution.c
108
execution.c
@ -1,34 +1,116 @@
|
|||||||
#include "libftx/libftx.h"
|
#include "libftx/libftx.h"
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
#include "utils/utils.h"
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
int main(int ac, char **av, char **env)
|
static char *ft_get_executable_path(char *executable_name, t_list **env)
|
||||||
{
|
{
|
||||||
|
char *path;
|
||||||
|
char *temp;
|
||||||
|
char **tab;
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
path = NULL;
|
||||||
|
if (executable_name[0] == '.' || executable_name[0] == '/')
|
||||||
|
{
|
||||||
|
path = ft_strdup(executable_name);
|
||||||
|
if (path == NULL)
|
||||||
|
{
|
||||||
|
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), ':');
|
||||||
|
if (tab == NULL)
|
||||||
|
return (NULL);
|
||||||
|
i = 0;
|
||||||
|
while (tab[i] != NULL)
|
||||||
|
{
|
||||||
|
temp = ft_strmerger(3, tab[i], "/", executable_name);
|
||||||
|
if (temp == NULL)
|
||||||
|
{
|
||||||
|
ft_freer_tab_ultimate(1, tab);
|
||||||
|
free(executable_name);
|
||||||
|
ft_eprintf("minishell: malloc failed\n");
|
||||||
|
}
|
||||||
|
if (access(temp, X_OK) == 0)
|
||||||
|
{
|
||||||
|
path = temp;
|
||||||
|
break ;
|
||||||
|
}
|
||||||
|
free(temp);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
if (path == NULL)
|
||||||
|
{
|
||||||
|
ft_eprintf("%s: command not found\n", executable_name);
|
||||||
|
}
|
||||||
|
ft_freer_tab_ultimate(1, tab);
|
||||||
|
}
|
||||||
|
return (path);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* char *ft_get_executable_path(t_data *data, char *executable) */
|
static int ft_excutor(t_cmd *cmd, t_list **env)
|
||||||
/* { */
|
|
||||||
/* if (ft_strcmp(executable, "env") == 0) */
|
|
||||||
/* return (ft_strjoin("", executable)); */
|
|
||||||
/* else */
|
|
||||||
/* return */
|
|
||||||
/* } */
|
|
||||||
|
|
||||||
int ft_excutor(t_cmd *cmd)
|
|
||||||
{
|
{
|
||||||
int pid;
|
int pid;
|
||||||
|
int return_value;
|
||||||
|
char **tab;
|
||||||
|
|
||||||
|
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);
|
||||||
if (pid == 0)
|
if (pid == 0)
|
||||||
{
|
{
|
||||||
|
tab = env_to_strs(env);
|
||||||
|
if (tab == NULL)
|
||||||
|
return (1);
|
||||||
|
tab = NULL;
|
||||||
dup2(cmd->fd_out, 1);
|
dup2(cmd->fd_out, 1);
|
||||||
dup2(cmd->fd_in, 0);
|
dup2(cmd->fd_in, 0);
|
||||||
//TODO ADD ENV VARIABLES
|
execve(cmd->executable, cmd->args, tab);
|
||||||
execve(cmd->executable, cmd->args, NULL);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
waitpid(pid);
|
waitpid(pid, &return_value, 0);
|
||||||
|
return (return_value);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_cmds_executor(t_list **cmds, t_list **env)
|
||||||
|
{
|
||||||
|
t_cmd *content;
|
||||||
|
t_list *current;
|
||||||
|
int fds[2];
|
||||||
|
|
||||||
|
current = *cmds;
|
||||||
|
while (current != NULL)
|
||||||
|
{
|
||||||
|
content = current->content;
|
||||||
|
if (current->next != NULL)
|
||||||
|
{
|
||||||
|
if (pipe(fds) == -1)
|
||||||
|
{
|
||||||
|
ft_eprintf("minishell: pipe failed\n");
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
content->fd_out = fds[1];
|
||||||
|
((t_cmd *) current->next->content)->fd_in = fds[0];
|
||||||
|
}
|
||||||
|
content->executable = ft_get_executable_path(content->executable, env);
|
||||||
|
if (content->executable != NULL)
|
||||||
|
ft_excutor(content, env);
|
||||||
|
if (content->fd_in != 0)
|
||||||
|
close(content->fd_in);
|
||||||
|
if (content->fd_out != 1 && content->fd_out != 2)
|
||||||
|
close(content->fd_out);
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
}
|
}
|
||||||
|
67
file.c
67
file.c
@ -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);
|
||||||
}
|
}
|
||||||
|
17
heredoc.c
17
heredoc.c
@ -1,5 +1,17 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* heredoc.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/02/17 15:36:26 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/02/17 16:21:02 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libftx/libftx.h"
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
#include <readline/history.h>
|
|
||||||
|
|
||||||
int ft_heredoc(char *stop)
|
int ft_heredoc(char *stop)
|
||||||
{
|
{
|
||||||
@ -15,8 +27,7 @@ int ft_heredoc(char *stop)
|
|||||||
free(line);
|
free(line);
|
||||||
break ;
|
break ;
|
||||||
}
|
}
|
||||||
ft_putstr_fd(line, fds[1]);
|
ft_putendl_fd(line, fds[1]);
|
||||||
add_history(line);
|
|
||||||
free(line);
|
free(line);
|
||||||
line = readline("> ");
|
line = readline("> ");
|
||||||
}
|
}
|
||||||
|
134
infile.c
134
infile.c
@ -1,76 +1,101 @@
|
|||||||
#include "minishell.h"
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* infile.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/02/15 17:52:10 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/02/17 13:19:09 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);
|
||||||
|
}
|
||||||
|
if (tab[0] == NULL)
|
||||||
|
return (1);
|
||||||
|
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]);
|
||||||
|
ft_freer_tab_ultimate(1, tab);
|
||||||
|
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 (tab[i][0] == '<')
|
||||||
{
|
|
||||||
i++;
|
|
||||||
if (fd != 0)
|
if (fd != 0)
|
||||||
close(fd);
|
close(fd);
|
||||||
if (line[i] == '<')
|
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)
|
||||||
path = ft_get_file_path(line + i);
|
fd = ft_heredoc(tab[i + 1]);
|
||||||
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 +103,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);
|
||||||
}
|
}
|
||||||
|
@ -6,13 +6,13 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/09/29 22:23:35 by cchauvet #+# #+# */
|
/* Created: 2022/09/29 22:23:35 by cchauvet #+# #+# */
|
||||||
/* Updated: 2023/01/05 18:54:54 by cchauvet ### ########.fr */
|
/* Updated: 2023/02/17 16:23:01 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
void ft_putchar_fd(int fd, char c)
|
void ft_putchar_fd(char c, int fd)
|
||||||
{
|
{
|
||||||
write(fd, &c, 1);
|
write(fd, &c, 1);
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/09/29 22:26:36 by cchauvet #+# #+# */
|
/* Created: 2022/09/29 22:26:36 by cchauvet #+# #+# */
|
||||||
/* Updated: 2022/09/29 22:43:19 by cchauvet ### ########.fr */
|
/* Updated: 2023/02/17 16:15:07 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/09/26 14:47:54 by cchauvet #+# #+# */
|
/* Created: 2022/09/26 14:47:54 by cchauvet #+# #+# */
|
||||||
/* Updated: 2023/01/06 19:34:13 by cchauvet ### ########.fr */
|
/* Updated: 2023/02/17 16:22:44 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -47,7 +47,7 @@ char **ft_split(char const *s, char c);
|
|||||||
char *ft_itoa(int n);
|
char *ft_itoa(int n);
|
||||||
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
|
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
|
||||||
void ft_striteri(char *s, void (*f)(unsigned int, char*));
|
void ft_striteri(char *s, void (*f)(unsigned int, char*));
|
||||||
void ft_putchar_fd(int fd, char c);
|
void ft_putchar_fd(char c, int fd);
|
||||||
void ft_putstr_fd(char *s, int fd);
|
void ft_putstr_fd(char *s, int fd);
|
||||||
void ft_putendl_fd(char *s, int fd);
|
void ft_putendl_fd(char *s, int fd);
|
||||||
void ft_putnbr_fd(int n, int fd);
|
void ft_putnbr_fd(int n, int fd);
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/09/26 14:47:54 by cchauvet #+# #+# */
|
/* Created: 2022/09/26 14:47:54 by cchauvet #+# #+# */
|
||||||
/* Updated: 2023/02/03 12:42:13 by cchauvet ### ########.fr */
|
/* Updated: 2023/02/17 16:24:42 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -69,10 +69,10 @@ char **ft_split(char const *s, char c);
|
|||||||
char *ft_itoa(int n);
|
char *ft_itoa(int n);
|
||||||
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
|
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
|
||||||
void ft_striteri(char *s, void (*f)(unsigned int, char*));
|
void ft_striteri(char *s, void (*f)(unsigned int, char*));
|
||||||
void ft_putchar_fd(int fd, char c);
|
void ft_putchar_fd(char c, int fd);
|
||||||
void ft_putstr_fd(char *s, int fd);
|
void ft_putstr_fd(char *s, int fd);
|
||||||
void ft_putendl_fd(int fd, char *s);
|
void ft_putendl_fd(char *s, int fd);
|
||||||
void ft_putnbr_fd(int fd, int n);
|
void ft_putnbr_fd(int n, int fd);
|
||||||
|
|
||||||
typedef struct s_list
|
typedef struct s_list
|
||||||
{
|
{
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/10/23 18:08:31 by cchauvet #+# #+# */
|
/* Created: 2022/10/23 18:08:31 by cchauvet #+# #+# */
|
||||||
/* Updated: 2023/02/03 12:52:38 by cchauvet ### ########.fr */
|
/* Updated: 2023/02/17 16:29:56 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -23,13 +23,13 @@ int ft_dprintarg(int fd, int arg, va_list args)
|
|||||||
if (arg == 'u')
|
if (arg == 'u')
|
||||||
return (ft_dprintul(fd, va_arg(args, unsigned int)));
|
return (ft_dprintul(fd, va_arg(args, unsigned int)));
|
||||||
if (arg == 'c')
|
if (arg == 'c')
|
||||||
return (ft_putchar_fd(fd, va_arg(args, int)));
|
return (ft_putchar_fd_p(fd, va_arg(args, int)));
|
||||||
if (arg == 'S')
|
if (arg == 'S')
|
||||||
return (ft_dprintstrtab(fd, va_arg(args, char **)));
|
return (ft_dprintstrtab(fd, va_arg(args, char **)));
|
||||||
if (arg == 's')
|
if (arg == 's')
|
||||||
return (ft_putstr_fd_p(fd, va_arg(args, char *)));
|
return (ft_putstr_fd_p(fd, va_arg(args, char *)));
|
||||||
if (arg == '%')
|
if (arg == '%')
|
||||||
return (ft_putchar_fd(fd, '%'));
|
return (ft_putchar_fd_p(fd, '%'));
|
||||||
if (arg == 'p')
|
if (arg == 'p')
|
||||||
return (ft_dprintptr(fd, va_arg(args, void *)));
|
return (ft_dprintptr(fd, va_arg(args, void *)));
|
||||||
return (0);
|
return (0);
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/10/10 03:26:21 by cchauvet #+# #+# */
|
/* Created: 2022/10/10 03:26:21 by cchauvet #+# #+# */
|
||||||
/* Updated: 2022/10/21 15:54:31 by cchauvet ### ########.fr */
|
/* Updated: 2023/02/17 16:30:25 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -23,7 +23,7 @@ int ft_dprintl_base(int fd, long long int n, char *base)
|
|||||||
if (n < 0)
|
if (n < 0)
|
||||||
{
|
{
|
||||||
nb = -n;
|
nb = -n;
|
||||||
i += ft_putchar_fd(fd, '-');
|
i += ft_putchar_fd_p(fd, '-');
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
nb = n;
|
nb = n;
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/01/05 17:26:55 by cchauvet #+# #+# */
|
/* Created: 2023/01/05 17:26:55 by cchauvet #+# #+# */
|
||||||
/* Updated: 2023/02/03 12:54:44 by cchauvet ### ########.fr */
|
/* Updated: 2023/02/17 16:30:44 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -22,7 +22,7 @@ int ft_dprintstrtab(int fd, char **tab)
|
|||||||
while (tab[index] != NULL)
|
while (tab[index] != NULL)
|
||||||
{
|
{
|
||||||
i += ft_putstr_fd_p(fd, tab[index]) + 1;
|
i += ft_putstr_fd_p(fd, tab[index]) + 1;
|
||||||
ft_putchar_fd(fd, '\n');
|
ft_putchar_fd_p(fd, '\n');
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
return (i);
|
return (i);
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/09/29 13:49:45 by cchauvet #+# #+# */
|
/* Created: 2022/09/29 13:49:45 by cchauvet #+# #+# */
|
||||||
/* Updated: 2022/10/23 14:23:44 by cchauvet ### ########.fr */
|
/* Updated: 2023/02/17 16:31:15 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -73,9 +73,9 @@ int ft_dprintul_base(int fd, unsigned long long n, char *base)
|
|||||||
if (n > base_size - 1)
|
if (n > base_size - 1)
|
||||||
{
|
{
|
||||||
ft_dprintul_base(fd, n / base_size, base);
|
ft_dprintul_base(fd, n / base_size, base);
|
||||||
ft_putchar_fd(fd, base[n % base_size]);
|
ft_putchar_fd_p(fd, base[n % base_size]);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
ft_putchar_fd(fd, base[n]);
|
ft_putchar_fd_p(fd, base[n]);
|
||||||
return (str_size - 1);
|
return (str_size - 1);
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/09/26 14:47:54 by cchauvet #+# #+# */
|
/* Created: 2022/09/26 14:47:54 by cchauvet #+# #+# */
|
||||||
/* Updated: 2023/02/03 12:51:07 by cchauvet ### ########.fr */
|
/* Updated: 2023/02/17 16:29:18 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -36,7 +36,7 @@ int ft_eprintf(const char *format, ...);
|
|||||||
|
|
||||||
int ft_vdprintf(int fd, const char *format, va_list va);
|
int ft_vdprintf(int fd, const char *format, va_list va);
|
||||||
|
|
||||||
int ft_putchar_fd(int fd, char c);
|
int ft_putchar_fd_p(int fd, char c);
|
||||||
int ft_putstr_fd_p(int fd, char *str);
|
int ft_putstr_fd_p(int fd, char *str);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -6,13 +6,13 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/09/29 22:23:35 by cchauvet #+# #+# */
|
/* Created: 2022/09/29 22:23:35 by cchauvet #+# #+# */
|
||||||
/* Updated: 2022/10/12 15:21:42 by cchauvet ### ########.fr */
|
/* Updated: 2023/02/17 16:28:59 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "ft_printf.h"
|
#include "ft_printf.h"
|
||||||
|
|
||||||
int ft_putchar_fd(int fd, char c)
|
int ft_putchar_fd_p(int fd, char c)
|
||||||
{
|
{
|
||||||
write(fd, &c, 1);
|
write(fd, &c, 1);
|
||||||
return (1);
|
return (1);
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/09/29 22:25:08 by cchauvet #+# #+# */
|
/* Created: 2022/09/29 22:25:08 by cchauvet #+# #+# */
|
||||||
/* Updated: 2023/02/03 12:49:18 by cchauvet ### ########.fr */
|
/* Updated: 2023/02/17 16:28:25 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -20,6 +20,6 @@ int ft_putstr_fd_p(int fd, char *str)
|
|||||||
str = "(null)";
|
str = "(null)";
|
||||||
i = 0;
|
i = 0;
|
||||||
while (str[i] != '\0')
|
while (str[i] != '\0')
|
||||||
ft_putchar_fd(fd, str[i++]);
|
ft_putchar_fd_p(fd, str[i++]);
|
||||||
return (i);
|
return (i);
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/10/12 14:34:28 by cchauvet #+# #+# */
|
/* Created: 2022/10/12 14:34:28 by cchauvet #+# #+# */
|
||||||
/* Updated: 2022/12/11 19:23:34 by cchauvet ### ########.fr */
|
/* Updated: 2023/02/17 16:31:29 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -19,7 +19,7 @@ static int ft_dprintseg(int fd, const char *str)
|
|||||||
i = 0;
|
i = 0;
|
||||||
while (str[i] != '\0' && ft_skipflag(str + i) == 0)
|
while (str[i] != '\0' && ft_skipflag(str + i) == 0)
|
||||||
{
|
{
|
||||||
ft_putchar_fd(fd, str[i]);
|
ft_putchar_fd_p(fd, str[i]);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
return (i);
|
return (i);
|
||||||
|
330
main.c
330
main.c
@ -1,228 +1,150 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* main.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/02/16 15:16:14 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/02/17 16:06:02 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "libftx/libftx.h"
|
#include "libftx/libftx.h"
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
void ft_lstdel(void *ptr)
|
static char *ft_get_user_input(t_list **env)
|
||||||
{
|
{
|
||||||
t_cmd *content;
|
char *line;
|
||||||
|
char *prompt;
|
||||||
|
|
||||||
content = (t_cmd *) ptr;
|
prompt = ft_strmerger(2, get_value_by_key("PWD", env), "$ ");
|
||||||
if (content->executable != NULL)
|
if (prompt == NULL)
|
||||||
free(content->executable);
|
{
|
||||||
if (content->args != NULL)
|
ft_eprintf("minishell: malloc failed\n");
|
||||||
ft_freer_tab_ultimate(1, content->args);
|
return (NULL);
|
||||||
free(content);
|
}
|
||||||
|
line = readline(prompt);
|
||||||
|
add_history(line);
|
||||||
|
free(prompt);
|
||||||
|
return (line);
|
||||||
}
|
}
|
||||||
|
|
||||||
int ft_cmds_init(t_list **cmds, size_t len)
|
static int ft_minishell(t_list **env, char *line)
|
||||||
{
|
|
||||||
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_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)
|
|
||||||
{
|
{
|
||||||
|
t_list **cmds;
|
||||||
|
char *line_clean;
|
||||||
int infile;
|
int infile;
|
||||||
int outfile;
|
int outfile;
|
||||||
t_list **cmds;
|
|
||||||
|
|
||||||
cmds = malloc(sizeof(t_list *));
|
line_clean = ft_normalizer(line);
|
||||||
outfile = ft_outfile(line);
|
if (line_clean == NULL)
|
||||||
infile = ft_infile(line);
|
return (1);
|
||||||
if (ft_syntatic_verif(line) == 1)
|
if (ft_syntatic_verif(line_clean))
|
||||||
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;
|
free(line_clean);
|
||||||
ft_printf("--- COMMAND %d\n", i);
|
return (1);
|
||||||
ft_printf("excutable: %s\n", content->executable);
|
}
|
||||||
ft_printf("args:\n%S", content->args);
|
outfile = ft_outfile(line_clean);
|
||||||
current = current->next;
|
if (outfile == -2)
|
||||||
i++;
|
{
|
||||||
|
free(line_clean);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
infile = ft_infile(line_clean);
|
||||||
|
if (infile == -2)
|
||||||
|
{
|
||||||
|
close(outfile);
|
||||||
|
free(line_clean);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
cmds = ft_parse_cmds(line_clean, env, infile, outfile);
|
||||||
|
if (cmds == NULL)
|
||||||
|
{
|
||||||
|
close(outfile);
|
||||||
|
close(infile);
|
||||||
|
ft_lstclear(cmds, ft_cmddel);
|
||||||
|
free(cmds);
|
||||||
|
free(line_clean);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
if (ft_cmds_executor(cmds, env) == 1)
|
||||||
|
{
|
||||||
|
close(outfile);
|
||||||
|
close(infile);
|
||||||
|
ft_lstclear(cmds, ft_cmddel);
|
||||||
|
free(cmds);
|
||||||
|
free(line_clean);
|
||||||
|
return (1);
|
||||||
}
|
}
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int ac, char **av)
|
#if DEBUG
|
||||||
|
|
||||||
|
int main(int ac, char **av, char **env)
|
||||||
{
|
{
|
||||||
t_list **cmds;
|
t_data data;
|
||||||
|
|
||||||
if (ac == 1)
|
if (ac == 1)
|
||||||
return (1);
|
return (1);
|
||||||
cmds = ft_parse_cmds(av[1]);
|
data.env = init_env(env);
|
||||||
if (cmds == NULL)
|
if (data.env == NULL)
|
||||||
return (1);
|
return (1);
|
||||||
if (ft_cmds_excutor(cmds) == 1)
|
free(data.env);
|
||||||
{
|
|
||||||
ft_lstclear(cmds, ft_lstdel);
|
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
ft_lstclear(cmds, ft_lstdel);
|
if (ft_minishell(data.env, av[1]) == 1)
|
||||||
free(cmds);
|
{
|
||||||
return (1);
|
ft_lstclear(data.env, env_del);
|
||||||
|
free(data.env);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
ft_lstclear(data.env, env_del);
|
||||||
|
free(data.env);
|
||||||
|
free(line);
|
||||||
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int main(int ac, char **av, char **env)
|
||||||
|
{
|
||||||
|
t_data data;
|
||||||
|
char *line;
|
||||||
|
|
||||||
|
data.env = init_env(env);
|
||||||
|
if (data.env == NULL)
|
||||||
|
return (1);
|
||||||
|
line = ft_get_user_input(data.env);
|
||||||
|
if (line == NULL)
|
||||||
|
{
|
||||||
|
ft_lstclear(data.env, env_del);
|
||||||
|
free(data.env);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
while (line != NULL)
|
||||||
|
{
|
||||||
|
if (ft_minishell(data.env, line) == 1)
|
||||||
|
{
|
||||||
|
ft_lstclear(data.env, env_del);
|
||||||
|
free(data.env);
|
||||||
|
free(line);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
free(line);
|
||||||
|
line = ft_get_user_input(data.env);
|
||||||
|
if (line == NULL)
|
||||||
|
{
|
||||||
|
ft_lstclear(data.env, env_del);
|
||||||
|
free(data.env);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ft_lstclear(data.env, env_del);
|
||||||
|
free(data.env);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
37
minishell.h
37
minishell.h
@ -1,3 +1,15 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* minishell.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/02/14 13:45:30 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/02/17 14:29:56 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#ifndef MINISHELL_H
|
#ifndef MINISHELL_H
|
||||||
# define MINISHELL_H
|
# define MINISHELL_H
|
||||||
# include "libftx/libftx.h"
|
# include "libftx/libftx.h"
|
||||||
@ -9,20 +21,28 @@
|
|||||||
# include <stdio.h>
|
# include <stdio.h>
|
||||||
# include <readline/readline.h>
|
# include <readline/readline.h>
|
||||||
# include <readline/history.h>
|
# include <readline/history.h>
|
||||||
|
# define DEBUG 0
|
||||||
t_list **init_env(char **env);
|
t_list **init_env(char **env);
|
||||||
char *get_value_index(int index, t_list **head);
|
int set_value_by_key(char *key, char *value, t_list **env);
|
||||||
char *get_value_key(char *key, t_list **head);
|
char *get_value_by_key(char *key, t_list **head);
|
||||||
|
|
||||||
int ft_syntatic_verif(const char *str);
|
int ft_syntatic_verif(const char *str);
|
||||||
int ft_file_is_readable(const char *path);
|
int ft_file_is_readable(const char *path);
|
||||||
int ft_file_is_writeable(const char *path);
|
int ft_file_is_writable(const char *path);
|
||||||
|
int ft_file_is_appendable(const char *path);
|
||||||
char *ft_get_file_path(const char *infile);
|
char *ft_get_file_path(const char *infile);
|
||||||
int ft_infile(char *line);
|
int ft_infile(char *line);
|
||||||
int ft_outfile(char *line);
|
int ft_outfile(char *line);
|
||||||
int ft_heredoc(char *stop);
|
int ft_heredoc(char *stop);
|
||||||
size_t ft_seglen_quoted(const char *str, char c);
|
size_t ft_seglen_quoted(const char *str, char c);
|
||||||
|
int ft_cmds_executor(t_list **cmds, t_list **env);
|
||||||
char **ft_split_quoted(const char *s, char c);
|
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 infile, int outfile);
|
||||||
|
int ft_cmd_filler(t_list *current, char **args, t_list **env);
|
||||||
|
char *ft_normalizer(char *str);
|
||||||
|
char *ft_env_filler(t_list **env, const char *str);
|
||||||
|
char **env_to_strs(t_list **head);
|
||||||
|
|
||||||
typedef struct s_cmd
|
typedef struct s_cmd
|
||||||
{
|
{
|
||||||
@ -35,6 +55,13 @@ typedef struct s_cmd
|
|||||||
typedef struct s_data
|
typedef struct s_data
|
||||||
{
|
{
|
||||||
t_list **env;
|
t_list **env;
|
||||||
|
int heredoc;
|
||||||
} t_data;
|
} t_data;
|
||||||
|
|
||||||
|
typedef struct s_env
|
||||||
|
{
|
||||||
|
void *key;
|
||||||
|
void *value;
|
||||||
|
} t_env;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
143
outfile.c
143
outfile.c
@ -1,76 +1,100 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* outfile.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/02/15 18:01:07 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/02/17 13:19:46 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libftx/libftx.h"
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
static int ft_get_outfile(char *line)
|
static int ft_outfile_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);
|
||||||
|
}
|
||||||
|
if (tab[0] == NULL)
|
||||||
|
return (1);
|
||||||
|
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]);
|
||||||
|
ft_freer_tab_ultimate(1, tab);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
ft_freer_tab_ultimate(1, tab);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int ft_get_outfile(const char *line)
|
||||||
{
|
{
|
||||||
size_t i;
|
size_t i;
|
||||||
int append;
|
int fd;
|
||||||
char *path;
|
char **tab;
|
||||||
|
|
||||||
path = NULL;
|
tab = ft_split_quoted(line, ' ');
|
||||||
append = 0;
|
if (tab == NULL)
|
||||||
i = 0;
|
|
||||||
while (line[i] != '\0')
|
|
||||||
{
|
{
|
||||||
if (line[i] == '>' && ft_is_in_quote(line, i) == 0)
|
ft_eprintf("minishell: malloc failed\n");
|
||||||
{
|
return (-2);
|
||||||
i++;
|
}
|
||||||
if (line[i] == '>')
|
fd = 1;
|
||||||
{
|
i = 0;
|
||||||
i++;
|
while (tab[i + 1] != NULL)
|
||||||
append = 1;
|
{
|
||||||
}
|
if (tab[i][0] == '>')
|
||||||
else
|
if (fd != 1)
|
||||||
append = 0;
|
close(fd);
|
||||||
if (path != NULL)
|
if (ft_strcmp(">", tab[i]) == 0)
|
||||||
free(path);
|
fd = ft_file_is_writable(ft_quote_remover(tab[i + 1]));
|
||||||
path = ft_get_file_path(line + i);
|
else if (ft_strcmp(">>", tab[i]) == 0)
|
||||||
if (path == NULL || ft_file_is_writeable(path) == 0)
|
fd = ft_file_is_appendable(ft_quote_remover(tab[i + 1]));
|
||||||
{
|
|
||||||
free(path);
|
|
||||||
return (-1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
if (path == NULL)
|
ft_freer_tab_ultimate(1, tab);
|
||||||
return (-2);
|
return (fd);
|
||||||
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)
|
static int ft_remove_outfile(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 +102,11 @@ int ft_outfile(char *line)
|
|||||||
{
|
{
|
||||||
int fd;
|
int fd;
|
||||||
|
|
||||||
|
if (ft_outfile_is_valid(line) == 0)
|
||||||
|
return (-2);
|
||||||
fd = ft_get_outfile(line);
|
fd = ft_get_outfile(line);
|
||||||
if (ft_remove_outfile(line))
|
if (fd == -2)
|
||||||
{
|
return (-2);
|
||||||
if (fd > 0)
|
ft_remove_outfile(line);
|
||||||
close(fd);
|
|
||||||
return (-1);
|
|
||||||
}
|
|
||||||
return (fd);
|
return (fd);
|
||||||
}
|
}
|
||||||
|
117
spacer.c
Normal file
117
spacer.c
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* spacer.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/02/15 13:35:50 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/02/16 16:26:15 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libftx/libftx.h"
|
||||||
|
#include "minishell.h"
|
||||||
|
#include "utils/utils.h"
|
||||||
|
|
||||||
|
static char *ft_spacer_after(const char *str)
|
||||||
|
{
|
||||||
|
char *out;
|
||||||
|
char *temp;
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
out = ft_strdup(str);
|
||||||
|
if (out == NULL)
|
||||||
|
return (NULL);
|
||||||
|
i = 1;
|
||||||
|
while (out[i] != '\0')
|
||||||
|
{
|
||||||
|
while (ft_is_in_quote(out, i))
|
||||||
|
i++;
|
||||||
|
if (ft_is_in("><|", out[i - 1]))
|
||||||
|
{
|
||||||
|
while (str[i] == out[i - 1])
|
||||||
|
i++;
|
||||||
|
temp = ft_strreplace(out, " ", i, i);
|
||||||
|
free(out);
|
||||||
|
out = temp;
|
||||||
|
if (out == NULL)
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (out);
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *ft_spacer_before(const char *str)
|
||||||
|
{
|
||||||
|
char *out;
|
||||||
|
char *temp;
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
out = ft_strdup(str);
|
||||||
|
if (out == NULL)
|
||||||
|
return (NULL);
|
||||||
|
i = 0;
|
||||||
|
while (out[i + 1] != '\0')
|
||||||
|
{
|
||||||
|
while (ft_is_in_quote(out, i))
|
||||||
|
i++;
|
||||||
|
if (ft_is_in("><|", out[i + 1]))
|
||||||
|
{
|
||||||
|
while (out[i] == ' ')
|
||||||
|
i++;
|
||||||
|
while (out[i] == out[i + 1])
|
||||||
|
i++;
|
||||||
|
temp = ft_strreplace(out, " ", i + 1, i + 1);
|
||||||
|
free(out);
|
||||||
|
out = temp;
|
||||||
|
if (out == NULL)
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (out);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ft_space_simplifier(char *str)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
size_t y;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (str[i] != '\0')
|
||||||
|
{
|
||||||
|
if (ft_is_in_quote(str, i))
|
||||||
|
i++;
|
||||||
|
y = 0;
|
||||||
|
while (str[y + i] == ' ')
|
||||||
|
y++;
|
||||||
|
if (y > 1)
|
||||||
|
{
|
||||||
|
ft_strshift(str + i, -y + 1);
|
||||||
|
y--;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
out = ft_spacer_before(temp);
|
||||||
|
free(temp);
|
||||||
|
if (out == NULL)
|
||||||
|
return (NULL);
|
||||||
|
ft_space_simplifier(out);
|
||||||
|
if (out[ft_strlen(out) - 1] == ' ')
|
||||||
|
out[ft_strlen(out) - 1] = '\0';
|
||||||
|
return (out);
|
||||||
|
}
|
78
syntatics.c
78
syntatics.c
@ -1,5 +1,6 @@
|
|||||||
#include "libftx/libftx.h"
|
#include "libftx/libftx.h"
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
#include "utils/utils.h"
|
||||||
|
|
||||||
static int ft_quote_verif(const char *str)
|
static int ft_quote_verif(const char *str)
|
||||||
{
|
{
|
||||||
@ -12,7 +13,42 @@ static int ft_quote_verif(const char *str)
|
|||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int ft_multipipe(const char *str)
|
static int ft_pipe_is_alone(const char *str)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
int check;
|
||||||
|
|
||||||
|
check = 0;
|
||||||
|
i = 0;
|
||||||
|
while (str[i] != '\0')
|
||||||
|
{
|
||||||
|
while (str[i] == ' ')
|
||||||
|
i++;
|
||||||
|
if (str[i] == '\0')
|
||||||
|
break ;
|
||||||
|
if (str[i] != '|')
|
||||||
|
check = 1;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (check == 0)
|
||||||
|
{
|
||||||
|
ft_eprintf("minishell: Pipe must be followed and ");
|
||||||
|
ft_eprintf("preced by a command or redirection\n");
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
check = 0;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
if (check == 0)
|
||||||
|
{
|
||||||
|
ft_eprintf("minishell: Pipe must be followed and ");
|
||||||
|
ft_eprintf("preced by a command or redirection\n");
|
||||||
|
}
|
||||||
|
return (check == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int ft_special_char_dub(const char *str)
|
||||||
{
|
{
|
||||||
size_t i;
|
size_t i;
|
||||||
size_t y;
|
size_t y;
|
||||||
@ -20,37 +56,43 @@ static int ft_multipipe(const char *str)
|
|||||||
i = 0;
|
i = 0;
|
||||||
while (str[i] != '\0')
|
while (str[i] != '\0')
|
||||||
{
|
{
|
||||||
y = 0;
|
while (ft_is_in_quote(str, i))
|
||||||
while (str[i + y] == '|' && !ft_is_in_quote(str, i))
|
i++;
|
||||||
|
if (ft_is_in("|<>", str[i]))
|
||||||
{
|
{
|
||||||
if (y > 0)
|
y = 0;
|
||||||
|
while (str[i] == str[i + y])
|
||||||
|
y++;
|
||||||
|
if ((y > 2 && (str[i] == '>' || str[i] == '<'))
|
||||||
|
|| (y > 1 && str[i] == '|'))
|
||||||
{
|
{
|
||||||
ft_eprintf("minishell: Multiple pipes is not supported\n");
|
ft_eprintf("minishell: to many %c in a row", str, str[i]);
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
y++;
|
i = i + y;
|
||||||
}
|
}
|
||||||
i++;
|
else
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int ft_pipe_is_alone(const char *str)
|
int ft_empty_verif(const char *str)
|
||||||
{
|
{
|
||||||
size_t i;
|
size_t i;
|
||||||
|
|
||||||
i = ft_strlen(str) - 1;
|
i = 0;
|
||||||
while (str[i] != '|' && i > 0)
|
while (str[i] == ' ')
|
||||||
{
|
i++;
|
||||||
if (str[i] != ' ')
|
if (str[i] == '\0')
|
||||||
return (0);
|
ft_eprintf("minishell: %s: command not found \n", str);
|
||||||
i--;
|
return (str[i] == '\0');
|
||||||
}
|
|
||||||
ft_eprintf("minishell: Pipe must be followed by a command or redirection\n");
|
|
||||||
return (1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int ft_syntatic_verif(const char *str)
|
int ft_syntatic_verif(const char *str)
|
||||||
{
|
{
|
||||||
return (ft_quote_verif(str) || ft_multipipe(str) || ft_pipe_is_alone(str));
|
return (ft_quote_verif(str)
|
||||||
|
|| ft_empty_verif(str)
|
||||||
|
|| ft_pipe_is_alone(str)
|
||||||
|
|| ft_special_char_dub(str));
|
||||||
}
|
}
|
||||||
|
@ -1,27 +0,0 @@
|
|||||||
#include "utils.h"
|
|
||||||
|
|
||||||
char *ft_getstr(const char *str, size_t n)
|
|
||||||
{
|
|
||||||
size_t start;
|
|
||||||
size_t stop;
|
|
||||||
char c;
|
|
||||||
int quote;
|
|
||||||
|
|
||||||
start = n;
|
|
||||||
stop = n;
|
|
||||||
quote = ft_is_in_quote(str, n);
|
|
||||||
if (quote == 0)
|
|
||||||
c = ' ';
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (quote == 1)
|
|
||||||
c = '\'';
|
|
||||||
else
|
|
||||||
c = '"';
|
|
||||||
}
|
|
||||||
while (str[start - 1] != c && start > 0)
|
|
||||||
start--;
|
|
||||||
while (str[stop] != c && str[stop] != '\0')
|
|
||||||
stop++;
|
|
||||||
return (ft_strndup(str + start, stop - start));
|
|
||||||
}
|
|
BIN
utils/ft_getstr.o
Normal file
BIN
utils/ft_getstr.o
Normal file
Binary file not shown.
25
utils/ft_printn.c
Normal file
25
utils/ft_printn.c
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_printn.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/02/14 14:43:00 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/02/14 14:47:27 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "./utils.h"
|
||||||
|
|
||||||
|
void ft_printn(const char *str, size_t n)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (i < n && str[i] != '\0')
|
||||||
|
{
|
||||||
|
ft_putchar_fd(1, str[i]);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
BIN
utils/ft_printn.o
Normal file
BIN
utils/ft_printn.o
Normal file
Binary file not shown.
43
utils/ft_quote_remover.c
Normal file
43
utils/ft_quote_remover.c
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_quote_remover.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/02/15 14:12:00 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/02/16 13:20:50 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
char *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);
|
||||||
|
ft_strshift(str + stop - 1, -1);
|
||||||
|
}
|
||||||
|
return (str);
|
||||||
|
}
|
@ -6,11 +6,11 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/10/05 19:04:34 by cchauvet #+# #+# */
|
/* Created: 2022/10/05 19:04:34 by cchauvet #+# #+# */
|
||||||
/* Updated: 2023/01/06 19:33:51 by cchauvet ### ########.fr */
|
/* Updated: 2023/02/15 14:25:56 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "utils.h"
|
||||||
|
|
||||||
size_t ft_seglen_quoted(const char *str, char c)
|
size_t ft_seglen_quoted(const char *str, char c)
|
||||||
{
|
{
|
||||||
@ -34,22 +34,22 @@ size_t ft_seglen_quoted(const char *str, char c)
|
|||||||
static char **ft_segsplitter(char **tab, size_t len, const char *s, char c)
|
static char **ft_segsplitter(char **tab, size_t len, const char *s, char c)
|
||||||
{
|
{
|
||||||
size_t tab_index;
|
size_t tab_index;
|
||||||
size_t let_index;
|
size_t let_i;
|
||||||
size_t start;
|
size_t start;
|
||||||
|
|
||||||
tab_index = 0;
|
tab_index = 0;
|
||||||
let_index = 0;
|
let_i = 0;
|
||||||
start = 0;
|
start = 0;
|
||||||
if (tab == NULL || s == NULL)
|
if (tab == NULL || s == NULL)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
while (tab_index < len)
|
while (tab_index < len)
|
||||||
{
|
{
|
||||||
while ((s[let_index] == c || ft_is_in_quote(s, let_index)) && s[let_index] != 0)
|
while ((s[let_i] == c || ft_is_in_quote(s, let_i)) && s[let_i] != 0)
|
||||||
let_index++;
|
let_i++;
|
||||||
start = let_index;
|
start = let_i;
|
||||||
while ((s[let_index] != c || ft_is_in_quote(s, let_index)) && s[let_index] != 0)
|
while ((s[let_i] != c || ft_is_in_quote(s, let_i)) && s[let_i] != 0)
|
||||||
let_index++;
|
let_i++;
|
||||||
tab[tab_index] = ft_substr(s, start, let_index - start);
|
tab[tab_index] = ft_substr(s, start, let_i - start);
|
||||||
if (tab[tab_index] == NULL)
|
if (tab[tab_index] == NULL)
|
||||||
return (ft_cancel((void *)tab, tab_index));
|
return (ft_cancel((void *)tab, tab_index));
|
||||||
tab_index++;
|
tab_index++;
|
23
utils/ft_str_is_empty.c
Normal file
23
utils/ft_str_is_empty.c
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* 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');
|
||||||
|
}
|
@ -1,12 +1,11 @@
|
|||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
char *ft_strreplace(char *str, const char *fill, size_t start, size_t stop)
|
char *ft_strreplace(const 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
|
out = malloc((ft_strlen(str) + ft_strlen(fill) - (stop - start) + 1 * sizeof(char)));
|
||||||
* sizeof(char)));
|
|
||||||
if (out == NULL)
|
if (out == NULL)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
ft_strncpy(out, str, start);
|
ft_strncpy(out, str, start);
|
||||||
|
30
utils/ft_strshift.c
Normal file
30
utils/ft_strshift.c
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_strshift.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/02/15 14:11:04 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/02/15 18:16:50 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
void ft_strshift(char *str, int shift)
|
||||||
|
{
|
||||||
|
ssize_t i;
|
||||||
|
|
||||||
|
if (shift > 0)
|
||||||
|
return ;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
i = 0;
|
||||||
|
while (str[i - shift - 1] != '\0')
|
||||||
|
{
|
||||||
|
str[i] = str[i - shift];
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,12 +1,29 @@
|
|||||||
#ifndef FT_UTILS
|
/* ************************************************************************** */
|
||||||
# define FT_UTILS
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* utils.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/02/14 14:46:40 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/02/16 16:13:47 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef UTILS_H
|
||||||
|
# define UTILS_H
|
||||||
# include <stdlib.h>
|
# include <stdlib.h>
|
||||||
# include "../libftx/libftx.h"
|
# include "../libftx/libftx.h"
|
||||||
|
|
||||||
size_t ft_strncpy(char *dst, const char *src, 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);
|
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);
|
char *ft_strreplace(const char *str, const char *fill,
|
||||||
|
size_t start, size_t stop);
|
||||||
ssize_t ft_strnchr(const char *str, char c);
|
ssize_t ft_strnchr(const char *str, char c);
|
||||||
char *ft_getstr(const char *str, size_t n);
|
char *ft_getstr(const char *str, size_t n);
|
||||||
|
int ft_str_is_empty(const char *str);
|
||||||
|
char **ft_split_quoted(const char *s, char c);
|
||||||
|
void ft_strshift(char *str, int shift);
|
||||||
|
char *ft_quote_remover(char *str);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Reference in New Issue
Block a user