Compare commits
No commits in common. "f20038e37bba79ed7f5c82a72ea8218e35893226" and "1fb6e0a2c1b06ee557cd960374324f2d431e7fe8" have entirely different histories.
f20038e37b
...
1fb6e0a2c1
4
Makefile
4
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
|
||||
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
|
||||
UTILS_SRC = utils/ft_is_in_quote.c utils/ft_strncpy.c utils/ft_strreplace.c utils/ft_strnchr.c utils/ft_getstr.c
|
||||
SRCS = ${UTILS_SRC} main.c file.c infile.c outfile.c heredoc.c syntatics.c ft_split_quoted.c env.c
|
||||
|
||||
OBJS = ${SRCS:.c=.o}
|
||||
|
||||
|
BIN
argprinter
Executable file
BIN
argprinter
Executable file
Binary file not shown.
@ -1,26 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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);
|
||||
}
|
@ -1,40 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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);
|
||||
}
|
44
cmd.c
44
cmd.c
@ -1,44 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* cmd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/15 14:18:21 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/02/15 14:18:38 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#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_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;
|
||||
content->executable = args[0];
|
||||
return (0);
|
||||
}
|
119
cmds.c
119
cmds.c
@ -1,119 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* cmds.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/15 14:17:26 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/02/16 14:42:08 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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_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)
|
||||
{
|
||||
int infile;
|
||||
int outfile;
|
||||
t_list **cmds;
|
||||
|
||||
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)
|
||||
return (NULL);
|
||||
if (ft_cmds_fill(cmds, line) == 1)
|
||||
return (NULL);
|
||||
return (cmds);
|
||||
}
|
13
env.c
13
env.c
@ -6,7 +6,7 @@
|
||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/02 14:39:56 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/02/16 15:18:48 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/02/16 14:30:12 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -26,6 +26,8 @@ int get_index(char *s, char c)
|
||||
void print_export(t_list **head, int fd)
|
||||
{
|
||||
t_list *current;
|
||||
char *ctn;
|
||||
int v;
|
||||
|
||||
current = *head;
|
||||
while (current->next != NULL)
|
||||
@ -106,15 +108,6 @@ char *get_value(char *str)
|
||||
return (s);
|
||||
}
|
||||
|
||||
void env_del(void *ptr)
|
||||
{
|
||||
t_env *content;
|
||||
|
||||
content = ptr;
|
||||
free(content->key);
|
||||
free(content->value);
|
||||
}
|
||||
|
||||
char *get_key(char *str)
|
||||
{
|
||||
char *s;
|
||||
|
108
execution.c
108
execution.c
@ -1,78 +1,24 @@
|
||||
#include "libftx/libftx.h"
|
||||
#include "minishell.h"
|
||||
#include "utils/utils.h"
|
||||
#include <unistd.h>
|
||||
|
||||
static char *ft_get_variable(char **env, char *variable)
|
||||
int main(int ac, char **av, char **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 *temp;
|
||||
char **tab;
|
||||
size_t i;
|
||||
/* char *ft_get_executable_path(t_data *data, char *executable) */
|
||||
/* { */
|
||||
/* if (ft_strcmp(executable, "env") == 0) */
|
||||
/* return (ft_strjoin("", executable)); */
|
||||
/* else */
|
||||
/* return */
|
||||
/* } */
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
tab = ft_split(ft_get_variable(env, "PATH"), ':');
|
||||
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);
|
||||
}
|
||||
|
||||
static int ft_excutor(t_cmd *cmd, char **env)
|
||||
int ft_excutor(t_cmd *cmd)
|
||||
{
|
||||
int pid;
|
||||
int return_value;
|
||||
|
||||
if (cmd->fd_in == -1 || cmd->fd_out == -1)
|
||||
return (1);
|
||||
pid = fork();
|
||||
if (pid == -1)
|
||||
return (1);
|
||||
@ -80,39 +26,9 @@ static int ft_excutor(t_cmd *cmd, char **env)
|
||||
{
|
||||
dup2(cmd->fd_out, 1);
|
||||
dup2(cmd->fd_in, 0);
|
||||
execve(cmd->executable, cmd->args, env);
|
||||
//TODO ADD ENV VARIABLES
|
||||
execve(cmd->executable, cmd->args, NULL);
|
||||
}
|
||||
else
|
||||
waitpid(pid, &return_value, 0);
|
||||
return (return_value);
|
||||
}
|
||||
|
||||
int ft_cmds_executor(t_list **cmds, char **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);
|
||||
close(content->fd_in);
|
||||
close(content->fd_out);
|
||||
current = current->next;
|
||||
}
|
||||
return (0);
|
||||
waitpid(pid);
|
||||
}
|
||||
|
67
file.c
67
file.c
@ -1,17 +1,4 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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 <unistd.h>
|
||||
|
||||
int ft_file_is_readable(const char *path)
|
||||
{
|
||||
@ -22,58 +9,58 @@ int ft_file_is_readable(const char *path)
|
||||
if (fd == -1)
|
||||
{
|
||||
ft_eprintf("minishell: %s: No such file or directory\n", path);
|
||||
return (-1);
|
||||
return (0);
|
||||
}
|
||||
readable = read(fd, "", 0);
|
||||
if (readable == -1)
|
||||
{
|
||||
ft_eprintf("minishell: %s: Permission denied\n", path);
|
||||
return (-1);
|
||||
return (0);
|
||||
}
|
||||
return (fd);
|
||||
close(fd);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int ft_file_is_writable(const char *path)
|
||||
int ft_file_is_writeable(const char *path)
|
||||
{
|
||||
int writeable;
|
||||
int fd;
|
||||
|
||||
fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
||||
fd = open(path, O_WRONLY | O_CREAT, 0644);
|
||||
if (fd == -1)
|
||||
{
|
||||
ft_eprintf("minishell: %s: Permission denied\n", path);
|
||||
return (-1);
|
||||
return (0);
|
||||
}
|
||||
writeable = write(fd, "", 0);
|
||||
if (writeable == -1)
|
||||
{
|
||||
ft_eprintf("minishell: %s: Permission denied\n", path);
|
||||
return (-1);
|
||||
return (0);
|
||||
}
|
||||
return (fd);
|
||||
close(fd);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int ft_file_is_appendable(const char *path)
|
||||
char *ft_get_file_path(const char *infile)
|
||||
{
|
||||
int writeable;
|
||||
int fd;
|
||||
size_t i;
|
||||
size_t n;
|
||||
|
||||
fd = open(path, O_WRONLY | O_CREAT | O_APPEND, 0644);
|
||||
if (fd == -1)
|
||||
n = 1;
|
||||
while (infile[-n] == infile[-1])
|
||||
n++;
|
||||
i = 0;
|
||||
while (infile[i] == ' ')
|
||||
i++;
|
||||
if (infile[i] == '\0' || infile[i] == '>' || infile[i] == '<')
|
||||
{
|
||||
ft_eprintf("minishell: %s: Permission denied\n", path);
|
||||
return (-1);
|
||||
ft_eprintf("minishell: syntax error near ");
|
||||
if ((infile[0] == '<' && n > 3) || (infile[0] == '>' && n > 2))
|
||||
ft_eprintf("unexpected token `%c`\n", infile[i - 1]);
|
||||
else
|
||||
ft_eprintf("unexpected token `newline`\n");
|
||||
return (NULL);
|
||||
}
|
||||
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);
|
||||
return (ft_getstr(infile, i + 1));
|
||||
}
|
||||
|
@ -6,11 +6,11 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/05 19:04:34 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/02/15 14:25:56 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/01/06 19:33:51 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "utils.h"
|
||||
#include "minishell.h"
|
||||
|
||||
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)
|
||||
{
|
||||
size_t tab_index;
|
||||
size_t let_i;
|
||||
size_t let_index;
|
||||
size_t start;
|
||||
|
||||
tab_index = 0;
|
||||
let_i = 0;
|
||||
let_index = 0;
|
||||
start = 0;
|
||||
if (tab == NULL || s == NULL)
|
||||
return (NULL);
|
||||
while (tab_index < len)
|
||||
{
|
||||
while ((s[let_i] == c || ft_is_in_quote(s, let_i)) && s[let_i] != 0)
|
||||
let_i++;
|
||||
start = let_i;
|
||||
while ((s[let_i] != c || ft_is_in_quote(s, let_i)) && s[let_i] != 0)
|
||||
let_i++;
|
||||
tab[tab_index] = ft_substr(s, start, let_i - start);
|
||||
while ((s[let_index] == c || ft_is_in_quote(s, let_index)) && s[let_index] != 0)
|
||||
let_index++;
|
||||
start = let_index;
|
||||
while ((s[let_index] != c || ft_is_in_quote(s, let_index)) && s[let_index] != 0)
|
||||
let_index++;
|
||||
tab[tab_index] = ft_substr(s, start, let_index - start);
|
||||
if (tab[tab_index] == NULL)
|
||||
return (ft_cancel((void *)tab, tab_index));
|
||||
tab_index++;
|
128
infile.c
128
infile.c
@ -1,97 +1,76 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* infile.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/15 17:52:10 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/02/15 21:22:24 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#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]);
|
||||
return (0);
|
||||
}
|
||||
ft_freer_tab_ultimate(1, tab);
|
||||
return (1);
|
||||
}
|
||||
|
||||
static int ft_get_infile(const char *line)
|
||||
static int ft_get_infile(char *line)
|
||||
{
|
||||
size_t i;
|
||||
int fd;
|
||||
char **tab;
|
||||
char *path;
|
||||
|
||||
tab = ft_split_quoted(line, ' ');
|
||||
if (tab == NULL)
|
||||
{
|
||||
ft_eprintf("minishell: malloc failed\n");
|
||||
return (-2);
|
||||
}
|
||||
fd = 0;
|
||||
i = 0;
|
||||
while (tab[i + 1] != NULL)
|
||||
while (line[i] != '\0')
|
||||
{
|
||||
if (ft_strcmp("<", tab[i]) == 0)
|
||||
fd = ft_file_is_readable(ft_quote_remover(tab[i + 1]));
|
||||
else if (ft_strcmp("<<", tab[i]) == 0)
|
||||
fd = ft_heredoc(tab[i + 1]);
|
||||
if (line[i] == '<' && ft_is_in_quote(line, i) == 0)
|
||||
{
|
||||
i++;
|
||||
if (fd != 0)
|
||||
close(fd);
|
||||
if (line[i] == '<')
|
||||
{
|
||||
i++;
|
||||
path = ft_get_file_path(line + i);
|
||||
if (path == NULL)
|
||||
return (-1);
|
||||
fd = ft_heredoc(path);
|
||||
}
|
||||
else
|
||||
{
|
||||
path = ft_get_file_path(line + i);
|
||||
if (path == NULL)
|
||||
return (-1);
|
||||
if (ft_file_is_readable(path) == 0)
|
||||
{
|
||||
free(path);
|
||||
return (-1);
|
||||
}
|
||||
fd = open(path, O_RDONLY);
|
||||
}
|
||||
free(path);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
ft_freer_tab_ultimate(1, tab);
|
||||
return (fd);
|
||||
}
|
||||
|
||||
static int ft_remove_infile(char *line)
|
||||
{
|
||||
size_t i;
|
||||
size_t y;
|
||||
char **tab;
|
||||
int separator;
|
||||
|
||||
tab = ft_split_quoted(line, ' ');
|
||||
if (tab == NULL)
|
||||
{
|
||||
ft_eprintf("minishell: malloc failed\n");
|
||||
return (1);
|
||||
}
|
||||
i = 0;
|
||||
y = 0;
|
||||
while (tab[i] != NULL)
|
||||
while (line[i] != '\0')
|
||||
{
|
||||
if (tab[i][0] == '<')
|
||||
if (line[i] == '<' && ft_is_in_quote(line, i) == 0)
|
||||
{
|
||||
ft_strshift(line + y, -1 * (ft_strlen(tab[i]) + 1));
|
||||
while (line[i] == '<')
|
||||
line[i++] = ' ';
|
||||
while (line[i] == ' ')
|
||||
i++;
|
||||
ft_strshift(line + y, -1 * (ft_strlen(tab[i]) + 1));
|
||||
}
|
||||
separator = ft_is_in_quote(line, i);
|
||||
if (separator == 0)
|
||||
separator = ' ';
|
||||
else if (separator == 1)
|
||||
separator = '\'';
|
||||
else
|
||||
y = y + ft_strlen(tab[i]);
|
||||
separator = '\"';
|
||||
while (line[i] != separator && line[i] != '\0')
|
||||
line[i++] = ' ';
|
||||
if (line[i] != '\0'
|
||||
&& (separator == '\'' || separator == '\"'))
|
||||
line[i++] = ' ';
|
||||
}
|
||||
i++;
|
||||
}
|
||||
ft_freer_tab_ultimate(1, tab);
|
||||
return (0);
|
||||
}
|
||||
|
||||
@ -99,11 +78,12 @@ int ft_infile(char *line)
|
||||
{
|
||||
int fd;
|
||||
|
||||
if (ft_infile_is_valid(line) == 0)
|
||||
return (-2);
|
||||
fd = ft_get_infile(line);
|
||||
if (fd == -2)
|
||||
return (-2);
|
||||
ft_remove_infile(line);
|
||||
if (ft_remove_infile(line))
|
||||
{
|
||||
if (fd > 0)
|
||||
close(fd);
|
||||
return (-1);
|
||||
}
|
||||
return (fd);
|
||||
}
|
||||
|
251
main.c
251
main.c
@ -1,49 +1,236 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/16 15:16:14 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/02/16 15:16:45 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libftx/libftx.h"
|
||||
#include "minishell.h"
|
||||
|
||||
int main(int ac, char **av, char **env)
|
||||
void ft_lstdel(void *ptr)
|
||||
{
|
||||
t_cmd *content;
|
||||
|
||||
content = (t_cmd *) ptr;
|
||||
if (content->executable != NULL)
|
||||
free(content->executable);
|
||||
if (content->args != NULL)
|
||||
ft_freer_tab_ultimate(1, content->args);
|
||||
free(content);
|
||||
}
|
||||
|
||||
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_lstdel);
|
||||
return (1);
|
||||
}
|
||||
content->args = NULL;
|
||||
content->executable = NULL;
|
||||
content->fd_in = -1;
|
||||
content->fd_out = -1;
|
||||
current->content = content;
|
||||
if (!((i + 1) < len))
|
||||
{
|
||||
current->next = NULL;
|
||||
return (0);
|
||||
}
|
||||
current->next = malloc(sizeof(t_list));
|
||||
if (current->next == NULL)
|
||||
ft_lstclear(cmds, ft_lstdel);
|
||||
current = current->next;
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int ft_cmds_prep(t_list **cmds, const char *line, int infile, int outfile)
|
||||
{
|
||||
size_t len;
|
||||
t_cmd *cmd;
|
||||
t_list *current;
|
||||
|
||||
len = ft_seglen_quoted(line, '|');
|
||||
if (len == 0)
|
||||
return (0);
|
||||
if (ft_cmds_init(cmds, ft_seglen_quoted(line, '|')))
|
||||
{
|
||||
free(cmds);
|
||||
return (1);
|
||||
}
|
||||
cmd = (t_cmd *)(*cmds)->content;
|
||||
cmd->fd_in = infile;
|
||||
current = *cmds;
|
||||
while (current->next != NULL)
|
||||
current = current->next;
|
||||
cmd = (t_cmd *) current->content;
|
||||
cmd->fd_out = outfile;
|
||||
return (0);
|
||||
}
|
||||
|
||||
void ft_strshift(char *str, int shift)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
if (shift > 0)
|
||||
return ;
|
||||
i = 0;
|
||||
while (str[i - shift] != '\0')
|
||||
{
|
||||
str[i] = str[i - shift];
|
||||
i++;
|
||||
}
|
||||
str[i + shift] = '\0';
|
||||
}
|
||||
|
||||
void ft_quote_remover(char *str)
|
||||
{
|
||||
size_t i;
|
||||
ssize_t start;
|
||||
ssize_t stop;
|
||||
|
||||
start = -1;
|
||||
i = 0;
|
||||
while (str[i] != '\0')
|
||||
{
|
||||
if ((str[i] == '\"' || str[i] == '\''))
|
||||
{
|
||||
if (start == -1)
|
||||
start = i;
|
||||
else if (str[i] == str[start])
|
||||
{
|
||||
stop = i;
|
||||
break ;
|
||||
}
|
||||
}
|
||||
i++;
|
||||
}
|
||||
if (start != -1)
|
||||
{
|
||||
ft_strshift(str, -1);
|
||||
str[stop] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
int ft_cmd_filler(t_list *element, char **args)
|
||||
{
|
||||
t_cmd *content;
|
||||
size_t i;
|
||||
|
||||
if (args == NULL)
|
||||
return (1);
|
||||
content = (t_cmd *)element->content;
|
||||
i = 0;
|
||||
while (args[i] != NULL)
|
||||
{
|
||||
ft_quote_remover(args[i]);
|
||||
i++;
|
||||
}
|
||||
content->args = args;
|
||||
//TODO check if executable exist
|
||||
//TODO change it by env value
|
||||
//TODO add switch to bultin
|
||||
content->executable = ft_strjoin("/usr/bin/", args[0]);
|
||||
if (content->executable == NULL)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int ft_cmds_fill(t_list **cmds, const char *line)
|
||||
{
|
||||
char **tab;
|
||||
char **args;
|
||||
t_list *current;
|
||||
size_t i;
|
||||
|
||||
tab = ft_split_quoted(line, '|');
|
||||
if (tab == NULL)
|
||||
return (1);
|
||||
i = 0;
|
||||
current = *cmds;
|
||||
while (tab[i] != NULL)
|
||||
{
|
||||
args = ft_split_quoted(tab[i], ' ');
|
||||
if (ft_cmd_filler(current, args) == 1)
|
||||
{
|
||||
ft_lstclear(cmds, ft_lstdel);
|
||||
ft_freer_tab_ultimate(2, args, tab);
|
||||
return (1);
|
||||
}
|
||||
current = current->next;
|
||||
i++;
|
||||
}
|
||||
ft_freer_tab_ultimate(1, tab);
|
||||
return (0);
|
||||
}
|
||||
|
||||
t_list **ft_parse_cmds(char *line)
|
||||
{
|
||||
int infile;
|
||||
int outfile;
|
||||
t_list **cmds;
|
||||
|
||||
cmds = malloc(sizeof(t_list *));
|
||||
outfile = ft_outfile(line);
|
||||
infile = ft_infile(line);
|
||||
if (ft_syntatic_verif(line) == 1)
|
||||
return (NULL);
|
||||
if (ft_cmds_prep(cmds, line, infile, outfile) == 1)
|
||||
return (NULL);
|
||||
if (ft_cmds_fill(cmds, line) == 1)
|
||||
return (NULL);
|
||||
ft_printf("%s\n", line);
|
||||
return (cmds);
|
||||
}
|
||||
|
||||
int ft_cmds_excutor(t_list **cmds)
|
||||
{
|
||||
t_cmd *content;
|
||||
t_list *current;
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
current = *cmds;
|
||||
while (current != NULL)
|
||||
{
|
||||
content = current->content;
|
||||
ft_printf("--- COMMAND %d\n", i);
|
||||
ft_printf("excutable: %s\n", content->executable);
|
||||
ft_printf("args:\n%S", content->args);
|
||||
current = current->next;
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
/*int main(int ac, char **av, char **env)
|
||||
{
|
||||
t_list **cmds;
|
||||
char *line;
|
||||
t_data data;
|
||||
|
||||
if (ac == 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);
|
||||
if (data.env == NULL)
|
||||
return (1);
|
||||
cmds = ft_parse_cmds(line);
|
||||
ft_putstr_fd(get_value_key("PATH", data.env), 1);
|
||||
ft_putstr_fd("/n", 1);
|
||||
set_value_key("PATH", i_env, "BITE");
|
||||
ft_putstr_fd(get_value_key("PATH", data.env), 1);
|
||||
cmds = ft_parse_cmds(av[1]);
|
||||
if (cmds == NULL)
|
||||
return (1);
|
||||
if (ft_cmds_executor(cmds, env) == 1)
|
||||
if (ft_cmds_excutor(cmds) == 1)
|
||||
{
|
||||
ft_lstclear(data.env, env_del);
|
||||
ft_lstclear(cmds, ft_cmddel);
|
||||
ft_lstclear(cmds, ft_lstdel);
|
||||
return (1);
|
||||
}
|
||||
ft_lstclear(data.env, env_del);
|
||||
ft_lstclear(cmds, ft_cmddel);
|
||||
ft_lstclear(cmds, ft_lstdel);
|
||||
free(cmds);
|
||||
free(data.env);
|
||||
free(line);
|
||||
return (0);
|
||||
}
|
||||
return (1);
|
||||
}*/
|
||||
|
25
minishell.h
25
minishell.h
@ -1,15 +1,3 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* minishell.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/14 13:45:30 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/02/16 15:17:07 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef MINISHELL_H
|
||||
# define MINISHELL_H
|
||||
# include "libftx/libftx.h"
|
||||
@ -23,25 +11,18 @@
|
||||
# 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 ft_syntatic_verif(const char *str);
|
||||
int ft_file_is_readable(const char *path);
|
||||
int ft_file_is_writable(const char *path);
|
||||
int ft_file_is_appendable(const char *path);
|
||||
int ft_file_is_writeable(const char *path);
|
||||
char *ft_get_file_path(const char *infile);
|
||||
int ft_infile(char *line);
|
||||
int ft_outfile(char *line);
|
||||
int ft_heredoc(char *stop);
|
||||
size_t ft_seglen_quoted(const char *str, char c);
|
||||
int ft_cmds_executor(t_list **cmds, char **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);
|
||||
char *ft_normalizer(char *str);
|
||||
|
||||
typedef struct s_cmd
|
||||
{
|
||||
@ -54,7 +35,6 @@ typedef struct s_cmd
|
||||
typedef struct s_data
|
||||
{
|
||||
t_list **env;
|
||||
int heredoc;
|
||||
} t_data;
|
||||
|
||||
typedef struct s_env
|
||||
@ -62,5 +42,4 @@ typedef struct s_env
|
||||
void *key;
|
||||
void *value;
|
||||
} t_env;
|
||||
|
||||
#endif
|
||||
|
129
outfile.c
129
outfile.c
@ -1,96 +1,76 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* outfile.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/15 18:01:07 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/02/15 21:22:51 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libftx/libftx.h"
|
||||
#include "minishell.h"
|
||||
|
||||
static int ft_outfile_is_valid(const char *line)
|
||||
static int ft_get_outfile(char *line)
|
||||
{
|
||||
char **tab;
|
||||
size_t i;
|
||||
int append;
|
||||
char *path;
|
||||
|
||||
tab = ft_split_quoted(line, ' ');
|
||||
if (tab == NULL)
|
||||
{
|
||||
ft_eprintf("minishell: malloc failed\n");
|
||||
return (0);
|
||||
}
|
||||
if (tab[0] == NULL)
|
||||
return (1);
|
||||
path = NULL;
|
||||
append = 0;
|
||||
i = 0;
|
||||
while (tab[i + 1] != NULL)
|
||||
while (line[i] != '\0')
|
||||
{
|
||||
if (line[i] == '>' && ft_is_in_quote(line, i) == 0)
|
||||
{
|
||||
i++;
|
||||
if (tab[i][0] == '>')
|
||||
if (line[i] == '>')
|
||||
{
|
||||
ft_eprintf("minishell: %s: must be followed by an infile\n", tab[i]);
|
||||
return (0);
|
||||
i++;
|
||||
append = 1;
|
||||
}
|
||||
ft_freer_tab_ultimate(1, tab);
|
||||
return (1);
|
||||
}
|
||||
|
||||
static int ft_get_outfile(const char *line)
|
||||
{
|
||||
size_t i;
|
||||
int fd;
|
||||
char **tab;
|
||||
|
||||
tab = ft_split_quoted(line, ' ');
|
||||
if (tab == NULL)
|
||||
else
|
||||
append = 0;
|
||||
if (path != NULL)
|
||||
free(path);
|
||||
path = ft_get_file_path(line + i);
|
||||
if (path == NULL || ft_file_is_writeable(path) == 0)
|
||||
{
|
||||
ft_eprintf("minishell: malloc failed\n");
|
||||
free(path);
|
||||
return (-1);
|
||||
}
|
||||
}
|
||||
i++;
|
||||
}
|
||||
if (path == NULL)
|
||||
return (-2);
|
||||
}
|
||||
fd = 0;
|
||||
i = 0;
|
||||
while (tab[i + 1] != NULL)
|
||||
{
|
||||
if (ft_strcmp(">", tab[i]) == 0)
|
||||
fd = ft_file_is_writable(ft_quote_remover(tab[i + 1]));
|
||||
else if (ft_strcmp(">>", tab[i]) == 0)
|
||||
fd = ft_file_is_appendable(ft_quote_remover(tab[i + 1]));
|
||||
i++;
|
||||
}
|
||||
ft_freer_tab_ultimate(1, tab);
|
||||
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)
|
||||
{
|
||||
size_t i;
|
||||
size_t y;
|
||||
char **tab;
|
||||
int separator;
|
||||
|
||||
tab = ft_split_quoted(line, ' ');
|
||||
if (tab == NULL)
|
||||
{
|
||||
ft_eprintf("minishell: malloc failed\n");
|
||||
return (1);
|
||||
}
|
||||
i = 0;
|
||||
y = 0;
|
||||
while (tab[i] != NULL)
|
||||
while (line[i] != '\0')
|
||||
{
|
||||
if (tab[i][0] == '>')
|
||||
if (line[i] == '>' && ft_is_in_quote(line, i) == 0)
|
||||
{
|
||||
ft_strshift(line + y, -1 * (ft_strlen(tab[i]) + 1));
|
||||
while (line[i] == '>')
|
||||
line[i++] = ' ';
|
||||
while (line[i] == ' ')
|
||||
i++;
|
||||
ft_strshift(line + y, -1 * (ft_strlen(tab[i]) + 1));
|
||||
}
|
||||
separator = ft_is_in_quote(line, i);
|
||||
if (separator == 0)
|
||||
separator = ' ';
|
||||
else if (separator == 1)
|
||||
separator = '\'';
|
||||
else
|
||||
y = y + ft_strlen(tab[i]);
|
||||
separator = '\"';
|
||||
while (line[i] != separator && line[i] != '\0')
|
||||
line[i++] = ' ';
|
||||
if (line[i] != '\0'
|
||||
&& (separator == '\'' || separator == '\"'))
|
||||
line[i++] = ' ';
|
||||
}
|
||||
i++;
|
||||
}
|
||||
ft_freer_tab_ultimate(1, tab);
|
||||
return (0);
|
||||
}
|
||||
|
||||
@ -98,11 +78,12 @@ int ft_outfile(char *line)
|
||||
{
|
||||
int fd;
|
||||
|
||||
if (ft_outfile_is_valid(line) == 0)
|
||||
return (-2);
|
||||
fd = ft_get_outfile(line);
|
||||
if (fd == -2)
|
||||
return (-2);
|
||||
ft_remove_outfile(line);
|
||||
if (ft_remove_outfile(line))
|
||||
{
|
||||
if (fd > 0)
|
||||
close(fd);
|
||||
return (-1);
|
||||
}
|
||||
return (fd);
|
||||
}
|
||||
|
114
spacer.c
114
spacer.c
@ -1,114 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* spacer.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/15 13:35:50 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/02/16 14:53:16 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;
|
||||
|
||||
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);
|
||||
out[ft_strlen(out) - 1] = '\0';
|
||||
return (out);
|
||||
}
|
73
syntatics.c
73
syntatics.c
@ -1,6 +1,5 @@
|
||||
#include "libftx/libftx.h"
|
||||
#include "minishell.h"
|
||||
#include "utils/utils.h"
|
||||
|
||||
static int ft_quote_verif(const char *str)
|
||||
{
|
||||
@ -13,83 +12,45 @@ static int ft_quote_verif(const char *str)
|
||||
return (0);
|
||||
}
|
||||
|
||||
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] != '|' && str[i] != '\0')
|
||||
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)
|
||||
static int ft_multipipe(const char *str)
|
||||
{
|
||||
size_t i;
|
||||
size_t y;
|
||||
|
||||
i = 0;
|
||||
while (str[i] != '\0')
|
||||
{
|
||||
while (ft_is_in_quote(str, i))
|
||||
i++;
|
||||
if (ft_is_in("|<>", str[i]))
|
||||
{
|
||||
y = 0;
|
||||
while (str[i] == str[i + y])
|
||||
y++;
|
||||
if ((y > 2 && (str[i] == '>' || str[i] == '<'))
|
||||
|| (y > 1 && str[i] == '|'))
|
||||
while (str[i + y] == '|' && !ft_is_in_quote(str, i))
|
||||
{
|
||||
ft_eprintf("minishell: to many %c in a row", str, str[i]);
|
||||
if (y > 0)
|
||||
{
|
||||
ft_eprintf("minishell: Multiple pipes is not supported\n");
|
||||
return (1);
|
||||
}
|
||||
i = i + y;
|
||||
y++;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int ft_empty_verif(const char *str)
|
||||
static int ft_pipe_is_alone(const char *str)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while (str[i] == ' ')
|
||||
i++;
|
||||
if (str[i] == '\0')
|
||||
ft_eprintf("minishell: %s: command not found \n", str);
|
||||
return (str[i] == '\0');
|
||||
i = ft_strlen(str) - 1;
|
||||
while (str[i] != '|' && i > 0)
|
||||
{
|
||||
if (str[i] != ' ')
|
||||
return (0);
|
||||
i--;
|
||||
}
|
||||
ft_eprintf("minishell: Pipe must be followed by a command or redirection\n");
|
||||
return (1);
|
||||
}
|
||||
|
||||
int ft_syntatic_verif(const char *str)
|
||||
{
|
||||
return (ft_quote_verif(str)
|
||||
|| ft_pipe_is_alone(str)
|
||||
|| ft_empty_verif(str)
|
||||
|| ft_special_char_dub(str));
|
||||
return (ft_quote_verif(str) || ft_multipipe(str) || ft_pipe_is_alone(str));
|
||||
}
|
||||
|
27
utils/ft_getstr.c
Normal file
27
utils/ft_getstr.c
Normal file
@ -0,0 +1,27 @@
|
||||
#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));
|
||||
}
|
Binary file not shown.
@ -1,25 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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++;
|
||||
}
|
||||
}
|
Binary file not shown.
@ -1,43 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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);
|
||||
}
|
@ -1,30 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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,29 +1,12 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* utils.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/14 14:46:40 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/02/16 15:15:05 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef UTILS_H
|
||||
# define UTILS_H
|
||||
#ifndef FT_UTILS
|
||||
# define FT_UTILS
|
||||
# include <stdlib.h>
|
||||
# include "../libftx/libftx.h"
|
||||
|
||||
size_t ft_strncpy(char *dst, const char *src, size_t n);
|
||||
int ft_is_in_quote(const char *str, size_t n);
|
||||
char *ft_strreplace(const 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);
|
||||
char *ft_getstr(const char *str, size_t n);
|
||||
void ft_printn(const char *str, size_t n);
|
||||
char **ft_split_quoted(const char *s, char c);
|
||||
void ft_strshift(char *str, int shift);
|
||||
char *ft_quote_remover(char *str);
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user