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