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