示例#1
0
				/*
				 * Was: strcpy(gpath, gpath + 1);
				 * but that's WRONG
				 */
				memmove(gpath, gpath+1, strlen(gpath+1)+1);
			} 
			else {
				(void) strcpy(gpath, home);
			}
			gpathp = strend(gpath);
		}
	}
	while (!any(*cs, globchars)) {
		if (*cs == 0) {
			if (!globbed)
				Gcat(gpath, "");
			else if (stat(gpath, &stb) >= 0) {
				Gcat(gpath, "");
				globcnt++;
			}
			goto endit;
		}
		addpath(*cs++);
	}
	oldcs = cs;
	while (cs > as && *cs != '/')
		cs--, gpathp--;
	if (*cs == '/')
		cs++, gpathp++;
	*gpathp = 0;
	if (*oldcs == '{') {
		(void) execbrc(cs, ((char *)0));
		return;
	}
	matchdir(cs);
endit:
	gpathp = sgpathp;
	*gpathp = 0;
}

static 
void
matchdir(const char *pattern)
{
	struct stat stb;
	register struct dirent *dp;
	DIR *dirp;

#if 0
#ifdef	__linux__
	if (gpath == NULL || *gpath == '\0')
		gpath = "./";
#endif
#endif
	dirp = opendir((!gpath || !*gpath) ? "./" : gpath);
	if (dirp == NULL) {
		if (globbed)
			return;
		goto patherr2;
	}
	if (fstat(dirfd(dirp), &stb) < 0)
		goto patherr1;
	if (!isdir(stb)) {
		errno = ENOTDIR;
		goto patherr1;
	}
	while ((dp = readdir(dirp)) != NULL) {
		if (dp->d_ino == 0)
			continue;
		if (match(dp->d_name, pattern)) {
			Gcat(gpath, dp->d_name);
			globcnt++;
		}
	}
	closedir(dirp);
	return;

patherr1:
	closedir(dirp);
patherr2:
	globerr = "Bad directory components";
}

static 
int
execbrc(const char *p, const char *s)
{
	char restbuf[BUFSIZ + 2];
	const char *pe, *pm, *pl;
	int brclev = 0;
	char *lm, *sgpathp;

	for (lm = restbuf; *p != '{'; *lm++ = *p++)
		continue;
	for (pe = ++p; *pe; pe++)
	switch (*pe) {

	case '{':
		brclev++;
		continue;

	case '}':
		if (brclev == 0)
			goto pend;
		brclev--;
		continue;

	case '[':
		for (pe++; *pe && *pe != ']'; pe++)
			continue;
		continue;
	}
pend:
	brclev = 0;
	for (pl = pm = p; pm <= pe; pm++)
	switch (*pm & (QUOTE|TRIM)) {

	case '{':
		brclev++;
		continue;

	case '}':
		if (brclev) {
			brclev--;
			continue;
		}
		goto doit;

	case ','|QUOTE:
	case ',':
		if (brclev)
			continue;
doit:
#if 0
		savec = *pm;
		*pm = 0;
		strcpy(lm, pl);
		*pm = savec;
#else
		strncpy(lm, pl, pm-pl);
		lm[pm-pl] = 0;
#endif
		(void) strcat(restbuf, pe + 1);
		if (s == 0) {
			sgpathp = gpathp;
			expand(restbuf);
			gpathp = sgpathp;
			*gpathp = 0;
		} 
		else if (amatch(s, restbuf)) {
			return (1);
		}
		sort();
		pl = pm + 1;
		if (brclev)
			return (0);
		continue;

	case '[':
		for (pm++; *pm && *pm != ']'; pm++)
			continue;
		if (!*pm)
			pm--;
		continue;
	}
	if (brclev)
		goto doit;
	return (0);
}

static 
int
match(const char *s, const char *p)
{
	int c;
	const char *sentp;
	char sglobbed = globbed;

	if (*s == '.' && *p != '.')
		return (0);
	sentp = entp;
	entp = s;
	c = amatch(s, p);
	entp = sentp;
	globbed = sglobbed;
	return (c);
}

