From 2ade60747db41771498ab2b85ce6e3c3389f2c26 Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Wed, 13 Oct 2021 09:08:59 -0700 Subject: feat(rc): added unix port of rc with linenoise --- sys/cmd/rc/prompt.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 sys/cmd/rc/prompt.c (limited to 'sys/cmd/rc/prompt.c') diff --git a/sys/cmd/rc/prompt.c b/sys/cmd/rc/prompt.c new file mode 100644 index 0000000..b51e509 --- /dev/null +++ b/sys/cmd/rc/prompt.c @@ -0,0 +1,101 @@ +#include "rc.h" +#include "fns.h" +#include "io.h" +#include "exec.h" +#include "getflags.h" +#include +#include +#include +#include + +#include "linenoise.h" + +#define DEBUG(f, a...) \ + if(0){}else{pfmt(err, "\n%s: "f, __FUNCTION__, ## a);flush(err);} + +#define HISTORY_PATH "~/.local/rc/history" + +#define COMPLETE_FN "complete" +#define COMPLETE_RESULTS "rc_complete_results" + +static +char* +completion_matches(const char *s, int i) +{ + word *w; + + for(w=vlook(COMPLETE_RESULTS)->val; w; w=w->next, i--) + if(!i) + return strdup(w->word); + return nil; +} + +static +void +completion(const char *text, linenoiseCompletions *lc) +{ + linenoiseAddCompletion(lc, "test"); +} + +static +void +savehist(void) +{ + linenoiseHistorySave(HISTORY_PATH); +} + +static +void +readline(void) +{ + static int first = 1; + io *f = runq->cmdfd; + char *s; + long n; + + if(first){ + linenoiseSetMultiLine(1); + linenoiseSetCompletionCallback(completion); + + linenoiseHistorySetMaxLen(10000); + + /* history */ + if(linenoiseHistoryLoad(HISTORY_PATH)!=0 && errno!=ENOENT) + pfmt(err, "rc: loadhistory: %s\n", strerror(errno)); + + atexit(savehist); + first = 0; + } + + s = linenoise(promptstr); + if(!s) + return; + + n = strlen(s); + assert(n < NBUF-1); + strcpy(f->buf, s); + f->buf[n++] = '\n'; + f->bufp = f->buf; + f->ebuf = f->buf+n; + + linenoiseHistoryAdd(s); + free(s); +} + +void +pprompt(void) +{ + var *prompt; + + if(runq->iflag){ + flush(err); + readline(); + prompt = vlook("prompt"); + if(prompt->val && prompt->val->next) + promptstr = prompt->val->next->word; + else + promptstr="\t"; + } + runq->lineno++; + doprompt = 0; +} -- cgit v1.2.1