Updated linenoise and printf to better work

This commit is contained in:
Maieul BOYER 2024-07-07 19:50:33 +02:00
parent 0e18e20181
commit 8808f81221
No known key found for this signature in database
28 changed files with 657 additions and 579 deletions

View file

@ -6,14 +6,13 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/19 15:12:18 by maiboyer #+# #+# */
/* Updated: 2024/05/24 15:03:40 by maiboyer ### ########.fr */
/* Updated: 2024/07/07 17:50:18 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FS_H
#define FS_H
#include "me/fs/read_to_vec.h"
#include "me/types.h"
#include <dirent.h>
#include <fcntl.h>
@ -78,9 +77,9 @@ typedef enum e_file_open_option
/// `G` means group
/// `U` means user
/// `ALL` means all
/// There are the raw permission, you can combine them to get the permission you want
/// And there are "aliases" that are common permission set
/// @note you can combine them with the `|` operator
/// There are the raw permission, you can combine them to get the permission you
/// want And there are "aliases" that are common permission set
/// @note you can combine them with the `|` operator
typedef enum e_file_perm
{
FP_OEXEC = 1 << 0,
@ -139,7 +138,6 @@ union u_file_slot {
t_file file;
};
/// @brief File slot structure
/// ty: the type of the slot
/// slot: the slot itself
@ -164,46 +162,44 @@ typedef t_const_str t_mode;
/// @note this is a simple typedef because I hate the struct keyword
typedef struct stat t_stat;
/// @brief Directory entry structure
/// @note this is a simple typedef because I hate the struct keyword and it is always behind a pointer
/// @note this is a simple typedef because I hate the struct keyword and it is
/// always behind a pointer
typedef struct dirent *t_dir_entry;
/*_____ _ _ _______ ______ _____ _ _ _
|_ _| \ | |__ __| ____| __ \| \ | | /\ | |
| | | \| | | | | |__ | |__) | \| | / \ | |
| | | . ` | | | | __| | _ /| . ` | / /\ \ | |
_| |_| |\ | | | | |____| | \ \| |\ |/ ____ \| |____
|_____|_| \_| |_| |______|_| \_\_| \_/_/ \_\______|
/*_____ _ _ _______ ______ _____ _ _ _
|_ _| \ | |__ __| ____| __ \| \ | | /\ | |
| | | \| | | | | |__ | |__) | \| | / \ | |
| | | . ` | | | | __| | _ /| . ` | / /\ \ | |
_| |_| |\ | | | | |____| | \ \| |\ |/ ____ \| |____
|_____|_| \_| |_| |______|_| \_\_| \_/_/ \_\______|
*/
/// @brief Get the fd arrays object
/// @return pointer to the files's array
/// @note internal function used to get the files array
t_fd_array *get_fd_arrays(void);
t_fd_array *get_fd_arrays(void);
/// @brief Get the unused fd slot object
/// @return pointer to the unused file slot
/// @note Will abort if no slot is available
struct s_file_slot *get_unused_fd_slot(void);
/// @brief Close all slots
/// @note This is probably NOT what you want
void close_all_slots(void);
void close_all_slots(void);
/// @note Close the given slot
/// @param[in] slot the slot to close
/// @note this is probably NOT what you want
void close_slot(struct s_file_slot *slot);
void close_slot(struct s_file_slot *slot);
/* ______ _____
| ____| __ \
/* ______ _____
| ____| __ \
| |__ | | | |
| __| | | | |
| | | |__| |
|_| |_____/
|_| |_____/
*/
/// @brief Open a file descriptor
@ -212,8 +208,8 @@ void close_slot(struct s_file_slot *slot);
/// @param open_options the options to open the file
/// @param fileperm the file permission
/// @return the file descriptor* on success, NULL otherwise
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);
/// @brief Read from a file descriptor
/// @param[in] fd the file descriptor
@ -241,29 +237,28 @@ t_error stat_fd(t_fd *fd, t_stat *stat);
/// @brief Close a file descriptor
/// @param[in] fd the file descriptor
/// @note Will close the file descriptor and free the slot
void close_fd(t_fd *fd);
void close_fd(t_fd *fd);
/// @brief write a number to a file descriptor
/// @note will fail silently if the fd is not open in write mode
void put_number_fd(t_fd *fd, t_u64 number);
void put_number_fd(t_fd *fd, t_u64 number);
/// @brief write a string to a file descriptor
/// @note will fail silently if the fd is not open in write mode
void put_string_fd(t_fd *fd, t_const_str string);
void put_string_fd(t_fd *fd, t_const_str string);
/// @brief write a char to a file descriptor
/// @note will fail silently if the fd is not open in write mode
void put_char_fd(t_fd *fd, t_u8 c);
void put_char_fd(t_fd *fd, t_u8 c);
/* _____ _____ _____ ______ _____ _______ ____ _______ __
| __ \_ _| __ \| ____/ ____|__ __/ __ \| __ \ \ / /
| | | || | | |__) | |__ | | | | | | | | |__) \ \_/ /
| | | || | | _ /| __|| | | | | | | | _ / \ /
| |__| || |_| | \ \| |___| |____ | | | |__| | | \ \ | |
|_____/_____|_| \_\______\_____| |_| \____/|_| \_\ |_|
| | | || | | |__) | |__ | | | | | | | | |__) \ \_/ /
| | | || | | _ /| __|| | | | | | | | _ / \ /
| |__| || |_| | \ \| |___| |____ | | | |__| | | \ \ | |
|_____/_____|_| \_\______\_____| |_| \____/|_| \_\ |_|
*/
/// @brief Open a file
/// @param[in] name the name of the file
/// @param[out] dir the file structure to fill
@ -283,11 +278,11 @@ t_error read_dir(t_dir *dir, t_dir_entry *out);
/// @note Will close the directory and free the slot
void close_dir(t_dir *dir);
/*______ _____ _ ______
/*______ _____ _ ______
| ____|_ _| | | ____|
| |__ | | | | | |__
| __| | | | | | __|
| | _| |_| |____| |____
| |__ | | | | | |__
| __| | | | | | __|
| | _| |_| |____| |____
|_| |_____|______|______|
*/
@ -304,7 +299,8 @@ t_error open_file(t_str name, t_mode mode, t_file **file);
/// @param[in] size the size of the buffer
/// @param[out] read_count the number of bytes read
/// @return true on error, false otherwise
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);
/// @brief Write to a file
/// @param[in] file the file to write to
@ -313,11 +309,25 @@ t_error read_file(t_file *file, t_u8 *buffer, t_usize size, t_isize *read_count)
/// @param[out] write_count the number of bytes written
/// @return true on error, false otherwise
/// @note write_count can be NULL
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);
/// @brief Close the underlying file stream
/// @param[in] file the file to close
/// @note Will close the file and free the slot
void close_file(t_file *file);
/* _____ ______ _______ _______ ______ _____ _____
/ ____| ____|__ __|__ __| ____| __ \ / ____|
| | __| |__ | | | | | |__ | |__) | (___
| | |_ | __| | | | | | __| | _ / \___ \
| |__| | |____ | | | | | |____| | \ \ ____) |
\_____|______| |_| |_| |______|_| \_\_____/
*/
//TODO: Documentation!
t_fd *get_stdin(void);
t_fd *get_stdout(void);
t_fd *get_stderr(void);
#endif /* FS_H */

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/03 15:43:08 by maiboyer #+# #+# */
/* Updated: 2024/01/06 18:39:58 by maiboyer ### ########.fr */
/* Updated: 2024/07/07 18:25:23 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -29,14 +29,14 @@ enum e_redirection
union u_redirection
{
struct s_fd
struct s_fd_redirection
{
int value;
} fd;
struct s_piped
struct s_piped_redirection
{
} piped;
struct s_inherited
struct s_inherited_redirection
{
} inherited;
};

View file

@ -0,0 +1,81 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* _internal_printf.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/07 17:36:38 by maiboyer #+# #+# */
/* Updated: 2024/07/07 18:01:17 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef _INTERNAL_PRINTF_H
#define _INTERNAL_PRINTF_H
#include "me/fs/fs.h"
#include "me/string/string.h"
#include "me/types.h"
typedef enum e_printf_flags t_printf_flags;
typedef enum e_printf_type t_printf_type;
typedef struct s_fprintf_arg t_fprintf_arg;
typedef struct s_printf_args t_printf_arg;
typedef struct s_printf_extra_args t_printf_extra_args;
typedef struct s_sprintf_arg t_sprintf_arg;
typedef void (*t_printf_func)(t_const_str to_write, t_usize to_write_len,
void *p_args);
struct s_fprintf_arg
{
t_usize total_print;
t_fd *fd;
};
struct s_sprintf_arg
{
t_usize total_print;
t_string *buffer;
};
enum e_printf_flags
{
PRECISION = 1 << 1,
ALIGN = 1 << 2,
ZERO_ALIGN = 1 << 3,
SIGN = 1 << 4,
};
enum e_printf_type
{
CHAR = 1 << 0,
STR = 1 << 1,
U64 = 1 << 2,
I64 = 1 << 3,
VOID_PTR = 1 << 4,
I32 = 1 << 5,
U32 = 1 << 6,
};
struct s_printf_extra_args
{
t_u64 precision;
t_u64 align;
bool left_align;
bool space_align;
bool pretty;
};
struct s_printf_args
{
void *argument;
void *p_args;
t_printf_extra_args extra;
t_printf_flags flags;
};
void me_printf_write(t_const_str to_write, t_usize to_write_len, void *p_args);
void me_printf_append_string(t_const_str to_write, t_usize to_write_len,
void *p_args);
#endif /* _INTERNAL_PRINTF_H */

