update: normed lots of stuff

This commit is contained in:
maix0 2024-09-16 19:23:28 +00:00
parent 84a825cb0b
commit 678587d481
11 changed files with 325 additions and 300 deletions

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/14 12:30:09 by maiboyer #+# #+# */
/* Updated: 2024/09/16 17:34:45 by maiboyer ### ########.fr */
/* Updated: 2024/09/16 18:22:10 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,6 +19,7 @@
#include <unistd.h>
#include <errno.h>
#include "line/line.h"
#include "me/types.h"
t_error _get_heredoc_input(t_fd *write, t_str delim)
{
@ -48,62 +49,84 @@ put_string_fd(lstate.output_fd, "^C\n"), line_refresh_line(&lstate), ERROR);
return (NO_ERROR);
}
t_error _redirection_fd(t_spawn_info *info, t_state *state, t_cmd_pipe cmd_pipe, t_ast_node red)
t_error _redir_input(\
t_spawn_info *info, t_state *state, t_vec_str *fname, t_ast_node red)
{
t_vec_str filename_args;
t_fd *red_fd;
filename_args = vec_str_new(16, str_free);
t_fd *red_fd;
if (red->data.file_redirection.op == AST_REDIR_INPUT)
{
if (info->stdin.tag == R_FD)
close_fd(info->stdin.fd.fd);
(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,
&filename_args))
if (_ast_into_str(red->data.file_redirection.output, state, fname))
return (ERROR);
if (filename_args.len != 1)
return (vec_str_free(filename_args), ERROR);
red_fd = open_fd(filename_args.buffer[0], FD_READ, O_CLOEXEC,
0);
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(filename_args), ERROR);
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)
{
if (info->stdout.tag == R_FD)
close_fd(info->stdout.fd.fd);
(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,
&filename_args))
if (_ast_into_str(red->data.file_redirection.output, state, fname))
return (ERROR);
if (filename_args.len != 1)
return (vec_str_free(filename_args), ERROR);
red_fd = open_fd(filename_args.buffer[0], FD_WRITE,
O_TRUNC | O_CREAT | O_CLOEXEC,
FP_ALL_READ | FP_ALL_WRITE);
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(filename_args), ERROR);
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)
{
if (info->stdout.tag == R_FD)
close_fd(info->stdout.fd.fd);
(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,
&filename_args))
if (_ast_into_str(red->data.file_redirection.output, state, fname))
return (ERROR);
if (filename_args.len != 1)
return (vec_str_free(filename_args), ERROR);
red_fd = open_fd(filename_args.buffer[0], FD_WRITE,
O_TRUNC | O_CREAT | O_CLOEXEC,
FP_ALL_READ | FP_ALL_WRITE);
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(filename_args), ERROR);
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;
t_fd *red_fd;
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
@ -111,18 +134,18 @@ t_error _redirection_fd(t_spawn_info *info, t_state *state, t_cmd_pipe cmd_pipe,
|| red->data.file_redirection.op == AST_REDIR_INPUT_OUTPUT
|| red->data.file_redirection.op == AST_REDIR_OUTPUT_CLOBBER)
return (ERROR);
vec_str_free(filename_args);
vec_str_free(fname);
return (NO_ERROR);
}
t_error _redirection_heredoc(t_spawn_info *info, t_state *state, t_cmd_pipe cmd_pipe, t_ast_node red)
t_error _redirection_heredoc(\
t_spawn_info *info, t_state *state, t_ast_node red)
{
t_pipe heredoc_pipe;
t_pipe heredoc_pipe;
if (red->data.heredoc_redirection.op == AST_REDIR_HEREDOC)
{
if (info->stdout.tag == R_FD)
close_fd(info->stdout.fd.fd);
(void)((info->stdout.tag == R_FD) && (close_fd(info->stdout.fd.fd), 1));
info->stdout.tag = R_INHERITED;
if (create_pipe(&heredoc_pipe))
return (ERROR);
@ -137,7 +160,9 @@ t_error _redirection_heredoc(t_spawn_info *info, t_state *state, t_cmd_pipe cmd_
return (NO_ERROR);
}
t_error _setup_redirection(t_spawn_info *info, t_state *state, t_cmd_pipe cmd_pipe, t_vec_ast *redirection)
t_error _setup_redirection(\
t_spawn_info *info, t_state *state, \
t_cmd_pipe cmd_pipe, t_vec_ast *redirection)
{
t_usize i;
t_ast_node red;
@ -150,38 +175,27 @@ t_error _setup_redirection(t_spawn_info *info, t_state *state, t_cmd_pipe cmd_pi
while (i < redirection->len)
{
red = redirection->buffer[i];
if (red == NULL)
if (red == NULL && (++i))
continue ;
if (red->kind == AST_FILE_REDIRECTION)
{
}
if (red->kind == AST_HEREDOC_REDIRECTION)
{
}
if (red->kind == AST_FILE_REDIRECTION && \
_redirection_fd(info, state, red))
return (ERROR);
if (red->kind == AST_HEREDOC_REDIRECTION && \
_redirection_heredoc(info, state, red))
return (ERROR);
i++;
}
return (NO_ERROR);
}
t_error _spawn_cmd_and_run(t_vec_str args, t_vec_ast redirection,
t_state *state, t_cmd_pipe cmd_pipe, t_command_result *out)
t_error _spawn_cmd_and_run_end(\
t_spawn_info info, t_state *state, \
t_cmd_pipe cmd_pipe, t_command_result *out)
{
t_spawn_info info;
struct s_ffree_state ffree;
int status;
info = (t_spawn_info){};
ffree = (struct s_ffree_state){.state = state, .cmd_pipe = cmd_pipe};
redirection.len = 0;
vec_ast_free(redirection);
info.arguments = args;
if (args.len == 0)
return (vec_str_free(args), ERROR);
info.binary_path = str_clone(info.arguments.buffer[0]);
if (_is_builtin(info.binary_path))
return (_handle_builtin(info, state, cmd_pipe, out));
if (build_envp(state->env, state->tmp_var, &info.environement))
return (str_free(info.binary_path), ERROR);
info.forked_free_args = &ffree;
info.forked_free = (void (*)(void *))_ffree_func;
signal(SIGINT, SIG_IGN);
@ -198,3 +212,24 @@ t_error _spawn_cmd_and_run(t_vec_str args, t_vec_ast redirection,
out->exit = WTERMSIG(status);
return (NO_ERROR);
}
t_error _spawn_cmd_and_run(t_vec_str args, t_redirections redirs,
t_state *state, t_command_result *out)
{
t_spawn_info info;
info = (t_spawn_info){};
if (_setup_redirection(&info, state, redirs.cmd_pipe, &redirs.redirections))
return (ERROR);
redirs.redirections.len = 0;
vec_ast_free(redirs.redirections);
info.arguments = args;
if (args.len == 0)
return (vec_str_free(args), ERROR);
info.binary_path = str_clone(info.arguments.buffer[0]);
if (_is_builtin(info.binary_path))
return (_handle_builtin(info, state, redirs.cmd_pipe, out));
if (build_envp(state->env, state->tmp_var, &info.environement))
return (str_free(info.binary_path), ERROR);
return (_spawn_cmd_and_run_end(info, state, redirs.cmd_pipe, out));
}