usr.sbin/makefs: Sync with sys/vfs/hammer2
[dragonfly.git] / bin / sh / expand.c
blob638da0773e773ff6eee511629f5fee886df0f824
1 /*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1997-2005
5 * Herbert Xu <herbert@gondor.apana.org.au>. All rights reserved.
6 * Copyright (c) 2010-2015
7 * Jilles Tjoelker <jilles@stack.nl>. All rights reserved.
9 * This code is derived from software contributed to Berkeley by
10 * Kenneth Almquist.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)expand.c 8.5 (Berkeley) 5/15/95";
40 #endif
41 #endif /* not lint */
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD: head/bin/sh/expand.c 341164 2018-11-28 20:03:53Z jilles $");
45 #include <sys/types.h>
46 #include <sys/time.h>
47 #include <sys/stat.h>
48 #include <dirent.h>
49 #include <errno.h>
50 #include <inttypes.h>
51 #include <limits.h>
52 #include <pwd.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57 #include <wchar.h>
58 #include <wctype.h>
61 * Routines to expand arguments to commands. We have to deal with
62 * backquotes, shell variables, and file metacharacters.
65 #include "shell.h"
66 #include "main.h"
67 #include "nodes.h"
68 #include "eval.h"
69 #include "expand.h"
70 #include "syntax.h"
71 #include "parser.h"
72 #include "jobs.h"
73 #include "options.h"
74 #include "var.h"
75 #include "input.h"
76 #include "output.h"
77 #include "memalloc.h"
78 #include "error.h"
79 #include "mystring.h"
80 #include "arith.h"
81 #include "show.h"
82 #include "builtins.h"
84 enum wordstate { WORD_IDLE, WORD_WS_DELIMITED, WORD_QUOTEMARK };
86 struct worddest {
87 struct arglist *list;
88 enum wordstate state;
91 static char *expdest; /* output of current string */
93 static const char *argstr(const char *, struct nodelist **restrict, int,
94 struct worddest *);
95 static const char *exptilde(const char *, int);
96 static const char *expari(const char *, struct nodelist **restrict, int,
97 struct worddest *);
98 static void expbackq(union node *, int, int, struct worddest *);
99 static const char *subevalvar_trim(const char *, struct nodelist **restrict,
100 int, int, int);
101 static const char *subevalvar_misc(const char *, struct nodelist **restrict,
102 const char *, int, int, int);
103 static const char *evalvar(const char *, struct nodelist **restrict, int,
104 struct worddest *);
105 static int varisset(const char *, int);
106 static void strtodest(const char *, int, int, int, struct worddest *);
107 static void reprocess(int, int, int, int, struct worddest *);
108 static void varvalue(const char *, int, int, int, struct worddest *);
109 static void expandmeta(char *, struct arglist *);
110 static void expmeta(char *, char *, struct arglist *);
111 static int expsortcmp(const void *, const void *);
112 static int patmatch(const char *, const char *);
113 static void cvtnum(int, char *);
114 static int collate_range_cmp(wchar_t, wchar_t);
116 void
117 emptyarglist(struct arglist *list)
120 list->args = list->smallarg;
121 list->count = 0;
122 list->capacity = sizeof(list->smallarg) / sizeof(list->smallarg[0]);
125 void
126 appendarglist(struct arglist *list, char *str)
128 char **newargs;
129 int newcapacity;
131 if (list->count >= list->capacity) {
132 newcapacity = list->capacity * 2;
133 if (newcapacity < 16)
134 newcapacity = 16;
135 if (newcapacity > INT_MAX / (int)sizeof(newargs[0]))
136 error("Too many entries in arglist");
137 newargs = stalloc(newcapacity * sizeof(newargs[0]));
138 memcpy(newargs, list->args, list->count * sizeof(newargs[0]));
139 list->args = newargs;
140 list->capacity = newcapacity;
142 list->args[list->count++] = str;
145 static int
146 collate_range_cmp(wchar_t c1, wchar_t c2)
148 wchar_t s1[2], s2[2];
150 s1[0] = c1;
151 s1[1] = L'\0';
152 s2[0] = c2;
153 s2[1] = L'\0';
154 return (wcscoll(s1, s2));
157 static char *
158 stputs_quotes(const char *data, const char *syntax, char *p)
160 while (*data) {
161 CHECKSTRSPACE(2, p);
162 if (syntax[(int)*data] == CCTL)
163 USTPUTC(CTLESC, p);
164 USTPUTC(*data++, p);
166 return (p);
168 #define STPUTS_QUOTES(data, syntax, p) p = stputs_quotes((data), syntax, p)
170 static char *
171 nextword(char c, int flag, char *p, struct worddest *dst)
173 int is_ws;
175 is_ws = c == '\t' || c == '\n' || c == ' ';
176 if (p != stackblock() || (is_ws ? dst->state == WORD_QUOTEMARK :
177 dst->state != WORD_WS_DELIMITED) || c == '\0') {
178 STPUTC('\0', p);
179 if (flag & EXP_GLOB)
180 expandmeta(grabstackstr(p), dst->list);
181 else
182 appendarglist(dst->list, grabstackstr(p));
183 dst->state = is_ws ? WORD_WS_DELIMITED : WORD_IDLE;
184 } else if (!is_ws && dst->state == WORD_WS_DELIMITED)
185 dst->state = WORD_IDLE;
186 /* Reserve space while the stack string is empty. */
187 appendarglist(dst->list, NULL);
188 dst->list->count--;
189 STARTSTACKSTR(p);
190 return p;
192 #define NEXTWORD(c, flag, p, dstlist) p = nextword(c, flag, p, dstlist)
194 static char *
195 stputs_split(const char *data, const char *syntax, int flag, char *p,
196 struct worddest *dst)
198 const char *ifs;
199 char c;
201 ifs = ifsset() ? ifsval() : " \t\n";
202 while (*data) {
203 CHECKSTRSPACE(2, p);
204 c = *data++;
205 if (strchr(ifs, c) != NULL) {
206 NEXTWORD(c, flag, p, dst);
207 continue;
209 if (flag & EXP_GLOB && syntax[(int)c] == CCTL)
210 USTPUTC(CTLESC, p);
211 USTPUTC(c, p);
213 return (p);
215 #define STPUTS_SPLIT(data, syntax, flag, p, dst) p = stputs_split((data), syntax, flag, p, dst)
218 * Perform expansions on an argument, placing the resulting list of arguments
219 * in arglist. Parameter expansion, command substitution and arithmetic
220 * expansion are always performed; additional expansions can be requested
221 * via flag (EXP_*).
222 * The result is left in the stack string.
223 * When arglist is NULL, perform here document expansion.
225 * When doing something that may cause this to be re-entered, make sure
226 * the stack string is empty via grabstackstr() and do not assume expdest
227 * remains valid.
229 void
230 expandarg(union node *arg, struct arglist *arglist, int flag)
232 struct worddest exparg;
233 struct nodelist *argbackq;
235 if (fflag)
236 flag &= ~EXP_GLOB;
237 argbackq = arg->narg.backquote;
238 exparg.list = arglist;
239 exparg.state = WORD_IDLE;
240 STARTSTACKSTR(expdest);
241 argstr(arg->narg.text, &argbackq, flag, &exparg);
242 if (arglist == NULL) {
243 STACKSTRNUL(expdest);
244 return; /* here document expanded */
246 if ((flag & EXP_SPLIT) == 0 || expdest != stackblock() ||
247 exparg.state == WORD_QUOTEMARK) {
248 STPUTC('\0', expdest);
249 if (flag & EXP_SPLIT) {
250 if (flag & EXP_GLOB)
251 expandmeta(grabstackstr(expdest), exparg.list);
252 else
253 appendarglist(exparg.list, grabstackstr(expdest));
256 if ((flag & EXP_SPLIT) == 0)
257 appendarglist(arglist, grabstackstr(expdest));
263 * Perform parameter expansion, command substitution and arithmetic
264 * expansion, and tilde expansion if requested via EXP_TILDE/EXP_VARTILDE.
265 * Processing ends at a CTLENDVAR or CTLENDARI character as well as '\0'.
266 * This is used to expand word in ${var+word} etc.
267 * If EXP_GLOB or EXP_CASE are set, keep and/or generate CTLESC
268 * characters to allow for further processing.
270 * If EXP_SPLIT is set, dst receives any complete words produced.
272 static const char *
273 argstr(const char *p, struct nodelist **restrict argbackq, int flag,
274 struct worddest *dst)
276 char c;
277 int quotes = flag & (EXP_GLOB | EXP_CASE); /* do CTLESC */
278 int firsteq = 1;
279 int split_lit;
280 int lit_quoted;
282 split_lit = flag & EXP_SPLIT_LIT;
283 lit_quoted = flag & EXP_LIT_QUOTED;
284 flag &= ~(EXP_SPLIT_LIT | EXP_LIT_QUOTED);
285 if (*p == '~' && (flag & (EXP_TILDE | EXP_VARTILDE)))
286 p = exptilde(p, flag);
287 for (;;) {
288 CHECKSTRSPACE(2, expdest);
289 switch (c = *p++) {
290 case '\0':
291 return (p - 1);
292 case CTLENDVAR:
293 case CTLENDARI:
294 return (p);
295 case CTLQUOTEMARK:
296 lit_quoted = 1;
297 /* "$@" syntax adherence hack */
298 if (p[0] == CTLVAR && (p[1] & VSQUOTE) != 0 &&
299 p[2] == '@' && p[3] == '=')
300 break;
301 if ((flag & EXP_SPLIT) != 0 && expdest == stackblock())
302 dst->state = WORD_QUOTEMARK;
303 break;
304 case CTLQUOTEEND:
305 lit_quoted = 0;
306 break;
307 case CTLESC:
308 c = *p++;
309 if (split_lit && !lit_quoted &&
310 strchr(ifsset() ? ifsval() : " \t\n", c) != NULL) {
311 NEXTWORD(c, flag, expdest, dst);
312 break;
314 if (quotes)
315 USTPUTC(CTLESC, expdest);
316 USTPUTC(c, expdest);
317 break;
318 case CTLVAR:
319 p = evalvar(p, argbackq, flag, dst);
320 break;
321 case CTLBACKQ:
322 case CTLBACKQ|CTLQUOTE:
323 expbackq((*argbackq)->n, c & CTLQUOTE, flag, dst);
324 *argbackq = (*argbackq)->next;
325 break;
326 case CTLARI:
327 p = expari(p, argbackq, flag, dst);
328 break;
329 case ':':
330 case '=':
332 * sort of a hack - expand tildes in variable
333 * assignments (after the first '=' and after ':'s).
335 if (split_lit && !lit_quoted &&
336 strchr(ifsset() ? ifsval() : " \t\n", c) != NULL) {
337 NEXTWORD(c, flag, expdest, dst);
338 break;
340 USTPUTC(c, expdest);
341 if (flag & EXP_VARTILDE && *p == '~' &&
342 (c != '=' || firsteq)) {
343 if (c == '=')
344 firsteq = 0;
345 p = exptilde(p, flag);
347 break;
348 default:
349 if (split_lit && !lit_quoted &&
350 strchr(ifsset() ? ifsval() : " \t\n", c) != NULL) {
351 NEXTWORD(c, flag, expdest, dst);
352 break;
354 USTPUTC(c, expdest);
360 * Perform tilde expansion, placing the result in the stack string and
361 * returning the next position in the input string to process.
363 static const char *
364 exptilde(const char *p, int flag)
366 char c;
367 const char *startp = p;
368 const char *user;
369 struct passwd *pw;
370 char *home;
371 int len;
373 for (;;) {
374 c = *p;
375 switch(c) {
376 case CTLESC: /* This means CTL* are always considered quoted. */
377 case CTLVAR:
378 case CTLBACKQ:
379 case CTLBACKQ | CTLQUOTE:
380 case CTLARI:
381 case CTLENDARI:
382 case CTLQUOTEMARK:
383 return (startp);
384 case ':':
385 if ((flag & EXP_VARTILDE) == 0)
386 break;
387 /* FALLTHROUGH */
388 case '\0':
389 case '/':
390 case CTLENDVAR:
391 len = p - startp - 1;
392 STPUTBIN(startp + 1, len, expdest);
393 STACKSTRNUL(expdest);
394 user = expdest - len;
395 if (*user == '\0') {
396 home = lookupvar("HOME");
397 } else {
398 pw = getpwnam(user);
399 home = pw != NULL ? pw->pw_dir : NULL;
401 STADJUST(-len, expdest);
402 if (home == NULL || *home == '\0')
403 return (startp);
404 strtodest(home, flag, VSNORMAL, 1, NULL);
405 return (p);
407 p++;
413 * Expand arithmetic expression.
415 static const char *
416 expari(const char *p, struct nodelist **restrict argbackq, int flag,
417 struct worddest *dst)
419 char *q, *start;
420 arith_t result;
421 int begoff;
422 int quoted;
423 int adj;
425 quoted = *p++ == '"';
426 begoff = expdest - stackblock();
427 p = argstr(p, argbackq, 0, NULL);
428 STPUTC('\0', expdest);
429 start = stackblock() + begoff;
431 q = grabstackstr(expdest);
432 result = arith(start);
433 ungrabstackstr(q, expdest);
435 start = stackblock() + begoff;
436 adj = start - expdest;
437 STADJUST(adj, expdest);
439 CHECKSTRSPACE((int)(DIGITS(result) + 1), expdest);
440 fmtstr(expdest, DIGITS(result), ARITH_FORMAT_STR, result);
441 adj = strlen(expdest);
442 STADJUST(adj, expdest);
444 * If this is quoted, a '-' must not indicate a range in [...].
445 * If this is not quoted, splitting may occur.
447 if (quoted ?
448 result < 0 && begoff > 1 && flag & (EXP_GLOB | EXP_CASE) :
449 flag & EXP_SPLIT)
450 reprocess(expdest - adj - stackblock(), flag, VSNORMAL, quoted,
451 dst);
452 return p;
457 * Perform command substitution.
459 static void
460 expbackq(union node *cmd, int quoted, int flag, struct worddest *dst)
462 struct backcmd in;
463 int i;
464 char buf[128];
465 char *p;
466 char *dest = expdest;
467 char lastc;
468 char const *syntax = quoted? DQSYNTAX : BASESYNTAX;
469 int quotes = flag & (EXP_GLOB | EXP_CASE);
470 size_t nnl;
471 const char *ifs;
472 int startloc;
474 INTOFF;
475 p = grabstackstr(dest);
476 evalbackcmd(cmd, &in);
477 ungrabstackstr(p, dest);
479 p = in.buf;
480 startloc = dest - stackblock();
481 nnl = 0;
482 if (!quoted && flag & EXP_SPLIT)
483 ifs = ifsset() ? ifsval() : " \t\n";
484 else
485 ifs = "";
486 /* Remove trailing newlines */
487 for (;;) {
488 if (--in.nleft < 0) {
489 if (in.fd < 0)
490 break;
491 while ((i = read(in.fd, buf, sizeof buf)) < 0 && errno == EINTR)
493 TRACE(("expbackq: read returns %d\n", i));
494 if (i <= 0)
495 break;
496 p = buf;
497 in.nleft = i - 1;
499 lastc = *p++;
500 if (lastc == '\0')
501 continue;
502 if (nnl > 0 && lastc != '\n') {
503 NEXTWORD('\n', flag, dest, dst);
504 nnl = 0;
506 if (strchr(ifs, lastc) != NULL) {
507 if (lastc == '\n')
508 nnl++;
509 else
510 NEXTWORD(lastc, flag, dest, dst);
511 } else {
512 CHECKSTRSPACE(2, dest);
513 if (quotes && syntax[(int)lastc] == CCTL)
514 USTPUTC(CTLESC, dest);
515 USTPUTC(lastc, dest);
518 while (dest > stackblock() + startloc && STTOPC(dest) == '\n')
519 STUNPUTC(dest);
521 if (in.fd >= 0)
522 close(in.fd);
523 if (in.buf)
524 ckfree(in.buf);
525 if (in.jp) {
526 p = grabstackstr(dest);
527 exitstatus = waitforjob(in.jp, (int *)NULL);
528 ungrabstackstr(p, dest);
530 TRACE(("expbackq: done\n"));
531 expdest = dest;
532 INTON;
537 static void
538 recordleft(const char *str, const char *loc, char *startp)
540 int amount;
542 amount = ((str - 1) - (loc - startp)) - expdest;
543 STADJUST(amount, expdest);
544 while (loc != str - 1)
545 *startp++ = *loc++;
548 static const char *
549 subevalvar_trim(const char *p, struct nodelist **restrict argbackq, int strloc,
550 int subtype, int startloc)
552 char *startp;
553 char *loc = NULL;
554 char *str;
555 int c = 0;
556 int amount;
558 p = argstr(p, argbackq, EXP_CASE | EXP_TILDE, NULL);
559 STACKSTRNUL(expdest);
560 startp = stackblock() + startloc;
561 str = stackblock() + strloc;
563 switch (subtype) {
564 case VSTRIMLEFT:
565 for (loc = startp; loc < str; loc++) {
566 c = *loc;
567 *loc = '\0';
568 if (patmatch(str, startp)) {
569 *loc = c;
570 recordleft(str, loc, startp);
571 return p;
573 *loc = c;
575 break;
577 case VSTRIMLEFTMAX:
578 for (loc = str - 1; loc >= startp;) {
579 c = *loc;
580 *loc = '\0';
581 if (patmatch(str, startp)) {
582 *loc = c;
583 recordleft(str, loc, startp);
584 return p;
586 *loc = c;
587 loc--;
589 break;
591 case VSTRIMRIGHT:
592 for (loc = str - 1; loc >= startp;) {
593 if (patmatch(str, loc)) {
594 amount = loc - expdest;
595 STADJUST(amount, expdest);
596 return p;
598 loc--;
600 break;
602 case VSTRIMRIGHTMAX:
603 for (loc = startp; loc < str - 1; loc++) {
604 if (patmatch(str, loc)) {
605 amount = loc - expdest;
606 STADJUST(amount, expdest);
607 return p;
610 break;
613 default:
614 abort();
616 amount = (expdest - stackblock() - strloc) + 1;
617 STADJUST(-amount, expdest);
618 return p;
622 static const char *
623 subevalvar_misc(const char *p, struct nodelist **restrict argbackq,
624 const char *var, int subtype, int startloc, int varflags)
626 const char *end;
627 char *startp;
628 int amount;
630 end = argstr(p, argbackq, EXP_TILDE, NULL);
631 STACKSTRNUL(expdest);
632 startp = stackblock() + startloc;
634 switch (subtype) {
635 case VSASSIGN:
636 setvar(var, startp, 0);
637 amount = startp - expdest;
638 STADJUST(amount, expdest);
639 return end;
641 case VSQUESTION:
642 if (*p != CTLENDVAR) {
643 outfmt(out2, "%s\n", startp);
644 error((char *)NULL);
646 error("%.*s: parameter %snot set", (int)(p - var - 1),
647 var, (varflags & VSNUL) ? "null or " : "");
649 default:
650 abort();
656 * Expand a variable, and return a pointer to the next character in the
657 * input string.
660 static const char *
661 evalvar(const char *p, struct nodelist **restrict argbackq, int flag,
662 struct worddest *dst)
664 int subtype;
665 int varflags;
666 const char *var;
667 const char *val;
668 int patloc;
669 int c;
670 int set;
671 int special;
672 int startloc;
673 int varlen;
674 int varlenb;
675 char buf[21];
677 varflags = (unsigned char)*p++;
678 subtype = varflags & VSTYPE;
679 var = p;
680 special = 0;
681 if (! is_name(*p))
682 special = 1;
683 p = strchr(p, '=') + 1;
684 if (varflags & VSLINENO) {
685 set = 1;
686 special = 1;
687 val = NULL;
688 } else if (special) {
689 set = varisset(var, varflags & VSNUL);
690 val = NULL;
691 } else {
692 val = bltinlookup(var, 1);
693 if (val == NULL || ((varflags & VSNUL) && val[0] == '\0')) {
694 val = NULL;
695 set = 0;
696 } else
697 set = 1;
699 varlen = 0;
700 startloc = expdest - stackblock();
701 if (!set && uflag && *var != '@' && *var != '*') {
702 switch (subtype) {
703 case VSNORMAL:
704 case VSTRIMLEFT:
705 case VSTRIMLEFTMAX:
706 case VSTRIMRIGHT:
707 case VSTRIMRIGHTMAX:
708 case VSLENGTH:
709 error("%.*s: parameter not set", (int)(p - var - 1),
710 var);
713 if (set && subtype != VSPLUS) {
714 /* insert the value of the variable */
715 if (special) {
716 if (varflags & VSLINENO) {
717 if (p - var > (ptrdiff_t)sizeof(buf))
718 abort();
719 memcpy(buf, var, p - var - 1);
720 buf[p - var - 1] = '\0';
721 strtodest(buf, flag, subtype,
722 varflags & VSQUOTE, dst);
723 } else
724 varvalue(var, varflags & VSQUOTE, subtype, flag,
725 dst);
726 if (subtype == VSLENGTH) {
727 varlenb = expdest - stackblock() - startloc;
728 varlen = varlenb;
729 if (localeisutf8) {
730 val = stackblock() + startloc;
731 for (;val != expdest; val++)
732 if ((*val & 0xC0) == 0x80)
733 varlen--;
735 STADJUST(-varlenb, expdest);
737 } else {
738 if (subtype == VSLENGTH) {
739 for (;*val; val++)
740 if (!localeisutf8 ||
741 (*val & 0xC0) != 0x80)
742 varlen++;
744 else
745 strtodest(val, flag, subtype,
746 varflags & VSQUOTE, dst);
750 if (subtype == VSPLUS)
751 set = ! set;
753 switch (subtype) {
754 case VSLENGTH:
755 cvtnum(varlen, buf);
756 strtodest(buf, flag, VSNORMAL, varflags & VSQUOTE, dst);
757 break;
759 case VSNORMAL:
760 return p;
762 case VSPLUS:
763 case VSMINUS:
764 if (!set) {
765 return argstr(p, argbackq,
766 flag | (flag & EXP_SPLIT ? EXP_SPLIT_LIT : 0) |
767 (varflags & VSQUOTE ? EXP_LIT_QUOTED : 0), dst);
769 break;
771 case VSTRIMLEFT:
772 case VSTRIMLEFTMAX:
773 case VSTRIMRIGHT:
774 case VSTRIMRIGHTMAX:
775 if (!set)
776 break;
778 * Terminate the string and start recording the pattern
779 * right after it
781 STPUTC('\0', expdest);
782 patloc = expdest - stackblock();
783 p = subevalvar_trim(p, argbackq, patloc, subtype, startloc);
784 reprocess(startloc, flag, VSNORMAL, varflags & VSQUOTE, dst);
785 if (flag & EXP_SPLIT && *var == '@' && varflags & VSQUOTE)
786 dst->state = WORD_QUOTEMARK;
787 return p;
789 case VSASSIGN:
790 case VSQUESTION:
791 if (!set) {
792 p = subevalvar_misc(p, argbackq, var, subtype,
793 startloc, varflags);
794 /* assert(subtype == VSASSIGN); */
795 val = lookupvar(var);
796 strtodest(val, flag, subtype, varflags & VSQUOTE, dst);
797 return p;
799 break;
801 case VSERROR:
802 c = p - var - 1;
803 error("${%.*s%s}: Bad substitution", c, var,
804 (c > 0 && *p != CTLENDVAR) ? "..." : "");
806 default:
807 abort();
810 { /* skip to end of alternative */
811 int nesting = 1;
812 for (;;) {
813 if ((c = *p++) == CTLESC)
814 p++;
815 else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE))
816 *argbackq = (*argbackq)->next;
817 else if (c == CTLVAR) {
818 if ((*p++ & VSTYPE) != VSNORMAL)
819 nesting++;
820 } else if (c == CTLENDVAR) {
821 if (--nesting == 0)
822 break;
826 return p;
832 * Test whether a special or positional parameter is set.
835 static int
836 varisset(const char *name, int nulok)
839 if (*name == '!')
840 return backgndpidset();
841 else if (*name == '@' || *name == '*') {
842 if (*shellparam.p == NULL)
843 return 0;
845 if (nulok) {
846 char **av;
848 for (av = shellparam.p; *av; av++)
849 if (**av != '\0')
850 return 1;
851 return 0;
853 } else if (is_digit(*name)) {
854 char *ap;
855 long num;
857 errno = 0;
858 num = strtol(name, NULL, 10);
859 if (errno != 0 || num > shellparam.nparam)
860 return 0;
862 if (num == 0)
863 ap = arg0;
864 else
865 ap = shellparam.p[num - 1];
867 if (nulok && (ap == NULL || *ap == '\0'))
868 return 0;
870 return 1;
873 static void
874 strtodest(const char *p, int flag, int subtype, int quoted,
875 struct worddest *dst)
877 if (subtype == VSLENGTH || subtype == VSTRIMLEFT ||
878 subtype == VSTRIMLEFTMAX || subtype == VSTRIMRIGHT ||
879 subtype == VSTRIMRIGHTMAX)
880 STPUTS(p, expdest);
881 else if (flag & EXP_SPLIT && !quoted && dst != NULL)
882 STPUTS_SPLIT(p, BASESYNTAX, flag, expdest, dst);
883 else if (flag & (EXP_GLOB | EXP_CASE))
884 STPUTS_QUOTES(p, quoted ? DQSYNTAX : BASESYNTAX, expdest);
885 else
886 STPUTS(p, expdest);
889 static void
890 reprocess(int startloc, int flag, int subtype, int quoted,
891 struct worddest *dst)
893 static char *buf = NULL;
894 static size_t buflen = 0;
895 char *startp;
896 size_t len, zpos, zlen;
898 startp = stackblock() + startloc;
899 len = expdest - startp;
900 if (len >= SIZE_MAX / 2 || len > PTRDIFF_MAX)
901 abort();
902 INTOFF;
903 if (len >= buflen) {
904 ckfree(buf);
905 buf = NULL;
907 if (buflen < 128)
908 buflen = 128;
909 while (len >= buflen)
910 buflen <<= 1;
911 if (buf == NULL)
912 buf = ckmalloc(buflen);
913 INTON;
914 memcpy(buf, startp, len);
915 buf[len] = '\0';
916 STADJUST(-(ptrdiff_t)len, expdest);
917 for (zpos = 0;;) {
918 zlen = strlen(buf + zpos);
919 strtodest(buf + zpos, flag, subtype, quoted, dst);
920 zpos += zlen + 1;
921 if (zpos == len + 1)
922 break;
923 if (flag & EXP_SPLIT && (quoted || (zlen > 0 && zpos < len)))
924 NEXTWORD('\0', flag, expdest, dst);
929 * Add the value of a special or positional parameter to the stack string.
932 static void
933 varvalue(const char *name, int quoted, int subtype, int flag,
934 struct worddest *dst)
936 int num;
937 char *p;
938 int i;
939 int splitlater;
940 char sep[2];
941 char **ap;
942 char buf[(NSHORTOPTS > 10 ? NSHORTOPTS : 10) + 1];
944 if (subtype == VSLENGTH)
945 flag &= ~EXP_FULL;
946 splitlater = subtype == VSTRIMLEFT || subtype == VSTRIMLEFTMAX ||
947 subtype == VSTRIMRIGHT || subtype == VSTRIMRIGHTMAX;
949 switch (*name) {
950 case '$':
951 num = rootpid;
952 break;
953 case '?':
954 num = oexitstatus;
955 break;
956 case '#':
957 num = shellparam.nparam;
958 break;
959 case '!':
960 num = backgndpidval();
961 break;
962 case '-':
963 p = buf;
964 for (i = 0 ; i < NSHORTOPTS ; i++) {
965 if (optval[i])
966 *p++ = optletter[i];
968 *p = '\0';
969 strtodest(buf, flag, subtype, quoted, dst);
970 return;
971 case '@':
972 if (flag & EXP_SPLIT && quoted) {
973 for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
974 strtodest(p, flag, subtype, quoted, dst);
975 if (*ap) {
976 if (splitlater)
977 STPUTC('\0', expdest);
978 else
979 NEXTWORD('\0', flag, expdest,
980 dst);
983 if (shellparam.nparam > 0)
984 dst->state = WORD_QUOTEMARK;
985 return;
987 /* FALLTHROUGH */
988 case '*':
989 if (ifsset())
990 sep[0] = ifsval()[0];
991 else
992 sep[0] = ' ';
993 sep[1] = '\0';
994 for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
995 strtodest(p, flag, subtype, quoted, dst);
996 if (!*ap)
997 break;
998 if (sep[0])
999 strtodest(sep, flag, subtype, quoted, dst);
1000 else if (flag & EXP_SPLIT && !quoted && **ap != '\0') {
1001 if (splitlater)
1002 STPUTC('\0', expdest);
1003 else
1004 NEXTWORD('\0', flag, expdest, dst);
1007 return;
1008 default:
1009 if (is_digit(*name)) {
1010 num = atoi(name);
1011 if (num == 0)
1012 p = arg0;
1013 else if (num > 0 && num <= shellparam.nparam)
1014 p = shellparam.p[num - 1];
1015 else
1016 return;
1017 strtodest(p, flag, subtype, quoted, dst);
1019 return;
1021 cvtnum(num, buf);
1022 strtodest(buf, flag, subtype, quoted, dst);
1027 static char expdir[PATH_MAX];
1028 #define expdir_end (expdir + sizeof(expdir))
1031 * Perform pathname generation and remove control characters.
1032 * At this point, the only control characters should be CTLESC.
1033 * The results are stored in the list dstlist.
1035 static void
1036 expandmeta(char *pattern, struct arglist *dstlist)
1038 char *p;
1039 int firstmatch;
1040 char c;
1042 firstmatch = dstlist->count;
1043 p = pattern;
1044 for (; (c = *p) != '\0'; p++) {
1045 /* fast check for meta chars */
1046 if (c == '*' || c == '?' || c == '[') {
1047 INTOFF;
1048 expmeta(expdir, pattern, dstlist);
1049 INTON;
1050 break;
1053 if (dstlist->count == firstmatch) {
1055 * no matches
1057 rmescapes(pattern);
1058 appendarglist(dstlist, pattern);
1059 } else {
1060 qsort(&dstlist->args[firstmatch],
1061 dstlist->count - firstmatch,
1062 sizeof(dstlist->args[0]), expsortcmp);
1068 * Do metacharacter (i.e. *, ?, [...]) expansion.
1071 static void
1072 expmeta(char *enddir, char *name, struct arglist *arglist)
1074 const char *p;
1075 const char *q;
1076 const char *start;
1077 char *endname;
1078 int metaflag;
1079 struct stat statb;
1080 DIR *dirp;
1081 struct dirent *dp;
1082 int atend;
1083 int matchdot;
1084 int esc;
1085 int namlen;
1087 metaflag = 0;
1088 start = name;
1089 for (p = name; esc = 0, *p; p += esc + 1) {
1090 if (*p == '*' || *p == '?')
1091 metaflag = 1;
1092 else if (*p == '[') {
1093 q = p + 1;
1094 if (*q == '!' || *q == '^')
1095 q++;
1096 for (;;) {
1097 if (*q == CTLESC)
1098 q++;
1099 if (*q == '/' || *q == '\0')
1100 break;
1101 if (*++q == ']') {
1102 metaflag = 1;
1103 break;
1106 } else if (*p == '\0')
1107 break;
1108 else {
1109 if (*p == CTLESC)
1110 esc++;
1111 if (p[esc] == '/') {
1112 if (metaflag)
1113 break;
1114 start = p + esc + 1;
1118 if (metaflag == 0) { /* we've reached the end of the file name */
1119 if (enddir != expdir)
1120 metaflag++;
1121 for (p = name ; ; p++) {
1122 if (*p == CTLESC)
1123 p++;
1124 *enddir++ = *p;
1125 if (*p == '\0')
1126 break;
1127 if (enddir == expdir_end)
1128 return;
1130 if (metaflag == 0 || lstat(expdir, &statb) >= 0)
1131 appendarglist(arglist, stsavestr(expdir));
1132 return;
1134 endname = name + (p - name);
1135 if (start != name) {
1136 p = name;
1137 while (p < start) {
1138 if (*p == CTLESC)
1139 p++;
1140 *enddir++ = *p++;
1141 if (enddir == expdir_end)
1142 return;
1145 if (enddir == expdir) {
1146 p = ".";
1147 } else if (enddir == expdir + 1 && *expdir == '/') {
1148 p = "/";
1149 } else {
1150 p = expdir;
1151 enddir[-1] = '\0';
1153 if ((dirp = opendir(p)) == NULL)
1154 return;
1155 if (enddir != expdir)
1156 enddir[-1] = '/';
1157 if (*endname == 0) {
1158 atend = 1;
1159 } else {
1160 atend = 0;
1161 *endname = '\0';
1162 endname += esc + 1;
1164 matchdot = 0;
1165 p = start;
1166 if (*p == CTLESC)
1167 p++;
1168 if (*p == '.')
1169 matchdot++;
1170 while (! int_pending() && (dp = readdir(dirp)) != NULL) {
1171 if (dp->d_name[0] == '.' && ! matchdot)
1172 continue;
1173 if (patmatch(start, dp->d_name)) {
1174 #ifdef _DIRENT_HAVE_D_NAMLEN
1175 namlen = dp->d_namlen;
1176 #else
1177 namlen = strlen(dp->d_name);
1178 #endif
1179 if (enddir + namlen + 1 > expdir_end)
1180 continue;
1181 memcpy(enddir, dp->d_name, namlen + 1);
1182 if (atend)
1183 appendarglist(arglist, stsavestr(expdir));
1184 else {
1185 if (dp->d_type != DT_UNKNOWN &&
1186 dp->d_type != DT_DIR &&
1187 dp->d_type != DT_LNK)
1188 continue;
1189 if (enddir + namlen + 2 > expdir_end)
1190 continue;
1191 enddir[namlen] = '/';
1192 enddir[namlen + 1] = '\0';
1193 expmeta(enddir + namlen + 1, endname, arglist);
1197 closedir(dirp);
1198 if (! atend)
1199 endname[-esc - 1] = esc ? CTLESC : '/';
1203 static int
1204 expsortcmp(const void *p1, const void *p2)
1206 const char *s1 = *(const char * const *)p1;
1207 const char *s2 = *(const char * const *)p2;
1209 return (strcoll(s1, s2));
1214 static wchar_t
1215 get_wc(const char **p)
1217 wchar_t c;
1218 int chrlen;
1220 chrlen = mbtowc(&c, *p, 4);
1221 if (chrlen == 0)
1222 return 0;
1223 else if (chrlen == -1)
1224 c = 0;
1225 else
1226 *p += chrlen;
1227 return c;
1232 * See if a character matches a character class, starting at the first colon
1233 * of "[:class:]".
1234 * If a valid character class is recognized, a pointer to the next character
1235 * after the final closing bracket is stored into *end, otherwise a null
1236 * pointer is stored into *end.
1238 static int
1239 match_charclass(const char *p, wchar_t chr, const char **end)
1241 char name[20];
1242 const char *nameend;
1243 wctype_t cclass;
1245 *end = NULL;
1246 p++;
1247 nameend = strstr(p, ":]");
1248 if (nameend == NULL || (size_t)(nameend - p) >= sizeof(name) ||
1249 nameend == p)
1250 return 0;
1251 memcpy(name, p, nameend - p);
1252 name[nameend - p] = '\0';
1253 *end = nameend + 2;
1254 cclass = wctype(name);
1255 /* An unknown class matches nothing but is valid nevertheless. */
1256 if (cclass == 0)
1257 return 0;
1258 return iswctype(chr, cclass);
1263 * Returns true if the pattern matches the string.
1266 static int
1267 patmatch(const char *pattern, const char *string)
1269 const char *p, *q, *end;
1270 const char *bt_p, *bt_q;
1271 char c;
1272 wchar_t wc, wc2;
1274 p = pattern;
1275 q = string;
1276 bt_p = NULL;
1277 bt_q = NULL;
1278 for (;;) {
1279 switch (c = *p++) {
1280 case '\0':
1281 if (*q != '\0')
1282 goto backtrack;
1283 return 1;
1284 case CTLESC:
1285 if (*q++ != *p++)
1286 goto backtrack;
1287 break;
1288 case '?':
1289 if (*q == '\0')
1290 return 0;
1291 if (localeisutf8) {
1292 wc = get_wc(&q);
1294 * A '?' does not match invalid UTF-8 but a
1295 * '*' does, so backtrack.
1297 if (wc == 0)
1298 goto backtrack;
1299 } else
1300 q++;
1301 break;
1302 case '*':
1303 c = *p;
1304 while (c == '*')
1305 c = *++p;
1307 * If the pattern ends here, we know the string
1308 * matches without needing to look at the rest of it.
1310 if (c == '\0')
1311 return 1;
1313 * First try the shortest match for the '*' that
1314 * could work. We can forget any earlier '*' since
1315 * there is no way having it match more characters
1316 * can help us, given that we are already here.
1318 bt_p = p;
1319 bt_q = q;
1320 break;
1321 case '[': {
1322 const char *savep, *saveq;
1323 int invert, found;
1324 wchar_t chr;
1326 savep = p, saveq = q;
1327 invert = 0;
1328 if (*p == '!' || *p == '^') {
1329 invert++;
1330 p++;
1332 found = 0;
1333 if (*q == '\0')
1334 return 0;
1335 if (localeisutf8) {
1336 chr = get_wc(&q);
1337 if (chr == 0)
1338 goto backtrack;
1339 } else
1340 chr = (unsigned char)*q++;
1341 c = *p++;
1342 do {
1343 if (c == '\0') {
1344 p = savep, q = saveq;
1345 c = '[';
1346 goto dft;
1348 if (c == '[' && *p == ':') {
1349 found |= match_charclass(p, chr, &end);
1350 if (end != NULL) {
1351 p = end;
1352 continue;
1355 if (c == CTLESC)
1356 c = *p++;
1357 if (localeisutf8 && c & 0x80) {
1358 p--;
1359 wc = get_wc(&p);
1360 if (wc == 0) /* bad utf-8 */
1361 return 0;
1362 } else
1363 wc = (unsigned char)c;
1364 if (*p == '-' && p[1] != ']') {
1365 p++;
1366 if (*p == CTLESC)
1367 p++;
1368 if (localeisutf8) {
1369 wc2 = get_wc(&p);
1370 if (wc2 == 0) /* bad utf-8 */
1371 return 0;
1372 } else
1373 wc2 = (unsigned char)*p++;
1374 if ( collate_range_cmp(chr, wc) >= 0
1375 && collate_range_cmp(chr, wc2) <= 0
1377 found = 1;
1378 } else {
1379 if (chr == wc)
1380 found = 1;
1382 } while ((c = *p++) != ']');
1383 if (found == invert)
1384 goto backtrack;
1385 break;
1387 dft: default:
1388 if (*q == '\0')
1389 return 0;
1390 if (*q++ == c)
1391 break;
1392 backtrack:
1394 * If we have a mismatch (other than hitting the end
1395 * of the string), go back to the last '*' seen and
1396 * have it match one additional character.
1398 if (bt_p == NULL)
1399 return 0;
1400 if (*bt_q == '\0')
1401 return 0;
1402 bt_q++;
1403 p = bt_p;
1404 q = bt_q;
1405 break;
1413 * Remove any CTLESC and CTLQUOTEMARK characters from a string.
1416 void
1417 rmescapes(char *str)
1419 char *p, *q;
1421 p = str;
1422 while (*p != CTLESC && *p != CTLQUOTEMARK && *p != CTLQUOTEEND) {
1423 if (*p++ == '\0')
1424 return;
1426 q = p;
1427 while (*p) {
1428 if (*p == CTLQUOTEMARK || *p == CTLQUOTEEND) {
1429 p++;
1430 continue;
1432 if (*p == CTLESC)
1433 p++;
1434 *q++ = *p++;
1436 *q = '\0';
1442 * See if a pattern matches in a case statement.
1446 casematch(union node *pattern, const char *val)
1448 struct stackmark smark;
1449 struct nodelist *argbackq;
1450 int result;
1451 char *p;
1453 setstackmark(&smark);
1454 argbackq = pattern->narg.backquote;
1455 STARTSTACKSTR(expdest);
1456 argstr(pattern->narg.text, &argbackq, EXP_TILDE | EXP_CASE, NULL);
1457 STPUTC('\0', expdest);
1458 p = grabstackstr(expdest);
1459 result = patmatch(p, val);
1460 popstackmark(&smark);
1461 return result;
1465 * Our own itoa().
1468 static void
1469 cvtnum(int num, char *buf)
1471 char temp[32];
1472 int neg = num < 0;
1473 char *p = temp + 31;
1475 temp[31] = '\0';
1477 do {
1478 *--p = num % 10 + '0';
1479 } while ((num /= 10) != 0);
1481 if (neg)
1482 *--p = '-';
1484 memcpy(buf, p, temp + 32 - p);
1488 * Do most of the work for wordexp(3).
1492 wordexpcmd(int argc, char **argv)
1494 size_t len;
1495 int i;
1497 out1fmt("%08x", argc - 1);
1498 for (i = 1, len = 0; i < argc; i++)
1499 len += strlen(argv[i]);
1500 out1fmt("%08x", (int)len);
1501 for (i = 1; i < argc; i++)
1502 outbin(argv[i], strlen(argv[i]) + 1, out1);
1503 return (0);
1507 * Do most of the work for wordexp(3), new version.
1511 wordexp2cmd(int argc __unused, char **argv __unused)
1513 struct arglist arglist;
1514 union node *args, *n;
1515 size_t len;
1516 int ch;
1517 int protected = 0;
1518 int fd = -1;
1519 int i;
1521 while ((ch = nextopt("f:p")) != '\0') {
1522 switch (ch) {
1523 case 'f':
1524 fd = number(shoptarg);
1525 break;
1526 case 'p':
1527 protected = 1;
1528 break;
1531 if (*argptr != NULL)
1532 error("wrong number of arguments");
1533 if (fd < 0)
1534 error("missing fd");
1535 INTOFF;
1536 setinputfd(fd, 1);
1537 INTON;
1538 args = parsewordexp();
1539 popfile(); /* will also close fd */
1540 if (protected)
1541 for (n = args; n != NULL; n = n->narg.next) {
1542 if (n->narg.backquote != NULL) {
1543 outcslow('C', out1);
1544 error("command substitution disabled");
1547 outcslow(' ', out1);
1548 emptyarglist(&arglist);
1549 for (n = args; n != NULL; n = n->narg.next)
1550 expandarg(n, &arglist, EXP_FULL | EXP_TILDE);
1551 for (i = 0, len = 0; i < arglist.count; i++)
1552 len += strlen(arglist.args[i]);
1553 out1fmt("%016x %016zx", arglist.count, len);
1554 for (i = 0; i < arglist.count; i++)
1555 outbin(arglist.args[i], strlen(arglist.args[i]) + 1, out1);
1556 return (0);