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