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/07/03 20:38:29 by maiboyer #+# #+# */
/* Updated: 2024/10/10 17:17:58 by maiboyer ### ########.fr */
/* Updated: 2024/10/11 22:37:25 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/14 17:57:57 by rparodi #+# #+# */
/* Updated: 2024/10/10 18:51:29 by maiboyer ### ########.fr */
/* Updated: 2024/10/11 22:38:07 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -139,7 +139,8 @@ t_error _ast_get_str__command_substitution(\
t_ast_node elem, t_word_iterator *state, t_vec_estr *out);
t_error _ast_get_str__expansion(\
t_ast_node elem, t_word_iterator *state, t_vec_estr *out);
t_error _ast_get_str__raw(t_ast_node elem, t_word_iterator *state, t_vec_estr *out);
t_error _ast_get_str__raw(\
t_ast_node elem, t_word_iterator *state, t_vec_estr *out);
t_error _ast_get_str__raw__double_quote(\
t_ast_node elem, t_word_iterator *state, t_vec_estr *out);
t_error _ast_get_str__raw__no_quote(\
@ -176,12 +177,7 @@ t_error _run_get_exit_code(\
t_ast_node self, t_state *state, int *out);
t_error _spawn_cmd_and_run(t_vec_str args, t_redirections redirs, \
t_state *state, t_command_result *out);
t_error _word_into_str(t_ast_node self, t_state *state, t_vec_str *append);
t_error run_command(\
t_ast_command *command, t_state *state, t_cmd_pipe cmd_pipe, \
t_command_result *out);

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)
{
return (getpid());
}
#else
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,7 +15,7 @@
#include "me/str/str.h"
#include "me/convert/numbers_to_str.h"
// non bonus only returns 1
// 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));
}

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/11 10:56:02 by maiboyer #+# #+# */
/* Updated: 2024/10/11 11:30:59 by maiboyer ### ########.fr */
/* Updated: 2024/10/11 22:44:32 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,7 +17,8 @@
#include <stdlib.h>
t_error _format_u64(t_num_str args, t_str *out);
//if (_format_u64((t_num_str){.value = tmp, .is_nonnegative = false, .base = "0123456789ABCDEF", .prefix = ""}, &str))
//if (_format_u64((t_num_str){.value = tmp, .is_nonnegative = false, .base
//= "0123456789ABCDEF", .prefix = ""}, &str))
// continue;
int main(int argc, char *argv[])