VirtualBox

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

Last change on this file since 3456 was 3456, checked in by bird, 4 years ago

kash: parser.c,memalloc.c/.h,expand.c: Prepared the parser for using a separate allocator.

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