input.c revision 1.70 1 /* $NetBSD: input.c,v 1.70 2019/02/09 03:35:55 kre Exp $ */
2
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 #include <sys/cdefs.h>
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)input.c 8.3 (Berkeley) 6/9/95";
39 #else
40 __RCSID("$NetBSD: input.c,v 1.70 2019/02/09 03:35:55 kre Exp $");
41 #endif
42 #endif /* not lint */
43
44 #include <stdio.h> /* defines BUFSIZ */
45 #include <fcntl.h>
46 #include <errno.h>
47 #include <unistd.h>
48 #include <limits.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <sys/stat.h>
52
53 /*
54 * This file implements the input routines used by the parser.
55 */
56
57 #include "shell.h"
58 #include "redir.h"
59 #include "syntax.h"
60 #include "input.h"
61 #include "output.h"
62 #include "options.h"
63 #include "memalloc.h"
64 #include "error.h"
65 #include "alias.h"
66 #include "parser.h"
67 #include "myhistedit.h"
68 #include "show.h"
69
70 #define EOF_NLEFT -99 /* value of parsenleft when EOF pushed back */
71
72 MKINIT
73 struct strpush {
74 struct strpush *prev; /* preceding string on stack */
75 const char *prevstring;
76 int prevnleft;
77 int prevlleft;
78 struct alias *ap; /* if push was associated with an alias */
79 };
80
81 /*
82 * The parsefile structure pointed to by the global variable parsefile
83 * contains information about the current file being read.
84 */
85
86 MKINIT
87 struct parsefile {
88 struct parsefile *prev; /* preceding file on stack */
89 int linno; /* current line */
90 int fd; /* file descriptor (or -1 if string) */
91 int nleft; /* number of chars left in this line */
92 int lleft; /* number of chars left in this buffer */
93 const char *nextc; /* next char in buffer */
94 char *buf; /* input buffer */
95 struct strpush *strpush; /* for pushing strings at this level */
96 struct strpush basestrpush; /* so pushing one is fast */
97 };
98
99
100 int plinno = 1; /* input line number */
101 int parsenleft; /* copy of parsefile->nleft */
102 MKINIT int parselleft; /* copy of parsefile->lleft */
103 const char *parsenextc; /* copy of parsefile->nextc */
104 MKINIT struct parsefile basepf; /* top level input file */
105 MKINIT char basebuf[BUFSIZ]; /* buffer for top level input file */
106 struct parsefile *parsefile = &basepf; /* current input file */
107 int init_editline = 0; /* editline library initialized? */
108 int whichprompt; /* 1 == PS1, 2 == PS2 */
109
110 STATIC void pushfile(void);
111 static int preadfd(void);
112
113 #ifdef mkinit
114 INCLUDE <stdio.h>
115 INCLUDE "input.h"
116 INCLUDE "error.h"
117
118 INIT {
119 basepf.nextc = basepf.buf = basebuf;
120 }
121
122 RESET {
123 if (exception != EXSHELLPROC)
124 parselleft = parsenleft = 0; /* clear input buffer */
125 popallfiles();
126 }
127
128 SHELLPROC {
129 popallfiles();
130 }
131 #endif
132
133
134 #if 0 /* this is unused */
135 /*
136 * Read a line from the script.
137 */
138
139 char *
140 pfgets(char *line, 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 == PFAKE) /* consecutive PFAKEs is impossible */
149 c = pgetc_macro();
150 if (c == PEOF) {
151 if (p == line)
152 return NULL;
153 break;
154 }
155 *p++ = c;
156 if (c == '\n') {
157 plinno++;
158 break;
159 }
160 }
161 *p = '\0';
162 return line;
163 }
164 #endif
165
166
167 /*
168 * Read a character from the script, returning PEOF on end of file.
169 * Nul characters in the input are silently discarded.
170 */
171
172 int
173 pgetc(void)
174 {
175 int c;
176
177 c = pgetc_macro();
178 if (c == PFAKE)
179 c = pgetc_macro();
180 return c;
181 }
182
183
184 static int
185 preadfd(void)
186 {
187 int nr;
188 char *buf = parsefile->buf;
189 parsenextc = buf;
190
191 retry:
192 #ifndef SMALL
193 if (parsefile->fd == 0 && el) {
194 static const char *rl_cp;
195 static int el_len;
196
197 if (rl_cp == NULL)
198 rl_cp = el_gets(el, &el_len);
199 if (rl_cp == NULL)
200 nr = el_len == 0 ? 0 : -1;
201 else {
202 nr = el_len;
203 if (nr > BUFSIZ - 8)
204 nr = BUFSIZ - 8;
205 memcpy(buf, rl_cp, nr);
206 if (nr != el_len) {
207 el_len -= nr;
208 rl_cp += nr;
209 } else
210 rl_cp = 0;
211 }
212
213 } else
214 #endif
215 nr = read(parsefile->fd, buf, BUFSIZ - 8);
216
217
218 if (nr <= 0) {
219 if (nr < 0) {
220 if (errno == EINTR)
221 goto retry;
222 if (parsefile->fd == 0 && errno == EWOULDBLOCK) {
223 int flags = fcntl(0, F_GETFL, 0);
224
225 if (flags >= 0 && flags & O_NONBLOCK) {
226 flags &=~ O_NONBLOCK;
227 if (fcntl(0, F_SETFL, flags) >= 0) {
228 out2str("sh: turning off NDELAY mode\n");
229 goto retry;
230 }
231 }
232 }
233 }
234 nr = -1;
235 }
236 return nr;
237 }
238
239 /*
240 * Refill the input buffer and return the next input character:
241 *
242 * 1) If a string was pushed back on the input, pop it;
243 * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading
244 * from a string so we can't refill the buffer, return EOF.
245 * 3) If there is more stuff in this buffer, use it else call read to fill it.
246 * 4) Process input up to the next newline, deleting nul characters.
247 */
248
249 int
250 preadbuffer(void)
251 {
252 char *p, *q;
253 int more;
254 #ifndef SMALL
255 int something;
256 #endif
257 char savec;
258
259 while (parsefile->strpush) {
260 if (parsenleft == -1 && parsefile->strpush->ap != NULL)
261 return PFAKE;
262 popstring();
263 if (--parsenleft >= 0)
264 return (*parsenextc++);
265 }
266 if (parsenleft == EOF_NLEFT || parsefile->buf == NULL)
267 return PEOF;
268 flushout(&output);
269 flushout(&errout);
270
271 again:
272 if (parselleft <= 0) {
273 if ((parselleft = preadfd()) == -1) {
274 parselleft = parsenleft = EOF_NLEFT;
275 return PEOF;
276 }
277 }
278
279 /* p = (not const char *)parsenextc; */
280 p = parsefile->buf + (parsenextc - parsefile->buf);
281 q = p;
282
283 /* delete nul characters */
284 #ifndef SMALL
285 something = 0;
286 #endif
287 for (more = 1; more;) {
288 switch (*p) {
289 case '\0':
290 p++; /* Skip nul */
291 goto check;
292
293 case '\t':
294 case ' ':
295 break;
296
297 case '\n':
298 parsenleft = q - parsenextc;
299 more = 0; /* Stop processing here */
300 break;
301
302 default:
303 #ifndef SMALL
304 something = 1;
305 #endif
306 break;
307 }
308
309 *q++ = *p++;
310 check:
311 if (--parselleft <= 0) {
312 parsenleft = q - parsenextc - 1;
313 if (parsenleft < 0)
314 goto again;
315 *q = '\0';
316 more = 0;
317 }
318 }
319
320 savec = *q;
321 *q = '\0';
322
323 #ifndef SMALL
324 if (parsefile->fd == 0 && hist && (something || whichprompt == 2)) {
325 HistEvent he;
326
327 INTOFF;
328 history(hist, &he, whichprompt != 2 ? H_ENTER : H_APPEND,
329 parsenextc);
330 INTON;
331 }
332 #endif
333
334 if (vflag) {
335 out2str(parsenextc);
336 flushout(out2);
337 }
338
339 *q = savec;
340
341 return *parsenextc++;
342 }
343
344 /*
345 * Test whether we have reached EOF on input stream.
346 * Return true only if certain (without attempting a read).
347 *
348 * Note the similarity to the opening section of preadbuffer()
349 */
350 int
351 at_eof(void)
352 {
353 struct strpush *sp = parsefile->strpush;
354
355 if (parsenleft > 0) /* more chars are in the buffer */
356 return 0;
357
358 while (sp != NULL) {
359 /*
360 * If any pushed string has any remaining data,
361 * then we are not at EOF (simulating popstring())
362 */
363 if (sp->prevnleft > 0)
364 return 0;
365 sp = sp->prev;
366 }
367
368 /*
369 * If we reached real EOF and pushed it back,
370 * or if we are just processing a string (not reading a file)
371 * then there is no more. Note that if a file pushes a
372 * string, the file's ->buf remains present.
373 */
374 if (parsenleft == EOF_NLEFT || parsefile->buf == NULL)
375 return 1;
376
377 /*
378 * In other cases, there might be more
379 */
380 return 0;
381 }
382
383 /*
384 * Undo the last call to pgetc. Only one character may be pushed back.
385 * PEOF may be pushed back.
386 */
387
388 void
389 pungetc(void)
390 {
391 parsenleft++;
392 parsenextc--;
393 }
394
395 /*
396 * Push a string back onto the input at this current parsefile level.
397 * We handle aliases this way.
398 */
399 void
400 pushstring(const char *s, int len, struct alias *ap)
401 {
402 struct strpush *sp;
403
404 VTRACE(DBG_INPUT,
405 ("pushstring(\"%.*s\", %d)%s%s%s had: nl=%d ll=%d \"%.*s\"\n",
406 len, s, len, ap ? " for alias:'" : "",
407 ap ? ap->name : "", ap ? "'" : "",
408 parsenleft, parselleft, parsenleft, parsenextc));
409
410 INTOFF;
411 if (parsefile->strpush) {
412 sp = ckmalloc(sizeof (struct strpush));
413 sp->prev = parsefile->strpush;
414 parsefile->strpush = sp;
415 } else
416 sp = parsefile->strpush = &(parsefile->basestrpush);
417
418 sp->prevstring = parsenextc;
419 sp->prevnleft = parsenleft;
420 sp->prevlleft = parselleft;
421 sp->ap = ap;
422 if (ap)
423 ap->flag |= ALIASINUSE;
424 parsenextc = s;
425 parsenleft = len;
426 INTON;
427 }
428
429 void
430 popstring(void)
431 {
432 struct strpush *sp = parsefile->strpush;
433
434 INTOFF;
435 if (sp->ap) {
436 int alen;
437
438 if ((alen = strlen(sp->ap->val)) > 0 &&
439 (sp->ap->val[alen - 1] == ' ' ||
440 sp->ap->val[alen - 1] == '\t'))
441 checkkwd |= CHKALIAS;
442 sp->ap->flag &= ~ALIASINUSE;
443 }
444 parsenextc = sp->prevstring;
445 parsenleft = sp->prevnleft;
446 parselleft = sp->prevlleft;
447
448 VTRACE(DBG_INPUT, ("popstring()%s%s%s nl=%d ll=%d \"%.*s\"\n",
449 sp->ap ? " from alias:'" : "", sp->ap ? sp->ap->name : "",
450 sp->ap ? "'" : "", parsenleft, parselleft, parsenleft, parsenextc));
451
452 parsefile->strpush = sp->prev;
453 if (sp != &(parsefile->basestrpush))
454 ckfree(sp);
455 INTON;
456 }
457
458 /*
459 * Set the input to take input from a file. If push is set, push the
460 * old input onto the stack first.
461 */
462
463 void
464 setinputfile(const char *fname, int push)
465 {
466 unsigned char magic[4];
467 int fd;
468 int fd2;
469 struct stat sb;
470
471 CTRACE(DBG_INPUT,("setinputfile(\"%s\", %spush)\n",fname,push?"":"no"));
472
473 INTOFF;
474 if ((fd = open(fname, O_RDONLY)) < 0)
475 error("Can't open %s", fname);
476
477 /* Since the message "Syntax error: "(" unexpected" is not very
478 * helpful, we check if the file starts with the ELF magic to
479 * avoid that message. The first lseek tries to make sure that
480 * we can later rewind the file.
481 */
482 if (fstat(fd, &sb) == 0 && S_ISREG(sb.st_mode) &&
483 lseek(fd, 0, SEEK_SET) == 0) {
484 if (read(fd, magic, 4) == 4) {
485 if (memcmp(magic, "\177ELF", 4) == 0) {
486 (void)close(fd);
487 error("Cannot execute ELF binary %s", fname);
488 }
489 }
490 if (lseek(fd, 0, SEEK_SET) != 0) {
491 (void)close(fd);
492 error("Cannot rewind the file %s", fname);
493 }
494 }
495
496 fd2 = to_upper_fd(fd); /* closes fd, returns higher equiv */
497 if (fd2 == fd) {
498 (void) close(fd);
499 error("Out of file descriptors");
500 }
501
502 setinputfd(fd2, push);
503 INTON;
504 }
505
506 /*
507 * When a shell fd needs to be altered (when the user wants to use
508 * the same fd - rare, but happens - we need to locate the ref to
509 * the fd, and update it. This happens via a callback.
510 * This is the callback func for fd's used for shell input
511 */
512 static void
513 input_fd_swap(int from, int to)
514 {
515 struct parsefile *pf;
516
517 pf = parsefile;
518 while (pf != NULL) { /* don't need to stop at basepf */
519 if (pf->fd == from)
520 pf->fd = to;
521 pf = pf->prev;
522 }
523 }
524
525 /*
526 * Like setinputfile, but takes an open file descriptor. Call this with
527 * interrupts off.
528 */
529
530 void
531 setinputfd(int fd, int push)
532 {
533 VTRACE(DBG_INPUT, ("setinputfd(%d, %spush)\n", fd, push?"":"no"));
534
535 INTOFF;
536 register_sh_fd(fd, input_fd_swap);
537 (void) fcntl(fd, F_SETFD, FD_CLOEXEC);
538 if (push)
539 pushfile();
540 if (parsefile->fd > 0)
541 sh_close(parsefile->fd);
542 parsefile->fd = fd;
543 if (parsefile->buf == NULL)
544 parsefile->buf = ckmalloc(BUFSIZ);
545 parselleft = parsenleft = 0;
546 plinno = 1;
547 INTON;
548
549 CTRACE(DBG_INPUT, ("setinputfd(%d, %spush) done; plinno=1\n", fd,
550 push ? "" : "no"));
551 }
552
553
554 /*
555 * Like setinputfile, but takes input from a string.
556 */
557
558 void
559 setinputstring(char *string, int push, int line1)
560 {
561
562 INTOFF;
563 if (push) /* XXX: always, as it happens */
564 pushfile();
565 parsenextc = string;
566 parselleft = parsenleft = strlen(string);
567 plinno = line1;
568 INTON;
569
570 CTRACE(DBG_INPUT,
571 ("setinputstring(\"%.20s%s\" (%d), %spush, @ %d)\n", string,
572 (parsenleft > 20 ? "..." : ""), parsenleft, push?"":"no", line1));
573 }
574
575
576
577 /*
578 * To handle the "." command, a stack of input files is used. Pushfile
579 * adds a new entry to the stack and popfile restores the previous level.
580 */
581
582 STATIC void
583 pushfile(void)
584 {
585 struct parsefile *pf;
586
587 VTRACE(DBG_INPUT,
588 ("pushfile(): fd=%d buf=%p nl=%d ll=%d \"%.*s\" plinno=%d\n",
589 parsefile->fd, parsefile->buf, parsenleft, parselleft,
590 parsenleft, parsenextc, plinno));
591
592 parsefile->nleft = parsenleft;
593 parsefile->lleft = parselleft;
594 parsefile->nextc = parsenextc;
595 parsefile->linno = plinno;
596 pf = (struct parsefile *)ckmalloc(sizeof (struct parsefile));
597 pf->prev = parsefile;
598 pf->fd = -1;
599 pf->strpush = NULL;
600 pf->basestrpush.prev = NULL;
601 pf->buf = NULL;
602 parsefile = pf;
603 }
604
605
606 void
607 popfile(void)
608 {
609 struct parsefile *pf = parsefile;
610
611 INTOFF;
612 if (pf->fd >= 0)
613 sh_close(pf->fd);
614 if (pf->buf)
615 ckfree(pf->buf);
616 while (pf->strpush)
617 popstring();
618 parsefile = pf->prev;
619 ckfree(pf);
620 parsenleft = parsefile->nleft;
621 parselleft = parsefile->lleft;
622 parsenextc = parsefile->nextc;
623
624 VTRACE(DBG_INPUT,
625 ("popfile(): fd=%d buf=%p nl=%d ll=%d \"%.*s\" plinno:%d->%d\n",
626 parsefile->fd, parsefile->buf, parsenleft, parselleft,
627 parsenleft, parsenextc, plinno, parsefile->linno));
628
629 plinno = parsefile->linno;
630 INTON;
631 }
632
633 /*
634 * Return current file (to go back to it later using popfilesupto()).
635 */
636
637 struct parsefile *
638 getcurrentfile(void)
639 {
640 return parsefile;
641 }
642
643
644 /*
645 * Pop files until the given file is on top again. Useful for regular
646 * builtins that read shell commands from files or strings.
647 * If the given file is not an active file, an error is raised.
648 */
649
650 void
651 popfilesupto(struct parsefile *file)
652 {
653 while (parsefile != file && parsefile != &basepf)
654 popfile();
655 if (parsefile != file)
656 error("popfilesupto() misused");
657 }
658
659
660 /*
661 * Return to top level.
662 */
663
664 void
665 popallfiles(void)
666 {
667 while (parsefile != &basepf)
668 popfile();
669 }
670
671
672
673 /*
674 * Close the file(s) that the shell is reading commands from. Called
675 * after a fork is done.
676 *
677 * Takes one arg, vfork, which tells it to not modify its global vars
678 * as it is still running in the parent.
679 *
680 * This code is (probably) unnecessary as the 'close on exec' flag is
681 * set and should be enough. In the vfork case it is definitely wrong
682 * to close the fds as another fork() may be done later to feed data
683 * from a 'here' document into a pipe and we don't want to close the
684 * pipe!
685 */
686
687 void
688 closescript(int vforked)
689 {
690 if (vforked)
691 return;
692 popallfiles();
693 if (parsefile->fd > 0) {
694 sh_close(parsefile->fd);
695 parsefile->fd = 0;
696 }
697 }
698