aboutsummaryrefslogtreecommitdiff
path: root/sys/cmd/rc/prompt.c
diff options
context:
space:
mode:
Diffstat (limited to 'sys/cmd/rc/prompt.c')
-rw-r--r--sys/cmd/rc/prompt.c101
1 files changed, 101 insertions, 0 deletions
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 <assert.h>
+#include <errno.h>
+#include <stdio.h>
+#include <sys/stat.h>
+
+#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;
+}