Skip to content

Commit

Permalink
signals in minishell lvl 2
Browse files Browse the repository at this point in the history
  • Loading branch information
emSali committed Aug 12, 2023
1 parent 7abb0d2 commit 52c473d
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 25 deletions.
2 changes: 1 addition & 1 deletion commands/env.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: esali <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/12 16:57:35 by simao #+# #+# */
/* Updated: 2023/08/08 11:25:23 by esali ### ########.fr */
/* Updated: 2023/08/12 18:08:29 by esali ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down
11 changes: 6 additions & 5 deletions commands/exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: esali <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/12 16:57:31 by simao #+# #+# */
/* Updated: 2023/08/12 16:44:45 by esali ### ########.fr */
/* Updated: 2023/08/12 18:29:08 by esali ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -48,8 +48,9 @@ int calc_exit(int exit)
}

/* Should release all allocated resources before exiting,
if is_cmd == 5 -> exit has more than 1 argument*/
void cmd_exit(char *exit_nr, int is_cmd)
if is_cmd == 5 -> exit has more than 1 argument
is_exit: */
void cmd_exit(char *exit_nr, int is_exit)
{
int number;

Expand All @@ -60,11 +61,11 @@ void cmd_exit(char *exit_nr, int is_cmd)
if (get_data()->envp)
free_keys(get_data()->envp);
free_env();
if (is_cmd)
if (is_exit)
ft_printf("exit\n");
if (has_alpha(exit_nr))
ft_printf("exit: %s: numeric argument required\n", exit_nr);
else if (is_cmd == 5)
else if (is_exit == 5)
{
ft_printf("exit: too many arguments\n");
number = 1;
Expand Down
6 changes: 3 additions & 3 deletions commands/export.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: esali <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/14 12:54:51 by simao #+# #+# */
/* Updated: 2023/08/12 16:08:19 by esali ### ########.fr */
/* Updated: 2023/08/12 17:44:49 by esali ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -39,9 +39,9 @@ void print_sorted_env(t_env *sorted_temp)
while (tmp)
{
if (tmp->value)
printf("declare -x %s=%s\n", tmp->key, tmp->value);
ft_printf("declare -x %s=%s\n", tmp->key, tmp->value);
else
printf("declare -x %s\n", tmp->key);
ft_printf("declare -x %s\n", tmp->key);
tmp = tmp->nxt;
}
}
Expand Down
7 changes: 5 additions & 2 deletions executor/execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: esali <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/18 18:12:49 by smagalha #+# #+# */
/* Updated: 2023/08/12 17:12:46 by esali ### ########.fr */
/* Updated: 2023/08/12 18:45:07 by esali ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -39,7 +39,9 @@ void execute_input(t_list *node, char **envp)
execute_builtin(node);
else
{
get_data()->executing_cmd = 0;
get_data()->executing_cmd = 1;
if (!ft_strncmp("./minishell", node->token[0], 12))
get_data()->mini_inception = 1;
pid1 = fork();
if (pid1 == 0)
{
Expand All @@ -54,6 +56,7 @@ void execute_input(t_list *node, char **envp)
if (WIFEXITED(status))
get_data()->exit = WEXITSTATUS(status);
get_data()->executing_cmd = 0;
get_data()->mini_inception = 0;
}
}

Expand Down
3 changes: 2 additions & 1 deletion minishell.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: esali <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/25 17:45:31 by esali #+# #+# */
/* Updated: 2023/08/12 17:32:54 by esali ### ########.fr */
/* Updated: 2023/08/12 18:43:48 by esali ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -17,6 +17,7 @@ int main(int argc, char **argv, char **env)
if (argc != 1 || argv[1] != NULL)
return (0);
create_env(env);
get_data()->executing_cmd = 0;
increase_shlvl();
get_data()->exit = 0;
get_pipe()->stdin = dup(STDIN_FILENO);
Expand Down
5 changes: 3 additions & 2 deletions minishell.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: esali <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/20 17:16:06 by esali #+# #+# */
/* Updated: 2023/08/12 16:49:55 by esali ### ########.fr */
/* Updated: 2023/08/12 18:39:49 by esali ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -68,6 +68,7 @@ typedef struct s_data
int executing_cmd;
int pid;
int log_fd;
int mini_inception;
} t_data;

/* Builtin Commands */
Expand All @@ -78,7 +79,7 @@ void cmd_echo(char **line);
void cmd_env(char **token);
void cmd_export(char **variable);
void cmd_unset(char **variable);
void cmd_exit(char *number, int is_cmd);
void cmd_exit(char *number, int is_exit);
t_env *sort_loop(t_env *dup_env, t_env *sorted_env);
void new_env(char *key, char *value);

Expand Down
22 changes: 14 additions & 8 deletions signal_handlers/sig_handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: esali <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/12 22:54:29 by simao #+# #+# */
/* Updated: 2023/08/08 14:45:25 by esali ### ########.fr */
/* Updated: 2023/08/12 18:48:11 by esali ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -16,8 +16,11 @@ void handle_sigint(int sig)
{
if (get_data()->executing_cmd)
{
get_data()->exit = 127 + sig;
ft_printf("\n");
if (!get_data()->mini_inception)
{
get_data()->exit = 127 + sig;
ft_printf("\n");
}
}
else
{
Expand All @@ -37,11 +40,14 @@ void handle_sigquit(int sig)
{
if (get_data()->executing_cmd)
{
get_data()->exit = 127 + sig;
ft_printf("\b \b");
ft_printf("\b \b");
ft_printf("Quit (core dumped)\n");
cmd_exit(ft_itoa(get_data()->exit), 0);
if (!get_data()->mini_inception)
{
get_data()->exit = 127 + sig;
ft_printf("\b \b");
ft_printf("\b \b");
ft_printf("Quit (core dumped)\n");
cmd_exit(ft_itoa(get_data()->exit), 0);
}
}
else
{
Expand Down
3 changes: 2 additions & 1 deletion utils/export_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: esali <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/01 19:44:27 by esali #+# #+# */
/* Updated: 2023/08/12 16:08:26 by esali ### ########.fr */
/* Updated: 2023/08/12 17:50:18 by esali ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -44,6 +44,7 @@ void new_env(char *key, char *value)
{
t_env *lst;

ft_printf("new_env\n");
lst = get_env();
while (lst->nxt != NULL)
lst = lst->nxt;
Expand Down
3 changes: 1 addition & 2 deletions utils/shlvl.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: esali <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/12 15:06:00 by esali #+# #+# */
/* Updated: 2023/08/12 17:22:42 by esali ### ########.fr */
/* Updated: 2023/08/12 18:08:36 by esali ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -46,7 +46,6 @@ void increase_shlvl(void)
char *env;

env = ft_getenv("SHLVL");
ft_printf("env: %s", env);
if (!env)
shlvl = 1;
if (is_digit(env) == 0)
Expand Down

0 comments on commit 52c473d

Please sign in to comment.