redirection start working
This commit is contained in:
parent
5178f7181c
commit
0981534a07
5
Makefile
5
Makefile
@ -6,7 +6,7 @@
|
||||
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2022/03/06 12:50:24 by apommier #+# #+# #
|
||||
# Updated: 2022/03/09 20:43:04 by apommier ### ########.fr #
|
||||
# Updated: 2022/03/09 23:29:16 by apommier ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
@ -15,7 +15,8 @@ SRCS = srcs/main.c\
|
||||
srcs/pipe/pipe.c\
|
||||
srcs/pipe/pipex_utils.c\
|
||||
srcs/set_cmd/free_cmd.c\
|
||||
srcs/set_cmd/set_cmd.c
|
||||
srcs/set_cmd/set_cmd.c\
|
||||
srcs/set_redirection/redirection.c
|
||||
OBJS = ${SRCS:.c=.o}
|
||||
CC = clang
|
||||
CFLAGS = -Wall -Wextra
|
||||
|
||||
@ -1,13 +1,13 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* minishell.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/03/06 12:49:28 by apommier #+# #+# */
|
||||
/* Updated: 2022/03/06 12:57:24 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* minishell.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/03/09 22:33:49 by apommier #+# #+# */
|
||||
/* Updated: 2022/03/10 00:17:00 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef MINISHELL_H
|
||||
@ -33,6 +33,8 @@
|
||||
typedef struct s_simple {
|
||||
int nb_args;
|
||||
char *infile;
|
||||
int in_type;
|
||||
int out_type;
|
||||
char *outfile;
|
||||
char **args;
|
||||
char *cmd;
|
||||
@ -67,5 +69,9 @@ void free_double(char **tab);
|
||||
|
||||
//free_cmd
|
||||
void free_cmd(t_cmd *cmd);
|
||||
void exit_shell(t_cmd *cmd);
|
||||
|
||||
//redirection.c set redirection and input good
|
||||
char *set_redirection(t_s_cmd *cmd, char *line);
|
||||
|
||||
#endif
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/03/06 13:27:11 by apommier #+# #+# */
|
||||
/* Updated: 2022/03/09 20:43:42 by apommier ### ########.fr */
|
||||
/* Updated: 2022/03/10 00:16:05 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -59,11 +59,18 @@ void print_prompt(char **path)
|
||||
|
||||
void test(int num)
|
||||
{
|
||||
num = 0;
|
||||
printf("CRTL-C ? try exit\n");
|
||||
}
|
||||
|
||||
int main(int ac, char **av, char **path)
|
||||
{
|
||||
av = 0;
|
||||
if (ac > 1)
|
||||
{
|
||||
printf("too much arguments\n");
|
||||
return (0);
|
||||
}
|
||||
printf("---MINISHELL START---\n");
|
||||
signal(SIGINT, test);
|
||||
print_prompt(path);
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/03/09 12:33:30 by apommier #+# #+# */
|
||||
/* Updated: 2022/03/09 20:43:42 by apommier ### ########.fr */
|
||||
/* Updated: 2022/03/10 01:32:44 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -27,6 +27,7 @@ void free_double(char **tab)
|
||||
|
||||
void exit_shell(t_cmd *cmd)
|
||||
{
|
||||
cmd = 0;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@ -35,8 +36,6 @@ void free_cmd(t_cmd *cmd)
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
printf("free_cmd\n");
|
||||
|
||||
while (cmd->s_cmds[i])
|
||||
{
|
||||
free_double(cmd->s_cmds[i]->args);
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/03/08 15:19:42 by apommier #+# #+# */
|
||||
/* Updated: 2022/03/09 20:43:42 by apommier ### ########.fr */
|
||||
/* Updated: 2022/03/10 01:27:01 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -29,24 +29,29 @@ t_s_cmd *set_s_cmd(char *line, t_cmd *cmd)
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
split_line = ft_split(line, ' ');
|
||||
//line = ft_split(line, ' ');
|
||||
s_cmd = malloc(sizeof(t_s_cmd));
|
||||
if (!s_cmd)
|
||||
return (0);
|
||||
s_cmd->infile = 0;
|
||||
s_cmd->outfile = 0;
|
||||
s_cmd->args = 0;
|
||||
//////////////////split_line = set_redirection(s_cmd, line);
|
||||
//printf("before redirect\n");
|
||||
line = set_redirection(s_cmd, line);//SET REDIRECTION
|
||||
split_line = ft_split(line, ' ');
|
||||
s_cmd->cmd = get_command(split_line, cmd->path);
|
||||
if (!s_cmd->cmd)
|
||||
{
|
||||
free(line);
|
||||
free_double(split_line);
|
||||
free(s_cmd);
|
||||
return (0);
|
||||
}
|
||||
|
||||
s_cmd->nb_args = double_size(split_line);
|
||||
//set_redirection(s_cmd);
|
||||
s_cmd->args = split_line;
|
||||
free(line);
|
||||
return (s_cmd);
|
||||
}
|
||||
|
||||
@ -89,7 +94,7 @@ t_cmd *set_cmd(char *input, char **env)
|
||||
cmd->infile = 0;
|
||||
cmd->nb_s_cmd = double_size(cmds);
|
||||
cmd = split_cmd(cmd, cmds); //split each cmd into args in s_cmd
|
||||
free_double(cmds);
|
||||
free(cmds);
|
||||
if (cmd)
|
||||
{
|
||||
cmd->current_s_cmd = cmd->s_cmds[0];//set first s_cmd
|
||||
|
||||
@ -6,23 +6,176 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/03/09 15:18:58 by apommier #+# #+# */
|
||||
/* Updated: 2022/03/09 20:43:42 by apommier ### ########.fr */
|
||||
/* Updated: 2022/03/10 02:40:46 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
void set_redirection(t_s_cmd *cmd, char *line)
|
||||
char next_space(char *str, int i)
|
||||
{
|
||||
i++;
|
||||
while (str[i] == ' ')
|
||||
i++;
|
||||
return (str[i]);
|
||||
}
|
||||
|
||||
char *cut_str(char *str, int start, int end)
|
||||
{
|
||||
char *swap;
|
||||
char *del;
|
||||
|
||||
//printf("before cut :%s\n", str);
|
||||
del = str;
|
||||
swap = ft_strjoin(&str[end], 0);
|
||||
//printf("&str[start] :%s\n", &str[start]);
|
||||
str[start] = 0;
|
||||
str = ft_strjoin(str, swap);
|
||||
free(del);
|
||||
free(swap);
|
||||
//printf("after cut :%s\n", str);
|
||||
return (str);
|
||||
}
|
||||
|
||||
char *get_word(char *str, int start, int end)
|
||||
{
|
||||
char *new;
|
||||
char *swap;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
//printf("str de get_word %s\n", str);
|
||||
while (str[start + i] == '<' || str[start + i] == '>')
|
||||
i++;
|
||||
while (str[start + i] == ' ')
|
||||
i++;
|
||||
printf("srt= %s\n", &str[start + i]);
|
||||
new = ft_strjoin(&str[start + i], 0);
|
||||
printf("char= %c\n",new[end - start - i]);
|
||||
printf("char str= %s end=%d start=%d out=%d\n", &new[0], end, start, end - start - i - 1);
|
||||
i = 0;
|
||||
while (new[i] && new[i] != ' ' && new[i] != '>' && new[i] != '<')
|
||||
i++;
|
||||
new[i] = 0;
|
||||
//new[end - start - start] = 0;
|
||||
//swap = new;
|
||||
//new = ft_strjoin(&new[i], 0);
|
||||
//free(swap);
|
||||
printf("get word= %s\n", new);
|
||||
return (new);
|
||||
}
|
||||
|
||||
void error_redirect(void)
|
||||
{
|
||||
printf("ERROR: bad syntax\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
char *set_input(char *line, t_s_cmd *cmd, int index)
|
||||
{
|
||||
int i;
|
||||
int word_index;
|
||||
|
||||
//printf("&line[index] :%s\n", &line[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, word_index, i);
|
||||
line = cut_str(line, index, i);
|
||||
return (line);
|
||||
}
|
||||
|
||||
char *set_output(char *line, t_s_cmd *cmd, int index)
|
||||
{
|
||||
int i;
|
||||
int word_index;
|
||||
|
||||
//printf("&line[index] :%s\n", &line[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, i);
|
||||
line = cut_str(line, index, i);
|
||||
return (line);
|
||||
}
|
||||
|
||||
//choose type of redirection, check error
|
||||
char *ft_input(char *line, t_s_cmd *cmd, int index)
|
||||
{
|
||||
int i;
|
||||
char next;
|
||||
|
||||
i = index;
|
||||
next = next_space(line, i);
|
||||
if (line[i + 1] == '<')
|
||||
{
|
||||
cmd->in_type = 1;
|
||||
next = next_space(line, i + 1);
|
||||
}
|
||||
else
|
||||
cmd->in_type = 0;
|
||||
if (next == '<' || next == '>' || !next)
|
||||
error_redirect();
|
||||
line = set_input(line, cmd, 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);
|
||||
if (line[i + 1] == '>')
|
||||
{
|
||||
cmd->in_type = 1;
|
||||
next = next_space(line, i + 1);
|
||||
}
|
||||
else
|
||||
cmd->in_type = 0;
|
||||
if (next == '<' || next == '>' || !next)
|
||||
error_redirect();
|
||||
line = set_output(line, cmd, i);
|
||||
return (line);
|
||||
}
|
||||
|
||||
char *set_redirection(t_s_cmd *cmd, char *line)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (line[i] && line[i] != ' ')
|
||||
i++;
|
||||
if (!line[i])
|
||||
return (0);
|
||||
while (line[i] && (line[i] != '<' || line[i] != '<'))
|
||||
i++;
|
||||
if (!line[i])
|
||||
return (0);
|
||||
//printf("enter redirection\n");
|
||||
while (line[i])
|
||||
{
|
||||
if(line[i] == '<')
|
||||
{
|
||||
line = ft_input(line, cmd, i);
|
||||
i = 0;
|
||||
}
|
||||
else if(line[i] == '>')
|
||||
{
|
||||
line = ft_output(line, cmd, i);
|
||||
i = 0;
|
||||
}
|
||||
else
|
||||
i++;
|
||||
}
|
||||
return(line);
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user