add: read input
This commit is contained in:
parent
fa37938073
commit
daaa9dea51
3
Makefile
3
Makefile
@ -28,7 +28,8 @@ fclean: clean
|
||||
make -C libftx fclean
|
||||
rm -f ${NAME}
|
||||
|
||||
re: fclean all
|
||||
re: fclean
|
||||
make all
|
||||
|
||||
.PHONY: all clean fclean re
|
||||
|
||||
|
2
cmds.c
2
cmds.c
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/15 14:17:26 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/02/17 13:09:13 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/02/17 15:20:29 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -106,7 +106,9 @@ int ft_cmds_executor(t_list **cmds, t_list **env)
|
||||
content->executable = ft_get_executable_path(content->executable, env);
|
||||
if (content->executable != NULL)
|
||||
ft_excutor(content, env);
|
||||
if (content->fd_in != 0)
|
||||
close(content->fd_in);
|
||||
if (content->fd_out != 1 && content->fd_out != 2)
|
||||
close(content->fd_out);
|
||||
current = current->next;
|
||||
}
|
||||
|
15
heredoc.c
15
heredoc.c
@ -1,3 +1,16 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* heredoc.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/17 15:36:26 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/02/17 16:21:02 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libftx/libftx.h"
|
||||
#include "minishell.h"
|
||||
|
||||
int ft_heredoc(char *stop)
|
||||
@ -14,7 +27,7 @@ int ft_heredoc(char *stop)
|
||||
free(line);
|
||||
break ;
|
||||
}
|
||||
ft_putstr_fd(line, fds[1]);
|
||||
ft_putendl_fd(line, fds[1]);
|
||||
free(line);
|
||||
line = readline("> ");
|
||||
}
|
||||
|
@ -6,13 +6,13 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/29 22:23:35 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/05 18:54:54 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/02/17 16:23:01 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
void ft_putchar_fd(int fd, char c)
|
||||
void ft_putchar_fd(char c, int fd)
|
||||
{
|
||||
write(fd, &c, 1);
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/29 22:26:36 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/09/29 22:43:19 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/02/17 16:15:07 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/26 14:47:54 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/06 19:34:13 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/02/17 16:22:44 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -47,7 +47,7 @@ char **ft_split(char const *s, char c);
|
||||
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_putchar_fd(char c, int fd);
|
||||
void ft_putstr_fd(char *s, int fd);
|
||||
void ft_putendl_fd(char *s, int fd);
|
||||
void ft_putnbr_fd(int n, int fd);
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/26 14:47:54 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/02/03 12:42:13 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/02/17 16:24:42 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -69,10 +69,10 @@ char **ft_split(char const *s, char c);
|
||||
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_putchar_fd(char c, int fd);
|
||||
void ft_putstr_fd(char *s, int fd);
|
||||
void ft_putendl_fd(int fd, char *s);
|
||||
void ft_putnbr_fd(int fd, int n);
|
||||
void ft_putendl_fd(char *s, int fd);
|
||||
void ft_putnbr_fd(int n, int fd);
|
||||
|
||||
typedef struct s_list
|
||||
{
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/23 18:08:31 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/02/03 12:52:38 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/02/17 16:29:56 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -23,13 +23,13 @@ int ft_dprintarg(int fd, int arg, va_list args)
|
||||
if (arg == 'u')
|
||||
return (ft_dprintul(fd, va_arg(args, unsigned int)));
|
||||
if (arg == 'c')
|
||||
return (ft_putchar_fd(fd, va_arg(args, int)));
|
||||
return (ft_putchar_fd_p(fd, va_arg(args, int)));
|
||||
if (arg == 'S')
|
||||
return (ft_dprintstrtab(fd, va_arg(args, char **)));
|
||||
if (arg == 's')
|
||||
return (ft_putstr_fd_p(fd, va_arg(args, char *)));
|
||||
if (arg == '%')
|
||||
return (ft_putchar_fd(fd, '%'));
|
||||
return (ft_putchar_fd_p(fd, '%'));
|
||||
if (arg == 'p')
|
||||
return (ft_dprintptr(fd, va_arg(args, void *)));
|
||||
return (0);
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/10 03:26:21 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/10/21 15:54:31 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/02/17 16:30:25 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -23,7 +23,7 @@ int ft_dprintl_base(int fd, long long int n, char *base)
|
||||
if (n < 0)
|
||||
{
|
||||
nb = -n;
|
||||
i += ft_putchar_fd(fd, '-');
|
||||
i += ft_putchar_fd_p(fd, '-');
|
||||
}
|
||||
else
|
||||
nb = n;
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/05 17:26:55 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/02/03 12:54:44 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/02/17 16:30:44 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -22,7 +22,7 @@ int ft_dprintstrtab(int fd, char **tab)
|
||||
while (tab[index] != NULL)
|
||||
{
|
||||
i += ft_putstr_fd_p(fd, tab[index]) + 1;
|
||||
ft_putchar_fd(fd, '\n');
|
||||
ft_putchar_fd_p(fd, '\n');
|
||||
index++;
|
||||
}
|
||||
return (i);
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/29 13:49:45 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/10/23 14:23:44 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/02/17 16:31:15 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -73,9 +73,9 @@ int ft_dprintul_base(int fd, unsigned long long n, char *base)
|
||||
if (n > base_size - 1)
|
||||
{
|
||||
ft_dprintul_base(fd, n / base_size, base);
|
||||
ft_putchar_fd(fd, base[n % base_size]);
|
||||
ft_putchar_fd_p(fd, base[n % base_size]);
|
||||
}
|
||||
else
|
||||
ft_putchar_fd(fd, base[n]);
|
||||
ft_putchar_fd_p(fd, base[n]);
|
||||
return (str_size - 1);
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/26 14:47:54 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/02/03 12:51:07 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/02/17 16:29:18 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -36,7 +36,7 @@ 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_putchar_fd_p(int fd, char c);
|
||||
int ft_putstr_fd_p(int fd, char *str);
|
||||
|
||||
#endif
|
||||
|
@ -6,13 +6,13 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/29 22:23:35 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/10/12 15:21:42 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/02/17 16:28:59 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_printf.h"
|
||||
|
||||
int ft_putchar_fd(int fd, char c)
|
||||
int ft_putchar_fd_p(int fd, char c)
|
||||
{
|
||||
write(fd, &c, 1);
|
||||
return (1);
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/29 22:25:08 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/02/03 12:49:18 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/02/17 16:28:25 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -20,6 +20,6 @@ int ft_putstr_fd_p(int fd, char *str)
|
||||
str = "(null)";
|
||||
i = 0;
|
||||
while (str[i] != '\0')
|
||||
ft_putchar_fd(fd, str[i++]);
|
||||
ft_putchar_fd_p(fd, str[i++]);
|
||||
return (i);
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/12 14:34:28 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/12/11 19:23:34 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/02/17 16:31:29 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -19,7 +19,7 @@ static int ft_dprintseg(int fd, const char *str)
|
||||
i = 0;
|
||||
while (str[i] != '\0' && ft_skipflag(str + i) == 0)
|
||||
{
|
||||
ft_putchar_fd(fd, str[i]);
|
||||
ft_putchar_fd_p(fd, str[i]);
|
||||
i++;
|
||||
}
|
||||
return (i);
|
||||
|
152
main.c
152
main.c
@ -6,18 +6,84 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/16 15:16:14 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/02/17 13:13:19 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/02/17 16:06:02 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libftx/libftx.h"
|
||||
#include "minishell.h"
|
||||
|
||||
static char *ft_get_user_input(t_list **env)
|
||||
{
|
||||
char *line;
|
||||
char *prompt;
|
||||
|
||||
prompt = ft_strmerger(2, get_value_by_key("PWD", env), "$ ");
|
||||
if (prompt == NULL)
|
||||
{
|
||||
ft_eprintf("minishell: malloc failed\n");
|
||||
return (NULL);
|
||||
}
|
||||
line = readline(prompt);
|
||||
add_history(line);
|
||||
free(prompt);
|
||||
return (line);
|
||||
}
|
||||
|
||||
static int ft_minishell(t_list **env, char *line)
|
||||
{
|
||||
t_list **cmds;
|
||||
char *line_clean;
|
||||
int infile;
|
||||
int outfile;
|
||||
|
||||
line_clean = ft_normalizer(line);
|
||||
if (line_clean == NULL)
|
||||
return (1);
|
||||
if (ft_syntatic_verif(line_clean))
|
||||
{
|
||||
free(line_clean);
|
||||
return (1);
|
||||
}
|
||||
outfile = ft_outfile(line_clean);
|
||||
if (outfile == -2)
|
||||
{
|
||||
free(line_clean);
|
||||
return (1);
|
||||
}
|
||||
infile = ft_infile(line_clean);
|
||||
if (infile == -2)
|
||||
{
|
||||
close(outfile);
|
||||
free(line_clean);
|
||||
return (1);
|
||||
}
|
||||
cmds = ft_parse_cmds(line_clean, env, infile, outfile);
|
||||
if (cmds == NULL)
|
||||
{
|
||||
close(outfile);
|
||||
close(infile);
|
||||
ft_lstclear(cmds, ft_cmddel);
|
||||
free(cmds);
|
||||
free(line_clean);
|
||||
return (1);
|
||||
}
|
||||
if (ft_cmds_executor(cmds, env) == 1)
|
||||
{
|
||||
close(outfile);
|
||||
close(infile);
|
||||
ft_lstclear(cmds, ft_cmddel);
|
||||
free(cmds);
|
||||
free(line_clean);
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
|
||||
int main(int ac, char **av, char **env)
|
||||
{
|
||||
t_list **cmds;
|
||||
char *line;
|
||||
int infile;
|
||||
int outfile;
|
||||
t_data data;
|
||||
|
||||
if (ac == 1)
|
||||
@ -25,64 +91,60 @@ int main(int ac, char **av, char **env)
|
||||
data.env = init_env(env);
|
||||
if (data.env == NULL)
|
||||
return (1);
|
||||
line = ft_normalizer(av[1]);
|
||||
free(data.env);
|
||||
return (1);
|
||||
}
|
||||
if (ft_minishell(data.env, av[1]) == 1)
|
||||
{
|
||||
ft_lstclear(data.env, env_del);
|
||||
free(data.env);
|
||||
return (1);
|
||||
}
|
||||
ft_lstclear(data.env, env_del);
|
||||
free(data.env);
|
||||
free(line);
|
||||
return (0);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
|
||||
|
||||
int main(int ac, char **av, char **env)
|
||||
{
|
||||
t_data data;
|
||||
char *line;
|
||||
|
||||
data.env = init_env(env);
|
||||
if (data.env == NULL)
|
||||
return (1);
|
||||
line = ft_get_user_input(data.env);
|
||||
if (line == NULL)
|
||||
{
|
||||
ft_lstclear(data.env, env_del);
|
||||
free(data.env);
|
||||
return (1);
|
||||
}
|
||||
if (ft_syntatic_verif(line) == 1)
|
||||
while (line != NULL)
|
||||
{
|
||||
if (ft_minishell(data.env, line) == 1)
|
||||
{
|
||||
ft_lstclear(data.env, env_del);
|
||||
free(data.env);
|
||||
free(line);
|
||||
return (1);
|
||||
}
|
||||
outfile = ft_outfile(line);
|
||||
if (outfile == -2)
|
||||
free(line);
|
||||
line = ft_get_user_input(data.env);
|
||||
if (line == NULL)
|
||||
{
|
||||
ft_lstclear(data.env, env_del);
|
||||
free(data.env);
|
||||
free(line);
|
||||
return (1);
|
||||
}
|
||||
infile = ft_infile(line);
|
||||
if (infile == -2)
|
||||
{
|
||||
close(outfile);
|
||||
ft_lstclear(data.env, env_del);
|
||||
free(data.env);
|
||||
free(line);
|
||||
return (1);
|
||||
}
|
||||
cmds = ft_parse_cmds(line, data.env, infile, outfile);
|
||||
if (cmds == NULL)
|
||||
{
|
||||
close(outfile);
|
||||
close(infile);
|
||||
ft_lstclear(data.env, env_del);
|
||||
ft_lstclear(cmds, ft_cmddel);
|
||||
free(cmds);
|
||||
free(data.env);
|
||||
free(line);
|
||||
return (1);
|
||||
}
|
||||
if (ft_cmds_executor(cmds, data.env) == 1)
|
||||
{
|
||||
close(outfile);
|
||||
close(infile);
|
||||
ft_lstclear(data.env, env_del);
|
||||
ft_lstclear(cmds, ft_cmddel);
|
||||
free(cmds);
|
||||
free(data.env);
|
||||
free(line);
|
||||
return (1);
|
||||
}
|
||||
ft_lstclear(data.env, env_del);
|
||||
ft_lstclear(cmds, ft_cmddel);
|
||||
free(cmds);
|
||||
free(data.env);
|
||||
free(line);
|
||||
return (0);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/14 13:45:30 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/02/17 13:26:53 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/02/17 14:29:56 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -21,7 +21,7 @@
|
||||
# include <stdio.h>
|
||||
# include <readline/readline.h>
|
||||
# include <readline/history.h>
|
||||
|
||||
# define DEBUG 0
|
||||
t_list **init_env(char **env);
|
||||
int set_value_by_key(char *key, char *value, t_list **env);
|
||||
char *get_value_by_key(char *key, t_list **head);
|
||||
|
Loading…
Reference in New Issue
Block a user