From 2d3c17c15295d88f9d6b54cc6b1d521222377e6c Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Mon, 20 Feb 2023 12:53:58 -0800 Subject: [PATCH] Linenoise with rax. --- implementations/C/Makefile | 2 +- implementations/C/try_rax.c | 38 +++++++++++++++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/implementations/C/Makefile b/implementations/C/Makefile index 4f28bb4..e8c5df1 100644 --- a/implementations/C/Makefile +++ b/implementations/C/Makefile @@ -47,4 +47,4 @@ linenoise.o: linenoise.c linenoise.h cc -c linenoise.c -o linenoise.o tryrax: try_rax.c - cc try_rax.c rax.o -lm -o tryrax + cc try_rax.c rax.o linenoise.o -lm -o tryrax diff --git a/implementations/C/try_rax.c b/implementations/C/try_rax.c index a9cee11..9a1cc9a 100644 --- a/implementations/C/try_rax.c +++ b/implementations/C/try_rax.c @@ -1,14 +1,38 @@ #include +#include +#include #include +#include "linenoise.h" #include "rax.h" +rax *rt; + #define INSERT(key) raxInsert(rt, (unsigned char*)(key), sizeof((key)), NULL, NULL); + +void +completion(const char *buf, linenoiseCompletions *lc) +{ + int n = strnlen(buf, 1024); + raxIterator iter; + raxStart(&iter, rt); + raxSeek(&iter, ">=", (unsigned char*)buf, n); + while(raxNext(&iter)) { + if (strncmp((const char *)iter.key, buf, n)) + break; + linenoiseAddCompletion(lc, (const char *)iter.key); + } + raxStop(&iter); +} + + int main() { - rax *rt = raxNew(); + char *line; + linenoiseSetCompletionCallback(completion); + rt = raxNew(); INSERT("=") INSERT(">") INSERT("<") @@ -177,6 +201,16 @@ main() INSERT("_\\/_") INSERT("/\\") INSERT("\\/") + /* raxShow(rt); -} + */ + while (1) { + line = linenoise("eh? "); + if (NULL == line) { + printf("\n"); + break; + } + linenoiseHistoryAdd(line); + } +}