Compare commits

..

13 Commits

28 changed files with 368 additions and 107 deletions

View File

@ -1,5 +1,5 @@
UTILS_SRC = utils/ft_is_in_quote.c utils/ft_strncpy.c utils/ft_strreplace.c utils/ft_strnchr.c utils/ft_split_quoted.c utils/ft_strshift.c utils/ft_quote_remover.c UTILS_SRC = utils/ft_is_in_quote.c utils/ft_strncpy.c utils/ft_strreplace.c utils/ft_strnchr.c utils/ft_split_quoted.c utils/ft_strshift.c utils/ft_quote_remover.c utils/ft_str_is_empty.c
SRCS = ${UTILS_SRC} main.c file.c infile.c outfile.c heredoc.c syntatics.c cmd.c cmds.c env.c execution.c spacer.c 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}
@ -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

16
cmd.c
View File

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

21
cmds.c
View File

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

34
env.c
View File

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/02 14:39:56 by erey-bet #+# #+# */ /* Created: 2023/02/02 14:39:56 by erey-bet #+# #+# */
/* Updated: 2023/02/16 15:18:48 by cchauvet ### ########.fr */ /* Updated: 2023/02/17 13:05:29 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -113,6 +113,7 @@ void env_del(void *ptr)
content = ptr; content = ptr;
free(content->key); free(content->key);
free(content->value); free(content->value);
free(content);
} }
char *get_key(char *str) char *get_key(char *str)
@ -204,7 +205,8 @@ int set_value_by_key(char *key, char *value, t_list **head)
int create_value_by_key(char *key, char *value, t_list **head) int create_value_by_key(char *key, char *value, t_list **head)
{ {
t_env *content; t_env *content;
if (set_value_by_key(key, value, head) == 0) if (set_value_by_key(key, value, head) == 0)
return (0); return (0);
content = ft_calloc(1, sizeof(t_env)); content = ft_calloc(1, sizeof(t_env));
@ -216,6 +218,25 @@ int create_value_by_key(char *key, char *value, t_list **head)
return (0); 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;
@ -240,3 +261,12 @@ t_list **init_env(char **env)
} }
return (head); return (head);
} }
/*int main(int argc, char *argv[], char **env)
{
t_list **new;
new = init_env(env);
ft_printf("%S", env_to_strs(new));
return (0);
}*/

76
env_fill.c Normal file
View 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);
}

View File

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

View File

