1*509d26c5Snia /* $NetBSD: glob.c,v 1.32 2024/04/24 15:49:03 nia Exp $ */
249f0ad86Scgd
361f28255Scgd /*-
4cee2bad8Smycroft * Copyright (c) 1980, 1991, 1993
5cee2bad8Smycroft * The Regents of the University of California. All rights reserved.
661f28255Scgd *
761f28255Scgd * Redistribution and use in source and binary forms, with or without
861f28255Scgd * modification, are permitted provided that the following conditions
961f28255Scgd * are met:
1061f28255Scgd * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd * notice, this list of conditions and the following disclaimer.
1261f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd * notice, this list of conditions and the following disclaimer in the
1461f28255Scgd * documentation and/or other materials provided with the distribution.
15b5b29542Sagc * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd * may be used to endorse or promote products derived from this software
1761f28255Scgd * without specific prior written permission.
1861f28255Scgd *
1961f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd * SUCH DAMAGE.
3061f28255Scgd */
3161f28255Scgd
328ea378c6Schristos #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
3449f0ad86Scgd #if 0
3549f0ad86Scgd static char sccsid[] = "@(#)glob.c 8.1 (Berkeley) 5/31/93";
3649f0ad86Scgd #else
37*509d26c5Snia __RCSID("$NetBSD: glob.c,v 1.32 2024/04/24 15:49:03 nia Exp $");
3849f0ad86Scgd #endif
3961f28255Scgd #endif /* not lint */
4061f28255Scgd
4161f28255Scgd #include <sys/param.h>
42b771e65bSwiz
4361f28255Scgd #include <errno.h>
44b771e65bSwiz #include <glob.h>
4518158540Swiz #include <stdarg.h>
4637e39248Schristos #include <stddef.h>
4761f28255Scgd #include <stdlib.h>
4861f28255Scgd #include <string.h>
4961f28255Scgd #include <unistd.h>
50b771e65bSwiz
5161f28255Scgd #include "csh.h"
5261f28255Scgd #include "extern.h"
5361f28255Scgd
54cee2bad8Smycroft static int noglob;
55b771e65bSwiz static int gargsiz, pargsiz;
5661f28255Scgd
5761f28255Scgd /*
5861f28255Scgd * Values for gflag
5961f28255Scgd */
6061f28255Scgd #define G_NONE 0 /* No globbing needed */
6161f28255Scgd #define G_GLOB 1 /* string contains *?[] characters */
6261f28255Scgd #define G_CSH 2 /* string contains ~`{ characters */
6361f28255Scgd
6461f28255Scgd #define GLOBSPACE 100 /* Alloc increment */
6561f28255Scgd
6661f28255Scgd #define LBRC '{'
6761f28255Scgd #define RBRC '}'
6861f28255Scgd #define LBRK '['
6961f28255Scgd #define RBRK ']'
7061f28255Scgd #define EOS '\0'
7161f28255Scgd
7261f28255Scgd Char **gargv = NULL;
7361f28255Scgd Char **pargv = NULL;
74b771e65bSwiz long gargc = 0;
7561f28255Scgd long pargc = 0;
7661f28255Scgd
7761f28255Scgd /*
7861f28255Scgd * globbing is now done in two stages. In the first pass we expand
7961f28255Scgd * csh globbing idioms ~`{ and then we proceed doing the normal
8061f28255Scgd * globbing if needed ?*[
8161f28255Scgd *
8261f28255Scgd * Csh type globbing is handled in globexpand() and the rest is
8361f28255Scgd * handled in glob() which is part of the 4.4BSD libc.
8461f28255Scgd *
8561f28255Scgd */
86b771e65bSwiz static Char *globtilde(Char **, Char *);
87b771e65bSwiz static Char *handleone(Char *, Char **, int);
88b771e65bSwiz static Char **libglob(Char **);
89b771e65bSwiz static Char **globexpand(Char **);
90b771e65bSwiz static int globbrace(Char *, Char *, Char ***);
9137e39248Schristos static void expbrace(Char ***, Char ***, size_t);
928080de59Schristos static int pmatch(const Char *, const Char *);
93b771e65bSwiz static void pword(void);
94b771e65bSwiz static void psave(int);
95b79c2ef2Schristos static void backeval(Char *, int);
9661f28255Scgd
9761f28255Scgd static Char *
globtilde(Char ** nv,Char * s)98b771e65bSwiz globtilde(Char **nv, Char *s)
9961f28255Scgd {
100b771e65bSwiz Char gbuf[MAXPATHLEN], *b, *e, *gstart, *u;
10161f28255Scgd
10261f28255Scgd gstart = gbuf;
10361f28255Scgd *gstart++ = *s++;
10461f28255Scgd u = s;
105cee2bad8Smycroft for (b = gstart, e = &gbuf[MAXPATHLEN - 1];
106cee2bad8Smycroft *s && *s != '/' && *s != ':' && b < e;
107cee2bad8Smycroft *b++ = *s++)
108cee2bad8Smycroft continue;
10961f28255Scgd *b = EOS;
11061f28255Scgd if (gethdir(gstart)) {
11161f28255Scgd blkfree(nv);
11261f28255Scgd if (*gstart)
113cee2bad8Smycroft stderror(ERR_UNKUSER, vis_str(gstart));
11461f28255Scgd else
11561f28255Scgd stderror(ERR_NOHOME);
11661f28255Scgd }
11761f28255Scgd b = &gstart[Strlen(gstart)];
11861f28255Scgd while (*s)
11961f28255Scgd *b++ = *s++;
12061f28255Scgd *b = EOS;
12161f28255Scgd --u;
1221767ce60Schristos free(u);
12361f28255Scgd return (Strsave(gstart));
12461f28255Scgd }
12561f28255Scgd
12661f28255Scgd static int
globbrace(Char * s,Char * p,Char *** bl)127b771e65bSwiz globbrace(Char *s, Char *p, Char ***bl)
12861f28255Scgd {
12961f28255Scgd Char gbuf[MAXPATHLEN];
130b771e65bSwiz Char *lm, *pe, *pl, *pm, **nv, **vl;
131b771e65bSwiz int i, len, size;
13261f28255Scgd
133b771e65bSwiz size = GLOBSPACE;
134*509d26c5Snia nv = vl = xreallocarray(NULL, sizeof(Char *), (size_t)size);
13561f28255Scgd *vl = NULL;
13661f28255Scgd len = 0;
13761f28255Scgd /* copy part up to the brace */
13861f28255Scgd for (lm = gbuf, p = s; *p != LBRC; *lm++ = *p++)
13961f28255Scgd continue;
14061f28255Scgd
14161f28255Scgd /* check for balanced braces */
14261f28255Scgd for (i = 0, pe = ++p; *pe; pe++)
14361f28255Scgd if (*pe == LBRK) {
14461f28255Scgd /* Ignore everything between [] */
14561f28255Scgd for (++pe; *pe != RBRK && *pe != EOS; pe++)
14661f28255Scgd continue;
14761f28255Scgd if (*pe == EOS) {
14861f28255Scgd blkfree(nv);
149cee2bad8Smycroft return (-RBRK);
15061f28255Scgd }
15161f28255Scgd }
15261f28255Scgd else if (*pe == LBRC)
15361f28255Scgd i++;
15461f28255Scgd else if (*pe == RBRC) {
15561f28255Scgd if (i == 0)
15661f28255Scgd break;
15761f28255Scgd i--;
15861f28255Scgd }
15961f28255Scgd
1604a0f440fScgd if (i != 0 || *pe == '\0') {
16161f28255Scgd blkfree(nv);
1624a0f440fScgd return (-RBRC);
16361f28255Scgd }
16461f28255Scgd
16561f28255Scgd for (i = 0, pl = pm = p; pm <= pe; pm++)
16661f28255Scgd switch (*pm) {
16761f28255Scgd case LBRK:
16861f28255Scgd for (++pm; *pm != RBRK && *pm != EOS; pm++)
16961f28255Scgd continue;
17061f28255Scgd if (*pm == EOS) {
17161f28255Scgd *vl = NULL;
17261f28255Scgd blkfree(nv);
17361f28255Scgd return (-RBRK);
17461f28255Scgd }
17561f28255Scgd break;
17661f28255Scgd case LBRC:
17761f28255Scgd i++;
17861f28255Scgd break;
17961f28255Scgd case RBRC:
18061f28255Scgd if (i) {
18161f28255Scgd i--;
18261f28255Scgd break;
18361f28255Scgd }
18461f28255Scgd /* FALLTHROUGH */
18561f28255Scgd case ',':
18661f28255Scgd if (i && *pm == ',')
18761f28255Scgd break;
18861f28255Scgd else {
18961f28255Scgd Char savec = *pm;
19061f28255Scgd
19161f28255Scgd *pm = EOS;
19261f28255Scgd (void)Strcpy(lm, pl);
19361f28255Scgd (void)Strcat(gbuf, pe + 1);
19461f28255Scgd *pm = savec;
19561f28255Scgd *vl++ = Strsave(gbuf);
19661f28255Scgd len++;
19761f28255Scgd pl = pm + 1;
19861f28255Scgd if (vl == &nv[size]) {
19961f28255Scgd size += GLOBSPACE;
200*509d26c5Snia nv = xreallocarray(nv, (size_t)size, sizeof(Char *));
20161f28255Scgd vl = &nv[size - GLOBSPACE];
20261f28255Scgd }
20361f28255Scgd }
20461f28255Scgd break;
205cee2bad8Smycroft default:
206cee2bad8Smycroft break;
20761f28255Scgd }
20861f28255Scgd *vl = NULL;
20961f28255Scgd *bl = nv;
21061f28255Scgd return (len);
21161f28255Scgd }
21261f28255Scgd
213cee2bad8Smycroft static void
expbrace(Char *** nvp,Char *** elp,size_t size)21437e39248Schristos expbrace(Char ***nvp, Char ***elp, size_t size)
215cee2bad8Smycroft {
2165e9a2dddSchristos Char **ex, **nv, *s, **vl;
217cee2bad8Smycroft
218cee2bad8Smycroft vl = nv = *nvp;
219cee2bad8Smycroft if (elp != NULL)
2205e9a2dddSchristos ex = *elp;
221cee2bad8Smycroft else
2225e9a2dddSchristos for (ex = vl; *ex; ex++)
223cee2bad8Smycroft continue;
224cee2bad8Smycroft
225cee2bad8Smycroft for (s = *vl; s; s = *++vl) {
226b771e65bSwiz Char *b, **bp, **vp;
227cee2bad8Smycroft
228cee2bad8Smycroft /* leave {} untouched for find */
229cee2bad8Smycroft if (s[0] == '{' && (s[1] == '\0' || (s[1] == '}' && s[2] == '\0')))
230cee2bad8Smycroft continue;
231cee2bad8Smycroft if ((b = Strchr(s, '{')) != NULL) {
232cee2bad8Smycroft Char **bl;
233cee2bad8Smycroft int len;
234cee2bad8Smycroft
235cee2bad8Smycroft if ((len = globbrace(s, b, &bl)) < 0) {
2361767ce60Schristos free(nv);
237cee2bad8Smycroft stderror(ERR_MISSING, -len);
238cee2bad8Smycroft }
2391767ce60Schristos free(s);
240cee2bad8Smycroft if (len == 1) {
241cee2bad8Smycroft *vl-- = *bl;
2421767ce60Schristos free(bl);
243cee2bad8Smycroft continue;
244cee2bad8Smycroft }
245cee2bad8Smycroft len = blklen(bl);
2465e9a2dddSchristos if (&ex[len] >= &nv[size]) {
24737e39248Schristos ptrdiff_t l, e;
248cee2bad8Smycroft
2495e9a2dddSchristos l = &ex[len] - &nv[size];
25037e39248Schristos size += (size_t)(GLOBSPACE > l ? GLOBSPACE : l);
251cee2bad8Smycroft l = vl - nv;
2525e9a2dddSchristos e = ex - nv;
253*509d26c5Snia nv = xreallocarray(nv, size, sizeof(Char *));
254cee2bad8Smycroft vl = nv + l;
2555e9a2dddSchristos ex = nv + e;
256cee2bad8Smycroft }
257cee2bad8Smycroft vp = vl--;
258cee2bad8Smycroft *vp = *bl;
259cee2bad8Smycroft len--;
2605e9a2dddSchristos for (bp = ex; bp != vp; bp--)
261cee2bad8Smycroft bp[len] = *bp;
2625e9a2dddSchristos ex += len;
263cee2bad8Smycroft vp++;
264cee2bad8Smycroft for (bp = bl + 1; *bp; *vp++ = *bp++)
265cee2bad8Smycroft continue;
2661767ce60Schristos free(bl);
267cee2bad8Smycroft }
268cee2bad8Smycroft
269cee2bad8Smycroft }
270cee2bad8Smycroft if (elp != NULL)
2715e9a2dddSchristos *elp = ex;
272cee2bad8Smycroft *nvp = nv;
273cee2bad8Smycroft }
274cee2bad8Smycroft
27561f28255Scgd static Char **
globexpand(Char ** v)276b771e65bSwiz globexpand(Char **v)
27761f28255Scgd {
2785e9a2dddSchristos Char **ex, **nv, *s, **vl;
27937e39248Schristos size_t size;
28061f28255Scgd
281b771e65bSwiz size = GLOBSPACE;
282*509d26c5Snia nv = vl = xreallocarray(NULL, sizeof(Char *), size);
28361f28255Scgd *vl = NULL;
28461f28255Scgd
28561f28255Scgd /*
28661f28255Scgd * Step 1: expand backquotes.
28761f28255Scgd */
288cee2bad8Smycroft while ((s = *v++) != NULL) {
28961f28255Scgd if (Strchr(s, '`')) {
29061f28255Scgd int i;
29161f28255Scgd
29261f28255Scgd (void) dobackp(s, 0);
29361f28255Scgd for (i = 0; i < pargc; i++) {
29461f28255Scgd *vl++ = pargv[i];
29561f28255Scgd if (vl == &nv[size]) {
29661f28255Scgd size += GLOBSPACE;
297*509d26c5Snia nv = xreallocarray(nv, size, sizeof(Char *));
29861f28255Scgd vl = &nv[size - GLOBSPACE];
29961f28255Scgd }
30061f28255Scgd }
3011767ce60Schristos free(pargv);
30261f28255Scgd pargv = NULL;
30361f28255Scgd }
30461f28255Scgd else {
30561f28255Scgd *vl++ = Strsave(s);
30661f28255Scgd if (vl == &nv[size]) {
30761f28255Scgd size += GLOBSPACE;
308*509d26c5Snia nv = xreallocarray(nv, size, sizeof(Char *));
30961f28255Scgd vl = &nv[size - GLOBSPACE];
31061f28255Scgd }
31161f28255Scgd }
31261f28255Scgd }
31361f28255Scgd *vl = NULL;
31461f28255Scgd
31561f28255Scgd if (noglob)
31661f28255Scgd return (nv);
31761f28255Scgd
31861f28255Scgd /*
31961f28255Scgd * Step 2: expand braces
32061f28255Scgd */
3215e9a2dddSchristos ex = vl;
3225e9a2dddSchristos expbrace(&nv, &ex, size);
32361f28255Scgd
32461f28255Scgd /*
32561f28255Scgd * Step 3: expand ~
32661f28255Scgd */
32761f28255Scgd vl = nv;
32861f28255Scgd for (s = *vl; s; s = *++vl)
32961f28255Scgd if (*s == '~')
33061f28255Scgd *vl = globtilde(nv, s);
33161f28255Scgd vl = nv;
33261f28255Scgd return (vl);
33361f28255Scgd }
33461f28255Scgd
33561f28255Scgd static Char *
handleone(Char * str,Char ** vl,int action)336b771e65bSwiz handleone(Char *str, Char **vl, int action)
33761f28255Scgd {
338b771e65bSwiz Char *cp, **vlp;
33961f28255Scgd
340b771e65bSwiz vlp = vl;
34161f28255Scgd switch (action) {
34261f28255Scgd case G_ERROR:
343cee2bad8Smycroft setname(vis_str(str));
34461f28255Scgd blkfree(vl);
34561f28255Scgd stderror(ERR_NAME | ERR_AMBIG);
346cdbd74daSmycroft /* NOTREACHED */
34761f28255Scgd case G_APPEND:
34861f28255Scgd trim(vlp);
34961f28255Scgd str = Strsave(*vlp++);
35061f28255Scgd do {
35161f28255Scgd cp = Strspl(str, STRspace);
3521767ce60Schristos free(str);
35361f28255Scgd str = Strspl(cp, *vlp);
3541767ce60Schristos free(cp);
35561f28255Scgd }
35661f28255Scgd while (*++vlp);
35761f28255Scgd blkfree(vl);
35861f28255Scgd break;
35961f28255Scgd case G_IGNORE:
36061f28255Scgd str = Strsave(strip(*vlp));
36161f28255Scgd blkfree(vl);
36261f28255Scgd break;
363cee2bad8Smycroft default:
364cee2bad8Smycroft break;
36561f28255Scgd }
36661f28255Scgd return (str);
36761f28255Scgd }
36861f28255Scgd
36961f28255Scgd static Char **
libglob(Char ** vl)370b771e65bSwiz libglob(Char **vl)
37161f28255Scgd {
37261f28255Scgd glob_t globv;
37361f28255Scgd char *ptr;
374b771e65bSwiz int gflgs, magic, match, nonomatch;
375b771e65bSwiz
376b771e65bSwiz gflgs = GLOB_NOMAGIC;
377b771e65bSwiz magic = 0;
378b771e65bSwiz match = 0;
379b771e65bSwiz nonomatch = adrof(STRnonomatch) != 0;
380cee2bad8Smycroft
381cee2bad8Smycroft if (!vl || !vl[0])
382cee2bad8Smycroft return (vl);
38361f28255Scgd
38461f28255Scgd globv.gl_offs = 0;
38561f28255Scgd globv.gl_pathv = 0;
38661f28255Scgd globv.gl_pathc = 0;
387cee2bad8Smycroft
388cee2bad8Smycroft if (nonomatch)
389cee2bad8Smycroft gflgs |= GLOB_NOCHECK;
390cee2bad8Smycroft
39161f28255Scgd do {
39261f28255Scgd ptr = short2qstr(*vl);
39361f28255Scgd switch (glob(ptr, gflgs, 0, &globv)) {
394b61a8b64Skleink case GLOB_ABORTED:
395cee2bad8Smycroft setname(vis_str(*vl));
39661f28255Scgd stderror(ERR_NAME | ERR_GLOB);
39761f28255Scgd /* NOTREACHED */
39861f28255Scgd case GLOB_NOSPACE:
39961f28255Scgd stderror(ERR_NOMEM);
40061f28255Scgd /* NOTREACHED */
40161f28255Scgd default:
40261f28255Scgd break;
40361f28255Scgd }
404cee2bad8Smycroft if (globv.gl_flags & GLOB_MAGCHAR) {
405cee2bad8Smycroft match |= (globv.gl_matchc != 0);
406cee2bad8Smycroft magic = 1;
407cee2bad8Smycroft }
40861f28255Scgd gflgs |= GLOB_APPEND;
40961f28255Scgd }
41061f28255Scgd while (*++vl);
411cee2bad8Smycroft vl = (globv.gl_pathc == 0 || (magic && !match && !nonomatch)) ?
412cee2bad8Smycroft NULL : blk2short(globv.gl_pathv);
41361f28255Scgd globfree(&globv);
41461f28255Scgd return (vl);
41561f28255Scgd }
41661f28255Scgd
41761f28255Scgd Char *
globone(Char * str,int action)418b771e65bSwiz globone(Char *str, int action)
41961f28255Scgd {
42061f28255Scgd Char *v[2], **vl, **vo;
421cee2bad8Smycroft int gflg;
42261f28255Scgd
42361f28255Scgd noglob = adrof(STRnoglob) != 0;
42461f28255Scgd gflag = 0;
42561f28255Scgd v[0] = str;
42661f28255Scgd v[1] = 0;
42761f28255Scgd tglob(v);
428cee2bad8Smycroft gflg = gflag;
429cee2bad8Smycroft if (gflg == G_NONE)
43061f28255Scgd return (strip(Strsave(str)));
43161f28255Scgd
432cee2bad8Smycroft if (gflg & G_CSH) {
43361f28255Scgd /*
43461f28255Scgd * Expand back-quote, tilde and brace
43561f28255Scgd */
43661f28255Scgd vo = globexpand(v);
437cee2bad8Smycroft if (noglob || (gflg & G_GLOB) == 0) {
43861f28255Scgd if (vo[0] == NULL) {
4391767ce60Schristos free(vo);
44061f28255Scgd return (Strsave(STRNULL));
44161f28255Scgd }
44261f28255Scgd if (vo[1] != NULL)
44361f28255Scgd return (handleone(str, vo, action));
44461f28255Scgd else {
44561f28255Scgd str = strip(vo[0]);
4461767ce60Schristos free(vo);
44761f28255Scgd return (str);
44861f28255Scgd }
44961f28255Scgd }
45061f28255Scgd }
451cee2bad8Smycroft else if (noglob || (gflg & G_GLOB) == 0)
45261f28255Scgd return (strip(Strsave(str)));
45361f28255Scgd else
45461f28255Scgd vo = v;
45561f28255Scgd
45661f28255Scgd vl = libglob(vo);
457cee2bad8Smycroft if ((gflg & G_CSH) && vl != vo)
45861f28255Scgd blkfree(vo);
45961f28255Scgd if (vl == NULL) {
460cee2bad8Smycroft setname(vis_str(str));
46161f28255Scgd stderror(ERR_NAME | ERR_NOMATCH);
46261f28255Scgd }
46361f28255Scgd if (vl[0] == NULL) {
4641767ce60Schristos free(vl);
46561f28255Scgd return (Strsave(STRNULL));
46661f28255Scgd }
46761f28255Scgd if (vl[1] != NULL)
46861f28255Scgd return (handleone(str, vl, action));
46961f28255Scgd else {
47061f28255Scgd str = strip(*vl);
4711767ce60Schristos free(vl);
47261f28255Scgd return (str);
47361f28255Scgd }
47461f28255Scgd }
47561f28255Scgd
47661f28255Scgd Char **
globall(Char ** v)477b771e65bSwiz globall(Char **v)
47861f28255Scgd {
47961f28255Scgd Char **vl, **vo;
480b771e65bSwiz int gflg;
48161f28255Scgd
482b771e65bSwiz gflg = gflag;
48361f28255Scgd if (!v || !v[0]) {
48461f28255Scgd gargv = saveblk(v);
48561f28255Scgd gargc = blklen(gargv);
48661f28255Scgd return (gargv);
48761f28255Scgd }
48861f28255Scgd
48961f28255Scgd noglob = adrof(STRnoglob) != 0;
49061f28255Scgd
491cee2bad8Smycroft if (gflg & G_CSH)
49261f28255Scgd /*
49361f28255Scgd * Expand back-quote, tilde and brace
49461f28255Scgd */
49561f28255Scgd vl = vo = globexpand(v);
49661f28255Scgd else
49761f28255Scgd vl = vo = saveblk(v);
49861f28255Scgd
4999dccee4fSmycroft if (!noglob && (gflg & G_GLOB)) {
50061f28255Scgd vl = libglob(vo);
501cee2bad8Smycroft if ((gflg & G_CSH) && vl != vo)
50261f28255Scgd blkfree(vo);
50361f28255Scgd }
504cee2bad8Smycroft else
505cee2bad8Smycroft trim(vl);
50661f28255Scgd
50761f28255Scgd gargc = vl ? blklen(vl) : 0;
50861f28255Scgd return (gargv = vl);
50961f28255Scgd }
51061f28255Scgd
51161f28255Scgd void
ginit(void)512b771e65bSwiz ginit(void)
51361f28255Scgd {
51461f28255Scgd gargsiz = GLOBSPACE;
515*509d26c5Snia gargv = xreallocarray(NULL, sizeof(Char *), (size_t)gargsiz);
51661f28255Scgd gargv[0] = 0;
51761f28255Scgd gargc = 0;
51861f28255Scgd }
51961f28255Scgd
52061f28255Scgd void
rscan(Char ** t,void (* f)(int))521b771e65bSwiz rscan(Char **t, void (*f)(int))
52261f28255Scgd {
52376adbe2bStls Char *p;
52461f28255Scgd
525cee2bad8Smycroft while ((p = *t++) != NULL)
52661f28255Scgd while (*p)
52761f28255Scgd (*f) (*p++);
52861f28255Scgd }
52961f28255Scgd
53061f28255Scgd void
trim(Char ** t)531b771e65bSwiz trim(Char **t)
53261f28255Scgd {
53376adbe2bStls Char *p;
53461f28255Scgd
535cee2bad8Smycroft while ((p = *t++) != NULL)
53661f28255Scgd while (*p)
53761f28255Scgd *p++ &= TRIM;
53861f28255Scgd }
53961f28255Scgd
54061f28255Scgd void
tglob(Char ** t)541b771e65bSwiz tglob(Char **t)
54261f28255Scgd {
54376adbe2bStls Char *p, c;
54461f28255Scgd
545cee2bad8Smycroft while ((p = *t++) != NULL) {
54661f28255Scgd if (*p == '~' || *p == '=')
54761f28255Scgd gflag |= G_CSH;
54861f28255Scgd else if (*p == '{' &&
549cee2bad8Smycroft (p[1] == '\0' || (p[1] == '}' && p[2] == '\0')))
55061f28255Scgd continue;
551cee2bad8Smycroft while ((c = *p++) != '\0') {
552cee2bad8Smycroft /*
553cee2bad8Smycroft * eat everything inside the matching backquotes
554cee2bad8Smycroft */
555cee2bad8Smycroft if (c == '`') {
556cee2bad8Smycroft gflag |= G_CSH;
557cee2bad8Smycroft while (*p && *p != '`')
558cee2bad8Smycroft if (*p++ == '\\') {
559cee2bad8Smycroft if (*p) /* Quoted chars */
560cee2bad8Smycroft p++;
561cee2bad8Smycroft else
562cee2bad8Smycroft break;
563cee2bad8Smycroft }
564cee2bad8Smycroft if (*p) /* The matching ` */
565cee2bad8Smycroft p++;
566cee2bad8Smycroft else
567cee2bad8Smycroft break;
568cee2bad8Smycroft }
569cee2bad8Smycroft else if (c == '{')
570cee2bad8Smycroft gflag |= G_CSH;
571cee2bad8Smycroft else if (isglob(c))
572cee2bad8Smycroft gflag |= G_GLOB;
573cee2bad8Smycroft }
57461f28255Scgd }
57561f28255Scgd }
57661f28255Scgd
57761f28255Scgd /*
57861f28255Scgd * Command substitute cp. If literal, then this is a substitution from a
57961f28255Scgd * << redirection, and so we should not crunch blanks and tabs, separating
58061f28255Scgd * words only at newlines.
58161f28255Scgd */
58261f28255Scgd Char **
dobackp(Char * cp,int literal)583b79c2ef2Schristos dobackp(Char *cp, int literal)
58461f28255Scgd {
585b771e65bSwiz Char word[MAXPATHLEN], *ep, *lp, *rp;
58661f28255Scgd
58761f28255Scgd if (pargv) {
588cee2bad8Smycroft #ifdef notdef
58961f28255Scgd abort();
590cee2bad8Smycroft #endif
59161f28255Scgd blkfree(pargv);
59261f28255Scgd }
59361f28255Scgd pargsiz = GLOBSPACE;
594*509d26c5Snia pargv = xreallocarray(NULL, sizeof(Char *), (size_t)pargsiz);
59561f28255Scgd pargv[0] = NULL;
59661f28255Scgd pargcp = pargs = word;
59761f28255Scgd pargc = 0;
59861f28255Scgd pnleft = MAXPATHLEN - 4;
59961f28255Scgd for (;;) {
60061f28255Scgd for (lp = cp; *lp != '`'; lp++) {
60161f28255Scgd if (*lp == 0) {
60261f28255Scgd if (pargcp != pargs)
60361f28255Scgd pword();
60461f28255Scgd return (pargv);
60561f28255Scgd }
60661f28255Scgd psave(*lp);
60761f28255Scgd }
60861f28255Scgd lp++;
60961f28255Scgd for (rp = lp; *rp && *rp != '`'; rp++)
61061f28255Scgd if (*rp == '\\') {
61161f28255Scgd rp++;
61261f28255Scgd if (!*rp)
61361f28255Scgd goto oops;
61461f28255Scgd }
615cdbd74daSmycroft if (!*rp) {
616cdbd74daSmycroft oops:
617cdbd74daSmycroft stderror(ERR_UNMATCHED, '`');
618cdbd74daSmycroft }
61961f28255Scgd ep = Strsave(lp);
62061f28255Scgd ep[rp - lp] = 0;
62161f28255Scgd backeval(ep, literal);
62261f28255Scgd cp = rp + 1;
62361f28255Scgd }
62461f28255Scgd }
62561f28255Scgd
62661f28255Scgd static void
backeval(Char * cp,int literal)627b79c2ef2Schristos backeval(Char *cp, int literal)
62861f28255Scgd {
62961f28255Scgd struct command faket;
6300bf6fd0cSchristos char tibuf[BUFSIZE];
631b771e65bSwiz Char ibuf[BUFSIZE], *fakecom[2], *ip;
63237e39248Schristos int pvec[2], c, quoted;
63337e39248Schristos ssize_t icnt;
634b79c2ef2Schristos int hadnl;
63561f28255Scgd
63661f28255Scgd hadnl = 0;
63761f28255Scgd icnt = 0;
63861f28255Scgd quoted = (literal || (cp[0] & QUOTE)) ? QUOTE : 0;
63961f28255Scgd faket.t_dtyp = NODE_COMMAND;
64061f28255Scgd faket.t_dflg = 0;
64161f28255Scgd faket.t_dlef = 0;
64261f28255Scgd faket.t_drit = 0;
64361f28255Scgd faket.t_dspr = 0;
64461f28255Scgd faket.t_dcom = fakecom;
64561f28255Scgd fakecom[0] = STRfakecom1;
64661f28255Scgd fakecom[1] = 0;
64761f28255Scgd
64861f28255Scgd /*
64961f28255Scgd * We do the psave job to temporarily change the current job so that the
65061f28255Scgd * following fork is considered a separate job. This is so that when
65161f28255Scgd * backquotes are used in a builtin function that calls glob the "current
65261f28255Scgd * job" is not corrupted. We only need one level of pushed jobs as long as
65361f28255Scgd * we are sure to fork here.
65461f28255Scgd */
65561f28255Scgd psavejob();
65661f28255Scgd
65761f28255Scgd /*
65861f28255Scgd * It would be nicer if we could integrate this redirection more with the
65961f28255Scgd * routines in sh.sem.c by doing a fake execute on a builtin function that
66061f28255Scgd * was piped out.
66161f28255Scgd */
66261f28255Scgd mypipe(pvec);
66361f28255Scgd if (pfork(&faket, -1) == 0) {
664537f55c6Slukem struct wordent fparaml;
66561f28255Scgd struct command *t;
66661f28255Scgd
66761f28255Scgd (void)close(pvec[0]);
66861f28255Scgd (void)dmove(pvec[1], 1);
669cee2bad8Smycroft (void)dmove(SHERR, 2);
67061f28255Scgd initdesc();
67161f28255Scgd /*
67261f28255Scgd * Bugfix for nested backquotes by Michael Greim <greim@sbsvax.UUCP>,
67361f28255Scgd * posted to comp.bugs.4bsd 12 Sep. 1989.
67461f28255Scgd */
67561f28255Scgd if (pargv) /* mg, 21.dec.88 */
67661f28255Scgd blkfree(pargv), pargv = 0, pargsiz = 0;
67761f28255Scgd /* mg, 21.dec.88 */
67861f28255Scgd arginp = cp;
6795d5763b4Schristos for (arginp = cp; *cp; cp++) {
6805d5763b4Schristos *cp &= TRIM;
6815d5763b4Schristos if (*cp == '\n' || *cp == '\r')
6825d5763b4Schristos *cp = ';';
6835d5763b4Schristos }
684cee2bad8Smycroft
685cee2bad8Smycroft /*
686cee2bad8Smycroft * In the child ``forget'' everything about current aliases or
687cee2bad8Smycroft * eval vectors.
688cee2bad8Smycroft */
689cee2bad8Smycroft alvec = NULL;
690cee2bad8Smycroft evalvec = NULL;
691cee2bad8Smycroft alvecp = NULL;
692cee2bad8Smycroft evalp = NULL;
693537f55c6Slukem (void) lex(&fparaml);
694ee9e50eaSmycroft if (seterr)
69561f28255Scgd stderror(ERR_OLD);
696537f55c6Slukem alias(&fparaml);
697537f55c6Slukem t = syntax(fparaml.next, &fparaml, 0);
698ee9e50eaSmycroft if (seterr)
69961f28255Scgd stderror(ERR_OLD);
70061f28255Scgd if (t)
70161f28255Scgd t->t_dflg |= F_NOFORK;
70261f28255Scgd (void)signal(SIGTSTP, SIG_IGN);
70361f28255Scgd (void)signal(SIGTTIN, SIG_IGN);
70461f28255Scgd (void)signal(SIGTTOU, SIG_IGN);
70561f28255Scgd execute(t, -1, NULL, NULL);
70661f28255Scgd exitstat();
70761f28255Scgd }
7081767ce60Schristos free(cp);
70961f28255Scgd (void)close(pvec[1]);
71061f28255Scgd c = 0;
71161f28255Scgd ip = NULL;
71261f28255Scgd do {
713b771e65bSwiz int cnt;
714b771e65bSwiz
715b771e65bSwiz cnt = 0;
71661f28255Scgd
71761f28255Scgd for (;;) {
71861f28255Scgd if (icnt == 0) {
71961f28255Scgd int i;
72061f28255Scgd
72161f28255Scgd ip = ibuf;
72261f28255Scgd do
7230bf6fd0cSchristos icnt = read(pvec[0], tibuf, BUFSIZE);
72461f28255Scgd while (icnt == -1 && errno == EINTR);
72561f28255Scgd if (icnt <= 0) {
72661f28255Scgd c = -1;
72761f28255Scgd break;
72861f28255Scgd }
72961f28255Scgd for (i = 0; i < icnt; i++)
73061f28255Scgd ip[i] = (unsigned char) tibuf[i];
73161f28255Scgd }
73261f28255Scgd if (hadnl)
73361f28255Scgd break;
73461f28255Scgd --icnt;
73561f28255Scgd c = (*ip++ & TRIM);
73661f28255Scgd if (c == 0)
73761f28255Scgd break;
73861f28255Scgd if (c == '\n') {
73961f28255Scgd /*
74061f28255Scgd * Continue around the loop one more time, so that we can eat
74161f28255Scgd * the last newline without terminating this word.
74261f28255Scgd */
74361f28255Scgd hadnl = 1;
74461f28255Scgd continue;
74561f28255Scgd }
74661f28255Scgd if (!quoted && (c == ' ' || c == '\t'))
74761f28255Scgd break;
74861f28255Scgd cnt++;
74961f28255Scgd psave(c | quoted);
75061f28255Scgd }
75161f28255Scgd /*
75261f28255Scgd * Unless at end-of-file, we will form a new word here if there were
75361f28255Scgd * characters in the word, or in any case when we take text literally.
75461f28255Scgd * If we didn't make empty words here when literal was set then we
75561f28255Scgd * would lose blank lines.
75661f28255Scgd */
75761f28255Scgd if (c != -1 && (cnt || literal))
75861f28255Scgd pword();
75961f28255Scgd hadnl = 0;
76061f28255Scgd } while (c >= 0);
76161f28255Scgd (void)close(pvec[0]);
76261f28255Scgd pwait();
76361f28255Scgd prestjob();
76461f28255Scgd }
76561f28255Scgd
76661f28255Scgd static void
psave(int c)767b771e65bSwiz psave(int c)
76861f28255Scgd {
769ee9e50eaSmycroft if (--pnleft <= 0)
77061f28255Scgd stderror(ERR_WTOOLONG);
77137e39248Schristos *pargcp++ = (Char)c;
77261f28255Scgd }
77361f28255Scgd
77461f28255Scgd static void
pword(void)775b771e65bSwiz pword(void)
77661f28255Scgd {
77761f28255Scgd psave(0);
77861f28255Scgd if (pargc == pargsiz - 1) {
77961f28255Scgd pargsiz += GLOBSPACE;
780*509d26c5Snia pargv = xreallocarray(pargv, (size_t)pargsiz, sizeof(Char *));
78161f28255Scgd }
78261f28255Scgd pargv[pargc++] = Strsave(pargs);
78361f28255Scgd pargv[pargc] = NULL;
78461f28255Scgd pargcp = pargs;
78561f28255Scgd pnleft = MAXPATHLEN - 4;
78661f28255Scgd }
78761f28255Scgd
78861f28255Scgd int
Gmatch(Char * string,Char * pattern)789b771e65bSwiz Gmatch(Char *string, Char *pattern)
790cee2bad8Smycroft {
791cee2bad8Smycroft Char **blk, **p;
792b771e65bSwiz int gpol, gres;
793b771e65bSwiz
794b771e65bSwiz gpol = 1;
795b771e65bSwiz gres = 0;
796cee2bad8Smycroft
797cee2bad8Smycroft if (*pattern == '^') {
798cee2bad8Smycroft gpol = 0;
799cee2bad8Smycroft pattern++;
800cee2bad8Smycroft }
801cee2bad8Smycroft
802*509d26c5Snia blk = xreallocarray(NULL, GLOBSPACE, sizeof(Char *));
803cee2bad8Smycroft blk[0] = Strsave(pattern);
804cee2bad8Smycroft blk[1] = NULL;
805cee2bad8Smycroft
806cee2bad8Smycroft expbrace(&blk, NULL, GLOBSPACE);
807cee2bad8Smycroft
808cee2bad8Smycroft for (p = blk; *p; p++)
809cee2bad8Smycroft gres |= pmatch(string, *p);
810cee2bad8Smycroft
811cee2bad8Smycroft blkfree(blk);
812cee2bad8Smycroft return(gres == gpol);
813cee2bad8Smycroft }
814cee2bad8Smycroft
815cee2bad8Smycroft static int
pmatch(const Char * name,const Char * pat)8168080de59Schristos pmatch(const Char *name, const Char *pat)
81761f28255Scgd {
818cee2bad8Smycroft int match, negate_range;
8198080de59Schristos Char patc, namec, c;
8208080de59Schristos const Char *nameNext, *nameStart, *nameEnd, *patNext;
82161f28255Scgd
8228080de59Schristos nameNext = nameStart = name;
8238080de59Schristos patNext = pat;
8248080de59Schristos nameEnd = NULL;
8258080de59Schristos
8268080de59Schristos for (;;) {
8278080de59Schristos namec = *name & TRIM;
8288080de59Schristos if (namec == 0)
8298080de59Schristos nameEnd = name;
8308080de59Schristos patc = *pat;
8318080de59Schristos switch (patc) {
83261f28255Scgd case 0:
8338080de59Schristos if (namec == 0)
8348080de59Schristos return 1;
83561f28255Scgd break;
8368080de59Schristos case '?':
8378080de59Schristos if (namec == 0)
8388080de59Schristos break;
8398080de59Schristos pat++;
8408080de59Schristos name++;
8418080de59Schristos continue;
84261f28255Scgd case '*':
8438080de59Schristos while ((pat[1] & TRIM) == '*')
8448080de59Schristos pat++;
8458080de59Schristos patNext = pat;
8468080de59Schristos nameNext = name + 1;
8478080de59Schristos pat++;
8488080de59Schristos continue;
84961f28255Scgd case '[':
85061f28255Scgd match = 0;
8518080de59Schristos if (namec == 0)
85261f28255Scgd break;
8538080de59Schristos pat++;
8548080de59Schristos name++;
8558080de59Schristos if ((negate_range = (*pat == '^')) != 0)
8568080de59Schristos pat++;
8578080de59Schristos while ((c = *pat++) != ']') {
8588080de59Schristos c &= TRIM;
8598080de59Schristos if (*pat == '-') {
8608080de59Schristos if (c <= namec && namec <= (pat[1] & TRIM))
8618080de59Schristos match = 1;
8628080de59Schristos pat += 2;
8638080de59Schristos } else if (c == namec)
8648080de59Schristos match = 1;
8658080de59Schristos else if (c == 0)
86661f28255Scgd stderror(ERR_NAME | ERR_MISSING, ']');
86761f28255Scgd }
8688080de59Schristos if (match == negate_range)
8698080de59Schristos break;
8708080de59Schristos continue;
8718080de59Schristos default:
8728080de59Schristos if ((patc & TRIM) != namec)
8738080de59Schristos break;
8748080de59Schristos pat++;
8758080de59Schristos name++;
8768080de59Schristos continue;
8778080de59Schristos }
8788080de59Schristos if (nameNext != nameStart && (nameEnd == NULL || nameNext <= nameEnd)) {
8798080de59Schristos pat = patNext;
8808080de59Schristos name = nameNext;
8818080de59Schristos continue;
8828080de59Schristos }
8838080de59Schristos return 0;
88461f28255Scgd }
88561f28255Scgd }
88661f28255Scgd
88761f28255Scgd void
Gcat(Char * s1,Char * s2)888b771e65bSwiz Gcat(Char *s1, Char *s2)
88961f28255Scgd {
89076adbe2bStls Char *p, *q;
89137e39248Schristos ptrdiff_t n;
89261f28255Scgd
893cee2bad8Smycroft for (p = s1; *p++;)
894cee2bad8Smycroft continue;
895cee2bad8Smycroft for (q = s2; *q++;)
896cee2bad8Smycroft continue;
89761f28255Scgd n = (p - s1) + (q - s2) - 1;
89861f28255Scgd if (++gargc >= gargsiz) {
89961f28255Scgd gargsiz += GLOBSPACE;
900*509d26c5Snia gargv = xreallocarray(gargv, (size_t)gargsiz, sizeof(Char *));
90161f28255Scgd }
90261f28255Scgd gargv[gargc] = 0;
903*509d26c5Snia p = gargv[gargc - 1] = xreallocarray(NULL, (size_t)n, sizeof(Char));
904cee2bad8Smycroft for (q = s1; (*p++ = *q++) != '\0';)
905cee2bad8Smycroft continue;
906cee2bad8Smycroft for (p--, q = s2; (*p++ = *q++) != '\0';)
907cee2bad8Smycroft continue;
90861f28255Scgd }
90961f28255Scgd
91061f28255Scgd #ifdef FILEC
91161f28255Scgd int
sortscmp(const void * va,const void * vb)9121767ce60Schristos sortscmp(const void *va, const void *vb)
91361f28255Scgd {
91461f28255Scgd #if defined(NLS) && !defined(NOSTRCOLL)
91561f28255Scgd char buf[2048];
91661f28255Scgd #endif
9171767ce60Schristos const Char * const *a = va;
9181767ce60Schristos const Char * const *b = vb;
91961f28255Scgd
92061f28255Scgd if (!a) /* check for NULL */
92161f28255Scgd return (b ? 1 : 0);
92261f28255Scgd if (!b)
9231767ce60Schristos return -1;
92461f28255Scgd
9251767ce60Schristos if (!*a) /* check for NULL */
9261767ce60Schristos return *b ? 1 : 0;
9271767ce60Schristos if (!*b)
92861f28255Scgd return (-1);
92961f28255Scgd
93061f28255Scgd #if defined(NLS) && !defined(NOSTRCOLL)
9311767ce60Schristos (void)strcpy(buf, short2str(*a));
9321767ce60Schristos return (int)strcoll(buf, short2str(*b));
93361f28255Scgd #else
9341767ce60Schristos return (int)Strcmp(*a, *b);
93561f28255Scgd #endif
93661f28255Scgd }
93761f28255Scgd #endif /* FILEC */
938