xref: /openbsd/lib/libedit/read.c (revision db3296cf)
1 /*	$OpenBSD: read.c,v 1.9 2003/06/02 20:18:40 millert Exp $	*/
2 /*	$NetBSD: read.c,v 1.4 1997/04/11 17:52:47 christos Exp $	*/
3 
4 /*-
5  * Copyright (c) 1992, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Christos Zoulas of Cornell University.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #if !defined(lint) && !defined(SCCSID)
37 #if 0
38 static char sccsid[] = "@(#)read.c	8.1 (Berkeley) 6/4/93";
39 #else
40 static const char rcsid[] = "$OpenBSD: read.c,v 1.9 2003/06/02 20:18:40 millert Exp $";
41 #endif
42 #endif /* not lint && not SCCSID */
43 
44 /*
45  * read.c: Clean this junk up! This is horrible code.
46  *	   Terminal read functions
47  */
48 #include "sys.h"
49 #include <sys/errno.h>
50 #include <unistd.h>
51 #include <stdlib.h>
52 extern int errno;
53 #include "el.h"
54 
55 #define OKCMD -1
56 
57 private int read__fixio(int, int);
58 private int read_preread(EditLine *);
59 private int read_getcmd(EditLine *, el_action_t *, char *);
60 
61 #ifdef DEBUG_EDIT
62 private void
63 read_debug(el)
64     EditLine *el;
65 {
66 
67     if (el->el_line.cursor > el->el_line.lastchar)
68 	(void)fprintf(el->el_errfile, "cursor > lastchar\r\n");
69     if (el->el_line.cursor < el->el_line.buffer)
70 	(void)fprintf(el->el_errfile, "cursor < buffer\r\n");
71     if (el->el_line.cursor > el->el_line.limit)
72 	(void)fprintf(el->el_errfile, "cursor > limit\r\n");
73     if (el->el_line.lastchar > el->el_line.limit)
74 	(void)fprintf(el->el_errfile, "lastchar > limit\r\n");
75     if (el->el_line.limit != &el->el_line.buffer[EL_BUFSIZ - 2])
76 	(void)fprintf(el->el_errfile, "limit != &buffer[EL_BUFSIZ-2]\r\n");
77 }
78 #endif /* DEBUG_EDIT */
79 
80 /* read__fixio():
81  *	Try to recover from a read error
82  */
83 private int
84 read__fixio(fd, e)
85     int fd, e;
86 {
87     switch (e) {
88     case -1:	/* Make sure that the code is reachable */
89 
90 #ifdef EWOULDBLOCK
91     case EWOULDBLOCK:
92 # ifndef TRY_AGAIN
93 #  define TRY_AGAIN
94 # endif
95 #endif /* EWOULDBLOCK */
96 
97 #if defined(POSIX) && defined(EAGAIN)
98 # if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
99     case EAGAIN:
100 #  ifndef TRY_AGAIN
101 #   define TRY_AGAIN
102 #  endif
103 # endif /* EWOULDBLOCK && EWOULDBLOCK != EAGAIN */
104 #endif /* POSIX && EAGAIN */
105 
106 	e = 0;
107 #ifdef TRY_AGAIN
108 # if defined(F_SETFL) && defined(O_NDELAY)
109 	if ((e = fcntl(fd, F_GETFL, 0)) == -1)
110 	    return -1;
111 
112 	if (fcntl(fd, F_SETFL, e & ~O_NDELAY) == -1)
113 	    return -1;
114 	else
115 	    e = 1;
116 # endif /* F_SETFL && O_NDELAY */
117 
118 # ifdef FIONBIO
119 	if (ioctl(fd, FIONBIO, (ioctl_t) &e) == -1)
120 	    return -1;
121 	else
122 	    e = 1;
123 # endif	/* FIONBIO */
124 
125 #endif /* TRY_AGAIN */
126 	return e ? 0 : -1;
127 
128     case EINTR:
129 	return 0;
130 
131     default:
132 	return -1;
133     }
134 }
135 
136 
137 /* read_preread():
138  *	Try to read the stuff in the input queue;
139  */
140 private int
141 read_preread(el)
142     EditLine *el;
143 {
144     int    chrs = 0;
145 
146     if (el->el_chared.c_macro.nline) {
147 	el_free((ptr_t) el->el_chared.c_macro.nline);
148 	el->el_chared.c_macro.nline = NULL;
149     }
150 
151     if (el->el_tty.t_mode == ED_IO)
152 	return 0;
153 
154 #ifdef FIONREAD
155     (void)ioctl(el->el_infd, FIONREAD, (ioctl_t) &chrs);
156     if (chrs > 0) {
157 	char    buf[EL_BUFSIZ];
158 
159 	chrs = read(el->el_infd, buf, (size_t) MIN(chrs, EL_BUFSIZ - 1));
160 	if (chrs > 0) {
161 	    buf[chrs] = '\0';
162 	    el->el_chared.c_macro.nline = strdup(buf);
163 	    el_push(el, el->el_chared.c_macro.nline);
164 	}
165     }
166 #endif  /* FIONREAD */
167 
168     return chrs > 0;
169 }
170 
171 
172 /* el_push():
173  *	Push a macro
174  */
175 public void
176 el_push(el, str)
177     EditLine *el;
178     const char   *str;
179 {
180     c_macro_t *ma = &el->el_chared.c_macro;
181 
182     if (str != NULL && ma->level + 1 < EL_MAXMACRO) {
183 	ma->level++;
184 	ma->macro[ma->level] = (char *) str;
185     }
186     else {
187 	term_beep(el);
188 	term__flush();
189     }
190 }
191 
192 
193 /* read_getcmd():
194  *	Return next command from the input stream.
195  */
196 private int
197 read_getcmd(el, cmdnum, ch)
198     EditLine *el;
199     el_action_t *cmdnum;
200     char *ch;
201 {
202     el_action_t  cmd = 0;
203     int     num;
204 
205     while (cmd == 0 || cmd == ED_SEQUENCE_LEAD_IN) {
206 	if ((num = el_getc(el, ch)) != 1)	/* if EOF or error */
207 	    return num;
208 
209 #ifdef	KANJI
210 	if ((*ch & 0200)) {
211 	    el->el_state.metanext = 0;
212 	    cmd = CcViMap[' '];
213 	    break;
214 	}
215 	else
216 #endif /* KANJI */
217 
218 	if (el->el_state.metanext) {
219 	    el->el_state.metanext = 0;
220 	    *ch |= 0200;
221 	}
222 	cmd = el->el_map.current[(unsigned char) *ch];
223 	if (cmd == ED_SEQUENCE_LEAD_IN) {
224 	    key_value_t val;
225 	    switch (key_get(el, ch, &val)) {
226 	    case XK_CMD:
227 		cmd = val.cmd;
228 		break;
229 	    case XK_STR:
230 		el_push(el, val.str);
231 		break;
232 #ifdef notyet
233 	    case XK_EXE:
234 		/* XXX: In the future to run a user function */
235 		RunCommand(val.str);
236 		break;
237 #endif
238 	    default:
239 		abort();
240 		break;
241 	    }
242 	}
243 	if (el->el_map.alt == NULL)
244 	    el->el_map.current = el->el_map.key;
245     }
246     *cmdnum = cmd;
247     return OKCMD;
248 }
249 
250 
251 /* el_getc():
252  *	Read a character
253  */
254 public int
255 el_getc(el, cp)
256     EditLine *el;
257     char *cp;
258 {
259     int num_read;
260     unsigned char tcp;
261     int tried = 0;
262 
263     c_macro_t *ma = &el->el_chared.c_macro;
264 
265     term__flush();
266     for (;;) {
267 	if (ma->level < 0) {
268 	    if (!read_preread(el))
269 		break;
270 	}
271 	if (ma->level < 0)
272 	    break;
273 
274 	if (*ma->macro[ma->level] == 0) {
275 	    ma->level--;
276 	    continue;
277 	}
278 	*cp = *ma->macro[ma->level]++ & 0377;
279 	if (*ma->macro[ma->level] == 0) {	/* Needed for QuoteMode On */
280 	    ma->level--;
281 	}
282 	return 1;
283     }
284 
285 #ifdef DEBUG_READ
286     (void)fprintf(el->el_errfile, "Turning raw mode on\n");
287 #endif /* DEBUG_READ */
288     if (tty_rawmode(el) < 0)	/* make sure the tty is set up correctly */
289 	return 0;
290 
291 #ifdef DEBUG_READ
292     (void)fprintf(el->el_errfile, "Reading a character\n");
293 #endif /* DEBUG_READ */
294     while ((num_read = read(el->el_infd, (char *) &tcp, 1)) == -1)
295 	if (!tried && read__fixio(el->el_infd, errno) == 0)
296 	    tried = 1;
297 	else {
298 	    *cp = '\0';
299 	    return -1;
300 	}
301 #ifdef DEBUG_READ
302     (void)fprintf(el->el_errfile, "Got it %c\n", tcp);
303 #endif /* DEBUG_READ */
304     *cp = tcp;
305     return num_read;
306 }
307 
308 
309 
310 public const char *
311 el_gets(el, nread)
312     EditLine *el;
313     int *nread;
314 {
315     int retval;
316     el_action_t  cmdnum = 0;
317     int     num;		/* how many chars we have read at NL */
318     char    ch;
319 #ifdef FIONREAD
320     c_macro_t *ma = &el->el_chared.c_macro;
321 #endif /* FIONREAD */
322 
323     if (el->el_flags & HANDLE_SIGNALS)
324 	sig_set(el);
325 
326     re_clear_display(el);		/* reset the display stuff */
327     ch_reset(el);
328 
329 #ifdef FIONREAD
330     if (el->el_tty.t_mode == EX_IO && ma->level < 0) {
331 	int    chrs = 0;
332 
333 	(void)ioctl(el->el_infd, FIONREAD, (ioctl_t) &chrs);
334 	if (chrs == 0) {
335 	    if (tty_rawmode(el) < 0) {
336 		if (nread)
337 			*nread = 0;
338 		return NULL;
339 	    }
340 	}
341     }
342 #endif /* FIONREAD */
343 
344     re_refresh(el);			/* print the prompt */
345 
346     for (num = OKCMD; num == OKCMD;) {	/* while still editing this line */
347 #ifdef DEBUG_EDIT
348 	read_debug(el);
349 #endif /* DEBUG_EDIT */
350 	/* if EOF or error */
351 	if ((num = read_getcmd(el, &cmdnum, &ch)) != OKCMD) {
352 #ifdef DEBUG_READ
353 	    (void)fprintf(el->el_errfile, "Returning from el_gets %d\n", num);
354 #endif /* DEBUG_READ */
355 	    break;
356 	}
357 
358 	if (cmdnum >= el->el_map.nfunc) {	/* BUG CHECK command */
359 #ifdef DEBUG_EDIT
360 	    (void)fprintf(el->el_errfile,
361 			   "ERROR: illegal command from key 0%o\r\n", ch);
362 #endif /* DEBUG_EDIT */
363 	    continue;		/* try again */
364 	}
365 
366 	/* now do the real command */
367 #ifdef DEBUG_READ
368 	{
369 	    el_bindings_t *b;
370 	    for (b = el->el_map.help; b->name; b++)
371 		if (b->func == cmdnum)
372 		    break;
373 	    if (b->name)
374 		(void)fprintf(el->el_errfile, "Executing %s\n", b->name);
375 	    else
376 		(void)fprintf(el->el_errfile, "Error command = %d\n", cmdnum);
377 	}
378 #endif /* DEBUG_READ */
379 	retval = (*el->el_map.func[cmdnum])(el, ch);
380 
381 	/* save the last command here */
382 	el->el_state.lastcmd = cmdnum;
383 
384 	/* use any return value */
385 	switch (retval) {
386 	case CC_CURSOR:
387 	    el->el_state.argument = 1;
388 	    el->el_state.doingarg = 0;
389 	    re_refresh_cursor(el);
390 	    break;
391 
392 	case CC_REDISPLAY:
393 	    re_clear_lines(el);
394 	    re_clear_display(el);
395 		/* FALLTHROUGH */
396 
397 	case CC_REFRESH:
398 	    el->el_state.argument = 1;
399 	    el->el_state.doingarg = 0;
400 	    re_refresh(el);
401 	    break;
402 
403 	case CC_NORM:		/* normal char */
404 	    el->el_state.argument = 1;
405 	    el->el_state.doingarg = 0;
406 	    break;
407 
408 	case CC_ARGHACK:	/* Suggested by Rich Salz */
409 	    /* <rsalz@pineapple.bbn.com> */
410 	    break;		/* keep going... */
411 
412 	case CC_EOF:		/* end of file typed */
413 	    num = 0;
414 	    break;
415 
416 	case CC_NEWLINE:	/* normal end of line */
417 	    num = el->el_line.lastchar - el->el_line.buffer;
418 	    break;
419 
420 	case CC_FATAL:		/* fatal error, reset to known state */
421 #ifdef DEBUG_READ
422 	    (void)fprintf(el->el_errfile, "*** editor fatal ERROR ***\r\n\n");
423 #endif /* DEBUG_READ */
424 	    /* put (real) cursor in a known place */
425 	    re_clear_display(el);	/* reset the display stuff */
426 	    ch_reset(el);		/* reset the input pointers */
427 	    re_refresh(el);		/* print the prompt again */
428 	    el->el_state.argument = 1;
429 	    el->el_state.doingarg = 0;
430 	    break;
431 
432 	case CC_ERROR:
433 	default:		/* functions we don't know about */
434 #ifdef DEBUG_READ
435 	    (void)fprintf(el->el_errfile, "*** editor ERROR ***\r\n\n");
436 #endif /* DEBUG_READ */
437 	    el->el_state.argument = 1;
438 	    el->el_state.doingarg = 0;
439 	    term_beep(el);
440 	    term__flush();
441 	    break;
442 	}
443     }
444 
445     (void)tty_cookedmode(el);	/* make sure the tty is set up correctly */
446     term__flush();		/* flush any buffered output */
447     if (el->el_flags & HANDLE_SIGNALS)
448 	sig_clr(el);
449     if (nread)
450 	    *nread = num;
451     return num ? el->el_line.buffer : NULL;
452 }
453