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