/* ::: :::::::: */
/* 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 */
/* */
/* ************************************************************************** */
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);
/* ::: :::::::: */
/* 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 */
/* */
/* ************************************************************************** */
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);
+}
/* +:+ +:+ +:+ */
/* 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;
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;
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;
++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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* 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] == '=');
+}