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