Fixed stuff

This commit is contained in:
Maix0 2024-05-18 18:49:15 +02:00
parent be1d3ff2d3
commit f4e8596f3b
15 changed files with 130 additions and 103 deletions

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/14 18:02:12 by maiboyer #+# #+# */
/* Updated: 2024/05/18 16:36:55 by maiboyer ### ########.fr */
/* Updated: 2024/05/18 18:22:32 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -232,7 +232,7 @@ t_error alloc_new_page(struct s_allocator_melloc *alloc, t_usize page_size)
void *m_alloc_error(struct s_allocator_melloc *self, t_str msg)
{
(void)(self);
self->uninit((void *)self);
me_abort(msg);
return (NULL);
}
@ -294,7 +294,7 @@ void *m_realloc(struct s_allocator_melloc *self, void *ptr, t_usize size)
old_size = chunk->size;
chunk->size += next->size + sizeof(*next);
vg_mem_defined(next, next->size + sizeof(*next));
//mem_set_zero(next, next->size + sizeof(*next));
mem_set_zero(next, next->size + sizeof(*next));
vg_block_resize((void *)chunk + sizeof(*chunk), old_size,
chunk->size);
vg_mem_no_access(chunk, sizeof(*chunk));

View file

@ -111,5 +111,9 @@ void insert_hashmap_env(t_hashmap_env *hmap, t_str key,
prev->next = entry;
}
else
{
hmap->drop(entry->kv);
entry->kv.key = key;
entry->kv.val = value;
}
}

View file

@ -39,7 +39,7 @@
#define STACK_VERSION_NONE ((t_stack_version)-1)
#define TS_DECODE_ERROR (-1)
#if false
#if true
# undef malloc
# undef calloc
# undef realloc

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/04 18:32:50 by maiboyer #+# #+# */
/* Updated: 2024/05/14 18:46:13 by maiboyer ### ########.fr */
/* Updated: 2024/05/18 18:44:24 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,15 +14,22 @@
#include "me/buffered_str/buf_str.h"
#include "me/hash/hasher.h"
#include "me/hashmap/hashmap_env.h"
#include "me/mem/mem.h"
#include "me/string/str_clone.h"
#include "me/string/str_compare.h"
#include "me/string/str_len.h"
#include "me/types.h"
#include "me/vec/vec_str.h"
#include "me/mem/mem.h"
#include <stdlib.h>
static void _hash_str(t_hasher *hasher, t_str *s)
{
hasher_write_str(hasher, *s);
t_usize len;
len = str_len(*s);
hasher_write_u64(hasher, (t_u64)len);
hasher_write_bytes(hasher, (t_u8 *)*s, len);
hasher_write_u8(hasher, 0);
}
static bool _cmp_str(t_str *s1, t_str *s2)
@ -33,9 +40,9 @@ static bool _cmp_str(t_str *s1, t_str *s2)
static void _free_env(t_kv_env kv)
{
if (kv.key)
free(kv.key);
mem_free(kv.key);
if (kv.val)
free(kv.val);
mem_free(kv.val);
}
t_hashmap_env *create_env_map(void)
@ -47,15 +54,17 @@ t_error _build_envp_iterator(t_usize idx, const t_str *key, t_str *val,
void *ctx)
{
struct s_build_envp_state *s;
t_str push;
(void)(idx);
s = ctx;
if (push_str_buffer(&s->buf, *key) && push_str_char(&s->buf, '=') &&
if (push_str_buffer(&s->buf, *key) || push_str_buffer(&s->buf, "=") ||
push_str_buffer(&s->buf, *val))
return (vec_str_free(s->out), ERROR);
if (vec_str_push(&s->out, s->buf.buf))
push = str_clone(s->buf.buf);
if (vec_str_push(&s->out, push))
return (str_free(s->buf), ERROR);
s->buf = alloc_new_buffer(50);
str_clear(&s->buf);
return (NO_ERROR);
}
@ -63,12 +72,12 @@ 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))mem_free);
state.buf = alloc_new_buffer(50);
state.buf = alloc_new_buffer(8096);
state.out = vec_str_new(1024, (void (*)(t_str))mem_free);
if (hashmap_env_iter(envs, _build_envp_iterator, &state))
return (ERROR);
return (str_free(state.buf), printf("iter failed\n"), ERROR);
if (vec_str_push(&state.out, NULL))
return (ERROR);
return (str_free(state.buf), printf("coun't push null\n"), ERROR);
*envp = state.out;
str_free(state.buf);
return (NO_ERROR);

View file

@ -6,7 +6,7 @@
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/09 15:00:53 by rparodi #+# #+# */
/* Updated: 2024/05/18 16:54:29 by rparodi ### ########.fr */
/* Updated: 2024/05/18 18:31:34 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -28,7 +28,8 @@ t_error handle_command(t_node *self, t_utils *shcat, t_i32 *out_exit_code)
t_spawn_info spawn_info;
t_str tmp;
spawn_info.arguments = vec_str_new(64, (void (*)(t_str))mem_free); // TODO: FIX VECTOR
spawn_info.arguments =
vec_str_new(64, (void (*)(t_str))mem_free); // TODO: FIX VECTOR
if (self->kind != sym_command)
return (ERROR);
i = 0;
@ -37,7 +38,8 @@ t_error handle_command(t_node *self, t_utils *shcat, t_i32 *out_exit_code)
if (self->childs[i].kind == sym_command_name)
{
spawn_info.binary_path = str_clone(node_getstr(&self->childs[i]));
vec_str_push(&spawn_info.arguments, str_clone(spawn_info.binary_path));
vec_str_push(&spawn_info.arguments,
str_clone(spawn_info.binary_path));
printf("%s\n", spawn_info.arguments.buffer[0]);
}
else if (self->childs[i].kind == sym_file_redirect)
@ -55,14 +57,15 @@ t_error handle_command(t_node *self, t_utils *shcat, t_i32 *out_exit_code)
}
i++;
}
printf("%zu\n", spawn_info.arguments.len);
// printf("%zu\n", spawn_info.arguments.len);
vec_str_push(&spawn_info.arguments, NULL);
for (i = 0; i < spawn_info.arguments.len; i++)
printf("[%zu]\t%s\n", i, spawn_info.arguments.buffer[i]);
// for (i = 0; i < spawn_info.arguments.len; i++)
// printf("[%zu]\t%s\n", i, spawn_info.arguments.buffer[i]);
spawn_info.stdin = inherited();
spawn_info.stdout = inherited();
spawn_info.stderr = inherited();
spawn_info.forked_free = NULL;
printf("building envp\n");
if (build_envp(shcat->env, &spawn_info.environement))
return (vec_str_free(spawn_info.arguments), ERROR);
if (spawn_process(spawn_info, &shcat->ret))

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/29 11:35:51 by rparodi #+# #+# */
/* Updated: 2024/05/14 18:46:30 by maiboyer ### ########.fr */
/* Updated: 2024/05/18 18:11:59 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -35,13 +35,15 @@ void ft_free_strs(t_str *strs)
void ft_free_utils(t_utils *s)
{
(void)(s);
if (s->str_input)
free(s->str_input);
if (s->path)
ft_free_strs(s->path);
if (s->env)
{
printf("Dropping HashMap\n");
drop_hashmap_env(s->env);
}
ts_parser_delete(s->parser.parser);
}

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/28 14:40:38 by rparodi #+# #+# */
/* Updated: 2024/05/18 17:09:37 by rparodi ### ########.fr */
/* Updated: 2024/05/18 18:37:39 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,13 +19,12 @@
#include "me/hashmap/hashmap_env.h"
#include "me/mem/mem.h"
#include "me/string/str_len.h"
#include "me/string/str_split.h"
#include "me/string/str_substring.h"
#include "me/types.h"
#include "minishell.h"
#include "parser/api.h"
#include <sys/_types/_null.h>
#include <sys/types.h>
#include "me/string/str_split.h"
#include "me/mem/mem.h"
#undef free
#undef malloc
@ -41,26 +40,44 @@ void ts_parser_delete(t_first_parser *self);
void ts_parser_set_language(t_first_parser *self, t_language *lang);
// Foutre envp dans env
// Chaque elemenet d'envp split au premier =
// Chaque elemenet d'envp split au premier =
// cle avant le =
// data apres le =
t_error populate_env(t_hashmap_env *env, t_str envp[])
t_error split_str_first(t_const_str s, char splitter, t_str *before,
t_str *after)
{
t_usize i;
t_str *temp;
t_usize i;
if (s == NULL || before == NULL || after == NULL || splitter == '\0')
return (ERROR);
i = 0;
while (s[i] != '\0' && s[i] != splitter)
i++;
if (s[i] == '\0')
return (ERROR);
if (i == 0)
i = 1;
*before = str_substring(s, 0, i);
*after = str_substring(s, i + 1, ~(t_usize)0);
return (NO_ERROR);
}
t_error populate_env(t_hashmap_env *env, t_str envp[])
{
t_usize i;
t_str temp[2];
i = 0;
temp = NULL;
if (envp == NULL || env == NULL)
return (ERROR);
return (printf("given a nullptr\n"), ERROR);
while (envp[i] != NULL)
{
temp = str_split(envp[i], '=');
if (temp[0] == NULL || temp[1] == NULL)
if (split_str_first(envp[i], '=', &temp[0], &temp[1]))
return (ERROR);
if (temp[0] == NULL || temp[1] == NULL)
return (printf("TEMP NULL\n"), ERROR);
insert_hashmap_env(env, temp[0], temp[1]);
mem_free(temp);
i++;
}
return (NO_ERROR);
@ -224,8 +241,10 @@ t_i32 main(t_i32 argc, t_str argv[], t_str envp[])
utils = (t_utils){};
utils.parser = create_myparser();
utils.env = create_env_map();
if (install_signal())
return (1);
// if (install_signal())
// return (1);
if (populate_env(utils.env, envp))
me_abort("unable to build ENV hashmap");
utils.name_shell = "\001\x1B[93m\002"
"42sh"
"\001\x1B[32m\002"

View file

@ -111,5 +111,9 @@ void insert_hashmap_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ key,
prev->next = entry;
}
else
{
hmap->drop(entry->kv);
entry->kv.key = key;
entry->kv.val = value;
}
}

View file

@ -34,18 +34,9 @@ t_vec_buf_str vec_buf_str_new(t_usize capacity,
/// Return true in case of an error
t_error vec_buf_str_push(t_vec_buf_str *vec, t_buffer_str element)
{
size_t new_capacity;
if (vec == NULL)
return (ERROR);
if (vec->len + 1 > vec->capacity)
{
new_capacity = (vec->capacity * 3) / 2 + 1;
while (vec->len + 1 > new_capacity)
new_capacity = (new_capacity * 3) / 2 + 1;
vec->buffer = mem_realloc_array(vec->buffer, new_capacity, sizeof(t_buffer_str));
vec->capacity = new_capacity;
}
vec_buf_str_reserve(vec, vec->len + 1);
vec->buffer[vec->len] = element;
vec->len += 1;
return (NO_ERROR);

View file

@ -34,18 +34,9 @@ t_vec_str vec_str_new(t_usize capacity,
/// Return true in case of an error
t_error vec_str_push(t_vec_str *vec, t_str element)
{
size_t new_capacity;
if (vec == NULL)
return (ERROR);
if (vec->len + 1 > vec->capacity)
{
new_capacity = (vec->capacity * 3) / 2 + 1;
while (vec->len + 1 > new_capacity)
new_capacity = (new_capacity * 3) / 2 + 1;
vec->buffer = mem_realloc_array(vec->buffer, new_capacity, sizeof(t_str));
vec->capacity = new_capacity;
}
vec_str_reserve(vec, vec->len + 1);
vec->buffer[vec->len] = element;
vec->len += 1;
return (NO_ERROR);

View file

@ -34,18 +34,9 @@ t_vec_u8 vec_u8_new(t_usize capacity,
/// Return true in case of an error
t_error vec_u8_push(t_vec_u8 *vec, t_u8 element)
{
size_t new_capacity;
if (vec == NULL)
return (ERROR);
if (vec->len + 1 > vec->capacity)
{
new_capacity = (vec->capacity * 3) / 2 + 1;
while (vec->len + 1 > new_capacity)
new_capacity = (new_capacity * 3) / 2 + 1;
vec->buffer = mem_realloc_array(vec->buffer, new_capacity, sizeof(t_u8));
vec->capacity = new_capacity;
}
vec_u8_reserve(vec, vec->len + 1);
vec->buffer[vec->len] = element;
vec->len += 1;
return (NO_ERROR);

View file

@ -6,12 +6,13 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 17:52:12 by maiboyer #+# #+# */
/* Updated: 2024/05/14 15:33:43 by maiboyer ### ########.fr */
/* Updated: 2024/05/18 18:06:37 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/mem/mem.h"
#include "me/buffered_str/buf_str.h"
#include "me/mem/mem.h"
#include "me/mem/mem_copy.h"
#include "me/mem/mem_set_zero.h"
#include "me/string/str_l_cat.h"
#include "me/string/str_len.h"
@ -27,11 +28,15 @@ t_error str_reserve(t_buffer_str *buf, t_usize size)
return (ERROR);
if (buf->capacity >= size)
return (NO_ERROR);
while (size > buf->capacity)
new_capacity = (buf->capacity * 3) / 2 + 1;
temp_buffer = mem_realloc_array(buf->buf, new_capacity, sizeof(char));
new_capacity = buf->capacity;
while (size > new_capacity)
new_capacity = (new_capacity * 3) / 2 + 1;
temp_buffer = mem_alloc_array(new_capacity, sizeof(char));
if (temp_buffer == NULL)
return (ERROR);
mem_set_zero(temp_buffer, new_capacity);
mem_copy(temp_buffer, buf->buf, buf->len);
mem_free(buf->buf);
buf->buf = temp_buffer;
buf->capacity = new_capacity;
return (NO_ERROR);
@ -46,8 +51,8 @@ t_error push_str_buffer(t_buffer_str *buf, t_const_str to_push)
to_push_len = str_len(to_push);
if (str_reserve(buf, buf->len + to_push_len + 2))
return (ERROR);
buf->len += to_push_len;
str_l_cat(buf->buf, to_push, buf->capacity);
buf->len += to_push_len;
return (NO_ERROR);
}

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/14 18:26:27 by maiboyer #+# #+# */
/* Updated: 2024/05/16 16:24:54 by maiboyer ### ########.fr */
/* Updated: 2024/05/18 18:48:25 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -26,6 +26,7 @@ t_allocator *global_allocator(void)
{
init = true;
global_alloc = m_init();
// global_alloc = lc_init();
}
return (&global_alloc);
}

View file

@ -6,16 +6,16 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/03 16:22:41 by maiboyer #+# #+# */
/* Updated: 2024/05/14 18:42:59 by maiboyer ### ########.fr */
/* Updated: 2024/05/18 18:33:53 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/buffered_str/buf_str.h"
#include "me/os/pipe.h"
#include "me/os/process.h"
#include "me/buffered_str/buf_str.h"
#include "me/mem/mem.h"
#include "me/os/pipe.h"
#include "me/string/str_find_chr.h"
#include "me/string/str_n_compare.h"
#include "me/mem/mem.h"
#include "me/string/str_split.h"
#include "me/types.h"
#include "me/vec/vec_str.h"
@ -23,14 +23,14 @@
#include <stdlib.h>
#include <unistd.h>
bool find_path(const t_str *s);
bool find_null(const t_str *s);
bool str_start_with(t_const_str s, t_const_str prefix);
t_error handle_redirections(t_spawn_info *info, t_process *process);
bool find_path(const t_str *s);
bool find_null(const t_str *s);
bool str_start_with(t_const_str s, t_const_str prefix);
t_error handle_redirections(t_spawn_info *info, t_process *process);
t_error spawn_process_exec(t_spawn_info info, t_process *process)
t_error spawn_process_exec(t_spawn_info info, t_process *process)
{
bool res;
bool res;
if (info.forked_free)
info.forked_free(info.forked_free_args);
@ -52,11 +52,11 @@ t_error spawn_process_exec(t_spawn_info info, t_process *process)
return (NO_ERROR);
}
t_error in_path(t_spawn_info *info, t_process *process, t_const_str path,
t_buffer_str *s)
t_error in_path(t_spawn_info *info, t_process *process, t_const_str path,
t_buffer_str *s)
{
t_str *splitted_path;
t_usize sp_index;
t_str *splitted_path;
t_usize sp_index;
splitted_path = str_split(path + 5, ':');
if (splitted_path == NULL)
@ -70,7 +70,7 @@ t_error in_path(t_spawn_info *info, t_process *process, t_const_str path,
push_str_buffer(s, info->binary_path);
sp_index++;
if (access(s->buf, X_OK | R_OK) == 0)
break ;
break;
}
sp_index = 0;
while (splitted_path[sp_index])
@ -79,22 +79,23 @@ t_error in_path(t_spawn_info *info, t_process *process, t_const_str path,
return (NO_ERROR);
}
t_error find_binary(t_spawn_info *info, t_process *process)
t_error find_binary(t_spawn_info *info, t_process *process)
{
t_usize p_idx;
t_buffer_str s;
t_usize p_idx;
t_buffer_str s;
(void)(process);
if (info->binary_path == NULL)
return (ERROR);
s = alloc_new_buffer(256);
if (str_start_with(info->binary_path, "/")
|| str_find_chr(info->binary_path, '/') != NULL)
if (str_start_with(info->binary_path, "/") ||
str_find_chr(info->binary_path, '/') != NULL)
push_str_buffer(&s, info->binary_path);
else
{
if (vec_str_find(&info->environement, find_path, &p_idx))
return (str_free(s), ERROR);
printf("finding in path\n");
if (in_path(info, process, info->environement.buffer[p_idx], &s))
return (ERROR);
}
@ -107,7 +108,7 @@ t_error find_binary(t_spawn_info *info, t_process *process)
return (str_free(s), ERROR);
}
static void cleanup(t_spawn_info info, t_process *process, bool cleanup_process)
static void cleanup(t_spawn_info info, t_process *process, bool cleanup_process)
{
if (cleanup_process && process->stdin.tag != INVALID)
close(process->stdin.vals.ro.fd);
@ -123,7 +124,7 @@ static void cleanup(t_spawn_info info, t_process *process, bool cleanup_process)
mem_free(info.binary_path);
}
t_error spawn_process(t_spawn_info info, t_process *process)
t_error spawn_process(t_spawn_info info, t_process *process)
{
if (handle_redirections(&info, process))
return (cleanup(info, process, true), ERROR);

View file

@ -6,19 +6,20 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/04 22:25:44 by maiboyer #+# #+# */
/* Updated: 2024/05/09 16:57:24 by rparodi ### ########.fr */
/* Updated: 2024/05/18 18:38:01 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/types.h"
#include <stdbool.h>
#include <stdio.h>
bool find_null(const t_str *s)
bool find_null(const t_str *s)
{
return (s == NULL);
}
bool str_start_with(t_const_str s, t_const_str prefix)
bool str_start_with(t_const_str s, t_const_str prefix)
{
while (*prefix && *s)
{
@ -28,9 +29,14 @@ bool str_start_with(t_const_str s, t_const_str prefix)
return (*prefix == '\0');
}
bool find_path(const t_str *s)
bool find_path(const t_str *s)
{
t_str ss;
if (*s == NULL)
return (false);
return (str_start_with(*s, "PATH="));
ss = *s;
printf("%s\n", ss);
return (ss[0] == 'P' && ss[1] == 'A' && ss[2] == 'T' && ss[3] == 'H' &&
ss[4] == '=');
}