make var.c:getint() more like misc.c:getn(), except for the API differences:
getn() parses a decimal 32-bit integer, getint() a POSIX- or ksh-style based integer with unsigned wraparound to 32 bit, then possible negation (so that, for example, -0xFFFFFFFF continues to work)
This commit is contained in:
61
var.c
61
var.c
@@ -28,7 +28,7 @@
|
|||||||
#include <sys/sysctl.h>
|
#include <sys/sysctl.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
__RCSID("$MirOS: src/bin/mksh/var.c,v 1.188 2015/04/19 14:22:09 tg Exp $");
|
__RCSID("$MirOS: src/bin/mksh/var.c,v 1.189 2015/04/19 18:13:31 tg Exp $");
|
||||||
|
|
||||||
/*-
|
/*-
|
||||||
* Variables
|
* Variables
|
||||||
@@ -481,7 +481,7 @@ setint(struct tbl *vq, mksh_ari_t n)
|
|||||||
static int
|
static int
|
||||||
getint(struct tbl *vp, mksh_ari_u *nump, bool arith)
|
getint(struct tbl *vp, mksh_ari_u *nump, bool arith)
|
||||||
{
|
{
|
||||||
mksh_uari_t c, num, base;
|
mksh_uari_t c, num = 0, base = 10;
|
||||||
const char *s;
|
const char *s;
|
||||||
bool have_base = false, neg = false;
|
bool have_base = false, neg = false;
|
||||||
|
|
||||||
@@ -495,27 +495,43 @@ getint(struct tbl *vp, mksh_ari_u *nump, bool arith)
|
|||||||
return (vp->type);
|
return (vp->type);
|
||||||
}
|
}
|
||||||
s = vp->val.s + vp->type;
|
s = vp->val.s + vp->type;
|
||||||
base = 10;
|
|
||||||
num = 0;
|
do {
|
||||||
if (arith && s[0] == '0' && (s[1] | 0x20) == 'x') {
|
c = (unsigned char)*s++;
|
||||||
s += 2;
|
} while (ksh_isspace(c));
|
||||||
base = 16;
|
|
||||||
have_base = true;
|
switch (c) {
|
||||||
|
case '-':
|
||||||
|
neg = true;
|
||||||
|
/* FALLTHROUGH */
|
||||||
|
case '+':
|
||||||
|
c = (unsigned char)*s++;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
if (Flag(FPOSIX) && arith && s[0] == '0' && ksh_isdigit(s[1]) &&
|
|
||||||
|
if (c == '0' && arith) {
|
||||||
|
if ((s[0] | 0x20) == 'x') {
|
||||||
|
/* interpret as hexadecimal */
|
||||||
|
base = 16;
|
||||||
|
++s;
|
||||||
|
goto getint_c_style_base;
|
||||||
|
} else if (Flag(FPOSIX) && ksh_isdigit(s[0]) &&
|
||||||
!(vp->flag & ZEROFIL)) {
|
!(vp->flag & ZEROFIL)) {
|
||||||
/* interpret as octal (deprecated) */
|
/* interpret as octal (deprecated) */
|
||||||
base = 8;
|
base = 8;
|
||||||
|
getint_c_style_base:
|
||||||
have_base = true;
|
have_base = true;
|
||||||
|
c = (unsigned char)*s++;
|
||||||
}
|
}
|
||||||
while ((c = (unsigned char)*s++)) {
|
}
|
||||||
if (c == '-') {
|
|
||||||
neg = true;
|
do {
|
||||||
continue;
|
if (c == '#') {
|
||||||
} else if (c == '#') {
|
/* ksh-style base determination */
|
||||||
if (have_base || num < 1)
|
if (have_base || num < 1)
|
||||||
return (-1);
|
return (-1);
|
||||||
if ((base = num) == 1) {
|
if ((base = num) == 1) {
|
||||||
|
/* mksh-specific extension */
|
||||||
unsigned int wc;
|
unsigned int wc;
|
||||||
|
|
||||||
if (!UTFMODE)
|
if (!UTFMODE)
|
||||||
@@ -535,18 +551,21 @@ getint(struct tbl *vp, mksh_ari_u *nump, bool arith)
|
|||||||
num = 0;
|
num = 0;
|
||||||
have_base = true;
|
have_base = true;
|
||||||
continue;
|
continue;
|
||||||
} else if (ksh_isdigit(c))
|
}
|
||||||
|
if (ksh_isdigit(c))
|
||||||
c -= '0';
|
c -= '0';
|
||||||
else if (ksh_islower(c))
|
else {
|
||||||
c -= 'a' - 10;
|
c |= 0x20;
|
||||||
else if (ksh_isupper(c))
|
if (!ksh_islower(c))
|
||||||
c -= 'A' - 10;
|
|
||||||
else
|
|
||||||
return (-1);
|
return (-1);
|
||||||
|
c -= 'a' - 10;
|
||||||
|
}
|
||||||
if (c >= base)
|
if (c >= base)
|
||||||
return (-1);
|
return (-1);
|
||||||
|
/* handle overflow as truncation */
|
||||||
num = num * base + c;
|
num = num * base + c;
|
||||||
}
|
} while ((c = (unsigned char)*s++));
|
||||||
|
|
||||||
if (neg)
|
if (neg)
|
||||||
num = -num;
|
num = -num;
|
||||||
nump->u = num;
|
nump->u = num;
|
||||||
|
Reference in New Issue
Block a user