expand.c revision 1.13 1 1.13 christos /* $NetBSD: expand.c,v 1.13 1998/12/19 20:32:17 christos Exp $ */
2 1.7 thorpej
3 1.1 cgd /*
4 1.5 cgd * Copyright (c) 1983, 1993
5 1.5 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.10 lukem #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.7 thorpej #if 0
39 1.7 thorpej static char sccsid[] = "@(#)expand.c 8.1 (Berkeley) 6/9/93";
40 1.7 thorpej #else
41 1.13 christos __RCSID("$NetBSD: expand.c,v 1.13 1998/12/19 20:32:17 christos Exp $");
42 1.7 thorpej #endif
43 1.1 cgd #endif /* not lint */
44 1.9 mrg
45 1.9 mrg #include <sys/types.h>
46 1.9 mrg
47 1.9 mrg #include <errno.h>
48 1.9 mrg #include <pwd.h>
49 1.1 cgd
50 1.1 cgd #include "defs.h"
51 1.1 cgd
52 1.1 cgd #define GAVSIZ NCARGS / 6
53 1.1 cgd #define LC '{'
54 1.1 cgd #define RC '}'
55 1.1 cgd
56 1.1 cgd static char shchars[] = "${[*?";
57 1.1 cgd
58 1.1 cgd int which; /* bit mask of types to expand */
59 1.1 cgd int eargc; /* expanded arg count */
60 1.1 cgd char **eargv; /* expanded arg vectors */
61 1.1 cgd char *path;
62 1.1 cgd char *pathp;
63 1.1 cgd char *lastpathp;
64 1.1 cgd char *tilde; /* "~user" if not expanding tilde, else "" */
65 1.1 cgd char *tpathp;
66 1.1 cgd int nleft;
67 1.1 cgd
68 1.1 cgd int expany; /* any expansions done? */
69 1.1 cgd char *entp;
70 1.1 cgd char **sortbase;
71 1.1 cgd
72 1.1 cgd #define sort() qsort((char *)sortbase, &eargv[eargc] - sortbase, \
73 1.1 cgd sizeof(*sortbase), argcmp), sortbase = &eargv[eargc]
74 1.1 cgd
75 1.5 cgd static void Cat __P((char *, char *));
76 1.5 cgd static void addpath __P((int));
77 1.5 cgd static int amatch __P((char *, char *));
78 1.5 cgd static int argcmp __P((const void *, const void *));
79 1.5 cgd static int execbrc __P((char *, char *));
80 1.5 cgd static void expsh __P((char *));
81 1.5 cgd static void expstr __P((char *));
82 1.5 cgd static int match __P((char *, char *));
83 1.5 cgd static void matchdir __P((char *));
84 1.5 cgd static int smatch __P((char *, char *));
85 1.5 cgd
86 1.1 cgd /*
87 1.1 cgd * Take a list of names and expand any macros, etc.
88 1.1 cgd * wh = E_VARS if expanding variables.
89 1.1 cgd * wh = E_SHELL if expanding shell characters.
90 1.1 cgd * wh = E_TILDE if expanding `~'.
91 1.1 cgd * or any of these or'ed together.
92 1.1 cgd *
93 1.1 cgd * Major portions of this were snarfed from csh/sh.glob.c.
94 1.1 cgd */
95 1.1 cgd struct namelist *
96 1.1 cgd expand(list, wh)
97 1.1 cgd struct namelist *list;
98 1.1 cgd int wh;
99 1.1 cgd {
100 1.10 lukem struct namelist *nl, *prev;
101 1.10 lukem int n;
102 1.1 cgd char pathbuf[BUFSIZ];
103 1.1 cgd char *argvbuf[GAVSIZ];
104 1.1 cgd
105 1.1 cgd if (debug) {
106 1.10 lukem printf("expand(%lx, %d)\nlist = ", (long)list, wh);
107 1.1 cgd prnames(list);
108 1.1 cgd }
109 1.1 cgd
110 1.1 cgd if (wh == 0) {
111 1.10 lukem char *cp;
112 1.1 cgd
113 1.1 cgd for (nl = list; nl != NULL; nl = nl->n_next)
114 1.1 cgd for (cp = nl->n_name; *cp; cp++)
115 1.1 cgd *cp = *cp & TRIM;
116 1.1 cgd return(list);
117 1.1 cgd }
118 1.1 cgd
119 1.1 cgd which = wh;
120 1.1 cgd path = tpathp = pathp = pathbuf;
121 1.1 cgd *pathp = '\0';
122 1.1 cgd lastpathp = &path[sizeof pathbuf - 2];
123 1.1 cgd tilde = "";
124 1.1 cgd eargc = 0;
125 1.1 cgd eargv = sortbase = argvbuf;
126 1.1 cgd *eargv = 0;
127 1.1 cgd nleft = NCARGS - 4;
128 1.1 cgd /*
129 1.1 cgd * Walk the name list and expand names into eargv[];
130 1.1 cgd */
131 1.1 cgd for (nl = list; nl != NULL; nl = nl->n_next)
132 1.1 cgd expstr(nl->n_name);
133 1.1 cgd /*
134 1.1 cgd * Take expanded list of names from eargv[] and build a new list.
135 1.1 cgd */
136 1.1 cgd list = prev = NULL;
137 1.1 cgd for (n = 0; n < eargc; n++) {
138 1.1 cgd nl = makenl(NULL);
139 1.1 cgd nl->n_name = eargv[n];
140 1.1 cgd if (prev == NULL)
141 1.1 cgd list = prev = nl;
142 1.1 cgd else {
143 1.1 cgd prev->n_next = nl;
144 1.1 cgd prev = nl;
145 1.1 cgd }
146 1.1 cgd }
147 1.1 cgd if (debug) {
148 1.1 cgd printf("expanded list = ");
149 1.1 cgd prnames(list);
150 1.1 cgd }
151 1.1 cgd return(list);
152 1.1 cgd }
153 1.1 cgd
154 1.5 cgd static void
155 1.1 cgd expstr(s)
156 1.1 cgd char *s;
157 1.1 cgd {
158 1.10 lukem char *cp, *cp1;
159 1.10 lukem struct namelist *tp;
160 1.1 cgd char *tail;
161 1.1 cgd char buf[BUFSIZ];
162 1.1 cgd int savec, oeargc;
163 1.1 cgd extern char homedir[];
164 1.1 cgd
165 1.1 cgd if (s == NULL || *s == '\0')
166 1.1 cgd return;
167 1.1 cgd
168 1.10 lukem if ((which & E_VARS) && (cp = strchr(s, '$')) != NULL) {
169 1.1 cgd *cp++ = '\0';
170 1.1 cgd if (*cp == '\0') {
171 1.1 cgd yyerror("no variable name after '$'");
172 1.1 cgd return;
173 1.1 cgd }
174 1.1 cgd if (*cp == LC) {
175 1.1 cgd cp++;
176 1.10 lukem if ((tail = strchr(cp, RC)) == NULL) {
177 1.1 cgd yyerror("unmatched '{'");
178 1.1 cgd return;
179 1.1 cgd }
180 1.1 cgd *tail++ = savec = '\0';
181 1.1 cgd if (*cp == '\0') {
182 1.1 cgd yyerror("no variable name after '$'");
183 1.1 cgd return;
184 1.1 cgd }
185 1.1 cgd } else {
186 1.1 cgd tail = cp + 1;
187 1.1 cgd savec = *tail;
188 1.1 cgd *tail = '\0';
189 1.1 cgd }
190 1.8 pk tp = lookup(cp, 0, 0);
191 1.1 cgd if (savec != '\0')
192 1.1 cgd *tail = savec;
193 1.1 cgd if (tp != NULL) {
194 1.1 cgd for (; tp != NULL; tp = tp->n_next) {
195 1.6 thorpej snprintf(buf, sizeof(buf), "%s%s%s", s,
196 1.6 thorpej tp->n_name, tail);
197 1.1 cgd expstr(buf);
198 1.1 cgd }
199 1.1 cgd return;
200 1.1 cgd }
201 1.6 thorpej snprintf(buf, sizeof(buf), "%s%s", s, tail);
202 1.1 cgd expstr(buf);
203 1.1 cgd return;
204 1.1 cgd }
205 1.1 cgd if ((which & ~E_VARS) == 0 || !strcmp(s, "{") || !strcmp(s, "{}")) {
206 1.1 cgd Cat(s, "");
207 1.1 cgd sort();
208 1.1 cgd return;
209 1.1 cgd }
210 1.1 cgd if (*s == '~') {
211 1.1 cgd cp = ++s;
212 1.1 cgd if (*cp == '\0' || *cp == '/') {
213 1.1 cgd tilde = "~";
214 1.1 cgd cp1 = homedir;
215 1.1 cgd } else {
216 1.1 cgd tilde = cp1 = buf;
217 1.1 cgd *cp1++ = '~';
218 1.1 cgd do
219 1.1 cgd *cp1++ = *cp++;
220 1.1 cgd while (*cp && *cp != '/');
221 1.1 cgd *cp1 = '\0';
222 1.1 cgd if (pw == NULL || strcmp(pw->pw_name, buf+1) != 0) {
223 1.1 cgd if ((pw = getpwnam(buf+1)) == NULL) {
224 1.1 cgd strcat(buf, ": unknown user name");
225 1.1 cgd yyerror(buf+1);
226 1.1 cgd return;
227 1.1 cgd }
228 1.1 cgd }
229 1.1 cgd cp1 = pw->pw_dir;
230 1.1 cgd s = cp;
231 1.1 cgd }
232 1.10 lukem for (cp = path; (*cp++ = *cp1++) != 0; )
233 1.1 cgd ;
234 1.1 cgd tpathp = pathp = cp - 1;
235 1.1 cgd } else {
236 1.1 cgd tpathp = pathp = path;
237 1.1 cgd tilde = "";
238 1.1 cgd }
239 1.1 cgd *pathp = '\0';
240 1.1 cgd if (!(which & E_SHELL)) {
241 1.1 cgd if (which & E_TILDE)
242 1.1 cgd Cat(path, s);
243 1.1 cgd else
244 1.1 cgd Cat(tilde, s);
245 1.1 cgd sort();
246 1.1 cgd return;
247 1.1 cgd }
248 1.1 cgd oeargc = eargc;
249 1.1 cgd expany = 0;
250 1.1 cgd expsh(s);
251 1.1 cgd if (eargc == oeargc)
252 1.1 cgd Cat(s, ""); /* "nonomatch" is set */
253 1.1 cgd sort();
254 1.1 cgd }
255 1.1 cgd
256 1.5 cgd static int
257 1.1 cgd argcmp(a1, a2)
258 1.5 cgd const void *a1, *a2;
259 1.1 cgd {
260 1.1 cgd
261 1.5 cgd return (strcmp(*(char **)a1, *(char **)a2));
262 1.1 cgd }
263 1.1 cgd
264 1.1 cgd /*
265 1.1 cgd * If there are any Shell meta characters in the name,
266 1.1 cgd * expand into a list, after searching directory
267 1.1 cgd */
268 1.5 cgd static void
269 1.1 cgd expsh(s)
270 1.1 cgd char *s;
271 1.1 cgd {
272 1.10 lukem char *cp;
273 1.10 lukem char *spathp, *oldcp;
274 1.1 cgd struct stat stb;
275 1.1 cgd
276 1.1 cgd spathp = pathp;
277 1.1 cgd cp = s;
278 1.1 cgd while (!any(*cp, shchars)) {
279 1.1 cgd if (*cp == '\0') {
280 1.1 cgd if (!expany || stat(path, &stb) >= 0) {
281 1.1 cgd if (which & E_TILDE)
282 1.1 cgd Cat(path, "");
283 1.1 cgd else
284 1.1 cgd Cat(tilde, tpathp);
285 1.1 cgd }
286 1.1 cgd goto endit;
287 1.1 cgd }
288 1.1 cgd addpath(*cp++);
289 1.1 cgd }
290 1.1 cgd oldcp = cp;
291 1.1 cgd while (cp > s && *cp != '/')
292 1.1 cgd cp--, pathp--;
293 1.1 cgd if (*cp == '/')
294 1.1 cgd cp++, pathp++;
295 1.1 cgd *pathp = '\0';
296 1.1 cgd if (*oldcp == '{') {
297 1.1 cgd execbrc(cp, NULL);
298 1.1 cgd return;
299 1.1 cgd }
300 1.1 cgd matchdir(cp);
301 1.1 cgd endit:
302 1.1 cgd pathp = spathp;
303 1.1 cgd *pathp = '\0';
304 1.1 cgd }
305 1.1 cgd
306 1.5 cgd static void
307 1.1 cgd matchdir(pattern)
308 1.1 cgd char *pattern;
309 1.1 cgd {
310 1.1 cgd struct stat stb;
311 1.13 christos struct dirent *dp;
312 1.1 cgd DIR *dirp;
313 1.1 cgd
314 1.1 cgd dirp = opendir(path);
315 1.1 cgd if (dirp == NULL) {
316 1.1 cgd if (expany)
317 1.1 cgd return;
318 1.1 cgd goto patherr2;
319 1.1 cgd }
320 1.1 cgd if (fstat(dirp->dd_fd, &stb) < 0)
321 1.1 cgd goto patherr1;
322 1.11 mycroft if (!S_ISDIR(stb.st_mode)) {
323 1.1 cgd errno = ENOTDIR;
324 1.1 cgd goto patherr1;
325 1.1 cgd }
326 1.1 cgd while ((dp = readdir(dirp)) != NULL)
327 1.1 cgd if (match(dp->d_name, pattern)) {
328 1.1 cgd if (which & E_TILDE)
329 1.1 cgd Cat(path, dp->d_name);
330 1.1 cgd else {
331 1.1 cgd strcpy(pathp, dp->d_name);
332 1.1 cgd Cat(tilde, tpathp);
333 1.1 cgd *pathp = '\0';
334 1.1 cgd }
335 1.1 cgd }
336 1.1 cgd closedir(dirp);
337 1.1 cgd return;
338 1.1 cgd
339 1.1 cgd patherr1:
340 1.1 cgd closedir(dirp);
341 1.1 cgd patherr2:
342 1.1 cgd strcat(path, ": ");
343 1.1 cgd strcat(path, strerror(errno));
344 1.1 cgd yyerror(path);
345 1.1 cgd }
346 1.1 cgd
347 1.5 cgd static int
348 1.1 cgd execbrc(p, s)
349 1.1 cgd char *p, *s;
350 1.1 cgd {
351 1.1 cgd char restbuf[BUFSIZ + 2];
352 1.10 lukem char *pe, *pm, *pl;
353 1.1 cgd int brclev = 0;
354 1.1 cgd char *lm, savec, *spathp;
355 1.1 cgd
356 1.1 cgd for (lm = restbuf; *p != '{'; *lm++ = *p++)
357 1.1 cgd continue;
358 1.1 cgd for (pe = ++p; *pe; pe++)
359 1.1 cgd switch (*pe) {
360 1.1 cgd
361 1.1 cgd case '{':
362 1.1 cgd brclev++;
363 1.1 cgd continue;
364 1.1 cgd
365 1.1 cgd case '}':
366 1.1 cgd if (brclev == 0)
367 1.1 cgd goto pend;
368 1.1 cgd brclev--;
369 1.1 cgd continue;
370 1.1 cgd
371 1.1 cgd case '[':
372 1.1 cgd for (pe++; *pe && *pe != ']'; pe++)
373 1.1 cgd continue;
374 1.1 cgd if (!*pe)
375 1.1 cgd yyerror("Missing ']'");
376 1.1 cgd continue;
377 1.1 cgd }
378 1.1 cgd pend:
379 1.1 cgd if (brclev || !*pe) {
380 1.1 cgd yyerror("Missing '}'");
381 1.1 cgd return (0);
382 1.1 cgd }
383 1.1 cgd for (pl = pm = p; pm <= pe; pm++)
384 1.1 cgd switch (*pm & (QUOTE|TRIM)) {
385 1.1 cgd
386 1.1 cgd case '{':
387 1.1 cgd brclev++;
388 1.1 cgd continue;
389 1.1 cgd
390 1.1 cgd case '}':
391 1.1 cgd if (brclev) {
392 1.1 cgd brclev--;
393 1.1 cgd continue;
394 1.1 cgd }
395 1.1 cgd goto doit;
396 1.1 cgd
397 1.1 cgd case ',':
398 1.1 cgd if (brclev)
399 1.1 cgd continue;
400 1.1 cgd doit:
401 1.1 cgd savec = *pm;
402 1.1 cgd *pm = 0;
403 1.1 cgd strcpy(lm, pl);
404 1.1 cgd strcat(restbuf, pe + 1);
405 1.1 cgd *pm = savec;
406 1.1 cgd if (s == 0) {
407 1.1 cgd spathp = pathp;
408 1.1 cgd expsh(restbuf);
409 1.1 cgd pathp = spathp;
410 1.1 cgd *pathp = 0;
411 1.1 cgd } else if (amatch(s, restbuf))
412 1.1 cgd return (1);
413 1.1 cgd sort();
414 1.1 cgd pl = pm + 1;
415 1.1 cgd continue;
416 1.1 cgd
417 1.1 cgd case '[':
418 1.1 cgd for (pm++; *pm && *pm != ']'; pm++)
419 1.1 cgd continue;
420 1.1 cgd if (!*pm)
421 1.1 cgd yyerror("Missing ']'");
422 1.1 cgd continue;
423 1.1 cgd }
424 1.1 cgd return (0);
425 1.1 cgd }
426 1.1 cgd
427 1.5 cgd static int
428 1.1 cgd match(s, p)
429 1.1 cgd char *s, *p;
430 1.1 cgd {
431 1.10 lukem int c;
432 1.10 lukem char *sentp;
433 1.1 cgd char sexpany = expany;
434 1.1 cgd
435 1.1 cgd if (*s == '.' && *p != '.')
436 1.1 cgd return (0);
437 1.1 cgd sentp = entp;
438 1.1 cgd entp = s;
439 1.1 cgd c = amatch(s, p);
440 1.1 cgd entp = sentp;
441 1.1 cgd expany = sexpany;
442 1.1 cgd return (c);
443 1.1 cgd }
444 1.1 cgd
445 1.5 cgd static int
446 1.1 cgd amatch(s, p)
447 1.10 lukem char *s, *p;
448 1.1 cgd {
449 1.10 lukem int scc;
450 1.1 cgd int ok, lc;
451 1.1 cgd char *spathp;
452 1.1 cgd struct stat stb;
453 1.1 cgd int c, cc;
454 1.1 cgd
455 1.1 cgd expany = 1;
456 1.1 cgd for (;;) {
457 1.1 cgd scc = *s++ & TRIM;
458 1.1 cgd switch (c = *p++) {
459 1.1 cgd
460 1.1 cgd case '{':
461 1.1 cgd return (execbrc(p - 1, s - 1));
462 1.1 cgd
463 1.1 cgd case '[':
464 1.1 cgd ok = 0;
465 1.1 cgd lc = 077777;
466 1.10 lukem while ((cc = *p++) != 0) {
467 1.1 cgd if (cc == ']') {
468 1.1 cgd if (ok)
469 1.1 cgd break;
470 1.1 cgd return (0);
471 1.1 cgd }
472 1.1 cgd if (cc == '-') {
473 1.1 cgd if (lc <= scc && scc <= *p++)
474 1.1 cgd ok++;
475 1.1 cgd } else
476 1.1 cgd if (scc == (lc = cc))
477 1.1 cgd ok++;
478 1.1 cgd }
479 1.1 cgd if (cc == 0) {
480 1.1 cgd yyerror("Missing ']'");
481 1.1 cgd return (0);
482 1.1 cgd }
483 1.1 cgd continue;
484 1.1 cgd
485 1.1 cgd case '*':
486 1.1 cgd if (!*p)
487 1.1 cgd return (1);
488 1.1 cgd if (*p == '/') {
489 1.1 cgd p++;
490 1.1 cgd goto slash;
491 1.1 cgd }
492 1.1 cgd for (s--; *s; s++)
493 1.1 cgd if (amatch(s, p))
494 1.1 cgd return (1);
495 1.1 cgd return (0);
496 1.1 cgd
497 1.1 cgd case '\0':
498 1.1 cgd return (scc == '\0');
499 1.1 cgd
500 1.1 cgd default:
501 1.1 cgd if ((c & TRIM) != scc)
502 1.1 cgd return (0);
503 1.1 cgd continue;
504 1.1 cgd
505 1.1 cgd case '?':
506 1.1 cgd if (scc == '\0')
507 1.1 cgd return (0);
508 1.1 cgd continue;
509 1.1 cgd
510 1.1 cgd case '/':
511 1.1 cgd if (scc)
512 1.1 cgd return (0);
513 1.1 cgd slash:
514 1.1 cgd s = entp;
515 1.1 cgd spathp = pathp;
516 1.1 cgd while (*s)
517 1.1 cgd addpath(*s++);
518 1.1 cgd addpath('/');
519 1.12 ross if (stat(path, &stb) == 0 && S_ISDIR(stb.st_mode)) {
520 1.1 cgd if (*p == '\0') {
521 1.1 cgd if (which & E_TILDE)
522 1.1 cgd Cat(path, "");
523 1.1 cgd else
524 1.1 cgd Cat(tilde, tpathp);
525 1.1 cgd } else
526 1.1 cgd expsh(p);
527 1.12 ross }
528 1.1 cgd pathp = spathp;
529 1.1 cgd *pathp = '\0';
530 1.1 cgd return (0);
531 1.1 cgd }
532 1.1 cgd }
533 1.1 cgd }
534 1.1 cgd
535 1.5 cgd static int
536 1.1 cgd smatch(s, p)
537 1.10 lukem char *s, *p;
538 1.1 cgd {
539 1.10 lukem int scc;
540 1.1 cgd int ok, lc;
541 1.1 cgd int c, cc;
542 1.1 cgd
543 1.1 cgd for (;;) {
544 1.1 cgd scc = *s++ & TRIM;
545 1.1 cgd switch (c = *p++) {
546 1.1 cgd
547 1.1 cgd case '[':
548 1.1 cgd ok = 0;
549 1.1 cgd lc = 077777;
550 1.10 lukem while ((cc = *p++) != 0) {
551 1.1 cgd if (cc == ']') {
552 1.1 cgd if (ok)
553 1.1 cgd break;
554 1.1 cgd return (0);
555 1.1 cgd }
556 1.1 cgd if (cc == '-') {
557 1.1 cgd if (lc <= scc && scc <= *p++)
558 1.1 cgd ok++;
559 1.1 cgd } else
560 1.1 cgd if (scc == (lc = cc))
561 1.1 cgd ok++;
562 1.1 cgd }
563 1.1 cgd if (cc == 0) {
564 1.1 cgd yyerror("Missing ']'");
565 1.1 cgd return (0);
566 1.1 cgd }
567 1.1 cgd continue;
568 1.1 cgd
569 1.1 cgd case '*':
570 1.1 cgd if (!*p)
571 1.1 cgd return (1);
572 1.1 cgd for (s--; *s; s++)
573 1.1 cgd if (smatch(s, p))
574 1.1 cgd return (1);
575 1.1 cgd return (0);
576 1.1 cgd
577 1.1 cgd case '\0':
578 1.1 cgd return (scc == '\0');
579 1.1 cgd
580 1.1 cgd default:
581 1.1 cgd if ((c & TRIM) != scc)
582 1.1 cgd return (0);
583 1.1 cgd continue;
584 1.1 cgd
585 1.1 cgd case '?':
586 1.1 cgd if (scc == 0)
587 1.1 cgd return (0);
588 1.1 cgd continue;
589 1.1 cgd
590 1.1 cgd }
591 1.1 cgd }
592 1.1 cgd }
593 1.1 cgd
594 1.5 cgd static void
595 1.1 cgd Cat(s1, s2)
596 1.10 lukem char *s1, *s2;
597 1.1 cgd {
598 1.1 cgd int len = strlen(s1) + strlen(s2) + 1;
599 1.10 lukem char *s;
600 1.1 cgd
601 1.1 cgd nleft -= len;
602 1.1 cgd if (nleft <= 0 || ++eargc >= GAVSIZ)
603 1.1 cgd yyerror("Arguments too long");
604 1.1 cgd eargv[eargc] = 0;
605 1.1 cgd eargv[eargc - 1] = s = malloc(len);
606 1.1 cgd if (s == NULL)
607 1.1 cgd fatal("ran out of memory\n");
608 1.10 lukem while ((*s++ = *s1++ & TRIM) != 0)
609 1.1 cgd ;
610 1.1 cgd s--;
611 1.10 lukem while ((*s++ = *s2++ & TRIM) != 0)
612 1.1 cgd ;
613 1.1 cgd }
614 1.1 cgd
615 1.5 cgd static void
616 1.1 cgd addpath(c)
617 1.5 cgd int c;
618 1.1 cgd {
619 1.1 cgd
620 1.1 cgd if (pathp >= lastpathp)
621 1.1 cgd yyerror("Pathname too long");
622 1.1 cgd else {
623 1.1 cgd *pathp++ = c & TRIM;
624 1.1 cgd *pathp = '\0';
625 1.1 cgd }
626 1.1 cgd }
627 1.1 cgd
628 1.1 cgd /*
629 1.1 cgd * Expand file names beginning with `~' into the
630 1.1 cgd * user's home directory path name. Return a pointer in buf to the
631 1.1 cgd * part corresponding to `file'.
632 1.1 cgd */
633 1.1 cgd char *
634 1.1 cgd exptilde(buf, file)
635 1.1 cgd char buf[];
636 1.10 lukem char *file;
637 1.1 cgd {
638 1.10 lukem char *s1, *s2, *s3;
639 1.1 cgd extern char homedir[];
640 1.1 cgd
641 1.1 cgd if (*file != '~') {
642 1.1 cgd strcpy(buf, file);
643 1.1 cgd return(buf);
644 1.1 cgd }
645 1.1 cgd if (*++file == '\0') {
646 1.1 cgd s2 = homedir;
647 1.1 cgd s3 = NULL;
648 1.1 cgd } else if (*file == '/') {
649 1.1 cgd s2 = homedir;
650 1.1 cgd s3 = file;
651 1.1 cgd } else {
652 1.1 cgd s3 = file;
653 1.1 cgd while (*s3 && *s3 != '/')
654 1.1 cgd s3++;
655 1.1 cgd if (*s3 == '/')
656 1.1 cgd *s3 = '\0';
657 1.1 cgd else
658 1.1 cgd s3 = NULL;
659 1.1 cgd if (pw == NULL || strcmp(pw->pw_name, file) != 0) {
660 1.1 cgd if ((pw = getpwnam(file)) == NULL) {
661 1.1 cgd error("%s: unknown user name\n", file);
662 1.1 cgd if (s3 != NULL)
663 1.1 cgd *s3 = '/';
664 1.1 cgd return(NULL);
665 1.1 cgd }
666 1.1 cgd }
667 1.1 cgd if (s3 != NULL)
668 1.1 cgd *s3 = '/';
669 1.1 cgd s2 = pw->pw_dir;
670 1.1 cgd }
671 1.10 lukem for (s1 = buf; (*s1++ = *s2++) != 0; )
672 1.1 cgd ;
673 1.1 cgd s2 = --s1;
674 1.1 cgd if (s3 != NULL) {
675 1.1 cgd s2++;
676 1.10 lukem while ((*s1++ = *s3++) != 0)
677 1.1 cgd ;
678 1.1 cgd }
679 1.1 cgd return(s2);
680 1.1 cgd }
681