• finally, the code and manual page text to deprecate, and code to not

handle any more, octal 010 style constants, as promised
• overhaul the manpage re. arithmetic expressions, make the guarantees
  mksh code has explicitly, precisely, clear
• to reduce burden of the compiler, getint() now operates on mksh_uari_t
  internally; it already applied the sign after operation, anyway (C99
  guarantees wraparound on unsigned types, but for signed types we need
  specific compiler support; apparently, this comes from hardware limits)
• use const and shuffle order of locals around while here
This commit is contained in:
tg 2011-12-10 13:34:19 +00:00
parent f8098a7f48
commit 3b87d173d4
4 changed files with 35 additions and 40 deletions

View File

@ -1,4 +1,4 @@
# $MirOS: src/bin/mksh/check.t,v 1.500 2011/12/09 20:40:02 tg Exp $ # $MirOS: src/bin/mksh/check.t,v 1.501 2011/12/10 13:34:14 tg Exp $
# $OpenBSD: bksl-nl.t,v 1.2 2001/01/28 23:04:56 niklas Exp $ # $OpenBSD: bksl-nl.t,v 1.2 2001/01/28 23:04:56 niklas Exp $
# $OpenBSD: history.t,v 1.5 2001/01/28 23:04:56 niklas Exp $ # $OpenBSD: history.t,v 1.5 2001/01/28 23:04:56 niklas Exp $
# $OpenBSD: read.t,v 1.3 2003/03/10 03:48:16 david Exp $ # $OpenBSD: read.t,v 1.3 2003/03/10 03:48:16 david Exp $
@ -28,7 +28,7 @@
# http://www.freebsd.org/cgi/cvsweb.cgi/src/tools/regression/bin/test/regress.sh?rev=HEAD # http://www.freebsd.org/cgi/cvsweb.cgi/src/tools/regression/bin/test/regress.sh?rev=HEAD
expected-stdout: expected-stdout:
@(#)MIRBSD KSH R40 2011/12/09 @(#)MIRBSD KSH R40 2011/12/10
description: description:
Check version of shell. Check version of shell.
stdin: stdin:
@ -3603,6 +3603,7 @@ expected-stdout:
name: integer-base-check-flat-posix name: integer-base-check-flat-posix
description: description:
Check behaviour of POSuX bases Check behaviour of POSuX bases
category: !nodeprecated
stdin: stdin:
echo :$((10)).$((010)).$((0x10)). echo :$((10)).$((010)).$((0x10)).
expected-stdout: expected-stdout:
@ -3612,6 +3613,7 @@ name: integer-base-check-flat-right
description: description:
Check behaviour does not match POSuX, because a not type-safe Check behaviour does not match POSuX, because a not type-safe
scripting language has *no* business interpreting "010" as octal scripting language has *no* business interpreting "010" as octal
category: nodeprecated
stdin: stdin:
echo :$((10)).$((010)).$((0x10)). echo :$((10)).$((010)).$((0x10)).
expected-stdout: expected-stdout:

37
mksh.1
View File

@ -1,4 +1,4 @@
.\" $MirOS: src/bin/mksh/mksh.1,v 1.280 2011/12/03 00:09:15 tg Exp $ .\" $MirOS: src/bin/mksh/mksh.1,v 1.281 2011/12/10 13:34:16 tg Exp $
.\" $OpenBSD: ksh.1,v 1.141 2011/09/03 22:59:08 jmc Exp $ .\" $OpenBSD: ksh.1,v 1.141 2011/09/03 22:59:08 jmc Exp $
.\"- .\"-
.\" Copyright © 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, .\" Copyright © 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
@ -72,7 +72,7 @@
.\" with -mandoc, it might implement .Mx itself, but we want to .\" with -mandoc, it might implement .Mx itself, but we want to
.\" use our own definition. And .Dd must come *first*, always. .\" use our own definition. And .Dd must come *first*, always.
.\" .\"
.Dd $Mdocdate: December 3 2011 $ .Dd $Mdocdate: December 10 2011 $
.\" .\"
.\" Check which macro package we use .\" Check which macro package we use
.\" .\"
@ -2465,14 +2465,8 @@ Grouping operators:
( ) ( )
.Ed .Ed
.Pp .Pp
Integer constants and expressions are calculated using the Integer constants and expressions are calculated using an exactly 32-bit
.Vt mksh_ari_t wide, signed or unsigned, type with silent wraparound on integer overflow.
.Po if signed Pc
or
.Vt mksh_uari_t
.Po if unsigned Pc
type, and are limited to 32 bits.
Overflows wrap silently.
Integer constants may be specified with arbitrary bases using the notation Integer constants may be specified with arbitrary bases using the notation
.Ar base Ns # Ns Ar number , .Ar base Ns # Ns Ar number ,
where where
@ -2480,22 +2474,15 @@ where
is a decimal integer specifying the base, and is a decimal integer specifying the base, and
.Ar number .Ar number
is a number in the specified base. is a number in the specified base.
Additionally, Additionally, base-16 integers may be specified by prefixing them with
integers may be prefixed with .Sq 0x Pq case-insensitive
.Sq 0X in all forms of arithmetic expressions, except as numeric arguments to the
or
.Sq 0x
(specifying base 16), similar to
.At
.Nm ksh ,
or
.Sq 0
(base 8), as an
.Nm
extension, in all forms of arithmetic expressions,
except as numeric arguments to the
.Ic test .Ic test
command. built-in command.
Base-8 integers may be specified by prefixing them with
.Sq 0 ,
but this is deprecated and will be removed in the next version of
.Nm .
As a special As a special
.Nm mksh .Nm mksh
extension, numbers to the base of one are treated as either (8-bit extension, numbers to the base of one are treated as either (8-bit

4
sh.h
View File

@ -151,9 +151,9 @@
#endif #endif
#ifdef EXTERN #ifdef EXTERN
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.509 2011/12/09 20:40:26 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/sh.h,v 1.510 2011/12/10 13:34:18 tg Exp $");
#endif #endif
#define MKSH_VERSION "R40 2011/12/09" #define MKSH_VERSION "R40 2011/12/10"
/* arithmetics types */ /* arithmetics types */
typedef int32_t mksh_ari_t; typedef int32_t mksh_ari_t;

28
var.c
View File

@ -26,7 +26,7 @@
#include <sys/sysctl.h> #include <sys/sysctl.h>
#endif #endif
__RCSID("$MirOS: src/bin/mksh/var.c,v 1.136 2011/11/26 00:45:03 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/var.c,v 1.137 2011/12/10 13:34:19 tg Exp $");
/*- /*-
* Variables * Variables
@ -469,10 +469,10 @@ setint(struct tbl *vq, mksh_ari_t n)
static int static int
getint(struct tbl *vp, mksh_ari_t *nump, bool arith) getint(struct tbl *vp, mksh_ari_t *nump, bool arith)
{ {
char *s;
int c, base, neg; int c, base, neg;
mksh_uari_t num;
const char *s;
bool have_base = false; bool have_base = false;
mksh_ari_t num;
if (vp->flag&SPECIAL) if (vp->flag&SPECIAL)
getspec(vp); getspec(vp);
@ -487,6 +487,13 @@ getint(struct tbl *vp, mksh_ari_t *nump, bool arith)
base = 10; base = 10;
num = 0; num = 0;
neg = 0; neg = 0;
#ifdef MKSH_DISABLE_DEPRECATED
if (arith && s[0] == '0' && (s[1] | 0x20) == 'x') {
s += 2;
base = 16;
have_base = true;
}
#else
if (arith && *s == '0' && *(s+1)) { if (arith && *s == '0' && *(s+1)) {
s++; s++;
if (*s == 'x' || *s == 'X') { if (*s == 'x' || *s == 'X') {
@ -499,19 +506,20 @@ getint(struct tbl *vp, mksh_ari_t *nump, bool arith)
base = 8; base = 8;
have_base = true; have_base = true;
} }
for (c = *s++; c ; c = *s++) { #endif
while ((c = *s++)) {
if (c == '-') { if (c == '-') {
neg++; neg++;
continue; continue;
} else if (c == '#') { } else if (c == '#') {
base = (int)num; if (have_base || num < 1 || num > 36)
if (have_base || base < 1 || base > 36)
return (-1); return (-1);
base = (int)num;
if (base == 1) { if (base == 1) {
unsigned int wc; unsigned int wc;
if (!UTFMODE) if (!UTFMODE)
wc = *(unsigned char *)s; wc = *(const unsigned char *)s;
else if (utf_mbtowc(&wc, s) == (size_t)-1) else if (utf_mbtowc(&wc, s) == (size_t)-1)
/* OPTU-8 -> OPTU-16 */ /* OPTU-8 -> OPTU-16 */
/* /*
@ -519,7 +527,7 @@ getint(struct tbl *vp, mksh_ari_t *nump, bool arith)
* the same as 1#\x80 does, thus is * the same as 1#\x80 does, thus is
* not round-tripping correctly XXX) * not round-tripping correctly XXX)
*/ */
wc = 0xEF00 + *(unsigned char *)s; wc = 0xEF00 + *(const unsigned char *)s;
*nump = (mksh_ari_t)wc; *nump = (mksh_ari_t)wc;
return (1); return (1);
} }
@ -538,9 +546,7 @@ getint(struct tbl *vp, mksh_ari_t *nump, bool arith)
return (-1); return (-1);
num = num * base + c; num = num * base + c;
} }
if (neg) *nump = neg ? -((mksh_ari_t)num) : (mksh_ari_t)num;
num = -num;
*nump = num;
return (base); return (base);
} }