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 * 4. 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 *);
67 static int smatch(char *, char *);
70 * Take a list of names and expand any macros, etc.
71 * wh = E_VARS if expanding variables.
72 * wh = E_SHELL if expanding shell characters.
73 * wh = E_TILDE if expanding `~'.
74 * or any of these or'ed together.
76 * Major portions of this were snarfed from csh/sh.glob.c.
79 expand(struct namelist *list, int wh)
81 struct namelist *nl, *prev;
84 char *argvbuf[GAVSIZ];
87 printf("expand(%p, %d)\nlist = ", list, wh);
94 for (nl = list; nl != NULL; nl = nl->n_next)
95 for (cp = nl->n_name; *cp; cp++)
101 path = tpathp = pathp = pathbuf;
103 lastpathp = &path[sizeof pathbuf - 2];
106 eargv = sortbase = argvbuf;
110 * Walk the name list and expand names into eargv[];
112 for (nl = list; nl != NULL; nl = nl->n_next)
115 * Take expanded list of names from eargv[] and build a new list.
118 for (n = 0; n < eargc; n++) {
120 nl->n_name = eargv[n];
129 printf("expanded list = ");
143 extern char homedir[];
145 if (s == NULL || *s == '\0')
148 if ((which & E_VARS) && (cp = index(s, '$')) != NULL) {
151 yyerror("no variable name after '$'");
156 if ((tail = index(cp, RC)) == NULL) {
157 yyerror("unmatched '{'");
160 *tail++ = savec = '\0';
162 yyerror("no variable name after '$'");
170 tp = lookup(cp, 0, NULL);
174 for (; tp != NULL; tp = tp->n_next) {
175 snprintf(buf, sizeof(buf),
176 "%s%s%s", s, tp->n_name, tail);
181 snprintf(buf, sizeof(buf), "%s%s", s, tail);
185 if ((which & ~E_VARS) == 0 || !strcmp(s, "{") || !strcmp(s, "{}")) {
192 if (*cp == '\0' || *cp == '/') {
200 while (*cp && *cp != '/');
202 if (pw == NULL || strcmp(pw->pw_name, buf+1) != 0) {
203 if ((pw = getpwnam(buf+1)) == NULL) {
204 strcat(buf, ": unknown user name");
212 for (cp = path; (*cp++ = *cp1++); )
214 tpathp = pathp = cp - 1;
216 tpathp = pathp = path;
220 if (!(which & E_SHELL)) {
232 Cat(s, ""); /* "nonomatch" is set */
237 argcmp(const void *a1, const void *a2)
240 return (strcmp(*(char **)a1, *(char **)a2));
244 * If there are any Shell meta characters in the name,
245 * expand into a list, after searching directory
251 char *spathp, *oldcp;
256 while (!any(*cp, shchars)) {
258 if (!expany || stat(path, &stb) >= 0) {
269 while (cp > s && *cp != '/')
285 matchdir(char *pattern)
291 dirp = opendir(path);
297 if (fstat(dirp->dd_fd, &stb) < 0)
299 if (!ISDIR(stb.st_mode)) {
303 while ((dp = readdir(dirp)) != NULL)
304 if (match(dp->d_name, pattern)) {
306 Cat(path, dp->d_name);
308 strcpy(pathp, dp->d_name);
320 strcat(path, strerror(errno));
325 execbrc(char *p, char *s)
327 char restbuf[BUFSIZ + 2];
330 char *lm, savec, *spathp;
334 for (lm = restbuf; *p != '{'; *lm++ = *p++)
336 for (pe = ++p; *pe; pe++)
350 for (pe++; *pe && *pe != ']'; pe++)
353 yyerror("Missing ']'");
357 if (brclev || !*pe) {
358 yyerror("Missing '}'");
361 for (pl = pm = p; pm <= pe; pm++)
362 switch (*pm & (QUOTE|TRIM)) {
382 strcat(restbuf, pe + 1);
389 } else if (amatch(s, restbuf))
396 for (pm++; *pm && *pm != ']'; pm++)
399 yyerror("Missing ']'");
406 match(char *s, char *p)
413 if (*s == '.' && *p != '.')
424 amatch(char *s, char *p)
437 return (execbrc(p - 1, s - 1));
442 while ((cc = *p++)) {
449 if (lc <= scc && scc <= *p++)
452 if (scc == (lc = cc))
456 yyerror("Missing ']'");
474 return (scc == '\0');
477 if ((c & TRIM) != scc)
495 if (stat(path, &stb) == 0 && ISDIR(stb.st_mode)) {
512 smatch(char *s, char *p)
524 while ((cc = *p++)) {
531 if (lc <= scc && scc <= *p++)
534 if (scc == (lc = cc))
538 yyerror("Missing ']'");
552 return (scc == '\0');
555 if ((c & TRIM) != scc)
569 Cat(char *s1, char *s2)
574 len = strlen(s1) + strlen(s2) + 1;
576 if (nleft <= 0 || ++eargc >= GAVSIZ)
577 yyerror("Arguments too long");
579 eargv[eargc - 1] = s = malloc(len);
581 fatal("ran out of memory\n");
582 while ((*s++ = *s1++ & TRIM))
585 while ((*s++ = *s2++ & TRIM))
593 if (pathp >= lastpathp)
594 yyerror("Pathname too long");
602 * Expand file names beginning with `~' into the
603 * user's home directory path name. Return a pointer in buf to the
604 * part corresponding to `file'.
607 exptilde(char buf[], char *file, int maxlen)
610 extern char homedir[];
612 if (strlen(file) >= maxlen)
618 if (*++file == '\0') {
621 } else if (*file == '/') {
626 while (*s3 && *s3 != '/')
632 if (pw == NULL || strcmp(pw->pw_name, file) != 0) {
633 if ((pw = getpwnam(file)) == NULL) {
634 error("%s: unknown user name\n", file);
644 for (s1 = buf; (*s1++ = *s2++) && s1 < buf+maxlen; )
647 if (s3 != NULL && s1 < buf+maxlen) {
649 while ((*s1++ = *s3++) && s1 < buf+maxlen)
652 if (s1 == buf+maxlen)