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 * $Id: input.c,v 1.9 1997/02/22 13:58:28 peter Exp $ 37 */ 38 39 #ifndef lint 40 static char const sccsid[] = "@(#)input.c 8.3 (Berkeley) 6/9/95"; 41 #endif /* not lint */ 42 43 #include <stdio.h> /* defines BUFSIZ */ 44 #include <fcntl.h> 45 #include <errno.h> 46 #include <unistd.h> 47 #include <stdlib.h> 48 #include <string.h> 49 50 /* 51 * This file implements the input routines used by the parser. 52 */ 53 54 #include "shell.h" 55 #include "redir.h" 56 #include "syntax.h" 57 #include "input.h" 58 #include "output.h" 59 #include "options.h" 60 #include "memalloc.h" 61 #include "error.h" 62 #include "alias.h" 63 #include "parser.h" 64 #include "myhistedit.h" 65 66 #define EOF_NLEFT -99 /* value of parsenleft when EOF pushed back */ 67 68 MKINIT 69 struct strpush { 70 struct strpush *prev; /* preceding string on stack */ 71 char *prevstring; 72 int prevnleft; 73 int prevlleft; 74 struct alias *ap; /* if push was associated with an alias */ 75 }; 76 77 /* 78 * The parsefile structure pointed to by the global variable parsefile 79 * contains information about the current file being read. 80 */ 81 82 MKINIT 83 struct parsefile { 84 struct parsefile *prev; /* preceding file on stack */ 85 int linno; /* current line */ 86 int fd; /* file descriptor (or -1 if string) */ 87 int nleft; /* number of chars left in this line */ 88 int lleft; /* number of lines left in this buffer */ 89 char *nextc; /* next char in buffer */ 90 char *buf; /* input buffer */ 91 struct strpush *strpush; /* for pushing strings at this level */ 92 struct strpush basestrpush; /* so pushing one is fast */ 93 }; 94 95 96 int plinno = 1; /* input line number */ 97 MKINIT int parsenleft; /* copy of parsefile->nleft */ 98 MKINIT int parselleft; /* copy of parsefile->lleft */ 99 char *parsenextc; /* copy of parsefile->nextc */ 100 MKINIT struct parsefile basepf; /* top level input file */ 101 char basebuf[BUFSIZ]; /* buffer for top level input file */ 102 struct parsefile *parsefile = &basepf; /* current input file */ 103 int init_editline = 0; /* editline library initialized? */ 104 int whichprompt; /* 1 == PS1, 2 == PS2 */ 105 106 EditLine *el; /* cookie for editline package */ 107 108 STATIC void pushfile __P((void)); 109 static int preadfd __P((void)); 110 111 #ifdef mkinit 112 INCLUDE "input.h" 113 INCLUDE "error.h" 114 115 INIT { 116 extern char basebuf[]; 117 118 basepf.nextc = basepf.buf = basebuf; 119 } 120 121 RESET { 122 if (exception != EXSHELLPROC) 123 parselleft = parsenleft = 0; /* clear input buffer */ 124 popallfiles(); 125 } 126 127 SHELLPROC { 128 popallfiles(); 129 } 130 #endif 131 132 133 /* 134 * Read a line from the script. 135 */ 136 137 char * 138 pfgets(line, len) 139 char *line; 140 int len; 141 { 142 char *p = line; 143 int nleft = len; 144 int c; 145 146 while (--nleft > 0) { 147 c = pgetc_macro(); 148 if (c == PEOF) { 149 if (p == line) 150 return NULL; 151 break; 152 } 153 *p++ = c; 154 if (c == '\n') 155 break; 156 } 157 *p = '\0'; 158 return line; 159 } 160 161 162 163 /* 164 * Read a character from the script, returning PEOF on end of file. 165 * Nul characters in the input are silently discarded. 166 */ 167 168 int 169 pgetc() 170 { 171 return pgetc_macro(); 172 } 173 174 175 static int 176 preadfd() 177 { 178 int nr; 179 parsenextc = parsefile->buf; 180 181 retry: 182 #ifndef NO_HISTORY 183 if (parsefile->fd == 0 && el) { 184 const char *rl_cp; 185 186 rl_cp = el_gets(el, &nr); 187 if (rl_cp == NULL) 188 nr = 0; 189 else { 190 /* XXX - BUFSIZE should redesign so not necessary */ 191 (void) strcpy(parsenextc, rl_cp); 192 } 193 } else 194 #endif 195 nr = read(parsefile->fd, parsenextc, BUFSIZ - 1); 196 197 if (nr <= 0) { 198 if (nr < 0) { 199 if (errno == EINTR) 200 goto retry; 201 if (parsefile->fd == 0 && errno == EWOULDBLOCK) { 202 int flags = fcntl(0, F_GETFL, 0); 203 if (flags >= 0 && flags & O_NONBLOCK) { 204 flags &=~ O_NONBLOCK; 205 if (fcntl(0, F_SETFL, flags) >= 0) { 206 out2str("sh: turning off NDELAY mode\n"); 207 goto retry; 208 } 209 } 210 } 211 } 212 nr = -1; 213 } 214 return nr; 215 } 216 217 /* 218 * Refill the input buffer and return the next input character: 219 * 220 * 1) If a string was pushed back on the input, pop it; 221 * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading 222 * from a string so we can't refill the buffer, return EOF. 223 * 3) If there is more in this buffer, use it else call read to fill it. 224 * 4) Process input up to the next newline, deleting nul characters. 225 */ 226 227 int 228 preadbuffer() 229 { 230 char *p, *q; 231 int more; 232 int something; 233 extern EditLine *el; 234 char savec; 235 236 if (parsefile->strpush) { 237 popstring(); 238 if (--parsenleft >= 0) 239 return (*parsenextc++); 240 } 241 if (parsenleft == EOF_NLEFT || parsefile->buf == NULL) 242 return PEOF; 243 flushout(&output); 244 flushout(&errout); 245 246 again: 247 if (parselleft <= 0) { 248 if ((parselleft = preadfd()) == -1) { 249 parselleft = parsenleft = EOF_NLEFT; 250 return PEOF; 251 } 252 } 253 254 q = p = parsenextc; 255 256 /* delete nul characters */ 257 something = 0; 258 for (more = 1; more;) { 259 switch (*p) { 260 case '\0': 261 p++; /* Skip nul */ 262 goto check; 263 264 case '\t': 265 case ' ': 266 break; 267 268 case '\n': 269 parsenleft = q - parsenextc; 270 more = 0; /* Stop processing here */ 271 break; 272 273 default: 274 something = 1; 275 break; 276 } 277 278 *q++ = *p++; 279 check: 280 if (--parselleft <= 0) { 281 parsenleft = q - parsenextc - 1; 282 if (parsenleft < 0) 283 goto again; 284 *q = '\0'; 285 more = 0; 286 } 287 } 288 289 savec = *q; 290 *q = '\0'; 291 292 #ifndef NO_HISTORY 293 if (parsefile->fd == 0 && hist && something) { 294 INTOFF; 295 history(hist, whichprompt == 1 ? H_ENTER : H_ADD, parsenextc); 296 INTON; 297 } 298 #endif 299 300 if (vflag) { 301 out2str(parsenextc); 302 flushout(out2); 303 } 304 305 *q = savec; 306 307 return *parsenextc++; 308 } 309 310 /* 311 * Undo the last call to pgetc. Only one character may be pushed back. 312 * PEOF may be pushed back. 313 */ 314 315 void 316 pungetc() { 317 parsenleft++; 318 parsenextc--; 319 } 320 321 /* 322 * Push a string back onto the input at this current parsefile level. 323 * We handle aliases this way. 324 */ 325 void 326 pushstring(s, len, ap) 327 char *s; 328 int len; 329 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() 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(fname, push) 377 char *fname; 378 int push; 379 { 380 int fd; 381 int fd2; 382 383 INTOFF; 384 if ((fd = open(fname, O_RDONLY)) < 0) 385 error("Can't open %s", fname); 386 if (fd < 10) { 387 fd2 = copyfd(fd, 10); 388 close(fd); 389 if (fd2 < 0) 390 error("Out of file descriptors"); 391 fd = fd2; 392 } 393 setinputfd(fd, push); 394 INTON; 395 } 396 397 398 /* 399 * Like setinputfile, but takes an open file descriptor. Call this with 400 * interrupts off. 401 */ 402 403 void 404 setinputfd(fd, push) 405 int fd, push; 406 { 407 (void)fcntl(fd, F_SETFD, FD_CLOEXEC); 408 if (push) { 409 pushfile(); 410 parsefile->buf = ckmalloc(BUFSIZ); 411 } 412 if (parsefile->fd > 0) 413 close(parsefile->fd); 414 parsefile->fd = fd; 415 if (parsefile->buf == NULL) 416 parsefile->buf = ckmalloc(BUFSIZ); 417 parselleft = parsenleft = 0; 418 plinno = 1; 419 } 420 421 422 /* 423 * Like setinputfile, but takes input from a string. 424 */ 425 426 void 427 setinputstring(string, push) 428 char *string; 429 int push; 430 { 431 INTOFF; 432 if (push) 433 pushfile(); 434 parsenextc = string; 435 parselleft = parsenleft = strlen(string); 436 parsefile->buf = NULL; 437 plinno = 1; 438 INTON; 439 } 440 441 442 443 /* 444 * To handle the "." command, a stack of input files is used. Pushfile 445 * adds a new entry to the stack and popfile restores the previous level. 446 */ 447 448 STATIC void 449 pushfile() { 450 struct parsefile *pf; 451 452 parsefile->nleft = parsenleft; 453 parsefile->lleft = parselleft; 454 parsefile->nextc = parsenextc; 455 parsefile->linno = plinno; 456 pf = (struct parsefile *)ckmalloc(sizeof (struct parsefile)); 457 pf->prev = parsefile; 458 pf->fd = -1; 459 pf->strpush = NULL; 460 pf->basestrpush.prev = NULL; 461 parsefile = pf; 462 } 463 464 465 void 466 popfile() { 467 struct parsefile *pf = parsefile; 468 469 INTOFF; 470 if (pf->fd >= 0) 471 close(pf->fd); 472 if (pf->buf) 473 ckfree(pf->buf); 474 while (pf->strpush) 475 popstring(); 476 parsefile = pf->prev; 477 ckfree(pf); 478 parsenleft = parsefile->nleft; 479 parselleft = parsefile->lleft; 480 parsenextc = parsefile->nextc; 481 plinno = parsefile->linno; 482 INTON; 483 } 484 485 486 /* 487 * Return to top level. 488 */ 489 490 void 491 popallfiles() { 492 while (parsefile != &basepf) 493 popfile(); 494 } 495 496 497 498 /* 499 * Close the file(s) that the shell is reading commands from. Called 500 * after a fork is done. 501 */ 502 503 void 504 closescript() { 505 popallfiles(); 506 if (parsefile->fd > 0) { 507 close(parsefile->fd); 508 parsefile->fd = 0; 509 } 510 } 511