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