tc1.c revision 1573
1311128Sdim/*-
2311128Sdim * Copyright (c) 1992, 1993
3311128Sdim *	The Regents of the University of California.  All rights reserved.
4311128Sdim *
5311128Sdim * This code is derived from software contributed to Berkeley by
6311128Sdim * Christos Zoulas of Cornell University.
7311128Sdim *
8311128Sdim * Redistribution and use in source and binary forms, with or without
9311128Sdim * modification, are permitted provided that the following conditions
10311128Sdim * are met:
11311128Sdim * 1. Redistributions of source code must retain the above copyright
12311128Sdim *    notice, this list of conditions and the following disclaimer.
13311128Sdim * 2. Redistributions in binary form must reproduce the above copyright
14311128Sdim *    notice, this list of conditions and the following disclaimer in the
15311128Sdim *    documentation and/or other materials provided with the distribution.
16311128Sdim * 3. All advertising materials mentioning features or use of this software
17311128Sdim *    must display the following acknowledgement:
18311128Sdim *	This product includes software developed by the University of
19311128Sdim *	California, Berkeley and its contributors.
20311128Sdim * 4. Neither the name of the University nor the names of its contributors
21311128Sdim *    may be used to endorse or promote products derived from this software
22311128Sdim *    without specific prior written permission.
23321369Sdim *
24321369Sdim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25321369Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26311128Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27311128Sdim * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28311128Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29311128Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30311128Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31311128Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32311128Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33311128Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34311128Sdim * SUCH DAMAGE.
35311128Sdim */
36311128Sdim
37311128Sdim#ifndef lint
38311128Sdimstatic char copyright[] =
39311128Sdim"@(#) Copyright (c) 1992, 1993\n\
40311128Sdim	The Regents of the University of California.  All rights reserved.\n";
41311128Sdim#endif /* not lint */
42311128Sdim
43311128Sdim#if !defined(lint) && !defined(SCCSID)
44311128Sdimstatic char sccsid[] = "@(#)test.c	8.1 (Berkeley) 6/4/93";
45311128Sdim#endif /* not lint && not SCCSID */
46311128Sdim
47311128Sdim/*
48311128Sdim * test.c: A little test program
49311128Sdim */
50311128Sdim#include "sys.h"
51311128Sdim#include <stdio.h>
52311128Sdim#include <string.h>
53311128Sdim#include <signal.h>
54311128Sdim#include <sys/wait.h>
55311128Sdim#include <ctype.h>
56311128Sdim#include <stdlib.h>
57311128Sdim#include <unistd.h>
58311128Sdim#include <dirent.h>
59311128Sdim
60311128Sdim#include "histedit.h"
61311128Sdim#include "tokenizer.h"
62311128Sdim
63311128Sdimstatic int continuation = 0;
64311128Sdimstatic EditLine *el = NULL;
65311128Sdim
66311128Sdimstatic char *
67311128Sdim/*ARGSUSED*/
68311128Sdimprompt(el)
69311128Sdim    EditLine *el;
70311128Sdim{
71311128Sdim    static char a[] = "Edit$";
72311128Sdim    static char b[] = "Edit>";
73311128Sdim    return continuation ? b : a;
74311128Sdim}
75311128Sdim
76311128Sdimstatic void
77311128Sdimsig(i)
78311128Sdim    int i;
79311128Sdim{
80311128Sdim    (void) fprintf(stderr, "Got signal %d.\n", i);
81311128Sdim    el_reset(el);
82311128Sdim}
83311128Sdim
84311128Sdimstatic unsigned char
85311128Sdim/*ARGSUSED*/
86311128Sdimcomplete(el, ch)
87311128Sdim    EditLine *el;
88311128Sdim    int ch;
89311128Sdim{
90311128Sdim    DIR *dd = opendir(".");
91311128Sdim    struct dirent *dp;
92311128Sdim    const char* ptr;
93311128Sdim    const LineInfo *lf = el_line(el);
94311128Sdim    int len;
95311128Sdim
96311128Sdim    /*
97311128Sdim     * Find the last word
98311128Sdim     */
99311128Sdim    for (ptr = lf->cursor - 1; !isspace(*ptr) && ptr > lf->buffer; ptr--)
100311128Sdim	continue;
101311128Sdim    len = lf->cursor - ++ptr;
102311128Sdim
103311128Sdim    for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) {
104311128Sdim	if (len > strlen(dp->d_name))
105311128Sdim	    continue;
106311128Sdim	if (strncmp(dp->d_name, ptr, len) == 0) {
107311128Sdim	    closedir(dd);
108311128Sdim	    if (el_insertstr(el, &dp->d_name[len]) == -1)
109311128Sdim		return CC_ERROR;
110311128Sdim	    else
111311128Sdim		return CC_REFRESH;
112311128Sdim	}
113311128Sdim    }
114311128Sdim
115311128Sdim    closedir(dd);
116311128Sdim    return CC_ERROR;
117311128Sdim}
118311128Sdim
119311128Sdimint
120311128Sdim/*ARGSUSED*/
121311128Sdimmain(argc, argv)
122311128Sdim    int argc;
123311128Sdim    char *argv[];
124311128Sdim{
125311128Sdim    int num;
126311128Sdim    const char *buf;
127311128Sdim    Tokenizer *tok;
128311128Sdim    History *hist;
129311128Sdim
130311128Sdim    (void) signal(SIGINT, sig);
131311128Sdim    (void) signal(SIGQUIT, sig);
132311128Sdim    (void) signal(SIGHUP, sig);
133311128Sdim    (void) signal(SIGTERM, sig);
134311128Sdim
135311128Sdim    hist = history_init();		/* Init the builtin history	*/
136311128Sdim    history(hist, H_EVENT, 100);	/* Remember 100 events		*/
137311128Sdim
138311128Sdim    tok  = tok_init(NULL);		/* Initialize the tokenizer	*/
139311128Sdim
140311128Sdim    el = el_init(*argv, stdin, stdout);	/* Initialize editline		*/
141311128Sdim
142311128Sdim    el_set(el, EL_EDITOR, "vi");	/* Default editor is vi 	*/
143311128Sdim    el_set(el, EL_SIGNAL, 1);		/* Handle signals gracefully	*/
144311128Sdim    el_set(el, EL_PROMPT, prompt);	/* Set the prompt function	*/
145311128Sdim
146311128Sdim    /* Tell editline to use this history interface			*/
147311128Sdim    el_set(el, EL_HIST, history, hist);
148311128Sdim
149311128Sdim    /* Add a user-defined function 					*/
150311128Sdim    el_set(el, EL_ADDFN, "ed-complete", "Complete argument", complete);
151311128Sdim
152311128Sdim    el_set(el, EL_BIND, "^I", "ed-complete", NULL);/* Bind tab to it 	*/
153311128Sdim
154311128Sdim    /*
155311128Sdim     * Bind j, k in vi command mode to previous and next line, instead
156311128Sdim     * of previous and next history.
157311128Sdim     */
158311128Sdim    el_set(el, EL_BIND, "-a", "k", "ed-prev-line", NULL);
159311128Sdim    el_set(el, EL_BIND, "-a", "j", "ed-next-line", NULL);
160311128Sdim
161311128Sdim    /*
162311128Sdim     * Source the user's defaults file.
163311128Sdim     */
164311128Sdim    el_source(el, NULL);
165311128Sdim
166311128Sdim    while ((buf = el_gets(el, &num)) != NULL && num != 0)  {
167311128Sdim	int ac;
168311128Sdim	char **av;
169311128Sdim#ifdef DEBUG
170311128Sdim	(void) fprintf(stderr, "got %d %s", num, buf);
171311128Sdim#endif
172311128Sdim	if (!continuation && num == 1)
173311128Sdim	    continue;
174311128Sdim	if (tok_line(tok, buf, &ac, &av) > 0) {
175311128Sdim	    history(hist, continuation ? H_ADD : H_ENTER, buf);
176311128Sdim	    continuation = 1;
177311128Sdim	    continue;
178311128Sdim	}
179311128Sdim	history(hist, continuation ? H_ADD : H_ENTER, buf);
180311128Sdim
181311128Sdim	continuation = 0;
182311128Sdim	if (el_parse(el, ac, av) != -1) {
183311128Sdim	    tok_reset(tok);
184311128Sdim	    continue;
185311128Sdim	}
186311128Sdim
187311128Sdim	switch (fork()) {
188311128Sdim	case 0:
189311128Sdim	    execvp(av[0], av);
190311128Sdim	    perror(av[0]);
191311128Sdim	    _exit(1);
192311128Sdim	    /*NOTREACHED*/
193311128Sdim	    break;
194311128Sdim
195311128Sdim	case -1:
196311128Sdim	    perror("fork");
197311128Sdim	    break;
198311128Sdim
199311128Sdim	default:
200311128Sdim	    if (wait(&num) == -1)
201311128Sdim		perror("wait");
202311128Sdim	    (void) fprintf(stderr, "Exit %x\n", num);
203311128Sdim	    break;
204311128Sdim	}
205311128Sdim	tok_reset(tok);
206311128Sdim    }
207311128Sdim
208311128Sdim    el_end(el);
209311128Sdim    tok_end(tok);
210311128Sdim    history_end(hist);
211311128Sdim
212311128Sdim    return 0;
213311128Sdim}
214311128Sdim