Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/signals' into rework_errors
Browse files Browse the repository at this point in the history
  • Loading branch information
LithiumOx committed Nov 22, 2023
2 parents 8c01a5d + 4b9bfdf commit 50a47d8
Show file tree
Hide file tree
Showing 13 changed files with 184 additions and 77 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ SRC = main \
lexer/index \
expander/index \
expander/expand_env \
expander/expand_quotes
expander/expand_quotes \
signals/index

SRCS = $(addsuffix .c, $(addprefix src/, $(SRC)))
OBJS = $(patsubst src/%.c, build/%.o, $(SRCS))
Expand Down
10 changes: 9 additions & 1 deletion includes/minishell.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: mdekker/jde-baai <team@codam.nl> +#+ */
/* +#+ */
/* Created: 2023/07/09 21:25:59 by mdekker #+# #+# */
/* Updated: 2023/11/21 18:15:24 by mdekker ######## odam.nl */
/* Updated: 2023/11/22 14:20:29 by mdekker ######## odam.nl */
/* */
/* ************************************************************************** */

Expand All @@ -18,6 +18,7 @@
# include <libft.h>
# include <readline/history.h>
# include <readline/readline.h>
# include <signal.h>
# include <stdarg.h>
# include <stdbool.h>
# include <stdio.h>
Expand Down Expand Up @@ -102,6 +103,13 @@ size_t **return_sorted_arr(t_vector *env);
void ft_free_size_t(size_t **arr, size_t len);
char *string_handler(char *input);

/* signals */
void signal_main(int signal_num);
void setup_hdoc_signals(void);
void signal_hdoc(int signal_num);
void setup_child_signals(void);
void signal_child(int signal_num);

/* structs */
t_token *create_token(char *value, t_types type);
void clear_token(void *data);
Expand Down
3 changes: 2 additions & 1 deletion src/exec/create_processes.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: mdekker/jde-baai <team@codam.nl> +#+ */
/* +#+ */
/* Created: 2023/08/19 12:40:07 by mdekker/jde #+# #+# */
/* Updated: 2023/11/18 23:22:14 by mdekker/jde ######## odam.nl */
/* Updated: 2023/11/22 14:13:18 by mdekker ######## odam.nl */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -83,6 +83,7 @@ bool create_processes(t_shell *data)
t_vector *group_vec;
t_group *group;

setup_child_signals();
group_vec = &data->exec->group_vec;
if (group_vec->length == 1)
{
Expand Down
3 changes: 2 additions & 1 deletion src/exec/exec_process.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: mdekker/jde-baai <team@codam.nl> +#+ */
/* +#+ */
/* Created: 2023/08/19 16:08:08 by mdekker/jde #+# #+# */
/* Updated: 2023/11/18 23:20:40 by mdekker/jde ######## odam.nl */
/* Updated: 2023/11/22 14:14:21 by mdekker ######## odam.nl */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -58,6 +58,7 @@ void exec_process(t_group *group, t_process type, t_shell *data)

close_unused(type, group);
validate_redirects(group);
dup_fd(group, type);
if (group->cmd == NULL)
exit(0);
if (is_builtin(group->cmd))
Expand Down
2 changes: 1 addition & 1 deletion src/exec/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: mdekker/jde-baai <team@codam.nl> +#+ */
/* +#+ */
/* Created: 2023/09/01 19:44:05 by mdekker/jde #+# #+# */
/* Updated: 2023/11/14 16:21:13 by mdekker ######## odam.nl */
/* Updated: 2023/11/22 14:15:03 by mdekker ######## odam.nl */
/* */
/* ************************************************************************** */

Expand Down
13 changes: 7 additions & 6 deletions src/group/group.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: mdekker/jde-baai <team@codam.nl> +#+ */
/* +#+ */
/* Created: 2023/07/31 19:55:05 by mdekker/jde #+# #+# */
/* Updated: 2023/10/30 21:54:33 by mdekker/jde ######## odam.nl */
/* Updated: 2023/11/21 20:48:13 by mdekker/jde ######## odam.nl */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -66,7 +66,7 @@ bool alloc_args(t_group *group, size_t i, t_shell *data)

/**
* @note check the else statement depending on how we will handle quotes
(ie. could be else if (token->type == STRING
(ie. could be else if (token->type == STRING
|| token->type == QUOTE_STRING)
*
*/
Expand Down Expand Up @@ -141,13 +141,14 @@ bool group_token_vec(t_shell *data)
if (!group)
return (set_err(MALLOC, "group_token_v", data));
if (!set_cmd(group, i, data))
return (false);
return (clear_group(group), free(group), false);
if (!alloc_args(group, i, data))
return (false);
return (clear_group(group), free(group), false);
if (!group_tokens(group, &i, data))
return (false);
return (clear_group(group), free(group), false);
if (!vec_push(&data->exec->group_vec, group))
return (set_err(MALLOC, "group_token_v", data));
return (clear_group(group), free(group), set_err(MALLOC,
"group_token_v", data));
i++;
}
return (true);
Expand Down
2 changes: 1 addition & 1 deletion src/group/hdoc_expand.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: mdekker/jde-baai <team@codam.nl> +#+ */
/* +#+ */
/* Created: 2023/10/30 22:03:02 by mdekker/jde #+# #+# */
/* Updated: 2023/10/30 22:38:55 by mdekker/jde ######## odam.nl */
/* Updated: 2023/11/18 20:33:47 by mdekker/jde ######## odam.nl */
/* */
/* ************************************************************************** */

