xref: /netbsd-src/bin/sh/input.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: input.c,v 1.39 2003/08/07 09:05:32 agc Exp $	*/
2 
3 /*-
4  * Copyright (c) 1991, 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  * Kenneth Almquist.
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. 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
20  *    without specific prior written permission.
21  *
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
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
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
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
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)input.c	8.3 (Berkeley) 6/9/95";
39 #else
40 __RCSID("$NetBSD: input.c,v 1.39 2003/08/07 09:05:32 agc Exp $");
41 #endif
42 #endif /* not lint */
43 
44 #include <stdio.h>	/* defines BUFSIZ */
45 #include <fcntl.h>
46 #include <errno.h>
47 #include <unistd.h>
48 #include <stdlib.h>
49 #include <string.h>
50 
51 /*
52  * This file implements the input routines used by the parser.
53  */
54 
55 #include "shell.h"
56 #include "redir.h"
57 #include "syntax.h"
58 #include "input.h"
59 #include "output.h"
60 #include "options.h"
61 #include "memalloc.h"
62 #include "error.h"
63 #include "alias.h"
64 #include "parser.h"
65 #include "myhistedit.h"
66 
67 #define EOF_NLEFT -99		/* value of parsenleft when EOF pushed back */
68 
69 MKINIT
70 struct strpush {
71 	struct strpush *prev;	/* preceding string on stack */
72 	char *prevstring;
73 	int prevnleft;
74 	int prevlleft;
75 	struct alias *ap;	/* if push was associated with an alias */
76 };
77 
78 /*
79  * The parsefile structure pointed to by the global variable parsefile
80  * contains information about the current file being read.
81  */
82 
83 MKINIT
84 struct parsefile {
85 	struct parsefile *prev;	/* preceding file on stack */
86 	int linno;		/* current line */
87 	int fd;			/* file descriptor (or -1 if string) */
88 	int nleft;		/* number of chars left in this line */
89 	int lleft;		/* number of chars left in this buffer */
90 	char *nextc;		/* next char in buffer */
91 	char *buf;		/* input buffer */
92 	struct strpush *strpush; /* for pushing strings at this level */
93 	struct strpush basestrpush; /* so pushing one is fast */
94 };
95 
96 
97 int plinno = 1;			/* input line number */
98 int parsenleft;			/* copy of parsefile->nleft */
99 MKINIT int parselleft;		/* copy of parsefile->lleft */
100 char *parsenextc;		/* copy of parsefile->nextc */
101 MKINIT struct parsefile basepf;	/* top level input file */
102 MKINIT char basebuf[BUFSIZ];	/* buffer for top level input file */
103 struct parsefile *parsefile = &basepf;	/* current input file */
104 int init_editline = 0;		/* editline library initialized? */
105 int whichprompt;		/* 1 == PS1, 2 == PS2 */
106 
107 EditLine *el;			/* cookie for editline package */
108 
109 STATIC void pushfile(void);
110 static int preadfd(void);
111 
112 #ifdef mkinit
113 INCLUDE <stdio.h>
114 INCLUDE "input.h"
115 INCLUDE "error.h"
116 
117 INIT {
118 	basepf.nextc = basepf.buf = basebuf;
119 }
120 
121 RESET {
122 	if (exception != EXSHELLPROC)
123 		parselleft = parsenleft = 0;	/* clear input buffer */
124 	popallfiles();
125 }
126 
127 SHELLPROC {
128 	popallfiles();
129 }
130 #endif
131 
132 
133 /*
134  * Read a line from the script.
135  */
136 
137 char *
138 pfgets(char *line, int len)
139 {
140 	char *p = line;
141 	int nleft = len;
142 	int c;
143 
144 	while (--nleft > 0) {
145 		c = pgetc_macro();
146 		if (c == PEOF) {
147 			if (p == line)
148 				return NULL;
149 			break;
150 		}
151 		*p++ = c;
152 		if (c == '\n')
153 			break;
154 	}
155 	*p = '\0';
156 	return line;
157 }
158 
159 
160 
161 /*
162  * Read a character from the script, returning PEOF on end of file.
163  * Nul characters in the input are silently discarded.
164  */
165 
166 int
167 pgetc(void)
168 {
169 	return pgetc_macro();
170 }
171 
172 
173 static int
174 preadfd(void)
175 {
176 	int nr;
177 	char *buf =  parsefile->buf;
178 	parsenextc = buf;
179 
180 retry:
181 #ifndef SMALL
182 	if (parsefile->fd == 0 && el) {
183 		static const char *rl_cp;
184 		static int el_len;
185 
186 		if (rl_cp == NULL)
187 			rl_cp = el_gets(el, &el_len);
188 		if (rl_cp == NULL)
189 			nr = 0;
190 		else {
191 			nr = el_len;
192 			if (nr > BUFSIZ - 8)
193 				nr = BUFSIZ - 8;
194 			memcpy(buf, rl_cp, nr);
195 			if (nr != el_len) {
196 				el_len -= nr;
197 				rl_cp += nr;
198 			} else
199 				rl_cp = 0;
200 		}
201 
202 	} else
203 #endif
204 		nr = read(parsefile->fd, buf, BUFSIZ - 8);
205 
206 
207 	if (nr <= 0) {
208                 if (nr < 0) {
209                         if (errno == EINTR)
210                                 goto retry;
211                         if (parsefile->fd == 0 && errno == EWOULDBLOCK) {
212                                 int flags = fcntl(0, F_GETFL, 0);
213                                 if (flags >= 0 && flags & O_NONBLOCK) {
214                                         flags &=~ O_NONBLOCK;
215                                         if (fcntl(0, F_SETFL, flags) >= 0) {
216 						out2str("sh: turning off NDELAY mode\n");
217                                                 goto retry;
218                                         }
219                                 }
220                         }
221                 }
222                 nr = -1;
223 	}
224 	return nr;
225 }
226 
227 /*
228  * Refill the input buffer and return the next input character:
229  *
230  * 1) If a string was pushed back on the input, pop it;
231  * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading
232  *    from a string so we can't refill the buffer, return EOF.
233  * 3) If the is more stuff in this buffer, use it else call read to fill it.
234  * 4) Process input up to the next newline, deleting nul characters.
235  */
236 
237 int
238 preadbuffer(void)
239 {
240 	char *p, *q;
241 	int more;
242 	int something;
243 	char savec;
244 
245 	if (parsefile->strpush) {
246 		popstring();
247 		if (--parsenleft >= 0)
248 			return (*parsenextc++);
249 	}
250 	if (parsenleft == EOF_NLEFT || parsefile->buf == NULL)
251 		return PEOF;
252 	flushout(&output);
253 	flushout(&errout);
254 
255 again:
256 	if (parselleft <= 0) {
257 		if ((parselleft = preadfd()) == -1) {
258 			parselleft = parsenleft = EOF_NLEFT;
259 			return PEOF;
260 		}
261 	}
262 
263 	q = p = parsenextc;
264 
265 	/* delete nul characters */
266 	something = 0;
267 	for (more = 1; more;) {
268 		switch (*p) {
269 		case '\0':
270 			p++;	/* Skip nul */
271 			goto check;
272 
273 		case '\t':
274 		case ' ':
275 			break;
276 
277 		case '\n':
278 			parsenleft = q - parsenextc;
279 			more = 0; /* Stop processing here */
280 			break;
281 
282 		default:
283 			something = 1;
284 			break;
285 		}
286 
287 		*q++ = *p++;
288 check:
289 		if (--parselleft <= 0) {
290 			parsenleft = q - parsenextc - 1;
291 			if (parsenleft < 0)
292 				goto again;
293 			*q = '\0';
294 			more = 0;
295 		}
296 	}
297 
298 	savec = *q;
299 	*q = '\0';
300 
301 #ifndef SMALL
302 	if (parsefile->fd == 0 && hist && something) {
303 		HistEvent he;
304 		INTOFF;
305 		history(hist, &he, whichprompt == 1? H_ENTER : H_APPEND,
306 		    parsenextc);
307 		INTON;
308 	}
309 #endif
310 
311 	if (vflag) {
312 		out2str(parsenextc);
313 		flushout(out2);
314 	}
315 
316 	*q = savec;
317 
318 	return *parsenextc++;
319 }
320 
321 /*
322  * Undo the last call to pgetc.  Only one character may be pushed back.
323  * PEOF may be pushed back.
324  */
325 
326 void
327 pungetc(void)
328 {
329 	parsenleft++;
330 	parsenextc--;
331 }
332 
333 /*
334  * Push a string back onto the input at this current parsefile level.
335  * We handle aliases this way.
336  */
337 void
338 pushstring(char *s, int len, void *ap)
339 {
340 	struct strpush *sp;
341 
342 	INTOFF;
343 /*dprintf("*** calling pushstring: %s, %d\n", s, len);*/
344 	if (parsefile->strpush) {
345 		sp = ckmalloc(sizeof (struct strpush));
346 		sp->prev = parsefile->strpush;
347 		parsefile->strpush = sp;
348 	} else
349 		sp = parsefile->strpush = &(parsefile->basestrpush);
350 	sp->prevstring = parsenextc;
351 	sp->prevnleft = parsenleft;
352 	sp->prevlleft = parselleft;
353 	sp->ap = (struct alias *)ap;
354 	if (ap)
355 		((struct alias *)ap)->flag |= ALIASINUSE;
356 	parsenextc = s;
357 	parsenleft = len;
358 	INTON;
359 }
360 
361 void
362 popstring(void)
363 {
364 	struct strpush *sp = parsefile->strpush;
365 
366 	INTOFF;
367 	parsenextc = sp->prevstring;
368 	parsenleft = sp->prevnleft;
369 	parselleft = sp->prevlleft;
370 /*dprintf("*** calling popstring: restoring to '%s'\n", parsenextc);*/
371 	if (sp->ap)
372 		sp->ap->flag &= ~ALIASINUSE;
373 	parsefile->strpush = sp->prev;
374 	if (sp != &(parsefile->basestrpush))
375 		ckfree(sp);
376 	INTON;
377 }
378 
379 /*
380  * Set the input to take input from a file.  If push is set, push the
381  * old input onto the stack first.
382  */
383 
384 void
385 setinputfile(const char *fname, int push)
386 {
387 	int fd;
388 	int fd2;
389 
390 	INTOFF;
391 	if ((fd = open(fname, O_RDONLY)) < 0)
392 		error("Can't open %s", fname);
393 	if (fd < 10) {
394 		fd2 = copyfd(fd, 10);
395 		close(fd);
396 		if (fd2 < 0)
397 			error("Out of file descriptors");
398 		fd = fd2;
399 	}
400 	setinputfd(fd, push);
401 	INTON;
402 }
403 
404 
405 /*
406  * Like setinputfile, but takes an open file descriptor.  Call this with
407  * interrupts off.
408  */
409 
410 void
411 setinputfd(int fd, int push)
412 {
413 	(void) fcntl(fd, F_SETFD, FD_CLOEXEC);
414 	if (push) {
415 		pushfile();
416 		parsefile->buf = ckmalloc(BUFSIZ);
417 	}
418 	if (parsefile->fd > 0)
419 		close(parsefile->fd);
420 	parsefile->fd = fd;
421 	if (parsefile->buf == NULL)
422 		parsefile->buf = ckmalloc(BUFSIZ);
423 	parselleft = parsenleft = 0;
424 	plinno = 1;
425 }
426 
427 
428 /*
429  * Like setinputfile, but takes input from a string.
430  */
431 
432 void
433 setinputstring(char *string, int push)
434 {
435 	INTOFF;
436 	if (push)
437 		pushfile();
438 	parsenextc = string;
439 	parselleft = parsenleft = strlen(string);
440 	parsefile->buf = NULL;
441 	plinno = 1;
442 	INTON;
443 }
444 
445 
446 
447 /*
448  * To handle the "." command, a stack of input files is used.  Pushfile
449  * adds a new entry to the stack and popfile restores the previous level.
450  */
451 
452 STATIC void
453 pushfile(void)
454 {
455 	struct parsefile *pf;
456 
457 	parsefile->nleft = parsenleft;
458 	parsefile->lleft = parselleft;
459 	parsefile->nextc = parsenextc;
460 	parsefile->linno = plinno;
461 	pf = (struct parsefile *)ckmalloc(sizeof (struct parsefile));
462 	pf->prev = parsefile;
463 	pf->fd = -1;
464 	pf->strpush = NULL;
465 	pf->basestrpush.prev = NULL;
466 	parsefile = pf;
467 }
468 
469 
470 void
471 popfile(void)
472 {
473 	struct parsefile *pf = parsefile;
474 
475 	INTOFF;
476 	if (pf->fd >= 0)
477 		close(pf->fd);
478 	if (pf->buf)
479 		ckfree(pf->buf);
480 	while (pf->strpush)
481 		popstring();
482 	parsefile = pf->prev;
483 	ckfree(pf);
484 	parsenleft = parsefile->nleft;
485 	parselleft = parsefile->lleft;
486 	parsenextc = parsefile->nextc;
487 	plinno = parsefile->linno;
488 	INTON;
489 }
490 
491 
492 /*
493  * Return to top level.
494  */
495 
496 void
497 popallfiles(void)
498 {
499 	while (parsefile != &basepf)
500 		popfile();
501 }
502 
503 
504 
505 /*
506  * Close the file(s) that the shell is reading commands from.  Called
507  * after a fork is done.
508  *
509  * Takes one arg, vfork, which tells it to not modify its global vars
510  * as it is still running in the parent.
511  *
512  * This code is (probably) unnecessary as the 'close on exec' flag is
513  * set and should be enough.  In the vfork case it is definitely wrong
514  * to close the fds as another fork() may be done later to feed data
515  * from a 'here' document into a pipe and we don't want to close the
516  * pipe!
517  */
518 
519 void
520 closescript(int vforked)
521 {
522 	if (vforked)
523 		return;
524 	popallfiles();
525 	if (parsefile->fd > 0) {
526 		close(parsefile->fd);
527 		parsefile->fd = 0;
528 	}
529 }
530