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