2
* Copyright (c) 1991, 1993
3
* The Regents of the University of California. All rights reserved.
4
* Copyright (c) 1997-2005
5
* Herbert Xu <herbert@gondor.apana.org.au>. All rights reserved.
7
* This code is derived from software contributed to Berkeley by
10
* Redistribution and use in source and binary forms, with or without
11
* modification, are permitted provided that the following conditions
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.
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
35
#include <stdio.h> /* defines BUFSIZ */
42
* This file implements the input routines used by the parser.
57
#include "myhistedit.h"
64
#define EOF_NLEFT -99 /* value of parsenleft when EOF pushed back */
65
#define IBUFSIZ (BUFSIZ + 1)
69
struct strpush *prev; /* preceding string on stack */
72
struct alias *ap; /* if push was associated with an alias */
73
char *string; /* remember the string since it may change */
77
* The parsefile structure pointed to by the global variable parsefile
78
* contains information about the current file being read.
83
struct parsefile *prev; /* preceding file on stack */
84
int linno; /* current line */
85
int fd; /* file descriptor (or -1 if string) */
86
int nleft; /* number of chars left in this line */
87
int lleft; /* number of chars left in this buffer */
88
char *nextc; /* next char in buffer */
89
char *buf; /* input buffer */
90
struct strpush *strpush; /* for pushing strings at this level */
91
struct strpush basestrpush; /* so pushing one is fast */
95
int plinno = 1; /* input line number */
96
int parsenleft; /* copy of parsefile->nleft */
97
MKINIT int parselleft; /* copy of parsefile->lleft */
98
char *parsenextc; /* copy of parsefile->nextc */
99
MKINIT struct parsefile basepf; /* top level input file */
100
MKINIT char basebuf[IBUFSIZ]; /* buffer for top level input file */
101
struct parsefile *parsefile = &basepf; /* current input file */
102
int whichprompt; /* 1 == PS1, 2 == PS2 */
105
EditLine *el; /* cookie for editline package */
108
STATIC void pushfile(void);
109
static int preadfd(void);
117
basepf.nextc = basepf.buf = basebuf;
121
parselleft = parsenleft = 0; /* clear input buffer */
128
* Read a line from the script.
132
pfgets(char *line, int len)
138
while (--nleft > 0) {
155
* Read a character from the script, returning PEOF on end of file.
156
* Nul characters in the input are silently discarded.
162
return pgetc_macro();
167
* Same as pgetc(), but ignores PEOA.
185
char *buf = parsefile->buf;
190
if (parsefile->fd == 0 && el) {
191
static const char *rl_cp;
195
rl_cp = el_gets(el, &el_len);
200
if (nr > IBUFSIZ - 1)
202
memcpy(buf, rl_cp, nr);
214
nr = hetio_read_input(parsefile->fd);
217
nr = read(parsefile->fd, buf, IBUFSIZ - 1);
223
if (parsefile->fd == 0 && errno == EWOULDBLOCK) {
224
int flags = fcntl(0, F_GETFL, 0);
225
if (flags >= 0 && flags & O_NONBLOCK) {
226
flags &=~ O_NONBLOCK;
227
if (fcntl(0, F_SETFL, flags) >= 0) {
228
out2str("sh: turning off NDELAY mode\n");
238
* Refill the input buffer and return the next input character:
240
* 1) If a string was pushed back on the input, pop it;
241
* 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading
242
* from a string so we can't refill the buffer, return EOF.
243
* 3) If the is more stuff in this buffer, use it else call read to fill it.
244
* 4) Process input up to the next newline, deleting nul characters.
257
while (unlikely(parsefile->strpush)) {
259
parsenleft == -1 && parsefile->strpush->ap &&
260
parsenextc[-1] != ' ' && parsenextc[-1] != '\t'
265
if (--parsenleft >= 0)
266
return (*parsenextc++);
268
if (unlikely(parsenleft == EOF_NLEFT || parsefile->buf == NULL))
278
if ((more = preadfd()) <= 0) {
279
parselleft = parsenleft = EOF_NLEFT;
286
/* delete nul characters */
297
memmove(q, q + 1, more);
302
parsenleft = q - parsenextc - 1;
319
parsenleft = q - parsenextc - 1;
331
if (parsefile->fd == 0 && hist && something) {
334
history(hist, &he, whichprompt == 1? H_ENTER : H_APPEND,
349
return *parsenextc++;
353
* Undo the last call to pgetc. Only one character may be pushed back.
354
* PEOF may be pushed back.
365
* Push a string back onto the input at this current parsefile level.
366
* We handle aliases this way.
369
pushstring(char *s, void *ap)
376
/*dprintf("*** calling pushstring: %s, %d\n", s, len);*/
377
if (parsefile->strpush) {
378
sp = ckmalloc(sizeof (struct strpush));
379
sp->prev = parsefile->strpush;
380
parsefile->strpush = sp;
382
sp = parsefile->strpush = &(parsefile->basestrpush);
383
sp->prevstring = parsenextc;
384
sp->prevnleft = parsenleft;
385
sp->ap = (struct alias *)ap;
387
((struct alias *)ap)->flag |= ALIASINUSE;
398
struct strpush *sp = parsefile->strpush;
402
if (parsenextc[-1] == ' ' || parsenextc[-1] == '\t') {
403
checkkwd |= CHKALIAS;
405
if (sp->string != sp->ap->val) {
408
sp->ap->flag &= ~ALIASINUSE;
409
if (sp->ap->flag & ALIASDEAD) {
410
unalias(sp->ap->name);
413
parsenextc = sp->prevstring;
414
parsenleft = sp->prevnleft;
415
/*dprintf("*** calling popstring: restoring to '%s'\n", parsenextc);*/
416
parsefile->strpush = sp->prev;
417
if (sp != &(parsefile->basestrpush))
423
* Set the input to take input from a file. If push is set, push the
424
* old input onto the stack first.
428
setinputfile(const char *fname, int flags)
434
if ((fd = open(fname, O_RDONLY)) < 0) {
435
if (flags & INPUT_NOFILE_OK)
437
sh_error("Can't open %s", fname);
440
fd2 = copyfd(fd, 10);
443
sh_error("Out of file descriptors");
446
setinputfd(fd, flags & INPUT_PUSH_FILE);
454
* Like setinputfile, but takes an open file descriptor. Call this with
459
setinputfd(int fd, int push)
461
(void) fcntl(fd, F_SETFD, FD_CLOEXEC);
467
if (parsefile->buf == NULL)
468
parsefile->buf = ckmalloc(IBUFSIZ);
469
parselleft = parsenleft = 0;
475
* Like setinputfile, but takes input from a string.
479
setinputstring(char *string)
484
parsenleft = strlen(string);
485
parsefile->buf = NULL;
493
* To handle the "." command, a stack of input files is used. Pushfile
494
* adds a new entry to the stack and popfile restores the previous level.
500
struct parsefile *pf;
502
parsefile->nleft = parsenleft;
503
parsefile->lleft = parselleft;
504
parsefile->nextc = parsenextc;
505
parsefile->linno = plinno;
506
pf = (struct parsefile *)ckmalloc(sizeof (struct parsefile));
507
pf->prev = parsefile;
510
pf->basestrpush.prev = NULL;
518
struct parsefile *pf = parsefile;
527
parsefile = pf->prev;
529
parsenleft = parsefile->nleft;
530
parselleft = parsefile->lleft;
531
parsenextc = parsefile->nextc;
532
plinno = parsefile->linno;
538
* Return to top level.
544
while (parsefile != &basepf)
551
* Close the file(s) that the shell is reading commands from. Called
552
* after a fork is done.
559
if (parsefile->fd > 0) {
560
close(parsefile->fd);