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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)expand.c 8.1 (Berkeley) 6/9/93
30 * $FreeBSD: src/usr.bin/rdist/expand.c,v 1.8 1999/08/28 01:05:06 peter Exp $
35 #define GAVSIZ NCARGS / 6
39 #define sort() qsort((char *)sortbase, &eargv[eargc] - sortbase, \
40 sizeof(*sortbase), argcmp), sortbase = &eargv[eargc]
42 static char shchars[] = "${[*?";
44 int which; /* bit mask of types to expand */
45 int eargc; /* expanded arg count */
46 char **eargv; /* expanded arg vectors */
50 char *tilde; /* "~user" if not expanding tilde, else "" */
54 int expany; /* any expansions done? */
58 static void Cat(char *, char *);
59 static void addpath(int);
60 static int amatch(char *, char *);
61 static int argcmp(const void *, const void *);
62 static int execbrc(char *, char *);
63 static void expsh(char *);
64 static void expstr(char *);
65 static int match(char *, char *);
66 static void matchdir(char *);
69 * Take a list of names and expand any macros, etc.
70 * wh = E_VARS if expanding variables.
71 * wh = E_SHELL if expanding shell characters.
72 * wh = E_TILDE if expanding `~'.
73 * or any of these or'ed together.
75 * Major portions of this were snarfed from csh/sh.glob.c.
78 expand(struct namelist *list, int wh)
80 struct namelist *nl, *prev;
83 char *argvbuf[GAVSIZ];
86 printf("expand(%p, %d)\nlist = ", list, wh);
93 for (nl = list; nl != NULL; nl = nl->n_next)
94 for (cp = nl->n_name; *cp; cp++)
100 path = tpathp = pathp = pathbuf;
102 lastpathp = &path[sizeof pathbuf - 2];
105 eargv = sortbase = argvbuf;
109 * Walk the name list and expand names into eargv[];
111 for (nl = list; nl != NULL; nl = nl->n_next)
114 * Take expanded list of names from eargv[] and build a new list.
117 for (n = 0; n < eargc; n++) {
119 nl->n_name = eargv[n];
128 printf("expanded list = ");
142 extern char homedir[];
144 if (s == NULL || *s == '\0')
147 if ((which & E_VARS) && (cp = index(s, '$')) != NULL) {
150 yyerror("no variable name after '$'");
155 if ((tail = index(cp, RC)) == NULL) {
156 yyerror("unmatched '{'");
159 *tail++ = savec = '\0';
161 yyerror("no variable name after '$'");
169 tp = lookup(cp, 0, NULL);
173 for (; tp != NULL; tp = tp->n_next) {
174 snprintf(buf, sizeof(buf),
175 "%s%s%s", s, tp->n_name, tail);
180 snprintf(buf, sizeof(buf), "%s%s", s, tail);
184 if ((which & ~E_VARS) == 0 || !strcmp(s, "{") || !strcmp(s, "{}")) {
191 if (*cp == '\0' || *cp == '/') {
199 while (*cp && *cp != '/');
201 if (pw == NULL || strcmp(pw->pw_name, buf+1) != 0) {
202 if ((pw = getpwnam(buf+1)) == NULL) {
203 strcat(buf, ": unknown user name");
211 for (cp = path; (*cp++ = *cp1++); )
213 tpathp = pathp = cp - 1;
215 tpathp = pathp = path;
219 if (!(which & E_SHELL)) {
231 Cat(s, ""); /* "nonomatch" is set */
236 argcmp(const void *a1, const void *a2)
239 return (strcmp(*(char **)a1, *(char **)a2));
243 * If there are any Shell meta characters in the name,
244 * expand into a list, after searching directory
250 char *spathp, *oldcp;
255 while (!any(*cp, shchars)) {
257 if (!expany || stat(path, &stb) >= 0) {
268 while (cp > s && *cp != '/')
284 matchdir(char *pattern)
290 dirp = opendir(path);
296 if (fstat(dirfd(dirp), &stb) < 0)
298 if (!ISDIR(stb.st_mode)) {
302 while ((dp = readdir(dirp)) != NULL)
303 if (match(dp->d_name, pattern)) {
305 Cat(path, dp->d_name);
307 strcpy(pathp, dp->d_name);
319 strcat(path, strerror(errno));
324 execbrc(char *p, char *s)
326 char restbuf[BUFSIZ + 2];
329 char *lm, savec, *spathp;
333 for (lm = restbuf; *p != '{'; *lm++ = *p++)
335 for (pe = ++p; *pe; pe++)
349 for (pe++; *pe && *pe != ']'; pe++)
352 yyerror("Missing ']'");
356 if (brclev || !*pe) {
357 yyerror("Missing '}'");
360 for (pl = pm = p; pm <= pe; pm++)
361 switch (*pm & (QUOTE|TRIM)) {
381 strcat(restbuf, pe + 1);
388 } else if (amatch(s, restbuf))
395 for (pm++; *pm && *pm != ']'; pm++)
398 yyerror("Missing ']'");
405 match(char *s, char *p)
412 if (*s == '.' && *p != '.')
423 amatch(char *s, char *p)
436 return (execbrc(p - 1, s - 1));
441 while ((cc = *p++)) {
448 if (lc <= scc && scc <= *p++)
451 if (scc == (lc = cc))
455 yyerror("Missing ']'");
473 return (scc == '\0');
476 if ((c & TRIM) != scc)
494 if (stat(path, &stb) == 0 && ISDIR(stb.st_mode)) {
511 Cat(char *s1, char *s2)
516 len = strlen(s1) + strlen(s2) + 1;
518 if (nleft <= 0 || ++eargc >= GAVSIZ)
519 yyerror("Arguments too long");
521 eargv[eargc - 1] = s = malloc(len);
523 fatal("ran out of memory\n");
524 while ((*s++ = *s1++ & TRIM))
527 while ((*s++ = *s2++ & TRIM))
535 if (pathp >= lastpathp)
536 yyerror("Pathname too long");
544 * Expand file names beginning with `~' into the
545 * user's home directory path name. Return a pointer in buf to the
546 * part corresponding to `file'.
549 exptilde(char buf[], char *file, int maxlen)
552 extern char homedir[];
554 if (strlen(file) >= maxlen)
560 if (*++file == '\0') {
563 } else if (*file == '/') {
568 while (*s3 && *s3 != '/')
574 if (pw == NULL || strcmp(pw->pw_name, file) != 0) {
575 if ((pw = getpwnam(file)) == NULL) {
576 error("%s: unknown user name\n", file);
586 for (s1 = buf; (*s1++ = *s2++) && s1 < buf+maxlen; )
589 if (s3 != NULL && s1 < buf+maxlen) {
591 while ((*s1++ = *s3++) && s1 < buf+maxlen)
594 if (s1 == buf+maxlen)