Skip to content
3 changes: 2 additions & 1 deletion inc/common/struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 17:10:22 by dayano #+# #+# */
/* Updated: 2025/05/11 16:24:51 by ttsubo ### ########.fr */
/* Updated: 2025/05/18 14:55:12 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -50,6 +50,7 @@ typedef struct s_minish
typedef struct s_cmd
{
t_redir_type type;
int is_expand_heredoc;
int argc;
char **argv;
int capa;
Expand Down
10 changes: 5 additions & 5 deletions inc/invoke_cmd/redirect.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* redirect.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dayano <dayano@student.42.fr> +#+ +:+ +#+ */
/* By: ttsubo <ttsubo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/28 15:20:03 by dayano #+# #+# */
/* Updated: 2025/05/15 21:30:20 by dayano ### ########.fr */
/* Updated: 2025/05/18 15:33:33 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -16,10 +16,10 @@
# include "common.h"

// redirect.c
bool redirect(t_cmd *cmd);
bool redirect(t_cmd *cmd, t_minish *minish);
bool redirect_stdout(t_cmd *cmd);
bool redirect_stdin(t_cmd *cmd);
void here_doc(char *limiter);
bool redirect_stdin(t_cmd *cmd, t_minish *minish);
void here_doc(t_cmd *cmd, char *limiter, t_minish *minish);

// redirect_handler.c
void handle_all_redirection(t_cmd *cmd, t_minish *minish);
Expand Down
5 changes: 3 additions & 2 deletions inc/parser/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/21 14:14:59 by ttsubo #+# #+# */
/* Updated: 2025/05/17 14:19:47 by ttsubo ### ########.fr */
/* Updated: 2025/05/18 15:01:09 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -38,7 +38,7 @@ t_cmd **calloc_cmds(char **tokens);
t_cmd *calloc_cmd(void);
char **calloc_argv(char **tokens);
char *calloc_arg(char *token);
char **trim_quote_tokens(char **tokens);
char *trim_quote_token(char *token);

t_cmd **parser(char **tokens, t_minish *minish);
char **expand_tokens(char **tokens, t_minish *minish);
Expand All @@ -51,5 +51,6 @@ t_cmd **setup_cmds(t_cmd **cmds, char **tokens);
t_list *split_by_quote(char *token);
char *join_expanded_parts(t_list *parts, t_minish *minish);
char *strjoin_and_free(char *s1, char *s2);
void set_heredoc_flg(t_cmd *cmd, char *token);

#endif
6 changes: 3 additions & 3 deletions src/invoke_cmd/exec_pipeline.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* exec_pipeline.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dayano <dayano@student.42.fr> +#+ +:+ +#+ */
/* By: ttsubo <ttsubo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/21 13:14:15 by dayano #+# #+# */
/* Updated: 2025/05/15 21:17:55 by dayano ### ########.fr */
/* Updated: 2025/05/18 15:36:09 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -64,7 +64,7 @@ static pid_t _fork_command(t_cmd *cmd, t_cmd *cmd_head, t_pipe_io *pipefds,
if (pid > 0)
return (_setup_process_signals(SIG_IGN), pid);
if (cmd->next && cmd->next->type == REDIR_HEREDOC)
here_doc(cmd->next->argv[0]);
here_doc(cmd->next, cmd->next->argv[0], minish);
_setup_stdin(cmd, cmd_head, pipefds->prev_fds);
_setup_stdout(cmd, pipefds->curr_fds);
_setup_process_signals(SIG_DFL);
Expand Down
4 changes: 2 additions & 2 deletions src/invoke_cmd/invoke_command.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/20 21:19:33 by dayano #+# #+# */
/* Updated: 2025/05/11 16:32:46 by ttsubo ### ########.fr */
/* Updated: 2025/05/18 16:47:57 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -73,7 +73,7 @@ int exec_unit_builtin(t_cmd *cmd, t_minish *minish)
redir_cmd = cmd->next;
while (redir_cmd && is_redirect(redir_cmd))
{
redir_result = redirect(redir_cmd);
redir_result = redirect(redir_cmd, minish);
if (!redir_result)
return (print_error(builtin_cmd->argv[0]), EXIT_FAILURE);
redir_cmd = redir_cmd->next;
Expand Down
22 changes: 13 additions & 9 deletions src/invoke_cmd/redirect.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* redirect.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dayano <dayano@student.42.fr> +#+ +:+ +#+ */
/* By: ttsubo <ttsubo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/28 15:19:19 by dayano #+# #+# */
/* Updated: 2025/05/15 13:05:46 by dayano ### ########.fr */
/* Updated: 2025/05/18 15:33:05 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -41,7 +41,7 @@ bool redirect_stdout(t_cmd *cmd)
return (true);
}

bool is_limiter(int fd, char *limiter)
bool is_limiter(t_cmd *cmd, int fd, char *limiter, t_minish *minish)
{
char *line;

Expand All @@ -57,13 +57,17 @@ bool is_limiter(int fd, char *limiter)
free(line);
return (true);
}
if (cmd->is_expand_heredoc)
line = expand_vars(line, minish);
if (!line)
return (free_str(&line), false);
write(fd, line, ft_strlen(line));
write(fd, "\n", 1);
free(line);
return (false);
}

void here_doc(char *limiter)
void here_doc(t_cmd *cmd, char *limiter, t_minish *minish)
{
int pipefd[2];

Expand All @@ -73,7 +77,7 @@ void here_doc(char *limiter)
perror("pipe");
return ;
}
while (!is_limiter(pipefd[1], limiter))
while (!is_limiter(cmd, pipefd[1], limiter, minish))
;
close(pipefd[1]);
if (dup2(pipefd[0], STDIN_FILENO) < 0)
Expand All @@ -84,14 +88,14 @@ void here_doc(char *limiter)
close(pipefd[0]);
}

bool redirect_stdin(t_cmd *cmd)
bool redirect_stdin(t_cmd *cmd, t_minish *minish)
{
int fd;
char *path;

path = cmd->argv[0];
if (cmd->type == REDIR_HEREDOC)
return (here_doc(path), true);
return (here_doc(cmd, path, minish), true);
if (cmd->type != REDIR_IN)
return (true);
fd = open(path, O_RDONLY);
Expand All @@ -116,10 +120,10 @@ bool redirect_stdin(t_cmd *cmd)
* @note This function is only called if the caller's is_redirect is true
* @note false if the internal function is not executed.
*/
bool redirect(t_cmd *cmd)
bool redirect(t_cmd *cmd, t_minish *minish)
{
if (cmd->type == REDIR_IN)
return (redirect_stdin(cmd));
return (redirect_stdin(cmd, minish));
if (cmd->type == REDIR_HEREDOC)
return (true);
if (cmd->type == REDIR_OUT || cmd->type == REDIR_APPEND)
Expand Down
6 changes: 3 additions & 3 deletions src/invoke_cmd/redirect_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* redirect_handler.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dayano <dayano@student.42.fr> +#+ +:+ +#+ */
/* By: ttsubo <ttsubo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/15 21:12:09 by dayano #+# #+# */
/* Updated: 2025/05/15 21:31:16 by dayano ### ########.fr */
/* Updated: 2025/05/17 16:50:05 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -19,7 +19,7 @@ void handle_all_redirection(t_cmd *cmd, t_minish *minish)
while (cmd->next && is_redirect(cmd->next))
{
redir = cmd->next;
if (!redirect(redir))
if (!redirect(redir, minish))
pipeline_exit(EXIT_FAILURE, minish);
cmd->next = cmd->next->next;
free_cmd(&redir);
Expand Down
3 changes: 1 addition & 2 deletions src/parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/21 14:14:32 by ttsubo #+# #+# */
/* Updated: 2025/05/17 15:39:05 by ttsubo ### ########.fr */
/* Updated: 2025/05/18 14:32:44 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -28,7 +28,6 @@ t_cmd **parser(char **tokens, t_minish *minish)
formatted_tokens = expand_tokens(tokens, minish);
if (!formatted_tokens)
return (NULL);
formatted_tokens = trim_quote_tokens(formatted_tokens);
cmds = allocate_cmds(formatted_tokens);
if (!cmds)
return (free_strs(&formatted_tokens), NULL);
Expand Down
11 changes: 10 additions & 1 deletion src/parser/parser_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/24 13:40:56 by ttsubo #+# #+# */
/* Updated: 2025/05/02 21:26:41 by ttsubo ### ########.fr */
/* Updated: 2025/05/18 15:59:41 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

#include "parser.h"

void set_heredoc_flg(t_cmd *cmd, char *token)
{
bool has_quote;

has_quote = ft_strchr(token, '\'') != NULL
|| ft_strchr(token, '"') != NULL;
cmd->is_expand_heredoc = !(cmd->type == REDIR_HEREDOC && has_quote);
}

void set_cmd_type(t_cmd *cmd, char *token)
{
if (!ft_strcmp(token, ">"))
Expand Down
68 changes: 41 additions & 27 deletions src/parser/setup_cmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,59 @@
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/24 13:38:36 by ttsubo #+# #+# */
/* Updated: 2025/05/04 22:06:35 by ttsubo ### ########.fr */
/* Updated: 2025/05/18 16:44:51 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

#include "parser.h"

static void _next_cmd(t_cmd **cmds, size_t *cmd_i, size_t *arg_i, char *token)
static bool _copy_token(char **arg_p, char **token_p)
{
cmds[*cmd_i]->argv[*arg_i] = NULL;
cmds[*cmd_i]->argc = *arg_i;
(*arg_i) = 0;
(*cmd_i)++;
set_cmd_type(cmds[*cmd_i], token);
size_t len;

if (!arg_p || !token_p || !*arg_p || !*token_p)
return (false);
len = ft_strlen(*token_p) + 1;
return (ft_strlcpy(*arg_p, *token_p, len) < len);
}

t_cmd *setup_cmd(t_cmd *cmd, char ***tokens_p)
{
size_t a_i;
char **tokens;

a_i = 0;
tokens = *tokens_p;
if (is_separator(tokens[0]))
{
set_cmd_type(cmd, tokens[0]);
if (tokens[1])
set_heredoc_flg(cmd, tokens[1]);
tokens++;
}
while (*tokens && !is_separator(*tokens))
{
*tokens = trim_quote_token(*tokens);
if (!_copy_token(&cmd->argv[a_i], tokens))
return (NULL);
tokens++;
a_i++;
}
cmd->argc = a_i;
*tokens_p = tokens;
return (cmd);
}

t_cmd **setup_cmds(t_cmd **cmds, char **tokens)
{
size_t token_i;
size_t cmd_i;
size_t arg_i;
size_t c_i;

token_i = 0;
cmd_i = 0;
arg_i = 0;
set_cmd_type(cmds[0], tokens[token_i]);
while (tokens[token_i])
c_i = 0;
while (*tokens)
{
if (is_separator(tokens[token_i]))
_next_cmd(cmds, &cmd_i, &arg_i, tokens[token_i]);
else
{
ft_strlcpy(cmds[cmd_i]->argv[arg_i], tokens[token_i],
ft_strlen(tokens[token_i]) + 1);
if (!cmds[cmd_i]->argv[arg_i])
return (free_cmds(&cmds), NULL);
arg_i++;
}
token_i++;
if (!setup_cmd(cmds[c_i], &tokens))
return (free_cmds(&cmds), NULL);
c_i++;
}
cmds[cmd_i]->argc = arg_i;
return (cmds);
}
17 changes: 2 additions & 15 deletions src/parser/trim_quote_tokens.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/05 14:52:11 by ttsubo #+# #+# */
/* Updated: 2025/05/06 20:12:44 by ttsubo ### ########.fr */
/* Updated: 2025/05/18 14:48:54 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -45,7 +45,7 @@ static void _exec(char *token, char *result, size_t *pt_i, size_t *pr_i)
result[(*pr_i)++] = token[(*pt_i)++];
}

static char *_trim_quote_token(char *token)
char *trim_quote_token(char *token)
{
size_t t_i;
size_t r_i;
Expand All @@ -63,16 +63,3 @@ static char *_trim_quote_token(char *token)
free(token);
return (result);
}

char **trim_quote_tokens(char **tokens)
{
size_t i;

i = 0;
while (tokens[i])
{
tokens[i] = _trim_quote_token(tokens[i]);
i++;
}
return (tokens);
}