Normed and fixed stuff

This commit is contained in:
Maieul BOYER 2024-07-30 15:03:00 +02:00
parent b693536a90
commit 970507dfdd
No known key found for this signature in database
2 changed files with 83 additions and 76 deletions

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/30 14:29:42 by rparodi #+# #+# */
/* Updated: 2024/07/30 14:52:31 by rparodi ### ########.fr */
/* Updated: 2024/07/30 14:58:13 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,6 +20,14 @@
#include "parser/api.h"
#include <stdio.h>
void ast_free_arith(t_ast_node elem);
void ast_free_condition(t_ast_node elem);
void ast_free_exec(t_ast_node elem);
void ast_free_loop(t_ast_node elem);
void ast_free_other(t_ast_node elem);
void ast_free_redirection(t_ast_node elem);
void ast_free_arith(t_ast_node elem)
{
if (elem->kind == AST_ARITHMETIC_EXPANSION)
@ -86,13 +94,8 @@ void ast_free_redirection(t_ast_node elem)
}
}
void ast_free(t_ast_node elem)
void ast_free_other(t_ast_node elem)
{
ast_free_arith(elem);
ast_free_loop(elem);
ast_free_condition(elem);
ast_free_exec(elem);
ast_free_redirection(elem);
if (elem->kind == AST_COMPOUND_STATEMENT)
{
vec_ast_free(elem->data.compound_statement.body);
@ -119,6 +122,12 @@ void ast_free(t_ast_node elem)
{
if (elem == NULL)
return ;
ast_free_arith(elem);
ast_free_loop(elem);
ast_free_condition(elem);
ast_free_exec(elem);
ast_free_other(elem);
ast_free_redirection(elem);
if (elem->kind == AST_RAW_STRING)
mem_free(elem->data.raw_string.str);
if (elem->kind == AST_WORD)

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/19 15:53:50 by maiboyer #+# #+# */
/* Updated: 2024/07/07 19:17:05 by maiboyer ### ########.fr */
/* Updated: 2024/07/30 15:01:04 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,23 +15,22 @@
#include "me/str/str.h"
#include "me/types.h"
#include "unistd.h"
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <sys/stat.h>
t_fd_array *get_fd_arrays(void)
t_fd_array *get_fd_arrays(void)
{
static t_fd_array val = {};
static t_fd_array val = {};
return (&val);
}
struct s_file_slot *get_unused_fd_slot(void)
struct s_file_slot *get_unused_fd_slot(void)
{
t_usize i;
t_fd_array *arr;
t_fd_array *arr;
arr = get_fd_arrays();
i = 0;
@ -41,15 +40,15 @@ struct s_file_slot *get_unused_fd_slot(void)
return (&arr->storage[i]);
i++;
}
me_abort(
"Unable to find slot for a file descriptor, increate FILE_SLOT_LEN");
me_abort("Unable to find slot for a file descriptor, " \
"increate FILE_SLOT_LEN");
return (NULL);
}
__attribute__((destructor(201))) void close_all_slots(void)
__attribute__((destructor(201))) void close_all_slots(void)
{
t_usize i;
t_fd_array *arr;
t_fd_array *arr;
arr = get_fd_arrays();
i = 0;
@ -57,10 +56,10 @@ __attribute__((destructor(201))) void close_all_slots(void)
close_slot(&arr->storage[i++]);
}
void close_slot(struct s_file_slot *slot)
void close_slot(struct s_file_slot *slot)
{
if (slot == NULL)
return;
return ;
if (slot->ty == SLOT_UNUSED)
;
else if (slot->ty == SLOT_FD)
@ -82,11 +81,11 @@ void close_slot(struct s_file_slot *slot)
|_| |_____/
*/
t_fd *open_fd(t_str name, t_fd_perm perms, t_file_open_option open_options,
t_file_perm file_perm)
t_fd *open_fd(t_str name, t_fd_perm perms, t_file_open_option open_options,
t_file_perm file_perm)
{
t_fd *fd;
struct s_file_slot *slot;
t_fd *fd;
struct s_file_slot *slot;
int actual_perms;
if (perms & FD_READ && perms & FD_WRITE)
@ -110,15 +109,14 @@ t_fd *open_fd(t_str name, t_fd_perm perms, t_file_open_option open_options,
return (fd);
}
t_error read_fd(t_fd *fd, t_u8 *buffer, t_usize size, t_isize *read_count)
t_error read_fd(t_fd *fd, t_u8 *buffer, t_usize size, t_isize *read_count)
{
t_isize ret;
t_isize false_ret;
t_isize ret;
t_isize false_ret;
if (read_count == NULL)
read_count = &false_ret;
if (fd == NULL || buffer == NULL || fd->fd == -1 ||
!(fd->perms & FD_READ))
if (fd == NULL || buffer == NULL || fd->fd == -1 || !(fd->perms & FD_READ))
return (ERROR);
ret = read(fd->fd, buffer, size);
if (ret == -1)
@ -127,10 +125,10 @@ t_error read_fd(t_fd *fd, t_u8 *buffer, t_usize size, t_isize *read_count)
return (NO_ERROR);
}
t_error write_fd(t_fd *fd, t_u8 *buffer, t_usize size, t_isize *write_count)
t_error write_fd(t_fd *fd, t_u8 *buffer, t_usize size, t_isize *write_count)
{
t_isize ret;
t_isize fake_ret;
t_isize ret;
t_isize fake_ret;
if (write_count == NULL)
write_count = &fake_ret;
@ -143,7 +141,7 @@ t_error write_fd(t_fd *fd, t_u8 *buffer, t_usize size, t_isize *write_count)
return (NO_ERROR);
}
t_error stat_fd(t_fd *fd, t_stat *stat)
t_error stat_fd(t_fd *fd, t_stat *stat)
{
if (fd == NULL || stat == NULL || fd->fd == -1)
return (ERROR);
@ -152,24 +150,24 @@ t_error stat_fd(t_fd *fd, t_stat *stat)
return (NO_ERROR);
}
void close_fd(t_fd *fd)
void close_fd(t_fd *fd)
{
struct s_file_slot *slot;
struct s_file_slot *slot;
if (fd == NULL)
return;
return ;
if (close(fd->fd) == -1)
return;
return ;
slot = (void *)(fd)-offsetof(struct s_file_slot, slot.fd);
mem_set_zero(slot, sizeof(*slot));
}
#define INLINE_BUFFER_SIZE 128
void put_number_fd(t_fd *fd, t_u64 number)
void put_number_fd(t_fd *fd, t_u64 number)
{
t_u8 buffer[INLINE_BUFFER_SIZE];
t_usize i;
t_usize i;
i = 0;
while (number > 0)
@ -181,12 +179,12 @@ void put_number_fd(t_fd *fd, t_u64 number)
write_fd(fd, &buffer[--i], 1, NULL);
}
void put_string_fd(t_fd *fd, t_const_str string)
void put_string_fd(t_fd *fd, t_const_str string)
{
write_fd(fd, (t_u8 *)string, str_len(string), NULL);
}
void put_char_fd(t_fd *fd, t_u8 c)
void put_char_fd(t_fd *fd, t_u8 c)
{
write_fd(fd, &c, 1, NULL);
}
@ -199,10 +197,10 @@ void put_char_fd(t_fd *fd, t_u8 c)
|_____/_____|_| \_\______\_____| |_| \____/|_| \_\ |_|
*/
t_error open_dir(t_str name, t_dir **dir)
t_error open_dir(t_str name, t_dir **dir)
{
t_dir *out;
struct s_file_slot *slot;
t_dir *out;
struct s_file_slot *slot;
slot = get_unused_fd_slot();
out = &slot->slot.dir;
@ -215,9 +213,9 @@ t_error open_dir(t_str name, t_dir **dir)
return (NO_ERROR);
}
t_error read_dir(t_dir *dir, t_dir_entry *out)
t_error read_dir(t_dir *dir, t_dir_entry *out)
{
struct dirent *entry;
struct dirent *entry;
if (dir == NULL || out == NULL || dir->ptr == NULL)
return (ERROR);
@ -229,14 +227,14 @@ t_error read_dir(t_dir *dir, t_dir_entry *out)
return (NO_ERROR);
}
void close_dir(t_dir *dir)
void close_dir(t_dir *dir)
{
struct s_file_slot *slot;
struct s_file_slot *slot;
if (dir == NULL)
return;
return ;
if (closedir(dir->ptr) == -1)
return;
return ;
slot = (void *)(dir)-offsetof(struct s_file_slot, slot.dir);
mem_set_zero(slot, sizeof(*slot));
}
@ -249,10 +247,10 @@ void close_dir(t_dir *dir)
|_| |_____|______|______|
*/
t_error open_file(t_str name, t_mode mode, t_file **file)
t_error open_file(t_str name, t_mode mode, t_file **file)
{
t_file *out;
struct s_file_slot *slot;
t_file *out;
struct s_file_slot *slot;
slot = get_unused_fd_slot();
out = &slot->slot.file;
@ -265,11 +263,11 @@ t_error open_file(t_str name, t_mode mode, t_file **file)
return (NO_ERROR);
}
t_error write_file(t_file *file, t_u8 *buffer, t_usize size,
t_isize *write_count)
t_error write_file(t_file *file, t_u8 *buffer, t_usize size,
t_isize *write_count)
{
t_isize ret;
t_isize fake_ret;
t_isize ret;
t_isize fake_ret;
if (write_count == NULL)
write_count = &fake_ret;
@ -282,12 +280,12 @@ t_error write_file(t_file *file, t_u8 *buffer, t_usize size,
return (NO_ERROR);
}
t_error read_file(t_file *file, t_u8 *buffer, t_usize size, t_isize *read_count)
t_error read_file(t_file *file, t_u8 *buffer, t_usize size, t_isize *read_count)
{
t_isize ret;
t_isize ret;
if (file == NULL || buffer == NULL || read_count == NULL ||
file->ptr == NULL)
if (file == NULL || buffer == NULL || read_count == NULL
|| file->ptr == NULL)
return (ERROR);
ret = fread(buffer, size, 1, file->ptr);
if (ret == -1)
@ -296,14 +294,14 @@ t_error read_file(t_file *file, t_u8 *buffer, t_usize size, t_isize *read_count)
return (NO_ERROR);
}
void close_file(t_file *file)
void close_file(t_file *file)
{
struct s_file_slot *slot;
struct s_file_slot *slot;
if (file == NULL)
return;
return ;
if (fclose(file->ptr) == -1)
return;
return ;
slot = (void *)(file)-offsetof(struct s_file_slot, slot.file);
mem_set_zero(slot, sizeof(*slot));
}
@ -316,11 +314,11 @@ void close_file(t_file *file)
\_____|______| |_| |_| |______|_| \_\_____/
*/
t_fd *get_stdin(void)
t_fd *get_stdin(void)
{
t_fd *out;
struct s_file_slot *slot;
static t_fd *value = NULL;
t_fd *out;
struct s_file_slot *slot;
static t_fd *value = NULL;
if (value == NULL)
{
@ -335,11 +333,11 @@ t_fd *get_stdin(void)
return (value);
}
t_fd *get_stdout(void)
t_fd *get_stdout(void)
{
t_fd *out;
struct s_file_slot *slot;
static t_fd *value = NULL;
t_fd *out;
struct s_file_slot *slot;
static t_fd *value = NULL;
if (value == NULL)
{
@ -354,11 +352,11 @@ t_fd *get_stdout(void)
return (value);
}
t_fd *get_stderr(void)
t_fd *get_stderr(void)
{
t_fd *out;
struct s_file_slot *slot;
static t_fd *value = NULL;
t_fd *out;
struct s_file_slot *slot;
static t_fd *value = NULL;
if (value == NULL)
{