read.c revision 1.97 1 1.97 christos /* $NetBSD: read.c,v 1.97 2016/05/22 19:44:26 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.97 christos __RCSID("$NetBSD: read.c,v 1.97 2016/05/22 19:44:26 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.94 christos #include "fcns.h"
58 1.95 christos #include "read.h"
59 1.95 christos
60 1.97 christos #define EL_MAXMACRO 10
61 1.97 christos
62 1.97 christos struct macros {
63 1.97 christos wchar_t **macro;
64 1.97 christos int level;
65 1.97 christos int offset;
66 1.97 christos };
67 1.97 christos
68 1.95 christos struct el_read_t {
69 1.97 christos struct macros macros;
70 1.95 christos el_rfunc_t read_char; /* Function to read a character. */
71 1.95 christos };
72 1.1 cgd
73 1.91 christos static int read__fixio(int, int);
74 1.91 christos static int read_char(EditLine *, wchar_t *);
75 1.91 christos static int read_getcmd(EditLine *, el_action_t *, wchar_t *);
76 1.97 christos static void read_clearmacros(struct macros *);
77 1.97 christos static void read_pop(struct macros *);
78 1.20 christos
79 1.20 christos /* read_init():
80 1.20 christos * Initialize the read stuff
81 1.20 christos */
82 1.96 christos libedit_private int
83 1.20 christos read_init(EditLine *el)
84 1.20 christos {
85 1.97 christos struct macros *ma;
86 1.97 christos
87 1.95 christos if ((el->el_read = el_malloc(sizeof(*el->el_read))) == NULL)
88 1.95 christos return -1;
89 1.97 christos
90 1.97 christos ma = &el->el_read->macros;
91 1.97 christos if ((ma->macro = el_malloc(EL_MAXMACRO *
92 1.97 christos sizeof(*ma->macro))) == NULL) {
93 1.97 christos free(el->el_read);
94 1.97 christos return -1;
95 1.97 christos }
96 1.97 christos ma->level = -1;
97 1.97 christos ma->offset = 0;
98 1.97 christos
99 1.20 christos /* builtin read_char */
100 1.95 christos el->el_read->read_char = read_char;
101 1.20 christos return 0;
102 1.20 christos }
103 1.20 christos
104 1.97 christos /* el_read_end():
105 1.97 christos * Free the data structures used by the read stuff.
106 1.97 christos */
107 1.97 christos libedit_private void
108 1.97 christos read_end(struct el_read_t *el_read)
109 1.97 christos {
110 1.97 christos read_clearmacros(&el_read->macros);
111 1.97 christos el_free(el_read->macros.macro);
112 1.97 christos el_read->macros.macro = NULL;
113 1.97 christos }
114 1.20 christos
115 1.20 christos /* el_read_setfn():
116 1.20 christos * Set the read char function to the one provided.
117 1.20 christos * If it is set to EL_BUILTIN_GETCFN, then reset to the builtin one.
118 1.20 christos */
119 1.96 christos libedit_private int
120 1.95 christos el_read_setfn(struct el_read_t *el_read, el_rfunc_t rc)
121 1.20 christos {
122 1.95 christos el_read->read_char = (rc == EL_BUILTIN_GETCFN) ? read_char : rc;
123 1.20 christos return 0;
124 1.20 christos }
125 1.20 christos
126 1.20 christos
127 1.20 christos /* el_read_getfn():
128 1.20 christos * return the current read char function, or EL_BUILTIN_GETCFN
129 1.20 christos * if it is the default one
130 1.20 christos */
131 1.96 christos libedit_private el_rfunc_t
132 1.95 christos el_read_getfn(struct el_read_t *el_read)
133 1.20 christos {
134 1.95 christos return el_read->read_char == read_char ?
135 1.95 christos EL_BUILTIN_GETCFN : el_read->read_char;
136 1.20 christos }
137 1.20 christos
138 1.1 cgd
139 1.1 cgd #ifdef DEBUG_EDIT
140 1.91 christos static void
141 1.17 lukem read_debug(EditLine *el)
142 1.1 cgd {
143 1.1 cgd
144 1.17 lukem if (el->el_line.cursor > el->el_line.lastchar)
145 1.17 lukem (void) fprintf(el->el_errfile, "cursor > lastchar\r\n");
146 1.17 lukem if (el->el_line.cursor < el->el_line.buffer)
147 1.17 lukem (void) fprintf(el->el_errfile, "cursor < buffer\r\n");
148 1.17 lukem if (el->el_line.cursor > el->el_line.limit)
149 1.17 lukem (void) fprintf(el->el_errfile, "cursor > limit\r\n");
150 1.17 lukem if (el->el_line.lastchar > el->el_line.limit)
151 1.17 lukem (void) fprintf(el->el_errfile, "lastchar > limit\r\n");
152 1.17 lukem if (el->el_line.limit != &el->el_line.buffer[EL_BUFSIZ - 2])
153 1.17 lukem (void) fprintf(el->el_errfile, "limit != &buffer[EL_BUFSIZ-2]\r\n");
154 1.1 cgd }
155 1.1 cgd #endif /* DEBUG_EDIT */
156 1.1 cgd
157 1.17 lukem
158 1.1 cgd /* read__fixio():
159 1.1 cgd * Try to recover from a read error
160 1.1 cgd */
161 1.10 christos /* ARGSUSED */
162 1.91 christos static int
163 1.25 christos read__fixio(int fd __attribute__((__unused__)), int e)
164 1.1 cgd {
165 1.17 lukem
166 1.17 lukem switch (e) {
167 1.17 lukem case -1: /* Make sure that the code is reachable */
168 1.1 cgd
169 1.1 cgd #ifdef EWOULDBLOCK
170 1.17 lukem case EWOULDBLOCK:
171 1.17 lukem #ifndef TRY_AGAIN
172 1.66 christos #define TRY_AGAIN
173 1.17 lukem #endif
174 1.1 cgd #endif /* EWOULDBLOCK */
175 1.1 cgd
176 1.1 cgd #if defined(POSIX) && defined(EAGAIN)
177 1.17 lukem #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
178 1.17 lukem case EAGAIN:
179 1.17 lukem #ifndef TRY_AGAIN
180 1.66 christos #define TRY_AGAIN
181 1.17 lukem #endif
182 1.17 lukem #endif /* EWOULDBLOCK && EWOULDBLOCK != EAGAIN */
183 1.1 cgd #endif /* POSIX && EAGAIN */
184 1.1 cgd
185 1.17 lukem e = 0;
186 1.1 cgd #ifdef TRY_AGAIN
187 1.17 lukem #if defined(F_SETFL) && defined(O_NDELAY)
188 1.17 lukem if ((e = fcntl(fd, F_GETFL, 0)) == -1)
189 1.65 christos return -1;
190 1.5 christos
191 1.17 lukem if (fcntl(fd, F_SETFL, e & ~O_NDELAY) == -1)
192 1.65 christos return -1;
193 1.5 christos else
194 1.17 lukem e = 1;
195 1.17 lukem #endif /* F_SETFL && O_NDELAY */
196 1.17 lukem
197 1.17 lukem #ifdef FIONBIO
198 1.17 lukem {
199 1.17 lukem int zero = 0;
200 1.17 lukem
201 1.64 christos if (ioctl(fd, FIONBIO, &zero) == -1)
202 1.65 christos return -1;
203 1.17 lukem else
204 1.17 lukem e = 1;
205 1.17 lukem }
206 1.17 lukem #endif /* FIONBIO */
207 1.1 cgd
208 1.1 cgd #endif /* TRY_AGAIN */
209 1.65 christos return e ? 0 : -1;
210 1.1 cgd
211 1.17 lukem case EINTR:
212 1.65 christos return 0;
213 1.1 cgd
214 1.17 lukem default:
215 1.65 christos return -1;
216 1.17 lukem }
217 1.1 cgd }
218 1.1 cgd
219 1.1 cgd
220 1.1 cgd /* el_push():
221 1.1 cgd * Push a macro
222 1.1 cgd */
223 1.91 christos void
224 1.90 christos el_wpush(EditLine *el, const wchar_t *str)
225 1.1 cgd {
226 1.97 christos struct macros *ma = &el->el_read->macros;
227 1.1 cgd
228 1.17 lukem if (str != NULL && ma->level + 1 < EL_MAXMACRO) {
229 1.17 lukem ma->level++;
230 1.89 christos if ((ma->macro[ma->level] = wcsdup(str)) != NULL)
231 1.30 christos return;
232 1.30 christos ma->level--;
233 1.17 lukem }
234 1.62 christos terminal_beep(el);
235 1.62 christos terminal__flush(el);
236 1.1 cgd }
237 1.1 cgd
238 1.1 cgd
239 1.1 cgd /* read_getcmd():
240 1.92 christos * Get next command from the input stream,
241 1.92 christos * return 0 on success or -1 on EOF or error.
242 1.53 christos * Character values > 255 are not looked up in the map, but inserted.
243 1.1 cgd */
244 1.91 christos static int
245 1.90 christos read_getcmd(EditLine *el, el_action_t *cmdnum, wchar_t *ch)
246 1.1 cgd {
247 1.90 christos static const wchar_t meta = (wchar_t)0x80;
248 1.23 christos el_action_t cmd;
249 1.17 lukem int num;
250 1.1 cgd
251 1.44 christos el->el_errno = 0;
252 1.23 christos do {
253 1.90 christos if ((num = el_wgetc(el, ch)) != 1) {/* if EOF or error */
254 1.49 christos el->el_errno = num == 0 ? 0 : errno;
255 1.92 christos return -1;
256 1.44 christos }
257 1.1 cgd
258 1.1 cgd #ifdef KANJI
259 1.75 christos if ((*ch & meta)) {
260 1.17 lukem el->el_state.metanext = 0;
261 1.17 lukem cmd = CcViMap[' '];
262 1.17 lukem break;
263 1.17 lukem } else
264 1.1 cgd #endif /* KANJI */
265 1.1 cgd
266 1.17 lukem if (el->el_state.metanext) {
267 1.17 lukem el->el_state.metanext = 0;
268 1.75 christos *ch |= meta;
269 1.17 lukem }
270 1.66 christos if (*ch >= N_KEYS)
271 1.66 christos cmd = ED_INSERT;
272 1.53 christos else
273 1.66 christos cmd = el->el_map.current[(unsigned char) *ch];
274 1.17 lukem if (cmd == ED_SEQUENCE_LEAD_IN) {
275 1.63 christos keymacro_value_t val;
276 1.63 christos switch (keymacro_get(el, ch, &val)) {
277 1.17 lukem case XK_CMD:
278 1.17 lukem cmd = val.cmd;
279 1.17 lukem break;
280 1.17 lukem case XK_STR:
281 1.89 christos el_wpush(el, val.str);
282 1.17 lukem break;
283 1.17 lukem default:
284 1.18 christos EL_ABORT((el->el_errfile, "Bad XK_ type \n"));
285 1.17 lukem break;
286 1.17 lukem }
287 1.17 lukem }
288 1.17 lukem if (el->el_map.alt == NULL)
289 1.17 lukem el->el_map.current = el->el_map.key;
290 1.23 christos } while (cmd == ED_SEQUENCE_LEAD_IN);
291 1.17 lukem *cmdnum = cmd;
292 1.92 christos return 0;
293 1.1 cgd }
294 1.1 cgd
295 1.6 christos /* read_char():
296 1.6 christos * Read a character from the tty.
297 1.6 christos */
298 1.91 christos static int
299 1.84 christos read_char(EditLine *el, wchar_t *cp)
300 1.6 christos {
301 1.45 christos ssize_t num_read;
302 1.17 lukem int tried = 0;
303 1.66 christos char cbuf[MB_LEN_MAX];
304 1.66 christos size_t cbp = 0;
305 1.72 christos int save_errno = errno;
306 1.6 christos
307 1.46 christos again:
308 1.46 christos el->el_signal->sig_no = 0;
309 1.66 christos while ((num_read = read(el->el_infd, cbuf + cbp, (size_t)1)) == -1) {
310 1.68 christos int e = errno;
311 1.56 christos switch (el->el_signal->sig_no) {
312 1.56 christos case SIGCONT:
313 1.89 christos el_wset(el, EL_REFRESH);
314 1.57 christos /*FALLTHROUGH*/
315 1.56 christos case SIGWINCH:
316 1.46 christos sig_set(el);
317 1.46 christos goto again;
318 1.56 christos default:
319 1.56 christos break;
320 1.46 christos }
321 1.72 christos if (!tried && read__fixio(el->el_infd, e) == 0) {
322 1.72 christos errno = save_errno;
323 1.17 lukem tried = 1;
324 1.72 christos } else {
325 1.68 christos errno = e;
326 1.84 christos *cp = L'\0';
327 1.65 christos return -1;
328 1.17 lukem }
329 1.46 christos }
330 1.53 christos
331 1.70 christos /* Test for EOF */
332 1.70 christos if (num_read == 0) {
333 1.84 christos *cp = L'\0';
334 1.70 christos return 0;
335 1.70 christos }
336 1.70 christos
337 1.77 christos for (;;) {
338 1.88 christos mbstate_t mbs;
339 1.77 christos
340 1.53 christos ++cbp;
341 1.88 christos /* This only works because UTF8 is stateless. */
342 1.88 christos memset(&mbs, 0, sizeof(mbs));
343 1.88 christos switch (mbrtowc(cp, cbuf, cbp, &mbs)) {
344 1.72 christos case (size_t)-1:
345 1.72 christos if (cbp > 1) {
346 1.72 christos /*
347 1.72 christos * Invalid sequence, discard all bytes
348 1.72 christos * except the last one.
349 1.72 christos */
350 1.72 christos cbuf[0] = cbuf[cbp - 1];
351 1.72 christos cbp = 0;
352 1.77 christos break;
353 1.72 christos } else {
354 1.72 christos /* Invalid byte, discard it. */
355 1.72 christos cbp = 0;
356 1.72 christos goto again;
357 1.72 christos }
358 1.72 christos case (size_t)-2:
359 1.75 christos /*
360 1.75 christos * We don't support other multibyte charsets.
361 1.75 christos * The second condition shouldn't happen
362 1.75 christos * and is here merely for additional safety.
363 1.75 christos */
364 1.75 christos if ((el->el_flags & CHARSET_IS_UTF8) == 0 ||
365 1.75 christos cbp >= MB_LEN_MAX) {
366 1.68 christos errno = EILSEQ;
367 1.84 christos *cp = L'\0';
368 1.65 christos return -1;
369 1.53 christos }
370 1.72 christos /* Incomplete sequence, read another byte. */
371 1.53 christos goto again;
372 1.72 christos default:
373 1.72 christos /* Valid character, process it. */
374 1.77 christos return 1;
375 1.53 christos }
376 1.77 christos }
377 1.6 christos }
378 1.6 christos
379 1.40 christos /* read_pop():
380 1.40 christos * Pop a macro from the stack
381 1.40 christos */
382 1.91 christos static void
383 1.97 christos read_pop(struct macros *ma)
384 1.40 christos {
385 1.40 christos int i;
386 1.40 christos
387 1.40 christos el_free(ma->macro[0]);
388 1.50 christos for (i = 0; i < ma->level; i++)
389 1.50 christos ma->macro[i] = ma->macro[i + 1];
390 1.51 christos ma->level--;
391 1.40 christos ma->offset = 0;
392 1.40 christos }
393 1.1 cgd
394 1.97 christos static void
395 1.97 christos read_clearmacros(struct macros *ma)
396 1.97 christos {
397 1.97 christos while (ma->level >= 0)
398 1.97 christos el_free(ma->macro[ma->level--]);
399 1.97 christos ma->offset = 0;
400 1.97 christos }
401 1.97 christos
402 1.83 christos /* el_wgetc():
403 1.83 christos * Read a wide character
404 1.1 cgd */
405 1.91 christos int
406 1.83 christos el_wgetc(EditLine *el, wchar_t *cp)
407 1.1 cgd {
408 1.97 christos struct macros *ma = &el->el_read->macros;
409 1.17 lukem int num_read;
410 1.1 cgd
411 1.62 christos terminal__flush(el);
412 1.17 lukem for (;;) {
413 1.17 lukem if (ma->level < 0)
414 1.17 lukem break;
415 1.12 simonb
416 1.40 christos if (ma->macro[0][ma->offset] == '\0') {
417 1.40 christos read_pop(ma);
418 1.17 lukem continue;
419 1.17 lukem }
420 1.40 christos
421 1.53 christos *cp = ma->macro[0][ma->offset++];
422 1.40 christos
423 1.40 christos if (ma->macro[0][ma->offset] == '\0') {
424 1.30 christos /* Needed for QuoteMode On */
425 1.40 christos read_pop(ma);
426 1.17 lukem }
427 1.40 christos
428 1.65 christos return 1;
429 1.1 cgd }
430 1.1 cgd
431 1.1 cgd #ifdef DEBUG_READ
432 1.17 lukem (void) fprintf(el->el_errfile, "Turning raw mode on\n");
433 1.1 cgd #endif /* DEBUG_READ */
434 1.17 lukem if (tty_rawmode(el) < 0)/* make sure the tty is set up correctly */
435 1.65 christos return 0;
436 1.1 cgd
437 1.1 cgd #ifdef DEBUG_READ
438 1.17 lukem (void) fprintf(el->el_errfile, "Reading a character\n");
439 1.1 cgd #endif /* DEBUG_READ */
440 1.95 christos num_read = (*el->el_read->read_char)(el, cp);
441 1.68 christos if (num_read < 0)
442 1.68 christos el->el_errno = errno;
443 1.1 cgd #ifdef DEBUG_READ
444 1.83 christos (void) fprintf(el->el_errfile, "Got it %lc\n", *cp);
445 1.1 cgd #endif /* DEBUG_READ */
446 1.65 christos return num_read;
447 1.1 cgd }
448 1.1 cgd
449 1.96 christos libedit_private void
450 1.33 christos read_prepare(EditLine *el)
451 1.28 christos {
452 1.28 christos if (el->el_flags & HANDLE_SIGNALS)
453 1.28 christos sig_set(el);
454 1.28 christos if (el->el_flags & NO_TTY)
455 1.28 christos return;
456 1.28 christos if ((el->el_flags & (UNBUFFERED|EDIT_DISABLED)) == UNBUFFERED)
457 1.28 christos tty_rawmode(el);
458 1.28 christos
459 1.28 christos /* This is relatively cheap, and things go terribly wrong if
460 1.28 christos we have the wrong size. */
461 1.28 christos el_resize(el);
462 1.28 christos re_clear_display(el); /* reset the display stuff */
463 1.97 christos ch_reset(el);
464 1.28 christos re_refresh(el); /* print the prompt */
465 1.35 christos
466 1.35 christos if (el->el_flags & UNBUFFERED)
467 1.62 christos terminal__flush(el);
468 1.28 christos }
469 1.28 christos
470 1.96 christos libedit_private void
471 1.28 christos read_finish(EditLine *el)
472 1.28 christos {
473 1.28 christos if ((el->el_flags & UNBUFFERED) == 0)
474 1.28 christos (void) tty_cookedmode(el);
475 1.28 christos if (el->el_flags & HANDLE_SIGNALS)
476 1.28 christos sig_clr(el);
477 1.28 christos }
478 1.1 cgd
479 1.91 christos const wchar_t *
480 1.89 christos el_wgets(EditLine *el, int *nread)
481 1.1 cgd {
482 1.17 lukem int retval;
483 1.17 lukem el_action_t cmdnum = 0;
484 1.17 lukem int num; /* how many chars we have read at NL */
485 1.84 christos wchar_t wc;
486 1.90 christos wchar_t ch, *cp;
487 1.28 christos int crlf = 0;
488 1.49 christos int nrb;
489 1.1 cgd
490 1.49 christos if (nread == NULL)
491 1.49 christos nread = &nrb;
492 1.52 christos *nread = 0;
493 1.49 christos
494 1.17 lukem if (el->el_flags & NO_TTY) {
495 1.19 jdolecek size_t idx;
496 1.6 christos
497 1.58 christos cp = el->el_line.buffer;
498 1.95 christos while ((num = (*el->el_read->read_char)(el, &wc)) == 1) {
499 1.90 christos *cp = wc;
500 1.19 jdolecek /* make sure there is space for next character */
501 1.19 jdolecek if (cp + 1 >= el->el_line.limit) {
502 1.67 christos idx = (size_t)(cp - el->el_line.buffer);
503 1.66 christos if (!ch_enlargebufs(el, (size_t)2))
504 1.19 jdolecek break;
505 1.19 jdolecek cp = &el->el_line.buffer[idx];
506 1.19 jdolecek }
507 1.17 lukem cp++;
508 1.28 christos if (el->el_flags & UNBUFFERED)
509 1.28 christos break;
510 1.17 lukem if (cp[-1] == '\r' || cp[-1] == '\n')
511 1.17 lukem break;
512 1.6 christos }
513 1.49 christos if (num == -1) {
514 1.49 christos if (errno == EINTR)
515 1.49 christos cp = el->el_line.buffer;
516 1.49 christos el->el_errno = errno;
517 1.49 christos }
518 1.19 jdolecek
519 1.58 christos goto noedit;
520 1.7 christos }
521 1.22 christos
522 1.1 cgd
523 1.1 cgd #ifdef FIONREAD
524 1.97 christos if (el->el_tty.t_mode == EX_IO && el->el_read->macros.level < 0) {
525 1.93 christos int chrs = 0;
526 1.1 cgd
527 1.64 christos (void) ioctl(el->el_infd, FIONREAD, &chrs);
528 1.17 lukem if (chrs == 0) {
529 1.17 lukem if (tty_rawmode(el) < 0) {
530 1.49 christos errno = 0;
531 1.49 christos *nread = 0;
532 1.65 christos return NULL;
533 1.17 lukem }
534 1.17 lukem }
535 1.1 cgd }
536 1.1 cgd #endif /* FIONREAD */
537 1.1 cgd
538 1.28 christos if ((el->el_flags & UNBUFFERED) == 0)
539 1.28 christos read_prepare(el);
540 1.13 sommerfe
541 1.17 lukem if (el->el_flags & EDIT_DISABLED) {
542 1.19 jdolecek size_t idx;
543 1.44 christos
544 1.34 christos if ((el->el_flags & UNBUFFERED) == 0)
545 1.34 christos cp = el->el_line.buffer;
546 1.34 christos else
547 1.34 christos cp = el->el_line.lastchar;
548 1.13 sommerfe
549 1.62 christos terminal__flush(el);
550 1.17 lukem
551 1.95 christos while ((num = (*el->el_read->read_char)(el, &wc)) == 1) {
552 1.90 christos *cp = wc;
553 1.19 jdolecek /* make sure there is space next character */
554 1.19 jdolecek if (cp + 1 >= el->el_line.limit) {
555 1.67 christos idx = (size_t)(cp - el->el_line.buffer);
556 1.66 christos if (!ch_enlargebufs(el, (size_t)2))
557 1.19 jdolecek break;
558 1.19 jdolecek cp = &el->el_line.buffer[idx];
559 1.19 jdolecek }
560 1.17 lukem cp++;
561 1.28 christos crlf = cp[-1] == '\r' || cp[-1] == '\n';
562 1.28 christos if (el->el_flags & UNBUFFERED)
563 1.28 christos break;
564 1.28 christos if (crlf)
565 1.17 lukem break;
566 1.19 jdolecek }
567 1.49 christos
568 1.49 christos if (num == -1) {
569 1.49 christos if (errno == EINTR)
570 1.49 christos cp = el->el_line.buffer;
571 1.49 christos el->el_errno = errno;
572 1.49 christos }
573 1.13 sommerfe
574 1.58 christos goto noedit;
575 1.13 sommerfe }
576 1.22 christos
577 1.92 christos for (num = -1; num == -1;) { /* while still editing this line */
578 1.1 cgd #ifdef DEBUG_EDIT
579 1.17 lukem read_debug(el);
580 1.1 cgd #endif /* DEBUG_EDIT */
581 1.17 lukem /* if EOF or error */
582 1.92 christos if (read_getcmd(el, &cmdnum, &ch) == -1) {
583 1.1 cgd #ifdef DEBUG_READ
584 1.17 lukem (void) fprintf(el->el_errfile,
585 1.92 christos "Returning from el_gets\n");
586 1.1 cgd #endif /* DEBUG_READ */
587 1.17 lukem break;
588 1.17 lukem }
589 1.44 christos if (el->el_errno == EINTR) {
590 1.44 christos el->el_line.buffer[0] = '\0';
591 1.44 christos el->el_line.lastchar =
592 1.44 christos el->el_line.cursor = el->el_line.buffer;
593 1.44 christos break;
594 1.44 christos }
595 1.71 christos if ((size_t)cmdnum >= el->el_map.nfunc) { /* BUG CHECK command */
596 1.1 cgd #ifdef DEBUG_EDIT
597 1.17 lukem (void) fprintf(el->el_errfile,
598 1.17 lukem "ERROR: illegal command from key 0%o\r\n", ch);
599 1.1 cgd #endif /* DEBUG_EDIT */
600 1.17 lukem continue; /* try again */
601 1.17 lukem }
602 1.17 lukem /* now do the real command */
603 1.1 cgd #ifdef DEBUG_READ
604 1.17 lukem {
605 1.17 lukem el_bindings_t *b;
606 1.17 lukem for (b = el->el_map.help; b->name; b++)
607 1.17 lukem if (b->func == cmdnum)
608 1.17 lukem break;
609 1.17 lukem if (b->name)
610 1.17 lukem (void) fprintf(el->el_errfile,
611 1.88 christos "Executing %ls\n", b->name);
612 1.17 lukem else
613 1.17 lukem (void) fprintf(el->el_errfile,
614 1.17 lukem "Error command = %d\n", cmdnum);
615 1.17 lukem }
616 1.1 cgd #endif /* DEBUG_READ */
617 1.23 christos /* vi redo needs these way down the levels... */
618 1.23 christos el->el_state.thiscmd = cmdnum;
619 1.23 christos el->el_state.thisch = ch;
620 1.23 christos if (el->el_map.type == MAP_VI &&
621 1.23 christos el->el_map.current == el->el_map.key &&
622 1.23 christos el->el_chared.c_redo.pos < el->el_chared.c_redo.lim) {
623 1.23 christos if (cmdnum == VI_DELETE_PREV_CHAR &&
624 1.23 christos el->el_chared.c_redo.pos != el->el_chared.c_redo.buf
625 1.88 christos && iswprint(el->el_chared.c_redo.pos[-1]))
626 1.23 christos el->el_chared.c_redo.pos--;
627 1.23 christos else
628 1.23 christos *el->el_chared.c_redo.pos++ = ch;
629 1.23 christos }
630 1.17 lukem retval = (*el->el_map.func[cmdnum]) (el, ch);
631 1.22 christos #ifdef DEBUG_READ
632 1.22 christos (void) fprintf(el->el_errfile,
633 1.22 christos "Returned state %d\n", retval );
634 1.22 christos #endif /* DEBUG_READ */
635 1.17 lukem
636 1.17 lukem /* save the last command here */
637 1.17 lukem el->el_state.lastcmd = cmdnum;
638 1.1 cgd
639 1.17 lukem /* use any return value */
640 1.17 lukem switch (retval) {
641 1.17 lukem case CC_CURSOR:
642 1.17 lukem re_refresh_cursor(el);
643 1.17 lukem break;
644 1.17 lukem
645 1.17 lukem case CC_REDISPLAY:
646 1.17 lukem re_clear_lines(el);
647 1.17 lukem re_clear_display(el);
648 1.17 lukem /* FALLTHROUGH */
649 1.17 lukem
650 1.17 lukem case CC_REFRESH:
651 1.17 lukem re_refresh(el);
652 1.17 lukem break;
653 1.17 lukem
654 1.17 lukem case CC_REFRESH_BEEP:
655 1.17 lukem re_refresh(el);
656 1.62 christos terminal_beep(el);
657 1.17 lukem break;
658 1.17 lukem
659 1.17 lukem case CC_NORM: /* normal char */
660 1.17 lukem break;
661 1.1 cgd
662 1.17 lukem case CC_ARGHACK: /* Suggested by Rich Salz */
663 1.17 lukem /* <rsalz (at) pineapple.bbn.com> */
664 1.23 christos continue; /* keep going... */
665 1.1 cgd
666 1.17 lukem case CC_EOF: /* end of file typed */
667 1.29 christos if ((el->el_flags & UNBUFFERED) == 0)
668 1.29 christos num = 0;
669 1.29 christos else if (num == -1) {
670 1.31 christos *el->el_line.lastchar++ = CONTROL('d');
671 1.29 christos el->el_line.cursor = el->el_line.lastchar;
672 1.29 christos num = 1;
673 1.29 christos }
674 1.17 lukem break;
675 1.17 lukem
676 1.17 lukem case CC_NEWLINE: /* normal end of line */
677 1.45 christos num = (int)(el->el_line.lastchar - el->el_line.buffer);
678 1.17 lukem break;
679 1.17 lukem
680 1.17 lukem case CC_FATAL: /* fatal error, reset to known state */
681 1.1 cgd #ifdef DEBUG_READ
682 1.17 lukem (void) fprintf(el->el_errfile,
683 1.17 lukem "*** editor fatal ERROR ***\r\n\n");
684 1.1 cgd #endif /* DEBUG_READ */
685 1.17 lukem /* put (real) cursor in a known place */
686 1.17 lukem re_clear_display(el); /* reset the display stuff */
687 1.97 christos ch_reset(el); /* reset the input pointers */
688 1.97 christos read_clearmacros(&el->el_read->macros);
689 1.66 christos re_refresh(el); /* print the prompt again */
690 1.17 lukem break;
691 1.1 cgd
692 1.17 lukem case CC_ERROR:
693 1.17 lukem default: /* functions we don't know about */
694 1.1 cgd #ifdef DEBUG_READ
695 1.17 lukem (void) fprintf(el->el_errfile,
696 1.17 lukem "*** editor ERROR ***\r\n\n");
697 1.1 cgd #endif /* DEBUG_READ */
698 1.62 christos terminal_beep(el);
699 1.62 christos terminal__flush(el);
700 1.17 lukem break;
701 1.17 lukem }
702 1.23 christos el->el_state.argument = 1;
703 1.23 christos el->el_state.doingarg = 0;
704 1.23 christos el->el_chared.c_vcmd.action = NOP;
705 1.28 christos if (el->el_flags & UNBUFFERED)
706 1.28 christos break;
707 1.1 cgd }
708 1.1 cgd
709 1.62 christos terminal__flush(el); /* flush any buffered output */
710 1.22 christos /* make sure the tty is set up correctly */
711 1.28 christos if ((el->el_flags & UNBUFFERED) == 0) {
712 1.28 christos read_finish(el);
713 1.49 christos *nread = num != -1 ? num : 0;
714 1.28 christos } else {
715 1.49 christos *nread = (int)(el->el_line.lastchar - el->el_line.buffer);
716 1.28 christos }
717 1.58 christos goto done;
718 1.58 christos noedit:
719 1.58 christos el->el_line.cursor = el->el_line.lastchar = cp;
720 1.58 christos *cp = '\0';
721 1.58 christos *nread = (int)(el->el_line.cursor - el->el_line.buffer);
722 1.49 christos done:
723 1.49 christos if (*nread == 0) {
724 1.49 christos if (num == -1) {
725 1.49 christos *nread = -1;
726 1.49 christos errno = el->el_errno;
727 1.49 christos }
728 1.49 christos return NULL;
729 1.49 christos } else
730 1.49 christos return el->el_line.buffer;
731 1.1 cgd }
732