1 /*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Christos Zoulas of Cornell University.
7 *
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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #ifndef lint
38 static char copyright[] =
39 "@(#) Copyright (c) 1992, 1993\n\
40 The Regents of the University of California. All rights reserved.\n";
41 #endif /* not lint */
42
43 #if !defined(lint) && !defined(SCCSID)
44 static char sccsid[] = "@(#)test.c 8.1 (Berkeley) 6/4/93";
45 #endif /* not lint && not SCCSID */
46
47 /*
48 * test.c: A little test program
49 */
50 #include "sys.h"
51 #include <stdio.h>
52 #include <string.h>
53 #include <signal.h>
54 #include <sys/wait.h>
55 #include <ctype.h>
56 #include <stdlib.h>
57 #include <unistd.h>
58 #include <dirent.h>
59
60 #include "histedit.h"
61 #include "tokenizer.h"
62
63 static int continuation = 0;
64 static EditLine *el = NULL;
65
66 static char *
67 /*ARGSUSED*/
prompt(el)68 prompt(el)
69 EditLine *el;
70 {
71 static char a[] = "Edit$";
72 static char b[] = "Edit>";
73 return continuation ? b : a;
74 }
75
76 static void
sig(i)77 sig(i)
78 int i;
79 {
80 (void) fprintf(stderr, "Got signal %d.\n", i);
81 el_reset(el);
82 }
83
84 static unsigned char
85 /*ARGSUSED*/
complete(el,ch)86 complete(el, ch)
87 EditLine *el;
88 int ch;
89 {
90 DIR *dd = opendir(".");
91 struct dirent *dp;
92 const char* ptr;
93 const LineInfo *lf = el_line(el);
94 int len;
95
96 /*
97 * Find the last word
98 */
99 for (ptr = lf->cursor - 1; !isspace(*ptr) && ptr > lf->buffer; ptr--)
100 continue;
101 len = lf->cursor - ++ptr;
102
103 for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) {
104 if (len > strlen(dp->d_name))
105 continue;
106 if (strncmp(dp->d_name, ptr, len) == 0) {
107 closedir(dd);
108 if (el_insertstr(el, &dp->d_name[len]) == -1)
109 return CC_ERROR;
110 else
111 return CC_REFRESH;
112 }
113 }
114
115 closedir(dd);
116 return CC_ERROR;
117 }
118
119 int
120 /*ARGSUSED*/
main(argc,argv)121 main(argc, argv)
122 int argc;
123 char *argv[];
124 {
125 int num;
126 const char *buf;
127 Tokenizer *tok;
128 History *hist;
129
130 (void) signal(SIGINT, sig);
131 (void) signal(SIGQUIT, sig);
132 (void) signal(SIGHUP, sig);
133 (void) signal(SIGTERM, sig);
134
135 hist = history_init(); /* Init the builtin history */
136 history(hist, H_EVENT, 100); /* Remember 100 events */
137
138 tok = tok_init(NULL); /* Initialize the tokenizer */
139
140 el = el_init(*argv, stdin, stdout); /* Initialize editline */
141
142 el_set(el, EL_EDITOR, "vi"); /* Default editor is vi */
143 el_set(el, EL_SIGNAL, 1); /* Handle signals gracefully */
144 el_set(el, EL_PROMPT, prompt); /* Set the prompt function */
145
146 /* Tell editline to use this history interface */
147 el_set(el, EL_HIST, history, hist);
148
149 /* Add a user-defined function */
150 el_set(el, EL_ADDFN, "ed-complete", "Complete argument", complete);
151
152 el_set(el, EL_BIND, "^I", "ed-complete", NULL);/* Bind tab to it */
153
154 /*
155 * Bind j, k in vi command mode to previous and next line, instead
156 * of previous and next history.
157 */
158 el_set(el, EL_BIND, "-a", "k", "ed-prev-line", NULL);
159 el_set(el, EL_BIND, "-a", "j", "ed-next-line", NULL);
160
161 /*
162 * Source the user's defaults file.
163 */
164 el_source(el, NULL);
165
166 while ((buf = el_gets(el, &num)) != NULL && num != 0) {
167 int ac;
168 char **av;
169 #ifdef DEBUG
170 (void) fprintf(stderr, "got %d %s", num, buf);
171 #endif
172 if (!continuation && num == 1)
173 continue;
174 if (tok_line(tok, buf, &ac, &av) > 0) {
175 history(hist, continuation ? H_ADD : H_ENTER, buf);
176 continuation = 1;
177 continue;
178 }
179 history(hist, continuation ? H_ADD : H_ENTER, buf);
180
181 continuation = 0;
182
183 if (strcmp(av[0], "history") == 0) {
184 const struct HistEvent *he;
185
186 switch (ac) {
187 case 1:
188 for (he = history(hist, H_LAST); he;
189 he = history(hist, H_PREV))
190 (void) fprintf(stdout, "%4d %s", he->num, he->str);
191 break;
192
193 case 2:
194 if (strcmp(av[1], "clear") == 0)
195 history(hist, H_CLEAR);
196 else
197 goto badhist;
198 break;
199
200 case 3:
201 if (strcmp(av[1], "load") == 0)
202 history(hist, H_LOAD, av[2]);
203 else if (strcmp(av[1], "save") == 0)
204 history(hist, H_SAVE, av[2]);
205 break;
206
207 badhist:
208 default:
209 (void) fprintf(stderr, "Bad history arguments\n");
210 break;
211 }
212 }
213 else if (el_parse(el, ac, av) == -1) {
214 switch (fork()) {
215 case 0:
216 execvp(av[0], av);
217 perror(av[0]);
218 _exit(1);
219 /*NOTREACHED*/
220 break;
221
222 case -1:
223 perror("fork");
224 break;
225
226 default:
227 if (wait(&num) == -1)
228 perror("wait");
229 (void) fprintf(stderr, "Exit %x\n", num);
230 break;
231 }
232 }
233 tok_reset(tok);
234 }
235
236 el_end(el);
237 tok_end(tok);
238 history_end(hist);
239
240 return 0;
241 }
242