xref: /openbsd-src/bin/ksh/misc.c (revision 505ee9ea3b177e2387d907a91ca7da069f3f14d8)
1 /*	$OpenBSD: misc.c,v 1.74 2020/07/07 10:33:58 jca Exp $	*/
2 
3 /*
4  * Miscellaneous functions
5  */
6 
7 #include <ctype.h>
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <limits.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <unistd.h>
14 
15 #include "sh.h"
16 #include "charclass.h"
17 
18 short ctypes [UCHAR_MAX+1];	/* type bits for unsigned char */
19 static int dropped_privileges;
20 
21 static int	do_gmatch(const unsigned char *, const unsigned char *,
22 		    const unsigned char *, const unsigned char *);
23 static const unsigned char *cclass(const unsigned char *, int);
24 
25 /*
26  * Fast character classes
27  */
28 void
29 setctypes(const char *s, int t)
30 {
31 	int i;
32 
33 	if (t & C_IFS) {
34 		for (i = 0; i < UCHAR_MAX+1; i++)
35 			ctypes[i] &= ~C_IFS;
36 		ctypes[0] |= C_IFS; /* include \0 in C_IFS */
37 	}
38 	while (*s != 0)
39 		ctypes[(unsigned char) *s++] |= t;
40 }
41 
42 void
43 initctypes(void)
44 {
45 	int c;
46 
47 	for (c = 'a'; c <= 'z'; c++)
48 		ctypes[c] |= C_ALPHA;
49 	for (c = 'A'; c <= 'Z'; c++)
50 		ctypes[c] |= C_ALPHA;
51 	ctypes['_'] |= C_ALPHA;
52 	setctypes(" \t\n|&;<>()", C_LEX1); /* \0 added automatically */
53 	setctypes("*@#!$-?", C_VAR1);
54 	setctypes(" \t\n", C_IFSWS);
55 	setctypes("=-+?", C_SUBOP1);
56 	setctypes("#%", C_SUBOP2);
57 	setctypes(" \n\t\"#$&'()*;<>?[\\`|", C_QUOTE);
58 }
59 
60 /* convert uint64_t to base N string */
61 
62 char *
63 u64ton(uint64_t n, int base)
64 {
65 	char *p;
66 	static char buf [20];
67 
68 	p = &buf[sizeof(buf)];
69 	*--p = '\0';
70 	do {
71 		*--p = "0123456789ABCDEF"[n%base];
72 		n /= base;
73 	} while (n != 0);
74 	return p;
75 }
76 
77 char *
78 str_save(const char *s, Area *ap)
79 {
80 	size_t len;
81 	char *p;
82 
83 	if (!s)
84 		return NULL;
85 	len = strlen(s)+1;
86 	p = alloc(len, ap);
87 	strlcpy(p, s, len);
88 	return (p);
89 }
90 
91 /* Allocate a string of size n+1 and copy upto n characters from the possibly
92  * null terminated string s into it.  Always returns a null terminated string
93  * (unless n < 0).
94  */
95 char *
96 str_nsave(const char *s, int n, Area *ap)
97 {
98 	char *ns;
99 
100 	if (n < 0)
101 		return 0;
102 	ns = alloc(n + 1, ap);
103 	ns[0] = '\0';
104 	return strncat(ns, s, n);
105 }
106 
107 /* called from expand.h:XcheckN() to grow buffer */
108 char *
109 Xcheck_grow_(XString *xsp, char *xp, size_t more)
110 {
111 	char *old_beg = xsp->beg;
112 
113 	xsp->len += more > xsp->len ? more : xsp->len;
114 	xsp->beg = aresize(xsp->beg, xsp->len + 8, xsp->areap);
115 	xsp->end = xsp->beg + xsp->len;
116 	return xsp->beg + (xp - old_beg);
117 }
118 
119 const struct option sh_options[] = {
120 	/* Special cases (see parse_args()): -A, -o, -s.
121 	 * Options are sorted by their longnames - the order of these
122 	 * entries MUST match the order of sh_flag F* enumerations in sh.h.
123 	 */
124 	{ "allexport",	'a',		OF_ANY },
125 	{ "braceexpand",  0,		OF_ANY }, /* non-standard */
126 	{ "bgnice",	  0,		OF_ANY },
127 	{ NULL,	'c',	    OF_CMDLINE },
128 	{ "csh-history",  0,		OF_ANY }, /* non-standard */
129 #ifdef EMACS
130 	{ "emacs",	  0,		OF_ANY },
131 #endif
132 	{ "errexit",	'e',		OF_ANY },
133 #ifdef EMACS
134 	{ "gmacs",	  0,		OF_ANY },
135 #endif
136 	{ "ignoreeof",	  0,		OF_ANY },
137 	{ "interactive",'i',	    OF_CMDLINE },
138 	{ "keyword",	'k',		OF_ANY },
139 	{ "login",	'l',	    OF_CMDLINE },
140 	{ "markdirs",	'X',		OF_ANY },
141 	{ "monitor",	'm',		OF_ANY },
142 	{ "noclobber",	'C',		OF_ANY },
143 	{ "noexec",	'n',		OF_ANY },
144 	{ "noglob",	'f',		OF_ANY },
145 	{ "nohup",	  0,		OF_ANY },
146 	{ "nolog",	  0,		OF_ANY }, /* no effect */
147 	{ "notify",	'b',		OF_ANY },
148 	{ "nounset",	'u',		OF_ANY },
149 	{ "physical",	  0,		OF_ANY }, /* non-standard */
150 	{ "pipefail",	  0,		OF_ANY }, /* non-standard */
151 	{ "posix",	  0,		OF_ANY }, /* non-standard */
152 	{ "privileged",	'p',		OF_ANY },
153 	{ "restricted",	'r',	    OF_CMDLINE },
154 	{ "sh",		  0,		OF_ANY }, /* non-standard */
155 	{ "stdin",	's',	    OF_CMDLINE }, /* pseudo non-standard */
156 	{ "trackall",	'h',		OF_ANY },
157 	{ "verbose",	'v',		OF_ANY },
158 #ifdef VI
159 	{ "vi",		  0,		OF_ANY },
160 	{ "viraw",	  0,		OF_ANY }, /* no effect */
161 	{ "vi-show8",	  0,		OF_ANY }, /* non-standard */
162 	{ "vi-tabcomplete",  0,		OF_ANY }, /* non-standard */
163 	{ "vi-esccomplete",  0,		OF_ANY }, /* non-standard */
164 #endif
165 	{ "xtrace",	'x',		OF_ANY },
166 	/* Anonymous flags: used internally by shell only
167 	 * (not visible to user)
168 	 */
169 	{ NULL,	0,		OF_INTERNAL }, /* FTALKING_I */
170 };
171 
172 /*
173  * translate -o option into F* constant (also used for test -o option)
174  */
175 int
176 option(const char *n)
177 {
178 	unsigned int ele;
179 
180 	for (ele = 0; ele < NELEM(sh_options); ele++)
181 		if (sh_options[ele].name && strcmp(sh_options[ele].name, n) == 0)
182 			return ele;
183 
184 	return -1;
185 }
186 
187 struct options_info {
188 	int opt_width;
189 	struct {
190 		const char *name;
191 		int	flag;
192 	} opts[NELEM(sh_options)];
193 };
194 
195 static char *options_fmt_entry(void *arg, int i, char *buf, int buflen);
196 static void printoptions(int verbose);
197 
198 /* format a single select menu item */
199 static char *
200 options_fmt_entry(void *arg, int i, char *buf, int buflen)
201 {
202 	struct options_info *oi = (struct options_info *) arg;
203 
204 	shf_snprintf(buf, buflen, "%-*s %s",
205 	    oi->opt_width, oi->opts[i].name,
206 	    Flag(oi->opts[i].flag) ? "on" : "off");
207 	return buf;
208 }
209 
210 static void
211 printoptions(int verbose)
212 {
213 	unsigned int ele;
214 
215 	if (verbose) {
216 		struct options_info oi;
217 		unsigned int n;
218 		int len;
219 
220 		/* verbose version */
221 		shprintf("Current option settings\n");
222 
223 		for (ele = n = oi.opt_width = 0; ele < NELEM(sh_options); ele++) {
224 			if (sh_options[ele].name) {
225 				len = strlen(sh_options[ele].name);
226 				oi.opts[n].name = sh_options[ele].name;
227 				oi.opts[n++].flag = ele;
228 				if (len > oi.opt_width)
229 					oi.opt_width = len;
230 			}
231 		}
232 		print_columns(shl_stdout, n, options_fmt_entry, &oi,
233 		    oi.opt_width + 5, 1);
234 	} else {
235 		/* short version ala ksh93 */
236 		shprintf("set");
237 		for (ele = 0; ele < NELEM(sh_options); ele++) {
238 			if (sh_options[ele].name)
239 				shprintf(" %co %s",
240 					 Flag(ele) ? '-' : '+',
241 					 sh_options[ele].name);
242 		}
243 		shprintf("\n");
244 	}
245 }
246 
247 char *
248 getoptions(void)
249 {
250 	unsigned int ele;
251 	char m[(int) FNFLAGS + 1];
252 	char *cp = m;
253 
254 	for (ele = 0; ele < NELEM(sh_options); ele++)
255 		if (sh_options[ele].c && Flag(ele))
256 			*cp++ = sh_options[ele].c;
257 	*cp = 0;
258 	return str_save(m, ATEMP);
259 }
260 
261 /* change a Flag(*) value; takes care of special actions */
262 void
263 change_flag(enum sh_flag f,
264     int what,		/* flag to change */
265     int newval)		/* what is changing the flag (command line vs set) */
266 {
267 	int oldval;
268 
269 	oldval = Flag(f);
270 	Flag(f) = newval;
271 	if (f == FMONITOR) {
272 		if (what != OF_CMDLINE && newval != oldval)
273 			j_change();
274 	} else
275 	if (0
276 #ifdef VI
277 	    || f == FVI
278 #endif /* VI */
279 #ifdef EMACS
280 	    || f == FEMACS || f == FGMACS
281 #endif /* EMACS */
282 	   )
283 	{
284 		if (newval) {
285 #ifdef VI
286 			Flag(FVI) = 0;
287 #endif /* VI */
288 #ifdef EMACS
289 			Flag(FEMACS) = Flag(FGMACS) = 0;
290 #endif /* EMACS */
291 			Flag(f) = newval;
292 		}
293 	} else
294 	/* Turning off -p? */
295 	if (f == FPRIVILEGED && oldval && !newval && issetugid() &&
296 	    !dropped_privileges) {
297 		gid_t gid = getgid();
298 
299 		setresgid(gid, gid, gid);
300 		setgroups(1, &gid);
301 		setresuid(ksheuid, ksheuid, ksheuid);
302 
303 		if (pledge("stdio rpath wpath cpath fattr flock getpw proc "
304 		    "exec tty", NULL) == -1)
305 			bi_errorf("pledge fail");
306 		dropped_privileges = 1;
307 	} else if (f == FPOSIX && newval) {
308 		Flag(FBRACEEXPAND) = 0;
309 	}
310 	/* Changing interactive flag? */
311 	if (f == FTALKING) {
312 		if ((what == OF_CMDLINE || what == OF_SET) && procpid == kshpid)
313 			Flag(FTALKING_I) = newval;
314 	}
315 }
316 
317 /* parse command line & set command arguments.  returns the index of
318  * non-option arguments, -1 if there is an error.
319  */
320 int
321 parse_args(char **argv,
322     int what,			/* OF_CMDLINE or OF_SET */
323     int *setargsp)
324 {
325 	static char cmd_opts[NELEM(sh_options) + 3]; /* o:\0 */
326 	static char set_opts[NELEM(sh_options) + 5]; /* Ao;s\0 */
327 	char *opts;
328 	char *array = NULL;
329 	Getopt go;
330 	int i, optc, sortargs = 0, arrayset = 0;
331 	unsigned int ele;
332 
333 	/* First call?  Build option strings... */
334 	if (cmd_opts[0] == '\0') {
335 		char *p, *q;
336 
337 		/* see cmd_opts[] declaration */
338 		strlcpy(cmd_opts, "o:", sizeof cmd_opts);
339 		p = cmd_opts + strlen(cmd_opts);
340 		/* see set_opts[] declaration */
341 		strlcpy(set_opts, "A:o;s", sizeof set_opts);
342 		q = set_opts + strlen(set_opts);
343 		for (ele = 0; ele < NELEM(sh_options); ele++) {
344 			if (sh_options[ele].c) {
345 				if (sh_options[ele].flags & OF_CMDLINE)
346 					*p++ = sh_options[ele].c;
347 				if (sh_options[ele].flags & OF_SET)
348 					*q++ = sh_options[ele].c;
349 			}
350 		}
351 		*p = '\0';
352 		*q = '\0';
353 	}
354 
355 	if (what == OF_CMDLINE) {
356 		char *p;
357 		/* Set FLOGIN before parsing options so user can clear
358 		 * flag using +l.
359 		 */
360 		Flag(FLOGIN) = (argv[0][0] == '-' ||
361 		    ((p = strrchr(argv[0], '/')) && *++p == '-'));
362 		opts = cmd_opts;
363 	} else
364 		opts = set_opts;
365 	ksh_getopt_reset(&go, GF_ERROR|GF_PLUSOPT);
366 	while ((optc = ksh_getopt(argv, &go, opts)) != -1) {
367 		int set = (go.info & GI_PLUS) ? 0 : 1;
368 		switch (optc) {
369 		case 'A':
370 			arrayset = set ? 1 : -1;
371 			array = go.optarg;
372 			break;
373 
374 		case 'o':
375 			if (go.optarg == NULL) {
376 				/* lone -o: print options
377 				 *
378 				 * Note that on the command line, -o requires
379 				 * an option (ie, can't get here if what is
380 				 * OF_CMDLINE).
381 				 */
382 				printoptions(set);
383 				break;
384 			}
385 			i = option(go.optarg);
386 			if (i != -1 && set == Flag(i))
387 				/* Don't check the context if the flag
388 				 * isn't changing - makes "set -o interactive"
389 				 * work if you're already interactive.  Needed
390 				 * if the output of "set +o" is to be used.
391 				 */
392 				;
393 			else if (i != -1 && (sh_options[i].flags & what))
394 				change_flag((enum sh_flag) i, what, set);
395 			else {
396 				bi_errorf("%s: bad option", go.optarg);
397 				return -1;
398 			}
399 			break;
400 
401 		case '?':
402 			return -1;
403 
404 		default:
405 			/* -s: sort positional params (at&t ksh stupidity) */
406 			if (what == OF_SET && optc == 's') {
407 				sortargs = 1;
408 				break;
409 			}
410 			for (ele = 0; ele < NELEM(sh_options); ele++)
411 				if (optc == sh_options[ele].c &&
412 				    (what & sh_options[ele].flags)) {
413 					change_flag((enum sh_flag) ele, what,
414 					    set);
415 					break;
416 				}
417 			if (ele == NELEM(sh_options)) {
418 				internal_errorf("%s: `%c'", __func__, optc);
419 				return -1; /* not reached */
420 			}
421 		}
422 	}
423 	if (!(go.info & GI_MINUSMINUS) && argv[go.optind] &&
424 	    (argv[go.optind][0] == '-' || argv[go.optind][0] == '+') &&
425 	    argv[go.optind][1] == '\0') {
426 		/* lone - clears -v and -x flags */
427 		if (argv[go.optind][0] == '-' && !Flag(FPOSIX))
428 			Flag(FVERBOSE) = Flag(FXTRACE) = 0;
429 		/* set skips lone - or + option */
430 		go.optind++;
431 	}
432 	if (setargsp)
433 		/* -- means set $#/$* even if there are no arguments */
434 		*setargsp = !arrayset && ((go.info & GI_MINUSMINUS) ||
435 		    argv[go.optind]);
436 
437 	if (arrayset && (!*array || *skip_varname(array, false))) {
438 		bi_errorf("%s: is not an identifier", array);
439 		return -1;
440 	}
441 	if (sortargs) {
442 		for (i = go.optind; argv[i]; i++)
443 			;
444 		qsortp((void **) &argv[go.optind], (size_t) (i - go.optind),
445 		    xstrcmp);
446 	}
447 	if (arrayset) {
448 		set_array(array, arrayset, argv + go.optind);
449 		for (; argv[go.optind]; go.optind++)
450 			;
451 	}
452 
453 	return go.optind;
454 }
455 
456 /* parse a decimal number: returns 0 if string isn't a number, 1 otherwise */
457 int
458 getn(const char *as, int *ai)
459 {
460 	char *p;
461 	long n;
462 
463 	n = strtol(as, &p, 10);
464 
465 	if (!*as || *p || INT_MIN >= n || n >= INT_MAX)
466 		return 0;
467 
468 	*ai = (int)n;
469 	return 1;
470 }
471 
472 /* getn() that prints error */
473 int
474 bi_getn(const char *as, int *ai)
475 {
476 	int rv = getn(as, ai);
477 
478 	if (!rv)
479 		bi_errorf("%s: bad number", as);
480 	return rv;
481 }
482 
483 /* -------- gmatch.c -------- */
484 
485 /*
486  * int gmatch(string, pattern)
487  * char *string, *pattern;
488  *
489  * Match a pattern as in sh(1).
490  * pattern character are prefixed with MAGIC by expand.
491  */
492 
493 int
494 gmatch(const char *s, const char *p, int isfile)
495 {
496 	const char *se, *pe;
497 
498 	if (s == NULL || p == NULL)
499 		return 0;
500 	se = s + strlen(s);
501 	pe = p + strlen(p);
502 	/* isfile is false iff no syntax check has been done on
503 	 * the pattern.  If check fails, just to a strcmp().
504 	 */
505 	if (!isfile && !has_globbing(p, pe)) {
506 		size_t len = pe - p + 1;
507 		char tbuf[64];
508 		char *t = len <= sizeof(tbuf) ? tbuf :
509 		    alloc(len, ATEMP);
510 		debunk(t, p, len);
511 		return !strcmp(t, s);
512 	}
513 	return do_gmatch((const unsigned char *) s, (const unsigned char *) se,
514 	    (const unsigned char *) p, (const unsigned char *) pe);
515 }
516 
517 /* Returns if p is a syntacticly correct globbing pattern, false
518  * if it contains no pattern characters or if there is a syntax error.
519  * Syntax errors are:
520  *	- [ with no closing ]
521  *	- imbalanced $(...) expression
522  *	- [...] and *(...) not nested (eg, [a$(b|]c), *(a[b|c]d))
523  */
524 /*XXX
525 - if no magic,
526 	if dest given, copy to dst
527 	return ?
528 - if magic && (no globbing || syntax error)
529 	debunk to dst
530 	return ?
531 - return ?
532 */
533 int
534 has_globbing(const char *xp, const char *xpe)
535 {
536 	const unsigned char *p = (const unsigned char *) xp;
537 	const unsigned char *pe = (const unsigned char *) xpe;
538 	int c;
539 	int nest = 0, bnest = 0;
540 	int saw_glob = 0;
541 	int in_bracket = 0; /* inside [...] */
542 
543 	for (; p < pe; p++) {
544 		if (!ISMAGIC(*p))
545 			continue;
546 		if ((c = *++p) == '*' || c == '?')
547 			saw_glob = 1;
548 		else if (c == '[') {
549 			if (!in_bracket) {
550 				saw_glob = 1;
551 				in_bracket = 1;
552 				if (ISMAGIC(p[1]) && p[2] == '!')
553 					p += 2;
554 				if (ISMAGIC(p[1]) && p[2] == ']')
555 					p += 2;
556 			}
557 			/* XXX Do we need to check ranges here? POSIX Q */
558 		} else if (c == ']') {
559 			if (in_bracket) {
560 				if (bnest)		/* [a*(b]) */
561 					return 0;
562 				in_bracket = 0;
563 			}
564 		} else if ((c & 0x80) && strchr("*+?@! ", c & 0x7f)) {
565 			saw_glob = 1;
566 			if (in_bracket)
567 				bnest++;
568 			else
569 				nest++;
570 		} else if (c == '|') {
571 			if (in_bracket && !bnest)	/* *(a[foo|bar]) */
572 				return 0;
573 		} else if (c == /*(*/ ')') {
574 			if (in_bracket) {
575 				if (!bnest--)		/* *(a[b)c] */
576 					return 0;
577 			} else if (nest)
578 				nest--;
579 		}
580 		/* else must be a MAGIC-MAGIC, or MAGIC-!, MAGIC--, MAGIC-]
581 			 MAGIC-{, MAGIC-,, MAGIC-} */
582 	}
583 	return saw_glob && !in_bracket && !nest;
584 }
585 
586 /* Function must return either 0 or 1 (assumed by code for 0x80|'!') */
587 static int
588 do_gmatch(const unsigned char *s, const unsigned char *se,
589     const unsigned char *p, const unsigned char *pe)
590 {
591 	int sc, pc;
592 	const unsigned char *prest, *psub, *pnext;
593 	const unsigned char *srest;
594 
595 	if (s == NULL || p == NULL)
596 		return 0;
597 	while (p < pe) {
598 		pc = *p++;
599 		sc = s < se ? *s : '\0';
600 		s++;
601 		if (!ISMAGIC(pc)) {
602 			if (sc != pc)
603 				return 0;
604 			continue;
605 		}
606 		switch (*p++) {
607 		case '[':
608 			if (sc == 0 || (p = cclass(p, sc)) == NULL)
609 				return 0;
610 			break;
611 
612 		case '?':
613 			if (sc == 0)
614 				return 0;
615 			break;
616 
617 		case '*':
618 			if (p == pe)
619 				return 1;
620 			s--;
621 			do {
622 				if (do_gmatch(s, se, p, pe))
623 					return 1;
624 			} while (s++ < se);
625 			return 0;
626 
627 		  /*
628 		   * [*+?@!](pattern|pattern|..)
629 		   *
630 		   * Not ifdef'd KSH as this is needed for ${..%..}, etc.
631 		   */
632 		case 0x80|'+': /* matches one or more times */
633 		case 0x80|'*': /* matches zero or more times */
634 			if (!(prest = pat_scan(p, pe, 0)))
635 				return 0;
636 			s--;
637 			/* take care of zero matches */
638 			if (p[-1] == (0x80 | '*') &&
639 			    do_gmatch(s, se, prest, pe))
640 				return 1;
641 			for (psub = p; ; psub = pnext) {
642 				pnext = pat_scan(psub, pe, 1);
643 				for (srest = s; srest <= se; srest++) {
644 					if (do_gmatch(s, srest, psub, pnext - 2) &&
645 					    (do_gmatch(srest, se, prest, pe) ||
646 					    (s != srest && do_gmatch(srest,
647 					    se, p - 2, pe))))
648 						return 1;
649 				}
650 				if (pnext == prest)
651 					break;
652 			}
653 			return 0;
654 
655 		case 0x80|'?': /* matches zero or once */
656 		case 0x80|'@': /* matches one of the patterns */
657 		case 0x80|' ': /* simile for @ */
658 			if (!(prest = pat_scan(p, pe, 0)))
659 				return 0;
660 			s--;
661 			/* Take care of zero matches */
662 			if (p[-1] == (0x80 | '?') &&
663 			    do_gmatch(s, se, prest, pe))
664 				return 1;
665 			for (psub = p; ; psub = pnext) {
666 				pnext = pat_scan(psub, pe, 1);
667 				srest = prest == pe ? se : s;
668 				for (; srest <= se; srest++) {
669 					if (do_gmatch(s, srest, psub, pnext - 2) &&
670 					    do_gmatch(srest, se, prest, pe))
671 						return 1;
672 				}
673 				if (pnext == prest)
674 					break;
675 			}
676 			return 0;
677 
678 		case 0x80|'!': /* matches none of the patterns */
679 			if (!(prest = pat_scan(p, pe, 0)))
680 				return 0;
681 			s--;
682 			for (srest = s; srest <= se; srest++) {
683 				int matched = 0;
684 
685 				for (psub = p; ; psub = pnext) {
686 					pnext = pat_scan(psub, pe, 1);
687 					if (do_gmatch(s, srest, psub,
688 					    pnext - 2)) {
689 						matched = 1;
690 						break;
691 					}
692 					if (pnext == prest)
693 						break;
694 				}
695 				if (!matched &&
696 				    do_gmatch(srest, se, prest, pe))
697 					return 1;
698 			}
699 			return 0;
700 
701 		default:
702 			if (sc != p[-1])
703 				return 0;
704 			break;
705 		}
706 	}
707 	return s == se;
708 }
709 
710 static int
711 posix_cclass(const unsigned char *pattern, int test, const unsigned char **ep)
712 {
713 	struct cclass *cc;
714 	const unsigned char *colon;
715 	size_t len;
716 	int rval = 0;
717 
718 	if ((colon = strchr(pattern, ':')) == NULL || colon[1] != MAGIC) {
719 		*ep = pattern - 2;
720 		return -1;
721 	}
722 	*ep = colon + 3; /* skip MAGIC */
723 	len = (size_t)(colon - pattern);
724 
725 	for (cc = cclasses; cc->name != NULL; cc++) {
726 		if (!strncmp(pattern, cc->name, len) && cc->name[len] == '\0') {
727 			if (cc->isctype(test))
728 				rval = 1;
729 			break;
730 		}
731 	}
732 	if (cc->name == NULL) {
733 		rval = -2;	/* invalid character class */
734 	}
735 	return rval;
736 }
737 
738 static const unsigned char *
739 cclass(const unsigned char *p, int sub)
740 {
741 	int c, d, rv, not, found = 0;
742 	const unsigned char *orig_p = p;
743 
744 	if ((not = (ISMAGIC(*p) && *++p == '!')))
745 		p++;
746 	do {
747 		/* check for POSIX character class (e.g. [[:alpha:]]) */
748 		if ((p[0] == MAGIC && p[1] == '[' && p[2] == ':') ||
749 		    (p[0] == '[' && p[1] == ':')) {
750 			do {
751 				const char *pp = p + (*p == MAGIC) + 2;
752 				rv = posix_cclass(pp, sub, &p);
753 				switch (rv) {
754 				case 1:
755 					found = 1;
756 					break;
757 				case -2:
758 					return NULL;
759 				}
760 			} while (rv != -1 && p[0] == MAGIC && p[1] == '[' && p[2] == ':');
761 			if (p[0] == MAGIC && p[1] == ']')
762 				break;
763 		}
764 
765 		c = *p++;
766 		if (ISMAGIC(c)) {
767 			c = *p++;
768 			if ((c & 0x80) && !ISMAGIC(c)) {
769 				c &= 0x7f;/* extended pattern matching: *+?@! */
770 				/* XXX the ( char isn't handled as part of [] */
771 				if (c == ' ') /* simile for @: plain (..) */
772 					c = '(' /*)*/;
773 			}
774 		}
775 		if (c == '\0')
776 			/* No closing ] - act as if the opening [ was quoted */
777 			return sub == '[' ? orig_p : NULL;
778 		if (ISMAGIC(p[0]) && p[1] == '-' &&
779 		    (!ISMAGIC(p[2]) || p[3] != ']')) {
780 			p += 2; /* MAGIC- */
781 			d = *p++;
782 			if (ISMAGIC(d)) {
783 				d = *p++;
784 				if ((d & 0x80) && !ISMAGIC(d))
785 					d &= 0x7f;
786 			}
787 			/* POSIX says this is an invalid expression */
788 			if (c > d)
789 				return NULL;
790 		} else
791 			d = c;
792 		if (c == sub || (c <= sub && sub <= d))
793 			found = 1;
794 	} while (!(ISMAGIC(p[0]) && p[1] == ']'));
795 
796 	return (found != not) ? p+2 : NULL;
797 }
798 
799 /* Look for next ) or | (if match_sep) in *(foo|bar) pattern */
800 const unsigned char *
801 pat_scan(const unsigned char *p, const unsigned char *pe, int match_sep)
802 {
803 	int nest = 0;
804 
805 	for (; p < pe; p++) {
806 		if (!ISMAGIC(*p))
807 			continue;
808 		if ((*++p == /*(*/ ')' && nest-- == 0) ||
809 		    (*p == '|' && match_sep && nest == 0))
810 			return ++p;
811 		if ((*p & 0x80) && strchr("*+?@! ", *p & 0x7f))
812 			nest++;
813 	}
814 	return NULL;
815 }
816 
817 /*
818  * quick sort of array of generic pointers to objects.
819  */
820 void
821 qsortp(void **base,			/* base address */
822     size_t n,				/* elements */
823     int (*f) (const void *, const void *)) /* compare function */
824 {
825 	qsort(base, n, sizeof(char *), f);
826 }
827 
828 int
829 xstrcmp(const void *p1, const void *p2)
830 {
831 	return (strcmp(*(char **)p1, *(char **)p2));
832 }
833 
834 /* Initialize a Getopt structure */
835 void
836 ksh_getopt_reset(Getopt *go, int flags)
837 {
838 	go->optind = 1;
839 	go->optarg = NULL;
840 	go->p = 0;
841 	go->flags = flags;
842 	go->info = 0;
843 	go->buf[1] = '\0';
844 }
845 
846 
847 /* getopt() used for shell built-in commands, the getopts command, and
848  * command line options.
849  * A leading ':' in options means don't print errors, instead return '?'
850  * or ':' and set go->optarg to the offending option character.
851  * If GF_ERROR is set (and option doesn't start with :), errors result in
852  * a call to bi_errorf().
853  *
854  * Non-standard features:
855  *	- ';' is like ':' in options, except the argument is optional
856  *	  (if it isn't present, optarg is set to 0).
857  *	  Used for 'set -o'.
858  *	- ',' is like ':' in options, except the argument always immediately
859  *	  follows the option character (optarg is set to the null string if
860  *	  the option is missing).
861  *	  Used for 'read -u2', 'print -u2' and fc -40.
862  *	- '#' is like ':' in options, expect that the argument is optional
863  *	  and must start with a digit or be the string "unlimited".  If the
864  *	  argument doesn't match, it is assumed to be missing and normal option
865  *	  processing continues (optarg is set to 0 if the option is missing).
866  *	  Used for 'typeset -LZ4' and 'ulimit -adunlimited'.
867  *	- accepts +c as well as -c IF the GF_PLUSOPT flag is present.  If an
868  *	  option starting with + is accepted, the GI_PLUS flag will be set
869  *	  in go->info.
870  */
871 int
872 ksh_getopt(char **argv, Getopt *go, const char *options)
873 {
874 	char c;
875 	char *o;
876 
877 	if (go->p == 0 || (c = argv[go->optind - 1][go->p]) == '\0') {
878 		char *arg = argv[go->optind], flag = arg ? *arg : '\0';
879 
880 		go->p = 1;
881 		if (flag == '-' && arg[1] == '-' && arg[2] == '\0') {
882 			go->optind++;
883 			go->p = 0;
884 			go->info |= GI_MINUSMINUS;
885 			return -1;
886 		}
887 		if (arg == NULL ||
888 		    ((flag != '-' ) && /* neither a - nor a + (if + allowed) */
889 		    (!(go->flags & GF_PLUSOPT) || flag != '+')) ||
890 		    (c = arg[1]) == '\0') {
891 			go->p = 0;
892 			return -1;
893 		}
894 		go->optind++;
895 		go->info &= ~(GI_MINUS|GI_PLUS);
896 		go->info |= flag == '-' ? GI_MINUS : GI_PLUS;
897 	}
898 	go->p++;
899 	if (c == '?' || c == ':' || c == ';' || c == ',' || c == '#' ||
900 	    !(o = strchr(options, c))) {
901 		if (options[0] == ':') {
902 			go->buf[0] = c;
903 			go->optarg = go->buf;
904 		} else {
905 			warningf(true, "%s%s-%c: unknown option",
906 			    (go->flags & GF_NONAME) ? "" : argv[0],
907 			    (go->flags & GF_NONAME) ? "" : ": ", c);
908 			if (go->flags & GF_ERROR)
909 				bi_errorf(NULL);
910 		}
911 		return '?';
912 	}
913 	/* : means argument must be present, may be part of option argument
914 	 *   or the next argument
915 	 * ; same as : but argument may be missing
916 	 * , means argument is part of option argument, and may be null.
917 	 */
918 	if (*++o == ':' || *o == ';') {
919 		if (argv[go->optind - 1][go->p])
920 			go->optarg = argv[go->optind - 1] + go->p;
921 		else if (argv[go->optind])
922 			go->optarg = argv[go->optind++];
923 		else if (*o == ';')
924 			go->optarg = NULL;
925 		else {
926 			if (options[0] == ':') {
927 				go->buf[0] = c;
928 				go->optarg = go->buf;
929 				return ':';
930 			}
931 			warningf(true, "%s%s-`%c' requires argument",
932 			    (go->flags & GF_NONAME) ? "" : argv[0],
933 			    (go->flags & GF_NONAME) ? "" : ": ", c);
934 			if (go->flags & GF_ERROR)
935 				bi_errorf(NULL);
936 			return '?';
937 		}
938 		go->p = 0;
939 	} else if (*o == ',') {
940 		/* argument is attached to option character, even if null */
941 		go->optarg = argv[go->optind - 1] + go->p;
942 		go->p = 0;
943 	} else if (*o == '#') {
944 		/* argument is optional and may be attached or unattached
945 		 * but must start with a digit.  optarg is set to 0 if the
946 		 * argument is missing.
947 		 */
948 		if (argv[go->optind - 1][go->p]) {
949 			if (digit(argv[go->optind - 1][go->p]) ||
950 			    !strcmp(&argv[go->optind - 1][go->p], "unlimited")) {
951 				go->optarg = argv[go->optind - 1] + go->p;
952 				go->p = 0;
953 			} else
954 				go->optarg = NULL;
955 		} else {
956 			if (argv[go->optind] && (digit(argv[go->optind][0]) ||
957 			    !strcmp(argv[go->optind], "unlimited"))) {
958 				go->optarg = argv[go->optind++];
959 				go->p = 0;
960 			} else
961 				go->optarg = NULL;
962 		}
963 	}
964 	return c;
965 }
966 
967 /* print variable/alias value using necessary quotes
968  * (POSIX says they should be suitable for re-entry...)
969  * No trailing newline is printed.
970  */
971 void
972 print_value_quoted(const char *s)
973 {
974 	const char *p;
975 	int inquote = 0;
976 
977 	/* Test if any quotes are needed */
978 	for (p = s; *p; p++)
979 		if (ctype(*p, C_QUOTE))
980 			break;
981 	if (!*p) {
982 		shprintf("%s", s);
983 		return;
984 	}
985 	for (p = s; *p; p++) {
986 		if (*p == '\'') {
987 			shprintf(inquote ? "'\\'" : "\\'");
988 			inquote = 0;
989 		} else {
990 			if (!inquote) {
991 				shprintf("'");
992 				inquote = 1;
993 			}
994 			shf_putc(*p, shl_stdout);
995 		}
996 	}
997 	if (inquote)
998 		shprintf("'");
999 }
1000 
1001 /* Print things in columns and rows - func() is called to format the ith
1002  * element
1003  */
1004 void
1005 print_columns(struct shf *shf, int n, char *(*func) (void *, int, char *, int),
1006     void *arg, int max_width, int prefcol)
1007 {
1008 	char *str = alloc(max_width + 1, ATEMP);
1009 	int i;
1010 	int r, c;
1011 	int rows, cols;
1012 	int nspace;
1013 	int col_width;
1014 
1015 	/* max_width + 1 for the space.  Note that no space
1016 	 * is printed after the last column to avoid problems
1017 	 * with terminals that have auto-wrap.
1018 	 */
1019 	cols = x_cols / (max_width + 1);
1020 	if (!cols)
1021 		cols = 1;
1022 	rows = (n + cols - 1) / cols;
1023 	if (prefcol && n && cols > rows) {
1024 		int tmp = rows;
1025 
1026 		rows = cols;
1027 		cols = tmp;
1028 		if (rows > n)
1029 			rows = n;
1030 	}
1031 
1032 	col_width = max_width;
1033 	if (cols == 1)
1034 		col_width = 0; /* Don't pad entries in single column output. */
1035 	nspace = (x_cols - max_width * cols) / cols;
1036 	if (nspace <= 0)
1037 		nspace = 1;
1038 	for (r = 0; r < rows; r++) {
1039 		for (c = 0; c < cols; c++) {
1040 			i = c * rows + r;
1041 			if (i < n) {
1042 				shf_fprintf(shf, "%-*s",
1043 				    col_width,
1044 				    (*func)(arg, i, str, max_width + 1));
1045 				if (c + 1 < cols)
1046 					shf_fprintf(shf, "%*s", nspace, "");
1047 			}
1048 		}
1049 		shf_putchar('\n', shf);
1050 	}
1051 	afree(str, ATEMP);
1052 }
1053 
1054 /* Strip any nul bytes from buf - returns new length (nbytes - # of nuls) */
1055 int
1056 strip_nuls(char *buf, int nbytes)
1057 {
1058 	char *dst;
1059 
1060 	if ((dst = memchr(buf, '\0', nbytes))) {
1061 		char *end = buf + nbytes;
1062 		char *p, *q;
1063 
1064 		for (p = dst; p < end; p = q) {
1065 			/* skip a block of nulls */
1066 			while (++p < end && *p == '\0')
1067 				;
1068 			/* find end of non-null block */
1069 			if (!(q = memchr(p, '\0', end - p)))
1070 				q = end;
1071 			memmove(dst, p, q - p);
1072 			dst += q - p;
1073 		}
1074 		*dst = '\0';
1075 		return dst - buf;
1076 	}
1077 	return nbytes;
1078 }
1079 
1080 /* Like read(2), but if read fails due to non-blocking flag, resets flag
1081  * and restarts read.
1082  */
1083 int
1084 blocking_read(int fd, char *buf, int nbytes)
1085 {
1086 	int ret;
1087 	int tried_reset = 0;
1088 
1089 	while ((ret = read(fd, buf, nbytes)) == -1) {
1090 		if (!tried_reset && errno == EAGAIN) {
1091 			int oerrno = errno;
1092 			if (reset_nonblock(fd) > 0) {
1093 				tried_reset = 1;
1094 				continue;
1095 			}
1096 			errno = oerrno;
1097 		}
1098 		break;
1099 	}
1100 	return ret;
1101 }
1102 
1103 /* Reset the non-blocking flag on the specified file descriptor.
1104  * Returns -1 if there was an error, 0 if non-blocking wasn't set,
1105  * 1 if it was.
1106  */
1107 int
1108 reset_nonblock(int fd)
1109 {
1110 	int flags;
1111 
1112 	if ((flags = fcntl(fd, F_GETFL)) == -1)
1113 		return -1;
1114 	if (!(flags & O_NONBLOCK))
1115 		return 0;
1116 	flags &= ~O_NONBLOCK;
1117 	if (fcntl(fd, F_SETFL, flags) == -1)
1118 		return -1;
1119 	return 1;
1120 }
1121 
1122 
1123 /* Like getcwd(), except bsize is ignored if buf is 0 (PATH_MAX is used) */
1124 char *
1125 ksh_get_wd(char *buf, int bsize)
1126 {
1127 	char *b;
1128 	char *ret;
1129 
1130 	/* Note: we could just use plain getcwd(), but then we'd had to
1131 	 * inject possibly allocated space into the ATEMP area. */
1132 	/* Assume getcwd() available */
1133 	if (!buf) {
1134 		bsize = PATH_MAX;
1135 		b = alloc(PATH_MAX + 1, ATEMP);
1136 	} else
1137 		b = buf;
1138 
1139 	ret = getcwd(b, bsize);
1140 
1141 	if (!buf) {
1142 		if (ret)
1143 			ret = aresize(b, strlen(b) + 1, ATEMP);
1144 		else
1145 			afree(b, ATEMP);
1146 	}
1147 
1148 	return ret;
1149 }
1150