View file

@ -6,14 +6,13 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 18:18:19 by maiboyer #+# #+# */
/* Updated: 2023/11/18 19:11:23 by maiboyer ### ########.fr */
/* Updated: 2024/07/07 17:38:52 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FORMATTER_H
# define FORMATTER_H
# include "me/printf/printf.h"
# include "me/types.h"
# include "me/printf/_internal_printf.h"
void printf_x_low(t_printf_arg data, t_printf_func f);
void printf_x_up(t_printf_arg data, t_printf_func f);

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 17:58:41 by maiboyer #+# #+# */
/* Updated: 2023/12/01 21:24:21 by maiboyer ### ########.fr */
/* Updated: 2024/07/07 17:38:49 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,7 +14,7 @@
# define UTILS_H
# include "me/printf/matchers/matchers.h"
# include "me/printf/printf.h"
# include "me/printf/_internal_printf.h"
# include "me/types.h"
# include <stdarg.h>

View file

@ -6,14 +6,14 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 18:09:07 by maiboyer #+# #+# */
/* Updated: 2023/11/18 18:10:33 by maiboyer ### ########.fr */
/* Updated: 2024/07/07 17:38:46 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MATCHERS_H
# define MATCHERS_H
# include "me/printf/printf.h"
# include "me/printf/_internal_printf.h"
# include "me/types.h"
# include <stdarg.h>
# define PRINTF_BUFFER_CHUNK 20

View file

@ -6,100 +6,72 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 18:10:27 by maiboyer #+# #+# */
/* Updated: 2024/07/05 19:54:25 by maiboyer ### ########.fr */
/* Updated: 2024/07/07 17:37:02 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PRINTF_H
# define PRINTF_H
# include "me/types.h"
# include <stdarg.h>
#define PRINTF_H
#include "me/types.h"
#include <stdarg.h>
# ifndef FS_H
#ifndef FS_H
typedef struct s_fd t_fd;
# endif
#endif
typedef struct s_fprintf_arg
{
t_usize total_print;
int fd;
} t_fprintf_arg;
typedef enum e_printf_flags
{
PRECISION = 1 << 1,
ALIGN = 1 << 2,
ZERO_ALIGN = 1 << 3,
SIGN = 1 << 4,
} t_printf_flags;
typedef enum e_printf_type
{
CHAR = 1 << 0,
STR = 1 << 1,
U64 = 1 << 2,
I64 = 1 << 3,
VOID_PTR = 1 << 4,
I32 = 1 << 5,
U32 = 1 << 6,
} t_printf_type;
typedef struct s_printf_extra_args
{
t_u64 precision;
t_u64 align;
bool left_align;
bool space_align;
bool pretty;
} t_printf_extra_args;
typedef struct s_printf_args
{
void *argument;
void *p_args;
t_printf_extra_args extra;
t_printf_flags flags;
} t_printf_arg;
typedef void (*t_printf_func)(t_const_str to_write,
t_usize to_write_len, void *p_args);
#ifndef STRING_H
typedef struct s_string t_string;
#endif
/// @brief Print a formatted string to stdout
/// @param fmt the format string
/// @param ... the arguments to format
/// @return the number of characters printed
t_usize me_printf(t_const_str fmt, ...);
t_usize me_printf(t_const_str fmt, ...);
/// @brief Print a formatted string to a stderr
/// @param fmt the format string
/// @param ... the arguments to format
/// @return the number of characters printed
t_usize me_eprintf(t_const_str fmt, ...);
t_usize me_eprintf(t_const_str fmt, ...);
/// @brief Print a formatted string to a stdout
/// @param fmt the format string
/// @param args the arguments to format as a va_list
/// @return the number of characters printed
t_usize me_vprintf(t_const_str fmt, va_list *args);
t_usize me_vprintf(t_const_str fmt, va_list *args);
/// @brief Print a formatted string to a stderr
/// @param fmt the format string
/// @param args the arguments to format as a va_list
/// @return the number of characters printed
t_usize me_veprintf(t_const_str fmt, va_list *args);
t_usize me_veprintf(t_const_str fmt, va_list *args);
/// @brief Print a formatted string to the given fd
/// @param fmt the format string
/// @param ... the arguments to format
/// @return the number of characters printed
t_usize me_printf_fd(t_fd *, t_const_str fmt, ...);
t_usize me_printf_fd(t_fd *, t_const_str fmt, ...);
/// @brief Print a formatted string to the given fd
/// @param fmt the format string
/// @param args the arguments to format as a va_list
/// @return the number of characters printed
t_usize me_vprintf_fd(t_fd *, t_const_str fmt, va_list *args);
t_usize me_vprintf_fd(t_fd *, t_const_str fmt, va_list *args);
/// @brief print a formatted string to a buffer
/// @param buffer the buffer to append to
/// @param fmt the format string
/// @param ... the arguments to format
/// @return the number of characters printed
t_usize me_printf_str(t_string *buffer, t_const_str fmt, ...);
/// @brief print a formatted string to a buffer
/// @param buffer the buffer to append to
/// @param fmt the format string
/// @param args the arguments to format
/// @return the number of characters printed
t_usize me_vprintf_str(t_string *buffer, t_const_str fmt, va_list *args);
#endif