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
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]
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"
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
;
49 unsigned char flagadr
[16];
51 unsigned char flagchar
[] =
91 /* ======== option handling ======== */
95 options(int argc
, unsigned char **argv
)
98 unsigned char **argp
= argv
;
100 unsigned char *flagp
;
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 "-"
120 flags
&= ~(execpr
|readpr
);
123 * Step along 'flagchar[]' looking for matches.
124 * 'sicrp' are not legal with 'set' command.
128 if ((len
= mbtowc(&wc
, (char *)cp
, MB_LEN_MAX
)) <= 0) {
130 wc
= (unsigned char)*cp
;
131 failed(argv
[1],badopt
);
136 while (*flagc
&& wc
!= *flagc
)
140 if (eq(argv
[0], "set") && any(wc
, "sicrp"))
141 failed(argv
[1], badopt
);
144 flags
|= flagval
[flagc
-flagchar
];
149 else if (wc
== 'c' && argc
> 2 && comdiv
== 0)
157 failed(argv
[1],badopt
);
162 else if (argc
> 1 && *argp
[1] == '+') /* unset flags x, k, t, n, v, e, u */
168 if ((len
= mbtowc(&wc
, (char *)cp
, MB_LEN_MAX
)) <= 0) {
174 while (*flagc
&& wc
!= *flagc
)
179 if (!any(wc
, "sicrp") && wc
== *flagc
) {
180 flags
&= ~(flagval
[flagc
-flagchar
]);
198 if (flags
& flagval
[flagc
-flagchar
])
208 * sets up positional parameters
211 setargs(unsigned char *argi
[])
213 unsigned char **argp
= argi
; /* count args */
216 while (*argp
++ != (unsigned char *)ENDARGS
)
219 * free old ones unless on for loop chain
222 dolh
= copyargs(argi
, argn
);
230 unsigned char **argp
;
231 struct dolnod
*argblk
;
235 if ((--argblk
->doluse
) == 0)
237 for (argp
= argblk
->dolarg
; *argp
!= (unsigned char *)ENDARGS
; argp
++)
239 free(argblk
->dolarg
);
249 unsigned char **argp
;
250 struct dolnod
*argr
= 0;
251 struct dolnod
*argblk
;
256 argr
= argblk
->dolnxt
;
257 cnt
= --argblk
->doluse
;
270 for (argp
= argblk
->dolarg
; *argp
!= (unsigned char *)ENDARGS
; argp
++)
272 free(argblk
->dolarg
);
280 static struct dolnod
*
282 unsigned char *from
[];
284 struct dolnod
*np
= (struct dolnod
*)alloc(sizeof (struct dolnod
));
285 unsigned char **fp
= from
;
289 np
->doluse
= 1; /* use count */
290 pp
= np
->dolarg
= (unsigned char **)alloc((n
+1)*sizeof(char *));
304 unsigned char **argp
;
305 struct dolnod
*argr
= 0;
306 struct dolnod
*argblk
;
310 argr
= argblk
->dolnxt
;
316 for (argp
= argblk
->dolarg
; *argp
!= (unsigned char *)ENDARGS
; argp
++)
318 free(argblk
->dolarg
);
329 * force `for' $* lists to go away
340 while (argfor
= clean_args(argfor
))
349 * Clean up pipe file descriptor
350 * from command substitution
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
372 struct dolnod
*savargs(funcnt
)
384 /* After function invocation, free positional parameters,
385 * restore old positional parameters, and restore
389 void restorargs(olddolh
, funcnt
)
390 struct dolnod
*olddolh
;
392 if(argfor
!= olddolh
)
393 while ((argfor
= clean_args(argfor
)) != olddolh
&& argfor
);
399 dolh
-> doluse
++; /* increment use count so arguments aren't freed */
400 argfor
= freeargs(dolh
);
413 if (dolh
->doluse
++ == 1)
415 dolh
->dolnxt
= argfor
;