tc1.c revision 1.4 1 /* $NetBSD: tc1.c,v 1.4 2010/04/15 00:50:46 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Christos Zoulas of Cornell University.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include "config.h"
36 #ifndef lint
37 __COPYRIGHT("@(#) Copyright (c) 1992, 1993\n\
38 The Regents of the University of California. All rights reserved.\n");
39 #endif /* not lint */
40
41 #if !defined(lint) && !defined(SCCSID)
42 #if 0
43 static char sccsid[] = "@(#)test.c 8.1 (Berkeley) 6/4/93";
44 #else
45 __RCSID("$NetBSD: tc1.c,v 1.4 2010/04/15 00:50:46 christos Exp $");
46 #endif
47 #endif /* not lint && not SCCSID */
48
49 /*
50 * test.c: A little test program
51 */
52 #include <stdio.h>
53 #include <string.h>
54 #include <signal.h>
55 #include <sys/wait.h>
56 #include <ctype.h>
57 #include <stdlib.h>
58 #include <unistd.h>
59 #include <dirent.h>
60 #include <locale.h>
61
62 #include "histedit.h"
63
64 static int continuation = 0;
65 volatile sig_atomic_t gotsig = 0;
66
67 static unsigned char complete(EditLine *, int);
68 int main(int, char **);
69 static char *prompt(EditLine *);
70 static void sig(int);
71
72 static char *
73 prompt(EditLine *el)
74 {
75 static char a[] = "\1\e[7m\1Edit$\1\e[0m\1 ";
76 static char b[] = "Edit> ";
77
78 return (continuation ? b : a);
79 }
80
81 static void
82 sig(int i)
83 {
84 gotsig = i;
85 }
86
87 static unsigned char
88 complete(EditLine *el, 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;
100 !isspace((unsigned char)*ptr) && ptr > lf->buffer; ptr--)
101 continue;
102 len = lf->cursor - ++ptr;
103
104 for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) {
105 if (len > strlen(dp->d_name))
106 continue;
107 if (strncmp(dp->d_name, ptr, len) == 0) {
108 closedir(dd);
109 if (el_insertstr(el, &dp->d_name[len]) == -1)
110 return (CC_ERROR);
111 else
112 return (CC_REFRESH);
113 }
114 }
115
116 closedir(dd);
117 return (CC_ERROR);
118 }
119
120 int
121 main(int argc, char *argv[])
122 {
123 EditLine *el = NULL;
124 int num;
125 const char *buf;
126 Tokenizer *tok;
127 #if 0
128 int lastevent = 0;
129 #endif
130 int ncontinuation;
131 History *hist;
132 HistEvent ev;
133
134 (void) setlocale(LC_CTYPE, "");
135 (void) signal(SIGINT, sig);
136 (void) signal(SIGQUIT, sig);
137 (void) signal(SIGHUP, sig);
138 (void) signal(SIGTERM, sig);
139
140 hist = history_init(); /* Init the builtin history */
141 /* Remember 100 events */
142 history(hist, &ev, H_SETSIZE, 100);
143
144 tok = tok_init(NULL); /* Initialize the tokenizer */
145
146 /* Initialize editline */
147 el = el_init(*argv, stdin, stdout, stderr);
148
149 el_set(el, EL_EDITOR, "vi"); /* Default editor is vi */
150 el_set(el, EL_SIGNAL, 1); /* Handle signals gracefully */
151 el_set(el, EL_PROMPT_ESC, prompt, '\1');/* Set the prompt function */
152
153 /* Tell editline to use this history interface */
154 el_set(el, EL_HIST, history, hist);
155
156 /* Add a user-defined function */
157 el_set(el, EL_ADDFN, "ed-complete", "Complete argument", complete);
158
159 /* Bind tab to it */
160 el_set(el, EL_BIND, "^I", "ed-complete", NULL);
161
162 /*
163 * Bind j, k in vi command mode to previous and next line, instead
164 * of previous and next history.
165 */
166 el_set(el, EL_BIND, "-a", "k", "ed-prev-line", NULL);
167 el_set(el, EL_BIND, "-a", "j", "ed-next-line", NULL);
168
169 /*
170 * Source the user's defaults file.
171 */
172 el_source(el, NULL);
173
174 while ((buf = el_gets(el, &num)) != NULL && num != 0) {
175 int ac, cc, co;
176 #ifdef DEBUG
177 int i;
178 #endif
179 const char **av;
180 const LineInfo *li;
181 li = el_line(el);
182 #ifdef DEBUG
183 (void) fprintf(stderr, "==> got %d %s", num, buf);
184 (void) fprintf(stderr, " > li `%.*s_%.*s'\n",
185 (li->cursor - li->buffer), li->buffer,
186 (li->lastchar - 1 - li->cursor),
187 (li->cursor >= li->lastchar) ? "" : li->cursor);
188
189 #endif
190 if (gotsig) {
191 (void) fprintf(stderr, "Got signal %d.\n", gotsig);
192 gotsig = 0;
193 el_reset(el);
194 }
195
196 if (!continuation && num == 1)
197 continue;
198
199 ac = cc = co = 0;
200 ncontinuation = tok_line(tok, li, &ac, &av, &cc, &co);
201 if (ncontinuation < 0) {
202 (void) fprintf(stderr, "Internal error\n");
203 continuation = 0;
204 continue;
205 }
206 #ifdef DEBUG
207 (void) fprintf(stderr, " > nc %d ac %d cc %d co %d\n",
208 ncontinuation, ac, cc, co);
209 #endif
210 #if 0
211 if (continuation) {
212 /*
213 * Append to the right event in case the user
214 * moved around in history.
215 */
216 if (history(hist, &ev, H_SET, lastevent) == -1)
217 err(1, "%d: %s", lastevent, ev.str);
218 history(hist, &ev, H_ADD , buf);
219 } else {
220 history(hist, &ev, H_ENTER, buf);
221 lastevent = ev.num;
222 }
223 #else
224 /* Simpler */
225 history(hist, &ev, continuation ? H_APPEND : H_ENTER, buf);
226 #endif
227
228 continuation = ncontinuation;
229 ncontinuation = 0;
230 if (continuation)
231 continue;
232 #ifdef DEBUG
233 for (i = 0; i < ac; i++) {
234 (void) fprintf(stderr, " > arg# %2d ", i);
235 if (i != cc)
236 (void) fprintf(stderr, "`%s'\n", av[i]);
237 else
238 (void) fprintf(stderr, "`%.*s_%s'\n",
239 co, av[i], av[i] + co);
240 }
241 #endif
242
243 if (strcmp(av[0], "history") == 0) {
244 int rv;
245
246 switch (ac) {
247 case 1:
248 for (rv = history(hist, &ev, H_LAST); rv != -1;
249 rv = history(hist, &ev, H_PREV))
250 (void) fprintf(stdout, "%4d %s",
251 ev.num, ev.str);
252 break;
253
254 case 2:
255 if (strcmp(av[1], "clear") == 0)
256 history(hist, &ev, H_CLEAR);
257 else
258 goto badhist;
259 break;
260
261 case 3:
262 if (strcmp(av[1], "load") == 0)
263 history(hist, &ev, H_LOAD, av[2]);
264 else if (strcmp(av[1], "save") == 0)
265 history(hist, &ev, H_SAVE, av[2]);
266 break;
267
268 badhist:
269 default:
270 (void) fprintf(stderr,
271 "Bad history arguments\n");
272 break;
273 }
274 } else if (el_parse(el, ac, av) == -1) {
275 switch (fork()) {
276 case 0:
277 execvp(av[0], (char *const *)__UNCONST(av));
278 perror(av[0]);
279 _exit(1);
280 /*NOTREACHED*/
281 break;
282
283 case -1:
284 perror("fork");
285 break;
286
287 default:
288 if (wait(&num) == -1)
289 perror("wait");
290 (void) fprintf(stderr, "Exit %x\n", num);
291 break;
292 }
293 }
294
295 tok_reset(tok);
296 }
297
298 el_end(el);
299 tok_end(tok);
300 history_end(hist);
301
302 return (0);
303 }
304