unstack - fix ipcvecs
[minix.git] / commands / ash / input.c
blobfb234d2a65d92827e5050fd3c0553456fa199eb4
1 /*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
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.
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>
40 __FBSDID("$FreeBSD: src/bin/sh/input.c,v 1.22 2004/04/06 20:06:51 markm Exp $");
43 #include <sys/types.h>
44 #include <stdio.h> /* defines BUFSIZ */
45 #include <fcntl.h>
46 #include <errno.h>
47 #include <unistd.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <sys/stat.h>
53 * This file implements the input routines used by the parser.
56 #include "shell.h"
57 #include "redir.h"
58 #include "syntax.h"
59 #include "input.h"
60 #include "output.h"
61 #include "options.h"
62 #include "memalloc.h"
63 #include "error.h"
64 #include "alias.h"
65 #include "parser.h"
66 #ifdef EDITLINE
67 #ifdef __minix_vmd
68 #include <readline/readline.h>
69 #else
70 /* What about other systems? */
71 char *readline(char *prompt);
72 #endif
73 #else
74 #include "myhistedit.h"
75 #endif
76 #include "redir.h"
77 #include "trap.h"
79 static void popstring(void);
81 #define EOF_NLEFT -99 /* value of parsenleft when EOF pushed back */
83 MKINIT
84 struct strpush {
85 struct strpush *prev; /* preceding string on stack */
86 char *prevstring;
87 int prevnleft;
88 int prevlleft;
89 struct alias *ap; /* if push was associated with an alias */
93 * The parsefile structure pointed to by the global variable parsefile
94 * contains information about the current file being read.
97 MKINIT
98 struct parsefile {
99 struct parsefile *prev; /* preceding file on stack */
100 int linno; /* current line */
101 int fd; /* file descriptor (or -1 if string) */
102 int nleft; /* number of chars left in this line */
103 int lleft; /* number of lines left in this buffer */
104 char *nextc; /* next char in buffer */
105 char *buf; /* input buffer */
106 struct strpush *strpush; /* for pushing strings at this level */
107 struct strpush basestrpush; /* so pushing one is fast */
111 int plinno = 1; /* input line number */
112 MKINIT int parsenleft; /* copy of parsefile->nleft */
113 MKINIT int parselleft; /* copy of parsefile->lleft */
114 char *parsenextc; /* copy of parsefile->nextc */
115 MKINIT struct parsefile basepf; /* top level input file */
116 char basebuf[BUFSIZ]; /* buffer for top level input file */
117 STATIC struct parsefile *parsefile = &basepf; /* current input file */
118 int init_editline = 0; /* editline library initialized? */
119 int whichprompt; /* -1 == PSE, 1 == PS1, 2 == PS2 */
121 #ifndef EDITLINE
122 EditLine *el; /* cookie for editline package */
123 #endif
125 STATIC void pushfile(void);
126 static int preadfd(void);
128 #ifdef mkinit
129 INCLUDE "input.h"
130 INCLUDE "error.h"
132 INIT {
133 extern char basebuf[];
135 basepf.nextc = basepf.buf = basebuf;
138 RESET {
139 if (exception != EXSHELLPROC)
140 parselleft = parsenleft = 0; /* clear input buffer */
141 popallfiles();
144 SHELLPROC {
145 popallfiles();
147 #endif
151 * Read a line from the script.
154 char *
155 pfgets(char *line, int len)
157 char *p = line;
158 int nleft = len;
159 int c;
161 while (--nleft > 0) {
162 c = pgetc_macro();
163 if (c == PEOF) {
164 if (p == line)
165 return NULL;
166 break;
168 *p++ = c;
169 if (c == '\n')
170 break;
172 *p = '\0';
173 return line;
179 * Read a character from the script, returning PEOF on end of file.
180 * Nul characters in the input are silently discarded.
184 pgetc(void)
186 return pgetc_macro();
190 static int
191 preadfd(void)
193 int nr;
194 parsenextc = parsefile->buf;
196 #if !defined(NO_HISTORY) && !defined(EDITLINE)
197 if (el != NULL && gotwinch) {
198 gotwinch = 0;
199 el_resize(el);
201 #endif
202 retry:
203 #ifndef NO_HISTORY
204 #ifdef EDITLINE
205 if (parsefile->fd == 0 && editable) {
206 static const char *rl_cp= NULL;
207 static size_t rl_off= 0;
209 if (!rl_cp)
211 rl_cp = readline(getprompt(NULL));
212 if (rl_cp == NULL)
213 nr = 0;
215 if (rl_cp)
217 nr= strlen(rl_cp+rl_off);
218 if (nr >= BUFSIZ-1)
220 nr= BUFSIZ-1;
221 (void) memcpy(parsenextc, rl_cp+rl_off, nr);
222 rl_off += nr;
224 else
226 (void) memcpy(parsenextc, rl_cp+rl_off, nr);
227 parsenextc[nr++]= '\n';
228 free((void *)rl_cp);
229 rl_cp= NULL;
230 rl_off= 0;
233 } else
234 #else /* !EDITLINE */
235 if (parsefile->fd == 0 && el) {
236 const char *rl_cp;
238 rl_cp = el_gets(el, &nr);
239 if (rl_cp == NULL)
240 nr = 0;
241 else {
242 /* XXX - BUFSIZE should redesign so not necessary */
243 (void) strcpy(parsenextc, rl_cp);
245 } else
246 #endif /* !EDITLINE */
247 #endif
248 nr = read(parsefile->fd, parsenextc, BUFSIZ - 1);
250 if (nr <= 0) {
251 if (nr < 0) {
252 if (errno == EINTR)
253 goto retry;
254 #ifdef EWOULDBLOCK
255 if (parsefile->fd == 0 && errno == EWOULDBLOCK) {
256 int flags = fcntl(0, F_GETFL, 0);
257 if (flags >= 0 && flags & O_NONBLOCK) {
258 flags &=~ O_NONBLOCK;
259 if (fcntl(0, F_SETFL, flags) >= 0) {
260 out2str("sh: turning off NDELAY mode\n");
261 goto retry;
265 #endif /* EWOULDBLOCK */
267 nr = -1;
269 return nr;
273 * Refill the input buffer and return the next input character:
275 * 1) If a string was pushed back on the input, pop it;
276 * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading
277 * from a string so we can't refill the buffer, return EOF.
278 * 3) If there is more in this buffer, use it else call read to fill it.
279 * 4) Process input up to the next newline, deleting nul characters.
283 preadbuffer(void)
285 char *p, *q;
286 int more;
287 int something;
288 char savec;
290 if (parsefile->strpush) {
291 popstring();
292 if (--parsenleft >= 0)
293 return (*parsenextc++);
295 if (parsenleft == EOF_NLEFT || parsefile->buf == NULL)
296 return PEOF;
297 flushout(&output);
298 flushout(&errout);
300 again:
301 if (parselleft <= 0) {
302 if ((parselleft = preadfd()) == -1) {
303 parselleft = parsenleft = EOF_NLEFT;
304 return PEOF;
308 q = p = parsenextc;
310 /* delete nul characters */
311 something = 0;
312 for (more = 1; more;) {
313 switch (*p) {
314 case '\0':
315 p++; /* Skip nul */
316 goto check;
318 case '\t':
319 case ' ':
320 break;
322 case '\n':
323 parsenleft = q - parsenextc;
324 more = 0; /* Stop processing here */
325 break;
327 default:
328 something = 1;
329 break;
332 *q++ = *p++;
333 check:
334 if (--parselleft <= 0) {
335 parsenleft = q - parsenextc - 1;
336 if (parsenleft < 0)
337 goto again;
338 *q = '\0';
339 more = 0;
343 savec = *q;
344 *q = '\0';
346 #if !defined(NO_HISTORY) && !defined(EDITLINE)
347 if (parsefile->fd == 0 && hist && something) {
348 HistEvent he;
349 INTOFF;
350 history(hist, &he, whichprompt == 1 ? H_ENTER : H_ADD,
351 parsenextc);
352 INTON;
354 #endif
356 if (vflag) {
357 out2str(parsenextc);
358 flushout(out2);
361 *q = savec;
363 return *parsenextc++;
367 * Undo the last call to pgetc. Only one character may be pushed back.
368 * PEOF may be pushed back.
371 void
372 pungetc(void)
374 parsenleft++;
375 parsenextc--;
379 * Push a string back onto the input at this current parsefile level.
380 * We handle aliases this way.
382 void
383 pushstring(char *s, int len, void *ap)
385 struct strpush *sp;
387 INTOFF;
388 /*dbgprintf("*** calling pushstring: %s, %d\n", s, len);*/
389 if (parsefile->strpush) {
390 sp = ckmalloc(sizeof (struct strpush));
391 sp->prev = parsefile->strpush;
392 parsefile->strpush = sp;
393 } else
394 sp = parsefile->strpush = &(parsefile->basestrpush);
395 sp->prevstring = parsenextc;
396 sp->prevnleft = parsenleft;
397 sp->prevlleft = parselleft;
398 sp->ap = (struct alias *)ap;
399 if (ap)
400 ((struct alias *)ap)->flag |= ALIASINUSE;
401 parsenextc = s;
402 parsenleft = len;
403 INTON;
406 static void
407 popstring(void)
409 struct strpush *sp = parsefile->strpush;
411 INTOFF;
412 parsenextc = sp->prevstring;
413 parsenleft = sp->prevnleft;
414 parselleft = sp->prevlleft;
415 /*dbgprintf("*** calling popstring: restoring to '%s'\n", parsenextc);*/
416 if (sp->ap)
417 sp->ap->flag &= ~ALIASINUSE;
418 parsefile->strpush = sp->prev;
419 if (sp != &(parsefile->basestrpush))
420 ckfree(sp);
421 INTON;
425 * Set the input to take input from a file. If push is set, push the
426 * old input onto the stack first.
429 void
430 setinputfile(char *fname, int push)
432 int fd;
433 int fd2;
434 struct stat statbuf;
435 int saved_errno;
437 INTOFF;
438 if ((fd = open(fname, O_RDONLY)) < 0)
439 error("Can't open %s: %s", fname, strerror(errno));
440 if (fstat(fd, &statbuf) < 0) {
441 saved_errno = errno;
442 close(fd);
443 error("Can't stat %s: %s", fname, strerror(saved_errno));
445 if (!S_ISREG(statbuf.st_mode)) {
446 close(fd);
447 error("Can't open %s: %s", fname, strerror(ENOEXEC));
449 if (fd < 10) {
450 fd2 = fcntl(fd, F_DUPFD, 10);
451 close(fd);
452 if (fd2 < 0)
453 error("Out of file descriptors");
454 fd = fd2;
456 setinputfd(fd, push);
457 INTON;
462 * Like setinputfile, but takes an open file descriptor. Call this with
463 * interrupts off.
466 void
467 setinputfd(int fd, int push)
469 (void)fcntl(fd, F_SETFD, FD_CLOEXEC);
470 if (push) {
471 pushfile();
472 parsefile->buf = ckmalloc(BUFSIZ);
474 if (parsefile->fd > 0)
475 close(parsefile->fd);
476 parsefile->fd = fd;
477 if (parsefile->buf == NULL)
478 parsefile->buf = ckmalloc(BUFSIZ);
479 parselleft = parsenleft = 0;
480 plinno = 1;
485 * Like setinputfile, but takes input from a string.
488 void
489 setinputstring(char *string, int push)
491 INTOFF;
492 if (push)
493 pushfile();
494 parsenextc = string;
495 parselleft = parsenleft = strlen(string);
496 parsefile->buf = NULL;
497 plinno = 1;
498 INTON;
504 * To handle the "." command, a stack of input files is used. Pushfile
505 * adds a new entry to the stack and popfile restores the previous level.
508 STATIC void
509 pushfile(void)
511 struct parsefile *pf;
513 parsefile->nleft = parsenleft;
514 parsefile->lleft = parselleft;
515 parsefile->nextc = parsenextc;
516 parsefile->linno = plinno;
517 pf = (struct parsefile *)ckmalloc(sizeof (struct parsefile));
518 pf->prev = parsefile;
519 pf->fd = -1;
520 pf->strpush = NULL;
521 pf->basestrpush.prev = NULL;
522 parsefile = pf;
526 void
527 popfile(void)
529 struct parsefile *pf = parsefile;
531 INTOFF;
532 if (pf->fd >= 0)
533 close(pf->fd);
534 if (pf->buf)
535 ckfree(pf->buf);
536 while (pf->strpush)
537 popstring();
538 parsefile = pf->prev;
539 ckfree(pf);
540 parsenleft = parsefile->nleft;
541 parselleft = parsefile->lleft;
542 parsenextc = parsefile->nextc;
543 plinno = parsefile->linno;
544 INTON;
549 * Return to top level.
552 void
553 popallfiles(void)
555 while (parsefile != &basepf)
556 popfile();
562 * Close the file(s) that the shell is reading commands from. Called
563 * after a fork is done.
566 void
567 closescript(void)
569 popallfiles();
570 if (parsefile->fd > 0) {
571 close(parsefile->fd);
572 parsefile->fd = 0;
577 * $PchId: input.c,v 1.7 2006/05/29 13:09:38 philip Exp $