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