summaryrefslogtreecommitdiff
path: root/source/ap/ash/patches/ash-glob.patch
blob: 70013536ecc8546d200cf237b8c6dda4cf94aaa0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
diff -urN netbsd-sh/expand.c ash-0.3.7.orig/expand.c
--- netbsd-sh/expand.c	Tue Mar 14 13:03:45 2000
+++ ash-0.3.7.orig/expand.c	Mon Apr 23 22:16:46 2001
@@ -54,6 +54,10 @@
 #include <pwd.h>
 #include <stdlib.h>
 #include <stdio.h>
+#if defined(__GLIBC__) && !defined(GLOB_BROKEN)
+#include <fnmatch.h>
+#include <glob.h>
+#endif
 
 /*
  * Routines to expand arguments to commands.  We have to deal with
@@ -102,17 +106,30 @@
 STATIC int subevalvar __P((char *, char *, int, int, int, int));
 STATIC char *evalvar __P((char *, int));
 STATIC int varisset __P((char *, int));
+STATIC char *strtodest __P((char *, int, int));
 STATIC void varvalue __P((char *, int, int));
 STATIC void recordregion __P((int, int, int));
 STATIC void removerecordregions __P((int)); 
 STATIC void ifsbreakup __P((char *, struct arglist *));
 STATIC void ifsfree __P((void));
 STATIC void expandmeta __P((struct strlist *, int));
+#if defined(__GLIBC__) && !defined(GLOB_BROKEN)
+STATIC const char *preglob __P((const char *));
+STATIC void addglob __P((const glob_t *));
+#else
 STATIC void expmeta __P((char *, char *));
+#endif
 STATIC void addfname __P((char *));
+#if defined(__GLIBC__) && !defined(GLOB_BROKEN)
+STATIC int patmatch __P((char *, char *, int));
+STATIC int patmatch2 __P((char *, char *, int));
+STATIC char * _rmescapes __P((char *, int));
+#else
 STATIC struct strlist *expsort __P((struct strlist *));
 STATIC struct strlist *msort __P((struct strlist *, int));
 STATIC int pmatch __P((char *, char *, int));
+#define patmatch2 patmatch
+#endif
 STATIC char *cvtnum __P((int, char *));
 
 /*
@@ -371,7 +388,7 @@
 	 * have to rescan starting from the beginning since CTLESC
 	 * characters have to be processed left to right.
 	 */
-	CHECKSTRSPACE(8, expdest);
+	CHECKSTRSPACE(10, expdest);
 	USTPUTC('\0', expdest);
 	start = stackblock();
 	p = expdest - 1;
@@ -393,7 +410,7 @@
 	if (quotes)
 		rmescapes(p+2);
 	result = arith(p+2);
-	fmtstr(p, 10, "%d", result);
+	fmtstr(p, 12, "%d", result);
 
 	while (*p++)
 		;
@@ -503,7 +520,7 @@
 	int amount;
 
 	herefd = -1;
-	argstr(p, 0);
+	argstr(p, subtype != VSASSIGN && subtype != VSQUESTION ? EXP_CASE : 0);
 	STACKSTRNUL(expdest);
 	herefd = saveherefd;
 	argbackq = saveargbackq;
