xref: /titanic_52/usr/src/cmd/sh/word.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1996, by Sun Microsystems, Inc.
28*7c478bd9Sstevel@tonic-gate  * All rights reserved.
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.11.2.2	*/
32*7c478bd9Sstevel@tonic-gate /*
33*7c478bd9Sstevel@tonic-gate  * UNIX shell
34*7c478bd9Sstevel@tonic-gate  */
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #include	"defs.h"
37*7c478bd9Sstevel@tonic-gate #include	"sym.h"
38*7c478bd9Sstevel@tonic-gate #include	<errno.h>
39*7c478bd9Sstevel@tonic-gate #include	<fcntl.h>
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate static int	readb(struct fileblk *, int, int);
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate /* ========	character handling for command lines	======== */
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate word()
47*7c478bd9Sstevel@tonic-gate {
48*7c478bd9Sstevel@tonic-gate 	register unsigned int	c, d, cc;
49*7c478bd9Sstevel@tonic-gate 	struct argnod	*arg = (struct argnod *)locstak();
50*7c478bd9Sstevel@tonic-gate 	register unsigned char	*argp = arg->argval;
51*7c478bd9Sstevel@tonic-gate 	unsigned char	*oldargp;
52*7c478bd9Sstevel@tonic-gate 	int		alpha = 1;
53*7c478bd9Sstevel@tonic-gate 	unsigned char *pc;
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate 	wdnum = 0;
56*7c478bd9Sstevel@tonic-gate 	wdset = 0;
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate 	while (1)
59*7c478bd9Sstevel@tonic-gate 	{
60*7c478bd9Sstevel@tonic-gate 		while (c = nextwc(), space(c))		/* skipc() */
61*7c478bd9Sstevel@tonic-gate 			;
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate 		if (c == COMCHAR)
64*7c478bd9Sstevel@tonic-gate 		{
65*7c478bd9Sstevel@tonic-gate 			while ((c = readwc()) != NL && c != EOF);
66*7c478bd9Sstevel@tonic-gate 			peekc = c;
67*7c478bd9Sstevel@tonic-gate 		}
68*7c478bd9Sstevel@tonic-gate 		else
69*7c478bd9Sstevel@tonic-gate 		{
70*7c478bd9Sstevel@tonic-gate 			break;	/* out of comment - white space loop */
71*7c478bd9Sstevel@tonic-gate 		}
72*7c478bd9Sstevel@tonic-gate 	}
73*7c478bd9Sstevel@tonic-gate 	if (!eofmeta(c))
74*7c478bd9Sstevel@tonic-gate 	{
75*7c478bd9Sstevel@tonic-gate 		do
76*7c478bd9Sstevel@tonic-gate 		{
77*7c478bd9Sstevel@tonic-gate 			if (c == LITERAL)
78*7c478bd9Sstevel@tonic-gate 			{
79*7c478bd9Sstevel@tonic-gate 				oldargp = argp;
80*7c478bd9Sstevel@tonic-gate 				while ((c = readwc()) && c != LITERAL){
81*7c478bd9Sstevel@tonic-gate 					/*
82*7c478bd9Sstevel@tonic-gate 					 * quote each character within
83*7c478bd9Sstevel@tonic-gate 					 * single quotes
84*7c478bd9Sstevel@tonic-gate 					 */
85*7c478bd9Sstevel@tonic-gate 					pc = readw(c);
86*7c478bd9Sstevel@tonic-gate 					if (argp >= brkend)
87*7c478bd9Sstevel@tonic-gate 						growstak(argp);
88*7c478bd9Sstevel@tonic-gate 					*argp++='\\';
89*7c478bd9Sstevel@tonic-gate 				/* Pick up rest of multibyte character */
90*7c478bd9Sstevel@tonic-gate 					if (c == NL)
91*7c478bd9Sstevel@tonic-gate 						chkpr();
92*7c478bd9Sstevel@tonic-gate 					while (c = *pc++) {
93*7c478bd9Sstevel@tonic-gate 						if (argp >= brkend)
94*7c478bd9Sstevel@tonic-gate 							growstak(argp);
95*7c478bd9Sstevel@tonic-gate 						*argp++ = (unsigned char)c;
96*7c478bd9Sstevel@tonic-gate 					}
97*7c478bd9Sstevel@tonic-gate 				}
98*7c478bd9Sstevel@tonic-gate 				if (argp == oldargp) { /* null argument - '' */
99*7c478bd9Sstevel@tonic-gate 				/*
100*7c478bd9Sstevel@tonic-gate 				 * Word will be represented by quoted null
101*7c478bd9Sstevel@tonic-gate 				 * in macro.c if necessary
102*7c478bd9Sstevel@tonic-gate 				 */
103*7c478bd9Sstevel@tonic-gate 					if (argp >= brkend)
104*7c478bd9Sstevel@tonic-gate 						growstak(argp);
105*7c478bd9Sstevel@tonic-gate 					*argp++ = '"';
106*7c478bd9Sstevel@tonic-gate 					if (argp >= brkend)
107*7c478bd9Sstevel@tonic-gate 						growstak(argp);
108*7c478bd9Sstevel@tonic-gate 					*argp++ = '"';
109*7c478bd9Sstevel@tonic-gate 				}
110*7c478bd9Sstevel@tonic-gate 			}
111*7c478bd9Sstevel@tonic-gate 			else
112*7c478bd9Sstevel@tonic-gate 			{
113*7c478bd9Sstevel@tonic-gate 				if (c == 0) {
114*7c478bd9Sstevel@tonic-gate 					if (argp >= brkend)
115*7c478bd9Sstevel@tonic-gate 						growstak(argp);
116*7c478bd9Sstevel@tonic-gate 					*argp++ = 0;
117*7c478bd9Sstevel@tonic-gate 				} else {
118*7c478bd9Sstevel@tonic-gate 					pc = readw(c);
119*7c478bd9Sstevel@tonic-gate 					while (*pc) {
120*7c478bd9Sstevel@tonic-gate 						if (argp >= brkend)
121*7c478bd9Sstevel@tonic-gate 							growstak(argp);
122*7c478bd9Sstevel@tonic-gate 						*argp++ = *pc++;
123*7c478bd9Sstevel@tonic-gate 					}
124*7c478bd9Sstevel@tonic-gate 				}
125*7c478bd9Sstevel@tonic-gate 				if (c == '\\') {
126*7c478bd9Sstevel@tonic-gate 					if ((cc = readwc()) == 0) {
127*7c478bd9Sstevel@tonic-gate 						if (argp >= brkend)
128*7c478bd9Sstevel@tonic-gate 							growstak(argp);
129*7c478bd9Sstevel@tonic-gate 						*argp++ = 0;
130*7c478bd9Sstevel@tonic-gate 					} else {
131*7c478bd9Sstevel@tonic-gate 						pc = readw(cc);
132*7c478bd9Sstevel@tonic-gate 						while (*pc) {
133*7c478bd9Sstevel@tonic-gate 							if (argp >= brkend)
134*7c478bd9Sstevel@tonic-gate 								growstak(argp);
135*7c478bd9Sstevel@tonic-gate 							*argp++ = *pc++;
136*7c478bd9Sstevel@tonic-gate 						}
137*7c478bd9Sstevel@tonic-gate 					}
138*7c478bd9Sstevel@tonic-gate 				}
139*7c478bd9Sstevel@tonic-gate 				if (c == '=')
140*7c478bd9Sstevel@tonic-gate 					wdset |= alpha;
141*7c478bd9Sstevel@tonic-gate 				if (!alphanum(c))
142*7c478bd9Sstevel@tonic-gate 					alpha = 0;
143*7c478bd9Sstevel@tonic-gate 				if (qotchar(c))
144*7c478bd9Sstevel@tonic-gate 				{
145*7c478bd9Sstevel@tonic-gate 					d = c;
146*7c478bd9Sstevel@tonic-gate 					for (;;)
147*7c478bd9Sstevel@tonic-gate 					{
148*7c478bd9Sstevel@tonic-gate 						if ((c = nextwc()) == 0) {
149*7c478bd9Sstevel@tonic-gate 							if (argp >= brkend)
150*7c478bd9Sstevel@tonic-gate 								growstak(argp);
151*7c478bd9Sstevel@tonic-gate 							*argp++ = 0;
152*7c478bd9Sstevel@tonic-gate 						} else {
153*7c478bd9Sstevel@tonic-gate 							pc = readw(c);
154*7c478bd9Sstevel@tonic-gate 							while (*pc) {
155*7c478bd9Sstevel@tonic-gate 								if (argp >= brkend)
156*7c478bd9Sstevel@tonic-gate 									growstak(argp);
157*7c478bd9Sstevel@tonic-gate 								*argp++ = *pc++;
158*7c478bd9Sstevel@tonic-gate 							}
159*7c478bd9Sstevel@tonic-gate 						}
160*7c478bd9Sstevel@tonic-gate 						if (c == 0 || c == d)
161*7c478bd9Sstevel@tonic-gate 							break;
162*7c478bd9Sstevel@tonic-gate 						if (c == NL)
163*7c478bd9Sstevel@tonic-gate 							chkpr();
164*7c478bd9Sstevel@tonic-gate 						/*
165*7c478bd9Sstevel@tonic-gate 						 * don't interpret quoted
166*7c478bd9Sstevel@tonic-gate 						 * characters
167*7c478bd9Sstevel@tonic-gate 						 */
168*7c478bd9Sstevel@tonic-gate 						if (c == '\\') {
169*7c478bd9Sstevel@tonic-gate 							if ((cc = readwc()) == 0) {
170*7c478bd9Sstevel@tonic-gate 								if (argp >= brkend)
171*7c478bd9Sstevel@tonic-gate 									growstak(argp);
172*7c478bd9Sstevel@tonic-gate 								*argp++ = 0;
173*7c478bd9Sstevel@tonic-gate 							} else {
174*7c478bd9Sstevel@tonic-gate 								pc = readw(cc);
175*7c478bd9Sstevel@tonic-gate 								while (*pc) {
176*7c478bd9Sstevel@tonic-gate 									if (argp >= brkend)
177*7c478bd9Sstevel@tonic-gate 										growstak(argp);
178*7c478bd9Sstevel@tonic-gate 									*argp++ = *pc++;
179*7c478bd9Sstevel@tonic-gate 								}
180*7c478bd9Sstevel@tonic-gate 							}
181*7c478bd9Sstevel@tonic-gate 						}
182*7c478bd9Sstevel@tonic-gate 					}
183*7c478bd9Sstevel@tonic-gate 				}
184*7c478bd9Sstevel@tonic-gate 			}
185*7c478bd9Sstevel@tonic-gate 		} while ((c = nextwc(), !eofmeta(c)));
186*7c478bd9Sstevel@tonic-gate 		argp = endstak(argp);
187*7c478bd9Sstevel@tonic-gate 		if (!letter(arg->argval[0]))
188*7c478bd9Sstevel@tonic-gate 			wdset = 0;
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate 		peekn = c | MARK;
191*7c478bd9Sstevel@tonic-gate 		if (arg->argval[1] == 0 &&
192*7c478bd9Sstevel@tonic-gate 		    (d = arg->argval[0], digit(d)) &&
193*7c478bd9Sstevel@tonic-gate 		    (c == '>' || c == '<'))
194*7c478bd9Sstevel@tonic-gate 		{
195*7c478bd9Sstevel@tonic-gate 			word();
196*7c478bd9Sstevel@tonic-gate 			wdnum = d - '0';
197*7c478bd9Sstevel@tonic-gate 		}else{ /* check for reserved words */
198*7c478bd9Sstevel@tonic-gate 			if (reserv == FALSE ||
199*7c478bd9Sstevel@tonic-gate 			    (wdval = syslook(arg->argval,
200*7c478bd9Sstevel@tonic-gate 					reserved, no_reserved)) == 0) {
201*7c478bd9Sstevel@tonic-gate 				wdval = 0;
202*7c478bd9Sstevel@tonic-gate 			}
203*7c478bd9Sstevel@tonic-gate 			/* set arg for reserved words too */
204*7c478bd9Sstevel@tonic-gate 			wdarg = arg;
205*7c478bd9Sstevel@tonic-gate 		}
206*7c478bd9Sstevel@tonic-gate 	}else if (dipchar(c)){
207*7c478bd9Sstevel@tonic-gate 		if ((d = nextwc()) == c)
208*7c478bd9Sstevel@tonic-gate 		{
209*7c478bd9Sstevel@tonic-gate 			wdval = c | SYMREP;
210*7c478bd9Sstevel@tonic-gate 			if (c == '<')
211*7c478bd9Sstevel@tonic-gate 			{
212*7c478bd9Sstevel@tonic-gate 				if ((d = nextwc()) == '-')
213*7c478bd9Sstevel@tonic-gate 					wdnum |= IOSTRIP;
214*7c478bd9Sstevel@tonic-gate 				else
215*7c478bd9Sstevel@tonic-gate 					peekn = d | MARK;
216*7c478bd9Sstevel@tonic-gate 			}
217*7c478bd9Sstevel@tonic-gate 		}
218*7c478bd9Sstevel@tonic-gate 		else
219*7c478bd9Sstevel@tonic-gate 		{
220*7c478bd9Sstevel@tonic-gate 			peekn = d | MARK;
221*7c478bd9Sstevel@tonic-gate 			wdval = c;
222*7c478bd9Sstevel@tonic-gate 		}
223*7c478bd9Sstevel@tonic-gate 	}
224*7c478bd9Sstevel@tonic-gate 	else
225*7c478bd9Sstevel@tonic-gate 	{
226*7c478bd9Sstevel@tonic-gate 		if ((wdval = c) == EOF)
227*7c478bd9Sstevel@tonic-gate 			wdval = EOFSYM;
228*7c478bd9Sstevel@tonic-gate 		if (iopend && eolchar(c))
229*7c478bd9Sstevel@tonic-gate 		{
230*7c478bd9Sstevel@tonic-gate 			struct ionod *tmp_iopend;
231*7c478bd9Sstevel@tonic-gate 			tmp_iopend = iopend;
232*7c478bd9Sstevel@tonic-gate 			iopend = 0;
233*7c478bd9Sstevel@tonic-gate 			copy(tmp_iopend);
234*7c478bd9Sstevel@tonic-gate 		}
235*7c478bd9Sstevel@tonic-gate 	}
236*7c478bd9Sstevel@tonic-gate 	reserv = FALSE;
237*7c478bd9Sstevel@tonic-gate 	return (wdval);
238*7c478bd9Sstevel@tonic-gate }
239*7c478bd9Sstevel@tonic-gate 
240*7c478bd9Sstevel@tonic-gate unsigned int skipwc()
241*7c478bd9Sstevel@tonic-gate {
242*7c478bd9Sstevel@tonic-gate 	register unsigned int c;
243*7c478bd9Sstevel@tonic-gate 
244*7c478bd9Sstevel@tonic-gate 	while (c = nextwc(), space(c))
245*7c478bd9Sstevel@tonic-gate 		;
246*7c478bd9Sstevel@tonic-gate 	return (c);
247*7c478bd9Sstevel@tonic-gate }
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate unsigned int nextwc()
250*7c478bd9Sstevel@tonic-gate {
251*7c478bd9Sstevel@tonic-gate 	register unsigned int	c, d;
252*7c478bd9Sstevel@tonic-gate 
253*7c478bd9Sstevel@tonic-gate retry:
254*7c478bd9Sstevel@tonic-gate 	if ((d = readwc()) == ESCAPE) {
255*7c478bd9Sstevel@tonic-gate 		if ((c = readwc()) == NL) {
256*7c478bd9Sstevel@tonic-gate 			chkpr();
257*7c478bd9Sstevel@tonic-gate 			goto retry;
258*7c478bd9Sstevel@tonic-gate 		}
259*7c478bd9Sstevel@tonic-gate 		peekc = c | MARK;
260*7c478bd9Sstevel@tonic-gate 	}
261*7c478bd9Sstevel@tonic-gate 	return (d);
262*7c478bd9Sstevel@tonic-gate }
263*7c478bd9Sstevel@tonic-gate 
264*7c478bd9Sstevel@tonic-gate unsigned char *readw(d)
265*7c478bd9Sstevel@tonic-gate wchar_t	d;
266*7c478bd9Sstevel@tonic-gate {
267*7c478bd9Sstevel@tonic-gate 	static unsigned char c[MULTI_BYTE_MAX + 1];
268*7c478bd9Sstevel@tonic-gate 	int length;
269*7c478bd9Sstevel@tonic-gate 	wchar_t l;
270*7c478bd9Sstevel@tonic-gate 	if (isascii(d)) {
271*7c478bd9Sstevel@tonic-gate 		c[0] = d;
272*7c478bd9Sstevel@tonic-gate 		c[1] = '\0';
273*7c478bd9Sstevel@tonic-gate 		return (c);
274*7c478bd9Sstevel@tonic-gate 	}
275*7c478bd9Sstevel@tonic-gate 
276*7c478bd9Sstevel@tonic-gate 	length = wctomb((char *)c, d);
277*7c478bd9Sstevel@tonic-gate 	if (length <= 0) {
278*7c478bd9Sstevel@tonic-gate 		c[0] = (unsigned char)d;
279*7c478bd9Sstevel@tonic-gate 		length = 1;
280*7c478bd9Sstevel@tonic-gate 	}
281*7c478bd9Sstevel@tonic-gate 	c[length] = '\0';
282*7c478bd9Sstevel@tonic-gate 	return (c);
283*7c478bd9Sstevel@tonic-gate }
284*7c478bd9Sstevel@tonic-gate 
285*7c478bd9Sstevel@tonic-gate unsigned int
286*7c478bd9Sstevel@tonic-gate readwc()
287*7c478bd9Sstevel@tonic-gate {
288*7c478bd9Sstevel@tonic-gate 	wchar_t	c;
289*7c478bd9Sstevel@tonic-gate 	int	len;
290*7c478bd9Sstevel@tonic-gate 	struct fileblk	*f;
291*7c478bd9Sstevel@tonic-gate 	int	mbmax = MB_CUR_MAX;
292*7c478bd9Sstevel@tonic-gate 	int	i, mlen;
293*7c478bd9Sstevel@tonic-gate 
294*7c478bd9Sstevel@tonic-gate 	if (peekn) {
295*7c478bd9Sstevel@tonic-gate 		c = peekn & 0x7fffffff;
296*7c478bd9Sstevel@tonic-gate 		peekn = 0;
297*7c478bd9Sstevel@tonic-gate 		return (c);
298*7c478bd9Sstevel@tonic-gate 	}
299*7c478bd9Sstevel@tonic-gate 	if (peekc) {
300*7c478bd9Sstevel@tonic-gate 		c = peekc & 0x7fffffff;
301*7c478bd9Sstevel@tonic-gate 		peekc = 0;
302*7c478bd9Sstevel@tonic-gate 		return (c);
303*7c478bd9Sstevel@tonic-gate 	}
304*7c478bd9Sstevel@tonic-gate 	f = standin;
305*7c478bd9Sstevel@tonic-gate 
306*7c478bd9Sstevel@tonic-gate retry:
307*7c478bd9Sstevel@tonic-gate 	if (f->fend > f->fnxt) {
308*7c478bd9Sstevel@tonic-gate 		/*
309*7c478bd9Sstevel@tonic-gate 		 * something in buffer
310*7c478bd9Sstevel@tonic-gate 		 */
311*7c478bd9Sstevel@tonic-gate 		if (*f->fnxt == 0) {
312*7c478bd9Sstevel@tonic-gate 			f->fnxt++;
313*7c478bd9Sstevel@tonic-gate 			f->nxtoff++;
314*7c478bd9Sstevel@tonic-gate 			if (f->feval == 0)
315*7c478bd9Sstevel@tonic-gate 				goto retry;	/* = c = readc(); */
316*7c478bd9Sstevel@tonic-gate 			if (estabf(*f->feval++))
317*7c478bd9Sstevel@tonic-gate 				c = EOF;
318*7c478bd9Sstevel@tonic-gate 			else
319*7c478bd9Sstevel@tonic-gate 				c = SPACE;
320*7c478bd9Sstevel@tonic-gate 			if (flags & readpr && standin->fstak == 0)
321*7c478bd9Sstevel@tonic-gate 				prc(c);
322*7c478bd9Sstevel@tonic-gate 			if (c == NL)
323*7c478bd9Sstevel@tonic-gate 				f->flin++;
324*7c478bd9Sstevel@tonic-gate 			return (c);
325*7c478bd9Sstevel@tonic-gate 		}
326*7c478bd9Sstevel@tonic-gate 
327*7c478bd9Sstevel@tonic-gate 		if (isascii(c = (unsigned char)*f->fnxt)) {
328*7c478bd9Sstevel@tonic-gate 			f->fnxt++;
329*7c478bd9Sstevel@tonic-gate 			f->nxtoff++;
330*7c478bd9Sstevel@tonic-gate 			if (flags & readpr && standin->fstak == 0)
331*7c478bd9Sstevel@tonic-gate 				prc(c);
332*7c478bd9Sstevel@tonic-gate 			if (c == NL)
333*7c478bd9Sstevel@tonic-gate 				f->flin++;
334*7c478bd9Sstevel@tonic-gate 			return (c);
335*7c478bd9Sstevel@tonic-gate 		}
336*7c478bd9Sstevel@tonic-gate 
337*7c478bd9Sstevel@tonic-gate 		for (i = 1; i <= mbmax; i++) {
338*7c478bd9Sstevel@tonic-gate 			int	rest;
339*7c478bd9Sstevel@tonic-gate 			if ((rest = f->fend - f->fnxt) < i) {
340*7c478bd9Sstevel@tonic-gate 				/*
341*7c478bd9Sstevel@tonic-gate 				 * not enough bytes available
342*7c478bd9Sstevel@tonic-gate 				 * f->fsiz could be BUFFERSIZE or 1
343*7c478bd9Sstevel@tonic-gate 				 * since mbmax is enough smaller than BUFFERSIZE,
344*7c478bd9Sstevel@tonic-gate 				 * this loop won't overrun the f->fbuf buffer.
345*7c478bd9Sstevel@tonic-gate 				 */
346*7c478bd9Sstevel@tonic-gate 				len = readb(f,
347*7c478bd9Sstevel@tonic-gate 					(f->fsiz == 1) ? 1 : (f->fsiz - rest),
348*7c478bd9Sstevel@tonic-gate 					rest);
349*7c478bd9Sstevel@tonic-gate 				if (len == 0)
350*7c478bd9Sstevel@tonic-gate 					break;
351*7c478bd9Sstevel@tonic-gate 			}
352*7c478bd9Sstevel@tonic-gate 			mlen = mbtowc(&c, (char *)f->fnxt, i);
353*7c478bd9Sstevel@tonic-gate 			if (mlen > 0)
354*7c478bd9Sstevel@tonic-gate 				break;
355*7c478bd9Sstevel@tonic-gate 		}
356*7c478bd9Sstevel@tonic-gate 
357*7c478bd9Sstevel@tonic-gate 		if (i > mbmax) {
358*7c478bd9Sstevel@tonic-gate 			/*
359*7c478bd9Sstevel@tonic-gate 			 * enough bytes available but cannot be converted to
360*7c478bd9Sstevel@tonic-gate 			 * a valid wchar.
361*7c478bd9Sstevel@tonic-gate 			 */
362*7c478bd9Sstevel@tonic-gate 			c = (unsigned char)*f->fnxt;
363*7c478bd9Sstevel@tonic-gate 			mlen = 1;
364*7c478bd9Sstevel@tonic-gate 		}
365*7c478bd9Sstevel@tonic-gate 
366*7c478bd9Sstevel@tonic-gate 		f->fnxt += mlen;
367*7c478bd9Sstevel@tonic-gate 		f->nxtoff += mlen;
368*7c478bd9Sstevel@tonic-gate 		if (flags & readpr && standin->fstak == 0)
369*7c478bd9Sstevel@tonic-gate 			prwc(c);
370*7c478bd9Sstevel@tonic-gate 		if (c == NL)
371*7c478bd9Sstevel@tonic-gate 			f->flin++;
372*7c478bd9Sstevel@tonic-gate 		return (c);
373*7c478bd9Sstevel@tonic-gate 	}
374*7c478bd9Sstevel@tonic-gate 
375*7c478bd9Sstevel@tonic-gate 	if (f->feof || f->fdes < 0){
376*7c478bd9Sstevel@tonic-gate 		c = EOF;
377*7c478bd9Sstevel@tonic-gate 		f->feof++;
378*7c478bd9Sstevel@tonic-gate 		return (c);
379*7c478bd9Sstevel@tonic-gate 	}
380*7c478bd9Sstevel@tonic-gate 
381*7c478bd9Sstevel@tonic-gate 	if (readb(f, f->fsiz, 0) <= 0){
382*7c478bd9Sstevel@tonic-gate 		if (f->fdes != input || !isatty(input)) {
383*7c478bd9Sstevel@tonic-gate 			close(f->fdes);
384*7c478bd9Sstevel@tonic-gate 			f->fdes = -1;
385*7c478bd9Sstevel@tonic-gate 		}
386*7c478bd9Sstevel@tonic-gate 		f->feof++;
387*7c478bd9Sstevel@tonic-gate 		c = EOF;
388*7c478bd9Sstevel@tonic-gate 		return (c);
389*7c478bd9Sstevel@tonic-gate 	}
390*7c478bd9Sstevel@tonic-gate 	goto retry;
391*7c478bd9Sstevel@tonic-gate }
392*7c478bd9Sstevel@tonic-gate 
393*7c478bd9Sstevel@tonic-gate static int
394*7c478bd9Sstevel@tonic-gate readb(struct fileblk *f, int toread, int rest)
395*7c478bd9Sstevel@tonic-gate {
396*7c478bd9Sstevel@tonic-gate 	int	len;
397*7c478bd9Sstevel@tonic-gate 	int	fflags;
398*7c478bd9Sstevel@tonic-gate 
399*7c478bd9Sstevel@tonic-gate 	if (rest) {
400*7c478bd9Sstevel@tonic-gate 		/*
401*7c478bd9Sstevel@tonic-gate 		 * copies the remaining 'rest' bytes from f->fnxt
402*7c478bd9Sstevel@tonic-gate 		 * to f->fbuf
403*7c478bd9Sstevel@tonic-gate 		 */
404*7c478bd9Sstevel@tonic-gate 		(void) memcpy(f->fbuf, f->fnxt, rest);
405*7c478bd9Sstevel@tonic-gate 		f->fnxt = f->fbuf;
406*7c478bd9Sstevel@tonic-gate 		f->fend = f->fnxt + rest;
407*7c478bd9Sstevel@tonic-gate 		f->nxtoff = 0;
408*7c478bd9Sstevel@tonic-gate 		f->endoff = rest;
409*7c478bd9Sstevel@tonic-gate 		if (f->fbuf[rest - 1] == '\n') {
410*7c478bd9Sstevel@tonic-gate 			/*
411*7c478bd9Sstevel@tonic-gate 			 * if '\n' found, it should be
412*7c478bd9Sstevel@tonic-gate 			 * a bondary of multibyte char.
413*7c478bd9Sstevel@tonic-gate 			 */
414*7c478bd9Sstevel@tonic-gate 			return (rest);
415*7c478bd9Sstevel@tonic-gate 		}
416*7c478bd9Sstevel@tonic-gate 	}
417*7c478bd9Sstevel@tonic-gate 
418*7c478bd9Sstevel@tonic-gate retry:
419*7c478bd9Sstevel@tonic-gate 	do {
420*7c478bd9Sstevel@tonic-gate 		if (trapnote & SIGSET) {
421*7c478bd9Sstevel@tonic-gate 			newline();
422*7c478bd9Sstevel@tonic-gate 			sigchk();
423*7c478bd9Sstevel@tonic-gate 		} else if ((trapnote & TRAPSET) && (rwait > 0)) {
424*7c478bd9Sstevel@tonic-gate 			newline();
425*7c478bd9Sstevel@tonic-gate 			chktrap();
426*7c478bd9Sstevel@tonic-gate 			clearup();
427*7c478bd9Sstevel@tonic-gate 		}
428*7c478bd9Sstevel@tonic-gate 	} while ((len = read(f->fdes, f->fbuf + rest, toread)) < 0 && trapnote);
429*7c478bd9Sstevel@tonic-gate 	/*
430*7c478bd9Sstevel@tonic-gate 	 * if child sets O_NDELAY or O_NONBLOCK on stdin
431*7c478bd9Sstevel@tonic-gate 	 * and exited then turn the modes off and retry
432*7c478bd9Sstevel@tonic-gate 	 */
433*7c478bd9Sstevel@tonic-gate 	if (len == 0) {
434*7c478bd9Sstevel@tonic-gate 		if (((flags & intflg) ||
435*7c478bd9Sstevel@tonic-gate 		    ((flags & oneflg) == 0 && isatty(input) &&
436*7c478bd9Sstevel@tonic-gate 		    (flags & stdflg))) &&
437*7c478bd9Sstevel@tonic-gate 		    ((fflags = fcntl(f->fdes, F_GETFL, 0)) & O_NDELAY)) {
438*7c478bd9Sstevel@tonic-gate 			fflags &= ~O_NDELAY;
439*7c478bd9Sstevel@tonic-gate 			fcntl(f->fdes, F_SETFL, fflags);
440*7c478bd9Sstevel@tonic-gate 			goto retry;
441*7c478bd9Sstevel@tonic-gate 		}
442*7c478bd9Sstevel@tonic-gate 	} else if (len < 0) {
443*7c478bd9Sstevel@tonic-gate 		if (errno == EAGAIN) {
444*7c478bd9Sstevel@tonic-gate 			fflags = fcntl(f->fdes, F_GETFL, 0);
445*7c478bd9Sstevel@tonic-gate 			fflags &= ~O_NONBLOCK;
446*7c478bd9Sstevel@tonic-gate 			fcntl(f->fdes, F_SETFL, fflags);
447*7c478bd9Sstevel@tonic-gate 			goto retry;
448*7c478bd9Sstevel@tonic-gate 		}
449*7c478bd9Sstevel@tonic-gate 		len = 0;
450*7c478bd9Sstevel@tonic-gate 	}
451*7c478bd9Sstevel@tonic-gate 	f->fnxt = f->fbuf;
452*7c478bd9Sstevel@tonic-gate 	f->fend = f->fnxt + (len + rest);
453*7c478bd9Sstevel@tonic-gate 	f->nxtoff = 0;
454*7c478bd9Sstevel@tonic-gate 	f->endoff = len + rest;
455*7c478bd9Sstevel@tonic-gate 	return (len + rest);
456*7c478bd9Sstevel@tonic-gate }
457