fixed: splitting
This commit is contained in:
parent
ca8e484183
commit
135a6429ee
14 changed files with 117 additions and 66 deletions
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/09/14 12:26:51 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/10/14 12:34:25 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/10/14 15:02:01 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -21,32 +21,62 @@ t_error _word_handle_star(t_ast_word *word, t_state *state, t_vec_str *out);
|
|||
t_error list_files_in_current_directory(t_vec_str *out);
|
||||
t_error _word_into_str_inner(struct s_word_str_args args);
|
||||
|
||||
t_error _word_pass_quote(t_state *state, t_word_result *res, t_vec_str *append)
|
||||
t_error _word_split_loop(\
|
||||
bool do_split, t_expandable_str val, t_vec_str *append, t_string *tmp)
|
||||
{
|
||||
t_vec_str split;
|
||||
t_str stmp;
|
||||
|
||||
if (do_split)
|
||||
{
|
||||
if (val.do_expand)
|
||||
{
|
||||
if (str_split(val.value, " ", &split))
|
||||
return (ERROR);
|
||||
while (!vec_str_pop_front(&split, &stmp))
|
||||
vec_str_push(append, stmp);
|
||||
vec_str_free(split);
|
||||
}
|
||||
else
|
||||
vec_str_push(append, str_clone(val.value));
|
||||
}
|
||||
else
|
||||
string_push(tmp, val.value);
|
||||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
t_error _word_split(\
|
||||
bool do_split, t_state *state, t_word_result *res, t_vec_str *append)
|
||||
{
|
||||
t_string tmp;
|
||||
t_usize i;
|
||||
|
||||
(void)(state);
|
||||
tmp = string_new(64);
|
||||
i = 0;
|
||||
while (i < res->value.len)
|
||||
string_push(&tmp, res->value.buffer[i++].value);
|
||||
vec_str_push(append, tmp.buf);
|
||||
return (NO_ERROR);
|
||||
{
|
||||
if (_word_split_loop(do_split, res->value.buffer[i++], append, &tmp))
|
||||
return (string_free(tmp), ERROR);
|
||||
}
|
||||
if (!do_split)
|
||||
vec_str_push(append, tmp.buf);
|
||||
else
|
||||
string_free(tmp);
|
||||
return ((void)state, NO_ERROR);
|
||||
}
|
||||
|
||||
t_error _word_into_str(t_ast_node self, t_state *state, t_vec_str *append)
|
||||
t_error _word_into_str(t_ast_node self, t_state *state, t_vec_str *ap)
|
||||
{
|
||||
t_word_result res;
|
||||
|
||||
if (self == NULL || state == NULL || append == NULL
|
||||
if (self == NULL || state == NULL || ap == NULL
|
||||
|| self->kind != AST_WORD)
|
||||
return (ERROR);
|
||||
if (_word_is_star(&self->data.word))
|
||||
return (_word_handle_star(&self->data.word, state, append));
|
||||
return (_word_handle_star(&self->data.word, state, ap));
|
||||
if (run_word(&self->data.word, state, &res))
|
||||
return (ERROR);
|
||||
if (_word_pass_quote(state, &res, append))
|
||||
if (_word_split(self->data.word.kind == AST_WORD_NO_QUOTE, state, &res, ap))
|
||||
return (ERROR);
|
||||
return (vec_estr_free(res.value), NO_ERROR);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,21 +6,21 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/09/14 12:30:09 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/10/13 17:59:35 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/10/14 15:07:42 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "app/env.h"
|
||||
#include "exec/_run_ast.h"
|
||||
#include "line/line.h"
|
||||
#include "me/fs/fs.h"
|
||||
#include "me/os/os.h"
|
||||
#include "me/printf/printf.h"
|
||||
#include "me/str/str.h"
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include "line/line.h"
|
||||
#include "me/types.h"
|
||||
#include <errno.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
t_error _redirection_fd(\
|
||||
t_spawn_info *info, t_state *state, t_ast_node red);
|
||||
|
|
@ -55,12 +55,20 @@ t_error _setup_redirection(\
|
|||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
static t_error _err_cmd(t_str bpath)
|
||||
{
|
||||
me_eprintf("failed to execute %s\n", bpath);
|
||||
str_free(bpath);
|
||||
return (ERROR);
|
||||
}
|
||||
|
||||
t_error _spawn_cmd_and_run_end(\
|
||||
t_spawn_info info, t_state *state, \
|
||||
t_cmd_pipe cmd_pipe, t_command_result *out)
|
||||
{
|
||||
struct s_ffree_state ffree;
|
||||
int status;
|
||||
t_str bpath;
|
||||
|
||||
status = 0;
|
||||
ffree = (struct s_ffree_state){.state = state, .cmd_pipe = cmd_pipe};
|
||||
|
|
@ -68,8 +76,10 @@ t_error _spawn_cmd_and_run_end(\
|
|||
info.forked_free = (void (*)(void *))_ffree_func;
|
||||
signal(SIGINT, SIG_IGN);
|
||||
signal(SIGQUIT, SIG_IGN);
|
||||
bpath = str_clone(info.binary_path);
|
||||
if (spawn_process(info, &out->process))
|
||||
return (close_fd(cmd_pipe.input), out->exit = 127, ERROR);
|
||||
return (close_fd(cmd_pipe.input), out->exit = 127, _err_cmd(bpath));
|
||||
str_free(bpath);
|
||||
if (cmd_pipe.create_output || cmd_pipe.input != NULL)
|
||||
return (out->exit = -1, NO_ERROR);
|
||||
if (waitpid(out->process.pid, &status, 0) == -1 && errno != ECHILD)
|
||||
|
|
@ -88,8 +98,6 @@ t_error _spawn_cmd_and_run(t_vec_str args, t_redirections redirs,
|
|||
t_spawn_info info;
|
||||
|
||||
info = (t_spawn_info){};
|
||||
for (t_usize i = 0; i < args.len; i++)
|
||||
printf("args[%zu] = %s\n", i, args.buffer[i]);
|
||||
if (_setup_redirection(&info, state, redirs.cmd_pipe, &redirs.redirections))
|
||||
return (ERROR);
|
||||
redirs.redirections.len = 0;
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/09/14 12:30:09 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/10/13 13:58:11 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/10/14 15:10:40 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -14,6 +14,7 @@
|
|||
#include "exec/_run_ast.h"
|
||||
#include "me/fs/fs.h"
|
||||
#include "me/os/os.h"
|
||||
#include "me/printf/printf.h"
|
||||
#include "me/str/str.h"
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
|
@ -21,8 +22,11 @@
|
|||
#include "line/line.h"
|
||||
#include "me/types.h"
|
||||
|
||||
#define ARG "Error: too may args for redirection\n"
|
||||
#define ERR "Error: failed to open %s\n"
|
||||
|
||||
t_error _redir_input(\
|
||||
t_spawn_info *info, t_state *state, t_vec_str *fname, t_ast_node red)
|
||||
t_spawn_info *info, t_state *state, t_vec_str *fn, t_ast_node red)
|
||||
{
|
||||
t_fd *red_fd;
|
||||
|
||||
|
|
@ -30,20 +34,20 @@ t_error _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))
|
||||
if (_ast_into_str(red->data.file_redirection.output, state, fn))
|
||||
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 (fn->len != 1)
|
||||
return (me_eprintf(ARG), vec_str_free(*fn), ERROR);
|
||||
red_fd = open_fd(fn->buffer[0], FD_READ, O_CLOEXEC, 0);
|
||||
if (red_fd == NULL)
|
||||
return (vec_str_free(*fname), ERROR);
|
||||
return (me_eprintf(ERR, fn->buffer[0]), vec_str_free(*fn), 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_spawn_info *info, t_state *state, t_vec_str *fn, t_ast_node red)
|
||||
{
|
||||
t_fd *red_fd;
|
||||
|
||||
|
|
@ -51,21 +55,21 @@ t_error _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))
|
||||
if (_ast_into_str(red->data.file_redirection.output, state, fn))
|
||||
return (ERROR);
|
||||
if (fname->len != 1)
|
||||
return (vec_str_free(*fname), ERROR);
|
||||
red_fd = open_fd(fname->buffer[0], FD_WRITE, \
|
||||
if (fn->len != 1)
|
||||
return (me_eprintf(ARG), vec_str_free(*fn), ERROR);
|
||||
red_fd = open_fd(fn->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);
|
||||
return (me_eprintf(ERR, fn->buffer[0]), vec_str_free(*fn), 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_spawn_info *info, t_state *state, t_vec_str *fn, t_ast_node red)
|
||||
{
|
||||
t_fd *red_fd;
|
||||
|
||||
|
|
@ -73,14 +77,14 @@ t_error _redir_output_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))
|
||||
if (_ast_into_str(red->data.file_redirection.output, state, fn))
|
||||
return (ERROR);
|
||||
if (fname->len != 1)
|
||||
return (vec_str_free(*fname), ERROR);
|
||||
red_fd = open_fd(fname->buffer[0], FD_WRITE, \
|
||||
if (fn->len != 1)
|
||||
return (me_eprintf(ARG), vec_str_free(*fn), ERROR);
|
||||
red_fd = open_fd(fn->buffer[0], FD_WRITE, \
|
||||
O_APPEND | O_CREAT | O_CLOEXEC, FP_ALL_READ | FP_ALL_WRITE);
|
||||
if (red_fd == NULL)
|
||||
return (vec_str_free(*fname), ERROR);
|
||||
return (me_eprintf(ERR, fn->buffer[0]), vec_str_free(*fn), ERROR);
|
||||
info->stdout = fd(red_fd);
|
||||
}
|
||||
return (NO_ERROR);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/09/14 12:32:37 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/10/13 18:01:06 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/10/14 15:02:28 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -51,6 +51,7 @@ void _wait_pipeline(t_vec_pid pids, t_state *state, t_pipeline_result *out)
|
|||
waitpid_status = 0;
|
||||
while (waitpid(pids.buffer[pids.len - 1], &waitpid_status, 0) < 0 \
|
||||
&& errno != ECHILD)
|
||||
;
|
||||
if (errno == ECHILD)
|
||||
waitpid_status = 0;
|
||||
while (waitpid(-1, NULL, 0) != -1)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue