[626] | 1 | /* $NetBSD: input.c,v 1.39 2003/08/07 09:05:32 agc Exp $ */
|
---|
| 2 |
|
---|
| 3 | /*-
|
---|
| 4 | * Copyright (c) 1991, 1993
|
---|
| 5 | * The Regents of the University of California. All rights reserved.
|
---|
| 6 | *
|
---|
| 7 | * This code is derived from software contributed to Berkeley by
|
---|
| 8 | * Kenneth Almquist.
|
---|
| 9 | *
|
---|
| 10 | * Redistribution and use in source and binary forms, with or without
|
---|
| 11 | * modification, are permitted provided that the following conditions
|
---|
| 12 | * are met:
|
---|
| 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.
|
---|
| 21 | *
|
---|
| 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
|
---|
| 32 | * SUCH DAMAGE.
|
---|
| 33 | */
|
---|
| 34 |
|
---|
[1214] | 35 | #if 0
|
---|
[626] | 36 | #ifndef lint
|
---|
| 37 | static char sccsid[] = "@(#)input.c 8.3 (Berkeley) 6/9/95";
|
---|
| 38 | #else
|
---|
| 39 | __RCSID("$NetBSD: input.c,v 1.39 2003/08/07 09:05:32 agc Exp $");
|
---|
[1214] | 40 | #endif /* not lint */
|
---|
[626] | 41 | #endif
|
---|
| 42 |
|
---|
| 43 | #include <stdio.h> /* defines BUFSIZ */
|
---|
| 44 | #include <errno.h>
|
---|
| 45 | #include <stdlib.h>
|
---|
| 46 | #include <string.h>
|
---|
| 47 |
|
---|
| 48 | /*
|
---|
| 49 | * This file implements the input routines used by the parser.
|
---|
| 50 | */
|
---|
| 51 |
|
---|
| 52 | #include "shell.h"
|
---|
| 53 | #include "redir.h"
|
---|
| 54 | #include "syntax.h"
|
---|
| 55 | #include "input.h"
|
---|
| 56 | #include "output.h"
|
---|
| 57 | #include "options.h"
|
---|
| 58 | #include "memalloc.h"
|
---|
| 59 | #include "error.h"
|
---|
| 60 | #include "alias.h"
|
---|
| 61 | #include "parser.h"
|
---|
| 62 | #include "myhistedit.h"
|
---|
[1201] | 63 | #include "shinstance.h"
|
---|
[626] | 64 |
|
---|
| 65 | #define EOF_NLEFT -99 /* value of parsenleft when EOF pushed back */
|
---|
| 66 |
|
---|
[1201] | 67 | //MKINIT
|
---|
| 68 | //struct strpush {
|
---|
| 69 | // struct strpush *prev; /* preceding string on stack */
|
---|
| 70 | // char *prevstring;
|
---|
| 71 | // int prevnleft;
|
---|
| 72 | // int prevlleft;
|
---|
| 73 | // struct alias *ap; /* if push was associated with an alias */
|
---|
| 74 | //};
|
---|
| 75 | //
|
---|
| 76 | ///*
|
---|
| 77 | // * The parsefile structure pointed to by the global variable parsefile
|
---|
| 78 | // * contains information about the current file being read.
|
---|
| 79 | // */
|
---|
| 80 | //
|
---|
| 81 | //MKINIT
|
---|
| 82 | //struct parsefile {
|
---|
| 83 | // struct parsefile *prev; /* preceding file on stack */
|
---|
| 84 | // int linno; /* current line */
|
---|
| 85 | // int fd; /* file descriptor (or -1 if string) */
|
---|
| 86 | // int nleft; /* number of chars left in this line */
|
---|
| 87 | // int lleft; /* number of chars left in this buffer */
|
---|
| 88 | // char *nextc; /* next char in buffer */
|
---|
| 89 | // char *buf; /* input buffer */
|
---|
| 90 | // struct strpush *strpush; /* for pushing strings at this level */
|
---|
| 91 | // struct strpush basestrpush; /* so pushing one is fast */
|
---|
| 92 | //};
|
---|
| 93 | //
|
---|
| 94 | //
|
---|
| 95 | //int plinno = 1; /* input line number */
|
---|
| 96 | //int parsenleft; /* copy of parsefile->nleft */
|
---|
| 97 | //MKINIT int parselleft; /* copy of parsefile->lleft */
|
---|
| 98 | //char *parsenextc; /* copy of parsefile->nextc */
|
---|
| 99 | //MKINIT struct parsefile basepf; /* top level input file */
|
---|
| 100 | //MKINIT char basebuf[BUFSIZ]; /* buffer for top level input file */
|
---|
| 101 | //struct parsefile *parsefile = &basepf; /* current input file */
|
---|
| 102 | //int init_editline = 0; /* editline library initialized? */
|
---|
| 103 | //int whichprompt; /* 1 == PS1, 2 == PS2 */
|
---|
| 104 | //
|
---|
| 105 | //#ifndef SMALL
|
---|
| 106 | //EditLine *el; /* cookie for editline package */
|
---|
| 107 | //#endif
|
---|
[626] | 108 |
|
---|
[1201] | 109 | STATIC void pushfile(shinstance *psh);
|
---|
| 110 | static int preadfd(shinstance *psh);
|
---|
[626] | 111 |
|
---|
| 112 | #ifdef mkinit
|
---|
| 113 | INCLUDE <stdio.h>
|
---|
| 114 | INCLUDE "input.h"
|
---|
| 115 | INCLUDE "error.h"
|
---|
| 116 |
|
---|
| 117 | INIT {
|
---|
[1201] | 118 | psh->basepf.nextc = psh->basepf.buf = psh->basebuf;
|
---|
[626] | 119 | }
|
---|
| 120 |
|
---|
| 121 | RESET {
|
---|
[1201] | 122 | if (psh->exception != EXSHELLPROC)
|
---|
| 123 | psh->parselleft = psh->parsenleft = 0; /* clear input buffer */
|
---|
| 124 | popallfiles(psh);
|
---|
[626] | 125 | }
|
---|
| 126 |
|
---|
| 127 | SHELLPROC {
|
---|
[1201] | 128 | popallfiles(psh);
|
---|
[626] | 129 | }
|
---|
| 130 | #endif
|
---|
| 131 |
|
---|
| 132 |
|
---|
| 133 | /*
|
---|
| 134 | * Read a line from the script.
|
---|
| 135 | */
|
---|
| 136 |
|
---|
| 137 | char *
|
---|
[1201] | 138 | pfgets(shinstance *psh, char *line, int len)
|
---|
[626] | 139 | {
|
---|
| 140 | char *p = line;
|
---|
| 141 | int nleft = len;
|
---|
| 142 | int c;
|
---|
| 143 |
|
---|
| 144 | while (--nleft > 0) {
|
---|
[1201] | 145 | c = pgetc_macro(psh);
|
---|
[626] | 146 | if (c == PEOF) {
|
---|
| 147 | if (p == line)
|
---|
| 148 | return NULL;
|
---|
| 149 | break;
|
---|
| 150 | }
|
---|
| 151 | *p++ = c;
|
---|
| 152 | if (c == '\n')
|
---|
| 153 | break;
|
---|
| 154 | }
|
---|
| 155 | *p = '\0';
|
---|
| 156 | return line;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 |
|
---|
| 160 |
|
---|
| 161 | /*
|
---|
| 162 | * Read a character from the script, returning PEOF on end of file.
|
---|
| 163 | * Nul characters in the input are silently discarded.
|
---|
| 164 | */
|
---|
| 165 |
|
---|
| 166 | int
|
---|
[1201] | 167 | pgetc(shinstance *psh)
|
---|
[626] | 168 | {
|
---|
[1201] | 169 | return pgetc_macro(psh);
|
---|
[626] | 170 | }
|
---|
| 171 |
|
---|
| 172 |
|
---|
| 173 | static int
|
---|
[2629] | 174 | preadfd_inner(shinstance *psh, char *buf, int bufsize)
|
---|
[626] | 175 | {
|
---|
| 176 | int nr;
|
---|
| 177 | retry:
|
---|
| 178 | #ifndef SMALL
|
---|
[1201] | 179 | if (psh->parsefile->fd == 0 && psh->el) {
|
---|
[626] | 180 | static const char *rl_cp;
|
---|
| 181 | static int el_len;
|
---|
| 182 |
|
---|
| 183 | if (rl_cp == NULL)
|
---|
[1201] | 184 | rl_cp = el_gets(psh->el, &el_len);
|
---|
[626] | 185 | if (rl_cp == NULL)
|
---|
| 186 | nr = 0;
|
---|
| 187 | else {
|
---|
| 188 | nr = el_len;
|
---|
[2629] | 189 | if (nr > bufsize)
|
---|
| 190 | nr = bufsize;
|
---|
[626] | 191 | memcpy(buf, rl_cp, nr);
|
---|
| 192 | if (nr != el_len) {
|
---|
| 193 | el_len -= nr;
|
---|
| 194 | rl_cp += nr;
|
---|
| 195 | } else
|
---|
| 196 | rl_cp = 0;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | } else
|
---|
| 200 | #endif
|
---|
[2629] | 201 | nr = shfile_read(&psh->fdtab, psh->parsefile->fd, buf, bufsize);
|
---|
[626] | 202 |
|
---|
| 203 |
|
---|
| 204 | if (nr <= 0) {
|
---|
| 205 | if (nr < 0) {
|
---|
| 206 | if (errno == EINTR)
|
---|
| 207 | goto retry;
|
---|
[1201] | 208 | if (psh->parsefile->fd == 0 && errno == EWOULDBLOCK) {
|
---|
| 209 | int flags = shfile_fcntl(&psh->fdtab, 0, F_GETFL, 0);
|
---|
[626] | 210 | if (flags >= 0 && flags & O_NONBLOCK) {
|
---|
| 211 | flags &=~ O_NONBLOCK;
|
---|
[1201] | 212 | if (shfile_fcntl(&psh->fdtab, 0, F_SETFL, flags) >= 0) {
|
---|
[1199] | 213 | out2str(psh, "sh: turning off NDELAY mode\n");
|
---|
[626] | 214 | goto retry;
|
---|
| 215 | }
|
---|
| 216 | }
|
---|
| 217 | }
|
---|
| 218 | }
|
---|
| 219 | nr = -1;
|
---|
[2629] | 220 | }
|
---|
| 221 | return nr;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 |
|
---|
| 225 |
|
---|
| 226 | static int
|
---|
| 227 | preadfd(shinstance *psh)
|
---|
| 228 | {
|
---|
| 229 | int nr;
|
---|
| 230 | char *buf = psh->parsefile->buf;
|
---|
| 231 | psh->parsenextc = buf;
|
---|
| 232 |
|
---|
[2310] | 233 | #ifdef SH_DEAL_WITH_CRLF
|
---|
[2629] | 234 | /* Convert CRLF to LF. */
|
---|
| 235 | nr = preadfd_inner(psh, buf, BUFSIZ - 9);
|
---|
| 236 | if (nr > 0) {
|
---|
[2310] | 237 | char *cr = memchr(buf, '\r', nr);
|
---|
| 238 | while (cr) {
|
---|
| 239 | size_t left = nr - (cr - buf);
|
---|
[2629] | 240 |
|
---|
[2310] | 241 | if (left > 1 && cr[1] == '\n') {
|
---|
| 242 | left--;
|
---|
| 243 | nr--;
|
---|
| 244 | memmove(cr, cr + 1, left);
|
---|
| 245 | cr = memchr(cr, '\r', left);
|
---|
[2629] | 246 | } else if (left == 1) {
|
---|
| 247 | /* Special case: \r at buffer end. Read one more char. Screw \r\r\n sequences. */
|
---|
| 248 | int nr2 = preadfd_inner(psh, cr + 1, 1);
|
---|
| 249 | if (nr2 != 1)
|
---|
| 250 | break;
|
---|
| 251 | if (cr[1] == '\n') {
|
---|
| 252 | *cr = '\n';
|
---|
| 253 | } else {
|
---|
| 254 | nr++;
|
---|
| 255 | }
|
---|
| 256 | break;
|
---|
[2310] | 257 | } else {
|
---|
| 258 | cr = memchr(cr + 1, '\r', left);
|
---|
| 259 | }
|
---|
| 260 | }
|
---|
[2629] | 261 | }
|
---|
| 262 | #else
|
---|
| 263 | nr = preadfd_inner(psh, buf, BUFSIZ - 8);
|
---|
[2310] | 264 | #endif
|
---|
[626] | 265 | return nr;
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | /*
|
---|
| 269 | * Refill the input buffer and return the next input character:
|
---|
| 270 | *
|
---|
| 271 | * 1) If a string was pushed back on the input, pop it;
|
---|
| 272 | * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading
|
---|
| 273 | * from a string so we can't refill the buffer, return EOF.
|
---|
| 274 | * 3) If the is more stuff in this buffer, use it else call read to fill it.
|
---|
| 275 | * 4) Process input up to the next newline, deleting nul characters.
|
---|
| 276 | */
|
---|
| 277 |
|
---|
| 278 | int
|
---|
[1201] | 279 | preadbuffer(shinstance *psh)
|
---|
[626] | 280 | {
|
---|
| 281 | char *p, *q;
|
---|
| 282 | int more;
|
---|
[3065] | 283 | #ifndef SMALL
|
---|
[626] | 284 | int something;
|
---|
[3065] | 285 | #endif
|
---|
[626] | 286 | char savec;
|
---|
| 287 |
|
---|
[1201] | 288 | if (psh->parsefile->strpush) {
|
---|
| 289 | popstring(psh);
|
---|
| 290 | if (--psh->parsenleft >= 0)
|
---|
| 291 | return (*psh->parsenextc++);
|
---|
[626] | 292 | }
|
---|
[1201] | 293 | if (psh->parsenleft == EOF_NLEFT || psh->parsefile->buf == NULL)
|
---|
[626] | 294 | return PEOF;
|
---|
[1201] | 295 | flushout(&psh->output);
|
---|
| 296 | flushout(&psh->errout);
|
---|
[626] | 297 |
|
---|
| 298 | again:
|
---|
[1201] | 299 | if (psh->parselleft <= 0) {
|
---|
| 300 | if ((psh->parselleft = preadfd(psh)) == -1) {
|
---|
| 301 | psh->parselleft = psh->parsenleft = EOF_NLEFT;
|
---|
[626] | 302 | return PEOF;
|
---|
| 303 | }
|
---|
| 304 | }
|
---|
| 305 |
|
---|
[1201] | 306 | q = p = psh->parsenextc;
|
---|
[626] | 307 |
|
---|
| 308 | /* delete nul characters */
|
---|
[3065] | 309 | #ifndef SMALL
|
---|
[626] | 310 | something = 0;
|
---|
[3065] | 311 | #endif
|
---|
[626] | 312 | for (more = 1; more;) {
|
---|
| 313 | switch (*p) {
|
---|
| 314 | case '\0':
|
---|
| 315 | p++; /* Skip nul */
|
---|
| 316 | goto check;
|
---|
| 317 |
|
---|
| 318 | case '\t':
|
---|
| 319 | case ' ':
|
---|
| 320 | break;
|
---|
| 321 |
|
---|
| 322 | case '\n':
|
---|
[1201] | 323 | psh->parsenleft = (int)(q - psh->parsenextc);
|
---|
[626] | 324 | more = 0; /* Stop processing here */
|
---|
| 325 | break;
|
---|
| 326 |
|
---|
| 327 | default:
|
---|
[3065] | 328 | #ifndef SMALL
|
---|
[626] | 329 | something = 1;
|
---|
[3065] | 330 | #endif
|
---|
[626] | 331 | break;
|
---|
| 332 | }
|
---|
| 333 |
|
---|
| 334 | *q++ = *p++;
|
---|
| 335 | check:
|
---|
[1201] | 336 | if (--psh->parselleft <= 0) {
|
---|
| 337 | psh->parsenleft = (int)(q - psh->parsenextc - 1);
|
---|
| 338 | if (psh->parsenleft < 0)
|
---|
[626] | 339 | goto again;
|
---|
| 340 | *q = '\0';
|
---|
| 341 | more = 0;
|
---|
| 342 | }
|
---|
| 343 | }
|
---|
| 344 |
|
---|
| 345 | savec = *q;
|
---|
| 346 | *q = '\0';
|
---|
| 347 |
|
---|
| 348 | #ifndef SMALL
|
---|
[1201] | 349 | if (psh->parsefile->fd == 0 && hist && something) {
|
---|
[626] | 350 | HistEvent he;
|
---|
| 351 | INTOFF;
|
---|
[1201] | 352 | history(hist, &he, psh->whichprompt == 1? H_ENTER : H_APPEND,
|
---|
| 353 | psh->parsenextc);
|
---|
[626] | 354 | INTON;
|
---|
| 355 | }
|
---|
| 356 | #endif
|
---|
| 357 |
|
---|
[1198] | 358 | if (vflag(psh)) {
|
---|
[1201] | 359 | out2str(psh, psh->parsenextc);
|
---|
| 360 | flushout(psh->out2);
|
---|
[626] | 361 | }
|
---|
| 362 |
|
---|
| 363 | *q = savec;
|
---|
| 364 |
|
---|
[1201] | 365 | return *psh->parsenextc++;
|
---|
[626] | 366 | }
|
---|
| 367 |
|
---|
| 368 | /*
|
---|
| 369 | * Undo the last call to pgetc. Only one character may be pushed back.
|
---|
| 370 | * PEOF may be pushed back.
|
---|
| 371 | */
|
---|
| 372 |
|
---|
| 373 | void
|
---|
[1201] | 374 | pungetc(shinstance *psh)
|
---|
[626] | 375 | {
|
---|
[1201] | 376 | psh->parsenleft++;
|
---|
| 377 | psh->parsenextc--;
|
---|
[626] | 378 | }
|
---|
| 379 |
|
---|
| 380 | /*
|
---|
| 381 | * Push a string back onto the input at this current parsefile level.
|
---|
| 382 | * We handle aliases this way.
|
---|
| 383 | */
|
---|
| 384 | void
|
---|
[1210] | 385 | pushstring(shinstance *psh, char *s, size_t len, void *ap)
|
---|
[626] | 386 | {
|
---|
| 387 | struct strpush *sp;
|
---|
| 388 |
|
---|
| 389 | INTOFF;
|
---|
| 390 | /*dprintf("*** calling pushstring: %s, %d\n", s, len);*/
|
---|
[1201] | 391 | if (psh->parsefile->strpush) {
|
---|
[2290] | 392 | sp = ckmalloc(psh, sizeof (struct strpush));
|
---|
[1201] | 393 | sp->prev = psh->parsefile->strpush;
|
---|
| 394 | psh->parsefile->strpush = sp;
|
---|
[626] | 395 | } else
|
---|
[1201] | 396 | sp = psh->parsefile->strpush = &(psh->parsefile->basestrpush);
|
---|
| 397 | sp->prevstring = psh->parsenextc;
|
---|
| 398 | sp->prevnleft = psh->parsenleft;
|
---|
| 399 | sp->prevlleft = psh->parselleft;
|
---|
[626] | 400 | sp->ap = (struct alias *)ap;
|
---|
| 401 | if (ap)
|
---|
| 402 | ((struct alias *)ap)->flag |= ALIASINUSE;
|
---|
[1201] | 403 | psh->parsenextc = s;
|
---|
[1210] | 404 | psh->parsenleft = (int)len;
|
---|
[626] | 405 | INTON;
|
---|
| 406 | }
|
---|
| 407 |
|
---|
| 408 | void
|
---|
[1201] | 409 | popstring(shinstance *psh)
|
---|
[626] | 410 | {
|
---|
[1201] | 411 | struct strpush *sp = psh->parsefile->strpush;
|
---|
[626] | 412 |
|
---|
| 413 | INTOFF;
|
---|
[1201] | 414 | psh->parsenextc = sp->prevstring;
|
---|
| 415 | psh->parsenleft = sp->prevnleft;
|
---|
| 416 | psh->parselleft = sp->prevlleft;
|
---|
| 417 | /*dprintf("*** calling popstring: restoring to '%s'\n", psh->parsenextc);*/
|
---|
[626] | 418 | if (sp->ap)
|
---|
| 419 | sp->ap->flag &= ~ALIASINUSE;
|
---|
[1201] | 420 | psh->parsefile->strpush = sp->prev;
|
---|
| 421 | if (sp != &(psh->parsefile->basestrpush))
|
---|
[2290] | 422 | ckfree(psh, sp);
|
---|
[626] | 423 | INTON;
|
---|
| 424 | }
|
---|
| 425 |
|
---|
| 426 | /*
|
---|
| 427 | * Set the input to take input from a file. If push is set, push the
|
---|
| 428 | * old input onto the stack first.
|
---|
| 429 | */
|
---|
| 430 |
|
---|
| 431 | void
|
---|
[1201] | 432 | setinputfile(shinstance *psh, const char *fname, int push)
|
---|
[626] | 433 | {
|
---|
| 434 | int fd;
|
---|
| 435 | int fd2;
|
---|
| 436 |
|
---|
| 437 | INTOFF;
|
---|
[1201] | 438 | /** @todo shfile fixme */
|
---|
[1222] | 439 | if ((fd = shfile_open(&psh->fdtab, fname, O_RDONLY, 0)) < 0)
|
---|
[1199] | 440 | error(psh, "Can't open %s", fname);
|
---|
[626] | 441 | if (fd < 10) {
|
---|
[2424] | 442 | fd2 = movefd_above(psh, fd, 10);
|
---|
[626] | 443 | if (fd2 < 0)
|
---|
[1199] | 444 | error(psh, "Out of file descriptors");
|
---|
[626] | 445 | fd = fd2;
|
---|
| 446 | }
|
---|
[1201] | 447 | setinputfd(psh, fd, push);
|
---|
[626] | 448 | INTON;
|
---|
| 449 | }
|
---|
| 450 |
|
---|
| 451 |
|
---|
| 452 | /*
|
---|
| 453 | * Like setinputfile, but takes an open file descriptor. Call this with
|
---|
| 454 | * interrupts off.
|
---|
| 455 | */
|
---|
| 456 |
|
---|
| 457 | void
|
---|
[1201] | 458 | setinputfd(shinstance *psh, int fd, int push)
|
---|
[626] | 459 | {
|
---|
[2288] | 460 | (void) shfile_cloexec(&psh->fdtab, fd, 1 /* close it */);
|
---|
[626] | 461 | if (push) {
|
---|
[1201] | 462 | pushfile(psh);
|
---|
[2290] | 463 | psh->parsefile->buf = ckmalloc(psh, BUFSIZ);
|
---|
[626] | 464 | }
|
---|
[1201] | 465 | if (psh->parsefile->fd > 0)
|
---|
| 466 | shfile_close(&psh->fdtab, psh->parsefile->fd);
|
---|
| 467 | psh->parsefile->fd = fd;
|
---|
| 468 | if (psh->parsefile->buf == NULL)
|
---|
[2290] | 469 | psh->parsefile->buf = ckmalloc(psh, BUFSIZ);
|
---|
[1201] | 470 | psh->parselleft = psh->parsenleft = 0;
|
---|
| 471 | psh->plinno = 1;
|
---|
[626] | 472 | }
|
---|
| 473 |
|
---|
| 474 |
|
---|
| 475 | /*
|
---|
| 476 | * Like setinputfile, but takes input from a string.
|
---|
| 477 | */
|
---|
| 478 |
|
---|
| 479 | void
|
---|
[1201] | 480 | setinputstring(shinstance *psh, char *string, int push)
|
---|
[626] | 481 | {
|
---|
| 482 | INTOFF;
|
---|
| 483 | if (push)
|
---|
[1201] | 484 | pushfile(psh);
|
---|
| 485 | psh->parsenextc = string;
|
---|
| 486 | psh->parselleft = psh->parsenleft = (int)strlen(string);
|
---|
| 487 | psh->parsefile->buf = NULL;
|
---|
| 488 | psh->plinno = 1;
|
---|
[626] | 489 | INTON;
|
---|
| 490 | }
|
---|
| 491 |
|
---|
| 492 |
|
---|
| 493 |
|
---|
| 494 | /*
|
---|
| 495 | * To handle the "." command, a stack of input files is used. Pushfile
|
---|
| 496 | * adds a new entry to the stack and popfile restores the previous level.
|
---|
| 497 | */
|
---|
| 498 |
|
---|
| 499 | STATIC void
|
---|
[1201] | 500 | pushfile(shinstance *psh)
|
---|
[626] | 501 | {
|
---|
| 502 | struct parsefile *pf;
|
---|
| 503 |
|
---|
[1201] | 504 | psh->parsefile->nleft = psh->parsenleft;
|
---|
| 505 | psh->parsefile->lleft = psh->parselleft;
|
---|
| 506 | psh->parsefile->nextc = psh->parsenextc;
|
---|
| 507 | psh->parsefile->linno = psh->plinno;
|
---|
[2290] | 508 | pf = (struct parsefile *)ckmalloc(psh, sizeof (struct parsefile));
|
---|
[1201] | 509 | pf->prev = psh->parsefile;
|
---|
[626] | 510 | pf->fd = -1;
|
---|
| 511 | pf->strpush = NULL;
|
---|
| 512 | pf->basestrpush.prev = NULL;
|
---|
[1201] | 513 | psh->parsefile = pf;
|
---|
[626] | 514 | }
|
---|
| 515 |
|
---|
| 516 |
|
---|
| 517 | void
|
---|
[1201] | 518 | popfile(shinstance *psh)
|
---|
[626] | 519 | {
|
---|
[1201] | 520 | struct parsefile *pf = psh->parsefile;
|
---|
[626] | 521 |
|
---|
| 522 | INTOFF;
|
---|
| 523 | if (pf->fd >= 0)
|
---|
[1201] | 524 | shfile_close(&psh->fdtab, pf->fd);
|
---|
[626] | 525 | if (pf->buf)
|
---|
[2290] | 526 | ckfree(psh, pf->buf);
|
---|
[626] | 527 | while (pf->strpush)
|
---|
[1201] | 528 | popstring(psh);
|
---|
| 529 | psh->parsefile = pf->prev;
|
---|
[2290] | 530 | ckfree(psh, pf);
|
---|
[1201] | 531 | psh->parsenleft = psh->parsefile->nleft;
|
---|
| 532 | psh->parselleft = psh->parsefile->lleft;
|
---|
| 533 | psh->parsenextc = psh->parsefile->nextc;
|
---|
| 534 | psh->plinno = psh->parsefile->linno;
|
---|
[626] | 535 | INTON;
|
---|
| 536 | }
|
---|
| 537 |
|
---|
| 538 |
|
---|
| 539 | /*
|
---|
| 540 | * Return to top level.
|
---|
| 541 | */
|
---|
| 542 |
|
---|
| 543 | void
|
---|
[1201] | 544 | popallfiles(shinstance *psh)
|
---|
[626] | 545 | {
|
---|
[1201] | 546 | while (psh->parsefile != &psh->basepf)
|
---|
| 547 | popfile(psh);
|
---|
[626] | 548 | }
|
---|
| 549 |
|
---|
| 550 |
|
---|
| 551 |
|
---|
| 552 | /*
|
---|
| 553 | * Close the file(s) that the shell is reading commands from. Called
|
---|
| 554 | * after a fork is done.
|
---|
| 555 | *
|
---|
| 556 | * Takes one arg, vfork, which tells it to not modify its global vars
|
---|
| 557 | * as it is still running in the parent.
|
---|
| 558 | *
|
---|
| 559 | * This code is (probably) unnecessary as the 'close on exec' flag is
|
---|
| 560 | * set and should be enough. In the vfork case it is definitely wrong
|
---|
| 561 | * to close the fds as another fork() may be done later to feed data
|
---|
| 562 | * from a 'here' document into a pipe and we don't want to close the
|
---|
| 563 | * pipe!
|
---|
| 564 | */
|
---|
| 565 |
|
---|
| 566 | void
|
---|
[3435] | 567 | closescript(shinstance *psh)
|
---|
[626] | 568 | {
|
---|
[1201] | 569 | popallfiles(psh);
|
---|
| 570 | if (psh->parsefile->fd > 0) {
|
---|
| 571 | shfile_close(&psh->fdtab, psh->parsefile->fd);
|
---|
| 572 | psh->parsefile->fd = 0;
|
---|
[626] | 573 | }
|
---|
| 574 | }
|
---|