VirtualBox

source: kBuild/trunk/src/kash/expand.c@ 2653

Last change on this file since 2653 was 2653, checked in by bird, 12 years ago

kash: fixed shfile_opendir on windows, and thereby argument expansion.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette