merge
+ readline() replace gnl() in here_doc + "int error" in struct "t_cmd"
This commit is contained in:
@@ -1,77 +0,0 @@
|
||||
#include "minishell.h"
|
||||
|
||||
void close_fd(t_cmd *cmd)
|
||||
{
|
||||
if (cmd->fd_in != 0)
|
||||
close(cmd->fd_in);
|
||||
if (cmd->fd_out != 1)
|
||||
close(cmd->fd_out);
|
||||
}
|
||||
|
||||
void execute_cmd(char **envp, t_cmd **cmd_arr)
|
||||
{
|
||||
pid_t pid;
|
||||
pid_t wpid;
|
||||
int status;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while(cmd_arr[i])
|
||||
{
|
||||
pid = fork();
|
||||
if (pid == 0)
|
||||
{
|
||||
if (cmd_arr[i]->fd_in != 0)
|
||||
dup2(cmd_arr[i]->fd_in, STDIN_FILENO);
|
||||
if (cmd_arr[i]->fd_out != 1)
|
||||
dup2(cmd_arr[i]->fd_out, STDOUT_FILENO);
|
||||
//close_fd(cmd_arr[i]);
|
||||
//Must close all fds, not just the two cmd_arr[i] fd
|
||||
execve(cmd_arr[i]->argv[0], cmd_arr[i]->argv, envp);
|
||||
}
|
||||
else
|
||||
close_fd(cmd_arr[i]); // Close here or after all execve() for simplicity ?
|
||||
i++;
|
||||
}
|
||||
// waitpid pour la derniere commande (pour '$?')
|
||||
while ((wpid = wait(&status)) > 0);
|
||||
}
|
||||
|
||||
/*
|
||||
int handle_fdd(char *input, int fdin, t_cmd *cmd)
|
||||
{
|
||||
int *pipes_fd;
|
||||
int next_in;
|
||||
char *tmp;
|
||||
|
||||
cmd->fd_in = fdin;
|
||||
cmd->fd_out = 1;
|
||||
next_in = 0;
|
||||
if (input + 1)
|
||||
{
|
||||
pipes_fd = calloc(2, sizeof(int));
|
||||
pipe(pipes_fd);
|
||||
cmd->fd_out = pipes_fd[1];
|
||||
next_in = pipes_fd[0];
|
||||
}
|
||||
tmp = ft_strchr(input, '>');
|
||||
if (tmp)
|
||||
{
|
||||
tmp[0] = '\0';
|
||||
tmp = ft_strtrim(tmp + 2, " "); // +2 for "> "
|
||||
if (cmd->fd_out != 1)
|
||||
close(cmd->fd_out);
|
||||
cmd->fd_out = open(tmp, O_RDWR | O_TRUNC);
|
||||
}
|
||||
tmp = ft_strchr(input, '<');
|
||||
if (tmp)
|
||||
{
|
||||
tmp[0] = '\0';
|
||||
tmp = ft_strtrim(tmp + 2, " "); // +2 for "> "
|
||||
if (cmd->fd_in != 0)
|
||||
close(cmd->fd_in);
|
||||
cmd->fd_in = open(tmp, O_RDONLY);
|
||||
}
|
||||
printf(" handle_fd: %s\n", input);
|
||||
return (next_in);
|
||||
} */
|
||||
10
srcs/builtins/cd.c
Normal file
10
srcs/builtins/cd.c
Normal file
@@ -0,0 +1,10 @@
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
int builtin_cd(int argc, char *argv[], t_all *c)
|
||||
{
|
||||
(void)argc;
|
||||
(void)c;
|
||||
chdir(argv[1]);
|
||||
return (0);
|
||||
}
|
||||
50
srcs/builtins/export.c
Normal file
50
srcs/builtins/export.c
Normal file
@@ -0,0 +1,50 @@
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
int getenv_position(char **envp, char *name)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (envp[i] && ft_strncmp(envp[i], name, ft_strlen(name)))
|
||||
i++;
|
||||
if (!envp[i])
|
||||
return (-1);
|
||||
return (i);
|
||||
}
|
||||
|
||||
int builtin_export(int argc, char *argv[], t_all *c)
|
||||
{
|
||||
char **var;
|
||||
int position;
|
||||
|
||||
(void)argc;
|
||||
var = ft_split(argv[1], '=');
|
||||
position = getenv_position(c->envp, var[0]);
|
||||
if (position == -1 || !ft_strchr(argv[1], '='))
|
||||
{
|
||||
ft_free_2d_arr(var);
|
||||
return (0);
|
||||
}
|
||||
c->envp[position] = ft_strjoin(var[0], "=");
|
||||
if (var[1]) // parce que ft_strjoin return NULL si var[1] est null, pourquoi ?
|
||||
c->envp[position] = ft_strjoinfree_s1(c->envp[position], var[1]);
|
||||
ft_free_2d_arr(var);
|
||||
return (0);
|
||||
}
|
||||
|
||||
/*
|
||||
** comportement de bash :
|
||||
** 1. modifier une variable dans bash :
|
||||
** > ca ne la modifie pas dans zsh
|
||||
** > ca ne la garde pas apres exit de bash
|
||||
** 2. ajouter une variable dans bash :
|
||||
** > ca ne la modifie pas dans zsh
|
||||
** > ca ne la garde pas apres exit de bash
|
||||
** 3. ajouter une variable avec erreur :
|
||||
** > 'export VARIABLE' n'exporte rien
|
||||
** > 'export VARIABLE=' exporte une variable vide
|
||||
** 4. ordre d'insertion d'une nouvelle variable :
|
||||
** > aucune idee
|
||||
*/
|
||||
|
||||
15
srcs/builtins/pwd.c
Normal file
15
srcs/builtins/pwd.c
Normal file
@@ -0,0 +1,15 @@
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
int builtin_pwd(int argc, char *argv[], t_all *c)
|
||||
{
|
||||
char *pwd;
|
||||
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
(void)c;
|
||||
pwd = getcwd(NULL, 0);
|
||||
write(1, pwd, ft_strlen(pwd));
|
||||
write(1, "\n", 1);
|
||||
return (0);
|
||||
}
|
||||
23
srcs/builtins/unset.c
Normal file
23
srcs/builtins/unset.c
Normal file
@@ -0,0 +1,23 @@
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
int builtin_unset(int argc, char *argv[], t_all *c)
|
||||
{
|
||||
int position;
|
||||
|
||||
(void)argc;
|
||||
(void)c;
|
||||
position = getenv_position(c->envp, argv[1]);
|
||||
if (position == -1)
|
||||
return (0);
|
||||
while (c->envp[position])
|
||||
{
|
||||
write(1, "1", 1);
|
||||
free(c->envp[position]);
|
||||
c->envp[position] = ft_strdup(c->envp[position + 1]);
|
||||
position++;
|
||||
}
|
||||
write(1, "2", 1);
|
||||
free(c->envp[position]);
|
||||
return (0);
|
||||
}
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/10 23:53:17 by lperrey #+# #+# */
|
||||
/* Updated: 2021/11/16 08:03:30 by lperrey ### ########.fr */
|
||||
/* Updated: 2021/11/16 22:24:36 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -19,9 +19,8 @@ int free_exit(t_all *c, int exit_status)
|
||||
ft_lstclear((t_list **)&c->token_list, free); // a voir avec Hugo, il y a un truc qui me semble superflu dans la fonction
|
||||
ft_free_2d_arr(c->envp);
|
||||
free_pipeline(&c->cmd_arr);
|
||||
if (c->termios_changed)
|
||||
tcsetattr(STDIN_FILENO, TCSANOW, &c->ori_termios);
|
||||
gnl(STDIN_FILENO, NULL, 1);
|
||||
//if (c->termios_changed)
|
||||
// tcsetattr(STDIN_FILENO, TCSANOW, &c->ori_termios);
|
||||
rl_clear_history();
|
||||
exit(exit_status);
|
||||
}
|
||||
|
||||
10
srcs/init.c
10
srcs/init.c
@@ -6,7 +6,7 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/08 09:22:12 by lperrey #+# #+# */
|
||||
/* Updated: 2021/11/16 03:45:10 by lperrey ### ########.fr */
|
||||
/* Updated: 2021/11/16 21:06:18 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -30,13 +30,7 @@ int init(t_all *c, char *envp[])
|
||||
c->prompt = init_prompt(c->prompt_base);
|
||||
if (!c->prompt)
|
||||
return (ft_reti_perror(0, "init_prompt() error"));
|
||||
set_signals_handling(&c->ori_signal_behaviour, &c->signal_behaviour);
|
||||
if (isatty(STDIN_FILENO))
|
||||
{
|
||||
if (!set_terminal_attributes(&c->ori_termios, &c->interactive_termios,
|
||||
&c->termios_changed))
|
||||
return (ft_reti_perror(0, "set_terminal_attributes() error"));
|
||||
}
|
||||
set_signals_handling(&c->signal_behaviour);
|
||||
return (1);
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/24 10:52:40 by lperrey #+# #+# */
|
||||
/* Updated: 2021/11/16 08:02:56 by lperrey ### ########.fr */
|
||||
/* Updated: 2021/11/16 21:18:27 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -32,6 +32,7 @@ void save_redirections_words(t_token *t)
|
||||
t = t->next;
|
||||
}
|
||||
}
|
||||
|
||||
void print_cmd_array(t_cmd **cmd_arr)
|
||||
{
|
||||
int i;
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/11/11 18:46:43 by lperrey #+# #+# */
|
||||
/* Updated: 2021/11/14 11:12:44 by lperrey ### ########.fr */
|
||||
/* Updated: 2021/11/16 22:29:01 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -39,7 +39,6 @@ int here_doc(char *delimiter)
|
||||
if (!here_doc_write(delimiter, here_doc))
|
||||
{
|
||||
free(delimiter);
|
||||
gnl(STDIN_FILENO, NULL, 1);
|
||||
return (0);
|
||||
}
|
||||
free(delimiter);
|
||||
@@ -56,12 +55,24 @@ int here_doc(char *delimiter)
|
||||
static int here_doc_write(char *delimiter, int doc_fd)
|
||||
{
|
||||
char *line;
|
||||
size_t line_count;
|
||||
|
||||
line_count = 0;
|
||||
while (1)
|
||||
{
|
||||
line_count++;
|
||||
line = NULL;
|
||||
if (gnl(STDIN_FILENO, &line, 0) == -1)
|
||||
return (ft_reti_perror_free(0, line, free, "gnl() STDIN"));
|
||||
line = readline("> ");
|
||||
if (!line)
|
||||
{ // TODO : error print wrapper
|
||||
ft_putstr_fd("minishell: ", 2);
|
||||
ft_putstr_fd("warning: here-document at line ", 2);
|
||||
ft_putnbr_fd(line_count, 2);
|
||||
ft_putstr_fd(" delimited by end-of-file (wanted `", 2);
|
||||
ft_putstr_fd(delimiter, 2);
|
||||
ft_putstr_fd("')\n", 2);
|
||||
return (0);
|
||||
}
|
||||
if (ft_strncmp(line, delimiter, ft_strlen(line) + 1) == 0) // Ou ft_strlen(delimiter) + 1 ? Ça devrais être identique et ça peux se calculer une seul fois.
|
||||
break ;
|
||||
if (write(doc_fd, line, ft_strlen(line)) == -1)
|
||||
@@ -71,7 +82,6 @@ static int here_doc_write(char *delimiter, int doc_fd)
|
||||
free(line);
|
||||
}
|
||||
free(line);
|
||||
gnl(STDIN_FILENO, NULL, 1);
|
||||
return (1);
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/11/11 18:46:43 by lperrey #+# #+# */
|
||||
/* Updated: 2021/11/16 08:08:18 by lperrey ### ########.fr */
|
||||
/* Updated: 2021/11/16 22:29:52 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -24,7 +24,7 @@ int redirections(t_token *t, t_cmd **cmd_arr)
|
||||
i = 0;
|
||||
while (t)
|
||||
{
|
||||
if (cmd_arr[i]->pid == 0) // Pid var as error mark, WIP
|
||||
if (!cmd_arr[i]->error)
|
||||
{
|
||||
if (t->id == '<' || t->id == T_DLESS)
|
||||
{
|
||||
@@ -60,7 +60,7 @@ static int redirect_cmd_input(t_token *t, t_cmd *cmd)
|
||||
if (cmd->fd_in == -1)
|
||||
{
|
||||
ft_perror_io("open() ", t->next->content); // todo error
|
||||
cmd->pid = -1; // Pid var as error mark, WIP
|
||||
cmd->error = 42; // WIP error status
|
||||
}
|
||||
}
|
||||
else if (t->id == T_DLESS)
|
||||
@@ -93,7 +93,7 @@ static int redirect_cmd_output(t_token *t, t_cmd *cmd)
|
||||
if (cmd->fd_out == -1)
|
||||
{
|
||||
ft_perror_io("open() ", t->next->content);
|
||||
cmd->pid = -1; // Pid var as error mark, WIP
|
||||
cmd->error = 42; // WIP error status
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
@@ -6,18 +6,14 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */
|
||||
/* Updated: 2021/11/16 03:52:37 by lperrey ### ########.fr */
|
||||
/* Updated: 2021/11/16 21:22:32 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
//static char **tokens_list_to_argv(t_token *t); // temp test
|
||||
void sigint_handler(int signum); //tmp
|
||||
void sigquit_aka_eof_handler(int signum); //tmp
|
||||
|
||||
void close_fd(t_cmd *cmd);
|
||||
void execute_cmd(char **envp, t_cmd **cmd_arr);
|
||||
void execute_cmd(char **envp, t_cmd **cmd_arr, t_all *c);
|
||||
|
||||
void shell_loop(t_all *c)
|
||||
{
|
||||
@@ -45,11 +41,79 @@ void shell_loop(t_all *c)
|
||||
exec_cmd_line(c);
|
||||
}
|
||||
else if (!line_input)
|
||||
write(1, "\n", 1);
|
||||
{
|
||||
write(1, "exit\n", 5);
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void test_signal(t_all *c)
|
||||
// WIP HUGO
|
||||
void close_fd(t_cmd *cmd)
|
||||
{
|
||||
if (cmd->fd_in != 0)
|
||||
close(cmd->fd_in);
|
||||
if (cmd->fd_out != 1)
|
||||
close(cmd->fd_out);
|
||||
}
|
||||
|
||||
// WIP HUGO
|
||||
void execute_cmd(char **envp, t_cmd **cmd_arr, t_all *c)
|
||||
{
|
||||
pid_t pid;
|
||||
pid_t wpid;
|
||||
int wstatus;
|
||||
int i;
|
||||
int argc;
|
||||
|
||||
// put signal handling for SIGINT to ignore so parent process will not activate signal_handling_executiv while childs are in process
|
||||
c->signal_behaviour.sa_handler = SIG_IGN;
|
||||
sigaction(SIGINT, &c->signal_behaviour, NULL);
|
||||
i = 0;
|
||||
while(cmd_arr[i])
|
||||
{
|
||||
pid = fork();
|
||||
if (pid == 0)
|
||||
{
|
||||
// activate singal handling for execution mode
|
||||
c->signal_behaviour.sa_handler = sigint_handler_execution;
|
||||
sigaction(SIGINT, &c->signal_behaviour, NULL);
|
||||
if (cmd_arr[i]->fd_in != 0)
|
||||
dup2(cmd_arr[i]->fd_in, STDIN_FILENO);
|
||||
if (cmd_arr[i]->fd_out != 1)
|
||||
dup2(cmd_arr[i]->fd_out, STDOUT_FILENO);
|
||||
close_fd(cmd_arr[i]);
|
||||
if (cmd_arr[i]->builtin_func)
|
||||
{
|
||||
argc = 0;
|
||||
while (cmd_arr[i]->argv[argc])
|
||||
argc++;
|
||||
cmd_arr[i]->builtin_func(argc, cmd_arr[i]->argv, c);
|
||||
exit(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
write(1, "1", 1);
|
||||
execve(cmd_arr[i]->argv[0], cmd_arr[i]->argv, envp);
|
||||
}
|
||||
}
|
||||
else
|
||||
close_fd(cmd_arr[i]);
|
||||
i++;
|
||||
}
|
||||
// waitpid pour la derniere commande (pour '$?')
|
||||
wpid = 1;
|
||||
while (wpid > 0)
|
||||
wpid = wait(&wstatus);
|
||||
// to print a \n after execve was terminated by a signal
|
||||
if (WIFSIGNALED(wstatus))
|
||||
write(1, "\n", 1);
|
||||
// put signal handling for sigint back to the signal handler for interactiv mode
|
||||
c->signal_behaviour.sa_handler = sigint_handler_interactiv;
|
||||
sigaction(SIGINT, &c->signal_behaviour, NULL);
|
||||
}
|
||||
|
||||
/* void test_signal(t_all *c)
|
||||
{
|
||||
// TEMP
|
||||
// A faire aprés être sortie du mode interactif
|
||||
@@ -80,4 +144,4 @@ void test_signal(t_all *c)
|
||||
sigaction(SIGQUIT, &c->signal_behaviour, NULL);
|
||||
tcsetattr(STDIN_FILENO, TCSANOW, &c->interactive_termios);
|
||||
}
|
||||
}
|
||||
} */
|
||||
|
||||
@@ -6,79 +6,33 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/23 18:56:53 by lperrey #+# #+# */
|
||||
/* Updated: 2021/10/30 14:28:08 by lperrey ### ########.fr */
|
||||
/* Updated: 2021/11/15 20:08:26 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
void sigint_handler(int signum)
|
||||
void sigint_handler_interactiv(int signum)
|
||||
{
|
||||
// Comment virer LE ^D De l'affichage ? Il ne fait pas partie de "rl_line_buffer".
|
||||
if (rl_line_buffer && *rl_line_buffer)
|
||||
{
|
||||
return ;
|
||||
}
|
||||
else
|
||||
{
|
||||
free_exit(g_all, g_all->last_exit_status);
|
||||
}
|
||||
return ;
|
||||
}
|
||||
|
||||
/* void sigquit_aka_eof_handler(int signum)
|
||||
{
|
||||
//ft_putstr_fd("TESTS\n", 1);
|
||||
ft_putstr_fd("\n", 1);
|
||||
(void)signum;
|
||||
write(1, "\n", 1);
|
||||
rl_replace_line("", 1);
|
||||
rl_on_new_line();
|
||||
rl_redisplay();
|
||||
return ;
|
||||
} */
|
||||
|
||||
int set_signals_handling(struct sigaction *ori_signal_behaviour,
|
||||
struct sigaction *signal_behaviour)
|
||||
{
|
||||
ori_signal_behaviour->sa_handler = SIG_DFL;
|
||||
|
||||
/* ctrl-D exit the shell.
|
||||
eof = ^D; */
|
||||
signal_behaviour->sa_handler = sigint_handler;
|
||||
sigaction(SIGINT, signal_behaviour, NULL);
|
||||
|
||||
/* ctrl-\ do nothing.
|
||||
quit = ^\; */
|
||||
signal_behaviour->sa_handler = SIG_IGN;
|
||||
//signal_behaviour->sa_handler = sigquit_aka_eof_handler;
|
||||
sigaction(SIGQUIT, signal_behaviour, NULL);
|
||||
|
||||
/*
|
||||
** remap (^D to ^C) and (^C to ^D) in terminal
|
||||
** ^D is now "SIGINT" (handle here)
|
||||
** ^C is now EOF (handle in shell_loop())
|
||||
*/
|
||||
return (1);
|
||||
}
|
||||
|
||||
/*
|
||||
ctrl-C print a new prompt on a newline.
|
||||
intr = ^C;
|
||||
ctrl-D exit the shell.
|
||||
eof = ^D;
|
||||
ctrl-\ do nothing.
|
||||
quit = ^\;
|
||||
*/
|
||||
void sigint_handler_execution(int signum)
|
||||
{
|
||||
(void)signum;
|
||||
write(1, "\n", 1);
|
||||
// exit(0);
|
||||
}
|
||||
|
||||
/*
|
||||
speed 38400 baud; rows 22; columns 90; line = 0;
|
||||
erase = ^?; kill = ^U;
|
||||
eol = M-^?; eol2 = M-^?;
|
||||
swtch = M-^?; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V;
|
||||
discard = ^O; min = 1; time = 0;
|
||||
-parenb -parodd -cmspar cs8 hupcl -cstopb cread -clocal -crtscts
|
||||
-ignbrk brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc ixany
|
||||
imaxbel iutf8
|
||||
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
|
||||
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke
|
||||
-flusho -extproc
|
||||
*/
|
||||
int set_signals_handling(struct sigaction *signal_behaviour)
|
||||
{
|
||||
signal_behaviour->sa_handler = sigint_handler_interactiv;
|
||||
sigaction(SIGINT, signal_behaviour, NULL);
|
||||
signal_behaviour->sa_handler = SIG_IGN;
|
||||
sigaction(SIGQUIT, signal_behaviour, NULL);
|
||||
return (1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user