Make all functions comply with the 42 Norm
authorLukas Jiriste <ljiriste@student.42prague.com>
Sun, 1 Sep 2024 06:50:38 +0000 (08:50 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Sun, 1 Sep 2024 07:00:36 +0000 (09:00 +0200)
src/builtins/export.c
src/execution.c
src/main.c

index 14a866336438d45de0cd8801d81599fa1ae6b7d1..a8a6d5a1d5a62edf58eedb37845972f99702fc98 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: lnikolov <lnikolov@student.42prague.com    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/08/23 09:40:38 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/08/30 14:08:30 by lnikolov         ###   ########.fr       */
+/*   Updated: 2024/09/01 08:55:31 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include <stdlib.h>
 #include <unistd.h>
 
-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;
index 81c78288d26d933c89e2c9e9637c0837bb8f8e38..0fd24fd022ba938c73dfd917c76b6dbc8da36873 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: lnikolov <lnikolov@student.42prague.com    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/07/21 08:57:54 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/08/31 18:35:42 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/09/01 08:34:42 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -207,41 +207,6 @@ static int write_line_to_pipe(int pipe_fd, const char *line,
        return (0);
 }
 
-/*
-static char    *unquote_delimiter(const char *str)
-{
-       size_t  i;
-       char    *res;
-       char    quote_char;
-
-       res = ft_strdup(str);
-       if (!res)
-               return (NULL);
-       i = 0;
-       quote_char = '\0';
-       while (res[i])
-       {
-               if ((res[i] == '"' || res[i] == '\''))
-               {
-                       if (!quote_char)
-                       {
-                               quote_char = res[i];
-                               ft_memmove(res + i, res + i + 1, ft_strlen(res + i + 1) + 1);
-                               continue ;
-                       }
-                       else if (res[i] == quote_char)
-                       {
-                               quote_char = '\0';
-                               ft_memmove(res + i, res + i + 1, ft_strlen(res + i + 1) + 1);
-                               continue ;
-                       }
-               }
-               ++i;
-       }
-       return (res);
-}
-*/
-
 static char    *here_file_getline(void)
 {
        ft_printf("> ");
@@ -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(&quote, 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,
index 1516de7649f256520762ccbd21ce97787ddfd2f7..e5e6f702119cfa91ae8aa9d668a47cb4e00bd894 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: lnikolov <lnikolov@student.42prague.com    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/04/26 13:11:47 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/08/31 12:52:28 by lnikolov         ###   ########.fr       */
+/*   Updated: 2024/09/01 08:25:02 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -90,10 +90,29 @@ const char  **envp = environ;
 int    main(int argc, char **argv)
 */
 
+void   main_loop(t_execution_env *env)
+{
+       char                    *line;
+
+       setup_terminal();
+       set_input_handler();
+       line = get_line();
+       if (!line)
+       {
+               env->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;