xref: /illumos-gate/usr/src/cmd/sh/macro.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
533bc07fdScraigm  * Common Development and Distribution License (the "License").
633bc07fdScraigm  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
2233bc07fdScraigm  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * UNIX shell
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include	"defs.h"
347c478bd9Sstevel@tonic-gate #include	"sym.h"
357c478bd9Sstevel@tonic-gate #include	<wait.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate static unsigned char	quote;	/* used locally */
387c478bd9Sstevel@tonic-gate static unsigned char	quoted;	/* used locally */
397c478bd9Sstevel@tonic-gate static int getch();
40965005c8Schin static void comsubst(int);
41965005c8Schin static void flush(int);
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate static void
copyto(unsigned char endch,int trimflag)44965005c8Schin copyto(unsigned char endch, int trimflag)
45965005c8Schin /* trimflag - flag to check if argument will be trimmed */
467c478bd9Sstevel@tonic-gate {
47965005c8Schin 	unsigned int	c;
48965005c8Schin 	unsigned int 	d;
49965005c8Schin 	unsigned char *pc;
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate 	while ((c = getch(endch, trimflag)) != endch && c)
527c478bd9Sstevel@tonic-gate 		if (quote) {
537c478bd9Sstevel@tonic-gate 			if(c == '\\') { /* don't interpret next character */
547c478bd9Sstevel@tonic-gate 				if (staktop >= brkend)
557c478bd9Sstevel@tonic-gate 					growstak(staktop);
567c478bd9Sstevel@tonic-gate 				pushstak(c);
577c478bd9Sstevel@tonic-gate 				d = readwc();
587c478bd9Sstevel@tonic-gate 				if(!escchar(d)) { /* both \ and following
597c478bd9Sstevel@tonic-gate 						     character are quoted if next
607c478bd9Sstevel@tonic-gate 						     character is not $, `, ", or \*/
617c478bd9Sstevel@tonic-gate 					if (staktop >= brkend)
627c478bd9Sstevel@tonic-gate 						growstak(staktop);
63*2a8bcb4eSToomas Soome 					pushstak('\\');
647c478bd9Sstevel@tonic-gate 					if (staktop >= brkend)
657c478bd9Sstevel@tonic-gate 						growstak(staktop);
667c478bd9Sstevel@tonic-gate 					pushstak('\\');
67*2a8bcb4eSToomas Soome 					pc = readw(d);
687c478bd9Sstevel@tonic-gate 					/* push entire multibyte char */
697c478bd9Sstevel@tonic-gate 					while(*pc) {
707c478bd9Sstevel@tonic-gate 						if (staktop >= brkend)
717c478bd9Sstevel@tonic-gate 							growstak(staktop);
727c478bd9Sstevel@tonic-gate 						pushstak(*pc++);
737c478bd9Sstevel@tonic-gate 					}
747c478bd9Sstevel@tonic-gate 				} else {
757c478bd9Sstevel@tonic-gate 					pc = readw(d);
767c478bd9Sstevel@tonic-gate 					/* d might be NULL */
777c478bd9Sstevel@tonic-gate 					/* Evenif d is NULL, we have to save it */
787c478bd9Sstevel@tonic-gate 					if (*pc) {
797c478bd9Sstevel@tonic-gate 						while (*pc) {
807c478bd9Sstevel@tonic-gate 							if (staktop >= brkend)
817c478bd9Sstevel@tonic-gate 								growstak(staktop);
827c478bd9Sstevel@tonic-gate 							pushstak(*pc++);
837c478bd9Sstevel@tonic-gate 						}
847c478bd9Sstevel@tonic-gate 					} else {
857c478bd9Sstevel@tonic-gate 						if (staktop >= brkend)
867c478bd9Sstevel@tonic-gate 							growstak(staktop);
877c478bd9Sstevel@tonic-gate 						pushstak(*pc);
887c478bd9Sstevel@tonic-gate 					}
897c478bd9Sstevel@tonic-gate 				}
907c478bd9Sstevel@tonic-gate 			} else { /* push escapes onto stack to quote characters */
917c478bd9Sstevel@tonic-gate 				pc = readw(c);
927c478bd9Sstevel@tonic-gate 				if (staktop >= brkend)
93*2a8bcb4eSToomas Soome 					growstak(staktop);
947c478bd9Sstevel@tonic-gate 				pushstak('\\');
957c478bd9Sstevel@tonic-gate 				while(*pc) {
967c478bd9Sstevel@tonic-gate 					if (staktop >= brkend)
977c478bd9Sstevel@tonic-gate 						growstak(staktop);
987c478bd9Sstevel@tonic-gate 					pushstak(*pc++);
997c478bd9Sstevel@tonic-gate 				}
1007c478bd9Sstevel@tonic-gate 			}
1017c478bd9Sstevel@tonic-gate 		} else if(c == '\\') {
1027c478bd9Sstevel@tonic-gate 			c = readwc(); /* get character to be escaped */
1037c478bd9Sstevel@tonic-gate 			if (staktop >= brkend)
104*2a8bcb4eSToomas Soome 				growstak(staktop);
1057c478bd9Sstevel@tonic-gate 			pushstak('\\');
1067c478bd9Sstevel@tonic-gate 			pc = readw(c);
1077c478bd9Sstevel@tonic-gate 			/* c might be NULL */
1087c478bd9Sstevel@tonic-gate 			/* Evenif c is NULL, we have to save it */
1097c478bd9Sstevel@tonic-gate 			if (*pc) {
1107c478bd9Sstevel@tonic-gate 				while (*pc) {
1117c478bd9Sstevel@tonic-gate 					if (staktop >= brkend)
112*2a8bcb4eSToomas Soome 						growstak(staktop);
1137c478bd9Sstevel@tonic-gate 					pushstak(*pc++);
1147c478bd9Sstevel@tonic-gate 				}
1157c478bd9Sstevel@tonic-gate 			} else {
1167c478bd9Sstevel@tonic-gate 				if (staktop >= brkend)
1177c478bd9Sstevel@tonic-gate 					growstak(staktop);
1187c478bd9Sstevel@tonic-gate 				pushstak(*pc);
1197c478bd9Sstevel@tonic-gate 			}
1207c478bd9Sstevel@tonic-gate 		} else {
1217c478bd9Sstevel@tonic-gate 			pc = readw(c);
1227c478bd9Sstevel@tonic-gate 			while (*pc) {
1237c478bd9Sstevel@tonic-gate 				if (staktop >= brkend)
124*2a8bcb4eSToomas Soome 					growstak(staktop);
1257c478bd9Sstevel@tonic-gate 				pushstak(*pc++);
1267c478bd9Sstevel@tonic-gate 			}
1277c478bd9Sstevel@tonic-gate 		}
1287c478bd9Sstevel@tonic-gate 	if (staktop >= brkend)
129*2a8bcb4eSToomas Soome 			growstak(staktop);
1307c478bd9Sstevel@tonic-gate 	zerostak();
1317c478bd9Sstevel@tonic-gate 	if (c != endch)
1327c478bd9Sstevel@tonic-gate 		error(badsub);
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate 
135965005c8Schin static void
skipto(unsigned char endch)136965005c8Schin skipto(unsigned char endch)
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate 	/*
1397c478bd9Sstevel@tonic-gate 	 * skip chars up to }
1407c478bd9Sstevel@tonic-gate 	 */
141965005c8Schin 	unsigned int	c;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	while ((c = readwc()) && c != endch)
1447c478bd9Sstevel@tonic-gate 	{
1457c478bd9Sstevel@tonic-gate 		switch (c)
1467c478bd9Sstevel@tonic-gate 		{
1477c478bd9Sstevel@tonic-gate 		case SQUOTE:
1487c478bd9Sstevel@tonic-gate 			skipto(SQUOTE);
1497c478bd9Sstevel@tonic-gate 			break;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 		case DQUOTE:
1527c478bd9Sstevel@tonic-gate 			skipto(DQUOTE);
1537c478bd9Sstevel@tonic-gate 			break;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 		case DOLLAR:
1567c478bd9Sstevel@tonic-gate 			if (readwc() == BRACE)
1577c478bd9Sstevel@tonic-gate 				skipto('}');
1587c478bd9Sstevel@tonic-gate 		}
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate 	if (c != endch)
1617c478bd9Sstevel@tonic-gate 		error(badsub);
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate static
getch(endch,trimflag)1657c478bd9Sstevel@tonic-gate int getch(endch, trimflag)
1667c478bd9Sstevel@tonic-gate unsigned char	endch;
1677c478bd9Sstevel@tonic-gate int trimflag; /* flag to check if an argument is going to be trimmed, here document
1687c478bd9Sstevel@tonic-gate 		 output is never trimmed
1697c478bd9Sstevel@tonic-gate 	 */
1707c478bd9Sstevel@tonic-gate {
171965005c8Schin 	unsigned int	d;
172*2a8bcb4eSToomas Soome 	int atflag;  /* flag to check if $@ has already been seen within double
1737c478bd9Sstevel@tonic-gate 		        quotes */
1747c478bd9Sstevel@tonic-gate retry:
1757c478bd9Sstevel@tonic-gate 	d = readwc();
1767c478bd9Sstevel@tonic-gate 	if (!subchar(d))
1777c478bd9Sstevel@tonic-gate 		return(d);
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	if (d == DOLLAR)
1807c478bd9Sstevel@tonic-gate 	{
1817c478bd9Sstevel@tonic-gate 		unsigned int c;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 		if ((c = readwc(), dolchar(c)))
1847c478bd9Sstevel@tonic-gate 		{
1857c478bd9Sstevel@tonic-gate 			struct namnod *n = (struct namnod *)NIL;
1867c478bd9Sstevel@tonic-gate 			int		dolg = 0;
1877c478bd9Sstevel@tonic-gate 			BOOL		bra;
1887c478bd9Sstevel@tonic-gate 			BOOL		nulflg;
189965005c8Schin 			unsigned char	*argp, *v;
1907c478bd9Sstevel@tonic-gate 			unsigned char		idb[2];
1917c478bd9Sstevel@tonic-gate 			unsigned char		*id = idb;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 			if (bra = (c == BRACE))
1947c478bd9Sstevel@tonic-gate 				c = readwc();
1957c478bd9Sstevel@tonic-gate 			if (letter(c))
1967c478bd9Sstevel@tonic-gate 			{
1977c478bd9Sstevel@tonic-gate 				argp = (unsigned char *)relstak();
1987c478bd9Sstevel@tonic-gate 				while (alphanum(c))
1997c478bd9Sstevel@tonic-gate 				{
2007c478bd9Sstevel@tonic-gate 					if (staktop >= brkend)
2017c478bd9Sstevel@tonic-gate 						growstak(staktop);
2027c478bd9Sstevel@tonic-gate 					pushstak(c);
2037c478bd9Sstevel@tonic-gate 					c = readwc();
2047c478bd9Sstevel@tonic-gate 				}
2057c478bd9Sstevel@tonic-gate 				if (staktop >= brkend)
2067c478bd9Sstevel@tonic-gate 					growstak(staktop);
2077c478bd9Sstevel@tonic-gate 				zerostak();
2087c478bd9Sstevel@tonic-gate 				n = lookup(absstak(argp));
2097c478bd9Sstevel@tonic-gate 				setstak(argp);
2107c478bd9Sstevel@tonic-gate 				if (n->namflg & N_FUNCTN)
2117c478bd9Sstevel@tonic-gate 					error(badsub);
2127c478bd9Sstevel@tonic-gate 				v = n->namval;
2137c478bd9Sstevel@tonic-gate 				id = (unsigned char *)n->namid;
2147c478bd9Sstevel@tonic-gate 				peekc = c | MARK;
2157c478bd9Sstevel@tonic-gate 			}
2167c478bd9Sstevel@tonic-gate 			else if (digchar(c))
2177c478bd9Sstevel@tonic-gate 			{
2187c478bd9Sstevel@tonic-gate 				*id = c;
2197c478bd9Sstevel@tonic-gate 				idb[1] = 0;
2207c478bd9Sstevel@tonic-gate 				if (astchar(c))
2217c478bd9Sstevel@tonic-gate 				{
2227c478bd9Sstevel@tonic-gate 					if(c == '@' && !atflag && quote) {
2237c478bd9Sstevel@tonic-gate 						quoted--;
2247c478bd9Sstevel@tonic-gate 						atflag = 1;
2257c478bd9Sstevel@tonic-gate 					}
2267c478bd9Sstevel@tonic-gate 					dolg = 1;
2277c478bd9Sstevel@tonic-gate 					c = '1';
2287c478bd9Sstevel@tonic-gate 				}
2297c478bd9Sstevel@tonic-gate 				c -= '0';
2307c478bd9Sstevel@tonic-gate 				v = ((c == 0) ? cmdadr : ((int)c <= dolc) ? dolv[c] : (unsigned char *)(dolg = 0));
2317c478bd9Sstevel@tonic-gate 			}
2327c478bd9Sstevel@tonic-gate 			else if (c == '$')
2337c478bd9Sstevel@tonic-gate 				v = pidadr;
2347c478bd9Sstevel@tonic-gate 			else if (c == '!')
2357c478bd9Sstevel@tonic-gate 				v = pcsadr;
2367c478bd9Sstevel@tonic-gate 			else if (c == '#')
2377c478bd9Sstevel@tonic-gate 			{
2387c478bd9Sstevel@tonic-gate 				itos(dolc);
2397c478bd9Sstevel@tonic-gate 				v = numbuf;
2407c478bd9Sstevel@tonic-gate 			}
2417c478bd9Sstevel@tonic-gate 			else if (c == '?')
2427c478bd9Sstevel@tonic-gate 			{
2437c478bd9Sstevel@tonic-gate 				itos(retval);
2447c478bd9Sstevel@tonic-gate 				v = numbuf;
2457c478bd9Sstevel@tonic-gate 			}
2467c478bd9Sstevel@tonic-gate 			else if (c == '-')
2477c478bd9Sstevel@tonic-gate 				v = flagadr;
2487c478bd9Sstevel@tonic-gate 			else if (bra)
2497c478bd9Sstevel@tonic-gate 				error(badsub);
2507c478bd9Sstevel@tonic-gate 			else
2517c478bd9Sstevel@tonic-gate 				goto retry;
2527c478bd9Sstevel@tonic-gate 			c = readwc();
2537c478bd9Sstevel@tonic-gate 			if (c == ':' && bra)	/* null and unset fix */
2547c478bd9Sstevel@tonic-gate 			{
2557c478bd9Sstevel@tonic-gate 				nulflg = 1;
2567c478bd9Sstevel@tonic-gate 				c = readwc();
2577c478bd9Sstevel@tonic-gate 			}
2587c478bd9Sstevel@tonic-gate 			else
2597c478bd9Sstevel@tonic-gate 				nulflg = 0;
2607c478bd9Sstevel@tonic-gate 			if (!defchar(c) && bra)
2617c478bd9Sstevel@tonic-gate 				error(badsub);
2627c478bd9Sstevel@tonic-gate 			argp = 0;
2637c478bd9Sstevel@tonic-gate 			if (bra)
2647c478bd9Sstevel@tonic-gate 			{
2657c478bd9Sstevel@tonic-gate 				if (c != '}')
2667c478bd9Sstevel@tonic-gate 				{
2677c478bd9Sstevel@tonic-gate 					argp = (unsigned char *)relstak();
2687c478bd9Sstevel@tonic-gate 					if ((v == 0 || (nulflg && *v == 0)) ^ (setchar(c)))
2697c478bd9Sstevel@tonic-gate 						copyto('}', trimflag);
2707c478bd9Sstevel@tonic-gate 					else
2717c478bd9Sstevel@tonic-gate 						skipto('}');
2727c478bd9Sstevel@tonic-gate 					argp = absstak(argp);
2737c478bd9Sstevel@tonic-gate 				}
2747c478bd9Sstevel@tonic-gate 			}
2757c478bd9Sstevel@tonic-gate 			else
2767c478bd9Sstevel@tonic-gate 			{
2777c478bd9Sstevel@tonic-gate 				peekc = c | MARK;
2787c478bd9Sstevel@tonic-gate 				c = 0;
2797c478bd9Sstevel@tonic-gate 			}
2807c478bd9Sstevel@tonic-gate 			if (v && (!nulflg || *v))
2817c478bd9Sstevel@tonic-gate 			{
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 				if (c != '+')
2847c478bd9Sstevel@tonic-gate 				{
2857c478bd9Sstevel@tonic-gate 					for (;;)
2867c478bd9Sstevel@tonic-gate 					{
2877c478bd9Sstevel@tonic-gate 						if (*v == 0 && quote) {
2887c478bd9Sstevel@tonic-gate 							if (staktop >= brkend)
2897c478bd9Sstevel@tonic-gate 								growstak(staktop);
2907c478bd9Sstevel@tonic-gate 							pushstak('\\');
2917c478bd9Sstevel@tonic-gate 							if (staktop >= brkend)
2927c478bd9Sstevel@tonic-gate 								growstak(staktop);
2937c478bd9Sstevel@tonic-gate 							pushstak('\0');
2947c478bd9Sstevel@tonic-gate 						} else {
2957c478bd9Sstevel@tonic-gate 							while (c = *v) {
2967c478bd9Sstevel@tonic-gate 								wchar_t 	wc;
297965005c8Schin 								int 	length;
2987c478bd9Sstevel@tonic-gate 								if ((length = mbtowc(&wc, (char *)v, MB_LEN_MAX)) <= 0)
2997c478bd9Sstevel@tonic-gate 									length = 1;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 								if(quote || (c == '\\' && trimflag)) {
3027c478bd9Sstevel@tonic-gate 									if (staktop >= brkend)
3037c478bd9Sstevel@tonic-gate 										growstak(staktop);
3047c478bd9Sstevel@tonic-gate 									pushstak('\\');
3057c478bd9Sstevel@tonic-gate 								}
3067c478bd9Sstevel@tonic-gate 								while(length-- > 0) {
3077c478bd9Sstevel@tonic-gate 									if (staktop >= brkend)
3087c478bd9Sstevel@tonic-gate 										growstak(staktop);
3097c478bd9Sstevel@tonic-gate 									pushstak(*v++);
3107c478bd9Sstevel@tonic-gate 								}
3117c478bd9Sstevel@tonic-gate 							}
3127c478bd9Sstevel@tonic-gate 						}
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 						if (dolg == 0 || (++dolg > dolc))
3157c478bd9Sstevel@tonic-gate 							break;
3167c478bd9Sstevel@tonic-gate 						else /* $* and $@ expansion */
3177c478bd9Sstevel@tonic-gate 						{
3187c478bd9Sstevel@tonic-gate 							v = dolv[dolg];
3197c478bd9Sstevel@tonic-gate 							if(*id == '*' && quote) {
3207c478bd9Sstevel@tonic-gate /* push quoted space so that " $* " will not be broken into separate arguments */
3217c478bd9Sstevel@tonic-gate 								if (staktop >= brkend)
3227c478bd9Sstevel@tonic-gate 									growstak(staktop);
3237c478bd9Sstevel@tonic-gate 								pushstak('\\');
3247c478bd9Sstevel@tonic-gate 							}
3257c478bd9Sstevel@tonic-gate 							if (staktop >= brkend)
3267c478bd9Sstevel@tonic-gate 								growstak(staktop);
3277c478bd9Sstevel@tonic-gate 							pushstak(' ');
3287c478bd9Sstevel@tonic-gate 						}
3297c478bd9Sstevel@tonic-gate 					}
3307c478bd9Sstevel@tonic-gate 				}
3317c478bd9Sstevel@tonic-gate 			}
3327c478bd9Sstevel@tonic-gate 			else if (argp)
3337c478bd9Sstevel@tonic-gate 			{
3347c478bd9Sstevel@tonic-gate 				if (c == '?') {
3357c478bd9Sstevel@tonic-gate 					if(trimflag)
3367c478bd9Sstevel@tonic-gate 						trim(argp);
337965005c8Schin 					failed(id, *argp ? (const char *)argp :
338965005c8Schin 					    badparam);
3397c478bd9Sstevel@tonic-gate 				}
3407c478bd9Sstevel@tonic-gate 				else if (c == '=')
3417c478bd9Sstevel@tonic-gate 				{
3427c478bd9Sstevel@tonic-gate 					if (n)
3437c478bd9Sstevel@tonic-gate 					{
3447c478bd9Sstevel@tonic-gate 						int strlngth = staktop - stakbot;
3457c478bd9Sstevel@tonic-gate 						unsigned char *savptr = fixstak();
3467c478bd9Sstevel@tonic-gate 						unsigned char *newargp;
3477c478bd9Sstevel@tonic-gate 					/*
3487c478bd9Sstevel@tonic-gate 					 * copy word onto stack, trim it, and then
349*2a8bcb4eSToomas Soome 					 * do assignment
3507c478bd9Sstevel@tonic-gate 					 */
3517c478bd9Sstevel@tonic-gate 						usestak();
3527c478bd9Sstevel@tonic-gate 						while(c = *argp) {
3537c478bd9Sstevel@tonic-gate 							wchar_t 	wc;
354965005c8Schin 							int 		len;
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 							if ((len = mbtowc(&wc, (char *)argp, MB_LEN_MAX)) <= 0)
3577c478bd9Sstevel@tonic-gate 								len = 1;
358*2a8bcb4eSToomas Soome 
3597c478bd9Sstevel@tonic-gate 							if(c == '\\' && trimflag) {
3607c478bd9Sstevel@tonic-gate 								argp++;
3617c478bd9Sstevel@tonic-gate 								if (*argp == 0) {
3627c478bd9Sstevel@tonic-gate 									argp++;
3637c478bd9Sstevel@tonic-gate 									continue;
3647c478bd9Sstevel@tonic-gate 								}
3657c478bd9Sstevel@tonic-gate 								if ((len = mbtowc(&wc, (char *)argp, MB_LEN_MAX)) <= 0)
3667c478bd9Sstevel@tonic-gate 									len = 1;
3677c478bd9Sstevel@tonic-gate 							}
3687c478bd9Sstevel@tonic-gate 							while(len-- > 0) {
3697c478bd9Sstevel@tonic-gate 								if (staktop >= brkend)
3707c478bd9Sstevel@tonic-gate 									growstak(staktop);
3717c478bd9Sstevel@tonic-gate 								pushstak(*argp++);
3727c478bd9Sstevel@tonic-gate 							}
3737c478bd9Sstevel@tonic-gate 						}
3747c478bd9Sstevel@tonic-gate 						newargp = fixstak();
3757c478bd9Sstevel@tonic-gate 						assign(n, newargp);
3767c478bd9Sstevel@tonic-gate 						tdystak(savptr);
37733bc07fdScraigm 						(void) memcpystak(stakbot, savptr, strlngth);
3787c478bd9Sstevel@tonic-gate 						staktop = stakbot + strlngth;
3797c478bd9Sstevel@tonic-gate 					}
3807c478bd9Sstevel@tonic-gate 					else
3817c478bd9Sstevel@tonic-gate 						error(badsub);
3827c478bd9Sstevel@tonic-gate 				}
3837c478bd9Sstevel@tonic-gate 			}
3847c478bd9Sstevel@tonic-gate 			else if (flags & setflg)
3857c478bd9Sstevel@tonic-gate 				failed(id, unset);
3867c478bd9Sstevel@tonic-gate 			goto retry;
3877c478bd9Sstevel@tonic-gate 		}
3887c478bd9Sstevel@tonic-gate 		else
3897c478bd9Sstevel@tonic-gate 			peekc = c | MARK;
3907c478bd9Sstevel@tonic-gate 	}
3917c478bd9Sstevel@tonic-gate 	else if (d == endch)
3927c478bd9Sstevel@tonic-gate 		return(d);
3937c478bd9Sstevel@tonic-gate 	else if (d == SQUOTE)
3947c478bd9Sstevel@tonic-gate 	{
3957c478bd9Sstevel@tonic-gate 		comsubst(trimflag);
3967c478bd9Sstevel@tonic-gate 		goto retry;
3977c478bd9Sstevel@tonic-gate 	}
3987c478bd9Sstevel@tonic-gate 	else if (d == DQUOTE && trimflag)
3997c478bd9Sstevel@tonic-gate 	{
4007c478bd9Sstevel@tonic-gate 		if(!quote) {
4017c478bd9Sstevel@tonic-gate 			atflag = 0;
4027c478bd9Sstevel@tonic-gate 			quoted++;
4037c478bd9Sstevel@tonic-gate 		}
4047c478bd9Sstevel@tonic-gate 		quote ^= QUOTE;
4057c478bd9Sstevel@tonic-gate 		goto retry;
4067c478bd9Sstevel@tonic-gate 	}
4077c478bd9Sstevel@tonic-gate 	return(d);
4087c478bd9Sstevel@tonic-gate }
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate unsigned char *
macro(as)4117c478bd9Sstevel@tonic-gate macro(as)
4127c478bd9Sstevel@tonic-gate unsigned char	*as;
4137c478bd9Sstevel@tonic-gate {
4147c478bd9Sstevel@tonic-gate 	/*
4157c478bd9Sstevel@tonic-gate 	 * Strip "" and do $ substitution
4167c478bd9Sstevel@tonic-gate 	 * Leaves result on top of stack
4177c478bd9Sstevel@tonic-gate 	 */
418965005c8Schin 	BOOL	savqu = quoted;
419965005c8Schin 	unsigned char	savq = quote;
4207c478bd9Sstevel@tonic-gate 	struct filehdr	fb;
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	push(&fb);
4237c478bd9Sstevel@tonic-gate 	estabf(as);
4247c478bd9Sstevel@tonic-gate 	usestak();
4257c478bd9Sstevel@tonic-gate 	quote = 0;
4267c478bd9Sstevel@tonic-gate 	quoted = 0;
4277c478bd9Sstevel@tonic-gate 	copyto(0, 1);
4287c478bd9Sstevel@tonic-gate 	pop();
4297c478bd9Sstevel@tonic-gate 	if (quoted && (stakbot == staktop)) {
4307c478bd9Sstevel@tonic-gate 		if (staktop >= brkend)
4317c478bd9Sstevel@tonic-gate 			growstak(staktop);
4327c478bd9Sstevel@tonic-gate 		pushstak('\\');
4337c478bd9Sstevel@tonic-gate 		if (staktop >= brkend)
4347c478bd9Sstevel@tonic-gate 			growstak(staktop);
4357c478bd9Sstevel@tonic-gate 		pushstak('\0');
4367c478bd9Sstevel@tonic-gate /*
4377c478bd9Sstevel@tonic-gate  * above is the fix for *'.c' bug
4387c478bd9Sstevel@tonic-gate  */
4397c478bd9Sstevel@tonic-gate 	}
4407c478bd9Sstevel@tonic-gate 	quote = savq;
4417c478bd9Sstevel@tonic-gate 	quoted = savqu;
4427c478bd9Sstevel@tonic-gate 	return(fixstak());
4437c478bd9Sstevel@tonic-gate }
4447c478bd9Sstevel@tonic-gate /* Save file descriptor for command substitution */
4457c478bd9Sstevel@tonic-gate int savpipe = -1;
4467c478bd9Sstevel@tonic-gate 
447965005c8Schin static void
comsubst(int trimflag)448965005c8Schin comsubst(int trimflag)
449965005c8Schin /* trimflag - used to determine if argument will later be trimmed */
4507c478bd9Sstevel@tonic-gate {
4517c478bd9Sstevel@tonic-gate 	/*
4527c478bd9Sstevel@tonic-gate 	 * command substn
4537c478bd9Sstevel@tonic-gate 	 */
4547c478bd9Sstevel@tonic-gate 	struct fileblk	cb;
455965005c8Schin 	unsigned int	d;
4567c478bd9Sstevel@tonic-gate 	int strlngth = staktop - stakbot;
457965005c8Schin 	unsigned char *oldstaktop;
4587c478bd9Sstevel@tonic-gate 	unsigned char *savptr = fixstak();
4597c478bd9Sstevel@tonic-gate 	unsigned char	*pc;
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	usestak();
4627c478bd9Sstevel@tonic-gate 	while ((d = readwc()) != SQUOTE && d) {
4637c478bd9Sstevel@tonic-gate 		if(d == '\\') {
4647c478bd9Sstevel@tonic-gate 			d = readwc();
4657c478bd9Sstevel@tonic-gate 			if(!escchar(d) || (d == '"' && !quote)) {
4667c478bd9Sstevel@tonic-gate 		/* trim quotes for `, \, or " if command substitution is within
4677c478bd9Sstevel@tonic-gate 		   double quotes */
4687c478bd9Sstevel@tonic-gate 				if (staktop >= brkend)
4697c478bd9Sstevel@tonic-gate 					growstak(staktop);
4707c478bd9Sstevel@tonic-gate 				pushstak('\\');
4717c478bd9Sstevel@tonic-gate 			}
4727c478bd9Sstevel@tonic-gate 		}
4737c478bd9Sstevel@tonic-gate 		pc = readw(d);
4747c478bd9Sstevel@tonic-gate 		/* d might be NULL */
4757c478bd9Sstevel@tonic-gate 		if (*pc) {
4767c478bd9Sstevel@tonic-gate 			while (*pc) {
4777c478bd9Sstevel@tonic-gate 				if (staktop >= brkend)
4787c478bd9Sstevel@tonic-gate 					growstak(staktop);
4797c478bd9Sstevel@tonic-gate 				pushstak(*pc++);
4807c478bd9Sstevel@tonic-gate 			}
4817c478bd9Sstevel@tonic-gate 		} else {
4827c478bd9Sstevel@tonic-gate 			if (staktop >= brkend)
4837c478bd9Sstevel@tonic-gate 				growstak(staktop);
4847c478bd9Sstevel@tonic-gate 			pushstak(*pc);
4857c478bd9Sstevel@tonic-gate 		}
4867c478bd9Sstevel@tonic-gate 	}
4877c478bd9Sstevel@tonic-gate 	{
488965005c8Schin 		unsigned char	*argc;
4897c478bd9Sstevel@tonic-gate 
490*2a8bcb4eSToomas Soome 		argc = fixstak();
4917c478bd9Sstevel@tonic-gate 		push(&cb);
4927c478bd9Sstevel@tonic-gate 		estabf(argc);  /* read from string */
4937c478bd9Sstevel@tonic-gate 	}
4947c478bd9Sstevel@tonic-gate 	{
495965005c8Schin 		struct trenod	*t;
4967c478bd9Sstevel@tonic-gate 		int		pv[2];
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 		/*
4997c478bd9Sstevel@tonic-gate 		 * this is done like this so that the pipe
5007c478bd9Sstevel@tonic-gate 		 * is open only when needed
5017c478bd9Sstevel@tonic-gate 		 */
5027c478bd9Sstevel@tonic-gate 	 	t = makefork(FPOU, cmd(EOFSYM, MTFLG | NLFLG ));
5037c478bd9Sstevel@tonic-gate 		chkpipe(pv);
5047c478bd9Sstevel@tonic-gate 		savpipe = pv[OTPIPE];
5057c478bd9Sstevel@tonic-gate 		initf(pv[INPIPE]); /* read from pipe */
5067c478bd9Sstevel@tonic-gate 		execute(t, XEC_NOSTOP, (int)(flags & errflg), 0, pv);
5077c478bd9Sstevel@tonic-gate 		close(pv[OTPIPE]);
5087c478bd9Sstevel@tonic-gate 		savpipe = -1;
5097c478bd9Sstevel@tonic-gate 	}
5107c478bd9Sstevel@tonic-gate 	tdystak(savptr);
51133bc07fdScraigm 	(void) memcpystak(stakbot, savptr, strlngth);
5127c478bd9Sstevel@tonic-gate 	oldstaktop = staktop = stakbot + strlngth;
5137c478bd9Sstevel@tonic-gate 	while (d = readwc()) {
5147c478bd9Sstevel@tonic-gate 		if(quote || (d == '\\' && trimflag)) {
515965005c8Schin 			unsigned char *rest;
516*2a8bcb4eSToomas Soome 			/* quote output from command subst. if within double
5177c478bd9Sstevel@tonic-gate 			   quotes or backslash part of output */
5187c478bd9Sstevel@tonic-gate 			rest = readw(d);
5197c478bd9Sstevel@tonic-gate 			if (staktop >= brkend)
5207c478bd9Sstevel@tonic-gate 				growstak(staktop);
5217c478bd9Sstevel@tonic-gate 			pushstak('\\');
5227c478bd9Sstevel@tonic-gate 			while(d = *rest++) {
5237c478bd9Sstevel@tonic-gate 			/* Pick up all of multibyte character */
5247c478bd9Sstevel@tonic-gate 				if (staktop >= brkend)
5257c478bd9Sstevel@tonic-gate 					growstak(staktop);
5267c478bd9Sstevel@tonic-gate 				pushstak(d);
5277c478bd9Sstevel@tonic-gate 			}
5287c478bd9Sstevel@tonic-gate 		}
5297c478bd9Sstevel@tonic-gate 		else {
5307c478bd9Sstevel@tonic-gate 			pc = readw(d);
5317c478bd9Sstevel@tonic-gate 			while (*pc) {
5327c478bd9Sstevel@tonic-gate 				if (staktop >= brkend)
5337c478bd9Sstevel@tonic-gate 					growstak(staktop);
5347c478bd9Sstevel@tonic-gate 				pushstak(*pc++);
5357c478bd9Sstevel@tonic-gate 			}
5367c478bd9Sstevel@tonic-gate 		}
5377c478bd9Sstevel@tonic-gate 	}
5387c478bd9Sstevel@tonic-gate 	{
5397c478bd9Sstevel@tonic-gate 		extern pid_t parent;
5407c478bd9Sstevel@tonic-gate 		int stat;
541965005c8Schin 		int rc;
5427c478bd9Sstevel@tonic-gate 		int	ret = 0;
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 		while ((ret = waitpid(parent,&stat,0)) != parent) {
5457c478bd9Sstevel@tonic-gate 			/* break out if waitpid(2) has failed */
5467c478bd9Sstevel@tonic-gate 			if (ret == -1)
5477c478bd9Sstevel@tonic-gate 				break;
5487c478bd9Sstevel@tonic-gate 		}
5497c478bd9Sstevel@tonic-gate 		if (WIFEXITED(stat))
5507c478bd9Sstevel@tonic-gate 			rc = WEXITSTATUS(stat);
5517c478bd9Sstevel@tonic-gate 		else
5527c478bd9Sstevel@tonic-gate 			rc = (WTERMSIG(stat) | SIGFLG);
5537c478bd9Sstevel@tonic-gate 		if (rc && (flags & errflg))
5547c478bd9Sstevel@tonic-gate 			exitsh(rc);
5557c478bd9Sstevel@tonic-gate 		exitval = rc;
5567c478bd9Sstevel@tonic-gate 		flags |= eflag;
5577c478bd9Sstevel@tonic-gate 		exitset();
5587c478bd9Sstevel@tonic-gate 	}
5597c478bd9Sstevel@tonic-gate 	while (oldstaktop != staktop)
5607c478bd9Sstevel@tonic-gate 	{ /* strip off trailing newlines from command substitution only */
5617c478bd9Sstevel@tonic-gate 		if ((*--staktop) != NL)
5627c478bd9Sstevel@tonic-gate 		{
5637c478bd9Sstevel@tonic-gate 			++staktop;
5647c478bd9Sstevel@tonic-gate 			break;
5657c478bd9Sstevel@tonic-gate 		} else if(quote)
566*2a8bcb4eSToomas Soome 			staktop--; /* skip past backslashes if quoting */
5677c478bd9Sstevel@tonic-gate 	}
5687c478bd9Sstevel@tonic-gate 	pop();
5697c478bd9Sstevel@tonic-gate }
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate #define CPYSIZ	512
5727c478bd9Sstevel@tonic-gate 
573965005c8Schin void
subst(int in,int ot)574965005c8Schin subst(int in, int ot)
5757c478bd9Sstevel@tonic-gate {
576965005c8Schin 	unsigned int	c;
5777c478bd9Sstevel@tonic-gate 	struct fileblk	fb;
578965005c8Schin 	int	count = CPYSIZ;
5797c478bd9Sstevel@tonic-gate 	unsigned char	*pc;
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	push(&fb);
5827c478bd9Sstevel@tonic-gate 	initf(in);
5837c478bd9Sstevel@tonic-gate 	/*
5847c478bd9Sstevel@tonic-gate 	 * DQUOTE used to stop it from quoting
5857c478bd9Sstevel@tonic-gate 	 */
586*2a8bcb4eSToomas Soome 	while (c = (getch(DQUOTE, 0))) /* read characters from here document
5877c478bd9Sstevel@tonic-gate 				       and interpret them */
5887c478bd9Sstevel@tonic-gate 	{
5897c478bd9Sstevel@tonic-gate 		if(c == '\\') {
590*2a8bcb4eSToomas Soome 			c = readwc(); /* check if character in here document is
5917c478bd9Sstevel@tonic-gate 					escaped */
5927c478bd9Sstevel@tonic-gate 			if(!escchar(c) || c == '"') {
5937c478bd9Sstevel@tonic-gate 				if (staktop >= brkend)
5947c478bd9Sstevel@tonic-gate 					growstak(staktop);
5957c478bd9Sstevel@tonic-gate 				pushstak('\\');
5967c478bd9Sstevel@tonic-gate 			}
5977c478bd9Sstevel@tonic-gate 		}
5987c478bd9Sstevel@tonic-gate 		pc = readw(c);
5997c478bd9Sstevel@tonic-gate 		/* c might be NULL */
6007c478bd9Sstevel@tonic-gate 		if (*pc) {
6017c478bd9Sstevel@tonic-gate 			while (*pc) {
6027c478bd9Sstevel@tonic-gate 				if (staktop >= brkend)
6037c478bd9Sstevel@tonic-gate 					growstak(staktop);
6047c478bd9Sstevel@tonic-gate 				pushstak(*pc++);
6057c478bd9Sstevel@tonic-gate 			}
6067c478bd9Sstevel@tonic-gate 		} else {
6077c478bd9Sstevel@tonic-gate 			if (staktop >= brkend)
6087c478bd9Sstevel@tonic-gate 				growstak(staktop);
6097c478bd9Sstevel@tonic-gate 			pushstak(*pc);
6107c478bd9Sstevel@tonic-gate 		}
6117c478bd9Sstevel@tonic-gate 		if (--count == 0)
6127c478bd9Sstevel@tonic-gate 		{
6137c478bd9Sstevel@tonic-gate 			flush(ot);
6147c478bd9Sstevel@tonic-gate 			count = CPYSIZ;
6157c478bd9Sstevel@tonic-gate 		}
6167c478bd9Sstevel@tonic-gate 	}
6177c478bd9Sstevel@tonic-gate 	flush(ot);
6187c478bd9Sstevel@tonic-gate 	pop();
6197c478bd9Sstevel@tonic-gate }
6207c478bd9Sstevel@tonic-gate 
621965005c8Schin static void
flush(int ot)622965005c8Schin flush(int ot)
6237c478bd9Sstevel@tonic-gate {
6247c478bd9Sstevel@tonic-gate 	write(ot, stakbot, staktop - stakbot);
6257c478bd9Sstevel@tonic-gate 	if (flags & execpr)
6267c478bd9Sstevel@tonic-gate 		write(output, stakbot, staktop - stakbot);
6277c478bd9Sstevel@tonic-gate 	staktop = stakbot;
6287c478bd9Sstevel@tonic-gate }
629