fix norm
This commit is contained in:
parent
854e71e5f1
commit
d44a1b5e44
8
Makefile
8
Makefile
@ -6,7 +6,7 @@
|
||||
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2022/03/06 12:50:24 by apommier #+# #+# #
|
||||
# Updated: 2022/04/16 09:58:10 by apommier ### ########.fr #
|
||||
# Updated: 2022/04/19 15:05:18 by apommier ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
@ -18,6 +18,9 @@ SRCS = srcs/main.c\
|
||||
srcs/set_cmd/set_cmd.c\
|
||||
srcs/set_redirection/redirection.c\
|
||||
srcs/set_redirection/utils.c\
|
||||
srcs/set_redirection/set_heredoc.c\
|
||||
srcs/set_redirection/set_input.c\
|
||||
srcs/set_redirection/set_output.c\
|
||||
srcs/built_in/unset.c\
|
||||
srcs/built_in/cd.c\
|
||||
srcs/built_in/echo.c\
|
||||
@ -29,10 +32,9 @@ SRCS = srcs/main.c\
|
||||
srcs/built_in/exit.c\
|
||||
srcs/set_quote/split_with_quote.c\
|
||||
srcs/set_quote/set_quote.c\
|
||||
srcs/set_quote/set_var.c\
|
||||
srcs/built_in/choose_builtin.c
|
||||
|
||||
|
||||
|
||||
OBJS = ${SRCS:.c=.o}
|
||||
CC = clang
|
||||
CFLAGS = -Wall -Wextra -g
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/03/09 22:33:49 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/19 12:18:26 by apommier ### ########.fr */
|
||||
/* Updated: 2022/04/19 15:05:51 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -91,10 +91,24 @@ void crtl_c(int num);
|
||||
void sig_heredoc(int num);
|
||||
|
||||
//redirection.c set redirection and input good
|
||||
char *get_word(char *str, int start);
|
||||
char **add_line(char **tab, char *line);
|
||||
char *set_redirection(t_s_cmd *cmd, char *line, int index);
|
||||
char next_space(char *str, int i);
|
||||
|
||||
//uitls redirection
|
||||
//set_heredoc
|
||||
int wait_prompt(t_s_cmd *cmd, int index);
|
||||
|
||||
//set_input.c
|
||||
char *ft_input(char *line, t_s_cmd *cmd, int index);
|
||||
|
||||
//set_output.c
|
||||
char *ft_output(char *line, t_s_cmd *cmd, int index);
|
||||
|
||||
//set_var.c
|
||||
char *set_var(t_cmd *big_cmd, char *cmd);
|
||||
|
||||
//utils redirection
|
||||
int parse_quote(t_cmd *cmd);
|
||||
char *get_str(char *str, int start, int end);
|
||||
char *cut_str(char *str, int start, int end);
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/03/09 12:33:30 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/19 13:30:02 by apommier ### ########.fr */
|
||||
/* Updated: 2022/04/19 15:01:32 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -40,11 +40,39 @@ void exit_shell(t_cmd *cmd, int ret)
|
||||
exit(ret);
|
||||
}
|
||||
|
||||
void del_heredoc(void)
|
||||
{
|
||||
long i;
|
||||
char *str;
|
||||
char *nbr;
|
||||
|
||||
i = 0;
|
||||
while (i < 2147483647)
|
||||
{
|
||||
nbr = ft_itoa(i);
|
||||
if (i == 0)
|
||||
str = ft_strdup(".heredoc");
|
||||
else
|
||||
str = ft_strjoin(".heredoc", nbr);
|
||||
free(nbr);
|
||||
if (access(str, F_OK) == 0)
|
||||
unlink(str);
|
||||
else
|
||||
{
|
||||
free(str);
|
||||
return ;
|
||||
}
|
||||
free(str);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
void free_cmd(t_cmd *cmd)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
del_heredoc();
|
||||
while (cmd->s_cmds && cmd->s_cmds[i])
|
||||
{
|
||||
free_double(cmd->s_cmds[i]->args);
|
||||
@ -62,22 +90,3 @@ void free_cmd(t_cmd *cmd)
|
||||
free(cmd->s_cmds);
|
||||
free(cmd);
|
||||
}
|
||||
|
||||
void check_heredoc()
|
||||
{
|
||||
int i;
|
||||
char *str;
|
||||
|
||||
while (i <= 2147483647)
|
||||
{
|
||||
str = ft_strjoin(".heredoc", ft_itoa(i));
|
||||
if (access(str, F_OK) == 0)
|
||||
{
|
||||
unlink(str);
|
||||
free(str);
|
||||
return ;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
free(str);
|
||||
}
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/03/08 15:19:42 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/19 12:46:09 by apommier ### ########.fr */
|
||||
/* Updated: 2022/04/19 14:40:24 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -113,7 +113,7 @@ t_cmd *set_cmd(char *input, char **env, int nb)
|
||||
cmd = split_cmd(cmd, cmds);
|
||||
if (!cmd)
|
||||
{
|
||||
free(cmds);
|
||||
free_double(cmds);
|
||||
return (0);
|
||||
}
|
||||
parse_quote(cmd);
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/04/09 23:58:21 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/19 13:00:38 by apommier ### ########.fr */
|
||||
/* Updated: 2022/04/19 15:07:01 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -76,121 +76,14 @@ int is_quote_good(char *str)
|
||||
return (1);
|
||||
}
|
||||
|
||||
char *del_char(char *str, int *index)
|
||||
{
|
||||
char *swap;
|
||||
char *swap2;
|
||||
|
||||
swap = 0;
|
||||
swap = ft_strdup(str + *index + 1);
|
||||
str[*index] = 0;
|
||||
swap2 = str;
|
||||
str = ft_strjoin(str, swap);
|
||||
free(swap2);
|
||||
free(swap);
|
||||
return (str);
|
||||
}
|
||||
|
||||
char *get_var(t_cmd *cmd, char *var_name)
|
||||
{
|
||||
char *line;
|
||||
char **split_line;
|
||||
int index;
|
||||
|
||||
line = 0;
|
||||
if (!ft_strcmp(var_name, "?"))
|
||||
return (ft_itoa(cmd->err_var));
|
||||
index = find_it(cmd->env, var_name);
|
||||
if (index >= 0)
|
||||
{
|
||||
line = cmd->env[index];
|
||||
split_line = ft_split(line, '=');
|
||||
if (split_line[1])
|
||||
line = ft_strdup(split_line[1]);
|
||||
else
|
||||
return (0);
|
||||
free_double(split_line);
|
||||
}
|
||||
return (line);
|
||||
}
|
||||
|
||||
char *change_var(t_cmd *big_cmd, char *cmd, int *index)
|
||||
{
|
||||
int i;
|
||||
char *swap;
|
||||
char *swap2;
|
||||
char *ret;
|
||||
char *var;
|
||||
|
||||
i = *index + 1;
|
||||
while (cmd[i] && (ft_isalnum(cmd[i]) || cmd[i] == '_' || cmd[i] == '?'))
|
||||
i++;
|
||||
swap = ft_substr(cmd, *index + 1, i - *index - 1);
|
||||
var = get_var(big_cmd, swap);
|
||||
free(swap);
|
||||
swap2 = ft_strdup(cmd + i);
|
||||
cmd[*index] = 0;
|
||||
ret = ft_strjoin(cmd, var);
|
||||
free(cmd);
|
||||
*index += ft_strlen(var) - 1;
|
||||
free(var);
|
||||
var = ret;
|
||||
ret = ft_strjoin(ret, swap2);
|
||||
free(var);
|
||||
free(swap2);
|
||||
return (ret);
|
||||
}
|
||||
|
||||
char *set_var(t_cmd *big_cmd, char *cmd)
|
||||
{
|
||||
int i;
|
||||
char *del;
|
||||
|
||||
i = 0;
|
||||
while (cmd[i])
|
||||
{
|
||||
if (cmd[i] == '\'')
|
||||
{
|
||||
cmd = del_char(cmd, &i);
|
||||
if (cmd[i])
|
||||
{
|
||||
while (cmd[i] != '\'')
|
||||
{
|
||||
i++;
|
||||
}
|
||||
cmd = del_char(cmd, &i);
|
||||
}
|
||||
}
|
||||
else if (cmd[i] == '"')
|
||||
{
|
||||
cmd = del_char(cmd, &i);
|
||||
if (cmd[i])
|
||||
{
|
||||
while (cmd[i] != '"')
|
||||
{
|
||||
if (cmd[i] == '$')
|
||||
cmd = change_var(big_cmd, cmd, &i);
|
||||
i++;
|
||||
}
|
||||
cmd = del_char(cmd, &i);
|
||||
}
|
||||
}
|
||||
else if (cmd[i] == '$')
|
||||
cmd = change_var(big_cmd, cmd, &i);
|
||||
else
|
||||
i++;
|
||||
}
|
||||
return (cmd);
|
||||
}
|
||||
|
||||
int parse_quote(t_cmd *cmd)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
char *swap;
|
||||
|
||||
i = 0;
|
||||
while (cmd->s_cmds[i])
|
||||
i = -1;
|
||||
while (cmd->s_cmds[++i])
|
||||
{
|
||||
j = -1;
|
||||
while (cmd->s_cmds[i]->args[++j])
|
||||
@ -208,7 +101,6 @@ int parse_quote(t_cmd *cmd)
|
||||
cmd->s_cmds[i]->cmd = ft_strdup(cmd->s_cmds[i]->args[0]);
|
||||
if (!cmd->s_cmds[i]->cmd)
|
||||
cmd->s_cmds[i]->cmd = ft_strdup(cmd->s_cmds[i]->args[0]);
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
119
srcs/set_quote/set_var.c
Normal file
119
srcs/set_quote/set_var.c
Normal file
@ -0,0 +1,119 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* set_var.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/04/19 15:02:01 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/19 15:04:45 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
char *del_char(char *str, int *index)
|
||||
{
|
||||
char *swap;
|
||||
char *swap2;
|
||||
|
||||
swap = 0;
|
||||
swap = ft_strdup(str + *index + 1);
|
||||
str[*index] = 0;
|
||||
swap2 = str;
|
||||
str = ft_strjoin(str, swap);
|
||||
free(swap2);
|
||||
free(swap);
|
||||
return (str);
|
||||
}
|
||||
|
||||
char *get_var(t_cmd *cmd, char *var_name)
|
||||
{
|
||||
char *line;
|
||||
char **split_line;
|
||||
int index;
|
||||
|
||||
line = 0;
|
||||
if (!ft_strcmp(var_name, "?"))
|
||||
return (ft_itoa(cmd->err_var));
|
||||
index = find_it(cmd->env, var_name);
|
||||
if (index >= 0)
|
||||
{
|
||||
line = cmd->env[index];
|
||||
split_line = ft_split(line, '=');
|
||||
if (split_line[1])
|
||||
line = ft_strdup(split_line[1]);
|
||||
else
|
||||
return (0);
|
||||
free_double(split_line);
|
||||
}
|
||||
return (line);
|
||||
}
|
||||
|
||||
char *change_var(t_cmd *big_cmd, char *cmd, int *index)
|
||||
{
|
||||
int i;
|
||||
char *swap;
|
||||
char *swap2;
|
||||
char *ret;
|
||||
char *var;
|
||||
|
||||
i = *index + 1;
|
||||
while (cmd[i] && (ft_isalnum(cmd[i]) || cmd[i] == '_' || cmd[i] == '?'))
|
||||
i++;
|
||||
swap = ft_substr(cmd, *index + 1, i - *index - 1);
|
||||
var = get_var(big_cmd, swap);
|
||||
free(swap);
|
||||
swap2 = ft_strdup(cmd + i);
|
||||
cmd[*index] = 0;
|
||||
ret = ft_strjoin(cmd, var);
|
||||
free(cmd);
|
||||
*index += ft_strlen(var) - 1;
|
||||
free(var);
|
||||
var = ret;
|
||||
ret = ft_strjoin(ret, swap2);
|
||||
free(var);
|
||||
free(swap2);
|
||||
return (ret);
|
||||
}
|
||||
|
||||
char *set_var(t_cmd *big_cmd, char *cmd)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (cmd[i])
|
||||
{
|
||||
if (cmd[i] == '\'')
|
||||
{
|
||||
cmd = del_char(cmd, &i);
|
||||
if (cmd[i])
|
||||
{
|
||||
while (cmd[i] != '\'')
|
||||
{
|
||||
i++;
|
||||
}
|
||||
cmd = del_char(cmd, &i);
|
||||
}
|
||||
}
|
||||
else if (cmd[i] == '"')
|
||||
{
|
||||
cmd = del_char(cmd, &i);
|
||||
if (cmd[i])
|
||||
{
|
||||
while (cmd[i] != '"')
|
||||
{
|
||||
if (cmd[i] == '$')
|
||||
cmd = change_var(big_cmd, cmd, &i);
|
||||
i++;
|
||||
}
|
||||
cmd = del_char(cmd, &i);
|
||||
}
|
||||
}
|
||||
else if (cmd[i] == '$')
|
||||
cmd = change_var(big_cmd, cmd, &i);
|
||||
else
|
||||
i++;
|
||||
}
|
||||
return (cmd);
|
||||
}
|
||||
@ -6,35 +6,12 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/03/09 15:18:58 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/19 13:07:33 by apommier ### ########.fr */
|
||||
/* Updated: 2022/04/19 14:56:33 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
char next_space(char *str, int i)
|
||||
{
|
||||
while (str[i] == ' ')
|
||||
i++;
|
||||
return (str[i]);
|
||||
}
|
||||
|
||||
char *cut_str(char *str, int start, int end)
|
||||
{
|
||||
char *swap;
|
||||
char *del;
|
||||
|
||||
swap = 0;
|
||||
del = str;
|
||||
if (str[end])
|
||||
swap = ft_strjoin(&str[end], 0);
|
||||
str[start] = 0;
|
||||
str = ft_strjoin(str, swap);
|
||||
free(del);
|
||||
free(swap);
|
||||
return (str);
|
||||
}
|
||||
|
||||
char *get_word(char *str, int start)
|
||||
{
|
||||
char *new;
|
||||
@ -53,100 +30,6 @@ char *get_word(char *str, int start)
|
||||
return (new);
|
||||
}
|
||||
|
||||
char *set_input(char *line, t_s_cmd *cmd, int index)
|
||||
{
|
||||
int i;
|
||||
int word_index;
|
||||
|
||||
word_index = 0;
|
||||
i = index;
|
||||
i++;
|
||||
if (line[i] == '<')
|
||||
i++;
|
||||
word_index = i;
|
||||
while (line[i] == ' ' && line[i])
|
||||
i++;
|
||||
while ((line[i] != ' ' && line[i] != '<' && line[i] != '>') && line[i])
|
||||
i++;
|
||||
cmd->infile = get_word(line, index);
|
||||
cmd->infile = set_var(cmd->big_cmd, cmd->infile);
|
||||
if (access(cmd->infile, R_OK))
|
||||
{
|
||||
ft_putstr_fd("Minishell: ", 2);
|
||||
ft_putstr_fd(cmd->infile, 2);
|
||||
if (access(cmd->infile, F_OK))
|
||||
ft_putstr_fd(": no such file\n", 2);
|
||||
else
|
||||
ft_putstr_fd(": Permission denied\n", 2);
|
||||
return (0);
|
||||
}
|
||||
line = cut_str(line, index, i);
|
||||
return (line);
|
||||
}
|
||||
|
||||
char *set_output(char *line, t_s_cmd *cmd, int index)
|
||||
{
|
||||
int i;
|
||||
int word_index;
|
||||
|
||||
word_index = 0;
|
||||
i = index;
|
||||
i++;
|
||||
if (line[i] == '>')
|
||||
i++;
|
||||
word_index = i;
|
||||
while (line[i] == ' ' && line[i])
|
||||
i++;
|
||||
while ((line[i] != ' ' && line[i] != '<' && line[i] != '>') && line[i])
|
||||
i++;
|
||||
cmd->outfile = get_word(line, index);
|
||||
cmd->outfile = set_var(cmd->big_cmd, cmd->outfile);
|
||||
line = cut_str(line, index, i);
|
||||
return (line);
|
||||
}
|
||||
|
||||
char *ft_input(char *line, t_s_cmd *cmd, int index)
|
||||
{
|
||||
int i;
|
||||
char next;
|
||||
|
||||
i = index;
|
||||
next = next_space(line, i + 1);
|
||||
if (line[i + 1] == '<')
|
||||
{
|
||||
cmd->in_type = 1;
|
||||
next = next_space(line, i + 2);
|
||||
}
|
||||
else
|
||||
cmd->in_type = 0;
|
||||
if (next == '<' || next == '>' || !next)
|
||||
return (0);
|
||||
line = set_input(line, cmd, i);
|
||||
if (!line)
|
||||
return (0);
|
||||
return (line);
|
||||
}
|
||||
|
||||
char *ft_output(char *line, t_s_cmd *cmd, int index)
|
||||
{
|
||||
int i;
|
||||
char next;
|
||||
|
||||
i = index;
|
||||
next = next_space(line, i + 1);
|
||||
if (line[i + 1] == '>')
|
||||
{
|
||||
cmd->in_type = 1;
|
||||
next = next_space(line, i + 2);
|
||||
}
|
||||
else
|
||||
cmd->in_type = 0;
|
||||
if (next == '<' || next == '>' || !next)
|
||||
return (0);
|
||||
line = set_output(line, cmd, i);
|
||||
return (line);
|
||||
}
|
||||
|
||||
int set_file(char *file)
|
||||
{
|
||||
int fd;
|
||||
@ -190,103 +73,6 @@ char **add_line(char **tab, char *line)
|
||||
return (ret);
|
||||
}
|
||||
|
||||
char *set_heredoc(int index, char **in)
|
||||
{
|
||||
char *nbr_file;
|
||||
char *file_name;
|
||||
int fd;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
if (index)
|
||||
{
|
||||
nbr_file = ft_itoa(index + 1);
|
||||
file_name = ft_strjoin(".heredoc", nbr_file);
|
||||
}
|
||||
else
|
||||
file_name = ft_strjoin(".heredoc", 0);
|
||||
fd = open(file_name, O_RDWR | O_CREAT | O_TRUNC, 0666);
|
||||
if (fd > 0)
|
||||
{
|
||||
print_double_fd(in, fd);
|
||||
free_double(in);
|
||||
}
|
||||
close(fd);
|
||||
return (file_name);
|
||||
}
|
||||
|
||||
void sig_heredoc(int num)
|
||||
{
|
||||
struct sigaction base;
|
||||
|
||||
(void)num;
|
||||
memset(&base, 0, sizeof(base));
|
||||
base.sa_handler = &crtl_c;
|
||||
base.sa_flags = 0;
|
||||
ft_putchar_fd('\n', 1);
|
||||
if (sigaction(SIGINT, &base, 0) == -1)
|
||||
{
|
||||
printf("sigaction error2\n");
|
||||
return ;
|
||||
}
|
||||
}
|
||||
|
||||
int wait_prompt(t_s_cmd *cmd, int index)
|
||||
{
|
||||
char *input;
|
||||
int i;
|
||||
char **history;
|
||||
char *in;
|
||||
char *dup;
|
||||
char *del;
|
||||
struct sigaction test;
|
||||
|
||||
memset(&test, 0, sizeof(test));
|
||||
test.sa_handler = &sig_heredoc;
|
||||
test.sa_flags = 0;
|
||||
in = ft_strjoin(cmd->infile, "\n");
|
||||
free(cmd->infile);
|
||||
cmd->infile = 0;
|
||||
if (sigaction(SIGINT, &test, 0) == -1)
|
||||
{
|
||||
printf("sigaction error\n");
|
||||
exit(1);
|
||||
}
|
||||
history = 0;
|
||||
input = 0;
|
||||
i = 0;
|
||||
while (i == 0 || (input && ft_strlen(input) && ft_strcmp(input, in)))
|
||||
{
|
||||
i = 1;
|
||||
if (input)
|
||||
free(input);
|
||||
ft_putstr_fd("> ", 0);
|
||||
input = get_next_line(0);
|
||||
if (!input)
|
||||
{
|
||||
free(in);
|
||||
free_double(history);
|
||||
return (-1);
|
||||
}
|
||||
if (ft_strcmp(input, in))
|
||||
{
|
||||
dup = ft_strdup(input);
|
||||
dup[ft_strlen(input) - 1] = 0;
|
||||
del = dup;
|
||||
dup = set_var(cmd->big_cmd, dup);
|
||||
history = add_line(history, dup);
|
||||
if (dup != del)
|
||||
free(del);
|
||||
free(dup);
|
||||
}
|
||||
}
|
||||
free(in);
|
||||
free(input);
|
||||
cmd->infile = set_heredoc(index, history);
|
||||
cmd->in_type = 0;
|
||||
return (1);
|
||||
}
|
||||
|
||||
char *set_redirection(t_s_cmd *cmd, char *line, int index)
|
||||
{
|
||||
int i;
|
||||
|
||||
110
srcs/set_redirection/set_heredoc.c
Normal file
110
srcs/set_redirection/set_heredoc.c
Normal file
@ -0,0 +1,110 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* set_heredoc.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/04/19 14:46:01 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/19 14:51:11 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
char *set_heredoc(int index, char **in)
|
||||
{
|
||||
char *nbr_file;
|
||||
char *file_name;
|
||||
int fd;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
if (index)
|
||||
{
|
||||
nbr_file = ft_itoa(index + 1);
|
||||
file_name = ft_strjoin(".heredoc", nbr_file);
|
||||
}
|
||||
else
|
||||
file_name = ft_strjoin(".heredoc", 0);
|
||||
fd = open(file_name, O_RDWR | O_CREAT | O_TRUNC, 0666);
|
||||
if (fd > 0)
|
||||
{
|
||||
print_double_fd(in, fd);
|
||||
free_double(in);
|
||||
}
|
||||
close(fd);
|
||||
return (file_name);
|
||||
}
|
||||
|
||||
void sig_heredoc(int num)
|
||||
{
|
||||
struct sigaction base;
|
||||
|
||||
(void)num;
|
||||
memset(&base, 0, sizeof(base));
|
||||
base.sa_handler = &crtl_c;
|
||||
base.sa_flags = 0;
|
||||
ft_putchar_fd('\n', 1);
|
||||
if (sigaction(SIGINT, &base, 0) == -1)
|
||||
{
|
||||
printf("sigaction error2\n");
|
||||
return ;
|
||||
}
|
||||
}
|
||||
|
||||
int wait_prompt(t_s_cmd *cmd, int index)
|
||||
{
|
||||
char *input;
|
||||
int i;
|
||||
char **history;
|
||||
char *in;
|
||||
char *dup;
|
||||
char *del;
|
||||
struct sigaction test;
|
||||
|
||||
memset(&test, 0, sizeof(test));
|
||||
test.sa_handler = &sig_heredoc;
|
||||
test.sa_flags = 0;
|
||||
in = ft_strjoin(cmd->infile, "\n");
|
||||
free(cmd->infile);
|
||||
cmd->infile = 0;
|
||||
if (sigaction(SIGINT, &test, 0) == -1)
|
||||
{
|
||||
printf("sigaction error\n");
|
||||
exit(1);
|
||||
}
|
||||
history = 0;
|
||||
input = 0;
|
||||
i = 0;
|
||||
while (i == 0 || (input && ft_strlen(input) && ft_strcmp(input, in)))
|
||||
{
|
||||
i = 1;
|
||||
if (input)
|
||||
free(input);
|
||||
ft_putstr_fd("> ", 0);
|
||||
input = get_next_line(0);
|
||||
if (!input)
|
||||
{
|
||||
free(in);
|
||||
free_double(history);
|
||||
return (-1);
|
||||
}
|
||||
if (ft_strcmp(input, in))
|
||||
{
|
||||
dup = ft_strdup(input);
|
||||
dup[ft_strlen(input) - 1] = 0;
|
||||
del = dup;
|
||||
dup = set_var(cmd->big_cmd, dup);
|
||||
history = add_line(history, dup);
|
||||
if (dup != del)
|
||||
free(del);
|
||||
free(dup);
|
||||
}
|
||||
}
|
||||
free(in);
|
||||
free(input);
|
||||
cmd->infile = set_heredoc(index, history);
|
||||
cmd->in_type = 0;
|
||||
return (1);
|
||||
}
|
||||
72
srcs/set_redirection/set_input.c
Normal file
72
srcs/set_redirection/set_input.c
Normal file
@ -0,0 +1,72 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* set_input.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/04/19 14:44:22 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/19 14:51:14 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
char *set_input(char *line, t_s_cmd *cmd, int index)
|
||||
{
|
||||
int i;
|
||||
int word_index;
|
||||
|
||||
word_index = 0;
|
||||
i = index;
|
||||
i++;
|
||||
if (line[i] == '<')
|
||||
i++;
|
||||
word_index = i;
|
||||
while (line[i] == ' ' && line[i])
|
||||
i++;
|
||||
while ((line[i] != ' ' && line[i] != '<' && line[i] != '>') && line[i])
|
||||
i++;
|
||||
if (cmd->infile)
|
||||
free(cmd->infile);
|
||||
cmd->infile = get_word(line, index);
|
||||
cmd->infile = set_var(cmd->big_cmd, cmd->infile);
|
||||
if (cmd->in_type == 0)
|
||||
{
|
||||
if (access(cmd->infile, R_OK))
|
||||
{
|
||||
ft_putstr_fd("Minishell: ", 2);
|
||||
ft_putstr_fd(cmd->infile, 2);
|
||||
if (access(cmd->infile, F_OK))
|
||||
ft_putstr_fd(": no such file\n", 2);
|
||||
else
|
||||
ft_putstr_fd(": Permission denied\n", 2);
|
||||
free(cmd->infile);
|
||||
return (0);
|
||||
}
|
||||
}
|
||||
line = cut_str(line, index, i);
|
||||
return (line);
|
||||
}
|
||||
|
||||
char *ft_input(char *line, t_s_cmd *cmd, int index)
|
||||
{
|
||||
int i;
|
||||
char next;
|
||||
|
||||
i = index;
|
||||
next = next_space(line, i + 1);
|
||||
if (line[i + 1] == '<')
|
||||
{
|
||||
cmd->in_type = 1;
|
||||
next = next_space(line, i + 2);
|
||||
}
|
||||
else
|
||||
cmd->in_type = 0;
|
||||
if (next == '<' || next == '>' || !next)
|
||||
return (0);
|
||||
line = set_input(line, cmd, i);
|
||||
if (!line)
|
||||
return (0);
|
||||
return (line);
|
||||
}
|
||||
54
srcs/set_redirection/set_output.c
Normal file
54
srcs/set_redirection/set_output.c
Normal file
@ -0,0 +1,54 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* set_output.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/04/19 14:45:10 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/19 14:51:17 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
char *set_output(char *line, t_s_cmd *cmd, int index)
|
||||
{
|
||||
int i;
|
||||
int word_index;
|
||||
|
||||
word_index = 0;
|
||||
i = index;
|
||||
i++;
|
||||
if (line[i] == '>')
|
||||
i++;
|
||||
word_index = i;
|
||||
while (line[i] == ' ' && line[i])
|
||||
i++;
|
||||
while ((line[i] != ' ' && line[i] != '<' && line[i] != '>') && line[i])
|
||||
i++;
|
||||
cmd->outfile = get_word(line, index);
|
||||
cmd->outfile = set_var(cmd->big_cmd, cmd->outfile);
|
||||
line = cut_str(line, index, i);
|
||||
return (line);
|
||||
}
|
||||
|
||||
char *ft_output(char *line, t_s_cmd *cmd, int index)
|
||||
{
|
||||
int i;
|
||||
char next;
|
||||
|
||||
i = index;
|
||||
next = next_space(line, i + 1);
|
||||
if (line[i + 1] == '>')
|
||||
{
|
||||
cmd->in_type = 1;
|
||||
next = next_space(line, i + 2);
|
||||
}
|
||||
else
|
||||
cmd->in_type = 0;
|
||||
if (next == '<' || next == '>' || !next)
|
||||
return (0);
|
||||
line = set_output(line, cmd, i);
|
||||
return (line);
|
||||
}
|
||||
@ -6,12 +6,35 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/03/11 16:35:37 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/19 13:07:55 by apommier ### ########.fr */
|
||||
/* Updated: 2022/04/19 14:47:21 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
char *cut_str(char *str, int start, int end)
|
||||
{
|
||||
char *swap;
|
||||
char *del;
|
||||
|
||||
swap = 0;
|
||||
del = str;
|
||||
if (str[end])
|
||||
swap = ft_strjoin(&str[end], 0);
|
||||
str[start] = 0;
|
||||
str = ft_strjoin(str, swap);
|
||||
free(del);
|
||||
free(swap);
|
||||
return (str);
|
||||
}
|
||||
|
||||
char next_space(char *str, int i)
|
||||
{
|
||||
while (str[i] == ' ')
|
||||
i++;
|
||||
return (str[i]);
|
||||
}
|
||||
|
||||
int double_size(char **tab)
|
||||
{
|
||||
int i;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user