1 /* $NetBSD: input.c,v 1.72 2021/02/16 15:30:26 kre 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 35 #include <sys/cdefs.h> 36 #ifndef lint 37 #if 0 38 static char sccsid[] = "@(#)input.c 8.3 (Berkeley) 6/9/95"; 39 #else 40 __RCSID("$NetBSD: input.c,v 1.72 2021/02/16 15:30:26 kre Exp $"); 41 #endif 42 #endif /* not lint */ 43 44 #include <stdio.h> /* defines BUFSIZ */ 45 #include <fcntl.h> 46 #include <errno.h> 47 #include <unistd.h> 48 #include <limits.h> 49 #include <stdlib.h> 50 #include <string.h> 51 #include <sys/stat.h> 52 53 /* 54 * This file implements the input routines used by the parser. 55 */ 56 57 #include "shell.h" 58 #include "redir.h" 59 #include "syntax.h" 60 #include "input.h" 61 #include "output.h" 62 #include "options.h" 63 #include "memalloc.h" 64 #include "error.h" 65 #include "alias.h" 66 #include "parser.h" 67 #include "myhistedit.h" 68 #include "show.h" 69 70 #define EOF_NLEFT -99 /* value of parsenleft when EOF pushed back */ 71 72 MKINIT 73 struct strpush { 74 struct strpush *prev; /* preceding string on stack */ 75 const char *prevstring; 76 int prevnleft; 77 int prevlleft; 78 struct alias *ap; /* if push was associated with an alias */ 79 }; 80 81 /* 82 * The parsefile structure pointed to by the global variable parsefile 83 * contains information about the current file being read. 84 */ 85 86 MKINIT 87 struct parsefile { 88 struct parsefile *prev; /* preceding file on stack */ 89 int linno; /* current line */ 90 int fd; /* file descriptor (or -1 if string) */ 91 int nleft; /* number of chars left in this line */ 92 int lleft; /* number of chars left in this buffer */ 93 int nskip; /* number of \0's dropped in previous line */ 94 const char *nextc; /* next char in buffer */ 95 char *buf; /* input buffer */ 96 struct strpush *strpush; /* for pushing strings at this level */ 97 struct strpush basestrpush; /* so pushing one is fast */ 98 }; 99 100 101 int plinno = 1; /* input line number */ 102 int parsenleft; /* copy of parsefile->nleft */ 103 MKINIT int parselleft; /* copy of parsefile->lleft */ 104 const char *parsenextc; /* copy of parsefile->nextc */ 105 MKINIT struct parsefile basepf; /* top level input file */ 106 MKINIT char basebuf[BUFSIZ]; /* buffer for top level input file */ 107 struct parsefile *parsefile = &basepf; /* current input file */ 108 int init_editline = 0; /* editline library initialized? */ 109 int whichprompt; /* 1 == PS1, 2 == PS2 */ 110 111 STATIC void pushfile(void); 112 static int preadfd(void); 113 114 #ifdef mkinit 115 INCLUDE <stdio.h> 116 INCLUDE "input.h" 117 INCLUDE "error.h" 118 119 INIT { 120 basepf.nextc = basepf.buf = basebuf; 121 } 122 123 RESET { 124 if (exception != EXSHELLPROC) 125 parselleft = parsenleft = 0; /* clear input buffer */ 126 popallfiles(); 127 } 128 129 SHELLPROC { 130 popallfiles(); 131 } 132 #endif 133 134 135 #if 0 /* this is unused */ 136 /* 137 * Read a line from the script. 138 */ 139 140 char * 141 pfgets(char *line, int len) 142 { 143 char *p = line; 144 int nleft = len; 145 int c; 146 147 while (--nleft > 0) { 148 c = pgetc_macro(); 149 if (c == PFAKE) /* consecutive PFAKEs is impossible */ 150 c = pgetc_macro(); 151 if (c == PEOF) { 152 if (p == line) 153 return NULL; 154 break; 155 } 156 *p++ = c; 157 if (c == '\n') { 158 plinno++; 159 break; 160 } 161 } 162 *p = '\0'; 163 return line; 164 } 165 #endif 166 167 168 /* 169 * Read a character from the script, returning PEOF on end of file. 170 * Nul characters in the input are silently discarded. 171 */ 172 173 int 174 pgetc(void) 175 { 176 int c; 177 178 c = pgetc_macro(); 179 if (c == PFAKE) 180 c = pgetc_macro(); 181 return c; 182 } 183 184 185 static int 186 preadfd(void) 187 { 188 int nr; 189 char *buf = parsefile->buf; 190 parsenextc = buf; 191 192 retry: 193 #ifndef SMALL 194 if (parsefile->fd == 0 && el) { 195 static const char *rl_cp; 196 static int el_len; 197 198 if (rl_cp == NULL) 199 rl_cp = el_gets(el, &el_len); 200 if (rl_cp == NULL) 201 nr = el_len == 0 ? 0 : -1; 202 else { 203 nr = el_len; 204 if (nr > BUFSIZ - 8) 205 nr = BUFSIZ - 8; 206 memcpy(buf, rl_cp, nr); 207 if (nr != el_len) { 208 el_len -= nr; 209 rl_cp += nr; 210 } else 211 rl_cp = 0; 212 } 213 214 } else 215 #endif 216 nr = read(parsefile->fd, buf, BUFSIZ - 8); 217 218 219 if (nr <= 0) { 220 if (nr < 0) { 221 if (errno == EINTR) 222 goto retry; 223 if (parsefile->fd == 0 && errno == EWOULDBLOCK) { 224 int flags = fcntl(0, F_GETFL, 0); 225 226 if (flags >= 0 && flags & O_NONBLOCK) { 227 flags &=~ O_NONBLOCK; 228 if (fcntl(0, F_SETFL, flags) >= 0) { 229 out2str("sh: turning off NDELAY mode\n"); 230 goto retry; 231 } 232 } 233 } 234 } 235 nr = -1; 236 } 237 return nr; 238 } 239 240 /* 241 * Refill the input buffer and return the next input character: 242 * 243 * 1) If a string was pushed back on the input, pop it; 244 * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading 245 * from a string so we can't refill the buffer, return EOF. 246 * 3) If there is more stuff in this buffer, use it else call read to fill it. 247 * 4) Process input up to the next newline, deleting nul characters. 248 */ 249 250 int 251 preadbuffer(void) 252 { 253 char *p, *q; 254 int more; 255 #ifndef SMALL 256 int something; 257 #endif 258 char savec; 259 260 while (parsefile->strpush) { 261 if (parsenleft == -1 && parsefile->strpush->ap != NULL) 262 return PFAKE; 263 popstring(); 264 if (--parsenleft >= 0) 265 return (*parsenextc++); 266 } 267 if (parsenleft == EOF_NLEFT || parsefile->buf == NULL) 268 return PEOF; 269 flushout(&output); 270 flushout(&errout); 271 272 again: 273 if (parselleft <= 0) { 274 if ((parselleft = preadfd()) == -1) { 275 parselleft = parsenleft = EOF_NLEFT; 276 return PEOF; 277 } 278 parsefile->nskip = 0; 279 } 280 281 /* jump over slots for any \0 chars that were dropped */ 282 parsenextc += parsefile->nskip; 283 parsefile->nskip = 0; 284 285 /* p = (not const char *)parsenextc; */ 286 p = parsefile->buf + (parsenextc - parsefile->buf); 287 q = p; 288 289 /* delete nul characters */ 290 #ifndef SMALL 291 something = 0; 292 #endif 293 for (more = 1; more;) { 294 switch (*p) { 295 case '\0': 296 p++; /* Skip nul */ 297 parsefile->nskip++; 298 goto check; 299 300 case '\t': 301 case ' ': 302 break; 303 304 case '\n': 305 parsenleft = q - parsenextc; 306 more = 0; /* Stop processing here */ 307 break; 308 309 default: 310 #ifndef SMALL 311 something = 1; 312 #endif 313 break; 314 } 315 316 if (parsefile->nskip) 317 *q++ = *p++; 318 else 319 q = ++p; 320 321 check: 322 if (--parselleft <= 0) { 323 parsenleft = q - parsenextc - 1; 324 if (parsenleft < 0) 325 goto again; 326 *q = '\0'; 327 more = 0; 328 } 329 } 330 331 savec = *q; 332 *q = '\0'; 333 334 #ifndef SMALL 335 if (parsefile->fd == 0 && hist && (something || whichprompt == 2)) { 336 HistEvent he; 337 338 INTOFF; 339 history(hist, &he, whichprompt != 2 ? H_ENTER : H_APPEND, 340 parsenextc); 341 INTON; 342 } 343 #endif 344 345 if (vflag) { 346 out2str(parsenextc); 347 flushout(out2); 348 } 349 350 *q = savec; 351 352 return *parsenextc++; 353 } 354 355 /* 356 * Test whether we have reached EOF on input stream. 357 * Return true only if certain (without attempting a read). 358 * 359 * Note the similarity to the opening section of preadbuffer() 360 */ 361 int 362 at_eof(void) 363 { 364 struct strpush *sp = parsefile->strpush; 365 366 if (parsenleft > 0) /* more chars are in the buffer */ 367 return 0; 368 369 while (sp != NULL) { 370 /* 371 * If any pushed string has any remaining data, 372 * then we are not at EOF (simulating popstring()) 373 */ 374 if (sp->prevnleft > 0) 375 return 0; 376 sp = sp->prev; 377 } 378 379 /* 380 * If we reached real EOF and pushed it back, 381 * or if we are just processing a string (not reading a file) 382 * then there is no more. Note that if a file pushes a 383 * string, the file's ->buf remains present. 384 */ 385 if (parsenleft == EOF_NLEFT || parsefile->buf == NULL) 386 return 1; 387 388 /* 389 * In other cases, there might be more 390 */ 391 return 0; 392 } 393 394 /* 395 * Undo the last call to pgetc. Only one character may be pushed back. 396 * PEOF may be pushed back. 397 */ 398 399 void 400 pungetc(void) 401 { 402 parsenleft++; 403 parsenextc--; 404 } 405 406 /* 407 * Push a string back onto the input at this current parsefile level. 408 * We handle aliases this way. 409 */ 410 void 411 pushstring(const char *s, int len, struct alias *ap) 412 { 413 struct strpush *sp; 414 415 VTRACE(DBG_INPUT, 416 ("pushstring(\"%.*s\", %d)%s%s%s had: nl=%d ll=%d \"%.*s\"\n", 417 len, s, len, ap ? " for alias:'" : "", 418 ap ? ap->name : "", ap ? "'" : "", 419 parsenleft, parselleft, parsenleft, parsenextc)); 420 421 INTOFF; 422 if (parsefile->strpush) { 423 sp = ckmalloc(sizeof (struct strpush)); 424 sp->prev = parsefile->strpush; 425 parsefile->strpush = sp; 426 } else 427 sp = parsefile->strpush = &(parsefile->basestrpush); 428 429 sp->prevstring = parsenextc; 430 sp->prevnleft = parsenleft; 431 sp->prevlleft = parselleft; 432 sp->ap = ap; 433 if (ap) 434 ap->flag |= ALIASINUSE; 435 parsenextc = s; 436 parsenleft = len; 437 INTON; 438 } 439 440 void 441 popstring(void) 442 { 443 struct strpush *sp = parsefile->strpush; 444 445 INTOFF; 446 if (sp->ap) { 447 int alen; 448 449 if ((alen = strlen(sp->ap->val)) > 0 && 450 (sp->ap->val[alen - 1] == ' ' || 451 sp->ap->val[alen - 1] == '\t')) 452 checkkwd |= CHKALIAS; 453 sp->ap->flag &= ~ALIASINUSE; 454 } 455 parsenextc = sp->prevstring; 456 parsenleft = sp->prevnleft; 457 parselleft = sp->prevlleft; 458 459 VTRACE(DBG_INPUT, ("popstring()%s%s%s nl=%d ll=%d \"%.*s\"\n", 460 sp->ap ? " from alias:'" : "", sp->ap ? sp->ap->name : "", 461 sp->ap ? "'" : "", parsenleft, parselleft, parsenleft, parsenextc)); 462 463 parsefile->strpush = sp->prev; 464 if (sp != &(parsefile->basestrpush)) 465 ckfree(sp); 466 INTON; 467 } 468 469 /* 470 * Set the input to take input from a file. If push is set, push the 471 * old input onto the stack first. 472 */ 473 474 void 475 setinputfile(const char *fname, int push) 476 { 477 unsigned char magic[4]; 478 int fd; 479 int fd2; 480 struct stat sb; 481 482 CTRACE(DBG_INPUT,("setinputfile(\"%s\", %spush)\n",fname,push?"":"no")); 483 484 INTOFF; 485 if ((fd = open(fname, O_RDONLY)) < 0) 486 error("Can't open %s", fname); 487 488 /* Since the message "Syntax error: "(" unexpected" is not very 489 * helpful, we check if the file starts with the ELF magic to 490 * avoid that message. The first lseek tries to make sure that 491 * we can later rewind the file. 492 */ 493 if (fstat(fd, &sb) == 0 && S_ISREG(sb.st_mode) && 494 lseek(fd, 0, SEEK_SET) == 0) { 495 if (read(fd, magic, 4) == 4) { 496 if (memcmp(magic, "\177ELF", 4) == 0) { 497 (void)close(fd); 498 error("Cannot execute ELF binary %s", fname); 499 } 500 } 501 if (lseek(fd, 0, SEEK_SET) != 0) { 502 (void)close(fd); 503 error("Cannot rewind the file %s", fname); 504 } 505 } 506 507 fd2 = to_upper_fd(fd); /* closes fd, returns higher equiv */ 508 if (fd2 == fd) { 509 (void) close(fd); 510 error("Out of file descriptors"); 511 } 512 513 setinputfd(fd2, push); 514 INTON; 515 } 516 517 /* 518 * When a shell fd needs to be altered (when the user wants to use 519 * the same fd - rare, but happens - we need to locate the ref to 520 * the fd, and update it. This happens via a callback. 521 * This is the callback func for fd's used for shell input 522 */ 523 static void 524 input_fd_swap(int from, int to) 525 { 526 struct parsefile *pf; 527 528 pf = parsefile; 529 while (pf != NULL) { /* don't need to stop at basepf */ 530 if (pf->fd == from) 531 pf->fd = to; 532 pf = pf->prev; 533 } 534 } 535 536 /* 537 * Like setinputfile, but takes an open file descriptor. Call this with 538 * interrupts off. 539 */ 540 541 void 542 setinputfd(int fd, int push) 543 { 544 VTRACE(DBG_INPUT, ("setinputfd(%d, %spush)\n", fd, push?"":"no")); 545 546 INTOFF; 547 register_sh_fd(fd, input_fd_swap); 548 (void) fcntl(fd, F_SETFD, FD_CLOEXEC); 549 if (push) 550 pushfile(); 551 if (parsefile->fd > 0) 552 sh_close(parsefile->fd); 553 parsefile->fd = fd; 554 if (parsefile->buf == NULL) 555 parsefile->buf = ckmalloc(BUFSIZ); 556 parselleft = parsenleft = 0; 557 plinno = 1; 558 INTON; 559 560 CTRACE(DBG_INPUT, ("setinputfd(%d, %spush) done; plinno=1\n", fd, 561 push ? "" : "no")); 562 } 563 564 565 /* 566 * Like setinputfile, but takes input from a string. 567 */ 568 569 void 570 setinputstring(char *string, int push, int line1) 571 { 572 573 INTOFF; 574 if (push) /* XXX: always, as it happens */ 575 pushfile(); 576 parsenextc = string; 577 parselleft = parsenleft = strlen(string); 578 plinno = line1; 579 INTON; 580 581 CTRACE(DBG_INPUT, 582 ("setinputstring(\"%.20s%s\" (%d), %spush, @ %d)\n", string, 583 (parsenleft > 20 ? "..." : ""), parsenleft, push?"":"no", line1)); 584 } 585 586 587 588 /* 589 * To handle the "." command, a stack of input files is used. Pushfile 590 * adds a new entry to the stack and popfile restores the previous level. 591 */ 592 593 STATIC void 594 pushfile(void) 595 { 596 struct parsefile *pf; 597 598 VTRACE(DBG_INPUT, 599 ("pushfile(): fd=%d buf=%p nl=%d ll=%d \"%.*s\" plinno=%d\n", 600 parsefile->fd, parsefile->buf, parsenleft, parselleft, 601 parsenleft, parsenextc, plinno)); 602 603 parsefile->nleft = parsenleft; 604 parsefile->lleft = parselleft; 605 parsefile->nextc = parsenextc; 606 parsefile->linno = plinno; 607 pf = (struct parsefile *)ckmalloc(sizeof (struct parsefile)); 608 pf->prev = parsefile; 609 pf->fd = -1; 610 pf->strpush = NULL; 611 pf->basestrpush.prev = NULL; 612 pf->buf = NULL; 613 parsefile = pf; 614 } 615 616 617 void 618 popfile(void) 619 { 620 struct parsefile *pf = parsefile; 621 622 INTOFF; 623 if (pf->fd >= 0) 624 sh_close(pf->fd); 625 if (pf->buf) 626 ckfree(pf->buf); 627 while (pf->strpush) 628 popstring(); 629 parsefile = pf->prev; 630 ckfree(pf); 631 parsenleft = parsefile->nleft; 632 parselleft = parsefile->lleft; 633 parsenextc = parsefile->nextc; 634 635 VTRACE(DBG_INPUT, 636 ("popfile(): fd=%d buf=%p nl=%d ll=%d \"%.*s\" plinno:%d->%d\n", 637 parsefile->fd, parsefile->buf, parsenleft, parselleft, 638 parsenleft, parsenextc, plinno, parsefile->linno)); 639 640 plinno = parsefile->linno; 641 INTON; 642 } 643 644 /* 645 * Return current file (to go back to it later using popfilesupto()). 646 */ 647 648 struct parsefile * 649 getcurrentfile(void) 650 { 651 return parsefile; 652 } 653 654 655 /* 656 * Pop files until the given file is on top again. Useful for regular 657 * builtins that read shell commands from files or strings. 658 * If the given file is not an active file, an error is raised. 659 */ 660 661 void 662 popfilesupto(struct parsefile *file) 663 { 664 while (parsefile != file && parsefile != &basepf) 665 popfile(); 666 if (parsefile != file) 667 error("popfilesupto() misused"); 668 } 669 670 671 /* 672 * Return to top level. 673 */ 674 675 void 676 popallfiles(void) 677 { 678 while (parsefile != &basepf) 679 popfile(); 680 } 681 682 683 684 /* 685 * Close the file(s) that the shell is reading commands from. Called 686 * after a fork is done. 687 * 688 * Takes one arg, vfork, which tells it to not modify its global vars 689 * as it is still running in the parent. 690 * 691 * This code is (probably) unnecessary as the 'close on exec' flag is 692 * set and should be enough. In the vfork case it is definitely wrong 693 * to close the fds as another fork() may be done later to feed data 694 * from a 'here' document into a pipe and we don't want to close the 695 * pipe! 696 */ 697 698 void 699 closescript(int vforked) 700 { 701 if (vforked) 702 return; 703 popallfiles(); 704 if (parsefile->fd > 0) { 705 sh_close(parsefile->fd); 706 parsefile->fd = 0; 707 } 708 } 709