static 
int
amatch(const char *s, const char *p)
{
	register int scc;
	int ok, lc;
	char *sgpathp;
	struct stat stb;
	int c, cc;

	globbed = 1;
	for (;;) {
		scc = *s++ & TRIM;
		switch (c = *p++) {

		case '{':
			return (execbrc(p - 1, s - 1));

		case '[':
			ok = 0;
			lc = 077777;
			while ((cc = *p++) != 0) {
				if (cc == ']') {
					if (ok)
						break;
					return (0);
				}
				if (cc == '-') {
					if (lc <= scc && scc <= *p++)
						ok++;
				} else
					if (scc == (lc = cc))
						ok++;
			}
			if (cc == 0) {
				if (ok)
					p--;
				else
					return 0;
			}
			continue;

		case '*':
			if (!*p)
				return (1);
			if (*p == '/') {
				p++;
				goto slash;
			}
			s--;
			do {
				if (amatch(s, p))
					return (1);
			} while (*s++);
			return (0);

		case 0:
			return (scc == 0);

		default:
			if (c != scc)
				return (0);
			continue;

		case '?':
			if (scc == 0)
				return (0);
			continue;

		case '/':
			if (scc)
				return (0);
slash:
			s = entp;
			sgpathp = gpathp;
			while (*s)
				addpath(*s++);
			addpath('/');
			if (stat(gpath, &stb) == 0 && isdir(stb)) {
				if (*p == 0) {
					Gcat(gpath, "");
					globcnt++;
				} else {
					expand(p);
				}
			}
			gpathp = sgpathp;
			*gpathp = 0;
			return (0);
		}
	}
}

#if 0 /* dead code */
static 
int
Gmatch(const char *s, const char *p)
{
	register int scc;
	int ok, lc;
	int c, cc;

	for (;;) {
		scc = *s++ & TRIM;
		switch (c = *p++) {

		case '[':
			ok = 0;
			lc = 077777;
			while ((cc = *p++) != 0) {
				if (cc == ']') {
					if (ok)
						break;
					return (0);
				}
				if (cc == '-') {
					if (lc <= scc && scc <= *p++)
						ok++;
				} else
					if (scc == (lc = cc))
						ok++;
			}
			if (cc == 0)
				if (ok)
					p--;
				else
					return 0;
			continue;

		case '*':
			if (!*p)
				return (1);
			for (s--; *s; s++)
				if (Gmatch(s, p))
					return (1);
			return (0);

		case 0:
			return (scc == 0);

		default:
			if ((c & TRIM) != scc)
				return (0);
			continue;

		case '?':
			if (scc == 0)
				return (0);
			continue;

		}
	}
}
示例#2
0
static struct varent *
madrof(Char *pat, struct varent *vp)
{
    struct varent *vp1;

    for (vp = vp->v_left; vp; vp = vp->v_right) {
	if (vp->v_left && (vp1 = madrof(pat, vp)) != NULL)
	    return vp1;
	if (Gmatch(vp->v_name, pat))
	    return vp;
    }
    return vp;
}
示例#3
0
/* tw_match():
 *	Match a string against the pattern given.
 *	and return the number of matched characters
 *	in a prefix of the string.
 */
static int
tw_match(const Char *str, const Char *pat, int exact)
{
    const Char *estr;
    int rv = exact ? Gmatch(estr = str, pat) : Gnmatch(str, pat, &estr);
#ifdef TDEBUG
    xprintf("G%smatch(%s, ", exact ? "" : "n", short2str(str));
    xprintf("%s, ", short2str(pat));
    xprintf("%s) = %d [%" TCSH_PTRDIFF_T_FMT "d]\n", short2str(estr), rv,
	estr - str);
#endif /* TDEBUG */
    return (int) (rv ? estr - str : -1);
}
示例#4
0
文件: pin.c 项目: kahrs/cda
int
pinmatch (Macro *m, char *pname, Pin ***ppp)
{
	static Pin	*pp[NPP];
	char		**v;
	int		Npp, i, j, n, npp;

	if (0 >= (n = expand (pname, &v, 0)))
		return (0);
	for (i = Npp = 0; i < n; i++) {
		for (j = npp = 0; j < m->m_npins; j++)
			if (Gmatch (m->m_psort[j]->p_name, v[i]))
				if (Npp >= NPP)
					fatal ("pinmatch: too many pins");
				else	pp[npp++, Npp++] = m->m_psort[j];
		if (!npp)
			error ("%s: pin not matched", v[i]);
	}
	if (Npp)
		*ppp = pp;
	return (Npp);
}