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