batch of optimisations

This commit is contained in:
tg 2017-04-27 20:22:28 +00:00
parent 1080008a8f
commit d54d4aab50
8 changed files with 50 additions and 67 deletions

28
edit.c
View File

@ -28,7 +28,7 @@
#ifndef MKSH_NO_CMDLINE_EDITING #ifndef MKSH_NO_CMDLINE_EDITING
__RCSID("$MirOS: src/bin/mksh/edit.c,v 1.326 2017/04/27 19:33:46 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/edit.c,v 1.327 2017/04/27 20:22:22 tg Exp $");
/* /*
* in later versions we might use libtermcap for this, but since external * in later versions we might use libtermcap for this, but since external
@ -553,7 +553,7 @@ x_command_glob(int flags, char *toglob, char ***wordsp)
return (nwords); return (nwords);
} }
#define IS_WORDC(c) (!ctype(c, C_LEX1) && (c) != '\'' && (c) != '"' && \ #define IS_WORDC(c) (!ctype(c, C_LEX1 | C_QC) && \
(c) != '`' && (c) != '=' && (c) != ':') (c) != '`' && (c) != '=' && (c) != ':')
static int static int
@ -859,8 +859,8 @@ x_escape(const char *s, size_t len, int (*putbuf_func)(const char *, size_t))
int rval = 0; int rval = 0;
while (wlen - add > 0) while (wlen - add > 0)
if (vstrchr("\"#$&'()*:;<=>?[\\`{|}", s[add]) || if (vstrchr("#*=?[\\`" ":{}", s[add]) || /*…1…*/
ctype(s[add], C_IFS)) { ctype(s[add], C_IFS | C_QC | C_DOLAR | CiQCL)) {
if (putbuf_func(s, add) != 0) { if (putbuf_func(s, add) != 0) {
rval = -1; rval = -1;
break; break;
@ -4470,14 +4470,13 @@ vi_cmd(int argcnt, const char *cmd)
if (histnum(-1) < 0) if (histnum(-1) < 0)
return (-1); return (-1);
p = *histpos(); p = *histpos();
#define issp(c) (ctype(c, C_SPACE) || (c) == '\n')
if (argcnt) { if (argcnt) {
while (*p && issp(*p)) while (ctype(*p, C_SPACE))
p++; p++;
while (*p && --argcnt) { while (*p && --argcnt) {
while (*p && !issp(*p)) while (*p && !ctype(*p, C_SPACE))
p++; p++;
while (*p && issp(*p)) while (ctype(*p, C_SPACE))
p++; p++;
} }
if (!*p) if (!*p)
@ -4487,7 +4486,7 @@ vi_cmd(int argcnt, const char *cmd)
sp = p; sp = p;
inspace = false; inspace = false;
while (*p) { while (*p) {
if (issp(*p)) if (ctype(*p, C_SPACE))
inspace = true; inspace = true;
else if (inspace) { else if (inspace) {
inspace = false; inspace = false;
@ -4501,7 +4500,7 @@ vi_cmd(int argcnt, const char *cmd)
hnum = hlast; hnum = hlast;
if (vs->cursor != vs->linelen) if (vs->cursor != vs->linelen)
vs->cursor++; vs->cursor++;
while (*p && !issp(*p)) { while (*p && !ctype(*p, C_SPACE)) {
argcnt++; argcnt++;
p++; p++;
} }
@ -4927,8 +4926,7 @@ forwword(int argcnt)
ncursor++; ncursor++;
else if (!ctype(vs->cbuf[ncursor], C_SPACE)) else if (!ctype(vs->cbuf[ncursor], C_SPACE))
while (ncursor < vs->linelen && while (ncursor < vs->linelen &&
!ctype(vs->cbuf[ncursor], C_ALNUX) && !ctype(vs->cbuf[ncursor], C_ALNUX | C_SPACE))
!ctype(vs->cbuf[ncursor], C_SPACE))
ncursor++; ncursor++;
while (ncursor < vs->linelen && while (ncursor < vs->linelen &&
ctype(vs->cbuf[ncursor], C_SPACE)) ctype(vs->cbuf[ncursor], C_SPACE))
@ -4953,8 +4951,7 @@ backword(int argcnt)
; ;
else else
while (--ncursor >= 0 && while (--ncursor >= 0 &&
!ctype(vs->cbuf[ncursor], C_ALNUX) && !ctype(vs->cbuf[ncursor], C_ALNUX | C_SPACE))
!ctype(vs->cbuf[ncursor], C_SPACE))
; ;
ncursor++; ncursor++;
} }
@ -4979,8 +4976,7 @@ endword(int argcnt)
; ;
else else
while (++ncursor < vs->linelen && while (++ncursor < vs->linelen &&
!ctype(vs->cbuf[ncursor], C_ALNUX) && !ctype(vs->cbuf[ncursor], C_ALNUX | C_SPACE))
!ctype(vs->cbuf[ncursor], C_SPACE))
; ;
ncursor--; ncursor--;
} }

4
expr.c
View File

@ -23,7 +23,7 @@
#include "sh.h" #include "sh.h"
__RCSID("$MirOS: src/bin/mksh/expr.c,v 1.94 2017/04/27 19:33:48 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/expr.c,v 1.95 2017/04/27 20:22:24 tg Exp $");
#define EXPRTOK_DEFNS #define EXPRTOK_DEFNS
#include "exprtok.h" #include "exprtok.h"
@ -618,7 +618,7 @@ exprtoken(Expr_state *es)
goto process_tvar; goto process_tvar;
#endif #endif
} else if (ctype(c, C_DIGIT)) { } else if (ctype(c, C_DIGIT)) {
while (c != '_' && (ctype(c, C_ALNUX) || c == '#')) while (ctype(c, C_ALNUM) || c == '#')
c = *cp++; c = *cp++;
strndupx(tvar, es->tokp, --cp - es->tokp, ATEMP); strndupx(tvar, es->tokp, --cp - es->tokp, ATEMP);
process_tvar: process_tvar:

13
funcs.c
View File

@ -38,7 +38,7 @@
#endif #endif
#endif #endif
__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.343 2017/04/27 19:33:48 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.344 2017/04/27 20:22:24 tg Exp $");
#if HAVE_KILLPG #if HAVE_KILLPG
/* /*
@ -746,10 +746,10 @@ bool
valid_alias_name(const char *cp) valid_alias_name(const char *cp)
{ {
while (*cp) while (*cp)
if (!ctype(*cp, C_ALIAS)) if (ctype(*cp, C_ALIAS))
return (false);
else
++cp; ++cp;
else
return (false);
return (true); return (true);
} }
@ -1067,8 +1067,7 @@ c_kill(const char **wp)
int i, n, rv, sig; int i, n, rv, sig;
/* assume old style options if -digits or -UPPERCASE */ /* assume old style options if -digits or -UPPERCASE */
if ((p = wp[1]) && *p == '-' && (ctype(p[1], C_DIGIT) || if ((p = wp[1]) && *p == '-' && ctype(p[1], C_DIGIT | C_UPPER)) {
ctype(p[1], C_UPPER))) {
if (!(t = gettrap(p + 1, false, false))) { if (!(t = gettrap(p + 1, false, false))) {
bi_errorf(Tbad_sig_s, p + 1); bi_errorf(Tbad_sig_s, p + 1);
return (1); return (1);
@ -1419,7 +1418,7 @@ c_umask(const char **wp)
if (ctype(*cp, C_DIGIT)) { if (ctype(*cp, C_DIGIT)) {
new_umask = 0; new_umask = 0;
while (asc(*cp) >= asc('0') && asc(*cp) <= asc('7')) { while (ctype(*cp, C_OCTAL)) {
new_umask = new_umask * 8 + ksh_numdig(*cp); new_umask = new_umask * 8 + ksh_numdig(*cp);
++cp; ++cp;
} }

11
lex.c
View File

@ -23,7 +23,7 @@
#include "sh.h" #include "sh.h"
__RCSID("$MirOS: src/bin/mksh/lex.c,v 1.235 2017/04/27 19:33:51 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/lex.c,v 1.236 2017/04/27 20:22:25 tg Exp $");
/* /*
* states while lexing word * states while lexing word
@ -444,10 +444,9 @@ yylex(int cf)
statep->ls_adelim.num = 1; statep->ls_adelim.num = 1;
statep->nparen = 0; statep->nparen = 0;
break; break;
} else if (ctype(c, C_DIGIT) || } else if (ctype(c, C_DIGIT | C_DOLAR | C_SPC) ||
c == '('/*)*/ || c == ' ' ||
/*XXX what else? */ /*XXX what else? */
c == '$') { c == '('/*)*/) {
/* substring subst. */ /* substring subst. */
if (c != ' ') { if (c != ' ') {
*wp++ = CHAR; *wp++ = CHAR;
@ -1275,7 +1274,7 @@ getsc_uu(void)
source->flags |= s->flags & SF_ALIAS; source->flags |= s->flags & SF_ALIAS;
s = source; s = source;
} else if (*s->u.tblp->val.s && } else if (*s->u.tblp->val.s &&
(c = strnul(s->u.tblp->val.s)[-1], ctype(c, C_SPACE))) { ctype((c = strnul(s->u.tblp->val.s)[-1]), C_SPACE)) {
/* pop source stack */ /* pop source stack */
source = s = s->next; source = s = s->next;
/* /*
@ -1435,7 +1434,7 @@ getsc_line(Source *s)
} else if (interactive && cur_prompt == PS1) { } else if (interactive && cur_prompt == PS1) {
check_for_sole_return: check_for_sole_return:
cp = Xstring(s->xs, xp); cp = Xstring(s->xs, xp);
while (*cp && ctype(*cp, C_IFSWS)) while (ctype(*cp, C_IFSWS))
++cp; ++cp;
if (!*cp) { if (!*cp) {
histsave(&s->line, NULL, HIST_FLUSH, true); histsave(&s->line, NULL, HIST_FLUSH, true);

4
main.c
View File

@ -34,7 +34,7 @@
#include <locale.h> #include <locale.h>
#endif #endif
__RCSID("$MirOS: src/bin/mksh/main.c,v 1.337 2017/04/27 19:33:51 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/main.c,v 1.338 2017/04/27 20:22:25 tg Exp $");
extern char **environ; extern char **environ;
@ -491,7 +491,7 @@ main_init(int argc, const char *argv[], Source **sp, struct block **lp)
if (!(s->start = s->str = argv[argi++])) if (!(s->start = s->str = argv[argi++]))
errorf(Tf_optfoo, "", "", 'c', Treq_arg); errorf(Tf_optfoo, "", "", 'c', Treq_arg);
while (*s->str) { while (*s->str) {
if (*s->str != ' ' && ctype(*s->str, C_QUOTE)) if (ctype(*s->str, C_QUOTE))
break; break;
s->str++; s->str++;
} }

23
misc.c
View File

@ -30,7 +30,7 @@
#include <grp.h> #include <grp.h>
#endif #endif
__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.260 2017/04/27 19:33:52 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/misc.c,v 1.261 2017/04/27 20:22:26 tg Exp $");
#define KSH_CHVT_FLAG #define KSH_CHVT_FLAG
#ifdef MKSH_SMALL #ifdef MKSH_SMALL
@ -1072,7 +1072,8 @@ ksh_getopt(const char **argv, Getopt *go, const char *optionsp)
} else } else
go->optarg = NULL; go->optarg = NULL;
} else { } else {
if (argv[go->optind] && ctype(argv[go->optind][0], C_DIGIT)) { if (argv[go->optind] &&
ctype(argv[go->optind][0], C_DIGIT)) {
go->optarg = argv[go->optind++]; go->optarg = argv[go->optind++];
go->p = 0; go->p = 0;
} else } else
@ -1096,7 +1097,7 @@ print_value_quoted(struct shf *shf, const char *s)
/* first, check whether any quotes are needed */ /* first, check whether any quotes are needed */
while ((c = *p++) >= 32) while ((c = *p++) >= 32)
if (ctype(c, C_QUOTE)) if (ctype(c, C_QUOTE | C_SPC))
inquote = false; inquote = false;
p = (const unsigned char *)s; p = (const unsigned char *)s;
@ -2192,7 +2193,7 @@ unbksl(bool cstyle, int (*fg)(void), void (*fp)(int))
wc = 0; wc = 0;
i = 3; i = 3;
while (i--) while (i--)
if (ctype((c = (*fg)()), C_DIGIT) && asc(c) <= asc('7')) if (ctype((c = (*fg)()), C_OCTAL))
wc = (wc << 3) + ksh_numdig(c); wc = (wc << 3) + ksh_numdig(c);
else { else {
(*fp)(c); (*fp)(c);
@ -2220,17 +2221,17 @@ unbksl(bool cstyle, int (*fg)(void), void (*fp)(int))
n = 0; n = 0;
while (n < i || i == -1) { while (n < i || i == -1) {
wc <<= 4; wc <<= 4;
if (ctype((c = (*fg)()), C_DIGIT)) if (!ctype((c = (*fg)()), C_SEDEC)) {
wc += ksh_numdig(c);
else if (asc(c) >= asc('A') && asc(c) <= asc('F'))
wc += ksh_numuc(c) + 10;
else if (asc(c) >= asc('a') && asc(c) <= asc('f'))
wc += ksh_numlc(c) + 10;
else {
wc >>= 4; wc >>= 4;
(*fp)(c); (*fp)(c);
break; break;
} }
if (ctype(c, C_DIGIT))
wc += ksh_numdig(c);
else if (ctype(c, C_UPPER))
wc += ksh_numuc(c) + 10;
else
wc += ksh_numlc(c) + 10;
++n; ++n;
} }
if (!n) if (!n)

28
sh.h
View File

@ -175,9 +175,9 @@
#endif #endif
#ifdef EXTERN #ifdef EXTERN
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.816 2017/04/27 19:19:05 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/sh.h,v 1.817 2017/04/27 20:22:27 tg Exp $");
#endif #endif
#define MKSH_VERSION "R55 2017/04/20" #define MKSH_VERSION "R55 2017/04/27"
/* arithmetic types: C implementation */ /* arithmetic types: C implementation */
#if !HAVE_CAN_INTTYPES #if !HAVE_CAN_INTTYPES
@ -1360,8 +1360,8 @@ EXTERN char ifs0;
#define C_PRINT (C_GRAPH | CiSP) #define C_PRINT (C_GRAPH | CiSP)
/* !"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~ POSIX punctuation */ /* !"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~ POSIX punctuation */
#define C_PUNCT (CiUNDER | CiALIAS | CiSS | CiQC | CiQCL | CiQCX) #define C_PUNCT (CiUNDER | CiALIAS | CiSS | CiQC | CiQCL | CiQCX)
/* \x09\x0A\x20"#$&'()*;<=>?[\\]`| characters requiring quoting */ /* \x09\x0A"#$&'()*;<=>?[\\]`| characters requiring quoting, minus space */
#define C_QUOTE (CiTAB | CiSP | CiNL | CiSS | CiQC | CiQCL | CiQCX) #define C_QUOTE (CiTAB | CiNL | CiSS | CiQC | CiQCL | CiQCX)
/* 0‥9A‥Fa‥f hexadecimal digit */ /* 0‥9A‥Fa‥f hexadecimal digit */
#define C_SEDEC (CiDIGIT | CiOCTAL | CiHEXLT) #define C_SEDEC (CiDIGIT | CiOCTAL | CiHEXLT)
/* \x09‥\x0D\x20 POSIX space class */ /* \x09‥\x0D\x20 POSIX space class */
@ -1400,24 +1400,12 @@ EXTERN char ifs0;
/* helper functions */ /* helper functions */
#define ksh_isdash(s) tobool(ord((s)[0]) == '-' && ord((s)[1]) == '\0') #define ksh_isdash(s) tobool(ord((s)[0]) == '-' && ord((s)[1]) == '\0')
/* invariant distance even in EBCDIC */ /* invariant distance even in EBCDIC */
#define ksh_tolower(c) (ksh_isupper(c) ? (c) - 'A' + 'a' : (c)) #define ksh_tolower(c) (ctype(c, C_UPPER) ? (c) - 'A' + 'a' : (c))
#define ksh_toupper(c) (ksh_islower(c) ? (c) - 'a' + 'A' : (c)) #define ksh_toupper(c) (ctype(c, C_LOWER) ? (c) - 'a' + 'A' : (c))
/* strictly speaking asc() here, but this works even in EBCDIC */ /* strictly speaking asc() here, but this works even in EBCDIC */
#define ksh_numdig(c) (ord(c) - ord('0')) #define ksh_numdig(c) (ord(c) - ord('0'))
#define ksh_numuc(c) (asc(c) - asc('A')) #define ksh_numuc(c) (asc(c) - asc('A'))
#define ksh_numlc(c) (asc(c) - asc('a')) #define ksh_numlc(c) (asc(c) - asc('a'))
/* legacy functions */
#define ksh_issubop2(c) ctype((c), C_SUB2)
#define ksh_isalias(c) ctype((c), C_ALIAS)
#define ksh_isalpha(c) ctype((c), C_ALPHA)
#define ksh_isalphx(c) ctype((c), C_ALPHX)
#define ksh_isalnux(c) ctype((c), C_ALNUX)
#define ksh_isdigit(c) ctype((c), C_DIGIT)
#define ksh_islower(c) ctype((c), C_LOWER)
#define ksh_isupper(c) ctype((c), C_UPPER)
#define ksh_isspace(c) ctype((c), C_SPACE)
#define is_cfs(c) ctype((c), C_CFS)
#define is_mfs(c) ctype((c), C_MFS)
/* Argument parsing for built-in commands and getopts command */ /* Argument parsing for built-in commands and getopts command */
@ -2583,7 +2571,7 @@ extern int tty_init_fd(void); /* initialise tty_fd, tty_devtty */
#define mksh_abspath(s) __extension__({ \ #define mksh_abspath(s) __extension__({ \
const char *mksh_abspath_s = (s); \ const char *mksh_abspath_s = (s); \
(mksh_cdirsep(mksh_abspath_s[0]) || \ (mksh_cdirsep(mksh_abspath_s[0]) || \
(ksh_isalpha(mksh_abspath_s[0]) && \ (ctype(mksh_abspath_s[0], C_ALPHA) && \
mksh_abspath_s[1] == ':')); \ mksh_abspath_s[1] == ':')); \
}) })
#define mksh_cdirsep(c) __extension__({ \ #define mksh_cdirsep(c) __extension__({ \
@ -2592,7 +2580,7 @@ extern int tty_init_fd(void); /* initialise tty_fd, tty_devtty */
}) })
#define mksh_sdirsep(s) __extension__({ \ #define mksh_sdirsep(s) __extension__({ \
const char *mksh_sdirsep_s = (s); \ const char *mksh_sdirsep_s = (s); \
((char *)((ksh_isalphx(mksh_sdirsep_s[0]) && \ ((char *)((ctype(mksh_sdirsep_s[0], C_ALPHA) && \
mksh_sdirsep_s[1] == ':' && \ mksh_sdirsep_s[1] == ':' && \
!mksh_cdirsep(mksh_sdirsep_s[2])) ? \ !mksh_cdirsep(mksh_sdirsep_s[2])) ? \
(mksh_sdirsep_s + 1) : strpbrk(mksh_sdirsep_s, "/\\"))); \ (mksh_sdirsep_s + 1) : strpbrk(mksh_sdirsep_s, "/\\"))); \

6
syn.c
View File

@ -23,7 +23,7 @@
#include "sh.h" #include "sh.h"
__RCSID("$MirOS: src/bin/mksh/syn.c,v 1.121 2017/04/27 19:33:53 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/syn.c,v 1.122 2017/04/27 20:22:28 tg Exp $");
struct nesting_state { struct nesting_state {
int start_token; /* token than began nesting (eg, FOR) */ int start_token; /* token than began nesting (eg, FOR) */
@ -697,10 +697,10 @@ function_body(char *name, int sALIAS,
* only allow [a-zA-Z_0-9] but this allows more as old pdkshs * only allow [a-zA-Z_0-9] but this allows more as old pdkshs
* have allowed more; the following were never allowed: * have allowed more; the following were never allowed:
* NUL TAB NL SP " $ & ' ( ) ; < = > \ ` | * NUL TAB NL SP " $ & ' ( ) ; < = > \ ` |
* C_QUOTE covers all but adds # * ? [ ] * C_QUOTE|C_SPC covers all but adds # * ? [ ]
*/ */
for (p = sname; *p; p++) for (p = sname; *p; p++)
if (ctype(*p, C_QUOTE)) if (ctype(*p, C_QUOTE | C_SPC))
yyerror(Tinvname, sname, Tfunction); yyerror(Tinvname, sname, Tfunction);
/* /*