From 27c900d6ae9cd0dcf4bc97bc9b8c1882e40ccfa8 Mon Sep 17 00:00:00 2001 From: Toby Goodwin Date: Fri, 15 May 2015 19:36:55 +0100 Subject: snapshot from cvs: 2005-03-03 --- edit-gnu.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 69 insertions(+), 23 deletions(-) (limited to 'edit-gnu.c') diff --git a/edit-gnu.c b/edit-gnu.c index 4b5d2b6..c0b953e 100644 --- a/edit-gnu.c +++ b/edit-gnu.c @@ -1,38 +1,84 @@ #include "rc.h" +#include #include #include +#include -#include "jbwrap.h" +#include "edit.h" -struct Jbwrap rl_buf; -volatile sig_atomic_t rl_active; +bool editing = 1; + +struct cookie { + char *buffer; +}; + +void *edit_begin(int fd) { + List *hist; + struct cookie *c; -void edit_init(void *ignored) { rl_catch_signals = 0; + + hist = varlookup("history"); + if (hist != NULL) + if (read_history(hist->w) != 0 && errno != ENOENT) /* ignore if missing */ + uerror(hist->w); + + c = ealloc(sizeof *c); + c->buffer = NULL; + return c; } -char *edit_getline(void *ignored, char *prompt) { - char *r = NULL; - int s; - - if ((s = sigsetjmp(rl_buf.j, 1)) == 0) { - rl_active = TRUE; - r = readline(prompt); - } else { - rl_free_line_state(); - rl_cleanup_after_signal(); - rl_active = FALSE; - rc_raise(eError); - } +static void edit_catcher(int sig) { + write(2, "\n", 1); + rc_raise(eError); +} + +static char *prompt; + +char *edit_alloc(void *cookie, int *count) { + struct cookie *c = cookie; + void (*oldint)(int), (*oldquit)(int); + + oldint = sys_signal(SIGINT, edit_catcher); + oldquit = sys_signal(SIGQUIT, edit_catcher); + + c->buffer = readline(prompt); - if (r) { - r = erealloc(r, strlen(r) + 2); /* 1 for \n + 1 for \0 */ - strcat(r, "\n"); + sys_signal(SIGINT, oldint); + sys_signal(SIGQUIT, oldquit); + + if (c->buffer) { + *count = strlen(c->buffer); + if (*count) + add_history(c->buffer); + c->buffer[*count] = '\n'; + ++*count; /* include the \n */ } - return r; + return c->buffer; +} + +void edit_prompt(void *cookie, char *pr) { + prompt = pr; +} + +void edit_free(void *cookie) { + struct cookie *c = cookie; + + efree(c->buffer); + /* Set c->buffer to NULL, allowing us to "overfree" it. This + is a bit of a kludge, but it's otherwise hard to deal with + the case where a signal causes an early return from + readline. */ + c->buffer = NULL; +} + +void edit_end(void *cookie) { + struct cookie *c = cookie; + + efree(c); } -void edit_free(void *ignored, void *buffer) { - efree(buffer); +void edit_reset(void *cookie) { + rl_reset_terminal(NULL); } -- cgit v1.2.3