Make vars.c comply with the 42 Norm
authorLilia-42 <nikolovalilianenkova@gmail.com>
Sat, 31 Aug 2024 12:47:38 +0000 (14:47 +0200)
committerLilia-42 <nikolovalilianenkova@gmail.com>
Sat, 31 Aug 2024 12:49:00 +0000 (14:49 +0200)
It was split to vars.c and vars_helpers.c

inc/minishell_structs.h
src/env.c
src/vars.c
src/vars_helpers.c [new file with mode: 0644]

index 3c04174d7bdca37cf9a107c71c39553dc0ef3822..d38c1cc88786d49a3f905b200738cd7884187f85 100644 (file)
@@ -3,10 +3,10 @@
 /*                                                        :::      ::::::::   */
 /*   minishell_structs.h                                :+:      :+:    :+:   */
 /*                                                    +:+ +:+         +:+     */
-/*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
+/*   By: lnikolov <lnikolov@student.42prague.com    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/08/26 09:08:46 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/08/30 17:30:11 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/08/31 14:29:55 by lnikolov         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -58,8 +58,6 @@ int                   init_tree(t_tree *tree);
 int                    has_valid_name(const char *str);
 int                    is_assignment_word(const char *str);
 int                    cmp_var_name(const char *a, const char *b);
-int                    add_var_line(t_vec *vec, const char *line);
-int                    add_var(t_vec *vec, const char *key, const char *value);
 int                    set_var_value(t_vec *vars, const char *name, const char *value);
 int                    set_env_var_value(t_execution_env *env, const char *name, const char *value);
 ssize_t                get_var_index(const t_vec *vars, const char *var_name);
index 42f7b37f1f55eec80f2270827e91e59c53c777c1..a5632be9537f1637553097ad4fbbb9ff405d3393 100644 (file)
--- a/src/env.c
+++ b/src/env.c
@@ -3,10 +3,10 @@
 /*                                                        :::      ::::::::   */
 /*   env.c                                              :+:      :+:    :+:   */
 /*                                                    +:+ +:+         +:+     */
-/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*   By: lnikolov <lnikolov@student.42prague.com    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/07/21 21:24:52 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/08/30 17:31:12 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/08/31 14:47:11 by lnikolov         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -42,3 +42,24 @@ void clean_env(t_execution_env *env)
        ft_vec_free(&env->child_pids, NULL);
        return ;
 }
