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