Skip to content

Commit 45fa01d

Browse files
authored
Merge pull request #273 from cacapon/feature/254-expand-line
Feature/254 expand line
2 parents ed4bb9c + 0a31bfa commit 45fa01d

File tree

11 files changed

+85
-70
lines changed

11 files changed

+85
-70
lines changed

inc/common/struct.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2025/04/15 17:10:22 by dayano #+# #+# */
9-
/* Updated: 2025/05/11 16:24:51 by ttsubo ### ########.fr */
9+
/* Updated: 2025/05/18 14:55:12 by ttsubo ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -50,6 +50,7 @@ typedef struct s_minish
5050
typedef struct s_cmd
5151
{
5252
t_redir_type type;
53+
int is_expand_heredoc;
5354
int argc;
5455
char **argv;
5556
int capa;

inc/invoke_cmd/redirect.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
/* ::: :::::::: */
44
/* redirect.h :+: :+: :+: */
55
/* +:+ +:+ +:+ */
6-
/* By: dayano <dayano@student.42.fr> +#+ +:+ +#+ */
6+
/* By: ttsubo <ttsubo@student.42.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2025/04/28 15:20:03 by dayano #+# #+# */
9-
/* Updated: 2025/05/15 21:30:20 by dayano ### ########.fr */
9+
/* Updated: 2025/05/18 15:33:33 by ttsubo ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -16,10 +16,10 @@
1616
# include "common.h"
1717

1818
// redirect.c
19-
bool redirect(t_cmd *cmd);
19+
bool redirect(t_cmd *cmd, t_minish *minish);
2020
bool redirect_stdout(t_cmd *cmd);
21-
bool redirect_stdin(t_cmd *cmd);
22-
void here_doc(char *limiter);
21+
bool redirect_stdin(t_cmd *cmd, t_minish *minish);
22+
void here_doc(t_cmd *cmd, char *limiter, t_minish *minish);
2323

2424
// redirect_handler.c
2525
void handle_all_redirection(t_cmd *cmd, t_minish *minish);

inc/parser/parser.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2025/04/21 14:14:59 by ttsubo #+# #+# */
9-
/* Updated: 2025/05/17 14:19:47 by ttsubo ### ########.fr */
9+
/* Updated: 2025/05/18 15:01:09 by ttsubo ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -38,7 +38,7 @@ t_cmd **calloc_cmds(char **tokens);
3838
t_cmd *calloc_cmd(void);
3939
char **calloc_argv(char **tokens);
4040
char *calloc_arg(char *token);
41-
char **trim_quote_tokens(char **tokens);
41+
char *trim_quote_token(char *token);
4242

4343
t_cmd **parser(char **tokens, t_minish *minish);
4444
char **expand_tokens(char **tokens, t_minish *minish);
@@ -51,5 +51,6 @@ t_cmd **setup_cmds(t_cmd **cmds, char **tokens);
5151
t_list *split_by_quote(char *token);
5252
char *join_expanded_parts(t_list *parts, t_minish *minish);
5353
char *strjoin_and_free(char *s1, char *s2);
54+
void set_heredoc_flg(t_cmd *cmd, char *token);
5455

5556
#endif

src/invoke_cmd/exec_pipeline.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
/* ::: :::::::: */
44
/* exec_pipeline.c :+: :+: :+: */
55
/* +:+ +:+ +:+ */
6-
/* By: dayano <dayano@student.42.fr> +#+ +:+ +#+ */
6+
/* By: ttsubo <ttsubo@student.42.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2025/04/21 13:14:15 by dayano #+# #+# */
9-
/* Updated: 2025/05/15 21:17:55 by dayano ### ########.fr */
9+
/* Updated: 2025/05/18 15:36:09 by ttsubo ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -64,7 +64,7 @@ static pid_t _fork_command(t_cmd *cmd, t_cmd *cmd_head, t_pipe_io *pipefds,
6464
if (pid > 0)
6565
return (_setup_process_signals(SIG_IGN), pid);
6666
if (cmd->next && cmd->next->type == REDIR_HEREDOC)
67-
here_doc(cmd->next->argv[0]);
67+
here_doc(cmd->next, cmd->next->argv[0], minish);
6868
_setup_stdin(cmd, cmd_head, pipefds->prev_fds);
6969
_setup_stdout(cmd, pipefds->curr_fds);
7070
_setup_process_signals(SIG_DFL);

src/invoke_cmd/invoke_command.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2025/04/20 21:19:33 by dayano #+# #+# */
9-
/* Updated: 2025/05/11 16:32:46 by ttsubo ### ########.fr */
9+
/* Updated: 2025/05/18 16:47:57 by ttsubo ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -73,7 +73,7 @@ int exec_unit_builtin(t_cmd *cmd, t_minish *minish)
7373
redir_cmd = cmd->next;
7474
while (redir_cmd && is_redirect(redir_cmd))
7575
{
76-
redir_result = redirect(redir_cmd);
76+
redir_result = redirect(redir_cmd, minish);
7777
if (!redir_result)
7878
return (print_error(builtin_cmd->argv[0]), EXIT_FAILURE);
7979
redir_cmd = redir_cmd->next;

src/invoke_cmd/redirect.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
/* ::: :::::::: */
44
/* redirect.c :+: :+: :+: */
55
/* +:+ +:+ +:+ */
6-
/* By: dayano <dayano@student.42.fr> +#+ +:+ +#+ */
6+
/* By: ttsubo <ttsubo@student.42.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2025/04/28 15:19:19 by dayano #+# #+# */
9-
/* Updated: 2025/05/15 13:05:46 by dayano ### ########.fr */
9+
/* Updated: 2025/05/18 15:33:05 by ttsubo ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -41,7 +41,7 @@ bool redirect_stdout(t_cmd *cmd)
4141
return (true);
4242
}
4343

44-
bool is_limiter(int fd, char *limiter)
44+
bool is_limiter(t_cmd *cmd, int fd, char *limiter, t_minish *minish)
4545
{
4646
char *line;
4747

@@ -57,13 +57,17 @@ bool is_limiter(int fd, char *limiter)
5757
free(line);
5858
return (true);
5959
}
60+
if (cmd->is_expand_heredoc)
61+
line = expand_vars(line, minish);
62+
if (!line)
63+
return (free_str(&line), false);
6064
write(fd, line, ft_strlen(line));
6165
write(fd, "\n", 1);
6266
free(line);
6367
return (false);
6468
}
6569

66-
void here_doc(char *limiter)
70+
void here_doc(t_cmd *cmd, char *limiter, t_minish *minish)
6771
{
6872
int pipefd[2];
6973

@@ -73,7 +77,7 @@ void here_doc(char *limiter)
7377
perror("pipe");
7478
return ;
7579
}
76-
while (!is_limiter(pipefd[1], limiter))
80+
while (!is_limiter(cmd, pipefd[1], limiter, minish))
7781
;
7882
close(pipefd[1]);
7983
if (dup2(pipefd[0], STDIN_FILENO) < 0)
@@ -84,14 +88,14 @@ void here_doc(char *limiter)
8488
close(pipefd[0]);
8589
}
8690

87-
bool redirect_stdin(t_cmd *cmd)
91+
bool redirect_stdin(t_cmd *cmd, t_minish *minish)
8892
{
8993
int fd;
9094
char *path;
9195

9296
path = cmd->argv[0];
9397
if (cmd->type == REDIR_HEREDOC)
94-
return (here_doc(path), true);
98+
return (here_doc(cmd, path, minish), true);
9599
if (cmd->type != REDIR_IN)
96100
return (true);
97101
fd = open(path, O_RDONLY);
@@ -116,10 +120,10 @@ bool redirect_stdin(t_cmd *cmd)
116120
* @note This function is only called if the caller's is_redirect is true
117121
* @note false if the internal function is not executed.
118122
*/
119-
bool redirect(t_cmd *cmd)
123+
bool redirect(t_cmd *cmd, t_minish *minish)
120124
{
121125
if (cmd->type == REDIR_IN)
122-
return (redirect_stdin(cmd));
126+
return (redirect_stdin(cmd, minish));
123127
if (cmd->type == REDIR_HEREDOC)
124128
return (true);
125129
if (cmd->type == REDIR_OUT || cmd->type == REDIR_APPEND)

src/invoke_cmd/redirect_handler.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
/* ::: :::::::: */
44
/* redirect_handler.c :+: :+: :+: */
55
/* +:+ +:+ +:+ */
6-
/* By: dayano <dayano@student.42.fr> +#+ +:+ +#+ */
6+
/* By: ttsubo <ttsubo@student.42.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2025/05/15 21:12:09 by dayano #+# #+# */
9-
/* Updated: 2025/05/15 21:31:16 by dayano ### ########.fr */
9+
/* Updated: 2025/05/17 16:50:05 by ttsubo ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -19,7 +19,7 @@ void handle_all_redirection(t_cmd *cmd, t_minish *minish)
1919
while (cmd->next && is_redirect(cmd->next))
2020
{
2121
redir = cmd->next;
22-
if (!redirect(redir))
22+
if (!redirect(redir, minish))
2323
pipeline_exit(EXIT_FAILURE, minish);
2424
cmd->next = cmd->next->next;
2525
free_cmd(&redir);

src/parser/parser.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2025/04/21 14:14:32 by ttsubo #+# #+# */
9-
/* Updated: 2025/05/17 15:39:05 by ttsubo ### ########.fr */
9+
/* Updated: 2025/05/18 14:32:44 by ttsubo ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -28,7 +28,6 @@ t_cmd **parser(char **tokens, t_minish *minish)
2828
formatted_tokens = expand_tokens(tokens, minish);
2929
if (!formatted_tokens)
3030
return (NULL);
31-
formatted_tokens = trim_quote_tokens(formatted_tokens);
3231
cmds = allocate_cmds(formatted_tokens);
3332
if (!cmds)
3433
return (free_strs(&formatted_tokens), NULL);

src/parser/parser_utils.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,21 @@
66
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2025/04/24 13:40:56 by ttsubo #+# #+# */
9-
/* Updated: 2025/05/02 21:26:41 by ttsubo ### ########.fr */
9+
/* Updated: 2025/05/18 15:59:41 by ttsubo ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

1313
#include "parser.h"
1414

15+
void set_heredoc_flg(t_cmd *cmd, char *token)
16+
{
17+
bool has_quote;
18+
19+
has_quote = ft_strchr(token, '\'') != NULL
20+
|| ft_strchr(token, '"') != NULL;
21+
cmd->is_expand_heredoc = !(cmd->type == REDIR_HEREDOC && has_quote);
22+
}
23+
1524
void set_cmd_type(t_cmd *cmd, char *token)
1625
{
1726
if (!ft_strcmp(token, ">"))

src/parser/setup_cmds.c

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,59 @@
66
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2025/04/24 13:38:36 by ttsubo #+# #+# */
9-
/* Updated: 2025/05/04 22:06:35 by ttsubo ### ########.fr */
9+
/* Updated: 2025/05/18 16:44:51 by ttsubo ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

1313
#include "parser.h"
1414

15-
static void _next_cmd(t_cmd **cmds, size_t *cmd_i, size_t *arg_i, char *token)
15+
static bool _copy_token(char **arg_p, char **token_p)
1616
{
17-
cmds[*cmd_i]->argv[*arg_i] = NULL;
18-
cmds[*cmd_i]->argc = *arg_i;
19-
(*arg_i) = 0;
20-
(*cmd_i)++;
21-
set_cmd_type(cmds[*cmd_i], token);
17+
size_t len;
18+
19+
if (!arg_p || !token_p || !*arg_p || !*token_p)
20+
return (false);
21+
len = ft_strlen(*token_p) + 1;
22+
return (ft_strlcpy(*arg_p, *token_p, len) < len);
23+
}
24+
25+
t_cmd *setup_cmd(t_cmd *cmd, char ***tokens_p)
26+
{
27+
size_t a_i;
28+
char **tokens;
29+
30+
a_i = 0;
31+
tokens = *tokens_p;
32+
if (is_separator(tokens[0]))
33+
{
34+
set_cmd_type(cmd, tokens[0]);
35+
if (tokens[1])
36+
set_heredoc_flg(cmd, tokens[1]);
37+
tokens++;
38+
}
39+
while (*tokens && !is_separator(*tokens))
40+
{
41+
*tokens = trim_quote_token(*tokens);
42+
if (!_copy_token(&cmd->argv[a_i], tokens))
43+
return (NULL);
44+
tokens++;
45+
a_i++;
46+
}
47+
cmd->argc = a_i;
48+
*tokens_p = tokens;
49+
return (cmd);
2250
}
2351

2452
t_cmd **setup_cmds(t_cmd **cmds, char **tokens)
2553
{
26-
size_t token_i;
27-
size_t cmd_i;
28-
size_t arg_i;
54+
size_t c_i;
2955

30-
token_i = 0;
31-
cmd_i = 0;
32-
arg_i = 0;
33-
set_cmd_type(cmds[0], tokens[token_i]);
34-
while (tokens[token_i])
56+
c_i = 0;
57+
while (*tokens)
3558
{
36-
if (is_separator(tokens[token_i]))
37-
_next_cmd(cmds, &cmd_i, &arg_i, tokens[token_i]);
38-
else
39-
{
40-
ft_strlcpy(cmds[cmd_i]->argv[arg_i], tokens[token_i],
41-
ft_strlen(tokens[token_i]) + 1);
42-
if (!cmds[cmd_i]->argv[arg_i])
43-
return (free_cmds(&cmds), NULL);
44-
arg_i++;
45-
}
46-
token_i++;
59+
if (!setup_cmd(cmds[c_i], &tokens))
60+
return (free_cmds(&cmds), NULL);
61+
c_i++;
4762
}
48-
cmds[cmd_i]->argc = arg_i;
4963
return (cmds);
5064
}

0 commit comments

Comments
 (0)