Expand Down
76 changes: 48 additions & 28 deletions src/group/heredoc.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,75 +6,90 @@
/* By: mdekker/jde-baai <team@codam.nl> +#+ */
/* +#+ */
/* Created: 2023/08/16 12:15:45 by mdekker/jde #+# #+# */
/* Updated: 2023/10/30 22:42:16 by mdekker/jde ######## odam.nl */
/* Updated: 2023/11/22 14:16:17 by mdekker ######## odam.nl */
/* */
/* ************************************************************************** */

#include <minishell.h>

static bool push_hdoc(char *filename, t_group *group, t_shell *data)
{
char *fname;
t_token *fname_token;
char **fname;
t_token *red_token;

fname = ft_strdup(filename);
fname = malloc(sizeof(char *));
if (!fname)
return (set_err(MALLOC, "push_hdoc", data));
red_token = create_token(filename, I_REDIRECT);
if (!red_token)
return (set_err(MALLOC, "push_hdoc", data));
return (free(fname), set_err(MALLOC, "push_hdoc", data));
if (!vec_push(&group->in_red, (void *)red_token))
return (set_err(MALLOC, "push_hdoc", data));
fname_token = create_token(fname, STRING);
if (!fname_token)
return (set_err(MALLOC, "push_hdoc", data));
if (!vec_push(&data->exec->fname_vec, (void *)fname_token))
return (set_err(MALLOC, "push_hdoc", data));
return (free(fname), set_err(MALLOC, "push_hdoc", data));
*fname = ft_strdup(filename);
if (!*fname)
return (free(fname), set_err(MALLOC, "push_hdoc", data));
if (!vec_push(&data->exec->fname_vec, (void *)fname))
return (free(*fname), free(fname), set_err(MALLOC, "push_hdoc", data));
return (true);
}

/**
* @note if signal ctrl D break while loop and return true
*/
static bool hdoc_read(size_t heredoc_fd, t_token *token, t_shell *data)
static void heredoc(char *filename, t_token *token, t_shell *data)
{
char *line;
int heredoc_fd;

setup_hdoc_signals();
heredoc_fd = open(filename, O_CREAT | O_TRUNC | O_RDWR, 0644);
if (heredoc_fd == -1)
exec_err(NULL, PERR);
while (1)
{
line = readline(">");
if (!line || ft_strcmp(line, token->value) == 0)
if (line == NULL || ft_strcmp(line, token->value) == 0)
break ;
if (token->type != HDOC_LITERAL)
{
if (!hdoc_expand(&line, data))
return (false);
exec_err("hdoc_expand", MALLOC);
}
write(heredoc_fd, line, ft_strlen(line));
write(heredoc_fd, "\n", 1);
free(line);
}
free(line);
return (true);
if (close(heredoc_fd) != 0)
exec_err(NULL, PERR);
exit(0);
}