@@ -535,7 +552,7 @@
 		for (loc = startp; loc < str; loc++) {
 			c = *loc;
 			*loc = '\0';
-			if (patmatch(str, startp, varflags & VSQUOTE))
+			if (patmatch2(str, startp, varflags & VSQUOTE))
 				goto recordleft;
 			*loc = c;
 			if ((varflags & VSQUOTE) && *loc == CTLESC)
@@ -547,7 +564,7 @@
 		for (loc = str - 1; loc >= startp;) {
 			c = *loc;
 			*loc = '\0';
-			if (patmatch(str, startp, varflags & VSQUOTE))
+			if (patmatch2(str, startp, varflags & VSQUOTE))
 				goto recordleft;
 			*loc = c;
 			loc--;
@@ -564,7 +581,7 @@
 
 	case VSTRIMRIGHT:
 	        for (loc = str - 1; loc >= startp;) {
-			if (patmatch(str, loc, varflags & VSQUOTE))
+			if (patmatch2(str, loc, varflags & VSQUOTE))
 				goto recordright;
 			loc--;
 			if ((varflags & VSQUOTE) && loc > startp &&
@@ -580,7 +597,7 @@
 
 	case VSTRIMRIGHTMAX:
 		for (loc = startp; loc < str - 1; loc++) {
-			if (patmatch(str, loc, varflags & VSQUOTE))
+			if (patmatch2(str, loc, varflags & VSQUOTE))
 				goto recordright;
 			if ((varflags & VSQUOTE) && *loc == CTLESC)
 			        loc++;
@@ -819,6 +836,34 @@
 
 
 /*
+ * Put a string on the stack.
+ */
+
+STATIC char *
+strtodest(p, quoted, allow_split)
+	char *p;
+	int quoted;
+	int allow_split;
+{
+	char const *syntax;
+
+	if (allow_split) {
+		syntax = quoted ? DQSYNTAX : BASESYNTAX;
+		while (*p) {
+			if (syntax[(int) *p] == CCTL)
+				STPUTC(CTLESC, expdest);
+			STPUTC(*p++, expdest);
+		}
+	} else
+		while (*p)
+			STPUTC(*p++, expdest);
+
+	return p;
+}
+
+
+
+/*
  * Add the value of a specialized variable to the stack string.
  */
 
@@ -834,22 +879,6 @@
 	extern int oexitstatus;
 	char sep;
 	char **ap;
-	char const *syntax;
-
-#define STRTODEST(p) \
-	do {\
-	if (allow_split) { \
-		syntax = quoted? DQSYNTAX : BASESYNTAX; \
-		while (*p) { \
-			if (syntax[(int)*p] == CCTL) \
-				STPUTC(CTLESC, expdest); \
-			STPUTC(*p++, expdest); \
-		} \
-	} else \
-		while (*p) \
-			STPUTC(*p++, expdest); \
-	} while (0)
-
 
 	switch (*name) {
 	case '$':
@@ -875,7 +904,7 @@
 	case '@':
 		if (allow_split && quoted) {
 			for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
-				STRTODEST(p);
+				p = strtodest(p, quoted, allow_split);
 				if (*ap)
 					STPUTC('\0', expdest);
 			}
@@ -888,21 +917,20 @@
 		else
 			sep = ' ';
 		for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
-			STRTODEST(p);
+			p = strtodest(p, quoted, allow_split);
 			if (*ap && sep)
 				STPUTC(sep, expdest);
 		}
 		break;
 	case '0':
-		p = arg0;
-		STRTODEST(p);
+		p = strtodest(arg0, quoted, allow_split);
 		break;
 	default:
 		if (is_digit(*name)) {
 			num = atoi(name);
 			if (num > 0 && num <= shellparam.nparam) {
-				p = shellparam.p[num - 1];
-				STRTODEST(p);
+				p = strtodest(shellparam.p[num - 1], quoted,
+					      allow_split);
 			}
 		}
 		break;
@@ -1054,6 +1082,98 @@
  * should be escapes.  The results are stored in the list exparg.
  */
 
+#if defined(__GLIBC__) && !defined(GLOB_BROKEN)
+STATIC void
+expandmeta(str, flag)
+	struct strlist *str;
+	int flag;
+{
+	const char *p;
+	glob_t pglob;
+	/* TODO - EXP_REDIR */
+
+	while (str) {
+		if (fflag)
+			goto nometa;
+		p = preglob(str->text);
+		INTOFF;
+		switch (glob(p, GLOB_NOMAGIC, 0, &pglob)) {
+		case 0:
+			if (!(pglob.gl_flags & GLOB_MAGCHAR))
+				goto nometa2;
+			addglob(&pglob);
+			globfree(&pglob);
+			INTON;
+			break;
+		case GLOB_NOMATCH:
+nometa2:
+			globfree(&pglob);
+			INTON;
+nometa:
+			*exparg.lastp = str;
+			rmescapes(str->text);
+			exparg.lastp = &str->next;
+			break;
+		default:	/* GLOB_NOSPACE */
+			error("Out of space");
+		}
+		str = str->next;
+	}
+}
+
+
+/*
+ * Prepare the string for glob(3).
+ */
+
+STATIC const char *
+preglob(str)
+	const char *str;
+{
+	const char *p;
+	char *q, *r;
+	size_t len;
+
+	p = str;
+	while (*p != CTLQUOTEMARK && *p != CTLESC) {
+		if (*p++ == '\0')
+			return str;
+	}
+	len = p - str;
+	q = r = stalloc(strlen(str) + 1);
+	if (len > 0) {
+		memcpy(q, str, len);
+		q += len;
+	}
+	do {
+		if (*p == CTLQUOTEMARK)
+			continue;
+		if (*p == CTLESC) {
+			if (*++p != '/')
+				*q++ = '\\';
+		}
+		*q++ = *p;
+	} while (*++p);
+	*q = '\0';
+	return r;
+}
+
+
+/*
+ * Add the result of glob(3) to the list.
+ */
+
+STATIC void
+addglob(pglob)
+	const glob_t *pglob;
+{
+	char **p = pglob->gl_pathv;
+
+	do {
+		addfname(*p);
+	} while (*++p);
+}
+#else
 char *expdir;
 
 
@@ -1238,6 +1358,7 @@
 	if (! atend)
 		endname[-1] = '/';
 }
+#endif
 
 
 /*
@@ -1260,6 +1381,7 @@
 }
 
 
+#if !(defined(__GLIBC__) && !defined(GLOB_BROKEN))
 /*
  * Sort the results of file name expansion.  It calculates the number of
  * strings to sort and then calls msort (short for merge sort) to do the
@@ -1321,6 +1443,7 @@
 	}
 	return list;
 }
+#endif
 
 
 
@@ -1328,6 +1451,39 @@
  * Returns true if the pattern matches the string.
  */
 
+#if defined(__GLIBC__) && !defined(GLOB_BROKEN)
+STATIC int
+patmatch(pattern, string, squoted)
+	char *pattern;
+	char *string;
+	int squoted;	/* string might have quote chars */
+	{
+	const char *p;
+	char *q;
+
+	p = preglob(pattern);
+	q = squoted ? _rmescapes(string, 1) : string;
+
+	return !fnmatch(p, q, 0);
+}
+
+
+STATIC int
+patmatch2(pattern, string, squoted)
+	char *pattern;
+	char *string;
+	int squoted;	/* string might have quote chars */
+	{
+	char *p;
+	int res;
+
+	sstrnleft--;
+	p = grabstackstr(expdest);
+	res = patmatch(pattern, string, squoted);
+	ungrabstackstr(p, expdest);
+	return res;
+}
+#else
 int
 patmatch(pattern, string, squoted)
 	char *pattern;
@@ -1462,6 +1618,7 @@
 		return 0;
 	return 1;
 }
+#endif
 
 
 
@@ -1469,6 +1626,50 @@
  * Remove any CTLESC characters from a string.
  */
 
+#if defined(__GLIBC__) && !defined(GLOB_BROKEN)
+void
+rmescapes(str)
+	char *str;
+{
+	_rmescapes(str, 0);
+}
+
+
+STATIC char *
+_rmescapes(str, flag)
+	char *str;
+	int flag;
+{
+	char *p, *q, *r;
+
+	p = str;
+	while (*p != CTLESC && *p != CTLQUOTEMARK) {
+		if (*p++ == '\0')
+			return str;
+	}
+	q = p;
+	r = str;
+	if (flag) {
+		size_t len = p - str;
+		q = r = stalloc(strlen(p) + len + 1);
+		if (len > 0) {
+			memcpy(q, str, len);
+			q += len;
+		}
+	}
+	while (*p) {
+		if (*p == CTLQUOTEMARK) {
+			p++;
+			continue;
+		}
+		if (*p == CTLESC)
+			p++;
+		*q++ = *p++;
+	}
+	*q = '\0';
+	return r;
+}
+#else
 void
 rmescapes(str)
 	char *str;
@@ -1492,6 +1693,7 @@
 	}
 	*q = '\0';
 }
+#endif
 
 
 

diff -urN netbsd-sh/expand.h ash-0.3.7.orig/expand.h
--- netbsd-sh/expand.h	Fri Jul  9 13:02:06 1999
+++ ash-0.3.7.orig/expand.h	Mon Apr 23 22:16:46 2001
@@ -64,7 +64,9 @@
 void expandhere __P((union node *, int));
 void expandarg __P((union node *, struct arglist *, int));
 void expari __P((int));
+#if !(defined(__GLIBC__) && !defined(GLOB_BROKEN))
 int patmatch __P((char *, char *, int));
+#endif
 void rmescapes __P((char *));
 int casematch __P((union node *, char *));