Home | History | Annotate | Line # | Download | only in rdist
expand.c revision 1.6
      1 /*
      2  * Copyright (c) 1983, 1993
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #ifndef lint
     35 /* from: static char sccsid[] = "@(#)expand.c	8.1 (Berkeley) 6/9/93"; */
     36 static char *rcsid = "$Id: expand.c,v 1.6 1996/07/12 00:06:34 thorpej Exp $";
     37 #endif /* not lint */
     38 
     39 #include "defs.h"
     40 
     41 #define	GAVSIZ	NCARGS / 6
     42 #define LC '{'
     43 #define RC '}'
     44 
     45 static char	shchars[] = "${[*?";
     46 
     47 int	which;		/* bit mask of types to expand */
     48 int	eargc;		/* expanded arg count */
     49 char	**eargv;	/* expanded arg vectors */
     50 char	*path;
     51 char	*pathp;
     52 char	*lastpathp;
     53 char	*tilde;		/* "~user" if not expanding tilde, else "" */
     54 char	*tpathp;
     55 int	nleft;
     56 
     57 int	expany;		/* any expansions done? */
     58 char	*entp;
     59 char	**sortbase;
     60 
     61 #define sort()	qsort((char *)sortbase, &eargv[eargc] - sortbase, \
     62 		      sizeof(*sortbase), argcmp), sortbase = &eargv[eargc]
     63 
     64 static void	Cat __P((char *, char *));
     65 static void	addpath __P((int));
     66 static int	amatch __P((char *, char *));
     67 static int	argcmp __P((const void *, const void *));
     68 static int	execbrc __P((char *, char *));
     69 static void	expsh __P((char *));
     70 static void	expstr __P((char *));
     71 static int	match __P((char *, char *));
     72 static void	matchdir __P((char *));
     73 static int	smatch __P((char *, char *));
     74 
     75 /*
     76  * Take a list of names and expand any macros, etc.
     77  * wh = E_VARS if expanding variables.
     78  * wh = E_SHELL if expanding shell characters.
     79  * wh = E_TILDE if expanding `~'.
     80  * or any of these or'ed together.
     81  *
     82  * Major portions of this were snarfed from csh/sh.glob.c.
     83  */
     84 struct namelist *
     85 expand(list, wh)
     86 	struct namelist *list;
     87 	int wh;
     88 {
     89 	register struct namelist *nl, *prev;
     90 	register int n;
     91 	char pathbuf[BUFSIZ];
     92 	char *argvbuf[GAVSIZ];
     93 
     94 	if (debug) {
     95 		printf("expand(%x, %d)\nlist = ", list, wh);
     96 		prnames(list);
     97 	}
     98 
     99 	if (wh == 0) {
    100 		register char *cp;
    101 
    102 		for (nl = list; nl != NULL; nl = nl->n_next)
    103 			for (cp = nl->n_name; *cp; cp++)
    104 				*cp = *cp & TRIM;
    105 		return(list);
    106 	}
    107 
    108 	which = wh;
    109 	path = tpathp = pathp = pathbuf;
    110 	*pathp = '\0';
    111 	lastpathp = &path[sizeof pathbuf - 2];
    112 	tilde = "";
    113 	eargc = 0;
    114 	eargv = sortbase = argvbuf;
    115 	*eargv = 0;
    116 	nleft = NCARGS - 4;
    117 	/*
    118 	 * Walk the name list and expand names into eargv[];
    119 	 */
    120 	for (nl = list; nl != NULL; nl = nl->n_next)
    121 		expstr(nl->n_name);
    122 	/*
    123 	 * Take expanded list of names from eargv[] and build a new list.
    124 	 */
    125 	list = prev = NULL;
    126 	for (n = 0; n < eargc; n++) {
    127 		nl = makenl(NULL);
    128 		nl->n_name = eargv[n];
    129 		if (prev == NULL)
    130 			list = prev = nl;
    131 		else {
    132 			prev->n_next = nl;
    133 			prev = nl;
    134 		}
    135 	}
    136 	if (debug) {
    137 		printf("expanded list = ");
    138 		prnames(list);
    139 	}
    140 	return(list);
    141 }
    142 
    143 static void
    144 expstr(s)
    145 	char *s;
    146 {
    147 	register char *cp, *cp1;
    148 	register struct namelist *tp;
    149 	char *tail;
    150 	char buf[BUFSIZ];
    151 	int savec, oeargc;
    152 	extern char homedir[];
    153 
    154 	if (s == NULL || *s == '\0')
    155 		return;
    156 
    157 	if ((which & E_VARS) && (cp = index(s, '$')) != NULL) {
    158 		*cp++ = '\0';
    159 		if (*cp == '\0') {
    160 			yyerror("no variable name after '$'");
    161 			return;
    162 		}
    163 		if (*cp == LC) {
    164 			cp++;
    165 			if ((tail = index(cp, RC)) == NULL) {
    166 				yyerror("unmatched '{'");
    167 				return;
    168 			}
    169 			*tail++ = savec = '\0';
    170 			if (*cp == '\0') {
    171 				yyerror("no variable name after '$'");
    172 				return;
    173 			}
    174 		} else {
    175 			tail = cp + 1;
    176 			savec = *tail;
    177 			*tail = '\0';
    178 		}
    179 		tp = lookup(cp, NULL, 0);
    180 		if (savec != '\0')
    181 			*tail = savec;
    182 		if (tp != NULL) {
    183 			for (; tp != NULL; tp = tp->n_next) {
    184 				snprintf(buf, sizeof(buf), "%s%s%s", s,
    185 				    tp->n_name, tail);
    186 				expstr(buf);
    187 			}
    188 			return;
    189 		}
    190 		snprintf(buf, sizeof(buf), "%s%s", s, tail);
    191 		expstr(buf);
    192 		return;
    193 	}
    194 	if ((which & ~E_VARS) == 0 || !strcmp(s, "{") || !strcmp(s, "{}")) {
    195 		Cat(s, "");
    196 		sort();
    197 		return;
    198 	}
    199 	if (*s == '~') {
    200 		cp = ++s;
    201 		if (*cp == '\0' || *cp == '/') {
    202 			tilde = "~";
    203 			cp1 = homedir;
    204 		} else {
    205 			tilde = cp1 = buf;
    206 			*cp1++ = '~';
    207 			do
    208 				*cp1++ = *cp++;
    209 			while (*cp && *cp != '/');
    210 			*cp1 = '\0';
    211 			if (pw == NULL || strcmp(pw->pw_name, buf+1) != 0) {
    212 				if ((pw = getpwnam(buf+1)) == NULL) {
    213 					strcat(buf, ": unknown user name");
    214 					yyerror(buf+1);
    215 					return;
    216 				}
    217 			}
    218 			cp1 = pw->pw_dir;
    219 			s = cp;
    220 		}
    221 		for (cp = path; *cp++ = *cp1++; )
    222 			;
    223 		tpathp = pathp = cp - 1;
    224 	} else {
    225 		tpathp = pathp = path;
    226 		tilde = "";
    227 	}
    228 	*pathp = '\0';
    229 	if (!(which & E_SHELL)) {
    230 		if (which & E_TILDE)
    231 			Cat(path, s);
    232 		else
    233 			Cat(tilde, s);
    234 		sort();
    235 		return;
    236 	}
    237 	oeargc = eargc;
    238 	expany = 0;
    239 	expsh(s);
    240 	if (eargc == oeargc)
    241 		Cat(s, "");		/* "nonomatch" is set */
    242 	sort();
    243 }
    244 
    245 static int
    246 argcmp(a1, a2)
    247 	const void *a1, *a2;
    248 {
    249 
    250 	return (strcmp(*(char **)a1, *(char **)a2));
    251 }
    252 
    253 /*
    254  * If there are any Shell meta characters in the name,
    255  * expand into a list, after searching directory
    256  */
    257 static void
    258 expsh(s)
    259 	char *s;
    260 {
    261 	register char *cp;
    262 	register char *spathp, *oldcp;
    263 	struct stat stb;
    264 
    265 	spathp = pathp;
    266 	cp = s;
    267 	while (!any(*cp, shchars)) {
    268 		if (*cp == '\0') {
    269 			if (!expany || stat(path, &stb) >= 0) {
    270 				if (which & E_TILDE)
    271 					Cat(path, "");
    272 				else
    273 					Cat(tilde, tpathp);
    274 			}
    275 			goto endit;
    276 		}
    277 		addpath(*cp++);
    278 	}
    279 	oldcp = cp;
    280 	while (cp > s && *cp != '/')
    281 		cp--, pathp--;
    282 	if (*cp == '/')
    283 		cp++, pathp++;
    284 	*pathp = '\0';
    285 	if (*oldcp == '{') {
    286 		execbrc(cp, NULL);
    287 		return;
    288 	}
    289 	matchdir(cp);
    290 endit:
    291 	pathp = spathp;
    292 	*pathp = '\0';
    293 }
    294 
    295 static void
    296 matchdir(pattern)
    297 	char *pattern;
    298 {
    299 	struct stat stb;
    300 	register struct direct *dp;
    301 	DIR *dirp;
    302 
    303 	dirp = opendir(path);
    304 	if (dirp == NULL) {
    305 		if (expany)
    306 			return;
    307 		goto patherr2;
    308 	}
    309 	if (fstat(dirp->dd_fd, &stb) < 0)
    310 		goto patherr1;
    311 	if (!ISDIR(stb.st_mode)) {
    312 		errno = ENOTDIR;
    313 		goto patherr1;
    314 	}
    315 	while ((dp = readdir(dirp)) != NULL)
    316 		if (match(dp->d_name, pattern)) {
    317 			if (which & E_TILDE)
    318 				Cat(path, dp->d_name);
    319 			else {
    320 				strcpy(pathp, dp->d_name);
    321 				Cat(tilde, tpathp);
    322 				*pathp = '\0';
    323 			}
    324 		}
    325 	closedir(dirp);
    326 	return;
    327 
    328 patherr1:
    329 	closedir(dirp);
    330 patherr2:
    331 	strcat(path, ": ");
    332 	strcat(path, strerror(errno));
    333 	yyerror(path);
    334 }
    335 
    336 static int
    337 execbrc(p, s)
    338 	char *p, *s;
    339 {
    340 	char restbuf[BUFSIZ + 2];
    341 	register char *pe, *pm, *pl;
    342 	int brclev = 0;
    343 	char *lm, savec, *spathp;
    344 
    345 	for (lm = restbuf; *p != '{'; *lm++ = *p++)
    346 		continue;
    347 	for (pe = ++p; *pe; pe++)
    348 		switch (*pe) {
    349 
    350 		case '{':
    351 			brclev++;
    352 			continue;
    353 
    354 		case '}':
    355 			if (brclev == 0)
    356 				goto pend;
    357 			brclev--;
    358 			continue;
    359 
    360 		case '[':
    361 			for (pe++; *pe && *pe != ']'; pe++)
    362 				continue;
    363 			if (!*pe)
    364 				yyerror("Missing ']'");
    365 			continue;
    366 		}
    367 pend:
    368 	if (brclev || !*pe) {
    369 		yyerror("Missing '}'");
    370 		return (0);
    371 	}
    372 	for (pl = pm = p; pm <= pe; pm++)
    373 		switch (*pm & (QUOTE|TRIM)) {
    374 
    375 		case '{':
    376 			brclev++;
    377 			continue;
    378 
    379 		case '}':
    380 			if (brclev) {
    381 				brclev--;
    382 				continue;
    383 			}
    384 			goto doit;
    385 
    386 		case ',':
    387 			if (brclev)
    388 				continue;
    389 doit:
    390 			savec = *pm;
    391 			*pm = 0;
    392 			strcpy(lm, pl);
    393 			strcat(restbuf, pe + 1);
    394 			*pm = savec;
    395 			if (s == 0) {
    396 				spathp = pathp;
    397 				expsh(restbuf);
    398 				pathp = spathp;
    399 				*pathp = 0;
    400 			} else if (amatch(s, restbuf))
    401 				return (1);
    402 			sort();
    403 			pl = pm + 1;
    404 			continue;
    405 
    406 		case '[':
    407 			for (pm++; *pm && *pm != ']'; pm++)
    408 				continue;
    409 			if (!*pm)
    410 				yyerror("Missing ']'");
    411 			continue;
    412 		}
    413 	return (0);
    414 }
    415 
    416 static int
    417 match(s, p)
    418 	char *s, *p;
    419 {
    420 	register int c;
    421 	register char *sentp;
    422 	char sexpany = expany;
    423 
    424 	if (*s == '.' && *p != '.')
    425 		return (0);
    426 	sentp = entp;
    427 	entp = s;
    428 	c = amatch(s, p);
    429 	entp = sentp;
    430 	expany = sexpany;
    431 	return (c);
    432 }
    433 
    434 static int
    435 amatch(s, p)
    436 	register char *s, *p;
    437 {
    438 	register int scc;
    439 	int ok, lc;
    440 	char *spathp;
    441 	struct stat stb;
    442 	int c, cc;
    443 
    444 	expany = 1;
    445 	for (;;) {
    446 		scc = *s++ & TRIM;
    447 		switch (c = *p++) {
    448 
    449 		case '{':
    450 			return (execbrc(p - 1, s - 1));
    451 
    452 		case '[':
    453 			ok = 0;
    454 			lc = 077777;
    455 			while (cc = *p++) {
    456 				if (cc == ']') {
    457 					if (ok)
    458 						break;
    459 					return (0);
    460 				}
    461 				if (cc == '-') {
    462 					if (lc <= scc && scc <= *p++)
    463 						ok++;
    464 				} else
    465 					if (scc == (lc = cc))
    466 						ok++;
    467 			}
    468 			if (cc == 0) {
    469 				yyerror("Missing ']'");
    470 				return (0);
    471 			}
    472 			continue;
    473 
    474 		case '*':
    475 			if (!*p)
    476 				return (1);
    477 			if (*p == '/') {
    478 				p++;
    479 				goto slash;
    480 			}
    481 			for (s--; *s; s++)
    482 				if (amatch(s, p))
    483 					return (1);
    484 			return (0);
    485 
    486 		case '\0':
    487 			return (scc == '\0');
    488 
    489 		default:
    490 			if ((c & TRIM) != scc)
    491 				return (0);
    492 			continue;
    493 
    494 		case '?':
    495 			if (scc == '\0')
    496 				return (0);
    497 			continue;
    498 
    499 		case '/':
    500 			if (scc)
    501 				return (0);
    502 slash:
    503 			s = entp;
    504 			spathp = pathp;
    505 			while (*s)
    506 				addpath(*s++);
    507 			addpath('/');
    508 			if (stat(path, &stb) == 0 && ISDIR(stb.st_mode))
    509 				if (*p == '\0') {
    510 					if (which & E_TILDE)
    511 						Cat(path, "");
    512 					else
    513 						Cat(tilde, tpathp);
    514 				} else
    515 					expsh(p);
    516 			pathp = spathp;
    517 			*pathp = '\0';
    518 			return (0);
    519 		}
    520 	}
    521 }
    522 
    523 static int
    524 smatch(s, p)
    525 	register char *s, *p;
    526 {
    527 	register int scc;
    528 	int ok, lc;
    529 	int c, cc;
    530 
    531 	for (;;) {
    532 		scc = *s++ & TRIM;
    533 		switch (c = *p++) {
    534 
    535 		case '[':
    536 			ok = 0;
    537 			lc = 077777;
    538 			while (cc = *p++) {
    539 				if (cc == ']') {
    540 					if (ok)
    541 						break;
    542 					return (0);
    543 				}
    544 				if (cc == '-') {
    545 					if (lc <= scc && scc <= *p++)
    546 						ok++;
    547 				} else
    548 					if (scc == (lc = cc))
    549 						ok++;
    550 			}
    551 			if (cc == 0) {
    552 				yyerror("Missing ']'");
    553 				return (0);
    554 			}
    555 			continue;
    556 
    557 		case '*':
    558 			if (!*p)
    559 				return (1);
    560 			for (s--; *s; s++)
    561 				if (smatch(s, p))
    562 					return (1);
    563 			return (0);
    564 
    565 		case '\0':
    566 			return (scc == '\0');
    567 
    568 		default:
    569 			if ((c & TRIM) != scc)
    570 				return (0);
    571 			continue;
    572 
    573 		case '?':
    574 			if (scc == 0)
    575 				return (0);
    576 			continue;
    577 
    578 		}
    579 	}
    580 }
    581 
    582 static void
    583 Cat(s1, s2)
    584 	register char *s1, *s2;
    585 {
    586 	int len = strlen(s1) + strlen(s2) + 1;
    587 	register char *s;
    588 
    589 	nleft -= len;
    590 	if (nleft <= 0 || ++eargc >= GAVSIZ)
    591 		yyerror("Arguments too long");
    592 	eargv[eargc] = 0;
    593 	eargv[eargc - 1] = s = malloc(len);
    594 	if (s == NULL)
    595 		fatal("ran out of memory\n");
    596 	while (*s++ = *s1++ & TRIM)
    597 		;
    598 	s--;
    599 	while (*s++ = *s2++ & TRIM)
    600 		;
    601 }
    602 
    603 static void
    604 addpath(c)
    605 	int c;
    606 {
    607 
    608 	if (pathp >= lastpathp)
    609 		yyerror("Pathname too long");
    610 	else {
    611 		*pathp++ = c & TRIM;
    612 		*pathp = '\0';
    613 	}
    614 }
    615 
    616 /*
    617  * Expand file names beginning with `~' into the
    618  * user's home directory path name. Return a pointer in buf to the
    619  * part corresponding to `file'.
    620  */
    621 char *
    622 exptilde(buf, file)
    623 	char buf[];
    624 	register char *file;
    625 {
    626 	register char *s1, *s2, *s3;
    627 	extern char homedir[];
    628 
    629 	if (*file != '~') {
    630 		strcpy(buf, file);
    631 		return(buf);
    632 	}
    633 	if (*++file == '\0') {
    634 		s2 = homedir;
    635 		s3 = NULL;
    636 	} else if (*file == '/') {
    637 		s2 = homedir;
    638 		s3 = file;
    639 	} else {
    640 		s3 = file;
    641 		while (*s3 && *s3 != '/')
    642 			s3++;
    643 		if (*s3 == '/')
    644 			*s3 = '\0';
    645 		else
    646 			s3 = NULL;
    647 		if (pw == NULL || strcmp(pw->pw_name, file) != 0) {
    648 			if ((pw = getpwnam(file)) == NULL) {
    649 				error("%s: unknown user name\n", file);
    650 				if (s3 != NULL)
    651 					*s3 = '/';
    652 				return(NULL);
    653 			}
    654 		}
    655 		if (s3 != NULL)
    656 			*s3 = '/';
    657 		s2 = pw->pw_dir;
    658 	}
    659 	for (s1 = buf; *s1++ = *s2++; )
    660 		;
    661 	s2 = --s1;
    662 	if (s3 != NULL) {
    663 		s2++;
    664 		while (*s1++ = *s3++)
    665 			;
    666 	}
    667 	return(s2);
    668 }
    669