xref: /titanic_41/usr/src/lib/libshell/common/sh/args.c (revision e802abbda8c322f24d47835734f4a793ef15ddc8)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1982-2008 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                  David Korn <dgk@research.att.com>                   *
18 *                                                                      *
19 ***********************************************************************/
20 #pragma prototyped
21 /*
22  * UNIX shell
23  *
24  * S. R. Bourne
25  * Rewritten by David Korn
26  * AT&T Labs
27  *
28  */
29 
30 #include	"defs.h"
31 #include	"path.h"
32 #include	"builtins.h"
33 #include	"terminal.h"
34 #include	"edit.h"
35 #include	"FEATURE/poll"
36 #if SHOPT_KIA
37 #   include	"shlex.h"
38 #   include	"io.h"
39 #endif /* SHOPT_KIA */
40 #if SHOPT_PFSH
41 #   define PFSHOPT	"P"
42 #else
43 #   define PFSHOPT
44 #endif
45 #if SHOPT_BASH
46 #   define BASHOPT	"\374"
47 #else
48 #   define BASHOPT
49 #endif
50 #if SHOPT_HISTEXPAND
51 #   define HFLAG        "H"
52 #else
53 #   define HFLAG        ""
54 #endif
55 
56 #define SORT		1
57 #define PRINT		2
58 
59 static	char		*null;
60 
61 /* The following order is determined by sh_optset */
62 static  const char optksh[] =  PFSHOPT BASHOPT "DircabefhkmnpstuvxBCGEl" HFLAG;
63 static const int flagval[]  =
64 {
65 #if SHOPT_PFSH
66 	SH_PFSH,
67 #endif
68 #if SHOPT_BASH
69 	SH_POSIX,
70 #endif
71 	SH_DICTIONARY, SH_INTERACTIVE, SH_RESTRICTED, SH_CFLAG,
72 	SH_ALLEXPORT, SH_NOTIFY, SH_ERREXIT, SH_NOGLOB, SH_TRACKALL,
73 	SH_KEYWORD, SH_MONITOR, SH_NOEXEC, SH_PRIVILEGED, SH_SFLAG, SH_TFLAG,
74 	SH_NOUNSET, SH_VERBOSE,  SH_XTRACE, SH_BRACEEXPAND, SH_NOCLOBBER,
75 	SH_GLOBSTARS, SH_RC, SH_LOGIN_SHELL,
76 #if SHOPT_HISTEXPAND
77         SH_HISTEXPAND,
78 #endif
79 	0
80 };
81 
82 #define NUM_OPTS	(sizeof(flagval)/sizeof(*flagval))
83 
84 typedef struct _arg_
85 {
86 	Shell_t		*sh;
87 	struct dolnod	*argfor; /* linked list of blocks to be cleaned up */
88 	struct dolnod	*dolh;
89 	char flagadr[NUM_OPTS+1];
90 #if SHOPT_KIA
91 	char	*kiafile;
92 #endif /* SHOPT_KIA */
93 } Arg_t;
94 
95 static int 		arg_expand(Shell_t*,struct argnod*,struct argnod**,int);
96 static void 		sh_argset(Arg_t*, char *[]);
97 
98 
99 /* ======== option handling	======== */
100 
101 void *sh_argopen(Shell_t *shp)
102 {
103 	void *addr = newof(0,Arg_t,1,0);
104 	Arg_t *ap = (Arg_t*)addr;
105 	ap->sh = shp;
106 	return(addr);
107 }
108 
109 static int infof(Opt_t* op, Sfio_t* sp, const char* s, Optdisc_t* dp)
110 {
111 #if SHOPT_BASH
112 	extern const char sh_bash1[], sh_bash2[];
113 	if(strcmp(s,"bash1")==0)
114 	{
115 		if(sh_isoption(SH_BASH))
116 			sfputr(sp,sh_bash1,-1);
117 	}
118 	else if(strcmp(s,"bash2")==0)
119 	{
120 		if(sh_isoption(SH_BASH))
121 			sfputr(sp,sh_bash2,-1);
122 	}
123 	else if(*s==':' && sh_isoption(SH_BASH))
124 		sfputr(sp,s,-1);
125 	else
126 #endif
127 	if(*s!=':')
128 		sfputr(sp,sh_set,-1);
129 	return(1);
130 }
131 
132 /*
133  *  This routine turns options on and off
134  *  The options "PDicr" are illegal from set command.
135  *  The -o option is used to set option by name
136  *  This routine returns the number of non-option arguments
137  */
138 int sh_argopts(int argc,register char *argv[], void *context)
139 {
140 	Shell_t		*shp = (Shell_t*)context;
141 	register int	n,o;
142 	register Arg_t	*ap = (Arg_t*)(shp->arg_context);
143 	Lex_t		*lp = (Lex_t*)(shp->lex_context);
144 	Shopt_t		newflags;
145 	int setflag=0, action=0, trace=(int)sh_isoption(SH_XTRACE);
146 	Namval_t *np = NIL(Namval_t*);
147 	const char *cp;
148 	int verbose,f;
149 	Optdisc_t disc;
150 	newflags=ap->sh->options;
151 	memset(&disc, 0, sizeof(disc));
152 	disc.version = OPT_VERSION;
153 	disc.infof = infof;
154 	opt_info.disc = &disc;
155 
156 	if(argc>0)
157 		setflag = 4;
158 	else
159 		argc = -argc;
160 	while((n = optget(argv,setflag?sh_optset:sh_optksh)))
161 	{
162 		o=0;
163 		f=*opt_info.option=='-' && (opt_info.num || opt_info.arg);
164 		switch(n)
165 		{
166 	 	    case 'A':
167 			np = nv_open(opt_info.arg,ap->sh->var_tree,NV_NOASSIGN|NV_ARRAY|NV_VARNAME);
168 			if(f)
169 				nv_unset(np);
170 			continue;
171 #if SHOPT_BASH
172 		    case 'O':	/* shopt options, only in bash mode */
173 			if(!sh_isoption(SH_BASH))
174 				errormsg(SH_DICT,ERROR_exit(1), e_option, opt_info.name);
175 #endif
176 		    case 'o':	/* set options */
177 		    byname:
178 			if(!opt_info.arg||!*opt_info.arg||*opt_info.arg=='-')
179 			{
180 				action = PRINT;
181 				/* print style: -O => shopt options
182 				 * bash => print unset options also, no heading
183 				 */
184 				verbose = (f?PRINT_VERBOSE:PRINT_NO_HEADER)|
185 					  (n=='O'?PRINT_SHOPT:0)|
186 					  (sh_isoption(SH_BASH)?PRINT_ALL|PRINT_NO_HEADER:0)|
187 					  ((opt_info.arg&&(!*opt_info.arg||*opt_info.arg=='-'))?(PRINT_TABLE|PRINT_NO_HEADER):0);
188 				continue;
189 			}
190 			o = sh_lookopt(opt_info.arg,&f);
191 			if(o<=0
192 				|| (!sh_isoption(SH_BASH) && (o&SH_BASHEXTRA))
193 				|| ((!sh_isoption(SH_BASH) || n=='o') && (o&SH_BASHOPT))
194 
195 				|| (setflag && (o&SH_COMMANDLINE)))
196 			{
197 				errormsg(SH_DICT,2, e_option, opt_info.arg);
198 				error_info.errors++;
199 			}
200 			o &= 0xff;
201 			if(sh_isoption(SH_RESTRICTED) && !f && o==SH_RESTRICTED)
202 				errormsg(SH_DICT,ERROR_exit(1), e_restricted, opt_info.arg);
203 			break;
204 #if SHOPT_BASH
205 		    case -1:	/* --rcfile */
206 			ap->sh->rcfile = opt_info.arg;
207 			continue;
208 		    case -2:	/* --noediting */
209 			if (!f)
210 			{
211 				off_option(&newflags,SH_VI);
212 				off_option(&newflags,SH_EMACS);
213 				off_option(&newflags,SH_GMACS);
214 			}
215 			continue;
216 		    case -3:	/* --profile */
217 			n = 'l';
218 			goto skip;
219 		    case -4:	/* --posix */
220 			/* mask lower 8 bits to find char in optksh string */
221 			n&=0xff;
222 			goto skip;
223 		    case -5:	/* --version */
224 			sfputr(sfstdout, "ksh bash emulation, version ",-1);
225 			np = nv_open("BASH_VERSION",ap->sh->var_tree,0);
226 			sfputr(sfstdout, nv_getval(np),-1);
227 			np = nv_open("MACHTYPE",ap->sh->var_tree,0);
228 			sfprintf(sfstdout, " (%s)\n", nv_getval(np));
229 			sh_exit(0);
230 #endif
231 		    case -6:	/* --default */
232 			{
233 				register const Shtable_t *tp;
234 				for(tp=shtab_options; o = tp->sh_number; tp++)
235 					if(!(o&SH_COMMANDLINE) && is_option(&newflags,o&0xff))
236 						off_option(&newflags,o&0xff);
237 			}
238 		    	continue;
239 	 	    case -7:
240 			f = 0;
241 		    	goto byname;
242 	 	    case 'D':
243 			on_option(&newflags,SH_NOEXEC);
244 			goto skip;
245 		    case 'T':
246 			if (opt_info.num)
247 				ap->sh->test |= opt_info.num;
248 			else
249 				ap->sh->test = 0;
250 		    	continue;
251 		    case 's':
252 			if(setflag)
253 			{
254 				action = SORT;
255 				continue;
256 			}
257 #if SHOPT_KIA
258 			goto skip;
259 		    case 'R':
260 			if(setflag)
261 				n = ':';
262 			else
263 			{
264 				ap->kiafile = opt_info.arg;
265 				n = 'n';
266 			}
267 			/* FALL THRU */
268 #endif /* SHOPT_KIA */
269 		    skip:
270 		    default:
271 			if(cp=strchr(optksh,n))
272 				o = flagval[cp-optksh];
273 			break;
274 		    case ':':
275 			if(opt_info.name[0]=='-'&&opt_info.name[1]=='-')
276 			{
277 				opt_info.arg = argv[opt_info.index-1] + 2;
278 				f = 1;
279 				goto byname;
280 			}
281 			errormsg(SH_DICT,2, "%s", opt_info.arg);
282 			continue;
283 		    case '?':
284 			errormsg(SH_DICT,ERROR_usage(0), "%s", opt_info.arg);
285 			return(-1);
286 		}
287 		if(f)
288 		{
289 			if(o==SH_VI || o==SH_EMACS || o==SH_GMACS)
290 			{
291 				off_option(&newflags,SH_VI);
292 				off_option(&newflags,SH_EMACS);
293 				off_option(&newflags,SH_GMACS);
294 			}
295 			on_option(&newflags,o);
296 			off_option(&ap->sh->offoptions,o);
297 		}
298 		else
299 		{
300 			if(o==SH_XTRACE)
301 				trace = 0;
302 			off_option(&newflags,o);
303 			if(setflag==0)
304 				on_option(&ap->sh->offoptions,o);
305 		}
306 	}
307 	if(error_info.errors)
308 		errormsg(SH_DICT,ERROR_usage(2),"%s",optusage(NIL(char*)));
309 	/* check for '-' or '+' argument */
310 	if((cp=argv[opt_info.index]) && cp[1]==0 && (*cp=='+' || *cp=='-') &&
311 		strcmp(argv[opt_info.index-1],"--"))
312 	{
313 		opt_info.index++;
314 		off_option(&newflags,SH_XTRACE);
315 		off_option(&newflags,SH_VERBOSE);
316 		trace = 0;
317 	}
318 	if(trace)
319 		sh_trace(argv,1);
320 	argc -= opt_info.index;
321 	argv += opt_info.index;
322 	if(action==PRINT)
323 		sh_printopts(newflags,verbose,0);
324 	if(setflag)
325 	{
326 		if(action==SORT)
327 		{
328 			if(argc>0)
329 				strsort(argv,argc,strcoll);
330 			else
331 				strsort(ap->sh->st.dolv+1,ap->sh->st.dolc,strcoll);
332 		}
333 		if(np)
334 		{
335 			nv_setvec(np,0,argc,argv);
336 			nv_close(np);
337 		}
338 		else if(argc>0 || ((cp=argv[-1]) && strcmp(cp,"--")==0))
339 			sh_argset(ap,argv-1);
340 	}
341 	else if(is_option(&newflags,SH_CFLAG))
342 	{
343 		if(!(ap->sh->comdiv = *argv++))
344 		{
345 			errormsg(SH_DICT,2,e_cneedsarg);
346 			errormsg(SH_DICT,ERROR_usage(2),optusage(NIL(char*)));
347 		}
348 		argc--;
349 	}
350 	/* handling SH_INTERACTIVE and SH_PRIVILEGED has been moved to
351 	 * sh_applyopts(), so that the code can be reused from b_shopt(), too
352 	 */
353 	sh_applyopts(ap->sh,newflags);
354 #if SHOPT_KIA
355 	if(ap->kiafile)
356 	{
357 		if(!argv[0])
358 			errormsg(SH_DICT,ERROR_usage(2),"-R requires scriptname");
359 		if(!(lp->kiafile=sfopen(NIL(Sfio_t*),ap->kiafile,"w+")))
360 			errormsg(SH_DICT,ERROR_system(3),e_create,ap->kiafile);
361 		if(!(lp->kiatmp=sftmp(2*SF_BUFSIZE)))
362 			errormsg(SH_DICT,ERROR_system(3),e_tmpcreate);
363 		sfputr(lp->kiafile,";vdb;CIAO/ksh",'\n');
364 		lp->kiabegin = sftell(lp->kiafile);
365 		lp->entity_tree = dtopen(&_Nvdisc,Dtbag);
366 		lp->scriptname = strdup(sh_fmtq(argv[0]));
367 		lp->script=kiaentity(lp,lp->scriptname,-1,'p',-1,0,0,'s',0,"");
368 		lp->fscript=kiaentity(lp,lp->scriptname,-1,'f',-1,0,0,'s',0,"");
369 		lp->unknown=kiaentity(lp,"<unknown>",-1,'p',-1,0,0,'0',0,"");
370 		kiaentity(lp,"<unknown>",-1,'p',0,0,lp->unknown,'0',0,"");
371 		lp->current = lp->script;
372 		ap->kiafile = 0;
373 	}
374 #endif /* SHOPT_KIA */
375 	return(argc);
376 }
377 
378 /* apply new options */
379 
380 void sh_applyopts(Shell_t* shp,Shopt_t newflags)
381 {
382 	/* cannot set -n for interactive shells since there is no way out */
383 	if(sh_isoption(SH_INTERACTIVE))
384 		off_option(&newflags,SH_NOEXEC);
385 	if(is_option(&newflags,SH_PRIVILEGED))
386 		on_option(&newflags,SH_NOUSRPROFILE);
387 	if(is_option(&newflags,SH_PRIVILEGED) != sh_isoption(SH_PRIVILEGED))
388 	{
389 		if(sh_isoption(SH_PRIVILEGED))
390 		{
391 			setuid(shp->userid);
392 			setgid(shp->groupid);
393 			if(shp->euserid==0)
394 			{
395 				shp->euserid = shp->userid;
396 				shp->egroupid = shp->groupid;
397 			}
398 		}
399 		else if((shp->userid!=shp->euserid && setuid(shp->euserid)<0) ||
400 			(shp->groupid!=shp->egroupid && setgid(shp->egroupid)<0) ||
401 			(shp->userid==shp->euserid && shp->groupid==shp->egroupid))
402 				off_option(&newflags,SH_PRIVILEGED);
403 	}
404 #if SHOPT_BASH
405 	on_option(&newflags,SH_CMDHIST);
406 	on_option(&newflags,SH_CHECKHASH);
407 	on_option(&newflags,SH_EXECFAIL);
408 	on_option(&newflags,SH_EXPAND_ALIASES);
409 	on_option(&newflags,SH_HISTAPPEND);
410 	on_option(&newflags,SH_INTERACTIVE_COMM);
411 	on_option(&newflags,SH_LITHIST);
412 	on_option(&newflags,SH_NOEMPTYCMDCOMPL);
413 
414 	if(!is_option(&newflags,SH_XPG_ECHO) && sh_isoption(SH_XPG_ECHO))
415 		astconf("UNIVERSE", 0, "ucb");
416 	if(is_option(&newflags,SH_XPG_ECHO) && !sh_isoption(SH_XPG_ECHO))
417 		astconf("UNIVERSE", 0, "att");
418 	if(!is_option(&newflags,SH_PHYSICAL) && sh_isoption(SH_PHYSICAL))
419 		astconf("PATH_RESOLVE", 0, "metaphysical");
420 	if(is_option(&newflags,SH_PHYSICAL) && !sh_isoption(SH_PHYSICAL))
421 		astconf("PATH_RESOLVE", 0, "physical");
422 	if(is_option(&newflags,SH_HISTORY2) && !sh_isoption(SH_HISTORY2))
423 	{
424 		sh_onstate(SH_HISTORY);
425                 sh_onoption(SH_HISTORY);
426 	}
427 	if(!is_option(&newflags,SH_HISTORY2) && sh_isoption(SH_HISTORY2))
428 	{
429 		sh_offstate(SH_HISTORY);
430 		sh_offoption(SH_HISTORY);
431 	}
432 #endif
433 	shp->options = newflags;
434 }
435 
436 /*
437  * returns the value of $-
438  */
439 char *sh_argdolminus(void* context)
440 {
441 	register Arg_t *ap = (Arg_t*)context;
442 	register const char *cp=optksh;
443 	register char *flagp=ap->flagadr;
444 	while(cp< &optksh[NUM_OPTS])
445 	{
446 		int n = flagval[cp-optksh];
447 		if(sh_isoption(n))
448 			*flagp++ = *cp;
449 		cp++;
450 	}
451 	*flagp = 0;
452 	return(ap->flagadr);
453 }
454 
455 /*
456  * set up positional parameters
457  */
458 static void sh_argset(Arg_t *ap,char *argv[])
459 {
460 	sh_argfree(ap->sh,ap->dolh,0);
461 	ap->dolh = sh_argcreate(argv);
462 	/* link into chain */
463 	ap->dolh->dolnxt = ap->argfor;
464 	ap->argfor = ap->dolh;
465 	ap->sh->st.dolc = ap->dolh->dolnum-1;
466 	ap->sh->st.dolv = ap->dolh->dolval;
467 }
468 
469 /*
470  * free the argument list if the use count is 1
471  * If count is greater than 1 decrement count and return same blk
472  * Free the argument list if the use count is 1 and return next blk
473  * Delete the blk from the argfor chain
474  * If flag is set, then the block dolh is not freed
475  */
476 struct dolnod *sh_argfree(Shell_t *shp, struct dolnod *blk,int flag)
477 {
478 	register struct dolnod*	argr=blk;
479 	register struct dolnod*	argblk;
480 	register Arg_t *ap = (Arg_t*)shp->arg_context;
481 	if(argblk=argr)
482 	{
483 		if((--argblk->dolrefcnt)==0)
484 		{
485 			argr = argblk->dolnxt;
486 			if(flag && argblk==ap->dolh)
487 				ap->dolh->dolrefcnt = 1;
488 			else
489 			{
490 				/* delete from chain */
491 				if(ap->argfor == argblk)
492 					ap->argfor = argblk->dolnxt;
493 				else
494 				{
495 					for(argr=ap->argfor;argr;argr=argr->dolnxt)
496 						if(argr->dolnxt==argblk)
497 							break;
498 					if(!argr)
499 						return(NIL(struct dolnod*));
500 					argr->dolnxt = argblk->dolnxt;
501 					argr = argblk->dolnxt;
502 				}
503 				free((void*)argblk);
504 			}
505 		}
506 	}
507 	return(argr);
508 }
509 
510 /*
511  * grab space for arglist and copy args
512  * The strings are copied after the argment vector
513  */
514 struct dolnod *sh_argcreate(register char *argv[])
515 {
516 	register struct dolnod *dp;
517 	register char **pp=argv, *sp;
518 	register int 	size=0,n;
519 	/* count args and number of bytes of arglist */
520 	while(sp= *pp++)
521 		size += strlen(sp);
522 	n = (pp - argv)-1;
523 	dp=new_of(struct dolnod,n*sizeof(char*)+size+n);
524 	dp->dolrefcnt=1;	/* use count */
525 	dp->dolnum = n;
526 	dp->dolnxt = 0;
527 	pp = dp->dolval;
528 	sp = (char*)dp + sizeof(struct dolnod) + n*sizeof(char*);
529 	while(n--)
530 	{
531 		*pp++ = sp;
532 		sp = strcopy(sp, *argv++) + 1;
533 	}
534 	*pp = NIL(char*);
535 	return(dp);
536 }
537 
538 /*
539  *  used to set new arguments for functions
540  */
541 struct dolnod *sh_argnew(Shell_t *shp,char *argi[], struct dolnod **savargfor)
542 {
543 	register Arg_t *ap = (Arg_t*)shp->arg_context;
544 	register struct dolnod *olddolh = ap->dolh;
545 	*savargfor = ap->argfor;
546 	ap->dolh = 0;
547 	ap->argfor = 0;
548 	sh_argset(ap,argi);
549 	return(olddolh);
550 }
551 
552 /*
553  * reset arguments as they were before function
554  */
555 void sh_argreset(Shell_t *shp,struct dolnod *blk, struct dolnod *afor)
556 {
557 	register Arg_t *ap = (Arg_t*)shp->arg_context;
558 	while(ap->argfor=sh_argfree(shp,ap->argfor,0));
559 	ap->argfor = afor;
560 	if(ap->dolh = blk)
561 	{
562 		shp->st.dolc = ap->dolh->dolnum-1;
563 		shp->st.dolv = ap->dolh->dolval;
564 	}
565 }
566 
567 /*
568  * increase the use count so that an sh_argset will not make it go away
569  */
570 struct dolnod *sh_arguse(Shell_t* shp)
571 {
572 	register struct dolnod *dh;
573 	register Arg_t *ap = (Arg_t*)shp->arg_context;
574 	if(dh=ap->dolh)
575 		dh->dolrefcnt++;
576 	return(dh);
577 }
578 
579 /*
580  *  Print option settings on standard output
581  *  if mode is inclusive or of PRINT_*
582  *  if <mask> is set, only options with this mask value are displayed
583  */
584 void sh_printopts(Shopt_t oflags,register int mode, Shopt_t *mask)
585 {
586 	register const Shtable_t *tp;
587 	const char *name;
588 	int on;
589 	int value;
590 	if(!(mode&PRINT_NO_HEADER))
591 		sfputr(sfstdout,sh_translate(e_heading),'\n');
592 	if(mode&PRINT_TABLE)
593 	{
594 		int	w;
595 		int	c;
596 		int	r;
597 		int	i;
598 
599 		c = 0;
600 		for(tp=shtab_options; value=tp->sh_number; tp++)
601 		{
602 			if(mask && !is_option(mask,value&0xff))
603 				continue;
604 			name = tp->sh_name;
605 			if(name[0] == 'n' && name[1] == 'o' && name[2] != 't')
606 				name += 2;
607 			if(c<(w=strlen(name)))
608 				c = w;
609 		}
610 		c += 4;
611 		if((w = ed_window()) < (2*c))
612 			w = 2*c;
613 		r = w / c;
614 		i = 0;
615 		for(tp=shtab_options; value=tp->sh_number; tp++)
616 		{
617 			if(mask && !is_option(mask,value&0xff))
618 				continue;
619 			on = !!is_option(&oflags,value);
620 			value &= 0xff;
621 			name = tp->sh_name;
622 			if(name[0] == 'n' && name[1] == 'o' && name[2] != 't')
623 			{
624 				name += 2;
625 				on = !on;
626 			}
627 			if(++i>=r)
628 			{
629 				i = 0;
630 				sfprintf(sfstdout, "%s%s\n", on ? "" : "no", name);
631 			}
632 			else
633 				sfprintf(sfstdout, "%s%-*s", on ? "" : "no", on ? c : (c-2), name);
634 		}
635 		if(i)
636 			sfputc(sfstdout,'\n');
637 		return;
638 	}
639 #if SHOPT_RAWONLY
640 	on_option(&oflags,SH_VIRAW);
641 #endif
642 	if(!(mode&(PRINT_ALL|PRINT_VERBOSE))) /* only print set options */
643 	{
644 		if(mode&PRINT_SHOPT)
645 			sfwrite(sfstdout,"shopt -s",3);
646 		else
647 			sfwrite(sfstdout,"set --default",13);
648 	}
649 	for(tp=shtab_options; value=tp->sh_number; tp++)
650 	{
651 		if(mask && !is_option(mask,value&0xff))
652 			continue;
653 		if(sh_isoption(SH_BASH))
654 		{
655 			if (!(mode&PRINT_SHOPT) != !(value&SH_BASHOPT))
656 				continue;
657 		}
658 		else if (value&(SH_BASHEXTRA|SH_BASHOPT))
659 			continue;
660 		on = !!is_option(&oflags,value);
661 		name = tp->sh_name;
662 		if(name[0] == 'n' && name[1] == 'o' && name[2] != 't')
663 		{
664 			name += 2;
665 			on = !on;
666 		}
667 		if(mode&PRINT_VERBOSE)
668 		{
669 			sfputr(sfstdout,name,' ');
670 			sfnputc(sfstdout,' ',24-strlen(name));
671 			sfputr(sfstdout,on ? sh_translate(e_on) : sh_translate(e_off),'\n');
672 		}
673 		else if(mode&PRINT_ALL) /* print unset options also */
674 		{
675 			if(mode&PRINT_SHOPT)
676 				sfprintf(sfstdout, "shopt -%c %s\n",
677 					on?'s':'u',
678 					name);
679 			else
680 				sfprintf(sfstdout, "set %co %s\n",
681 					on?'-':'+',
682 					name);
683 		}
684 		else if(!(value&SH_COMMANDLINE) && is_option(&oflags,value&0xff))
685 			sfprintf(sfstdout," %s%s%s",(mode&PRINT_SHOPT)?"":"--",on?"":"no",name);
686 	}
687 	if(!(mode&(PRINT_VERBOSE|PRINT_ALL)))
688 		sfputc(sfstdout,'\n');
689 }
690 
691 /*
692  * build an argument list
693  */
694 char **sh_argbuild(Shell_t *shp,int *nargs, const struct comnod *comptr,int flag)
695 {
696 	register struct argnod	*argp;
697 	struct argnod *arghead=0;
698 	shp->xargmin = 0;
699 	{
700 		register const struct comnod	*ac = comptr;
701 		register int n;
702 		/* see if the arguments have already been expanded */
703 		if(!ac->comarg)
704 		{
705 			*nargs = 0;
706 			return(&null);
707 		}
708 		else if(!(ac->comtyp&COMSCAN))
709 		{
710 			register struct dolnod *ap = (struct dolnod*)ac->comarg;
711 			*nargs = ap->dolnum;
712 			return(ap->dolval+ap->dolbot);
713 		}
714 		shp->lastpath = 0;
715 		*nargs = 0;
716 		if(ac)
717 		{
718 			if(ac->comnamp == SYSLET)
719 				flag |= ARG_LET;
720 			argp = ac->comarg;
721 			while(argp)
722 			{
723 				n = arg_expand(shp,argp,&arghead,flag);
724 				if(n>1)
725 				{
726 					if(shp->xargmin==0)
727 						shp->xargmin = *nargs;
728 					shp->xargmax = *nargs+n;
729 				}
730 				*nargs += n;
731 				argp = argp->argnxt.ap;
732 			}
733 			argp = arghead;
734 		}
735 	}
736 	{
737 		register char	**comargn;
738 		register int	argn;
739 		register char	**comargm;
740 		argn = *nargs;
741 		/* allow room to prepend args */
742 		argn += 1;
743 
744 		comargn=(char**)stkalloc(shp->stk,(unsigned)(argn+1)*sizeof(char*));
745 		comargm = comargn += argn;
746 		*comargn = NIL(char*);
747 		if(!argp)
748 		{
749 			/* reserve an extra null pointer */
750 			*--comargn = 0;
751 			return(comargn);
752 		}
753 		while(argp)
754 		{
755 			struct argnod *nextarg = argp->argchn.ap;
756 			argp->argchn.ap = 0;
757 			*--comargn = argp->argval;
758 			if(!(argp->argflag&ARG_RAW))
759 				sh_trim(*comargn);
760 			if(!(argp=nextarg) || (argp->argflag&ARG_MAKE))
761 			{
762 				if((argn=comargm-comargn)>1)
763 					strsort(comargn,argn,strcoll);
764 				comargm = comargn;
765 			}
766 		}
767 		shp->last_table = 0;
768 		return(comargn);
769 	}
770 }
771 
772 #if _pipe_socketpair && !_socketpair_devfd
773 #   define sh_pipe	arg_pipe
774 /*
775  * create a real pipe (not a socket) and print message on failure
776  */
777 static int	arg_pipe(register int pv[])
778 {
779 	int fd[2];
780 	if(pipe(fd)<0 || (pv[0]=fd[0])<0 || (pv[1]=fd[1])<0)
781 		errormsg(SH_DICT,ERROR_system(1),e_pipe);
782 	pv[0] = sh_iomovefd(pv[0]);
783 	pv[1] = sh_iomovefd(pv[1]);
784 	sh.fdstatus[pv[0]] = IONOSEEK|IOREAD;
785 	sh.fdstatus[pv[1]] = IONOSEEK|IOWRITE;
786 	sh_subsavefd(pv[0]);
787 	sh_subsavefd(pv[1]);
788 	return(0);
789 }
790 #endif
791 
792 /* Argument expansion */
793 static int arg_expand(Shell_t *shp,register struct argnod *argp, struct argnod **argchain,int flag)
794 {
795 	register int count = 0;
796 	argp->argflag &= ~ARG_MAKE;
797 #if SHOPT_DEVFD
798 	if(*argp->argval==0 && (argp->argflag&ARG_EXP))
799 	{
800 		/* argument of the form (cmd) */
801 		register struct argnod *ap;
802 		int monitor, fd, pv[2];
803 		ap = (struct argnod*)stkseek(shp->stk,ARGVAL);
804 		ap->argflag |= ARG_MAKE;
805 		ap->argflag &= ~ARG_RAW;
806 		ap->argchn.ap = *argchain;
807 		*argchain = ap;
808 		count++;
809 		sfwrite(shp->stk,e_devfdNN,8);
810 		sh_pipe(pv);
811 		fd = argp->argflag&ARG_RAW;
812 		sfputr(shp->stk,fmtbase((long)pv[fd],10,0),0);
813 		ap = (struct argnod*)stkfreeze(shp->stk,0);
814 		shp->inpipe = shp->outpipe = 0;
815 		if(monitor = (sh_isstate(SH_MONITOR)!=0))
816 			sh_offstate(SH_MONITOR);
817 		if(fd)
818 		{
819 			shp->inpipe = pv;
820 			sh_exec((Shnode_t*)argp->argchn.ap,(int)sh_isstate(SH_ERREXIT));
821 		}
822 		else
823 		{
824 			shp->outpipe = pv;
825 			sh_exec((Shnode_t*)argp->argchn.ap,(int)sh_isstate(SH_ERREXIT));
826 		}
827 		if(monitor)
828 			sh_onstate(SH_MONITOR);
829 		close(pv[1-fd]);
830 		sh_iosave(shp,-pv[fd], shp->topfd, (char*)0);
831 	}
832 	else
833 #endif	/* SHOPT_DEVFD */
834 	if(!(argp->argflag&ARG_RAW))
835 	{
836 #if SHOPT_OPTIMIZE
837 		struct argnod *ap;
838 		sh_stats(STAT_ARGEXPAND);
839 		if(flag&ARG_OPTIMIZE)
840 			argp->argchn.ap=0;
841 		if(ap=argp->argchn.ap)
842 		{
843 			sh_stats(STAT_ARGHITS);
844 			count = 1;
845 			ap->argchn.ap = *argchain;
846 			ap->argflag |= ARG_RAW;
847 			ap->argflag &= ~ARG_EXP;
848 			*argchain = ap;
849 		}
850 		else
851 #endif /* SHOPT_OPTIMIZE */
852 		count = sh_macexpand(shp,argp,argchain,flag);
853 	}
854 	else
855 	{
856 		argp->argchn.ap = *argchain;
857 		*argchain = argp;
858 		argp->argflag |= ARG_MAKE;
859 		count++;
860 	}
861 	return(count);
862 }
863 
864