From dc94c3d2059d7fa381260b8f2005f6e21f832f4b Mon Sep 17 00:00:00 2001 From: tg Date: Sat, 27 Apr 2013 18:50:25 +0000 Subject: [PATCH] =?UTF-8?q?after=20enough=20complaints=20by=20POSIX=20sh?= =?UTF-8?q?=20advocates,=20=E2=80=A2=20make=20parsing=20numbers=20with=20l?= =?UTF-8?q?eading=20digit-zero=20as=20octal=20independent=20of=20=20=20mks?= =?UTF-8?q?h/lksh=20and=20dependent=20on=20set=20-o=20posix;=20adjust=20ma?= =?UTF-8?q?npages=20to=20match=20=E2=80=A2=20warn=20about=20these=20change?= =?UTF-8?q?s=20and=20why=20mksh=20uses=2032-bit=20consistent=20arithmetics?= =?UTF-8?q?=20=20=20and=20point=20people=20to=20lksh=20for=20host-long=20u?= =?UTF-8?q?ndefined-behaviour=20arithmetics=20=E2=80=A2=20point=20out,=20e?= =?UTF-8?q?xplicitly,=20that=20it=20is=20*legal*=20for=20the=20operating?= =?UTF-8?q?=20environment=20=20=20to=20make=20'print=20$((2147483647=20+?= =?UTF-8?q?=201))'=20(on=20a=2032-bit=20system;=20adjust=20for=20a=20=20?= =?UTF-8?q?=2064-bit=20system)=20to=20run=20'rm=20-rf=20~=20/'=20instead?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 7 +++++-- check.t | 25 ++++++++++--------------- lksh.1 | 33 +++++++++++++++++++++------------ mksh.1 | 27 +++++++++++++++++++++------ var.c | 6 ++---- 5 files changed, 59 insertions(+), 39 deletions(-) diff --git a/Makefile b/Makefile index cff9d78..70bd892 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -# $MirOS: src/bin/mksh/Makefile,v 1.119 2013/04/27 18:22:47 tg Exp $ +# $MirOS: src/bin/mksh/Makefile,v 1.120 2013/04/27 18:50:21 tg Exp $ #- # Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, # 2011, 2012, 2013 @@ -104,6 +104,9 @@ test-build-lksh: .PHONY cd ${.CURDIR} && exec ${MAKE} lksh.cat1 test-build \ _TBF=-L USE_PRINTF_BUILTIN=0 +bothmans: .PHONY + cd ${.CURDIR} && exec ${MAKE} MAN='lksh.1 mksh.1' __MANALL + cleandir: clean-extra clean-extra: .PHONY @@ -132,7 +135,7 @@ CLEANFILES+= ${MAN:S/$/.txt/} ${MAN:S/$/.txt.gz/} CATS_KW= mksh, ksh, sh CATS_TITLE_mksh_1=mksh - The MirBSD Korn Shell cats: ${MANALL} ${MANALL:S/.cat/.ps/} -.if "${MANALL:Nmksh.cat1}" != "" +.if "${MANALL:Nlksh.cat1:Nmksh.cat1}" != "" . error Adjust here. .endif .for _m _n in mksh 1 diff --git a/check.t b/check.t index 84bffc0..0ba027c 100644 --- a/check.t +++ b/check.t @@ -1,4 +1,4 @@ -# $MirOS: src/bin/mksh/check.t,v 1.607 2013/04/26 21:22:42 tg Exp $ +# $MirOS: src/bin/mksh/check.t,v 1.608 2013/04/27 18:50:21 tg 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: read.t,v 1.3 2003/03/10 03:48:16 david Exp $ @@ -3779,22 +3779,17 @@ expected-stdout: --- name: integer-base-check-flat description: - Check behaviour does not match POSuX, because a not type-safe - scripting language has *no* business interpreting "010" as octal -category: shell:legacy-no + Check behaviour does not match POSuX (except if set -o posix), + because a not type-safe scripting language has *no* business + interpreting the string "010" as octal numer eight (dangerous). stdin: - echo :$((10)).$((010)).$((0x10)). + echo 1 "$("$__progname" -c 'echo :$((10))/$((010)),$((0x10)):')" . + echo 2 "$("$__progname" -o posix -c 'echo :$((10))/$((010)),$((0x10)):')" . + echo 3 "$("$__progname" -o sh -c 'echo :$((10))/$((010)),$((0x10)):')" . expected-stdout: - :10.10.16. ---- -name: integer-base-check-flat-legacy -description: - Check behaviour matches POSuX for LEGACY KSH -category: shell:legacy-yes -stdin: - echo :$((10)).$((010)).$((0x10)). -expected-stdout: - :10.8.16. + 1 :10/10,16: . + 2 :10/8,16: . + 3 :10/10,16: . --- name: integer-base-check-numeric-from description: diff --git a/lksh.1 b/lksh.1 index b58a9bb..8f8a1e8 100644 --- a/lksh.1 +++ b/lksh.1 @@ -1,7 +1,22 @@ -.\" $MirOS: src/bin/mksh/lksh.1,v 1.1 2013/04/27 18:13:58 tg Exp $ +.\" $MirOS: src/bin/mksh/lksh.1,v 1.2 2013/04/27 18:50:24 tg Exp $ .\"- .\" Copyright (c) 2008, 2009, 2010, 2012, 2013 .\" Thorsten “mirabilos” Glaser +.\" +.\" Provided that these terms and disclaimer and all copyright notices +.\" are retained or reproduced in an accompanying document, permission +.\" is granted to deal in this work without restriction, including un‐ +.\" limited rights to use, publicly perform, distribute, sell, modify, +.\" merge, give away, or sublicence. +.\" +.\" This work is provided “AS IS” and WITHOUT WARRANTY of any kind, to +.\" the utmost extent permitted by applicable law, neither express nor +.\" implied; without malicious intent or gross negligence. In no event +.\" may a licensor, author or contributor be held liable for indirect, +.\" direct, other damage, loss, or other issues arising in any way out +.\" of dealing in the work, even if advised of the possibility of such +.\" damage or existence of a defect, except proven that it results out +.\" of said person’s immediate fault when using the work as intended. .\"- .\" Try to make GNU groff and AT&T nroff more compatible .\" * ` generates ‘ in gnroff, so use \` @@ -149,11 +164,15 @@ .Ek .Sh DESCRIPTION .Nm -is a command interpreter intended exclusive for running legacy +is a command interpreter intended exclusively for running legacy shell scripts. It is built on .Nm mksh ; refer to its manual page for details on the scripting language. +It is recommended to port scripts to +.Nm mksh +instead of relying on legacy or idiotic POSIX-mandated behaviour, +since the MirBSD Korn Shell scripting language is much more consistent. .Sh LEGACY MODE .Nm has the following differences from @@ -212,9 +231,6 @@ unlike .Nm ksh , does not keep file descriptors \*(Gt 2 private. .It -.Nm -parses leading-zero numbers as octal (base 8). -.It Integers use the host C environment's .Vt long type, not @@ -246,13 +262,6 @@ tries to make a cross between a legacy bourne/posix compatibl-ish shell and a legacy pdksh-alike but .Dq legacy is not exactly specified. -Parsing numbers with leading zero digits or -.Dq 0x -is relatively recent in all -.Nm pdksh -derivates, but supported here for completeness. -It might make sense to make this a run-time option, but -that might also be overkill. .Pp The .Ic set diff --git a/mksh.1 b/mksh.1 index 777994f..e15a022 100644 --- a/mksh.1 +++ b/mksh.1 @@ -1,4 +1,4 @@ -.\" $MirOS: src/bin/mksh/mksh.1,v 1.310 2013/04/26 21:22:47 tg Exp $ +.\" $MirOS: src/bin/mksh/mksh.1,v 1.311 2013/04/27 18:50:24 tg Exp $ .\" $OpenBSD: ksh.1,v 1.146 2013/03/18 11:10:52 mpi Exp $ .\"- .\" Copyright © 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, @@ -74,7 +74,7 @@ .\" with -mandoc, it might implement .Mx itself, but we want to .\" use our own definition. And .Dd must come *first*, always. .\" -.Dd $Mdocdate: April 26 2013 $ +.Dd $Mdocdate: April 27 2013 $ .\" .\" Check which macro package we use, and do other -mdoc setup. .\" @@ -2563,9 +2563,14 @@ Additionally, base-16 integers may be specified by prefixing them with in all forms of arithmetic expressions, except as numeric arguments to the .Ic test built-in command. -It is discouraged to prefix numbers with a sole zero -.Pq Sq 0 , -because some shells may interpret them as base-8 integers. +Prefixing numbers with a sole digit zero +.Pq Sq 0 +leads to the shell interpreting it as base-8 integer in +.Ic posix +mode +.Em only ; +historically, (pd)ksh has never done so either anyway, +and it's unsafe to do that, but POSIX demands it nowadays. As a special .Nm mksh extension, numbers to the base of one are treated as either (8-bit @@ -6334,6 +6339,16 @@ Use co-processes instead. foo \*(Ba bar \*(Ba read baz # will not change $baz foo \*(Ba bar \*(Ba& read \-p baz # will, however, do so .Ed +.Pp +.Nm mksh +provides a consistent set of 32-bit integer arithmetics, both signed +and unsigned, with defined wraparound and sign of the result of a modulo +operation, even (defying POSIX) on 64-bit systems. +If you require 64-bit integer arithmetics, use +.Nm lksh Pq legacy mksh +instead, but be aware that, in POSIX, it's legal for the OS to make +.Li print $((2147483647 + 1)) +delete all files on your system, as it's Undefined Behaviour. .Sh BUGS Suspending (using \*(haZ) pipelines like the one below will only suspend the currently running part of the pipeline; in this example, @@ -6345,7 +6360,7 @@ $ /bin/sleep 666 && echo fubar .Ed .Pp This document attempts to describe -.Nm mksh\ R45 +.Nm mksh\ R46 and up, compiled without any options impacting functionality, such as .Dv MKSH_SMALL , diff --git a/var.c b/var.c index 437ad64..5c6b2ac 100644 --- a/var.c +++ b/var.c @@ -27,7 +27,7 @@ #include #endif -__RCSID("$MirOS: src/bin/mksh/var.c,v 1.170 2013/04/07 14:11:54 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/var.c,v 1.171 2013/04/27 18:50:25 tg Exp $"); /*- * Variables @@ -495,14 +495,12 @@ getint(struct tbl *vp, mksh_ari_u *nump, bool arith) base = 16; have_base = true; } -#ifdef MKSH_LEGACY_MODE - if (arith && s[0] == '0' && ksh_isdigit(s[1]) && + if (Flag(FPOSIX) && arith && s[0] == '0' && ksh_isdigit(s[1]) && !(vp->flag & ZEROFIL)) { /* interpret as octal (deprecated) */ base = 8; have_base = true; } -#endif while ((c = *s++)) { if (c == '-') { neg = true;