Home | History | Annotate | Line # | Download | only in TEST
      1  1.1  christos #include <sys/wait.h>
      2  1.1  christos #include <ctype.h>
      3  1.1  christos #include <dirent.h>
      4  1.8  christos #include <err.h>
      5  1.1  christos #include <limits.h>
      6  1.1  christos #include <locale.h>
      7  1.8  christos #include <signal.h>
      8  1.8  christos #include <stdio.h>
      9  1.8  christos #include <stdlib.h>
     10  1.8  christos #include <string.h>
     11  1.8  christos #include <unistd.h>
     12  1.1  christos 
     13  1.1  christos #include "../histedit.h"
     14  1.1  christos 
     15  1.1  christos 
     16  1.1  christos static int continuation;
     17  1.1  christos volatile sig_atomic_t gotsig;
     18  1.3  christos static const char hfile[] = ".whistory";
     19  1.1  christos 
     20  1.1  christos static wchar_t *
     21  1.1  christos prompt(EditLine *el)
     22  1.1  christos {
     23  1.2  christos 	static wchar_t a[] = L"\1\033[7m\1Edit$\1\033[0m\1 ";
     24  1.1  christos 	static wchar_t b[] = L"Edit> ";
     25  1.1  christos 
     26  1.1  christos 	return continuation ? b : a;
     27  1.1  christos }
     28  1.1  christos 
     29  1.1  christos 
     30  1.1  christos static void
     31  1.1  christos sig(int i)
     32  1.1  christos {
     33  1.1  christos 	gotsig = i;
     34  1.1  christos }
     35  1.1  christos 
     36  1.1  christos const char *
     37  1.1  christos my_wcstombs(const wchar_t *wstr)
     38  1.1  christos {
     39  1.1  christos 	static struct {
     40  1.1  christos 		char *str;
     41  1.1  christos 		int len;
     42  1.1  christos 	} buf;
     43  1.1  christos 
     44  1.1  christos 	int needed = wcstombs(0, wstr, 0) + 1;
     45  1.1  christos 	if (needed > buf.len) {
     46  1.1  christos 		buf.str = malloc(needed);
     47  1.1  christos 		buf.len = needed;
     48  1.1  christos 	}
     49  1.1  christos 	wcstombs(buf.str, wstr, needed);
     50  1.1  christos 	buf.str[needed - 1] = 0;
     51  1.1  christos 
     52  1.1  christos 	return buf.str;
     53  1.1  christos }
     54  1.1  christos 
     55  1.1  christos 
     56  1.1  christos static unsigned char
     57  1.1  christos complete(EditLine *el, int ch)
     58  1.1  christos {
     59  1.1  christos 	DIR *dd = opendir(".");
     60  1.1  christos 	struct dirent *dp;
     61  1.1  christos 	const wchar_t *ptr;
     62  1.1  christos 	char *buf, *bptr;
     63  1.1  christos 	const LineInfoW *lf = el_wline(el);
     64  1.1  christos 	int len, mblen, i;
     65  1.6  christos 	unsigned char res = 0;
     66  1.4  christos 	wchar_t dir[1024];
     67  1.1  christos 
     68  1.1  christos 	/* Find the last word */
     69  1.1  christos 	for (ptr = lf->cursor -1; !iswspace(*ptr) && ptr > lf->buffer; --ptr)
     70  1.1  christos 		continue;
     71  1.1  christos 	len = lf->cursor - ++ptr;
     72  1.1  christos 
     73  1.1  christos 	/* Convert last word to multibyte encoding, so we can compare to it */
     74  1.1  christos 	wctomb(NULL, 0); /* Reset shift state */
     75  1.1  christos 	mblen = MB_LEN_MAX * len + 1;
     76  1.4  christos 	buf = bptr = malloc(mblen);
     77  1.4  christos 	if (buf == NULL)
     78  1.4  christos 		err(1, "malloc");
     79  1.1  christos 	for (i = 0; i < len; ++i) {
     80  1.1  christos 		/* Note: really should test for -1 return from wctomb */
     81  1.1  christos 		bptr += wctomb(bptr, ptr[i]);
     82  1.1  christos 	}
     83  1.1  christos 	*bptr = 0; /* Terminate multibyte string */
     84  1.1  christos 	mblen = bptr - buf;
     85  1.1  christos 
     86  1.1  christos 	/* Scan directory for matching name */
     87  1.1  christos 	for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) {
     88  1.1  christos 		if (mblen > strlen(dp->d_name))
     89  1.1  christos 			continue;
     90  1.1  christos 		if (strncmp(dp->d_name, buf, mblen) == 0) {
     91  1.4  christos 			mbstowcs(dir, &dp->d_name[mblen],
     92  1.4  christos 			    sizeof(dir) / sizeof(*dir));
     93  1.4  christos 			if (el_winsertstr(el, dir) == -1)
     94  1.1  christos 				res = CC_ERROR;
     95  1.1  christos 			else
     96  1.1  christos 				res = CC_REFRESH;
     97  1.1  christos 			break;
     98  1.1  christos 		}
     99  1.1  christos 	}
    100  1.1  christos 
    101  1.1  christos 	closedir(dd);
    102  1.1  christos 	free(buf);
    103  1.1  christos 	return res;
    104  1.1  christos }
    105  1.1  christos 
    106  1.1  christos 
    107  1.1  christos int
    108  1.1  christos main(int argc, char *argv[])
    109  1.1  christos {
    110  1.1  christos 	EditLine *el = NULL;
    111  1.1  christos 	int numc, ncontinuation;
    112  1.1  christos 	const wchar_t *line;
    113  1.1  christos 	TokenizerW *tok;
    114  1.1  christos 	HistoryW *hist;
    115  1.1  christos 	HistEventW ev;
    116  1.1  christos #ifdef DEBUG
    117  1.1  christos 	int i;
    118  1.1  christos #endif
    119  1.1  christos 
    120  1.1  christos 	setlocale(LC_ALL, "");
    121  1.1  christos 
    122  1.1  christos 	(void)signal(SIGINT,  sig);
    123  1.1  christos 	(void)signal(SIGQUIT, sig);
    124  1.1  christos 	(void)signal(SIGHUP,  sig);
    125  1.1  christos 	(void)signal(SIGTERM, sig);
    126  1.1  christos 
    127  1.1  christos 	hist = history_winit();		/* Init built-in history     */
    128  1.1  christos 	history_w(hist, &ev, H_SETSIZE, 100);	/* Remember 100 events	     */
    129  1.3  christos 	history_w(hist, &ev, H_LOAD, hfile);
    130  1.1  christos 
    131  1.1  christos 	tok = tok_winit(NULL);			/* Init the tokenizer	     */
    132  1.1  christos 
    133  1.1  christos 	el = el_init(argv[0], stdin, stdout, stderr);
    134  1.1  christos 
    135  1.1  christos 	el_wset(el, EL_EDITOR, L"vi");		/* Default editor is vi	     */
    136  1.1  christos 	el_wset(el, EL_SIGNAL, 1);		/* Handle signals gracefully */
    137  1.1  christos 	el_wset(el, EL_PROMPT_ESC, prompt, '\1'); /* Set the prompt function */
    138  1.1  christos 
    139  1.1  christos 	el_wset(el, EL_HIST, history_w, hist);	/* FIXME - history_w? */
    140  1.1  christos 
    141  1.1  christos 					/* Add a user-defined function	*/
    142  1.1  christos 	el_wset(el, EL_ADDFN, L"ed-complete", L"Complete argument", complete);
    143  1.1  christos 
    144  1.1  christos 					/* Bind <tab> to it */
    145  1.1  christos 	el_wset(el, EL_BIND, L"^I", L"ed-complete", NULL);
    146  1.1  christos 
    147  1.1  christos 	/*
    148  1.1  christos 	* Bind j, k in vi command mode to previous and next line, instead
    149  1.1  christos 	* of previous and next history.
    150  1.1  christos 	*/
    151  1.1  christos 	el_wset(el, EL_BIND, L"-a", L"k", L"ed-prev-line", NULL);
    152  1.1  christos 	el_wset(el, EL_BIND, L"-a", L"j", L"ed-next-line", NULL);
    153  1.1  christos 
    154  1.1  christos 	/* Source the user's defaults file. */
    155  1.1  christos 	el_source(el, NULL);
    156  1.1  christos 
    157  1.1  christos 	while((line = el_wgets(el, &numc)) != NULL && numc != 0) {
    158  1.1  christos 		int ac, cc, co, rc;
    159  1.1  christos 		const wchar_t **av;
    160  1.1  christos 
    161  1.1  christos 		const LineInfoW *li;
    162  1.1  christos 		li = el_wline(el);
    163  1.1  christos 
    164  1.1  christos #ifdef DEBUG
    165  1.1  christos 		(void)fwprintf(stderr, L"==> got %d %ls", numc, line);
    166  1.1  christos 		(void)fwprintf(stderr, L"  > li `%.*ls_%.*ls'\n",
    167  1.1  christos 		    (li->cursor - li->buffer), li->buffer,
    168  1.1  christos 		    (li->lastchar - 1 - li->cursor),
    169  1.1  christos 		    (li->cursor >= li->lastchar) ? L"" : li->cursor);
    170  1.1  christos #endif
    171  1.1  christos 
    172  1.1  christos 		if (gotsig) {
    173  1.7  christos 			(void)fprintf(stderr, "Got signal %d.\n", (int)gotsig);
    174  1.1  christos 			gotsig = 0;
    175  1.1  christos 			el_reset(el);
    176  1.1  christos 		}
    177  1.1  christos 
    178  1.1  christos 		if(!continuation && numc == 1)
    179  1.1  christos 			continue;	/* Only got a linefeed */
    180  1.1  christos 
    181  1.1  christos 		ac = cc = co = 0;
    182  1.1  christos 		ncontinuation = tok_wline(tok, li, &ac, &av, &cc, &co);
    183  1.1  christos 		if (ncontinuation < 0) {
    184  1.1  christos 			(void) fprintf(stderr, "Internal error\n");
    185  1.1  christos 			continuation = 0;
    186  1.1  christos 			continue;
    187  1.1  christos 		}
    188  1.1  christos 
    189  1.1  christos #ifdef DEBUG
    190  1.1  christos 		(void)fprintf(stderr, "  > nc %d ac %d cc %d co %d\n",
    191  1.1  christos 			ncontinuation, ac, cc, co);
    192  1.1  christos #endif
    193  1.1  christos 		history_w(hist, &ev, continuation ? H_APPEND : H_ENTER, line);
    194  1.1  christos 
    195  1.1  christos 		continuation = ncontinuation;
    196  1.1  christos 		ncontinuation = 0;
    197  1.1  christos 		if(continuation)
    198  1.1  christos 			continue;
    199  1.1  christos 
    200  1.1  christos #ifdef DEBUG
    201  1.1  christos 		for (i = 0; i < ac; ++i) {
    202  1.1  christos 			(void)fwprintf(stderr, L"  > arg# %2d ", i);
    203  1.1  christos 			if (i != cc)
    204  1.1  christos 				(void)fwprintf(stderr, L"`%ls'\n", av[i]);
    205  1.1  christos 			else
    206  1.1  christos 				(void)fwprintf(stderr, L"`%.*ls_%ls'\n",
    207  1.1  christos 				    co, av[i], av[i] + co);
    208  1.1  christos 		}
    209  1.1  christos #endif
    210  1.1  christos 
    211  1.1  christos 		if (wcscmp (av[0], L"history") == 0) {
    212  1.1  christos 			switch(ac) {
    213  1.1  christos 			case 1:
    214  1.1  christos 				for(rc = history_w(hist, &ev, H_LAST);
    215  1.1  christos 				     rc != -1;
    216  1.1  christos 				     rc = history_w(hist, &ev, H_PREV))
    217  1.1  christos 					(void)fwprintf(stdout, L"%4d %ls",
    218  1.1  christos 					     ev.num, ev.str);
    219  1.1  christos 				break;
    220  1.1  christos 			case 2:
    221  1.1  christos 				if (wcscmp(av[1], L"clear") == 0)
    222  1.1  christos 					history_w(hist, &ev, H_CLEAR);
    223  1.1  christos 				else
    224  1.1  christos 					goto badhist;
    225  1.1  christos 				break;
    226  1.1  christos 			case 3:
    227  1.1  christos 				if (wcscmp(av[1], L"load") == 0)
    228  1.1  christos 					history_w(hist, &ev, H_LOAD,
    229  1.1  christos 					    my_wcstombs(av[2]));
    230  1.1  christos 				else if (wcscmp(av[1], L"save") == 0)
    231  1.1  christos 					history_w(hist, &ev, H_SAVE,
    232  1.1  christos 					    my_wcstombs(av[2]));
    233  1.1  christos 				else
    234  1.1  christos 					goto badhist;
    235  1.1  christos 				break;
    236  1.1  christos 			badhist:
    237  1.1  christos 			default:
    238  1.1  christos 				(void)fprintf(stderr,
    239  1.1  christos 				    "Bad history arguments\n");
    240  1.1  christos 				break;
    241  1.1  christos 			}
    242  1.1  christos 		} else if (el_wparse(el, ac, av) == -1) {
    243  1.1  christos 			switch (fork()) {
    244  1.1  christos 			case 0: {
    245  1.1  christos 				Tokenizer *ntok = tok_init(NULL);
    246  1.1  christos 				int nargc;
    247  1.1  christos 				const char **nav;
    248  1.1  christos 				tok_str(ntok, my_wcstombs(line), &nargc, &nav);
    249  1.1  christos 				execvp(nav[0],(char **)nav);
    250  1.1  christos 				perror(nav[0]);
    251  1.1  christos 				_exit(1);
    252  1.1  christos 				/* NOTREACHED */
    253  1.1  christos 				break;
    254  1.1  christos 			}
    255  1.1  christos 			case -1:
    256  1.1  christos 				perror("fork");
    257  1.1  christos 				break;
    258  1.1  christos 			default:
    259  1.1  christos 				if (wait(&rc) == -1)
    260  1.1  christos 					perror("wait");
    261  1.1  christos 				(void)fprintf(stderr, "Exit %x\n", rc);
    262  1.1  christos 				break;
    263  1.1  christos 			}
    264  1.1  christos 		}
    265  1.1  christos 
    266  1.1  christos 		tok_wreset(tok);
    267  1.1  christos 	}
    268  1.1  christos 
    269  1.1  christos 	el_end(el);
    270  1.1  christos 	tok_wend(tok);
    271  1.3  christos 	history_w(hist, &ev, H_SAVE, hfile);
    272  1.1  christos 	history_wend(hist);
    273  1.1  christos 
    274  1.1  christos 	fprintf(stdout, "\n");
    275  1.1  christos 	return 0;
    276  1.1  christos }
    277  1.1  christos 
    278  1.1  christos 
    279