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