diff --git a/src/readline.c b/src/readline.c index 1f201f7..907ead0 100644 --- a/src/readline.c +++ b/src/readline.c @@ -15,35 +15,10 @@ #include #include "readline.h" -#include "debug.h" #include "repl.h" #include "u_str.h" #include "vt100_esc_codes.h" -bool ensure_buff_av_capacity(buff_t *buff, size_t requested) -{ - char *new_str; - size_t endsize = BUFF_INIT_SZ; - - if (buff->str == NULL) { - buff->str = malloc((sizeof *buff->str) * requested); - if (buff->str == NULL) - return false; - buff->cap = requested; - } - if ((buff->sz + requested) < buff->cap) - return true; - for (; endsize < buff->sz + requested; endsize <<= 1); - if (endsize > buff->cap) { - new_str = realloc(buff->str, (sizeof *buff->str) * endsize); - if (new_str == NULL) - return false; - buff->str = new_str; - buff->cap = endsize; - } - return true; -} - static bool append_null_terminator(buff_t *out) { @@ -120,13 +95,30 @@ void refresh_line(readline_helper_t *rh) write(STDOUT_FILENO, rh->out->str + rh->cursor - 1, 1); return; } - if (rh->out->sz > 1 && *rh->cpy == '\n') { - WRITE_CONST(STDOUT_FILENO, "\n"); - return; - } print_buff(rh); } +static +bool handle_copy_buff( + readline_helper_t *rh, ssize_t skip, ssize_t *i, bool *done) +{ + if (skip < 0) { + rh->tpi->used = *i; + return false; + } + if (skip) { + *i += skip - 1; + return true; + } + if (copy_single_char(rh, rh->out, &rh->cpy[rh->tpi->written], + rh->in[*i])) { + *done = rh->cpy[rh->tpi->written] == '\n'; + rh->tpi->written++; + return true; + } + return true; +} + static bool populate_copy_buff( readline_helper_t *rh, ssize_t rd, @@ -137,19 +129,8 @@ bool populate_copy_buff( for (bool done = false; !done && i < rd; i++) { skip = handle_keys(rh, rh->out, &rh->in[i], BULK_READ_BUFF_SZ - i); - if (skip < 0) { - tpi->used = i; + if (!handle_copy_buff(rh, skip, &i, &done)) return false; - } - if (skip) { - i += skip - 1; - continue; - } - if (copy_single_char(rh, rh->out, &rh->cpy[tpi->written], rh->in[i])) { - done = rh->cpy[tpi->written] == '\n'; - tpi->written++; - continue; - } } refresh_line(rh); tpi->used = i; @@ -162,6 +143,7 @@ bool read_until_line_ending( { text_parse_info_t tpi; + rh->tpi = &tpi; for (;;) { if (ec->isatty) ioctl(STDOUT_FILENO, TIOCGWINSZ, &rh->winsz); @@ -181,6 +163,19 @@ bool read_until_line_ending( return true; } +static +bool finish_buffer(readline_helper_t *rh) +{ + bool result; + + if (rh->ec->isatty && *rh->cpy == '\n') + WRITE_CONST(STDOUT_FILENO, "\n"); + result = append_null_terminator(rh->out); + if (*rh->cpy == '\n') + rh->out->sz++; + return result; +} + bool readline(exec_ctx_t *ec, buff_t *out) { static char read_buff[BULK_READ_BUFF_SZ] = {0}; @@ -201,5 +196,5 @@ bool readline(exec_ctx_t *ec, buff_t *out) rd = BULK_READ_BUFF_SZ; if (!read_until_line_ending(ec, &rh, rd)) return false; - return append_null_terminator(out); + return finish_buffer(&rh); } diff --git a/src/readline.h b/src/readline.h index 5712c16..5add94e 100644 --- a/src/readline.h +++ b/src/readline.h @@ -7,6 +7,7 @@ #ifndef READLINE #define READLINE + #include #include #include @@ -16,6 +17,12 @@ #define BUFF_INIT_SZ 16 #define BULK_READ_BUFF_SZ 32 + +typedef struct { + size_t used; + size_t written; +} text_parse_info_t; + typedef struct { exec_ctx_t *ec; buff_t *out; @@ -24,13 +31,10 @@ typedef struct { size_t cursor; int history_idx; struct winsize winsz; + glob_t glob; + text_parse_info_t *tpi; } readline_helper_t; -typedef struct { - size_t used; - size_t written; -} text_parse_info_t; - bool readline(exec_ctx_t *ec, buff_t *out); void write_buff(readline_helper_t *rh); void refresh_line(readline_helper_t *rh); diff --git a/src/repl.h b/src/repl.h index 1c808fc..ece40b7 100644 --- a/src/repl.h +++ b/src/repl.h @@ -26,4 +26,5 @@ ssize_t handle_keys( void print_shell_prompt(exec_ctx_t *ec); void print_second_shell_prompt(exec_ctx_t *ec); +bool is_sequence(char *read_buff); #endif /* REPL_H */ diff --git a/src/utils.h b/src/utils.h index 0197ed3..d16f74b 100644 --- a/src/utils.h +++ b/src/utils.h @@ -9,7 +9,8 @@ #ifndef UTILS_H #define UTILS_H #include "history.h" - #include "exec.h" + #include "builtins_handler.h" + #include "u_str.h" char *strn_to_ndup(int start, int size, char *str); bool is_a_token(char *str, int index_str); @@ -18,4 +19,5 @@ int len_array(char **array); char *insert_str(const char *base, const char *insert, size_t pos); void free_everything(exec_ctx_t *exec_ctx); void free_args(char **args); +bool ensure_buff_av_capacity(buff_t *buff, size_t requested); #endif /* UTILS_H */ diff --git a/src/utils/ensure_cap.c b/src/utils/ensure_cap.c new file mode 100644 index 0000000..c41cff5 --- /dev/null +++ b/src/utils/ensure_cap.c @@ -0,0 +1,35 @@ +/* +** EPITECH PROJECT, 2025 +** __ +** File description: +** _ +*/ + +#include + +#include "u_str.h" +#include "readline.h" + +bool ensure_buff_av_capacity(buff_t *buff, size_t requested) +{ + char *new_str; + size_t endsize = BUFF_INIT_SZ; + + if (buff->str == NULL) { + buff->str = malloc((sizeof *buff->str) * requested); + if (buff->str == NULL) + return false; + buff->cap = requested; + } + if ((buff->sz + requested) < buff->cap) + return true; + for (; endsize < buff->sz + requested; endsize <<= 1); + if (endsize > buff->cap) { + new_str = realloc(buff->str, (sizeof *buff->str) * endsize); + if (new_str == NULL) + return false; + buff->str = new_str; + buff->cap = endsize; + } + return true; +}