Add ctrl-y and ctrl-w

This commit is contained in:
savalet
2025-05-22 15:23:15 +02:00
parent 670448b9e4
commit 4920b9a9da
9 changed files with 80 additions and 2 deletions

2
FIX.md
View File

@@ -2,7 +2,7 @@
- [x] Crash with history (!x)
- [x] Up arrow that not get args
- [ ] !x no args
- [ ] Conditional jump/ invalid read with cd (PWD env var set)
- [x] Conditional jump/ invalid read with cd (PWD env var set)
- [x] Alias loop
- [ ] All AFL crashes
- [ ] Fix CTRL+E and ->

View File

@@ -45,6 +45,7 @@ typedef struct {
char *cwdcmd;
bool ignoreof;
jobs_t jobs;
char *paste_buff;
} exec_ctx_t;
size_t update_command(char **buffer,

View File

@@ -5,6 +5,7 @@
** _
*/
#include <stdio.h>
#include <unistd.h>
#include "key_handler.h"
@@ -32,3 +33,11 @@ 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_k(readline_helper_t *rh, exec_ctx_t *, buff_t *buff)
{
printf("aaaa\n");
buff->sz = rh->cursor;
refresh_line(rh);
return true;
}

View File

@@ -0,0 +1,42 @@
/*
** EPITECH PROJECT, 2025
** __
** File description:
** _
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "key_handler.h"
#include "u_str.h"
bool handle_key_ctrl_w(readline_helper_t *rh, exec_ctx_t *, buff_t *buff)
{
if (!rh->cursor || !buff->sz)
return false;
rh->ec->paste_buff = u_strndup(buff->str, rh->cursor);
if (rh->ec->paste_buff == nullptr)
return true;
memmove(buff->str, buff->str + rh->cursor, rh->cursor);
buff->sz -= rh->cursor;
rh->cursor = 0;
refresh_line(rh);
return false;
}
bool handle_key_ctrl_y(readline_helper_t *rh, exec_ctx_t *, buff_t *buff)
{
size_t paste_len;
if (rh->ec->paste_buff == nullptr)
return false;
paste_len = strlen(rh->ec->paste_buff);
if (!ensure_buff_av_capacity(buff, paste_len))
return true;
strcpy(buff->str + paste_len, rh->ec->paste_buff);
buff->sz += paste_len - 2;
rh->cursor += paste_len;
return false;
}

View File

@@ -25,6 +25,9 @@ bool handle_key_ctrl_e(readline_helper_t *rh, exec_ctx_t *ec, buff_t *buff);
bool handle_key_ctrl_a(readline_helper_t *rh, exec_ctx_t *ec, buff_t *buff);
bool handle_key_ctrl_b(readline_helper_t *rh, exec_ctx_t *ec, buff_t *buff);
bool handle_key_ctrl_f(readline_helper_t *rh, exec_ctx_t *ec, buff_t *buff);
bool handle_key_ctrl_k(readline_helper_t *rh, exec_ctx_t *, buff_t *buff);
bool handle_key_ctrl_w(readline_helper_t *rh, exec_ctx_t *, buff_t *buff);
bool handle_key_ctrl_y(readline_helper_t *rh, exec_ctx_t *, buff_t *buff);
bool handle_backspace(readline_helper_t *rh, exec_ctx_t *ec, buff_t *buff);
bool handle_delete(readline_helper_t *rh, exec_ctx_t *, buff_t *buff);

View File

@@ -27,6 +27,8 @@ const key_handler_t KEY_HANDLERS[] = {
{"\01", handle_key_ctrl_a}, // ^A
{"\02", handle_key_ctrl_b}, // ^B
{"\06", handle_key_ctrl_f}, // ^F
{"\027", handle_key_ctrl_w}, // ^W
{"\031", handle_key_ctrl_y}, // ^Y
{ESC "[A", handle_key_arrow_up},
{ESC "[B", handle_key_arrow_down},
{ESC "[C", handle_key_arrow_right},

View File

@@ -154,7 +154,7 @@ int shell(opt_t *opt, char **env_ptr)
exec_ctx_t exec_ctx = {.env = &env, .local = &local, .opt = opt,
.read_fd = get_read_fd(opt), .history = &history, .precmd = nullptr,
.history_command = cmd_history, .alias = &alias, 0, .cwdcmd = nullptr,
.ignoreof = false };
.ignoreof = false, .paste_buff = nullptr };
int shell_result;
if (exec_ctx.read_fd == -1 || (int)error_in_init(&exec_ctx)

20
ulib/str/strndup.c Normal file
View File

@@ -0,0 +1,20 @@
/*
** EPITECH PROJECT, 2025
** __
** File description:
** _
*/
#include <stdlib.h>
#include <string.h>
char *u_strndup(char const *src, size_t n)
{
char *dst = malloc((sizeof *src * n) + 1);
if (dst == nullptr)
return nullptr;
dst = strncpy(dst, src, n);
dst[n] = '\0';
return dst;
}

View File

@@ -38,5 +38,6 @@ void puterror(char const *prefix);
int my_array_len(char **tab);
char **arraydup(char **array);
char *u_strnchr(char *str, char to_search, size_t n);
char *u_strndup(char const *src, size_t n);
#endif /* STRING_H */