xref: /original-bsd/lib/libedit/TEST/test.c (revision c3e32dec)
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  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 static char copyright[] =
13 "@(#) Copyright (c) 1992, 1993\n\
14 	The Regents of the University of California.  All rights reserved.\n";
15 #endif /* not lint */
16 
17 #if !defined(lint) && !defined(SCCSID)
18 static char sccsid[] = "@(#)test.c	8.1 (Berkeley) 06/04/93";
19 #endif /* not lint && not SCCSID */
20 
21 /*
22  * test.c: A little test program
23  */
24 #include "sys.h"
25 #include <stdio.h>
26 #include <string.h>
27 #include <signal.h>
28 #include <sys/wait.h>
29 #include <ctype.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <dirent.h>
33 
34 #include "histedit.h"
35 #include "tokenizer.h"
36 
37 static int continuation = 0;
38 static EditLine *el = NULL;
39 
40 static char *
41 /*ARGSUSED*/
42 prompt(el)
43     EditLine *el;
44 {
45     static char a[] = "Edit$";
46     static char b[] = "Edit>";
47     return continuation ? b : a;
48 }
49 
50 static void
51 sig(i)
52     int i;
53 {
54     (void) fprintf(stderr, "Got signal %d.\n", i);
55     el_reset(el);
56 }
57 
58 static unsigned char
59 /*ARGSUSED*/
60 complete(el, ch)
61     EditLine *el;
62     int ch;
63 {
64     DIR *dd = opendir(".");
65     struct dirent *dp;
66     const char* ptr;
67     const LineInfo *lf = el_line(el);
68     int len;
69 
70     /*
71      * Find the last word
72      */
73     for (ptr = lf->cursor - 1; !isspace(*ptr) && ptr > lf->buffer; ptr--)
74 	continue;
75     len = lf->cursor - ++ptr;
76 
77     for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) {
78 	if (len > strlen(dp->d_name))
79 	    continue;
80 	if (strncmp(dp->d_name, ptr, len) == 0) {
81 	    closedir(dd);
82 	    if (el_insertstr(el, &dp->d_name[len]) == -1)
83 		return CC_ERROR;
84 	    else
85 		return CC_REFRESH;
86 	}
87     }
88 
89     closedir(dd);
90     return CC_ERROR;
91 }
92 
93 int
94 /*ARGSUSED*/
95 main(argc, argv)
96     int argc;
97     char *argv[];
98 {
99     int num;
100     const char *buf;
101     Tokenizer *tok;
102     History *hist;
103 
104     (void) signal(SIGINT, sig);
105     (void) signal(SIGQUIT, sig);
106     (void) signal(SIGHUP, sig);
107     (void) signal(SIGTERM, sig);
108 
109     hist = history_init();		/* Init the builtin history	*/
110     history(hist, H_EVENT, 100);	/* Remember 100 events		*/
111 
112     tok  = tok_init(NULL);		/* Initialize the tokenizer	*/
113 
114     el = el_init(*argv, stdin, stdout);	/* Initialize editline		*/
115 
116     el_set(el, EL_EDITOR, "vi");	/* Default editor is vi 	*/
117     el_set(el, EL_SIGNAL, 1);		/* Handle signals gracefully	*/
118     el_set(el, EL_PROMPT, prompt);	/* Set the prompt function	*/
119 
120     /* Tell editline to use this history interface			*/
121     el_set(el, EL_HIST, history, hist);
122 
123     /* Add a user-defined function 					*/
124     el_set(el, EL_ADDFN, "ed-complete", "Complete argument", complete);
125 
126     el_set(el, EL_BIND, "^I", "ed-complete", NULL);/* Bind tab to it 	*/
127 
128     /*
129      * Bind j, k in vi command mode to previous and next line, instead
130      * of previous and next history.
131      */
132     el_set(el, EL_BIND, "-a", "k", "ed-prev-line", NULL);
133     el_set(el, EL_BIND, "-a", "j", "ed-next-line", NULL);
134 
135     /*
136      * Source the user's defaults file.
137      */
138     el_source(el, NULL);
139 
140     while ((buf = el_gets(el, &num)) != NULL && num != 0)  {
141 	int ac;
142 	char **av;
143 #ifdef DEBUG
144 	(void) fprintf(stderr, "got %d %s", num, buf);
145 #endif
146 	if (!continuation && num == 1)
147 	    continue;
148 	if (tok_line(tok, buf, &ac, &av) > 0) {
149 	    history(hist, continuation ? H_ADD : H_ENTER, buf);
150 	    continuation = 1;
151 	    continue;
152 	}
153 	history(hist, continuation ? H_ADD : H_ENTER, buf);
154 
155 	continuation = 0;
156 	if (el_parse(el, ac, av) != -1) {
157 	    tok_reset(tok);
158 	    continue;
159 	}
160 
161 	switch (fork()) {
162 	case 0:
163 	    execvp(av[0], av);
164 	    perror(av[0]);
165 	    _exit(1);
166 	    /*NOTREACHED*/
167 	    break;
168 
169 	case -1:
170 	    perror("fork");
171 	    break;
172 
173 	default:
174 	    if (wait(&num) == -1)
175 		perror("wait");
176 	    (void) fprintf(stderr, "Exit %x\n", num);
177 	    break;
178 	}
179 	tok_reset(tok);
180     }
181 
182     el_end(el);
183     tok_end(tok);
184     history_end(hist);
185 
186     return 0;
187 }
188