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
)
96 if (parsefile
->fd
> 0) {
104 static void freestrings(struct strpush
*sp
)
111 sp
->ap
->flag
&= ~ALIASINUSE
;
112 if (sp
->ap
->flag
& ALIASDEAD
) {
113 unalias(sp
->ap
->name
);
120 if (psp
!= &(parsefile
->basestrpush
))
124 parsefile
->spfree
= NULL
;
129 static int __pgetc(void)
133 if (parsefile
->unget
)
134 return parsefile
->lastc
[--parsefile
->unget
];
136 if (--parsefile
->nleft
>= 0)
137 c
= (signed char)*parsefile
->nextc
++;
141 parsefile
->lastc
[1] = parsefile
->lastc
[0];
142 parsefile
->lastc
[0] = c
;
149 * Read a character from the script, returning PEOF on end of file.
150 * Nul characters in the input are silently discarded.
155 struct strpush
*sp
= parsefile
->spfree
;
163 static int stdin_clear_nonblock(void)
165 int flags
= fcntl(0, F_GETFL
, 0);
168 flags
&=~ O_NONBLOCK
;
169 flags
= fcntl(0, F_SETFL
, flags
);
179 char *buf
= parsefile
->buf
;
180 parsefile
->nextc
= buf
;
184 if (parsefile
->fd
== 0 && el
) {
185 static const char *rl_cp
;
189 struct stackmark smark
;
190 pushstackmark(&smark
, stackblocksize());
191 rl_cp
= el_gets(el
, &el_len
);
192 popstackmark(&smark
);
198 if (nr
> IBUFSIZ
- 1)
200 memcpy(buf
, rl_cp
, nr
);
211 nr
= read(parsefile
->fd
, buf
, IBUFSIZ
- 1);
213 unsigned len
= IBUFSIZ
- 1;
220 err
= read(0, buf
, 1);
226 if (errno
!= EWOULDBLOCK
)
228 if (stdin_clear_nonblock() < 0)
231 out2str("sh: turning off NDELAY mode\n");
236 } while (!IS_DEFINED_SMALL
&& *buf
++ != '\n' && --len
);
247 * Refill the input buffer and return the next input character:
249 * 1) If a string was pushed back on the input, pop it;
250 * 2) If we are reading from a string we can't refill the buffer, return EOF.
251 * 3) If there is more stuff in this buffer, use it else call read to fill it.
252 * 4) Process input up to the next newline, deleting nul characters.
255 static int preadbuffer(void)
262 if (unlikely(parsefile
->strpush
)) {
266 if (parsefile
->buf
== NULL
)
270 more
= input_get_lleft(parsefile
);
273 if ((more
= preadfd()) <= 0) {
274 input_set_lleft(parsefile
, parsefile
->nleft
= 0);
279 q
= parsefile
->nextc
;
281 /* delete nul characters */
290 memmove(q
, q
+ 1, more
);
296 if (IS_DEFINED_SMALL
)
301 parsefile
->nleft
= q
- parsefile
->nextc
- 1;
315 parsefile
->nleft
= q
- parsefile
->nextc
- 1;
316 if (parsefile
->nleft
< 0)
321 input_set_lleft(parsefile
, more
);
323 if (!IS_DEFINED_SMALL
)
327 if (parsefile
->fd
== 0 && hist
&& something
) {
330 history(hist
, &he
, whichprompt
== 1? H_ENTER
: H_APPEND
,
336 out2str(parsefile
->nextc
);
342 if (!IS_DEFINED_SMALL
)
345 return (signed char)*parsefile
->nextc
++;
349 * Undo a call to pgetc. Only two characters may be pushed back.
350 * PEOF may be pushed back.
360 * Push a string back onto the input at this current parsefile level.
361 * We handle aliases this way.
364 pushstring(char *s
, void *ap
)
371 /*dprintf("*** calling pushstring: %s, %d\n", s, len);*/
372 if ((unsigned long)parsefile
->strpush
|
373 (unsigned long)parsefile
->spfree
) {
374 sp
= ckmalloc(sizeof (struct strpush
));
375 sp
->prev
= parsefile
->strpush
;
376 parsefile
->strpush
= sp
;
378 sp
= parsefile
->strpush
= &(parsefile
->basestrpush
);
379 sp
->prevstring
= parsefile
->nextc
;
380 sp
->prevnleft
= parsefile
->nleft
;
381 sp
->unget
= parsefile
->unget
;
382 sp
->spfree
= parsefile
->spfree
;
383 memcpy(sp
->lastc
, parsefile
->lastc
, sizeof(sp
->lastc
));
384 sp
->ap
= (struct alias
*)ap
;
386 ((struct alias
*)ap
)->flag
|= ALIASINUSE
;
389 parsefile
->nextc
= s
;
390 parsefile
->nleft
= len
;
391 parsefile
->unget
= 0;
392 parsefile
->spfree
= NULL
;
396 static void popstring(void)
398 struct strpush
*sp
= parsefile
->strpush
;
402 if (parsefile
->nextc
[-1] == ' ' ||
403 parsefile
->nextc
[-1] == '\t') {
404 checkkwd
|= CHKALIAS
;
406 if (sp
->string
!= sp
->ap
->val
) {
410 parsefile
->nextc
= sp
->prevstring
;
411 parsefile
->nleft
= sp
->prevnleft
;
412 parsefile
->unget
= sp
->unget
;
413 memcpy(parsefile
->lastc
, sp
->lastc
, sizeof(sp
->lastc
));
414 /*dprintf("*** calling popstring: restoring to '%s'\n", parsenextc);*/
415 parsefile
->strpush
= sp
->prev
;
416 parsefile
->spfree
= sp
;
421 * Set the input to take input from a file. If push is set, push the
422 * old input onto the stack first.
426 setinputfile(const char *fname
, int flags
)
431 fd
= sh_open(fname
, O_RDONLY
, flags
& INPUT_NOFILE_OK
);
436 setinputfd(fd
, flags
& INPUT_PUSH_FILE
);
444 * Like setinputfile, but takes an open file descriptor. Call this with
449 setinputfd(int fd
, int push
)
456 if (parsefile
->buf
== NULL
)
457 parsefile
->buf
= ckmalloc(IBUFSIZ
);
458 input_set_lleft(parsefile
, parsefile
->nleft
= 0);
464 * Like setinputfile, but takes input from a string.
468 setinputstring(char *string
)
472 parsefile
->nextc
= string
;
473 parsefile
->nleft
= strlen(string
);
474 parsefile
->buf
= NULL
;
482 * To handle the "." command, a stack of input files is used. Pushfile
483 * adds a new entry to the stack and popfile restores the previous level.
489 struct parsefile
*pf
;
491 pf
= (struct parsefile
*)ckmalloc(sizeof (struct parsefile
));
492 pf
->prev
= parsefile
;
496 pf
->basestrpush
.prev
= NULL
;
505 struct parsefile
*pf
= parsefile
;
512 if (parsefile
->spfree
)
513 freestrings(parsefile
->spfree
);
514 while (pf
->strpush
) {
516 freestrings(parsefile
->spfree
);
518 parsefile
= pf
->prev
;
524 void unwindfiles(struct parsefile
*stop
)
526 while (parsefile
!= stop
)
532 * Return to top level.
538 unwindfiles(&basepf
);