36 #if !defined(lint) && !defined(SCCSID)
38 static char sccsid[] =
"@(#)read.c 8.1 (Berkeley) 6/4/93";
56 private int read__fixio(
int,
int);
57 private int read_preread(
EditLine *);
58 private int read_char(
EditLine *, Char *);
59 private int read_getcmd(
EditLine *, el_action_t *, Char *);
69 el->el_read.read_char = read_char;
79 el_read_setfn(
EditLine *el, el_rfunc_t rc)
81 el->el_read.read_char = (rc == EL_BUILTIN_GETCFN) ? read_char : rc;
93 return el->el_read.read_char == read_char ?
94 EL_BUILTIN_GETCFN : el->el_read.read_char;
99 #define MIN(A,B) ((A) < (B) ? (A) : (B))
107 if (el->el_line.cursor > el->el_line.lastchar)
108 (void) fprintf(el->el_errfile,
"cursor > lastchar\r\n");
109 if (el->el_line.cursor < el->el_line.buffer)
110 (void) fprintf(el->el_errfile,
"cursor < buffer\r\n");
111 if (el->el_line.cursor > el->el_line.limit)
112 (void) fprintf(el->el_errfile,
"cursor > limit\r\n");
113 if (el->el_line.lastchar > el->el_line.limit)
114 (void) fprintf(el->el_errfile,
"lastchar > limit\r\n");
115 if (el->el_line.limit != &el->el_line.buffer[EL_BUFSIZ - 2])
116 (void) fprintf(el->el_errfile,
"limit != &buffer[EL_BUFSIZ-2]\r\n");
126 read__fixio(
int fd __attribute__((__unused__)),
int e)
139 #if defined(POSIX) && defined(EAGAIN)
140 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
150 #if defined(F_SETFL) && defined(O_NDELAY)
151 if ((e = fcntl(fd, F_GETFL, 0)) == -1)
154 if (fcntl(fd, F_SETFL, e & ~O_NDELAY) == -1)
164 if (ioctl(fd, FIONBIO, &zero) == -1)
191 if (el->el_tty.t_mode == ED_IO)
198 (void) ioctl(el->el_infd, FIONREAD, &chrs);
202 chrs = read(el->el_infd, buf,
203 (
size_t) MIN(chrs, EL_BUFSIZ - 1));
219 FUN(el,push)(
EditLine *el,
const Char *str)
223 if (str != NULL && ma->level + 1 < EL_MAXMACRO) {
225 if ((ma->macro[ma->level] = Strdup(str)) != NULL)
239 read_getcmd(
EditLine *el, el_action_t *cmdnum, Char *ch)
246 if ((num = FUN(el,getc)(el, ch)) != 1) {
247 el->el_errno = num == 0 ? 0 : errno;
253 el->el_state.metanext = 0;
259 if (el->el_state.metanext) {
260 el->el_state.metanext = 0;
268 cmd = el->el_map.current[(
unsigned char) *ch];
269 if (cmd == ED_SEQUENCE_LEAD_IN) {
271 switch (keymacro_get(el, ch, &val)) {
276 FUN(el,push)(el, val.str);
285 EL_ABORT((el->el_errfile,
"Bad XK_ type \n"));
289 if (el->el_map.alt == NULL)
290 el->el_map.current = el->el_map.key;
291 }
while (cmd == ED_SEQUENCE_LEAD_IN);
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);
326 if (!tried && read__fixio(el->el_infd, errno) == 0)
336 if (cbp > (
size_t) MB_CUR_MAX) {
343 if ((bytes = mbrtowc(cp, cbuf, cbp, &state)) == -2)
349 else if (bytes == -1)
353 memset(&state, 0,
sizeof(mbstate_t));
358 *cp = (
unsigned char)cbuf[0];
362 if ((el->el_flags & IGNORE_EXTCHARS) && bytes > 1) {
368 return (
int)num_read;
379 el_free(ma->macro[0]);
380 for (i = 0; i < ma->level; i++)
381 ma->macro[i] = ma->macro[i + 1];
390 FUN(el,getc)(
EditLine *el, Char *cp)
398 if (!read_preread(el))
405 if (ma->macro[0][ma->offset] ==
'\0') {
410 *cp = ma->macro[0][ma->offset++];
412 if (ma->macro[0][ma->offset] ==
'\0') {
421 (void) fprintf(el->el_errfile,
"Turning raw mode on\n");
423 if (tty_rawmode(el) < 0)
427 (void) fprintf(el->el_errfile,
"Reading a character\n");
429 num_read = (*el->el_read.read_char)(el, cp);
431 if (el->el_flags & NARROW_READ)
432 *cp = *(
char *)(
void *)cp;
435 (void) fprintf(el->el_errfile,
"Got it %c\n", *cp);
443 if (el->el_flags & HANDLE_SIGNALS)
445 if (el->el_flags & NO_TTY)
447 if ((el->el_flags & (UNBUFFERED|EDIT_DISABLED)) == UNBUFFERED)
453 re_clear_display(el);
457 if (el->el_flags & UNBUFFERED)
464 if ((el->el_flags & UNBUFFERED) == 0)
465 (void) tty_cookedmode(el);
466 if (el->el_flags & HANDLE_SIGNALS)
471 FUN(el,gets)(
EditLine *el,
int *nread)
474 el_action_t cmdnum = 0;
487 if (el->el_flags & NO_TTY) {
490 cp = el->el_line.buffer;
491 while ((num = (*el->el_read.read_char)(el, cp)) == 1) {
493 if (cp + 1 >= el->el_line.limit) {
494 idx = (size_t)(cp - el->el_line.buffer);
495 if (!ch_enlargebufs(el, (
size_t)2))
497 cp = &el->el_line.buffer[idx];
500 if (el->el_flags & UNBUFFERED)
502 if (cp[-1] ==
'\r' || cp[-1] ==
'\n')
507 cp = el->el_line.buffer;
508 el->el_errno = errno;
516 if (el->el_tty.t_mode == EX_IO && ma->level < 0) {
519 (void) ioctl(el->el_infd, FIONREAD, &chrs);
521 if (tty_rawmode(el) < 0) {
530 if ((el->el_flags & UNBUFFERED) == 0)
533 if (el->el_flags & EDIT_DISABLED) {
536 if ((el->el_flags & UNBUFFERED) == 0)
537 cp = el->el_line.buffer;
539 cp = el->el_line.lastchar;
543 while ((num = (*el->el_read.read_char)(el, cp)) == 1) {
545 if (cp + 1 >= el->el_line.limit) {
546 idx = (size_t)(cp - el->el_line.buffer);
547 if (!ch_enlargebufs(el, (
size_t)2))
549 cp = &el->el_line.buffer[idx];
552 crlf = cp[-1] ==
'\r' || cp[-1] ==
'\n';
553 if (el->el_flags & UNBUFFERED)
561 cp = el->el_line.buffer;
562 el->el_errno = errno;
568 for (num = OKCMD; num == OKCMD;) {
574 if ((num = read_getcmd(el, &cmdnum, &ch)) != OKCMD) {
576 (void) fprintf(el->el_errfile,
577 "Returning from el_gets %d\n", num);
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;
587 if ((
unsigned int)cmdnum >= (
unsigned int)el->el_map.nfunc) {
589 (void) fprintf(el->el_errfile,
590 "ERROR: illegal command from key 0%o\r\n", ch);
598 for (b = el->el_map.help; b->name; b++)
599 if (b->func == cmdnum)
602 (void) fprintf(el->el_errfile,
603 "Executing %s\n", b->name);
605 (
void) fprintf(el->el_errfile,
606 "Error command = %d\n", cmdnum);
610 el->el_state.thiscmd = cmdnum;
611 el->el_state.thisch = ch;
612 if (el->el_map.type == MAP_VI &&
613 el->el_map.current == el->el_map.key &&
614 el->el_chared.c_redo.pos < el->el_chared.c_redo.lim) {
615 if (cmdnum == VI_DELETE_PREV_CHAR &&
616 el->el_chared.c_redo.pos != el->el_chared.c_redo.buf
617 && Isprint(el->el_chared.c_redo.pos[-1]))
618 el->el_chared.c_redo.pos--;
620 *el->el_chared.c_redo.pos++ = ch;
622 retval = (*el->el_map.func[cmdnum]) (el, ch);
624 (void) fprintf(el->el_errfile,
625 "Returned state %d\n", retval );
629 el->el_state.lastcmd = cmdnum;
634 re_refresh_cursor(el);
639 re_clear_display(el);
646 case CC_REFRESH_BEEP:
659 if ((el->el_flags & UNBUFFERED) == 0)
661 else if (num == -1) {
662 *el->el_line.lastchar++ = CONTROL(
'd');
663 el->el_line.cursor = el->el_line.lastchar;
669 num = (int)(el->el_line.lastchar - el->el_line.buffer);
674 (void) fprintf(el->el_errfile,
675 "*** editor fatal ERROR ***\r\n\n");
678 re_clear_display(el);
686 (void) fprintf(el->el_errfile,
687 "*** editor ERROR ***\r\n\n");
693 el->el_state.argument = 1;
694 el->el_state.doingarg = 0;
695 el->el_chared.c_vcmd.action = NOP;
696 if (el->el_flags & UNBUFFERED)
702 if ((el->el_flags & UNBUFFERED) == 0) {
704 *nread = num != -1 ? num : 0;
706 *nread = (int)(el->el_line.lastchar - el->el_line.buffer);
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;