input.c (12802B)
1 /* - 2 * spdx-license-identifier: bsd-3-clause 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 #if FEATURE_SH_EXT 36 #ifndef _DEFAULT_SOURCE 37 #define _DEFAULT_SOURCE 38 #endif 39 #ifndef _GNU_SOURCE 40 #define _GNU_SOURCE 41 #endif 42 #endif 43 #include <errno.h> 44 #include <fcntl.h> 45 #include <stdio.h> /* defines BUFSIZ */ 46 #include <stdlib.h> 47 #include <string.h> 48 #include <unistd.h> 49 50 /* 51 * this file implements the input routines used by the parser 52 */ 53 54 #include "alias.h" 55 #include "error.h" 56 #include "input.h" 57 #include "memalloc.h" 58 #include "options.h" 59 #include "output.h" 60 #include "parser.h" 61 #include "redir.h" 62 #include "shell.h" 63 #include "syntax.h" 64 #ifndef NO_HISTORY 65 #include "lineedit.h" 66 #endif 67 #include "trap.h" 68 69 #define EOF_NLEFT -99 /* value of parsenleft when EOF pushed back */ 70 71 struct strpush { 72 struct strpush *prev; /* preceding string on stack */ 73 const char *prevstring; 74 int prevnleft; 75 int prevlleft; 76 struct alias *ap; /* if push was associated with an alias */ 77 }; 78 79 /* 80 * the parsefile structure pointed to by the global variable parsefile 81 * contains information about the current file being read 82 */ 83 84 struct parsefile { 85 struct parsefile *prev; /* preceding file on stack */ 86 int linno; /* current line */ 87 int fd; /* file descriptor (or -1 if string) */ 88 int nleft; /* number of chars left in this line */ 89 int lleft; /* number of lines left in this buffer */ 90 const char *nextc; /* next char in buffer */ 91 char *buf; /* input buffer */ 92 size_t bufsize; /* input buffer size */ 93 struct strpush *strpush; /* for pushing strings at this level */ 94 struct strpush basestrpush; /* so pushing one is fast */ 95 }; 96 97 int plinno = 1; /* input line number */ 98 int parsenleft; /* copy of parsefile->nleft */ 99 static int parselleft; /* copy of parsefile->lleft */ 100 const char *parsenextc; /* copy of parsefile->nextc */ 101 static char basebuf[BUFSIZ + 1]; /* buffer for top level input file */ 102 static struct parsefile basepf = { 103 /* top level input file */ 104 .nextc = basebuf, 105 .buf = basebuf, 106 .bufsize = sizeof(basebuf), 107 }; 108 static struct parsefile *parsefile = &basepf; /* current input file */ 109 int whichprompt; /* 1 == PS1, 2 == PS2 */ 110 111 static void pushfile(void); 112 static int preadfd(void); 113 static void popstring(void); 114 115 void 116 resetinput(void) 117 { 118 popallfiles(); 119 parselleft = parsenleft = 0; /* clear input buffer */ 120 } 121 122 /* 123 * read a character from the script, returning PEOF on end of file 124 * nul characters in the input are silently discarded 125 */ 126 127 int 128 pgetc(void) 129 { 130 return pgetc_macro(); 131 } 132 133 static int 134 preadfd(void) 135 { 136 int nr; 137 138 retry: 139 #ifndef NO_HISTORY 140 if (parsefile->fd == 0 && sh_history_enabled) { 141 char *line; 142 143 line = redline(getprompt(NULL)); 144 if (line != NULL) { 145 nr = strlen(line) + 1; 146 if (parsefile->bufsize < (size_t)nr + 1) { 147 size_t bufsize; 148 149 INTOFF; 150 if (parsefile->buf != basebuf) { 151 ckfree(parsefile->buf); 152 parsefile->buf = NULL; 153 parsefile->bufsize = 0; 154 } 155 bufsize = (size_t)nr + BUFSIZ + 1; 156 bufsize -= bufsize % BUFSIZ; 157 parsefile->buf = ckmalloc(bufsize); 158 parsefile->bufsize = bufsize; 159 INTON; 160 } 161 memcpy(parsefile->buf, line, nr - 1); 162 parsefile->buf[nr - 1] = '\n'; 163 parsefile->buf[nr] = '\0'; 164 free(line); 165 } else { 166 nr = 0; 167 } 168 } else 169 #endif 170 nr = read(parsefile->fd, parsefile->buf, parsefile->bufsize - 1); 171 172 if (nr < 0) 173 switch (errno) { 174 int flags; 175 176 case EINTR: 177 goto retry; 178 case EWOULDBLOCK: 179 if (parsefile->fd != 0) 180 break; 181 if ((flags = fcntl(0, F_GETFL, 0)) < 0) 182 break; 183 if (!(flags & O_NONBLOCK)) 184 break; 185 if (fcntl(0, F_SETFL, flags & ~O_NONBLOCK) < 0) 186 break; 187 out2fmt_flush("sh: turning off NDELAY mode\n"); 188 goto retry; 189 } 190 else if (nr > 0) 191 parsefile->buf[nr] = '\0'; 192 else 193 nr = -1; 194 195 parsenextc = parsefile->buf; 196 return nr; 197 } 198 199 /* 200 * refill the input buffer and return the next input character: 201 * 202 * 1) if a string was pushed back on the input, pop it; 203 * 2) if an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading 204 * from a string so we cant refill the buffer, return EOF 205 * 3) if there is more in this buffer, use it else call read to fill it 206 * 4) process input up to the next newline, deleting nul characters 207 */ 208 209 int 210 preadbuffer(void) 211 { 212 const char *end; 213 char *q, *r; 214 char savec; 215 216 while (parsefile->strpush) { 217 /* 218 * add a space to the end of an alias to ensure that the 219 * alias remains in use while parsing its last word 220 * this avoids alias recursions 221 */ 222 if (parsenleft == -1 && parsefile->strpush->ap != NULL) 223 return ' '; 224 popstring(); 225 if (--parsenleft >= 0) 226 return (*parsenextc++); 227 } 228 if (parsenleft == EOF_NLEFT || parsefile->buf == NULL) 229 return PEOF; 230 231 again: 232 if (parselleft <= 0 && (parselleft = preadfd()) == -1) { 233 parselleft = parsenleft = EOF_NLEFT; 234 return (PEOF); 235 } 236 end = parsenextc + parselleft; 237 q = strchrnul(parsenextc, '\n'); 238 if (*q == '\0' && q != end) { 239 /* delete nul characters */ 240 for (r = q++; q != end; q++) 241 if (*q != '\0') 242 *r++ = *q; 243 *r = '\0'; 244 parselleft = r - parsenextc; 245 goto again; 246 } 247 if (*q == '\0') { 248 parsenleft = parselleft; 249 parselleft = 0; 250 } else /* *q == '\n' */ { 251 q++; 252 parsenleft = q - parsenextc; 253 parselleft -= parsenleft; 254 } 255 parsenleft--; 256 257 savec = *q; 258 *q = '\0'; 259 260 #ifndef NO_HISTORY 261 if (parsefile->fd == 0 && sh_history_enabled && parsenextc[strspn(parsenextc, " \t\n")] != '\0') { 262 char *histline = strdup(parsenextc); 263 if (histline) { 264 char *nl = strchr(histline, '\n'); 265 if (nl) 266 *nl = '\0'; 267 INTOFF; 268 redlineHistoryAdd(histline); 269 INTON; 270 free(histline); 271 } 272 } 273 #endif 274 275 if (vflag) { 276 out2str(parsenextc); 277 flushout(out2); 278 } 279 280 *q = savec; 281 282 return *parsenextc++; 283 } 284 285 /* 286 * returns if we are certain we are at EOF. does not cause any more input 287 * to be read from the outside world 288 */ 289 290 int 291 preadateof(void) 292 { 293 if (parsenleft > 0) 294 return 0; 295 if (parsefile->strpush) 296 return 0; 297 if (parsenleft == EOF_NLEFT || parsefile->buf == NULL) 298 return 1; 299 return 0; 300 } 301 302 /* 303 * undo the last call to pgetc. only one character may be pushed back 304 * PEOF may be pushed back 305 */ 306 307 void 308 pungetc(void) 309 { 310 parsenleft++; 311 parsenextc--; 312 } 313 314 /* 315 * push a string back onto the input at this current parsefile level 316 * we handle aliases this way 317 */ 318 void 319 pushstring(const char *s, int len, struct alias *ap) 320 { 321 struct strpush *sp; 322 323 INTOFF; 324 /* out2fmt_flush("*** calling pushstring: %s, %d\n", s, len); */ 325 if (parsefile->strpush) { 326 sp = ckmalloc(sizeof(struct strpush)); 327 sp->prev = parsefile->strpush; 328 parsefile->strpush = sp; 329 } else 330 sp = parsefile->strpush = &(parsefile->basestrpush); 331 sp->prevstring = parsenextc; 332 sp->prevnleft = parsenleft; 333 sp->prevlleft = parselleft; 334 sp->ap = ap; 335 if (ap) 336 ap->flag |= ALIASINUSE; 337 parsenextc = s; 338 parsenleft = len; 339 INTON; 340 } 341 342 static void 343 popstring(void) 344 { 345 struct strpush *sp = parsefile->strpush; 346 347 INTOFF; 348 if (sp->ap) { 349 if (parsenextc != sp->ap->val && (parsenextc[-1] == ' ' || parsenextc[-1] == '\t')) 350 forcealias(); 351 sp->ap->flag &= ~ALIASINUSE; 352 } 353 parsenextc = sp->prevstring; 354 parsenleft = sp->prevnleft; 355 parselleft = sp->prevlleft; 356 /* out2fmt_flush("*** calling popstring: restoring to '%s'\n", 357 * parsenextc); */ 358 parsefile->strpush = sp->prev; 359 if (sp != &(parsefile->basestrpush)) 360 ckfree(sp); 361 INTON; 362 } 363 364 /* 365 * set the input to take input from a file. if push is set, push the 366 * old input onto the stack first 367 * about verify: 368 * -1: obey verifyflag 369 * 0: do not verify 370 * 1: do verify 371 */ 372 373 void 374 setinputfile(const char *fname, int push, int verify) 375 { 376 int e; 377 int fd; 378 int fd2; 379 int oflags = O_RDONLY | O_CLOEXEC; 380 381 if (verify == 1 || (verify == -1 && verifyflag)) 382 oflags |= O_VERIFY; 383 384 INTOFF; 385 if ((fd = open(fname, oflags)) < 0) { 386 e = errno; 387 errorwithstatus( 388 e == ENOENT || e == ENOTDIR ? 127 : 126, "cannot open %s: %s", fname, strerror(e) 389 ); 390 } 391 if (fd < 10) { 392 fd2 = fcntl(fd, F_DUPFD_CLOEXEC, 10); 393 close(fd); 394 if (fd2 < 0) 395 error("Out of file descriptors"); 396 fd = fd2; 397 } 398 setinputfd(fd, push); 399 INTON; 400 } 401 402 /* 403 * like setinputfile, but takes an open file descriptor (which should have 404 * its FD_CLOEXEC flag already set). call this with interrupts off 405 */ 406 407 void 408 setinputfd(int fd, int push) 409 { 410 if (push) 411 pushfile(); 412 if (parsefile->fd > 0) 413 close(parsefile->fd); 414 parsefile->fd = fd; 415 if (parsefile->buf == NULL) { 416 parsefile->buf = ckmalloc(BUFSIZ + 1); 417 parsefile->bufsize = BUFSIZ + 1; 418 } 419 parselleft = parsenleft = 0; 420 plinno = 1; 421 } 422 423 /* 424 * like setinputfile, but takes input from a string 425 */ 426 427 void 428 setinputstring(const char *string) 429 { 430 INTOFF; 431 pushfile(); 432 parsenextc = string; 433 parselleft = parsenleft = strlen(string); 434 plinno = 1; 435 INTON; 436 } 437 438 /* 439 * to handle the "." command, a stack of input files is used. pushfile 440 * adds a new entry to the stack and popfile restores the previous level 441 */ 442 443 static void 444 pushfile(void) 445 { 446 struct parsefile *pf; 447 448 pf = (struct parsefile *)ckmalloc(sizeof(struct parsefile)); 449 *pf = (struct parsefile){.prev = parsefile, .fd = -1}; 450 parsefile->nleft = parsenleft; 451 parsefile->lleft = parselleft; 452 parsefile->nextc = parsenextc; 453 parsefile->linno = plinno; 454 parsefile = pf; 455 } 456 457 void 458 popfile(void) 459 { 460 struct parsefile *pf = parsefile; 461 462 INTOFF; 463 if (pf->fd >= 0) 464 close(pf->fd); 465 if (pf->buf) 466 ckfree(pf->buf); 467 while (pf->strpush) 468 popstring(); 469 parsefile = pf->prev; 470 ckfree(pf); 471 parsenleft = parsefile->nleft; 472 parselleft = parsefile->lleft; 473 parsenextc = parsefile->nextc; 474 plinno = parsefile->linno; 475 INTON; 476 } 477 478 /* 479 * return current file (to go back to it later using popfilesupto()) 480 */ 481 482 struct parsefile * 483 getcurrentfile(void) 484 { 485 return parsefile; 486 } 487 488 /* 489 * pop files until the given file is on top again. useful for regular 490 * builtins that read shell commands from files or strings 491 * if the given file is not an active file, an error is raised 492 */ 493 494 void 495 popfilesupto(struct parsefile *file) 496 { 497 while (parsefile != file && parsefile != &basepf) 498 popfile(); 499 if (parsefile != file) 500 error("popfilesupto() misused"); 501 } 502 503 /* 504 * return to top level 505 */ 506 507 void 508 popallfiles(void) 509 { 510 while (parsefile != &basepf) 511 popfile(); 512 } 513 514 /* 515 * close the file(s) that the shell is reading commands from. called 516 * after a fork is done 517 */ 518 519 void 520 closescript(void) 521 { 522 popallfiles(); 523 if (parsefile->fd > 0) { 524 close(parsefile->fd); 525 parsefile->fd = 0; 526 } 527 }