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