source: trunk/src/kash/expand.c@ 2304

Last change on this file since 2304 was 2304, checked in by bird, 16 years ago

kash: CRLF hacking, tests run cleanly on windows now!

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