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.
51 #include "myhistedit.h"
60 #define IBUFSIZ (BUFSIZ + PUNGETC_MAX + 1)
63 MKINIT
struct parsefile basepf
; /* top level input file */
64 MKINIT
char basebuf
[IBUFSIZ
]; /* buffer for top level input file */
65 MKINIT
struct parsefile
*toppf
= &basepf
;
66 struct parsefile
*parsefile
= &basepf
; /* current input file */
67 int whichprompt
; /* 1 == PS1, 2 == PS2 */
69 STATIC
void pushfile(void);
70 static void popstring(void);
71 static int preadfd(void);
72 static void setinputfd(int fd
, int push
);
73 static int preadbuffer(void);
83 basepf
.nextc
= basepf
.buf
= basebuf
;
90 /* clear input buffer */
94 if (toppf
->nextc
- toppf
->buf
> toppf
->unget
)
95 c
= toppf
->nextc
[-toppf
->unget
- 1];
96 while (c
!= '\n' && c
!= PEOF
&& !int_pending())
102 if (parsefile
->fd
> 0) {
103 close(parsefile
->fd
);
110 static void freestrings(struct strpush
*sp
)
117 sp
->ap
->flag
&= ~ALIASINUSE
;
118 if (sp
->ap
->flag
& ALIASDEAD
) {
119 unalias(sp
->ap
->name
);
126 if (psp
!= &(parsefile
->basestrpush
))
130 parsefile
->spfree
= NULL
;
135 static int __pgetc(void)
139 if (parsefile
->unget
) {
140 long unget
= -(long)(unsigned)parsefile
->unget
--;
142 if (parsefile
->nleft
< 0)
143 return preadbuffer();
145 return parsefile
->nextc
[unget
];
148 if (--parsefile
->nleft
>= 0)
149 c
= (signed char)*parsefile
->nextc
++;
158 * Read a character from the script, returning PEOF on end of file.
159 * Nul characters in the input are silently discarded.
162 int __attribute__((noinline
)) pgetc(void)
164 struct strpush
*sp
= parsefile
->spfree
;
174 return parsefile
->strpush
&& parsefile
->nleft
== -1 &&
175 parsefile
->strpush
->ap
? PEOA
: pgetc();
178 static int stdin_clear_nonblock(void)
180 int flags
= fcntl(0, F_GETFL
, 0);
183 flags
&=~ O_NONBLOCK
;
184 flags
= fcntl(0, F_SETFL
, flags
);
193 char *buf
= parsefile
->buf
;
197 unget
= parsefile
->nextc
- buf
;
198 if (unget
> PUNGETC_MAX
)
201 memmove(buf
, parsefile
->nextc
- unget
, unget
);
202 parsefile
->nextc
= buf
+= unget
;
206 if (parsefile
->fd
== 0 && el
) {
207 static const char *rl_cp
;
211 struct stackmark smark
;
212 pushstackmark(&smark
, stackblocksize());
213 rl_cp
= el_gets(el
, &el_len
);
214 popstackmark(&smark
);
222 memcpy(buf
, rl_cp
, nr
);
233 nr
= read(parsefile
->fd
, buf
, BUFSIZ
);
235 unsigned len
= BUFSIZ
;
242 err
= read(0, buf
, 1);
248 if (errno
!= EWOULDBLOCK
)
250 if (stdin_clear_nonblock() < 0)
253 out2str("sh: turning off NDELAY mode\n");
258 } while (!IS_DEFINED_SMALL
&& *buf
++ != '\n' && --len
);
262 if (errno
== EINTR
&& !(basepf
.prev
&& pending_sig
))
269 * Refill the input buffer and return the next input character:
271 * 1) If a string was pushed back on the input, pop it;
272 * 2) If we are reading from a string we can't refill the buffer, return EOF.
273 * 3) If there is more stuff in this buffer, use it else call read to fill it.
274 * 4) Process input up to the next newline, deleting nul characters.
277 static int preadbuffer(void)
279 int first
= whichprompt
== 1;
285 if (unlikely(parsefile
->strpush
)) {
289 if (parsefile
->buf
== NULL
)
293 more
= input_get_lleft(parsefile
);
296 if ((more
= preadfd()) <= 0) {
297 input_set_lleft(parsefile
, parsefile
->nleft
= 0);
302 q
= parsefile
->nextc
;
304 /* delete nul characters */
313 memmove(q
, q
+ 1, more
);
319 if (IS_DEFINED_SMALL
)
324 parsefile
->nleft
= q
- parsefile
->nextc
- 1;
338 parsefile
->nleft
= q
- parsefile
->nextc
- 1;
339 if (parsefile
->nleft
< 0)
345 input_set_lleft(parsefile
, more
);
347 if (!IS_DEFINED_SMALL
)
351 if (parsefile
->fd
== 0 && hist
&& something
) {
354 history(hist
, &he
, first
? H_ENTER
: H_APPEND
,
360 out2str(parsefile
->nextc
);
366 if (!IS_DEFINED_SMALL
)
369 return (signed char)*parsefile
->nextc
++;
374 parsefile
->unget
+= n
;
378 * Undo a call to pgetc. Only two characters may be pushed back.
379 * PEOF may be pushed back.
389 * Push a string back onto the input at this current parsefile level.
390 * We handle aliases this way.
393 pushstring(char *s
, void *ap
)
400 /*dprintf("*** calling pushstring: %s, %d\n", s, len);*/
401 if ((unsigned long)parsefile
->strpush
|
402 (unsigned long)parsefile
->spfree
) {
403 sp
= ckmalloc(sizeof (struct strpush
));
404 sp
->prev
= parsefile
->strpush
;
405 parsefile
->strpush
= sp
;
407 sp
= parsefile
->strpush
= &(parsefile
->basestrpush
);
408 sp
->prevstring
= parsefile
->nextc
;
409 sp
->prevnleft
= parsefile
->nleft
;
410 sp
->unget
= parsefile
->unget
;
411 sp
->spfree
= parsefile
->spfree
;
412 sp
->ap
= (struct alias
*)ap
;
414 ((struct alias
*)ap
)->flag
|= ALIASINUSE
;
415 sp
->string
= ((struct alias
*)ap
)->name
;
417 parsefile
->nextc
= s
;
418 parsefile
->nleft
= len
;
419 parsefile
->unget
= 0;
420 parsefile
->spfree
= NULL
;
424 static void popstring(void)
426 struct strpush
*sp
= parsefile
->strpush
;
429 if (sp
->ap
&& parsefile
->nextc
> sp
->string
) {
430 if (parsefile
->nextc
[-1] == ' ' ||
431 parsefile
->nextc
[-1] == '\t') {
432 checkkwd
|= CHKALIAS
;
434 if (sp
->string
!= sp
->ap
->name
) {
438 parsefile
->nextc
= sp
->prevstring
;
439 parsefile
->nleft
= sp
->prevnleft
;
440 parsefile
->unget
= sp
->unget
;
441 /*dprintf("*** calling popstring: restoring to '%s'\n", parsenextc);*/
442 parsefile
->strpush
= sp
->prev
;
443 parsefile
->spfree
= sp
;
448 * Set the input to take input from a file. If push is set, push the
449 * old input onto the stack first.
453 setinputfile(const char *fname
, int flags
)
458 fd
= sh_open(fname
, O_RDONLY
, flags
& INPUT_NOFILE_OK
);
463 setinputfd(fd
, flags
& INPUT_PUSH_FILE
);
471 * Like setinputfile, but takes an open file descriptor. Call this with
476 setinputfd(int fd
, int push
)
482 parsefile
->nextc
= parsefile
->buf
= ckmalloc(IBUFSIZ
);
483 input_set_lleft(parsefile
, parsefile
->nleft
= 0);
489 * Like setinputfile, but takes input from a string.
493 setinputstring(char *string
)
497 parsefile
->nextc
= string
;
498 parsefile
->nleft
= strlen(string
);
499 parsefile
->buf
= NULL
;
507 * To handle the "." command, a stack of input files is used. Pushfile
508 * adds a new entry to the stack and popfile restores the previous level.
514 struct parsefile
*pf
;
516 pf
= (struct parsefile
*)ckmalloc(sizeof (struct parsefile
));
517 pf
->prev
= parsefile
;
521 pf
->basestrpush
.prev
= NULL
;
529 basepf
.prev
= parsefile
;
537 struct parsefile
*pf
= parsefile
;
540 parsefile
= pf
->prev
;
549 if (parsefile
->spfree
)
550 freestrings(parsefile
->spfree
);
551 while (pf
->strpush
) {
553 freestrings(parsefile
->spfree
);
562 void __attribute__((noinline
)) unwindfiles(struct parsefile
*stop
)
564 while (basepf
.prev
|| parsefile
!= stop
)
570 * Return to top level.