1
/* $NetBSD: read.c,v 1.43 2009/02/05 19:15:44 christos Exp $ */
1
/* $NetBSD: read.c,v 1.67 2011/08/16 16:25:15 christos Exp $ */
4
4
* Copyright (c) 1992, 1993
49
49
#include <unistd.h>
50
50
#include <stdlib.h>
54
#define OKCMD -1 /* must be -1! */
55
56
private int read__fixio(int, int);
56
57
private int read_preread(EditLine *);
57
private int read_char(EditLine *, char *);
58
private int read_getcmd(EditLine *, el_action_t *, char *);
58
private int read_char(EditLine *, Char *);
59
private int read_getcmd(EditLine *, el_action_t *, Char *);
59
60
private void read_pop(c_macro_t *);
149
150
#if defined(F_SETFL) && defined(O_NDELAY)
150
151
if ((e = fcntl(fd, F_GETFL, 0)) == -1)
153
154
if (fcntl(fd, F_SETFL, e & ~O_NDELAY) == -1)
157
158
#endif /* F_SETFL && O_NDELAY */
190
191
if (el->el_tty.t_mode == ED_IO)
195
/* FIONREAD attempts to buffer up multiple bytes, and to make that work
196
* properly with partial wide/UTF-8 characters would need some careful work. */
194
(void) ioctl(el->el_infd, FIONREAD, (ioctl_t) & chrs);
198
(void) ioctl(el->el_infd, FIONREAD, &chrs);
196
200
char buf[EL_BUFSIZ];
215
el_push(EditLine *el, const char *str)
219
FUN(el,push)(EditLine *el, const Char *str)
217
221
c_macro_t *ma = &el->el_chared.c_macro;
219
223
if (str != NULL && ma->level + 1 < EL_MAXMACRO) {
221
if ((ma->macro[ma->level] = el_strdup(str)) != NULL)
225
if ((ma->macro[ma->level] = Strdup(str)) != NULL)
230
234
/* read_getcmd():
231
235
* Return next command from the input stream.
236
* Character values > 255 are not looked up in the map, but inserted.
234
read_getcmd(EditLine *el, el_action_t *cmdnum, char *ch)
239
read_getcmd(EditLine *el, el_action_t *cmdnum, Char *ch)
240
if ((num = el_getc(el, ch)) != 1) /* if EOF or error */
246
if ((num = FUN(el,getc)(el, ch)) != 1) {/* if EOF or error */
247
el->el_errno = num == 0 ? 0 : errno;
244
252
if ((*ch & 0200)) {
252
260
el->el_state.metanext = 0;
255
cmd = el->el_map.current[(unsigned char) *ch];
268
cmd = el->el_map.current[(unsigned char) *ch];
256
269
if (cmd == ED_SEQUENCE_LEAD_IN) {
258
switch (key_get(el, ch, &val)) {
270
keymacro_value_t val;
271
switch (keymacro_get(el, ch, &val)) {
263
el_push(el, val.str);
276
FUN(el,push)(el, val.str);
277
290
el->el_map.current = el->el_map.key;
278
291
} while (cmd == ED_SEQUENCE_LEAD_IN);
285
297
* Read a character from the tty.
288
read_char(EditLine *el, char *cp)
300
read_char(EditLine *el, Char *cp)
293
while ((num_read = read(el->el_infd, cp, 1)) == -1)
304
char cbuf[MB_LEN_MAX];
309
static mbstate_t state, temp_state;
310
memset(&state, 0, sizeof(mbstate_t));
314
el->el_signal->sig_no = 0;
315
while ((num_read = read(el->el_infd, cbuf + cbp, (size_t)1)) == -1) {
316
switch (el->el_signal->sig_no) {
318
FUN(el,set)(el, EL_REFRESH);
294
326
if (!tried && read__fixio(el->el_infd, errno) == 0)
336
if (cbp > (size_t) MB_CUR_MAX) { /* "shouldn't happen" */
343
if ((bytes = mbrtowc(cp, cbuf, cbp, &state)) == -2)
345
/* Incomplete sequence, restore the state and scan more bytes. */
349
else if (bytes == -1)
351
/* Invalid sequence, reset the state and continue. */
353
memset(&state, 0, sizeof(mbstate_t));
356
/* We successfully read one single or multi-byte character */
358
*cp = (unsigned char)cbuf[0];
362
if ((el->el_flags & IGNORE_EXTCHARS) && bytes > 1) {
363
cbp = 0; /* skip this character */
368
return (int)num_read;
319
387
* Read a character
322
el_getc(EditLine *el, char *cp)
390
FUN(el,getc)(EditLine *el, Char *cp)
325
393
c_macro_t *ma = &el->el_chared.c_macro;
329
397
if (ma->level < 0) {
330
398
if (!read_preread(el))
342
*cp = ma->macro[0][ma->offset++] & 0377;
410
*cp = ma->macro[0][ma->offset++];
344
412
if (ma->macro[0][ma->offset] == '\0') {
345
413
/* Needed for QuoteMode On */
352
420
#ifdef DEBUG_READ
353
421
(void) fprintf(el->el_errfile, "Turning raw mode on\n");
354
422
#endif /* DEBUG_READ */
355
423
if (tty_rawmode(el) < 0)/* make sure the tty is set up correctly */
358
426
#ifdef DEBUG_READ
359
427
(void) fprintf(el->el_errfile, "Reading a character\n");
360
428
#endif /* DEBUG_READ */
361
429
num_read = (*el->el_read.read_char)(el, cp);
431
if (el->el_flags & NARROW_READ)
432
*cp = *(char *)(void *)cp;
362
434
#ifdef DEBUG_READ
363
435
(void) fprintf(el->el_errfile, "Got it %c\n", *cp);
364
436
#endif /* DEBUG_READ */
399
el_gets(EditLine *el, int *nread)
471
FUN(el,gets)(EditLine *el, int *nread)
402
474
el_action_t cmdnum = 0;
403
475
int num; /* how many chars we have read at NL */
407
480
c_macro_t *ma = &el->el_chared.c_macro;
408
481
#endif /* FIONREAD */
410
487
if (el->el_flags & NO_TTY) {
411
char *cp = el->el_line.buffer;
414
while ((*el->el_read.read_char)(el, cp) == 1) {
490
cp = el->el_line.buffer;
491
while ((num = (*el->el_read.read_char)(el, cp)) == 1) {
415
492
/* make sure there is space for next character */
416
493
if (cp + 1 >= el->el_line.limit) {
417
idx = (cp - el->el_line.buffer);
418
if (!ch_enlargebufs(el, 2))
494
idx = (size_t)(cp - el->el_line.buffer);
495
if (!ch_enlargebufs(el, (size_t)2))
420
497
cp = &el->el_line.buffer[idx];
438
516
if (el->el_tty.t_mode == EX_IO && ma->level < 0) {
441
(void) ioctl(el->el_infd, FIONREAD, (ioctl_t) & chrs);
519
(void) ioctl(el->el_infd, FIONREAD, &chrs);
443
521
if (tty_rawmode(el) < 0) {
453
531
read_prepare(el);
455
533
if (el->el_flags & EDIT_DISABLED) {
458
536
if ((el->el_flags & UNBUFFERED) == 0)
459
537
cp = el->el_line.buffer;
461
539
cp = el->el_line.lastchar;
465
while ((*el->el_read.read_char)(el, cp) == 1) {
543
while ((num = (*el->el_read.read_char)(el, cp)) == 1) {
466
544
/* make sure there is space next character */
467
545
if (cp + 1 >= el->el_line.limit) {
468
idx = (cp - el->el_line.buffer);
469
if (!ch_enlargebufs(el, 2))
546
idx = (size_t)(cp - el->el_line.buffer);
547
if (!ch_enlargebufs(el, (size_t)2))
471
549
cp = &el->el_line.buffer[idx];
473
if (*cp == 4) /* ought to be stty eof */
476
552
crlf = cp[-1] == '\r' || cp[-1] == '\n';
477
553
if (el->el_flags & UNBUFFERED)
500
578
#endif /* DEBUG_READ */
581
if (el->el_errno == EINTR) {
582
el->el_line.buffer[0] = '\0';
583
el->el_line.lastchar =
584
el->el_line.cursor = el->el_line.buffer;
503
587
if ((unsigned int)cmdnum >= (unsigned int)el->el_map.nfunc) { /* BUG CHECK command */
504
588
#ifdef DEBUG_EDIT
505
589
(void) fprintf(el->el_errfile,
530
614
el->el_chared.c_redo.pos < el->el_chared.c_redo.lim) {
531
615
if (cmdnum == VI_DELETE_PREV_CHAR &&
532
616
el->el_chared.c_redo.pos != el->el_chared.c_redo.buf
533
&& el_isprint((unsigned char)el->el_chared.c_redo.pos[-1]))
617
&& Isprint(el->el_chared.c_redo.pos[-1]))
534
618
el->el_chared.c_redo.pos--;
536
620
*el->el_chared.c_redo.pos++ = ch;
584
668
case CC_NEWLINE: /* normal end of line */
585
num = el->el_line.lastchar - el->el_line.buffer;
669
num = (int)(el->el_line.lastchar - el->el_line.buffer);
588
672
case CC_FATAL: /* fatal error, reset to known state */
593
677
/* put (real) cursor in a known place */
594
678
re_clear_display(el); /* reset the display stuff */
595
679
ch_reset(el, 1); /* reset the input pointers */
596
re_refresh(el); /* print the prompt again */
680
re_refresh(el); /* print the prompt again */
616
term__flush(el); /* flush any buffered output */
700
terminal__flush(el); /* flush any buffered output */
617
701
/* make sure the tty is set up correctly */
618
702
if ((el->el_flags & UNBUFFERED) == 0) {
704
*nread = num != -1 ? num : 0;
624
*nread = el->el_line.lastchar - el->el_line.buffer;
706
*nread = (int)(el->el_line.lastchar - el->el_line.buffer);
626
return (num ? el->el_line.buffer : NULL);
710
el->el_line.cursor = el->el_line.lastchar = cp;
712
*nread = (int)(el->el_line.cursor - el->el_line.buffer);
717
errno = el->el_errno;
721
return el->el_line.buffer;