10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
22*527Schin
23*527Schin /*
24*527Schin * Copyright 1995 Sun Microsystems, Inc. All rights reserved.
25*527Schin * Use is subject to license terms.
26*527Schin */
27*527Schin
280Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
290Sstevel@tonic-gate /* All Rights Reserved */
300Sstevel@tonic-gate
31*527Schin #pragma ident "%Z%%M% %I% %E% SMI"
320Sstevel@tonic-gate
330Sstevel@tonic-gate /*
340Sstevel@tonic-gate * UNIX shell
350Sstevel@tonic-gate */
360Sstevel@tonic-gate
370Sstevel@tonic-gate #include "defs.h"
380Sstevel@tonic-gate
390Sstevel@tonic-gate static struct dolnod *copyargs();
40*527Schin static void freedolh(void);
410Sstevel@tonic-gate extern struct dolnod *freeargs();
420Sstevel@tonic-gate static struct dolnod *dolh;
430Sstevel@tonic-gate
440Sstevel@tonic-gate /* Used to save outermost positional parameters */
450Sstevel@tonic-gate static struct dolnod *globdolh;
460Sstevel@tonic-gate static unsigned char **globdolv;
470Sstevel@tonic-gate static int globdolc;
480Sstevel@tonic-gate
490Sstevel@tonic-gate unsigned char flagadr[16];
500Sstevel@tonic-gate
510Sstevel@tonic-gate unsigned char flagchar[] =
520Sstevel@tonic-gate {
530Sstevel@tonic-gate 'x',
540Sstevel@tonic-gate 'n',
550Sstevel@tonic-gate 'v',
560Sstevel@tonic-gate 't',
570Sstevel@tonic-gate STDFLG,
580Sstevel@tonic-gate 'i',
590Sstevel@tonic-gate 'e',
600Sstevel@tonic-gate 'r',
610Sstevel@tonic-gate 'k',
620Sstevel@tonic-gate 'u',
630Sstevel@tonic-gate 'h',
640Sstevel@tonic-gate 'f',
650Sstevel@tonic-gate 'a',
660Sstevel@tonic-gate 'm',
670Sstevel@tonic-gate 'p',
680Sstevel@tonic-gate 0
690Sstevel@tonic-gate };
700Sstevel@tonic-gate
710Sstevel@tonic-gate long flagval[] =
720Sstevel@tonic-gate {
730Sstevel@tonic-gate execpr,
740Sstevel@tonic-gate noexec,
750Sstevel@tonic-gate readpr,
760Sstevel@tonic-gate oneflg,
770Sstevel@tonic-gate stdflg,
780Sstevel@tonic-gate intflg,
790Sstevel@tonic-gate errflg,
800Sstevel@tonic-gate rshflg,
810Sstevel@tonic-gate keyflg,
820Sstevel@tonic-gate setflg,
830Sstevel@tonic-gate hashflg,
840Sstevel@tonic-gate nofngflg,
850Sstevel@tonic-gate exportflg,
860Sstevel@tonic-gate monitorflg,
870Sstevel@tonic-gate privflg,
880Sstevel@tonic-gate 0
890Sstevel@tonic-gate };
900Sstevel@tonic-gate
910Sstevel@tonic-gate /* ======== option handling ======== */
920Sstevel@tonic-gate
930Sstevel@tonic-gate
94*527Schin int
options(int argc,unsigned char ** argv)95*527Schin options(int argc, unsigned char **argv)
960Sstevel@tonic-gate {
97*527Schin unsigned char *cp;
98*527Schin unsigned char **argp = argv;
99*527Schin unsigned char *flagc;
1000Sstevel@tonic-gate unsigned char *flagp;
1010Sstevel@tonic-gate int len;
1020Sstevel@tonic-gate wchar_t wc;
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate if (argc > 1 && *argp[1] == '-')
1050Sstevel@tonic-gate {
1060Sstevel@tonic-gate /*
1070Sstevel@tonic-gate * if first argument is "--" then options are not
1080Sstevel@tonic-gate * to be changed. Fix for problems getting
1090Sstevel@tonic-gate * $1 starting with a "-"
1100Sstevel@tonic-gate */
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate cp = argp[1];
1130Sstevel@tonic-gate if (cp[1] == '-')
1140Sstevel@tonic-gate {
1150Sstevel@tonic-gate argp[1] = argp[0];
1160Sstevel@tonic-gate argc--;
1170Sstevel@tonic-gate return(argc);
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate if (cp[1] == '\0')
1200Sstevel@tonic-gate flags &= ~(execpr|readpr);
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate /*
1230Sstevel@tonic-gate * Step along 'flagchar[]' looking for matches.
1240Sstevel@tonic-gate * 'sicrp' are not legal with 'set' command.
1250Sstevel@tonic-gate */
1260Sstevel@tonic-gate cp++;
1270Sstevel@tonic-gate while (*cp) {
1280Sstevel@tonic-gate if ((len = mbtowc(&wc, (char *)cp, MB_LEN_MAX)) <= 0) {
1290Sstevel@tonic-gate len = 1;
1300Sstevel@tonic-gate wc = (unsigned char)*cp;
1310Sstevel@tonic-gate failed(argv[1],badopt);
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate cp += len;
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate flagc = flagchar;
1360Sstevel@tonic-gate while (*flagc && wc != *flagc)
1370Sstevel@tonic-gate flagc++;
1380Sstevel@tonic-gate if (wc == *flagc)
1390Sstevel@tonic-gate {
1400Sstevel@tonic-gate if (eq(argv[0], "set") && any(wc, "sicrp"))
1410Sstevel@tonic-gate failed(argv[1], badopt);
1420Sstevel@tonic-gate else
1430Sstevel@tonic-gate {
1440Sstevel@tonic-gate flags |= flagval[flagc-flagchar];
1450Sstevel@tonic-gate if (flags & errflg)
1460Sstevel@tonic-gate eflag = errflg;
1470Sstevel@tonic-gate }
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate else if (wc == 'c' && argc > 2 && comdiv == 0)
1500Sstevel@tonic-gate {
1510Sstevel@tonic-gate comdiv = argp[2];
1520Sstevel@tonic-gate argp[1] = argp[0];
1530Sstevel@tonic-gate argp++;
1540Sstevel@tonic-gate argc--;
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate else
1570Sstevel@tonic-gate failed(argv[1],badopt);
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate argp[1] = argp[0];
1600Sstevel@tonic-gate argc--;
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate else if (argc > 1 && *argp[1] == '+') /* unset flags x, k, t, n, v, e, u */
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate cp = argp[1];
1650Sstevel@tonic-gate cp++;
1660Sstevel@tonic-gate while (*cp)
1670Sstevel@tonic-gate {
1680Sstevel@tonic-gate if ((len = mbtowc(&wc, (char *)cp, MB_LEN_MAX)) <= 0) {
1690Sstevel@tonic-gate cp++;
1700Sstevel@tonic-gate continue;
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate flagc = flagchar;
1740Sstevel@tonic-gate while (*flagc && wc != *flagc)
1750Sstevel@tonic-gate flagc++;
1760Sstevel@tonic-gate /*
1770Sstevel@tonic-gate * step through flags
1780Sstevel@tonic-gate */
1790Sstevel@tonic-gate if (!any(wc, "sicrp") && wc == *flagc) {
1800Sstevel@tonic-gate flags &= ~(flagval[flagc-flagchar]);
1810Sstevel@tonic-gate if (wc == 'e')
1820Sstevel@tonic-gate eflag = 0;
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate cp += len;
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate argp[1] = argp[0];
1870Sstevel@tonic-gate argc--;
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate /*
1900Sstevel@tonic-gate * set up $-
1910Sstevel@tonic-gate */
1920Sstevel@tonic-gate flagp = flagadr;
1930Sstevel@tonic-gate if (flags)
1940Sstevel@tonic-gate {
1950Sstevel@tonic-gate flagc = flagchar;
1960Sstevel@tonic-gate while (*flagc)
1970Sstevel@tonic-gate {
1980Sstevel@tonic-gate if (flags & flagval[flagc-flagchar])
1990Sstevel@tonic-gate *flagp++ = *flagc;
2000Sstevel@tonic-gate flagc++;
2010Sstevel@tonic-gate }
2020Sstevel@tonic-gate }
2030Sstevel@tonic-gate *flagp = 0;
2040Sstevel@tonic-gate return(argc);
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate /*
2080Sstevel@tonic-gate * sets up positional parameters
2090Sstevel@tonic-gate */
210*527Schin void
setargs(unsigned char * argi[])211*527Schin setargs(unsigned char *argi[])
2120Sstevel@tonic-gate {
213*527Schin unsigned char **argp = argi; /* count args */
214*527Schin int argn = 0;
2150Sstevel@tonic-gate
2160Sstevel@tonic-gate while (*argp++ != (unsigned char *)ENDARGS)
2170Sstevel@tonic-gate argn++;
2180Sstevel@tonic-gate /*
2190Sstevel@tonic-gate * free old ones unless on for loop chain
2200Sstevel@tonic-gate */
2210Sstevel@tonic-gate freedolh();
2220Sstevel@tonic-gate dolh = copyargs(argi, argn);
2230Sstevel@tonic-gate dolc = argn - 1;
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate
2260Sstevel@tonic-gate
227*527Schin static void
freedolh(void)228*527Schin freedolh(void)
2290Sstevel@tonic-gate {
230*527Schin unsigned char **argp;
231*527Schin struct dolnod *argblk;
2320Sstevel@tonic-gate
2330Sstevel@tonic-gate if (argblk = dolh)
2340Sstevel@tonic-gate {
2350Sstevel@tonic-gate if ((--argblk->doluse) == 0)
2360Sstevel@tonic-gate {
2370Sstevel@tonic-gate for (argp = argblk->dolarg; *argp != (unsigned char *)ENDARGS; argp++)
2380Sstevel@tonic-gate free(*argp);
2390Sstevel@tonic-gate free(argblk->dolarg);
2400Sstevel@tonic-gate free(argblk);
2410Sstevel@tonic-gate }
2420Sstevel@tonic-gate }
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate struct dolnod *
freeargs(blk)2460Sstevel@tonic-gate freeargs(blk)
2470Sstevel@tonic-gate struct dolnod *blk;
2480Sstevel@tonic-gate {
249*527Schin unsigned char **argp;
250*527Schin struct dolnod *argr = 0;
251*527Schin struct dolnod *argblk;
2520Sstevel@tonic-gate int cnt;
2530Sstevel@tonic-gate
2540Sstevel@tonic-gate if (argblk = blk)
2550Sstevel@tonic-gate {
2560Sstevel@tonic-gate argr = argblk->dolnxt;
2570Sstevel@tonic-gate cnt = --argblk->doluse;
2580Sstevel@tonic-gate
2590Sstevel@tonic-gate if (argblk == dolh)
2600Sstevel@tonic-gate {
2610Sstevel@tonic-gate if (cnt == 1)
2620Sstevel@tonic-gate return(argr);
2630Sstevel@tonic-gate else
2640Sstevel@tonic-gate return(argblk);
2650Sstevel@tonic-gate }
2660Sstevel@tonic-gate else
2670Sstevel@tonic-gate {
2680Sstevel@tonic-gate if (cnt == 0)
2690Sstevel@tonic-gate {
2700Sstevel@tonic-gate for (argp = argblk->dolarg; *argp != (unsigned char *)ENDARGS; argp++)
2710Sstevel@tonic-gate free(*argp);
2720Sstevel@tonic-gate free(argblk->dolarg);
2730Sstevel@tonic-gate free(argblk);
2740Sstevel@tonic-gate }
2750Sstevel@tonic-gate }
2760Sstevel@tonic-gate }
2770Sstevel@tonic-gate return(argr);
2780Sstevel@tonic-gate }
2790Sstevel@tonic-gate
2800Sstevel@tonic-gate static struct dolnod *
copyargs(from,n)2810Sstevel@tonic-gate copyargs(from, n)
2820Sstevel@tonic-gate unsigned char *from[];
2830Sstevel@tonic-gate {
284*527Schin struct dolnod *np = (struct dolnod *)alloc(sizeof (struct dolnod));
285*527Schin unsigned char **fp = from;
286*527Schin unsigned char **pp;
2870Sstevel@tonic-gate
2880Sstevel@tonic-gate np -> dolnxt = 0;
2890Sstevel@tonic-gate np->doluse = 1; /* use count */
2900Sstevel@tonic-gate pp = np->dolarg = (unsigned char **)alloc((n+1)*sizeof(char *));
2910Sstevel@tonic-gate dolv = pp;
2920Sstevel@tonic-gate
2930Sstevel@tonic-gate while (n--)
2940Sstevel@tonic-gate *pp++ = make(*fp++);
2950Sstevel@tonic-gate *pp++ = ENDARGS;
2960Sstevel@tonic-gate return(np);
2970Sstevel@tonic-gate }
2980Sstevel@tonic-gate
2990Sstevel@tonic-gate
3000Sstevel@tonic-gate struct dolnod *
clean_args(blk)3010Sstevel@tonic-gate clean_args(blk)
3020Sstevel@tonic-gate struct dolnod *blk;
3030Sstevel@tonic-gate {
304*527Schin unsigned char **argp;
305*527Schin struct dolnod *argr = 0;
306*527Schin struct dolnod *argblk;
3070Sstevel@tonic-gate
3080Sstevel@tonic-gate if (argblk = blk)
3090Sstevel@tonic-gate {
3100Sstevel@tonic-gate argr = argblk->dolnxt;
3110Sstevel@tonic-gate
3120Sstevel@tonic-gate if (argblk == dolh)
3130Sstevel@tonic-gate argblk->doluse = 1;
3140Sstevel@tonic-gate else
3150Sstevel@tonic-gate {
3160Sstevel@tonic-gate for (argp = argblk->dolarg; *argp != (unsigned char *)ENDARGS; argp++)
3170Sstevel@tonic-gate free(*argp);
3180Sstevel@tonic-gate free(argblk->dolarg);
3190Sstevel@tonic-gate free(argblk);
3200Sstevel@tonic-gate }
3210Sstevel@tonic-gate }
3220Sstevel@tonic-gate return(argr);
3230Sstevel@tonic-gate }
3240Sstevel@tonic-gate
325*527Schin void
clearup(void)326*527Schin clearup(void)
3270Sstevel@tonic-gate {
3280Sstevel@tonic-gate /*
3290Sstevel@tonic-gate * force `for' $* lists to go away
3300Sstevel@tonic-gate */
3310Sstevel@tonic-gate if(globdolv)
3320Sstevel@tonic-gate dolv = globdolv;
3330Sstevel@tonic-gate if(globdolc)
3340Sstevel@tonic-gate dolc = globdolc;
3350Sstevel@tonic-gate if(globdolh)
3360Sstevel@tonic-gate dolh = globdolh;
3370Sstevel@tonic-gate globdolv = 0;
3380Sstevel@tonic-gate globdolc = 0;
3390Sstevel@tonic-gate globdolh = 0;
3400Sstevel@tonic-gate while (argfor = clean_args(argfor))
3410Sstevel@tonic-gate ;
3420Sstevel@tonic-gate /*
3430Sstevel@tonic-gate * clean up io files
3440Sstevel@tonic-gate */
3450Sstevel@tonic-gate while (pop())
3460Sstevel@tonic-gate ;
3470Sstevel@tonic-gate
3480Sstevel@tonic-gate /*
3490Sstevel@tonic-gate * Clean up pipe file descriptor
3500Sstevel@tonic-gate * from command substitution
3510Sstevel@tonic-gate */
3520Sstevel@tonic-gate
3530Sstevel@tonic-gate if(savpipe != -1) {
3540Sstevel@tonic-gate close(savpipe);
3550Sstevel@tonic-gate savpipe = -1;
3560Sstevel@tonic-gate }
3570Sstevel@tonic-gate
3580Sstevel@tonic-gate /*
3590Sstevel@tonic-gate * clean up tmp files
3600Sstevel@tonic-gate */
3610Sstevel@tonic-gate while (poptemp())
3620Sstevel@tonic-gate ;
3630Sstevel@tonic-gate }
3640Sstevel@tonic-gate
3650Sstevel@tonic-gate /*
3660Sstevel@tonic-gate * Save positiional parameters before outermost function invocation
3670Sstevel@tonic-gate * in case we are interrupted.
3680Sstevel@tonic-gate * Increment use count for current positional parameters so that they aren't thrown
3690Sstevel@tonic-gate * away.
3700Sstevel@tonic-gate */
3710Sstevel@tonic-gate
savargs(funcnt)3720Sstevel@tonic-gate struct dolnod *savargs(funcnt)
3730Sstevel@tonic-gate int funcnt;
3740Sstevel@tonic-gate {
3750Sstevel@tonic-gate if (!funcnt) {
3760Sstevel@tonic-gate globdolh = dolh;
3770Sstevel@tonic-gate globdolv = dolv;
3780Sstevel@tonic-gate globdolc = dolc;
3790Sstevel@tonic-gate }
3800Sstevel@tonic-gate useargs();
3810Sstevel@tonic-gate return(dolh);
3820Sstevel@tonic-gate }
3830Sstevel@tonic-gate
3840Sstevel@tonic-gate /* After function invocation, free positional parameters,
3850Sstevel@tonic-gate * restore old positional parameters, and restore
3860Sstevel@tonic-gate * use count.
3870Sstevel@tonic-gate */
3880Sstevel@tonic-gate
restorargs(olddolh,funcnt)3890Sstevel@tonic-gate void restorargs(olddolh, funcnt)
3900Sstevel@tonic-gate struct dolnod *olddolh;
3910Sstevel@tonic-gate {
3920Sstevel@tonic-gate if(argfor != olddolh)
3930Sstevel@tonic-gate while ((argfor = clean_args(argfor)) != olddolh && argfor);
3940Sstevel@tonic-gate if(!argfor)
3950Sstevel@tonic-gate return;
3960Sstevel@tonic-gate freedolh();
3970Sstevel@tonic-gate dolh = olddolh;
3980Sstevel@tonic-gate if(dolh)
3990Sstevel@tonic-gate dolh -> doluse++; /* increment use count so arguments aren't freed */
4000Sstevel@tonic-gate argfor = freeargs(dolh);
4010Sstevel@tonic-gate if(funcnt == 1) {
4020Sstevel@tonic-gate globdolh = 0;
4030Sstevel@tonic-gate globdolv = 0;
4040Sstevel@tonic-gate globdolc = 0;
4050Sstevel@tonic-gate }
4060Sstevel@tonic-gate }
4070Sstevel@tonic-gate
4080Sstevel@tonic-gate struct dolnod *
useargs()4090Sstevel@tonic-gate useargs()
4100Sstevel@tonic-gate {
4110Sstevel@tonic-gate if (dolh)
4120Sstevel@tonic-gate {
4130Sstevel@tonic-gate if (dolh->doluse++ == 1)
4140Sstevel@tonic-gate {
4150Sstevel@tonic-gate dolh->dolnxt = argfor;
4160Sstevel@tonic-gate argfor = dolh;
4170Sstevel@tonic-gate }
4180Sstevel@tonic-gate }
4190Sstevel@tonic-gate return(dolh);
4200Sstevel@tonic-gate }
4210Sstevel@tonic-gate
422