--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* minishell.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/05/02 13:22:57 by ljiriste #+# #+# */
+/* Updated: 2024/05/02 13:25:41 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#ifndef MINISHELL_H
+# define MINISHELL_H
+
+# include "libft.h"
+
+typedef struct s_vars
+{
+ t_vec exported;
+ t_vec other;
+} t_vars;
+
+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);
+
+#endif // MINISHELL_H
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/26 13:11:47 by ljiriste #+# #+# */
-/* Updated: 2024/04/26 14:13:15 by ljiriste ### ########.fr */
+/* Updated: 2024/05/02 13:54:36 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
+#include "minishell.h"
#include "libft.h"
#include <stdlib.h>
#include <unistd.h> // getcwd
return (line);
}
-void handle_input(__attribute__((unused)) const char *input)
+void handle_input(__attribute__((unused)) const char *input, __attribute__((unused)) t_vars *vars)
{
return ;
}
-int main(void)
+void print_help(void)
+{
+ ft_printf("Minishell should be used without any arguments.\n");
+ return ;
+}
+
+/*
+// This would be more portable than the main with 3 input args
+extern char **environ;
+const char **envp = environ;
+int main(int argc, char **argv)
+*/
+int main(int argc, __attribute__((unused)) char **argv, char **envp)
{
char *line;
+ t_vars vars;
+ if (argc > 1)
+ {
+ print_help();
+ return (1);
+ }
+ if (init_vars(&vars, envp))
+ {
+ clean_vars(&vars);
+ return (2);
+ }
while (1)
{
line = rl_get_line();
if (!line || !ft_strcmp(line, "exit"))
- {
- rl_clear_history();
- ft_printf("exit\n");
- return (0);
- }
- handle_input(line);
+ break ;
+ handle_input(line, &vars);
free(line);
}
+ rl_clear_history();
+ clean_vars(&vars);
+ free(line);
+ ft_printf("exit\n");
+ return (0);
}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* vars.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/05/02 13:21:32 by ljiriste #+# #+# */
+/* Updated: 2024/05/02 13:24:39 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "minishell.h"
+#include "libft.h"
+#include <stdlib.h>
+
+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);
+}
+
+static 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 ;
+}