1305 lines · 27.7 KB · C
RawDownload
/* * Miscellaneous functions */ #include "sh.h"#include <ctype.h>	/* for FILECHCONV */#ifdef HAVE_LIMITS_H# include <limits.h>#endif #ifndef UCHAR_MAX# define UCHAR_MAX	0xFF#endif short ctypes [UCHAR_MAX+1];	/* type bits for unsigned char */ static int	do_gmatch ARGS((const unsigned char *s, const unsigned char *p,			const unsigned char *se, const unsigned char *pe,			int isfile));static const unsigned char *cclass ARGS((const unsigned char *p, int sub)); /* * Fast character classes */voidsetctypes(s, t)	register const char *s;	register int t;{	register int i; 	if (t & C_IFS) {		for (i = 0; i < UCHAR_MAX+1; i++)			ctypes[i] &= ~C_IFS;		ctypes[0] |= C_IFS; /* include \0 in C_IFS */	}	while (*s != 0)		ctypes[(unsigned char) *s++] |= t;} voidinitctypes(){	register int c; 	for (c = 'a'; c <= 'z'; c++)		ctypes[c] |= C_ALPHA;	for (c = 'A'; c <= 'Z'; c++)		ctypes[c] |= C_ALPHA;	ctypes['_'] |= C_ALPHA;	setctypes("0123456789", C_DIGIT);	setctypes(" \t\n|&;<>()", C_LEX1); /* \0 added automatically */	setctypes("*@#!$-?", C_VAR1);	setctypes(" \t\n", C_IFSWS);	setctypes("=-+?", C_SUBOP1);	setctypes("#%", C_SUBOP2);	setctypes(" \n\t\"#$&'()*;<>?[\\`|", C_QUOTE);} /* convert unsigned long to base N string */ char *ulton(n, base)	register unsigned long n;	int base;{	register char *p;	static char buf [20]; 	p = &buf[sizeof(buf)];	*--p = '\0';	do {		*--p = "0123456789ABCDEF"[n%base];		n /= base;	} while (n != 0);	return p;} char *str_save(s, ap)	register const char *s;	Area *ap;{	return s ? strcpy((char*) alloc((size_t)strlen(s)+1, ap), s) : NULL;} /* Allocate a string of size n+1 and copy upto n characters from the possibly * null terminated string s into it.  Always returns a null terminated string * (unless n < 0). */char *str_nsave(s, n, ap)	register const char *s;	int n;	Area *ap;{	char *ns; 	if (n < 0)		return 0;	ns = alloc(n + 1, ap);	ns[0] = '\0';	return strncat(ns, s, n);} /* called from expand.h:XcheckN() to grow buffer */char *Xcheck_grow_(xsp, xp, more)	XString *xsp;	char *xp;	int more;{	char *old_beg = xsp->beg; 	xsp->len += more > xsp->len ? more : xsp->len;	xsp->beg = aresize(xsp->beg, xsp->len + 8, xsp->areap);	xsp->end = xsp->beg + xsp->len;	return xsp->beg + (xp - old_beg);} const struct option options[] = {	/* Special cases (see parse_args()): -A, -o, -s.	 * Options are sorted by their longnames - the order of these	 * entries MUST match the order of sh_flag F* enumerations in sh.h.	 */	{ "allexport",	'a',		OF_ANY },#ifdef BRACE_EXPAND	{ "braceexpand",  0,		OF_ANY }, /* non-standard */#endif	{ "bgnice",	  0,		OF_ANY },	{ (char *) 0, 	'c',	    OF_CMDLINE },	{ "errexit",	'e',		OF_ANY },	{ "ignoreeof",	  0,		OF_ANY },	{ "interactive",'i',	    OF_CMDLINE },	{ "keyword",	'k',		OF_ANY },	{ "login",	'l',	    OF_CMDLINE },	{ "markdirs",	'X',		OF_ANY },#ifdef JOBS	{ "monitor",	'm',		OF_ANY },#else /* JOBS */	{ (char *) 0,	'm',		     0 }, /* so FMONITOR not ifdef'd */#endif /* JOBS */	{ "noclobber",	'C',		OF_ANY },	{ "noexec",	'n',		OF_ANY },	{ "noglob",	'f',		OF_ANY },	{ "nohup",	  0,		OF_ANY },	{ "nointeractive", 'I',		OF_CMDLINE },	{ "nolog",	  0,		OF_ANY }, /* no effect */#ifdef	JOBS	{ "notify",	'b',		OF_ANY },#endif	/* JOBS */	{ "nounset",	'u',		OF_ANY },	{ "physical",	  0,		OF_ANY }, /* non-standard */	{ "posix",	  0,		OF_ANY }, /* non-standard */	{ "privileged",	'p',		OF_ANY },	{ "restricted",	'r',	    OF_CMDLINE },	{ "stdin",	's',	    OF_CMDLINE }, /* pseudo non-standard */	{ "trackall",	'h',		OF_ANY },	{ "verbose",	'v',		OF_ANY },	{ "xtrace",	'x',		OF_ANY },	/* Anonymous flags: used internally by shell only	 * (not visable to user)	 */	{ (char *) 0,	0,		OF_INTERNAL }, /* FTALKING_I */}; /* * translate -o option into F* constant (also used for test -o option) */intoption(n)	const char *n;{	int i; 	for (i = 0; i < NELEM(options); i++)		if (options[i].name && strcmp(options[i].name, n) == 0)			return i; 	return -1;} struct options_info {	int opt_width;	struct {		const char *name;		int	flag;	} opts[NELEM(options)];}; static char *options_fmt_entry ARGS((void *arg, int i, char *buf, int buflen));static void printoptions ARGS((int verbose)); /* format a single select menu item */static char *options_fmt_entry(arg, i, buf, buflen)	void *arg;	int i;	char *buf;	int buflen;{	struct options_info *oi = (struct options_info *) arg; 	shf_snprintf(buf, buflen, "%-*s %s",		oi->opt_width, oi->opts[i].name,		Flag(oi->opts[i].flag) ? "on" : "off");	return buf;} static voidprintoptions(verbose)	int verbose;{	int i; 	if (verbose) {		struct options_info oi;		int n, len; 		/* verbose version */		shprintf("Current option settings\n"); 		for (i = n = oi.opt_width = 0; i < NELEM(options); i++)			if (options[i].name) {				len = strlen(options[i].name);				oi.opts[n].name = options[i].name;				oi.opts[n++].flag = i;				if (len > oi.opt_width)					oi.opt_width = len;			}		print_columns(shl_stdout, n, options_fmt_entry, &oi,			      oi.opt_width + 5);	} else {		/* short version ala ksh93 */		shprintf("set");		for (i = 0; i < NELEM(options); i++)			if (Flag(i) && options[i].name)				shprintf(" -o %s", options[i].name);		shprintf(newline);	}} char *getoptions(){	int i;	char m[(int) FNFLAGS + 1];	register char *cp = m; 	for (i = 0; i < NELEM(options); i++)		if (options[i].c && Flag(i))			*cp++ = options[i].c;	*cp = 0;	return str_save(m, ATEMP);} /* change a Flag(*) value; takes care of special actions */voidchange_flag(f, what, newval)	enum sh_flag f;	/* flag to change */	int what;	/* what is changing the flag (command line vs set) */	int newval;{	int oldval; 	oldval = Flag(f);	Flag(f) = newval;	/* Turning off -p? */	if (f == FPRIVILEGED && oldval && !newval) {		setuid(ksheuid = getuid());		setgid(getgid());	} else if (f == FPOSIX && newval) {#ifdef BRACE_EXPAND		Flag(FBRACEEXPAND) = 0#endif /* BRACE_EXPAND */		;	}	/* Changing interactive flag? */	if (f == FTALKING) {		if ((what == OF_CMDLINE || what == OF_SET) && procpid == kshpid)			Flag(FTALKING_I) = newval;	} else if(f == FNOTTALKING) {		if ((what == OF_CMDLINE || what == OF_SET) && procpid == kshpid)			Flag(FTALKING_I) = !newval;	}} /* parse command line & set command arguments.  returns the index of * non-option arguments, -1 if there is an error. */intparse_args(argv, what, setargsp)	char **argv;	int	what;		/* OF_CMDLINE or OF_SET */	int	*setargsp;{	static char cmd_opts[NELEM(options) + 3]; /* o:\0 */	static char set_opts[NELEM(options) + 5]; /* Ao;s\0 */	char *opts;	char *array = (char *) 0;	Getopt go;	int i, optc, set, sortargs = 0, arrayset = 0; 	/* First call?  Build option strings... */	if (cmd_opts[0] == '\0') {		char *p, *q; 		strcpy(cmd_opts, "o:"); /* see cmd_opts[] declaration */		p = cmd_opts + strlen(cmd_opts);		strcpy(set_opts, "A:o;s"); /* see set_opts[] declaration */		q = set_opts + strlen(set_opts);		for (i = 0; i < NELEM(options); i++) {			if (options[i].c) {				if (options[i].flags & OF_CMDLINE)					*p++ = options[i].c;				if (options[i].flags & OF_SET)					*q++ = options[i].c;			}		}		*p = '\0';		*q = '\0';	} 	if (what == OF_CMDLINE) {		char *p;		/* Set FLOGIN before parsing options so user can clear		 * flag using +l.		 */		Flag(FLOGIN) = (argv[0][0] == '-'				|| ((p = ksh_strrchr_dirsep(argv[0]))				     && *++p == '-'));		opts = cmd_opts;	} else		opts = set_opts;	ksh_getopt_reset(&go, GF_ERROR|GF_PLUSOPT);	while ((optc = ksh_getopt(argv, &go, opts)) != EOF) {		set = (go.info & GI_PLUS) ? 0 : 1;		switch (optc) {		  case 'A':			arrayset = set ? 1 : -1;			array = go.optarg;			break; 		  case 'o':			if (go.optarg == (char *) 0) {				/* lone -o: print options				 *				 * Note that on the command line, -o requires				 * an option (ie, can't get here if what is				 * OF_CMDLINE).				 */				printoptions(set);				break;			}			i = option(go.optarg);			if (i >= 0 && set == Flag(i))				/* Don't check the context if the flag				 * isn't changing - makes "set -o interactive"				 * work if you're already interactive.  Needed				 * if the output of "set +o" is to be used.				 */				;			else if (i >= 0 && (options[i].flags & what))				change_flag((enum sh_flag) i, what, set);			else {				bi_errorf("%s: bad option", go.optarg);				return -1;			}			break; 		  case '?':			return -1; 		  default:			/* -s: sort positional params (at&t ksh stupidity) */			if (what == OF_SET && optc == 's') {				sortargs = 1;				break;			}			for (i = 0; i < NELEM(options); i++)				if (optc == options[i].c				    && (what & options[i].flags))				{					change_flag((enum sh_flag) i, what,						    set);					break;				}			if (i == NELEM(options)) {				internal_errorf(1, "parse_args: `%c'", optc);				return -1; /* not reached */			}		}	}	if (!(go.info & GI_MINUSMINUS) && argv[go.optind]	    && (argv[go.optind][0] == '-' || argv[go.optind][0] == '+')	    && argv[go.optind][1] == '\0')	{		/* lone - clears -v and -x flags */		if (argv[go.optind][0] == '-' && !Flag(FPOSIX))			Flag(FVERBOSE) = Flag(FXTRACE) = 0;		/* set skips lone - or + option */		go.optind++;	}	if (setargsp)		/* -- means set $#/$* even if there are no arguments */		*setargsp = !arrayset && ((go.info & GI_MINUSMINUS)					  || argv[go.optind]); 	if (arrayset && (!*array || *skip_varname(array, FALSE))) {		bi_errorf("%s: is not an identifier", array);		return -1;	}	if (sortargs) {		for (i = go.optind; argv[i]; i++)			;		qsortp((void **) &argv[go.optind], (size_t) (i - go.optind),			xstrcmp);	}	if (arrayset) {		set_array(array, arrayset, argv + go.optind);		for (; argv[go.optind]; go.optind++)			;	} 	return go.optind;} /* parse a decimal number: returns 0 if string isn't a number, 1 otherwise */intgetn(as, ai)	const char *as;	int *ai;{	const char *s;	register int n;	int sawdigit = 0; 	s = as;	if (*s == '-' || *s == '+')		s++;	for (n = 0; digit(*s); s++, sawdigit = 1)		n = n * 10 + (*s - '0');	*ai = (*as == '-') ? -n : n;	if (*s || !sawdigit)		return 0;	return 1;} /* getn() that prints error */intbi_getn(as, ai)	const char *as;	int *ai;{	int rv = getn(as, ai); 	if (!rv)		bi_errorf("%s: bad number", as);	return rv;} /* -------- gmatch.c -------- */ /* * int gmatch(string, pattern) * char *string, *pattern; * * Match a pattern as in sh(1). * pattern character are prefixed with MAGIC by expand. */ intgmatch(s, p, isfile)	const char *s, *p;	int isfile;{	const char *se, *pe; 	if (s == NULL || p == NULL)		return 0;	se = s + strlen(s);	pe = p + strlen(p);	/* isfile is false iff no syntax check has been done on	 * the pattern.  If check fails, just to a strcmp().	 */	if (!isfile && !has_globbing(p, pe)) {		int len = pe - p + 1;		char tbuf[64];		char *t = len <= sizeof(tbuf) ? tbuf				: (char *) alloc(len, ATEMP);		debunk(t, p);		return !strcmp(t, s);	}	return do_gmatch((const unsigned char *) s, (const unsigned char *) se,			 (const unsigned char *) p, (const unsigned char *) pe,			 isfile);} /* Returns if p is a syntacticly correct globbing pattern, false * if it contains no pattern characters or if there is a syntax error.

Lines 1–500 of 1305.