update: normed exec and finish the glob for current directory
This commit is contained in:
parent
cc1e2b7fdc
commit
fba538e344
24 changed files with 647 additions and 429 deletions
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/09/14 12:30:09 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/09/18 14:20:32 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/09/18 21:50:18 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -21,144 +21,10 @@
|
|||
#include "line/line.h"
|
||||
#include "me/types.h"
|
||||
|
||||
t_error _get_heredoc_input(t_fd *write, t_str delim)
|
||||
{
|
||||
struct s_line_state lstate;
|
||||
t_str line;
|
||||
|
||||
if (write == NULL || delim == NULL)
|
||||
return (ERROR);
|
||||
while (true)
|
||||
{
|
||||
if (line_edit_start(&lstate, get_stdin(), get_stdout(), "> "))
|
||||
return (ERROR);
|
||||
while (!line_edit_feed(&lstate, &line))
|
||||
{
|
||||
if (errno == EAGAIN)
|
||||
{
|
||||
errno = 0;
|
||||
return (lstate.pos = 0, string_clear(&lstate.buf), \
|
||||
put_string_fd(lstate.output_fd, "^C\n"), line_refresh_line(&lstate), ERROR);
|
||||
}
|
||||
}
|
||||
line_edit_stop(&lstate);
|
||||
if (line == NULL || str_compare(delim, line))
|
||||
break ;
|
||||
(put_string_fd(write, line), put_char_fd(write, '\n'), str_free(line));
|
||||
}
|
||||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
t_error _redir_input(\
|
||||
t_spawn_info *info, t_state *state, t_vec_str *fname, t_ast_node red)
|
||||
{
|
||||
t_fd *red_fd;
|
||||
|
||||
if (red->data.file_redirection.op == AST_REDIR_INPUT)
|
||||
{
|
||||
(void)((info->stdin.tag == R_FD) && (close_fd(info->stdin.fd.fd), 1));
|
||||
info->stdin.tag = R_INHERITED;
|
||||
if (_ast_into_str(red->data.file_redirection.output, state, fname))
|
||||
return (ERROR);
|
||||
if (fname->len != 1)
|
||||
return (vec_str_free(*fname), ERROR);
|
||||
red_fd = open_fd(fname->buffer[0], FD_READ, O_CLOEXEC, 0);
|
||||
if (red_fd == NULL)
|
||||
return (vec_str_free(*fname), ERROR);
|
||||
info->stdin = fd(red_fd);
|
||||
}
|
||||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
t_error _redir_output(\
|
||||
t_spawn_info *info, t_state *state, t_vec_str *fname, t_ast_node red)
|
||||
{
|
||||
t_fd *red_fd;
|
||||
|
||||
if (red->data.file_redirection.op == AST_REDIR_OUTPUT)
|
||||
{
|
||||
(void)((info->stdout.tag == R_FD) && (close_fd(info->stdout.fd.fd), 1));
|
||||
info->stdout.tag = R_INHERITED;
|
||||
if (_ast_into_str(red->data.file_redirection.output, state, fname))
|
||||
return (ERROR);
|
||||
if (fname->len != 1)
|
||||
return (vec_str_free(*fname), ERROR);
|
||||
red_fd = open_fd(fname->buffer[0], FD_WRITE, \
|
||||
O_TRUNC | O_CREAT | O_CLOEXEC, FP_ALL_READ | FP_ALL_WRITE);
|
||||
if (red_fd == NULL)
|
||||
return (vec_str_free(*fname), ERROR);
|
||||
info->stdout = fd(red_fd);
|
||||
}
|
||||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
t_error _redir_output_append(\
|
||||
t_spawn_info *info, t_state *state, t_vec_str *fname, t_ast_node red)
|
||||
{
|
||||
t_fd *red_fd;
|
||||
|
||||
if (red->data.file_redirection.op == AST_REDIR_APPEND)
|
||||
{
|
||||
(void)((info->stdout.tag == R_FD) && (close_fd(info->stdout.fd.fd), 1));
|
||||
info->stdout.tag = R_INHERITED;
|
||||
if (_ast_into_str(red->data.file_redirection.output, state, fname))
|
||||
return (ERROR);
|
||||
if (fname->len != 1)
|
||||
return (vec_str_free(*fname), ERROR);
|
||||
red_fd = open_fd(fname->buffer[0], FD_WRITE, \
|
||||
O_TRUNC | O_CREAT | O_CLOEXEC, FP_ALL_READ | FP_ALL_WRITE);
|
||||
if (red_fd == NULL)
|
||||
return (vec_str_free(*fname), ERROR);
|
||||
info->stdout = fd(red_fd);
|
||||
}
|
||||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
t_error _redirection_fd(\
|
||||
t_spawn_info *info, t_state *state, t_ast_node red)
|
||||
{
|
||||
t_vec_str fname;
|
||||
|
||||
fname = vec_str_new(16, str_free);
|
||||
if (_redir_input(info, state, &fname, red))
|
||||
return (ERROR);
|
||||
if (_redir_output(info, state, &fname, red))
|
||||
return (ERROR);
|
||||
if (_redir_output_append(info, state, &fname, red))
|
||||
return (ERROR);
|
||||
if (red->data.file_redirection.op == AST_REDIR_DUP_INPUT
|
||||
|| red->data.file_redirection.op == AST_REDIR_DUP_OUTPUT
|
||||
|| red->data.file_redirection.op == AST_REDIR_CLOSE_INPUT
|
||||
|| red->data.file_redirection.op == AST_REDIR_CLOSE_OUTPUT
|
||||
|| red->data.file_redirection.op == AST_REDIR_INPUT_OUTPUT
|
||||
|| red->data.file_redirection.op == AST_REDIR_OUTPUT_CLOBBER)
|
||||
return (ERROR);
|
||||
vec_str_free(fname);
|
||||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
t_spawn_info *info, t_state *state, t_ast_node red);
|
||||
t_error _redirection_heredoc(\
|
||||
t_spawn_info *info, t_state *state, t_ast_node red)
|
||||
{
|
||||
t_pipe heredoc_pipe;
|
||||
|
||||
(void)state;
|
||||
if (red->data.heredoc_redirection.op == AST_REDIR_HEREDOC)
|
||||
{
|
||||
(void)((info->stdin.tag == R_FD) && (close_fd(info->stdin.fd.fd), 1));
|
||||
info->stdin.tag = R_INHERITED;
|
||||
if (create_pipe(&heredoc_pipe))
|
||||
return (ERROR);
|
||||
if (_get_heredoc_input(heredoc_pipe.write, \
|
||||
red->data.heredoc_redirection.delimiter))
|
||||
return (close_fd(heredoc_pipe.write), ERROR);
|
||||
close_fd(heredoc_pipe.write);
|
||||
info->stdin = fd(heredoc_pipe.read);
|
||||
}
|
||||
if (red->data.heredoc_redirection.op == AST_REDIR_HEREDOC_INDENT)
|
||||
return (ERROR);
|
||||
return (NO_ERROR);
|
||||
}
|
||||
t_spawn_info *info, t_state *state, t_ast_node red);
|
||||
|
||||
t_error _setup_redirection(\
|
||||
t_spawn_info *info, t_state *state, \
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue