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