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