Home | History | Annotate | Line # | Download | only in ksh
c_ksh.c revision 1.19
      1 /*	$NetBSD: c_ksh.c,v 1.19 2017/06/22 13:32:04 kamil Exp $	*/
      2 
      3 /*
      4  * built-in Korn commands: c_*
      5  */
      6 #include <sys/cdefs.h>
      7 
      8 #ifndef lint
      9 __RCSID("$NetBSD: c_ksh.c,v 1.19 2017/06/22 13:32:04 kamil Exp $");
     10 #endif
     11 
     12 #include "sh.h"
     13 #include "ksh_stat.h"
     14 #include <ctype.h>
     15 
     16 int
     17 c_cd(wp)
     18 	char	**wp;
     19 {
     20 	int optc;
     21 	int physical = Flag(FPHYSICAL);
     22 	int cdnode;			/* was a node from cdpath added in? */
     23 	int printpath = 0;		/* print where we cd'd? */
     24 	int rval;
     25 	struct tbl *pwd_s, *oldpwd_s;
     26 	XString xs;
     27 	char *xp;
     28 	char *dir, *try, *pwd;
     29 	int phys_path;
     30 	char *cdpath;
     31 	char *fdir = NULL;
     32 
     33 	while ((optc = ksh_getopt(wp, &builtin_opt, "LP")) != EOF)
     34 		switch (optc) {
     35 		case 'L':
     36 			physical = 0;
     37 			break;
     38 		case 'P':
     39 			physical = 1;
     40 			break;
     41 		case '?':
     42 			return 1;
     43 		}
     44 	wp += builtin_opt.optind;
     45 
     46 	if (Flag(FRESTRICTED)) {
     47 		bi_errorf("restricted shell - can't cd");
     48 		return 1;
     49 	}
     50 
     51 	pwd_s = global("PWD");
     52 	oldpwd_s = global("OLDPWD");
     53 
     54 	if (!wp[0]) {
     55 		/* No arguments - go home */
     56 		if ((dir = str_val(global("HOME"))) == null) {
     57 			bi_errorf("no home directory (HOME not set)");
     58 			return 1;
     59 		}
     60 	} else if (!wp[1]) {
     61 		/* One argument: - or dir */
     62 		dir = wp[0];
     63 		if (strcmp(dir, "-") == 0) {
     64 			dir = str_val(oldpwd_s);
     65 			if (dir == null) {
     66 				bi_errorf("no OLDPWD");
     67 				return 1;
     68 			}
     69 			printpath++;
     70 		}
     71 	} else if (!wp[2]) {
     72 		/* Two arguments - substitute arg1 in PWD for arg2 */
     73 		int ilen, olen, nlen, elen;
     74 		char *cp;
     75 
     76 		if (!current_wd[0]) {
     77 			bi_errorf("don't know current directory");
     78 			return 1;
     79 		}
     80 		/* substitute arg1 for arg2 in current path.
     81 		 * if the first substitution fails because the cd fails
     82 		 * we could try to find another substitution. For now
     83 		 * we don't
     84 		 */
     85 		if ((cp = strstr(current_wd, wp[0])) == (char *) 0) {
     86 			bi_errorf("bad substitution");
     87 			return 1;
     88 		}
     89 		ilen = cp - current_wd;
     90 		olen = strlen(wp[0]);
     91 		nlen = strlen(wp[1]);
     92 		elen = strlen(current_wd + ilen + olen) + 1;
     93 		fdir = dir = alloc(ilen + nlen + elen, ATEMP);
     94 		memcpy(dir, current_wd, ilen);
     95 		memcpy(dir + ilen, wp[1], nlen);
     96 		memcpy(dir + ilen + nlen, current_wd + ilen + olen, elen);
     97 		printpath++;
     98 	} else {
     99 		bi_errorf("too many arguments");
    100 		return 1;
    101 	}
    102 
    103 	Xinit(xs, xp, PATH, ATEMP);
    104 	/* xp will have a bogus value after make_path() - set it to 0
    105 	 * so that if it's used, it will cause a dump
    106 	 */
    107 	xp = (char *) 0;
    108 
    109 	cdpath = str_val(global("CDPATH"));
    110 	do {
    111 		cdnode = make_path(current_wd, dir, &cdpath, &xs, &phys_path);
    112 #ifdef S_ISLNK
    113 		if (physical)
    114 			rval = chdir(try = Xstring(xs, xp) + phys_path);
    115 		else
    116 #endif /* S_ISLNK */
    117 		{
    118 			simplify_path(Xstring(xs, xp));
    119 			rval = chdir(try = Xstring(xs, xp));
    120 		}
    121 	} while (rval < 0 && cdpath != (char *) 0);
    122 
    123 	if (rval < 0) {
    124 		if (cdnode)
    125 			bi_errorf("%s: bad directory", dir);
    126 		else
    127 			bi_errorf("%s - %s", try, strerror(errno));
    128 		if (fdir)
    129 			afree(fdir, ATEMP);
    130 		return 1;
    131 	}
    132 
    133 	/* Clear out tracked aliases with relative paths */
    134 	flushcom(0);
    135 
    136 	/* Set OLDPWD (note: unsetting OLDPWD does not disable this
    137 	 * setting in at&t ksh)
    138 	 */
    139 	if (current_wd[0])
    140 		/* Ignore failure (happens if readonly or integer) */
    141 		setstr(oldpwd_s, current_wd, KSH_RETURN_ERROR);
    142 
    143 	if (!ISABSPATH(Xstring(xs, xp))) {
    144 #ifdef OS2
    145 		/* simplify_path() doesn't know about os/2's drive contexts,
    146 		 * so it can't set current_wd when changing to a:foo.
    147 		 * Handle this by calling getcwd()...
    148 		 */
    149 		pwd = ksh_get_wd((char *) 0, 0);
    150 #else /* OS2 */
    151 		pwd = (char *) 0;
    152 #endif /* OS2 */
    153 	} else
    154 #ifdef S_ISLNK
    155 	if (!physical || !(pwd = get_phys_path(Xstring(xs, xp))))
    156 #endif /* S_ISLNK */
    157 		pwd = Xstring(xs, xp);
    158 
    159 	/* Set PWD */
    160 	if (pwd) {
    161 		char *ptmp = pwd;
    162 		set_current_wd(ptmp);
    163 		/* Ignore failure (happens if readonly or integer) */
    164 		setstr(pwd_s, ptmp, KSH_RETURN_ERROR);
    165 	} else {
    166 		set_current_wd(null);
    167 		pwd = Xstring(xs, xp);
    168 		/* XXX unset $PWD? */
    169 	}
    170 	if (printpath || cdnode)
    171 		shprintf("%s\n", pwd);
    172 
    173 	if (fdir)
    174 		afree(fdir, ATEMP);
    175 
    176 	return 0;
    177 }
    178 
    179 int
    180 c_pwd(wp)
    181 	char	**wp;
    182 {
    183 	int optc;
    184 	int physical = Flag(FPHYSICAL);
    185 	char *p, *freep = NULL;
    186 
    187 	while ((optc = ksh_getopt(wp, &builtin_opt, "LP")) != EOF)
    188 		switch (optc) {
    189 		case 'L':
    190 			physical = 0;
    191 			break;
    192 		case 'P':
    193 			physical = 1;
    194 			break;
    195 		case '?':
    196 			return 1;
    197 		}
    198 	wp += builtin_opt.optind;
    199 
    200 	if (wp[0]) {
    201 		bi_errorf("too many arguments");
    202 		return 1;
    203 	}
    204 #ifdef S_ISLNK
    205 	p = current_wd[0] ? (physical ? get_phys_path(current_wd) : current_wd)
    206 			  : (char *) 0;
    207 #else /* S_ISLNK */
    208 	p = current_wd[0] ? current_wd : (char *) 0;
    209 #endif /* S_ISLNK */
    210 	if (p && eaccess(p, R_OK) < 0)
    211 		p = (char *) 0;
    212 	if (!p) {
    213 		freep = p = ksh_get_wd((char *) 0, 0);
    214 		if (!p) {
    215 			bi_errorf("can't get current directory - %s",
    216 				strerror(errno));
    217 			return 1;
    218 		}
    219 	}
    220 	shprintf("%s\n", p);
    221 	if (freep)
    222 		afree(freep, ATEMP);
    223 	return 0;
    224 }
    225 
    226 int
    227 c_print(wp)
    228 	char **wp;
    229 {
    230 #define PO_NL		BIT(0)	/* print newline */
    231 #define PO_EXPAND	BIT(1)	/* expand backslash sequences */
    232 #define PO_PMINUSMINUS	BIT(2)	/* print a -- argument */
    233 #define PO_HIST		BIT(3)	/* print to history instead of stdout */
    234 #define PO_COPROC	BIT(4)	/* printing to coprocess: block SIGPIPE */
    235 #define PO_FSLASH	BIT(5)  /* swap slash for backslash (for os2 ) */
    236 	int fd = 1;
    237 	int flags = PO_EXPAND|PO_NL;
    238 	char *s;
    239 	const char *emsg;
    240 	XString xs;
    241 	char *xp;
    242 
    243 	if (wp[0][0] == 'e') {	/* echo command */
    244 		int nflags = flags;
    245 
    246 		/* A compromise between sysV and BSD echo commands:
    247 		 * escape sequences are enabled by default, and
    248 		 * -n, -e and -E are recognized if they appear
    249 		 * in arguments with no illegal options (ie, echo -nq
    250 		 * will print -nq).
    251 		 * Different from sysV echo since options are recognized,
    252 		 * different from BSD echo since escape sequences are enabled
    253 		 * by default.
    254 		 */
    255 		wp += 1;
    256 		while ((s = *wp) && *s == '-' && s[1]) {
    257 			while (*++s)
    258 				if (*s == 'n')
    259 					nflags &= ~PO_NL;
    260 				else if (*s == 'e')
    261 					nflags |= PO_EXPAND;
    262 				else if (*s == 'E')
    263 					nflags &= ~PO_EXPAND;
    264 				else
    265 					/* bad option: don't use nflags, print
    266 					 * argument
    267 					 */
    268 					break;
    269 			if (*s)
    270 				break;
    271 			wp++;
    272 			flags = nflags;
    273 		}
    274 	} else {
    275 		int optc;
    276 #if OS2
    277 		const char *options = "Rnpfrsu,"; /* added f flag */
    278 #else
    279 		const char *options = "Rnprsu,";
    280 #endif
    281 		while ((optc = ksh_getopt(wp, &builtin_opt, options)) != EOF)
    282 			switch (optc) {
    283 			  case 'R': /* fake BSD echo command */
    284 				flags |= PO_PMINUSMINUS;
    285 				flags &= ~PO_EXPAND;
    286 				options = "ne";
    287 				break;
    288 			  case 'e':
    289 				flags |= PO_EXPAND;
    290 				break;
    291 #ifdef OS2
    292 			  case 'f':
    293 				flags |= PO_FSLASH;
    294 				break;
    295 #endif
    296 			  case 'n':
    297 				flags &= ~PO_NL;
    298 				break;
    299 #ifdef KSH
    300 			  case 'p':
    301 				if ((fd = coproc_getfd(W_OK, &emsg)) < 0) {
    302 					bi_errorf("-p: %s", emsg);
    303 					return 1;
    304 				}
    305 				break;
    306 #endif /* KSH */
    307 			  case 'r':
    308 				flags &= ~PO_EXPAND;
    309 				break;
    310 			  case 's':
    311 				flags |= PO_HIST;
    312 				break;
    313 			  case 'u':
    314 				if (!*(s = builtin_opt.optarg))
    315 					fd = 0;
    316 				else if ((fd = check_fd(s, W_OK, &emsg)) < 0) {
    317 					bi_errorf("-u: %s: %s", s, emsg);
    318 					return 1;
    319 				}
    320 				break;
    321 			  case '?':
    322 				return 1;
    323 			}
    324 		if (!(builtin_opt.info & GI_MINUSMINUS)) {
    325 			/* treat a lone - like -- */
    326 			if (wp[builtin_opt.optind]
    327 			    && strcmp(wp[builtin_opt.optind], "-") == 0)
    328 				builtin_opt.optind++;
    329 		} else if (flags & PO_PMINUSMINUS)
    330 			builtin_opt.optind--;
    331 		wp += builtin_opt.optind;
    332 	}
    333 
    334 	Xinit(xs, xp, 128, ATEMP);
    335 
    336 	while (*wp != NULL) {
    337 		register int c;
    338 		s = *wp;
    339 		while ((c = *s++) != '\0') {
    340 			Xcheck(xs, xp);
    341 #ifdef OS2
    342 			if ((flags & PO_FSLASH) && c == '\\')
    343 				if (*s == '\\')
    344 					*s++;
    345 				else
    346 					c = '/';
    347 #endif /* OS2 */
    348 			if ((flags & PO_EXPAND) && c == '\\') {
    349 				int i;
    350 
    351 				switch ((c = *s++)) {
    352 				/* Oddly enough, \007 seems more portable than
    353 				 * \a (due to HP-UX cc, Ultrix cc, old pcc's,
    354 				 * etc.).
    355 				 */
    356 				case 'a': c = '\007'; break;
    357 				case 'b': c = '\b'; break;
    358 				case 'c': flags &= ~PO_NL;
    359 					  continue; /* AT&T brain damage */
    360 				case 'f': c = '\f'; break;
    361 				case 'n': c = '\n'; break;
    362 				case 'r': c = '\r'; break;
    363 				case 't': c = '\t'; break;
    364 				case 'v': c = 0x0B; break;
    365 				case '0':
    366 					/* Look for an octal number: can have
    367 					 * three digits (not counting the
    368 					 * leading 0).  Truly burnt.
    369 					 */
    370 					c = 0;
    371 					for (i = 0; i < 3; i++) {
    372 						if (*s >= '0' && *s <= '7')
    373 							c = c*8 + *s++ - '0';
    374 						else
    375 							break;
    376 					}
    377 					break;
    378 				case '\0': s--; c = '\\'; break;
    379 				case '\\': break;
    380 				default:
    381 					Xput(xs, xp, '\\');
    382 				}
    383 			}
    384 			Xput(xs, xp, c);
    385 		}
    386 		if (*++wp != NULL)
    387 			Xput(xs, xp, ' ');
    388 	}
    389 	if (flags & PO_NL)
    390 		Xput(xs, xp, '\n');
    391 
    392 	if (flags & PO_HIST) {
    393 		Xput(xs, xp, '\0');
    394 		source->line++;
    395 		histsave(source->line, Xstring(xs, xp), 1);
    396 		Xfree(xs, xp);
    397 	} else {
    398 		int n, len = Xlength(xs, xp);
    399 		int UNINITIALIZED(opipe);
    400 #ifdef KSH
    401 
    402 		/* Ensure we aren't killed by a SIGPIPE while writing to
    403 		 * a coprocess.  at&t ksh doesn't seem to do this (seems
    404 		 * to just check that the co-process is alive, which is
    405 		 * not enough).
    406 		 */
    407 		if (coproc.write >= 0 && coproc.write == fd) {
    408 			flags |= PO_COPROC;
    409 			opipe = block_pipe();
    410 		}
    411 #endif /* KSH */
    412 		for (s = Xstring(xs, xp); len > 0; ) {
    413 			n = write(fd, s, len);
    414 			if (n < 0) {
    415 #ifdef KSH
    416 				if (flags & PO_COPROC)
    417 					restore_pipe(opipe);
    418 #endif /* KSH */
    419 				if (errno == EINTR) {
    420 					/* allow user to ^C out */
    421 					intrcheck();
    422 #ifdef KSH
    423 					if (flags & PO_COPROC)
    424 						opipe = block_pipe();
    425 #endif /* KSH */
    426 					continue;
    427 				}
    428 #ifdef KSH
    429 				/* This doesn't really make sense - could
    430 				 * break scripts (print -p generates
    431 				 * error message).
    432 				*if (errno == EPIPE)
    433 				*	coproc_write_close(fd);
    434 				 */
    435 #endif /* KSH */
    436 				return 1;
    437 			}
    438 			s += n;
    439 			len -= n;
    440 		}
    441 #ifdef KSH
    442 		if (flags & PO_COPROC)
    443 			restore_pipe(opipe);
    444 #endif /* KSH */
    445 	}
    446 
    447 	return 0;
    448 }
    449 
    450 int
    451 c_whence(wp)
    452 	char **wp;
    453 {
    454 	struct tbl *tp;
    455 	char *id;
    456 	int pflag = 0, vflag = 0, Vflag = 0;
    457 	int ret = 0;
    458 	int optc;
    459 	int iam_whence = wp[0][0] == 'w';
    460 	int fcflags;
    461 	const char *options = iam_whence ? "pv" : "pvV";
    462 
    463 	while ((optc = ksh_getopt(wp, &builtin_opt, options)) != EOF)
    464 		switch (optc) {
    465 		case 'p':
    466 			pflag = 1;
    467 			break;
    468 		case 'v':
    469 			vflag = 1;
    470 			break;
    471 		case 'V':
    472 			Vflag = 1;
    473 			break;
    474 		case '?':
    475 			return 1;
    476 		}
    477 	wp += builtin_opt.optind;
    478 
    479 
    480 	fcflags = FC_BI | FC_PATH | FC_FUNC;
    481 	if (!iam_whence) {
    482 		/* Note that -p on its own is deal with in comexec() */
    483 		if (pflag)
    484 			fcflags |= FC_DEFPATH;
    485 		/* Convert command options to whence options - note that
    486 		 * command -pV uses a different path search than whence -v
    487 		 * or whence -pv.  This should be considered a feature.
    488 		 */
    489 		vflag = Vflag;
    490 	}
    491 	if (pflag)
    492 		fcflags &= ~(FC_BI | FC_FUNC);
    493 
    494 	while ((vflag || ret == 0) && (id = *wp++) != NULL) {
    495 		tp = NULL;
    496 		if ((iam_whence || vflag) && !pflag)
    497 			tp = tsearch(&keywords, id, hash(id));
    498 		if (!tp && !pflag) {
    499 			tp = tsearch(&aliases, id, hash(id));
    500 			if (tp && !(tp->flag & ISSET))
    501 				tp = NULL;
    502 		}
    503 		if (!tp)
    504 			tp = findcom(id, fcflags);
    505 		if (vflag || (tp->type != CALIAS && tp->type != CEXEC
    506 			      && tp->type != CTALIAS))
    507 			shprintf("%s", id);
    508 		switch (tp->type) {
    509 		  case CKEYWD:
    510 			if (vflag)
    511 				shprintf(" is a reserved word");
    512 			break;
    513 		  case CALIAS:
    514 			if (vflag)
    515 				shprintf(" is an %salias for ",
    516 					(tp->flag & EXPORT) ? "exported "
    517 							    : null);
    518 			if (!iam_whence && !vflag)
    519 				shprintf("alias %s=", id);
    520 			print_value_quoted(tp->val.s);
    521 			break;
    522 		  case CFUNC:
    523 			if (vflag) {
    524 				shprintf(" is a");
    525 				if (tp->flag & EXPORT)
    526 					shprintf("n exported");
    527 				if (tp->flag & TRACE)
    528 					shprintf(" traced");
    529 				if (!(tp->flag & ISSET)) {
    530 					shprintf(" undefined");
    531 					if (tp->u.fpath)
    532 						shprintf(" (autoload from %s)",
    533 							tp->u.fpath);
    534 				}
    535 				shprintf(" function");
    536 			}
    537 			break;
    538 		  case CSHELL:
    539 			if (vflag)
    540 				shprintf(" is a%s shell builtin",
    541 				    (tp->flag & SPEC_BI) ? " special" : null);
    542 			break;
    543 		  case CTALIAS:
    544 		  case CEXEC:
    545 			if (tp->flag & ISSET) {
    546 				if (vflag) {
    547 					shprintf(" is ");
    548 					if (tp->type == CTALIAS)
    549 						shprintf(
    550 						    "a tracked %salias for ",
    551 							(tp->flag & EXPORT) ?
    552 								"exported "
    553 							      : null);
    554 				}
    555 				shprintf("%s", tp->val.s);
    556 			} else {
    557 				if (vflag)
    558 					shprintf(" not found");
    559 				ret = 1;
    560 			}
    561 			break;
    562 		  default:
    563 			shprintf("%s is *GOK*", id);
    564 			break;
    565 		}
    566 		if (vflag || !ret)
    567 			shprintf("%s", newline);
    568 	}
    569 	return ret;
    570 }
    571 
    572 /* Deal with command -vV - command -p dealt with in comexec() */
    573 int
    574 c_command(wp)
    575 	char **wp;
    576 {
    577 	/* Let c_whence do the work.  Note that c_command() must be
    578 	 * a distinct function from c_whence() (tested in comexec()).
    579 	 */
    580 	return c_whence(wp);
    581 }
    582 
    583 /* typeset, export, and readonly */
    584 int
    585 c_typeset(wp)
    586 	char **wp;
    587 {
    588 	struct block *l = e->loc;
    589 	struct tbl *vp, **p;
    590 	Tflag fset = 0, fclr = 0;
    591 	int thing = 0, func = 0, localv = 0;
    592 	const char *options = "L#R#UZ#fi#lprtux";	/* see comment below */
    593 	char *fieldstr, *basestr;
    594 	int field, base;
    595 	int optc;
    596 	Tflag flag;
    597 	int pflag = 0;
    598 
    599 	switch (**wp) {
    600  	  case 'e':		/* export */
    601  		fset |= EXPORT;
    602 		options = "p";
    603  		break;
    604  	  case 'r':		/* readonly */
    605  		fset |= RDONLY;
    606 		options = "p";
    607  		break;
    608 	  case 's':		/* set */
    609 		/* called with 'typeset -' */
    610 		break;
    611  	  case 't':		/* typeset */
    612  		localv = 1;
    613  		break;
    614  	}
    615 
    616 	fieldstr = basestr = (char *) 0;
    617 	builtin_opt.flags |= GF_PLUSOPT;
    618 	/* at&t ksh seems to have 0-9 as options, which are multiplied
    619 	 * to get a number that is used with -L, -R, -Z or -i (eg, -1R2
    620 	 * sets right justify in a field of 12).  This allows options
    621 	 * to be grouped in an order (eg, -Lu12), but disallows -i8 -L3 and
    622 	 * does not allow the number to be specified as a separate argument
    623 	 * Here, the number must follow the RLZi option, but is optional
    624 	 * (see the # kludge in ksh_getopt()).
    625 	 */
    626 	while ((optc = ksh_getopt(wp, &builtin_opt, options)) != EOF) {
    627 		flag = 0;
    628 		switch (optc) {
    629 		  case 'L':
    630 			flag = LJUST;
    631 			fieldstr = builtin_opt.optarg;
    632 			break;
    633 		  case 'R':
    634 			flag = RJUST;
    635 			fieldstr = builtin_opt.optarg;
    636 			break;
    637 		  case 'U':
    638 			/* at&t ksh uses u, but this conflicts with
    639 			 * upper/lower case.  If this option is changed,
    640 			 * need to change the -U below as well
    641 			 */
    642 			flag = INT_U;
    643 			break;
    644 		  case 'Z':
    645 			flag = ZEROFIL;
    646 			fieldstr = builtin_opt.optarg;
    647 			break;
    648 		  case 'f':
    649 			func = 1;
    650 			break;
    651 		  case 'i':
    652 			flag = INTEGER;
    653 			basestr = builtin_opt.optarg;
    654 			break;
    655 		  case 'l':
    656 			flag = LCASEV;
    657 			break;
    658 		  case 'p': /* posix export/readonly -p flag.
    659 			     * typeset -p is the same as typeset (in pdksh);
    660 			     * here for compatibility with ksh93.
    661 			     */
    662 			pflag = 1;
    663 			break;
    664 		  case 'r':
    665 			flag = RDONLY;
    666 			break;
    667 		  case 't':
    668 			flag = TRACE;
    669 			break;
    670 		  case 'u':
    671 			flag = UCASEV_AL;	/* upper case / autoload */
    672 			break;
    673 		  case 'x':
    674 			flag = EXPORT;
    675 			break;
    676 		  case '?':
    677 			return 1;
    678 		}
    679 		if (builtin_opt.info & GI_PLUS) {
    680 			fclr |= flag;
    681 			fset &= ~flag;
    682 			thing = '+';
    683 		} else {
    684 			fset |= flag;
    685 			fclr &= ~flag;
    686 			thing = '-';
    687 		}
    688 	}
    689 
    690 	field = 0;
    691 	if (fieldstr && !bi_getn(fieldstr, &field))
    692 		return 1;
    693 	base = 0;
    694 	if (basestr && !bi_getn(basestr, &base))
    695 		return 1;
    696 
    697 	if (!(builtin_opt.info & GI_MINUSMINUS) && wp[builtin_opt.optind]
    698 	    && (wp[builtin_opt.optind][0] == '-'
    699 		|| wp[builtin_opt.optind][0] == '+')
    700 	    && wp[builtin_opt.optind][1] == '\0')
    701 	{
    702 		thing = wp[builtin_opt.optind][0];
    703 		builtin_opt.optind++;
    704 	}
    705 
    706 	if (func && ((fset|fclr) & ~(TRACE|UCASEV_AL|EXPORT))) {
    707 		bi_errorf("only -t, -u and -x options may be used with -f");
    708 		return 1;
    709 	}
    710 	if (wp[builtin_opt.optind]) {
    711 		/* Take care of exclusions.
    712 		 * At this point, flags in fset are cleared in fclr and vise
    713 		 * versa.  This property should be preserved.
    714 		 */
    715 		if (fset & LCASEV)	/* LCASEV has priority over UCASEV_AL */
    716 			fset &= ~UCASEV_AL;
    717 		if (fset & LJUST)	/* LJUST has priority over RJUST */
    718 			fset &= ~RJUST;
    719 		if ((fset & (ZEROFIL|LJUST)) == ZEROFIL) { /* -Z implies -ZR */
    720 			fset |= RJUST;
    721 			fclr &= ~RJUST;
    722 		}
    723 		/* Setting these attributes clears the others, unless they
    724 		 * are also set in this command
    725 		 */
    726 		if (fset & (LJUST|RJUST|ZEROFIL|UCASEV_AL|LCASEV|INTEGER
    727 			    |INT_U|INT_L))
    728 			fclr |= ~fset &
    729 				(LJUST|RJUST|ZEROFIL|UCASEV_AL|LCASEV|INTEGER
    730 				 |INT_U|INT_L);
    731 	}
    732 
    733 	/* set variables and attributes */
    734 	if (wp[builtin_opt.optind]) {
    735 		int i;
    736 		int rval = 0;
    737 		struct tbl *f;
    738 
    739 		if (localv && !func)
    740 			fset |= LOCAL;
    741 		for (i = builtin_opt.optind; wp[i]; i++) {
    742 			if (func) {
    743 				f = findfunc(wp[i], hash(wp[i]),
    744 					     (fset&UCASEV_AL) ? TRUE : FALSE);
    745 				if (!f) {
    746 					/* at&t ksh does ++rval: bogus */
    747 					rval = 1;
    748 					continue;
    749 				}
    750 				if (fset | fclr) {
    751 					f->flag |= fset;
    752 					f->flag &= ~fclr;
    753 				} else
    754 					fptreef(shl_stdout, 0,
    755 						f->flag & FKSH ?
    756 						    "function %s %T\n"
    757 						    : "%s() %T\n"
    758 						,
    759 						wp[i], f->val.t);
    760 			} else if (!typeset(wp[i], fset, fclr, field, base)) {
    761 				bi_errorf("%s: not identifier", wp[i]);
    762 				return 1;
    763 			}
    764 		}
    765 		return rval;
    766 	}
    767 
    768 	/* list variables and attributes */
    769 	flag = fset | fclr; /* no difference at this point.. */
    770 	if (func) {
    771 	    for (l = e->loc; l; l = l->next) {
    772 		for (p = tsort(&l->funs); (vp = *p++); ) {
    773 		    if (flag && (vp->flag & flag) == 0)
    774 			    continue;
    775 		    if (thing == '-')
    776 			fptreef(shl_stdout, 0, vp->flag & FKSH ?
    777 						    "function %s %T\n"
    778 						    : "%s() %T\n",
    779 				vp->name, vp->val.t);
    780 		    else
    781 			shprintf("%s\n", vp->name);
    782 		}
    783 	    }
    784 	} else {
    785 	    for (l = e->loc; l; l = l->next) {
    786 		for (p = tsort(&l->vars); (vp = *p++); ) {
    787 		    struct tbl *tvp;
    788 		    int any_set = 0;
    789 		    /*
    790 		     * See if the parameter is set (for arrays, if any
    791 		     * element is set).
    792 		     */
    793 		    for (tvp = vp; tvp; tvp = tvp->u.array)
    794 			if (tvp->flag & ISSET) {
    795 			    any_set = 1;
    796 			    break;
    797 			}
    798 		    /*
    799 		     * Check attributes - note that all array elements
    800 		     * have (should have?) the same attributes, so checking
    801 		     * the first is sufficient.
    802 		     *
    803 		     * Report an unset param only if the user has
    804 		     * explicitly given it some attribute (like export);
    805 		     * otherwise, after "echo $FOO", we would report FOO...
    806 		     */
    807 		    if (!any_set && !(vp->flag & USERATTRIB))
    808 			continue;
    809 		    if (flag && (vp->flag & flag) == 0)
    810 			continue;
    811 		    for (; vp; vp = vp->u.array) {
    812 			/* Ignore array elements that aren't set unless there
    813 			 * are no set elements, in which case the first is
    814 			 * reported on
    815 			 */
    816 			if ((vp->flag&ARRAY) && any_set && !(vp->flag & ISSET))
    817 			    continue;
    818 			/* no arguments */
    819 			if (thing == 0 && flag == 0) {
    820 			    /* at&t ksh prints things like export, integer,
    821 			     * leftadj, zerofill, etc., but POSIX says must
    822 			     * be suitable for re-entry...
    823 			     */
    824 			    shprintf("typeset ");
    825 			    if ((vp->flag&INTEGER))
    826 				shprintf("-i ");
    827 			    if ((vp->flag&EXPORT))
    828 				shprintf("-x ");
    829 			    if ((vp->flag&RDONLY))
    830 				shprintf("-r ");
    831 			    if ((vp->flag&TRACE))
    832 				shprintf("-t ");
    833 			    if ((vp->flag&LJUST))
    834 				shprintf("-L%d ", vp->u2.field);
    835 			    if ((vp->flag&RJUST))
    836 				shprintf("-R%d ", vp->u2.field);
    837 			    if ((vp->flag&ZEROFIL))
    838 				shprintf("-Z ");
    839 			    if ((vp->flag&LCASEV))
    840 				shprintf("-l ");
    841 			    if ((vp->flag&UCASEV_AL))
    842 				shprintf("-u ");
    843 			    if ((vp->flag&INT_U))
    844 				shprintf("-U ");
    845 			    shprintf("%s\n", vp->name);
    846 			    if (vp->flag&ARRAY)
    847 				break;
    848 			} else {
    849 			    if (pflag)
    850 				shprintf("%s ",
    851 				    (flag & EXPORT) ?  "export" : "readonly");
    852 			    if ((vp->flag&ARRAY) && any_set)
    853 				shprintf("%s[%d]", vp->name, vp->index);
    854 			    else
    855 				shprintf("%s", vp->name);
    856 			    if (thing == '-' && (vp->flag&ISSET)) {
    857 				char *s = str_val(vp);
    858 
    859 				shprintf("=");
    860 				/* at&t ksh can't have justified integers.. */
    861 				if ((vp->flag & (INTEGER|LJUST|RJUST))
    862 								== INTEGER)
    863 				    shprintf("%s", s);
    864 				else
    865 				    print_value_quoted(s);
    866 			    }
    867 			    shprintf("%s", newline);
    868 			}
    869 			/* Only report first `element' of an array with
    870 			 * no set elements.
    871 			 */
    872 			if (!any_set)
    873 			    break;
    874 		    }
    875 		}
    876 	    }
    877 	}
    878 	return 0;
    879 }
    880 
    881 int
    882 c_alias(wp)
    883 	char **wp;
    884 {
    885 	struct table *t = &aliases;
    886 	int rv = 0, rflag = 0, tflag, Uflag = 0, pflag = 0;
    887 	int prefix = 0;
    888 	Tflag xflag = 0;
    889 	int optc;
    890 
    891 	builtin_opt.flags |= GF_PLUSOPT;
    892 	while ((optc = ksh_getopt(wp, &builtin_opt, "dprtUx")) != EOF) {
    893 		prefix = builtin_opt.info & GI_PLUS ? '+' : '-';
    894 		switch (optc) {
    895 		  case 'd':
    896 			t = &homedirs;
    897 			break;
    898 		  case 'p':
    899 			pflag = 1;
    900 			break;
    901 		  case 'r':
    902 			rflag = 1;
    903 			break;
    904 		  case 't':
    905 			t = &taliases;
    906 			break;
    907 		  case 'U': /* kludge for tracked alias initialization
    908 			     * (don't do a path search, just make an entry)
    909 			     */
    910 			Uflag = 1;
    911 			break;
    912 		  case 'x':
    913 			xflag = EXPORT;
    914 			break;
    915 		  case '?':
    916 			return 1;
    917 		}
    918 	}
    919 	wp += builtin_opt.optind;
    920 
    921 	if (!(builtin_opt.info & GI_MINUSMINUS) && *wp
    922 	    && (wp[0][0] == '-' || wp[0][0] == '+') && wp[0][1] == '\0')
    923 	{
    924 		prefix = wp[0][0];
    925 		wp++;
    926 	}
    927 
    928 	tflag = t == &taliases;
    929 
    930 	/* "hash -r" means reset all the tracked aliases.. */
    931 	if (rflag) {
    932 		static const char *const args[] = {
    933 			    "unalias", "-ta", (const char *) 0
    934 			};
    935 
    936 		if (!tflag || *wp) {
    937 			shprintf(
    938 	    "alias: -r flag can only be used with -t and without arguments\n");
    939 			return 1;
    940 		}
    941 		ksh_getopt_reset(&builtin_opt, GF_ERROR);
    942 		return c_unalias((char **)__UNCONST(args));
    943 	}
    944 
    945 
    946 	if (*wp == NULL) {
    947 		struct tbl *ap, **p;
    948 
    949 		for (p = tsort(t); (ap = *p++) != NULL; )
    950 			if ((ap->flag & (ISSET|xflag)) == (ISSET|xflag)) {
    951 				if (pflag)
    952 					shf_puts("alias ", shl_stdout);
    953 				shf_puts(ap->name, shl_stdout);
    954 				if (prefix != '+') {
    955 					shf_putc('=', shl_stdout);
    956 					print_value_quoted(ap->val.s);
    957 				}
    958 				shprintf("%s", newline);
    959 			}
    960 	}
    961 
    962 	for (; *wp != NULL; wp++) {
    963 		char *alias = *wp;
    964 		char *val = strchr(alias, '=');
    965 		char *newval;
    966 		struct tbl *ap;
    967 		int h;
    968 
    969 		if (val)
    970 			alias = str_nsave(alias, val++ - alias, ATEMP);
    971 		h = hash(alias);
    972 		if (val == NULL && !tflag && !xflag) {
    973 			ap = tsearch(t, alias, h);
    974 			if (ap != NULL && (ap->flag&ISSET)) {
    975 				if (pflag)
    976 					shf_puts("alias ", shl_stdout);
    977 				shf_puts(ap->name, shl_stdout);
    978 				if (prefix != '+') {
    979 					shf_putc('=', shl_stdout);
    980 					print_value_quoted(ap->val.s);
    981 				}
    982 				shprintf("%s", newline);
    983 			} else {
    984 				shprintf("%s alias not found\n", alias);
    985 				rv = 1;
    986 			}
    987 			continue;
    988 		}
    989 		ap = tenter(t, alias, h);
    990 		ap->type = tflag ? CTALIAS : CALIAS;
    991 		/* Are we setting the value or just some flags? */
    992 		if ((val && !tflag) || (!val && tflag && !Uflag)) {
    993 			if (ap->flag&ALLOC) {
    994 				ap->flag &= ~(ALLOC|ISSET);
    995 				afree((void*)ap->val.s, APERM);
    996 			}
    997 			/* ignore values for -t (at&t ksh does this) */
    998 			newval = tflag ? search(alias, path, X_OK, (int *) 0)
    999 					: val;
   1000 			if (newval) {
   1001 				ap->val.s = str_save(newval, APERM);
   1002 				ap->flag |= ALLOC|ISSET;
   1003 			} else
   1004 				ap->flag &= ~ISSET;
   1005 		}
   1006 		ap->flag |= DEFINED;
   1007 		if (prefix == '+')
   1008 			ap->flag &= ~xflag;
   1009 		else
   1010 			ap->flag |= xflag;
   1011 		if (val)
   1012 			afree(alias, ATEMP);
   1013 	}
   1014 
   1015 	return rv;
   1016 }
   1017 
   1018 int
   1019 c_unalias(wp)
   1020 	char **wp;
   1021 {
   1022 	register struct table *t = &aliases;
   1023 	register struct tbl *ap;
   1024 	int rv = 0, all = 0;
   1025 	int optc;
   1026 
   1027 	while ((optc = ksh_getopt(wp, &builtin_opt, "adt")) != EOF)
   1028 		switch (optc) {
   1029 		  case 'a':
   1030 			all = 1;
   1031 			break;
   1032 		  case 'd':
   1033 			t = &homedirs;
   1034 			break;
   1035 		  case 't':
   1036 			t = &taliases;
   1037 			break;
   1038 		  case '?':
   1039 			return 1;
   1040 		}
   1041 	wp += builtin_opt.optind;
   1042 
   1043 	for (; *wp != NULL; wp++) {
   1044 		ap = tsearch(t, *wp, hash(*wp));
   1045 		if (ap == NULL) {
   1046 			rv = 1;	/* POSIX */
   1047 			continue;
   1048 		}
   1049 		if (ap->flag&ALLOC) {
   1050 			ap->flag &= ~(ALLOC|ISSET);
   1051 			afree((void*)ap->val.s, APERM);
   1052 		}
   1053 		ap->flag &= ~(DEFINED|ISSET|EXPORT);
   1054 	}
   1055 
   1056 	if (all) {
   1057 		struct tstate ts;
   1058 
   1059 		for (twalk(&ts, t); (ap = tnext(&ts)); ) {
   1060 			if (ap->flag&ALLOC) {
   1061 				ap->flag &= ~(ALLOC|ISSET);
   1062 				afree((void*)ap->val.s, APERM);
   1063 			}
   1064 			ap->flag &= ~(DEFINED|ISSET|EXPORT);
   1065 		}
   1066 	}
   1067 
   1068 	return rv;
   1069 }
   1070 
   1071 #ifdef KSH
   1072 int
   1073 c_let(wp)
   1074 	char **wp;
   1075 {
   1076 	int rv = 1;
   1077 	long val;
   1078 
   1079 	if (wp[1] == (char *) 0) /* at&t ksh does this */
   1080 		bi_errorf("no arguments");
   1081 	else
   1082 		for (wp++; *wp; wp++)
   1083 			if (!evaluate(*wp, &val, KSH_RETURN_ERROR)) {
   1084 				rv = 2;	/* distinguish error from zero result */
   1085 				break;
   1086 			} else
   1087 				rv = val == 0;
   1088 	return rv;
   1089 }
   1090 #endif /* KSH */
   1091 
   1092 int
   1093 c_jobs(wp)
   1094 	char **wp;
   1095 {
   1096 	int optc;
   1097 	int flag = 0;
   1098 	int nflag = 0;
   1099 	int rv = 0;
   1100 
   1101 	while ((optc = ksh_getopt(wp, &builtin_opt, "lpnz")) != EOF)
   1102 		switch (optc) {
   1103 		  case 'l':
   1104 			flag = 1;
   1105 			break;
   1106 		  case 'p':
   1107 			flag = 2;
   1108 			break;
   1109 		  case 'n':
   1110 			nflag = 1;
   1111 			break;
   1112 		  case 'z':	/* debugging: print zombies */
   1113 			nflag = -1;
   1114 			break;
   1115 		  case '?':
   1116 			return 1;
   1117 		}
   1118 	wp += builtin_opt.optind;
   1119 	if (!*wp) {
   1120 		if (j_jobs((char *) 0, flag, nflag))
   1121 			rv = 1;
   1122 	} else {
   1123 		for (; *wp; wp++)
   1124 			if (j_jobs(*wp, flag, nflag))
   1125 				rv = 1;
   1126 	}
   1127 	return rv;
   1128 }
   1129 
   1130 #ifdef JOBS
   1131 int
   1132 c_fgbg(wp)
   1133 	char **wp;
   1134 {
   1135 	int bg = strcmp(*wp, "bg") == 0;
   1136 	int UNINITIALIZED(rv);
   1137 
   1138 	if (!Flag(FMONITOR)) {
   1139 		bi_errorf("job control not enabled");
   1140 		return 1;
   1141 	}
   1142 	if (ksh_getopt(wp, &builtin_opt, null) == '?')
   1143 		return 1;
   1144 	wp += builtin_opt.optind;
   1145 	if (*wp)
   1146 		for (; *wp; wp++)
   1147 			rv = j_resume(*wp, bg);
   1148 	else
   1149 		rv = j_resume("%%", bg);
   1150 	/* POSIX says fg shall return 0 (unless an error occurs).
   1151 	 * at&t ksh returns the exit value of the job...
   1152 	 */
   1153 	return (bg || Flag(FPOSIX)) ? 0 : rv;
   1154 }
   1155 #endif
   1156 
   1157 struct kill_info {
   1158 	int num_width;
   1159 	int name_width;
   1160 };
   1161 static char *kill_fmt_entry ARGS((void *arg, int i, char *buf, int buflen));
   1162 
   1163 /* format a single kill item */
   1164 static char *
   1165 kill_fmt_entry(arg, i, buf, buflen)
   1166 	void *arg;
   1167 	int i;
   1168 	char *buf;
   1169 	int buflen;
   1170 {
   1171 	struct kill_info *ki = (struct kill_info *) arg;
   1172 
   1173 	i++;
   1174 	if (sigtraps[i].name)
   1175 		shf_snprintf(buf, buflen, "%*d %*s %s",
   1176 			ki->num_width, i,
   1177 			ki->name_width, sigtraps[i].name,
   1178 			sigtraps[i].mess);
   1179 	else
   1180 		shf_snprintf(buf, buflen, "%*d %*d %s",
   1181 			ki->num_width, i,
   1182 			ki->name_width, sigtraps[i].signal,
   1183 			sigtraps[i].mess);
   1184 	return buf;
   1185 }
   1186 
   1187 
   1188 int
   1189 c_kill(wp)
   1190 	char **wp;
   1191 {
   1192 	Trap *t = (Trap *) 0;
   1193 	char *p;
   1194 	int lflag = 0;
   1195 	int i, n, rv, sig;
   1196 
   1197 	/* assume old style options if -digits or -UPPERCASE */
   1198 	if ((p = wp[1]) && *p == '-'
   1199 	    && (digit(p[1]) || isupper((unsigned char)p[1]))) {
   1200 		if (!(t = gettrap(p + 1, TRUE))) {
   1201 			bi_errorf("bad signal `%s'", p + 1);
   1202 			return 1;
   1203 		}
   1204 		i = (wp[2] && strcmp(wp[2], "--") == 0) ? 3 : 2;
   1205 	} else {
   1206 		int optc;
   1207 
   1208 		while ((optc = ksh_getopt(wp, &builtin_opt, "ls:")) != EOF)
   1209 			switch (optc) {
   1210 			  case 'l':
   1211 				lflag = 1;
   1212 				break;
   1213 			  case 's':
   1214 				if (!(t = gettrap(builtin_opt.optarg, TRUE))) {
   1215 					bi_errorf("bad signal `%s'",
   1216 						builtin_opt.optarg);
   1217 					return 1;
   1218 				}
   1219 				break;
   1220 			  case '?':
   1221 				return 1;
   1222 			}
   1223 		i = builtin_opt.optind;
   1224 	}
   1225 	if ((lflag && t) || (!wp[i] && !lflag)) {
   1226 		shf_fprintf(shl_out,
   1227 "usage: kill [ -s signame | -signum | -signame ] {pid|job}...\n\
   1228        kill -l [exit_status]\n"
   1229 			);
   1230 		bi_errorf("%s", null);
   1231 		return 1;
   1232 	}
   1233 
   1234 	if (lflag) {
   1235 		if (wp[i]) {
   1236 			for (; wp[i]; i++) {
   1237 				if (!bi_getn(wp[i], &n))
   1238 					return 1;
   1239 				if (n > 128 && n < 128 + SIGNALS)
   1240 					n -= 128;
   1241 				if (n > 0 && n < SIGNALS && sigtraps[n].name)
   1242 					shprintf("%s\n", sigtraps[n].name);
   1243 				else
   1244 					shprintf("%d\n", n);
   1245 			}
   1246 		} else if (Flag(FPOSIX)) {
   1247 			p = null;
   1248 			for (i = 1; i < SIGNALS; i++, p = space)
   1249 				if (sigtraps[i].name)
   1250 					shprintf("%s%s", p, sigtraps[i].name);
   1251 			shprintf("%s", newline);
   1252 		} else {
   1253 			int w, si;
   1254 			int mess_width;
   1255 			struct kill_info ki;
   1256 
   1257 			for (si = SIGNALS, ki.num_width = 1; si >= 10; si /= 10)
   1258 				ki.num_width++;
   1259 			ki.name_width = mess_width = 0;
   1260 			for (si = 0; si < SIGNALS; si++) {
   1261 				w = sigtraps[si].name ?
   1262 				    (int)strlen(sigtraps[si].name) :
   1263 				    ki.num_width;
   1264 				if (w > ki.name_width)
   1265 					ki.name_width = w;
   1266 				w = strlen(sigtraps[si].mess);
   1267 				if (w > mess_width)
   1268 					mess_width = w;
   1269 			}
   1270 
   1271 			print_columns(shl_stdout, SIGNALS - 1,
   1272 				kill_fmt_entry, (void *) &ki,
   1273 				ki.num_width + ki.name_width + mess_width + 3, 1);
   1274 		}
   1275 		return 0;
   1276 	}
   1277 	rv = 0;
   1278 	sig = t ? t->signal : SIGTERM;
   1279 	for (; (p = wp[i]); i++) {
   1280 		if (*p == '%') {
   1281 			if (j_kill(p, sig))
   1282 				rv = 1;
   1283 		} else if (!getn(p, &n)) {
   1284 			bi_errorf("%s: arguments must be jobs or process IDs",
   1285 				p);
   1286 			rv = 1;
   1287 		} else {
   1288 			/* use killpg if < -1 since -1 does special things for
   1289 			 * some non-killpg-endowed kills
   1290 			 */
   1291 			if ((n < -1 ? killpg(-n, sig) : kill(n, sig)) < 0) {
   1292 				bi_errorf("%s: %s", p, strerror(errno));
   1293 				rv = 1;
   1294 			}
   1295 		}
   1296 	}
   1297 	return rv;
   1298 }
   1299 
   1300 void
   1301 getopts_reset(val)
   1302 	int val;
   1303 {
   1304 	if (val >= 1) {
   1305 		ksh_getopt_reset(&user_opt,
   1306 			GF_NONAME | (Flag(FPOSIX) ? 0 : GF_PLUSOPT));
   1307 		user_opt.optind = user_opt.uoptind = val;
   1308 	}
   1309 }
   1310 
   1311 int
   1312 c_getopts(wp)
   1313 	char **wp;
   1314 {
   1315 	int	argc;
   1316 	const char *options;
   1317 	const char *var;
   1318 	int	optc;
   1319 	int	ret;
   1320 	char	buf[3];
   1321 	struct tbl *vq, *voptarg;
   1322 
   1323 	if (ksh_getopt(wp, &builtin_opt, null) == '?')
   1324 		return 1;
   1325 	wp += builtin_opt.optind;
   1326 
   1327 	options = *wp++;
   1328 	if (!options) {
   1329 		bi_errorf("missing options argument");
   1330 		return 1;
   1331 	}
   1332 
   1333 	var = *wp++;
   1334 	if (!var) {
   1335 		bi_errorf("missing name argument");
   1336 		return 1;
   1337 	}
   1338 	if (!*var || *skip_varname(var, TRUE)) {
   1339 		bi_errorf("%s: is not an identifier", var);
   1340 		return 1;
   1341 	}
   1342 
   1343 	if (e->loc->next == (struct block *) 0) {
   1344 		internal_errorf(0, "c_getopts: no argv");
   1345 		return 1;
   1346 	}
   1347 	/* Which arguments are we parsing... */
   1348 	if (*wp == (char *) 0)
   1349 		wp = e->loc->next->argv;
   1350 	else
   1351 		*--wp = e->loc->next->argv[0];
   1352 
   1353 	/* Check that our saved state won't cause a core dump... */
   1354 	for (argc = 0; wp[argc]; argc++)
   1355 		;
   1356 	if (user_opt.optind > argc
   1357 	    || (user_opt.p != 0
   1358 		&& user_opt.p > strlen(wp[user_opt.optind - 1])))
   1359 	{
   1360 	      bi_errorf("arguments changed since last call");
   1361 	      return 1;
   1362 	}
   1363 
   1364 	user_opt.optarg = (char *) 0;
   1365 	optc = ksh_getopt(wp, &user_opt, options);
   1366 
   1367 	if (optc >= 0 && optc != '?' && (user_opt.info & GI_PLUS)) {
   1368 		buf[0] = '+';
   1369 		buf[1] = optc;
   1370 		buf[2] = '\0';
   1371 	} else {
   1372 		/* POSIX says var is set to ? at end-of-options, at&t ksh
   1373 		 * sets it to null - we go with POSIX...
   1374 		 */
   1375 		buf[0] = optc < 0 ? '?' : optc;
   1376 		buf[1] = '\0';
   1377 	}
   1378 
   1379 	/* at&t ksh does not change OPTIND if it was an unknown option.
   1380 	 * Scripts counting on this are prone to break... (ie, don't count
   1381 	 * on this staying).
   1382 	 */
   1383 	if (optc != '?') {
   1384 		user_opt.uoptind = user_opt.optind;
   1385 	}
   1386 
   1387 	voptarg = global("OPTARG");
   1388 	voptarg->flag &= ~RDONLY;	/* at&t ksh clears ro and int */
   1389 	/* Paranoia: ensure no bizarre results. */
   1390 	if (voptarg->flag & INTEGER)
   1391 	    typeset("OPTARG", 0, INTEGER, 0, 0);
   1392 	if (user_opt.optarg == (char *) 0)
   1393 		unset(voptarg, 0);
   1394 	else
   1395 		/* This can't fail (have cleared readonly/integer) */
   1396 		setstr(voptarg, user_opt.optarg, KSH_RETURN_ERROR);
   1397 
   1398 	ret = 0;
   1399 
   1400 	vq = global(var);
   1401 	/* Error message already printed (integer, readonly) */
   1402 	if (!setstr(vq, buf, KSH_RETURN_ERROR))
   1403 	    ret = 1;
   1404 	if (Flag(FEXPORT))
   1405 		typeset(var, EXPORT, 0, 0, 0);
   1406 
   1407 	return optc < 0 ? 1 : ret;
   1408 }
   1409 
   1410 #ifdef EMACS
   1411 int
   1412 c_bind(wp)
   1413 	char **wp;
   1414 {
   1415 	int rv = 0, macro = 0, list = 0;
   1416 	register char *cp;
   1417 	int optc;
   1418 
   1419 	while ((optc = ksh_getopt(wp, &builtin_opt, "lm")) != EOF)
   1420 		switch (optc) {
   1421 		  case 'l':
   1422 			list = 1;
   1423 			break;
   1424 		  case 'm':
   1425 			macro = 1;
   1426 			break;
   1427 		  case '?':
   1428 			return 1;
   1429 		}
   1430 	wp += builtin_opt.optind;
   1431 
   1432 	if (*wp == NULL)	/* list all */
   1433 		rv = x_bind(NULL, NULL, 0, list);
   1434 
   1435 	for (; *wp != NULL; wp++) {
   1436 		cp = strchr(*wp, '=');
   1437 		if (cp != NULL)
   1438 			*cp++ = '\0';
   1439 		if (x_bind(*wp, cp, macro, 0))
   1440 			rv = 1;
   1441 	}
   1442 
   1443 	return rv;
   1444 }
   1445 #endif
   1446 
   1447 /* A leading = means assignments before command are kept;
   1448  * a leading * means a POSIX special builtin;
   1449  * a leading + means a POSIX regular builtin
   1450  * (* and + should not be combined).
   1451  */
   1452 const struct builtin kshbuiltins [] = {
   1453 	{"+alias", c_alias},	/* no =: at&t manual wrong */
   1454 	{"+cd", c_cd},
   1455 	{"+command", c_command},
   1456 	{"echo", c_print},
   1457  	{"*=export", c_typeset},
   1458 #ifdef HISTORY
   1459 	{"+fc", c_fc},
   1460 #endif /* HISTORY */
   1461 	{"+getopts", c_getopts},
   1462 	{"+jobs", c_jobs},
   1463 	{"+kill", c_kill},
   1464 #ifdef KSH
   1465 	{"let", c_let},
   1466 #endif /* KSH */
   1467 	{"print", c_print},
   1468 	{"pwd", c_pwd},
   1469  	{"*=readonly", c_typeset},
   1470 	{"=typeset", c_typeset},
   1471 	{"+unalias", c_unalias},
   1472 	{"whence", c_whence},
   1473 #ifdef JOBS
   1474 	{"+bg", c_fgbg},
   1475 	{"+fg", c_fgbg},
   1476 #endif
   1477 #ifdef EMACS
   1478 	{"bind", c_bind},
   1479 #endif
   1480 	{NULL, NULL}
   1481 };
   1482