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