6
6
/* By: gbudau <gbudau@student.42.fr> +#+ +:+ +#+ */
7
7
/* +#+#+#+#+#+ +#+ */
8
8
/* Created: 2020/12/15 23:16:30 by gbudau #+# #+# */
9
- /* Updated: 2021/01/23 02:21:36 by gbudau ### ########.fr */
9
+ /* Updated: 2021/01/23 23:18:22 by gbudau ### ########.fr */
10
10
/* */
11
11
/* ************************************************************************** */
12
12
15
15
#include "../include/lexer.h"
16
16
#include "../include/wordexp.h"
17
17
#include "../include/command.h"
18
- #include "../include/ioredirection.h"
19
18
20
19
static void add_argument (t_list * * tokens , t_command * cmd )
21
20
{
@@ -61,11 +60,12 @@ static int add_pipe(t_list **tokens, t_command *cmd)
61
60
return (NO_PARSER_ERROR );
62
61
}
63
62
64
- static int add_input_redirection (t_list * * tokens , t_command * cmd )
63
+ static int add_input_redirection (t_list * * tokens , t_shell * shell )
65
64
{
66
- t_token * token ;
67
- int fd ;
65
+ t_token * token ;
66
+ t_command * cmd ;
68
67
68
+ cmd = shell -> commands -> content ;
69
69
* tokens = (* tokens )-> next ;
70
70
if (* tokens == NULL )
71
71
return (ERR_UNEXPECTED_NEWLINE );
@@ -74,64 +74,57 @@ static int add_input_redirection(t_list **tokens, t_command *cmd)
74
74
return (parse_error_token_type (token -> type ));
75
75
if (cmd -> redirection_error == FALSE)
76
76
{
77
- if (cmd -> input != NULL )
78
- {
79
- free (cmd -> input );
80
- cmd -> input = NULL ;
81
- }
82
77
if (cmd -> output == NULL )
83
78
cmd -> redirection_order = REDIRECT_INPUT_FIRST ;
84
- if ((fd = open (token -> str , O_RDONLY )) == -1 )
85
- {
86
- cmd -> redirection_error = TRUE;
87
- print_error_io (token -> str );
88
- }
89
- if (fd != -1 )
90
- close (fd );
91
- if (cmd -> redirection_error == FALSE)
92
- {
93
- cmd -> input = ft_strdup (token -> str );
94
- if (cmd -> input == NULL )
95
- error_exit ();
96
- }
79
+ if (cmd -> input != NULL )
80
+ cmd -> input = ft_free (cmd -> input );
81
+ cmd -> redirection_error = io_expand_open (& token -> str , shell ,
82
+ REDIRECTION_INPUT );
83
+ if (cmd -> redirection_error == FALSE &&
84
+ (cmd -> input = ft_strdup (token -> str )) == NULL )
85
+ error_exit ();
97
86
}
98
87
* tokens = (* tokens )-> next ;
99
88
return (NO_PARSER_ERROR );
100
89
}
101
90
102
- static int add_output_redirection (t_list * * tokens , t_command * cmd ,
91
+ static int add_output_redirection (t_list * * tokens , t_shell * shell ,
103
92
int redirect_type )
104
93
{
105
- t_token * token ;
94
+ t_token * token ;
95
+ t_command * cmd ;
106
96
97
+ cmd = shell -> commands -> content ;
107
98
* tokens = (* tokens )-> next ;
108
99
if (* tokens == NULL )
109
100
return (ERR_UNEXPECTED_NEWLINE );
110
- if (cmd -> output != NULL )
111
- {
112
- if (redirect_type == REDIRECTION_OUTPUT )
113
- return (ERR_UNEXPECTED_TOKEN_GREAT );
114
- else if (redirect_type == REDIRECTION_APPEND )
115
- return (ERR_UNEXPECTED_TOKEN_DGREAT );
116
- }
117
- if (cmd -> input == NULL )
118
- cmd -> redirection_order = REDIRECT_OUTPUT_FIRST ;
119
101
token = (* tokens )-> content ;
120
102
if (token -> type != TOKEN_WORD )
121
103
return (parse_error_token_type (token -> type ));
122
- cmd -> output = ft_strdup (token -> str );
123
- if (cmd -> output == NULL )
124
- error_exit ();
125
- cmd -> redirect_type = redirect_type ;
104
+ if (cmd -> redirection_error == FALSE)
105
+ {
106
+ if (cmd -> input == NULL )
107
+ cmd -> redirection_order = REDIRECT_OUTPUT_FIRST ;
108
+ if (cmd -> output != NULL )
109
+ cmd -> output = ft_free (cmd -> output );
110
+ cmd -> redirection_error = io_expand_open (& token -> str , shell ,
111
+ redirect_type );
112
+ if (cmd -> redirection_error == FALSE &&
113
+ (cmd -> output = ft_strdup (token -> str )) == NULL )
114
+ error_exit ();
115
+ cmd -> redirect_type = redirect_type ;
116
+ }
126
117
* tokens = (* tokens )-> next ;
127
118
return (NO_PARSER_ERROR );
128
119
}
129
120
130
- int add_command (t_list * * tokens , t_command * cmd )
121
+ int add_command (t_list * * tokens , t_shell * shell )
131
122
{
132
- t_token * token ;
133
- int error ;
123
+ t_token * token ;
124
+ int error ;
125
+ t_command * cmd ;
134
126
127
+ cmd = shell -> commands -> content ;
135
128
error = NO_PARSER_ERROR ;
136
129
while (* tokens != NULL && !error )
137
130
{
@@ -143,11 +136,11 @@ int add_command(t_list **tokens, t_command *cmd)
143
136
else if (token -> type == TOKEN_WORD )
144
137
add_argument (tokens , cmd );
145
138
else if (token -> type == TOKEN_LESS )
146
- error = add_input_redirection (tokens , cmd );
139
+ error = add_input_redirection (tokens , shell );
147
140
else if (token -> type == TOKEN_GREAT )
148
- error = add_output_redirection (tokens , cmd , REDIRECTION_OUTPUT );
141
+ error = add_output_redirection (tokens , shell , REDIRECTION_OUTPUT );
149
142
else if (token -> type == TOKEN_DGREAT )
150
- error = add_output_redirection (tokens , cmd , REDIRECTION_APPEND );
143
+ error = add_output_redirection (tokens , shell , REDIRECTION_APPEND );
151
144
}
152
145
if (cmd -> redirection_error )
153
146
error = ERR_REDIRECTION ;
0 commit comments