From d300bff66c664a8c2a51fda65282aa2c780e0ae5 Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Sun, 1 Sep 2024 08:50:38 +0200 Subject: [PATCH] Make all functions comply with the 42 Norm --- src/builtins/export.c | 40 +++++++++++++++-------------- src/execution.c | 59 ++++++++++--------------------------------- src/main.c | 38 ++++++++++++++++------------ 3 files changed, 57 insertions(+), 80 deletions(-) diff --git a/src/builtins/export.c b/src/builtins/export.c index 14a8663..a8a6d5a 100644 --- a/src/builtins/export.c +++ b/src/builtins/export.c @@ -6,7 +6,7 @@ /* By: lnikolov #include -static int export_single(const char *var, t_execution_env *env) +static int export_name(const char *var, const char *name, t_execution_env *env) { - char *name; char *value; const char *const_value; int res; - if (!has_valid_name(var)) - { - ft_dprintf(STDERR_FILENO, - "export: '%s': not a valid identifier\n", var); - return (1); - } - name = get_var_name(var); - if (!name) - { - free(name); - return (1); - } if (var[ft_strlen(name)] == '=') const_value = var + ft_strlen(name) + 1; else @@ -44,17 +31,32 @@ static int export_single(const char *var, t_execution_env *env) } value = ft_strdup(const_value); if (!value) - { - free(name); return (1); - } unset_single(name, env); res = set_var_value(&env->vars->exported, name, value); - free(name); free(value); return (res); } +static int export_single(const char *var, t_execution_env *env) +{ + char *name; + int res; + + if (!has_valid_name(var)) + { + ft_dprintf(STDERR_FILENO, + "export: '%s': not a valid identifier\n", var); + return (1); + } + name = get_var_name(var); + if (!name) + return (1); + res = export_name(var, name, env); + free(name); + return (res); +} + int export(__attribute__((unused)) int argc, char **argv, t_execution_env *env) { int res; diff --git a/src/execution.c b/src/execution.c index 81c7828..0fd24fd 100644 --- a/src/execution.c +++ b/src/execution.c @@ -6,7 +6,7 @@ /* By: lnikolov "); @@ -399,6 +364,8 @@ static int append_value(t_vec *exp_str, const char *value, int enquote_result) { int error; + if (!value) + return (1); if (enquote_result) { error = ft_vec_append(exp_str, "'") != success; @@ -427,6 +394,14 @@ int set_quote(char *quote, char c) return (0); } +int is_expandable(const char *str) +{ + return (str[0] == '$' + && (ft_isalpha(str[1]) + || str[1] == '_' + || str[1] == '?')); +} + int add_word(t_vec *exp_str, const char *word, const t_execution_env *env, int enquote_result) { @@ -440,13 +415,9 @@ int add_word(t_vec *exp_str, const char *word, while (word[i]) { set_quote("e, word[i]); - if (word[i] == '$' && quote != '\'' && (ft_isalnum(word[i + 1]) - || word[i + 1] == '_' || word[i + 1] == '?')) + if (is_expandable(word + (i++)) && quote != '\'') { - ++i; value = get_value(word, &i, env); - if (!value) - return (1); if (append_value(exp_str, value, enquote_result)) { free(value); @@ -455,12 +426,10 @@ int add_word(t_vec *exp_str, const char *word, free(value); } else - if (ft_vec_append(exp_str, word + (i++)) != success) + if (ft_vec_append(exp_str, word + i - 1) != success) return (1); } - if (ft_vec_append(exp_str, &space) != success) - return (1); - return (0); + return (ft_vec_append(exp_str, &space) != success); } int expand_cmd(t_vec *exp_str, diff --git a/src/main.c b/src/main.c index 1516de7..e5e6f70 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: lnikolov ret_val = 0; + env->exit = 1; + return ; + } + set_execution_handler(); + regenerate_terminal(); + handle_input(&line, env); + free(line); + return ; +} + int main(int argc, __attribute__((unused)) char **argv, char **envp) { int res; - char *line; t_execution_env env; if (argc > 1) @@ -107,20 +126,7 @@ int main(int argc, __attribute__((unused)) char **argv, char **envp) return (2); } while (env.exit == 0) - { - setup_terminal(); - set_input_handler(); - line = get_line(); - if (!line) - { - env.ret_val = 0; - break ; - } - set_execution_handler(); - regenerate_terminal(); - handle_input(&line, &env); - free(line); - } + main_loop(&env); if (!env.subshell) ft_printf("exit\n"); res = env.ret_val; -- 2.30.2