VirtualBox

source: kBuild/trunk/src/kash/input.c@ 1240

Last change on this file since 1240 was 1233, checked in by bird, 17 years ago

keywords.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
File size: 12.2 KB
Line 
1/* $NetBSD: input.c,v 1.39 2003/08/07 09:05:32 agc 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#if 0
36#ifndef lint
37static char sccsid[] = "@(#)input.c 8.3 (Berkeley) 6/9/95";
38#else
39__RCSID("$NetBSD: input.c,v 1.39 2003/08/07 09:05:32 agc Exp $");
40#endif /* not lint */
41#endif
42
43#include <stdio.h> /* defines BUFSIZ */
44#include <errno.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 "shinstance.h"
64
65#define EOF_NLEFT -99 /* value of parsenleft when EOF pushed back */
66
67//MKINIT
68//struct strpush {
69// struct strpush *prev; /* preceding string on stack */
70// char *prevstring;
71// int prevnleft;
72// int prevlleft;
73// struct alias *ap; /* if push was associated with an alias */
74//};
75//
76///*
77// * The parsefile structure pointed to by the global variable parsefile
78// * contains information about the current file being read.
79// */
80//
81//MKINIT
82//struct parsefile {
83// struct parsefile *prev; /* preceding file on stack */
84// int linno; /* current line */
85// int fd; /* file descriptor (or -1 if string) */
86// int nleft; /* number of chars left in this line */
87// int lleft; /* number of chars left in this buffer */
88// char *nextc; /* next char in buffer */
89// char *buf; /* input buffer */
90// struct strpush *strpush; /* for pushing strings at this level */
91// struct strpush basestrpush; /* so pushing one is fast */
92//};
93//
94//
95//int plinno = 1; /* input line number */
96//int parsenleft; /* copy of parsefile->nleft */
97//MKINIT int parselleft; /* copy of parsefile->lleft */
98//char *parsenextc; /* copy of parsefile->nextc */
99//MKINIT struct parsefile basepf; /* top level input file */
100//MKINIT char basebuf[BUFSIZ]; /* buffer for top level input file */
101//struct parsefile *parsefile = &basepf; /* current input file */
102//int init_editline = 0; /* editline library initialized? */
103//int whichprompt; /* 1 == PS1, 2 == PS2 */
104//
105//#ifndef SMALL
106//EditLine *el; /* cookie for editline package */
107//#endif
108
109STATIC void pushfile(shinstance *psh);
110static int preadfd(shinstance *psh);
111
112#ifdef mkinit
113INCLUDE <stdio.h>
114INCLUDE "input.h"
115INCLUDE "error.h"
116
117INIT {
118 psh->basepf.nextc = psh->basepf.buf = psh->basebuf;
119}
120
121RESET {
122 if (psh->exception != EXSHELLPROC)
123 psh->parselleft = psh->parsenleft = 0; /* clear input buffer */
124 popallfiles(psh);
125}
126
127SHELLPROC {
128 popallfiles(psh);
129}
130#endif
131
132
133/*
134 * Read a line from the script.
135 */
136
137char *
138pfgets(shinstance *psh, char *line, int len)
139{
140 char *p = line;
141 int nleft = len;
142 int c;
143
144 while (--nleft > 0) {
145 c = pgetc_macro(psh);
146 if (c == PEOF) {
147 if (p == line)
148 return NULL;
149 break;
150 }
151 *p++ = c;
152 if (c == '\n')
153 break;
154 }
155 *p = '\0';
156 return line;
157}
158
159
160
161/*
162 * Read a character from the script, returning PEOF on end of file.
163 * Nul characters in the input are silently discarded.
164 */
165
166int
167pgetc(shinstance *psh)
168{
169 return pgetc_macro(psh);
170}
171
172
173static int
174preadfd(shinstance *psh)
175{
176 int nr;
177 char *buf = psh->parsefile->buf;
178 psh->parsenextc = buf;
179
180retry:
181#ifndef SMALL
182 if (psh->parsefile->fd == 0 && psh->el) {
183 static const char *rl_cp;
184 static int el_len;
185
186 if (rl_cp == NULL)
187 rl_cp = el_gets(psh->el, &el_len);
188 if (rl_cp == NULL)
189 nr = 0;
190 else {
191 nr = el_len;
192 if (nr > BUFSIZ - 8)
193 nr = BUFSIZ - 8;
194 memcpy(buf, rl_cp, nr);
195 if (nr != el_len) {
196 el_len -= nr;
197 rl_cp += nr;
198 } else
199 rl_cp = 0;
200 }
201
202 } else
203#endif
204 nr = shfile_read(&psh->fdtab, psh->parsefile->fd, buf, BUFSIZ - 8);
205
206
207 if (nr <= 0) {
208 if (nr < 0) {
209 if (errno == EINTR)
210 goto retry;
211 if (psh->parsefile->fd == 0 && errno == EWOULDBLOCK) {
212 int flags = shfile_fcntl(&psh->fdtab, 0, F_GETFL, 0);
213 if (flags >= 0 && flags & O_NONBLOCK) {
214 flags &=~ O_NONBLOCK;
215 if (shfile_fcntl(&psh->fdtab, 0, F_SETFL, flags) >= 0) {
216 out2str(psh, "sh: turning off NDELAY mode\n");
217 goto retry;
218 }
219 }
220 }
221 }
222 nr = -1;
223 }
224 return nr;
225}
226
227/*
228 * Refill the input buffer and return the next input character:
229 *
230 * 1) If a string was pushed back on the input, pop it;
231 * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading
232 * from a string so we can't refill the buffer, return EOF.
233 * 3) If the is more stuff in this buffer, use it else call read to fill it.
234 * 4) Process input up to the next newline, deleting nul characters.
235 */
236
237int
238preadbuffer(shinstance *psh)
239{
240 char *p, *q;
241 int more;
242 int something;
243 char savec;
244
245 if (psh->parsefile->strpush) {
246 popstring(psh);
247 if (--psh->parsenleft >= 0)
248 return (*psh->parsenextc++);
249 }
250 if (psh->parsenleft == EOF_NLEFT || psh->parsefile->buf == NULL)
251 return PEOF;
252 flushout(&psh->output);
253 flushout(&psh->errout);
254
255again:
256 if (psh->parselleft <= 0) {
257 if ((psh->parselleft = preadfd(psh)) == -1) {
258 psh->parselleft = psh->parsenleft = EOF_NLEFT;
259 return PEOF;
260 }
261 }
262
263 q = p = psh->parsenextc;
264
265 /* delete nul characters */
266 something = 0;
267 for (more = 1; more;) {
268 switch (*p) {
269 case '\0':
270 p++; /* Skip nul */
271 goto check;
272
273 case '\t':
274 case ' ':
275 break;
276
277 case '\n':
278 psh->parsenleft = (int)(q - psh->parsenextc);
279 more = 0; /* Stop processing here */
280 break;
281
282 default:
283 something = 1;
284 break;
285 }
286
287 *q++ = *p++;
288check:
289 if (--psh->parselleft <= 0) {
290 psh->parsenleft = (int)(q - psh->parsenextc - 1);
291 if (psh->parsenleft < 0)
292 goto again;
293 *q = '\0';
294 more = 0;
295 }
296 }
297
298 savec = *q;
299 *q = '\0';
300
301#ifndef SMALL
302 if (psh->parsefile->fd == 0 && hist && something) {
303 HistEvent he;
304 INTOFF;
305 history(hist, &he, psh->whichprompt == 1? H_ENTER : H_APPEND,
306 psh->parsenextc);
307 INTON;
308 }
309#endif
310
311 if (vflag(psh)) {
312 out2str(psh, psh->parsenextc);
313 flushout(psh->out2);
314 }
315
316 *q = savec;
317
318 return *psh->parsenextc++;
319}
320
321/*
322 * Undo the last call to pgetc. Only one character may be pushed back.
323 * PEOF may be pushed back.
324 */
325
326void
327pungetc(shinstance *psh)
328{
329 psh->parsenleft++;
330 psh->parsenextc--;
331}
332
333/*
334 * Push a string back onto the input at this current parsefile level.
335 * We handle aliases this way.
336 */
337void
338pushstring(shinstance *psh, char *s, size_t len, void *ap)
339{
340 struct strpush *sp;
341
342 INTOFF;
343/*dprintf("*** calling pushstring: %s, %d\n", s, len);*/
344 if (psh->parsefile->strpush) {
345 sp = ckmalloc(sizeof (struct strpush));
346 sp->prev = psh->parsefile->strpush;
347 psh->parsefile->strpush = sp;
348 } else
349 sp = psh->parsefile->strpush = &(psh->parsefile->basestrpush);
350 sp->prevstring = psh->parsenextc;
351 sp->prevnleft = psh->parsenleft;
352 sp->prevlleft = psh->parselleft;
353 sp->ap = (struct alias *)ap;
354 if (ap)
355 ((struct alias *)ap)->flag |= ALIASINUSE;
356 psh->parsenextc = s;
357 psh->parsenleft = (int)len;
358 INTON;
359}
360
361void
362popstring(shinstance *psh)
363{
364 struct strpush *sp = psh->parsefile->strpush;
365
366 INTOFF;
367 psh->parsenextc = sp->prevstring;
368 psh->parsenleft = sp->prevnleft;
369 psh->parselleft = sp->prevlleft;
370/*dprintf("*** calling popstring: restoring to '%s'\n", psh->parsenextc);*/
371 if (sp->ap)
372 sp->ap->flag &= ~ALIASINUSE;
373 psh->parsefile->strpush = sp->prev;
374 if (sp != &(psh->parsefile->basestrpush))
375 ckfree(sp);
376 INTON;
377}
378
379/*
380 * Set the input to take input from a file. If push is set, push the
381 * old input onto the stack first.
382 */
383
384void
385setinputfile(shinstance *psh, const char *fname, int push)
386{
387 int fd;
388 int fd2;
389
390 INTOFF;
391/** @todo shfile fixme */
392 if ((fd = shfile_open(&psh->fdtab, fname, O_RDONLY, 0)) < 0)
393 error(psh, "Can't open %s", fname);
394 if (fd < 10) {
395 fd2 = copyfd(psh, fd, 10);
396 shfile_close(&psh->fdtab, fd);
397 if (fd2 < 0)
398 error(psh, "Out of file descriptors");
399 fd = fd2;
400 }
401 setinputfd(psh, fd, push);
402 INTON;
403}
404
405
406/*
407 * Like setinputfile, but takes an open file descriptor. Call this with
408 * interrupts off.
409 */
410
411void
412setinputfd(shinstance *psh, int fd, int push)
413{
414 (void) shfile_fcntl(&psh->fdtab, fd, F_SETFD, FD_CLOEXEC);
415 if (push) {
416 pushfile(psh);
417 psh->parsefile->buf = ckmalloc(BUFSIZ);
418 }
419 if (psh->parsefile->fd > 0)
420 shfile_close(&psh->fdtab, psh->parsefile->fd);
421 psh->parsefile->fd = fd;
422 if (psh->parsefile->buf == NULL)
423 psh->parsefile->buf = ckmalloc(BUFSIZ);
424 psh->parselleft = psh->parsenleft = 0;
425 psh->plinno = 1;
426}
427
428
429/*
430 * Like setinputfile, but takes input from a string.
431 */
432
433void
434setinputstring(shinstance *psh, char *string, int push)
435{
436 INTOFF;
437 if (push)
438 pushfile(psh);
439 psh->parsenextc = string;
440 psh->parselleft = psh->parsenleft = (int)strlen(string);
441 psh->parsefile->buf = NULL;
442 psh->plinno = 1;
443 INTON;
444}
445
446
447
448/*
449 * To handle the "." command, a stack of input files is used. Pushfile
450 * adds a new entry to the stack and popfile restores the previous level.
451 */
452
453STATIC void
454pushfile(shinstance *psh)
455{
456 struct parsefile *pf;
457
458 psh->parsefile->nleft = psh->parsenleft;
459 psh->parsefile->lleft = psh->parselleft;
460 psh->parsefile->nextc = psh->parsenextc;
461 psh->parsefile->linno = psh->plinno;
462 pf = (struct parsefile *)ckmalloc(sizeof (struct parsefile));
463 pf->prev = psh->parsefile;
464 pf->fd = -1;
465 pf->strpush = NULL;
466 pf->basestrpush.prev = NULL;
467 psh->parsefile = pf;
468}
469
470
471void
472popfile(shinstance *psh)
473{
474 struct parsefile *pf = psh->parsefile;
475
476 INTOFF;
477 if (pf->fd >= 0)
478 shfile_close(&psh->fdtab, pf->fd);
479 if (pf->buf)
480 ckfree(pf->buf);
481 while (pf->strpush)
482 popstring(psh);
483 psh->parsefile = pf->prev;
484 ckfree(pf);
485 psh->parsenleft = psh->parsefile->nleft;
486 psh->parselleft = psh->parsefile->lleft;
487 psh->parsenextc = psh->parsefile->nextc;
488 psh->plinno = psh->parsefile->linno;
489 INTON;
490}
491
492
493/*
494 * Return to top level.
495 */
496
497void
498popallfiles(shinstance *psh)
499{
500 while (psh->parsefile != &psh->basepf)
501 popfile(psh);
502}
503
504
505
506/*
507 * Close the file(s) that the shell is reading commands from. Called
508 * after a fork is done.
509 *
510 * Takes one arg, vfork, which tells it to not modify its global vars
511 * as it is still running in the parent.
512 *
513 * This code is (probably) unnecessary as the 'close on exec' flag is
514 * set and should be enough. In the vfork case it is definitely wrong
515 * to close the fds as another fork() may be done later to feed data
516 * from a 'here' document into a pipe and we don't want to close the
517 * pipe!
518 */
519
520void
521closescript(shinstance *psh, int vforked)
522{
523 if (vforked)
524 return;
525 popallfiles(psh);
526 if (psh->parsefile->fd > 0) {
527 shfile_close(&psh->fdtab, psh->parsefile->fd);
528 psh->parsefile->fd = 0;
529 }
530}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette