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"
59 #define IBUFSIZ (BUFSIZ + 1)
62 MKINIT
struct parsefile basepf
; /* top level input file */
63 MKINIT
char basebuf
[IBUFSIZ
]; /* buffer for top level input file */
64 struct parsefile
*parsefile
= &basepf
; /* current input file */
65 int whichprompt
; /* 1 == PS1, 2 == PS2 */
67 STATIC
void pushfile(void);
68 static void popstring(void);
69 static int preadfd(void);
70 static void setinputfd(int fd
, int push
);
71 static int preadbuffer(void);
81 basepf
.nextc
= basepf
.buf
= basebuf
;
86 /* clear input buffer */
89 while (basepf
.lastc
[0] != '\n' &&
90 basepf
.lastc
[0] != PEOF
&&
97 if (parsefile
->fd
> 0) {
105 static void freestrings(struct strpush
*sp
)
112 sp
->ap
->flag
&= ~ALIASINUSE
;
113 if (sp
->ap
->flag
& ALIASDEAD
) {
114 unalias(sp
->ap
->name
);
121 if (psp
!= &(parsefile
->basestrpush
))
125 parsefile
->spfree
= NULL
;
130 static int __pgetc(void)
134 if (parsefile
->unget
)
135 return parsefile
->lastc
[--parsefile
->unget
];
137 if (--parsefile
->nleft
>= 0)
138 c
= (signed char)*parsefile
->nextc
++;
142 parsefile
->lastc
[1] = parsefile
->lastc
[0];
143 parsefile
->lastc
[0] = c
;
150 * Read a character from the script, returning PEOF on end of file.
151 * Nul characters in the input are silently discarded.
156 struct strpush
*sp
= parsefile
->spfree
;
164 static int stdin_clear_nonblock(void)
166 int flags
= fcntl(0, F_GETFL
, 0);
169 flags
&=~ O_NONBLOCK
;
170 flags
= fcntl(0, F_SETFL
, flags
);
180 char *buf
= parsefile
->buf
;
181 parsefile
->nextc
= buf
;
185 if (parsefile
->fd
== 0 && el
) {
186 static const char *rl_cp
;
190 struct stackmark smark
;
191 pushstackmark(&smark
, stackblocksize());
192 rl_cp
= el_gets(el
, &el_len
);
193 popstackmark(&smark
);
199 if (nr
> IBUFSIZ
- 1)
201 memcpy(buf
, rl_cp
, nr
);
212 nr
= read(parsefile
->fd
, buf
, IBUFSIZ
- 1);
214 unsigned len
= IBUFSIZ
- 1;
221 err
= read(0, buf
, 1);
227 if (errno
!= EWOULDBLOCK
)
229 if (stdin_clear_nonblock() < 0)
232 out2str("sh: turning off NDELAY mode\n");
237 } while (!IS_DEFINED_SMALL
&& *buf
++ != '\n' && --len
);
248 * Refill the input buffer and return the next input character:
250 * 1) If a string was pushed back on the input, pop it;
251 * 2) If we are reading from a string we can't refill the buffer, return EOF.
252 * 3) If there is more stuff in this buffer, use it else call read to fill it.
253 * 4) Process input up to the next newline, deleting nul characters.
256 static int preadbuffer(void)
258 int first
= whichprompt
== 1;
264 if (unlikely(parsefile
->strpush
)) {
268 if (parsefile
->buf
== NULL
)
272 more
= input_get_lleft(parsefile
);
275 if ((more
= preadfd()) <= 0) {
276 input_set_lleft(parsefile
, parsefile
->nleft
= 0);
281 q
= parsefile
->nextc
;
283 /* delete nul characters */
292 memmove(q
, q
+ 1, more
);
298 if (IS_DEFINED_SMALL
)
303 parsefile
->nleft
= q
- parsefile
->nextc
- 1;
317 parsefile
->nleft
= q
- parsefile
->nextc
- 1;
318 if (parsefile
->nleft
< 0)
324 input_set_lleft(parsefile
, more
);
326 if (!IS_DEFINED_SMALL
)
330 if (parsefile
->fd
== 0 && hist
&& something
) {
333 history(hist
, &he
, first
? H_ENTER
: H_APPEND
,
339 out2str(parsefile
->nextc
);
345 if (!IS_DEFINED_SMALL
)
348 return (signed char)*parsefile
->nextc
++;
352 * Undo a call to pgetc. Only two characters may be pushed back.
353 * PEOF may be pushed back.
363 * Push a string back onto the input at this current parsefile level.
364 * We handle aliases this way.
367 pushstring(char *s
, void *ap
)
374 /*dprintf("*** calling pushstring: %s, %d\n", s, len);*/
375 if ((unsigned long)parsefile
->strpush
|
376 (unsigned long)parsefile
->spfree
) {
377 sp
= ckmalloc(sizeof (struct strpush
));
378 sp
->prev
= parsefile
->strpush
;
379 parsefile
->strpush
= sp
;
381 sp
= parsefile
->strpush
= &(parsefile
->basestrpush
);
382 sp
->prevstring
= parsefile
->nextc
;
383 sp
->prevnleft
= parsefile
->nleft
;
384 sp
->unget
= parsefile
->unget
;
385 sp
->spfree
= parsefile
->spfree
;
386 memcpy(sp
->lastc
, parsefile
->lastc
, sizeof(sp
->lastc
));
387 sp
->ap
= (struct alias
*)ap
;
389 ((struct alias
*)ap
)->flag
|= ALIASINUSE
;
390 sp
->string
= ((struct alias
*)ap
)->name
;
392 parsefile
->nextc
= s
;
393 parsefile
->nleft
= len
;
394 parsefile
->unget
= 0;
395 parsefile
->spfree
= NULL
;
399 static void popstring(void)
401 struct strpush
*sp
= parsefile
->strpush
;
404 if (sp
->ap
&& parsefile
->nextc
> sp
->string
) {
405 if (parsefile
->nextc
[-1] == ' ' ||
406 parsefile
->nextc
[-1] == '\t') {
407 checkkwd
|= CHKALIAS
;
409 if (sp
->string
!= sp
->ap
->name
) {
413 parsefile
->nextc
= sp
->prevstring
;
414 parsefile
->nleft
= sp
->prevnleft
;
415 parsefile
->unget
= sp
->unget
;
416 memcpy(parsefile
->lastc
, sp
->lastc
, sizeof(sp
->lastc
));
417 /*dprintf("*** calling popstring: restoring to '%s'\n", parsenextc);*/
418 parsefile
->strpush
= sp
->prev
;
419 parsefile
->spfree
= sp
;
424 * Set the input to take input from a file. If push is set, push the
425 * old input onto the stack first.
429 setinputfile(const char *fname
, int flags
)
434 fd
= sh_open(fname
, O_RDONLY
, flags
& INPUT_NOFILE_OK
);
439 setinputfd(fd
, flags
& INPUT_PUSH_FILE
);
447 * Like setinputfile, but takes an open file descriptor. Call this with
452 setinputfd(int fd
, int push
)
459 if (parsefile
->buf
== NULL
)
460 parsefile
->buf
= ckmalloc(IBUFSIZ
);
461 input_set_lleft(parsefile
, parsefile
->nleft
= 0);
467 * Like setinputfile, but takes input from a string.
471 setinputstring(char *string
)
475 parsefile
->nextc
= string
;
476 parsefile
->nleft
= strlen(string
);
477 parsefile
->buf
= NULL
;
485 * To handle the "." command, a stack of input files is used. Pushfile
486 * adds a new entry to the stack and popfile restores the previous level.
492 struct parsefile
*pf
;
494 pf
= (struct parsefile
*)ckmalloc(sizeof (struct parsefile
));
495 pf
->prev
= parsefile
;
499 pf
->basestrpush
.prev
= NULL
;
508 struct parsefile
*pf
= parsefile
;
515 if (parsefile
->spfree
)
516 freestrings(parsefile
->spfree
);
517 while (pf
->strpush
) {
519 freestrings(parsefile
->spfree
);
521 parsefile
= pf
->prev
;
527 void unwindfiles(struct parsefile
*stop
)
529 while (parsefile
!= stop
)
535 * Return to top level.
541 unwindfiles(&basepf
);