Home | History | Annotate | Line # | Download | only in sh
input.c revision 1.1
      1  1.1  cgd /*-
      2  1.1  cgd  * Copyright (c) 1991 The Regents of the University of California.
      3  1.1  cgd  * All rights reserved.
      4  1.1  cgd  *
      5  1.1  cgd  * This code is derived from software contributed to Berkeley by
      6  1.1  cgd  * Kenneth Almquist.
      7  1.1  cgd  *
      8  1.1  cgd  * Redistribution and use in source and binary forms, with or without
      9  1.1  cgd  * modification, are permitted provided that the following conditions
     10  1.1  cgd  * are met:
     11  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
     12  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     13  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     16  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     17  1.1  cgd  *    must display the following acknowledgement:
     18  1.1  cgd  *	This product includes software developed by the University of
     19  1.1  cgd  *	California, Berkeley and its contributors.
     20  1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     21  1.1  cgd  *    may be used to endorse or promote products derived from this software
     22  1.1  cgd  *    without specific prior written permission.
     23  1.1  cgd  *
     24  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.1  cgd  * SUCH DAMAGE.
     35  1.1  cgd  */
     36  1.1  cgd 
     37  1.1  cgd #ifndef lint
     38  1.1  cgd static char sccsid[] = "@(#)input.c	5.4 (Berkeley) 7/1/91";
     39  1.1  cgd #endif /* not lint */
     40  1.1  cgd 
     41  1.1  cgd /*
     42  1.1  cgd  * This file implements the input routines used by the parser.
     43  1.1  cgd  */
     44  1.1  cgd 
     45  1.1  cgd #include <stdio.h>	/* defines BUFSIZ */
     46  1.1  cgd #include "shell.h"
     47  1.1  cgd #include <fcntl.h>
     48  1.1  cgd #include <errno.h>
     49  1.1  cgd #include "syntax.h"
     50  1.1  cgd #include "input.h"
     51  1.1  cgd #include "output.h"
     52  1.1  cgd #include "memalloc.h"
     53  1.1  cgd #include "error.h"
     54  1.1  cgd 
     55  1.1  cgd #define EOF_NLEFT -99		/* value of parsenleft when EOF pushed back */
     56  1.1  cgd 
     57  1.1  cgd 
     58  1.1  cgd /*
     59  1.1  cgd  * The parsefile structure pointed to by the global variable parsefile
     60  1.1  cgd  * contains information about the current file being read.
     61  1.1  cgd  */
     62  1.1  cgd 
     63  1.1  cgd MKINIT
     64  1.1  cgd struct parsefile {
     65  1.1  cgd 	int linno;		/* current line */
     66  1.1  cgd 	int fd;			/* file descriptor (or -1 if string) */
     67  1.1  cgd 	int nleft;		/* number of chars left in buffer */
     68  1.1  cgd 	char *nextc;		/* next char in buffer */
     69  1.1  cgd 	struct parsefile *prev;	/* preceding file on stack */
     70  1.1  cgd 	char *buf;		/* input buffer */
     71  1.1  cgd };
     72  1.1  cgd 
     73  1.1  cgd 
     74  1.1  cgd int plinno = 1;			/* input line number */
     75  1.1  cgd MKINIT int parsenleft;		/* copy of parsefile->nleft */
     76  1.1  cgd char *parsenextc;		/* copy of parsefile->nextc */
     77  1.1  cgd MKINIT struct parsefile basepf;	/* top level input file */
     78  1.1  cgd char basebuf[BUFSIZ];		/* buffer for top level input file */
     79  1.1  cgd struct parsefile *parsefile = &basepf;	/* current input file */
     80  1.1  cgd char *pushedstring;		/* copy of parsenextc when text pushed back */
     81  1.1  cgd int pushednleft;		/* copy of parsenleft when text pushed back */
     82  1.1  cgd 
     83  1.1  cgd #ifdef __STDC__
     84  1.1  cgd STATIC void pushfile(void);
     85  1.1  cgd #else
     86  1.1  cgd STATIC void pushfile();
     87  1.1  cgd #endif
     88  1.1  cgd 
     89  1.1  cgd 
     90  1.1  cgd 
     91  1.1  cgd #ifdef mkinit
     92  1.1  cgd INCLUDE "input.h"
     93  1.1  cgd INCLUDE "error.h"
     94  1.1  cgd 
     95  1.1  cgd INIT {
     96  1.1  cgd 	extern char basebuf[];
     97  1.1  cgd 
     98  1.1  cgd 	basepf.nextc = basepf.buf = basebuf;
     99  1.1  cgd }
    100  1.1  cgd 
    101  1.1  cgd RESET {
    102  1.1  cgd 	if (exception != EXSHELLPROC)
    103  1.1  cgd 		parsenleft = 0;            /* clear input buffer */
    104  1.1  cgd 	popallfiles();
    105  1.1  cgd }
    106  1.1  cgd 
    107  1.1  cgd SHELLPROC {
    108  1.1  cgd 	popallfiles();
    109  1.1  cgd }
    110  1.1  cgd #endif
    111  1.1  cgd 
    112  1.1  cgd 
    113  1.1  cgd /*
    114  1.1  cgd  * Read a line from the script.
    115  1.1  cgd  */
    116  1.1  cgd 
    117  1.1  cgd char *
    118  1.1  cgd pfgets(line, len)
    119  1.1  cgd 	char *line;
    120  1.1  cgd 	{
    121  1.1  cgd 	register char *p = line;
    122  1.1  cgd 	int nleft = len;
    123  1.1  cgd 	int c;
    124  1.1  cgd 
    125  1.1  cgd 	while (--nleft > 0) {
    126  1.1  cgd 		c = pgetc_macro();
    127  1.1  cgd 		if (c == PEOF) {
    128  1.1  cgd 			if (p == line)
    129  1.1  cgd 				return NULL;
    130  1.1  cgd 			break;
    131  1.1  cgd 		}
    132  1.1  cgd 		*p++ = c;
    133  1.1  cgd 		if (c == '\n')
    134  1.1  cgd 			break;
    135  1.1  cgd 	}
    136  1.1  cgd 	*p = '\0';
    137  1.1  cgd 	return line;
    138  1.1  cgd }
    139  1.1  cgd 
    140  1.1  cgd 
    141  1.1  cgd 
    142  1.1  cgd /*
    143  1.1  cgd  * Read a character from the script, returning PEOF on end of file.
    144  1.1  cgd  * Nul characters in the input are silently discarded.
    145  1.1  cgd  */
    146  1.1  cgd 
    147  1.1  cgd int
    148  1.1  cgd pgetc() {
    149  1.1  cgd 	return pgetc_macro();
    150  1.1  cgd }
    151  1.1  cgd 
    152  1.1  cgd 
    153  1.1  cgd /*
    154  1.1  cgd  * Refill the input buffer and return the next input character:
    155  1.1  cgd  *
    156  1.1  cgd  * 1) If a string was pushed back on the input, switch back to the regular
    157  1.1  cgd  *    buffer.
    158  1.1  cgd  * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading
    159  1.1  cgd  *    from a string so we can't refill the buffer, return EOF.
    160  1.1  cgd  * 3) Call read to read in the characters.
    161  1.1  cgd  * 4) Delete all nul characters from the buffer.
    162  1.1  cgd  */
    163  1.1  cgd 
    164  1.1  cgd int
    165  1.1  cgd preadbuffer() {
    166  1.1  cgd 	register char *p, *q;
    167  1.1  cgd 	register int i;
    168  1.1  cgd 
    169  1.1  cgd 	if (pushedstring) {
    170  1.1  cgd 		parsenextc = pushedstring;
    171  1.1  cgd 		pushedstring = NULL;
    172  1.1  cgd 		parsenleft = pushednleft;
    173  1.1  cgd 		if (--parsenleft >= 0)
    174  1.1  cgd 			return *parsenextc++;
    175  1.1  cgd 	}
    176  1.1  cgd 	if (parsenleft == EOF_NLEFT || parsefile->buf == NULL)
    177  1.1  cgd 		return PEOF;
    178  1.1  cgd 	flushout(&output);
    179  1.1  cgd 	flushout(&errout);
    180  1.1  cgd retry:
    181  1.1  cgd 	p = parsenextc = parsefile->buf;
    182  1.1  cgd 	i = read(parsefile->fd, p, BUFSIZ);
    183  1.1  cgd 	if (i <= 0) {
    184  1.1  cgd                 if (i < 0) {
    185  1.1  cgd                         if (errno == EINTR)
    186  1.1  cgd                                 goto retry;
    187  1.1  cgd                         if (parsefile->fd == 0 && errno == EWOULDBLOCK) {
    188  1.1  cgd                                 int flags = fcntl(0, F_GETFL, 0);
    189  1.1  cgd                                 if (flags >= 0 && flags & O_NONBLOCK) {
    190  1.1  cgd                                         flags &=~ O_NONBLOCK;
    191  1.1  cgd                                         if (fcntl(0, F_SETFL, flags) >= 0) {
    192  1.1  cgd 						out2str("sh: turning off NDELAY mode\n");
    193  1.1  cgd                                                 goto retry;
    194  1.1  cgd                                         }
    195  1.1  cgd                                 }
    196  1.1  cgd                         }
    197  1.1  cgd                 }
    198  1.1  cgd                 parsenleft = EOF_NLEFT;
    199  1.1  cgd                 return PEOF;
    200  1.1  cgd 	}
    201  1.1  cgd 	parsenleft = i - 1;
    202  1.1  cgd 
    203  1.1  cgd 	/* delete nul characters */
    204  1.1  cgd 	for (;;) {
    205  1.1  cgd 		if (*p++ == '\0')
    206  1.1  cgd 			break;
    207  1.1  cgd 		if (--i <= 0)
    208  1.1  cgd 			return *parsenextc++;		/* no nul characters */
    209  1.1  cgd 	}
    210  1.1  cgd 	q = p - 1;
    211  1.1  cgd 	while (--i > 0) {
    212  1.1  cgd 		if (*p != '\0')
    213  1.1  cgd 			*q++ = *p;
    214  1.1  cgd 		p++;
    215  1.1  cgd 	}
    216  1.1  cgd 	if (q == parsefile->buf)
    217  1.1  cgd 		goto retry;			/* buffer contained nothing but nuls */
    218  1.1  cgd 	parsenleft = q - parsefile->buf - 1;
    219  1.1  cgd 	return *parsenextc++;
    220  1.1  cgd }
    221  1.1  cgd 
    222  1.1  cgd 
    223  1.1  cgd /*
    224  1.1  cgd  * Undo the last call to pgetc.  Only one character may be pushed back.
    225  1.1  cgd  * PEOF may be pushed back.
    226  1.1  cgd  */
    227  1.1  cgd 
    228  1.1  cgd void
    229  1.1  cgd pungetc() {
    230  1.1  cgd 	parsenleft++;
    231  1.1  cgd 	parsenextc--;
    232  1.1  cgd }
    233  1.1  cgd 
    234  1.1  cgd 
    235  1.1  cgd /*
    236  1.1  cgd  * Push a string back onto the input.  This code doesn't work if the user
    237  1.1  cgd  * tries to push back more than one string at once.
    238  1.1  cgd  */
    239  1.1  cgd 
    240  1.1  cgd void
    241  1.1  cgd ppushback(string, length)
    242  1.1  cgd 	char *string;
    243  1.1  cgd 	{
    244  1.1  cgd 	pushedstring = parsenextc;
    245  1.1  cgd 	pushednleft = parsenleft;
    246  1.1  cgd 	parsenextc = string;
    247  1.1  cgd 	parsenleft = length;
    248  1.1  cgd }
    249  1.1  cgd 
    250  1.1  cgd 
    251  1.1  cgd 
    252  1.1  cgd /*
    253  1.1  cgd  * Set the input to take input from a file.  If push is set, push the
    254  1.1  cgd  * old input onto the stack first.
    255  1.1  cgd  */
    256  1.1  cgd 
    257  1.1  cgd void
    258  1.1  cgd setinputfile(fname, push)
    259  1.1  cgd 	char *fname;
    260  1.1  cgd 	{
    261  1.1  cgd 	int fd;
    262  1.1  cgd 	int fd2;
    263  1.1  cgd 
    264  1.1  cgd 	INTOFF;
    265  1.1  cgd 	if ((fd = open(fname, O_RDONLY)) < 0)
    266  1.1  cgd 		error("Can't open %s", fname);
    267  1.1  cgd 	if (fd < 10) {
    268  1.1  cgd 		fd2 = copyfd(fd, 10);
    269  1.1  cgd 		close(fd);
    270  1.1  cgd 		if (fd2 < 0)
    271  1.1  cgd 			error("Out of file descriptors");
    272  1.1  cgd 		fd = fd2;
    273  1.1  cgd 	}
    274  1.1  cgd 	setinputfd(fd, push);
    275  1.1  cgd 	INTON;
    276  1.1  cgd }
    277  1.1  cgd 
    278  1.1  cgd 
    279  1.1  cgd /*
    280  1.1  cgd  * Like setinputfile, but takes an open file descriptor.  Call this with
    281  1.1  cgd  * interrupts off.
    282  1.1  cgd  */
    283  1.1  cgd 
    284  1.1  cgd void
    285  1.1  cgd setinputfd(fd, push) {
    286  1.1  cgd 	if (push) {
    287  1.1  cgd 		pushfile();
    288  1.1  cgd 		parsefile->buf = ckmalloc(BUFSIZ);
    289  1.1  cgd 	}
    290  1.1  cgd 	if (parsefile->fd > 0)
    291  1.1  cgd 		close(parsefile->fd);
    292  1.1  cgd 	parsefile->fd = fd;
    293  1.1  cgd 	if (parsefile->buf == NULL)
    294  1.1  cgd 		parsefile->buf = ckmalloc(BUFSIZ);
    295  1.1  cgd 	parsenleft = 0;
    296  1.1  cgd 	plinno = 1;
    297  1.1  cgd }
    298  1.1  cgd 
    299  1.1  cgd 
    300  1.1  cgd /*
    301  1.1  cgd  * Like setinputfile, but takes input from a string.
    302  1.1  cgd  */
    303  1.1  cgd 
    304  1.1  cgd void
    305  1.1  cgd setinputstring(string, push)
    306  1.1  cgd 	char *string;
    307  1.1  cgd 	{
    308  1.1  cgd 	INTOFF;
    309  1.1  cgd 	if (push)
    310  1.1  cgd 		pushfile();
    311  1.1  cgd 	parsenextc = string;
    312  1.1  cgd 	parsenleft = strlen(string);
    313  1.1  cgd 	parsefile->buf = NULL;
    314  1.1  cgd 	plinno = 1;
    315  1.1  cgd 	INTON;
    316  1.1  cgd }
    317  1.1  cgd 
    318  1.1  cgd 
    319  1.1  cgd 
    320  1.1  cgd /*
    321  1.1  cgd  * To handle the "." command, a stack of input files is used.  Pushfile
    322  1.1  cgd  * adds a new entry to the stack and popfile restores the previous level.
    323  1.1  cgd  */
    324  1.1  cgd 
    325  1.1  cgd STATIC void
    326  1.1  cgd pushfile() {
    327  1.1  cgd 	struct parsefile *pf;
    328  1.1  cgd 
    329  1.1  cgd 	parsefile->nleft = parsenleft;
    330  1.1  cgd 	parsefile->nextc = parsenextc;
    331  1.1  cgd 	parsefile->linno = plinno;
    332  1.1  cgd 	pf = (struct parsefile *)ckmalloc(sizeof (struct parsefile));
    333  1.1  cgd 	pf->prev = parsefile;
    334  1.1  cgd 	pf->fd = -1;
    335  1.1  cgd 	parsefile = pf;
    336  1.1  cgd }
    337  1.1  cgd 
    338  1.1  cgd 
    339  1.1  cgd void
    340  1.1  cgd popfile() {
    341  1.1  cgd 	struct parsefile *pf = parsefile;
    342  1.1  cgd 
    343  1.1  cgd 	INTOFF;
    344  1.1  cgd 	if (pf->fd >= 0)
    345  1.1  cgd 		close(pf->fd);
    346  1.1  cgd 	if (pf->buf)
    347  1.1  cgd 		ckfree(pf->buf);
    348  1.1  cgd 	parsefile = pf->prev;
    349  1.1  cgd 	ckfree(pf);
    350  1.1  cgd 	parsenleft = parsefile->nleft;
    351  1.1  cgd 	parsenextc = parsefile->nextc;
    352  1.1  cgd 	plinno = parsefile->linno;
    353  1.1  cgd 	INTON;
    354  1.1  cgd }
    355  1.1  cgd 
    356  1.1  cgd 
    357  1.1  cgd /*
    358  1.1  cgd  * Return to top level.
    359  1.1  cgd  */
    360  1.1  cgd 
    361  1.1  cgd void
    362  1.1  cgd popallfiles() {
    363  1.1  cgd 	while (parsefile != &basepf)
    364  1.1  cgd 		popfile();
    365  1.1  cgd }
    366  1.1  cgd 
    367  1.1  cgd 
    368  1.1  cgd 
    369  1.1  cgd /*
    370  1.1  cgd  * Close the file(s) that the shell is reading commands from.  Called
    371  1.1  cgd  * after a fork is done.
    372  1.1  cgd  */
    373  1.1  cgd 
    374  1.1  cgd void
    375  1.1  cgd closescript() {
    376  1.1  cgd 	popallfiles();
    377  1.1  cgd 	if (parsefile->fd > 0) {
    378  1.1  cgd 		close(parsefile->fd);
    379  1.1  cgd 		parsefile->fd = 0;
    380  1.1  cgd 	}
    381  1.1  cgd }
    382