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