@ -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("> ");
} }

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/15 17:52:10 by cchauvet #+# #+# */ /* Created: 2023/02/15 17:52:10 by cchauvet #+# #+# */
/* Updated: 2023/02/15 21:22:24 by cchauvet ### ########.fr */ /* Updated: 2023/02/17 13:19:09 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -33,6 +33,7 @@ static int ft_infile_is_valid(const char *line)
if (tab[i][0] == '<') if (tab[i][0] == '<')
{ {
ft_eprintf("minishell: %s: must be followed by an infile\n", tab[i]); ft_eprintf("minishell: %s: must be followed by an infile\n", tab[i]);
ft_freer_tab_ultimate(1, tab);
return (0); return (0);
} }
ft_freer_tab_ultimate(1, tab); ft_freer_tab_ultimate(1, tab);
@ -55,6 +56,9 @@ static int ft_get_infile(const char *line)
i = 0; i = 0;
while (tab[i + 1] != NULL) while (tab[i + 1] != NULL)
{ {
if (tab[i][0] == '<')
if (fd != 0)
close(fd);
if (ft_strcmp("<", tab[i]) == 0) if (ft_strcmp("<", tab[i]) == 0)
fd = ft_file_is_readable(ft_quote_remover(tab[i + 1])); fd = ft_file_is_readable(ft_quote_remover(tab[i + 1]));
else if (ft_strcmp("<<", tab[i]) == 0) else if (ft_strcmp("<<", tab[i]) == 0)

View File

@ -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);
} }

View File

@ -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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -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);

View File

@ -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
{ {

View File

@ -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);

View File

@ -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;

View File

@ -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);

View File

@ -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);
} }

View File

@ -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

View File

@ -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);

View File

@ -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);
} }

View File

@ -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);

135
main.c
View File

@ -6,44 +6,145 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/16 15:16:14 by cchauvet #+# #+# */ /* Created: 2023/02/16 15:16:14 by cchauvet #+# #+# */
/* Updated: 2023/02/16 15:16:45 by cchauvet ### ########.fr */ /* Updated: 2023/02/17 16:06:02 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "libftx/libftx.h"
#include "minishell.h" #include "minishell.h"
static char *ft_get_user_input(t_list **env)
{
char *line;
char *prompt;
prompt = ft_strmerger(2, get_value_by_key("PWD", env), "$ ");
if (prompt == NULL)
{
ft_eprintf("minishell: malloc failed\n");
return (NULL);
}
line = readline(prompt);
add_history(line);
free(prompt);
return (line);
}
static int ft_minishell(t_list **env, char *line)
{
t_list **cmds;
char *line_clean;
int infile;
int outfile;
line_clean = ft_normalizer(line);
if (line_clean == NULL)
return (1);
if (ft_syntatic_verif(line_clean))
{
free(line_clean);
return (1);
}
outfile = ft_outfile(line_clean);
if (outfile == -2)
{
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);
}
#if DEBUG
int main(int ac, char **av, char **env) int main(int ac, char **av, char **env)
{ {
t_list **cmds;
char *line;
t_data data; t_data data;
if (ac == 1) if (ac == 1)
return (1); return (1);
line = ft_normalizer(av[1]);
if (line == NULL)
return (1);
if (ft_syntatic_verif(line) == 1)
{
free(line);
return (1);
}
data.env = init_env(env); data.env = init_env(env);
if (data.env == NULL) if (data.env == NULL)
return (1); return (1);
cmds = ft_parse_cmds(line); free(data.env);
if (cmds == NULL)
return (1); return (1);
if (ft_cmds_executor(cmds, env) == 1) }
if (ft_minishell(data.env, av[1]) == 1)
{ {
ft_lstclear(data.env, env_del); ft_lstclear(data.env, env_del);
ft_lstclear(cmds, ft_cmddel); free(data.env);
return (1); return (1);
} }
ft_lstclear(data.env, env_del); ft_lstclear(data.env, env_del);
ft_lstclear(cmds, ft_cmddel);
free(cmds);
free(data.env); free(data.env);
free(line); free(line);
return (0); 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

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/14 13:45:30 by cchauvet #+# #+# */ /* Created: 2023/02/14 13:45:30 by cchauvet #+# #+# */
/* Updated: 2023/02/16 15:17:07 by cchauvet ### ########.fr */ /* Updated: 2023/02/17 14:29:56 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,11 +21,10 @@
# 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);
int set_value_key(t_list **env, char *key, char *value); int set_value_by_key(char *key, char *value, t_list **env);
char *get_value_index(int index, t_list **head); char *get_value_by_key(char *key, t_list **head);
char *get_value_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_writable(const char *path); int ft_file_is_writable(const char *path);
@ -35,13 +34,15 @@ 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, char **env); 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 ft_cmddel(void *content);
void env_del(void *content); void env_del(void *content);
t_list **ft_parse_cmds(char *line); t_list **ft_parse_cmds(char *line, t_list **env, int infile, int outfile);
int ft_cmd_filler(t_list *current, char **args); int ft_cmd_filler(t_list *current, char **args, t_list **env);
char *ft_normalizer(char *str); 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
{ {

View File

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

View File

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

View File

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

1
t Normal file
View File

@ -0,0 +1 @@
bozt

23
utils/ft_str_is_empty.c Normal file
View 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');
}

View File

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