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. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)input.c 8.3 (Berkeley) 6/9/95 37 * $FreeBSD: src/bin/sh/input.c,v 1.14.2.2 2002/08/27 01:36:28 tjr Exp $ 38 * $DragonFly: src/bin/sh/input.c,v 1.7 2006/09/28 22:29:44 pavalos Exp $ 39 */ 40 41 #include <stdio.h> /* defines BUFSIZ */ 42 #include <fcntl.h> 43 #include <errno.h> 44 #include <unistd.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" 63 #include "trap.h" 64 65 #define EOF_NLEFT -99 /* value of parsenleft when EOF pushed back */ 66 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 lines 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 STATIC struct parsefile *parsefile = &basepf; /* current input file */ 102 int init_editline = 0; /* editline library initialized? */ 103 int whichprompt; /* 1 == PS1, 2 == PS2 */ 104 105 EditLine *el; /* cookie for editline package */ 106 107 STATIC void pushfile(void); 108 static int preadfd(void); 109 110 #ifdef mkinit 111 INCLUDE <stdio.h> 112 INCLUDE "input.h" 113 INCLUDE "error.h" 114 115 INIT { 116 basepf.nextc = basepf.buf = basebuf; 117 } 118 119 RESET { 120 if (exception != EXSHELLPROC) 121 parselleft = parsenleft = 0; /* clear input buffer */ 122 popallfiles(); 123 } 124 125 SHELLPROC { 126 popallfiles(); 127 } 128 #endif 129 130 131 /* 132 * Read a line from the script. 133 */ 134 135 char * 136 pfgets(char *line, int len) 137 { 138 char *p = line; 139 int nleft = len; 140 int c; 141 142 while (--nleft > 0) { 143 c = pgetc_macro(); 144 if (c == PEOF) { 145 if (p == line) 146 return NULL; 147 break; 148 } 149 *p++ = c; 150 if (c == '\n') 151 break; 152 } 153 *p = '\0'; 154 return line; 155 } 156 157 158 159 /* 160 * Read a character from the script, returning PEOF on end of file. 161 * Nul characters in the input are silently discarded. 162 */ 163 164 int 165 pgetc(void) 166 { 167 return pgetc_macro(); 168 } 169 170 171 static int 172 preadfd(void) 173 { 174 int nr; 175 parsenextc = parsefile->buf; 176 177 #ifndef NO_HISTORY 178 if (el != NULL && gotwinch) { 179 gotwinch = 0; 180 el_resize(el); 181 } 182 #endif 183 retry: 184 #ifndef NO_HISTORY 185 if (parsefile->fd == 0 && el) { 186 const char *rl_cp; 187 188 rl_cp = el_gets(el, &nr); 189 if (rl_cp == NULL) 190 nr = 0; 191 else { 192 /* XXX - BUFSIZE should redesign so not necessary */ 193 strcpy(parsenextc, rl_cp); 194 } 195 } else 196 #endif 197 nr = read(parsefile->fd, parsenextc, BUFSIZ - 1); 198 199 if (nr <= 0) { 200 if (nr < 0) { 201 if (errno == EINTR) 202 goto retry; 203 if (parsefile->fd == 0 && errno == EWOULDBLOCK) { 204 int flags = fcntl(0, F_GETFL, 0); 205 if (flags >= 0 && flags & O_NONBLOCK) { 206 flags &=~ O_NONBLOCK; 207 if (fcntl(0, F_SETFL, flags) >= 0) { 208 out2str("sh: turning off NDELAY mode\n"); 209 goto retry; 210 } 211 } 212 } 213 } 214 nr = -1; 215 } 216 return nr; 217 } 218 219 /* 220 * Refill the input buffer and return the next input character: 221 * 222 * 1) If a string was pushed back on the input, pop it; 223 * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading 224 * from a string so we can't refill the buffer, return EOF. 225 * 3) If there is more in this buffer, use it else call read to fill it. 226 * 4) Process input up to the next newline, deleting nul characters. 227 */ 228 229 int 230 preadbuffer(void) 231 { 232 char *p, *q; 233 int more; 234 int something; 235 char savec; 236 237 if (parsefile->strpush) { 238 popstring(); 239 if (--parsenleft >= 0) 240 return (*parsenextc++); 241 } 242 if (parsenleft == EOF_NLEFT || parsefile->buf == NULL) 243 return PEOF; 244 flushout(&output); 245 flushout(&errout); 246 247 again: 248 if (parselleft <= 0) { 249 if ((parselleft = preadfd()) == -1) { 250 parselleft = parsenleft = EOF_NLEFT; 251 return PEOF; 252 } 253 } 254 255 q = p = parsenextc; 256 257 /* delete nul characters */ 258 something = 0; 259 for (more = 1; more;) { 260 switch (*p) { 261 case '\0': 262 p++; /* Skip nul */ 263 goto check; 264 265 case '\t': 266 case ' ': 267 break; 268 269 case '\n': 270 parsenleft = q - parsenextc; 271 more = 0; /* Stop processing here */ 272 break; 273 274 default: 275 something = 1; 276 break; 277 } 278 279 *q++ = *p++; 280 check: 281 if (--parselleft <= 0) { 282 parsenleft = q - parsenextc - 1; 283 if (parsenleft < 0) 284 goto again; 285 *q = '\0'; 286 more = 0; 287 } 288 } 289 290 savec = *q; 291 *q = '\0'; 292 293 #ifndef NO_HISTORY 294 if (parsefile->fd == 0 && hist && something) { 295 HistEvent he; 296 INTOFF; 297 history(hist, &he, whichprompt == 1 ? H_ENTER : H_APPEND, parsenextc); 298 INTON; 299 } 300 #endif 301 302 if (vflag) { 303 out2str(parsenextc); 304 flushout(out2); 305 } 306 307 *q = savec; 308 309 return *parsenextc++; 310 } 311 312 /* 313 * Undo the last call to pgetc. Only one character may be pushed back. 314 * PEOF may be pushed back. 315 */ 316 317 void 318 pungetc(void) 319 { 320 parsenleft++; 321 parsenextc--; 322 } 323 324 /* 325 * Push a string back onto the input at this current parsefile level. 326 * We handle aliases this way. 327 */ 328 void 329 pushstring(char *s, int len, void *ap) 330 { 331 struct strpush *sp; 332 333 INTOFF; 334 /*dprintf("*** calling pushstring: %s, %d\n", s, len);*/ 335 if (parsefile->strpush) { 336 sp = ckmalloc(sizeof (struct strpush)); 337 sp->prev = parsefile->strpush; 338 parsefile->strpush = sp; 339 } else 340 sp = parsefile->strpush = &(parsefile->basestrpush); 341 sp->prevstring = parsenextc; 342 sp->prevnleft = parsenleft; 343 sp->prevlleft = parselleft; 344 sp->ap = (struct alias *)ap; 345 if (ap) 346 ((struct alias *)ap)->flag |= ALIASINUSE; 347 parsenextc = s; 348 parsenleft = len; 349 INTON; 350 } 351 352 void 353 popstring(void) 354 { 355 struct strpush *sp = parsefile->strpush; 356 357 INTOFF; 358 parsenextc = sp->prevstring; 359 parsenleft = sp->prevnleft; 360 parselleft = sp->prevlleft; 361 /*dprintf("*** calling popstring: restoring to '%s'\n", parsenextc);*/ 362 if (sp->ap) 363 sp->ap->flag &= ~ALIASINUSE; 364 parsefile->strpush = sp->prev; 365 if (sp != &(parsefile->basestrpush)) 366 ckfree(sp); 367 INTON; 368 } 369 370 /* 371 * Set the input to take input from a file. If push is set, push the 372 * old input onto the stack first. 373 */ 374 375 void 376 setinputfile(const char *fname, int push) 377 { 378 int fd; 379 int fd2; 380 381 INTOFF; 382 if ((fd = open(fname, O_RDONLY)) < 0) 383 error("Can't open %s: %s", fname, strerror(errno)); 384 if (fd < 10) { 385 fd2 = copyfd(fd, 10); 386 close(fd); 387 if (fd2 < 0) 388 error("Out of file descriptors"); 389 fd = fd2; 390 } 391 setinputfd(fd, push); 392 INTON; 393 } 394 395 396 /* 397 * Like setinputfile, but takes an open file descriptor. Call this with 398 * interrupts off. 399 */ 400 401 void 402 setinputfd(int fd, int push) 403 { 404 fcntl(fd, F_SETFD, FD_CLOEXEC); 405 if (push) { 406 pushfile(); 407 parsefile->buf = ckmalloc(BUFSIZ); 408 } 409 if (parsefile->fd > 0) 410 close(parsefile->fd); 411 parsefile->fd = fd; 412 if (parsefile->buf == NULL) 413 parsefile->buf = ckmalloc(BUFSIZ); 414 parselleft = parsenleft = 0; 415 plinno = 1; 416 } 417 418 419 /* 420 * Like setinputfile, but takes input from a string. 421 */ 422 423 void 424 setinputstring(char *string, int push) 425 { 426 INTOFF; 427 if (push) 428 pushfile(); 429 parsenextc = string; 430 parselleft = parsenleft = strlen(string); 431 parsefile->buf = NULL; 432 plinno = 1; 433 INTON; 434 } 435 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 parsefile->nleft = parsenleft; 449 parsefile->lleft = parselleft; 450 parsefile->nextc = parsenextc; 451 parsefile->linno = plinno; 452 pf = (struct parsefile *)ckmalloc(sizeof (struct parsefile)); 453 pf->prev = parsefile; 454 pf->fd = -1; 455 pf->strpush = NULL; 456 pf->basestrpush.prev = NULL; 457 parsefile = pf; 458 } 459 460 461 void 462 popfile(void) 463 { 464 struct parsefile *pf = parsefile; 465 466 INTOFF; 467 if (pf->fd >= 0) 468 close(pf->fd); 469 if (pf->buf) 470 ckfree(pf->buf); 471 while (pf->strpush) 472 popstring(); 473 parsefile = pf->prev; 474 ckfree(pf); 475 parsenleft = parsefile->nleft; 476 parselleft = parsefile->lleft; 477 parsenextc = parsefile->nextc; 478 plinno = parsefile->linno; 479 INTON; 480 } 481 482 483 /* 484 * Return to top level. 485 */ 486 487 void 488 popallfiles(void) 489 { 490 while (parsefile != &basepf) 491 popfile(); 492 } 493 494 495 496 /* 497 * Close the file(s) that the shell is reading commands from. Called 498 * after a fork is done. 499 */ 500 501 void 502 closescript(void) 503 { 504 popallfiles(); 505 if (parsefile->fd > 0) { 506 close(parsefile->fd); 507 parsefile->fd = 0; 508 } 509 } 510