xref: /illumos-gate/usr/src/cmd/sh/expand.c (revision 7c478bd9)
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 /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1995 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*7c478bd9Sstevel@tonic-gate /*
32*7c478bd9Sstevel@tonic-gate  *	UNIX shell
33*7c478bd9Sstevel@tonic-gate  *
34*7c478bd9Sstevel@tonic-gate  */
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #include	"defs.h"
37*7c478bd9Sstevel@tonic-gate #include	<sys/types.h>
38*7c478bd9Sstevel@tonic-gate #include	<sys/stat.h>
39*7c478bd9Sstevel@tonic-gate #include	<dirent.h>
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate /*
44*7c478bd9Sstevel@tonic-gate  * globals (file name generation)
45*7c478bd9Sstevel@tonic-gate  *
46*7c478bd9Sstevel@tonic-gate  * "*" in params matches r.e ".*"
47*7c478bd9Sstevel@tonic-gate  * "?" in params matches r.e. "."
48*7c478bd9Sstevel@tonic-gate  * "[...]" in params matches character class
49*7c478bd9Sstevel@tonic-gate  * "[...a-z...]" in params matches a through z.
50*7c478bd9Sstevel@tonic-gate  *
51*7c478bd9Sstevel@tonic-gate  */
52*7c478bd9Sstevel@tonic-gate static int	addg();
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate expand(as, rcnt)
55*7c478bd9Sstevel@tonic-gate 	unsigned char	*as;
56*7c478bd9Sstevel@tonic-gate {
57*7c478bd9Sstevel@tonic-gate 	int	count;
58*7c478bd9Sstevel@tonic-gate 	DIR	*dirf;
59*7c478bd9Sstevel@tonic-gate 	BOOL	dir = 0;
60*7c478bd9Sstevel@tonic-gate 	unsigned char	*rescan = 0;
61*7c478bd9Sstevel@tonic-gate 	unsigned char 	*slashsav = 0;
62*7c478bd9Sstevel@tonic-gate 	register unsigned char	*s, *cs;
63*7c478bd9Sstevel@tonic-gate 	unsigned char *s2 = 0;
64*7c478bd9Sstevel@tonic-gate 	struct argnod	*schain = gchain;
65*7c478bd9Sstevel@tonic-gate 	BOOL	slash;
66*7c478bd9Sstevel@tonic-gate 	int	len;
67*7c478bd9Sstevel@tonic-gate 	wchar_t	wc;
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate 	if (trapnote & SIGSET)
70*7c478bd9Sstevel@tonic-gate 		return (0);
71*7c478bd9Sstevel@tonic-gate 	s = cs = as;
72*7c478bd9Sstevel@tonic-gate 	/*
73*7c478bd9Sstevel@tonic-gate 	 * check for meta chars
74*7c478bd9Sstevel@tonic-gate 	 */
75*7c478bd9Sstevel@tonic-gate 	{
76*7c478bd9Sstevel@tonic-gate 		register BOOL open;
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate 		slash = 0;
79*7c478bd9Sstevel@tonic-gate 		open = 0;
80*7c478bd9Sstevel@tonic-gate 		do
81*7c478bd9Sstevel@tonic-gate 		{
82*7c478bd9Sstevel@tonic-gate 			if ((len = mbtowc(&wc, (char *)cs, MB_LEN_MAX)) <= 0) {
83*7c478bd9Sstevel@tonic-gate 				len = 1;
84*7c478bd9Sstevel@tonic-gate 				wc = (unsigned char)*cs;
85*7c478bd9Sstevel@tonic-gate 			}
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate 			cs += len;
88*7c478bd9Sstevel@tonic-gate 			switch (wc) {
89*7c478bd9Sstevel@tonic-gate 			case 0:
90*7c478bd9Sstevel@tonic-gate 				if (rcnt && slash)
91*7c478bd9Sstevel@tonic-gate 					break;
92*7c478bd9Sstevel@tonic-gate 				else
93*7c478bd9Sstevel@tonic-gate 					return (0);
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate 			case '/':
96*7c478bd9Sstevel@tonic-gate 				slash++;
97*7c478bd9Sstevel@tonic-gate 				open = 0;
98*7c478bd9Sstevel@tonic-gate 				continue;
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate 			case '[':
101*7c478bd9Sstevel@tonic-gate 				open++;
102*7c478bd9Sstevel@tonic-gate 				continue;
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 			case ']':
105*7c478bd9Sstevel@tonic-gate 				if (open == 0)
106*7c478bd9Sstevel@tonic-gate 					continue;
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate 			case '?':
109*7c478bd9Sstevel@tonic-gate 			case '*':
110*7c478bd9Sstevel@tonic-gate 				if (rcnt > slash)
111*7c478bd9Sstevel@tonic-gate 					continue;
112*7c478bd9Sstevel@tonic-gate 				else
113*7c478bd9Sstevel@tonic-gate 					cs--;
114*7c478bd9Sstevel@tonic-gate 				break;
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate 			case '\\':
117*7c478bd9Sstevel@tonic-gate 				cs++;
118*7c478bd9Sstevel@tonic-gate 			default:
119*7c478bd9Sstevel@tonic-gate 				continue;
120*7c478bd9Sstevel@tonic-gate 			}
121*7c478bd9Sstevel@tonic-gate 			break;
122*7c478bd9Sstevel@tonic-gate 		} while (TRUE);
123*7c478bd9Sstevel@tonic-gate 	}
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate 	for (;;)
126*7c478bd9Sstevel@tonic-gate 	{
127*7c478bd9Sstevel@tonic-gate 		if (cs == s)
128*7c478bd9Sstevel@tonic-gate 		{
129*7c478bd9Sstevel@tonic-gate 			s = (unsigned char *)nullstr;
130*7c478bd9Sstevel@tonic-gate 			break;
131*7c478bd9Sstevel@tonic-gate 		} else if (*--cs == '/')
132*7c478bd9Sstevel@tonic-gate 		{
133*7c478bd9Sstevel@tonic-gate 			*cs = 0;
134*7c478bd9Sstevel@tonic-gate 			if (s == cs)
135*7c478bd9Sstevel@tonic-gate 				s = (unsigned char *)"/";
136*7c478bd9Sstevel@tonic-gate 			else {
137*7c478bd9Sstevel@tonic-gate 				/*
138*7c478bd9Sstevel@tonic-gate 				 * push trimmed copy of directory prefix
139*7c478bd9Sstevel@tonic-gate 				 * onto stack
140*7c478bd9Sstevel@tonic-gate 				 */
141*7c478bd9Sstevel@tonic-gate 				s2 = cpystak(s);
142*7c478bd9Sstevel@tonic-gate 				trim(s2);
143*7c478bd9Sstevel@tonic-gate 				s = s2;
144*7c478bd9Sstevel@tonic-gate 			}
145*7c478bd9Sstevel@tonic-gate 			break;
146*7c478bd9Sstevel@tonic-gate 		}
147*7c478bd9Sstevel@tonic-gate 	}
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 	if ((dirf = opendir(*s ? (char *)s : (char *)".")) != 0)
150*7c478bd9Sstevel@tonic-gate 		dir++;
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 	/* Let s point to original string because it will be trimmed later */
153*7c478bd9Sstevel@tonic-gate 	if (s2)
154*7c478bd9Sstevel@tonic-gate 		s = as;
155*7c478bd9Sstevel@tonic-gate 	count = 0;
156*7c478bd9Sstevel@tonic-gate 	if (*cs == 0)
157*7c478bd9Sstevel@tonic-gate 		slashsav = cs++; /* remember where first slash in as is */
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate 	/* check for rescan */
160*7c478bd9Sstevel@tonic-gate 	if (dir)
161*7c478bd9Sstevel@tonic-gate 	{
162*7c478bd9Sstevel@tonic-gate 		register unsigned char *rs;
163*7c478bd9Sstevel@tonic-gate 		struct dirent *e;
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 		rs = cs;
166*7c478bd9Sstevel@tonic-gate 		do /* find next / in as */
167*7c478bd9Sstevel@tonic-gate 		{
168*7c478bd9Sstevel@tonic-gate 			if (*rs == '/')
169*7c478bd9Sstevel@tonic-gate 			{
170*7c478bd9Sstevel@tonic-gate 				rescan = rs;
171*7c478bd9Sstevel@tonic-gate 				*rs = 0;
172*7c478bd9Sstevel@tonic-gate 				gchain = 0;
173*7c478bd9Sstevel@tonic-gate 			}
174*7c478bd9Sstevel@tonic-gate 		} while (*rs++);
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate 		while ((e = readdir(dirf)) && (trapnote & SIGSET) == 0)
177*7c478bd9Sstevel@tonic-gate 		{
178*7c478bd9Sstevel@tonic-gate 			if (e->d_name[0] == '.' && *cs != '.')
179*7c478bd9Sstevel@tonic-gate 				continue;
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 			if (gmatch(e->d_name, cs))
182*7c478bd9Sstevel@tonic-gate 			{
183*7c478bd9Sstevel@tonic-gate 				addg(s, e->d_name, rescan, slashsav);
184*7c478bd9Sstevel@tonic-gate 				count++;
185*7c478bd9Sstevel@tonic-gate 			}
186*7c478bd9Sstevel@tonic-gate 		}
187*7c478bd9Sstevel@tonic-gate 		(void) closedir(dirf);
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate 		if (rescan)
190*7c478bd9Sstevel@tonic-gate 		{
191*7c478bd9Sstevel@tonic-gate 			register struct argnod	*rchain;
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate 			rchain = gchain;
194*7c478bd9Sstevel@tonic-gate 			gchain = schain;
195*7c478bd9Sstevel@tonic-gate 			if (count)
196*7c478bd9Sstevel@tonic-gate 			{
197*7c478bd9Sstevel@tonic-gate 				count = 0;
198*7c478bd9Sstevel@tonic-gate 				while (rchain)
199*7c478bd9Sstevel@tonic-gate 				{
200*7c478bd9Sstevel@tonic-gate 					count += expand(rchain->argval,
201*7c478bd9Sstevel@tonic-gate 							slash + 1);
202*7c478bd9Sstevel@tonic-gate 					rchain = rchain->argnxt;
203*7c478bd9Sstevel@tonic-gate 				}
204*7c478bd9Sstevel@tonic-gate 			}
205*7c478bd9Sstevel@tonic-gate 			*rescan = '/';
206*7c478bd9Sstevel@tonic-gate 		}
207*7c478bd9Sstevel@tonic-gate 	}
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate 	if (slashsav)
210*7c478bd9Sstevel@tonic-gate 		*slashsav = '/';
211*7c478bd9Sstevel@tonic-gate 	return (count);
212*7c478bd9Sstevel@tonic-gate }
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate static int
215*7c478bd9Sstevel@tonic-gate addg(as1, as2, as3, as4)
216*7c478bd9Sstevel@tonic-gate unsigned char	*as1, *as2, *as3, *as4;
217*7c478bd9Sstevel@tonic-gate {
218*7c478bd9Sstevel@tonic-gate 	register unsigned char	*s1, *s2;
219*7c478bd9Sstevel@tonic-gate 	register int	c;
220*7c478bd9Sstevel@tonic-gate 	int		len;
221*7c478bd9Sstevel@tonic-gate 	wchar_t		wc;
222*7c478bd9Sstevel@tonic-gate 
223*7c478bd9Sstevel@tonic-gate 	s2 = locstak() + BYTESPERWORD;
224*7c478bd9Sstevel@tonic-gate 	s1 = as1;
225*7c478bd9Sstevel@tonic-gate 	if (as4) {
226*7c478bd9Sstevel@tonic-gate 		while (c = *s1++)
227*7c478bd9Sstevel@tonic-gate 		{
228*7c478bd9Sstevel@tonic-gate 			if (s2 >= brkend)
229*7c478bd9Sstevel@tonic-gate 				growstak(s2);
230*7c478bd9Sstevel@tonic-gate 			*s2++ = c;
231*7c478bd9Sstevel@tonic-gate 		}
232*7c478bd9Sstevel@tonic-gate 		/*
233*7c478bd9Sstevel@tonic-gate 		 * Restore first slash before the first metacharacter
234*7c478bd9Sstevel@tonic-gate 		 * if as1 is not "/"
235*7c478bd9Sstevel@tonic-gate 		 */
236*7c478bd9Sstevel@tonic-gate 		if (as4 + 1 == s1) {
237*7c478bd9Sstevel@tonic-gate 			if (s2 >= brkend)
238*7c478bd9Sstevel@tonic-gate 				growstak(s2);
239*7c478bd9Sstevel@tonic-gate 			*s2++ = '/';
240*7c478bd9Sstevel@tonic-gate 		}
241*7c478bd9Sstevel@tonic-gate 	}
242*7c478bd9Sstevel@tonic-gate /* add matched entries, plus extra \\ to escape \\'s */
243*7c478bd9Sstevel@tonic-gate 	s1 = as2;
244*7c478bd9Sstevel@tonic-gate 	for (;;)
245*7c478bd9Sstevel@tonic-gate 	{
246*7c478bd9Sstevel@tonic-gate 		if ((len = mbtowc(&wc, (char *)s1, MB_LEN_MAX)) <= 0) {
247*7c478bd9Sstevel@tonic-gate 			len = 1;
248*7c478bd9Sstevel@tonic-gate 			wc = (unsigned char)*s1;
249*7c478bd9Sstevel@tonic-gate 		}
250*7c478bd9Sstevel@tonic-gate 		if (s2 >= brkend)
251*7c478bd9Sstevel@tonic-gate 			growstak(s2);
252*7c478bd9Sstevel@tonic-gate 
253*7c478bd9Sstevel@tonic-gate 		if (wc == 0) {
254*7c478bd9Sstevel@tonic-gate 			*s2 = *s1++;
255*7c478bd9Sstevel@tonic-gate 			break;
256*7c478bd9Sstevel@tonic-gate 		}
257*7c478bd9Sstevel@tonic-gate 
258*7c478bd9Sstevel@tonic-gate 		if (wc == '\\') {
259*7c478bd9Sstevel@tonic-gate 			*s2++ = '\\';
260*7c478bd9Sstevel@tonic-gate 			if (s2 >= brkend)
261*7c478bd9Sstevel@tonic-gate 				growstak(s2);
262*7c478bd9Sstevel@tonic-gate 			*s2++ = '\\';
263*7c478bd9Sstevel@tonic-gate 			s1++;
264*7c478bd9Sstevel@tonic-gate 			continue;
265*7c478bd9Sstevel@tonic-gate 		}
266*7c478bd9Sstevel@tonic-gate 		if ((s2 + len) >= brkend)
267*7c478bd9Sstevel@tonic-gate 			growstak(s2 + len);
268*7c478bd9Sstevel@tonic-gate 		memcpy(s2, s1, len);
269*7c478bd9Sstevel@tonic-gate 		s2 += len;
270*7c478bd9Sstevel@tonic-gate 		s1 += len;
271*7c478bd9Sstevel@tonic-gate 	}
272*7c478bd9Sstevel@tonic-gate 	if (s1 = as3)
273*7c478bd9Sstevel@tonic-gate 	{
274*7c478bd9Sstevel@tonic-gate 		if (s2 >= brkend)
275*7c478bd9Sstevel@tonic-gate 			growstak(s2);
276*7c478bd9Sstevel@tonic-gate 		*s2++ = '/';
277*7c478bd9Sstevel@tonic-gate 		do
278*7c478bd9Sstevel@tonic-gate 		{
279*7c478bd9Sstevel@tonic-gate 			if (s2 >= brkend)
280*7c478bd9Sstevel@tonic-gate 				growstak(s2);
281*7c478bd9Sstevel@tonic-gate 		}
282*7c478bd9Sstevel@tonic-gate 		while (*s2++ = *++s1);
283*7c478bd9Sstevel@tonic-gate 	}
284*7c478bd9Sstevel@tonic-gate 	makearg(endstak(s2));
285*7c478bd9Sstevel@tonic-gate }
286*7c478bd9Sstevel@tonic-gate 
287*7c478bd9Sstevel@tonic-gate makearg(args)
288*7c478bd9Sstevel@tonic-gate 	register struct argnod *args;
289*7c478bd9Sstevel@tonic-gate {
290*7c478bd9Sstevel@tonic-gate 	args->argnxt = gchain;
291*7c478bd9Sstevel@tonic-gate 	gchain = args;
292*7c478bd9Sstevel@tonic-gate }
293