input.c revision 1.42 1 1.42 roy /* $NetBSD: input.c,v 1.42 2009/03/10 21:21:11 roy Exp $ */
2 1.13 cgd
3 1.1 cgd /*-
4 1.5 jtc * Copyright (c) 1991, 1993
5 1.5 jtc * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Kenneth Almquist.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.39 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd */
34 1.1 cgd
35 1.27 christos #include <sys/cdefs.h>
36 1.1 cgd #ifndef lint
37 1.13 cgd #if 0
38 1.16 christos static char sccsid[] = "@(#)input.c 8.3 (Berkeley) 6/9/95";
39 1.13 cgd #else
40 1.42 roy __RCSID("$NetBSD: input.c,v 1.42 2009/03/10 21:21:11 roy Exp $");
41 1.13 cgd #endif
42 1.1 cgd #endif /* not lint */
43 1.1 cgd
44 1.14 christos #include <stdio.h> /* defines BUFSIZ */
45 1.14 christos #include <fcntl.h>
46 1.14 christos #include <errno.h>
47 1.14 christos #include <unistd.h>
48 1.14 christos #include <stdlib.h>
49 1.15 cgd #include <string.h>
50 1.14 christos
51 1.1 cgd /*
52 1.1 cgd * This file implements the input routines used by the parser.
53 1.1 cgd */
54 1.1 cgd
55 1.6 jtc #include "shell.h"
56 1.14 christos #include "redir.h"
57 1.1 cgd #include "syntax.h"
58 1.1 cgd #include "input.h"
59 1.1 cgd #include "output.h"
60 1.5 jtc #include "options.h"
61 1.1 cgd #include "memalloc.h"
62 1.1 cgd #include "error.h"
63 1.5 jtc #include "alias.h"
64 1.5 jtc #include "parser.h"
65 1.5 jtc #include "myhistedit.h"
66 1.1 cgd
67 1.1 cgd #define EOF_NLEFT -99 /* value of parsenleft when EOF pushed back */
68 1.1 cgd
69 1.5 jtc MKINIT
70 1.5 jtc struct strpush {
71 1.5 jtc struct strpush *prev; /* preceding string on stack */
72 1.5 jtc char *prevstring;
73 1.5 jtc int prevnleft;
74 1.17 christos int prevlleft;
75 1.5 jtc struct alias *ap; /* if push was associated with an alias */
76 1.5 jtc };
77 1.1 cgd
78 1.1 cgd /*
79 1.1 cgd * The parsefile structure pointed to by the global variable parsefile
80 1.1 cgd * contains information about the current file being read.
81 1.1 cgd */
82 1.1 cgd
83 1.1 cgd MKINIT
84 1.1 cgd struct parsefile {
85 1.5 jtc struct parsefile *prev; /* preceding file on stack */
86 1.1 cgd int linno; /* current line */
87 1.1 cgd int fd; /* file descriptor (or -1 if string) */
88 1.17 christos int nleft; /* number of chars left in this line */
89 1.17 christos int lleft; /* number of chars left in this buffer */
90 1.1 cgd char *nextc; /* next char in buffer */
91 1.1 cgd char *buf; /* input buffer */
92 1.5 jtc struct strpush *strpush; /* for pushing strings at this level */
93 1.5 jtc struct strpush basestrpush; /* so pushing one is fast */
94 1.1 cgd };
95 1.1 cgd
96 1.1 cgd
97 1.1 cgd int plinno = 1; /* input line number */
98 1.35 christos int parsenleft; /* copy of parsefile->nleft */
99 1.17 christos MKINIT int parselleft; /* copy of parsefile->lleft */
100 1.1 cgd char *parsenextc; /* copy of parsefile->nextc */
101 1.1 cgd MKINIT struct parsefile basepf; /* top level input file */
102 1.35 christos MKINIT char basebuf[BUFSIZ]; /* buffer for top level input file */
103 1.1 cgd struct parsefile *parsefile = &basepf; /* current input file */
104 1.5 jtc int init_editline = 0; /* editline library initialized? */
105 1.5 jtc int whichprompt; /* 1 == PS1, 2 == PS2 */
106 1.5 jtc
107 1.37 christos STATIC void pushfile(void);
108 1.37 christos static int preadfd(void);
109 1.1 cgd
110 1.1 cgd #ifdef mkinit
111 1.35 christos INCLUDE <stdio.h>
112 1.1 cgd INCLUDE "input.h"
113 1.1 cgd INCLUDE "error.h"
114 1.1 cgd
115 1.1 cgd INIT {
116 1.1 cgd basepf.nextc = basepf.buf = basebuf;
117 1.1 cgd }
118 1.1 cgd
119 1.1 cgd RESET {
120 1.1 cgd if (exception != EXSHELLPROC)
121 1.17 christos parselleft = parsenleft = 0; /* clear input buffer */
122 1.1 cgd popallfiles();
123 1.1 cgd }
124 1.1 cgd
125 1.1 cgd SHELLPROC {
126 1.1 cgd popallfiles();
127 1.1 cgd }
128 1.1 cgd #endif
129 1.1 cgd
130 1.1 cgd
131 1.1 cgd /*
132 1.1 cgd * Read a line from the script.
133 1.1 cgd */
134 1.1 cgd
135 1.1 cgd char *
136 1.37 christos pfgets(char *line, int len)
137 1.11 cgd {
138 1.22 tls char *p = line;
139 1.1 cgd int nleft = len;
140 1.1 cgd int c;
141 1.1 cgd
142 1.1 cgd while (--nleft > 0) {
143 1.1 cgd c = pgetc_macro();
144 1.1 cgd if (c == PEOF) {
145 1.1 cgd if (p == line)
146 1.1 cgd return NULL;
147 1.1 cgd break;
148 1.1 cgd }
149 1.1 cgd *p++ = c;
150 1.1 cgd if (c == '\n')
151 1.1 cgd break;
152 1.1 cgd }
153 1.1 cgd *p = '\0';
154 1.1 cgd return line;
155 1.1 cgd }
156 1.1 cgd
157 1.1 cgd
158 1.1 cgd
159 1.1 cgd /*
160 1.1 cgd * Read a character from the script, returning PEOF on end of file.
161 1.1 cgd * Nul characters in the input are silently discarded.
162 1.1 cgd */
163 1.1 cgd
164 1.1 cgd int
165 1.37 christos pgetc(void)
166 1.17 christos {
167 1.1 cgd return pgetc_macro();
168 1.1 cgd }
169 1.1 cgd
170 1.1 cgd
171 1.17 christos static int
172 1.37 christos preadfd(void)
173 1.17 christos {
174 1.17 christos int nr;
175 1.32 christos char *buf = parsefile->buf;
176 1.32 christos parsenextc = buf;
177 1.17 christos
178 1.17 christos retry:
179 1.25 christos #ifndef SMALL
180 1.17 christos if (parsefile->fd == 0 && el) {
181 1.37 christos static const char *rl_cp;
182 1.37 christos static int el_len;
183 1.17 christos
184 1.37 christos if (rl_cp == NULL)
185 1.37 christos rl_cp = el_gets(el, &el_len);
186 1.17 christos if (rl_cp == NULL)
187 1.42 roy nr = el_len == 0 ? 0 : -1;
188 1.17 christos else {
189 1.37 christos nr = el_len;
190 1.37 christos if (nr > BUFSIZ - 8)
191 1.37 christos nr = BUFSIZ - 8;
192 1.37 christos memcpy(buf, rl_cp, nr);
193 1.37 christos if (nr != el_len) {
194 1.37 christos el_len -= nr;
195 1.37 christos rl_cp += nr;
196 1.37 christos } else
197 1.37 christos rl_cp = 0;
198 1.17 christos }
199 1.37 christos
200 1.24 christos } else
201 1.24 christos #endif
202 1.37 christos nr = read(parsefile->fd, buf, BUFSIZ - 8);
203 1.24 christos
204 1.17 christos
205 1.17 christos if (nr <= 0) {
206 1.17 christos if (nr < 0) {
207 1.17 christos if (errno == EINTR)
208 1.17 christos goto retry;
209 1.17 christos if (parsefile->fd == 0 && errno == EWOULDBLOCK) {
210 1.17 christos int flags = fcntl(0, F_GETFL, 0);
211 1.17 christos if (flags >= 0 && flags & O_NONBLOCK) {
212 1.17 christos flags &=~ O_NONBLOCK;
213 1.17 christos if (fcntl(0, F_SETFL, flags) >= 0) {
214 1.17 christos out2str("sh: turning off NDELAY mode\n");
215 1.17 christos goto retry;
216 1.17 christos }
217 1.17 christos }
218 1.17 christos }
219 1.17 christos }
220 1.17 christos nr = -1;
221 1.17 christos }
222 1.17 christos return nr;
223 1.17 christos }
224 1.17 christos
225 1.1 cgd /*
226 1.1 cgd * Refill the input buffer and return the next input character:
227 1.1 cgd *
228 1.5 jtc * 1) If a string was pushed back on the input, pop it;
229 1.1 cgd * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading
230 1.1 cgd * from a string so we can't refill the buffer, return EOF.
231 1.17 christos * 3) If the is more stuff in this buffer, use it else call read to fill it.
232 1.17 christos * 4) Process input up to the next newline, deleting nul characters.
233 1.1 cgd */
234 1.1 cgd
235 1.1 cgd int
236 1.37 christos preadbuffer(void)
237 1.17 christos {
238 1.17 christos char *p, *q;
239 1.17 christos int more;
240 1.17 christos int something;
241 1.17 christos char savec;
242 1.1 cgd
243 1.5 jtc if (parsefile->strpush) {
244 1.5 jtc popstring();
245 1.1 cgd if (--parsenleft >= 0)
246 1.5 jtc return (*parsenextc++);
247 1.1 cgd }
248 1.1 cgd if (parsenleft == EOF_NLEFT || parsefile->buf == NULL)
249 1.1 cgd return PEOF;
250 1.1 cgd flushout(&output);
251 1.1 cgd flushout(&errout);
252 1.5 jtc
253 1.17 christos again:
254 1.17 christos if (parselleft <= 0) {
255 1.26 christos if ((parselleft = preadfd()) == -1) {
256 1.17 christos parselleft = parsenleft = EOF_NLEFT;
257 1.17 christos return PEOF;
258 1.5 jtc }
259 1.17 christos }
260 1.5 jtc
261 1.17 christos q = p = parsenextc;
262 1.1 cgd
263 1.1 cgd /* delete nul characters */
264 1.5 jtc something = 0;
265 1.17 christos for (more = 1; more;) {
266 1.17 christos switch (*p) {
267 1.17 christos case '\0':
268 1.17 christos p++; /* Skip nul */
269 1.17 christos goto check;
270 1.20 christos
271 1.17 christos case '\t':
272 1.17 christos case ' ':
273 1.1 cgd break;
274 1.17 christos
275 1.17 christos case '\n':
276 1.17 christos parsenleft = q - parsenextc;
277 1.17 christos more = 0; /* Stop processing here */
278 1.17 christos break;
279 1.17 christos
280 1.17 christos default:
281 1.5 jtc something = 1;
282 1.17 christos break;
283 1.17 christos }
284 1.17 christos
285 1.17 christos *q++ = *p++;
286 1.17 christos check:
287 1.17 christos if (--parselleft <= 0) {
288 1.17 christos parsenleft = q - parsenextc - 1;
289 1.17 christos if (parsenleft < 0)
290 1.17 christos goto again;
291 1.17 christos *q = '\0';
292 1.17 christos more = 0;
293 1.5 jtc }
294 1.1 cgd }
295 1.17 christos
296 1.17 christos savec = *q;
297 1.5 jtc *q = '\0';
298 1.5 jtc
299 1.25 christos #ifndef SMALL
300 1.5 jtc if (parsefile->fd == 0 && hist && something) {
301 1.28 christos HistEvent he;
302 1.5 jtc INTOFF;
303 1.31 christos history(hist, &he, whichprompt == 1? H_ENTER : H_APPEND,
304 1.31 christos parsenextc);
305 1.5 jtc INTON;
306 1.5 jtc }
307 1.19 christos #endif
308 1.17 christos
309 1.5 jtc if (vflag) {
310 1.17 christos out2str(parsenextc);
311 1.5 jtc flushout(out2);
312 1.5 jtc }
313 1.17 christos
314 1.17 christos *q = savec;
315 1.17 christos
316 1.1 cgd return *parsenextc++;
317 1.1 cgd }
318 1.1 cgd
319 1.1 cgd /*
320 1.1 cgd * Undo the last call to pgetc. Only one character may be pushed back.
321 1.1 cgd * PEOF may be pushed back.
322 1.1 cgd */
323 1.1 cgd
324 1.1 cgd void
325 1.37 christos pungetc(void)
326 1.37 christos {
327 1.1 cgd parsenleft++;
328 1.1 cgd parsenextc--;
329 1.1 cgd }
330 1.1 cgd
331 1.1 cgd /*
332 1.5 jtc * Push a string back onto the input at this current parsefile level.
333 1.5 jtc * We handle aliases this way.
334 1.1 cgd */
335 1.1 cgd void
336 1.37 christos pushstring(char *s, int len, void *ap)
337 1.37 christos {
338 1.5 jtc struct strpush *sp;
339 1.5 jtc
340 1.5 jtc INTOFF;
341 1.5 jtc /*dprintf("*** calling pushstring: %s, %d\n", s, len);*/
342 1.5 jtc if (parsefile->strpush) {
343 1.5 jtc sp = ckmalloc(sizeof (struct strpush));
344 1.5 jtc sp->prev = parsefile->strpush;
345 1.5 jtc parsefile->strpush = sp;
346 1.5 jtc } else
347 1.5 jtc sp = parsefile->strpush = &(parsefile->basestrpush);
348 1.5 jtc sp->prevstring = parsenextc;
349 1.5 jtc sp->prevnleft = parsenleft;
350 1.17 christos sp->prevlleft = parselleft;
351 1.5 jtc sp->ap = (struct alias *)ap;
352 1.5 jtc if (ap)
353 1.5 jtc ((struct alias *)ap)->flag |= ALIASINUSE;
354 1.5 jtc parsenextc = s;
355 1.5 jtc parsenleft = len;
356 1.5 jtc INTON;
357 1.1 cgd }
358 1.1 cgd
359 1.11 cgd void
360 1.37 christos popstring(void)
361 1.5 jtc {
362 1.5 jtc struct strpush *sp = parsefile->strpush;
363 1.1 cgd
364 1.5 jtc INTOFF;
365 1.5 jtc parsenextc = sp->prevstring;
366 1.5 jtc parsenleft = sp->prevnleft;
367 1.17 christos parselleft = sp->prevlleft;
368 1.5 jtc /*dprintf("*** calling popstring: restoring to '%s'\n", parsenextc);*/
369 1.5 jtc if (sp->ap)
370 1.5 jtc sp->ap->flag &= ~ALIASINUSE;
371 1.5 jtc parsefile->strpush = sp->prev;
372 1.5 jtc if (sp != &(parsefile->basestrpush))
373 1.5 jtc ckfree(sp);
374 1.5 jtc INTON;
375 1.5 jtc }
376 1.1 cgd
377 1.1 cgd /*
378 1.1 cgd * Set the input to take input from a file. If push is set, push the
379 1.1 cgd * old input onto the stack first.
380 1.1 cgd */
381 1.1 cgd
382 1.1 cgd void
383 1.37 christos setinputfile(const char *fname, int push)
384 1.11 cgd {
385 1.40 rillig unsigned char magic[4];
386 1.1 cgd int fd;
387 1.1 cgd int fd2;
388 1.1 cgd
389 1.1 cgd INTOFF;
390 1.1 cgd if ((fd = open(fname, O_RDONLY)) < 0)
391 1.1 cgd error("Can't open %s", fname);
392 1.40 rillig
393 1.40 rillig /* Since the message "Syntax error: "(" unexpected" is not very
394 1.40 rillig * helpful, we check if the file starts with the ELF magic to
395 1.40 rillig * avoid that message. The first lseek tries to make sure that
396 1.40 rillig * we can later rewind the file.
397 1.40 rillig */
398 1.40 rillig if (lseek(fd, 0, SEEK_SET) == 0) {
399 1.40 rillig if (read(fd, magic, 4) == 4) {
400 1.40 rillig if (memcmp(magic, "\177ELF", 4) == 0)
401 1.40 rillig error("Cannot execute ELF binary %s", fname);
402 1.40 rillig }
403 1.40 rillig if (lseek(fd, 0, SEEK_SET) != 0)
404 1.40 rillig error("Cannot rewind the file %s", fname);
405 1.40 rillig }
406 1.40 rillig
407 1.1 cgd if (fd < 10) {
408 1.1 cgd fd2 = copyfd(fd, 10);
409 1.1 cgd close(fd);
410 1.1 cgd if (fd2 < 0)
411 1.1 cgd error("Out of file descriptors");
412 1.1 cgd fd = fd2;
413 1.1 cgd }
414 1.1 cgd setinputfd(fd, push);
415 1.1 cgd INTON;
416 1.1 cgd }
417 1.1 cgd
418 1.1 cgd
419 1.1 cgd /*
420 1.1 cgd * Like setinputfile, but takes an open file descriptor. Call this with
421 1.1 cgd * interrupts off.
422 1.1 cgd */
423 1.1 cgd
424 1.1 cgd void
425 1.37 christos setinputfd(int fd, int push)
426 1.11 cgd {
427 1.23 mycroft (void) fcntl(fd, F_SETFD, FD_CLOEXEC);
428 1.1 cgd if (push) {
429 1.1 cgd pushfile();
430 1.1 cgd parsefile->buf = ckmalloc(BUFSIZ);
431 1.1 cgd }
432 1.1 cgd if (parsefile->fd > 0)
433 1.1 cgd close(parsefile->fd);
434 1.1 cgd parsefile->fd = fd;
435 1.1 cgd if (parsefile->buf == NULL)
436 1.1 cgd parsefile->buf = ckmalloc(BUFSIZ);
437 1.17 christos parselleft = parsenleft = 0;
438 1.1 cgd plinno = 1;
439 1.1 cgd }
440 1.1 cgd
441 1.1 cgd
442 1.1 cgd /*
443 1.1 cgd * Like setinputfile, but takes input from a string.
444 1.1 cgd */
445 1.1 cgd
446 1.1 cgd void
447 1.37 christos setinputstring(char *string, int push)
448 1.37 christos {
449 1.1 cgd INTOFF;
450 1.1 cgd if (push)
451 1.1 cgd pushfile();
452 1.1 cgd parsenextc = string;
453 1.17 christos parselleft = parsenleft = strlen(string);
454 1.1 cgd parsefile->buf = NULL;
455 1.1 cgd plinno = 1;
456 1.1 cgd INTON;
457 1.1 cgd }
458 1.1 cgd
459 1.1 cgd
460 1.1 cgd
461 1.1 cgd /*
462 1.1 cgd * To handle the "." command, a stack of input files is used. Pushfile
463 1.1 cgd * adds a new entry to the stack and popfile restores the previous level.
464 1.1 cgd */
465 1.1 cgd
466 1.1 cgd STATIC void
467 1.37 christos pushfile(void)
468 1.37 christos {
469 1.1 cgd struct parsefile *pf;
470 1.1 cgd
471 1.1 cgd parsefile->nleft = parsenleft;
472 1.17 christos parsefile->lleft = parselleft;
473 1.1 cgd parsefile->nextc = parsenextc;
474 1.1 cgd parsefile->linno = plinno;
475 1.1 cgd pf = (struct parsefile *)ckmalloc(sizeof (struct parsefile));
476 1.1 cgd pf->prev = parsefile;
477 1.1 cgd pf->fd = -1;
478 1.5 jtc pf->strpush = NULL;
479 1.5 jtc pf->basestrpush.prev = NULL;
480 1.1 cgd parsefile = pf;
481 1.1 cgd }
482 1.1 cgd
483 1.1 cgd
484 1.1 cgd void
485 1.37 christos popfile(void)
486 1.37 christos {
487 1.1 cgd struct parsefile *pf = parsefile;
488 1.1 cgd
489 1.1 cgd INTOFF;
490 1.1 cgd if (pf->fd >= 0)
491 1.1 cgd close(pf->fd);
492 1.1 cgd if (pf->buf)
493 1.1 cgd ckfree(pf->buf);
494 1.5 jtc while (pf->strpush)
495 1.5 jtc popstring();
496 1.1 cgd parsefile = pf->prev;
497 1.1 cgd ckfree(pf);
498 1.1 cgd parsenleft = parsefile->nleft;
499 1.17 christos parselleft = parsefile->lleft;
500 1.1 cgd parsenextc = parsefile->nextc;
501 1.1 cgd plinno = parsefile->linno;
502 1.1 cgd INTON;
503 1.1 cgd }
504 1.1 cgd
505 1.1 cgd
506 1.1 cgd /*
507 1.1 cgd * Return to top level.
508 1.1 cgd */
509 1.1 cgd
510 1.1 cgd void
511 1.37 christos popallfiles(void)
512 1.37 christos {
513 1.1 cgd while (parsefile != &basepf)
514 1.1 cgd popfile();
515 1.1 cgd }
516 1.1 cgd
517 1.1 cgd
518 1.1 cgd
519 1.1 cgd /*
520 1.1 cgd * Close the file(s) that the shell is reading commands from. Called
521 1.1 cgd * after a fork is done.
522 1.36 christos *
523 1.36 christos * Takes one arg, vfork, which tells it to not modify its global vars
524 1.36 christos * as it is still running in the parent.
525 1.38 dsl *
526 1.38 dsl * This code is (probably) unnecessary as the 'close on exec' flag is
527 1.38 dsl * set and should be enough. In the vfork case it is definitely wrong
528 1.38 dsl * to close the fds as another fork() may be done later to feed data
529 1.38 dsl * from a 'here' document into a pipe and we don't want to close the
530 1.38 dsl * pipe!
531 1.1 cgd */
532 1.1 cgd
533 1.1 cgd void
534 1.37 christos closescript(int vforked)
535 1.37 christos {
536 1.38 dsl if (vforked)
537 1.36 christos return;
538 1.1 cgd popallfiles();
539 1.1 cgd if (parsefile->fd > 0) {
540 1.1 cgd close(parsefile->fd);
541 1.1 cgd parsefile->fd = 0;
542 1.1 cgd }
543 1.1 cgd }
544