fixes: first pass of norminette on easy stuff

This commit is contained in:
maix0 2024-10-11 22:45:46 +02:00
parent 1e4185a544
commit 85a327a0c4
7 changed files with 73 additions and 58 deletions

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/10 18:43:18 by maiboyer #+# #+# */
/* Updated: 2024/10/11 14:54:48 by rparodi ### ########.fr */
/* Updated: 2024/10/11 22:43:35 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,11 +16,29 @@
#include "me/string/string.h"
#include "me/types.h"
void _skip_options(t_usize *i, bool *print_line, t_builtin_spawn_info *info)
{
t_usize j;
while (*i < info->args.len && info->args.buffer[*i][0] == '-')
{
j = 1;
while (info->args.buffer[*i][j] == 'n')
j++;
if (info->args.buffer[*i][j] == '\0')
{
*print_line = false;
(*i)++;
}
else
break ;
}
}
t_error builtin_echo__(\
t_state *state, t_builtin_spawn_info info, t_i32 *exit_code)
{
t_usize i;
t_usize j;
bool print_line;
t_string s;
@ -28,19 +46,7 @@ t_error builtin_echo__(\
print_line = true;
i = 1;
s = string_new(1024);
while (i < info.args.len && info.args.buffer[i][0] == '-')
{
j = 1;
while (info.args.buffer[i][j] == 'n')
j++;
if (info.args.buffer[i][j] == '\0')
{
print_line = false;
i++;
}
else
break ;
}
_skip_options(&i, &print_line, &info);
while (i < info.args.len - 1)
{
string_push(&s, info.args.buffer[i++]);

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/14 12:26:51 by maiboyer #+# #+# */
/* Updated: 2024/10/11 22:33:02 by maiboyer ### ########.fr */
/* Updated: 2024/10/11 22:38:42 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -63,7 +63,7 @@ t_error _word_into_str(t_ast_node self, t_state *state, t_vec_str *append)
|| self->kind != AST_WORD)
return (ERROR);
if (_word_is_star(&self->data.word))
return (dprintf(2, "word is star !\n"), _word_handle_star(&self->data.word, state, append));
return (_word_handle_star(&self->data.word, state, append));
if (run_word(&self->data.word, state, &res))
return (ERROR);
if (res.kind == AST_WORD_NO_QUOTE)

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/06 14:25:20 by maiboyer #+# #+# */
/* Updated: 2024/10/06 14:41:51 by maiboyer ### ########.fr */
/* Updated: 2024/10/11 22:38:30 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,14 +14,17 @@
#include "me/types.h"
#if BONUS
t_pid get_self_pid(void)
t_pid get_self_pid(void)
{
return (getpid());
}
#else
t_pid get_self_pid(void)
t_pid get_self_pid(void)
{
return (1);
return (-1);
}
#endif

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/14 12:38:38 by maiboyer #+# #+# */
/* Updated: 2024/10/11 22:16:10 by maiboyer ### ########.fr */
/* Updated: 2024/10/11 22:41:10 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,8 +15,8 @@
#include "me/str/str.h"
#include "me/convert/numbers_to_str.h"
// non bonus only returns 1
t_pid get_self_pid(void);
// non bonus only returns -1
t_pid get_self_pid(void);
bool _is_special_var(t_ast_expansion *self)
{
@ -35,6 +35,33 @@ bool _is_special_var(t_ast_expansion *self)
return (false);
}
static inline t_error _run_expansion_special_var_inner(\
t_ast_expansion *self, t_state *state, \
t_expansion_result *out, char name)
{
(void)(self);
(void)(state);
if (name == '#')
{
*out = (t_expansion_result){.exists = true, .value = NULL};
if (i32_to_str(1, &out->value))
return (ERROR);
}
if (name == '!')
{
*out = (t_expansion_result){.exists = true, .value = NULL};
if (i32_to_str(1, &out->value))
return (ERROR);
}
if (name == '$')
{
*out = (t_expansion_result){.exists = true, .value = NULL};
if (i32_to_str(get_self_pid(), &out->value))
return (ERROR);
}
return (NO_ERROR);
}
// return pid of last run program
// return `argc - 1` bc we don't care about argv[0]
// return pid of self (the shell)
@ -54,26 +81,8 @@ t_error _run_expansion_special_var(t_ast_expansion *self, t_state *state,
if (name == '?')
{
*out = (t_expansion_result){.exists = true, .value = NULL};
if (i32_to_str(state->last_exit, &out->value)) // TODO: fix this shit
if (i32_to_str(state->last_exit, &out->value))
return (ERROR);
}
if (name == '#')
{
*out = (t_expansion_result){.exists = true, .value = NULL};
if (i32_to_str(1, &out->value))
return (ERROR);
}
if (name == '!')
{
*out = (t_expansion_result){.exists = true, .value = NULL};
if (i32_to_str(1, &out->value))
return (ERROR);
}
if (name == '$')
{
*out = (t_expansion_result){.exists = true, .value = NULL};
if (i32_to_str(get_self_pid(), &out->value))
return (ERROR);
}
return (NO_ERROR);
return (_run_expansion_special_var_inner(self, state, out, name));
}