Clean up readline code

This commit is contained in:
Martin Lund 2024-07-02 19:13:14 +02:00
parent 2c700a90b0
commit 14ee38a0d9

View file

@ -22,17 +22,16 @@
#include "print.h" #include "print.h"
#include "misc.h" #include "misc.h"
#define MAX_LINE_LEN PATH_MAX #define RL_LINE_LENGTH_MAX PATH_MAX
#define MAX_HISTORY 1000 #define RL_HISTORY_MAX 1000
static char line[MAX_LINE_LEN] = {}; static char rl_line[RL_LINE_LENGTH_MAX] = {};
static int history_count = 0; static char *rl_history[RL_HISTORY_MAX];
static int history_index = 0; static int rl_history_count = 0;
static int line_length = 0; static int rl_history_index = 0;
static int cursor_pos = 0; static int rl_line_length = 0;
static int escape = 0; static int rl_cursor_pos = 0;
static int rl_escape = 0;
static char *history[MAX_HISTORY];
static void print_line(const char *string, int cursor_pos) static void print_line(const char *string, int cursor_pos)
{ {
@ -47,58 +46,58 @@ static void print_line(const char *string, int cursor_pos)
void readline_init(void) void readline_init(void)
{ {
history_count = 0; rl_history_count = 0;
history_index = 0; rl_history_index = 0;
for (int i = 0; i < MAX_HISTORY; ++i) for (int i = 0; i < RL_HISTORY_MAX; ++i)
{ {
history[i] = NULL; rl_history[i] = NULL;
} }
line[0] = 0; rl_line[0] = 0;
line_length = 0; rl_line_length = 0;
cursor_pos = 0; rl_cursor_pos = 0;
escape = 0; rl_escape = 0;
} }
char * readline_get(void) char * readline_get(void)
{ {
return line; return rl_line;
} }
static void readline_input_char(char input_char) static void readline_input_char(char input_char)
{ {
if (line_length < MAX_LINE_LEN - 1) if (rl_line_length < RL_LINE_LENGTH_MAX - 1)
{ {
memmove(&line[cursor_pos + 1], &line[cursor_pos], line_length - cursor_pos); memmove(&rl_line[rl_cursor_pos + 1], &rl_line[rl_cursor_pos], rl_line_length - rl_cursor_pos);
line[cursor_pos] = input_char; rl_line[rl_cursor_pos] = input_char;
line_length++; rl_line_length++;
cursor_pos++; rl_cursor_pos++;
line[line_length] = '\0'; rl_line[rl_line_length] = '\0';
print_line(line, cursor_pos); print_line(rl_line, rl_cursor_pos);
} }
escape = 0; rl_escape = 0;
} }
static void readline_input_cr(void) static void readline_input_cr(void)
{ {
if (line_length > 0) if (rl_line_length > 0)
{ {
// Save to history // Save to history
if (history_count < MAX_HISTORY) if (rl_history_count < RL_HISTORY_MAX)
{ {
history[history_count] = strndup(line, line_length); rl_history[rl_history_count] = strndup(rl_line, rl_line_length);
history_count++; rl_history_count++;
} }
else else
{ {
free(history[0]); free(rl_history[0]);
memmove(&history[0], &history[1], (MAX_HISTORY - 1) * sizeof(char*)); memmove(&rl_history[0], &rl_history[1], (RL_HISTORY_MAX - 1) * sizeof(char*));
history[MAX_HISTORY - 1] = strndup(line, line_length); rl_history[RL_HISTORY_MAX - 1] = strndup(rl_line, rl_line_length);
} }
} }
line[line_length] = '\0'; rl_line[rl_line_length] = '\0';
if (option.local_echo == false) if (option.local_echo == false)
{ {
clear_line(); clear_line();
@ -108,54 +107,54 @@ static void readline_input_cr(void)
print("\r\n"); print("\r\n");
} }
line_length = 0; rl_line_length = 0;
cursor_pos = 0; rl_cursor_pos = 0;
history_index = history_count; rl_history_index = rl_history_count;
escape = 0; rl_escape = 0;
} }
static void readline_input_bs(void) static void readline_input_bs(void)
{ {
if (cursor_pos > 0) if (rl_cursor_pos > 0)
{ {
memmove(&line[cursor_pos - 1], &line[cursor_pos], line_length - cursor_pos); memmove(&rl_line[rl_cursor_pos - 1], &rl_line[rl_cursor_pos], rl_line_length - rl_cursor_pos);
line_length--; rl_line_length--;
cursor_pos--; rl_cursor_pos--;
line[line_length] = '\0'; rl_line[rl_line_length] = '\0';
print_line(line, cursor_pos); print_line(rl_line, rl_cursor_pos);
} }
escape = 0; rl_escape = 0;
} }
static void readline_input_escape(void) static void readline_input_escape(void)
{ {
escape = 1; rl_escape = 1;
} }
static void readline_input_left_bracket(void) static void readline_input_left_bracket(void)
{ {
if (escape == 1) if (rl_escape == 1)
{ {
escape = 2; rl_escape = 2;
} }
else else
{ {
escape = 0; rl_escape = 0;
} }
} }
static void readline_input_A(void) static void readline_input_A(void)
{ {
if (escape == 2) if (rl_escape == 2)
{ {
// Up arrow // Up arrow
if (history_index > 0) if (rl_history_index > 0)
{ {
history_index--; rl_history_index--;
strncpy(line, history[history_index], MAX_LINE_LEN-1); strncpy(rl_line, rl_history[rl_history_index], RL_LINE_LENGTH_MAX-1);
line_length = strlen(line); rl_line_length = strlen(rl_line);
cursor_pos = line_length; rl_cursor_pos = rl_line_length;
print_line(line, cursor_pos); print_line(rl_line, rl_cursor_pos);
} }
} }
else else
@ -163,29 +162,29 @@ static void readline_input_A(void)
readline_input_char('A'); readline_input_char('A');
} }
escape = 0; rl_escape = 0;
} }
static void readline_input_B(void) static void readline_input_B(void)
{ {
if (escape == 2) if (rl_escape == 2)
{ {
// Down arrow // Down arrow
if (history_index < history_count - 1) if (rl_history_index < rl_history_count - 1)
{ {
history_index++; rl_history_index++;
strncpy(line, history[history_index], MAX_LINE_LEN-1); strncpy(rl_line, rl_history[rl_history_index], RL_LINE_LENGTH_MAX-1);
line_length = strlen(line); rl_line_length = strlen(rl_line);
cursor_pos = line_length; rl_cursor_pos = rl_line_length;
print_line(line, cursor_pos); print_line(rl_line, rl_cursor_pos);
} }
else if (history_index == history_count - 1) else if (rl_history_index == rl_history_count - 1)
{ {
history_index++; rl_history_index++;
line_length = 0; rl_line_length = 0;
cursor_pos = 0; rl_cursor_pos = 0;
line[line_length] = '\0'; rl_line[rl_line_length] = '\0';
print_line(line, cursor_pos); print_line(rl_line, rl_cursor_pos);
} }
} }
else else
@ -193,17 +192,17 @@ static void readline_input_B(void)
readline_input_char('B'); readline_input_char('B');
} }
escape = 0; rl_escape = 0;
} }
static void readline_input_C(void) static void readline_input_C(void)
{ {
if (escape == 2) if (rl_escape == 2)
{ {
// Right arrow // Right arrow
if (cursor_pos < line_length) if (rl_cursor_pos < rl_line_length)
{ {
cursor_pos++; rl_cursor_pos++;
print("\x1b[C"); print("\x1b[C");
} }
} }
@ -212,17 +211,17 @@ static void readline_input_C(void)
readline_input_char('C'); readline_input_char('C');
} }
escape = 0; rl_escape = 0;
} }
static void readline_input_D(void) static void readline_input_D(void)
{ {
if (escape == 2) if (rl_escape == 2)
{ {
// Left arrow // Left arrow
if (cursor_pos > 0) if (rl_cursor_pos > 0)
{ {
cursor_pos--; rl_cursor_pos--;
print("\b"); print("\b");
} }
} }
@ -231,7 +230,7 @@ static void readline_input_D(void)
readline_input_char('D'); readline_input_char('D');
} }
escape = 0; rl_escape = 0;
} }
void readline_input(char input_char) void readline_input(char input_char)