8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / sh / args.c
blob4e6062def23f060e71336716425e5720c7f51e9f
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
24 * Copyright 1995 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
28 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
29 /* All Rights Reserved */
31 #pragma ident "%Z%%M% %I% %E% SMI"
34 * UNIX shell
37 #include "defs.h"
39 static struct dolnod *copyargs();
40 static void freedolh(void);
41 extern struct dolnod *freeargs();
42 static struct dolnod *dolh;
44 /* Used to save outermost positional parameters */
45 static struct dolnod *globdolh;
46 static unsigned char **globdolv;
47 static int globdolc;
49 unsigned char flagadr[16];
51 unsigned char flagchar[] =
53 'x',
54 'n',
55 'v',
56 't',
57 STDFLG,
58 'i',
59 'e',
60 'r',
61 'k',
62 'u',
63 'h',
64 'f',
65 'a',
66 'm',
67 'p',
71 long flagval[] =
73 execpr,
74 noexec,
75 readpr,
76 oneflg,
77 stdflg,
78 intflg,
79 errflg,
80 rshflg,
81 keyflg,
82 setflg,
83 hashflg,
84 nofngflg,
85 exportflg,
86 monitorflg,
87 privflg,
91 /* ======== option handling ======== */
94 int
95 options(int argc, unsigned char **argv)
97 unsigned char *cp;
98 unsigned char **argp = argv;
99 unsigned char *flagc;
100 unsigned char *flagp;
101 int len;
102 wchar_t wc;
104 if (argc > 1 && *argp[1] == '-')
107 * if first argument is "--" then options are not
108 * to be changed. Fix for problems getting
109 * $1 starting with a "-"
112 cp = argp[1];
113 if (cp[1] == '-')
115 argp[1] = argp[0];
116 argc--;
117 return(argc);
119 if (cp[1] == '\0')
120 flags &= ~(execpr|readpr);
123 * Step along 'flagchar[]' looking for matches.
124 * 'sicrp' are not legal with 'set' command.
126 cp++;
127 while (*cp) {
128 if ((len = mbtowc(&wc, (char *)cp, MB_LEN_MAX)) <= 0) {
129 len = 1;
130 wc = (unsigned char)*cp;
131 failed(argv[1],badopt);
133 cp += len;
135 flagc = flagchar;
136 while (*flagc && wc != *flagc)
137 flagc++;
138 if (wc == *flagc)
140 if (eq(argv[0], "set") && any(wc, "sicrp"))
141 failed(argv[1], badopt);
142 else
144 flags |= flagval[flagc-flagchar];
145 if (flags & errflg)
146 eflag = errflg;
149 else if (wc == 'c' && argc > 2 && comdiv == 0)
151 comdiv = argp[2];
152 argp[1] = argp[0];
153 argp++;
154 argc--;
156 else
157 failed(argv[1],badopt);
159 argp[1] = argp[0];
160 argc--;
162 else if (argc > 1 && *argp[1] == '+') /* unset flags x, k, t, n, v, e, u */
164 cp = argp[1];
165 cp++;
166 while (*cp)
168 if ((len = mbtowc(&wc, (char *)cp, MB_LEN_MAX)) <= 0) {
169 cp++;
170 continue;
173 flagc = flagchar;
174 while (*flagc && wc != *flagc)
175 flagc++;
177 * step through flags
179 if (!any(wc, "sicrp") && wc == *flagc) {
180 flags &= ~(flagval[flagc-flagchar]);
181 if (wc == 'e')
182 eflag = 0;
184 cp += len;
186 argp[1] = argp[0];
187 argc--;
190 * set up $-
192 flagp = flagadr;
193 if (flags)
195 flagc = flagchar;
196 while (*flagc)
198 if (flags & flagval[flagc-flagchar])
199 *flagp++ = *flagc;
200 flagc++;
203 *flagp = 0;
204 return(argc);
208 * sets up positional parameters
210 void
211 setargs(unsigned char *argi[])
213 unsigned char **argp = argi; /* count args */
214 int argn = 0;
216 while (*argp++ != (unsigned char *)ENDARGS)
217 argn++;
219 * free old ones unless on for loop chain
221 freedolh();
222 dolh = copyargs(argi, argn);
223 dolc = argn - 1;
227 static void
228 freedolh(void)
230 unsigned char **argp;
231 struct dolnod *argblk;
233 if (argblk = dolh)
235 if ((--argblk->doluse) == 0)
237 for (argp = argblk->dolarg; *argp != (unsigned char *)ENDARGS; argp++)
238 free(*argp);
239 free(argblk->dolarg);
240 free(argblk);
245 struct dolnod *
246 freeargs(blk)
247 struct dolnod *blk;
249 unsigned char **argp;
250 struct dolnod *argr = 0;
251 struct dolnod *argblk;
252 int cnt;
254 if (argblk = blk)
256 argr = argblk->dolnxt;
257 cnt = --argblk->doluse;
259 if (argblk == dolh)
261 if (cnt == 1)
262 return(argr);
263 else
264 return(argblk);
266 else
268 if (cnt == 0)
270 for (argp = argblk->dolarg; *argp != (unsigned char *)ENDARGS; argp++)
271 free(*argp);
272 free(argblk->dolarg);
273 free(argblk);
277 return(argr);
280 static struct dolnod *
281 copyargs(from, n)
282 unsigned char *from[];
284 struct dolnod *np = (struct dolnod *)alloc(sizeof (struct dolnod));
285 unsigned char **fp = from;
286 unsigned char **pp;
288 np -> dolnxt = 0;
289 np->doluse = 1; /* use count */
290 pp = np->dolarg = (unsigned char **)alloc((n+1)*sizeof(char *));
291 dolv = pp;
293 while (n--)
294 *pp++ = make(*fp++);
295 *pp++ = ENDARGS;
296 return(np);
300 struct dolnod *
301 clean_args(blk)
302 struct dolnod *blk;
304 unsigned char **argp;
305 struct dolnod *argr = 0;
306 struct dolnod *argblk;
308 if (argblk = blk)
310 argr = argblk->dolnxt;
312 if (argblk == dolh)
313 argblk->doluse = 1;
314 else
316 for (argp = argblk->dolarg; *argp != (unsigned char *)ENDARGS; argp++)
317 free(*argp);
318 free(argblk->dolarg);
319 free(argblk);
322 return(argr);
325 void
326 clearup(void)
329 * force `for' $* lists to go away
331 if(globdolv)
332 dolv = globdolv;
333 if(globdolc)
334 dolc = globdolc;
335 if(globdolh)
336 dolh = globdolh;
337 globdolv = 0;
338 globdolc = 0;
339 globdolh = 0;
340 while (argfor = clean_args(argfor))
343 * clean up io files
345 while (pop())
349 * Clean up pipe file descriptor
350 * from command substitution
353 if(savpipe != -1) {
354 close(savpipe);
355 savpipe = -1;
359 * clean up tmp files
361 while (poptemp())
366 * Save positiional parameters before outermost function invocation
367 * in case we are interrupted.
368 * Increment use count for current positional parameters so that they aren't thrown
369 * away.
372 struct dolnod *savargs(funcnt)
373 int funcnt;
375 if (!funcnt) {
376 globdolh = dolh;
377 globdolv = dolv;
378 globdolc = dolc;
380 useargs();
381 return(dolh);
384 /* After function invocation, free positional parameters,
385 * restore old positional parameters, and restore
386 * use count.
389 void restorargs(olddolh, funcnt)
390 struct dolnod *olddolh;
392 if(argfor != olddolh)
393 while ((argfor = clean_args(argfor)) != olddolh && argfor);
394 if(!argfor)
395 return;
396 freedolh();
397 dolh = olddolh;
398 if(dolh)
399 dolh -> doluse++; /* increment use count so arguments aren't freed */
400 argfor = freeargs(dolh);
401 if(funcnt == 1) {
402 globdolh = 0;
403 globdolv = 0;
404 globdolc = 0;
408 struct dolnod *
409 useargs()
411 if (dolh)
413 if (dolh->doluse++ == 1)
415 dolh->dolnxt = argfor;
416 argfor = dolh;
419 return(dolh);