Fix up arrow without args

This commit is contained in:
savalet
2025-05-18 23:58:21 +02:00
parent c3d70d3e08
commit 734320805b
6 changed files with 85 additions and 81 deletions

3
FIX.md
View File

@@ -1,8 +1,9 @@
# To fix
- [x] Crash with history (!x)
- [ ] Up arrow that not get args
- [x] Up arrow that not get args
- [ ] !x no args
- [ ] Conditional jump/ invalid read with cd (PWD env var set)
- [x] Alias loop
- [ ] All AFL crashes
- [ ] Fix CTRL+E and ->
- [ ] Multi line editing

View File

@@ -5,3 +5,4 @@
- [ ] Finish line editing
- [ ] Syntax highlighting
- [ ] CTRL+R
- [ ] Git integration

View File

@@ -13,6 +13,27 @@
#include "readline.h"
#include "u_str.h"
static
bool cat_history_entry(buff_t *buff, size_t i, his_command_t *cmd_history)
{
if (!ensure_buff_av_capacity(buff,
strlen(cmd_history[i].command)))
return false;
strcpy(buff->str, cmd_history[i].command);
if (buff->str == nullptr)
return true;
buff->sz = strlen(buff->str);
if (cmd_history[i].arg != nullptr) {
if (!ensure_buff_av_capacity(buff,
strlen(cmd_history[i].arg) + 1))
return false;
buff->str[buff->sz] = ' ';
strcat(buff->str, cmd_history[i].arg);
buff->sz += strlen(cmd_history[i].arg) + 1;
}
return true;
}
bool handle_key_arrow_up(
readline_helper_t *rh, exec_ctx_t *ec, buff_t *buff)
{
@@ -20,17 +41,8 @@ bool handle_key_arrow_up(
if (cmd_history->sz && rh->history_idx) {
rh->history_idx--;
ensure_buff_av_capacity(buff,
strlen(cmd_history[rh->history_idx].command));
strcpy(buff->str, cmd_history[rh->history_idx].command);
if (buff->str == nullptr)
return true;
buff->sz = strlen(buff->str);
if (cmd_history[rh->history_idx].arg) {
ensure_buff_av_capacity(buff,
strlen(cmd_history[rh->history_idx].arg));
strcat(buff->str, cmd_history[rh->history_idx].arg);
}
if (!cat_history_entry(buff, rh->history_idx, cmd_history))
return false;
rh->cursor = buff->sz;
refresh_line(rh);
}
@@ -44,17 +56,8 @@ bool handle_key_arrow_down(
if (cmd_history->sz && rh->history_idx + 1 < cmd_history->sz) {
rh->history_idx++;
ensure_buff_av_capacity(buff,
strlen(cmd_history[rh->history_idx].command));
strcpy(buff->str, cmd_history[rh->history_idx].command);
if (buff->str == nullptr)
return true;
buff->sz = strlen(buff->str);
if (cmd_history[rh->history_idx].arg) {
ensure_buff_av_capacity(buff,
strlen(cmd_history[rh->history_idx].arg));
strcat(buff->str, cmd_history[rh->history_idx].arg);
}
if (!cat_history_entry(buff, rh->history_idx, cmd_history))
return false;
rh->cursor = buff->sz;
refresh_line(rh);
}

View File

@@ -5,10 +5,12 @@
** _
*/
#include <string.h>
#include <unistd.h>
#include "key_handler.h"
#include "repl.h"
#include "vt100_esc_codes.h"
bool handle_key_ctrl_c(readline_helper_t *rh, exec_ctx_t *ec, buff_t *buff)
{
@@ -27,21 +29,33 @@ bool handle_key_ctrl_d(readline_helper_t *, exec_ctx_t *ec, buff_t *buff)
return true;
}
bool handle_key_ctrl_e(readline_helper_t *rh, exec_ctx_t *, buff_t *buff)
bool handle_key_ctrl_l(readline_helper_t *, exec_ctx_t *ec, buff_t *)
{
rh->cursor = buff->sz;
WRITE_CONST(STDOUT_FILENO, ESC "[2J");
WRITE_CONST(STDOUT_FILENO, ESC "[H");
print_shell_prompt(ec);
return false;
}
bool handle_backspace(readline_helper_t *rh, exec_ctx_t *, buff_t *buff)
{
if (rh->cursor == 0)
return false;
rh->cursor--;
memmove(&buff->str[rh->cursor], &buff->str[rh->cursor + 1],
buff->sz - rh->cursor);
buff->sz--;
refresh_line(rh);
return false;
}
bool handle_key_ctrl_a(readline_helper_t *rh, exec_ctx_t *, buff_t *)
bool handle_delete(readline_helper_t *rh, exec_ctx_t *, buff_t *buff)
{
rh->cursor = 0;
if (rh->cursor >= buff->sz)
return false;
memmove(&buff->str[rh->cursor], &buff->str[rh->cursor + 1],
buff->sz - rh->cursor);
buff->sz--;
refresh_line(rh);
return false;
}
bool handle_key_ctrl_b(readline_helper_t *rh, exec_ctx_t *ec, buff_t *buff)
{
return handle_key_arrow_left(rh, ec, buff);
}

View File

@@ -1,49 +0,0 @@
/*
** EPITECH PROJECT, 2025
** __
** File description:
** _
*/
#include <string.h>
#include <unistd.h>
#include "key_handler.h"
#include "repl.h"
#include "vt100_esc_codes.h"
bool handle_key_ctrl_f(readline_helper_t *rh, exec_ctx_t *ec, buff_t *buff)
{
return handle_key_arrow_right(rh, ec, buff);
}
bool handle_key_ctrl_l(readline_helper_t *, exec_ctx_t *ec, buff_t *)
{
WRITE_CONST(STDOUT_FILENO, ESC "[2J");
WRITE_CONST(STDOUT_FILENO, ESC "[H");
print_shell_prompt(ec);
return false;
}
bool handle_backspace(readline_helper_t *rh, exec_ctx_t *, buff_t *buff)
{
if (rh->cursor == 0)
return false;
rh->cursor--;
memmove(&buff->str[rh->cursor], &buff->str[rh->cursor + 1],
buff->sz - rh->cursor);
buff->sz--;
refresh_line(rh);
return false;
}
bool handle_delete(readline_helper_t *rh, exec_ctx_t *, buff_t *buff)
{
if (rh->cursor >= buff->sz)
return false;
memmove(&buff->str[rh->cursor], &buff->str[rh->cursor + 1],
buff->sz - rh->cursor);
buff->sz--;
refresh_line(rh);
return false;
}

View File

@@ -0,0 +1,34 @@
/*
** EPITECH PROJECT, 2025
** __
** File description:
** _
*/
#include <unistd.h>
#include "key_handler.h"
bool handle_key_ctrl_e(readline_helper_t *rh, exec_ctx_t *, buff_t *buff)
{
rh->cursor = buff->sz;
refresh_line(rh);
return false;
}
bool handle_key_ctrl_a(readline_helper_t *rh, exec_ctx_t *, buff_t *)
{
rh->cursor = 0;
refresh_line(rh);
return false;
}
bool handle_key_ctrl_b(readline_helper_t *rh, exec_ctx_t *ec, buff_t *buff)
{
return handle_key_arrow_left(rh, ec, buff);
}
bool handle_key_ctrl_f(readline_helper_t *rh, exec_ctx_t *ec, buff_t *buff)
{
return handle_key_arrow_right(rh, ec, buff);
}