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