+
+int    set_env_var_value(t_execution_env *env, const char *name, const char *value)
+{
+       if (get_var_value(&env->vars->other, name))
+               return (set_var_value(&env->vars->other, name, value));
+       else if (get_var_value(&env->vars->exported, name))
+               return (set_var_value(&env->vars->exported, name, value));
+       else
+               return (set_var_value(&env->vars->other, name, value));
+}
+
+const char     *get_env_var_value(const t_execution_env *env, const char *var_name)
+{
+       const char      *res;
+
+       res = get_var_value(&env->vars->other, var_name);
+       if (res)
+               return (res);
+       res = get_var_value(&env->vars->exported, var_name);
+       return (res);
+}
index 089d31c6f4035a63403d9356e342e776d3c70928..dc88fb83e16e801b4233c1a09eaccefd1ea39a44 100644 (file)
@@ -5,8 +5,8 @@
 /*                                                    +:+ +:+         +:+     */
 /*   By: lnikolov <lnikolov@student.42prague.com    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
-/*   Created: 2024/05/02 13:21:32 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/08/30 14:30:49 by lnikolov         ###   ########.fr       */
+/*   Created: 2024/08/31 14:35:06 by lnikolov          #+#    #+#             */
+/*   Updated: 2024/08/31 14:44:27 by lnikolov         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include "libft.h"
 #include <stdlib.h>
 
-// This function returns 0 when str does not contain valid variable name
-// in front of = else it returns the length of the valid name
-int    has_valid_name(const char *str)
-{
-       size_t  i;
-
-       if (!str)
-               return (0);
-       if (ft_isdigit(str[0]))
-               return (0);
-       i = 0;
-       while (str[i])
-       {
-               if (str[i] == '=')
-                       return (i);
-               if (!ft_isalnum(str[i]) && str[i] != '_')
-                       return (0);
-               ++i;
-       }
-       return (i);
-}
-
-int    is_assignment_word(const char *str)
-{
-       size_t  len;
-
-       len = has_valid_name(str);
-       return (len > 0 && str[len] == '=');
-}
-
-int    add_var_line(t_vec *vec, const char *line)
-{
-       char    *tmp;
-
-       tmp = ft_strdup(line);
-       if (!tmp)
-               return (1);
-       if (ft_vec_insert(vec, &tmp, vec->size - 1) != success)
-       {
-               free(tmp);
-               return (1);
-       }
-       return (0);
-}
-
-// The keyval_pair is freed only in case of error
-// When allocation fails in ft_strcal_alloc, it is freed automatically
-// When allocation the insert fails, it needs to be freed here
-// When everything goes smoothly, the memory is freed when the vec
-// erases this entry
-int    add_var(t_vec *vec, const char *key, const char *value)
-{
-       char    *keyval_pair;
-
-       keyval_pair = ft_strdup(key);
-       if (!keyval_pair)
-               return (1);
-       if (!ft_strcat_alloc(&keyval_pair, "="))
-               return (1);
-       if (!ft_strcat_alloc(&keyval_pair, value))
-               return (1);
-       if (ft_vec_insert(vec, &keyval_pair, vec->size - 1) != success)
-       {
-               free(keyval_pair);
-               return (1);
-       }
-       return (0);
-}
-
-int    init_vars(t_vars *vars, char **envp)
-{
-       char    *tmp;
-
-       ft_vec_init(&vars->exported, sizeof(char *));
-       ft_vec_init(&vars->other, sizeof(char *));
-       tmp = NULL;
-       ft_vec_append(&vars->exported, &tmp);
-       ft_vec_append(&vars->other, &tmp);
-       while (*envp)
-       {
-               if (add_var_line(&vars->exported, *envp))
-                       return (1);
-               ++envp;
-       }
-       return (0);
-}
-
-void   clean_vars(t_vars *vars)
-{
-       ft_vec_free(&vars->exported, free_str);
-       ft_vec_free(&vars->other, free_str);
-       return ;
-}
-
-int    cmp_var_name(const char *a, const char *b)
-{
-       int             namelen;
-       char    *eq_ptr;
-
-       eq_ptr = ft_strchr(a, '=');
-       namelen = eq_ptr - a;
-       eq_ptr = ft_strchr(b, '=');
-       if (namelen != eq_ptr - b)
-               return (1);
-       return (ft_strncmp(a, b, namelen));
-}
-
 int    set_var_value(t_vec *vars, const char *name, const char *value)
 {
        ssize_t i;
@@ -144,16 +37,6 @@ int set_var_value(t_vec *vars, const char *name, const char *value)
        return (0);
 }
 
-int    set_env_var_value(t_execution_env *env, const char *name, const char *value)
-{
-       if (get_var_value(&env->vars->other, name))
-               return (set_var_value(&env->vars->other, name, value));
-       else if (get_var_value(&env->vars->exported, name))
-               return (set_var_value(&env->vars->exported, name, value));
-       else
-               return (set_var_value(&env->vars->other, name, value));
-}
-
 ssize_t        get_var_index(const t_vec *vars, const char *var_name)
 {
        size_t                          len;
@@ -190,17 +73,6 @@ const char  *get_var_value(const t_vec *vars, const char *var_name)
        return (NULL);
 }
 
-const char     *get_env_var_value(const t_execution_env *env, const char *var_name)
-{
-       const char      *res;
-
-       res = get_var_value(&env->vars->other, var_name);
-       if (res)
-               return (res);
-       res = get_var_value(&env->vars->exported, var_name);
-       return (res);
-}
-
 char   *get_var_name(const char *line)
 {
        size_t  i;
@@ -210,3 +82,18 @@ char        *get_var_name(const char *line)
                ++i;
        return (ft_strndup(line, i));
 }
+
+static int     add_var_line(t_vec *vec, const char *line)
+{
+       char    *tmp;
+
+       tmp = ft_strdup(line);
+       if (!tmp)
+               return (1);
+       if (ft_vec_insert(vec, &tmp, vec->size - 1) != success)
+       {
+               free(tmp);
+               return (1);
+       }
+       return (0);
+}
diff --git a/src/vars_helpers.c b/src/vars_helpers.c
new file mode 100644 (file)
index 0000000..df1e3bf
--- /dev/null
@@ -0,0 +1,107 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   vars_helpers.c                                     :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: lnikolov <lnikolov@student.42prague.com    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/05/02 13:21:32 by ljiriste          #+#    #+#             */
+/*   Updated: 2024/08/31 14:45:08 by lnikolov         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "minishell_structs.h"
+#include "libft.h"
+#include <stdlib.h>
+
+// The keyval_pair is freed only in case of error
+// When allocation fails in ft_strcal_alloc, it is freed automatically
+// When allocation the insert fails, it needs to be freed here
+// When everything goes smoothly, the memory is freed when the vec
+// erases this entry
+// static int  add_var(t_vec *vec, const char *key, const char *value)
+// {
+//     char    *keyval_pair;
+
+//     keyval_pair = ft_strdup(key);
+//     if (!keyval_pair)
+//             return (1);
+//     if (!ft_strcat_alloc(&keyval_pair, "="))
+//             return (1);
+//     if (!ft_strcat_alloc(&keyval_pair, value))
+//             return (1);
+//     if (ft_vec_insert(vec, &keyval_pair, vec->size - 1) != success)
+//     {
+//             free(keyval_pair);
+//             return (1);
+//     }
+//     return (0);
+// }
+
+int    init_vars(t_vars *vars, char **envp)
+{
+       char    *tmp;
+
+       ft_vec_init(&vars->exported, sizeof(char *));
+       ft_vec_init(&vars->other, sizeof(char *));
+       tmp = NULL;
+       ft_vec_append(&vars->exported, &tmp);
+       ft_vec_append(&vars->other, &tmp);
+       while (*envp)
+       {
+               if (add_var_line(&vars->exported, *envp))
+                       return (1);
+               ++envp;
+       }
+       return (0);
+}
+
+void   clean_vars(t_vars *vars)
+{
+       ft_vec_free(&vars->exported, free_str);
+       ft_vec_free(&vars->other, free_str);
+       return ;
+}
+
+int    cmp_var_name(const char *a, const char *b)
+{
+       int             namelen;
+       char    *eq_ptr;
+
+       eq_ptr = ft_strchr(a, '=');
+       namelen = eq_ptr - a;
+       eq_ptr = ft_strchr(b, '=');
+       if (namelen != eq_ptr - b)
+               return (1);
+       return (ft_strncmp(a, b, namelen));
+}
+
+// This function returns 0 when str does not contain valid variable name
+// in front of = else it returns the length of the valid name
+int    has_valid_name(const char *str)
+{
+       size_t  i;
+
+       if (!str)
+               return (0);
+       if (ft_isdigit(str[0]))
+               return (0);
+       i = 0;
+       while (str[i])
+       {
+               if (str[i] == '=')
+                       return (i);
+               if (!ft_isalnum(str[i]) && str[i] != '_')
+                       return (0);
+               ++i;
+       }
+       return (i);
+}
+
+int    is_assignment_word(const char *str)
+{
+       size_t  len;
+
+       len = has_valid_name(str);
+       return (len > 0 && str[len] == '=');
+}