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