summaryrefslogtreecommitdiffstats
path: root/lib/libedit/TEST/test.c
diff options
context:
space:
mode:
authorotto <otto@openbsd.org>2003-10-31 08:42:23 +0000
committerotto <otto@openbsd.org>2003-10-31 08:42:23 +0000
commitd484b7d03ace7dfad66bf1845501ed21cdb16b83 (patch)
tree7217919f9a70564b29131d02c66a64d9abcfe577 /lib/libedit/TEST/test.c
parentregen. (diff)
downloadwireguard-openbsd-d484b7d03ace7dfad66bf1845501ed21cdb16b83.tar.xz
wireguard-openbsd-d484b7d03ace7dfad66bf1845501ed21cdb16b83.zip
Update to NetBSD libedit (from Oct 1, 2003), adding some string
cleaning and history bug fixes. The code includes GNU libreadline functionality, but the corresponding header files are not installed, since some libreadline functions are missing. There are some minor API changes, notably: old: EditLine *el_init(const char *, FILE *, FILE *); new: EditLine *el_init(const char *, FILE *, FILE *, FILE *); old: HistEvent *history(History *h, int op, ...); new: int history(History *h, HistEvent *ev, int op, ...); plus some changes in operation names. See editline(3) for details. Tested by djm@, mouring@, jmc@. ok deraadt@
Diffstat (limited to 'lib/libedit/TEST/test.c')
-rw-r--r--lib/libedit/TEST/test.c319
1 files changed, 169 insertions, 150 deletions
diff --git a/lib/libedit/TEST/test.c b/lib/libedit/TEST/test.c
index 42ec0f7904c..78adcf6a374 100644
--- a/lib/libedit/TEST/test.c
+++ b/lib/libedit/TEST/test.c
@@ -1,4 +1,5 @@
-/* $OpenBSD: test.c,v 1.5 2003/06/02 20:18:40 millert Exp $ */
+/* $OpenBSD: test.c,v 1.6 2003/10/31 08:42:24 otto Exp $ */
+/* $NetBSD: test.c,v 1.13 2003/08/07 16:44:35 agc Exp $ */
/*-
* Copyright (c) 1992, 1993
@@ -32,24 +33,23 @@
* SUCH DAMAGE.
*/
+#include "config.h"
#ifndef lint
-static char copyright[] =
-"@(#) Copyright (c) 1992, 1993\n\
- The Regents of the University of California. All rights reserved.\n";
+//__COPYRIGHT("@(#) Copyright (c) 1992, 1993\n\
+// The Regents of the University of California. All rights reserved.\n");
#endif /* not lint */
#if !defined(lint) && !defined(SCCSID)
#if 0
static char sccsid[] = "@(#)test.c 8.1 (Berkeley) 6/4/93";
#else
-static const char rcsid[] = "$OpenBSD: test.c,v 1.5 2003/06/02 20:18:40 millert Exp $";
+static const char *rcsid = "$OpenBSD: test.c,v 1.6 2003/10/31 08:42:24 otto Exp $";
#endif
#endif /* not lint && not SCCSID */
/*
* test.c: A little test program
*/
-#include "sys.h"
#include <stdio.h>
#include <string.h>
#include <signal.h>
@@ -65,182 +65,201 @@ static const char rcsid[] = "$OpenBSD: test.c,v 1.5 2003/06/02 20:18:40 millert
static int continuation = 0;
static EditLine *el = NULL;
+static u_char complete(EditLine *, int);
+ int main(int, char **);
+static char *prompt(EditLine *);
+static void sig(int);
+
static char *
-/*ARGSUSED*/
-prompt(el)
- EditLine *el;
+prompt(EditLine *el)
{
- static char a[] = "Edit$";
- static char b[] = "Edit>";
- return continuation ? b : a;
+ static char a[] = "Edit$";
+ static char b[] = "Edit>";
+
+ return (continuation ? b : a);
}
static void
-sig(i)
- int i;
+sig(int i)
{
- (void) fprintf(stderr, "Got signal %d.\n", i);
- el_reset(el);
+
+ (void) fprintf(stderr, "Got signal %d.\n", i);
+ el_reset(el);
}
static unsigned char
-/*ARGSUSED*/
-complete(el, ch)
- EditLine *el;
- int ch;
+complete(EditLine *el, int ch)
{
- DIR *dd = opendir(".");
- struct dirent *dp;
- const char* ptr;
- const LineInfo *lf = el_line(el);
- int len;
-
- /*
- * Find the last word
- */
- for (ptr = lf->cursor - 1; !isspace(*ptr) && ptr > lf->buffer; ptr--)
- continue;
- len = lf->cursor - ++ptr;
-
- for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) {
- if (len > strlen(dp->d_name))
- continue;
- if (strncmp(dp->d_name, ptr, len) == 0) {
- closedir(dd);
- if (el_insertstr(el, &dp->d_name[len]) == -1)
- return CC_ERROR;
- else
- return CC_REFRESH;
+ DIR *dd = opendir(".");
+ struct dirent *dp;
+ const char* ptr;
+ const LineInfo *lf = el_line(el);
+ int len;
+
+ /*
+ * Find the last word
+ */
+ for (ptr = lf->cursor - 1; !isspace(*ptr) && ptr > lf->buffer; ptr--)
+ continue;
+ len = lf->cursor - ++ptr;
+
+ for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) {
+ if (len > strlen(dp->d_name))
+ continue;
+ if (strncmp(dp->d_name, ptr, len) == 0) {
+ closedir(dd);
+ if (el_insertstr(el, &dp->d_name[len]) == -1)
+ return (CC_ERROR);
+ else
+ return (CC_REFRESH);
+ }
}
- }
- closedir(dd);
- return CC_ERROR;
+ closedir(dd);
+ return (CC_ERROR);
}
int
-/*ARGSUSED*/
-main(argc, argv)
- int argc;
- char *argv[];
+main(int argc, char *argv[])
{
- int num;
- const char *buf;
- Tokenizer *tok;
- History *hist;
+ int num;
+ const char *buf;
+ Tokenizer *tok;
+#if 0
+ int lastevent = 0;
+#endif
+ int ncontinuation;
+ History *hist;
+ HistEvent ev;
- (void) signal(SIGINT, sig);
- (void) signal(SIGQUIT, sig);
- (void) signal(SIGHUP, sig);
- (void) signal(SIGTERM, sig);
+ (void) signal(SIGINT, sig);
+ (void) signal(SIGQUIT, sig);
+ (void) signal(SIGHUP, sig);
+ (void) signal(SIGTERM, sig);
- hist = history_init(); /* Init the builtin history */
- history(hist, H_EVENT, 100); /* Remember 100 events */
+ hist = history_init(); /* Init the builtin history */
+ /* Remember 100 events */
+ history(hist, &ev, H_SETSIZE, 100);
- tok = tok_init(NULL); /* Initialize the tokenizer */
+ tok = tok_init(NULL); /* Initialize the tokenizer */
- el = el_init(*argv, stdin, stdout); /* Initialize editline */
+ /* Initialize editline */
+ el = el_init(*argv, stdin, stdout, stderr);
- el_set(el, EL_EDITOR, "vi"); /* Default editor is vi */
- el_set(el, EL_SIGNAL, 1); /* Handle signals gracefully */
- el_set(el, EL_PROMPT, prompt); /* Set the prompt function */
+ el_set(el, EL_EDITOR, "vi"); /* Default editor is vi */
+ el_set(el, EL_SIGNAL, 1); /* Handle signals gracefully */
+ el_set(el, EL_PROMPT, prompt); /* Set the prompt function */
- /* Tell editline to use this history interface */
- el_set(el, EL_HIST, history, hist);
+ /* Tell editline to use this history interface */
+ el_set(el, EL_HIST, history, hist);
- /* Add a user-defined function */
- el_set(el, EL_ADDFN, "ed-complete", "Complete argument", complete);
+ /* Add a user-defined function */
+ el_set(el, EL_ADDFN, "ed-complete", "Complete argument", complete);
- el_set(el, EL_BIND, "^I", "ed-complete", NULL);/* Bind tab to it */
+ /* Bind tab to it */
+ el_set(el, EL_BIND, "^I", "ed-complete", NULL);
- /*
- * Bind j, k in vi command mode to previous and next line, instead
- * of previous and next history.
- */
- el_set(el, EL_BIND, "-a", "k", "ed-prev-line", NULL);
- el_set(el, EL_BIND, "-a", "j", "ed-next-line", NULL);
+ /*
+ * Bind j, k in vi command mode to previous and next line, instead
+ * of previous and next history.
+ */
+ el_set(el, EL_BIND, "-a", "k", "ed-prev-line", NULL);
+ el_set(el, EL_BIND, "-a", "j", "ed-next-line", NULL);
- /*
- * Source the user's defaults file.
- */
- el_source(el, NULL);
+ /*
+ * Source the user's defaults file.
+ */
+ el_source(el, NULL);
- while ((buf = el_gets(el, &num)) != NULL && num != 0) {
- int ac;
- char **av;
+ while ((buf = el_gets(el, &num)) != NULL && num != 0) {
+ int ac;
+ const char **av;
#ifdef DEBUG
- (void) fprintf(stderr, "got %d %s", num, buf);
+ (void) fprintf(stderr, "got %d %s", num, buf);
#endif
- if (!continuation && num == 1)
- continue;
+ if (!continuation && num == 1)
+ continue;
- if (tok_line(tok, buf, &ac, &av) > 0) {
- history(hist, continuation ? H_ADD : H_ENTER, buf);
- continuation = 1;
- continue;
- }
+ ncontinuation = tok_line(tok, buf, &ac, &av) > 0;
+#if 0
+ if (continuation) {
+ /*
+ * Append to the right event in case the user
+ * moved around in history.
+ */
+ if (history(hist, &ev, H_SET, lastevent) == -1)
+ err(1, "%d: %s", lastevent, ev.str);
+ history(hist, &ev, H_ADD , buf);
+ } else {
+ history(hist, &ev, H_ENTER, buf);
+ lastevent = ev.num;
+ }
+#else
+ /* Simpler */
+ history(hist, &ev, continuation ? H_APPEND : H_ENTER, buf);
+#endif
- history(hist, continuation ? H_ADD : H_ENTER, buf);
-
- continuation = 0;
-
- if (strcmp(av[0], "history") == 0) {
- const struct HistEvent *he;
-
- switch (ac) {
- case 1:
- for (he = history(hist, H_LAST); he;
- he = history(hist, H_PREV))
- (void) fprintf(stdout, "%4d %s", he->num, he->str);
- break;
-
- case 2:
- if (strcmp(av[1], "clear") == 0)
- history(hist, H_CLEAR);
- else
- goto badhist;
- break;
-
- case 3:
- if (strcmp(av[1], "load") == 0)
- history(hist, H_LOAD, av[2]);
- else if (strcmp(av[1], "save") == 0)
- history(hist, H_SAVE, av[2]);
- break;
-
- badhist:
- default:
- (void) fprintf(stderr, "Bad history arguments\n");
- break;
- }
+ continuation = ncontinuation;
+ ncontinuation = 0;
+
+ if (strcmp(av[0], "history") == 0) {
+ int rv;
+
+ switch (ac) {
+ case 1:
+ for (rv = history(hist, &ev, H_LAST); rv != -1;
+ rv = history(hist, &ev, H_PREV))
+ (void) fprintf(stdout, "%4d %s",
+ ev.num, ev.str);
+ break;
+
+ case 2:
+ if (strcmp(av[1], "clear") == 0)
+ history(hist, &ev, H_CLEAR);
+ else
+ goto badhist;
+ break;
+
+ case 3:
+ if (strcmp(av[1], "load") == 0)
+ history(hist, &ev, H_LOAD, av[2]);
+ else if (strcmp(av[1], "save") == 0)
+ history(hist, &ev, H_SAVE, av[2]);
+ break;
+
+ badhist:
+ default:
+ (void) fprintf(stderr,
+ "Bad history arguments\n");
+ break;
+ }
+ } else if (el_parse(el, ac, av) == -1) {
+ switch (fork()) {
+ case 0:
+ execvp(av[0], (char *const *)av);
+ perror(av[0]);
+ _exit(1);
+ /*NOTREACHED*/
+ break;
+
+ case -1:
+ perror("fork");
+ break;
+
+ default:
+ if (wait(&num) == -1)
+ perror("wait");
+ (void) fprintf(stderr, "Exit %x\n", num);
+ break;
+ }
+ }
+
+ tok_reset(tok);
}
- else if (el_parse(el, ac, av) == -1) {
- switch (fork()) {
- case 0:
- execvp(av[0], av);
- perror(av[0]);
- _exit(1);
- /*NOTREACHED*/
- break;
-
- case -1:
- perror("fork");
- break;
-
- default:
- if (wait(&num) == -1)
- perror("wait");
- (void) fprintf(stderr, "Exit %x\n", num);
- break;
- }
- }
-
- tok_reset(tok);
- }
- el_end(el);
- tok_end(tok);
- history_end(hist);
+ el_end(el);
+ tok_end(tok);
+ history_end(hist);
- return 0;
+ return (0);
}