]>
Commit | Line | Data |
---|---|---|
71aad674 A |
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. | |
254f12f7 | 16 | * 3. Neither the name of the University nor the names of its contributors |
71aad674 A |
17 | * may be used to endorse or promote products derived from this software |
18 | * without specific prior written permission. | |
19 | * | |
20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
30 | * SUCH DAMAGE. | |
31 | */ | |
32 | ||
33 | #ifndef lint | |
34 | #if 0 | |
35 | static char sccsid[] = "@(#)input.c 8.3 (Berkeley) 6/9/95"; | |
36 | #endif | |
37 | #endif /* not lint */ | |
38 | #include <sys/cdefs.h> | |
254f12f7 | 39 | __FBSDID("$FreeBSD: head/bin/sh/input.c 314436 2017-02-28 23:42:47Z imp $"); |
71aad674 A |
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 | struct strpush { | |
68 | struct strpush *prev; /* preceding string on stack */ | |
69 | const char *prevstring; | |
70 | int prevnleft; | |
71 | int prevlleft; | |
72 | struct alias *ap; /* if push was associated with an alias */ | |
73 | }; | |
74 | ||
75 | /* | |
76 | * The parsefile structure pointed to by the global variable parsefile | |
77 | * contains information about the current file being read. | |
78 | */ | |
79 | ||
80 | struct parsefile { | |
81 | struct parsefile *prev; /* preceding file on stack */ | |
82 | int linno; /* current line */ | |
83 | int fd; /* file descriptor (or -1 if string) */ | |
84 | int nleft; /* number of chars left in this line */ | |
85 | int lleft; /* number of lines left in this buffer */ | |
86 | const char *nextc; /* next char in buffer */ | |
87 | char *buf; /* input buffer */ | |
88 | struct strpush *strpush; /* for pushing strings at this level */ | |
89 | struct strpush basestrpush; /* so pushing one is fast */ | |
90 | }; | |
91 | ||
92 | ||
93 | int plinno = 1; /* input line number */ | |
94 | int parsenleft; /* copy of parsefile->nleft */ | |
95 | static int parselleft; /* copy of parsefile->lleft */ | |
96 | const char *parsenextc; /* copy of parsefile->nextc */ | |
97 | static char basebuf[BUFSIZ + 1];/* buffer for top level input file */ | |
98 | static struct parsefile basepf = { /* top level input file */ | |
99 | .nextc = basebuf, | |
100 | .buf = basebuf | |
101 | }; | |
102 | static struct parsefile *parsefile = &basepf; /* current input file */ | |
103 | int whichprompt; /* 1 == PS1, 2 == PS2 */ | |
104 | ||
105 | #ifndef __APPLE__ | |
106 | EditLine *el; /* cookie for editline package */ | |
107 | #endif /* !__APPLE__ */ | |
108 | ||
109 | static void pushfile(void); | |
110 | static int preadfd(void); | |
111 | static void popstring(void); | |
112 | ||
deb63bfb A |
113 | #ifdef __APPLE__ |
114 | // Remove when 28988691 is fixed. Added _ to avoid conflict with future libc definition. | |
115 | static char * | |
116 | strchrnul_(const char *p, int ch) | |
117 | { | |
118 | char c; | |
119 | ||
120 | c = ch; | |
121 | for (;; ++p) { | |
122 | if (*p == c || *p == '\0') | |
123 | return ((char *)p); | |
124 | } | |
125 | /* NOTREACHED */ | |
126 | } | |
127 | #endif /* __APPLE__ */ | |
128 | ||
71aad674 A |
129 | void |
130 | resetinput(void) | |
131 | { | |
132 | popallfiles(); | |
133 | parselleft = parsenleft = 0; /* clear input buffer */ | |
134 | } | |
135 | ||
136 | ||
137 | ||
138 | /* | |
139 | * Read a character from the script, returning PEOF on end of file. | |
140 | * Nul characters in the input are silently discarded. | |
141 | */ | |
142 | ||
143 | int | |
144 | pgetc(void) | |
145 | { | |
146 | return pgetc_macro(); | |
147 | } | |
148 | ||
149 | ||
150 | static int | |
151 | preadfd(void) | |
152 | { | |
153 | int nr; | |
154 | parsenextc = parsefile->buf; | |
155 | ||
156 | retry: | |
157 | #ifndef NO_HISTORY | |
158 | if (parsefile->fd == 0 && el) { | |
159 | static const char *rl_cp; | |
160 | static int el_len; | |
161 | ||
162 | if (rl_cp == NULL) { | |
163 | el_resize(el); | |
164 | rl_cp = el_gets(el, &el_len); | |
165 | } | |
166 | if (rl_cp == NULL) | |
167 | nr = el_len == 0 ? 0 : -1; | |
168 | else { | |
169 | nr = el_len; | |
170 | if (nr > BUFSIZ) | |
171 | nr = BUFSIZ; | |
172 | memcpy(parsefile->buf, rl_cp, nr); | |
173 | if (nr != el_len) { | |
174 | el_len -= nr; | |
175 | rl_cp += nr; | |
176 | } else | |
177 | rl_cp = NULL; | |
178 | } | |
179 | } else | |
180 | #endif | |
181 | nr = read(parsefile->fd, parsefile->buf, BUFSIZ); | |
182 | ||
183 | if (nr <= 0) { | |
184 | if (nr < 0) { | |
185 | if (errno == EINTR) | |
186 | goto retry; | |
187 | if (parsefile->fd == 0 && errno == EWOULDBLOCK) { | |
188 | int flags = fcntl(0, F_GETFL, 0); | |
189 | if (flags >= 0 && flags & O_NONBLOCK) { | |
190 | flags &=~ O_NONBLOCK; | |
191 | if (fcntl(0, F_SETFL, flags) >= 0) { | |
192 | out2fmt_flush("sh: turning off NDELAY mode\n"); | |
193 | goto retry; | |
194 | } | |
195 | } | |
196 | } | |
197 | } | |
198 | nr = -1; | |
199 | } | |
200 | return nr; | |
201 | } | |
202 | ||
203 | /* | |
204 | * Refill the input buffer and return the next input character: | |
205 | * | |
206 | * 1) If a string was pushed back on the input, pop it; | |
207 | * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading | |
208 | * from a string so we can't refill the buffer, return EOF. | |
209 | * 3) If there is more in this buffer, use it else call read to fill it. | |
210 | * 4) Process input up to the next newline, deleting nul characters. | |
211 | */ | |
212 | ||
213 | int | |
214 | preadbuffer(void) | |
215 | { | |
deb63bfb | 216 | char *p, *q, *r, *end; |
71aad674 A |
217 | char savec; |
218 | ||
219 | while (parsefile->strpush) { | |
220 | /* | |
221 | * Add a space to the end of an alias to ensure that the | |
222 | * alias remains in use while parsing its last word. | |
223 | * This avoids alias recursions. | |
224 | */ | |
225 | if (parsenleft == -1 && parsefile->strpush->ap != NULL) | |
226 | return ' '; | |
227 | popstring(); | |
228 | if (--parsenleft >= 0) | |
229 | return (*parsenextc++); | |
230 | } | |
231 | if (parsenleft == EOF_NLEFT || parsefile->buf == NULL) | |
232 | return PEOF; | |
71aad674 A |
233 | |
234 | again: | |
235 | if (parselleft <= 0) { | |
236 | if ((parselleft = preadfd()) == -1) { | |
237 | parselleft = parsenleft = EOF_NLEFT; | |
238 | return PEOF; | |
239 | } | |
240 | } | |
241 | ||
deb63bfb A |
242 | p = parsefile->buf + (parsenextc - parsefile->buf); |
243 | end = p + parselleft; | |
244 | *end = '\0'; | |
245 | q = strchrnul_(p, '\n'); | |
246 | if (q != end && *q == '\0') { | |
247 | /* delete nul characters */ | |
248 | for (r = q; q != end; q++) { | |
249 | if (*q != '\0') | |
250 | *r++ = *q; | |
71aad674 | 251 | } |
deb63bfb A |
252 | parselleft -= end - r; |
253 | if (parselleft == 0) | |
254 | goto again; | |
255 | end = p + parselleft; | |
256 | *end = '\0'; | |
257 | q = strchrnul_(p, '\n'); | |
258 | } | |
259 | if (q == end) { | |
260 | parsenleft = parselleft; | |
261 | parselleft = 0; | |
262 | } else /* *q == '\n' */ { | |
263 | q++; | |
264 | parsenleft = q - parsenextc; | |
265 | parselleft -= parsenleft; | |
71aad674 | 266 | } |
deb63bfb | 267 | parsenleft--; |
71aad674 A |
268 | |
269 | savec = *q; | |
270 | *q = '\0'; | |
271 | ||
272 | #ifndef NO_HISTORY | |
273 | if (parsefile->fd == 0 && hist && | |
274 | parsenextc[strspn(parsenextc, " \t\n")] != '\0') { | |
275 | HistEvent he; | |
276 | INTOFF; | |
277 | history(hist, &he, whichprompt == 1 ? H_ENTER : H_ADD, | |
278 | parsenextc); | |
279 | INTON; | |
280 | } | |
281 | #endif | |
282 | ||
283 | if (vflag) { | |
284 | out2str(parsenextc); | |
285 | flushout(out2); | |
286 | } | |
287 | ||
288 | *q = savec; | |
289 | ||
290 | return *parsenextc++; | |
291 | } | |
292 | ||
293 | /* | |
294 | * Returns if we are certain we are at EOF. Does not cause any more input | |
295 | * to be read from the outside world. | |
296 | */ | |
297 | ||
298 | int | |
299 | preadateof(void) | |
300 | { | |
301 | if (parsenleft > 0) | |
302 | return 0; | |
303 | if (parsefile->strpush) | |
304 | return 0; | |
305 | if (parsenleft == EOF_NLEFT || parsefile->buf == NULL) | |
306 | return 1; | |
307 | return 0; | |
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(void) | |
317 | { | |
318 | parsenleft++; | |
319 | parsenextc--; | |
320 | } | |
321 | ||
322 | /* | |
323 | * Push a string back onto the input at this current parsefile level. | |
324 | * We handle aliases this way. | |
325 | */ | |
326 | void | |
327 | pushstring(const char *s, int len, struct alias *ap) | |
328 | { | |
329 | struct strpush *sp; | |
330 | ||
331 | INTOFF; | |
332 | /*out2fmt_flush("*** calling pushstring: %s, %d\n", s, len);*/ | |
333 | if (parsefile->strpush) { | |
334 | sp = ckmalloc(sizeof (struct strpush)); | |
335 | sp->prev = parsefile->strpush; | |
336 | parsefile->strpush = sp; | |
337 | } else | |
338 | sp = parsefile->strpush = &(parsefile->basestrpush); | |
339 | sp->prevstring = parsenextc; | |
340 | sp->prevnleft = parsenleft; | |
341 | sp->prevlleft = parselleft; | |
342 | sp->ap = ap; | |
343 | if (ap) | |
344 | ap->flag |= ALIASINUSE; | |
345 | parsenextc = s; | |
346 | parsenleft = len; | |
347 | INTON; | |
348 | } | |
349 | ||
350 | static void | |
351 | popstring(void) | |
352 | { | |
353 | struct strpush *sp = parsefile->strpush; | |
354 | ||
355 | INTOFF; | |
356 | if (sp->ap) { | |
357 | if (parsenextc != sp->ap->val && | |
358 | (parsenextc[-1] == ' ' || parsenextc[-1] == '\t')) | |
359 | forcealias(); | |
360 | sp->ap->flag &= ~ALIASINUSE; | |
361 | } | |
362 | parsenextc = sp->prevstring; | |
363 | parsenleft = sp->prevnleft; | |
364 | parselleft = sp->prevlleft; | |
365 | /*out2fmt_flush("*** calling popstring: restoring to '%s'\n", parsenextc);*/ | |
366 | parsefile->strpush = sp->prev; | |
367 | if (sp != &(parsefile->basestrpush)) | |
368 | ckfree(sp); | |
369 | INTON; | |
370 | } | |
371 | ||
372 | /* | |
373 | * Set the input to take input from a file. If push is set, push the | |
374 | * old input onto the stack first. | |
375 | */ | |
376 | ||
377 | void | |
378 | setinputfile(const char *fname, int push) | |
379 | { | |
380 | int fd; | |
381 | int fd2; | |
382 | ||
383 | INTOFF; | |
384 | if ((fd = open(fname, O_RDONLY | O_CLOEXEC)) < 0) | |
385 | error("cannot open %s: %s", fname, strerror(errno)); | |
386 | if (fd < 10) { | |
387 | fd2 = fcntl(fd, F_DUPFD_CLOEXEC, 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 (which should have | |
400 | * its FD_CLOEXEC flag already set). Call this with interrupts off. | |
401 | */ | |
402 | ||
403 | void | |
404 | setinputfd(int fd, int push) | |
405 | { | |
406 | if (push) { | |
407 | pushfile(); | |
408 | parsefile->buf = ckmalloc(BUFSIZ + 1); | |
409 | } | |
410 | if (parsefile->fd > 0) | |
411 | close(parsefile->fd); | |
412 | parsefile->fd = fd; | |
413 | if (parsefile->buf == NULL) | |
414 | parsefile->buf = ckmalloc(BUFSIZ + 1); | |
415 | parselleft = parsenleft = 0; | |
416 | plinno = 1; | |
417 | } | |
418 | ||
419 | ||
420 | /* | |
421 | * Like setinputfile, but takes input from a string. | |
422 | */ | |
423 | ||
424 | void | |
425 | setinputstring(const char *string, 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 | ||
444 | static void | |
445 | pushfile(void) | |
446 | { | |
447 | struct parsefile *pf; | |
448 | ||
449 | parsefile->nleft = parsenleft; | |
450 | parsefile->lleft = parselleft; | |
451 | parsefile->nextc = parsenextc; | |
452 | parsefile->linno = plinno; | |
453 | pf = (struct parsefile *)ckmalloc(sizeof (struct parsefile)); | |
454 | pf->prev = parsefile; | |
455 | pf->fd = -1; | |
456 | pf->strpush = NULL; | |
457 | pf->basestrpush.prev = NULL; | |
458 | parsefile = pf; | |
459 | } | |
460 | ||
461 | ||
462 | void | |
463 | popfile(void) | |
464 | { | |
465 | struct parsefile *pf = parsefile; | |
466 | ||
467 | INTOFF; | |
468 | if (pf->fd >= 0) | |
469 | close(pf->fd); | |
470 | if (pf->buf) | |
471 | ckfree(pf->buf); | |
472 | while (pf->strpush) | |
473 | popstring(); | |
474 | parsefile = pf->prev; | |
475 | ckfree(pf); | |
476 | parsenleft = parsefile->nleft; | |
477 | parselleft = parsefile->lleft; | |
478 | parsenextc = parsefile->nextc; | |
479 | plinno = parsefile->linno; | |
480 | INTON; | |
481 | } | |
482 | ||
483 | ||
484 | /* | |
485 | * Return current file (to go back to it later using popfilesupto()). | |
486 | */ | |
487 | ||
488 | struct parsefile * | |
489 | getcurrentfile(void) | |
490 | { | |
491 | return parsefile; | |
492 | } | |
493 | ||
494 | ||
495 | /* | |
496 | * Pop files until the given file is on top again. Useful for regular | |
497 | * builtins that read shell commands from files or strings. | |
498 | * If the given file is not an active file, an error is raised. | |
499 | */ | |
500 | ||
501 | void | |
502 | popfilesupto(struct parsefile *file) | |
503 | { | |
504 | while (parsefile != file && parsefile != &basepf) | |
505 | popfile(); | |
506 | if (parsefile != file) | |
507 | error("popfilesupto() misused"); | |
508 | } | |
509 | ||
510 | /* | |
511 | * Return to top level. | |
512 | */ | |
513 | ||
514 | void | |
515 | popallfiles(void) | |
516 | { | |
517 | while (parsefile != &basepf) | |
518 | popfile(); | |
519 | } | |
520 | ||
521 | ||
522 | ||
523 | /* | |
524 | * Close the file(s) that the shell is reading commands from. Called | |
525 | * after a fork is done. | |
526 | */ | |
527 | ||
528 | void | |
529 | closescript(void) | |
530 | { | |
531 | popallfiles(); | |
532 | if (parsefile->fd > 0) { | |
533 | close(parsefile->fd); | |
534 | parsefile->fd = 0; | |
535 | } | |
536 | } |