add: env variable support, but invalid read for a non existant variable
This commit is contained in:
parent
e48ee8b693
commit
96da8e54c3
Binary file not shown.
2
Makefile
2
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_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}
|
||||
|
||||
|
16
cmd.c
16
cmd.c
@ -6,10 +6,11 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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"
|
||||
|
||||
void ft_cmddel(void *ptr)
|
||||
@ -24,9 +25,10 @@ void ft_cmddel(void *ptr)
|
||||
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;
|
||||
char *temp;
|
||||
size_t i;
|
||||
|
||||
if (args == NULL)
|
||||
@ -35,7 +37,15 @@ int ft_cmd_filler(t_list *element, char **args)
|
||||
i = 0;
|
||||
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++;
|
||||
}
|
||||
content->args = args;
|
||||
|
15
cmds.c
15
cmds.c
@ -6,13 +6,14 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/15 14:17:26 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/02/16 14:42:08 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/02/16 18:20:10 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libftx/libftx.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_list *current;
|
||||
@ -48,7 +49,7 @@ int ft_cmds_init(t_list **cmds, size_t len)
|
||||
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;
|
||||
t_cmd *cmd;
|
||||
@ -72,7 +73,7 @@ int ft_cmds_prep(t_list **cmds, const char *line, int infile, int outfile)
|
||||
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 **args;
|
||||
@ -87,7 +88,7 @@ int ft_cmds_fill(t_list **cmds, const char *line)
|
||||
while (tab[i] != NULL)
|
||||
{
|
||||
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_freer_tab_ultimate(2, args, tab);
|
||||
@ -100,7 +101,7 @@ int ft_cmds_fill(t_list **cmds, const char *line)
|
||||
return (0);
|
||||
}
|
||||
|
||||
t_list **ft_parse_cmds(char *line)
|
||||
t_list **ft_parse_cmds(char *line, t_list **env)
|
||||
{
|
||||
int infile;
|
||||
int outfile;
|
||||
@ -113,7 +114,7 @@ t_list **ft_parse_cmds(char *line)
|
||||
return (NULL);
|
||||
if (ft_cmds_prep(cmds, line, infile, outfile) == 1)
|
||||
return (NULL);
|
||||
if (ft_cmds_fill(cmds, line) == 1)
|
||||
if (ft_cmds_fill(cmds, env, line) == 1)
|
||||
return (NULL);
|
||||
return (cmds);
|
||||
}
|
||||
|
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/16 17:49:03 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);
|
||||
i = i + ft_strlen(value) - 1;
|
||||
free(out);
|
||||
if (temp == NULL)
|
||||
{
|
||||
ft_eprintf("minishell: malloc failed\n");
|
||||
return (NULL);
|
||||
}
|
||||
out = temp;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return (out);
|
||||
}
|
35
execution.c
35
execution.c
@ -3,21 +3,7 @@
|
||||
#include "utils/utils.h"
|
||||
#include <unistd.h>
|
||||
|
||||
static char *ft_get_variable(char **env, char *variable)
|
||||
{
|
||||
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)
|
||||
static char *ft_get_executable_path(char *executable_name, t_list **env)
|
||||
{
|
||||
char *path;
|
||||
char *temp;
|
||||
@ -33,10 +19,15 @@ static char *ft_get_executable_path(char *executable_name, char **env)
|
||||
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(ft_get_variable(env, "PATH"), ':');
|
||||
tab = ft_split(get_value_by_key("PATH", env), ':');
|
||||
if (tab == NULL)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
@ -66,10 +57,11 @@ static char *ft_get_executable_path(char *executable_name, char **env)
|
||||
return (path);
|
||||
}
|
||||
|
||||
static int ft_excutor(t_cmd *cmd, char **env)
|
||||
static int ft_excutor(t_cmd *cmd, t_list **env)
|
||||
{
|
||||
int pid;
|
||||
int return_value;
|
||||
char **tab;
|
||||
|
||||
if (cmd->fd_in == -1 || cmd->fd_out == -1)
|
||||
return (1);
|
||||
@ -78,16 +70,21 @@ static int ft_excutor(t_cmd *cmd, char **env)
|
||||
return (1);
|
||||
if (pid == 0)
|
||||
{
|
||||
//TODO DO THIS
|
||||
/* tab = t_env2tab(env); */
|
||||
/* if (tab == NULL) */
|
||||
/* return (1); */
|
||||
tab = NULL;
|
||||
dup2(cmd->fd_out, 1);
|
||||
dup2(cmd->fd_in, 0);
|
||||
execve(cmd->executable, cmd->args, env);
|
||||
execve(cmd->executable, cmd->args, tab);
|
||||
}
|
||||
else
|
||||
waitpid(pid, &return_value, 0);
|
||||
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_list *current;
|
||||
|
5
infile.c
5
infile.c
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/15 17:52:10 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/02/15 21:22:24 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/02/16 17:52:29 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -55,6 +55,9 @@ static int ft_get_infile(const char *line)
|
||||
i = 0;
|
||||
while (tab[i + 1] != NULL)
|
||||
{
|
||||
if (tab[i][0] == '<')
|
||||
if (fd != 0)
|
||||
close(fd);
|
||||
if (ft_strcmp("<", tab[i]) == 0)
|
||||
fd = ft_file_is_readable(ft_quote_remover(tab[i + 1]));
|
||||
else if (ft_strcmp("<<", tab[i]) == 0)
|
||||
|
6
main.c
6
main.c
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/16 15:16:14 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/02/16 15:16:45 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/02/16 18:19:49 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -31,10 +31,10 @@ int main(int ac, char **av, char **env)
|
||||
data.env = init_env(env);
|
||||
if (data.env == NULL)
|
||||
return (1);
|
||||
cmds = ft_parse_cmds(line);
|
||||
cmds = ft_parse_cmds(line, data.env);
|
||||
if (cmds == NULL)
|
||||
return (1);
|
||||
if (ft_cmds_executor(cmds, env) == 1)
|
||||
if (ft_cmds_executor(cmds, data.env) == 1)
|
||||
{
|
||||
ft_lstclear(data.env, env_del);
|
||||
ft_lstclear(cmds, ft_cmddel);
|
||||
|
14
minishell.h
14
minishell.h
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/14 13:45:30 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/02/16 16:12:26 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/02/16 18:19:34 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -23,9 +23,8 @@
|
||||
# include <readline/history.h>
|
||||
|
||||
t_list **init_env(char **env);
|
||||
int set_value_key(t_list **env, char *key, char *value);
|
||||
char *get_value_index(int index, t_list **head);
|
||||
char *get_value_key(char *key, t_list **head);
|
||||
int set_value_by_key(char *key, char *value, t_list **env);
|
||||
char *get_value_by_key(char *key, t_list **head);
|
||||
int ft_syntatic_verif(const char *str);
|
||||
int ft_file_is_readable(const char *path);
|
||||
int ft_file_is_writable(const char *path);
|
||||
@ -35,13 +34,14 @@ int ft_infile(char *line);
|
||||
int ft_outfile(char *line);
|
||||
int ft_heredoc(char *stop);
|
||||
size_t ft_seglen_quoted(const char *str, char c);
|
||||
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);
|
||||
void ft_cmddel(void *content);
|
||||
void env_del(void *content);
|
||||
t_list **ft_parse_cmds(char *line);
|
||||
int ft_cmd_filler(t_list *current, char **args);
|
||||
t_list **ft_parse_cmds(char *line, t_list **env);
|
||||
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);
|
||||
|
||||
typedef struct s_cmd
|
||||
{
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/15 18:01:07 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/02/15 21:22:51 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/02/16 17:53:28 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -50,10 +50,13 @@ static int ft_get_outfile(const char *line)
|
||||
ft_eprintf("minishell: malloc failed\n");
|
||||
return (-2);
|
||||
}
|
||||
fd = 0;
|
||||
fd = 1;
|
||||
i = 0;
|
||||
while (tab[i + 1] != NULL)
|
||||
{
|
||||
if (tab[i][0] == '>')
|
||||
if (fd != 1)
|
||||
close(fd);
|
||||
if (ft_strcmp(">", tab[i]) == 0)
|
||||
fd = ft_file_is_writable(ft_quote_remover(tab[i + 1]));
|
||||
else if (ft_strcmp(">>", tab[i]) == 0)
|
||||
|
Loading…
Reference in New Issue
Block a user