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