1 | /* $NetBSD: expand.c,v 1.71 2005/06/01 15:41:19 lukem Exp $ */
|
---|
2 |
|
---|
3 | /*-
|
---|
4 | * Copyright (c) 1991, 1993
|
---|
5 | * The Regents of the University of California. All rights reserved.
|
---|
6 | *
|
---|
7 | * This code is derived from software contributed to Berkeley by
|
---|
8 | * Kenneth Almquist.
|
---|
9 | *
|
---|
10 | * Redistribution and use in source and binary forms, with or without
|
---|
11 | * modification, are permitted provided that the following conditions
|
---|
12 | * are met:
|
---|
13 | * 1. Redistributions of source code must retain the above copyright
|
---|
14 | * notice, this list of conditions and the following disclaimer.
|
---|
15 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
16 | * notice, this list of conditions and the following disclaimer in the
|
---|
17 | * documentation and/or other materials provided with the distribution.
|
---|
18 | * 3. Neither the name of the University nor the names of its contributors
|
---|
19 | * may be used to endorse or promote products derived from this software
|
---|
20 | * without specific prior written permission.
|
---|
21 | *
|
---|
22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
---|
23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
---|
26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
32 | * SUCH DAMAGE.
|
---|
33 | */
|
---|
34 |
|
---|
35 | #ifdef HAVE_SYS_CDEFS_H
|
---|
36 | #include <sys/cdefs.h>
|
---|
37 | #endif
|
---|
38 | #ifndef lint
|
---|
39 | #if 0
|
---|
40 | static char sccsid[] = "@(#)expand.c 8.5 (Berkeley) 5/15/95";
|
---|
41 | #else
|
---|
42 | __RCSID("$NetBSD: expand.c,v 1.71 2005/06/01 15:41:19 lukem Exp $");
|
---|
43 | #endif
|
---|
44 | #endif /* not lint */
|
---|
45 |
|
---|
46 | #include <sys/types.h>
|
---|
47 | #include <sys/time.h>
|
---|
48 | #include <sys/stat.h>
|
---|
49 | #include <errno.h>
|
---|
50 | #include <dirent.h>
|
---|
51 | #include <unistd.h>
|
---|
52 | #include <pwd.h>
|
---|
53 | #include <stdlib.h>
|
---|
54 | #include <stdio.h>
|
---|
55 | #ifdef __sun__
|
---|
56 | #include <iso/limits_iso.h>
|
---|
57 | #endif
|
---|
58 |
|
---|
59 | /*
|
---|
60 | * Routines to expand arguments to commands. We have to deal with
|
---|
61 | * backquotes, shell variables, and file metacharacters.
|
---|
62 | */
|
---|
63 |
|
---|
64 | #include "shell.h"
|
---|
65 | #include "main.h"
|
---|
66 | #include "nodes.h"
|
---|
67 | #include "eval.h"
|
---|
68 | #include "expand.h"
|
---|
69 | #include "syntax.h"
|
---|
70 | #include "parser.h"
|
---|
71 | #include "jobs.h"
|
---|
72 | #include "options.h"
|
---|
73 | #include "var.h"
|
---|
74 | #include "input.h"
|
---|
75 | #include "output.h"
|
---|
76 | #include "memalloc.h"
|
---|
77 | #include "error.h"
|
---|
78 | #include "mystring.h"
|
---|
79 | #include "show.h"
|
---|
80 |
|
---|
81 | /*
|
---|
82 | * Structure specifying which parts of the string should be searched
|
---|
83 | * for IFS characters.
|
---|
84 | */
|
---|
85 |
|
---|
86 | struct ifsregion {
|
---|
87 | struct ifsregion *next; /* next region in list */
|
---|
88 | int begoff; /* offset of start of region */
|
---|
89 | int endoff; /* offset of end of region */
|
---|
90 | int inquotes; /* search for nul bytes only */
|
---|
91 | };
|
---|
92 |
|
---|
93 |
|
---|
94 | char *expdest; /* output of current string */
|
---|
95 | struct nodelist *argbackq; /* list of back quote expressions */
|
---|
96 | struct ifsregion ifsfirst; /* first struct in list of ifs regions */
|
---|
97 | struct ifsregion *ifslastp; /* last struct in list */
|
---|
98 | struct arglist exparg; /* holds expanded arg list */
|
---|
99 |
|
---|
100 | STATIC void argstr(char *, int);
|
---|
101 | STATIC char *exptilde(char *, int);
|
---|
102 | STATIC void expbackq(union node *, int, int);
|
---|
103 | STATIC int subevalvar(char *, char *, int, int, int, int);
|
---|
104 | STATIC char *evalvar(char *, int);
|
---|
105 | STATIC int varisset(char *, int);
|
---|
106 | STATIC void varvalue(char *, int, int, int);
|
---|
107 | STATIC void recordregion(int, int, int);
|
---|
108 | STATIC void removerecordregions(int);
|
---|
109 | STATIC void ifsbreakup(char *, struct arglist *);
|
---|
110 | STATIC void ifsfree(void);
|
---|
111 | STATIC void expandmeta(struct strlist *, int);
|
---|
112 | STATIC void expmeta(char *, char *);
|
---|
113 | STATIC void addfname(char *);
|
---|
114 | STATIC struct strlist *expsort(struct strlist *);
|
---|
115 | STATIC struct strlist *msort(struct strlist *, int);
|
---|
116 | STATIC int pmatch(char *, char *, int);
|
---|
117 | STATIC char *cvtnum(int, char *);
|
---|
118 |
|
---|
119 | /*
|
---|
120 | * Expand shell variables and backquotes inside a here document.
|
---|
121 | */
|
---|
122 |
|
---|
123 | void
|
---|
124 | expandhere(union node *arg, int fd)
|
---|
125 | {
|
---|
126 | herefd = fd;
|
---|
127 | expandarg(arg, (struct arglist *)NULL, 0);
|
---|
128 | xwrite(fd, stackblock(), expdest - stackblock());
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | /*
|
---|
133 | * Perform variable substitution and command substitution on an argument,
|
---|
134 | * placing the resulting list of arguments in arglist. If EXP_FULL is true,
|
---|
135 | * perform splitting and file name expansion. When arglist is NULL, perform
|
---|
136 | * here document expansion.
|
---|
137 | */
|
---|
138 |
|
---|
139 | void
|
---|
140 | expandarg(union node *arg, struct arglist *arglist, int flag)
|
---|
141 | {
|
---|
142 | struct strlist *sp;
|
---|
143 | char *p;
|
---|
144 |
|
---|
145 | argbackq = arg->narg.backquote;
|
---|
146 | STARTSTACKSTR(expdest);
|
---|
147 | ifsfirst.next = NULL;
|
---|
148 | ifslastp = NULL;
|
---|
149 | argstr(arg->narg.text, flag);
|
---|
150 | if (arglist == NULL) {
|
---|
151 | return; /* here document expanded */
|
---|
152 | }
|
---|
153 | STPUTC('\0', expdest);
|
---|
154 | p = grabstackstr(expdest);
|
---|
155 | exparg.lastp = &exparg.list;
|
---|
156 | /*
|
---|
157 | * TODO - EXP_REDIR
|
---|
158 | */
|
---|
159 | if (flag & EXP_FULL) {
|
---|
160 | ifsbreakup(p, &exparg);
|
---|
161 | *exparg.lastp = NULL;
|
---|
162 | exparg.lastp = &exparg.list;
|
---|
163 | expandmeta(exparg.list, flag);
|
---|
164 | } else {
|
---|
165 | if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */
|
---|
166 | rmescapes(p);
|
---|
167 | sp = (struct strlist *)stalloc(sizeof (struct strlist));
|
---|
168 | sp->text = p;
|
---|
169 | *exparg.lastp = sp;
|
---|
170 | exparg.lastp = &sp->next;
|
---|
171 | }
|
---|
172 | ifsfree();
|
---|
173 | *exparg.lastp = NULL;
|
---|
174 | if (exparg.list) {
|
---|
175 | *arglist->lastp = exparg.list;
|
---|
176 | arglist->lastp = exparg.lastp;
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 |
|
---|
181 |
|
---|
182 | /*
|
---|
183 | * Perform variable and command substitution.
|
---|
184 | * If EXP_FULL is set, output CTLESC characters to allow for further processing.
|
---|
185 | * Otherwise treat $@ like $* since no splitting will be performed.
|
---|
186 | */
|
---|
187 |
|
---|
188 | STATIC void
|
---|
189 | argstr(char *p, int flag)
|
---|
190 | {
|
---|
191 | char c;
|
---|
192 | int quotes = flag & (EXP_FULL | EXP_CASE); /* do CTLESC */
|
---|
193 | int firsteq = 1;
|
---|
194 | const char *ifs = NULL;
|
---|
195 | int ifs_split = EXP_IFS_SPLIT;
|
---|
196 |
|
---|
197 | if (flag & EXP_IFS_SPLIT)
|
---|
198 | ifs = ifsset() ? ifsval() : " \t\n";
|
---|
199 |
|
---|
200 | if (*p == '~' && (flag & (EXP_TILDE | EXP_VARTILDE)))
|
---|
201 | p = exptilde(p, flag);
|
---|
202 | for (;;) {
|
---|
203 | switch (c = *p++) {
|
---|
204 | case '\0':
|
---|
205 | case CTLENDVAR: /* end of expanding yyy in ${xxx-yyy} */
|
---|
206 | return;
|
---|
207 | case CTLQUOTEMARK:
|
---|
208 | /* "$@" syntax adherence hack */
|
---|
209 | if (p[0] == CTLVAR && p[2] == '@' && p[3] == '=')
|
---|
210 | break;
|
---|
211 | if ((flag & EXP_FULL) != 0)
|
---|
212 | STPUTC(c, expdest);
|
---|
213 | ifs_split = 0;
|
---|
214 | break;
|
---|
215 | case CTLQUOTEEND:
|
---|
216 | ifs_split = EXP_IFS_SPLIT;
|
---|
217 | break;
|
---|
218 | case CTLESC:
|
---|
219 | if (quotes)
|
---|
220 | STPUTC(c, expdest);
|
---|
221 | c = *p++;
|
---|
222 | STPUTC(c, expdest);
|
---|
223 | break;
|
---|
224 | case CTLVAR:
|
---|
225 | p = evalvar(p, (flag & ~EXP_IFS_SPLIT) | (flag & ifs_split));
|
---|
226 | break;
|
---|
227 | case CTLBACKQ:
|
---|
228 | case CTLBACKQ|CTLQUOTE:
|
---|
229 | expbackq(argbackq->n, c & CTLQUOTE, flag);
|
---|
230 | argbackq = argbackq->next;
|
---|
231 | break;
|
---|
232 | case CTLENDARI:
|
---|
233 | expari(flag);
|
---|
234 | break;
|
---|
235 | case ':':
|
---|
236 | case '=':
|
---|
237 | /*
|
---|
238 | * sort of a hack - expand tildes in variable
|
---|
239 | * assignments (after the first '=' and after ':'s).
|
---|
240 | */
|
---|
241 | STPUTC(c, expdest);
|
---|
242 | if (flag & EXP_VARTILDE && *p == '~') {
|
---|
243 | if (c == '=') {
|
---|
244 | if (firsteq)
|
---|
245 | firsteq = 0;
|
---|
246 | else
|
---|
247 | break;
|
---|
248 | }
|
---|
249 | p = exptilde(p, flag);
|
---|
250 | }
|
---|
251 | break;
|
---|
252 | default:
|
---|
253 | STPUTC(c, expdest);
|
---|
254 | if (flag & EXP_IFS_SPLIT & ifs_split && strchr(ifs, c) != NULL) {
|
---|
255 | /* We need to get the output split here... */
|
---|
256 | recordregion(expdest - stackblock() - 1,
|
---|
257 | expdest - stackblock(), 0);
|
---|
258 | }
|
---|
259 | break;
|
---|
260 | }
|
---|
261 | }
|
---|
262 | }
|
---|
263 |
|
---|
264 | STATIC char *
|
---|
265 | exptilde(char *p, int flag)
|
---|
266 | {
|
---|
267 | char c, *startp = p;
|
---|
268 | struct passwd *pw;
|
---|
269 | const char *home;
|
---|
270 | int quotes = flag & (EXP_FULL | EXP_CASE);
|
---|
271 |
|
---|
272 | while ((c = *p) != '\0') {
|
---|
273 | switch(c) {
|
---|
274 | case CTLESC:
|
---|
275 | return (startp);
|
---|
276 | case CTLQUOTEMARK:
|
---|
277 | return (startp);
|
---|
278 | case ':':
|
---|
279 | if (flag & EXP_VARTILDE)
|
---|
280 | goto done;
|
---|
281 | break;
|
---|
282 | case '/':
|
---|
283 | goto done;
|
---|
284 | }
|
---|
285 | p++;
|
---|
286 | }
|
---|
287 | done:
|
---|
288 | *p = '\0';
|
---|
289 | if (*(startp+1) == '\0') {
|
---|
290 | if ((home = lookupvar("HOME")) == NULL)
|
---|
291 | goto lose;
|
---|
292 | } else {
|
---|
293 | if ((pw = getpwnam(startp+1)) == NULL)
|
---|
294 | goto lose;
|
---|
295 | home = pw->pw_dir;
|
---|
296 | }
|
---|
297 | if (*home == '\0')
|
---|
298 | goto lose;
|
---|
299 | *p = c;
|
---|
300 | while ((c = *home++) != '\0') {
|
---|
301 | if (quotes && SQSYNTAX[(int)c] == CCTL)
|
---|
302 | STPUTC(CTLESC, expdest);
|
---|
303 | STPUTC(c, expdest);
|
---|
304 | }
|
---|
305 | return (p);
|
---|
306 | lose:
|
---|
307 | *p = c;
|
---|
308 | return (startp);
|
---|
309 | }
|
---|
310 |
|
---|
311 |
|
---|
312 | STATIC void
|
---|
313 | removerecordregions(int endoff)
|
---|
314 | {
|
---|
315 | if (ifslastp == NULL)
|
---|
316 | return;
|
---|
317 |
|
---|
318 | if (ifsfirst.endoff > endoff) {
|
---|
319 | while (ifsfirst.next != NULL) {
|
---|
320 | struct ifsregion *ifsp;
|
---|
321 | INTOFF;
|
---|
322 | ifsp = ifsfirst.next->next;
|
---|
323 | ckfree(ifsfirst.next);
|
---|
324 | ifsfirst.next = ifsp;
|
---|
325 | INTON;
|
---|
326 | }
|
---|
327 | if (ifsfirst.begoff > endoff)
|
---|
328 | ifslastp = NULL;
|
---|
329 | else {
|
---|
330 | ifslastp = &ifsfirst;
|
---|
331 | ifsfirst.endoff = endoff;
|
---|
332 | }
|
---|
333 | return;
|
---|
334 | }
|
---|
335 |
|
---|
336 | ifslastp = &ifsfirst;
|
---|
337 | while (ifslastp->next && ifslastp->next->begoff < endoff)
|
---|
338 | ifslastp=ifslastp->next;
|
---|
339 | while (ifslastp->next != NULL) {
|
---|
340 | struct ifsregion *ifsp;
|
---|
341 | INTOFF;
|
---|
342 | ifsp = ifslastp->next->next;
|
---|
343 | ckfree(ifslastp->next);
|
---|
344 | ifslastp->next = ifsp;
|
---|
345 | INTON;
|
---|
346 | }
|
---|
347 | if (ifslastp->endoff > endoff)
|
---|
348 | ifslastp->endoff = endoff;
|
---|
349 | }
|
---|
350 |
|
---|
351 |
|
---|
352 | /*
|
---|
353 | * Expand arithmetic expression. Backup to start of expression,
|
---|
354 | * evaluate, place result in (backed up) result, adjust string position.
|
---|
355 | */
|
---|
356 | void
|
---|
357 | expari(int flag)
|
---|
358 | {
|
---|
359 | char *p, *start;
|
---|
360 | int result;
|
---|
361 | int begoff;
|
---|
362 | int quotes = flag & (EXP_FULL | EXP_CASE);
|
---|
363 | int quoted;
|
---|
364 |
|
---|
365 | /* ifsfree(); */
|
---|
366 |
|
---|
367 | /*
|
---|
368 | * This routine is slightly over-complicated for
|
---|
369 | * efficiency. First we make sure there is
|
---|
370 | * enough space for the result, which may be bigger
|
---|
371 | * than the expression if we add exponentation. Next we
|
---|
372 | * scan backwards looking for the start of arithmetic. If the
|
---|
373 | * next previous character is a CTLESC character, then we
|
---|
374 | * have to rescan starting from the beginning since CTLESC
|
---|
375 | * characters have to be processed left to right.
|
---|
376 | */
|
---|
377 | #if INT_MAX / 1000000000 >= 10 || INT_MIN / 1000000000 <= -10
|
---|
378 | #error "integers with more than 10 digits are not supported"
|
---|
379 | #endif
|
---|
380 | CHECKSTRSPACE(12 - 2, expdest);
|
---|
381 | USTPUTC('\0', expdest);
|
---|
382 | start = stackblock();
|
---|
383 | p = expdest - 1;
|
---|
384 | while (*p != CTLARI && p >= start)
|
---|
385 | --p;
|
---|
386 | if (*p != CTLARI)
|
---|
387 | error("missing CTLARI (shouldn't happen)");
|
---|
388 | if (p > start && *(p-1) == CTLESC)
|
---|
389 | for (p = start; *p != CTLARI; p++)
|
---|
390 | if (*p == CTLESC)
|
---|
391 | p++;
|
---|
392 |
|
---|
393 | if (p[1] == '"')
|
---|
394 | quoted=1;
|
---|
395 | else
|
---|
396 | quoted=0;
|
---|
397 | begoff = p - start;
|
---|
398 | removerecordregions(begoff);
|
---|
399 | if (quotes)
|
---|
400 | rmescapes(p+2);
|
---|
401 | result = arith(p+2);
|
---|
402 | fmtstr(p, 12, "%d", result);
|
---|
403 |
|
---|
404 | while (*p++)
|
---|
405 | ;
|
---|
406 |
|
---|
407 | if (quoted == 0)
|
---|
408 | recordregion(begoff, p - 1 - start, 0);
|
---|
409 | result = expdest - p + 1;
|
---|
410 | STADJUST(-result, expdest);
|
---|
411 | }
|
---|
412 |
|
---|
413 |
|
---|
414 | /*
|
---|
415 | * Expand stuff in backwards quotes.
|
---|
416 | */
|
---|
417 |
|
---|
418 | STATIC void
|
---|
419 | expbackq(union node *cmd, int quoted, int flag)
|
---|
420 | {
|
---|
421 | struct backcmd in;
|
---|
422 | int i;
|
---|
423 | char buf[128];
|
---|
424 | char *p;
|
---|
425 | char *dest = expdest;
|
---|
426 | struct ifsregion saveifs, *savelastp;
|
---|
427 | struct nodelist *saveargbackq;
|
---|
428 | char lastc;
|
---|
429 | int startloc = dest - stackblock();
|
---|
430 | char const *syntax = quoted? DQSYNTAX : BASESYNTAX;
|
---|
431 | int saveherefd;
|
---|
432 | int quotes = flag & (EXP_FULL | EXP_CASE);
|
---|
433 |
|
---|
434 | INTOFF;
|
---|
435 | saveifs = ifsfirst;
|
---|
436 | savelastp = ifslastp;
|
---|
437 | saveargbackq = argbackq;
|
---|
438 | saveherefd = herefd;
|
---|
439 | herefd = -1;
|
---|
440 | p = grabstackstr(dest);
|
---|
441 | evalbackcmd(cmd, &in);
|
---|
442 | ungrabstackstr(p, dest);
|
---|
443 | ifsfirst = saveifs;
|
---|
444 | ifslastp = savelastp;
|
---|
445 | argbackq = saveargbackq;
|
---|
446 | herefd = saveherefd;
|
---|
447 |
|
---|
448 | p = in.buf;
|
---|
449 | lastc = '\0';
|
---|
450 | for (;;) {
|
---|
451 | if (--in.nleft < 0) {
|
---|
452 | if (in.fd < 0)
|
---|
453 | break;
|
---|
454 | while ((i = read(in.fd, buf, sizeof buf)) < 0 && errno == EINTR);
|
---|
455 | TRACE(("expbackq: read returns %d\n", i));
|
---|
456 | if (i <= 0)
|
---|
457 | break;
|
---|
458 | p = buf;
|
---|
459 | in.nleft = i - 1;
|
---|
460 | }
|
---|
461 | lastc = *p++;
|
---|
462 | if (lastc != '\0') {
|
---|
463 | if (quotes && syntax[(int)lastc] == CCTL)
|
---|
464 | STPUTC(CTLESC, dest);
|
---|
465 | STPUTC(lastc, dest);
|
---|
466 | }
|
---|
467 | }
|
---|
468 |
|
---|
469 | /* Eat all trailing newlines */
|
---|
470 | p = stackblock() + startloc;
|
---|
471 | while (dest > p && dest[-1] == '\n')
|
---|
472 | STUNPUTC(dest);
|
---|
473 |
|
---|
474 | if (in.fd >= 0)
|
---|
475 | close(in.fd);
|
---|
476 | if (in.buf)
|
---|
477 | ckfree(in.buf);
|
---|
478 | if (in.jp)
|
---|
479 | back_exitstatus = waitforjob(in.jp);
|
---|
480 | if (quoted == 0)
|
---|
481 | recordregion(startloc, dest - stackblock(), 0);
|
---|
482 | TRACE(("evalbackq: size=%d: \"%.*s\"\n",
|
---|
483 | (dest - stackblock()) - startloc,
|
---|
484 | (dest - stackblock()) - startloc,
|
---|
485 | stackblock() + startloc));
|
---|
486 | expdest = dest;
|
---|
487 | INTON;
|
---|
488 | }
|
---|
489 |
|
---|
490 |
|
---|
491 |
|
---|
492 | STATIC int
|
---|
493 | subevalvar(char *p, char *str, int strloc, int subtype, int startloc, int varflags)
|
---|
494 | {
|
---|
495 | char *startp;
|
---|
496 | char *loc = NULL;
|
---|
497 | char *q;
|
---|
498 | int c = 0;
|
---|
499 | int saveherefd = herefd;
|
---|
500 | struct nodelist *saveargbackq = argbackq;
|
---|
501 | int amount;
|
---|
502 |
|
---|
503 | herefd = -1;
|
---|
504 | argstr(p, 0);
|
---|
505 | STACKSTRNUL(expdest);
|
---|
506 | herefd = saveherefd;
|
---|
507 | argbackq = saveargbackq;
|
---|
508 | startp = stackblock() + startloc;
|
---|
509 | if (str == NULL)
|
---|
510 | str = stackblock() + strloc;
|
---|
511 |
|
---|
512 | switch (subtype) {
|
---|
513 | case VSASSIGN:
|
---|
514 | setvar(str, startp, 0);
|
---|
515 | amount = startp - expdest;
|
---|
516 | STADJUST(amount, expdest);
|
---|
517 | varflags &= ~VSNUL;
|
---|
518 | if (c != 0)
|
---|
519 | *loc = c;
|
---|
520 | return 1;
|
---|
521 |
|
---|
522 | case VSQUESTION:
|
---|
523 | if (*p != CTLENDVAR) {
|
---|
524 | outfmt(&errout, "%s\n", startp);
|
---|
525 | error((char *)NULL);
|
---|
526 | }
|
---|
527 | error("%.*s: parameter %snot set", p - str - 1,
|
---|
528 | str, (varflags & VSNUL) ? "null or "
|
---|
529 | : nullstr);
|
---|
530 | /* NOTREACHED */
|
---|
531 |
|
---|
532 | case VSTRIMLEFT:
|
---|
533 | for (loc = startp; loc < str; loc++) {
|
---|
534 | c = *loc;
|
---|
535 | *loc = '\0';
|
---|
536 | if (patmatch(str, startp, varflags & VSQUOTE))
|
---|
537 | goto recordleft;
|
---|
538 | *loc = c;
|
---|
539 | if ((varflags & VSQUOTE) && *loc == CTLESC)
|
---|
540 | loc++;
|
---|
541 | }
|
---|
542 | return 0;
|
---|
543 |
|
---|
544 | case VSTRIMLEFTMAX:
|
---|
545 | for (loc = str - 1; loc >= startp;) {
|
---|
546 | c = *loc;
|
---|
547 | *loc = '\0';
|
---|
548 | if (patmatch(str, startp, varflags & VSQUOTE))
|
---|
549 | goto recordleft;
|
---|
550 | *loc = c;
|
---|
551 | loc--;
|
---|
552 | if ((varflags & VSQUOTE) && loc > startp &&
|
---|
553 | *(loc - 1) == CTLESC) {
|
---|
554 | for (q = startp; q < loc; q++)
|
---|
555 | if (*q == CTLESC)
|
---|
556 | q++;
|
---|
557 | if (q > loc)
|
---|
558 | loc--;
|
---|
559 | }
|
---|
560 | }
|
---|
561 | return 0;
|
---|
562 |
|
---|
563 | case VSTRIMRIGHT:
|
---|
564 | for (loc = str - 1; loc >= startp;) {
|
---|
565 | if (patmatch(str, loc, varflags & VSQUOTE))
|
---|
566 | goto recordright;
|
---|
567 | loc--;
|
---|
568 | if ((varflags & VSQUOTE) && loc > startp &&
|
---|
569 | *(loc - 1) == CTLESC) {
|
---|
570 | for (q = startp; q < loc; q++)
|
---|
571 | if (*q == CTLESC)
|
---|
572 | q++;
|
---|
573 | if (q > loc)
|
---|
574 | loc--;
|
---|
575 | }
|
---|
576 | }
|
---|
577 | return 0;
|
---|
578 |
|
---|
579 | case VSTRIMRIGHTMAX:
|
---|
580 | for (loc = startp; loc < str - 1; loc++) {
|
---|
581 | if (patmatch(str, loc, varflags & VSQUOTE))
|
---|
582 | goto recordright;
|
---|
583 | if ((varflags & VSQUOTE) && *loc == CTLESC)
|
---|
584 | loc++;
|
---|
585 | }
|
---|
586 | return 0;
|
---|
587 |
|
---|
588 | default:
|
---|
589 | abort();
|
---|
590 | }
|
---|
591 |
|
---|
592 | recordleft:
|
---|
593 | *loc = c;
|
---|
594 | amount = ((str - 1) - (loc - startp)) - expdest;
|
---|
595 | STADJUST(amount, expdest);
|
---|
596 | while (loc != str - 1)
|
---|
597 | *startp++ = *loc++;
|
---|
598 | return 1;
|
---|
599 |
|
---|
600 | recordright:
|
---|
601 | amount = loc - expdest;
|
---|
602 | STADJUST(amount, expdest);
|
---|
603 | STPUTC('\0', expdest);
|
---|
604 | STADJUST(-1, expdest);
|
---|
605 | return 1;
|
---|
606 | }
|
---|
607 |
|
---|
608 |
|
---|
609 | /*
|
---|
610 | * Expand a variable, and return a pointer to the next character in the
|
---|
611 | * input string.
|
---|
612 | */
|
---|
613 |
|
---|
614 | STATIC char *
|
---|
615 | evalvar(char *p, int flag)
|
---|
616 | {
|
---|
617 | int subtype;
|
---|
618 | int varflags;
|
---|
619 | char *var;
|
---|
620 | char *val;
|
---|
621 | int patloc;
|
---|
622 | int c;
|
---|
623 | int set;
|
---|
624 | int special;
|
---|
625 | int startloc;
|
---|
626 | int varlen;
|
---|
627 | int apply_ifs;
|
---|
628 | int quotes = flag & (EXP_FULL | EXP_CASE);
|
---|
629 |
|
---|
630 | varflags = (unsigned char)*p++;
|
---|
631 | subtype = varflags & VSTYPE;
|
---|
632 | var = p;
|
---|
633 | special = !is_name(*p);
|
---|
634 | p = strchr(p, '=') + 1;
|
---|
635 |
|
---|
636 | again: /* jump here after setting a variable with ${var=text} */
|
---|
637 | if (special) {
|
---|
638 | set = varisset(var, varflags & VSNUL);
|
---|
639 | val = NULL;
|
---|
640 | } else {
|
---|
641 | val = lookupvar(var);
|
---|
642 | if (val == NULL || ((varflags & VSNUL) && val[0] == '\0')) {
|
---|
643 | val = NULL;
|
---|
644 | set = 0;
|
---|
645 | } else
|
---|
646 | set = 1;
|
---|
647 | }
|
---|
648 |
|
---|
649 | varlen = 0;
|
---|
650 | startloc = expdest - stackblock();
|
---|
651 |
|
---|
652 | if (!set && uflag) {
|
---|
653 | switch (subtype) {
|
---|
654 | case VSNORMAL:
|
---|
655 | case VSTRIMLEFT:
|
---|
656 | case VSTRIMLEFTMAX:
|
---|
657 | case VSTRIMRIGHT:
|
---|
658 | case VSTRIMRIGHTMAX:
|
---|
659 | case VSLENGTH:
|
---|
660 | error("%.*s: parameter not set", p - var - 1, var);
|
---|
661 | /* NOTREACHED */
|
---|
662 | }
|
---|
663 | }
|
---|
664 |
|
---|
665 | if (set && subtype != VSPLUS) {
|
---|
666 | /* insert the value of the variable */
|
---|
667 | if (special) {
|
---|
668 | varvalue(var, varflags & VSQUOTE, subtype, flag);
|
---|
669 | if (subtype == VSLENGTH) {
|
---|
670 | varlen = expdest - stackblock() - startloc;
|
---|
671 | STADJUST(-varlen, expdest);
|
---|
672 | }
|
---|
673 | } else {
|
---|
674 | char const *syntax = (varflags & VSQUOTE) ? DQSYNTAX
|
---|
675 | : BASESYNTAX;
|
---|
676 |
|
---|
677 | if (subtype == VSLENGTH) {
|
---|
678 | for (;*val; val++)
|
---|
679 | varlen++;
|
---|
680 | } else {
|
---|
681 | while (*val) {
|
---|
682 | if (quotes && syntax[(int)*val] == CCTL)
|
---|
683 | STPUTC(CTLESC, expdest);
|
---|
684 | STPUTC(*val++, expdest);
|
---|
685 | }
|
---|
686 |
|
---|
687 | }
|
---|
688 | }
|
---|
689 | }
|
---|
690 |
|
---|
691 |
|
---|
692 | apply_ifs = ((varflags & VSQUOTE) == 0 ||
|
---|
693 | (*var == '@' && shellparam.nparam != 1));
|
---|
694 |
|
---|
695 | switch (subtype) {
|
---|
696 | case VSLENGTH:
|
---|
697 | expdest = cvtnum(varlen, expdest);
|
---|
698 | break;
|
---|
699 |
|
---|
700 | case VSNORMAL:
|
---|
701 | break;
|
---|
702 |
|
---|
703 | case VSPLUS:
|
---|
704 | set = !set;
|
---|
705 | /* FALLTHROUGH */
|
---|
706 | case VSMINUS:
|
---|
707 | if (!set) {
|
---|
708 | argstr(p, flag | (apply_ifs ? EXP_IFS_SPLIT : 0));
|
---|
709 | /*
|
---|
710 | * ${x-a b c} doesn't get split, but removing the
|
---|
711 | * 'apply_ifs = 0' apparantly breaks ${1+"$@"}..
|
---|
712 | * ${x-'a b' c} should generate 2 args.
|
---|
713 | */
|
---|
714 | /* We should have marked stuff already */
|
---|
715 | apply_ifs = 0;
|
---|
716 | }
|
---|
717 | break;
|
---|
718 |
|
---|
719 | case VSTRIMLEFT:
|
---|
720 | case VSTRIMLEFTMAX:
|
---|
721 | case VSTRIMRIGHT:
|
---|
722 | case VSTRIMRIGHTMAX:
|
---|
723 | if (!set)
|
---|
724 | break;
|
---|
725 | /*
|
---|
726 | * Terminate the string and start recording the pattern
|
---|
727 | * right after it
|
---|
728 | */
|
---|
729 | STPUTC('\0', expdest);
|
---|
730 | patloc = expdest - stackblock();
|
---|
731 | if (subevalvar(p, NULL, patloc, subtype,
|
---|
732 | startloc, varflags) == 0) {
|
---|
733 | int amount = (expdest - stackblock() - patloc) + 1;
|
---|
734 | STADJUST(-amount, expdest);
|
---|
735 | }
|
---|
736 | /* Remove any recorded regions beyond start of variable */
|
---|
737 | removerecordregions(startloc);
|
---|
738 | apply_ifs = 1;
|
---|
739 | break;
|
---|
740 |
|
---|
741 | case VSASSIGN:
|
---|
742 | case VSQUESTION:
|
---|
743 | if (set)
|
---|
744 | break;
|
---|
745 | if (subevalvar(p, var, 0, subtype, startloc, varflags)) {
|
---|
746 | varflags &= ~VSNUL;
|
---|
747 | /*
|
---|
748 | * Remove any recorded regions beyond
|
---|
749 | * start of variable
|
---|
750 | */
|
---|
751 | removerecordregions(startloc);
|
---|
752 | goto again;
|
---|
753 | }
|
---|
754 | apply_ifs = 0;
|
---|
755 | break;
|
---|
756 |
|
---|
757 | default:
|
---|
758 | abort();
|
---|
759 | }
|
---|
760 |
|
---|
761 | if (apply_ifs)
|
---|
762 | recordregion(startloc, expdest - stackblock(),
|
---|
763 | varflags & VSQUOTE);
|
---|
764 |
|
---|
765 | if (subtype != VSNORMAL) { /* skip to end of alternative */
|
---|
766 | int nesting = 1;
|
---|
767 | for (;;) {
|
---|
768 | if ((c = *p++) == CTLESC)
|
---|
769 | p++;
|
---|
770 | else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) {
|
---|
771 | if (set)
|
---|
772 | argbackq = argbackq->next;
|
---|
773 | } else if (c == CTLVAR) {
|
---|
774 | if ((*p++ & VSTYPE) != VSNORMAL)
|
---|
775 | nesting++;
|
---|
776 | } else if (c == CTLENDVAR) {
|
---|
777 | if (--nesting == 0)
|
---|
778 | break;
|
---|
779 | }
|
---|
780 | }
|
---|
781 | }
|
---|
782 | return p;
|
---|
783 | }
|
---|
784 |
|
---|
785 |
|
---|
786 |
|
---|
787 | /*
|
---|
788 | * Test whether a specialized variable is set.
|
---|
789 | */
|
---|
790 |
|
---|
791 | STATIC int
|
---|
792 | varisset(char *name, int nulok)
|
---|
793 | {
|
---|
794 | if (*name == '!')
|
---|
795 | return backgndpid != -1;
|
---|
796 | else if (*name == '@' || *name == '*') {
|
---|
797 | if (*shellparam.p == NULL)
|
---|
798 | return 0;
|
---|
799 |
|
---|
800 | if (nulok) {
|
---|
801 | char **av;
|
---|
802 |
|
---|
803 | for (av = shellparam.p; *av; av++)
|
---|
804 | if (**av != '\0')
|
---|
805 | return 1;
|
---|
806 | return 0;
|
---|
807 | }
|
---|
808 | } else if (is_digit(*name)) {
|
---|
809 | char *ap;
|
---|
810 | int num = atoi(name);
|
---|
811 |
|
---|
812 | if (num > shellparam.nparam)
|
---|
813 | return 0;
|
---|
814 |
|
---|
815 | if (num == 0)
|
---|
816 | ap = arg0;
|
---|
817 | else
|
---|
818 | ap = shellparam.p[num - 1];
|
---|
819 |
|
---|
820 | if (nulok && (ap == NULL || *ap == '\0'))
|
---|
821 | return 0;
|
---|
822 | }
|
---|
823 | return 1;
|
---|
824 | }
|
---|
825 |
|
---|
826 |
|
---|
827 |
|
---|
828 | /*
|
---|
829 | * Add the value of a specialized variable to the stack string.
|
---|
830 | */
|
---|
831 |
|
---|
832 | STATIC void
|
---|
833 | varvalue(char *name, int quoted, int subtype, int flag)
|
---|
834 | {
|
---|
835 | int num;
|
---|
836 | char *p;
|
---|
837 | int i;
|
---|
838 | char sep;
|
---|
839 | char **ap;
|
---|
840 | char const *syntax;
|
---|
841 |
|
---|
842 | #define STRTODEST(p) \
|
---|
843 | do {\
|
---|
844 | if (flag & (EXP_FULL | EXP_CASE) && subtype != VSLENGTH) { \
|
---|
845 | syntax = quoted? DQSYNTAX : BASESYNTAX; \
|
---|
846 | while (*p) { \
|
---|
847 | if (syntax[(int)*p] == CCTL) \
|
---|
848 | STPUTC(CTLESC, expdest); \
|
---|
849 | STPUTC(*p++, expdest); \
|
---|
850 | } \
|
---|
851 | } else \
|
---|
852 | while (*p) \
|
---|
853 | STPUTC(*p++, expdest); \
|
---|
854 | } while (0)
|
---|
855 |
|
---|
856 |
|
---|
857 | switch (*name) {
|
---|
858 | case '$':
|
---|
859 | num = rootpid;
|
---|
860 | goto numvar;
|
---|
861 | case '?':
|
---|
862 | num = exitstatus;
|
---|
863 | goto numvar;
|
---|
864 | case '#':
|
---|
865 | num = shellparam.nparam;
|
---|
866 | goto numvar;
|
---|
867 | case '!':
|
---|
868 | num = backgndpid;
|
---|
869 | numvar:
|
---|
870 | expdest = cvtnum(num, expdest);
|
---|
871 | break;
|
---|
872 | case '-':
|
---|
873 | for (i = 0; optlist[i].name; i++) {
|
---|
874 | if (optlist[i].val)
|
---|
875 | STPUTC(optlist[i].letter, expdest);
|
---|
876 | }
|
---|
877 | break;
|
---|
878 | case '@':
|
---|
879 | if (flag & EXP_FULL && quoted) {
|
---|
880 | for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
|
---|
881 | STRTODEST(p);
|
---|
882 | if (*ap)
|
---|
883 | STPUTC('\0', expdest);
|
---|
884 | }
|
---|
885 | break;
|
---|
886 | }
|
---|
887 | /* fall through */
|
---|
888 | case '*':
|
---|
889 | if (ifsset() != 0)
|
---|
890 | sep = ifsval()[0];
|
---|
891 | else
|
---|
892 | sep = ' ';
|
---|
893 | for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
|
---|
894 | STRTODEST(p);
|
---|
895 | if (*ap && sep)
|
---|
896 | STPUTC(sep, expdest);
|
---|
897 | }
|
---|
898 | break;
|
---|
899 | case '0':
|
---|
900 | p = arg0;
|
---|
901 | STRTODEST(p);
|
---|
902 | break;
|
---|
903 | default:
|
---|
904 | if (is_digit(*name)) {
|
---|
905 | num = atoi(name);
|
---|
906 | if (num > 0 && num <= shellparam.nparam) {
|
---|
907 | p = shellparam.p[num - 1];
|
---|
908 | STRTODEST(p);
|
---|
909 | }
|
---|
910 | }
|
---|
911 | break;
|
---|
912 | }
|
---|
913 | }
|
---|
914 |
|
---|
915 |
|
---|
916 |
|
---|
917 | /*
|
---|
918 | * Record the fact that we have to scan this region of the
|
---|
919 | * string for IFS characters.
|
---|
920 | */
|
---|
921 |
|
---|
922 | STATIC void
|
---|
923 | recordregion(int start, int end, int inquotes)
|
---|
924 | {
|
---|
925 | struct ifsregion *ifsp;
|
---|
926 |
|
---|
927 | if (ifslastp == NULL) {
|
---|
928 | ifsp = &ifsfirst;
|
---|
929 | } else {
|
---|
930 | if (ifslastp->endoff == start
|
---|
931 | && ifslastp->inquotes == inquotes) {
|
---|
932 | /* extend previous area */
|
---|
933 | ifslastp->endoff = end;
|
---|
934 | return;
|
---|
935 | }
|
---|
936 | ifsp = (struct ifsregion *)ckmalloc(sizeof (struct ifsregion));
|
---|
937 | ifslastp->next = ifsp;
|
---|
938 | }
|
---|
939 | ifslastp = ifsp;
|
---|
940 | ifslastp->next = NULL;
|
---|
941 | ifslastp->begoff = start;
|
---|
942 | ifslastp->endoff = end;
|
---|
943 | ifslastp->inquotes = inquotes;
|
---|
944 | }
|
---|
945 |
|
---|
946 |
|
---|
947 |
|
---|
948 | /*
|
---|
949 | * Break the argument string into pieces based upon IFS and add the
|
---|
950 | * strings to the argument list. The regions of the string to be
|
---|
951 | * searched for IFS characters have been stored by recordregion.
|
---|
952 | */
|
---|
953 | STATIC void
|
---|
954 | ifsbreakup(char *string, struct arglist *arglist)
|
---|
955 | {
|
---|
956 | struct ifsregion *ifsp;
|
---|
957 | struct strlist *sp;
|
---|
958 | char *start;
|
---|
959 | char *p;
|
---|
960 | char *q;
|
---|
961 | const char *ifs;
|
---|
962 | const char *ifsspc;
|
---|
963 | int inquotes;
|
---|
964 |
|
---|
965 | start = string;
|
---|
966 | ifsspc = NULL;
|
---|
967 | inquotes = 0;
|
---|
968 |
|
---|
969 | if (ifslastp == NULL) {
|
---|
970 | /* Return entire argument, IFS doesn't apply to any of it */
|
---|
971 | sp = (struct strlist *)stalloc(sizeof *sp);
|
---|
972 | sp->text = start;
|
---|
973 | *arglist->lastp = sp;
|
---|
974 | arglist->lastp = &sp->next;
|
---|
975 | return;
|
---|
976 | }
|
---|
977 |
|
---|
978 | ifs = ifsset() ? ifsval() : " \t\n";
|
---|
979 |
|
---|
980 | for (ifsp = &ifsfirst; ifsp != NULL; ifsp = ifsp->next) {
|
---|
981 | p = string + ifsp->begoff;
|
---|
982 | inquotes = ifsp->inquotes;
|
---|
983 | ifsspc = NULL;
|
---|
984 | while (p < string + ifsp->endoff) {
|
---|
985 | q = p;
|
---|
986 | if (*p == CTLESC)
|
---|
987 | p++;
|
---|
988 | if (inquotes) {
|
---|
989 | /* Only NULs (probably from "$@") end args */
|
---|
990 | if (*p != 0) {
|
---|
991 | p++;
|
---|
992 | continue;
|
---|
993 | }
|
---|
994 | } else {
|
---|
995 | if (!strchr(ifs, *p)) {
|
---|
996 | p++;
|
---|
997 | continue;
|
---|
998 | }
|
---|
999 | ifsspc = strchr(" \t\n", *p);
|
---|
1000 |
|
---|
1001 | /* Ignore IFS whitespace at start */
|
---|
1002 | if (q == start && ifsspc != NULL) {
|
---|
1003 | p++;
|
---|
1004 | start = p;
|
---|
1005 | continue;
|
---|
1006 | }
|
---|
1007 | }
|
---|
1008 |
|
---|
1009 | /* Save this argument... */
|
---|
1010 | *q = '\0';
|
---|
1011 | sp = (struct strlist *)stalloc(sizeof *sp);
|
---|
1012 | sp->text = start;
|
---|
1013 | *arglist->lastp = sp;
|
---|
1014 | arglist->lastp = &sp->next;
|
---|
1015 | p++;
|
---|
1016 |
|
---|
1017 | if (ifsspc != NULL) {
|
---|
1018 | /* Ignore further trailing IFS whitespace */
|
---|
1019 | for (; p < string + ifsp->endoff; p++) {
|
---|
1020 | q = p;
|
---|
1021 | if (*p == CTLESC)
|
---|
1022 | p++;
|
---|
1023 | if (strchr(ifs, *p) == NULL) {
|
---|
1024 | p = q;
|
---|
1025 | break;
|
---|
1026 | }
|
---|
1027 | if (strchr(" \t\n", *p) == NULL) {
|
---|
1028 | p++;
|
---|
1029 | break;
|
---|
1030 | }
|
---|
1031 | }
|
---|
1032 | }
|
---|
1033 | start = p;
|
---|
1034 | }
|
---|
1035 | }
|
---|
1036 |
|
---|
1037 | /*
|
---|
1038 | * Save anything left as an argument.
|
---|
1039 | * Traditionally we have treated 'IFS=':'; set -- x$IFS' as
|
---|
1040 | * generating 2 arguments, the second of which is empty.
|
---|
1041 | * Some recent clarification of the Posix spec say that it
|
---|
1042 | * should only generate one....
|
---|
1043 | */
|
---|
1044 | if (*start /* || (!ifsspc && start > string) */) {
|
---|
1045 | sp = (struct strlist *)stalloc(sizeof *sp);
|
---|
1046 | sp->text = start;
|
---|
1047 | *arglist->lastp = sp;
|
---|
1048 | arglist->lastp = &sp->next;
|
---|
1049 | }
|
---|
1050 | }
|
---|
1051 |
|
---|
1052 | STATIC void
|
---|
1053 | ifsfree(void)
|
---|
1054 | {
|
---|
1055 | while (ifsfirst.next != NULL) {
|
---|
1056 | struct ifsregion *ifsp;
|
---|
1057 | INTOFF;
|
---|
1058 | ifsp = ifsfirst.next->next;
|
---|
1059 | ckfree(ifsfirst.next);
|
---|
1060 | ifsfirst.next = ifsp;
|
---|
1061 | INTON;
|
---|
1062 | }
|
---|
1063 | ifslastp = NULL;
|
---|
1064 | ifsfirst.next = NULL;
|
---|
1065 | }
|
---|
1066 |
|
---|
1067 |
|
---|
1068 |
|
---|
1069 | /*
|
---|
1070 | * Expand shell metacharacters. At this point, the only control characters
|
---|
1071 | * should be escapes. The results are stored in the list exparg.
|
---|
1072 | */
|
---|
1073 |
|
---|
1074 | char *expdir;
|
---|
1075 |
|
---|
1076 |
|
---|
1077 | STATIC void
|
---|
1078 | expandmeta(struct strlist *str, int flag)
|
---|
1079 | {
|
---|
1080 | char *p;
|
---|
1081 | struct strlist **savelastp;
|
---|
1082 | struct strlist *sp;
|
---|
1083 | char c;
|
---|
1084 | /* TODO - EXP_REDIR */
|
---|
1085 |
|
---|
1086 | while (str) {
|
---|
1087 | if (fflag)
|
---|
1088 | goto nometa;
|
---|
1089 | p = str->text;
|
---|
1090 | for (;;) { /* fast check for meta chars */
|
---|
1091 | if ((c = *p++) == '\0')
|
---|
1092 | goto nometa;
|
---|
1093 | if (c == '*' || c == '?' || c == '[' || c == '!')
|
---|
1094 | break;
|
---|
1095 | }
|
---|
1096 | savelastp = exparg.lastp;
|
---|
1097 | INTOFF;
|
---|
1098 | if (expdir == NULL) {
|
---|
1099 | int i = strlen(str->text);
|
---|
1100 | expdir = ckmalloc(i < 2048 ? 2048 : i); /* XXX */
|
---|
1101 | }
|
---|
1102 |
|
---|
1103 | expmeta(expdir, str->text);
|
---|
1104 | ckfree(expdir);
|
---|
1105 | expdir = NULL;
|
---|
1106 | INTON;
|
---|
1107 | if (exparg.lastp == savelastp) {
|
---|
1108 | /*
|
---|
1109 | * no matches
|
---|
1110 | */
|
---|
1111 | nometa:
|
---|
1112 | *exparg.lastp = str;
|
---|
1113 | rmescapes(str->text);
|
---|
1114 | exparg.lastp = &str->next;
|
---|
1115 | } else {
|
---|
1116 | *exparg.lastp = NULL;
|
---|
1117 | *savelastp = sp = expsort(*savelastp);
|
---|
1118 | while (sp->next != NULL)
|
---|
1119 | sp = sp->next;
|
---|
1120 | exparg.lastp = &sp->next;
|
---|
1121 | }
|
---|
1122 | str = str->next;
|
---|
1123 | }
|
---|
1124 | }
|
---|
1125 |
|
---|
1126 |
|
---|
1127 | /*
|
---|
1128 | * Do metacharacter (i.e. *, ?, [...]) expansion.
|
---|
1129 | */
|
---|
1130 |
|
---|
1131 | STATIC void
|
---|
1132 | expmeta(char *enddir, char *name)
|
---|
1133 | {
|
---|
1134 | char *p;
|
---|
1135 | const char *cp;
|
---|
1136 | char *q;
|
---|
1137 | char *start;
|
---|
1138 | char *endname;
|
---|
1139 | int metaflag;
|
---|
1140 | struct stat statb;
|
---|
1141 | DIR *dirp;
|
---|
1142 | struct dirent *dp;
|
---|
1143 | int atend;
|
---|
1144 | int matchdot;
|
---|
1145 |
|
---|
1146 | metaflag = 0;
|
---|
1147 | start = name;
|
---|
1148 | for (p = name ; ; p++) {
|
---|
1149 | if (*p == '*' || *p == '?')
|
---|
1150 | metaflag = 1;
|
---|
1151 | else if (*p == '[') {
|
---|
1152 | q = p + 1;
|
---|
1153 | if (*q == '!')
|
---|
1154 | q++;
|
---|
1155 | for (;;) {
|
---|
1156 | while (*q == CTLQUOTEMARK)
|
---|
1157 | q++;
|
---|
1158 | if (*q == CTLESC)
|
---|
1159 | q++;
|
---|
1160 | if (*q == '/' || *q == '\0')
|
---|
1161 | break;
|
---|
1162 | if (*++q == ']') {
|
---|
1163 | metaflag = 1;
|
---|
1164 | break;
|
---|
1165 | }
|
---|
1166 | }
|
---|
1167 | } else if (*p == '!' && p[1] == '!' && (p == name || p[-1] == '/')) {
|
---|
1168 | metaflag = 1;
|
---|
1169 | } else if (*p == '\0')
|
---|
1170 | break;
|
---|
1171 | else if (*p == CTLQUOTEMARK)
|
---|
1172 | continue;
|
---|
1173 | else if (*p == CTLESC)
|
---|
1174 | p++;
|
---|
1175 | if (*p == '/') {
|
---|
1176 | if (metaflag)
|
---|
1177 | break;
|
---|
1178 | start = p + 1;
|
---|
1179 | }
|
---|
1180 | }
|
---|
1181 | if (metaflag == 0) { /* we've reached the end of the file name */
|
---|
1182 | if (enddir != expdir)
|
---|
1183 | metaflag++;
|
---|
1184 | for (p = name ; ; p++) {
|
---|
1185 | if (*p == CTLQUOTEMARK)
|
---|
1186 | continue;
|
---|
1187 | if (*p == CTLESC)
|
---|
1188 | p++;
|
---|
1189 | *enddir++ = *p;
|
---|
1190 | if (*p == '\0')
|
---|
1191 | break;
|
---|
1192 | }
|
---|
1193 | if (metaflag == 0 || lstat(expdir, &statb) >= 0)
|
---|
1194 | addfname(expdir);
|
---|
1195 | return;
|
---|
1196 | }
|
---|
1197 | endname = p;
|
---|
1198 | if (start != name) {
|
---|
1199 | p = name;
|
---|
1200 | while (p < start) {
|
---|
1201 | while (*p == CTLQUOTEMARK)
|
---|
1202 | p++;
|
---|
1203 | if (*p == CTLESC)
|
---|
1204 | p++;
|
---|
1205 | *enddir++ = *p++;
|
---|
1206 | }
|
---|
1207 | }
|
---|
1208 | if (enddir == expdir) {
|
---|
1209 | cp = ".";
|
---|
1210 | } else if (enddir == expdir + 1 && *expdir == '/') {
|
---|
1211 | cp = "/";
|
---|
1212 | } else {
|
---|
1213 | cp = expdir;
|
---|
1214 | enddir[-1] = '\0';
|
---|
1215 | }
|
---|
1216 | if ((dirp = opendir(cp)) == NULL)
|
---|
1217 | return;
|
---|
1218 | if (enddir != expdir)
|
---|
1219 | enddir[-1] = '/';
|
---|
1220 | if (*endname == 0) {
|
---|
1221 | atend = 1;
|
---|
1222 | } else {
|
---|
1223 | atend = 0;
|
---|
1224 | *endname++ = '\0';
|
---|
1225 | }
|
---|
1226 | matchdot = 0;
|
---|
1227 | p = start;
|
---|
1228 | while (*p == CTLQUOTEMARK)
|
---|
1229 | p++;
|
---|
1230 | if (*p == CTLESC)
|
---|
1231 | p++;
|
---|
1232 | if (*p == '.')
|
---|
1233 | matchdot++;
|
---|
1234 | while (! int_pending() && (dp = readdir(dirp)) != NULL) {
|
---|
1235 | if (dp->d_name[0] == '.' && ! matchdot)
|
---|
1236 | continue;
|
---|
1237 | if (patmatch(start, dp->d_name, 0)) {
|
---|
1238 | if (atend) {
|
---|
1239 | scopy(dp->d_name, enddir);
|
---|
1240 | addfname(expdir);
|
---|
1241 | } else {
|
---|
1242 | for (p = enddir, cp = dp->d_name;
|
---|
1243 | (*p++ = *cp++) != '\0';)
|
---|
1244 | continue;
|
---|
1245 | p[-1] = '/';
|
---|
1246 | expmeta(p, endname);
|
---|
1247 | }
|
---|
1248 | }
|
---|
1249 | }
|
---|
1250 | closedir(dirp);
|
---|
1251 | if (! atend)
|
---|
1252 | endname[-1] = '/';
|
---|
1253 | }
|
---|
1254 |
|
---|
1255 |
|
---|
1256 | /*
|
---|
1257 | * Add a file name to the list.
|
---|
1258 | */
|
---|
1259 |
|
---|
1260 | STATIC void
|
---|
1261 | addfname(char *name)
|
---|
1262 | {
|
---|
1263 | char *p;
|
---|
1264 | struct strlist *sp;
|
---|
1265 |
|
---|
1266 | p = stalloc(strlen(name) + 1);
|
---|
1267 | scopy(name, p);
|
---|
1268 | sp = (struct strlist *)stalloc(sizeof *sp);
|
---|
1269 | sp->text = p;
|
---|
1270 | *exparg.lastp = sp;
|
---|
1271 | exparg.lastp = &sp->next;
|
---|
1272 | }
|
---|
1273 |
|
---|
1274 |
|
---|
1275 | /*
|
---|
1276 | * Sort the results of file name expansion. It calculates the number of
|
---|
1277 | * strings to sort and then calls msort (short for merge sort) to do the
|
---|
1278 | * work.
|
---|
1279 | */
|
---|
1280 |
|
---|
1281 | STATIC struct strlist *
|
---|
1282 | expsort(struct strlist *str)
|
---|
1283 | {
|
---|
1284 | int len;
|
---|
1285 | struct strlist *sp;
|
---|
1286 |
|
---|
1287 | len = 0;
|
---|
1288 | for (sp = str ; sp ; sp = sp->next)
|
---|
1289 | len++;
|
---|
1290 | return msort(str, len);
|
---|
1291 | }
|
---|
1292 |
|
---|
1293 |
|
---|
1294 | STATIC struct strlist *
|
---|
1295 | msort(struct strlist *list, int len)
|
---|
1296 | {
|
---|
1297 | struct strlist *p, *q = NULL;
|
---|
1298 | struct strlist **lpp;
|
---|
1299 | int half;
|
---|
1300 | int n;
|
---|
1301 |
|
---|
1302 | if (len <= 1)
|
---|
1303 | return list;
|
---|
1304 | half = len >> 1;
|
---|
1305 | p = list;
|
---|
1306 | for (n = half ; --n >= 0 ; ) {
|
---|
1307 | q = p;
|
---|
1308 | p = p->next;
|
---|
1309 | }
|
---|
1310 | q->next = NULL; /* terminate first half of list */
|
---|
1311 | q = msort(list, half); /* sort first half of list */
|
---|
1312 | p = msort(p, len - half); /* sort second half */
|
---|
1313 | lpp = &list;
|
---|
1314 | for (;;) {
|
---|
1315 | if (strcmp(p->text, q->text) < 0) {
|
---|
1316 | *lpp = p;
|
---|
1317 | lpp = &p->next;
|
---|
1318 | if ((p = *lpp) == NULL) {
|
---|
1319 | *lpp = q;
|
---|
1320 | break;
|
---|
1321 | }
|
---|
1322 | } else {
|
---|
1323 | *lpp = q;
|
---|
1324 | lpp = &q->next;
|
---|
1325 | if ((q = *lpp) == NULL) {
|
---|
1326 | *lpp = p;
|
---|
1327 | break;
|
---|
1328 | }
|
---|
1329 | }
|
---|
1330 | }
|
---|
1331 | return list;
|
---|
1332 | }
|
---|
1333 |
|
---|
1334 |
|
---|
1335 |
|
---|
1336 | /*
|
---|
1337 | * Returns true if the pattern matches the string.
|
---|
1338 | */
|
---|
1339 |
|
---|
1340 | int
|
---|
1341 | patmatch(char *pattern, char *string, int squoted)
|
---|
1342 | {
|
---|
1343 | #ifdef notdef
|
---|
1344 | if (pattern[0] == '!' && pattern[1] == '!')
|
---|
1345 | return 1 - pmatch(pattern + 2, string);
|
---|
1346 | else
|
---|
1347 | #endif
|
---|
1348 | return pmatch(pattern, string, squoted);
|
---|
1349 | }
|
---|
1350 |
|
---|
1351 |
|
---|
1352 | STATIC int
|
---|
1353 | pmatch(char *pattern, char *string, int squoted)
|
---|
1354 | {
|
---|
1355 | char *p, *q;
|
---|
1356 | char c;
|
---|
1357 |
|
---|
1358 | p = pattern;
|
---|
1359 | q = string;
|
---|
1360 | for (;;) {
|
---|
1361 | switch (c = *p++) {
|
---|
1362 | case '\0':
|
---|
1363 | goto breakloop;
|
---|
1364 | case CTLESC:
|
---|
1365 | if (squoted && *q == CTLESC)
|
---|
1366 | q++;
|
---|
1367 | if (*q++ != *p++)
|
---|
1368 | return 0;
|
---|
1369 | break;
|
---|
1370 | case CTLQUOTEMARK:
|
---|
1371 | continue;
|
---|
1372 | case '?':
|
---|
1373 | if (squoted && *q == CTLESC)
|
---|
1374 | q++;
|
---|
1375 | if (*q++ == '\0')
|
---|
1376 | return 0;
|
---|
1377 | break;
|
---|
1378 | case '*':
|
---|
1379 | c = *p;
|
---|
1380 | while (c == CTLQUOTEMARK || c == '*')
|
---|
1381 | c = *++p;
|
---|
1382 | if (c != CTLESC && c != CTLQUOTEMARK &&
|
---|
1383 | c != '?' && c != '*' && c != '[') {
|
---|
1384 | while (*q != c) {
|
---|
1385 | if (squoted && *q == CTLESC &&
|
---|
1386 | q[1] == c)
|
---|
1387 | break;
|
---|
1388 | if (*q == '\0')
|
---|
1389 | return 0;
|
---|
1390 | if (squoted && *q == CTLESC)
|
---|
1391 | q++;
|
---|
1392 | q++;
|
---|
1393 | }
|
---|
1394 | }
|
---|
1395 | do {
|
---|
1396 | if (pmatch(p, q, squoted))
|
---|
1397 | return 1;
|
---|
1398 | if (squoted && *q == CTLESC)
|
---|
1399 | q++;
|
---|
1400 | } while (*q++ != '\0');
|
---|
1401 | return 0;
|
---|
1402 | case '[': {
|
---|
1403 | char *endp;
|
---|
1404 | int invert, found;
|
---|
1405 | char chr;
|
---|
1406 |
|
---|
1407 | endp = p;
|
---|
1408 | if (*endp == '!')
|
---|
1409 | endp++;
|
---|
1410 | for (;;) {
|
---|
1411 | while (*endp == CTLQUOTEMARK)
|
---|
1412 | endp++;
|
---|
1413 | if (*endp == '\0')
|
---|
1414 | goto dft; /* no matching ] */
|
---|
1415 | if (*endp == CTLESC)
|
---|
1416 | endp++;
|
---|
1417 | if (*++endp == ']')
|
---|
1418 | break;
|
---|
1419 | }
|
---|
1420 | invert = 0;
|
---|
1421 | if (*p == '!') {
|
---|
1422 | invert++;
|
---|
1423 | p++;
|
---|
1424 | }
|
---|
1425 | found = 0;
|
---|
1426 | chr = *q++;
|
---|
1427 | if (squoted && chr == CTLESC)
|
---|
1428 | chr = *q++;
|
---|
1429 | if (chr == '\0')
|
---|
1430 | return 0;
|
---|
1431 | c = *p++;
|
---|
1432 | do {
|
---|
1433 | if (c == CTLQUOTEMARK)
|
---|
1434 | continue;
|
---|
1435 | if (c == CTLESC)
|
---|
1436 | c = *p++;
|
---|
1437 | if (*p == '-' && p[1] != ']') {
|
---|
1438 | p++;
|
---|
1439 | while (*p == CTLQUOTEMARK)
|
---|
1440 | p++;
|
---|
1441 | if (*p == CTLESC)
|
---|
1442 | p++;
|
---|
1443 | if (chr >= c && chr <= *p)
|
---|
1444 | found = 1;
|
---|
1445 | p++;
|
---|
1446 | } else {
|
---|
1447 | if (chr == c)
|
---|
1448 | found = 1;
|
---|
1449 | }
|
---|
1450 | } while ((c = *p++) != ']');
|
---|
1451 | if (found == invert)
|
---|
1452 | return 0;
|
---|
1453 | break;
|
---|
1454 | }
|
---|
1455 | dft: default:
|
---|
1456 | if (squoted && *q == CTLESC)
|
---|
1457 | q++;
|
---|
1458 | if (*q++ != c)
|
---|
1459 | return 0;
|
---|
1460 | break;
|
---|
1461 | }
|
---|
1462 | }
|
---|
1463 | breakloop:
|
---|
1464 | if (*q != '\0')
|
---|
1465 | return 0;
|
---|
1466 | return 1;
|
---|
1467 | }
|
---|
1468 |
|
---|
1469 |
|
---|
1470 |
|
---|
1471 | /*
|
---|
1472 | * Remove any CTLESC characters from a string.
|
---|
1473 | */
|
---|
1474 |
|
---|
1475 | void
|
---|
1476 | rmescapes(char *str)
|
---|
1477 | {
|
---|
1478 | char *p, *q;
|
---|
1479 |
|
---|
1480 | p = str;
|
---|
1481 | while (*p != CTLESC && *p != CTLQUOTEMARK) {
|
---|
1482 | if (*p++ == '\0')
|
---|
1483 | return;
|
---|
1484 | }
|
---|
1485 | q = p;
|
---|
1486 | while (*p) {
|
---|
1487 | if (*p == CTLQUOTEMARK) {
|
---|
1488 | p++;
|
---|
1489 | continue;
|
---|
1490 | }
|
---|
1491 | if (*p == CTLESC)
|
---|
1492 | p++;
|
---|
1493 | *q++ = *p++;
|
---|
1494 | }
|
---|
1495 | *q = '\0';
|
---|
1496 | }
|
---|
1497 |
|
---|
1498 |
|
---|
1499 |
|
---|
1500 | /*
|
---|
1501 | * See if a pattern matches in a case statement.
|
---|
1502 | */
|
---|
1503 |
|
---|
1504 | int
|
---|
1505 | casematch(union node *pattern, char *val)
|
---|
1506 | {
|
---|
1507 | struct stackmark smark;
|
---|
1508 | int result;
|
---|
1509 | char *p;
|
---|
1510 |
|
---|
1511 | setstackmark(&smark);
|
---|
1512 | argbackq = pattern->narg.backquote;
|
---|
1513 | STARTSTACKSTR(expdest);
|
---|
1514 | ifslastp = NULL;
|
---|
1515 | argstr(pattern->narg.text, EXP_TILDE | EXP_CASE);
|
---|
1516 | STPUTC('\0', expdest);
|
---|
1517 | p = grabstackstr(expdest);
|
---|
1518 | result = patmatch(p, val, 0);
|
---|
1519 | popstackmark(&smark);
|
---|
1520 | return result;
|
---|
1521 | }
|
---|
1522 |
|
---|
1523 | /*
|
---|
1524 | * Our own itoa().
|
---|
1525 | */
|
---|
1526 |
|
---|
1527 | STATIC char *
|
---|
1528 | cvtnum(int num, char *buf)
|
---|
1529 | {
|
---|
1530 | char temp[32];
|
---|
1531 | int neg = num < 0;
|
---|
1532 | char *p = temp + 31;
|
---|
1533 |
|
---|
1534 | temp[31] = '\0';
|
---|
1535 |
|
---|
1536 | do {
|
---|
1537 | *--p = num % 10 + '0';
|
---|
1538 | } while ((num /= 10) != 0);
|
---|
1539 |
|
---|
1540 | if (neg)
|
---|
1541 | *--p = '-';
|
---|
1542 |
|
---|
1543 | while (*p)
|
---|
1544 | STPUTC(*p++, buf);
|
---|
1545 | return buf;
|
---|
1546 | }
|
---|
1547 |
|
---|
1548 | /*
|
---|
1549 | * Do most of the work for wordexp(3).
|
---|
1550 | */
|
---|
1551 |
|
---|
1552 | int
|
---|
1553 | wordexpcmd(int argc, char **argv)
|
---|
1554 | {
|
---|
1555 | size_t len;
|
---|
1556 | int i;
|
---|
1557 |
|
---|
1558 | out1fmt("%d", argc - 1);
|
---|
1559 | out1c('\0');
|
---|
1560 | for (i = 1, len = 0; i < argc; i++)
|
---|
1561 | len += strlen(argv[i]);
|
---|
1562 | out1fmt("%zd", len);
|
---|
1563 | out1c('\0');
|
---|
1564 | for (i = 1; i < argc; i++) {
|
---|
1565 | out1str(argv[i]);
|
---|
1566 | out1c('\0');
|
---|
1567 | }
|
---|
1568 | return (0);
|
---|
1569 | }
|
---|