xref: /onnv-gate/usr/src/cmd/sh/expand.c (revision 528:b9644e88fdfe)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 1995 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate /*
320Sstevel@tonic-gate  *	UNIX shell
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  */
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #include	"defs.h"
370Sstevel@tonic-gate #include	<sys/types.h>
380Sstevel@tonic-gate #include	<sys/stat.h>
390Sstevel@tonic-gate #include	<dirent.h>
400Sstevel@tonic-gate 
410Sstevel@tonic-gate 
420Sstevel@tonic-gate 
430Sstevel@tonic-gate /*
440Sstevel@tonic-gate  * globals (file name generation)
450Sstevel@tonic-gate  *
460Sstevel@tonic-gate  * "*" in params matches r.e ".*"
470Sstevel@tonic-gate  * "?" in params matches r.e. "."
480Sstevel@tonic-gate  * "[...]" in params matches character class
490Sstevel@tonic-gate  * "[...a-z...]" in params matches a through z.
500Sstevel@tonic-gate  *
510Sstevel@tonic-gate  */
52527Schin static void addg(unsigned char *, unsigned char *, unsigned char *,
53527Schin     unsigned char *);
54527Schin void makearg(struct argnod *);
550Sstevel@tonic-gate 
56527Schin int
expand(unsigned char * as,int rcnt)57527Schin expand(unsigned char	*as, int rcnt)
580Sstevel@tonic-gate {
590Sstevel@tonic-gate 	int	count;
600Sstevel@tonic-gate 	DIR	*dirf;
610Sstevel@tonic-gate 	BOOL	dir = 0;
620Sstevel@tonic-gate 	unsigned char	*rescan = 0;
630Sstevel@tonic-gate 	unsigned char 	*slashsav = 0;
64527Schin 	unsigned char	*s, *cs;
650Sstevel@tonic-gate 	unsigned char *s2 = 0;
660Sstevel@tonic-gate 	struct argnod	*schain = gchain;
670Sstevel@tonic-gate 	BOOL	slash;
680Sstevel@tonic-gate 	int	len;
690Sstevel@tonic-gate 	wchar_t	wc;
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 	if (trapnote & SIGSET)
720Sstevel@tonic-gate 		return (0);
730Sstevel@tonic-gate 	s = cs = as;
740Sstevel@tonic-gate 	/*
750Sstevel@tonic-gate 	 * check for meta chars
760Sstevel@tonic-gate 	 */
770Sstevel@tonic-gate 	{
78527Schin 		BOOL open;
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 		slash = 0;
810Sstevel@tonic-gate 		open = 0;
820Sstevel@tonic-gate 		do
830Sstevel@tonic-gate 		{
840Sstevel@tonic-gate 			if ((len = mbtowc(&wc, (char *)cs, MB_LEN_MAX)) <= 0) {
850Sstevel@tonic-gate 				len = 1;
860Sstevel@tonic-gate 				wc = (unsigned char)*cs;
870Sstevel@tonic-gate 			}
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 			cs += len;
900Sstevel@tonic-gate 			switch (wc) {
910Sstevel@tonic-gate 			case 0:
920Sstevel@tonic-gate 				if (rcnt && slash)
930Sstevel@tonic-gate 					break;
940Sstevel@tonic-gate 				else
950Sstevel@tonic-gate 					return (0);
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 			case '/':
980Sstevel@tonic-gate 				slash++;
990Sstevel@tonic-gate 				open = 0;
1000Sstevel@tonic-gate 				continue;
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 			case '[':
1030Sstevel@tonic-gate 				open++;
1040Sstevel@tonic-gate 				continue;
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 			case ']':
1070Sstevel@tonic-gate 				if (open == 0)
1080Sstevel@tonic-gate 					continue;
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 			case '?':
1110Sstevel@tonic-gate 			case '*':
1120Sstevel@tonic-gate 				if (rcnt > slash)
1130Sstevel@tonic-gate 					continue;
1140Sstevel@tonic-gate 				else
1150Sstevel@tonic-gate 					cs--;
1160Sstevel@tonic-gate 				break;
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 			case '\\':
1190Sstevel@tonic-gate 				cs++;
1200Sstevel@tonic-gate 			default:
1210Sstevel@tonic-gate 				continue;
1220Sstevel@tonic-gate 			}
1230Sstevel@tonic-gate 			break;
1240Sstevel@tonic-gate 		} while (TRUE);
1250Sstevel@tonic-gate 	}
1260Sstevel@tonic-gate 
127*528Schin 	for (;;) {
128*528Schin 		if (cs == s) {
1290Sstevel@tonic-gate 			s = (unsigned char *)nullstr;
1300Sstevel@tonic-gate 			break;
131*528Schin 		} else if (*--cs == '/') {
1320Sstevel@tonic-gate 			*cs = 0;
1330Sstevel@tonic-gate 			if (s == cs)
1340Sstevel@tonic-gate 				s = (unsigned char *)"/";
1350Sstevel@tonic-gate 			else {
1360Sstevel@tonic-gate 				/*
1370Sstevel@tonic-gate 				 * push trimmed copy of directory prefix
1380Sstevel@tonic-gate 				 * onto stack
1390Sstevel@tonic-gate 				 */
1400Sstevel@tonic-gate 				s2 = cpystak(s);
1410Sstevel@tonic-gate 				trim(s2);
1420Sstevel@tonic-gate 				s = s2;
1430Sstevel@tonic-gate 			}
1440Sstevel@tonic-gate 			break;
1450Sstevel@tonic-gate 		}
1460Sstevel@tonic-gate 	}
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	if ((dirf = opendir(*s ? (char *)s : (char *)".")) != 0)
1490Sstevel@tonic-gate 		dir++;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	/* Let s point to original string because it will be trimmed later */
1520Sstevel@tonic-gate 	if (s2)
1530Sstevel@tonic-gate 		s = as;
1540Sstevel@tonic-gate 	count = 0;
1550Sstevel@tonic-gate 	if (*cs == 0)
1560Sstevel@tonic-gate 		slashsav = cs++; /* remember where first slash in as is */
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	/* check for rescan */
159*528Schin 	if (dir) {
160527Schin 		unsigned char *rs;
1610Sstevel@tonic-gate 		struct dirent *e;
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 		rs = cs;
1640Sstevel@tonic-gate 		do /* find next / in as */
1650Sstevel@tonic-gate 		{
166*528Schin 			if (*rs == '/') {
1670Sstevel@tonic-gate 				rescan = rs;
1680Sstevel@tonic-gate 				*rs = 0;
1690Sstevel@tonic-gate 				gchain = 0;
1700Sstevel@tonic-gate 			}
1710Sstevel@tonic-gate 		} while (*rs++);
1720Sstevel@tonic-gate 
173*528Schin 		while ((e = readdir(dirf)) && (trapnote & SIGSET) == 0) {
1740Sstevel@tonic-gate 			if (e->d_name[0] == '.' && *cs != '.')
1750Sstevel@tonic-gate 				continue;
1760Sstevel@tonic-gate 
177*528Schin 			if (gmatch(e->d_name, cs)) {
178527Schin 				addg(s, (unsigned char *)e->d_name, rescan,
179527Schin 				    slashsav);
1800Sstevel@tonic-gate 				count++;
1810Sstevel@tonic-gate 			}
1820Sstevel@tonic-gate 		}
1830Sstevel@tonic-gate 		(void) closedir(dirf);
1840Sstevel@tonic-gate 
185*528Schin 		if (rescan) {
186527Schin 			struct argnod	*rchain;
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 			rchain = gchain;
1890Sstevel@tonic-gate 			gchain = schain;
190*528Schin 			if (count) {
1910Sstevel@tonic-gate 				count = 0;
192*528Schin 				while (rchain) {
1930Sstevel@tonic-gate 					count += expand(rchain->argval,
1940Sstevel@tonic-gate 							slash + 1);
1950Sstevel@tonic-gate 					rchain = rchain->argnxt;
1960Sstevel@tonic-gate 				}
1970Sstevel@tonic-gate 			}
1980Sstevel@tonic-gate 			*rescan = '/';
1990Sstevel@tonic-gate 		}
2000Sstevel@tonic-gate 	}
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	if (slashsav)
2030Sstevel@tonic-gate 		*slashsav = '/';
2040Sstevel@tonic-gate 	return (count);
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate 
207527Schin static void
addg(unsigned char * as1,unsigned char * as2,unsigned char * as3,unsigned char * as4)208527Schin addg(unsigned char *as1, unsigned char *as2, unsigned char *as3,
209527Schin     unsigned char *as4)
2100Sstevel@tonic-gate {
211527Schin 	unsigned char	*s1, *s2;
212527Schin 	int	c;
2130Sstevel@tonic-gate 	int		len;
2140Sstevel@tonic-gate 	wchar_t		wc;
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	s2 = locstak() + BYTESPERWORD;
2170Sstevel@tonic-gate 	s1 = as1;
2180Sstevel@tonic-gate 	if (as4) {
219*528Schin 		while (c = *s1++) {
2200Sstevel@tonic-gate 			if (s2 >= brkend)
2210Sstevel@tonic-gate 				growstak(s2);
2220Sstevel@tonic-gate 			*s2++ = c;
2230Sstevel@tonic-gate 		}
2240Sstevel@tonic-gate 		/*
2250Sstevel@tonic-gate 		 * Restore first slash before the first metacharacter
2260Sstevel@tonic-gate 		 * if as1 is not "/"
2270Sstevel@tonic-gate 		 */
2280Sstevel@tonic-gate 		if (as4 + 1 == s1) {
2290Sstevel@tonic-gate 			if (s2 >= brkend)
2300Sstevel@tonic-gate 				growstak(s2);
2310Sstevel@tonic-gate 			*s2++ = '/';
2320Sstevel@tonic-gate 		}
2330Sstevel@tonic-gate 	}
2340Sstevel@tonic-gate /* add matched entries, plus extra \\ to escape \\'s */
2350Sstevel@tonic-gate 	s1 = as2;
236*528Schin 	for (;;) {
2370Sstevel@tonic-gate 		if ((len = mbtowc(&wc, (char *)s1, MB_LEN_MAX)) <= 0) {
2380Sstevel@tonic-gate 			len = 1;
2390Sstevel@tonic-gate 			wc = (unsigned char)*s1;
2400Sstevel@tonic-gate 		}
2410Sstevel@tonic-gate 		if (s2 >= brkend)
2420Sstevel@tonic-gate 			growstak(s2);
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 		if (wc == 0) {
2450Sstevel@tonic-gate 			*s2 = *s1++;
2460Sstevel@tonic-gate 			break;
2470Sstevel@tonic-gate 		}
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 		if (wc == '\\') {
2500Sstevel@tonic-gate 			*s2++ = '\\';
2510Sstevel@tonic-gate 			if (s2 >= brkend)
2520Sstevel@tonic-gate 				growstak(s2);
2530Sstevel@tonic-gate 			*s2++ = '\\';
2540Sstevel@tonic-gate 			s1++;
2550Sstevel@tonic-gate 			continue;
2560Sstevel@tonic-gate 		}
2570Sstevel@tonic-gate 		if ((s2 + len) >= brkend)
2580Sstevel@tonic-gate 			growstak(s2 + len);
2590Sstevel@tonic-gate 		memcpy(s2, s1, len);
2600Sstevel@tonic-gate 		s2 += len;
2610Sstevel@tonic-gate 		s1 += len;
2620Sstevel@tonic-gate 	}
263*528Schin 	if (s1 = as3) {
2640Sstevel@tonic-gate 		if (s2 >= brkend)
2650Sstevel@tonic-gate 			growstak(s2);
2660Sstevel@tonic-gate 		*s2++ = '/';
2670Sstevel@tonic-gate 		do
2680Sstevel@tonic-gate 		{
2690Sstevel@tonic-gate 			if (s2 >= brkend)
2700Sstevel@tonic-gate 				growstak(s2);
2710Sstevel@tonic-gate 		}
2720Sstevel@tonic-gate 		while (*s2++ = *++s1);
2730Sstevel@tonic-gate 	}
274527Schin 	makearg((struct argnod *)endstak(s2));
2750Sstevel@tonic-gate }
2760Sstevel@tonic-gate 
277527Schin void
makearg(struct argnod * args)278527Schin makearg(struct argnod *args)
2790Sstevel@tonic-gate {
2800Sstevel@tonic-gate 	args->argnxt = gchain;
2810Sstevel@tonic-gate 	gchain = args;
2820Sstevel@tonic-gate }
283