make ord() result unsigned int; add asc() which is:

• not designed to be emitted, only used in comparisons with
  other asc() results
• on EBCDIC platforms, the mapping of an EBCDIC octet to their
  corresponding ASCII or Unicode/UCS-4 codepoint or, if there
  is no mapping, a distinct value above all valid Unicode codepoints
• on nōn-EBCDIC platforms, just the identity mapping of the input
  octet into their ord() value

Intended use are ASCII-ish character ops, including ranges (“A-Z”),
mapping from those to the corresponding digit offset, and sorting
of things in an ASCIIbetical way
This commit is contained in:
tg 2017-04-21 19:50:09 +00:00
parent 36ea8e6171
commit e18a509a80
4 changed files with 25 additions and 23 deletions

6
edit.c
View File

@ -28,7 +28,7 @@
#ifndef MKSH_NO_CMDLINE_EDITING
__RCSID("$MirOS: src/bin/mksh/edit.c,v 1.322 2017/04/21 19:08:17 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/edit.c,v 1.323 2017/04/21 19:50:06 tg Exp $");
/*
* in later versions we might use libtermcap for this, but since external
@ -3685,7 +3685,7 @@ vi_hook(int ch)
return (1);
cmdlen = 0;
argc1 = 0;
if (ch >= ord('1') && ch <= ord('9')) {
if (ksh_isdigit(ch)) {
argc1 = ksh_numdig(ch);
state = VARG1;
} else {
@ -3740,7 +3740,7 @@ vi_hook(int ch)
case VEXTCMD:
argc2 = 0;
if (ch >= ord('1') && ch <= ord('9')) {
if (ksh_isdigit(ch)) {
argc2 = ksh_numdig(ch);
state = VARG2;
return (0);

View File

@ -38,7 +38,7 @@
#endif
#endif
__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.341 2017/04/17 19:51:46 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.342 2017/04/21 19:50:07 tg Exp $");
#if HAVE_KILLPG
/*
@ -1419,7 +1419,7 @@ c_umask(const char **wp)
if (ksh_isdigit(*cp)) {
new_umask = 0;
while (*cp >= ord('0') && *cp <= ord('7')) {
while (asc(*cp) >= asc('0') && asc(*cp) <= asc('7')) {
new_umask = new_umask * 8 + ksh_numdig(*cp);
++cp;
}

10
misc.c
View File

@ -30,7 +30,7 @@
#include <grp.h>
#endif
__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.256 2017/04/20 16:34:39 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.257 2017/04/21 19:50:08 tg Exp $");
#define KSH_CHVT_FLAG
#ifdef MKSH_SMALL
@ -2209,7 +2209,7 @@ unbksl(bool cstyle, int (*fg)(void), void (*fp)(int))
wc = 0;
i = 3;
while (i--)
if ((c = (*fg)()) >= ord('0') && c <= ord('7'))
if (ksh_isdigit((c = (*fg)())) && asc(c) <= asc('7'))
wc = (wc << 3) + ksh_numdig(c);
else {
(*fp)(c);
@ -2237,11 +2237,11 @@ unbksl(bool cstyle, int (*fg)(void), void (*fp)(int))
n = 0;
while (n < i || i == -1) {
wc <<= 4;
if ((c = (*fg)()) >= ord('0') && c <= ord('9'))
if (ksh_isdigit((c = (*fg)())))
wc += ksh_numdig(c);
else if (c >= ord('A') && c <= ord('F'))
else if (asc(c) >= asc('A') && asc(c) <= asc('F'))
wc += ksh_numuc(c) + 10;
else if (c >= ord('a') && c <= ord('f'))
else if (asc(c) >= asc('a') && asc(c) <= asc('f'))
wc += ksh_numlc(c) + 10;
else {
wc >>= 4;

28
sh.h
View File

@ -175,7 +175,7 @@
#endif
#ifdef EXTERN
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.811 2017/04/20 20:50:14 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.812 2017/04/21 19:50:09 tg Exp $");
#endif
#define MKSH_VERSION "R55 2017/04/20"
@ -1300,26 +1300,28 @@ EXTERN bool really_exit;
extern unsigned char chtypes[];
#define ctype(c, t) tobool(chtypes[(unsigned char)(c)] & (t))
#define ord(c) ((int)(unsigned char)(c))
#define ksh_issubop2(c) tobool((c) == ord('#') || (c) == ord('%'))
#define ksh_isalias(c) (ctype((c), C_ALPHX | C_DIGIT) || (c) == ord('!') || \
(c) == ord('%') || (c) == ord(',') || \
(c) == ord('.') || (c) == ord('@') || \
(c) == ord('-'))
#define ksh_isalpha(c) (ctype((c), C_ALPHX) && (c) != ord('_'))
#define ord(c) ((unsigned int)(unsigned char)(c))
/* identity transformation in !EBCDIC; Unicode map (or higher) in EBCDIC */
#define asc(c) ord(c)
#define ksh_issubop2(c) tobool(ord(c) == ord('#') || ord(c) == ord('%'))
#define ksh_isalias(c) (ctype((c), C_ALPHX | C_DIGIT) || \
ord(c) == ord('!') || ord(c) == ord('%') || \
ord(c) == ord(',') || ord(c) == ord('.') || \
ord(c) == ord('@') || ord(c) == ord('-'))
#define ksh_isalpha(c) (ctype((c), C_ALPHX) && ord(c) != ord('_'))
#define ksh_isalphx(c) ctype((c), C_ALPHX)
#define ksh_isalnux(c) ctype((c), C_ALPHX | C_DIGIT)
#define ksh_isdigit(c) ctype((c), C_DIGIT)
#define ksh_islower(c) (((c) >= 'a') && ((c) <= 'z'))
#define ksh_isupper(c) (((c) >= 'A') && ((c) <= 'Z'))
#define ksh_islower(c) ((asc(c) >= asc('a')) && (asc(c) <= asc('z')))
#define ksh_isupper(c) ((asc(c) >= asc('A')) && (asc(c) <= asc('Z')))
#define ksh_tolower(c) (ksh_isupper(c) ? (c) - 'A' + 'a' : (c))
#define ksh_toupper(c) (ksh_islower(c) ? (c) - 'a' + 'A' : (c))
#define ksh_isdash(s) (((s)[0] == '-') && ((s)[1] == '\0'))
#define ksh_isspace(c) ((((c) >= 0x09) && ((c) <= 0x0D)) || ((c) == 0x20))
#define ksh_eq(c,u,l) (((c) | 0x20) == (l))
#define ksh_numdig(c) ((c) - ord('0'))
#define ksh_numuc(c) ((c) - ord('A'))
#define ksh_numlc(c) ((c) - ord('a'))
#define ksh_numdig(c) (asc(c) - asc('0'))
#define ksh_numuc(c) (asc(c) - asc('A'))
#define ksh_numlc(c) (asc(c) - asc('a'))
EXTERN int ifs0 E_INIT(' '); /* for "$*" */