glob.c revision 1.18 1 1.18 wiz /* $NetBSD: glob.c,v 1.18 2001/09/14 14:04:00 wiz Exp $ */
2 1.10 cgd
3 1.1 cgd /*-
4 1.8 mycroft * Copyright (c) 1980, 1991, 1993
5 1.8 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.12 christos #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.10 cgd #if 0
39 1.10 cgd static char sccsid[] = "@(#)glob.c 8.1 (Berkeley) 5/31/93";
40 1.10 cgd #else
41 1.18 wiz __RCSID("$NetBSD: glob.c,v 1.18 2001/09/14 14:04:00 wiz Exp $");
42 1.10 cgd #endif
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.1 cgd #include <sys/param.h>
46 1.18 wiz
47 1.18 wiz #include <errno.h>
48 1.1 cgd #include <glob.h>
49 1.1 cgd #include <stdlib.h>
50 1.1 cgd #include <string.h>
51 1.1 cgd #include <unistd.h>
52 1.18 wiz
53 1.1 cgd #if __STDC__
54 1.1 cgd # include <stdarg.h>
55 1.1 cgd #else
56 1.1 cgd # include <varargs.h>
57 1.1 cgd #endif
58 1.1 cgd
59 1.1 cgd #include "csh.h"
60 1.1 cgd #include "extern.h"
61 1.1 cgd
62 1.8 mycroft static int noglob;
63 1.18 wiz static int gargsiz, pargsiz;
64 1.1 cgd
65 1.1 cgd /*
66 1.1 cgd * Values for gflag
67 1.1 cgd */
68 1.18 wiz #define G_NONE 0 /* No globbing needed */
69 1.18 wiz #define G_GLOB 1 /* string contains *?[] characters */
70 1.18 wiz #define G_CSH 2 /* string contains ~`{ characters */
71 1.1 cgd
72 1.18 wiz #define GLOBSPACE 100 /* Alloc increment */
73 1.1 cgd
74 1.1 cgd #define LBRC '{'
75 1.1 cgd #define RBRC '}'
76 1.1 cgd #define LBRK '['
77 1.1 cgd #define RBRK ']'
78 1.1 cgd #define EOS '\0'
79 1.1 cgd
80 1.18 wiz Char **gargv = NULL;
81 1.18 wiz Char **pargv = NULL;
82 1.18 wiz long gargc = 0;
83 1.18 wiz long pargc = 0;
84 1.1 cgd
85 1.1 cgd /*
86 1.1 cgd * globbing is now done in two stages. In the first pass we expand
87 1.1 cgd * csh globbing idioms ~`{ and then we proceed doing the normal
88 1.1 cgd * globbing if needed ?*[
89 1.1 cgd *
90 1.1 cgd * Csh type globbing is handled in globexpand() and the rest is
91 1.1 cgd * handled in glob() which is part of the 4.4BSD libc.
92 1.1 cgd *
93 1.1 cgd */
94 1.18 wiz static Char *globtilde(Char **, Char *);
95 1.18 wiz static Char *handleone(Char *, Char **, int);
96 1.18 wiz static Char **libglob(Char **);
97 1.18 wiz static Char **globexpand(Char **);
98 1.18 wiz static int globbrace(Char *, Char *, Char ***);
99 1.18 wiz static void expbrace(Char ***, Char ***, int);
100 1.18 wiz static int pmatch(Char *, Char *);
101 1.18 wiz static void pword(void);
102 1.18 wiz static void psave(int);
103 1.18 wiz static void backeval(Char *, bool);
104 1.1 cgd
105 1.1 cgd static Char *
106 1.18 wiz globtilde(Char **nv, Char *s)
107 1.1 cgd {
108 1.18 wiz Char gbuf[MAXPATHLEN], *b, *e, *gstart, *u;
109 1.1 cgd
110 1.1 cgd gstart = gbuf;
111 1.1 cgd *gstart++ = *s++;
112 1.1 cgd u = s;
113 1.8 mycroft for (b = gstart, e = &gbuf[MAXPATHLEN - 1];
114 1.8 mycroft *s && *s != '/' && *s != ':' && b < e;
115 1.8 mycroft *b++ = *s++)
116 1.8 mycroft continue;
117 1.1 cgd *b = EOS;
118 1.1 cgd if (gethdir(gstart)) {
119 1.1 cgd blkfree(nv);
120 1.1 cgd if (*gstart)
121 1.8 mycroft stderror(ERR_UNKUSER, vis_str(gstart));
122 1.1 cgd else
123 1.1 cgd stderror(ERR_NOHOME);
124 1.1 cgd }
125 1.1 cgd b = &gstart[Strlen(gstart)];
126 1.1 cgd while (*s)
127 1.1 cgd *b++ = *s++;
128 1.1 cgd *b = EOS;
129 1.1 cgd --u;
130 1.1 cgd xfree((ptr_t) u);
131 1.1 cgd return (Strsave(gstart));
132 1.1 cgd }
133 1.1 cgd
134 1.1 cgd static int
135 1.18 wiz globbrace(Char *s, Char *p, Char ***bl)
136 1.1 cgd {
137 1.18 wiz Char gbuf[MAXPATHLEN];
138 1.18 wiz Char *lm, *pe, *pl, *pm, **nv, **vl;
139 1.18 wiz int i, len, size;
140 1.1 cgd
141 1.18 wiz size = GLOBSPACE;
142 1.18 wiz nv = vl = (Char **)xmalloc((size_t) sizeof(Char *) * size);
143 1.1 cgd *vl = NULL;
144 1.1 cgd len = 0;
145 1.1 cgd /* copy part up to the brace */
146 1.1 cgd for (lm = gbuf, p = s; *p != LBRC; *lm++ = *p++)
147 1.1 cgd continue;
148 1.1 cgd
149 1.1 cgd /* check for balanced braces */
150 1.1 cgd for (i = 0, pe = ++p; *pe; pe++)
151 1.1 cgd if (*pe == LBRK) {
152 1.1 cgd /* Ignore everything between [] */
153 1.1 cgd for (++pe; *pe != RBRK && *pe != EOS; pe++)
154 1.1 cgd continue;
155 1.1 cgd if (*pe == EOS) {
156 1.1 cgd blkfree(nv);
157 1.8 mycroft return (-RBRK);
158 1.1 cgd }
159 1.1 cgd }
160 1.1 cgd else if (*pe == LBRC)
161 1.1 cgd i++;
162 1.1 cgd else if (*pe == RBRC) {
163 1.1 cgd if (i == 0)
164 1.1 cgd break;
165 1.1 cgd i--;
166 1.1 cgd }
167 1.1 cgd
168 1.6 cgd if (i != 0 || *pe == '\0') {
169 1.1 cgd blkfree(nv);
170 1.6 cgd return (-RBRC);
171 1.1 cgd }
172 1.1 cgd
173 1.1 cgd for (i = 0, pl = pm = p; pm <= pe; pm++)
174 1.1 cgd switch (*pm) {
175 1.1 cgd case LBRK:
176 1.1 cgd for (++pm; *pm != RBRK && *pm != EOS; pm++)
177 1.1 cgd continue;
178 1.1 cgd if (*pm == EOS) {
179 1.1 cgd *vl = NULL;
180 1.1 cgd blkfree(nv);
181 1.1 cgd return (-RBRK);
182 1.1 cgd }
183 1.1 cgd break;
184 1.1 cgd case LBRC:
185 1.1 cgd i++;
186 1.1 cgd break;
187 1.1 cgd case RBRC:
188 1.1 cgd if (i) {
189 1.1 cgd i--;
190 1.1 cgd break;
191 1.1 cgd }
192 1.1 cgd /* FALLTHROUGH */
193 1.1 cgd case ',':
194 1.1 cgd if (i && *pm == ',')
195 1.1 cgd break;
196 1.1 cgd else {
197 1.1 cgd Char savec = *pm;
198 1.1 cgd
199 1.1 cgd *pm = EOS;
200 1.18 wiz (void)Strcpy(lm, pl);
201 1.18 wiz (void)Strcat(gbuf, pe + 1);
202 1.1 cgd *pm = savec;
203 1.1 cgd *vl++ = Strsave(gbuf);
204 1.1 cgd len++;
205 1.1 cgd pl = pm + 1;
206 1.1 cgd if (vl == &nv[size]) {
207 1.1 cgd size += GLOBSPACE;
208 1.18 wiz nv = (Char **)xrealloc((ptr_t) nv,
209 1.18 wiz (size_t)size * sizeof(Char *));
210 1.1 cgd vl = &nv[size - GLOBSPACE];
211 1.1 cgd }
212 1.1 cgd }
213 1.1 cgd break;
214 1.8 mycroft default:
215 1.8 mycroft break;
216 1.1 cgd }
217 1.1 cgd *vl = NULL;
218 1.1 cgd *bl = nv;
219 1.1 cgd return (len);
220 1.1 cgd }
221 1.1 cgd
222 1.8 mycroft static void
223 1.18 wiz expbrace(Char ***nvp, Char ***elp, int size)
224 1.8 mycroft {
225 1.18 wiz Char **el, **nv, *s, **vl;
226 1.8 mycroft
227 1.8 mycroft vl = nv = *nvp;
228 1.8 mycroft if (elp != NULL)
229 1.8 mycroft el = *elp;
230 1.8 mycroft else
231 1.8 mycroft for (el = vl; *el; el++)
232 1.8 mycroft continue;
233 1.8 mycroft
234 1.8 mycroft for (s = *vl; s; s = *++vl) {
235 1.18 wiz Char *b, **bp, **vp;
236 1.8 mycroft
237 1.8 mycroft /* leave {} untouched for find */
238 1.8 mycroft if (s[0] == '{' && (s[1] == '\0' || (s[1] == '}' && s[2] == '\0')))
239 1.8 mycroft continue;
240 1.8 mycroft if ((b = Strchr(s, '{')) != NULL) {
241 1.18 wiz Char **bl;
242 1.18 wiz int len;
243 1.8 mycroft
244 1.8 mycroft if ((len = globbrace(s, b, &bl)) < 0) {
245 1.18 wiz xfree((ptr_t)nv);
246 1.8 mycroft stderror(ERR_MISSING, -len);
247 1.8 mycroft }
248 1.8 mycroft xfree((ptr_t) s);
249 1.8 mycroft if (len == 1) {
250 1.8 mycroft *vl-- = *bl;
251 1.8 mycroft xfree((ptr_t) bl);
252 1.8 mycroft continue;
253 1.8 mycroft }
254 1.8 mycroft len = blklen(bl);
255 1.8 mycroft if (&el[len] >= &nv[size]) {
256 1.18 wiz int e, l;
257 1.8 mycroft
258 1.8 mycroft l = &el[len] - &nv[size];
259 1.8 mycroft size += GLOBSPACE > l ? GLOBSPACE : l;
260 1.8 mycroft l = vl - nv;
261 1.8 mycroft e = el - nv;
262 1.18 wiz nv = (Char **)xrealloc((ptr_t)nv,
263 1.18 wiz (size_t)size * sizeof(Char *));
264 1.8 mycroft vl = nv + l;
265 1.8 mycroft el = nv + e;
266 1.8 mycroft }
267 1.8 mycroft vp = vl--;
268 1.8 mycroft *vp = *bl;
269 1.8 mycroft len--;
270 1.8 mycroft for (bp = el; bp != vp; bp--)
271 1.8 mycroft bp[len] = *bp;
272 1.8 mycroft el += len;
273 1.8 mycroft vp++;
274 1.8 mycroft for (bp = bl + 1; *bp; *vp++ = *bp++)
275 1.8 mycroft continue;
276 1.18 wiz xfree((ptr_t)bl);
277 1.8 mycroft }
278 1.8 mycroft
279 1.8 mycroft }
280 1.8 mycroft if (elp != NULL)
281 1.8 mycroft *elp = el;
282 1.8 mycroft *nvp = nv;
283 1.8 mycroft }
284 1.8 mycroft
285 1.1 cgd static Char **
286 1.18 wiz globexpand(Char **v)
287 1.1 cgd {
288 1.18 wiz Char **el, **nv, *s, **vl;
289 1.18 wiz int size;
290 1.1 cgd
291 1.18 wiz size = GLOBSPACE;
292 1.18 wiz nv = vl = (Char **)xmalloc((size_t)sizeof(Char *) * size);
293 1.1 cgd *vl = NULL;
294 1.1 cgd
295 1.1 cgd /*
296 1.1 cgd * Step 1: expand backquotes.
297 1.1 cgd */
298 1.8 mycroft while ((s = *v++) != NULL) {
299 1.1 cgd if (Strchr(s, '`')) {
300 1.18 wiz int i;
301 1.1 cgd
302 1.1 cgd (void) dobackp(s, 0);
303 1.1 cgd for (i = 0; i < pargc; i++) {
304 1.1 cgd *vl++ = pargv[i];
305 1.1 cgd if (vl == &nv[size]) {
306 1.1 cgd size += GLOBSPACE;
307 1.18 wiz nv = (Char **)xrealloc((ptr_t) nv,
308 1.18 wiz (size_t)size * sizeof(Char *));
309 1.1 cgd vl = &nv[size - GLOBSPACE];
310 1.1 cgd }
311 1.1 cgd }
312 1.18 wiz xfree((ptr_t)pargv);
313 1.1 cgd pargv = NULL;
314 1.1 cgd }
315 1.1 cgd else {
316 1.1 cgd *vl++ = Strsave(s);
317 1.1 cgd if (vl == &nv[size]) {
318 1.1 cgd size += GLOBSPACE;
319 1.18 wiz nv = (Char **)xrealloc((ptr_t)nv,
320 1.18 wiz (size_t)size * sizeof(Char *));
321 1.1 cgd vl = &nv[size - GLOBSPACE];
322 1.1 cgd }
323 1.1 cgd }
324 1.1 cgd }
325 1.1 cgd *vl = NULL;
326 1.1 cgd
327 1.1 cgd if (noglob)
328 1.1 cgd return (nv);
329 1.1 cgd
330 1.1 cgd /*
331 1.1 cgd * Step 2: expand braces
332 1.1 cgd */
333 1.1 cgd el = vl;
334 1.8 mycroft expbrace(&nv, &el, size);
335 1.1 cgd
336 1.1 cgd /*
337 1.1 cgd * Step 3: expand ~
338 1.1 cgd */
339 1.1 cgd vl = nv;
340 1.1 cgd for (s = *vl; s; s = *++vl)
341 1.1 cgd if (*s == '~')
342 1.1 cgd *vl = globtilde(nv, s);
343 1.1 cgd vl = nv;
344 1.1 cgd return (vl);
345 1.1 cgd }
346 1.1 cgd
347 1.1 cgd static Char *
348 1.18 wiz handleone(Char *str, Char **vl, int action)
349 1.1 cgd {
350 1.18 wiz Char *cp, **vlp;
351 1.1 cgd
352 1.18 wiz vlp = vl;
353 1.1 cgd switch (action) {
354 1.1 cgd case G_ERROR:
355 1.8 mycroft setname(vis_str(str));
356 1.1 cgd blkfree(vl);
357 1.1 cgd stderror(ERR_NAME | ERR_AMBIG);
358 1.15 mycroft /* NOTREACHED */
359 1.1 cgd case G_APPEND:
360 1.1 cgd trim(vlp);
361 1.1 cgd str = Strsave(*vlp++);
362 1.1 cgd do {
363 1.1 cgd cp = Strspl(str, STRspace);
364 1.18 wiz xfree((ptr_t)str);
365 1.1 cgd str = Strspl(cp, *vlp);
366 1.18 wiz xfree((ptr_t)cp);
367 1.1 cgd }
368 1.1 cgd while (*++vlp);
369 1.1 cgd blkfree(vl);
370 1.1 cgd break;
371 1.1 cgd case G_IGNORE:
372 1.1 cgd str = Strsave(strip(*vlp));
373 1.1 cgd blkfree(vl);
374 1.1 cgd break;
375 1.8 mycroft default:
376 1.8 mycroft break;
377 1.1 cgd }
378 1.1 cgd return (str);
379 1.1 cgd }
380 1.1 cgd
381 1.1 cgd static Char **
382 1.18 wiz libglob(Char **vl)
383 1.1 cgd {
384 1.18 wiz glob_t globv;
385 1.18 wiz char *ptr;
386 1.18 wiz int gflgs, magic, match, nonomatch;
387 1.18 wiz
388 1.18 wiz gflgs = GLOB_NOMAGIC;
389 1.18 wiz magic = 0;
390 1.18 wiz match = 0;
391 1.18 wiz nonomatch = adrof(STRnonomatch) != 0;
392 1.8 mycroft
393 1.8 mycroft if (!vl || !vl[0])
394 1.8 mycroft return (vl);
395 1.1 cgd
396 1.1 cgd globv.gl_offs = 0;
397 1.1 cgd globv.gl_pathv = 0;
398 1.1 cgd globv.gl_pathc = 0;
399 1.8 mycroft
400 1.8 mycroft if (nonomatch)
401 1.8 mycroft gflgs |= GLOB_NOCHECK;
402 1.8 mycroft
403 1.1 cgd do {
404 1.1 cgd ptr = short2qstr(*vl);
405 1.1 cgd switch (glob(ptr, gflgs, 0, &globv)) {
406 1.13 kleink case GLOB_ABORTED:
407 1.8 mycroft setname(vis_str(*vl));
408 1.1 cgd stderror(ERR_NAME | ERR_GLOB);
409 1.1 cgd /* NOTREACHED */
410 1.1 cgd case GLOB_NOSPACE:
411 1.1 cgd stderror(ERR_NOMEM);
412 1.1 cgd /* NOTREACHED */
413 1.1 cgd default:
414 1.1 cgd break;
415 1.1 cgd }
416 1.8 mycroft if (globv.gl_flags & GLOB_MAGCHAR) {
417 1.8 mycroft match |= (globv.gl_matchc != 0);
418 1.8 mycroft magic = 1;
419 1.8 mycroft }
420 1.1 cgd gflgs |= GLOB_APPEND;
421 1.1 cgd }
422 1.1 cgd while (*++vl);
423 1.8 mycroft vl = (globv.gl_pathc == 0 || (magic && !match && !nonomatch)) ?
424 1.8 mycroft NULL : blk2short(globv.gl_pathv);
425 1.1 cgd globfree(&globv);
426 1.1 cgd return (vl);
427 1.1 cgd }
428 1.1 cgd
429 1.18 wiz Char *
430 1.18 wiz globone(Char *str, int action)
431 1.1 cgd {
432 1.18 wiz Char *v[2], **vl, **vo;
433 1.18 wiz int gflg;
434 1.1 cgd
435 1.1 cgd noglob = adrof(STRnoglob) != 0;
436 1.1 cgd gflag = 0;
437 1.1 cgd v[0] = str;
438 1.1 cgd v[1] = 0;
439 1.1 cgd tglob(v);
440 1.8 mycroft gflg = gflag;
441 1.8 mycroft if (gflg == G_NONE)
442 1.1 cgd return (strip(Strsave(str)));
443 1.1 cgd
444 1.8 mycroft if (gflg & G_CSH) {
445 1.1 cgd /*
446 1.1 cgd * Expand back-quote, tilde and brace
447 1.1 cgd */
448 1.1 cgd vo = globexpand(v);
449 1.8 mycroft if (noglob || (gflg & G_GLOB) == 0) {
450 1.1 cgd if (vo[0] == NULL) {
451 1.18 wiz xfree((ptr_t)vo);
452 1.1 cgd return (Strsave(STRNULL));
453 1.1 cgd }
454 1.1 cgd if (vo[1] != NULL)
455 1.1 cgd return (handleone(str, vo, action));
456 1.1 cgd else {
457 1.1 cgd str = strip(vo[0]);
458 1.1 cgd xfree((ptr_t) vo);
459 1.1 cgd return (str);
460 1.1 cgd }
461 1.1 cgd }
462 1.1 cgd }
463 1.8 mycroft else if (noglob || (gflg & G_GLOB) == 0)
464 1.1 cgd return (strip(Strsave(str)));
465 1.1 cgd else
466 1.1 cgd vo = v;
467 1.1 cgd
468 1.1 cgd vl = libglob(vo);
469 1.8 mycroft if ((gflg & G_CSH) && vl != vo)
470 1.1 cgd blkfree(vo);
471 1.1 cgd if (vl == NULL) {
472 1.8 mycroft setname(vis_str(str));
473 1.1 cgd stderror(ERR_NAME | ERR_NOMATCH);
474 1.1 cgd }
475 1.1 cgd if (vl[0] == NULL) {
476 1.18 wiz xfree((ptr_t)vl);
477 1.1 cgd return (Strsave(STRNULL));
478 1.1 cgd }
479 1.1 cgd if (vl[1] != NULL)
480 1.1 cgd return (handleone(str, vl, action));
481 1.1 cgd else {
482 1.1 cgd str = strip(*vl);
483 1.18 wiz xfree((ptr_t)vl);
484 1.1 cgd return (str);
485 1.1 cgd }
486 1.1 cgd }
487 1.1 cgd
488 1.1 cgd Char **
489 1.18 wiz globall(Char **v)
490 1.1 cgd {
491 1.18 wiz Char **vl, **vo;
492 1.18 wiz int gflg;
493 1.1 cgd
494 1.18 wiz gflg = gflag;
495 1.1 cgd if (!v || !v[0]) {
496 1.1 cgd gargv = saveblk(v);
497 1.1 cgd gargc = blklen(gargv);
498 1.1 cgd return (gargv);
499 1.1 cgd }
500 1.1 cgd
501 1.1 cgd noglob = adrof(STRnoglob) != 0;
502 1.1 cgd
503 1.8 mycroft if (gflg & G_CSH)
504 1.1 cgd /*
505 1.1 cgd * Expand back-quote, tilde and brace
506 1.1 cgd */
507 1.1 cgd vl = vo = globexpand(v);
508 1.1 cgd else
509 1.1 cgd vl = vo = saveblk(v);
510 1.1 cgd
511 1.9 mycroft if (!noglob && (gflg & G_GLOB)) {
512 1.1 cgd vl = libglob(vo);
513 1.8 mycroft if ((gflg & G_CSH) && vl != vo)
514 1.1 cgd blkfree(vo);
515 1.1 cgd }
516 1.8 mycroft else
517 1.8 mycroft trim(vl);
518 1.1 cgd
519 1.1 cgd gargc = vl ? blklen(vl) : 0;
520 1.1 cgd return (gargv = vl);
521 1.1 cgd }
522 1.1 cgd
523 1.1 cgd void
524 1.18 wiz ginit(void)
525 1.1 cgd {
526 1.1 cgd gargsiz = GLOBSPACE;
527 1.18 wiz gargv = (Char **)xmalloc((size_t)sizeof(Char *) * gargsiz);
528 1.1 cgd gargv[0] = 0;
529 1.1 cgd gargc = 0;
530 1.1 cgd }
531 1.1 cgd
532 1.1 cgd void
533 1.18 wiz rscan(Char **t, void (*f)(int))
534 1.1 cgd {
535 1.11 tls Char *p;
536 1.1 cgd
537 1.8 mycroft while ((p = *t++) != NULL)
538 1.1 cgd while (*p)
539 1.1 cgd (*f) (*p++);
540 1.1 cgd }
541 1.1 cgd
542 1.1 cgd void
543 1.18 wiz trim(Char **t)
544 1.1 cgd {
545 1.11 tls Char *p;
546 1.1 cgd
547 1.8 mycroft while ((p = *t++) != NULL)
548 1.1 cgd while (*p)
549 1.1 cgd *p++ &= TRIM;
550 1.1 cgd }
551 1.1 cgd
552 1.1 cgd void
553 1.18 wiz tglob(Char **t)
554 1.1 cgd {
555 1.11 tls Char *p, c;
556 1.1 cgd
557 1.8 mycroft while ((p = *t++) != NULL) {
558 1.1 cgd if (*p == '~' || *p == '=')
559 1.1 cgd gflag |= G_CSH;
560 1.1 cgd else if (*p == '{' &&
561 1.8 mycroft (p[1] == '\0' || (p[1] == '}' && p[2] == '\0')))
562 1.1 cgd continue;
563 1.8 mycroft while ((c = *p++) != '\0') {
564 1.8 mycroft /*
565 1.8 mycroft * eat everything inside the matching backquotes
566 1.8 mycroft */
567 1.8 mycroft if (c == '`') {
568 1.8 mycroft gflag |= G_CSH;
569 1.8 mycroft while (*p && *p != '`')
570 1.8 mycroft if (*p++ == '\\') {
571 1.8 mycroft if (*p) /* Quoted chars */
572 1.8 mycroft p++;
573 1.8 mycroft else
574 1.8 mycroft break;
575 1.8 mycroft }
576 1.8 mycroft if (*p) /* The matching ` */
577 1.8 mycroft p++;
578 1.8 mycroft else
579 1.8 mycroft break;
580 1.8 mycroft }
581 1.8 mycroft else if (c == '{')
582 1.8 mycroft gflag |= G_CSH;
583 1.8 mycroft else if (isglob(c))
584 1.8 mycroft gflag |= G_GLOB;
585 1.8 mycroft }
586 1.1 cgd }
587 1.1 cgd }
588 1.1 cgd
589 1.1 cgd /*
590 1.1 cgd * Command substitute cp. If literal, then this is a substitution from a
591 1.1 cgd * << redirection, and so we should not crunch blanks and tabs, separating
592 1.1 cgd * words only at newlines.
593 1.1 cgd */
594 1.18 wiz Char **
595 1.18 wiz dobackp(Char *cp, bool literal)
596 1.1 cgd {
597 1.18 wiz Char word[MAXPATHLEN], *ep, *lp, *rp;
598 1.1 cgd
599 1.1 cgd if (pargv) {
600 1.8 mycroft #ifdef notdef
601 1.1 cgd abort();
602 1.8 mycroft #endif
603 1.1 cgd blkfree(pargv);
604 1.1 cgd }
605 1.1 cgd pargsiz = GLOBSPACE;
606 1.18 wiz pargv = (Char **)xmalloc((size_t)sizeof(Char *) * pargsiz);
607 1.1 cgd pargv[0] = NULL;
608 1.1 cgd pargcp = pargs = word;
609 1.1 cgd pargc = 0;
610 1.1 cgd pnleft = MAXPATHLEN - 4;
611 1.1 cgd for (;;) {
612 1.1 cgd for (lp = cp; *lp != '`'; lp++) {
613 1.1 cgd if (*lp == 0) {
614 1.1 cgd if (pargcp != pargs)
615 1.1 cgd pword();
616 1.1 cgd return (pargv);
617 1.1 cgd }
618 1.1 cgd psave(*lp);
619 1.1 cgd }
620 1.1 cgd lp++;
621 1.1 cgd for (rp = lp; *rp && *rp != '`'; rp++)
622 1.1 cgd if (*rp == '\\') {
623 1.1 cgd rp++;
624 1.1 cgd if (!*rp)
625 1.1 cgd goto oops;
626 1.1 cgd }
627 1.15 mycroft if (!*rp) {
628 1.15 mycroft oops:
629 1.15 mycroft stderror(ERR_UNMATCHED, '`');
630 1.15 mycroft }
631 1.1 cgd ep = Strsave(lp);
632 1.1 cgd ep[rp - lp] = 0;
633 1.1 cgd backeval(ep, literal);
634 1.1 cgd cp = rp + 1;
635 1.1 cgd }
636 1.1 cgd }
637 1.1 cgd
638 1.1 cgd static void
639 1.18 wiz backeval(Char *cp, bool literal)
640 1.1 cgd {
641 1.1 cgd struct command faket;
642 1.18 wiz char tibuf[BUFSIZE];
643 1.18 wiz Char ibuf[BUFSIZE], *fakecom[2], *ip;
644 1.18 wiz int pvec[2], c, icnt, quoted;
645 1.18 wiz bool hadnl;
646 1.1 cgd
647 1.1 cgd hadnl = 0;
648 1.1 cgd icnt = 0;
649 1.1 cgd quoted = (literal || (cp[0] & QUOTE)) ? QUOTE : 0;
650 1.1 cgd faket.t_dtyp = NODE_COMMAND;
651 1.1 cgd faket.t_dflg = 0;
652 1.1 cgd faket.t_dlef = 0;
653 1.1 cgd faket.t_drit = 0;
654 1.1 cgd faket.t_dspr = 0;
655 1.1 cgd faket.t_dcom = fakecom;
656 1.1 cgd fakecom[0] = STRfakecom1;
657 1.1 cgd fakecom[1] = 0;
658 1.1 cgd
659 1.1 cgd /*
660 1.1 cgd * We do the psave job to temporarily change the current job so that the
661 1.1 cgd * following fork is considered a separate job. This is so that when
662 1.1 cgd * backquotes are used in a builtin function that calls glob the "current
663 1.1 cgd * job" is not corrupted. We only need one level of pushed jobs as long as
664 1.1 cgd * we are sure to fork here.
665 1.1 cgd */
666 1.1 cgd psavejob();
667 1.1 cgd
668 1.1 cgd /*
669 1.1 cgd * It would be nicer if we could integrate this redirection more with the
670 1.1 cgd * routines in sh.sem.c by doing a fake execute on a builtin function that
671 1.1 cgd * was piped out.
672 1.1 cgd */
673 1.1 cgd mypipe(pvec);
674 1.1 cgd if (pfork(&faket, -1) == 0) {
675 1.1 cgd struct wordent paraml;
676 1.1 cgd struct command *t;
677 1.1 cgd
678 1.18 wiz (void)close(pvec[0]);
679 1.18 wiz (void)dmove(pvec[1], 1);
680 1.18 wiz (void)dmove(SHERR, 2);
681 1.1 cgd initdesc();
682 1.1 cgd /*
683 1.1 cgd * Bugfix for nested backquotes by Michael Greim <greim (at) sbsvax.UUCP>,
684 1.1 cgd * posted to comp.bugs.4bsd 12 Sep. 1989.
685 1.1 cgd */
686 1.1 cgd if (pargv) /* mg, 21.dec.88 */
687 1.1 cgd blkfree(pargv), pargv = 0, pargsiz = 0;
688 1.1 cgd /* mg, 21.dec.88 */
689 1.1 cgd arginp = cp;
690 1.1 cgd while (*cp)
691 1.1 cgd *cp++ &= TRIM;
692 1.8 mycroft
693 1.8 mycroft /*
694 1.8 mycroft * In the child ``forget'' everything about current aliases or
695 1.8 mycroft * eval vectors.
696 1.8 mycroft */
697 1.8 mycroft alvec = NULL;
698 1.8 mycroft evalvec = NULL;
699 1.8 mycroft alvecp = NULL;
700 1.8 mycroft evalp = NULL;
701 1.1 cgd (void) lex(¶ml);
702 1.16 mycroft if (seterr)
703 1.1 cgd stderror(ERR_OLD);
704 1.1 cgd alias(¶ml);
705 1.1 cgd t = syntax(paraml.next, ¶ml, 0);
706 1.16 mycroft if (seterr)
707 1.1 cgd stderror(ERR_OLD);
708 1.1 cgd if (t)
709 1.1 cgd t->t_dflg |= F_NOFORK;
710 1.18 wiz (void)signal(SIGTSTP, SIG_IGN);
711 1.18 wiz (void)signal(SIGTTIN, SIG_IGN);
712 1.18 wiz (void)signal(SIGTTOU, SIG_IGN);
713 1.1 cgd execute(t, -1, NULL, NULL);
714 1.1 cgd exitstat();
715 1.1 cgd }
716 1.18 wiz xfree((ptr_t)cp);
717 1.18 wiz (void)close(pvec[1]);
718 1.1 cgd c = 0;
719 1.1 cgd ip = NULL;
720 1.1 cgd do {
721 1.18 wiz int cnt;
722 1.18 wiz
723 1.18 wiz cnt = 0;
724 1.1 cgd
725 1.1 cgd for (;;) {
726 1.1 cgd if (icnt == 0) {
727 1.18 wiz int i;
728 1.1 cgd
729 1.1 cgd ip = ibuf;
730 1.1 cgd do
731 1.17 christos icnt = read(pvec[0], tibuf, BUFSIZE);
732 1.1 cgd while (icnt == -1 && errno == EINTR);
733 1.1 cgd if (icnt <= 0) {
734 1.1 cgd c = -1;
735 1.1 cgd break;
736 1.1 cgd }
737 1.1 cgd for (i = 0; i < icnt; i++)
738 1.1 cgd ip[i] = (unsigned char) tibuf[i];
739 1.1 cgd }
740 1.1 cgd if (hadnl)
741 1.1 cgd break;
742 1.1 cgd --icnt;
743 1.1 cgd c = (*ip++ & TRIM);
744 1.1 cgd if (c == 0)
745 1.1 cgd break;
746 1.1 cgd if (c == '\n') {
747 1.1 cgd /*
748 1.1 cgd * Continue around the loop one more time, so that we can eat
749 1.1 cgd * the last newline without terminating this word.
750 1.1 cgd */
751 1.1 cgd hadnl = 1;
752 1.1 cgd continue;
753 1.1 cgd }
754 1.1 cgd if (!quoted && (c == ' ' || c == '\t'))
755 1.1 cgd break;
756 1.1 cgd cnt++;
757 1.1 cgd psave(c | quoted);
758 1.1 cgd }
759 1.1 cgd /*
760 1.1 cgd * Unless at end-of-file, we will form a new word here if there were
761 1.1 cgd * characters in the word, or in any case when we take text literally.
762 1.1 cgd * If we didn't make empty words here when literal was set then we
763 1.1 cgd * would lose blank lines.
764 1.1 cgd */
765 1.1 cgd if (c != -1 && (cnt || literal))
766 1.1 cgd pword();
767 1.1 cgd hadnl = 0;
768 1.1 cgd } while (c >= 0);
769 1.18 wiz (void)close(pvec[0]);
770 1.1 cgd pwait();
771 1.1 cgd prestjob();
772 1.1 cgd }
773 1.1 cgd
774 1.1 cgd static void
775 1.18 wiz psave(int c)
776 1.1 cgd {
777 1.16 mycroft if (--pnleft <= 0)
778 1.1 cgd stderror(ERR_WTOOLONG);
779 1.1 cgd *pargcp++ = c;
780 1.1 cgd }
781 1.1 cgd
782 1.1 cgd static void
783 1.18 wiz pword(void)
784 1.1 cgd {
785 1.1 cgd psave(0);
786 1.1 cgd if (pargc == pargsiz - 1) {
787 1.1 cgd pargsiz += GLOBSPACE;
788 1.18 wiz pargv = (Char **)xrealloc((ptr_t)pargv,
789 1.18 wiz (size_t)pargsiz * sizeof(Char *));
790 1.1 cgd }
791 1.1 cgd pargv[pargc++] = Strsave(pargs);
792 1.1 cgd pargv[pargc] = NULL;
793 1.1 cgd pargcp = pargs;
794 1.1 cgd pnleft = MAXPATHLEN - 4;
795 1.1 cgd }
796 1.1 cgd
797 1.8 mycroft int
798 1.18 wiz Gmatch(Char *string, Char *pattern)
799 1.8 mycroft {
800 1.8 mycroft Char **blk, **p;
801 1.18 wiz int gpol, gres;
802 1.18 wiz
803 1.18 wiz gpol = 1;
804 1.18 wiz gres = 0;
805 1.8 mycroft
806 1.8 mycroft if (*pattern == '^') {
807 1.8 mycroft gpol = 0;
808 1.8 mycroft pattern++;
809 1.8 mycroft }
810 1.8 mycroft
811 1.18 wiz blk = (Char **)xmalloc(GLOBSPACE * sizeof(Char *));
812 1.8 mycroft blk[0] = Strsave(pattern);
813 1.8 mycroft blk[1] = NULL;
814 1.8 mycroft
815 1.8 mycroft expbrace(&blk, NULL, GLOBSPACE);
816 1.8 mycroft
817 1.8 mycroft for (p = blk; *p; p++)
818 1.8 mycroft gres |= pmatch(string, *p);
819 1.8 mycroft
820 1.8 mycroft blkfree(blk);
821 1.8 mycroft return(gres == gpol);
822 1.8 mycroft }
823 1.8 mycroft
824 1.8 mycroft static int
825 1.18 wiz pmatch(Char *string, Char *pattern)
826 1.1 cgd {
827 1.18 wiz int match, negate_range;
828 1.18 wiz Char patternc, rangec, stringc;
829 1.1 cgd
830 1.1 cgd for (;; ++string) {
831 1.1 cgd stringc = *string & TRIM;
832 1.1 cgd patternc = *pattern++;
833 1.1 cgd switch (patternc) {
834 1.1 cgd case 0:
835 1.1 cgd return (stringc == 0);
836 1.1 cgd case '?':
837 1.1 cgd if (stringc == 0)
838 1.1 cgd return (0);
839 1.1 cgd break;
840 1.1 cgd case '*':
841 1.1 cgd if (!*pattern)
842 1.1 cgd return (1);
843 1.1 cgd while (*string)
844 1.1 cgd if (Gmatch(string++, pattern))
845 1.1 cgd return (1);
846 1.1 cgd return (0);
847 1.1 cgd case '[':
848 1.1 cgd match = 0;
849 1.8 mycroft if ((negate_range = (*pattern == '^')) != 0)
850 1.8 mycroft pattern++;
851 1.8 mycroft while ((rangec = *pattern++) != '\0') {
852 1.1 cgd if (rangec == ']')
853 1.8 mycroft break;
854 1.1 cgd if (match)
855 1.1 cgd continue;
856 1.8 mycroft if (rangec == '-' && *(pattern-2) != '[' && *pattern != ']') {
857 1.1 cgd match = (stringc <= (*pattern & TRIM) &&
858 1.8 mycroft (*(pattern-2) & TRIM) <= stringc);
859 1.1 cgd pattern++;
860 1.1 cgd }
861 1.8 mycroft else
862 1.8 mycroft match = (stringc == (rangec & TRIM));
863 1.1 cgd }
864 1.16 mycroft if (rangec == 0)
865 1.1 cgd stderror(ERR_NAME | ERR_MISSING, ']');
866 1.8 mycroft if (match == negate_range)
867 1.8 mycroft return (0);
868 1.1 cgd break;
869 1.1 cgd default:
870 1.1 cgd if ((patternc & TRIM) != stringc)
871 1.1 cgd return (0);
872 1.1 cgd break;
873 1.1 cgd
874 1.1 cgd }
875 1.1 cgd }
876 1.1 cgd }
877 1.1 cgd
878 1.1 cgd void
879 1.18 wiz Gcat(Char *s1, Char *s2)
880 1.1 cgd {
881 1.11 tls Char *p, *q;
882 1.18 wiz int n;
883 1.1 cgd
884 1.8 mycroft for (p = s1; *p++;)
885 1.8 mycroft continue;
886 1.8 mycroft for (q = s2; *q++;)
887 1.8 mycroft continue;
888 1.1 cgd n = (p - s1) + (q - s2) - 1;
889 1.1 cgd if (++gargc >= gargsiz) {
890 1.1 cgd gargsiz += GLOBSPACE;
891 1.18 wiz gargv = (Char **)xrealloc((ptr_t)gargv,
892 1.18 wiz (size_t)gargsiz * sizeof(Char *));
893 1.1 cgd }
894 1.1 cgd gargv[gargc] = 0;
895 1.18 wiz p = gargv[gargc - 1] = (Char *)xmalloc((size_t)n * sizeof(Char));
896 1.8 mycroft for (q = s1; (*p++ = *q++) != '\0';)
897 1.8 mycroft continue;
898 1.8 mycroft for (p--, q = s2; (*p++ = *q++) != '\0';)
899 1.8 mycroft continue;
900 1.1 cgd }
901 1.1 cgd
902 1.1 cgd #ifdef FILEC
903 1.1 cgd int
904 1.18 wiz sortscmp(const ptr_t a, const ptr_t b)
905 1.1 cgd {
906 1.1 cgd #if defined(NLS) && !defined(NOSTRCOLL)
907 1.18 wiz char buf[2048];
908 1.1 cgd #endif
909 1.1 cgd
910 1.1 cgd if (!a) /* check for NULL */
911 1.1 cgd return (b ? 1 : 0);
912 1.1 cgd if (!b)
913 1.1 cgd return (-1);
914 1.1 cgd
915 1.8 mycroft if (!*(Char **)a) /* check for NULL */
916 1.8 mycroft return (*(Char **)b ? 1 : 0);
917 1.8 mycroft if (!*(Char **)b)
918 1.1 cgd return (-1);
919 1.1 cgd
920 1.1 cgd #if defined(NLS) && !defined(NOSTRCOLL)
921 1.18 wiz (void)strcpy(buf, short2str(*(Char **)a));
922 1.18 wiz return ((int)strcoll(buf, short2str(*(Char **)b)));
923 1.1 cgd #else
924 1.18 wiz return ((int)Strcmp(*(Char **)a, *(Char **)b));
925 1.1 cgd #endif
926 1.1 cgd }
927 1.1 cgd #endif /* FILEC */
928