17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * Copyright (c) 1983 Regents of the University of California.
37c478bd9Sstevel@tonic-gate * All rights reserved.
47c478bd9Sstevel@tonic-gate *
57c478bd9Sstevel@tonic-gate * Redistribution and use in source and binary forms are permitted
67c478bd9Sstevel@tonic-gate * provided that the above copyright notice and this paragraph are
77c478bd9Sstevel@tonic-gate * duplicated in all such forms and that any documentation,
87c478bd9Sstevel@tonic-gate * advertising materials, and other materials related to such
97c478bd9Sstevel@tonic-gate * distribution and use acknowledge that the software was developed
107c478bd9Sstevel@tonic-gate * by the University of California, Berkeley. The name of the
117c478bd9Sstevel@tonic-gate * University may not be used to endorse or promote products derived
127c478bd9Sstevel@tonic-gate * from this software without specific prior written permission.
137c478bd9Sstevel@tonic-gate *
14740638c8Sbw * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
15740638c8Sbw * Use is subject to license terms.
167c478bd9Sstevel@tonic-gate */
177c478bd9Sstevel@tonic-gate
187c478bd9Sstevel@tonic-gate #include "defs.h"
197c478bd9Sstevel@tonic-gate #include <string.h>
20*bf31a5a2SToomas Soome #include <strings.h>
217c478bd9Sstevel@tonic-gate
227c478bd9Sstevel@tonic-gate #define GAVSIZ NCARGS / 6
237c478bd9Sstevel@tonic-gate #define LC '{'
247c478bd9Sstevel@tonic-gate #define RC '}'
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate static char shchars[] = "${[*?";
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate int which; /* bit mask of types to expand */
297c478bd9Sstevel@tonic-gate int eargc; /* expanded arg count */
307c478bd9Sstevel@tonic-gate char **eargv; /* expanded arg vectors */
317c478bd9Sstevel@tonic-gate char *path;
327c478bd9Sstevel@tonic-gate char *pathp;
337c478bd9Sstevel@tonic-gate char *lastpathp;
347c478bd9Sstevel@tonic-gate char *tilde; /* "~user" if not expanding tilde, else "" */
357c478bd9Sstevel@tonic-gate char *tpathp;
367c478bd9Sstevel@tonic-gate int nleft;
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gate int expany; /* any expansions done? */
397c478bd9Sstevel@tonic-gate char *entp;
407c478bd9Sstevel@tonic-gate char **sortbase;
41*bf31a5a2SToomas Soome char *argvbuf[GAVSIZ];
42*bf31a5a2SToomas Soome char pathbuf[LINESIZE];
43740638c8Sbw
44740638c8Sbw static int argcmp(const void *arg1, const void *arg2);
45740638c8Sbw static void addpath(char c);
46740638c8Sbw static void Cat(char *s1, char *s2);
47740638c8Sbw static void matchdir(char *pattern);
48740638c8Sbw static void expsh(char *s);
49740638c8Sbw static void expstr(char *s);
50740638c8Sbw static int execbrc(char *p, char *s);
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate #define sort() qsort((char *)sortbase, &eargv[eargc] - sortbase, \
537c478bd9Sstevel@tonic-gate sizeof (*sortbase), argcmp), sortbase = &eargv[eargc]
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate #define MIN(a, b) ((a) < (b) ? (a) : (b))
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate /*
587c478bd9Sstevel@tonic-gate * Take a list of names and expand any macros, etc.
597c478bd9Sstevel@tonic-gate * wh = E_VARS if expanding variables.
607c478bd9Sstevel@tonic-gate * wh = E_SHELL if expanding shell characters.
617c478bd9Sstevel@tonic-gate * wh = E_TILDE if expanding `~'.
627c478bd9Sstevel@tonic-gate * or any of these or'ed together.
637c478bd9Sstevel@tonic-gate *
647c478bd9Sstevel@tonic-gate * Major portions of this were snarfed from csh/sh.glob.c.
657c478bd9Sstevel@tonic-gate */
667c478bd9Sstevel@tonic-gate struct namelist *
expand(struct namelist * list,int wh)67*bf31a5a2SToomas Soome expand(struct namelist *list, int wh)
687c478bd9Sstevel@tonic-gate {
69*bf31a5a2SToomas Soome struct namelist *nl, *prev;
70*bf31a5a2SToomas Soome int n;
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate if (debug) {
737c478bd9Sstevel@tonic-gate printf("expand(%x, %d)\nlist = ", list, wh);
747c478bd9Sstevel@tonic-gate prnames(list);
757c478bd9Sstevel@tonic-gate }
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate if (wh == 0) {
78*bf31a5a2SToomas Soome char *cp;
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate for (nl = list; nl != NULL; nl = nl->n_next)
817c478bd9Sstevel@tonic-gate for (cp = nl->n_name; *cp; cp++)
827c478bd9Sstevel@tonic-gate *cp = *cp & TRIM;
837c478bd9Sstevel@tonic-gate return (list);
847c478bd9Sstevel@tonic-gate }
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate which = wh;
877c478bd9Sstevel@tonic-gate path = tpathp = pathp = pathbuf;
887c478bd9Sstevel@tonic-gate *pathp = '\0';
89*bf31a5a2SToomas Soome lastpathp = &path[sizeof (pathbuf) - 2];
907c478bd9Sstevel@tonic-gate tilde = "";
917c478bd9Sstevel@tonic-gate eargc = 0;
927c478bd9Sstevel@tonic-gate eargv = sortbase = argvbuf;
937c478bd9Sstevel@tonic-gate *eargv = 0;
947c478bd9Sstevel@tonic-gate nleft = NCARGS - 4;
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate * Walk the name list and expand names into eargv[];
977c478bd9Sstevel@tonic-gate */
987c478bd9Sstevel@tonic-gate for (nl = list; nl != NULL; nl = nl->n_next)
997c478bd9Sstevel@tonic-gate expstr(nl->n_name);
1007c478bd9Sstevel@tonic-gate /*
1017c478bd9Sstevel@tonic-gate * Take expanded list of names from eargv[] and build a new list.
1027c478bd9Sstevel@tonic-gate */
1037c478bd9Sstevel@tonic-gate list = prev = NULL;
1047c478bd9Sstevel@tonic-gate for (n = 0; n < eargc; n++) {
1057c478bd9Sstevel@tonic-gate nl = makenl(NULL);
1067c478bd9Sstevel@tonic-gate nl->n_name = eargv[n];
1077c478bd9Sstevel@tonic-gate if (prev == NULL)
1087c478bd9Sstevel@tonic-gate list = prev = nl;
1097c478bd9Sstevel@tonic-gate else {
1107c478bd9Sstevel@tonic-gate prev->n_next = nl;
1117c478bd9Sstevel@tonic-gate prev = nl;
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate if (debug) {
1157c478bd9Sstevel@tonic-gate printf("expanded list = ");
1167c478bd9Sstevel@tonic-gate prnames(list);
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate return (list);
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate
121740638c8Sbw static void
expstr(char * s)122*bf31a5a2SToomas Soome expstr(char *s)
1237c478bd9Sstevel@tonic-gate {
124*bf31a5a2SToomas Soome char *cp, *cp1;
125*bf31a5a2SToomas Soome struct namelist *tp;
1267c478bd9Sstevel@tonic-gate char *tail;
1277c478bd9Sstevel@tonic-gate char buf[LINESIZE];
1287c478bd9Sstevel@tonic-gate int savec, oeargc;
1297c478bd9Sstevel@tonic-gate extern char homedir[];
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate if (s == NULL || *s == '\0')
1327c478bd9Sstevel@tonic-gate return;
1337c478bd9Sstevel@tonic-gate
1347c478bd9Sstevel@tonic-gate if ((which & E_VARS) && (cp = index(s, '$')) != NULL) {
1357c478bd9Sstevel@tonic-gate *cp++ = '\0';
1367c478bd9Sstevel@tonic-gate if (*cp == '\0') {
1377c478bd9Sstevel@tonic-gate yyerror("no variable name after '$'");
1387c478bd9Sstevel@tonic-gate return;
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate if (*cp == LC) {
1417c478bd9Sstevel@tonic-gate cp++;
1427c478bd9Sstevel@tonic-gate if ((tail = index(cp, RC)) == NULL) {
1437c478bd9Sstevel@tonic-gate yyerror("unmatched '{'");
1447c478bd9Sstevel@tonic-gate return;
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate *tail++ = savec = '\0';
1477c478bd9Sstevel@tonic-gate if (*cp == '\0') {
1487c478bd9Sstevel@tonic-gate yyerror("no variable name after '$'");
1497c478bd9Sstevel@tonic-gate return;
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate } else {
1527c478bd9Sstevel@tonic-gate tail = cp + 1;
1537c478bd9Sstevel@tonic-gate savec = *tail;
1547c478bd9Sstevel@tonic-gate *tail = '\0';
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate tp = lookup(cp, NULL, 0);
1577c478bd9Sstevel@tonic-gate if (savec != '\0')
1587c478bd9Sstevel@tonic-gate *tail = savec;
1597c478bd9Sstevel@tonic-gate if (tp != NULL) {
1607c478bd9Sstevel@tonic-gate for (; tp != NULL; tp = tp->n_next) {
1617c478bd9Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%s%s%s", s,
1627c478bd9Sstevel@tonic-gate tp->n_name, tail);
1637c478bd9Sstevel@tonic-gate expstr(buf);
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate return;
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%s%s", s, tail);
1687c478bd9Sstevel@tonic-gate expstr(buf);
1697c478bd9Sstevel@tonic-gate return;
1707c478bd9Sstevel@tonic-gate }
171*bf31a5a2SToomas Soome if ((which & ~E_VARS) == 0 ||
172*bf31a5a2SToomas Soome strcmp(s, "{") == 0 ||
173*bf31a5a2SToomas Soome strcmp(s, "{}") == 0) {
1747c478bd9Sstevel@tonic-gate Cat(s, "");
1757c478bd9Sstevel@tonic-gate sort();
1767c478bd9Sstevel@tonic-gate return;
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate if (*s == '~') {
1797c478bd9Sstevel@tonic-gate cp = ++s;
1807c478bd9Sstevel@tonic-gate if (*cp == '\0' || *cp == '/') {
1817c478bd9Sstevel@tonic-gate tilde = "~";
1827c478bd9Sstevel@tonic-gate cp1 = homedir;
1837c478bd9Sstevel@tonic-gate } else {
1847c478bd9Sstevel@tonic-gate tilde = cp1 = buf;
1857c478bd9Sstevel@tonic-gate *cp1++ = '~';
1867c478bd9Sstevel@tonic-gate do {
1877c478bd9Sstevel@tonic-gate if (cp1 >= &buf[sizeof (buf)]) {
1887c478bd9Sstevel@tonic-gate yyerror("User name too long");
1897c478bd9Sstevel@tonic-gate return;
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate *cp1++ = *cp++;
1927c478bd9Sstevel@tonic-gate } while (*cp && *cp != '/');
1937c478bd9Sstevel@tonic-gate *cp1 = '\0';
1947c478bd9Sstevel@tonic-gate if (pw == NULL || strcmp(pw->pw_name, buf+1) != 0) {
1957c478bd9Sstevel@tonic-gate if ((pw = getpwnam(buf+1)) == NULL) {
1967c478bd9Sstevel@tonic-gate static char unknown_user[] =
1977c478bd9Sstevel@tonic-gate ": unknown user name";
1987c478bd9Sstevel@tonic-gate
1997c478bd9Sstevel@tonic-gate cp1 = MIN(cp1,
2007c478bd9Sstevel@tonic-gate &buf[sizeof (buf)] -
2017c478bd9Sstevel@tonic-gate sizeof (unknown_user));
2027c478bd9Sstevel@tonic-gate strcpy(cp1, unknown_user);
2037c478bd9Sstevel@tonic-gate yyerror(buf+1);
2047c478bd9Sstevel@tonic-gate return;
2057c478bd9Sstevel@tonic-gate }
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate cp1 = pw->pw_dir;
2087c478bd9Sstevel@tonic-gate s = cp;
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate for (cp = path; cp <= lastpathp + 1 && (*cp++ = *cp1++); )
2117c478bd9Sstevel@tonic-gate ;
2127c478bd9Sstevel@tonic-gate tpathp = pathp = cp - 1;
2137c478bd9Sstevel@tonic-gate } else {
2147c478bd9Sstevel@tonic-gate tpathp = pathp = path;
2157c478bd9Sstevel@tonic-gate tilde = "";
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate *pathp = '\0';
2187c478bd9Sstevel@tonic-gate if (!(which & E_SHELL)) {
2197c478bd9Sstevel@tonic-gate if (which & E_TILDE)
2207c478bd9Sstevel@tonic-gate Cat(path, s);
2217c478bd9Sstevel@tonic-gate else
2227c478bd9Sstevel@tonic-gate Cat(tilde, s);
2237c478bd9Sstevel@tonic-gate sort();
2247c478bd9Sstevel@tonic-gate return;
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate oeargc = eargc;
2277c478bd9Sstevel@tonic-gate expany = 0;
2287c478bd9Sstevel@tonic-gate expsh(s);
2297c478bd9Sstevel@tonic-gate if (eargc == oeargc)
2307c478bd9Sstevel@tonic-gate Cat(s, ""); /* "nonomatch" is set */
2317c478bd9Sstevel@tonic-gate sort();
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate
234740638c8Sbw static int
argcmp(const void * arg1,const void * arg2)235740638c8Sbw argcmp(const void *arg1, const void *arg2)
2367c478bd9Sstevel@tonic-gate {
237740638c8Sbw char *a1 = *(char **)arg1;
238740638c8Sbw char *a2 = *(char **)arg2;
2397c478bd9Sstevel@tonic-gate
240740638c8Sbw return (strcmp(a1, a2));
2417c478bd9Sstevel@tonic-gate }
2427c478bd9Sstevel@tonic-gate
2437c478bd9Sstevel@tonic-gate /*
2447c478bd9Sstevel@tonic-gate * If there are any Shell meta characters in the name,
2457c478bd9Sstevel@tonic-gate * expand into a list, after searching directory
2467c478bd9Sstevel@tonic-gate */
247740638c8Sbw static void
expsh(char * s)248*bf31a5a2SToomas Soome expsh(char *s)
2497c478bd9Sstevel@tonic-gate {
250*bf31a5a2SToomas Soome char *cp;
251*bf31a5a2SToomas Soome char *spathp, *oldcp;
2527c478bd9Sstevel@tonic-gate struct stat stb;
2537c478bd9Sstevel@tonic-gate
2547c478bd9Sstevel@tonic-gate spathp = pathp;
2557c478bd9Sstevel@tonic-gate cp = s;
2567c478bd9Sstevel@tonic-gate while (!any(*cp, shchars)) {
2577c478bd9Sstevel@tonic-gate if (*cp == '\0') {
2587c478bd9Sstevel@tonic-gate if (!expany || stat(path, &stb) >= 0) {
2597c478bd9Sstevel@tonic-gate if (which & E_TILDE)
2607c478bd9Sstevel@tonic-gate Cat(path, "");
2617c478bd9Sstevel@tonic-gate else
2627c478bd9Sstevel@tonic-gate Cat(tilde, tpathp);
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate goto endit;
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate addpath(*cp++);
2677c478bd9Sstevel@tonic-gate }
2687c478bd9Sstevel@tonic-gate oldcp = cp;
2697c478bd9Sstevel@tonic-gate while (cp > s && *cp != '/')
2707c478bd9Sstevel@tonic-gate cp--, pathp--;
2717c478bd9Sstevel@tonic-gate if (*cp == '/')
2727c478bd9Sstevel@tonic-gate cp++, pathp++;
2737c478bd9Sstevel@tonic-gate *pathp = '\0';
2747c478bd9Sstevel@tonic-gate if (*oldcp == '{') {
2757c478bd9Sstevel@tonic-gate execbrc(cp, NULL);
2767c478bd9Sstevel@tonic-gate return;
2777c478bd9Sstevel@tonic-gate }
2787c478bd9Sstevel@tonic-gate matchdir(cp);
2797c478bd9Sstevel@tonic-gate endit:
2807c478bd9Sstevel@tonic-gate pathp = spathp;
2817c478bd9Sstevel@tonic-gate *pathp = '\0';
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate
284740638c8Sbw static void
matchdir(char * pattern)285*bf31a5a2SToomas Soome matchdir(char *pattern)
2867c478bd9Sstevel@tonic-gate {
2877c478bd9Sstevel@tonic-gate struct stat stb;
288*bf31a5a2SToomas Soome struct dirent *dp;
2897c478bd9Sstevel@tonic-gate DIR *dirp;
2907c478bd9Sstevel@tonic-gate
2917c478bd9Sstevel@tonic-gate dirp = opendir(path);
2927c478bd9Sstevel@tonic-gate if (dirp == NULL) {
2937c478bd9Sstevel@tonic-gate if (expany)
2947c478bd9Sstevel@tonic-gate return;
2957c478bd9Sstevel@tonic-gate goto patherr2;
2967c478bd9Sstevel@tonic-gate }
2977c478bd9Sstevel@tonic-gate if (fstat(dirp->dd_fd, &stb) < 0)
2987c478bd9Sstevel@tonic-gate goto patherr1;
2997c478bd9Sstevel@tonic-gate if (!ISDIR(stb.st_mode)) {
3007c478bd9Sstevel@tonic-gate errno = ENOTDIR;
3017c478bd9Sstevel@tonic-gate goto patherr1;
3027c478bd9Sstevel@tonic-gate }
3037c478bd9Sstevel@tonic-gate while ((dp = readdir(dirp)) != NULL)
3047c478bd9Sstevel@tonic-gate if (match(dp->d_name, pattern)) {
3057c478bd9Sstevel@tonic-gate if (which & E_TILDE)
3067c478bd9Sstevel@tonic-gate Cat(path, dp->d_name);
3077c478bd9Sstevel@tonic-gate else {
3087c478bd9Sstevel@tonic-gate if (pathp + strlen(dp->d_name) - 1 >
3097c478bd9Sstevel@tonic-gate lastpathp) {
3107c478bd9Sstevel@tonic-gate errno = ENAMETOOLONG;
3117c478bd9Sstevel@tonic-gate goto patherr1;
3127c478bd9Sstevel@tonic-gate }
3137c478bd9Sstevel@tonic-gate strcpy(pathp, dp->d_name);
3147c478bd9Sstevel@tonic-gate Cat(tilde, tpathp);
3157c478bd9Sstevel@tonic-gate *pathp = '\0';
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate }
3187c478bd9Sstevel@tonic-gate closedir(dirp);
3197c478bd9Sstevel@tonic-gate return;
3207c478bd9Sstevel@tonic-gate
3217c478bd9Sstevel@tonic-gate patherr1:
3227c478bd9Sstevel@tonic-gate closedir(dirp);
3237c478bd9Sstevel@tonic-gate patherr2:
3247c478bd9Sstevel@tonic-gate {
3257c478bd9Sstevel@tonic-gate char *strerr = strerror(errno);
3267c478bd9Sstevel@tonic-gate
3277c478bd9Sstevel@tonic-gate if (path + strlen(path) + strlen(strerr) + 1 > lastpathp)
3287c478bd9Sstevel@tonic-gate strcpy(lastpathp - strlen(strerr) - 1, ": ");
3297c478bd9Sstevel@tonic-gate else
3307c478bd9Sstevel@tonic-gate strcat(path, ": ");
3317c478bd9Sstevel@tonic-gate strcat(path, strerr);
3327c478bd9Sstevel@tonic-gate }
3337c478bd9Sstevel@tonic-gate yyerror(path);
3347c478bd9Sstevel@tonic-gate }
3357c478bd9Sstevel@tonic-gate
336740638c8Sbw static int
execbrc(char * p,char * s)337*bf31a5a2SToomas Soome execbrc(char *p, char *s)
3387c478bd9Sstevel@tonic-gate {
3397c478bd9Sstevel@tonic-gate char restbuf[LINESIZE + 2];
340*bf31a5a2SToomas Soome char *pe, *pm, *pl;
3417c478bd9Sstevel@tonic-gate int brclev = 0;
3427c478bd9Sstevel@tonic-gate char *lm, savec, *spathp;
3437c478bd9Sstevel@tonic-gate
3447c478bd9Sstevel@tonic-gate for (lm = restbuf; *p != '{'; *lm++ = *p++) {
3457c478bd9Sstevel@tonic-gate if (lm >= &restbuf[sizeof (restbuf)]) {
3467c478bd9Sstevel@tonic-gate yyerror("Pathname too long");
3477c478bd9Sstevel@tonic-gate return (0);
3487c478bd9Sstevel@tonic-gate }
3497c478bd9Sstevel@tonic-gate }
3507c478bd9Sstevel@tonic-gate for (pe = ++p; *pe; pe++)
3517c478bd9Sstevel@tonic-gate switch (*pe) {
3527c478bd9Sstevel@tonic-gate
3537c478bd9Sstevel@tonic-gate case '{':
3547c478bd9Sstevel@tonic-gate brclev++;
3557c478bd9Sstevel@tonic-gate continue;
3567c478bd9Sstevel@tonic-gate
3577c478bd9Sstevel@tonic-gate case '}':
3587c478bd9Sstevel@tonic-gate if (brclev == 0)
3597c478bd9Sstevel@tonic-gate goto pend;
3607c478bd9Sstevel@tonic-gate brclev--;
3617c478bd9Sstevel@tonic-gate continue;
3627c478bd9Sstevel@tonic-gate
3637c478bd9Sstevel@tonic-gate case '[':
3647c478bd9Sstevel@tonic-gate for (pe++; *pe && *pe != ']'; pe++)
3657c478bd9Sstevel@tonic-gate continue;
3667c478bd9Sstevel@tonic-gate if (!*pe)
3677c478bd9Sstevel@tonic-gate yyerror("Missing ']'");
3687c478bd9Sstevel@tonic-gate continue;
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate pend:
3717c478bd9Sstevel@tonic-gate if (brclev || !*pe) {
3727c478bd9Sstevel@tonic-gate yyerror("Missing '}'");
3737c478bd9Sstevel@tonic-gate return (0);
3747c478bd9Sstevel@tonic-gate }
3757c478bd9Sstevel@tonic-gate for (pl = pm = p; pm <= pe; pm++)
3767c478bd9Sstevel@tonic-gate switch (*pm & (QUOTE|TRIM)) {
3777c478bd9Sstevel@tonic-gate
3787c478bd9Sstevel@tonic-gate case '{':
3797c478bd9Sstevel@tonic-gate brclev++;
3807c478bd9Sstevel@tonic-gate continue;
3817c478bd9Sstevel@tonic-gate
3827c478bd9Sstevel@tonic-gate case '}':
3837c478bd9Sstevel@tonic-gate if (brclev) {
3847c478bd9Sstevel@tonic-gate brclev--;
3857c478bd9Sstevel@tonic-gate continue;
3867c478bd9Sstevel@tonic-gate }
3877c478bd9Sstevel@tonic-gate goto doit;
3887c478bd9Sstevel@tonic-gate
3897c478bd9Sstevel@tonic-gate case ',':
3907c478bd9Sstevel@tonic-gate if (brclev)
3917c478bd9Sstevel@tonic-gate continue;
3927c478bd9Sstevel@tonic-gate doit:
3937c478bd9Sstevel@tonic-gate savec = *pm;
3947c478bd9Sstevel@tonic-gate *pm = 0;
3957c478bd9Sstevel@tonic-gate if (lm + strlen(pl) + strlen(pe + 1) >=
3967c478bd9Sstevel@tonic-gate &restbuf[sizeof (restbuf)]) {
3977c478bd9Sstevel@tonic-gate yyerror("Pathname too long");
3987c478bd9Sstevel@tonic-gate return (0);
3997c478bd9Sstevel@tonic-gate }
4007c478bd9Sstevel@tonic-gate strcpy(lm, pl);
4017c478bd9Sstevel@tonic-gate strcat(restbuf, pe + 1);
4027c478bd9Sstevel@tonic-gate *pm = savec;
4037c478bd9Sstevel@tonic-gate if (s == 0) {
4047c478bd9Sstevel@tonic-gate spathp = pathp;
4057c478bd9Sstevel@tonic-gate expsh(restbuf);
4067c478bd9Sstevel@tonic-gate pathp = spathp;
4077c478bd9Sstevel@tonic-gate *pathp = 0;
4087c478bd9Sstevel@tonic-gate } else if (amatch(s, restbuf))
4097c478bd9Sstevel@tonic-gate return (1);
4107c478bd9Sstevel@tonic-gate sort();
4117c478bd9Sstevel@tonic-gate pl = pm + 1;
4127c478bd9Sstevel@tonic-gate continue;
4137c478bd9Sstevel@tonic-gate
4147c478bd9Sstevel@tonic-gate case '[':
4157c478bd9Sstevel@tonic-gate for (pm++; *pm && *pm != ']'; pm++)
4167c478bd9Sstevel@tonic-gate continue;
4177c478bd9Sstevel@tonic-gate if (!*pm)
4187c478bd9Sstevel@tonic-gate yyerror("Missing ']'");
4197c478bd9Sstevel@tonic-gate continue;
4207c478bd9Sstevel@tonic-gate }
4217c478bd9Sstevel@tonic-gate return (0);
4227c478bd9Sstevel@tonic-gate }
4237c478bd9Sstevel@tonic-gate
424740638c8Sbw int
match(char * s,char * p)425*bf31a5a2SToomas Soome match(char *s, char *p)
4267c478bd9Sstevel@tonic-gate {
427*bf31a5a2SToomas Soome int c;
428*bf31a5a2SToomas Soome char *sentp;
4297c478bd9Sstevel@tonic-gate char sexpany = expany;
4307c478bd9Sstevel@tonic-gate
4317c478bd9Sstevel@tonic-gate if (*s == '.' && *p != '.')
4327c478bd9Sstevel@tonic-gate return (0);
4337c478bd9Sstevel@tonic-gate sentp = entp;
4347c478bd9Sstevel@tonic-gate entp = s;
4357c478bd9Sstevel@tonic-gate c = amatch(s, p);
4367c478bd9Sstevel@tonic-gate entp = sentp;
4377c478bd9Sstevel@tonic-gate expany = sexpany;
4387c478bd9Sstevel@tonic-gate return (c);
4397c478bd9Sstevel@tonic-gate }
4407c478bd9Sstevel@tonic-gate
441740638c8Sbw int
amatch(char * s,char * p)442*bf31a5a2SToomas Soome amatch(char *s, char *p)
4437c478bd9Sstevel@tonic-gate {
444*bf31a5a2SToomas Soome int scc;
4457c478bd9Sstevel@tonic-gate int ok, lc;
4467c478bd9Sstevel@tonic-gate char *spathp;
4477c478bd9Sstevel@tonic-gate struct stat stb;
4487c478bd9Sstevel@tonic-gate int c, cc;
4497c478bd9Sstevel@tonic-gate
4507c478bd9Sstevel@tonic-gate expany = 1;
4517c478bd9Sstevel@tonic-gate for (;;) {
4527c478bd9Sstevel@tonic-gate scc = *s++ & TRIM;
4537c478bd9Sstevel@tonic-gate switch (c = *p++) {
4547c478bd9Sstevel@tonic-gate
4557c478bd9Sstevel@tonic-gate case '{':
4567c478bd9Sstevel@tonic-gate return (execbrc(p - 1, s - 1));
4577c478bd9Sstevel@tonic-gate
4587c478bd9Sstevel@tonic-gate case '[':
4597c478bd9Sstevel@tonic-gate ok = 0;
4607c478bd9Sstevel@tonic-gate lc = 077777;
4617c478bd9Sstevel@tonic-gate while (cc = *p++) {
4627c478bd9Sstevel@tonic-gate if (cc == ']') {
4637c478bd9Sstevel@tonic-gate if (ok)
4647c478bd9Sstevel@tonic-gate break;
4657c478bd9Sstevel@tonic-gate return (0);
4667c478bd9Sstevel@tonic-gate }
4677c478bd9Sstevel@tonic-gate if (cc == '-') {
4687c478bd9Sstevel@tonic-gate if (lc <= scc && scc <= *p++)
4697c478bd9Sstevel@tonic-gate ok++;
4707c478bd9Sstevel@tonic-gate } else
4717c478bd9Sstevel@tonic-gate if (scc == (lc = cc))
4727c478bd9Sstevel@tonic-gate ok++;
4737c478bd9Sstevel@tonic-gate }
4747c478bd9Sstevel@tonic-gate if (cc == 0) {
4757c478bd9Sstevel@tonic-gate yyerror("Missing ']'");
4767c478bd9Sstevel@tonic-gate return (0);
4777c478bd9Sstevel@tonic-gate }
4787c478bd9Sstevel@tonic-gate continue;
4797c478bd9Sstevel@tonic-gate
4807c478bd9Sstevel@tonic-gate case '*':
4817c478bd9Sstevel@tonic-gate if (!*p)
4827c478bd9Sstevel@tonic-gate return (1);
4837c478bd9Sstevel@tonic-gate if (*p == '/') {
4847c478bd9Sstevel@tonic-gate p++;
4857c478bd9Sstevel@tonic-gate goto slash;
4867c478bd9Sstevel@tonic-gate }
4877c478bd9Sstevel@tonic-gate for (s--; *s; s++)
4887c478bd9Sstevel@tonic-gate if (amatch(s, p))
4897c478bd9Sstevel@tonic-gate return (1);
4907c478bd9Sstevel@tonic-gate return (0);
4917c478bd9Sstevel@tonic-gate
4927c478bd9Sstevel@tonic-gate case '\0':
4937c478bd9Sstevel@tonic-gate return (scc == '\0');
4947c478bd9Sstevel@tonic-gate
4957c478bd9Sstevel@tonic-gate default:
4967c478bd9Sstevel@tonic-gate if ((c & TRIM) != scc)
4977c478bd9Sstevel@tonic-gate return (0);
4987c478bd9Sstevel@tonic-gate continue;
4997c478bd9Sstevel@tonic-gate
5007c478bd9Sstevel@tonic-gate case '?':
5017c478bd9Sstevel@tonic-gate if (scc == '\0')
5027c478bd9Sstevel@tonic-gate return (0);
5037c478bd9Sstevel@tonic-gate continue;
5047c478bd9Sstevel@tonic-gate
5057c478bd9Sstevel@tonic-gate case '/':
5067c478bd9Sstevel@tonic-gate if (scc)
5077c478bd9Sstevel@tonic-gate return (0);
5087c478bd9Sstevel@tonic-gate slash:
5097c478bd9Sstevel@tonic-gate s = entp;
5107c478bd9Sstevel@tonic-gate spathp = pathp;
5117c478bd9Sstevel@tonic-gate while (*s)
5127c478bd9Sstevel@tonic-gate addpath(*s++);
5137c478bd9Sstevel@tonic-gate addpath('/');
5147c478bd9Sstevel@tonic-gate if (stat(path, &stb) == 0 && ISDIR(stb.st_mode))
5157c478bd9Sstevel@tonic-gate if (*p == '\0') {
5167c478bd9Sstevel@tonic-gate if (which & E_TILDE)
5177c478bd9Sstevel@tonic-gate Cat(path, "");
5187c478bd9Sstevel@tonic-gate else
5197c478bd9Sstevel@tonic-gate Cat(tilde, tpathp);
5207c478bd9Sstevel@tonic-gate } else
5217c478bd9Sstevel@tonic-gate expsh(p);
5227c478bd9Sstevel@tonic-gate pathp = spathp;
5237c478bd9Sstevel@tonic-gate *pathp = '\0';
5247c478bd9Sstevel@tonic-gate return (0);
5257c478bd9Sstevel@tonic-gate }
5267c478bd9Sstevel@tonic-gate }
5277c478bd9Sstevel@tonic-gate }
5287c478bd9Sstevel@tonic-gate
529740638c8Sbw int
smatch(char * s,char * p)530*bf31a5a2SToomas Soome smatch(char *s, char *p)
5317c478bd9Sstevel@tonic-gate {
532*bf31a5a2SToomas Soome int scc;
5337c478bd9Sstevel@tonic-gate int ok, lc;
5347c478bd9Sstevel@tonic-gate int c, cc;
5357c478bd9Sstevel@tonic-gate
5367c478bd9Sstevel@tonic-gate for (;;) {
5377c478bd9Sstevel@tonic-gate scc = *s++ & TRIM;
5387c478bd9Sstevel@tonic-gate switch (c = *p++) {
5397c478bd9Sstevel@tonic-gate
5407c478bd9Sstevel@tonic-gate case '[':
5417c478bd9Sstevel@tonic-gate ok = 0;
5427c478bd9Sstevel@tonic-gate lc = 077777;
5437c478bd9Sstevel@tonic-gate while (cc = *p++) {
5447c478bd9Sstevel@tonic-gate if (cc == ']') {
5457c478bd9Sstevel@tonic-gate if (ok)
5467c478bd9Sstevel@tonic-gate break;
5477c478bd9Sstevel@tonic-gate return (0);
5487c478bd9Sstevel@tonic-gate }
5497c478bd9Sstevel@tonic-gate if (cc == '-') {
5507c478bd9Sstevel@tonic-gate if (lc <= scc && scc <= *p++)
5517c478bd9Sstevel@tonic-gate ok++;
5527c478bd9Sstevel@tonic-gate } else
5537c478bd9Sstevel@tonic-gate if (scc == (lc = cc))
5547c478bd9Sstevel@tonic-gate ok++;
5557c478bd9Sstevel@tonic-gate }
5567c478bd9Sstevel@tonic-gate if (cc == 0) {
5577c478bd9Sstevel@tonic-gate yyerror("Missing ']'");
5587c478bd9Sstevel@tonic-gate return (0);
5597c478bd9Sstevel@tonic-gate }
5607c478bd9Sstevel@tonic-gate continue;
5617c478bd9Sstevel@tonic-gate
5627c478bd9Sstevel@tonic-gate case '*':
5637c478bd9Sstevel@tonic-gate if (!*p)
5647c478bd9Sstevel@tonic-gate return (1);
5657c478bd9Sstevel@tonic-gate for (s--; *s; s++)
5667c478bd9Sstevel@tonic-gate if (smatch(s, p))
5677c478bd9Sstevel@tonic-gate return (1);
5687c478bd9Sstevel@tonic-gate return (0);
5697c478bd9Sstevel@tonic-gate
5707c478bd9Sstevel@tonic-gate case '\0':
5717c478bd9Sstevel@tonic-gate return (scc == '\0');
5727c478bd9Sstevel@tonic-gate
5737c478bd9Sstevel@tonic-gate default:
5747c478bd9Sstevel@tonic-gate if ((c & TRIM) != scc)
5757c478bd9Sstevel@tonic-gate return (0);
5767c478bd9Sstevel@tonic-gate continue;
5777c478bd9Sstevel@tonic-gate
5787c478bd9Sstevel@tonic-gate case '?':
5797c478bd9Sstevel@tonic-gate if (scc == 0)
5807c478bd9Sstevel@tonic-gate return (0);
5817c478bd9Sstevel@tonic-gate continue;
5827c478bd9Sstevel@tonic-gate
5837c478bd9Sstevel@tonic-gate }
5847c478bd9Sstevel@tonic-gate }
5857c478bd9Sstevel@tonic-gate }
5867c478bd9Sstevel@tonic-gate
587740638c8Sbw static void
Cat(char * s1,char * s2)588*bf31a5a2SToomas Soome Cat(char *s1, char *s2)
5897c478bd9Sstevel@tonic-gate {
5907c478bd9Sstevel@tonic-gate int len = strlen(s1) + strlen(s2) + 1;
591*bf31a5a2SToomas Soome char *s;
5927c478bd9Sstevel@tonic-gate
5937c478bd9Sstevel@tonic-gate nleft -= len;
5947c478bd9Sstevel@tonic-gate if (nleft <= 0 || ++eargc >= GAVSIZ)
5957c478bd9Sstevel@tonic-gate fatal("Arguments too long\n");
5967c478bd9Sstevel@tonic-gate eargv[eargc] = 0;
5977c478bd9Sstevel@tonic-gate eargv[eargc - 1] = s = (char *)malloc(len);
5987c478bd9Sstevel@tonic-gate if (s == NULL)
5997c478bd9Sstevel@tonic-gate fatal("ran out of memory\n");
6007c478bd9Sstevel@tonic-gate while (*s++ = *s1++ & TRIM)
6017c478bd9Sstevel@tonic-gate ;
6027c478bd9Sstevel@tonic-gate s--;
6037c478bd9Sstevel@tonic-gate while (*s++ = *s2++ & TRIM)
6047c478bd9Sstevel@tonic-gate ;
6057c478bd9Sstevel@tonic-gate }
6067c478bd9Sstevel@tonic-gate
607740638c8Sbw static void
addpath(char c)608740638c8Sbw addpath(char c)
6097c478bd9Sstevel@tonic-gate {
6107c478bd9Sstevel@tonic-gate
6117c478bd9Sstevel@tonic-gate if (pathp > lastpathp)
6127c478bd9Sstevel@tonic-gate yyerror("Pathname too long");
6137c478bd9Sstevel@tonic-gate else {
6147c478bd9Sstevel@tonic-gate *pathp++ = c & TRIM;
6157c478bd9Sstevel@tonic-gate *pathp = '\0';
6167c478bd9Sstevel@tonic-gate }
6177c478bd9Sstevel@tonic-gate }
6187c478bd9Sstevel@tonic-gate
6197c478bd9Sstevel@tonic-gate /*
6207c478bd9Sstevel@tonic-gate * Expand file names beginning with `~' into the
6217c478bd9Sstevel@tonic-gate * user's home directory path name. Return a pointer in buf to the
6227c478bd9Sstevel@tonic-gate * part corresponding to `file'.
6237c478bd9Sstevel@tonic-gate */
6247c478bd9Sstevel@tonic-gate char *
exptilde(char buf[],unsigned int len,char * file)625*bf31a5a2SToomas Soome exptilde(char buf[], unsigned int len, char *file)
6267c478bd9Sstevel@tonic-gate {
627*bf31a5a2SToomas Soome char *s1, *s2, *s3;
6287c478bd9Sstevel@tonic-gate extern char homedir[];
6297c478bd9Sstevel@tonic-gate
6307c478bd9Sstevel@tonic-gate if (*file != '~') {
6317c478bd9Sstevel@tonic-gate if (strlen(file) + 1 > len) {
6327c478bd9Sstevel@tonic-gate error("pathname too long: %s\n", file);
6337c478bd9Sstevel@tonic-gate return (NULL);
6347c478bd9Sstevel@tonic-gate }
6357c478bd9Sstevel@tonic-gate strcpy(buf, file);
6367c478bd9Sstevel@tonic-gate return (buf);
6377c478bd9Sstevel@tonic-gate }
6387c478bd9Sstevel@tonic-gate if (*++file == '\0') {
6397c478bd9Sstevel@tonic-gate s2 = homedir;
6407c478bd9Sstevel@tonic-gate s3 = NULL;
6417c478bd9Sstevel@tonic-gate } else if (*file == '/') {
6427c478bd9Sstevel@tonic-gate s2 = homedir;
6437c478bd9Sstevel@tonic-gate s3 = file;
6447c478bd9Sstevel@tonic-gate } else {
6457c478bd9Sstevel@tonic-gate s3 = file;
6467c478bd9Sstevel@tonic-gate while (*s3 && *s3 != '/')
6477c478bd9Sstevel@tonic-gate s3++;
6487c478bd9Sstevel@tonic-gate if (*s3 == '/')
6497c478bd9Sstevel@tonic-gate *s3 = '\0';
6507c478bd9Sstevel@tonic-gate else
6517c478bd9Sstevel@tonic-gate s3 = NULL;
6527c478bd9Sstevel@tonic-gate if (pw == NULL || strcmp(pw->pw_name, file) != 0) {
6537c478bd9Sstevel@tonic-gate if ((pw = getpwnam(file)) == NULL) {
6547c478bd9Sstevel@tonic-gate error("%s: unknown user name\n", file);
6557c478bd9Sstevel@tonic-gate if (s3 != NULL)
6567c478bd9Sstevel@tonic-gate *s3 = '/';
6577c478bd9Sstevel@tonic-gate return (NULL);
6587c478bd9Sstevel@tonic-gate }
6597c478bd9Sstevel@tonic-gate }
6607c478bd9Sstevel@tonic-gate if (s3 != NULL)
6617c478bd9Sstevel@tonic-gate *s3 = '/';
6627c478bd9Sstevel@tonic-gate s2 = pw->pw_dir;
6637c478bd9Sstevel@tonic-gate }
6647c478bd9Sstevel@tonic-gate for (s1 = buf; s1 < &buf[len] && (*s1++ = *s2++); )
6657c478bd9Sstevel@tonic-gate ;
6667c478bd9Sstevel@tonic-gate s2 = --s1;
6677c478bd9Sstevel@tonic-gate if (s3 != NULL) {
6687c478bd9Sstevel@tonic-gate s2++;
6697c478bd9Sstevel@tonic-gate while (s1 < &buf[len] && (*s1++ = *s3++))
6707c478bd9Sstevel@tonic-gate ;
6717c478bd9Sstevel@tonic-gate }
6727c478bd9Sstevel@tonic-gate if (s1 == &buf[len]) {
6737c478bd9Sstevel@tonic-gate error("pathname too long: %s\n", file - 1);
6747c478bd9Sstevel@tonic-gate return (NULL);
6757c478bd9Sstevel@tonic-gate }
6767c478bd9Sstevel@tonic-gate return (s2);
6777c478bd9Sstevel@tonic-gate }
678