Normed the builtins
This commit is contained in:
parent
efb18f605c
commit
ef7677f2ee
5 changed files with 70 additions and 57 deletions
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/08/10 18:43:18 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/08/14 18:14:27 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/08/30 16:51:53 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -17,11 +17,12 @@
|
|||
#include "me/string/string.h"
|
||||
#include "me/types.h"
|
||||
|
||||
#define DEBUG_USAGE \
|
||||
"Usage:\n" \
|
||||
" - print_fd: print the opened file descritors informations"
|
||||
/*#define DEBUG_USAGE\*/
|
||||
/* "Usage:\n"\*/
|
||||
/* " - print_fd: print the opened file descritors informations"*/
|
||||
|
||||
static t_error _debug_fd(t_state *state, t_builtin_spawn_info info, t_i32 *exit_code)
|
||||
static t_error _debug_fd(\
|
||||
t_state *state, t_builtin_spawn_info info, t_i32 *exit_code)
|
||||
{
|
||||
const t_fd_array *fds = get_fd_arrays();
|
||||
t_usize i;
|
||||
|
|
@ -31,18 +32,22 @@ static t_error _debug_fd(t_state *state, t_builtin_spawn_info info, t_i32 *exit_
|
|||
while (i < FILE_SLOT_LEN)
|
||||
{
|
||||
if (fds->storage[i].ty == SLOT_FD)
|
||||
me_printf_fd(info.stderr, " FD[%i] => %s\n", fds->storage[i].slot.fd.fd, fds->storage[i].slot.fd.name);
|
||||
me_printf_fd(info.stderr, " FD[%i] => %s\n", \
|
||||
fds->storage[i].slot.fd.fd, fds->storage[i].slot.fd.name);
|
||||
if (fds->storage[i].ty == SLOT_FILE)
|
||||
me_printf_fd(info.stderr, "FILE[%p] => %s\n", fds->storage[i].slot.file.ptr, fds->storage[i].slot.file.name);
|
||||
me_printf_fd(info.stderr, "FILE[%p] => %s\n", \
|
||||
fds->storage[i].slot.file.ptr, fds->storage[i].slot.file.name);
|
||||
if (fds->storage[i].ty == SLOT_DIR)
|
||||
me_printf_fd(info.stderr, " DIR[%p] => %s\n", fds->storage[i].slot.dir.ptr, fds->storage[i].slot.dir.name);
|
||||
me_printf_fd(info.stderr, " DIR[%p] => %s\n", \
|
||||
fds->storage[i].slot.dir.ptr, fds->storage[i].slot.dir.name);
|
||||
i++;
|
||||
}
|
||||
*exit_code = 0;
|
||||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
t_error builtin_debug_(t_state *state, t_builtin_spawn_info info, t_i32 *exit_code)
|
||||
t_error builtin_debug_(\
|
||||
t_state *state, t_builtin_spawn_info info, t_i32 *exit_code)
|
||||
{
|
||||
if (info.args.len != 2)
|
||||
return (me_printf_fd(info.stdout, DEBUG_USAGE), *exit_code = 1, ERROR);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/08/10 18:43:18 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/08/14 18:15:22 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/08/30 16:52:42 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -18,7 +18,8 @@
|
|||
#include "me/types.h"
|
||||
#include "unistd.h"
|
||||
|
||||
t_error builtin_cd____(t_state *state, t_builtin_spawn_info info, t_i32 *exit_code)
|
||||
t_error builtin_cd____(\
|
||||
t_state *state, t_builtin_spawn_info info, t_i32 *exit_code)
|
||||
{
|
||||
const t_str key = "HOME";
|
||||
t_str *home;
|
||||
|
|
@ -37,6 +38,7 @@ t_error builtin_cd____(t_state *state, t_builtin_spawn_info info, t_i32 *exit_co
|
|||
else
|
||||
path = info.args.buffer[1];
|
||||
if (chdir(path) == -1)
|
||||
return (*exit_code = 2, me_printf_fd(info.stderr, "cd: Unable to change directory\n"), NO_ERROR);
|
||||
return (*exit_code = 2, me_printf_fd(\
|
||||
info.stderr, "cd: Unable to change directory\n"), NO_ERROR);
|
||||
return (*exit_code = 0, NO_ERROR);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/08/10 18:43:18 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/08/14 18:15:30 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/08/30 16:53:28 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -16,7 +16,8 @@
|
|||
#include "me/string/string.h"
|
||||
#include "me/types.h"
|
||||
|
||||
t_error builtin_echo__(t_state *state, t_builtin_spawn_info info, t_i32 *exit_code)
|
||||
t_error builtin_echo__(\
|
||||
t_state *state, t_builtin_spawn_info info, t_i32 *exit_code)
|
||||
{
|
||||
t_usize i;
|
||||
bool print_line;
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/08/10 18:43:18 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/08/14 18:15:05 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/08/30 16:55:02 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -14,7 +14,8 @@
|
|||
#include "me/convert/str_to_numbers.h"
|
||||
#include "me/types.h"
|
||||
|
||||
t_error builtin_exit__(t_state *state, t_builtin_spawn_info info, t_i32 *exit_code)
|
||||
t_error builtin_exit__(\
|
||||
t_state *state, t_builtin_spawn_info info, t_i32 *exit_code)
|
||||
{
|
||||
t_i32 actual_exit_code;
|
||||
|
||||
|
|
@ -22,7 +23,8 @@ t_error builtin_exit__(t_state *state, t_builtin_spawn_info info, t_i32 *exit_co
|
|||
if (info.args.len < 2)
|
||||
actual_exit_code = 0;
|
||||
else if (str_to_i32(info.args.buffer[1], 10, &actual_exit_code))
|
||||
return (printf("info.args.buffer[1] = %s\n", info.args.buffer[1]), ERROR);
|
||||
return (\
|
||||
printf("info.args.buffer[1] = %s\n", info.args.buffer[1]), ERROR);
|
||||
*exit_code = actual_exit_code;
|
||||
me_exit(actual_exit_code);
|
||||
return (NO_ERROR);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/08/07 14:13:41 by rparodi #+# #+# */
|
||||
/* Updated: 2024/08/18 22:54:44 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/08/30 16:59:21 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -44,7 +44,8 @@ static void _assign_export(t_usize idx, t_str *arg, void *vctx)
|
|||
ctx->err = ERROR;
|
||||
}
|
||||
|
||||
static t_error _append_key_to_vec(t_usize _idx, const t_str *key, t_str *value, void *vec)
|
||||
static t_error _append_key_to_vec(\
|
||||
t_usize _idx, const t_str *key, t_str *value, void *vec)
|
||||
{
|
||||
(void)(value);
|
||||
(void)(_idx);
|
||||
|
|
@ -95,12 +96,14 @@ static t_error handle_quotes(t_str raw, t_string *out)
|
|||
return (*out = ret, NO_ERROR);
|
||||
}
|
||||
|
||||
t_error _print_export_env(t_state *state, t_builtin_spawn_info info, t_i32 *exit_code)
|
||||
t_error _print_export_env(\
|
||||
t_state *state, t_builtin_spawn_info info, t_i32 *exit_code)
|
||||
{
|
||||
t_vec_str keys;
|
||||
t_vec_str keys_uniq;
|
||||
t_usize i;
|
||||
t_str *value;
|
||||
t_string buf;
|
||||
|
||||
keys = vec_str_new(16, NULL);
|
||||
hmap_env_iter(state->env, _append_key_to_vec, &keys);
|
||||
|
|
@ -112,13 +115,13 @@ t_error _print_export_env(t_state *state, t_builtin_spawn_info info, t_i32 *exit
|
|||
vec_str_sort(&keys, _sort_str);
|
||||
while (i < keys.len)
|
||||
{
|
||||
while (i < keys.len - 1 && str_compare(keys.buffer[i], keys.buffer[i + 1]))
|
||||
while (i < keys.len - 1 && \
|
||||
str_compare(keys.buffer[i], keys.buffer[i + 1]))
|
||||
i++;
|
||||
vec_str_push(&keys_uniq, keys.buffer[i]);
|
||||
i++;
|
||||
}
|
||||
vec_str_free(keys);
|
||||
|
||||
i = 0;
|
||||
while (i < keys_uniq.len)
|
||||
{
|
||||
|
|
@ -126,15 +129,13 @@ t_error _print_export_env(t_state *state, t_builtin_spawn_info info, t_i32 *exit
|
|||
if (value == NULL)
|
||||
value = hmap_env_get(state->env, &keys_uniq.buffer[i]);
|
||||
if (value == NULL || *value == NULL)
|
||||
{
|
||||
me_printf_fd(info.stdout, "export %s\n", keys_uniq.buffer[i]);
|
||||
}
|
||||
else
|
||||
{
|
||||
t_string buf;
|
||||
if (!handle_quotes(*value, &buf))
|
||||
{
|
||||
me_printf_fd(info.stdout, "export %s='%s'\n", keys_uniq.buffer[i], buf.buf);
|
||||
me_printf_fd(\
|
||||
info.stdout, "export %s='%s'\n", keys_uniq.buffer[i], buf.buf);
|
||||
string_free(buf);
|
||||
}
|
||||
}
|
||||
|
|
@ -145,7 +146,8 @@ t_error _print_export_env(t_state *state, t_builtin_spawn_info info, t_i32 *exit
|
|||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
t_error builtin_export(t_state *state, t_builtin_spawn_info info, t_i32 *exit_code)
|
||||
t_error builtin_export(\
|
||||
t_state *state, t_builtin_spawn_info info, t_i32 *exit_code)
|
||||
{
|
||||
struct s_assign_export_state assign_ctx;
|
||||
|
||||
|
|
@ -155,5 +157,6 @@ t_error builtin_export(t_state *state, t_builtin_spawn_info info, t_i32 *exit_co
|
|||
assign_ctx.err = NO_ERROR;
|
||||
if (info.args.len == 1)
|
||||
return (_print_export_env(state, info, exit_code));
|
||||
return (*exit_code = 0, vec_str_iter(&info.args, _assign_export, &assign_ctx), assign_ctx.err);
|
||||
return (*exit_code = 0, vec_str_iter(\
|
||||
&info.args, _assign_export, &assign_ctx), assign_ctx.err);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue