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