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