2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Christos Zoulas of Cornell University.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
35 __COPYRIGHT("@(#) Copyright (c) 1992, 1993\n\
36 The Regents of the University of California. All rights reserved.\n");
39 #if !defined(lint) && !defined(SCCSID)
40 static char sccsid[] = "@(#)test.c 8.1 (Berkeley) 6/4/93";
41 #endif /* not lint && not SCCSID */
42 __RCSID("$NetBSD: test.c,v 1.3 2009/07/17 12:25:52 christos Exp $");
43 __FBSDID("$FreeBSD$");
46 * test.c: A little test program
60 static int continuation = 0;
61 volatile sig_atomic_t gotsig = 0;
63 static unsigned char complete(EditLine *, int);
64 int main(int, char **);
65 static char *prompt(EditLine *);
71 static char a[] = "\1\e[7m\1Edit$\1\e[0m\1 ";
72 static char b[] = "Edit> ";
74 return (continuation ? b : a);
84 complete(EditLine *el, int ch)
86 DIR *dd = opendir(".");
89 const LineInfo *lf = el_line(el);
95 for (ptr = lf->cursor - 1;
96 !isspace((unsigned char)*ptr) && ptr > lf->buffer; ptr--)
98 len = lf->cursor - ++ptr;
100 for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) {
101 if (len > strlen(dp->d_name))
103 if (strncmp(dp->d_name, ptr, len) == 0) {
105 if (el_insertstr(el, &dp->d_name[len]) == -1)
117 main(int argc, char *argv[])
130 (void) signal(SIGINT, sig);
131 (void) signal(SIGQUIT, sig);
132 (void) signal(SIGHUP, sig);
133 (void) signal(SIGTERM, sig);
135 hist = history_init(); /* Init the builtin history */
136 /* Remember 100 events */
137 history(hist, &ev, H_SETSIZE, 100);
139 tok = tok_init(NULL); /* Initialize the tokenizer */
141 /* Initialize editline */
142 el = el_init(*argv, stdin, stdout, stderr);
144 el_set(el, EL_EDITOR, "vi"); /* Default editor is vi */
145 el_set(el, EL_SIGNAL, 1); /* Handle signals gracefully */
146 el_set(el, EL_PROMPT_ESC, prompt, '\1');/* Set the prompt function */
148 /* Tell editline to use this history interface */
149 el_set(el, EL_HIST, history, hist);
151 /* Add a user-defined function */
152 el_set(el, EL_ADDFN, "ed-complete", "Complete argument", complete);
155 el_set(el, EL_BIND, "^I", "ed-complete", NULL);
158 * Bind j, k in vi command mode to previous and next line, instead
159 * of previous and next history.
161 el_set(el, EL_BIND, "-a", "k", "ed-prev-line", NULL);
162 el_set(el, EL_BIND, "-a", "j", "ed-next-line", NULL);
165 * Source the user's defaults file.
169 while ((buf = el_gets(el, &num)) != NULL && num != 0) {
178 (void) fprintf(stderr, "==> got %d %s", num, buf);
179 (void) fprintf(stderr, " > li `%.*s_%.*s'\n",
180 (li->cursor - li->buffer), li->buffer,
181 (li->lastchar - 1 - li->cursor),
182 (li->cursor >= li->lastchar) ? "" : li->cursor);
186 (void) fprintf(stderr, "Got signal %d.\n", (int)gotsig);
191 if (!continuation && num == 1)
195 ncontinuation = tok_line(tok, li, &ac, &av, &cc, &co);
196 if (ncontinuation < 0) {
197 (void) fprintf(stderr, "Internal error\n");
202 (void) fprintf(stderr, " > nc %d ac %d cc %d co %d\n",
203 ncontinuation, ac, cc, co);
208 * Append to the right event in case the user
209 * moved around in history.
211 if (history(hist, &ev, H_SET, lastevent) == -1)
212 err(1, "%d: %s", lastevent, ev.str);
213 history(hist, &ev, H_ADD , buf);
215 history(hist, &ev, H_ENTER, buf);
220 history(hist, &ev, continuation ? H_APPEND : H_ENTER, buf);
223 continuation = ncontinuation;
228 for (i = 0; i < ac; i++) {
229 (void) fprintf(stderr, " > arg# %2d ", i);
231 (void) fprintf(stderr, "`%s'\n", av[i]);
233 (void) fprintf(stderr, "`%.*s_%s'\n",
234 co, av[i], av[i] + co);
238 if (strcmp(av[0], "history") == 0) {
243 for (rv = history(hist, &ev, H_LAST); rv != -1;
244 rv = history(hist, &ev, H_PREV))
245 (void) fprintf(stdout, "%4d %s",
250 if (strcmp(av[1], "clear") == 0)
251 history(hist, &ev, H_CLEAR);
257 if (strcmp(av[1], "load") == 0)
258 history(hist, &ev, H_LOAD, av[2]);
259 else if (strcmp(av[1], "save") == 0)
260 history(hist, &ev, H_SAVE, av[2]);
265 (void) fprintf(stderr,
266 "Bad history arguments\n");
269 } else if (el_parse(el, ac, av) == -1) {
272 execvp(av[0], __DECONST(char **, av));
283 if (wait(&num) == -1)
285 (void) fprintf(stderr, "Exit %x\n", num);