1 /* $NetBSD: input.c,v 1.74 2024/08/03 03:05:58 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.74 2024/08/03 03:05:58 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 if (whichprompt != 2 && HistFP != NULL) { 342 history(hist, &he, H_NSAVE_FP, (size_t)0, HistFP); 343 fflush(HistFP); 344 } 345 INTON; 346 } 347 #endif 348 349 if (vflag) { 350 out2str(parsenextc); 351 flushout(out2); 352 } 353 354 *q = savec; 355 356 return *parsenextc++; 357 } 358 359 /* 360 * Test whether we have reached EOF on input stream. 361 * Return true only if certain (without attempting a read). 362 * 363 * Note the similarity to the opening section of preadbuffer() 364 */ 365 int 366 at_eof(void) 367 { 368 struct strpush *sp = parsefile->strpush; 369 370 if (parsenleft > 0) /* more chars are in the buffer */ 371 return 0; 372 373 while (sp != NULL) { 374 /* 375 * If any pushed string has any remaining data, 376 * then we are not at EOF (simulating popstring()) 377 */ 378 if (sp->prevnleft > 0) 379 return 0; 380 sp = sp->prev; 381 } 382 383 /* 384 * If we reached real EOF and pushed it back, 385 * or if we are just processing a string (not reading a file) 386 * then there is no more. Note that if a file pushes a 387 * string, the file's ->buf remains present. 388 */ 389 if (parsenleft == EOF_NLEFT || parsefile->buf == NULL) 390 return 1; 391 392 /* 393 * In other cases, there might be more 394 */ 395 return 0; 396 } 397 398 /* 399 * Undo the last call to pgetc. Only one character may be pushed back. 400 * PEOF may be pushed back. 401 */ 402 403 void 404 pungetc(void) 405 { 406 parsenleft++; 407 parsenextc--; 408 } 409 410 /* 411 * Push a string back onto the input at this current parsefile level. 412 * We handle aliases this way. 413 */ 414 void 415 pushstring(const char *s, int len, struct alias *ap) 416 { 417 struct strpush *sp; 418 419 VTRACE(DBG_INPUT, 420 ("pushstring(\"%.*s\", %d)%s%s%s had: nl=%d ll=%d \"%.*s\"\n", 421 len, s, len, ap ? " for alias:'" : "", 422 ap ? ap->name : "", ap ? "'" : "", 423 parsenleft, parselleft, parsenleft, parsenextc)); 424 425 INTOFF; 426 if (parsefile->strpush) { 427 sp = ckmalloc(sizeof (struct strpush)); 428 sp->prev = parsefile->strpush; 429 parsefile->strpush = sp; 430 } else 431 sp = parsefile->strpush = &(parsefile->basestrpush); 432 433 sp->prevstring = parsenextc; 434 sp->prevnleft = parsenleft; 435 sp->prevlleft = parselleft; 436 sp->ap = ap; 437 if (ap) 438 ap->flag |= ALIASINUSE; 439 parsenextc = s; 440 parsenleft = len; 441 INTON; 442 } 443 444 void 445 popstring(void) 446 { 447 struct strpush *sp = parsefile->strpush; 448 449 INTOFF; 450 if (sp->ap) { 451 int alen; 452 453 if ((alen = strlen(sp->ap->val)) > 0 && 454 (sp->ap->val[alen - 1] == ' ' || 455 sp->ap->val[alen - 1] == '\t')) 456 checkkwd |= CHKALIAS; 457 sp->ap->flag &= ~ALIASINUSE; 458 } 459 parsenextc = sp->prevstring; 460 parsenleft = sp->prevnleft; 461 parselleft = sp->prevlleft; 462 463 VTRACE(DBG_INPUT, ("popstring()%s%s%s nl=%d ll=%d \"%.*s\"\n", 464 sp->ap ? " from alias:'" : "", sp->ap ? sp->ap->name : "", 465 sp->ap ? "'" : "", parsenleft, parselleft, parsenleft, parsenextc)); 466 467 parsefile->strpush = sp->prev; 468 if (sp != &(parsefile->basestrpush)) 469 ckfree(sp); 470 INTON; 471 } 472 473 /* 474 * Set the input to take input from a file. If push is set, push the 475 * old input onto the stack first. 476 */ 477 478 void 479 setinputfile(const char *fname, int push) 480 { 481 unsigned char magic[4]; 482 int fd; 483 int fd2; 484 struct stat sb; 485 486 CTRACE(DBG_INPUT,("setinputfile(\"%s\", %spush)\n",fname,push?"":"no")); 487 488 INTOFF; 489 if ((fd = open(fname, O_RDONLY)) < 0) 490 error("Can't open %s", fname); 491 492 /* Since the message "Syntax error: "(" unexpected" is not very 493 * helpful, we check if the file starts with the ELF magic to 494 * avoid that message. The first lseek tries to make sure that 495 * we can later rewind the file. 496 */ 497 if (fstat(fd, &sb) == 0 && S_ISREG(sb.st_mode) && 498 lseek(fd, 0, SEEK_SET) == 0) { 499 if (read(fd, magic, 4) == 4) { 500 if (memcmp(magic, "\177ELF", 4) == 0) { 501 (void)close(fd); 502 error("Cannot execute ELF binary %s", fname); 503 } 504 } 505 if (lseek(fd, 0, SEEK_SET) != 0) { 506 (void)close(fd); 507 error("Cannot rewind the file %s", fname); 508 } 509 } 510 511 fd2 = to_upper_fd(fd); /* closes fd, returns higher equiv */ 512 if (fd2 == fd) { 513 (void) close(fd); 514 error("Out of file descriptors"); 515 } 516 517 setinputfd(fd2, push); 518 INTON; 519 } 520 521 /* 522 * When a shell fd needs to be altered (when the user wants to use 523 * the same fd - rare, but happens - we need to locate the ref to 524 * the fd, and update it. This happens via a callback. 525 * This is the callback func for fd's used for shell input 526 */ 527 static void 528 input_fd_swap(int from, int to) 529 { 530 struct parsefile *pf; 531 532 pf = parsefile; 533 while (pf != NULL) { /* don't need to stop at basepf */ 534 if (pf->fd == from) 535 pf->fd = to; 536 pf = pf->prev; 537 } 538 } 539 540 /* 541 * Like setinputfile, but takes an open file descriptor. Call this with 542 * interrupts off. 543 */ 544 545 void 546 setinputfd(int fd, int push) 547 { 548 VTRACE(DBG_INPUT, ("setinputfd(%d, %spush)\n", fd, push?"":"no")); 549 550 INTOFF; 551 register_sh_fd(fd, input_fd_swap); 552 (void) fcntl(fd, F_SETFD, FD_CLOEXEC); 553 if (push) 554 pushfile(); 555 if (parsefile->fd > 0) 556 sh_close(parsefile->fd); 557 parsefile->fd = fd; 558 if (parsefile->buf == NULL) 559 parsefile->buf = ckmalloc(BUFSIZ); 560 parselleft = parsenleft = 0; 561 plinno = 1; 562 INTON; 563 564 CTRACE(DBG_INPUT, ("setinputfd(%d, %spush) done; plinno=1\n", fd, 565 push ? "" : "no")); 566 } 567 568 569 /* 570 * Like setinputfile, but takes input from a string. 571 */ 572 573 void 574 setinputstring(const char *string, int push, int line1) 575 { 576 577 INTOFF; 578 if (push) /* XXX: always, as it happens */ 579 pushfile(); 580 parsenextc = string; 581 parselleft = parsenleft = strlen(string); 582 plinno = line1; 583 INTON; 584 585 CTRACE(DBG_INPUT, 586 ("setinputstring(\"%.20s%s\" (%d), %spush, @ %d)\n", string, 587 (parsenleft > 20 ? "..." : ""), parsenleft, push?"":"no", line1)); 588 } 589 590 591 592 /* 593 * To handle the "." command, a stack of input files is used. Pushfile 594 * adds a new entry to the stack and popfile restores the previous level. 595 */ 596 597 STATIC void 598 pushfile(void) 599 { 600 struct parsefile *pf; 601 602 VTRACE(DBG_INPUT, 603 ("pushfile(): fd=%d buf=%p nl=%d ll=%d \"%.*s\" plinno=%d\n", 604 parsefile->fd, parsefile->buf, parsenleft, parselleft, 605 parsenleft, parsenextc, plinno)); 606 607 parsefile->nleft = parsenleft; 608 parsefile->lleft = parselleft; 609 parsefile->nextc = parsenextc; 610 parsefile->linno = plinno; 611 pf = (struct parsefile *)ckmalloc(sizeof (struct parsefile)); 612 pf->prev = parsefile; 613 pf->fd = -1; 614 pf->strpush = NULL; 615 pf->basestrpush.prev = NULL; 616 pf->buf = NULL; 617 parsefile = pf; 618 } 619 620 621 void 622 popfile(void) 623 { 624 struct parsefile *pf = parsefile; 625 626 INTOFF; 627 if (pf->fd >= 0) 628 sh_close(pf->fd); 629 if (pf->buf) 630 ckfree(pf->buf); 631 while (pf->strpush) 632 popstring(); 633 parsefile = pf->prev; 634 ckfree(pf); 635 parsenleft = parsefile->nleft; 636 parselleft = parsefile->lleft; 637 parsenextc = parsefile->nextc; 638 639 VTRACE(DBG_INPUT, 640 ("popfile(): fd=%d buf=%p nl=%d ll=%d \"%.*s\" plinno:%d->%d\n", 641 parsefile->fd, parsefile->buf, parsenleft, parselleft, 642 parsenleft, parsenextc, plinno, parsefile->linno)); 643 644 plinno = parsefile->linno; 645 INTON; 646 } 647 648 /* 649 * Return current file (to go back to it later using popfilesupto()). 650 */ 651 652 struct parsefile * 653 getcurrentfile(void) 654 { 655 return parsefile; 656 } 657 658 659 /* 660 * Pop files until the given file is on top again. Useful for regular 661 * builtins that read shell commands from files or strings. 662 * If the given file is not an active file, an error is raised. 663 */ 664 665 void 666 popfilesupto(struct parsefile *file) 667 { 668 while (parsefile != file && parsefile != &basepf) 669 popfile(); 670 if (parsefile != file) 671 error("popfilesupto() misused"); 672 } 673 674 675 /* 676 * Return to top level. 677 */ 678 679 void 680 popallfiles(void) 681 { 682 while (parsefile != &basepf) 683 popfile(); 684 } 685 686 687 688 /* 689 * Close the file(s) that the shell is reading commands from. Called 690 * after a fork is done. 691 * 692 * Takes one arg, vfork, which tells it to not modify its global vars 693 * as it is still running in the parent. 694 * 695 * This code is (probably) unnecessary as the 'close on exec' flag is 696 * set and should be enough. In the vfork case it is definitely wrong 697 * to close the fds as another fork() may be done later to feed data 698 * from a 'here' document into a pipe and we don't want to close the 699 * pipe! 700 */ 701 702 void 703 closescript(int vforked) 704 { 705 if (vforked) 706 return; 707 popallfiles(); 708 if (parsefile->fd > 0) { 709 sh_close(parsefile->fd); 710 parsefile->fd = 0; 711 } 712 } 713