tc1.c revision 276881
1/*	$NetBSD: tc1.c,v 1.6 2014/06/18 20:12:15 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
43static char sccsid[] = "@(#)test.c	8.1 (Berkeley) 6/4/93";
44#else
45__RCSID("$NetBSD: tc1.c,v 1.6 2014/06/18 20:12:15 christos Exp $");
46#endif
47#endif /* not lint && not SCCSID */
48__FBSDID("$FreeBSD: head/lib/libedit/TEST/tc1.c 276881 2015-01-09 07:40:56Z bapt $");
49
50/*
51 * test.c: A little test program
52 */
53#include <stdio.h>
54#include <string.h>
55#include <signal.h>
56#include <sys/wait.h>
57#include <ctype.h>
58#include <stdlib.h>
59#include <unistd.h>
60#include <dirent.h>
61#include <locale.h>
62
63#include "histedit.h"
64
65static int continuation = 0;
66volatile sig_atomic_t gotsig = 0;
67
68static	unsigned char	complete(EditLine *, int);
69	int	main(int, char **);
70static	char   *prompt(EditLine *);
71static	void	sig(int);
72
73static char *
74prompt(EditLine *el)
75{
76	static char a[] = "\1\033[7m\1Edit$\1\033[0m\1 ";
77	static char b[] = "Edit> ";
78
79	return (continuation ? b : a);
80}
81
82static void
83sig(int i)
84{
85	gotsig = i;
86}
87
88static unsigned char
89complete(EditLine *el, int ch)
90{
91	DIR *dd = opendir(".");
92	struct dirent *dp;
93	const char* ptr;
94	const LineInfo *lf = el_line(el);
95	int len;
96	int res = CC_ERROR;
97
98	/*
99	 * Find the last word
100	 */
101	for (ptr = lf->cursor - 1;
102	    !isspace((unsigned char)*ptr) && ptr > lf->buffer; ptr--)
103		continue;
104	len = lf->cursor - ++ptr;
105
106	for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) {
107		if (len > strlen(dp->d_name))
108			continue;
109		if (strncmp(dp->d_name, ptr, len) == 0) {
110			if (el_insertstr(el, &dp->d_name[len]) == -1)
111				res = CC_ERROR;
112			else
113				res = CC_REFRESH;
114			break;
115		}
116	}
117
118	closedir(dd);
119	return res;
120}
121
122int
123main(int argc, char *argv[])
124{
125	EditLine *el = NULL;
126	int num;
127	const char *buf;
128	Tokenizer *tok;
129#if 0
130	int lastevent = 0;
131#endif
132	int ncontinuation;
133	History *hist;
134	HistEvent ev;
135
136	(void) setlocale(LC_CTYPE, "");
137	(void) signal(SIGINT, sig);
138	(void) signal(SIGQUIT, sig);
139	(void) signal(SIGHUP, sig);
140	(void) signal(SIGTERM, sig);
141
142	hist = history_init();		/* Init the builtin history	*/
143					/* Remember 100 events		*/
144	history(hist, &ev, H_SETSIZE, 100);
145
146	tok  = tok_init(NULL);		/* Initialize the tokenizer	*/
147
148					/* Initialize editline		*/
149	el = el_init(*argv, stdin, stdout, stderr);
150
151	el_set(el, EL_EDITOR, "vi");	/* Default editor is vi		*/
152	el_set(el, EL_SIGNAL, 1);	/* Handle signals gracefully	*/
153	el_set(el, EL_PROMPT_ESC, prompt, '\1');/* Set the prompt function */
154
155			/* Tell editline to use this history interface	*/
156	el_set(el, EL_HIST, history, hist);
157
158					/* Add a user-defined function	*/
159	el_set(el, EL_ADDFN, "ed-complete", "Complete argument", complete);
160
161					/* Bind tab to it 		*/
162	el_set(el, EL_BIND, "^I", "ed-complete", NULL);
163
164	/*
165	 * Bind j, k in vi command mode to previous and next line, instead
166	 * of previous and next history.
167	 */
168	el_set(el, EL_BIND, "-a", "k", "ed-prev-line", NULL);
169	el_set(el, EL_BIND, "-a", "j", "ed-next-line", NULL);
170
171	/*
172	 * Source the user's defaults file.
173	 */
174	el_source(el, NULL);
175
176	while ((buf = el_gets(el, &num)) != NULL && num != 0)  {
177		int ac, cc, co;
178#ifdef DEBUG
179		int i;
180#endif
181		const char **av;
182		const LineInfo *li;
183		li = el_line(el);
184#ifdef DEBUG
185		(void) fprintf(stderr, "==> got %d %s", num, buf);
186		(void) fprintf(stderr, "  > li `%.*s_%.*s'\n",
187		    (li->cursor - li->buffer), li->buffer,
188		    (li->lastchar - 1 - li->cursor),
189		    (li->cursor >= li->lastchar) ? "" : li->cursor);
190
191#endif
192		if (gotsig) {
193			(void) fprintf(stderr, "Got signal %d.\n", (int)gotsig);
194			gotsig = 0;
195			el_reset(el);
196		}
197
198		if (!continuation && num == 1)
199			continue;
200
201		ac = cc = co = 0;
202		ncontinuation = tok_line(tok, li, &ac, &av, &cc, &co);
203		if (ncontinuation < 0) {
204			(void) fprintf(stderr, "Internal error\n");
205			continuation = 0;
206			continue;
207		}
208#ifdef DEBUG
209		(void) fprintf(stderr, "  > nc %d ac %d cc %d co %d\n",
210		    ncontinuation, ac, cc, co);
211#endif
212#if 0
213		if (continuation) {
214			/*
215			 * Append to the right event in case the user
216			 * moved around in history.
217			 */
218			if (history(hist, &ev, H_SET, lastevent) == -1)
219				err(1, "%d: %s", lastevent, ev.str);
220			history(hist, &ev, H_ADD , buf);
221		} else {
222			history(hist, &ev, H_ENTER, buf);
223			lastevent = ev.num;
224		}
225#else
226				/* Simpler */
227		history(hist, &ev, continuation ? H_APPEND : H_ENTER, buf);
228#endif
229
230		continuation = ncontinuation;
231		ncontinuation = 0;
232		if (continuation)
233			continue;
234#ifdef DEBUG
235		for (i = 0; i < ac; i++) {
236			(void) fprintf(stderr, "  > arg# %2d ", i);
237			if (i != cc)
238				(void) fprintf(stderr, "`%s'\n", av[i]);
239			else
240				(void) fprintf(stderr, "`%.*s_%s'\n",
241				    co, av[i], av[i] + co);
242		}
243#endif
244
245		if (strcmp(av[0], "history") == 0) {
246			int rv;
247
248			switch (ac) {
249			case 1:
250				for (rv = history(hist, &ev, H_LAST); rv != -1;
251				    rv = history(hist, &ev, H_PREV))
252					(void) fprintf(stdout, "%4d %s",
253					    ev.num, ev.str);
254				break;
255
256			case 2:
257				if (strcmp(av[1], "clear") == 0)
258					 history(hist, &ev, H_CLEAR);
259				else
260					 goto badhist;
261				break;
262
263			case 3:
264				if (strcmp(av[1], "load") == 0)
265					 history(hist, &ev, H_LOAD, av[2]);
266				else if (strcmp(av[1], "save") == 0)
267					 history(hist, &ev, H_SAVE, av[2]);
268				break;
269
270			badhist:
271			default:
272				(void) fprintf(stderr,
273				    "Bad history arguments\n");
274				break;
275			}
276		} else if (el_parse(el, ac, av) == -1) {
277			switch (fork()) {
278			case 0:
279				execvp(av[0], __DECONST(char **, av));
280				perror(av[0]);
281				_exit(1);
282				/*NOTREACHED*/
283				break;
284
285			case -1:
286				perror("fork");
287				break;
288
289			default:
290				if (wait(&num) == -1)
291					perror("wait");
292				(void) fprintf(stderr, "Exit %x\n", num);
293				break;
294			}
295		}
296
297		tok_reset(tok);
298	}
299
300	el_end(el);
301	tok_end(tok);
302	history_end(hist);
303
304	return (0);
305}
306