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
36 static char sccsid[] = "@(#)expand.c 8.1 (Berkeley) 6/9/93";
38 static const char rcsid[] =
39 "$FreeBSD: src/usr.bin/rdist/expand.c,v 1.8 1999/08/28 01:05:06 peter Exp $";
44 #define GAVSIZ NCARGS / 6
48 static char shchars[] = "${[*?";
50 int which; /* bit mask of types to expand */
51 int eargc; /* expanded arg count */
52 char **eargv; /* expanded arg vectors */
56 char *tilde; /* "~user" if not expanding tilde, else "" */
60 int expany; /* any expansions done? */
64 #define sort() qsort((char *)sortbase, &eargv[eargc] - sortbase, \
65 sizeof(*sortbase), argcmp), sortbase = &eargv[eargc]
67 static void Cat __P((char *, char *));
68 static void addpath __P((int));
69 static int amatch __P((char *, char *));
70 static int argcmp __P((const void *, const void *));
71 static int execbrc __P((char *, char *));
72 static void expsh __P((char *));
73 static void expstr __P((char *));
74 static int match __P((char *, char *));
75 static void matchdir __P((char *));
76 static int smatch __P((char *, char *));
79 * Take a list of names and expand any macros, etc.
80 * wh = E_VARS if expanding variables.
81 * wh = E_SHELL if expanding shell characters.
82 * wh = E_TILDE if expanding `~'.
83 * or any of these or'ed together.
85 * Major portions of this were snarfed from csh/sh.glob.c.
89 struct namelist *list;
92 register struct namelist *nl, *prev;
95 char *argvbuf[GAVSIZ];
98 printf("expand(%p, %d)\nlist = ", list, wh);
105 for (nl = list; nl != NULL; nl = nl->n_next)
106 for (cp = nl->n_name; *cp; cp++)
112 path = tpathp = pathp = pathbuf;
114 lastpathp = &path[sizeof pathbuf - 2];
117 eargv = sortbase = argvbuf;
121 * Walk the name list and expand names into eargv[];
123 for (nl = list; nl != NULL; nl = nl->n_next)
126 * Take expanded list of names from eargv[] and build a new list.
129 for (n = 0; n < eargc; n++) {
131 nl->n_name = eargv[n];
140 printf("expanded list = ");
150 register char *cp, *cp1;
151 register struct namelist *tp;
155 extern char homedir[];
157 if (s == NULL || *s == '\0')
160 if ((which & E_VARS) && (cp = index(s, '$')) != NULL) {
163 yyerror("no variable name after '$'");
168 if ((tail = index(cp, RC)) == NULL) {
169 yyerror("unmatched '{'");
172 *tail++ = savec = '\0';
174 yyerror("no variable name after '$'");
182 tp = lookup(cp, NULL, 0);
186 for (; tp != NULL; tp = tp->n_next) {
187 snprintf(buf, sizeof(buf),
188 "%s%s%s", s, tp->n_name, tail);
193 snprintf(buf, sizeof(buf), "%s%s", s, tail);
197 if ((which & ~E_VARS) == 0 || !strcmp(s, "{") || !strcmp(s, "{}")) {
204 if (*cp == '\0' || *cp == '/') {
212 while (*cp && *cp != '/');
214 if (pw == NULL || strcmp(pw->pw_name, buf+1) != 0) {
215 if ((pw = getpwnam(buf+1)) == NULL) {
216 strcat(buf, ": unknown user name");
224 for (cp = path; (*cp++ = *cp1++); )
226 tpathp = pathp = cp - 1;
228 tpathp = pathp = path;
232 if (!(which & E_SHELL)) {
244 Cat(s, ""); /* "nonomatch" is set */
253 return (strcmp(*(char **)a1, *(char **)a2));
257 * If there are any Shell meta characters in the name,
258 * expand into a list, after searching directory
265 register char *spathp, *oldcp;
270 while (!any(*cp, shchars)) {
272 if (!expany || stat(path, &stb) >= 0) {
283 while (cp > s && *cp != '/')
303 register struct dirent *dp;
306 dirp = opendir(path);
312 if (fstat(dirp->dd_fd, &stb) < 0)
314 if (!ISDIR(stb.st_mode)) {
318 while ((dp = readdir(dirp)) != NULL)
319 if (match(dp->d_name, pattern)) {
321 Cat(path, dp->d_name);
323 strcpy(pathp, dp->d_name);
335 strcat(path, strerror(errno));
343 char restbuf[BUFSIZ + 2];
344 register char *pe, *pm, *pl;
346 char *lm, savec, *spathp;
348 for (lm = restbuf; *p != '{'; *lm++ = *p++)
350 for (pe = ++p; *pe; pe++)
364 for (pe++; *pe && *pe != ']'; pe++)
367 yyerror("Missing ']'");
371 if (brclev || !*pe) {
372 yyerror("Missing '}'");
375 for (pl = pm = p; pm <= pe; pm++)
376 switch (*pm & (QUOTE|TRIM)) {
396 strcat(restbuf, pe + 1);
403 } else if (amatch(s, restbuf))
410 for (pm++; *pm && *pm != ']'; pm++)
413 yyerror("Missing ']'");
424 register char *sentp;
425 char sexpany = expany;
427 if (*s == '.' && *p != '.')
439 register char *s, *p;
453 return (execbrc(p - 1, s - 1));
458 while ((cc = *p++)) {
465 if (lc <= scc && scc <= *p++)
468 if (scc == (lc = cc))
472 yyerror("Missing ']'");
490 return (scc == '\0');
493 if ((c & TRIM) != scc)
511 if (stat(path, &stb) == 0 && ISDIR(stb.st_mode)) {
529 register char *s, *p;
542 while ((cc = *p++)) {
549 if (lc <= scc && scc <= *p++)
552 if (scc == (lc = cc))
556 yyerror("Missing ']'");
570 return (scc == '\0');
573 if ((c & TRIM) != scc)
588 register char *s1, *s2;
590 int len = strlen(s1) + strlen(s2) + 1;
594 if (nleft <= 0 || ++eargc >= GAVSIZ)
595 yyerror("Arguments too long");
597 eargv[eargc - 1] = s = malloc(len);
599 fatal("ran out of memory\n");
600 while ((*s++ = *s1++ & TRIM))
603 while ((*s++ = *s2++ & TRIM))
612 if (pathp >= lastpathp)
613 yyerror("Pathname too long");
621 * Expand file names beginning with `~' into the
622 * user's home directory path name. Return a pointer in buf to the
623 * part corresponding to `file'.
626 exptilde(buf, file, maxlen)
631 register char *s1, *s2, *s3;
632 extern char homedir[];
634 if (strlen(file) >= maxlen)
640 if (*++file == '\0') {
643 } else if (*file == '/') {
648 while (*s3 && *s3 != '/')
654 if (pw == NULL || strcmp(pw->pw_name, file) != 0) {
655 if ((pw = getpwnam(file)) == NULL) {
656 error("%s: unknown user name\n", file);
666 for (s1 = buf; (*s1++ = *s2++) && s1 < buf+maxlen; )
669 if (s3 != NULL && s1 < buf+maxlen) {
671 while ((*s1++ = *s3++) && s1 < buf+maxlen)
674 if (s1 == buf+maxlen)