mirror of
https://github.com/Savapitech/42sh.git
synced 2026-03-18 21:50:35 +01:00
Fix ctrl+e and ->
This commit is contained in:
@@ -15,35 +15,10 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#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);
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
#ifndef READLINE
|
||||
#define READLINE
|
||||
#include <glob.h>
|
||||
#include <stdbool.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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 */
|
||||
|
||||
@@ -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 */
|
||||
|
||||
35
src/utils/ensure_cap.c
Normal file
35
src/utils/ensure_cap.c
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
** EPITECH PROJECT, 2025
|
||||
** __
|
||||
** File description:
|
||||
** _
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user