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