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