diff --git a/ast/src/print_ast/ast_print.c b/ast/src/print_ast/ast_print.c index 13a42d01..317cebce 100644 --- a/ast/src/print_ast/ast_print.c +++ b/ast/src/print_ast/ast_print.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ @@ -45,7 +45,7 @@ static inline void ast_print_block1(t_ast_node self); static inline void ast_print_block2(t_ast_node self); static inline void ast_print_block3(t_ast_node self); -static inline void ast_print_notdone(t_ast_node self) +static inline void ast_print_notdone(t_ast_node self) { printf(" "); (void)(self); diff --git a/exec/include/exec/_run_ast.h b/exec/include/exec/_run_ast.h index 58b55e07..532eba4c 100644 --- a/exec/include/exec/_run_ast.h +++ b/exec/include/exec/_run_ast.h @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); diff --git a/exec/src/builtins/echo.c b/exec/src/builtins/echo.c index 25fc2482..a670d50e 100644 --- a/exec/src/builtins/echo.c +++ b/exec/src/builtins/echo.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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++]); diff --git a/exec/src/run_ast/_ast_into_str.c b/exec/src/run_ast/_ast_into_str.c index 6c16724f..6693f9dd 100644 --- a/exec/src/run_ast/_ast_into_str.c +++ b/exec/src/run_ast/_ast_into_str.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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) diff --git a/exec/src/run_ast/_get_pid.c b/exec/src/run_ast/_get_pid.c index ff68b9fb..2d3ce393 100644 --- a/exec/src/run_ast/_get_pid.c +++ b/exec/src/run_ast/_get_pid.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 diff --git a/exec/src/run_ast/run_expansion_builtin.c b/exec/src/run_ast/run_expansion_builtin.c index c4cb6225..a4dd775e 100644 --- a/exec/src/run_ast/run_expansion_builtin.c +++ b/exec/src/run_ast/run_expansion_builtin.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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)); } diff --git a/test/src/main.c b/test/src/main.c index d390610f..cf1aeb9e 100644 --- a/test/src/main.c +++ b/test/src/main.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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,12 +17,13 @@ #include 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[]) +int main(int argc, char *argv[]) { - t_usize i; + t_usize i; t_u64 tmp; t_str str; @@ -33,7 +34,7 @@ int main(int argc, char *argv[]) { tmp = atoll(argv[i++]); if (u64_to_str(tmp, &str)) - continue; + continue ; printf("%lu -> %s\n", tmp, str); mem_free(str); }