2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
33 * @(#)expand.c 8.1 (Berkeley) 6/9/93
34 * $FreeBSD: src/usr.bin/rdist/expand.c,v 1.8 1999/08/28 01:05:06 peter Exp $
39 #define GAVSIZ NCARGS / 6
43 #define sort() qsort((char *)sortbase, &eargv[eargc] - sortbase, \
44 sizeof(*sortbase), argcmp), sortbase = &eargv[eargc]
46 static char shchars[] = "${[*?";
48 int which; /* bit mask of types to expand */
49 int eargc; /* expanded arg count */
50 char **eargv; /* expanded arg vectors */
54 char *tilde; /* "~user" if not expanding tilde, else "" */
58 int expany; /* any expansions done? */
62 static void Cat(char *, char *);
63 static void addpath(int);
64 static int amatch(char *, char *);
65 static int argcmp(const void *, const void *);
66 static int execbrc(char *, char *);
67 static void expsh(char *);
68 static void expstr(char *);
69 static int match(char *, char *);
70 static void matchdir(char *);
71 static int smatch(char *, char *);
74 * Take a list of names and expand any macros, etc.
75 * wh = E_VARS if expanding variables.
76 * wh = E_SHELL if expanding shell characters.
77 * wh = E_TILDE if expanding `~'.
78 * or any of these or'ed together.
80 * Major portions of this were snarfed from csh/sh.glob.c.
83 expand(struct namelist *list, int wh)
85 struct namelist *nl, *prev;
88 char *argvbuf[GAVSIZ];
91 printf("expand(%p, %d)\nlist = ", list, wh);
98 for (nl = list; nl != NULL; nl = nl->n_next)
99 for (cp = nl->n_name; *cp; cp++)
105 path = tpathp = pathp = pathbuf;
107 lastpathp = &path[sizeof pathbuf - 2];
110 eargv = sortbase = argvbuf;
114 * Walk the name list and expand names into eargv[];
116 for (nl = list; nl != NULL; nl = nl->n_next)
119 * Take expanded list of names from eargv[] and build a new list.
122 for (n = 0; n < eargc; n++) {
124 nl->n_name = eargv[n];
133 printf("expanded list = ");
147 extern char homedir[];
149 if (s == NULL || *s == '\0')
152 if ((which & E_VARS) && (cp = index(s, '$')) != NULL) {
155 yyerror("no variable name after '$'");
160 if ((tail = index(cp, RC)) == NULL) {
161 yyerror("unmatched '{'");
164 *tail++ = savec = '\0';
166 yyerror("no variable name after '$'");
174 tp = lookup(cp, 0, NULL);
178 for (; tp != NULL; tp = tp->n_next) {
179 snprintf(buf, sizeof(buf),
180 "%s%s%s", s, tp->n_name, tail);
185 snprintf(buf, sizeof(buf), "%s%s", s, tail);
189 if ((which & ~E_VARS) == 0 || !strcmp(s, "{") || !strcmp(s, "{}")) {
196 if (*cp == '\0' || *cp == '/') {
204 while (*cp && *cp != '/');
206 if (pw == NULL || strcmp(pw->pw_name, buf+1) != 0) {
207 if ((pw = getpwnam(buf+1)) == NULL) {
208 strcat(buf, ": unknown user name");
216 for (cp = path; (*cp++ = *cp1++); )
218 tpathp = pathp = cp - 1;
220 tpathp = pathp = path;
224 if (!(which & E_SHELL)) {
236 Cat(s, ""); /* "nonomatch" is set */
241 argcmp(const void *a1, const void *a2)
244 return (strcmp(*(char **)a1, *(char **)a2));
248 * If there are any Shell meta characters in the name,
249 * expand into a list, after searching directory
255 char *spathp, *oldcp;
260 while (!any(*cp, shchars)) {
262 if (!expany || stat(path, &stb) >= 0) {
273 while (cp > s && *cp != '/')
289 matchdir(char *pattern)
295 dirp = opendir(path);
301 if (fstat(dirp->dd_fd, &stb) < 0)
303 if (!ISDIR(stb.st_mode)) {
307 while ((dp = readdir(dirp)) != NULL)
308 if (match(dp->d_name, pattern)) {
310 Cat(path, dp->d_name);
312 strcpy(pathp, dp->d_name);
324 strcat(path, strerror(errno));
329 execbrc(char *p, char *s)
331 char restbuf[BUFSIZ + 2];
334 char *lm, savec, *spathp;
338 for (lm = restbuf; *p != '{'; *lm++ = *p++)
340 for (pe = ++p; *pe; pe++)
354 for (pe++; *pe && *pe != ']'; pe++)
357 yyerror("Missing ']'");
361 if (brclev || !*pe) {
362 yyerror("Missing '}'");
365 for (pl = pm = p; pm <= pe; pm++)
366 switch (*pm & (QUOTE|TRIM)) {
386 strcat(restbuf, pe + 1);
393 } else if (amatch(s, restbuf))
400 for (pm++; *pm && *pm != ']'; pm++)
403 yyerror("Missing ']'");
410 match(char *s, char *p)
417 if (*s == '.' && *p != '.')
428 amatch(char *s, char *p)
441 return (execbrc(p - 1, s - 1));
446 while ((cc = *p++)) {
453 if (lc <= scc && scc <= *p++)
456 if (scc == (lc = cc))
460 yyerror("Missing ']'");
478 return (scc == '\0');
481 if ((c & TRIM) != scc)
499 if (stat(path, &stb) == 0 && ISDIR(stb.st_mode)) {
516 smatch(char *s, char *p)
528 while ((cc = *p++)) {
535 if (lc <= scc && scc <= *p++)
538 if (scc == (lc = cc))
542 yyerror("Missing ']'");
556 return (scc == '\0');
559 if ((c & TRIM) != scc)
573 Cat(char *s1, char *s2)
578 len = strlen(s1) + strlen(s2) + 1;
580 if (nleft <= 0 || ++eargc >= GAVSIZ)
581 yyerror("Arguments too long");
583 eargv[eargc - 1] = s = malloc(len);
585 fatal("ran out of memory\n");
586 while ((*s++ = *s1++ & TRIM))
589 while ((*s++ = *s2++ & TRIM))
597 if (pathp >= lastpathp)
598 yyerror("Pathname too long");
606 * Expand file names beginning with `~' into the
607 * user's home directory path name. Return a pointer in buf to the
608 * part corresponding to `file'.
611 exptilde(char buf[], char *file, int maxlen)
614 extern char homedir[];
616 if (strlen(file) >= maxlen)
622 if (*++file == '\0') {
625 } else if (*file == '/') {
630 while (*s3 && *s3 != '/')
636 if (pw == NULL || strcmp(pw->pw_name, file) != 0) {
637 if ((pw = getpwnam(file)) == NULL) {
638 error("%s: unknown user name\n", file);
648 for (s1 = buf; (*s1++ = *s2++) && s1 < buf+maxlen; )
651 if (s3 != NULL && s1 < buf+maxlen) {
653 while ((*s1++ = *s3++) && s1 < buf+maxlen)
656 if (s1 == buf+maxlen)