Also move var managing functions from execution.c to vars.c.
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/02 17:40:19 by ljiriste #+# #+# */
-/* Updated: 2024/08/23 18:01:03 by ljiriste ### ########.fr */
+/* Updated: 2024/08/26 11:41:25 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
-const char *get_env_var_value(const t_execution_env *env,
- const char *var_name);
-int set_env_var_value(t_execution_env *env, const char *var_name,
- const char *new_value);
-char *get_var_name(const char *line);
-ssize_t get_var_index(const t_vec *vars, const char *var_name);
-const char *get_var_value(const t_vec *vars, const char *var_name);
-int set_var_value(t_vec *vars, const char *name, const char *value);
-
int cd(int argc, char **argv, t_execution_env *env);
int echo(int argc, char **argv);
int pwd(void);
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/02 13:22:57 by ljiriste #+# #+# */
-/* Updated: 2024/08/23 16:15:16 by ljiriste ### ########.fr */
+/* Updated: 2024/08/26 09:23:40 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MINISHELL_H
# define MINISHELL_H
+# include "minishell_structs.h"
# include "libft.h"
-typedef struct s_vars
-{
- t_vec exported;
- t_vec other;
-} t_vars;
-
-typedef struct s_redirection
-{
- int from_to_fds[2];
-} t_redirection;
-
-typedef struct s_execution_env
-{
- int stdin_fd;
- int stdout_fd;
- int ret_val;
- t_vars *vars;
-} t_execution_env;
-
-typedef struct s_wildcard_info
-{
- const char *to_expand;
- const char *current_expand_char;
- const char *entry;
- const char *current_entry_char;
- const char *path;
-} t_wildcard_info;
-
-typedef t_parse_tree_node t_tree;
-
-int init_env(t_execution_env *env, char **envp);
-int init_vars(t_vars *vars, char **envp);
-int add_var_line(t_vec *vec, const char *line);
-int add_var(t_vec *vec, const char *key, const char *value);
-void clean_vars(t_vars *vars);
-void clean_env(t_execution_env *env);
-
void handle_input(char **line, t_execution_env *env);
void unquote_field(char *field);
int parse(t_vec *tokens, t_tree **parse_tree);
int execute(t_tree *parse_tree, t_execution_env *env);
-int init_tree(t_tree *tree);
-void free_tree(t_tree *tree);
-void free_token(void *token);
-void free_str(void *str);
-
-const char *get_env_var_value(const t_execution_env *env, const char *var_name);
char **quoted_split(const char *str);
-
#endif // MINISHELL_H
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* minishell_structs.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/08/26 09:08:46 by ljiriste #+# #+# */
+/* Updated: 2024/08/26 09:31:09 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#ifndef MINISHELL_STRUCTS_H
+# define MINISHELL_STRUCTS_H
+
+#include "libft.h"
+
+typedef struct s_vars
+{
+ t_vec exported;
+ t_vec other;
+} t_vars;
+
+typedef struct s_redirection
+{
+ int from_to_fds[2];
+} t_redirection;
+
+typedef struct s_execution_env
+{
+ int stdin_fd;
+ int stdout_fd;
+ int ret_val;
+ t_vars *vars;
+} t_execution_env;
+
+typedef struct s_wildcard_info
+{
+ const char *to_expand;
+ const char *current_expand_char;
+ const char *entry;
+ const char *current_entry_char;
+ const char *path;
+} t_wildcard_info;
+
+typedef t_parse_tree_node t_tree;
+
+int init_vars(t_vars *vars, char **envp);
+int init_env(t_execution_env *env, char **envp);
+int init_tree(t_tree *tree);
+
+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);
+const char *get_var_value(const t_vec *vars, const char *var_name);
+const char *get_env_var_value(const t_execution_env *env, const char *var_name);
+char *get_var_name(const char *line);
+
+void free_str(void *str);
+void free_token(void *token);
+void clean_vars(t_vars *vars);
+void clean_env(t_execution_env *env);
+
+#endif // MINISHELL_STRUCTS
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/21 21:24:52 by ljiriste #+# #+# */
-/* Updated: 2024/08/02 10:46:15 by ljiriste ### ########.fr */
+/* Updated: 2024/08/26 09:19:22 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
-#include "minishell.h"
+#include "minishell_structs.h"
#include <stdlib.h>
#include <unistd.h>
/* By: lnikolov <lnikolov@student.42prague.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/21 08:57:54 by ljiriste #+# #+# */
-/* Updated: 2024/08/23 17:48:17 by ljiriste ### ########.fr */
+/* Updated: 2024/08/26 09:40:31 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
-#include "minishell.h"
+#include "minishell_structs.h"
#include "execution.h"
+#include "minishell.h"
#include "libft.h"
#include <stdlib.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/wait.h>
-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 is_token_type(const t_parse_tree_node *node, const char *type)
{
return (!ft_strcmp(node->token.type, type));
return (ft_vec_append(redirections, &redir) != success);
}
-char *get_var_name(const char *line)
-{
- size_t i;
-
- i = 0;
- while (ft_isalnum(line[i]) || line[i] == '_')
- ++i;
- return (ft_strndup(line, i));
-}
-
-ssize_t get_var_index(const t_vec *vars, const char *var_name)
-{
- size_t len;
- ssize_t i;
- const char *const *line;
-
- len = ft_strlen(var_name);
- i = 0;
- while ((size_t)i < vars->size)
- {
- line = ft_vec_caccess(vars, i);
- if (!*line)
- break ;
- if (!ft_strncmp(*line, var_name, len) && (*line)[len] == '=')
- return (i);
- ++i;
- }
- return (-1);
-}
-
-const char *get_var_value(const t_vec *vars, const char *var_name)
-{
- size_t len;
- ssize_t i;
- const char *const *line;
-
- len = ft_strlen(var_name);
- i = get_var_index(vars, var_name);
- if (i >= 0)
- {
- line = ft_vec_caccess(vars, i);
- return (*line + len + 1);
- }
- 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);
-}
-
int write_line_to_pipe(int pipe_fd, const char *line, const t_execution_env *env, int expand)
{
size_t i;
return (fields);
}
-int set_var_value(t_vec *vars, const char *name, const char *value)
-{
- ssize_t i;
- char *new_entry;
-
- i = get_var_index(vars, name);
- new_entry = malloc(ft_strlen(name) + 1 + ft_strlen(value) + 1);
- if (!new_entry)
- return (1);
- ft_memcpy(new_entry, name, ft_strlen(name));
- new_entry[ft_strlen(name)] = '=';
- ft_memcpy(new_entry + ft_strlen(name) + 1, value, ft_strlen(value) + 1);
- if (add_var_line(vars, new_entry))
- {
- free(new_entry);
- return (1);
- }
- if (i >= 0)
- ft_vec_erase(vars, i, free_str);
- free(new_entry);
- 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));
-}
-
int assignments_to_env(const t_vec *assignments, t_execution_env *env)
{
return (ft_vec_insert_range(&env->vars->other, assignments->vec, assignments->size, 0) == success);
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/21 16:35:16 by ljiriste #+# #+# */
-/* Updated: 2024/06/21 16:40:21 by ljiriste ### ########.fr */
+/* Updated: 2024/08/26 09:20:18 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
-#include "minishell.h"
+#include "minishell_structs.h"
#include <stdlib.h>
void free_token(void *token)
free(((t_token *)token)->str);
return ;
}
+
+void free_str(void *str)
+{
+ free(*(char **)str);
+ return ;
+}
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/03 09:00:00 by ljiriste #+# #+# */
-/* Updated: 2024/08/08 14:26:59 by ljiriste ### ########.fr */
+/* Updated: 2024/08/26 09:43:17 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
+#include "minishell_structs.h"
#include "minishell.h"
#include "libft.h"
#include <stdlib.h>
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/26 13:11:47 by ljiriste #+# #+# */
-/* Updated: 2024/08/02 18:50:53 by ljiriste ### ########.fr */
+/* Updated: 2024/08/26 09:41:50 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
+#include "minishell_structs.h"
#include "minishell.h"
#include "libft.h"
#include <stdlib.h>
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/02 13:21:32 by ljiriste #+# #+# */
-/* Updated: 2024/08/02 18:34:58 by ljiriste ### ########.fr */
+/* Updated: 2024/08/26 09:20:20 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
-#include "minishell.h"
+#include "minishell_structs.h"
#include "libft.h"
#include <stdlib.h>
return (0);
}
-void free_str(void *str)
-{
- free(*(char **)str);
- return ;
-}
-
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;
+ char *new_entry;
+
+ i = get_var_index(vars, name);
+ new_entry = malloc(ft_strlen(name) + 1 + ft_strlen(value) + 1);
+ if (!new_entry)
+ return (1);
+ ft_memcpy(new_entry, name, ft_strlen(name));
+ new_entry[ft_strlen(name)] = '=';
+ ft_memcpy(new_entry + ft_strlen(name) + 1, value, ft_strlen(value) + 1);
+ if (add_var_line(vars, new_entry))
+ {
+ free(new_entry);
+ return (1);
+ }
+ if (i >= 0)
+ ft_vec_erase(vars, i, free_str);
+ free(new_entry);
+ 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;
+ ssize_t i;
+ const char *const *line;
+
+ len = ft_strlen(var_name);
+ i = 0;
+ while ((size_t)i < vars->size)
+ {
+ line = ft_vec_caccess(vars, i);
+ if (!*line)
+ break ;
+ if (!ft_strncmp(*line, var_name, len) && (*line)[len] == '=')
+ return (i);
+ ++i;
+ }
+ return (-1);
+}
+
+const char *get_var_value(const t_vec *vars, const char *var_name)
+{
+ size_t len;
+ ssize_t i;
+ const char *const *line;
+
+ len = ft_strlen(var_name);
+ i = get_var_index(vars, var_name);
+ if (i >= 0)
+ {
+ line = ft_vec_caccess(vars, i);
+ return (*line + len + 1);
+ }
+ 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 = 0;
+ while (ft_isalnum(line[i]) || line[i] == '_')
+ ++i;
+ return (ft_strndup(line, i));
+}
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/08 10:50:26 by ljiriste #+# #+# */
-/* Updated: 2024/08/23 16:15:58 by ljiriste ### ########.fr */
+/* Updated: 2024/08/26 09:42:45 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
+#include "minishell_structs.h"
#include "minishell.h"
#include "libft.h"
#include <stdlib.h>