libinput Generic way of gathering user commandline input
All checks were successful
Build documentation / build-and-deploy (push) Successful in 3m49s

This commit is contained in:
2026-03-20 18:04:26 +01:00
parent 3e44106752
commit bf1e8e8573
22 changed files with 310 additions and 202 deletions

View File

@@ -9,6 +9,7 @@ $(eval $(call add_lib,libterminal))
$(eval $(call add_lib,libfat))
$(eval $(call add_lib,libmalloc))
$(eval $(call add_lib,libdebugconsole))
$(eval $(call add_lib,libinput))
$(eval $(call add_include,libkb))
cflags += -DPRINTF_INCLUDE_CONFIG_H=1

108
ce/ce.c
View File

@@ -1,12 +1,11 @@
#include "arena_alloc.h"
#include "context.h"
#include "gapbuffer.h"
#include "interp.h"
#include "strbuf.h"
#include <arena.h>
#include <debugconsole.h>
#include <kb.h>
#include <list.h>
#include <in_gb.h>
#include <in_input.h>
#include <malloc.h>
#include <mprintf.h>
#include <printf.h>
@@ -14,14 +13,10 @@
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <streams.h>
#include <string.h>
#include <system.h>
#include <tcursor.h>
#include <tscreen.h>
#define LINE_INIT_MAX 40
#define PROMPT "$ "
#define PROMPT "$ "
void* wmalloc (void* ctx, size_t size) {
(void)ctx;
@@ -51,7 +46,7 @@ void* warena_realloc (void* ctx, void* mem, size_t old, size_t new) {
void warena_free (void* ctx, void* mem) { (void)ctx, (void)mem; }
struct edit_line {
struct gapbuffer gb;
struct in_gb gb;
size_t cursor;
};
@@ -67,99 +62,14 @@ static void exec_line (const char* line) {
void app_main (void) {
libprocess_self_init ();
struct edit_line edit_line;
const char* render = NULL;
char line[1024];
while (interp_is_running ()) {
gapbuffer_init (&warena_malloc, &arena, &edit_line.gb, LINE_INIT_MAX);
edit_line.cursor = 0;
memset (line, 0, sizeof (line));
mprintf (PROMPT);
in_stream_read_line (PROMPT, line, sizeof (line) - 1);
for (;;) {
uint8_t ch;
if (stream_read (process_get_pgid (), STREAM_IN, &ch, 1) <= 0) {
sched ();
continue;
}
if (ch == '\n')
break;
gapbuffer_move (&edit_line.gb, edit_line.cursor);
switch (ch) {
case '\b':
if (edit_line.cursor > 0) {
edit_line.cursor--;
gapbuffer_backspace (&edit_line.gb);
mprintf (ANSIQ_CUR_LEFT (1));
char* tail =
gapbuffer_string_at (&warena_malloc, &arena, &edit_line.gb, edit_line.cursor);
mprintf ("%s" ANSIQ_SCR_CLR2LEND, tail);
int move_back = strlen (tail);
if (move_back > 0)
mprintf (ANSIQ_DYN_CUR_LEFT, move_back);
}
break;
case KB_DELETE:
if (edit_line.cursor < gapbuffer_length (&edit_line.gb)) {
edit_line.gb.gap_end++;
char* tail =
gapbuffer_string_at (&warena_malloc, &arena, &edit_line.gb, edit_line.cursor);
mprintf ("%s" ANSIQ_SCR_CLR2LEND, tail);
int move_back = strlen (tail);
if (move_back > 0)
mprintf (ANSIQ_DYN_CUR_LEFT, move_back);
}
break;
case KB_LEFT:
if (edit_line.cursor > 0) {
edit_line.cursor--;
mprintf (ANSIQ_CUR_LEFT (1));
}
break;
case KB_RIGHT:
if (edit_line.cursor < gapbuffer_length (&edit_line.gb)) {
edit_line.cursor++;
mprintf (ANSIQ_CUR_RIGHT (1));
}
break;
default:
if (isprint (ch)) {
gapbuffer_insert (&warena_realloc, &arena, &edit_line.gb, ch);
edit_line.cursor++;
if (edit_line.cursor == gapbuffer_length (&edit_line.gb)) {
mprintf ("%c", ch);
} else {
char* tail =
gapbuffer_string_at (&warena_malloc, &arena, &edit_line.gb, edit_line.cursor - 1);
size_t move_back = strlen (tail) - 1;
mprintf ("%s" ANSIQ_DYN_CUR_LEFT, tail, move_back);
}
}
break;
}
}
mprintf ("\n");
render = gapbuffer_string_at (&warena_malloc, &arena, &edit_line.gb, 0);
if (render != NULL)
exec_line (render);
gapbuffer_fini (&warena_free, &arena, &edit_line.gb);
arena_reset (&arena);
if (line[0] != '\0')
exec_line (line);
}
arena_destroy (&arena);
}

View File

@@ -1,9 +1,9 @@
#include "edit.h"
#include "arena_alloc.h"
#include "gapbuffer.h"
#include "walloc.h"
#include <arena.h>
#include <filewriter.h>
#include <in_gb.h>
#include <kb.h>
#include <list.h>
#include <malloc.h>
@@ -33,7 +33,7 @@ struct edit_line {
struct list_node_link select_link;
size_t select_start;
size_t select_end;
struct gapbuffer gb;
struct in_gb gb;
};
struct cursor {
@@ -79,10 +79,10 @@ static bool prepare_lines_cb (void* ctx, const char* start, size_t len) {
memset (line, 0, sizeof (*line));
size_t init_len = len > 0 ? len : 32;
gapbuffer_init (&wmalloc, NULL, &line->gb, init_len);
in_gb_init (&wmalloc, NULL, &line->gb, init_len);
for (size_t chr = 0; chr < len; chr++)
gapbuffer_insert (&wrealloc, NULL, &line->gb, start[chr]);
in_gb_insert (&wrealloc, NULL, &line->gb, start[chr]);
list_append (editor.lines, &line->lines_link);
@@ -246,17 +246,17 @@ void edit_start (const char* volume, const char* file_path, const char* text, si
case '\b':
if (editor.mode == EDIT_MODE_NORMAL) {
if (editor.cursor.col > 0) {
gapbuffer_move (&editor.current_line->gb, editor.cursor.col);
in_gb_move (&editor.current_line->gb, editor.cursor.col);
editor.cursor.col--;
gapbuffer_backspace (&editor.current_line->gb);
in_gb_backspace (&editor.current_line->gb);
} else {
if (editor.cursor.line > 0) {
struct list_node_link* prev = editor.current_line->lines_link.prev;
struct edit_line* prev_line = list_entry (prev, struct edit_line, lines_link);
size_t prev_len = gapbuffer_length (&prev_line->gb);
size_t prev_len = in_gb_length (&prev_line->gb);
gapbuffer_concat (&wrealloc, NULL, &prev_line->gb, &editor.current_line->gb);
in_gb_concat (&wrealloc, NULL, &prev_line->gb, &editor.current_line->gb);
list_remove (editor.lines, &editor.current_line->lines_link);
free (editor.current_line->gb.buffer);
@@ -272,26 +272,26 @@ void edit_start (const char* volume, const char* file_path, const char* text, si
break;
case '\t':
if (editor.mode == EDIT_MODE_NORMAL) {
gapbuffer_move (&editor.current_line->gb, editor.cursor.col);
in_gb_move (&editor.current_line->gb, editor.cursor.col);
for (size_t i = 0; i < sizeof (TAB_INSERT) - 1; i++)
gapbuffer_insert (&wrealloc, NULL, &editor.current_line->gb, TAB_INSERT[i]);
in_gb_insert (&wrealloc, NULL, &editor.current_line->gb, TAB_INSERT[i]);
editor.cursor.col += sizeof (TAB_INSERT) - 1;
}
break;
case '\n': {
if (editor.mode == EDIT_MODE_NORMAL) {
gapbuffer_move (&editor.current_line->gb, editor.cursor.col);
in_gb_move (&editor.current_line->gb, editor.cursor.col);
struct edit_line* new_line = malloc (sizeof (*new_line));
memset (new_line, 0, sizeof (*new_line));
char* tail = gapbuffer_string_at (&warena_malloc, &temp_arena, &editor.current_line->gb,
editor.cursor.col);
char* tail = in_gb_string_at (&warena_malloc, &temp_arena, &editor.current_line->gb,
editor.cursor.col);
size_t tail_size = strlen (tail);
size_t init_cap = tail_size > 0 ? tail_size : 32;
gapbuffer_init (&wmalloc, NULL, &new_line->gb, init_cap);
in_gb_init (&wmalloc, NULL, &new_line->gb, init_cap);
if (tail_size > 0) {
memcpy (new_line->gb.buffer,
@@ -337,10 +337,10 @@ void edit_start (const char* volume, const char* file_path, const char* text, si
} break;
case KB_DELETE:
if (editor.mode == EDIT_MODE_NORMAL) {
size_t len = gapbuffer_length (&editor.current_line->gb);
size_t len = in_gb_length (&editor.current_line->gb);
if (editor.cursor.col < len) {
gapbuffer_move (&editor.current_line->gb, editor.cursor.col);
in_gb_move (&editor.current_line->gb, editor.cursor.col);
if (editor.current_line->gb.gap_end < editor.current_line->gb.size)
editor.current_line->gb.gap_end++;
} else {
@@ -348,7 +348,7 @@ void edit_start (const char* volume, const char* file_path, const char* text, si
struct list_node_link* next = editor.current_line->lines_link.next;
struct edit_line* next_line = list_entry (next, struct edit_line, lines_link);
gapbuffer_concat (&wrealloc, NULL, &editor.current_line->gb, &next_line->gb);
in_gb_concat (&wrealloc, NULL, &editor.current_line->gb, &next_line->gb);
list_remove (editor.lines, &next_line->lines_link);
free (next_line->gb.buffer);
@@ -362,7 +362,7 @@ void edit_start (const char* volume, const char* file_path, const char* text, si
editor.cursor.col--;
break;
case KB_RIGHT:
if (editor.cursor.col < gapbuffer_length (&editor.current_line->gb))
if (editor.cursor.col < in_gb_length (&editor.current_line->gb))
editor.cursor.col++;
break;
case KB_UP: {
@@ -370,7 +370,7 @@ void edit_start (const char* volume, const char* file_path, const char* text, si
editor.cursor.line--;
editor.current_line =
list_entry (editor.current_line->lines_link.prev, struct edit_line, lines_link);
size_t len = gapbuffer_length (&editor.current_line->gb);
size_t len = in_gb_length (&editor.current_line->gb);
if (editor.cursor.col > len)
editor.cursor.col = len;
}
@@ -380,7 +380,7 @@ void edit_start (const char* volume, const char* file_path, const char* text, si
editor.cursor.line++;
editor.current_line =
list_entry (editor.current_line->lines_link.next, struct edit_line, lines_link);
size_t len = gapbuffer_length (&editor.current_line->gb);
size_t len = in_gb_length (&editor.current_line->gb);
if (editor.cursor.col > len)
editor.cursor.col = len;
}
@@ -389,7 +389,7 @@ void edit_start (const char* volume, const char* file_path, const char* text, si
editor.cursor.col = 0;
break;
case KB_END:
editor.cursor.col = gapbuffer_length (&editor.current_line->gb);
editor.cursor.col = in_gb_length (&editor.current_line->gb);
break;
case KB_ESCAPE:
if (editor.mode == EDIT_MODE_COMAMND) {
@@ -411,8 +411,8 @@ void edit_start (const char* volume, const char* file_path, const char* text, si
editor.cmdbuf.buffer[editor.cmdbuf.len] = '\0';
}
} else if (editor.mode == EDIT_MODE_NORMAL) {
gapbuffer_move (&editor.current_line->gb, editor.cursor.col);
gapbuffer_insert (&wrealloc, NULL, &editor.current_line->gb, ch);
in_gb_move (&editor.current_line->gb, editor.cursor.col);
in_gb_insert (&wrealloc, NULL, &editor.current_line->gb, ch);
editor.cursor.col++;
}
}

View File

@@ -1,137 +0,0 @@
#include "gapbuffer.h"
#include "arena_alloc.h"
#include <arena.h>
#include <malloc.h>
#include <stddef.h>
#include <string.h>
void gapbuffer_init (gb_malloc_func_t mallocfn, void* ctx, struct gapbuffer* gb, size_t capacity) {
gb->buffer = mallocfn (ctx, capacity);
if (gb->buffer == NULL)
return;
memset (gb->buffer, 0, capacity);
gb->size = capacity;
gb->gap_start = 0;
gb->gap_end = capacity;
}
void gapbuffer_fini (gb_free_func_t freefn, void* ctx, struct gapbuffer* gb) {
freefn (ctx, gb->buffer);
memset (gb, 0, sizeof (*gb));
}
void gapbuffer_grow (gb_realloc_func_t reallocfn, void* ctx, struct gapbuffer* gb) {
size_t old_size = gb->size;
size_t new_size = gb->size * 2;
char* new = reallocfn (ctx, gb->buffer, old_size, new_size);
if (new == NULL)
return;
gb->buffer = new;
size_t post_gap_size = old_size - gb->gap_end;
size_t new_gap_end = new_size - post_gap_size;
memmove (gb->buffer + new_gap_end, gb->buffer + gb->gap_end, post_gap_size);
gb->gap_end = new_gap_end;
gb->size = new_size;
}
void gapbuffer_move (struct gapbuffer* gb, size_t pos) {
if (pos > (gb->size - (gb->gap_end - gb->gap_start)))
pos = (gb->size - (gb->gap_end - gb->gap_start));
while (gb->gap_start > pos) {
gb->gap_start--;
gb->gap_end--;
gb->buffer[gb->gap_end] = gb->buffer[gb->gap_start];
}
while (gb->gap_start < pos) {
gb->buffer[gb->gap_start] = gb->buffer[gb->gap_end];
gb->gap_start++;
gb->gap_end++;
}
}
void gapbuffer_insert (gb_realloc_func_t reallocfn, void* ctx, struct gapbuffer* gb, char c) {
if (gb->gap_start == gb->gap_end)
gapbuffer_grow (reallocfn, ctx, gb);
gb->buffer[gb->gap_start] = c;
gb->gap_start++;
}
void gapbuffer_backspace (struct gapbuffer* gb) {
if (gb->gap_start > 0)
gb->gap_start--;
}
size_t gapbuffer_length (struct gapbuffer* gb) { return gb->size - (gb->gap_end - gb->gap_start); }
char* gapbuffer_string_at (gb_malloc_func_t mallocfn, void* ctx, struct gapbuffer* gb, size_t pos) {
size_t total_size = gapbuffer_length (gb);
if (pos >= total_size)
return "";
size_t tail_size = total_size - pos;
char* res = mallocfn (ctx, tail_size + 1);
if (res == NULL)
return NULL;
size_t written = 0;
if (pos < gb->gap_start) {
size_t copy = gb->gap_start - pos;
if (copy > tail_size)
copy = tail_size;
memcpy (res, &gb->buffer[pos], copy);
written += copy;
pos += copy;
}
if (written < tail_size) {
size_t gap_size = gb->gap_end - gb->gap_start;
size_t phys_pos = pos + gap_size;
size_t copy = tail_size - written;
memcpy (&res[written], &gb->buffer[phys_pos], copy);
written += copy;
}
res[written] = '\0';
return res;
}
void gapbuffer_concat (gb_realloc_func_t reallocfn, void* ctx, struct gapbuffer* dest,
struct gapbuffer* src) {
size_t src_len = gapbuffer_length (src);
if (src_len == 0)
return;
while ((dest->gap_end - dest->gap_start) < src_len)
gapbuffer_grow (reallocfn, ctx, dest);
gapbuffer_move (dest, gapbuffer_length (dest));
size_t part1_len = src->gap_start;
memcpy (dest->buffer + dest->gap_start, src->buffer, part1_len);
dest->gap_start += part1_len;
size_t part2_len = src->size - src->gap_end;
if (part2_len > 0) {
memcpy (dest->buffer + dest->gap_start, src->buffer + src->gap_end, part2_len);
dest->gap_start += part2_len;
}
}

View File

@@ -1,39 +0,0 @@
#ifndef _GAPBUFFER_H
#define _GAPBUFFER_H
#include <arena.h>
#include <stddef.h>
struct gapbuffer {
char* buffer;
size_t size;
size_t gap_start;
size_t gap_end;
};
typedef void* (*gb_malloc_func_t) (void*, size_t);
typedef void* (*gb_realloc_func_t) (void*, void*, size_t, size_t);
typedef void (*gb_free_func_t) (void*, void*);
void gapbuffer_init (gb_malloc_func_t mallocfn, void* ctx, struct gapbuffer* gb, size_t capacity);
void gapbuffer_fini (gb_free_func_t freefn, void* ctx, struct gapbuffer* gb);
void gapbuffer_move (struct gapbuffer* gb, size_t pos);
void gapbuffer_insert (gb_realloc_func_t reallocfn, void* ctx, struct gapbuffer* gb, char c);
void gapbuffer_backspace (struct gapbuffer* gb);
void gapbuffer_grow (gb_realloc_func_t reallocfn, void* ctx, struct gapbuffer* gb);
size_t gapbuffer_length (struct gapbuffer* gb);
char* gapbuffer_string_at (gb_malloc_func_t mallocfn, void* ctx, struct gapbuffer* gb, size_t pos);
void gapbuffer_concat (gb_realloc_func_t reallocfn, void* ctx, struct gapbuffer* dest,
struct gapbuffer* src);
#endif // _GAPBUFFER_H

View File

@@ -340,26 +340,15 @@ static void help (struct context* context) {
cprintf (context, "quit\n");
}
static void cmd_cancel_proc (void* arg) {
int pid = (int)(uintptr_t)arg;
uint8_t ch = 0;
for (;;) {
if (stream_read (process_get_pgid (), STREAM_IN, &ch, 1) > 0 && ch == KB_CTRL ('C'))
break;
else
sched ();
}
kill (pid);
}
struct cmd_write_proc_ctx {
int pgid;
int cancel_pid;
};
static void cmd_collect_proc (void* arg) {
#define RECV_MAX (1024 * 16)
int pgid = (int)(uintptr_t)arg;
char recv[RECV_MAX];
char recv[1024];
int n;
for (;;) {
if ((n = stream_read (pgid, STREAM_OUT, (void*)recv, sizeof (recv) - 1)) > 0)
@@ -369,6 +358,24 @@ static void cmd_collect_proc (void* arg) {
}
}
static void cmd_write_proc (void* arg) {
struct cmd_write_proc_ctx* ctx = arg;
uint8_t ch = 0;
for (;;) {
if (stream_read (process_get_pgid (), STREAM_IN, &ch, 1) > 0) {
if (ch == KB_CTRL ('C')) {
kill (ctx->cancel_pid);
return;
}
stream_write (ctx->pgid, STREAM_IN, &ch, 1);
} else {
sched ();
}
}
}
static void execute_cmd (struct ast_cmd* cmd, struct context* context) {
if (strcmp (cmd->name, "echo") == 0) {
echo (context, cmd->args, cmd->arg_count);
@@ -442,16 +449,18 @@ static void execute_cmd (struct ast_cmd* cmd, struct context* context) {
}
}
struct cmd_write_proc_ctx wpctx = {.pgid = pgid, .cancel_pid = pid};
struct process_data* write_pdata = process_spawn (&cmd_write_proc, (void*)&wpctx);
struct process_data* collect_pdata = process_spawn (&cmd_collect_proc, (void*)(uintptr_t)pgid);
struct process_data* cancel_pdata = process_spawn (&cmd_cancel_proc, (void*)(uintptr_t)pid);
exec_partial_fini (pid);
wait_for_pid (pid);
kill (write_pdata->pid);
process_data_free (write_pdata);
kill (collect_pdata->pid);
process_data_free (collect_pdata);
kill (cancel_pdata->pid);
process_data_free (cancel_pdata);
}
}

View File

@@ -4,7 +4,6 @@ c += ce.c \
context.c \
parser.c \
interp.c \
gapbuffer.c \
edit.c
o += ce.o \
@@ -13,5 +12,4 @@ o += ce.o \
context.o \
parser.o \
interp.o \
gapbuffer.o \
edit.o