input.c revision 1.58 1 1.58 kre /* $NetBSD: input.c,v 1.58 2017/06/07 05:08:32 kre Exp $ */
2 1.13 cgd
3 1.1 cgd /*-
4 1.5 jtc * Copyright (c) 1991, 1993
5 1.5 jtc * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Kenneth Almquist.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.39 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd */
34 1.1 cgd
35 1.27 christos #include <sys/cdefs.h>
36 1.1 cgd #ifndef lint
37 1.13 cgd #if 0
38 1.16 christos static char sccsid[] = "@(#)input.c 8.3 (Berkeley) 6/9/95";
39 1.13 cgd #else
40 1.58 kre __RCSID("$NetBSD: input.c,v 1.58 2017/06/07 05:08:32 kre Exp $");
41 1.13 cgd #endif
42 1.1 cgd #endif /* not lint */
43 1.1 cgd
44 1.14 christos #include <stdio.h> /* defines BUFSIZ */
45 1.14 christos #include <fcntl.h>
46 1.14 christos #include <errno.h>
47 1.14 christos #include <unistd.h>
48 1.45 christos #include <limits.h>
49 1.14 christos #include <stdlib.h>
50 1.15 cgd #include <string.h>
51 1.51 kre #include <sys/stat.h>
52 1.14 christos
53 1.1 cgd /*
54 1.1 cgd * This file implements the input routines used by the parser.
55 1.1 cgd */
56 1.1 cgd
57 1.6 jtc #include "shell.h"
58 1.14 christos #include "redir.h"
59 1.1 cgd #include "syntax.h"
60 1.1 cgd #include "input.h"
61 1.1 cgd #include "output.h"
62 1.5 jtc #include "options.h"
63 1.1 cgd #include "memalloc.h"
64 1.1 cgd #include "error.h"
65 1.5 jtc #include "alias.h"
66 1.5 jtc #include "parser.h"
67 1.5 jtc #include "myhistedit.h"
68 1.58 kre #include "show.h"
69 1.1 cgd
70 1.1 cgd #define EOF_NLEFT -99 /* value of parsenleft when EOF pushed back */
71 1.1 cgd
72 1.5 jtc MKINIT
73 1.5 jtc struct strpush {
74 1.5 jtc struct strpush *prev; /* preceding string on stack */
75 1.55 kre const char *prevstring;
76 1.5 jtc int prevnleft;
77 1.17 christos int prevlleft;
78 1.5 jtc struct alias *ap; /* if push was associated with an alias */
79 1.5 jtc };
80 1.1 cgd
81 1.1 cgd /*
82 1.1 cgd * The parsefile structure pointed to by the global variable parsefile
83 1.1 cgd * contains information about the current file being read.
84 1.1 cgd */
85 1.1 cgd
86 1.1 cgd MKINIT
87 1.1 cgd struct parsefile {
88 1.5 jtc struct parsefile *prev; /* preceding file on stack */
89 1.1 cgd int linno; /* current line */
90 1.1 cgd int fd; /* file descriptor (or -1 if string) */
91 1.17 christos int nleft; /* number of chars left in this line */
92 1.17 christos int lleft; /* number of chars left in this buffer */
93 1.55 kre const char *nextc; /* next char in buffer */
94 1.1 cgd char *buf; /* input buffer */
95 1.5 jtc struct strpush *strpush; /* for pushing strings at this level */
96 1.5 jtc struct strpush basestrpush; /* so pushing one is fast */
97 1.1 cgd };
98 1.1 cgd
99 1.1 cgd
100 1.1 cgd int plinno = 1; /* input line number */
101 1.35 christos int parsenleft; /* copy of parsefile->nleft */
102 1.17 christos MKINIT int parselleft; /* copy of parsefile->lleft */
103 1.55 kre const char *parsenextc; /* copy of parsefile->nextc */
104 1.1 cgd MKINIT struct parsefile basepf; /* top level input file */
105 1.35 christos MKINIT char basebuf[BUFSIZ]; /* buffer for top level input file */
106 1.1 cgd struct parsefile *parsefile = &basepf; /* current input file */
107 1.5 jtc int init_editline = 0; /* editline library initialized? */
108 1.5 jtc int whichprompt; /* 1 == PS1, 2 == PS2 */
109 1.5 jtc
110 1.37 christos STATIC void pushfile(void);
111 1.37 christos static int preadfd(void);
112 1.1 cgd
113 1.1 cgd #ifdef mkinit
114 1.35 christos INCLUDE <stdio.h>
115 1.1 cgd INCLUDE "input.h"
116 1.1 cgd INCLUDE "error.h"
117 1.1 cgd
118 1.1 cgd INIT {
119 1.1 cgd basepf.nextc = basepf.buf = basebuf;
120 1.1 cgd }
121 1.1 cgd
122 1.1 cgd RESET {
123 1.1 cgd if (exception != EXSHELLPROC)
124 1.17 christos parselleft = parsenleft = 0; /* clear input buffer */
125 1.1 cgd popallfiles();
126 1.1 cgd }
127 1.1 cgd
128 1.1 cgd SHELLPROC {
129 1.1 cgd popallfiles();
130 1.1 cgd }
131 1.1 cgd #endif
132 1.1 cgd
133 1.1 cgd
134 1.1 cgd /*
135 1.1 cgd * Read a line from the script.
136 1.1 cgd */
137 1.1 cgd
138 1.1 cgd char *
139 1.37 christos pfgets(char *line, int len)
140 1.11 cgd {
141 1.22 tls char *p = line;
142 1.1 cgd int nleft = len;
143 1.1 cgd int c;
144 1.1 cgd
145 1.1 cgd while (--nleft > 0) {
146 1.1 cgd c = pgetc_macro();
147 1.1 cgd if (c == PEOF) {
148 1.1 cgd if (p == line)
149 1.1 cgd return NULL;
150 1.1 cgd break;
151 1.1 cgd }
152 1.1 cgd *p++ = c;
153 1.1 cgd if (c == '\n')
154 1.1 cgd break;
155 1.1 cgd }
156 1.1 cgd *p = '\0';
157 1.1 cgd return line;
158 1.1 cgd }
159 1.1 cgd
160 1.1 cgd
161 1.1 cgd
162 1.1 cgd /*
163 1.1 cgd * Read a character from the script, returning PEOF on end of file.
164 1.1 cgd * Nul characters in the input are silently discarded.
165 1.1 cgd */
166 1.1 cgd
167 1.1 cgd int
168 1.37 christos pgetc(void)
169 1.17 christos {
170 1.1 cgd return pgetc_macro();
171 1.1 cgd }
172 1.1 cgd
173 1.1 cgd
174 1.17 christos static int
175 1.37 christos preadfd(void)
176 1.17 christos {
177 1.17 christos int nr;
178 1.32 christos char *buf = parsefile->buf;
179 1.32 christos parsenextc = buf;
180 1.17 christos
181 1.17 christos retry:
182 1.25 christos #ifndef SMALL
183 1.17 christos if (parsefile->fd == 0 && el) {
184 1.37 christos static const char *rl_cp;
185 1.37 christos static int el_len;
186 1.17 christos
187 1.37 christos if (rl_cp == NULL)
188 1.37 christos rl_cp = el_gets(el, &el_len);
189 1.17 christos if (rl_cp == NULL)
190 1.42 roy nr = el_len == 0 ? 0 : -1;
191 1.17 christos else {
192 1.37 christos nr = el_len;
193 1.37 christos if (nr > BUFSIZ - 8)
194 1.37 christos nr = BUFSIZ - 8;
195 1.37 christos memcpy(buf, rl_cp, nr);
196 1.37 christos if (nr != el_len) {
197 1.37 christos el_len -= nr;
198 1.37 christos rl_cp += nr;
199 1.37 christos } else
200 1.37 christos rl_cp = 0;
201 1.17 christos }
202 1.37 christos
203 1.24 christos } else
204 1.24 christos #endif
205 1.37 christos nr = read(parsefile->fd, buf, BUFSIZ - 8);
206 1.24 christos
207 1.17 christos
208 1.17 christos if (nr <= 0) {
209 1.17 christos if (nr < 0) {
210 1.17 christos if (errno == EINTR)
211 1.17 christos goto retry;
212 1.17 christos if (parsefile->fd == 0 && errno == EWOULDBLOCK) {
213 1.17 christos int flags = fcntl(0, F_GETFL, 0);
214 1.17 christos if (flags >= 0 && flags & O_NONBLOCK) {
215 1.17 christos flags &=~ O_NONBLOCK;
216 1.17 christos if (fcntl(0, F_SETFL, flags) >= 0) {
217 1.17 christos out2str("sh: turning off NDELAY mode\n");
218 1.17 christos goto retry;
219 1.17 christos }
220 1.17 christos }
221 1.17 christos }
222 1.17 christos }
223 1.17 christos nr = -1;
224 1.17 christos }
225 1.17 christos return nr;
226 1.17 christos }
227 1.17 christos
228 1.1 cgd /*
229 1.1 cgd * Refill the input buffer and return the next input character:
230 1.1 cgd *
231 1.5 jtc * 1) If a string was pushed back on the input, pop it;
232 1.1 cgd * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading
233 1.1 cgd * from a string so we can't refill the buffer, return EOF.
234 1.17 christos * 3) If the is more stuff in this buffer, use it else call read to fill it.
235 1.17 christos * 4) Process input up to the next newline, deleting nul characters.
236 1.1 cgd */
237 1.1 cgd
238 1.1 cgd int
239 1.37 christos preadbuffer(void)
240 1.17 christos {
241 1.17 christos char *p, *q;
242 1.17 christos int more;
243 1.46 mrg #ifndef SMALL
244 1.17 christos int something;
245 1.46 mrg #endif
246 1.17 christos char savec;
247 1.1 cgd
248 1.56 kre while (parsefile->strpush) {
249 1.5 jtc popstring();
250 1.1 cgd if (--parsenleft >= 0)
251 1.5 jtc return (*parsenextc++);
252 1.1 cgd }
253 1.1 cgd if (parsenleft == EOF_NLEFT || parsefile->buf == NULL)
254 1.1 cgd return PEOF;
255 1.1 cgd flushout(&output);
256 1.1 cgd flushout(&errout);
257 1.5 jtc
258 1.17 christos again:
259 1.17 christos if (parselleft <= 0) {
260 1.26 christos if ((parselleft = preadfd()) == -1) {
261 1.17 christos parselleft = parsenleft = EOF_NLEFT;
262 1.17 christos return PEOF;
263 1.5 jtc }
264 1.17 christos }
265 1.5 jtc
266 1.55 kre /* p = (not const char *)parsenextc; */
267 1.55 kre p = parsefile->buf + (parsenextc - parsefile->buf);
268 1.55 kre q = p;
269 1.1 cgd
270 1.1 cgd /* delete nul characters */
271 1.46 mrg #ifndef SMALL
272 1.5 jtc something = 0;
273 1.46 mrg #endif
274 1.17 christos for (more = 1; more;) {
275 1.17 christos switch (*p) {
276 1.17 christos case '\0':
277 1.17 christos p++; /* Skip nul */
278 1.17 christos goto check;
279 1.20 christos
280 1.17 christos case '\t':
281 1.17 christos case ' ':
282 1.1 cgd break;
283 1.17 christos
284 1.17 christos case '\n':
285 1.17 christos parsenleft = q - parsenextc;
286 1.17 christos more = 0; /* Stop processing here */
287 1.17 christos break;
288 1.17 christos
289 1.17 christos default:
290 1.46 mrg #ifndef SMALL
291 1.5 jtc something = 1;
292 1.46 mrg #endif
293 1.17 christos break;
294 1.17 christos }
295 1.17 christos
296 1.17 christos *q++ = *p++;
297 1.17 christos check:
298 1.17 christos if (--parselleft <= 0) {
299 1.17 christos parsenleft = q - parsenextc - 1;
300 1.17 christos if (parsenleft < 0)
301 1.17 christos goto again;
302 1.17 christos *q = '\0';
303 1.17 christos more = 0;
304 1.5 jtc }
305 1.1 cgd }
306 1.17 christos
307 1.17 christos savec = *q;
308 1.5 jtc *q = '\0';
309 1.5 jtc
310 1.25 christos #ifndef SMALL
311 1.5 jtc if (parsefile->fd == 0 && hist && something) {
312 1.28 christos HistEvent he;
313 1.5 jtc INTOFF;
314 1.31 christos history(hist, &he, whichprompt == 1? H_ENTER : H_APPEND,
315 1.31 christos parsenextc);
316 1.5 jtc INTON;
317 1.5 jtc }
318 1.19 christos #endif
319 1.17 christos
320 1.5 jtc if (vflag) {
321 1.17 christos out2str(parsenextc);
322 1.5 jtc flushout(out2);
323 1.5 jtc }
324 1.17 christos
325 1.17 christos *q = savec;
326 1.17 christos
327 1.1 cgd return *parsenextc++;
328 1.1 cgd }
329 1.1 cgd
330 1.1 cgd /*
331 1.1 cgd * Undo the last call to pgetc. Only one character may be pushed back.
332 1.1 cgd * PEOF may be pushed back.
333 1.1 cgd */
334 1.1 cgd
335 1.1 cgd void
336 1.37 christos pungetc(void)
337 1.37 christos {
338 1.1 cgd parsenleft++;
339 1.1 cgd parsenextc--;
340 1.1 cgd }
341 1.1 cgd
342 1.1 cgd /*
343 1.5 jtc * Push a string back onto the input at this current parsefile level.
344 1.5 jtc * We handle aliases this way.
345 1.1 cgd */
346 1.1 cgd void
347 1.55 kre pushstring(const char *s, int len, struct alias *ap)
348 1.37 christos {
349 1.5 jtc struct strpush *sp;
350 1.5 jtc
351 1.5 jtc INTOFF;
352 1.43 christos /*debugprintf("*** calling pushstring: %s, %d\n", s, len);*/
353 1.5 jtc if (parsefile->strpush) {
354 1.5 jtc sp = ckmalloc(sizeof (struct strpush));
355 1.5 jtc sp->prev = parsefile->strpush;
356 1.5 jtc parsefile->strpush = sp;
357 1.5 jtc } else
358 1.5 jtc sp = parsefile->strpush = &(parsefile->basestrpush);
359 1.5 jtc sp->prevstring = parsenextc;
360 1.5 jtc sp->prevnleft = parsenleft;
361 1.17 christos sp->prevlleft = parselleft;
362 1.53 kre sp->ap = ap;
363 1.5 jtc if (ap)
364 1.53 kre ap->flag |= ALIASINUSE;
365 1.5 jtc parsenextc = s;
366 1.5 jtc parsenleft = len;
367 1.5 jtc INTON;
368 1.1 cgd }
369 1.1 cgd
370 1.11 cgd void
371 1.37 christos popstring(void)
372 1.5 jtc {
373 1.5 jtc struct strpush *sp = parsefile->strpush;
374 1.1 cgd
375 1.5 jtc INTOFF;
376 1.5 jtc parsenextc = sp->prevstring;
377 1.5 jtc parsenleft = sp->prevnleft;
378 1.17 christos parselleft = sp->prevlleft;
379 1.43 christos /*debugprintf("*** calling popstring: restoring to '%s'\n", parsenextc);*/
380 1.5 jtc if (sp->ap)
381 1.5 jtc sp->ap->flag &= ~ALIASINUSE;
382 1.5 jtc parsefile->strpush = sp->prev;
383 1.5 jtc if (sp != &(parsefile->basestrpush))
384 1.5 jtc ckfree(sp);
385 1.5 jtc INTON;
386 1.5 jtc }
387 1.1 cgd
388 1.1 cgd /*
389 1.1 cgd * Set the input to take input from a file. If push is set, push the
390 1.1 cgd * old input onto the stack first.
391 1.1 cgd */
392 1.1 cgd
393 1.1 cgd void
394 1.37 christos setinputfile(const char *fname, int push)
395 1.11 cgd {
396 1.40 rillig unsigned char magic[4];
397 1.1 cgd int fd;
398 1.1 cgd int fd2;
399 1.51 kre struct stat sb;
400 1.1 cgd
401 1.1 cgd INTOFF;
402 1.1 cgd if ((fd = open(fname, O_RDONLY)) < 0)
403 1.1 cgd error("Can't open %s", fname);
404 1.40 rillig
405 1.40 rillig /* Since the message "Syntax error: "(" unexpected" is not very
406 1.40 rillig * helpful, we check if the file starts with the ELF magic to
407 1.40 rillig * avoid that message. The first lseek tries to make sure that
408 1.40 rillig * we can later rewind the file.
409 1.40 rillig */
410 1.51 kre if (fstat(fd, &sb) == 0 && S_ISREG(sb.st_mode) &&
411 1.51 kre lseek(fd, 0, SEEK_SET) == 0) {
412 1.40 rillig if (read(fd, magic, 4) == 4) {
413 1.50 kre if (memcmp(magic, "\177ELF", 4) == 0) {
414 1.50 kre (void)close(fd);
415 1.40 rillig error("Cannot execute ELF binary %s", fname);
416 1.50 kre }
417 1.40 rillig }
418 1.50 kre if (lseek(fd, 0, SEEK_SET) != 0) {
419 1.50 kre (void)close(fd);
420 1.40 rillig error("Cannot rewind the file %s", fname);
421 1.50 kre }
422 1.40 rillig }
423 1.40 rillig
424 1.49 christos fd2 = to_upper_fd(fd); /* closes fd, returns higher equiv */
425 1.49 christos if (fd2 == fd) {
426 1.49 christos (void) close(fd);
427 1.49 christos error("Out of file descriptors");
428 1.1 cgd }
429 1.49 christos
430 1.49 christos setinputfd(fd2, push);
431 1.1 cgd INTON;
432 1.1 cgd }
433 1.1 cgd
434 1.52 kre /*
435 1.52 kre * When a shell fd needs to be altered (when the user wants to use
436 1.52 kre * the same fd - rare, but happens - we need to locate the ref to
437 1.52 kre * the fd, and update it. This happens via a callback.
438 1.52 kre * This is the callback func for fd's used for shell input
439 1.52 kre */
440 1.52 kre static void
441 1.52 kre input_fd_swap(int from, int to)
442 1.52 kre {
443 1.52 kre struct parsefile *pf;
444 1.52 kre
445 1.52 kre pf = parsefile;
446 1.52 kre while (pf != NULL) { /* don't need to stop at basepf */
447 1.52 kre if (pf->fd == from)
448 1.52 kre pf->fd = to;
449 1.52 kre pf = pf->prev;
450 1.52 kre }
451 1.52 kre }
452 1.1 cgd
453 1.1 cgd /*
454 1.1 cgd * Like setinputfile, but takes an open file descriptor. Call this with
455 1.1 cgd * interrupts off.
456 1.1 cgd */
457 1.1 cgd
458 1.1 cgd void
459 1.37 christos setinputfd(int fd, int push)
460 1.11 cgd {
461 1.52 kre register_sh_fd(fd, input_fd_swap);
462 1.23 mycroft (void) fcntl(fd, F_SETFD, FD_CLOEXEC);
463 1.1 cgd if (push) {
464 1.1 cgd pushfile();
465 1.1 cgd parsefile->buf = ckmalloc(BUFSIZ);
466 1.1 cgd }
467 1.1 cgd if (parsefile->fd > 0)
468 1.52 kre sh_close(parsefile->fd);
469 1.1 cgd parsefile->fd = fd;
470 1.1 cgd if (parsefile->buf == NULL)
471 1.1 cgd parsefile->buf = ckmalloc(BUFSIZ);
472 1.17 christos parselleft = parsenleft = 0;
473 1.1 cgd plinno = 1;
474 1.1 cgd }
475 1.1 cgd
476 1.1 cgd
477 1.1 cgd /*
478 1.1 cgd * Like setinputfile, but takes input from a string.
479 1.1 cgd */
480 1.1 cgd
481 1.1 cgd void
482 1.57 kre setinputstring(char *string, int push, int line1)
483 1.37 christos {
484 1.48 christos
485 1.1 cgd INTOFF;
486 1.58 kre if (push) /* XXX: always, as it happens */
487 1.1 cgd pushfile();
488 1.1 cgd parsenextc = string;
489 1.17 christos parselleft = parsenleft = strlen(string);
490 1.1 cgd parsefile->buf = NULL;
491 1.57 kre plinno = line1;
492 1.58 kre TRACE(("setinputstring(\"%.20s%s\" (%d), %d, %d)\n", string,
493 1.58 kre (parsenleft > 20 ? "..." : ""), parsenleft, push, line1));
494 1.1 cgd INTON;
495 1.1 cgd }
496 1.1 cgd
497 1.1 cgd
498 1.1 cgd
499 1.1 cgd /*
500 1.1 cgd * To handle the "." command, a stack of input files is used. Pushfile
501 1.1 cgd * adds a new entry to the stack and popfile restores the previous level.
502 1.1 cgd */
503 1.1 cgd
504 1.1 cgd STATIC void
505 1.37 christos pushfile(void)
506 1.37 christos {
507 1.1 cgd struct parsefile *pf;
508 1.1 cgd
509 1.1 cgd parsefile->nleft = parsenleft;
510 1.17 christos parsefile->lleft = parselleft;
511 1.1 cgd parsefile->nextc = parsenextc;
512 1.1 cgd parsefile->linno = plinno;
513 1.1 cgd pf = (struct parsefile *)ckmalloc(sizeof (struct parsefile));
514 1.1 cgd pf->prev = parsefile;
515 1.1 cgd pf->fd = -1;
516 1.5 jtc pf->strpush = NULL;
517 1.5 jtc pf->basestrpush.prev = NULL;
518 1.1 cgd parsefile = pf;
519 1.1 cgd }
520 1.1 cgd
521 1.1 cgd
522 1.1 cgd void
523 1.37 christos popfile(void)
524 1.37 christos {
525 1.1 cgd struct parsefile *pf = parsefile;
526 1.1 cgd
527 1.1 cgd INTOFF;
528 1.1 cgd if (pf->fd >= 0)
529 1.52 kre sh_close(pf->fd);
530 1.1 cgd if (pf->buf)
531 1.1 cgd ckfree(pf->buf);
532 1.5 jtc while (pf->strpush)
533 1.5 jtc popstring();
534 1.1 cgd parsefile = pf->prev;
535 1.1 cgd ckfree(pf);
536 1.1 cgd parsenleft = parsefile->nleft;
537 1.17 christos parselleft = parsefile->lleft;
538 1.1 cgd parsenextc = parsefile->nextc;
539 1.1 cgd plinno = parsefile->linno;
540 1.1 cgd INTON;
541 1.1 cgd }
542 1.1 cgd
543 1.1 cgd
544 1.1 cgd /*
545 1.1 cgd * Return to top level.
546 1.1 cgd */
547 1.1 cgd
548 1.1 cgd void
549 1.37 christos popallfiles(void)
550 1.37 christos {
551 1.1 cgd while (parsefile != &basepf)
552 1.1 cgd popfile();
553 1.1 cgd }
554 1.1 cgd
555 1.1 cgd
556 1.1 cgd
557 1.1 cgd /*
558 1.1 cgd * Close the file(s) that the shell is reading commands from. Called
559 1.1 cgd * after a fork is done.
560 1.36 christos *
561 1.36 christos * Takes one arg, vfork, which tells it to not modify its global vars
562 1.36 christos * as it is still running in the parent.
563 1.38 dsl *
564 1.38 dsl * This code is (probably) unnecessary as the 'close on exec' flag is
565 1.38 dsl * set and should be enough. In the vfork case it is definitely wrong
566 1.38 dsl * to close the fds as another fork() may be done later to feed data
567 1.38 dsl * from a 'here' document into a pipe and we don't want to close the
568 1.38 dsl * pipe!
569 1.1 cgd */
570 1.1 cgd
571 1.1 cgd void
572 1.37 christos closescript(int vforked)
573 1.37 christos {
574 1.38 dsl if (vforked)
575 1.36 christos return;
576 1.1 cgd popallfiles();
577 1.1 cgd if (parsefile->fd > 0) {
578 1.52 kre sh_close(parsefile->fd);
579 1.1 cgd parsefile->fd = 0;
580 1.1 cgd }
581 1.1 cgd }
582