Move struct managing declarations to new file
authorLukáš Jiřiště <jiriste@icpf.cas.cz>
Mon, 26 Aug 2024 07:44:55 +0000 (09:44 +0200)
committerLukáš Jiřiště <jiriste@icpf.cas.cz>
Mon, 26 Aug 2024 09:41:53 +0000 (11:41 +0200)
Also move var managing functions from execution.c to vars.c.

inc/execution.h
inc/minishell.h
inc/minishell_structs.h [new file with mode: 0644]
src/env.c
src/execution.c
src/helpers.c
src/input_handling.c
src/main.c
src/vars.c
src/wildcards.c

index 9e6a39ae8cf6d4ad882b141df06bc244e178b16f..5e4d6a54691fe17de623fbdbba3caa2a39dd8aa8 100644 (file)
@@ -6,7 +6,7 @@
 /*   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);
index 6f30ad7d414a5ba3f5649fa20adc30e8667f99d9..368990c3365292799b85180bb9dd33754e9299a8 100644 (file)
@@ -6,52 +6,16 @@
 /*   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);
@@ -64,13 +28,6 @@ int          tokenize(char **line, t_vec *tokens);
 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
diff --git a/inc/minishell_structs.h b/inc/minishell_structs.h
new file mode 100644 (file)
index 0000000..eb44ef1
--- /dev/null
@@ -0,0 +1,67 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   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
index 236cfdf144141ee355a4b572b8420b681473c92f..ad5dca6a68c644cac93797ee400db7dab67268ac 100644 (file)
--- a/src/env.c
+++ b/src/env.c
@@ -6,11 +6,11 @@
 /*   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>
 
index a43309676d1c28e69f474c7ad5775e3ba7d0628a..3a8efda05feb9390f776c114c2c1e1565b4acfcb 100644 (file)
@@ -6,12 +6,13 @@
 /*   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));
@@ -163,63 +151,6 @@ int        add_redirection_file(t_vec *redirections, t_parse_tree_node *io_file, int fd
        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;
@@ -620,39 +551,6 @@ char       **expand(t_parse_tree_node *simple_command, const t_execution_env *env)
        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);
index 3f193e150f583747f7a7a459f7c027ed533431e6..02dec682ae6c2f2da6091213dfdd6b5e335a4103 100644 (file)
@@ -6,11 +6,11 @@
 /*   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)
@@ -18,3 +18,9 @@ void  free_token(void *token)
        free(((t_token *)token)->str);
        return ;
 }
+
+void   free_str(void *str)
+{
+       free(*(char **)str);
+       return ;
+}
index 9e98b552df814240ca9a6ffe114657e261c78ef5..d89287026280f3cebf6f4c8ec8dd79bc801df412 100644 (file)
@@ -6,10 +6,11 @@
 /*   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>
index d990bff847e286b1623de95ac79d7630336b13de..75762e774a3728438221af2544cfa8a206cd6d52 100644 (file)
@@ -6,10 +6,11 @@
 /*   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>
index 3a11e63f5e3fd1fb148f920cb9aa1dca4a59dab3..bebd8bf9e61488762f6316fc9e7cecc4cd1a8f28 100644 (file)
@@ -6,11 +6,11 @@
 /*   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>
 
@@ -71,15 +71,112 @@ int        init_vars(t_vars *vars, char **envp)
        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));
+}
index e386bc782fd67721a51fd99584867c86c78c90c9..7159aafa06f0bd30f00cc9fd94aa568d4d442bbf 100644 (file)
@@ -6,10 +6,11 @@
 /*   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>