read.c revision 1.89 1 1.89 christos /* $NetBSD: read.c,v 1.89 2016/04/11 00:22:48 christos Exp $ */
2 1.2 lukem
3 1.1 cgd /*-
4 1.1 cgd * Copyright (c) 1992, 1993
5 1.1 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Christos Zoulas of Cornell University.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.26 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd */
34 1.1 cgd
35 1.21 christos #include "config.h"
36 1.1 cgd #if !defined(lint) && !defined(SCCSID)
37 1.2 lukem #if 0
38 1.1 cgd static char sccsid[] = "@(#)read.c 8.1 (Berkeley) 6/4/93";
39 1.2 lukem #else
40 1.89 christos __RCSID("$NetBSD: read.c,v 1.89 2016/04/11 00:22:48 christos Exp $");
41 1.2 lukem #endif
42 1.2 lukem #endif /* not lint && not SCCSID */
43 1.1 cgd
44 1.1 cgd /*
45 1.1 cgd * read.c: Clean this junk up! This is horrible code.
46 1.1 cgd * Terminal read functions
47 1.1 cgd */
48 1.81 christos #include <ctype.h>
49 1.11 kleink #include <errno.h>
50 1.27 mycroft #include <fcntl.h>
51 1.82 christos #include <limits.h>
52 1.1 cgd #include <stdlib.h>
53 1.81 christos #include <string.h>
54 1.82 christos #include <unistd.h>
55 1.81 christos
56 1.1 cgd #include "el.h"
57 1.1 cgd
58 1.66 christos #define OKCMD -1 /* must be -1! */
59 1.1 cgd
60 1.17 lukem private int read__fixio(int, int);
61 1.84 christos private int read_char(EditLine *, wchar_t *);
62 1.53 christos private int read_getcmd(EditLine *, el_action_t *, Char *);
63 1.40 christos private void read_pop(c_macro_t *);
64 1.20 christos
65 1.20 christos /* read_init():
66 1.20 christos * Initialize the read stuff
67 1.20 christos */
68 1.20 christos protected int
69 1.20 christos read_init(EditLine *el)
70 1.20 christos {
71 1.20 christos /* builtin read_char */
72 1.20 christos el->el_read.read_char = read_char;
73 1.20 christos return 0;
74 1.20 christos }
75 1.20 christos
76 1.20 christos
77 1.20 christos /* el_read_setfn():
78 1.20 christos * Set the read char function to the one provided.
79 1.20 christos * If it is set to EL_BUILTIN_GETCFN, then reset to the builtin one.
80 1.20 christos */
81 1.20 christos protected int
82 1.20 christos el_read_setfn(EditLine *el, el_rfunc_t rc)
83 1.20 christos {
84 1.20 christos el->el_read.read_char = (rc == EL_BUILTIN_GETCFN) ? read_char : rc;
85 1.20 christos return 0;
86 1.20 christos }
87 1.20 christos
88 1.20 christos
89 1.20 christos /* el_read_getfn():
90 1.20 christos * return the current read char function, or EL_BUILTIN_GETCFN
91 1.20 christos * if it is the default one
92 1.20 christos */
93 1.20 christos protected el_rfunc_t
94 1.20 christos el_read_getfn(EditLine *el)
95 1.20 christos {
96 1.65 christos return el->el_read.read_char == read_char ?
97 1.20 christos EL_BUILTIN_GETCFN : el->el_read.read_char;
98 1.20 christos }
99 1.20 christos
100 1.1 cgd
101 1.25 christos #ifndef MIN
102 1.25 christos #define MIN(A,B) ((A) < (B) ? (A) : (B))
103 1.25 christos #endif
104 1.25 christos
105 1.1 cgd #ifdef DEBUG_EDIT
106 1.1 cgd private void
107 1.17 lukem read_debug(EditLine *el)
108 1.1 cgd {
109 1.1 cgd
110 1.17 lukem if (el->el_line.cursor > el->el_line.lastchar)
111 1.17 lukem (void) fprintf(el->el_errfile, "cursor > lastchar\r\n");
112 1.17 lukem if (el->el_line.cursor < el->el_line.buffer)
113 1.17 lukem (void) fprintf(el->el_errfile, "cursor < buffer\r\n");
114 1.17 lukem if (el->el_line.cursor > el->el_line.limit)
115 1.17 lukem (void) fprintf(el->el_errfile, "cursor > limit\r\n");
116 1.17 lukem if (el->el_line.lastchar > el->el_line.limit)
117 1.17 lukem (void) fprintf(el->el_errfile, "lastchar > limit\r\n");
118 1.17 lukem if (el->el_line.limit != &el->el_line.buffer[EL_BUFSIZ - 2])
119 1.17 lukem (void) fprintf(el->el_errfile, "limit != &buffer[EL_BUFSIZ-2]\r\n");
120 1.1 cgd }
121 1.1 cgd #endif /* DEBUG_EDIT */
122 1.1 cgd
123 1.17 lukem
124 1.1 cgd /* read__fixio():
125 1.1 cgd * Try to recover from a read error
126 1.1 cgd */
127 1.10 christos /* ARGSUSED */
128 1.1 cgd private int
129 1.25 christos read__fixio(int fd __attribute__((__unused__)), int e)
130 1.1 cgd {
131 1.17 lukem
132 1.17 lukem switch (e) {
133 1.17 lukem case -1: /* Make sure that the code is reachable */
134 1.1 cgd
135 1.1 cgd #ifdef EWOULDBLOCK
136 1.17 lukem case EWOULDBLOCK:
137 1.17 lukem #ifndef TRY_AGAIN
138 1.66 christos #define TRY_AGAIN
139 1.17 lukem #endif
140 1.1 cgd #endif /* EWOULDBLOCK */
141 1.1 cgd
142 1.1 cgd #if defined(POSIX) && defined(EAGAIN)
143 1.17 lukem #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
144 1.17 lukem case EAGAIN:
145 1.17 lukem #ifndef TRY_AGAIN
146 1.66 christos #define TRY_AGAIN
147 1.17 lukem #endif
148 1.17 lukem #endif /* EWOULDBLOCK && EWOULDBLOCK != EAGAIN */
149 1.1 cgd #endif /* POSIX && EAGAIN */
150 1.1 cgd
151 1.17 lukem e = 0;
152 1.1 cgd #ifdef TRY_AGAIN
153 1.17 lukem #if defined(F_SETFL) && defined(O_NDELAY)
154 1.17 lukem if ((e = fcntl(fd, F_GETFL, 0)) == -1)
155 1.65 christos return -1;
156 1.5 christos
157 1.17 lukem if (fcntl(fd, F_SETFL, e & ~O_NDELAY) == -1)
158 1.65 christos return -1;
159 1.5 christos else
160 1.17 lukem e = 1;
161 1.17 lukem #endif /* F_SETFL && O_NDELAY */
162 1.17 lukem
163 1.17 lukem #ifdef FIONBIO
164 1.17 lukem {
165 1.17 lukem int zero = 0;
166 1.17 lukem
167 1.64 christos if (ioctl(fd, FIONBIO, &zero) == -1)
168 1.65 christos return -1;
169 1.17 lukem else
170 1.17 lukem e = 1;
171 1.17 lukem }
172 1.17 lukem #endif /* FIONBIO */
173 1.1 cgd
174 1.1 cgd #endif /* TRY_AGAIN */
175 1.65 christos return e ? 0 : -1;
176 1.1 cgd
177 1.17 lukem case EINTR:
178 1.65 christos return 0;
179 1.1 cgd
180 1.17 lukem default:
181 1.65 christos return -1;
182 1.17 lukem }
183 1.1 cgd }
184 1.1 cgd
185 1.1 cgd
186 1.1 cgd /* el_push():
187 1.1 cgd * Push a macro
188 1.1 cgd */
189 1.1 cgd public void
190 1.89 christos el_wpush(EditLine *el, const Char *str)
191 1.1 cgd {
192 1.17 lukem c_macro_t *ma = &el->el_chared.c_macro;
193 1.1 cgd
194 1.17 lukem if (str != NULL && ma->level + 1 < EL_MAXMACRO) {
195 1.17 lukem ma->level++;
196 1.89 christos if ((ma->macro[ma->level] = wcsdup(str)) != NULL)
197 1.30 christos return;
198 1.30 christos ma->level--;
199 1.17 lukem }
200 1.62 christos terminal_beep(el);
201 1.62 christos terminal__flush(el);
202 1.1 cgd }
203 1.1 cgd
204 1.1 cgd
205 1.1 cgd /* read_getcmd():
206 1.69 christos * Get next command from the input stream, return OKCMD on success.
207 1.53 christos * Character values > 255 are not looked up in the map, but inserted.
208 1.1 cgd */
209 1.1 cgd private int
210 1.53 christos read_getcmd(EditLine *el, el_action_t *cmdnum, Char *ch)
211 1.1 cgd {
212 1.75 christos static const Char meta = (Char)0x80;
213 1.23 christos el_action_t cmd;
214 1.83 christos wchar_t wc;
215 1.17 lukem int num;
216 1.1 cgd
217 1.44 christos el->el_errno = 0;
218 1.23 christos do {
219 1.83 christos if ((num = el_wgetc(el, &wc)) != 1) {/* if EOF or error */
220 1.49 christos el->el_errno = num == 0 ? 0 : errno;
221 1.69 christos return 0; /* not OKCMD */
222 1.44 christos }
223 1.83 christos *ch = (Char)wc;
224 1.1 cgd
225 1.1 cgd #ifdef KANJI
226 1.75 christos if ((*ch & meta)) {
227 1.17 lukem el->el_state.metanext = 0;
228 1.17 lukem cmd = CcViMap[' '];
229 1.17 lukem break;
230 1.17 lukem } else
231 1.1 cgd #endif /* KANJI */
232 1.1 cgd
233 1.17 lukem if (el->el_state.metanext) {
234 1.17 lukem el->el_state.metanext = 0;
235 1.75 christos *ch |= meta;
236 1.17 lukem }
237 1.66 christos if (*ch >= N_KEYS)
238 1.66 christos cmd = ED_INSERT;
239 1.53 christos else
240 1.66 christos cmd = el->el_map.current[(unsigned char) *ch];
241 1.17 lukem if (cmd == ED_SEQUENCE_LEAD_IN) {
242 1.63 christos keymacro_value_t val;
243 1.63 christos switch (keymacro_get(el, ch, &val)) {
244 1.17 lukem case XK_CMD:
245 1.17 lukem cmd = val.cmd;
246 1.17 lukem break;
247 1.17 lukem case XK_STR:
248 1.89 christos el_wpush(el, val.str);
249 1.17 lukem break;
250 1.1 cgd #ifdef notyet
251 1.17 lukem case XK_EXE:
252 1.17 lukem /* XXX: In the future to run a user function */
253 1.17 lukem RunCommand(val.str);
254 1.17 lukem break;
255 1.1 cgd #endif
256 1.17 lukem default:
257 1.18 christos EL_ABORT((el->el_errfile, "Bad XK_ type \n"));
258 1.17 lukem break;
259 1.17 lukem }
260 1.17 lukem }
261 1.17 lukem if (el->el_map.alt == NULL)
262 1.17 lukem el->el_map.current = el->el_map.key;
263 1.23 christos } while (cmd == ED_SEQUENCE_LEAD_IN);
264 1.17 lukem *cmdnum = cmd;
265 1.65 christos return OKCMD;
266 1.1 cgd }
267 1.1 cgd
268 1.6 christos /* read_char():
269 1.6 christos * Read a character from the tty.
270 1.6 christos */
271 1.6 christos private int
272 1.84 christos read_char(EditLine *el, wchar_t *cp)
273 1.6 christos {
274 1.45 christos ssize_t num_read;
275 1.17 lukem int tried = 0;
276 1.66 christos char cbuf[MB_LEN_MAX];
277 1.66 christos size_t cbp = 0;
278 1.72 christos int save_errno = errno;
279 1.6 christos
280 1.46 christos again:
281 1.46 christos el->el_signal->sig_no = 0;
282 1.66 christos while ((num_read = read(el->el_infd, cbuf + cbp, (size_t)1)) == -1) {
283 1.68 christos int e = errno;
284 1.56 christos switch (el->el_signal->sig_no) {
285 1.56 christos case SIGCONT:
286 1.89 christos el_wset(el, EL_REFRESH);
287 1.57 christos /*FALLTHROUGH*/
288 1.56 christos case SIGWINCH:
289 1.46 christos sig_set(el);
290 1.46 christos goto again;
291 1.56 christos default:
292 1.56 christos break;
293 1.46 christos }
294 1.72 christos if (!tried && read__fixio(el->el_infd, e) == 0) {
295 1.72 christos errno = save_errno;
296 1.17 lukem tried = 1;
297 1.72 christos } else {
298 1.68 christos errno = e;
299 1.84 christos *cp = L'\0';
300 1.65 christos return -1;
301 1.17 lukem }
302 1.46 christos }
303 1.53 christos
304 1.70 christos /* Test for EOF */
305 1.70 christos if (num_read == 0) {
306 1.84 christos *cp = L'\0';
307 1.70 christos return 0;
308 1.70 christos }
309 1.70 christos
310 1.77 christos for (;;) {
311 1.88 christos mbstate_t mbs;
312 1.77 christos
313 1.53 christos ++cbp;
314 1.88 christos /* This only works because UTF8 is stateless. */
315 1.88 christos memset(&mbs, 0, sizeof(mbs));
316 1.88 christos switch (mbrtowc(cp, cbuf, cbp, &mbs)) {
317 1.72 christos case (size_t)-1:
318 1.72 christos if (cbp > 1) {
319 1.72 christos /*
320 1.72 christos * Invalid sequence, discard all bytes
321 1.72 christos * except the last one.
322 1.72 christos */
323 1.72 christos cbuf[0] = cbuf[cbp - 1];
324 1.72 christos cbp = 0;
325 1.77 christos break;
326 1.72 christos } else {
327 1.72 christos /* Invalid byte, discard it. */
328 1.72 christos cbp = 0;
329 1.72 christos goto again;
330 1.72 christos }
331 1.72 christos case (size_t)-2:
332 1.75 christos /*
333 1.75 christos * We don't support other multibyte charsets.
334 1.75 christos * The second condition shouldn't happen
335 1.75 christos * and is here merely for additional safety.
336 1.75 christos */
337 1.75 christos if ((el->el_flags & CHARSET_IS_UTF8) == 0 ||
338 1.75 christos cbp >= MB_LEN_MAX) {
339 1.68 christos errno = EILSEQ;
340 1.84 christos *cp = L'\0';
341 1.65 christos return -1;
342 1.53 christos }
343 1.72 christos /* Incomplete sequence, read another byte. */
344 1.53 christos goto again;
345 1.72 christos default:
346 1.72 christos /* Valid character, process it. */
347 1.77 christos return 1;
348 1.53 christos }
349 1.77 christos }
350 1.6 christos }
351 1.6 christos
352 1.40 christos /* read_pop():
353 1.40 christos * Pop a macro from the stack
354 1.40 christos */
355 1.40 christos private void
356 1.40 christos read_pop(c_macro_t *ma)
357 1.40 christos {
358 1.40 christos int i;
359 1.40 christos
360 1.40 christos el_free(ma->macro[0]);
361 1.50 christos for (i = 0; i < ma->level; i++)
362 1.50 christos ma->macro[i] = ma->macro[i + 1];
363 1.51 christos ma->level--;
364 1.40 christos ma->offset = 0;
365 1.40 christos }
366 1.1 cgd
367 1.83 christos /* el_wgetc():
368 1.83 christos * Read a wide character
369 1.1 cgd */
370 1.1 cgd public int
371 1.83 christos el_wgetc(EditLine *el, wchar_t *cp)
372 1.1 cgd {
373 1.17 lukem int num_read;
374 1.17 lukem c_macro_t *ma = &el->el_chared.c_macro;
375 1.1 cgd
376 1.62 christos terminal__flush(el);
377 1.17 lukem for (;;) {
378 1.17 lukem if (ma->level < 0)
379 1.17 lukem break;
380 1.12 simonb
381 1.40 christos if (ma->macro[0][ma->offset] == '\0') {
382 1.40 christos read_pop(ma);
383 1.17 lukem continue;
384 1.17 lukem }
385 1.40 christos
386 1.53 christos *cp = ma->macro[0][ma->offset++];
387 1.40 christos
388 1.40 christos if (ma->macro[0][ma->offset] == '\0') {
389 1.30 christos /* Needed for QuoteMode On */
390 1.40 christos read_pop(ma);
391 1.17 lukem }
392 1.40 christos
393 1.65 christos return 1;
394 1.1 cgd }
395 1.1 cgd
396 1.1 cgd #ifdef DEBUG_READ
397 1.17 lukem (void) fprintf(el->el_errfile, "Turning raw mode on\n");
398 1.1 cgd #endif /* DEBUG_READ */
399 1.17 lukem if (tty_rawmode(el) < 0)/* make sure the tty is set up correctly */
400 1.65 christos return 0;
401 1.1 cgd
402 1.1 cgd #ifdef DEBUG_READ
403 1.17 lukem (void) fprintf(el->el_errfile, "Reading a character\n");
404 1.1 cgd #endif /* DEBUG_READ */
405 1.84 christos num_read = (*el->el_read.read_char)(el, cp);
406 1.68 christos if (num_read < 0)
407 1.68 christos el->el_errno = errno;
408 1.1 cgd #ifdef DEBUG_READ
409 1.83 christos (void) fprintf(el->el_errfile, "Got it %lc\n", *cp);
410 1.1 cgd #endif /* DEBUG_READ */
411 1.65 christos return num_read;
412 1.1 cgd }
413 1.1 cgd
414 1.28 christos protected void
415 1.33 christos read_prepare(EditLine *el)
416 1.28 christos {
417 1.28 christos if (el->el_flags & HANDLE_SIGNALS)
418 1.28 christos sig_set(el);
419 1.28 christos if (el->el_flags & NO_TTY)
420 1.28 christos return;
421 1.28 christos if ((el->el_flags & (UNBUFFERED|EDIT_DISABLED)) == UNBUFFERED)
422 1.28 christos tty_rawmode(el);
423 1.28 christos
424 1.28 christos /* This is relatively cheap, and things go terribly wrong if
425 1.28 christos we have the wrong size. */
426 1.28 christos el_resize(el);
427 1.28 christos re_clear_display(el); /* reset the display stuff */
428 1.37 christos ch_reset(el, 0);
429 1.28 christos re_refresh(el); /* print the prompt */
430 1.35 christos
431 1.35 christos if (el->el_flags & UNBUFFERED)
432 1.62 christos terminal__flush(el);
433 1.28 christos }
434 1.28 christos
435 1.28 christos protected void
436 1.28 christos read_finish(EditLine *el)
437 1.28 christos {
438 1.28 christos if ((el->el_flags & UNBUFFERED) == 0)
439 1.28 christos (void) tty_cookedmode(el);
440 1.28 christos if (el->el_flags & HANDLE_SIGNALS)
441 1.28 christos sig_clr(el);
442 1.28 christos }
443 1.1 cgd
444 1.53 christos public const Char *
445 1.89 christos el_wgets(EditLine *el, int *nread)
446 1.1 cgd {
447 1.17 lukem int retval;
448 1.17 lukem el_action_t cmdnum = 0;
449 1.17 lukem int num; /* how many chars we have read at NL */
450 1.84 christos wchar_t wc;
451 1.58 christos Char ch, *cp;
452 1.28 christos int crlf = 0;
453 1.49 christos int nrb;
454 1.8 lukem #ifdef FIONREAD
455 1.17 lukem c_macro_t *ma = &el->el_chared.c_macro;
456 1.8 lukem #endif /* FIONREAD */
457 1.1 cgd
458 1.49 christos if (nread == NULL)
459 1.49 christos nread = &nrb;
460 1.52 christos *nread = 0;
461 1.49 christos
462 1.17 lukem if (el->el_flags & NO_TTY) {
463 1.19 jdolecek size_t idx;
464 1.6 christos
465 1.58 christos cp = el->el_line.buffer;
466 1.84 christos while ((num = (*el->el_read.read_char)(el, &wc)) == 1) {
467 1.84 christos *cp = (Char)wc;
468 1.19 jdolecek /* make sure there is space for next character */
469 1.19 jdolecek if (cp + 1 >= el->el_line.limit) {
470 1.67 christos idx = (size_t)(cp - el->el_line.buffer);
471 1.66 christos if (!ch_enlargebufs(el, (size_t)2))
472 1.19 jdolecek break;
473 1.19 jdolecek cp = &el->el_line.buffer[idx];
474 1.19 jdolecek }
475 1.17 lukem cp++;
476 1.28 christos if (el->el_flags & UNBUFFERED)
477 1.28 christos break;
478 1.17 lukem if (cp[-1] == '\r' || cp[-1] == '\n')
479 1.17 lukem break;
480 1.6 christos }
481 1.49 christos if (num == -1) {
482 1.49 christos if (errno == EINTR)
483 1.49 christos cp = el->el_line.buffer;
484 1.49 christos el->el_errno = errno;
485 1.49 christos }
486 1.19 jdolecek
487 1.58 christos goto noedit;
488 1.7 christos }
489 1.22 christos
490 1.1 cgd
491 1.1 cgd #ifdef FIONREAD
492 1.17 lukem if (el->el_tty.t_mode == EX_IO && ma->level < 0) {
493 1.17 lukem long chrs = 0;
494 1.1 cgd
495 1.64 christos (void) ioctl(el->el_infd, FIONREAD, &chrs);
496 1.17 lukem if (chrs == 0) {
497 1.17 lukem if (tty_rawmode(el) < 0) {
498 1.49 christos errno = 0;
499 1.49 christos *nread = 0;
500 1.65 christos return NULL;
501 1.17 lukem }
502 1.17 lukem }
503 1.1 cgd }
504 1.1 cgd #endif /* FIONREAD */
505 1.1 cgd
506 1.28 christos if ((el->el_flags & UNBUFFERED) == 0)
507 1.28 christos read_prepare(el);
508 1.13 sommerfe
509 1.17 lukem if (el->el_flags & EDIT_DISABLED) {
510 1.19 jdolecek size_t idx;
511 1.44 christos
512 1.34 christos if ((el->el_flags & UNBUFFERED) == 0)
513 1.34 christos cp = el->el_line.buffer;
514 1.34 christos else
515 1.34 christos cp = el->el_line.lastchar;
516 1.13 sommerfe
517 1.62 christos terminal__flush(el);
518 1.17 lukem
519 1.84 christos while ((num = (*el->el_read.read_char)(el, &wc)) == 1) {
520 1.84 christos *cp = (Char)wc;
521 1.19 jdolecek /* make sure there is space next character */
522 1.19 jdolecek if (cp + 1 >= el->el_line.limit) {
523 1.67 christos idx = (size_t)(cp - el->el_line.buffer);
524 1.66 christos if (!ch_enlargebufs(el, (size_t)2))
525 1.19 jdolecek break;
526 1.19 jdolecek cp = &el->el_line.buffer[idx];
527 1.19 jdolecek }
528 1.17 lukem cp++;
529 1.28 christos crlf = cp[-1] == '\r' || cp[-1] == '\n';
530 1.28 christos if (el->el_flags & UNBUFFERED)
531 1.28 christos break;
532 1.28 christos if (crlf)
533 1.17 lukem break;
534 1.19 jdolecek }
535 1.49 christos
536 1.49 christos if (num == -1) {
537 1.49 christos if (errno == EINTR)
538 1.49 christos cp = el->el_line.buffer;
539 1.49 christos el->el_errno = errno;
540 1.49 christos }
541 1.13 sommerfe
542 1.58 christos goto noedit;
543 1.13 sommerfe }
544 1.22 christos
545 1.17 lukem for (num = OKCMD; num == OKCMD;) { /* while still editing this
546 1.17 lukem * line */
547 1.1 cgd #ifdef DEBUG_EDIT
548 1.17 lukem read_debug(el);
549 1.1 cgd #endif /* DEBUG_EDIT */
550 1.17 lukem /* if EOF or error */
551 1.17 lukem if ((num = read_getcmd(el, &cmdnum, &ch)) != OKCMD) {
552 1.68 christos num = -1;
553 1.1 cgd #ifdef DEBUG_READ
554 1.17 lukem (void) fprintf(el->el_errfile,
555 1.17 lukem "Returning from el_gets %d\n", num);
556 1.1 cgd #endif /* DEBUG_READ */
557 1.17 lukem break;
558 1.17 lukem }
559 1.44 christos if (el->el_errno == EINTR) {
560 1.44 christos el->el_line.buffer[0] = '\0';
561 1.44 christos el->el_line.lastchar =
562 1.44 christos el->el_line.cursor = el->el_line.buffer;
563 1.44 christos break;
564 1.44 christos }
565 1.71 christos if ((size_t)cmdnum >= el->el_map.nfunc) { /* BUG CHECK command */
566 1.1 cgd #ifdef DEBUG_EDIT
567 1.17 lukem (void) fprintf(el->el_errfile,
568 1.17 lukem "ERROR: illegal command from key 0%o\r\n", ch);
569 1.1 cgd #endif /* DEBUG_EDIT */
570 1.17 lukem continue; /* try again */
571 1.17 lukem }
572 1.17 lukem /* now do the real command */
573 1.1 cgd #ifdef DEBUG_READ
574 1.17 lukem {
575 1.17 lukem el_bindings_t *b;
576 1.17 lukem for (b = el->el_map.help; b->name; b++)
577 1.17 lukem if (b->func == cmdnum)
578 1.17 lukem break;
579 1.17 lukem if (b->name)
580 1.17 lukem (void) fprintf(el->el_errfile,
581 1.88 christos "Executing %ls\n", b->name);
582 1.17 lukem else
583 1.17 lukem (void) fprintf(el->el_errfile,
584 1.17 lukem "Error command = %d\n", cmdnum);
585 1.17 lukem }
586 1.1 cgd #endif /* DEBUG_READ */
587 1.23 christos /* vi redo needs these way down the levels... */
588 1.23 christos el->el_state.thiscmd = cmdnum;
589 1.23 christos el->el_state.thisch = ch;
590 1.23 christos if (el->el_map.type == MAP_VI &&
591 1.23 christos el->el_map.current == el->el_map.key &&
592 1.23 christos el->el_chared.c_redo.pos < el->el_chared.c_redo.lim) {
593 1.23 christos if (cmdnum == VI_DELETE_PREV_CHAR &&
594 1.23 christos el->el_chared.c_redo.pos != el->el_chared.c_redo.buf
595 1.88 christos && iswprint(el->el_chared.c_redo.pos[-1]))
596 1.23 christos el->el_chared.c_redo.pos--;
597 1.23 christos else
598 1.23 christos *el->el_chared.c_redo.pos++ = ch;
599 1.23 christos }
600 1.17 lukem retval = (*el->el_map.func[cmdnum]) (el, ch);
601 1.22 christos #ifdef DEBUG_READ
602 1.22 christos (void) fprintf(el->el_errfile,
603 1.22 christos "Returned state %d\n", retval );
604 1.22 christos #endif /* DEBUG_READ */
605 1.17 lukem
606 1.17 lukem /* save the last command here */
607 1.17 lukem el->el_state.lastcmd = cmdnum;
608 1.1 cgd
609 1.17 lukem /* use any return value */
610 1.17 lukem switch (retval) {
611 1.17 lukem case CC_CURSOR:
612 1.17 lukem re_refresh_cursor(el);
613 1.17 lukem break;
614 1.17 lukem
615 1.17 lukem case CC_REDISPLAY:
616 1.17 lukem re_clear_lines(el);
617 1.17 lukem re_clear_display(el);
618 1.17 lukem /* FALLTHROUGH */
619 1.17 lukem
620 1.17 lukem case CC_REFRESH:
621 1.17 lukem re_refresh(el);
622 1.17 lukem break;
623 1.17 lukem
624 1.17 lukem case CC_REFRESH_BEEP:
625 1.17 lukem re_refresh(el);
626 1.62 christos terminal_beep(el);
627 1.17 lukem break;
628 1.17 lukem
629 1.17 lukem case CC_NORM: /* normal char */
630 1.17 lukem break;
631 1.1 cgd
632 1.17 lukem case CC_ARGHACK: /* Suggested by Rich Salz */
633 1.17 lukem /* <rsalz (at) pineapple.bbn.com> */
634 1.23 christos continue; /* keep going... */
635 1.1 cgd
636 1.17 lukem case CC_EOF: /* end of file typed */
637 1.29 christos if ((el->el_flags & UNBUFFERED) == 0)
638 1.29 christos num = 0;
639 1.29 christos else if (num == -1) {
640 1.31 christos *el->el_line.lastchar++ = CONTROL('d');
641 1.29 christos el->el_line.cursor = el->el_line.lastchar;
642 1.29 christos num = 1;
643 1.29 christos }
644 1.17 lukem break;
645 1.17 lukem
646 1.17 lukem case CC_NEWLINE: /* normal end of line */
647 1.45 christos num = (int)(el->el_line.lastchar - el->el_line.buffer);
648 1.17 lukem break;
649 1.17 lukem
650 1.17 lukem case CC_FATAL: /* fatal error, reset to known state */
651 1.1 cgd #ifdef DEBUG_READ
652 1.17 lukem (void) fprintf(el->el_errfile,
653 1.17 lukem "*** editor fatal ERROR ***\r\n\n");
654 1.1 cgd #endif /* DEBUG_READ */
655 1.17 lukem /* put (real) cursor in a known place */
656 1.17 lukem re_clear_display(el); /* reset the display stuff */
657 1.39 christos ch_reset(el, 1); /* reset the input pointers */
658 1.66 christos re_refresh(el); /* print the prompt again */
659 1.17 lukem break;
660 1.1 cgd
661 1.17 lukem case CC_ERROR:
662 1.17 lukem default: /* functions we don't know about */
663 1.1 cgd #ifdef DEBUG_READ
664 1.17 lukem (void) fprintf(el->el_errfile,
665 1.17 lukem "*** editor ERROR ***\r\n\n");
666 1.1 cgd #endif /* DEBUG_READ */
667 1.62 christos terminal_beep(el);
668 1.62 christos terminal__flush(el);
669 1.17 lukem break;
670 1.17 lukem }
671 1.23 christos el->el_state.argument = 1;
672 1.23 christos el->el_state.doingarg = 0;
673 1.23 christos el->el_chared.c_vcmd.action = NOP;
674 1.28 christos if (el->el_flags & UNBUFFERED)
675 1.28 christos break;
676 1.1 cgd }
677 1.1 cgd
678 1.62 christos terminal__flush(el); /* flush any buffered output */
679 1.22 christos /* make sure the tty is set up correctly */
680 1.28 christos if ((el->el_flags & UNBUFFERED) == 0) {
681 1.28 christos read_finish(el);
682 1.49 christos *nread = num != -1 ? num : 0;
683 1.28 christos } else {
684 1.49 christos *nread = (int)(el->el_line.lastchar - el->el_line.buffer);
685 1.28 christos }
686 1.58 christos goto done;
687 1.58 christos noedit:
688 1.58 christos el->el_line.cursor = el->el_line.lastchar = cp;
689 1.58 christos *cp = '\0';
690 1.58 christos *nread = (int)(el->el_line.cursor - el->el_line.buffer);
691 1.49 christos done:
692 1.49 christos if (*nread == 0) {
693 1.49 christos if (num == -1) {
694 1.49 christos *nread = -1;
695 1.49 christos errno = el->el_errno;
696 1.49 christos }
697 1.49 christos return NULL;
698 1.49 christos } else
699 1.49 christos return el->el_line.buffer;
700 1.1 cgd }
701