Added env hashmap with build_env function to build envp

This commit is contained in:
Maix0 2024-05-04 19:21:56 +02:00
parent f35e986145
commit f86947a852
54 changed files with 2010 additions and 105 deletions

74
sources/env.c Normal file
View file

@ -0,0 +1,74 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* env.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/04 18:32:50 by maiboyer #+# #+# */
/* Updated: 2024/05/04 19:16:37 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "app/env.h"
#include "me/buffered_str/buf_str.h"
#include "me/hash/hasher.h"
#include "me/hashmap/hashmap_env.h"
#include "me/string/str_compare.h"
#include "me/types.h"
#include "me/vec/vec_str.h"
#include <stdlib.h>
static void _hash_str(t_hasher *hasher, t_str *s)
{
hasher_write_str(hasher, *s);
}
static bool _cmp_str(t_str *s1, t_str *s2)
{
return (str_compare(*s1, *s2));
}
static void _free_env(t_kv_env kv)
{
if (kv.key)
free(kv.key);
if (kv.val)
free(kv.val);
}
t_hashmap_env *create_env_map(void)
{
return (new_hashmap_env(_hash_str, _cmp_str, _free_env));
}
t_error _build_envp_iterator(t_usize idx, const t_str *key, t_str *val,
void *ctx)
{
struct s_build_envp_state *s;
(void)(idx);
s = ctx;
if (push_str_buffer(&s->buf, *key) && push_str_char(&s->buf, '=') &&
push_str_buffer(&s->buf, *val))
return (vec_str_free(s->out), ERROR);
if (vec_str_push(&s->out, s->buf.buf))
return (str_free(s->buf), ERROR);
s->buf = alloc_new_buffer(50);
return (NO_ERROR);
}
t_error build_envp(t_hashmap_env *envs, t_vec_str *envp)
{
struct s_build_envp_state state;
state.out = vec_str_new(envs->num_buckets, (void (*)(t_str))free);
state.buf = alloc_new_buffer(50);
if (hashmap_env_iter(envs, _build_envp_iterator, &state))
return (ERROR);
if (vec_str_push(&state.out, NULL))
return (ERROR);
*envp = state.out;
str_free(state.buf);
return (NO_ERROR);
}

View file

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* handle_expension.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/03 15:24:25 by maiboyer #+# #+# */
/* Updated: 2024/05/04 18:28:39 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "app/node/handle_expension.h"
#include "gmr/symbols.h"
#include "me/types.h"
t_error handle_expension_complex(t_node *self, t_utils *shcat, t_str *ret)
{
(void)(self);
(void)(shcat);
(void)(ret);
return (ERROR);
}
t_error handle_expension_simple(t_node *self, t_utils *shcat, t_str *ret)
{
(void)(self);
(void)(shcat);
(void)(ret);
return (ERROR);
}
t_error handle_expension(t_node *self, t_utils *shcat, t_str *ret)
{
if (self->childs_count != 0 && self->childs[0].kind == anon_sym_DOLLAR)
return (handle_expension_simple(self, shcat, ret));
if (self->childs_count != 0 &&
self->childs[0].kind == anon_sym_DOLLAR_LBRACE)
return (handle_expension_complex(self, shcat, ret));
return (ERROR);
}

View file

@ -6,47 +6,47 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/13 20:26:13 by rparodi #+# #+# */
/* Updated: 2024/04/30 21:31:27 by maiboyer ### ########.fr */
/* Updated: 2024/05/04 18:59:58 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/minishell.h"
void ft_exec_cmd(t_utils *utils, t_str cmd, t_str cmd_args[])
{
if (execve(cmd, cmd_args, utils->envp) == -1)
{
printf("ERROR >\n execve\n %s\n", cmd);
ft_free_strs((char **)cmd_args);
ft_exit(utils, 1);
}
}
void ft_other_cmd(t_utils *shcat, t_usize i, t_usize prev_i)
{
pid_t pid;
t_i32 options;
t_str *args;
t_usize k;
// t_str cmd;
k = prev_i;
args = (t_str *)malloc(sizeof(t_str) * (i + 2));
while (prev_i < i)
{
printf("ft_other_cmd = %s\n", shcat->strs_input[prev_i]);
args[k] = malloc(ft_strlen(shcat->strs_input[prev_i]));
ft_strlcpy(args[k], shcat->strs_input[prev_i], ft_strlen(shcat->strs_input[prev_i]));
prev_i++;
k++;
}
args[k] = NULL;
// cmd = ft_check_cmds(shcat, args[tmp]);
options = 0;
pid = fork();
if (pid == -1)
ft_exit(shcat, 1);
// if (pid == 0)
// ft_exec_cmd(shcat, cmd, args);
waitpid(pid, NULL, options);
}
// void ft_exec_cmd(t_utils *utils, t_str cmd, t_str cmd_args[])
// {
// if (execve(cmd, cmd_args, utils->envp) == -1)
// {
// printf("ERROR >\n execve\n %s\n", cmd);
// ft_free_strs((char **)cmd_args);
// ft_exit(utils, 1);
// }
// }
//
// void ft_other_cmd(t_utils *shcat, t_usize i, t_usize prev_i)
// {
// pid_t pid;
// t_i32 options;
// t_str *args;
// t_usize k;
// // t_str cmd;
//
// k = prev_i;
// args = (t_str *)malloc(sizeof(t_str) * (i + 2));
// while (prev_i < i)
// {
// printf("ft_other_cmd = %s\n", shcat->strs_input[prev_i]);
// args[k] = malloc(ft_strlen(shcat->strs_input[prev_i]));
// ft_strlcpy(args[k], shcat->strs_input[prev_i], ft_strlen(shcat->strs_input[prev_i]));
// prev_i++;
// k++;
// }
// args[k] = NULL;
// // cmd = ft_check_cmds(shcat, args[tmp]);
// options = 0;
// pid = fork();
// if (pid == -1)
// ft_exit(shcat, 1);
// // if (pid == 0)
// // ft_exec_cmd(shcat, cmd, args);
// waitpid(pid, NULL, options);
// }

View file

@ -6,15 +6,16 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/28 14:40:38 by rparodi #+# #+# */
/* Updated: 2024/05/02 15:26:55 by maiboyer ### ########.fr */
/* Updated: 2024/05/04 19:21:16 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/minishell.h"
#include "app/env.h"
#include "app/node.h"
#include "app/signal_handler.h"
#include "gmr/symbols.h"
#include "me/string/str_len.h"
#include "minishell.h"
#include "parser/api.h"
t_first_parser *ts_parser_new();
@ -55,27 +56,27 @@ t_node parse_str(t_parser *parser, t_const_str input)
return (parse_to_nodes(parser->parser, input));
}
void ft_check(t_utils *shcat, char **input)
{
t_usize i;
t_usize prev_i;
// void ft_check(t_utils *shcat, char **input)
// {
// t_usize i;
// t_usize prev_i;
//
// i = 0;
// prev_i = 0;
// while (input[i] != NULL)
// {
// if (ft_strcmp(input[i], "exit") == 0)
// ft_exit(shcat, 0);
// else if (ft_strcmp(input[i], "pwd") == 0)
// ft_pwd();
// else
// ft_other_cmd(shcat, i, prev_i);
// prev_i = i;
// i++;
// }
// }
i = 0;
prev_i = 0;
while (input[i] != NULL)
{
if (ft_strcmp(input[i], "exit") == 0)
ft_exit(shcat, 0);
else if (ft_strcmp(input[i], "pwd") == 0)
ft_pwd();
else
ft_other_cmd(shcat, i, prev_i);
prev_i = i;
i++;
}
}
t_error handle_concat(t_node *self, t_utils* shcat, t_str *ret);
t_error handle_concat(t_node *self, t_utils *shcat, t_str *ret);
void print_node_concat(t_node *self)
{
@ -163,6 +164,7 @@ t_i32 main(t_i32 argc, t_str argv[], t_str envp[])
if (install_signal())
return (1);
utils = (t_utils){};
utils.env = create_env_map();
utils.parser = create_myparser();
utils.name_shell = "\001\x1B[93m\002"
"42sh"