Thu Jul 28 00:44:35 2011 UTC ()
term -> terminal
fix wide function confusion


(christos)
diff -r1.61 -r1.62 src/lib/libedit/read.c

cvs diff -r1.61 -r1.62 src/lib/libedit/read.c (switch to unified diff)

--- src/lib/libedit/read.c 2011/07/09 23:54:39 1.61
+++ src/lib/libedit/read.c 2011/07/28 00:44:35 1.62
@@ -1,722 +1,722 @@ @@ -1,722 +1,722 @@
1/* $NetBSD: read.c,v 1.61 2011/07/09 23:54:39 christos Exp $ */ 1/* $NetBSD: read.c,v 1.62 2011/07/28 00:44:35 christos Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 1992, 1993 4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to Berkeley by 7 * This code is derived from software contributed to Berkeley by
8 * Christos Zoulas of Cornell University. 8 * Christos Zoulas of Cornell University.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright 15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the 16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution. 17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors 18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software 19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission. 20 * without specific prior written permission.
21 * 21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE. 32 * SUCH DAMAGE.
33 */ 33 */
34 34
35#include "config.h" 35#include "config.h"
36#if !defined(lint) && !defined(SCCSID) 36#if !defined(lint) && !defined(SCCSID)
37#if 0 37#if 0
38static char sccsid[] = "@(#)read.c 8.1 (Berkeley) 6/4/93"; 38static char sccsid[] = "@(#)read.c 8.1 (Berkeley) 6/4/93";
39#else 39#else
40__RCSID("$NetBSD: read.c,v 1.61 2011/07/09 23:54:39 christos Exp $"); 40__RCSID("$NetBSD: read.c,v 1.62 2011/07/28 00:44:35 christos Exp $");
41#endif 41#endif
42#endif /* not lint && not SCCSID */ 42#endif /* not lint && not SCCSID */
43 43
44/* 44/*
45 * read.c: Clean this junk up! This is horrible code. 45 * read.c: Clean this junk up! This is horrible code.
46 * Terminal read functions 46 * Terminal read functions
47 */ 47 */
48#include <errno.h> 48#include <errno.h>
49#include <fcntl.h> 49#include <fcntl.h>
50#include <unistd.h> 50#include <unistd.h>
51#include <stdlib.h> 51#include <stdlib.h>
52#include <limits.h> 52#include <limits.h>
53#include "el.h" 53#include "el.h"
54 54
55#define OKCMD -1 /* must be -1! */ 55#define OKCMD -1 /* must be -1! */
56 56
57private int read__fixio(int, int); 57private int read__fixio(int, int);
58private int read_preread(EditLine *); 58private int read_preread(EditLine *);
59private int read_char(EditLine *, Char *); 59private int read_char(EditLine *, Char *);
60private int read_getcmd(EditLine *, el_action_t *, Char *); 60private int read_getcmd(EditLine *, el_action_t *, Char *);
61private void read_pop(c_macro_t *); 61private void read_pop(c_macro_t *);
62 62
63/* read_init(): 63/* read_init():
64 * Initialize the read stuff 64 * Initialize the read stuff
65 */ 65 */
66protected int 66protected int
67read_init(EditLine *el) 67read_init(EditLine *el)
68{ 68{
69 /* builtin read_char */ 69 /* builtin read_char */
70 el->el_read.read_char = read_char; 70 el->el_read.read_char = read_char;
71 return 0; 71 return 0;
72} 72}
73 73
74 74
75/* el_read_setfn(): 75/* el_read_setfn():
76 * Set the read char function to the one provided. 76 * Set the read char function to the one provided.
77 * If it is set to EL_BUILTIN_GETCFN, then reset to the builtin one. 77 * If it is set to EL_BUILTIN_GETCFN, then reset to the builtin one.
78 */ 78 */
79protected int 79protected int
80el_read_setfn(EditLine *el, el_rfunc_t rc) 80el_read_setfn(EditLine *el, el_rfunc_t rc)
81{ 81{
82 el->el_read.read_char = (rc == EL_BUILTIN_GETCFN) ? read_char : rc; 82 el->el_read.read_char = (rc == EL_BUILTIN_GETCFN) ? read_char : rc;
83 return 0; 83 return 0;
84} 84}
85 85
86 86
87/* el_read_getfn(): 87/* el_read_getfn():
88 * return the current read char function, or EL_BUILTIN_GETCFN 88 * return the current read char function, or EL_BUILTIN_GETCFN
89 * if it is the default one 89 * if it is the default one
90 */ 90 */
91protected el_rfunc_t 91protected el_rfunc_t
92el_read_getfn(EditLine *el) 92el_read_getfn(EditLine *el)
93{ 93{
94 return (el->el_read.read_char == read_char) ? 94 return (el->el_read.read_char == read_char) ?
95 EL_BUILTIN_GETCFN : el->el_read.read_char; 95 EL_BUILTIN_GETCFN : el->el_read.read_char;
96} 96}
97 97
98 98
99#ifndef MIN 99#ifndef MIN
100#define MIN(A,B) ((A) < (B) ? (A) : (B)) 100#define MIN(A,B) ((A) < (B) ? (A) : (B))
101#endif 101#endif
102 102
103#ifdef DEBUG_EDIT 103#ifdef DEBUG_EDIT
104private void 104private void
105read_debug(EditLine *el) 105read_debug(EditLine *el)
106{ 106{
107 107
108 if (el->el_line.cursor > el->el_line.lastchar) 108 if (el->el_line.cursor > el->el_line.lastchar)
109 (void) fprintf(el->el_errfile, "cursor > lastchar\r\n"); 109 (void) fprintf(el->el_errfile, "cursor > lastchar\r\n");
110 if (el->el_line.cursor < el->el_line.buffer) 110 if (el->el_line.cursor < el->el_line.buffer)
111 (void) fprintf(el->el_errfile, "cursor < buffer\r\n"); 111 (void) fprintf(el->el_errfile, "cursor < buffer\r\n");
112 if (el->el_line.cursor > el->el_line.limit) 112 if (el->el_line.cursor > el->el_line.limit)
113 (void) fprintf(el->el_errfile, "cursor > limit\r\n"); 113 (void) fprintf(el->el_errfile, "cursor > limit\r\n");
114 if (el->el_line.lastchar > el->el_line.limit) 114 if (el->el_line.lastchar > el->el_line.limit)
115 (void) fprintf(el->el_errfile, "lastchar > limit\r\n"); 115 (void) fprintf(el->el_errfile, "lastchar > limit\r\n");
116 if (el->el_line.limit != &el->el_line.buffer[EL_BUFSIZ - 2]) 116 if (el->el_line.limit != &el->el_line.buffer[EL_BUFSIZ - 2])
117 (void) fprintf(el->el_errfile, "limit != &buffer[EL_BUFSIZ-2]\r\n"); 117 (void) fprintf(el->el_errfile, "limit != &buffer[EL_BUFSIZ-2]\r\n");
118} 118}
119#endif /* DEBUG_EDIT */ 119#endif /* DEBUG_EDIT */
120 120
121 121
122/* read__fixio(): 122/* read__fixio():
123 * Try to recover from a read error 123 * Try to recover from a read error
124 */ 124 */
125/* ARGSUSED */ 125/* ARGSUSED */
126private int 126private int
127read__fixio(int fd __attribute__((__unused__)), int e) 127read__fixio(int fd __attribute__((__unused__)), int e)
128{ 128{
129 129
130 switch (e) { 130 switch (e) {
131 case -1: /* Make sure that the code is reachable */ 131 case -1: /* Make sure that the code is reachable */
132 132
133#ifdef EWOULDBLOCK 133#ifdef EWOULDBLOCK
134 case EWOULDBLOCK: 134 case EWOULDBLOCK:
135#ifndef TRY_AGAIN 135#ifndef TRY_AGAIN
136#define TRY_AGAIN 136#define TRY_AGAIN
137#endif 137#endif
138#endif /* EWOULDBLOCK */ 138#endif /* EWOULDBLOCK */
139 139
140#if defined(POSIX) && defined(EAGAIN) 140#if defined(POSIX) && defined(EAGAIN)
141#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN 141#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
142 case EAGAIN: 142 case EAGAIN:
143#ifndef TRY_AGAIN 143#ifndef TRY_AGAIN
144#define TRY_AGAIN 144#define TRY_AGAIN
145#endif 145#endif
146#endif /* EWOULDBLOCK && EWOULDBLOCK != EAGAIN */ 146#endif /* EWOULDBLOCK && EWOULDBLOCK != EAGAIN */
147#endif /* POSIX && EAGAIN */ 147#endif /* POSIX && EAGAIN */
148 148
149 e = 0; 149 e = 0;
150#ifdef TRY_AGAIN 150#ifdef TRY_AGAIN
151#if defined(F_SETFL) && defined(O_NDELAY) 151#if defined(F_SETFL) && defined(O_NDELAY)
152 if ((e = fcntl(fd, F_GETFL, 0)) == -1) 152 if ((e = fcntl(fd, F_GETFL, 0)) == -1)
153 return (-1); 153 return (-1);
154 154
155 if (fcntl(fd, F_SETFL, e & ~O_NDELAY) == -1) 155 if (fcntl(fd, F_SETFL, e & ~O_NDELAY) == -1)
156 return (-1); 156 return (-1);
157 else 157 else
158 e = 1; 158 e = 1;
159#endif /* F_SETFL && O_NDELAY */ 159#endif /* F_SETFL && O_NDELAY */
160 160
161#ifdef FIONBIO 161#ifdef FIONBIO
162 { 162 {
163 int zero = 0; 163 int zero = 0;
164 164
165 if (ioctl(fd, FIONBIO, (ioctl_t) & zero) == -1) 165 if (ioctl(fd, FIONBIO, (ioctl_t) & zero) == -1)
166 return (-1); 166 return (-1);
167 else 167 else
168 e = 1; 168 e = 1;
169 } 169 }
170#endif /* FIONBIO */ 170#endif /* FIONBIO */
171 171
172#endif /* TRY_AGAIN */ 172#endif /* TRY_AGAIN */
173 return (e ? 0 : -1); 173 return (e ? 0 : -1);
174 174
175 case EINTR: 175 case EINTR:
176 return (0); 176 return (0);
177 177
178 default: 178 default:
179 return (-1); 179 return (-1);
180 } 180 }
181} 181}
182 182
183 183
184/* read_preread(): 184/* read_preread():
185 * Try to read the stuff in the input queue; 185 * Try to read the stuff in the input queue;
186 */ 186 */
187private int 187private int
188read_preread(EditLine *el) 188read_preread(EditLine *el)
189{ 189{
190 int chrs = 0; 190 int chrs = 0;
191 191
192 if (el->el_tty.t_mode == ED_IO) 192 if (el->el_tty.t_mode == ED_IO)
193 return (0); 193 return (0);
194 194
195#ifndef WIDECHAR 195#ifndef WIDECHAR
196/* FIONREAD attempts to buffer up multiple bytes, and to make that work 196/* FIONREAD attempts to buffer up multiple bytes, and to make that work
197 * properly with partial wide/UTF-8 characters would need some careful work. */ 197 * properly with partial wide/UTF-8 characters would need some careful work. */
198#ifdef FIONREAD 198#ifdef FIONREAD
199 (void) ioctl(el->el_infd, FIONREAD, (ioctl_t) & chrs); 199 (void) ioctl(el->el_infd, FIONREAD, (ioctl_t) & chrs);
200 if (chrs > 0) { 200 if (chrs > 0) {
201 char buf[EL_BUFSIZ]; 201 char buf[EL_BUFSIZ];
202 202
203 chrs = read(el->el_infd, buf, 203 chrs = read(el->el_infd, buf,
204 (size_t) MIN(chrs, EL_BUFSIZ - 1)); 204 (size_t) MIN(chrs, EL_BUFSIZ - 1));
205 if (chrs > 0) { 205 if (chrs > 0) {
206 buf[chrs] = '\0'; 206 buf[chrs] = '\0';
207 el_push(el, buf); 207 el_push(el, buf);
208 } 208 }
209 } 209 }
210#endif /* FIONREAD */ 210#endif /* FIONREAD */
211#endif 211#endif
212 return (chrs > 0); 212 return (chrs > 0);
213} 213}
214 214
215 215
216/* el_push(): 216/* el_push():
217 * Push a macro 217 * Push a macro
218 */ 218 */
219public void 219public void
220FUN(el,push)(EditLine *el, const Char *str) 220FUN(el,push)(EditLine *el, const Char *str)
221{ 221{
222 c_macro_t *ma = &el->el_chared.c_macro; 222 c_macro_t *ma = &el->el_chared.c_macro;
223 223
224 if (str != NULL && ma->level + 1 < EL_MAXMACRO) { 224 if (str != NULL && ma->level + 1 < EL_MAXMACRO) {
225 ma->level++; 225 ma->level++;
226 if ((ma->macro[ma->level] = Strdup(str)) != NULL) 226 if ((ma->macro[ma->level] = Strdup(str)) != NULL)
227 return; 227 return;
228 ma->level--; 228 ma->level--;
229 } 229 }
230 term_beep(el); 230 terminal_beep(el);
231 term__flush(el); 231 terminal__flush(el);
232} 232}
233 233
234 234
235/* read_getcmd(): 235/* read_getcmd():
236 * Return next command from the input stream. 236 * Return next command from the input stream.
237 * Character values > 255 are not looked up in the map, but inserted. 237 * Character values > 255 are not looked up in the map, but inserted.
238 */ 238 */
239private int 239private int
240read_getcmd(EditLine *el, el_action_t *cmdnum, Char *ch) 240read_getcmd(EditLine *el, el_action_t *cmdnum, Char *ch)
241{ 241{
242 el_action_t cmd; 242 el_action_t cmd;
243 int num; 243 int num;
244 244
245 el->el_errno = 0; 245 el->el_errno = 0;
246 do { 246 do {
247 if ((num = FUN(el,getc)(el, ch)) != 1) {/* if EOF or error */ 247 if ((num = FUN(el,getc)(el, ch)) != 1) {/* if EOF or error */
248 el->el_errno = num == 0 ? 0 : errno; 248 el->el_errno = num == 0 ? 0 : errno;
249 return (num); 249 return (num);
250 } 250 }
251 251
252#ifdef KANJI 252#ifdef KANJI
253 if ((*ch & 0200)) { 253 if ((*ch & 0200)) {
254 el->el_state.metanext = 0; 254 el->el_state.metanext = 0;
255 cmd = CcViMap[' ']; 255 cmd = CcViMap[' '];
256 break; 256 break;
257 } else 257 } else
258#endif /* KANJI */ 258#endif /* KANJI */
259 259
260 if (el->el_state.metanext) { 260 if (el->el_state.metanext) {
261 el->el_state.metanext = 0; 261 el->el_state.metanext = 0;
262 *ch |= 0200; 262 *ch |= 0200;
263 } 263 }
264#ifdef WIDECHAR 264#ifdef WIDECHAR
265 if (*ch >= N_KEYS) 265 if (*ch >= N_KEYS)
266 cmd = ED_INSERT; 266 cmd = ED_INSERT;
267 else 267 else
268#endif 268#endif
269 cmd = el->el_map.current[(unsigned char) *ch]; 269 cmd = el->el_map.current[(unsigned char) *ch];
270 if (cmd == ED_SEQUENCE_LEAD_IN) { 270 if (cmd == ED_SEQUENCE_LEAD_IN) {
271 key_value_t val; 271 key_value_t val;
272 switch (key_get(el, ch, &val)) { 272 switch (key_get(el, ch, &val)) {
273 case XK_CMD: 273 case XK_CMD:
274 cmd = val.cmd; 274 cmd = val.cmd;
275 break; 275 break;
276 case XK_STR: 276 case XK_STR:
277 FUN(el,push)(el, val.str); 277 FUN(el,push)(el, val.str);
278 break; 278 break;
279#ifdef notyet 279#ifdef notyet
280 case XK_EXE: 280 case XK_EXE:
281 /* XXX: In the future to run a user function */ 281 /* XXX: In the future to run a user function */
282 RunCommand(val.str); 282 RunCommand(val.str);
283 break; 283 break;
284#endif 284#endif
285 default: 285 default:
286 EL_ABORT((el->el_errfile, "Bad XK_ type \n")); 286 EL_ABORT((el->el_errfile, "Bad XK_ type \n"));
287 break; 287 break;
288 } 288 }
289 } 289 }
290 if (el->el_map.alt == NULL) 290 if (el->el_map.alt == NULL)
291 el->el_map.current = el->el_map.key; 291 el->el_map.current = el->el_map.key;
292 } while (cmd == ED_SEQUENCE_LEAD_IN); 292 } while (cmd == ED_SEQUENCE_LEAD_IN);
293 *cmdnum = cmd; 293 *cmdnum = cmd;
294 return (OKCMD); 294 return (OKCMD);
295} 295}
296 296
297#ifdef WIDECHAR 297#ifdef WIDECHAR
298/* utf8_islead(): 298/* utf8_islead():
299 * Test whether a byte is a leading byte of a UTF-8 sequence. 299 * Test whether a byte is a leading byte of a UTF-8 sequence.
300 */ 300 */
301private int 301private int
302utf8_islead(unsigned char c) 302utf8_islead(unsigned char c)
303{ 303{
304 return (c < 0x80) || /* single byte char */ 304 return (c < 0x80) || /* single byte char */
305 (c >= 0xc2 && c <= 0xf4); /* start of multibyte sequence */ 305 (c >= 0xc2 && c <= 0xf4); /* start of multibyte sequence */
306} 306}
307#endif 307#endif
308 308
309/* read_char(): 309/* read_char():
310 * Read a character from the tty. 310 * Read a character from the tty.
311 */ 311 */
312private int 312private int
313read_char(EditLine *el, Char *cp) 313read_char(EditLine *el, Char *cp)
314{ 314{
315 ssize_t num_read; 315 ssize_t num_read;
316 int tried = 0; 316 int tried = 0;
317 char cbuf[MB_LEN_MAX]; 317 char cbuf[MB_LEN_MAX];
318 int cbp = 0; 318 int cbp = 0;
319 int bytes = 0; 319 int bytes = 0;
320 320
321 again: 321 again:
322 el->el_signal->sig_no = 0; 322 el->el_signal->sig_no = 0;
323 while ((num_read = read(el->el_infd, cbuf + cbp, 1)) == -1) { 323 while ((num_read = read(el->el_infd, cbuf + cbp, 1)) == -1) {
324 switch (el->el_signal->sig_no) { 324 switch (el->el_signal->sig_no) {
325 case SIGCONT: 325 case SIGCONT:
326 el_set(el, EL_REFRESH); 326 FUN(el,set)(el, EL_REFRESH);
327 /*FALLTHROUGH*/ 327 /*FALLTHROUGH*/
328 case SIGWINCH: 328 case SIGWINCH:
329 sig_set(el); 329 sig_set(el);
330 goto again; 330 goto again;
331 default: 331 default:
332 break; 332 break;
333 } 333 }
334 if (!tried && read__fixio(el->el_infd, errno) == 0) 334 if (!tried && read__fixio(el->el_infd, errno) == 0)
335 tried = 1; 335 tried = 1;
336 else { 336 else {
337 *cp = '\0'; 337 *cp = '\0';
338 return (-1); 338 return (-1);
339 } 339 }
340 } 340 }
341 341
342#ifdef WIDECHAR 342#ifdef WIDECHAR
343 if (el->el_flags & CHARSET_IS_UTF8) { 343 if (el->el_flags & CHARSET_IS_UTF8) {
344 if (!utf8_islead((unsigned char)cbuf[0])) 344 if (!utf8_islead((unsigned char)cbuf[0]))
345 goto again; /* discard the byte we read and try again */ 345 goto again; /* discard the byte we read and try again */
346 ++cbp; 346 ++cbp;
347 if ((bytes = ct_mbtowc(cp, cbuf, cbp)) == -1) { 347 if ((bytes = ct_mbtowc(cp, cbuf, cbp)) == -1) {
348 ct_mbtowc_reset; 348 ct_mbtowc_reset;
349 if (cbp >= MB_LEN_MAX) { /* "shouldn't happen" */ 349 if (cbp >= MB_LEN_MAX) { /* "shouldn't happen" */
350 *cp = '\0'; 350 *cp = '\0';
351 return (-1); 351 return (-1);
352 } 352 }
353 goto again; 353 goto again;
354 } 354 }
355 } else if (isascii((unsigned char)cbuf[0]) || 355 } else if (isascii((unsigned char)cbuf[0]) ||
356 /* we don't support other multibyte charsets */ 356 /* we don't support other multibyte charsets */
357 ++cbp != 1 || 357 ++cbp != 1 ||
358 /* Try non-ASCII characters in a 8-bit character set */ 358 /* Try non-ASCII characters in a 8-bit character set */
359 (bytes = ct_mbtowc(cp, cbuf, cbp)) != 1) 359 (bytes = ct_mbtowc(cp, cbuf, cbp)) != 1)
360#endif 360#endif
361 *cp = (unsigned char)cbuf[0]; 361 *cp = (unsigned char)cbuf[0];
362 362
363 if ((el->el_flags & IGNORE_EXTCHARS) && bytes > 1) { 363 if ((el->el_flags & IGNORE_EXTCHARS) && bytes > 1) {
364 cbp = 0; /* skip this character */ 364 cbp = 0; /* skip this character */
365 goto again; 365 goto again;
366 } 366 }
367 367
368 return (int)num_read; 368 return (int)num_read;
369} 369}
370 370
371/* read_pop(): 371/* read_pop():
372 * Pop a macro from the stack 372 * Pop a macro from the stack
373 */ 373 */
374private void 374private void
375read_pop(c_macro_t *ma) 375read_pop(c_macro_t *ma)
376{ 376{
377 int i; 377 int i;
378 378
379 el_free(ma->macro[0]); 379 el_free(ma->macro[0]);
380 for (i = 0; i < ma->level; i++) 380 for (i = 0; i < ma->level; i++)
381 ma->macro[i] = ma->macro[i + 1]; 381 ma->macro[i] = ma->macro[i + 1];
382 ma->level--; 382 ma->level--;
383 ma->offset = 0; 383 ma->offset = 0;
384} 384}
385 385
386/* el_getc(): 386/* el_getc():
387 * Read a character 387 * Read a character
388 */ 388 */
389public int 389public int
390FUN(el,getc)(EditLine *el, Char *cp) 390FUN(el,getc)(EditLine *el, Char *cp)
391{ 391{
392 int num_read; 392 int num_read;
393 c_macro_t *ma = &el->el_chared.c_macro; 393 c_macro_t *ma = &el->el_chared.c_macro;
394 394
395 term__flush(el); 395 terminal__flush(el);
396 for (;;) { 396 for (;;) {
397 if (ma->level < 0) { 397 if (ma->level < 0) {
398 if (!read_preread(el)) 398 if (!read_preread(el))
399 break; 399 break;
400 } 400 }
401 401
402 if (ma->level < 0) 402 if (ma->level < 0)
403 break; 403 break;
404 404
405 if (ma->macro[0][ma->offset] == '\0') { 405 if (ma->macro[0][ma->offset] == '\0') {
406 read_pop(ma); 406 read_pop(ma);
407 continue; 407 continue;
408 } 408 }
409 409
410 *cp = ma->macro[0][ma->offset++]; 410 *cp = ma->macro[0][ma->offset++];
411 411
412 if (ma->macro[0][ma->offset] == '\0') { 412 if (ma->macro[0][ma->offset] == '\0') {
413 /* Needed for QuoteMode On */ 413 /* Needed for QuoteMode On */
414 read_pop(ma); 414 read_pop(ma);
415 } 415 }
416 416
417 return (1); 417 return (1);
418 } 418 }
419 419
420#ifdef DEBUG_READ 420#ifdef DEBUG_READ
421 (void) fprintf(el->el_errfile, "Turning raw mode on\n"); 421 (void) fprintf(el->el_errfile, "Turning raw mode on\n");
422#endif /* DEBUG_READ */ 422#endif /* DEBUG_READ */
423 if (tty_rawmode(el) < 0)/* make sure the tty is set up correctly */ 423 if (tty_rawmode(el) < 0)/* make sure the tty is set up correctly */
424 return (0); 424 return (0);
425 425
426#ifdef DEBUG_READ 426#ifdef DEBUG_READ
427 (void) fprintf(el->el_errfile, "Reading a character\n"); 427 (void) fprintf(el->el_errfile, "Reading a character\n");
428#endif /* DEBUG_READ */ 428#endif /* DEBUG_READ */
429 num_read = (*el->el_read.read_char)(el, cp); 429 num_read = (*el->el_read.read_char)(el, cp);
430#ifdef WIDECHAR 430#ifdef WIDECHAR
431 if (el->el_flags & NARROW_READ) 431 if (el->el_flags & NARROW_READ)
432 *cp = *(char *)(void *)cp; 432 *cp = *(char *)(void *)cp;
433#endif 433#endif
434#ifdef DEBUG_READ 434#ifdef DEBUG_READ
435 (void) fprintf(el->el_errfile, "Got it %c\n", *cp); 435 (void) fprintf(el->el_errfile, "Got it %c\n", *cp);
436#endif /* DEBUG_READ */ 436#endif /* DEBUG_READ */
437 return (num_read); 437 return (num_read);
438} 438}
439 439
440protected void 440protected void
441read_prepare(EditLine *el) 441read_prepare(EditLine *el)
442{ 442{
443 if (el->el_flags & HANDLE_SIGNALS) 443 if (el->el_flags & HANDLE_SIGNALS)
444 sig_set(el); 444 sig_set(el);
445 if (el->el_flags & NO_TTY) 445 if (el->el_flags & NO_TTY)
446 return; 446 return;
447 if ((el->el_flags & (UNBUFFERED|EDIT_DISABLED)) == UNBUFFERED) 447 if ((el->el_flags & (UNBUFFERED|EDIT_DISABLED)) == UNBUFFERED)
448 tty_rawmode(el); 448 tty_rawmode(el);
449 449
450 /* This is relatively cheap, and things go terribly wrong if 450 /* This is relatively cheap, and things go terribly wrong if
451 we have the wrong size. */ 451 we have the wrong size. */
452 el_resize(el); 452 el_resize(el);
453 re_clear_display(el); /* reset the display stuff */ 453 re_clear_display(el); /* reset the display stuff */
454 ch_reset(el, 0); 454 ch_reset(el, 0);
455 re_refresh(el); /* print the prompt */ 455 re_refresh(el); /* print the prompt */
456 456
457 if (el->el_flags & UNBUFFERED) 457 if (el->el_flags & UNBUFFERED)
458 term__flush(el); 458 terminal__flush(el);
459} 459}
460 460
461protected void 461protected void
462read_finish(EditLine *el) 462read_finish(EditLine *el)
463{ 463{
464 if ((el->el_flags & UNBUFFERED) == 0) 464 if ((el->el_flags & UNBUFFERED) == 0)
465 (void) tty_cookedmode(el); 465 (void) tty_cookedmode(el);
466 if (el->el_flags & HANDLE_SIGNALS) 466 if (el->el_flags & HANDLE_SIGNALS)
467 sig_clr(el); 467 sig_clr(el);
468} 468}
469 469
470public const Char * 470public const Char *
471FUN(el,gets)(EditLine *el, int *nread) 471FUN(el,gets)(EditLine *el, int *nread)
472{ 472{
473 int retval; 473 int retval;
474 el_action_t cmdnum = 0; 474 el_action_t cmdnum = 0;
475 int num; /* how many chars we have read at NL */ 475 int num; /* how many chars we have read at NL */
476 Char ch, *cp; 476 Char ch, *cp;
477 int crlf = 0; 477 int crlf = 0;
478 int nrb; 478 int nrb;
479#ifdef FIONREAD 479#ifdef FIONREAD
480 c_macro_t *ma = &el->el_chared.c_macro; 480 c_macro_t *ma = &el->el_chared.c_macro;
481#endif /* FIONREAD */ 481#endif /* FIONREAD */
482 482
483 if (nread == NULL) 483 if (nread == NULL)
484 nread = &nrb; 484 nread = &nrb;
485 *nread = 0; 485 *nread = 0;
486 486
487 if (el->el_flags & NO_TTY) { 487 if (el->el_flags & NO_TTY) {
488 size_t idx; 488 size_t idx;
489 489
490 cp = el->el_line.buffer; 490 cp = el->el_line.buffer;
491 while ((num = (*el->el_read.read_char)(el, cp)) == 1) { 491 while ((num = (*el->el_read.read_char)(el, cp)) == 1) {
492 /* make sure there is space for next character */ 492 /* make sure there is space for next character */
493 if (cp + 1 >= el->el_line.limit) { 493 if (cp + 1 >= el->el_line.limit) {
494 idx = (cp - el->el_line.buffer); 494 idx = (cp - el->el_line.buffer);
495 if (!ch_enlargebufs(el, 2)) 495 if (!ch_enlargebufs(el, 2))
496 break; 496 break;
497 cp = &el->el_line.buffer[idx]; 497 cp = &el->el_line.buffer[idx];
498 } 498 }
499 cp++; 499 cp++;
500 if (el->el_flags & UNBUFFERED) 500 if (el->el_flags & UNBUFFERED)
501 break; 501 break;
502 if (cp[-1] == '\r' || cp[-1] == '\n') 502 if (cp[-1] == '\r' || cp[-1] == '\n')
503 break; 503 break;
504 } 504 }
505 if (num == -1) { 505 if (num == -1) {
506 if (errno == EINTR) 506 if (errno == EINTR)
507 cp = el->el_line.buffer; 507 cp = el->el_line.buffer;
508 el->el_errno = errno; 508 el->el_errno = errno;
509 } 509 }
510 510
511 goto noedit; 511 goto noedit;
512 } 512 }
513 513
514 514
515#ifdef FIONREAD 515#ifdef FIONREAD
516 if (el->el_tty.t_mode == EX_IO && ma->level < 0) { 516 if (el->el_tty.t_mode == EX_IO && ma->level < 0) {
517 long chrs = 0; 517 long chrs = 0;
518 518
519 (void) ioctl(el->el_infd, FIONREAD, (ioctl_t) & chrs); 519 (void) ioctl(el->el_infd, FIONREAD, (ioctl_t) & chrs);
520 if (chrs == 0) { 520 if (chrs == 0) {
521 if (tty_rawmode(el) < 0) { 521 if (tty_rawmode(el) < 0) {
522 errno = 0; 522 errno = 0;
523 *nread = 0; 523 *nread = 0;
524 return (NULL); 524 return (NULL);
525 } 525 }
526 } 526 }
527 } 527 }
528#endif /* FIONREAD */ 528#endif /* FIONREAD */
529 529
530 if ((el->el_flags & UNBUFFERED) == 0) 530 if ((el->el_flags & UNBUFFERED) == 0)
531 read_prepare(el); 531 read_prepare(el);
532 532
533 if (el->el_flags & EDIT_DISABLED) { 533 if (el->el_flags & EDIT_DISABLED) {
534 size_t idx; 534 size_t idx;
535 535
536 if ((el->el_flags & UNBUFFERED) == 0) 536 if ((el->el_flags & UNBUFFERED) == 0)
537 cp = el->el_line.buffer; 537 cp = el->el_line.buffer;
538 else 538 else
539 cp = el->el_line.lastchar; 539 cp = el->el_line.lastchar;
540 540
541 term__flush(el); 541 terminal__flush(el);
542 542
543 while ((num = (*el->el_read.read_char)(el, cp)) == 1) { 543 while ((num = (*el->el_read.read_char)(el, cp)) == 1) {
544 /* make sure there is space next character */ 544 /* make sure there is space next character */
545 if (cp + 1 >= el->el_line.limit) { 545 if (cp + 1 >= el->el_line.limit) {
546 idx = (cp - el->el_line.buffer); 546 idx = (cp - el->el_line.buffer);
547 if (!ch_enlargebufs(el, 2)) 547 if (!ch_enlargebufs(el, 2))
548 break; 548 break;
549 cp = &el->el_line.buffer[idx]; 549 cp = &el->el_line.buffer[idx];
550 } 550 }
551 cp++; 551 cp++;
552 crlf = cp[-1] == '\r' || cp[-1] == '\n'; 552 crlf = cp[-1] == '\r' || cp[-1] == '\n';
553 if (el->el_flags & UNBUFFERED) 553 if (el->el_flags & UNBUFFERED)
554 break; 554 break;
555 if (crlf) 555 if (crlf)
556 break; 556 break;
557 } 557 }
558 558
559 if (num == -1) { 559 if (num == -1) {
560 if (errno == EINTR) 560 if (errno == EINTR)
561 cp = el->el_line.buffer; 561 cp = el->el_line.buffer;
562 el->el_errno = errno; 562 el->el_errno = errno;
563 } 563 }
564 564
565 goto noedit; 565 goto noedit;
566 } 566 }
567 567
568 for (num = OKCMD; num == OKCMD;) { /* while still editing this 568 for (num = OKCMD; num == OKCMD;) { /* while still editing this
569 * line */ 569 * line */
570#ifdef DEBUG_EDIT 570#ifdef DEBUG_EDIT
571 read_debug(el); 571 read_debug(el);
572#endif /* DEBUG_EDIT */ 572#endif /* DEBUG_EDIT */
573 /* if EOF or error */ 573 /* if EOF or error */
574 if ((num = read_getcmd(el, &cmdnum, &ch)) != OKCMD) { 574 if ((num = read_getcmd(el, &cmdnum, &ch)) != OKCMD) {
575#ifdef DEBUG_READ 575#ifdef DEBUG_READ
576 (void) fprintf(el->el_errfile, 576 (void) fprintf(el->el_errfile,
577 "Returning from el_gets %d\n", num); 577 "Returning from el_gets %d\n", num);
578#endif /* DEBUG_READ */ 578#endif /* DEBUG_READ */
579 break; 579 break;
580 } 580 }
581 if (el->el_errno == EINTR) { 581 if (el->el_errno == EINTR) {
582 el->el_line.buffer[0] = '\0'; 582 el->el_line.buffer[0] = '\0';
583 el->el_line.lastchar = 583 el->el_line.lastchar =
584 el->el_line.cursor = el->el_line.buffer; 584 el->el_line.cursor = el->el_line.buffer;
585 break; 585 break;
586 } 586 }
587 if ((unsigned int)cmdnum >= (unsigned int)el->el_map.nfunc) { /* BUG CHECK command */ 587 if ((unsigned int)cmdnum >= (unsigned int)el->el_map.nfunc) { /* BUG CHECK command */
588#ifdef DEBUG_EDIT 588#ifdef DEBUG_EDIT
589 (void) fprintf(el->el_errfile, 589 (void) fprintf(el->el_errfile,
590 "ERROR: illegal command from key 0%o\r\n", ch); 590 "ERROR: illegal command from key 0%o\r\n", ch);
591#endif /* DEBUG_EDIT */ 591#endif /* DEBUG_EDIT */
592 continue; /* try again */ 592 continue; /* try again */
593 } 593 }
594 /* now do the real command */ 594 /* now do the real command */
595#ifdef DEBUG_READ 595#ifdef DEBUG_READ
596 { 596 {
597 el_bindings_t *b; 597 el_bindings_t *b;
598 for (b = el->el_map.help; b->name; b++) 598 for (b = el->el_map.help; b->name; b++)
599 if (b->func == cmdnum) 599 if (b->func == cmdnum)
600 break; 600 break;
601 if (b->name) 601 if (b->name)
602 (void) fprintf(el->el_errfile, 602 (void) fprintf(el->el_errfile,
603 "Executing %s\n", b->name); 603 "Executing %s\n", b->name);
604 else 604 else
605 (void) fprintf(el->el_errfile, 605 (void) fprintf(el->el_errfile,
606 "Error command = %d\n", cmdnum); 606 "Error command = %d\n", cmdnum);
607 } 607 }
608#endif /* DEBUG_READ */ 608#endif /* DEBUG_READ */
609 /* vi redo needs these way down the levels... */ 609 /* vi redo needs these way down the levels... */
610 el->el_state.thiscmd = cmdnum; 610 el->el_state.thiscmd = cmdnum;
611 el->el_state.thisch = ch; 611 el->el_state.thisch = ch;
612 if (el->el_map.type == MAP_VI && 612 if (el->el_map.type == MAP_VI &&
613 el->el_map.current == el->el_map.key && 613 el->el_map.current == el->el_map.key &&
614 el->el_chared.c_redo.pos < el->el_chared.c_redo.lim) { 614 el->el_chared.c_redo.pos < el->el_chared.c_redo.lim) {
615 if (cmdnum == VI_DELETE_PREV_CHAR && 615 if (cmdnum == VI_DELETE_PREV_CHAR &&
616 el->el_chared.c_redo.pos != el->el_chared.c_redo.buf 616 el->el_chared.c_redo.pos != el->el_chared.c_redo.buf
617 && Isprint(el->el_chared.c_redo.pos[-1])) 617 && Isprint(el->el_chared.c_redo.pos[-1]))
618 el->el_chared.c_redo.pos--; 618 el->el_chared.c_redo.pos--;
619 else 619 else
620 *el->el_chared.c_redo.pos++ = ch; 620 *el->el_chared.c_redo.pos++ = ch;
621 } 621 }
622 retval = (*el->el_map.func[cmdnum]) (el, ch); 622 retval = (*el->el_map.func[cmdnum]) (el, ch);
623#ifdef DEBUG_READ 623#ifdef DEBUG_READ
624 (void) fprintf(el->el_errfile, 624 (void) fprintf(el->el_errfile,
625 "Returned state %d\n", retval ); 625 "Returned state %d\n", retval );
626#endif /* DEBUG_READ */ 626#endif /* DEBUG_READ */
627 627
628 /* save the last command here */ 628 /* save the last command here */
629 el->el_state.lastcmd = cmdnum; 629 el->el_state.lastcmd = cmdnum;
630 630
631 /* use any return value */ 631 /* use any return value */
632 switch (retval) { 632 switch (retval) {
633 case CC_CURSOR: 633 case CC_CURSOR:
634 re_refresh_cursor(el); 634 re_refresh_cursor(el);
635 break; 635 break;
636 636
637 case CC_REDISPLAY: 637 case CC_REDISPLAY:
638 re_clear_lines(el); 638 re_clear_lines(el);
639 re_clear_display(el); 639 re_clear_display(el);
640 /* FALLTHROUGH */ 640 /* FALLTHROUGH */
641 641
642 case CC_REFRESH: 642 case CC_REFRESH:
643 re_refresh(el); 643 re_refresh(el);
644 break; 644 break;
645 645
646 case CC_REFRESH_BEEP: 646 case CC_REFRESH_BEEP:
647 re_refresh(el); 647 re_refresh(el);
648 term_beep(el); 648 terminal_beep(el);
649 break; 649 break;
650 650
651 case CC_NORM: /* normal char */ 651 case CC_NORM: /* normal char */
652 break; 652 break;
653 653
654 case CC_ARGHACK: /* Suggested by Rich Salz */ 654 case CC_ARGHACK: /* Suggested by Rich Salz */
655 /* <rsalz@pineapple.bbn.com> */ 655 /* <rsalz@pineapple.bbn.com> */
656 continue; /* keep going... */ 656 continue; /* keep going... */
657 657
658 case CC_EOF: /* end of file typed */ 658 case CC_EOF: /* end of file typed */
659 if ((el->el_flags & UNBUFFERED) == 0) 659 if ((el->el_flags & UNBUFFERED) == 0)
660 num = 0; 660 num = 0;
661 else if (num == -1) { 661 else if (num == -1) {
662 *el->el_line.lastchar++ = CONTROL('d'); 662 *el->el_line.lastchar++ = CONTROL('d');
663 el->el_line.cursor = el->el_line.lastchar; 663 el->el_line.cursor = el->el_line.lastchar;
664 num = 1; 664 num = 1;
665 } 665 }
666 break; 666 break;
667 667
668 case CC_NEWLINE: /* normal end of line */ 668 case CC_NEWLINE: /* normal end of line */
669 num = (int)(el->el_line.lastchar - el->el_line.buffer); 669 num = (int)(el->el_line.lastchar - el->el_line.buffer);
670 break; 670 break;
671 671
672 case CC_FATAL: /* fatal error, reset to known state */ 672 case CC_FATAL: /* fatal error, reset to known state */
673#ifdef DEBUG_READ 673#ifdef DEBUG_READ
674 (void) fprintf(el->el_errfile, 674 (void) fprintf(el->el_errfile,
675 "*** editor fatal ERROR ***\r\n\n"); 675 "*** editor fatal ERROR ***\r\n\n");
676#endif /* DEBUG_READ */ 676#endif /* DEBUG_READ */
677 /* put (real) cursor in a known place */ 677 /* put (real) cursor in a known place */
678 re_clear_display(el); /* reset the display stuff */ 678 re_clear_display(el); /* reset the display stuff */
679 ch_reset(el, 1); /* reset the input pointers */ 679 ch_reset(el, 1); /* reset the input pointers */
680 re_refresh(el); /* print the prompt again */ 680 re_refresh(el); /* print the prompt again */
681 break; 681 break;
682 682
683 case CC_ERROR: 683 case CC_ERROR:
684 default: /* functions we don't know about */ 684 default: /* functions we don't know about */
685#ifdef DEBUG_READ 685#ifdef DEBUG_READ
686 (void) fprintf(el->el_errfile, 686 (void) fprintf(el->el_errfile,
687 "*** editor ERROR ***\r\n\n"); 687 "*** editor ERROR ***\r\n\n");
688#endif /* DEBUG_READ */ 688#endif /* DEBUG_READ */
689 term_beep(el); 689 terminal_beep(el);
690 term__flush(el); 690 terminal__flush(el);
691 break; 691 break;
692 } 692 }
693 el->el_state.argument = 1; 693 el->el_state.argument = 1;
694 el->el_state.doingarg = 0; 694 el->el_state.doingarg = 0;
695 el->el_chared.c_vcmd.action = NOP; 695 el->el_chared.c_vcmd.action = NOP;
696 if (el->el_flags & UNBUFFERED) 696 if (el->el_flags & UNBUFFERED)
697 break; 697 break;
698 } 698 }
699 699
700 term__flush(el); /* flush any buffered output */ 700 terminal__flush(el); /* flush any buffered output */
701 /* make sure the tty is set up correctly */ 701 /* make sure the tty is set up correctly */
702 if ((el->el_flags & UNBUFFERED) == 0) { 702 if ((el->el_flags & UNBUFFERED) == 0) {
703 read_finish(el); 703 read_finish(el);
704 *nread = num != -1 ? num : 0; 704 *nread = num != -1 ? num : 0;
705 } else { 705 } else {
706 *nread = (int)(el->el_line.lastchar - el->el_line.buffer); 706 *nread = (int)(el->el_line.lastchar - el->el_line.buffer);
707 } 707 }
708 goto done; 708 goto done;
709noedit: 709noedit:
710 el->el_line.cursor = el->el_line.lastchar = cp; 710 el->el_line.cursor = el->el_line.lastchar = cp;
711 *cp = '\0'; 711 *cp = '\0';
712 *nread = (int)(el->el_line.cursor - el->el_line.buffer); 712 *nread = (int)(el->el_line.cursor - el->el_line.buffer);
713done: 713done:
714 if (*nread == 0) { 714 if (*nread == 0) {
715 if (num == -1) { 715 if (num == -1) {
716 *nread = -1; 716 *nread = -1;
717 errno = el->el_errno; 717 errno = el->el_errno;
718 } 718 }
719 return NULL; 719 return NULL;
720 } else 720 } else
721 return el->el_line.buffer; 721 return el->el_line.buffer;
722} 722}