VirtualBox

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

Last change on this file since 2629 was 2629, checked in by bird, 12 years ago

kash/parser.c: Fixed incorrect handling of \r\n -> \n conversion when there is a lone \r at the end of the input buffer.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
File size: 13.0 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_inner(shinstance *psh, char *buf, int bufsize)
175{
176 int nr;
177retry:
178#ifndef SMALL
179 if (psh->parsefile->fd == 0 && psh->el) {
180 static const char *rl_cp;
181 static int el_len;
182
183 if (rl_cp == NULL)
184 rl_cp = el_gets(psh->el, &el_len);
185 if (rl_cp == NULL)
186 nr = 0;
187 else {
188 nr = el_len;
189 if (nr > bufsize)
190 nr = bufsize;
191 memcpy(buf, rl_cp, nr);
192 if (nr != el_len) {
193 el_len -= nr;
194 rl_cp += nr;
195 } else
196 rl_cp = 0;
197 }
198
199 } else
200#endif
201 nr = shfile_read(&psh->fdtab, psh->parsefile->fd, buf, bufsize);
202
203
204 if (nr <= 0) {
205 if (nr < 0) {
206 if (errno == EINTR)
207 goto retry;
208 if (psh->parsefile->fd == 0 && errno == EWOULDBLOCK) {
209 int flags = shfile_fcntl(&psh->fdtab, 0, F_GETFL, 0);
210 if (flags >= 0 && flags & O_NONBLOCK) {
211 flags &=~ O_NONBLOCK;
212 if (shfile_fcntl(&psh->fdtab, 0, F_SETFL, flags) >= 0) {
213 out2str(psh, "sh: turning off NDELAY mode\n");
214 goto retry;
215 }
216 }
217 }
218 }
219 nr = -1;
220 }
221 return nr;
222}
223
224
225
226static int
227preadfd(shinstance *psh)
228{
229 int nr;
230 char *buf = psh->parsefile->buf;
231 psh->parsenextc = buf;
232
233#ifdef SH_DEAL_WITH_CRLF
234 /* Convert CRLF to LF. */
235 nr = preadfd_inner(psh, buf, BUFSIZ - 9);
236 if (nr > 0) {
237 char *cr = memchr(buf, '\r', nr);
238 while (cr) {
239 size_t left = nr - (cr - buf);
240
241 if (left > 1 && cr[1] == '\n') {
242 left--;
243 nr--;
244 memmove(cr, cr + 1, left);
245 cr = memchr(cr, '\r', left);
246 } else if (left == 1) {
247 /* Special case: \r at buffer end. Read one more char. Screw \r\r\n sequences. */
248 int nr2 = preadfd_inner(psh, cr + 1, 1);
249 if (nr2 != 1)
250 break;
251 if (cr[1] == '\n') {
252 *cr = '\n';
253 } else {
254 nr++;
255 }
256 break;
257 } else {
258 cr = memchr(cr + 1, '\r', left);
259 }
260 }
261 }
262#else
263 nr = preadfd_inner(psh, buf, BUFSIZ - 8);
264#endif
265 return nr;
266}
267
268/*
269 * Refill the input buffer and return the next input character:
270 *
271 * 1) If a string was pushed back on the input, pop it;
272 * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading
273 * from a string so we can't refill the buffer, return EOF.
274 * 3) If the is more stuff in this buffer, use it else call read to fill it.
275 * 4) Process input up to the next newline, deleting nul characters.
276 */
277
278int
279preadbuffer(shinstance *psh)
280{
281 char *p, *q;
282 int more;
283 int something;
284 char savec;
285
286 if (psh->parsefile->strpush) {
287 popstring(psh);
288 if (--psh->parsenleft >= 0)
289 return (*psh->parsenextc++);
290 }
291 if (psh->parsenleft == EOF_NLEFT || psh->parsefile->buf == NULL)
292 return PEOF;
293 flushout(&psh->output);
294 flushout(&psh->errout);
295
296again:
297 if (psh->parselleft <= 0) {
298 if ((psh->parselleft = preadfd(psh)) == -1) {
299 psh->parselleft = psh->parsenleft = EOF_NLEFT;
300 return PEOF;
301 }
302 }
303
304 q = p = psh->parsenextc;
305
306 /* delete nul characters */
307 something = 0;
308 for (more = 1; more;) {
309 switch (*p) {
310 case '\0':
311 p++; /* Skip nul */
312 goto check;
313
314 case '\t':
315 case ' ':
316 break;
317
318 case '\n':
319 psh->parsenleft = (int)(q - psh->parsenextc);
320 more = 0; /* Stop processing here */
321 break;
322
323 default:
324 something = 1;
325 break;
326 }
327
328 *q++ = *p++;
329check:
330 if (--psh->parselleft <= 0) {
331 psh->parsenleft = (int)(q - psh->parsenextc - 1);
332 if (psh->parsenleft < 0)
333 goto again;
334 *q = '\0';
335 more = 0;
336 }
337 }
338
339 savec = *q;
340 *q = '\0';
341
342#ifndef SMALL
343 if (psh->parsefile->fd == 0 && hist && something) {
344 HistEvent he;
345 INTOFF;
346 history(hist, &he, psh->whichprompt == 1? H_ENTER : H_APPEND,
347 psh->parsenextc);
348 INTON;
349 }
350#endif
351
352 if (vflag(psh)) {
353 out2str(psh, psh->parsenextc);
354 flushout(psh->out2);
355 }
356
357 *q = savec;
358
359 return *psh->parsenextc++;
360}
361
362/*
363 * Undo the last call to pgetc. Only one character may be pushed back.
364 * PEOF may be pushed back.
365 */
366
367void
368pungetc(shinstance *psh)
369{
370 psh->parsenleft++;
371 psh->parsenextc--;
372}
373
374/*
375 * Push a string back onto the input at this current parsefile level.
376 * We handle aliases this way.
377 */
378void
379pushstring(shinstance *psh, char *s, size_t len, void *ap)
380{
381 struct strpush *sp;
382
383 INTOFF;
384/*dprintf("*** calling pushstring: %s, %d\n", s, len);*/
385 if (psh->parsefile->strpush) {
386 sp = ckmalloc(psh, sizeof (struct strpush));
387 sp->prev = psh->parsefile->strpush;
388 psh->parsefile->strpush = sp;
389 } else
390 sp = psh->parsefile->strpush = &(psh->parsefile->basestrpush);
391 sp->prevstring = psh->parsenextc;
392 sp->prevnleft = psh->parsenleft;
393 sp->prevlleft = psh->parselleft;
394 sp->ap = (struct alias *)ap;
395 if (ap)
396 ((struct alias *)ap)->flag |= ALIASINUSE;
397 psh->parsenextc = s;
398 psh->parsenleft = (int)len;
399 INTON;
400}
401
402void
403popstring(shinstance *psh)
404{
405 struct strpush *sp = psh->parsefile->strpush;
406
407 INTOFF;
408 psh->parsenextc = sp->prevstring;
409 psh->parsenleft = sp->prevnleft;
410 psh->parselleft = sp->prevlleft;
411/*dprintf("*** calling popstring: restoring to '%s'\n", psh->parsenextc);*/
412 if (sp->ap)
413 sp->ap->flag &= ~ALIASINUSE;
414 psh->parsefile->strpush = sp->prev;
415 if (sp != &(psh->parsefile->basestrpush))
416 ckfree(psh, sp);
417 INTON;
418}
419
420/*
421 * Set the input to take input from a file. If push is set, push the
422 * old input onto the stack first.
423 */
424
425void
426setinputfile(shinstance *psh, const char *fname, int push)
427{
428 int fd;
429 int fd2;
430
431 INTOFF;
432/** @todo shfile fixme */
433 if ((fd = shfile_open(&psh->fdtab, fname, O_RDONLY, 0)) < 0)
434 error(psh, "Can't open %s", fname);
435 if (fd < 10) {
436 fd2 = movefd_above(psh, fd, 10);
437 if (fd2 < 0)
438 error(psh, "Out of file descriptors");
439 fd = fd2;
440 }
441 setinputfd(psh, fd, push);
442 INTON;
443}
444
445
446/*
447 * Like setinputfile, but takes an open file descriptor. Call this with
448 * interrupts off.
449 */
450
451void
452setinputfd(shinstance *psh, int fd, int push)
453{
454 (void) shfile_cloexec(&psh->fdtab, fd, 1 /* close it */);
455 if (push) {
456 pushfile(psh);
457 psh->parsefile->buf = ckmalloc(psh, BUFSIZ);
458 }
459 if (psh->parsefile->fd > 0)
460 shfile_close(&psh->fdtab, psh->parsefile->fd);
461 psh->parsefile->fd = fd;
462 if (psh->parsefile->buf == NULL)
463 psh->parsefile->buf = ckmalloc(psh, BUFSIZ);
464 psh->parselleft = psh->parsenleft = 0;
465 psh->plinno = 1;
466}
467
468
469/*
470 * Like setinputfile, but takes input from a string.
471 */
472
473void
474setinputstring(shinstance *psh, char *string, int push)
475{
476 INTOFF;
477 if (push)
478 pushfile(psh);
479 psh->parsenextc = string;
480 psh->parselleft = psh->parsenleft = (int)strlen(string);
481 psh->parsefile->buf = NULL;
482 psh->plinno = 1;
483 INTON;
484}
485
486
487
488/*
489 * To handle the "." command, a stack of input files is used. Pushfile
490 * adds a new entry to the stack and popfile restores the previous level.
491 */
492
493STATIC void
494pushfile(shinstance *psh)
495{
496 struct parsefile *pf;
497
498 psh->parsefile->nleft = psh->parsenleft;
499 psh->parsefile->lleft = psh->parselleft;
500 psh->parsefile->nextc = psh->parsenextc;
501 psh->parsefile->linno = psh->plinno;
502 pf = (struct parsefile *)ckmalloc(psh, sizeof (struct parsefile));
503 pf->prev = psh->parsefile;
504 pf->fd = -1;
505 pf->strpush = NULL;
506 pf->basestrpush.prev = NULL;
507 psh->parsefile = pf;
508}
509
510
511void
512popfile(shinstance *psh)
513{
514 struct parsefile *pf = psh->parsefile;
515
516 INTOFF;
517 if (pf->fd >= 0)
518 shfile_close(&psh->fdtab, pf->fd);
519 if (pf->buf)
520 ckfree(psh, pf->buf);
521 while (pf->strpush)
522 popstring(psh);
523 psh->parsefile = pf->prev;
524 ckfree(psh, pf);
525 psh->parsenleft = psh->parsefile->nleft;
526 psh->parselleft = psh->parsefile->lleft;
527 psh->parsenextc = psh->parsefile->nextc;
528 psh->plinno = psh->parsefile->linno;
529 INTON;
530}
531
532
533/*
534 * Return to top level.
535 */
536
537void
538popallfiles(shinstance *psh)
539{
540 while (psh->parsefile != &psh->basepf)
541 popfile(psh);
542}
543
544
545
546/*
547 * Close the file(s) that the shell is reading commands from. Called
548 * after a fork is done.
549 *
550 * Takes one arg, vfork, which tells it to not modify its global vars
551 * as it is still running in the parent.
552 *
553 * This code is (probably) unnecessary as the 'close on exec' flag is
554 * set and should be enough. In the vfork case it is definitely wrong
555 * to close the fds as another fork() may be done later to feed data
556 * from a 'here' document into a pipe and we don't want to close the
557 * pipe!
558 */
559
560void
561closescript(shinstance *psh, int vforked)
562{
563 if (vforked)
564 return;
565 popallfiles(psh);
566 if (psh->parsefile->fd > 0) {
567 shfile_close(&psh->fdtab, psh->parsefile->fd);
568 psh->parsefile->fd = 0;
569 }
570}
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