Switch from bioq_insert_tail() to bioqdisksort(). When the kernel is
[dragonfly.git] / lib / libedit / TEST / test.c
blob3682527f632326fb463801c729bee4924b19b017
1 /*-
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
10 * are met:
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
30 * SUCH DAMAGE.
32 * @(#)test.c 8.1 (Berkeley) 6/4/93
33 * $NetBSD: test.c,v 1.18 2005/06/01 11:37:52 lukem Exp $
34 * $DragonFly: src/lib/libedit/TEST/test.c,v 1.3 2005/11/13 11:58:30 corecode Exp $
37 #include "config.h"
38 #ifndef lint
39 __COPYRIGHT("@(#) Copyright (c) 1992, 1993\n\
40 The Regents of the University of California. All rights reserved.\n");
41 #endif /* not lint */
44 * test.c: A little test program
46 #include <stdio.h>
47 #include <string.h>
48 #include <signal.h>
49 #include <sys/wait.h>
50 #include <ctype.h>
51 #include <stdlib.h>
52 #include <unistd.h>
53 #include <dirent.h>
55 #include "histedit.h"
57 static int continuation = 0;
58 volatile sig_atomic_t gotsig = 0;
60 static unsigned char complete(EditLine *, int);
61 int main(int, char **);
62 static char *prompt(EditLine *);
63 static void sig(int);
65 static char *
66 prompt(EditLine *el)
68 static char a[] = "Edit$ ";
69 static char b[] = "Edit> ";
71 return (continuation ? b : a);
74 static void
75 sig(int i)
77 gotsig = i;
80 static unsigned char
81 complete(EditLine *el, int ch)
83 DIR *dd = opendir(".");
84 struct dirent *dp;
85 const char* ptr;
86 const LineInfo *lf = el_line(el);
87 int len;
90 * Find the last word
92 for (ptr = lf->cursor - 1;
93 !isspace((unsigned char)*ptr) && ptr > lf->buffer; ptr--)
94 continue;
95 len = lf->cursor - ++ptr;
97 for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) {
98 if (len > strlen(dp->d_name))
99 continue;
100 if (strncmp(dp->d_name, ptr, len) == 0) {
101 closedir(dd);
102 if (el_insertstr(el, &dp->d_name[len]) == -1)
103 return (CC_ERROR);
104 else
105 return (CC_REFRESH);
109 closedir(dd);
110 return (CC_ERROR);
114 main(int argc, char *argv[])
116 EditLine *el = NULL;
117 int num;
118 const char *buf;
119 Tokenizer *tok;
120 #if 0
121 int lastevent = 0;
122 #endif
123 int ncontinuation;
124 History *hist;
125 HistEvent ev;
127 (void) signal(SIGINT, sig);
128 (void) signal(SIGQUIT, sig);
129 (void) signal(SIGHUP, sig);
130 (void) signal(SIGTERM, sig);
132 hist = history_init(); /* Init the builtin history */
133 /* Remember 100 events */
134 history(hist, &ev, H_SETSIZE, 100);
136 tok = tok_init(NULL); /* Initialize the tokenizer */
138 /* Initialize editline */
139 el = el_init(*argv, stdin, stdout, stderr);
141 el_set(el, EL_EDITOR, "vi"); /* Default editor is vi */
142 el_set(el, EL_SIGNAL, 1); /* Handle signals gracefully */
143 el_set(el, EL_PROMPT, prompt); /* Set the prompt function */
145 /* Tell editline to use this history interface */
146 el_set(el, EL_HIST, history, hist);
148 /* Add a user-defined function */
149 el_set(el, EL_ADDFN, "ed-complete", "Complete argument", complete);
151 /* Bind tab to it */
152 el_set(el, EL_BIND, "^I", "ed-complete", NULL);
155 * Bind j, k in vi command mode to previous and next line, instead
156 * of previous and next history.
158 el_set(el, EL_BIND, "-a", "k", "ed-prev-line", NULL);
159 el_set(el, EL_BIND, "-a", "j", "ed-next-line", NULL);
162 * Source the user's defaults file.
164 el_source(el, NULL);
166 while ((buf = el_gets(el, &num)) != NULL && num != 0) {
167 int ac, cc, co;
168 #ifdef DEBUG
169 int i;
170 #endif
171 const char **av;
172 const LineInfo *li;
173 li = el_line(el);
174 #ifdef DEBUG
175 (void) fprintf(stderr, "==> got %d %s", num, buf);
176 (void) fprintf(stderr, " > li `%.*s_%.*s'\n",
177 (li->cursor - li->buffer), li->buffer,
178 (li->lastchar - 1 - li->cursor),
179 (li->cursor >= li->lastchar) ? "" : li->cursor);
181 #endif
182 if (gotsig) {
183 (void) fprintf(stderr, "Got signal %d.\n", gotsig);
184 gotsig = 0;
185 el_reset(el);
188 if (!continuation && num == 1)
189 continue;
191 ac = cc = co = 0;
192 ncontinuation = tok_line(tok, li, &ac, &av, &cc, &co);
193 if (ncontinuation < 0) {
194 (void) fprintf(stderr, "Internal error\n");
195 continuation = 0;
196 continue;
198 #ifdef DEBUG
199 (void) fprintf(stderr, " > nc %d ac %d cc %d co %d\n",
200 ncontinuation, ac, cc, co);
201 #endif
202 #if 0
203 if (continuation) {
205 * Append to the right event in case the user
206 * moved around in history.
208 if (history(hist, &ev, H_SET, lastevent) == -1)
209 err(1, "%d: %s", lastevent, ev.str);
210 history(hist, &ev, H_ADD , buf);
211 } else {
212 history(hist, &ev, H_ENTER, buf);
213 lastevent = ev.num;
215 #else
216 /* Simpler */
217 history(hist, &ev, continuation ? H_APPEND : H_ENTER, buf);
218 #endif
220 continuation = ncontinuation;
221 ncontinuation = 0;
222 if (continuation)
223 continue;
224 #ifdef DEBUG
225 for (i = 0; i < ac; i++) {
226 (void) fprintf(stderr, " > arg# %2d ", i);
227 if (i != cc)
228 (void) fprintf(stderr, "`%s'\n", av[i]);
229 else
230 (void) fprintf(stderr, "`%.*s_%s'\n",
231 co, av[i], av[i] + co);
233 #endif
235 if (strcmp(av[0], "history") == 0) {
236 int rv;
238 switch (ac) {
239 case 1:
240 for (rv = history(hist, &ev, H_LAST); rv != -1;
241 rv = history(hist, &ev, H_PREV))
242 (void) fprintf(stdout, "%4d %s",
243 ev.num, ev.str);
244 break;
246 case 2:
247 if (strcmp(av[1], "clear") == 0)
248 history(hist, &ev, H_CLEAR);
249 else
250 goto badhist;
251 break;
253 case 3:
254 if (strcmp(av[1], "load") == 0)
255 history(hist, &ev, H_LOAD, av[2]);
256 else if (strcmp(av[1], "save") == 0)
257 history(hist, &ev, H_SAVE, av[2]);
258 break;
260 badhist:
261 default:
262 (void) fprintf(stderr,
263 "Bad history arguments\n");
264 break;
266 } else if (el_parse(el, ac, av) == -1) {
267 switch (fork()) {
268 case 0:
269 execvp(av[0], (char *const *)(void *)(unsigned long)(const void *)(av));
270 perror(av[0]);
271 _exit(1);
272 /*NOTREACHED*/
273 break;
275 case -1:
276 perror("fork");
277 break;
279 default:
280 if (wait(&num) == -1)
281 perror("wait");
282 (void) fprintf(stderr, "Exit %x\n", num);
283 break;
287 tok_reset(tok);
290 el_end(el);
291 tok_end(tok);
292 history_end(hist);
294 return (0);