/**
* @param filename the filename to be given to the new file
* @param token the hdoc token
* @note depending on expansion DQ logic can be removed
* @note writes its own error messages.
*/
bool heredoc(char *filename, t_token *token, t_shell *data)
bool hdoc_child(char *filename, t_token *token, t_shell *data)
{
int heredoc_fd;
int heredoc_fd;
pid_t pid;
int status;

heredoc_fd = open(filename, O_CREAT | O_TRUNC | O_RDWR, 0644);
if (heredoc_fd == -1)
return (set_err(PERR, filename, data));
if (!hdoc_read(heredoc_fd, token, data))
return (close(heredoc_fd), false);
if (close(heredoc_fd) != 0)
return (set_err(PERR, filename, data));
pid = fork();
if (pid == -1)
return (set_err(PERR, NULL, data));
if (pid == 0)
heredoc(filename, token, data);
else
{
waitpid(pid, &status, 0);
if (WEXITSTATUS(status) != 0)
{
data->error_type = WEXITSTATUS(status);
return (false);
}
}
return (true);
}

Expand All @@ -89,12 +104,17 @@ bool hdoc_found(t_group *group, size_t i, t_shell *data)
{
t_token *token;
char *filename;
char *nb;

filename = ft_strjoin("./src/hdoc_files/", ft_itoa((i)));
nb = ft_itoa(i);
filename = ft_strjoin("./src/hdoc_files/", nb);
free(nb);
if (!filename)
return (set_err(MALLOC, "hdoc_found", data));
if (!push_hdoc(filename, group, data))
return (false);
token = vec_get(&data->token_vec, i);
if (!heredoc(filename, token, data))
if (!hdoc_child(filename, token, data))
return (false);
return (push_hdoc(filename, group, data));
return (true);
}
6 changes: 3 additions & 3 deletions src/lexer/index.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: mdekker/jde-baai <team@codam.nl> +#+ */
/* +#+ */
/* Created: 2023/10/26 11:55:20 by mdekker/jde #+# #+# */
/* Updated: 2023/11/03 14:46:50 by mdekker ######## odam.nl */
/* Updated: 2023/11/22 14:17:00 by mdekker ######## odam.nl */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -56,7 +56,7 @@ static bool check_quote_finish(char *input, size_t *i, t_shell *data)
while (input[(*i)] && input[(*i)] != quote)
(*i)++;
if (input[(*i)] == '\0')
return (set_err(SYNTAX_MINI, (char[2]){quote, '\0'}, data));
return (set_err(SYNTAX_MINI, NULL, data));
(*i)++;
return (true);
}
Expand Down Expand Up @@ -107,7 +107,7 @@ bool lexer(char *input, t_shell *data)
{
if (checkchar(input[i], "();\\&"))
{
return (set_err(OUT_OF_SCOPE, ((char[2]){input[i], '\0'}), data));
return (set_err(OUT_OF_SCOPE, NULL, data));
}
if (checkchar(input[i], "<>"))
if (!build_redir_token(input, &i, data))
Expand Down
25 changes: 22 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,24 @@
/* By: mdekker/jde-baai <team@codam.nl> +#+ */
/* +#+ */
/* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */
/* Updated: 2023/11/21 17:59:51 by mdekker ######## odam.nl */
/* Updated: 2023/11/22 14:20:16 by mdekker ######## odam.nl */
/* */
/* ************************************************************************** */

#include <minishell.h>

/**
* @brief sets up signal handling and avoid readline catching sigs
* @note SIGINT = Ctrl-C
* @note SIGQUIT = Ctrl-\
*/
static void setup_signals(void)
{
rl_catch_signals = 0;
signal(SIGINT, signal_main);
signal(SIGQUIT, SIG_IGN);
}

static void debug(void)
{
printf("\033[1;32m●\n");
Expand Down Expand Up @@ -40,7 +52,7 @@ static bool function_map(char *input, t_shell *data)
function[3] = group_token_vec;
function[4] = executor;
if (!lexer(input, data))
return (false);
return (soft_exit(input, data), false);
i = -1;
while (i++ < 4)
{
Expand All @@ -61,8 +73,15 @@ static void loop(t_shell *data)

while (1)
{
setup_signals();
input = readline(" ❯ ");
if (!input || input[0] == '\0')
if (input == NULL)
{
free_shell(data, true);
printf("exit\n");
exit(0);
}
if (input[0] == '\0')
{
free(input);
continue ;
Expand Down
Loading

0 comments on commit 50a47d8

Please sign in to comment.