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
;
198 nr
= input_get_lleft(parsefile
);
200 unget
= parsefile
->nextc
- buf
;
201 if (unget
> PUNGETC_MAX
)
204 memmove(buf
, parsefile
->nextc
- unget
, unget
+ nr
);
206 parsefile
->nextc
= buf
;
210 if (!IS_DEFINED_SMALL
&& !nr
)
217 if (parsefile
->fd
== 0 && el
) {
218 static const char *rl_cp
;
222 struct stackmark smark
;
223 pushstackmark(&smark
, stackblocksize());
224 rl_cp
= el_gets(el
, &el_len
);
225 popstackmark(&smark
);
232 memcpy(buf
, rl_cp
, nr
);
243 nr
= read(parsefile
->fd
, buf
, nr
);
250 err
= read(0, buf
, 1);
256 if (errno
!= EWOULDBLOCK
)
258 if (stdin_clear_nonblock() < 0)
261 out2str("sh: turning off NDELAY mode\n");
270 if (errno
== EINTR
&& !(basepf
.prev
&& pending_sig
))
277 * Refill the input buffer and return the next input character:
279 * 1) If a string was pushed back on the input, pop it;
280 * 2) If we are reading from a string we can't refill the buffer, return EOF.
281 * 3) If there is more stuff in this buffer, use it else call read to fill it.
282 * 4) Process input up to the next newline, deleting nul characters.
285 static int preadbuffer(void)
287 int first
= whichprompt
== 1;
293 if (unlikely(parsefile
->strpush
)) {
297 if (parsefile
->buf
== NULL
)
301 q
= parsefile
->nextc
;
304 more
= input_get_lleft(parsefile
);
309 nr
= q
- parsefile
->nextc
;
311 q
= parsefile
->nextc
+ nr
;
313 input_set_lleft(parsefile
, parsefile
->nleft
= 0);
314 if (!IS_DEFINED_SMALL
&& nr
> 0)
320 /* delete nul characters */
328 memmove(q
, q
+ 1, more
);
334 if (IS_DEFINED_SMALL
)
352 if (!IS_DEFINED_SMALL
)
358 input_set_lleft(parsefile
, more
);
361 parsefile
->nleft
= q
- parsefile
->nextc
- 1;
362 if (!IS_DEFINED_SMALL
)
366 if (parsefile
->fd
== 0 && hist
&& something
) {
369 history(hist
, &he
, first
? H_ENTER
: H_APPEND
,
375 out2str(parsefile
->nextc
);
381 if (!IS_DEFINED_SMALL
)
384 return (signed char)*parsefile
->nextc
++;
389 parsefile
->unget
+= n
;
393 * Undo a call to pgetc. Only two characters may be pushed back.
394 * PEOF may be pushed back.
404 * Push a string back onto the input at this current parsefile level.
405 * We handle aliases this way.
408 pushstring(char *s
, void *ap
)
415 /*dprintf("*** calling pushstring: %s, %d\n", s, len);*/
416 if ((unsigned long)parsefile
->strpush
|
417 (unsigned long)parsefile
->spfree
) {
418 sp
= ckmalloc(sizeof (struct strpush
));
419 sp
->prev
= parsefile
->strpush
;
420 parsefile
->strpush
= sp
;
422 sp
= parsefile
->strpush
= &(parsefile
->basestrpush
);
423 sp
->prevstring
= parsefile
->nextc
;
424 sp
->prevnleft
= parsefile
->nleft
;
425 sp
->unget
= parsefile
->unget
;
426 sp
->spfree
= parsefile
->spfree
;
427 sp
->ap
= (struct alias
*)ap
;
429 ((struct alias
*)ap
)->flag
|= ALIASINUSE
;
430 sp
->string
= ((struct alias
*)ap
)->name
;
432 parsefile
->nextc
= s
;
433 parsefile
->nleft
= len
;
434 parsefile
->unget
= 0;
435 parsefile
->spfree
= NULL
;
439 static void popstring(void)
441 struct strpush
*sp
= parsefile
->strpush
;
444 if (sp
->ap
&& parsefile
->nextc
> sp
->string
) {
445 if (parsefile
->nextc
[-1] == ' ' ||
446 parsefile
->nextc
[-1] == '\t') {
447 checkkwd
|= CHKALIAS
;
449 if (sp
->string
!= sp
->ap
->name
) {
453 parsefile
->nextc
= sp
->prevstring
;
454 parsefile
->nleft
= sp
->prevnleft
;
455 parsefile
->unget
= sp
->unget
;
456 /*dprintf("*** calling popstring: restoring to '%s'\n", parsenextc);*/
457 parsefile
->strpush
= sp
->prev
;
458 parsefile
->spfree
= sp
;
463 * Set the input to take input from a file. If push is set, push the
464 * old input onto the stack first.
468 setinputfile(const char *fname
, int flags
)
473 fd
= sh_open(fname
, O_RDONLY
, flags
& INPUT_NOFILE_OK
);
478 setinputfd(fd
, flags
& INPUT_PUSH_FILE
);
486 * Like setinputfile, but takes an open file descriptor. Call this with
491 setinputfd(int fd
, int push
)
497 parsefile
->nextc
= parsefile
->buf
= ckmalloc(IBUFSIZ
);
498 input_set_lleft(parsefile
, parsefile
->nleft
= 0);
504 * Like setinputfile, but takes input from a string.
508 setinputstring(char *string
)
512 parsefile
->nextc
= string
;
513 parsefile
->nleft
= strlen(string
);
514 parsefile
->buf
= NULL
;
522 * To handle the "." command, a stack of input files is used. Pushfile
523 * adds a new entry to the stack and popfile restores the previous level.
529 struct parsefile
*pf
;
531 pf
= (struct parsefile
*)ckmalloc(sizeof (struct parsefile
));
532 pf
->prev
= parsefile
;
536 pf
->basestrpush
.prev
= NULL
;
544 basepf
.prev
= parsefile
;
552 struct parsefile
*pf
= parsefile
;
555 parsefile
= pf
->prev
;
564 if (parsefile
->spfree
)
565 freestrings(parsefile
->spfree
);
566 while (pf
->strpush
) {
568 freestrings(parsefile
->spfree
);
577 void __attribute__((noinline
)) unwindfiles(struct parsefile
*stop
)
579 while (basepf
.prev
|| parsefile
!= stop
)
585 * Return to top level.