hand-sorted ctypes/chtypes upgrade; use table-driven where they make

sense and preprocessored otherwise; unify the logic
saves 144t 1i and lots of cpp(1) time, as well as improves readability
This commit is contained in:
tg 2006-11-10 07:52:04 +00:00
parent feb7dddd44
commit 56ffbf7e70
11 changed files with 72 additions and 76 deletions

24
edit.c
View File

@ -5,7 +5,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/edit.c,v 1.71 2006/11/10 07:18:56 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/edit.c,v 1.72 2006/11/10 07:52:01 tg Exp $");
/* tty driver characters we are interested in */
typedef struct {
@ -1050,8 +1050,7 @@ struct x_defbindings {
/* Separator for completion */
#define is_cfs(c) ((c) == ' ' || (c) == '\t' || (c) == '"' || (c) == '\'')
/* Separator for motion */
#define is_mfs(c) (!(ksh_isdigit(c) || ksh_islower(c) || \
ksh_isupper(c) || (c) == '_' || (c) == '$'))
#define is_mfs(c) (!(ksh_isalnux(c) || (c) == '$'))
#define CHARMASK 0xFF /* 8-bit character mask */
#define X_NTABS 3 /* normal, meta1, meta2 */
@ -3343,7 +3342,6 @@ x_mode(bool onoff)
/* +++ vi editing mode +++ */
#define Ctrl(c) (c&0x1f)
#define is_wordch(c) (letnum(c))
struct edstate {
int winleft;
@ -4841,12 +4839,12 @@ forwword(int argcnt)
ncursor = es->cursor;
while (ncursor < es->linelen && argcnt--) {
if (is_wordch(es->cbuf[ncursor]))
while (is_wordch(es->cbuf[ncursor]) &&
if (ksh_isalnux(es->cbuf[ncursor]))
while (ksh_isalnux(es->cbuf[ncursor]) &&
ncursor < es->linelen)
ncursor++;
else if (!ksh_isspace(es->cbuf[ncursor]))
while (!is_wordch(es->cbuf[ncursor]) &&
while (!ksh_isalnux(es->cbuf[ncursor]) &&
!ksh_isspace(es->cbuf[ncursor]) &&
ncursor < es->linelen)
ncursor++;
@ -4867,13 +4865,13 @@ backword(int argcnt)
while (--ncursor > 0 && ksh_isspace(es->cbuf[ncursor]))
;
if (ncursor > 0) {
if (is_wordch(es->cbuf[ncursor]))
if (ksh_isalnux(es->cbuf[ncursor]))
while (--ncursor >= 0 &&
is_wordch(es->cbuf[ncursor]))
ksh_isalnux(es->cbuf[ncursor]))
;
else
while (--ncursor >= 0 &&
!is_wordch(es->cbuf[ncursor]) &&
!ksh_isalnux(es->cbuf[ncursor]) &&
!ksh_isspace(es->cbuf[ncursor]))
;
ncursor++;
@ -4893,13 +4891,13 @@ endword(int argcnt)
ksh_isspace(es->cbuf[ncursor]))
;
if (ncursor < es->linelen - 1) {
if (is_wordch(es->cbuf[ncursor]))
if (ksh_isalnux(es->cbuf[ncursor]))
while (++ncursor < es->linelen &&
is_wordch(es->cbuf[ncursor]))
ksh_isalnux(es->cbuf[ncursor]))
;
else
while (++ncursor < es->linelen &&
!is_wordch(es->cbuf[ncursor]) &&
!ksh_isalnux(es->cbuf[ncursor]) &&
!ksh_isspace(es->cbuf[ncursor]))
;
ncursor--;

4
eval.c
View File

@ -2,7 +2,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/eval.c,v 1.17 2006/11/10 06:40:05 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/eval.c,v 1.18 2006/11/10 07:52:02 tg Exp $");
/*
* string expansion
@ -797,7 +797,7 @@ varsub(Expand *xp, char *sp, char *word,
} else {
/* Can't assign things like $! or $1 */
if ((stype & 0x7f) == '=' &&
(ctype(*sp, C_VAR1) || digit(*sp)))
ctype(*sp, C_VAR1 | C_DIGIT))
return -1;
xp->var = global(sp);
xp->str = str_val(xp->var);

10
expr.c
View File

@ -2,7 +2,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/expr.c,v 1.5 2006/11/10 07:18:57 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/expr.c,v 1.6 2006/11/10 07:52:02 tg Exp $");
/* The order of these enums is constrained by the order of opinfo[] */
enum token {
@ -454,8 +454,8 @@ token(Expr_state *es)
if (c == '\0')
es->tok = END;
else if (letter(c)) {
for (; letnum(c); c = *cp)
else if (ksh_isalphx(c)) {
for (; ksh_isalnux(c); c = *cp)
cp++;
if (c == '[') {
int len;
@ -480,8 +480,8 @@ token(Expr_state *es)
afree(tvar, ATEMP);
}
es->tok = VAR;
} else if (digit(c)) {
for (; c != '_' && (letnum(c) || c == '#'); c = *cp++)
} else if (ksh_isdigit(c)) {
for (; c != '_' && (ksh_isalnux(c) || c == '#'); c = *cp++)
;
tvar = str_nsave(es->tokp, --cp - es->tokp, ATEMP);
es->val = tempvar();

10
funcs.c
View File

@ -5,7 +5,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.35 2006/11/10 06:53:26 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.36 2006/11/10 07:52:02 tg Exp $");
int
c_cd(char **wp)
@ -1103,8 +1103,8 @@ c_kill(char **wp)
int i, n, rv, sig;
/* assume old style options if -digits or -UPPERCASE */
if ((p = wp[1]) && *p == '-' && (digit(p[1]) ||
ksh_isupper((unsigned char)p[1]))) {
if ((p = wp[1]) && *p == '-' && (ksh_isdigit(p[1]) ||
ksh_isupper(p[1]))) {
if (!(t = gettrap(p + 1, true))) {
bi_errorf("bad signal '%s'", p + 1);
return 1;
@ -1450,7 +1450,7 @@ c_umask(char **wp)
} else {
mode_t new_umask;
if (digit(*cp)) {
if (ksh_isdigit(*cp)) {
for (new_umask = 0; *cp >= '0' && *cp <= '7'; cp++)
new_umask = new_umask * 8 + (*cp - '0');
if (*cp) {
@ -2962,7 +2962,7 @@ c_ulimit(char **wp)
* add parameter to evaluate() to control
* if unset params are 0 or an error.
*/
if (!rval && !digit(wp[0][0])) {
if (!rval && !ksh_isdigit(wp[0][0])) {
bi_errorf("invalid limit: %s", wp[0]);
return 1;
}

View File

@ -3,7 +3,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/histrap.c,v 1.32 2006/11/10 06:40:05 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/histrap.c,v 1.33 2006/11/10 07:52:02 tg Exp $");
#ifndef mksh_siglist
#if defined(BSD) || defined(__APPLE__)
@ -1057,7 +1057,7 @@ gettrap(const char *name, int igncase)
int i;
Trap *p;
if (digit(*name)) {
if (ksh_isdigit(*name)) {
int n;
if (getn(name, &n) && 0 <= n && n < NSIG)

4
jobs.c
View File

@ -2,7 +2,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/jobs.c,v 1.13 2006/11/10 04:03:59 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/jobs.c,v 1.14 2006/11/10 07:52:03 tg Exp $");
/* Order important! */
#define PRUNNING 0
@ -1301,7 +1301,7 @@ j_lookup(const char *cp, int *ecodep)
Proc *p;
int len, job = 0;
if (digit(*cp)) {
if (ksh_isdigit(*cp)) {
getn(cp, &job);
/* Look for last_proc->pid (what $! returns) first... */
for (j = job_list; j != NULL; j = j->next)

18
lex.c
View File

@ -2,7 +2,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/lex.c,v 1.23 2006/11/10 07:18:57 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/lex.c,v 1.24 2006/11/10 07:52:03 tg Exp $");
/* Structure to keep track of the lexing state and the various pieces of info
* needed for each particular state. */
@ -273,19 +273,19 @@ yylex(int cf)
ungetsc(c);
PUSH_STATE(SBRACE);
}
} else if (ctype(c, C_ALPHA)) {
} else if (ksh_isalphx(c)) {
*wp++ = OSUBST;
*wp++ = 'X';
do {
Xcheck(ws, wp);
*wp++ = c;
c = getsc();
} while (ctype(c, C_ALPHA|C_DIGIT));
} while (ksh_isalnux(c));
*wp++ = '\0';
*wp++ = CSUBST;
*wp++ = 'X';
ungetsc(c);
} else if (ctype(c, C_DIGIT|C_VAR1)) {
} else if (ctype(c, C_VAR1 | C_DIGIT)) {
Xcheck(ws, wp);
*wp++ = OSUBST;
*wp++ = 'X';
@ -615,7 +615,7 @@ yylex(int cf)
dp = Xstring(ws, wp);
if ((c == '<' || c == '>') && state == SBASE &&
((c2 = Xlength(ws, wp)) == 0 ||
(c2 == 2 && dp[0] == CHAR && digit(dp[1])))) {
(c2 == 2 && dp[0] == CHAR && ksh_isdigit(dp[1])))) {
struct ioword *iop = (struct ioword *) alloc(sizeof(*iop), ATEMP);
if (c2 == 2)
@ -1174,9 +1174,9 @@ get_brace_var(XString *wsp, char *wp)
}
/* FALLTHRU */
case PS_SAW_HASH:
if (letter(c))
if (ksh_isalphx(c))
state = PS_IDENT;
else if (digit(c))
else if (ksh_isdigit(c))
state = PS_NUMBER;
else if (ctype(c, C_VAR1))
state = PS_VAR1;
@ -1184,7 +1184,7 @@ get_brace_var(XString *wsp, char *wp)
state = PS_END;
break;
case PS_IDENT:
if (!letnum(c)) {
if (!ksh_isalnux(c)) {
state = PS_END;
if (c == '[') {
char *tmp, *p;
@ -1202,7 +1202,7 @@ get_brace_var(XString *wsp, char *wp)
}
break;
case PS_NUMBER:
if (!digit(c))
if (!ksh_isdigit(c))
state = PS_END;
break;
case PS_VAR1:

6
misc.c
View File

@ -3,7 +3,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.37 2006/11/10 07:18:57 tg Exp $\t"
__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.38 2006/11/10 07:52:03 tg Exp $\t"
MKSH_SH_H_ID);
#undef USE_CHVT
@ -873,13 +873,13 @@ ksh_getopt(char **argv, Getopt *go, const char *optionsp)
* argument is missing.
*/
if (argv[go->optind - 1][go->p]) {
if (digit(argv[go->optind - 1][go->p])) {
if (ksh_isdigit(argv[go->optind - 1][go->p])) {
go->optarg = argv[go->optind - 1] + go->p;
go->p = 0;
} else
go->optarg = NULL;
} else {
if (argv[go->optind] && digit(argv[go->optind][0])) {
if (argv[go->optind] && ksh_isdigit(argv[go->optind][0])) {
go->optarg = argv[go->optind++];
go->p = 0;
} else

17
sh.h
View File

@ -8,7 +8,7 @@
/* $OpenBSD: c_test.h,v 1.4 2004/12/20 11:34:26 otto Exp $ */
/* $OpenBSD: tty.h,v 1.5 2004/12/20 11:34:26 otto Exp $ */
#define MKSH_SH_H_ID "$MirOS: src/bin/mksh/sh.h,v 1.79 2006/11/10 07:18:57 tg Exp $"
#define MKSH_SH_H_ID "$MirOS: src/bin/mksh/sh.h,v 1.80 2006/11/10 07:52:03 tg Exp $"
#define MKSH_VERSION "R29 2006/11/10"
#if HAVE_SYS_PARAM_H
@ -401,18 +401,17 @@ EXTERN int really_exit;
#define C_VAR1 BIT(3) /* *@#!$-? */
#define C_IFSWS BIT(4) /* \t \n (IFS white space) */
#define C_SUBOP1 BIT(5) /* "=-+?" */
#define C_SUBOP2 BIT(8) /* "#%" (not realised via chtypes array) */
#define C_IFS BIT(7) /* $IFS */
#define C_QUOTE BIT(6) /* \n\t"#$&'()*;<>?[]\`| (needing quoting) */
#define C_IFS BIT(7) /* $IFS */
#define C_SUBOP2 BIT(8) /* "#%" (magic, see below) */
extern unsigned char chtypes[];
#define ctype(c, t) !!(((t) == C_SUBOP2) ? \
(((c) == '#' || (c) == '%') ? 1 : 0) : \
(chtypes[(unsigned char)(c)]&(t)))
#define letter(c) ctype(c, C_ALPHA)
#define digit(c) ctype(c, C_DIGIT)
#define letnum(c) ctype(c, C_ALPHA|C_DIGIT)
#define ctype(c, t) !!( ((t) == C_SUBOP2) ? \
(((c) == '#' || (c) == '%') ? 1 : 0) : \
(chtypes[(unsigned char)(c)]&(t)) )
#define ksh_isalphx(c) ctype((c), C_ALPHA)
#define ksh_isalnux(c) ctype((c), C_ALPHA | C_DIGIT)
EXTERN int ifs0 I__(' '); /* for "$*" */

6
shf.c
View File

@ -2,7 +2,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/shf.c,v 1.7 2006/11/09 21:00:13 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/shf.c,v 1.8 2006/11/10 07:52:04 tg Exp $");
/* flags to shf_emptybuf() */
#define EB_READSW 0x01 /* about to switch to reading */
@ -819,9 +819,9 @@ shf_vfprintf(struct shf *shf, const char *fmt, va_list args)
flags |= FL_SHORT;
continue;
}
if (digit(c)) {
if (ksh_isdigit(c)) {
tmp = c - '0';
while (c = *fmt++, digit(c))
while (c = *fmt++, ksh_isdigit(c))
tmp = tmp * 10 + c - '0';
--fmt;
if (tmp < 0) /* overflow? */

45
var.c
View File

@ -2,7 +2,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/var.c,v 1.30 2006/11/10 07:18:58 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/var.c,v 1.31 2006/11/10 07:52:04 tg Exp $");
/*
* Variables
@ -157,7 +157,7 @@ global(const char *n)
n = array_index_calc(n, &array, &val);
h = hash(n);
c = n[0];
if (!letter(c)) {
if (!ksh_isalphx(c)) {
if (array)
errorf("bad substitution");
vp = &vtemp;
@ -165,8 +165,8 @@ global(const char *n)
vp->type = 0;
vp->areap = ATEMP;
*vp->name = c;
if (digit(c)) {
for (c = 0; digit(*n); n++)
if (ksh_isdigit(c)) {
for (c = 0; ksh_isdigit(*n); n++)
c = c*10 + *n-'0';
if (c <= l->argc)
/* setstr can't fail here */
@ -237,7 +237,7 @@ local(const char *n, bool copy)
/* Check to see if this is an array */
n = array_index_calc(n, &array, &val);
h = hash(n);
if (!letter(*n)) {
if (!ksh_isalphx(*n)) {
vp = &vtemp;
vp->flag = DEFINED|RDONLY;
vp->type = 0;
@ -432,26 +432,25 @@ getint(struct tbl *vp, long int *nump, bool arith)
for (c = *s++; c ; c = *s++) {
if (c == '-') {
neg++;
continue;
} else if (c == '#') {
base = (int) num;
if (have_base || base < 2 || base > 36)
return -1;
num = 0;
have_base = 1;
} else if (letnum(c)) {
if (ksh_isdigit(c))
c -= '0';
else if (ksh_islower(c))
c -= 'a' - 10;
else if (ksh_isupper(c))
c -= 'A' - 10;
else
c = -1; /* _: force error */
if (c < 0 || c >= base)
return -1;
num = num * base + c;
} else
continue;
} else if (ksh_isdigit(c))
c -= '0';
else if (ksh_islower(c))
c -= 'a' - 10;
else if (ksh_isupper(c))
c -= 'A' - 10;
else
return -1;
if (c < 0 || c >= base)
return -1;
num = num * base + c;
}
if (neg)
num = -num;
@ -596,7 +595,7 @@ typeset(const char *var, Tflag set, Tflag clr, int field, int base)
if (set & IMPORT) {
int i;
for (i = 1; i < len - 1; i++)
if (!digit(val[i]))
if (!ksh_isdigit(val[i]))
return NULL;
}
val += len;
@ -751,8 +750,8 @@ skip_varname(const char *s, int aok)
{
int alen;
if (s && letter(*s)) {
while (*++s && letnum(*s))
if (s && ksh_isalphx(*s)) {
while (*++s && ksh_isalnux(*s))
;
if (aok && *s == '[' && (alen = array_ref_len(s)))
s += alen;
@ -765,10 +764,10 @@ char *
skip_wdvarname(const char *s,
int aok) /* skip array de-reference? */
{
if (s[0] == CHAR && letter(s[1])) {
if (s[0] == CHAR && ksh_isalphx(s[1])) {
do {
s += 2;
} while (s[0] == CHAR && letnum(s[1]));
} while (s[0] == CHAR && ksh_isalnux(s[1]));
if (aok && s[0] == CHAR && s[1] == '[') {
/* skip possible array de-reference */
const char *p = s;