Browse Source

Some functions refactoring and use handle_status on last cmd only

master
narnaud 3 years ago
parent
commit
0390007bfb
  1. 3
      caller.c
  2. 65
      minishell.c
  3. 6
      minishell.h
  4. 28
      utils.c

3
caller.c

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/03 11:48:16 by narnaud #+# #+# */ /* Created: 2022/05/03 11:48:16 by narnaud #+# #+# */
/* Updated: 2022/05/06 11:22:52 by narnaud ### ########.fr */ /* Updated: 2022/05/06 12:14:27 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -107,6 +107,7 @@ int piper(t_datas *datas, t_command *cmd)
if (cmd->next) if (cmd->next)
piper(datas, cmd->next); piper(datas, cmd->next);
waitpid(pid, &status, 0); waitpid(pid, &status, 0);
if (!cmd->next)
handle_status(datas, status); handle_status(datas, status);
if (cmd->fd[0]) if (cmd->fd[0])
close(cmd->fd[0]); close(cmd->fd[0]);

65
minishell.c

@ -6,16 +6,59 @@
/* By: mea <marvin@42.fr> +#+ +:+ +#+ */ /* By: mea <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 12:14:09 by narnaud #+# #+# */ /* Created: 2022/05/02 12:14:09 by narnaud #+# #+# */
/* Updated: 2022/05/06 11:23:19 by narnaud ### ########.fr */ /* Updated: 2022/05/06 12:03:13 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
# include "minishell.h" # include "minishell.h"
int main(int argc, char **argv, char **envp)
void termios(int ctl)
{ {
struct termios termios_p;
int tty;
tty = ttyslot();
if (tcgetattr(tty, &termios_p) < 0)
exit ((perror("stdin"), EXIT_FAILURE));
if (ctl)
{
termios_p.c_lflag |= ECHOCTL;
signal(SIGINT, nothing);
signal(SIGQUIT, nothing);
}
else
{
termios_p.c_lflag &= ~(ECHOCTL);
signal(SIGINT, sigs_handler);
signal(SIGQUIT, sigs_handler);
}
termios_p.c_cc[VQUIT] = ctl * 28;
tcsetattr(tty, TCSANOW, &termios_p);
}
void prompt(t_datas *datas)
{
char *line;
t_command *cmd; t_command *cmd;
termios(0);
line = readline("$ ");
termios(1);
if (line == NULL)
halt(EXIT_FAILURE);
if (is_empty(line))
return ;
cmd = parser(datas, lexer(datas, line), NULL);
if (!cmd->prev && !cmd->next && is_builtin(cmd->argv[0]))
builtin_call(datas, cmd);
else
piper(datas, cmd);
add_history(line);
}
int main(int argc, char **argv, char **envp)
{
t_datas datas; t_datas datas;
char *line;
int i; int i;
i = 0; i = 0;
@ -26,21 +69,7 @@ int main(int argc, char **argv, char **envp)
if (argc < 2) if (argc < 2)
(void)argv; (void)argv;
while (1) while (1)
{ prompt(&datas);
termios(0);
line = readline("$ ");
termios(1);
if (line == NULL)
halt(EXIT_FAILURE);
if (is_empty(line))
continue ;
cmd = parser(&datas, lexer(&datas, line), NULL);
if (!cmd->prev && !cmd->next && is_builtin(cmd->argv[0]))
builtin_call(&datas, cmd);
else
piper(&datas, cmd);
add_history(line);
}
clear_history(); clear_history();
return (EXIT_SUCCESS); return (EXIT_SUCCESS);
} }

6
minishell.h

@ -6,7 +6,7 @@
/* By: mea <marvin@42.fr> +#+ +:+ +#+ */ /* By: mea <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 13:50:44 by narnaud #+# #+# */ /* Created: 2022/05/02 13:50:44 by narnaud #+# #+# */
/* Updated: 2022/05/06 11:38:29 by narnaud ### ########.fr */ /* Updated: 2022/05/06 11:49:32 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -50,8 +50,8 @@ typedef struct s_datas
// ----------------------------------Utils.c // ----------------------------------Utils.c
int is_empty(char *line); int is_empty(char *line);
void halt(int ret_code); void halt(int ret_code);
void termios(int ctl);
void sigs_handler(int sig_num); void sigs_handler(int sig_num);
void nothing(int sig_num);
void handle_status(t_datas *datas, int status); void handle_status(t_datas *datas, int status);
// ----------------------------------Builtins.c // ----------------------------------Builtins.c
@ -74,7 +74,6 @@ pid_t caller(t_datas *datas, t_command *cmd);
int piper(t_datas *datas, t_command *cmd); int piper(t_datas *datas, t_command *cmd);
// ----------------------------------Parser.c // ----------------------------------Parser.c
typedef enum e_type typedef enum e_type
{ {
WORD, WORD,
@ -97,7 +96,6 @@ void update_redir(t_datas *datas, t_command *cmd, t_token *tok);
t_command *parser(t_datas *datas, t_token *tok, t_command *prev); t_command *parser(t_datas *datas, t_token *tok, t_command *prev);
// ----------------------------------Lexer.c // ----------------------------------Lexer.c
typedef enum e_state typedef enum e_state
{ {
OLD_ST, OLD_ST,

28
utils.c

@ -6,7 +6,7 @@
/* By: mea <marvin@42.fr> +#+ +:+ +#+ */ /* By: mea <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/03 08:57:53 by narnaud #+# #+# */ /* Created: 2022/05/03 08:57:53 by narnaud #+# #+# */
/* Updated: 2022/05/06 11:39:05 by narnaud ### ########.fr */ /* Updated: 2022/05/06 12:13:48 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -55,29 +55,3 @@ void halt(int ret_code)
printf("exit\n"); printf("exit\n");
exit(ret_code); exit(ret_code);
} }
void termios(int ctl)
{
struct termios termios_p;
int tty;
tty = ttyslot();
if (tcgetattr(tty, &termios_p) < 0)
exit ((perror("stdin"), EXIT_FAILURE));
if (ctl)
{
termios_p.c_lflag |= ECHOCTL;
signal(SIGINT, nothing);
signal(SIGQUIT, nothing);
}
else
{
termios_p.c_lflag &= ~(ECHOCTL);
signal(SIGINT, sigs_handler);
signal(SIGQUIT, sigs_handler);
}
termios_p.c_cc[VQUIT] = ctl * 28;
tcsetattr(tty, TCSANOW, &termios_p);
}

Loading…
Cancel
Save