heredoc support
This commit is contained in:
parent
3654ff91bb
commit
b8017175e8
BIN
.file.c.swp
Normal file
BIN
.file.c.swp
Normal file
Binary file not shown.
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_getstr.c
|
||||
SRCS = main.c file.c infile.c outfile.c ${UTILS_SRC}
|
||||
SRCS = ${UTILS_SRC} main.c file.c infile.c outfile.c heredoc.c
|
||||
|
||||
OBJS = ${SRCS:.c=.o}
|
||||
|
||||
@ -18,7 +18,7 @@ all: ${NAME}
|
||||
|
||||
${NAME}: ${OBJS}
|
||||
make -C libftx all
|
||||
${CC} ${OBJS} -o ${NAME} ${LIBS}
|
||||
${CC} ${OBJS} -o ${NAME} ${LIBS} -lreadline
|
||||
|
||||
clean:
|
||||
make -C libftx clean
|
||||
|
15
file.c
15
file.c
@ -47,21 +47,20 @@ char *ft_get_file_path(char *infile)
|
||||
size_t i;
|
||||
size_t n;
|
||||
|
||||
n = 0;
|
||||
while (infile[-n] == infile[0])
|
||||
n = 1;
|
||||
while (infile[-n] == infile[-1])
|
||||
n++;
|
||||
i = 1;
|
||||
i = 0;
|
||||
while (infile[i] == ' ')
|
||||
i++;
|
||||
if (infile[i] == '\0')
|
||||
if (infile[i] == '\0' || infile[i] == '>' || infile[i] == '<')
|
||||
{
|
||||
ft_eprintf("minishell: syntax error near ");
|
||||
if ((infile[0] == '<' && n < 3) || (infile[0] == '>' && n < 4))
|
||||
ft_eprintf("unexpected token `newline`\n");
|
||||
if ((infile[0] == '<' && n > 3) || (infile[0] == '>' && n > 2))
|
||||
ft_eprintf("unexpected token `%c`\n", infile[i - 1]);
|
||||
else
|
||||
ft_eprintf("unexpected token `%c`\n", infile[i]);
|
||||
ft_eprintf("unexpected token `newline`\n");
|
||||
return (NULL);
|
||||
}
|
||||
return (ft_getstr(infile, i + 1));
|
||||
//j'aime jackie et michel
|
||||
}
|
||||
|
25
heredoc.c
Normal file
25
heredoc.c
Normal file
@ -0,0 +1,25 @@
|
||||
#include "minishell.h"
|
||||
#include <readline/history.h>
|
||||
|
||||
int ft_heredoc(char *stop)
|
||||
{
|
||||
int fds[2];
|
||||
char *line;
|
||||
|
||||
pipe(fds);
|
||||
line = readline("> ");
|
||||
while (line != NULL)
|
||||
{
|
||||
if (ft_strcmp(line, stop) == 0)
|
||||
{
|
||||
free(line);
|
||||
break ;
|
||||
}
|
||||
ft_putstr_fd(line, fds[1]);
|
||||
add_history(line);
|
||||
free(line);
|
||||
line = readline("> ");
|
||||
}
|
||||
close(fds[1]);
|
||||
return (fds[0]);
|
||||
}
|
40
infile.c
40
infile.c
@ -3,30 +3,43 @@
|
||||
static int ft_get_infile(char *line)
|
||||
{
|
||||
size_t i;
|
||||
int fd;
|
||||
char *path;
|
||||
|
||||
path = NULL;
|
||||
fd = 0;
|
||||
i = 0;
|
||||
while (line[i] != '\0')
|
||||
{
|
||||
if (line[i] == '<' && ft_is_in_quote(line, i) == 0)
|
||||
{
|
||||
if (path != NULL)
|
||||
free(path);
|
||||
path = ft_get_file_path(line + i);
|
||||
if (path == NULL || ft_file_is_readable(path) == 0)
|
||||
i++;
|
||||
if (fd != 0)
|
||||
close(fd);
|
||||
if (line[i] == '<')
|
||||
{
|
||||
free(path);
|
||||
return (-1);
|
||||
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++;
|
||||
}
|
||||
if (path == NULL)
|
||||
return (-2);
|
||||
i = open(path, O_RDONLY);
|
||||
free(path);
|
||||
return (i);
|
||||
return (fd);
|
||||
}
|
||||
|
||||
static int ft_remove_infile(char *line)
|
||||
@ -39,7 +52,8 @@ static int ft_remove_infile(char *line)
|
||||
{
|
||||
if (line[i] == '<' && ft_is_in_quote(line, i) == 0)
|
||||
{
|
||||
line[i++] = ' ';
|
||||
while (line[i] == '<')
|
||||
line[i++] = ' ';
|
||||
while (line[i] == ' ')
|
||||
i++;
|
||||
separator = ft_is_in_quote(line, i);
|
||||
|
BIN
libftx/libftx.a
BIN
libftx/libftx.a
Binary file not shown.
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/26 14:47:54 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/02/01 16:20:05 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/02/03 12:42:13 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -67,7 +67,7 @@ char *ft_itoa(int n);
|
||||
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
|
||||
void ft_striteri(char *s, void (*f)(unsigned int, char*));
|
||||
void ft_putchar_fd(int fd, char c);
|
||||
void ft_putstr_fd(int fd, char *s);
|
||||
void ft_putstr_fd(char *s, int fd);
|
||||
void ft_putendl_fd(int fd, char *s);
|
||||
void ft_putnbr_fd(int fd, int n);
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/23 18:08:31 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/05 17:37:12 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/02/03 12:52:38 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -27,7 +27,7 @@ int ft_dprintarg(int fd, int arg, va_list args)
|
||||
if (arg == 'S')
|
||||
return (ft_dprintstrtab(fd, va_arg(args, char **)));
|
||||
if (arg == 's')
|
||||
return (ft_putstr_fd(fd, va_arg(args, char *)));
|
||||
return (ft_putstr_fd_p(fd, va_arg(args, char *)));
|
||||
if (arg == '%')
|
||||
return (ft_putchar_fd(fd, '%'));
|
||||
if (arg == 'p')
|
||||
|
Binary file not shown.
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/06 17:41:04 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/10/12 15:22:40 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/02/03 12:53:45 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -18,10 +18,10 @@ int ft_dprintptr(int fd, void *ptr)
|
||||
int i;
|
||||
|
||||
if (ptr == NULL)
|
||||
return (ft_putstr_fd(fd, "(nil)"));
|
||||
return (ft_putstr_fd_p(fd, "(nil)"));
|
||||
address = (unsigned long) ptr;
|
||||
i = 2;
|
||||
ft_putstr_fd(fd, "0x");
|
||||
ft_putstr_fd_p(fd, "0x");
|
||||
i += ft_dprintul_base(fd, address, "0123456789abcdef");
|
||||
return (i);
|
||||
}
|
||||
|
Binary file not shown.
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/05 17:26:55 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/05 18:52:56 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/02/03 12:54:44 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -21,7 +21,7 @@ int ft_dprintstrtab(int fd, char **tab)
|
||||
index = 0;
|
||||
while (tab[index] != NULL)
|
||||
{
|
||||
i += ft_putstr_fd(fd, tab[index]) + 1;
|
||||
i += ft_putstr_fd_p(fd, tab[index]) + 1;
|
||||
ft_putchar_fd(fd, '\n');
|
||||
index++;
|
||||
}
|
||||
|
Binary file not shown.
Binary file not shown.
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/26 14:47:54 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/02/01 16:19:38 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/02/03 12:51:07 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -37,6 +37,6 @@ int ft_eprintf(const char *format, ...);
|
||||
int ft_vdprintf(int fd, const char *format, va_list va);
|
||||
|
||||
int ft_putchar_fd(int fd, char c);
|
||||
int ft_putstr_fd(int fd, char *str);
|
||||
int ft_putstr_fd_p(int fd, char *str);
|
||||
|
||||
#endif
|
||||
|
@ -6,13 +6,13 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/29 22:25:08 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/10/12 16:51:51 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/02/03 12:49:18 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_printf.h"
|
||||
|
||||
int ft_putstr_fd(int fd, char *str)
|
||||
int ft_putstr_fd_p(int fd, char *str)
|
||||
{
|
||||
int i;
|
||||
|
||||
|
Binary file not shown.
@ -5,12 +5,16 @@
|
||||
# include <sys/types.h>
|
||||
# include <sys/stat.h>
|
||||
# include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <readline/readline.h>
|
||||
#include <readline/history.h>
|
||||
|
||||
int ft_file_is_readable(char *path);
|
||||
int ft_file_is_writeable(char *path);
|
||||
char *ft_get_file_path(char *infile);
|
||||
int ft_infile(char *line);
|
||||
int ft_outfile(char *line);
|
||||
int ft_heredoc(char *stop);
|
||||
|
||||
typedef struct cmd
|
||||
{
|
||||
|
@ -13,7 +13,8 @@ static int ft_get_outfile(char *line)
|
||||
{
|
||||
if (line[i] == '>' && ft_is_in_quote(line, i) == 0)
|
||||
{
|
||||
if (line[i + 1] == '>')
|
||||
i++;
|
||||
if (line[i] == '>')
|
||||
{
|
||||
i++;
|
||||
append = 1;
|
||||
|
Loading…
Reference in New Issue
Block a user