2011-08-16 20:25:15 +04:00
|
|
|
/* $NetBSD: read.c,v 1.67 2011/08/16 16:25:15 christos Exp $ */
|
1997-01-11 09:47:47 +03:00
|
|
|
|
1994-05-06 10:01:42 +04:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1992, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Christos Zoulas of Cornell University.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 20:42:00 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-05-06 10:01:42 +04:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2002-03-18 19:00:50 +03:00
|
|
|
#include "config.h"
|
1994-05-06 10:01:42 +04:00
|
|
|
#if !defined(lint) && !defined(SCCSID)
|
1997-01-11 09:47:47 +03:00
|
|
|
#if 0
|
1994-05-06 10:01:42 +04:00
|
|
|
static char sccsid[] = "@(#)read.c 8.1 (Berkeley) 6/4/93";
|
1997-01-11 09:47:47 +03:00
|
|
|
#else
|
2011-08-16 20:25:15 +04:00
|
|
|
__RCSID("$NetBSD: read.c,v 1.67 2011/08/16 16:25:15 christos Exp $");
|
1997-01-11 09:47:47 +03:00
|
|
|
#endif
|
1994-05-06 10:01:42 +04:00
|
|
|
#endif /* not lint && not SCCSID */
|
1997-01-11 09:47:47 +03:00
|
|
|
|
1994-05-06 10:01:42 +04:00
|
|
|
/*
|
|
|
|
* read.c: Clean this junk up! This is horrible code.
|
|
|
|
* Terminal read functions
|
|
|
|
*/
|
1999-01-12 01:39:59 +03:00
|
|
|
#include <errno.h>
|
2003-09-13 08:18:00 +04:00
|
|
|
#include <fcntl.h>
|
1994-05-06 10:01:42 +04:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
2009-12-31 01:37:40 +03:00
|
|
|
#include <limits.h>
|
1994-05-06 10:01:42 +04:00
|
|
|
#include "el.h"
|
|
|
|
|
2011-07-30 03:44:44 +04:00
|
|
|
#define OKCMD -1 /* must be -1! */
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
private int read__fixio(int, int);
|
|
|
|
private int read_preread(EditLine *);
|
2009-12-31 01:37:40 +03:00
|
|
|
private int read_char(EditLine *, Char *);
|
|
|
|
private int read_getcmd(EditLine *, el_action_t *, Char *);
|
2007-03-02 00:41:45 +03:00
|
|
|
private void read_pop(c_macro_t *);
|
2001-09-27 23:29:50 +04:00
|
|
|
|
|
|
|
/* read_init():
|
|
|
|
* Initialize the read stuff
|
|
|
|
*/
|
|
|
|
protected int
|
|
|
|
read_init(EditLine *el)
|
|
|
|
{
|
|
|
|
/* builtin read_char */
|
|
|
|
el->el_read.read_char = read_char;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* el_read_setfn():
|
|
|
|
* Set the read char function to the one provided.
|
|
|
|
* If it is set to EL_BUILTIN_GETCFN, then reset to the builtin one.
|
|
|
|
*/
|
|
|
|
protected int
|
|
|
|
el_read_setfn(EditLine *el, el_rfunc_t rc)
|
|
|
|
{
|
|
|
|
el->el_read.read_char = (rc == EL_BUILTIN_GETCFN) ? read_char : rc;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* el_read_getfn():
|
|
|
|
* return the current read char function, or EL_BUILTIN_GETCFN
|
|
|
|
* if it is the default one
|
|
|
|
*/
|
|
|
|
protected el_rfunc_t
|
|
|
|
el_read_getfn(EditLine *el)
|
|
|
|
{
|
2011-07-29 19:16:33 +04:00
|
|
|
return el->el_read.read_char == read_char ?
|
2001-09-27 23:29:50 +04:00
|
|
|
EL_BUILTIN_GETCFN : el->el_read.read_char;
|
|
|
|
}
|
|
|
|
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2003-06-19 19:55:05 +04:00
|
|
|
#ifndef MIN
|
|
|
|
#define MIN(A,B) ((A) < (B) ? (A) : (B))
|
|
|
|
#endif
|
|
|
|
|
1994-05-06 10:01:42 +04:00
|
|
|
#ifdef DEBUG_EDIT
|
|
|
|
private void
|
2000-09-05 02:06:28 +04:00
|
|
|
read_debug(EditLine *el)
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
if (el->el_line.cursor > el->el_line.lastchar)
|
|
|
|
(void) fprintf(el->el_errfile, "cursor > lastchar\r\n");
|
|
|
|
if (el->el_line.cursor < el->el_line.buffer)
|
|
|
|
(void) fprintf(el->el_errfile, "cursor < buffer\r\n");
|
|
|
|
if (el->el_line.cursor > el->el_line.limit)
|
|
|
|
(void) fprintf(el->el_errfile, "cursor > limit\r\n");
|
|
|
|
if (el->el_line.lastchar > el->el_line.limit)
|
|
|
|
(void) fprintf(el->el_errfile, "lastchar > limit\r\n");
|
|
|
|
if (el->el_line.limit != &el->el_line.buffer[EL_BUFSIZ - 2])
|
|
|
|
(void) fprintf(el->el_errfile, "limit != &buffer[EL_BUFSIZ-2]\r\n");
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
#endif /* DEBUG_EDIT */
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
|
1994-05-06 10:01:42 +04:00
|
|
|
/* read__fixio():
|
|
|
|
* Try to recover from a read error
|
|
|
|
*/
|
1998-12-12 23:08:21 +03:00
|
|
|
/* ARGSUSED */
|
1994-05-06 10:01:42 +04:00
|
|
|
private int
|
2003-06-19 19:55:05 +04:00
|
|
|
read__fixio(int fd __attribute__((__unused__)), int e)
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
|
|
|
|
switch (e) {
|
|
|
|
case -1: /* Make sure that the code is reachable */
|
1994-05-06 10:01:42 +04:00
|
|
|
|
|
|
|
#ifdef EWOULDBLOCK
|
2000-09-05 02:06:28 +04:00
|
|
|
case EWOULDBLOCK:
|
|
|
|
#ifndef TRY_AGAIN
|
2011-07-30 03:44:44 +04:00
|
|
|
#define TRY_AGAIN
|
2000-09-05 02:06:28 +04:00
|
|
|
#endif
|
1994-05-06 10:01:42 +04:00
|
|
|
#endif /* EWOULDBLOCK */
|
|
|
|
|
|
|
|
#if defined(POSIX) && defined(EAGAIN)
|
2000-09-05 02:06:28 +04:00
|
|
|
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
|
|
|
|
case EAGAIN:
|
|
|
|
#ifndef TRY_AGAIN
|
2011-07-30 03:44:44 +04:00
|
|
|
#define TRY_AGAIN
|
2000-09-05 02:06:28 +04:00
|
|
|
#endif
|
|
|
|
#endif /* EWOULDBLOCK && EWOULDBLOCK != EAGAIN */
|
1994-05-06 10:01:42 +04:00
|
|
|
#endif /* POSIX && EAGAIN */
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
e = 0;
|
1994-05-06 10:01:42 +04:00
|
|
|
#ifdef TRY_AGAIN
|
2000-09-05 02:06:28 +04:00
|
|
|
#if defined(F_SETFL) && defined(O_NDELAY)
|
|
|
|
if ((e = fcntl(fd, F_GETFL, 0)) == -1)
|
2011-07-29 19:16:33 +04:00
|
|
|
return -1;
|
2000-09-05 02:06:28 +04:00
|
|
|
|
|
|
|
if (fcntl(fd, F_SETFL, e & ~O_NDELAY) == -1)
|
2011-07-29 19:16:33 +04:00
|
|
|
return -1;
|
1997-07-06 22:25:21 +04:00
|
|
|
else
|
2000-09-05 02:06:28 +04:00
|
|
|
e = 1;
|
|
|
|
#endif /* F_SETFL && O_NDELAY */
|
|
|
|
|
|
|
|
#ifdef FIONBIO
|
|
|
|
{
|
|
|
|
int zero = 0;
|
|
|
|
|
2011-07-29 00:50:55 +04:00
|
|
|
if (ioctl(fd, FIONBIO, &zero) == -1)
|
2011-07-29 19:16:33 +04:00
|
|
|
return -1;
|
2000-09-05 02:06:28 +04:00
|
|
|
else
|
|
|
|
e = 1;
|
|
|
|
}
|
|
|
|
#endif /* FIONBIO */
|
1994-05-06 10:01:42 +04:00
|
|
|
|
|
|
|
#endif /* TRY_AGAIN */
|
2011-07-29 19:16:33 +04:00
|
|
|
return e ? 0 : -1;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
case EINTR:
|
2011-07-29 19:16:33 +04:00
|
|
|
return 0;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
default:
|
2011-07-29 19:16:33 +04:00
|
|
|
return -1;
|
2000-09-05 02:06:28 +04:00
|
|
|
}
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* read_preread():
|
|
|
|
* Try to read the stuff in the input queue;
|
|
|
|
*/
|
|
|
|
private int
|
2000-09-05 02:06:28 +04:00
|
|
|
read_preread(EditLine *el)
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
int chrs = 0;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
if (el->el_tty.t_mode == ED_IO)
|
2011-07-29 19:16:33 +04:00
|
|
|
return 0;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2009-12-31 01:37:40 +03:00
|
|
|
#ifndef WIDECHAR
|
|
|
|
/* FIONREAD attempts to buffer up multiple bytes, and to make that work
|
|
|
|
* properly with partial wide/UTF-8 characters would need some careful work. */
|
1994-05-06 10:01:42 +04:00
|
|
|
#ifdef FIONREAD
|
2011-07-29 00:50:55 +04:00
|
|
|
(void) ioctl(el->el_infd, FIONREAD, &chrs);
|
1994-05-06 10:01:42 +04:00
|
|
|
if (chrs > 0) {
|
2000-09-05 02:06:28 +04:00
|
|
|
char buf[EL_BUFSIZ];
|
|
|
|
|
|
|
|
chrs = read(el->el_infd, buf,
|
|
|
|
(size_t) MIN(chrs, EL_BUFSIZ - 1));
|
|
|
|
if (chrs > 0) {
|
|
|
|
buf[chrs] = '\0';
|
2003-10-19 03:48:42 +04:00
|
|
|
el_push(el, buf);
|
2000-09-05 02:06:28 +04:00
|
|
|
}
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
2000-09-05 02:06:28 +04:00
|
|
|
#endif /* FIONREAD */
|
2009-12-31 01:37:40 +03:00
|
|
|
#endif
|
2011-07-29 19:16:33 +04:00
|
|
|
return chrs > 0;
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* el_push():
|
|
|
|
* Push a macro
|
|
|
|
*/
|
|
|
|
public void
|
2009-12-31 01:37:40 +03:00
|
|
|
FUN(el,push)(EditLine *el, const Char *str)
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
c_macro_t *ma = &el->el_chared.c_macro;
|
|
|
|
|
|
|
|
if (str != NULL && ma->level + 1 < EL_MAXMACRO) {
|
|
|
|
ma->level++;
|
2009-12-31 01:37:40 +03:00
|
|
|
if ((ma->macro[ma->level] = Strdup(str)) != NULL)
|
2003-10-19 03:48:42 +04:00
|
|
|
return;
|
|
|
|
ma->level--;
|
2000-09-05 02:06:28 +04:00
|
|
|
}
|
2011-07-28 04:44:35 +04:00
|
|
|
terminal_beep(el);
|
|
|
|
terminal__flush(el);
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* read_getcmd():
|
|
|
|
* Return next command from the input stream.
|
2009-12-31 01:37:40 +03:00
|
|
|
* Character values > 255 are not looked up in the map, but inserted.
|
1994-05-06 10:01:42 +04:00
|
|
|
*/
|
|
|
|
private int
|
2009-12-31 01:37:40 +03:00
|
|
|
read_getcmd(EditLine *el, el_action_t *cmdnum, Char *ch)
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2002-11-15 17:32:32 +03:00
|
|
|
el_action_t cmd;
|
2000-09-05 02:06:28 +04:00
|
|
|
int num;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2009-02-16 00:24:13 +03:00
|
|
|
el->el_errno = 0;
|
2002-11-15 17:32:32 +03:00
|
|
|
do {
|
2009-12-31 01:37:40 +03:00
|
|
|
if ((num = FUN(el,getc)(el, ch)) != 1) {/* if EOF or error */
|
2009-03-10 23:46:15 +03:00
|
|
|
el->el_errno = num == 0 ? 0 : errno;
|
2011-07-29 19:16:33 +04:00
|
|
|
return num;
|
2009-02-16 00:24:13 +03:00
|
|
|
}
|
1994-05-06 10:01:42 +04:00
|
|
|
|
|
|
|
#ifdef KANJI
|
2000-09-05 02:06:28 +04:00
|
|
|
if ((*ch & 0200)) {
|
|
|
|
el->el_state.metanext = 0;
|
|
|
|
cmd = CcViMap[' '];
|
|
|
|
break;
|
|
|
|
} else
|
1994-05-06 10:01:42 +04:00
|
|
|
#endif /* KANJI */
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
if (el->el_state.metanext) {
|
|
|
|
el->el_state.metanext = 0;
|
|
|
|
*ch |= 0200;
|
|
|
|
}
|
2009-12-31 01:37:40 +03:00
|
|
|
#ifdef WIDECHAR
|
2011-07-30 03:44:44 +04:00
|
|
|
if (*ch >= N_KEYS)
|
|
|
|
cmd = ED_INSERT;
|
2009-12-31 01:37:40 +03:00
|
|
|
else
|
|
|
|
#endif
|
2011-07-30 03:44:44 +04:00
|
|
|
cmd = el->el_map.current[(unsigned char) *ch];
|
2000-09-05 02:06:28 +04:00
|
|
|
if (cmd == ED_SEQUENCE_LEAD_IN) {
|
2011-07-28 05:56:26 +04:00
|
|
|
keymacro_value_t val;
|
|
|
|
switch (keymacro_get(el, ch, &val)) {
|
2000-09-05 02:06:28 +04:00
|
|
|
case XK_CMD:
|
|
|
|
cmd = val.cmd;
|
|
|
|
break;
|
|
|
|
case XK_STR:
|
2009-12-31 01:37:40 +03:00
|
|
|
FUN(el,push)(el, val.str);
|
2000-09-05 02:06:28 +04:00
|
|
|
break;
|
1994-05-06 10:01:42 +04:00
|
|
|
#ifdef notyet
|
2000-09-05 02:06:28 +04:00
|
|
|
case XK_EXE:
|
|
|
|
/* XXX: In the future to run a user function */
|
|
|
|
RunCommand(val.str);
|
|
|
|
break;
|
1994-05-06 10:01:42 +04:00
|
|
|
#endif
|
2000-09-05 02:06:28 +04:00
|
|
|
default:
|
2000-11-12 01:18:57 +03:00
|
|
|
EL_ABORT((el->el_errfile, "Bad XK_ type \n"));
|
2000-09-05 02:06:28 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (el->el_map.alt == NULL)
|
|
|
|
el->el_map.current = el->el_map.key;
|
2002-11-15 17:32:32 +03:00
|
|
|
} while (cmd == ED_SEQUENCE_LEAD_IN);
|
2000-09-05 02:06:28 +04:00
|
|
|
*cmdnum = cmd;
|
2011-07-29 19:16:33 +04:00
|
|
|
return OKCMD;
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
2009-12-31 01:37:40 +03:00
|
|
|
#ifdef WIDECHAR
|
|
|
|
/* utf8_islead():
|
2011-07-30 03:44:44 +04:00
|
|
|
* Test whether a byte is a leading byte of a UTF-8 sequence.
|
2009-12-31 01:37:40 +03:00
|
|
|
*/
|
|
|
|
private int
|
2011-07-30 03:44:44 +04:00
|
|
|
utf8_islead(int c)
|
2009-12-31 01:37:40 +03:00
|
|
|
{
|
2011-07-30 03:44:44 +04:00
|
|
|
return c < 0x80 || /* single byte char */
|
|
|
|
(c >= 0xc2 && c <= 0xf4); /* start of multibyte sequence */
|
2009-12-31 01:37:40 +03:00
|
|
|
}
|
|
|
|
#endif
|
2000-09-05 02:06:28 +04:00
|
|
|
|
1997-10-26 23:19:48 +03:00
|
|
|
/* read_char():
|
|
|
|
* Read a character from the tty.
|
|
|
|
*/
|
|
|
|
private int
|
2009-12-31 01:37:40 +03:00
|
|
|
read_char(EditLine *el, Char *cp)
|
1997-10-26 23:19:48 +03:00
|
|
|
{
|
2009-02-16 00:55:23 +03:00
|
|
|
ssize_t num_read;
|
2000-09-05 02:06:28 +04:00
|
|
|
int tried = 0;
|
2011-07-30 03:44:44 +04:00
|
|
|
char cbuf[MB_LEN_MAX];
|
|
|
|
size_t cbp = 0;
|
|
|
|
int bytes = 0;
|
2000-09-05 02:06:28 +04:00
|
|
|
|
2009-02-19 18:20:22 +03:00
|
|
|
again:
|
|
|
|
el->el_signal->sig_no = 0;
|
2011-07-30 03:44:44 +04:00
|
|
|
while ((num_read = read(el->el_infd, cbuf + cbp, (size_t)1)) == -1) {
|
2010-07-19 21:18:13 +04:00
|
|
|
switch (el->el_signal->sig_no) {
|
|
|
|
case SIGCONT:
|
2011-07-28 04:44:35 +04:00
|
|
|
FUN(el,set)(el, EL_REFRESH);
|
2010-07-21 22:18:52 +04:00
|
|
|
/*FALLTHROUGH*/
|
2010-07-19 21:18:13 +04:00
|
|
|
case SIGWINCH:
|
2009-02-19 18:20:22 +03:00
|
|
|
sig_set(el);
|
|
|
|
goto again;
|
2010-07-19 21:18:13 +04:00
|
|
|
default:
|
|
|
|
break;
|
2009-02-19 18:20:22 +03:00
|
|
|
}
|
2000-09-05 02:06:28 +04:00
|
|
|
if (!tried && read__fixio(el->el_infd, errno) == 0)
|
|
|
|
tried = 1;
|
|
|
|
else {
|
|
|
|
*cp = '\0';
|
2011-07-29 19:16:33 +04:00
|
|
|
return -1;
|
2000-09-05 02:06:28 +04:00
|
|
|
}
|
2009-02-19 18:20:22 +03:00
|
|
|
}
|
2009-12-31 01:37:40 +03:00
|
|
|
|
|
|
|
#ifdef WIDECHAR
|
|
|
|
if (el->el_flags & CHARSET_IS_UTF8) {
|
|
|
|
if (!utf8_islead((unsigned char)cbuf[0]))
|
|
|
|
goto again; /* discard the byte we read and try again */
|
|
|
|
++cbp;
|
|
|
|
if ((bytes = ct_mbtowc(cp, cbuf, cbp)) == -1) {
|
|
|
|
ct_mbtowc_reset;
|
|
|
|
if (cbp >= MB_LEN_MAX) { /* "shouldn't happen" */
|
|
|
|
*cp = '\0';
|
2011-07-29 19:16:33 +04:00
|
|
|
return -1;
|
2009-12-31 01:37:40 +03:00
|
|
|
}
|
|
|
|
goto again;
|
|
|
|
}
|
2011-07-10 03:54:39 +04:00
|
|
|
} else if (isascii((unsigned char)cbuf[0]) ||
|
2011-07-08 19:54:56 +04:00
|
|
|
/* we don't support other multibyte charsets */
|
|
|
|
++cbp != 1 ||
|
|
|
|
/* Try non-ASCII characters in a 8-bit character set */
|
|
|
|
(bytes = ct_mbtowc(cp, cbuf, cbp)) != 1)
|
2009-12-31 01:37:40 +03:00
|
|
|
#endif
|
|
|
|
*cp = (unsigned char)cbuf[0];
|
|
|
|
|
|
|
|
if ((el->el_flags & IGNORE_EXTCHARS) && bytes > 1) {
|
|
|
|
cbp = 0; /* skip this character */
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
2009-02-16 00:55:23 +03:00
|
|
|
return (int)num_read;
|
1997-10-26 23:19:48 +03:00
|
|
|
}
|
|
|
|
|
2007-03-02 00:41:45 +03:00
|
|
|
/* read_pop():
|
|
|
|
* Pop a macro from the stack
|
|
|
|
*/
|
|
|
|
private void
|
|
|
|
read_pop(c_macro_t *ma)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
el_free(ma->macro[0]);
|
2009-06-08 19:10:53 +04:00
|
|
|
for (i = 0; i < ma->level; i++)
|
|
|
|
ma->macro[i] = ma->macro[i + 1];
|
2009-06-09 17:04:33 +04:00
|
|
|
ma->level--;
|
2007-03-02 00:41:45 +03:00
|
|
|
ma->offset = 0;
|
|
|
|
}
|
1994-05-06 10:01:42 +04:00
|
|
|
|
|
|
|
/* el_getc():
|
|
|
|
* Read a character
|
|
|
|
*/
|
|
|
|
public int
|
2009-12-31 01:37:40 +03:00
|
|
|
FUN(el,getc)(EditLine *el, Char *cp)
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
int num_read;
|
|
|
|
c_macro_t *ma = &el->el_chared.c_macro;
|
1999-07-02 19:14:07 +04:00
|
|
|
|
2011-07-28 04:44:35 +04:00
|
|
|
terminal__flush(el);
|
2000-09-05 02:06:28 +04:00
|
|
|
for (;;) {
|
|
|
|
if (ma->level < 0) {
|
|
|
|
if (!read_preread(el))
|
|
|
|
break;
|
|
|
|
}
|
2007-03-02 00:41:45 +03:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
if (ma->level < 0)
|
|
|
|
break;
|
|
|
|
|
2007-03-02 00:41:45 +03:00
|
|
|
if (ma->macro[0][ma->offset] == '\0') {
|
|
|
|
read_pop(ma);
|
2000-09-05 02:06:28 +04:00
|
|
|
continue;
|
|
|
|
}
|
2007-03-02 00:41:45 +03:00
|
|
|
|
2009-12-31 01:37:40 +03:00
|
|
|
*cp = ma->macro[0][ma->offset++];
|
2007-03-02 00:41:45 +03:00
|
|
|
|
|
|
|
if (ma->macro[0][ma->offset] == '\0') {
|
2003-10-19 03:48:42 +04:00
|
|
|
/* Needed for QuoteMode On */
|
2007-03-02 00:41:45 +03:00
|
|
|
read_pop(ma);
|
2000-09-05 02:06:28 +04:00
|
|
|
}
|
2007-03-02 00:41:45 +03:00
|
|
|
|
2011-07-29 19:16:33 +04:00
|
|
|
return 1;
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG_READ
|
2000-09-05 02:06:28 +04:00
|
|
|
(void) fprintf(el->el_errfile, "Turning raw mode on\n");
|
1994-05-06 10:01:42 +04:00
|
|
|
#endif /* DEBUG_READ */
|
2000-09-05 02:06:28 +04:00
|
|
|
if (tty_rawmode(el) < 0)/* make sure the tty is set up correctly */
|
2011-07-29 19:16:33 +04:00
|
|
|
return 0;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG_READ
|
2000-09-05 02:06:28 +04:00
|
|
|
(void) fprintf(el->el_errfile, "Reading a character\n");
|
1994-05-06 10:01:42 +04:00
|
|
|
#endif /* DEBUG_READ */
|
2001-09-27 23:29:50 +04:00
|
|
|
num_read = (*el->el_read.read_char)(el, cp);
|
2009-12-31 18:58:26 +03:00
|
|
|
#ifdef WIDECHAR
|
|
|
|
if (el->el_flags & NARROW_READ)
|
|
|
|
*cp = *(char *)(void *)cp;
|
|
|
|
#endif
|
1994-05-06 10:01:42 +04:00
|
|
|
#ifdef DEBUG_READ
|
2000-09-05 02:06:28 +04:00
|
|
|
(void) fprintf(el->el_errfile, "Got it %c\n", *cp);
|
1994-05-06 10:01:42 +04:00
|
|
|
#endif /* DEBUG_READ */
|
2011-07-29 19:16:33 +04:00
|
|
|
return num_read;
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
2003-09-26 21:44:51 +04:00
|
|
|
protected void
|
2004-02-27 17:52:18 +03:00
|
|
|
read_prepare(EditLine *el)
|
2003-09-26 21:44:51 +04:00
|
|
|
{
|
|
|
|
if (el->el_flags & HANDLE_SIGNALS)
|
|
|
|
sig_set(el);
|
|
|
|
if (el->el_flags & NO_TTY)
|
|
|
|
return;
|
|
|
|
if ((el->el_flags & (UNBUFFERED|EDIT_DISABLED)) == UNBUFFERED)
|
|
|
|
tty_rawmode(el);
|
|
|
|
|
|
|
|
/* This is relatively cheap, and things go terribly wrong if
|
|
|
|
we have the wrong size. */
|
|
|
|
el_resize(el);
|
|
|
|
re_clear_display(el); /* reset the display stuff */
|
2005-08-02 03:00:15 +04:00
|
|
|
ch_reset(el, 0);
|
2003-09-26 21:44:51 +04:00
|
|
|
re_refresh(el); /* print the prompt */
|
2005-03-10 02:55:02 +03:00
|
|
|
|
|
|
|
if (el->el_flags & UNBUFFERED)
|
2011-07-28 04:44:35 +04:00
|
|
|
terminal__flush(el);
|
2003-09-26 21:44:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
protected void
|
|
|
|
read_finish(EditLine *el)
|
|
|
|
{
|
|
|
|
if ((el->el_flags & UNBUFFERED) == 0)
|
|
|
|
(void) tty_cookedmode(el);
|
|
|
|
if (el->el_flags & HANDLE_SIGNALS)
|
|
|
|
sig_clr(el);
|
|
|
|
}
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2009-12-31 01:37:40 +03:00
|
|
|
public const Char *
|
|
|
|
FUN(el,gets)(EditLine *el, int *nread)
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
int retval;
|
|
|
|
el_action_t cmdnum = 0;
|
|
|
|
int num; /* how many chars we have read at NL */
|
2011-02-18 23:53:05 +03:00
|
|
|
Char ch, *cp;
|
2003-09-26 21:44:51 +04:00
|
|
|
int crlf = 0;
|
2009-03-10 23:46:15 +03:00
|
|
|
int nrb;
|
1998-01-21 13:12:22 +03:00
|
|
|
#ifdef FIONREAD
|
2000-09-05 02:06:28 +04:00
|
|
|
c_macro_t *ma = &el->el_chared.c_macro;
|
1998-01-21 13:12:22 +03:00
|
|
|
#endif /* FIONREAD */
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2009-03-10 23:46:15 +03:00
|
|
|
if (nread == NULL)
|
|
|
|
nread = &nrb;
|
2009-07-22 19:57:00 +04:00
|
|
|
*nread = 0;
|
2009-03-10 23:46:15 +03:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
if (el->el_flags & NO_TTY) {
|
2001-01-10 10:45:41 +03:00
|
|
|
size_t idx;
|
1997-10-26 23:19:48 +03:00
|
|
|
|
2011-02-18 23:53:05 +03:00
|
|
|
cp = el->el_line.buffer;
|
2009-02-16 00:24:13 +03:00
|
|
|
while ((num = (*el->el_read.read_char)(el, cp)) == 1) {
|
2001-01-10 10:45:41 +03:00
|
|
|
/* make sure there is space for next character */
|
|
|
|
if (cp + 1 >= el->el_line.limit) {
|
2011-08-16 20:25:15 +04:00
|
|
|
idx = (size_t)(cp - el->el_line.buffer);
|
2011-07-30 03:44:44 +04:00
|
|
|
if (!ch_enlargebufs(el, (size_t)2))
|
2001-01-10 10:45:41 +03:00
|
|
|
break;
|
|
|
|
cp = &el->el_line.buffer[idx];
|
2000-09-05 02:06:28 +04:00
|
|
|
}
|
2001-01-10 10:45:41 +03:00
|
|
|
cp++;
|
2003-09-26 21:44:51 +04:00
|
|
|
if (el->el_flags & UNBUFFERED)
|
|
|
|
break;
|
2000-09-05 02:06:28 +04:00
|
|
|
if (cp[-1] == '\r' || cp[-1] == '\n')
|
|
|
|
break;
|
1997-10-26 23:19:48 +03:00
|
|
|
}
|
2009-03-10 23:46:15 +03:00
|
|
|
if (num == -1) {
|
|
|
|
if (errno == EINTR)
|
|
|
|
cp = el->el_line.buffer;
|
|
|
|
el->el_errno = errno;
|
|
|
|
}
|
2001-01-10 10:45:41 +03:00
|
|
|
|
2011-02-18 23:53:05 +03:00
|
|
|
goto noedit;
|
1997-12-20 22:15:50 +03:00
|
|
|
}
|
2002-10-28 00:41:50 +03:00
|
|
|
|
1994-05-06 10:01:42 +04:00
|
|
|
|
|
|
|
#ifdef FIONREAD
|
2000-09-05 02:06:28 +04:00
|
|
|
if (el->el_tty.t_mode == EX_IO && ma->level < 0) {
|
|
|
|
long chrs = 0;
|
|
|
|
|
2011-07-29 00:50:55 +04:00
|
|
|
(void) ioctl(el->el_infd, FIONREAD, &chrs);
|
2000-09-05 02:06:28 +04:00
|
|
|
if (chrs == 0) {
|
|
|
|
if (tty_rawmode(el) < 0) {
|
2009-03-10 23:46:15 +03:00
|
|
|
errno = 0;
|
|
|
|
*nread = 0;
|
2011-07-29 19:16:33 +04:00
|
|
|
return NULL;
|
2000-09-05 02:06:28 +04:00
|
|
|
}
|
|
|
|
}
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
#endif /* FIONREAD */
|
|
|
|
|
2003-09-26 21:44:51 +04:00
|
|
|
if ((el->el_flags & UNBUFFERED) == 0)
|
|
|
|
read_prepare(el);
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
if (el->el_flags & EDIT_DISABLED) {
|
2001-01-10 10:45:41 +03:00
|
|
|
size_t idx;
|
2009-02-16 00:24:13 +03:00
|
|
|
|
2004-07-08 04:51:36 +04:00
|
|
|
if ((el->el_flags & UNBUFFERED) == 0)
|
|
|
|
cp = el->el_line.buffer;
|
|
|
|
else
|
|
|
|
cp = el->el_line.lastchar;
|
1999-08-02 05:01:55 +04:00
|
|
|
|
2011-07-28 04:44:35 +04:00
|
|
|
terminal__flush(el);
|
2000-09-05 02:06:28 +04:00
|
|
|
|
2009-02-16 00:24:13 +03:00
|
|
|
while ((num = (*el->el_read.read_char)(el, cp)) == 1) {
|
2001-01-10 10:45:41 +03:00
|
|
|
/* make sure there is space next character */
|
|
|
|
if (cp + 1 >= el->el_line.limit) {
|
2011-08-16 20:25:15 +04:00
|
|
|
idx = (size_t)(cp - el->el_line.buffer);
|
2011-07-30 03:44:44 +04:00
|
|
|
if (!ch_enlargebufs(el, (size_t)2))
|
2001-01-10 10:45:41 +03:00
|
|
|
break;
|
|
|
|
cp = &el->el_line.buffer[idx];
|
2000-09-05 02:06:28 +04:00
|
|
|
}
|
2001-01-10 10:45:41 +03:00
|
|
|
cp++;
|
2003-09-26 21:44:51 +04:00
|
|
|
crlf = cp[-1] == '\r' || cp[-1] == '\n';
|
|
|
|
if (el->el_flags & UNBUFFERED)
|
|
|
|
break;
|
|
|
|
if (crlf)
|
2000-09-05 02:06:28 +04:00
|
|
|
break;
|
1999-08-02 05:01:55 +04:00
|
|
|
}
|
2009-03-10 23:46:15 +03:00
|
|
|
|
|
|
|
if (num == -1) {
|
|
|
|
if (errno == EINTR)
|
|
|
|
cp = el->el_line.buffer;
|
|
|
|
el->el_errno = errno;
|
|
|
|
}
|
2001-01-10 10:45:41 +03:00
|
|
|
|
2011-02-18 23:53:05 +03:00
|
|
|
goto noedit;
|
1999-08-02 05:01:55 +04:00
|
|
|
}
|
2002-10-28 00:41:50 +03:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
for (num = OKCMD; num == OKCMD;) { /* while still editing this
|
|
|
|
* line */
|
1994-05-06 10:01:42 +04:00
|
|
|
#ifdef DEBUG_EDIT
|
2000-09-05 02:06:28 +04:00
|
|
|
read_debug(el);
|
1994-05-06 10:01:42 +04:00
|
|
|
#endif /* DEBUG_EDIT */
|
2000-09-05 02:06:28 +04:00
|
|
|
/* if EOF or error */
|
|
|
|
if ((num = read_getcmd(el, &cmdnum, &ch)) != OKCMD) {
|
1994-05-06 10:01:42 +04:00
|
|
|
#ifdef DEBUG_READ
|
2000-09-05 02:06:28 +04:00
|
|
|
(void) fprintf(el->el_errfile,
|
|
|
|
"Returning from el_gets %d\n", num);
|
1994-05-06 10:01:42 +04:00
|
|
|
#endif /* DEBUG_READ */
|
2000-09-05 02:06:28 +04:00
|
|
|
break;
|
|
|
|
}
|
2009-02-16 00:24:13 +03:00
|
|
|
if (el->el_errno == EINTR) {
|
|
|
|
el->el_line.buffer[0] = '\0';
|
|
|
|
el->el_line.lastchar =
|
|
|
|
el->el_line.cursor = el->el_line.buffer;
|
|
|
|
break;
|
|
|
|
}
|
2009-01-18 15:14:16 +03:00
|
|
|
if ((unsigned int)cmdnum >= (unsigned int)el->el_map.nfunc) { /* BUG CHECK command */
|
1994-05-06 10:01:42 +04:00
|
|
|
#ifdef DEBUG_EDIT
|
2000-09-05 02:06:28 +04:00
|
|
|
(void) fprintf(el->el_errfile,
|
|
|
|
"ERROR: illegal command from key 0%o\r\n", ch);
|
1994-05-06 10:01:42 +04:00
|
|
|
#endif /* DEBUG_EDIT */
|
2000-09-05 02:06:28 +04:00
|
|
|
continue; /* try again */
|
|
|
|
}
|
|
|
|
/* now do the real command */
|
1994-05-06 10:01:42 +04:00
|
|
|
#ifdef DEBUG_READ
|
2000-09-05 02:06:28 +04:00
|
|
|
{
|
|
|
|
el_bindings_t *b;
|
|
|
|
for (b = el->el_map.help; b->name; b++)
|
|
|
|
if (b->func == cmdnum)
|
|
|
|
break;
|
|
|
|
if (b->name)
|
|
|
|
(void) fprintf(el->el_errfile,
|
|
|
|
"Executing %s\n", b->name);
|
|
|
|
else
|
|
|
|
(void) fprintf(el->el_errfile,
|
|
|
|
"Error command = %d\n", cmdnum);
|
|
|
|
}
|
1994-05-06 10:01:42 +04:00
|
|
|
#endif /* DEBUG_READ */
|
2002-11-15 17:32:32 +03:00
|
|
|
/* vi redo needs these way down the levels... */
|
|
|
|
el->el_state.thiscmd = cmdnum;
|
|
|
|
el->el_state.thisch = ch;
|
|
|
|
if (el->el_map.type == MAP_VI &&
|
|
|
|
el->el_map.current == el->el_map.key &&
|
|
|
|
el->el_chared.c_redo.pos < el->el_chared.c_redo.lim) {
|
|
|
|
if (cmdnum == VI_DELETE_PREV_CHAR &&
|
|
|
|
el->el_chared.c_redo.pos != el->el_chared.c_redo.buf
|
2009-12-31 01:37:40 +03:00
|
|
|
&& Isprint(el->el_chared.c_redo.pos[-1]))
|
2002-11-15 17:32:32 +03:00
|
|
|
el->el_chared.c_redo.pos--;
|
|
|
|
else
|
|
|
|
*el->el_chared.c_redo.pos++ = ch;
|
|
|
|
}
|
2000-09-05 02:06:28 +04:00
|
|
|
retval = (*el->el_map.func[cmdnum]) (el, ch);
|
2002-10-28 00:41:50 +03:00
|
|
|
#ifdef DEBUG_READ
|
|
|
|
(void) fprintf(el->el_errfile,
|
|
|
|
"Returned state %d\n", retval );
|
|
|
|
#endif /* DEBUG_READ */
|
2000-09-05 02:06:28 +04:00
|
|
|
|
|
|
|
/* save the last command here */
|
|
|
|
el->el_state.lastcmd = cmdnum;
|
|
|
|
|
|
|
|
/* use any return value */
|
|
|
|
switch (retval) {
|
|
|
|
case CC_CURSOR:
|
|
|
|
re_refresh_cursor(el);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CC_REDISPLAY:
|
|
|
|
re_clear_lines(el);
|
|
|
|
re_clear_display(el);
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
|
|
|
|
case CC_REFRESH:
|
|
|
|
re_refresh(el);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CC_REFRESH_BEEP:
|
|
|
|
re_refresh(el);
|
2011-07-28 04:44:35 +04:00
|
|
|
terminal_beep(el);
|
2000-09-05 02:06:28 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CC_NORM: /* normal char */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CC_ARGHACK: /* Suggested by Rich Salz */
|
|
|
|
/* <rsalz@pineapple.bbn.com> */
|
2002-11-15 17:32:32 +03:00
|
|
|
continue; /* keep going... */
|
2000-09-05 02:06:28 +04:00
|
|
|
|
|
|
|
case CC_EOF: /* end of file typed */
|
2003-10-09 04:42:28 +04:00
|
|
|
if ((el->el_flags & UNBUFFERED) == 0)
|
|
|
|
num = 0;
|
|
|
|
else if (num == -1) {
|
2004-01-17 20:57:40 +03:00
|
|
|
*el->el_line.lastchar++ = CONTROL('d');
|
2003-10-09 04:42:28 +04:00
|
|
|
el->el_line.cursor = el->el_line.lastchar;
|
|
|
|
num = 1;
|
|
|
|
}
|
2000-09-05 02:06:28 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CC_NEWLINE: /* normal end of line */
|
2009-02-16 00:55:23 +03:00
|
|
|
num = (int)(el->el_line.lastchar - el->el_line.buffer);
|
2000-09-05 02:06:28 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CC_FATAL: /* fatal error, reset to known state */
|
1994-05-06 10:01:42 +04:00
|
|
|
#ifdef DEBUG_READ
|
2000-09-05 02:06:28 +04:00
|
|
|
(void) fprintf(el->el_errfile,
|
|
|
|
"*** editor fatal ERROR ***\r\n\n");
|
1994-05-06 10:01:42 +04:00
|
|
|
#endif /* DEBUG_READ */
|
2000-09-05 02:06:28 +04:00
|
|
|
/* put (real) cursor in a known place */
|
|
|
|
re_clear_display(el); /* reset the display stuff */
|
2005-08-02 16:11:14 +04:00
|
|
|
ch_reset(el, 1); /* reset the input pointers */
|
2011-07-30 03:44:44 +04:00
|
|
|
re_refresh(el); /* print the prompt again */
|
2000-09-05 02:06:28 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CC_ERROR:
|
|
|
|
default: /* functions we don't know about */
|
1994-05-06 10:01:42 +04:00
|
|
|
#ifdef DEBUG_READ
|
2000-09-05 02:06:28 +04:00
|
|
|
(void) fprintf(el->el_errfile,
|
|
|
|
"*** editor ERROR ***\r\n\n");
|
1994-05-06 10:01:42 +04:00
|
|
|
#endif /* DEBUG_READ */
|
2011-07-28 04:44:35 +04:00
|
|
|
terminal_beep(el);
|
|
|
|
terminal__flush(el);
|
2000-09-05 02:06:28 +04:00
|
|
|
break;
|
|
|
|
}
|
2002-11-15 17:32:32 +03:00
|
|
|
el->el_state.argument = 1;
|
|
|
|
el->el_state.doingarg = 0;
|
|
|
|
el->el_chared.c_vcmd.action = NOP;
|
2003-09-26 21:44:51 +04:00
|
|
|
if (el->el_flags & UNBUFFERED)
|
|
|
|
break;
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
2000-09-05 02:06:28 +04:00
|
|
|
|
2011-07-28 04:44:35 +04:00
|
|
|
terminal__flush(el); /* flush any buffered output */
|
2002-10-28 00:41:50 +03:00
|
|
|
/* make sure the tty is set up correctly */
|
2003-09-26 21:44:51 +04:00
|
|
|
if ((el->el_flags & UNBUFFERED) == 0) {
|
|
|
|
read_finish(el);
|
2009-03-10 23:46:15 +03:00
|
|
|
*nread = num != -1 ? num : 0;
|
2003-09-26 21:44:51 +04:00
|
|
|
} else {
|
2009-03-10 23:46:15 +03:00
|
|
|
*nread = (int)(el->el_line.lastchar - el->el_line.buffer);
|
2003-09-26 21:44:51 +04:00
|
|
|
}
|
2011-02-18 23:53:05 +03:00
|
|
|
goto done;
|
|
|
|
noedit:
|
|
|
|
el->el_line.cursor = el->el_line.lastchar = cp;
|
|
|
|
*cp = '\0';
|
|
|
|
*nread = (int)(el->el_line.cursor - el->el_line.buffer);
|
2009-03-10 23:46:15 +03:00
|
|
|
done:
|
|
|
|
if (*nread == 0) {
|
|
|
|
if (num == -1) {
|
|
|
|
*nread = -1;
|
|
|
|
errno = el->el_errno;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
} else
|
|
|
|
return el->el_line.buffer;
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|