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