input.c revision 17987
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.4 1995/11/03 18:50:14 peter Exp $ 37 */ 38 39#ifndef lint 40static char 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 68MKINIT 69struct 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 82MKINIT 83struct 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 line */ 88 int lleft; /* number of lines left in 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 96int plinno = 1; /* input line number */ 97MKINIT int parsenleft; /* copy of parsefile->nleft */ 98MKINIT int parselleft; /* copy of parsefile->lleft */ 99char *parsenextc; /* copy of parsefile->nextc */ 100MKINIT struct parsefile basepf; /* top level input file */ 101char basebuf[BUFSIZ]; /* buffer for top level input file */ 102struct parsefile *parsefile = &basepf; /* current input file */ 103int init_editline = 0; /* editline library initialized? */ 104int whichprompt; /* 1 == PS1, 2 == PS2 */ 105 106EditLine *el; /* cookie for editline package */ 107 108STATIC void pushfile __P((void)); 109 110#ifdef mkinit 111INCLUDE "input.h" 112INCLUDE "error.h" 113 114INIT { 115 extern char basebuf[]; 116 117 basepf.nextc = basepf.buf = basebuf; 118} 119 120RESET { 121 if (exception != EXSHELLPROC) 122 parselleft = parsenleft = 0; /* clear input buffer */ 123 popallfiles(); 124} 125 126SHELLPROC { 127 popallfiles(); 128} 129#endif 130 131 132/* 133 * Read a line from the script. 134 */ 135 136char * 137pfgets(line, len) 138 char *line; 139 int len; 140{ 141 register char *p = line; 142 int nleft = len; 143 int c; 144 145 while (--nleft > 0) { 146 c = pgetc_macro(); 147 if (c == PEOF) { 148 if (p == line) 149 return NULL; 150 break; 151 } 152 *p++ = c; 153 if (c == '\n') 154 break; 155 } 156 *p = '\0'; 157 return line; 158} 159 160 161 162/* 163 * Read a character from the script, returning PEOF on end of file. 164 * Nul characters in the input are silently discarded. 165 */ 166 167int 168pgetc() { 169 return pgetc_macro(); 170} 171 172static int 173pread() 174{ 175 int nr; 176 177 parsenextc = parsefile->buf; 178retry: 179 if (parsefile->fd == 0 && el) { 180 const char *rl_cp; 181 int len; 182 183 rl_cp = el_gets(el, &nr); 184 if (rl_cp == NULL) 185 nr = 0; 186 else { 187 /* XXX - BUFSIZE should redesign so not necessary */ 188 strcpy(parsenextc, rl_cp); 189 } 190 191 } else { 192 nr = read(parsefile->fd, parsenextc, BUFSIZ - 1); 193 } 194 195 if (nr <= 0) { 196 if (nr < 0) { 197 if (errno == EINTR) 198 goto retry; 199 if (parsefile->fd == 0 && errno == EWOULDBLOCK) { 200 int flags = fcntl(0, F_GETFL, 0); 201 if (flags >= 0 && flags & O_NONBLOCK) { 202 flags &=~ O_NONBLOCK; 203 if (fcntl(0, F_SETFL, flags) >= 0) { 204 out2str("sh: turning off NDELAY mode\n"); 205 goto retry; 206 } 207 } 208 } 209 } 210 nr = -1; 211 } 212 return nr; 213} 214 215/* 216 * Refill the input buffer and return the next input character: 217 * 218 * 1) If a string was pushed back on the input, pop it; 219 * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading 220 * from a string so we can't refill the buffer, return EOF. 221 * 3) If there is more in the buffer, use it; else call read to fill it. 222 * 4) Process input up to next newline, deleting nul characters. 223 */ 224 225int 226preadbuffer() { 227 char *p, *q; 228 int more; 229 int something; 230 extern EditLine *el; 231 char savec; 232 233 if (parsefile->strpush) { 234 popstring(); 235 if (--parsenleft >= 0) 236 return (*parsenextc++); 237 } 238 if (parsenleft == EOF_NLEFT || parsefile->buf == NULL) 239 return PEOF; 240 flushout(&output); 241 flushout(&errout); 242 243again: 244 if (parselleft <= 0) { 245 if ((parselleft = pread()) == -1) { 246 parselleft = parsenleft = EOF_NLEFT; 247 return PEOF; 248 } 249 } 250 251 q = p = parsenextc; 252 253 /* delete nul characters */ 254 something = 0; 255 for (more = 1; more;) { 256 switch (*p) { 257 case '\0': 258 p++; /* Skip nul */ 259 goto check; 260 261 case '\t': 262 case ' ': 263 break; 264 265 case '\n': 266 parsenleft = q - parsenextc; 267 more = 0; /* Stop processing here */ 268 break; 269 270 default: 271 something = 1; 272 break; 273 } 274 275 *q++ = *p++; 276check: 277 if (--parselleft <= 0) { 278 parsenleft = q - parsenextc - 1; 279 if (parsenleft < 0) 280 goto again; 281 *q = '\0'; 282 more = 0; 283 } 284 } 285 286 savec = *q; 287 *q = '\0'; 288 289 290 if (parsefile->fd == 0 && hist && something) { 291 INTOFF; 292 history(hist, whichprompt == 1 ? H_ENTER : H_ADD, parsenextc); 293 INTON; 294 } 295 296 297 if (vflag) { 298 out2str(parsenextc); 299 flushout(out2); 300 } 301 302 *q = savec; 303 304 return *parsenextc++; 305} 306 307/* 308 * Undo the last call to pgetc. Only one character may be pushed back. 309 * PEOF may be pushed back. 310 */ 311 312void 313pungetc() { 314 parsenleft++; 315 parsenextc--; 316} 317 318/* 319 * Push a string back onto the input at this current parsefile level. 320 * We handle aliases this way. 321 */ 322void 323pushstring(s, len, ap) 324 char *s; 325 int len; 326 void *ap; 327 { 328 struct strpush *sp; 329 330 INTOFF; 331/*dprintf("*** calling pushstring: %s, %d\n", s, len);*/ 332 if (parsefile->strpush) { 333 sp = ckmalloc(sizeof (struct strpush)); 334 sp->prev = parsefile->strpush; 335 parsefile->strpush = sp; 336 } else 337 sp = parsefile->strpush = &(parsefile->basestrpush); 338 sp->prevstring = parsenextc; 339 sp->prevnleft = parsenleft; 340 sp->prevlleft = parselleft; 341 sp->ap = (struct alias *)ap; 342 if (ap) 343 ((struct alias *)ap)->flag |= ALIASINUSE; 344 parsenextc = s; 345 parsenleft = len; 346 INTON; 347} 348 349void 350popstring() 351{ 352 struct strpush *sp = parsefile->strpush; 353 354 INTOFF; 355 parsenextc = sp->prevstring; 356 parsenleft = sp->prevnleft; 357 parselleft = sp->prevlleft; 358/*dprintf("*** calling popstring: restoring to '%s'\n", parsenextc);*/ 359 if (sp->ap) 360 sp->ap->flag &= ~ALIASINUSE; 361 parsefile->strpush = sp->prev; 362 if (sp != &(parsefile->basestrpush)) 363 ckfree(sp); 364 INTON; 365} 366 367/* 368 * Set the input to take input from a file. If push is set, push the 369 * old input onto the stack first. 370 */ 371 372void 373setinputfile(fname, push) 374 char *fname; 375 int push; 376{ 377 int fd; 378 int fd2; 379 380 INTOFF; 381 if ((fd = open(fname, O_RDONLY)) < 0) 382 error("Can't open %s", fname); 383 if (fd < 10) { 384 fd2 = copyfd(fd, 10); 385 close(fd); 386 if (fd2 < 0) 387 error("Out of file descriptors"); 388 fd = fd2; 389 } 390 setinputfd(fd, push); 391 INTON; 392} 393 394 395/* 396 * Like setinputfile, but takes an open file descriptor. Call this with 397 * interrupts off. 398 */ 399 400void 401setinputfd(fd, push) 402 int fd, push; 403{ 404 if (push) { 405 pushfile(); 406 parsefile->buf = ckmalloc(BUFSIZ); 407 } 408 if (parsefile->fd > 0) 409 close(parsefile->fd); 410 parsefile->fd = fd; 411 if (parsefile->buf == NULL) 412 parsefile->buf = ckmalloc(BUFSIZ); 413 parselleft = parsenleft = 0; 414 plinno = 1; 415} 416 417 418/* 419 * Like setinputfile, but takes input from a string. 420 */ 421 422void 423setinputstring(string, push) 424 char *string; 425 int push; 426 { 427 INTOFF; 428 if (push) 429 pushfile(); 430 parsenextc = string; 431 parselleft = parsenleft = strlen(string); 432 parsefile->buf = NULL; 433 plinno = 1; 434 INTON; 435} 436 437 438 439/* 440 * To handle the "." command, a stack of input files is used. Pushfile 441 * adds a new entry to the stack and popfile restores the previous level. 442 */ 443 444STATIC void 445pushfile() { 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 461void 462popfile() { 463 struct parsefile *pf = parsefile; 464 465 INTOFF; 466 if (pf->fd >= 0) 467 close(pf->fd); 468 if (pf->buf) 469 ckfree(pf->buf); 470 while (pf->strpush) 471 popstring(); 472 parsefile = pf->prev; 473 ckfree(pf); 474 parsenleft = parsefile->nleft; 475 parselleft = parsefile->lleft; 476 parsenextc = parsefile->nextc; 477 plinno = parsefile->linno; 478 INTON; 479} 480 481 482/* 483 * Return to top level. 484 */ 485 486void 487popallfiles() { 488 while (parsefile != &basepf) 489 popfile(); 490} 491 492 493 494/* 495 * Close the file(s) that the shell is reading commands from. Called 496 * after a fork is done. 497 */ 498 499void 500closescript() { 501 popallfiles(); 502 if (parsefile->fd > 0) { 503 close(parsefile->fd); 504 parsefile->fd = 0; 505 } 506} 507