17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
22*965005c8Schin
23*965005c8Schin /*
24*965005c8Schin * Copyright 2000 Sun Microsystems, Inc. All rights reserved.
25*965005c8Schin * Use is subject to license terms.
26*965005c8Schin */
27*965005c8Schin
287c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
297c478bd9Sstevel@tonic-gate /* All Rights Reserved */
307c478bd9Sstevel@tonic-gate
31*965005c8Schin #pragma ident "%Z%%M% %I% %E% SMI"
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate * UNIX shell
347c478bd9Sstevel@tonic-gate */
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate #include "defs.h"
377c478bd9Sstevel@tonic-gate #include "sym.h"
387c478bd9Sstevel@tonic-gate #include <errno.h>
397c478bd9Sstevel@tonic-gate #include <fcntl.h>
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate static int readb(struct fileblk *, int, int);
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gate /* ======== character handling for command lines ======== */
447c478bd9Sstevel@tonic-gate
45*965005c8Schin int
word(void)46*965005c8Schin word(void)
477c478bd9Sstevel@tonic-gate {
48*965005c8Schin unsigned int c, d, cc;
497c478bd9Sstevel@tonic-gate struct argnod *arg = (struct argnod *)locstak();
50*965005c8Schin unsigned char *argp = arg->argval;
517c478bd9Sstevel@tonic-gate unsigned char *oldargp;
527c478bd9Sstevel@tonic-gate int alpha = 1;
537c478bd9Sstevel@tonic-gate unsigned char *pc;
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate wdnum = 0;
567c478bd9Sstevel@tonic-gate wdset = 0;
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate while (1)
597c478bd9Sstevel@tonic-gate {
607c478bd9Sstevel@tonic-gate while (c = nextwc(), space(c)) /* skipc() */
617c478bd9Sstevel@tonic-gate ;
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate if (c == COMCHAR)
647c478bd9Sstevel@tonic-gate {
657c478bd9Sstevel@tonic-gate while ((c = readwc()) != NL && c != EOF);
667c478bd9Sstevel@tonic-gate peekc = c;
677c478bd9Sstevel@tonic-gate }
687c478bd9Sstevel@tonic-gate else
697c478bd9Sstevel@tonic-gate {
707c478bd9Sstevel@tonic-gate break; /* out of comment - white space loop */
717c478bd9Sstevel@tonic-gate }
727c478bd9Sstevel@tonic-gate }
737c478bd9Sstevel@tonic-gate if (!eofmeta(c))
747c478bd9Sstevel@tonic-gate {
757c478bd9Sstevel@tonic-gate do
767c478bd9Sstevel@tonic-gate {
777c478bd9Sstevel@tonic-gate if (c == LITERAL)
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate oldargp = argp;
807c478bd9Sstevel@tonic-gate while ((c = readwc()) && c != LITERAL){
817c478bd9Sstevel@tonic-gate /*
827c478bd9Sstevel@tonic-gate * quote each character within
837c478bd9Sstevel@tonic-gate * single quotes
847c478bd9Sstevel@tonic-gate */
857c478bd9Sstevel@tonic-gate pc = readw(c);
867c478bd9Sstevel@tonic-gate if (argp >= brkend)
877c478bd9Sstevel@tonic-gate growstak(argp);
887c478bd9Sstevel@tonic-gate *argp++='\\';
897c478bd9Sstevel@tonic-gate /* Pick up rest of multibyte character */
907c478bd9Sstevel@tonic-gate if (c == NL)
917c478bd9Sstevel@tonic-gate chkpr();
927c478bd9Sstevel@tonic-gate while (c = *pc++) {
937c478bd9Sstevel@tonic-gate if (argp >= brkend)
947c478bd9Sstevel@tonic-gate growstak(argp);
957c478bd9Sstevel@tonic-gate *argp++ = (unsigned char)c;
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate }
987c478bd9Sstevel@tonic-gate if (argp == oldargp) { /* null argument - '' */
997c478bd9Sstevel@tonic-gate /*
1007c478bd9Sstevel@tonic-gate * Word will be represented by quoted null
1017c478bd9Sstevel@tonic-gate * in macro.c if necessary
1027c478bd9Sstevel@tonic-gate */
1037c478bd9Sstevel@tonic-gate if (argp >= brkend)
1047c478bd9Sstevel@tonic-gate growstak(argp);
1057c478bd9Sstevel@tonic-gate *argp++ = '"';
1067c478bd9Sstevel@tonic-gate if (argp >= brkend)
1077c478bd9Sstevel@tonic-gate growstak(argp);
1087c478bd9Sstevel@tonic-gate *argp++ = '"';
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate }
1117c478bd9Sstevel@tonic-gate else
1127c478bd9Sstevel@tonic-gate {
1137c478bd9Sstevel@tonic-gate if (c == 0) {
1147c478bd9Sstevel@tonic-gate if (argp >= brkend)
1157c478bd9Sstevel@tonic-gate growstak(argp);
1167c478bd9Sstevel@tonic-gate *argp++ = 0;
1177c478bd9Sstevel@tonic-gate } else {
1187c478bd9Sstevel@tonic-gate pc = readw(c);
1197c478bd9Sstevel@tonic-gate while (*pc) {
1207c478bd9Sstevel@tonic-gate if (argp >= brkend)
1217c478bd9Sstevel@tonic-gate growstak(argp);
1227c478bd9Sstevel@tonic-gate *argp++ = *pc++;
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate if (c == '\\') {
1267c478bd9Sstevel@tonic-gate if ((cc = readwc()) == 0) {
1277c478bd9Sstevel@tonic-gate if (argp >= brkend)
1287c478bd9Sstevel@tonic-gate growstak(argp);
1297c478bd9Sstevel@tonic-gate *argp++ = 0;
1307c478bd9Sstevel@tonic-gate } else {
1317c478bd9Sstevel@tonic-gate pc = readw(cc);
1327c478bd9Sstevel@tonic-gate while (*pc) {
1337c478bd9Sstevel@tonic-gate if (argp >= brkend)
1347c478bd9Sstevel@tonic-gate growstak(argp);
1357c478bd9Sstevel@tonic-gate *argp++ = *pc++;
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate if (c == '=')
1407c478bd9Sstevel@tonic-gate wdset |= alpha;
1417c478bd9Sstevel@tonic-gate if (!alphanum(c))
1427c478bd9Sstevel@tonic-gate alpha = 0;
1437c478bd9Sstevel@tonic-gate if (qotchar(c))
1447c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate d = c;
1467c478bd9Sstevel@tonic-gate for (;;)
1477c478bd9Sstevel@tonic-gate {
1487c478bd9Sstevel@tonic-gate if ((c = nextwc()) == 0) {
1497c478bd9Sstevel@tonic-gate if (argp >= brkend)
1507c478bd9Sstevel@tonic-gate growstak(argp);
1517c478bd9Sstevel@tonic-gate *argp++ = 0;
1527c478bd9Sstevel@tonic-gate } else {
1537c478bd9Sstevel@tonic-gate pc = readw(c);
1547c478bd9Sstevel@tonic-gate while (*pc) {
1557c478bd9Sstevel@tonic-gate if (argp >= brkend)
1567c478bd9Sstevel@tonic-gate growstak(argp);
1577c478bd9Sstevel@tonic-gate *argp++ = *pc++;
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate if (c == 0 || c == d)
1617c478bd9Sstevel@tonic-gate break;
1627c478bd9Sstevel@tonic-gate if (c == NL)
1637c478bd9Sstevel@tonic-gate chkpr();
1647c478bd9Sstevel@tonic-gate /*
1657c478bd9Sstevel@tonic-gate * don't interpret quoted
1667c478bd9Sstevel@tonic-gate * characters
1677c478bd9Sstevel@tonic-gate */
1687c478bd9Sstevel@tonic-gate if (c == '\\') {
1697c478bd9Sstevel@tonic-gate if ((cc = readwc()) == 0) {
1707c478bd9Sstevel@tonic-gate if (argp >= brkend)
1717c478bd9Sstevel@tonic-gate growstak(argp);
1727c478bd9Sstevel@tonic-gate *argp++ = 0;
1737c478bd9Sstevel@tonic-gate } else {
1747c478bd9Sstevel@tonic-gate pc = readw(cc);
1757c478bd9Sstevel@tonic-gate while (*pc) {
1767c478bd9Sstevel@tonic-gate if (argp >= brkend)
1777c478bd9Sstevel@tonic-gate growstak(argp);
1787c478bd9Sstevel@tonic-gate *argp++ = *pc++;
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate } while ((c = nextwc(), !eofmeta(c)));
1867c478bd9Sstevel@tonic-gate argp = endstak(argp);
1877c478bd9Sstevel@tonic-gate if (!letter(arg->argval[0]))
1887c478bd9Sstevel@tonic-gate wdset = 0;
1897c478bd9Sstevel@tonic-gate
1907c478bd9Sstevel@tonic-gate peekn = c | MARK;
1917c478bd9Sstevel@tonic-gate if (arg->argval[1] == 0 &&
1927c478bd9Sstevel@tonic-gate (d = arg->argval[0], digit(d)) &&
1937c478bd9Sstevel@tonic-gate (c == '>' || c == '<'))
1947c478bd9Sstevel@tonic-gate {
1957c478bd9Sstevel@tonic-gate word();
1967c478bd9Sstevel@tonic-gate wdnum = d - '0';
1977c478bd9Sstevel@tonic-gate }else{ /* check for reserved words */
1987c478bd9Sstevel@tonic-gate if (reserv == FALSE ||
1997c478bd9Sstevel@tonic-gate (wdval = syslook(arg->argval,
2007c478bd9Sstevel@tonic-gate reserved, no_reserved)) == 0) {
2017c478bd9Sstevel@tonic-gate wdval = 0;
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate /* set arg for reserved words too */
2047c478bd9Sstevel@tonic-gate wdarg = arg;
2057c478bd9Sstevel@tonic-gate }
2067c478bd9Sstevel@tonic-gate }else if (dipchar(c)){
2077c478bd9Sstevel@tonic-gate if ((d = nextwc()) == c)
2087c478bd9Sstevel@tonic-gate {
2097c478bd9Sstevel@tonic-gate wdval = c | SYMREP;
2107c478bd9Sstevel@tonic-gate if (c == '<')
2117c478bd9Sstevel@tonic-gate {
2127c478bd9Sstevel@tonic-gate if ((d = nextwc()) == '-')
2137c478bd9Sstevel@tonic-gate wdnum |= IOSTRIP;
2147c478bd9Sstevel@tonic-gate else
2157c478bd9Sstevel@tonic-gate peekn = d | MARK;
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate else
2197c478bd9Sstevel@tonic-gate {
2207c478bd9Sstevel@tonic-gate peekn = d | MARK;
2217c478bd9Sstevel@tonic-gate wdval = c;
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate else
2257c478bd9Sstevel@tonic-gate {
2267c478bd9Sstevel@tonic-gate if ((wdval = c) == EOF)
2277c478bd9Sstevel@tonic-gate wdval = EOFSYM;
2287c478bd9Sstevel@tonic-gate if (iopend && eolchar(c))
2297c478bd9Sstevel@tonic-gate {
2307c478bd9Sstevel@tonic-gate struct ionod *tmp_iopend;
2317c478bd9Sstevel@tonic-gate tmp_iopend = iopend;
2327c478bd9Sstevel@tonic-gate iopend = 0;
2337c478bd9Sstevel@tonic-gate copy(tmp_iopend);
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate reserv = FALSE;
2377c478bd9Sstevel@tonic-gate return (wdval);
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate
skipwc()2407c478bd9Sstevel@tonic-gate unsigned int skipwc()
2417c478bd9Sstevel@tonic-gate {
242*965005c8Schin unsigned int c;
2437c478bd9Sstevel@tonic-gate
2447c478bd9Sstevel@tonic-gate while (c = nextwc(), space(c))
2457c478bd9Sstevel@tonic-gate ;
2467c478bd9Sstevel@tonic-gate return (c);
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate
nextwc()2497c478bd9Sstevel@tonic-gate unsigned int nextwc()
2507c478bd9Sstevel@tonic-gate {
251*965005c8Schin unsigned int c, d;
2527c478bd9Sstevel@tonic-gate
2537c478bd9Sstevel@tonic-gate retry:
2547c478bd9Sstevel@tonic-gate if ((d = readwc()) == ESCAPE) {
2557c478bd9Sstevel@tonic-gate if ((c = readwc()) == NL) {
2567c478bd9Sstevel@tonic-gate chkpr();
2577c478bd9Sstevel@tonic-gate goto retry;
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate peekc = c | MARK;
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate return (d);
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate
readw(d)2647c478bd9Sstevel@tonic-gate unsigned char *readw(d)
2657c478bd9Sstevel@tonic-gate wchar_t d;
2667c478bd9Sstevel@tonic-gate {
2677c478bd9Sstevel@tonic-gate static unsigned char c[MULTI_BYTE_MAX + 1];
2687c478bd9Sstevel@tonic-gate int length;
2697c478bd9Sstevel@tonic-gate wchar_t l;
2707c478bd9Sstevel@tonic-gate if (isascii(d)) {
2717c478bd9Sstevel@tonic-gate c[0] = d;
2727c478bd9Sstevel@tonic-gate c[1] = '\0';
2737c478bd9Sstevel@tonic-gate return (c);
2747c478bd9Sstevel@tonic-gate }
2757c478bd9Sstevel@tonic-gate
2767c478bd9Sstevel@tonic-gate length = wctomb((char *)c, d);
2777c478bd9Sstevel@tonic-gate if (length <= 0) {
2787c478bd9Sstevel@tonic-gate c[0] = (unsigned char)d;
2797c478bd9Sstevel@tonic-gate length = 1;
2807c478bd9Sstevel@tonic-gate }
2817c478bd9Sstevel@tonic-gate c[length] = '\0';
2827c478bd9Sstevel@tonic-gate return (c);
2837c478bd9Sstevel@tonic-gate }
2847c478bd9Sstevel@tonic-gate
2857c478bd9Sstevel@tonic-gate unsigned int
readwc()2867c478bd9Sstevel@tonic-gate readwc()
2877c478bd9Sstevel@tonic-gate {
2887c478bd9Sstevel@tonic-gate wchar_t c;
2897c478bd9Sstevel@tonic-gate int len;
2907c478bd9Sstevel@tonic-gate struct fileblk *f;
2917c478bd9Sstevel@tonic-gate int mbmax = MB_CUR_MAX;
2927c478bd9Sstevel@tonic-gate int i, mlen;
2937c478bd9Sstevel@tonic-gate
2947c478bd9Sstevel@tonic-gate if (peekn) {
2957c478bd9Sstevel@tonic-gate c = peekn & 0x7fffffff;
2967c478bd9Sstevel@tonic-gate peekn = 0;
2977c478bd9Sstevel@tonic-gate return (c);
2987c478bd9Sstevel@tonic-gate }
2997c478bd9Sstevel@tonic-gate if (peekc) {
3007c478bd9Sstevel@tonic-gate c = peekc & 0x7fffffff;
3017c478bd9Sstevel@tonic-gate peekc = 0;
3027c478bd9Sstevel@tonic-gate return (c);
3037c478bd9Sstevel@tonic-gate }
3047c478bd9Sstevel@tonic-gate f = standin;
3057c478bd9Sstevel@tonic-gate
3067c478bd9Sstevel@tonic-gate retry:
3077c478bd9Sstevel@tonic-gate if (f->fend > f->fnxt) {
3087c478bd9Sstevel@tonic-gate /*
3097c478bd9Sstevel@tonic-gate * something in buffer
3107c478bd9Sstevel@tonic-gate */
3117c478bd9Sstevel@tonic-gate if (*f->fnxt == 0) {
3127c478bd9Sstevel@tonic-gate f->fnxt++;
3137c478bd9Sstevel@tonic-gate f->nxtoff++;
3147c478bd9Sstevel@tonic-gate if (f->feval == 0)
3157c478bd9Sstevel@tonic-gate goto retry; /* = c = readc(); */
3167c478bd9Sstevel@tonic-gate if (estabf(*f->feval++))
3177c478bd9Sstevel@tonic-gate c = EOF;
3187c478bd9Sstevel@tonic-gate else
3197c478bd9Sstevel@tonic-gate c = SPACE;
3207c478bd9Sstevel@tonic-gate if (flags & readpr && standin->fstak == 0)
3217c478bd9Sstevel@tonic-gate prc(c);
3227c478bd9Sstevel@tonic-gate if (c == NL)
3237c478bd9Sstevel@tonic-gate f->flin++;
3247c478bd9Sstevel@tonic-gate return (c);
3257c478bd9Sstevel@tonic-gate }
3267c478bd9Sstevel@tonic-gate
3277c478bd9Sstevel@tonic-gate if (isascii(c = (unsigned char)*f->fnxt)) {
3287c478bd9Sstevel@tonic-gate f->fnxt++;
3297c478bd9Sstevel@tonic-gate f->nxtoff++;
3307c478bd9Sstevel@tonic-gate if (flags & readpr && standin->fstak == 0)
3317c478bd9Sstevel@tonic-gate prc(c);
3327c478bd9Sstevel@tonic-gate if (c == NL)
3337c478bd9Sstevel@tonic-gate f->flin++;
3347c478bd9Sstevel@tonic-gate return (c);
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate
3377c478bd9Sstevel@tonic-gate for (i = 1; i <= mbmax; i++) {
3387c478bd9Sstevel@tonic-gate int rest;
3397c478bd9Sstevel@tonic-gate if ((rest = f->fend - f->fnxt) < i) {
3407c478bd9Sstevel@tonic-gate /*
3417c478bd9Sstevel@tonic-gate * not enough bytes available
3427c478bd9Sstevel@tonic-gate * f->fsiz could be BUFFERSIZE or 1
3437c478bd9Sstevel@tonic-gate * since mbmax is enough smaller than BUFFERSIZE,
3447c478bd9Sstevel@tonic-gate * this loop won't overrun the f->fbuf buffer.
3457c478bd9Sstevel@tonic-gate */
3467c478bd9Sstevel@tonic-gate len = readb(f,
3477c478bd9Sstevel@tonic-gate (f->fsiz == 1) ? 1 : (f->fsiz - rest),
3487c478bd9Sstevel@tonic-gate rest);
3497c478bd9Sstevel@tonic-gate if (len == 0)
3507c478bd9Sstevel@tonic-gate break;
3517c478bd9Sstevel@tonic-gate }
3527c478bd9Sstevel@tonic-gate mlen = mbtowc(&c, (char *)f->fnxt, i);
3537c478bd9Sstevel@tonic-gate if (mlen > 0)
3547c478bd9Sstevel@tonic-gate break;
3557c478bd9Sstevel@tonic-gate }
3567c478bd9Sstevel@tonic-gate
3577c478bd9Sstevel@tonic-gate if (i > mbmax) {
3587c478bd9Sstevel@tonic-gate /*
3597c478bd9Sstevel@tonic-gate * enough bytes available but cannot be converted to
3607c478bd9Sstevel@tonic-gate * a valid wchar.
3617c478bd9Sstevel@tonic-gate */
3627c478bd9Sstevel@tonic-gate c = (unsigned char)*f->fnxt;
3637c478bd9Sstevel@tonic-gate mlen = 1;
3647c478bd9Sstevel@tonic-gate }
3657c478bd9Sstevel@tonic-gate
3667c478bd9Sstevel@tonic-gate f->fnxt += mlen;
3677c478bd9Sstevel@tonic-gate f->nxtoff += mlen;
3687c478bd9Sstevel@tonic-gate if (flags & readpr && standin->fstak == 0)
3697c478bd9Sstevel@tonic-gate prwc(c);
3707c478bd9Sstevel@tonic-gate if (c == NL)
3717c478bd9Sstevel@tonic-gate f->flin++;
3727c478bd9Sstevel@tonic-gate return (c);
3737c478bd9Sstevel@tonic-gate }
3747c478bd9Sstevel@tonic-gate
3757c478bd9Sstevel@tonic-gate if (f->feof || f->fdes < 0){
3767c478bd9Sstevel@tonic-gate c = EOF;
3777c478bd9Sstevel@tonic-gate f->feof++;
3787c478bd9Sstevel@tonic-gate return (c);
3797c478bd9Sstevel@tonic-gate }
3807c478bd9Sstevel@tonic-gate
3817c478bd9Sstevel@tonic-gate if (readb(f, f->fsiz, 0) <= 0){
3827c478bd9Sstevel@tonic-gate if (f->fdes != input || !isatty(input)) {
3837c478bd9Sstevel@tonic-gate close(f->fdes);
3847c478bd9Sstevel@tonic-gate f->fdes = -1;
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate f->feof++;
3877c478bd9Sstevel@tonic-gate c = EOF;
3887c478bd9Sstevel@tonic-gate return (c);
3897c478bd9Sstevel@tonic-gate }
3907c478bd9Sstevel@tonic-gate goto retry;
3917c478bd9Sstevel@tonic-gate }
3927c478bd9Sstevel@tonic-gate
3937c478bd9Sstevel@tonic-gate static int
readb(struct fileblk * f,int toread,int rest)3947c478bd9Sstevel@tonic-gate readb(struct fileblk *f, int toread, int rest)
3957c478bd9Sstevel@tonic-gate {
3967c478bd9Sstevel@tonic-gate int len;
3977c478bd9Sstevel@tonic-gate int fflags;
3987c478bd9Sstevel@tonic-gate
3997c478bd9Sstevel@tonic-gate if (rest) {
4007c478bd9Sstevel@tonic-gate /*
4017c478bd9Sstevel@tonic-gate * copies the remaining 'rest' bytes from f->fnxt
4027c478bd9Sstevel@tonic-gate * to f->fbuf
4037c478bd9Sstevel@tonic-gate */
4047c478bd9Sstevel@tonic-gate (void) memcpy(f->fbuf, f->fnxt, rest);
4057c478bd9Sstevel@tonic-gate f->fnxt = f->fbuf;
4067c478bd9Sstevel@tonic-gate f->fend = f->fnxt + rest;
4077c478bd9Sstevel@tonic-gate f->nxtoff = 0;
4087c478bd9Sstevel@tonic-gate f->endoff = rest;
4097c478bd9Sstevel@tonic-gate if (f->fbuf[rest - 1] == '\n') {
4107c478bd9Sstevel@tonic-gate /*
4117c478bd9Sstevel@tonic-gate * if '\n' found, it should be
4127c478bd9Sstevel@tonic-gate * a bondary of multibyte char.
4137c478bd9Sstevel@tonic-gate */
4147c478bd9Sstevel@tonic-gate return (rest);
4157c478bd9Sstevel@tonic-gate }
4167c478bd9Sstevel@tonic-gate }
4177c478bd9Sstevel@tonic-gate
4187c478bd9Sstevel@tonic-gate retry:
4197c478bd9Sstevel@tonic-gate do {
4207c478bd9Sstevel@tonic-gate if (trapnote & SIGSET) {
4217c478bd9Sstevel@tonic-gate newline();
4227c478bd9Sstevel@tonic-gate sigchk();
4237c478bd9Sstevel@tonic-gate } else if ((trapnote & TRAPSET) && (rwait > 0)) {
4247c478bd9Sstevel@tonic-gate newline();
4257c478bd9Sstevel@tonic-gate chktrap();
4267c478bd9Sstevel@tonic-gate clearup();
4277c478bd9Sstevel@tonic-gate }
4287c478bd9Sstevel@tonic-gate } while ((len = read(f->fdes, f->fbuf + rest, toread)) < 0 && trapnote);
4297c478bd9Sstevel@tonic-gate /*
4307c478bd9Sstevel@tonic-gate * if child sets O_NDELAY or O_NONBLOCK on stdin
4317c478bd9Sstevel@tonic-gate * and exited then turn the modes off and retry
4327c478bd9Sstevel@tonic-gate */
4337c478bd9Sstevel@tonic-gate if (len == 0) {
4347c478bd9Sstevel@tonic-gate if (((flags & intflg) ||
4357c478bd9Sstevel@tonic-gate ((flags & oneflg) == 0 && isatty(input) &&
4367c478bd9Sstevel@tonic-gate (flags & stdflg))) &&
4377c478bd9Sstevel@tonic-gate ((fflags = fcntl(f->fdes, F_GETFL, 0)) & O_NDELAY)) {
4387c478bd9Sstevel@tonic-gate fflags &= ~O_NDELAY;
4397c478bd9Sstevel@tonic-gate fcntl(f->fdes, F_SETFL, fflags);
4407c478bd9Sstevel@tonic-gate goto retry;
4417c478bd9Sstevel@tonic-gate }
4427c478bd9Sstevel@tonic-gate } else if (len < 0) {
4437c478bd9Sstevel@tonic-gate if (errno == EAGAIN) {
4447c478bd9Sstevel@tonic-gate fflags = fcntl(f->fdes, F_GETFL, 0);
4457c478bd9Sstevel@tonic-gate fflags &= ~O_NONBLOCK;
4467c478bd9Sstevel@tonic-gate fcntl(f->fdes, F_SETFL, fflags);
4477c478bd9Sstevel@tonic-gate goto retry;
4487c478bd9Sstevel@tonic-gate }
4497c478bd9Sstevel@tonic-gate len = 0;
4507c478bd9Sstevel@tonic-gate }
4517c478bd9Sstevel@tonic-gate f->fnxt = f->fbuf;
4527c478bd9Sstevel@tonic-gate f->fend = f->fnxt + (len + rest);
4537c478bd9Sstevel@tonic-gate f->nxtoff = 0;
4547c478bd9Sstevel@tonic-gate f->endoff = len + rest;
4557c478bd9Sstevel@tonic-gate return (len + rest);
4567c478bd9Sstevel@tonic-gate }
457