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