1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-03-09 15:50:02 +00:00

enum: remove arbitrary one-argument limitation

b_enum() contains a check that exactly one argument is given:

237:	if (error_info.errors || !*argv || *(argv + 1))

But the subsequent argument handling loop will happily deal with
multiple arguments:

246:	while(cp = *argv++)

Every other declaration command supports multiple arguments and I
see no reason why enum shouldn't. Simply removing the '*(argv + 1)'
check allows 'enum' to create more than one type per invocation.

src/cmd/ksh93/bltins/enum.c:
- b_enum(): Remove check for >1 args as described above.
- Update documentation to describe the behaviour of enumeration
  types in arithmetic expressions and to add an example: a bool
  type with two enumeration values 'false' (0) and 'true' (1).
  That type is predefined in ksh 93v- and 2020. We're not going
  to do that in 93u+m but it's good to document the possibility.

src/cmd/ksh93/sh.1:
- Make changes parallel to the enum.c self-doc update.
This commit is contained in:
Martijn Dekker 2022-03-05 01:43:29 +01:00
parent b398f33c49
commit fae1932e62
4 changed files with 39 additions and 12 deletions

4
NEWS
View file

@ -3,6 +3,10 @@ For full details, see the git log at: https://github.com/ksh93/ksh/tree/1.0
Any uppercase BUG_* names are modernish shell bug IDs.
2022-03-05:
- The 'enum' command can now create more than one type per invocation.
2022-02-23:
- When reading input from the keyboard, ksh now turns off nonblocking I/O

View file

@ -20,21 +20,33 @@
***********************************************************************/
#include "defs.h"
#define ENUM_ID "enum (ksh 93u+m) 2021-12-17"
#define ENUM_ID "enum (ksh 93u+m) 2022-03-05"
const char sh_optenum[] =
"[-?@(#)$Id: " ENUM_ID " $\n]"
"[--catalog?" ERROR_CATALOG "]"
"[+NAME?enum - create an enumeration type]"
"[+DESCRIPTION?\benum\b is a declaration command that creates an enumeration "
"type \atypename\a that can only store any one of the values in the indexed "
"array variable \atypename\a.]"
"[+DESCRIPTION?\benum\b is a declaration command that creates one or more "
"enumeration type declaration commands named \atypename\a. Variables "
"of the created type can only store any one of the \avalue\as given. "
"For example, \benum bool=(false true)\b creates a Boolean variable "
"type of which variables may be declared like \bbool x=true y=false\b.]"
"[+?If the list of \avalue\as is omitted, then \atypename\a must name an "
"indexed array variable with at least two elements.]"
"[+?For more information, see \atypename\a \b--man\b.]"
"[+?For more information, create a type, then use \atypename\a \b--man\b.]"
"[+USE IN ARITHMETIC EXPRESSIONS?When an enumeration variable is used in an "
"arithmetic expression, its value is the index into the array that "
"defined it, starting from 0. Taking the \bbool\b type from the "
"example above, if a variable of this type is used in an arithmetic "
"expression, \bfalse\b translates to 0 and \btrue\b to 1.]"
"[+?Enumeration values may also be used directly in an arithmetic expression "
"that refers to a variable of an enumeration type. "
"To continue our example, for a \bbool\b variable \bv\b, "
"\b((v==true))\b is the same as \b((v==1))\b and "
"if a variable named \btrue\b exists, it is ignored.]"
"[i:ignorecase?The values are case insensitive.]"
"\n"
"\n\atypename\a[\b=(\b \avalue\a ... \b)\b]\n"
"\n\atypename\a[\b=(\b \avalue\a ... \b)\b] ...\n"
"\n"
"[+EXIT STATUS]"
"{"
@ -234,7 +246,7 @@ int b_enum(int argc, char** argv, Shbltin_t *context)
break;
}
argv += opt_info.index;
if (error_info.errors || !*argv || *(argv + 1))
if (error_info.errors || !*argv)
{
error(ERROR_USAGE|2, "%s", optusage(NiL));
return 1;

View file

@ -21,7 +21,7 @@
#define SH_RELEASE_FORK "93u+m" /* only change if you develop a new ksh93 fork */
#define SH_RELEASE_SVER "1.0.0-beta.2" /* semantic version number: https://semver.org */
#define SH_RELEASE_DATE "2022-02-23" /* must be in this format for $((.sh.version)) */
#define SH_RELEASE_DATE "2022-03-05" /* must be in this format for $((.sh.version)) */
#define SH_RELEASE_CPYR "(c) 2020-2022 Contributors to ksh " SH_RELEASE_FORK
/* Scripts sometimes field-split ${.sh.version}, so don't change amount of whitespace. */

View file

@ -6392,10 +6392,13 @@ See
.IR echo (1)
for usage and description.
.TP
\(dd \f3enum\fP \*(OK \f3\-i\fP \*(CK \f2type\^\fP\*(OK=(\f2value\^\fP .\|.\|.) \*(CK
Creates a declaration command named \f2type\^\fP that
allows one of the specified \f2value\fPs as
enumeration names. If \f3=(\fP\f2value\^\ .\|.\|.\|\fP\f3)\fP is
\(dd \f3enum\fP \*(OK \f3\-i\fP \*(CK \f2type\^\fP\*(OK=(\f2value\^\fP .\|.\|.) \*(CK .\|.\|.
Creates, for each \f2type\^\fP specified,
an enumeration type declaration command named \f2type\^\fP.
Variables of the created type can only store any one of the \f2value\fPs given.
For example, \f3enum bool=(false true)\fP creates a Boolean variable
type of which variables may be declared like \f3bool x=true y=false\fP.
If \f3=(\fP\f2value\^\ .\|.\|.\|\fP\f3)\fP is
omitted, then \f2type\^\fP must be an indexed array variable with at
least two elements and the values are taken from this array variable.
If
@ -6412,6 +6415,14 @@ Within arithmetic expressions (see
above), enumeration type values translate to index numbers between 0 and the
number of defined values minus 1. It is an error for an arithmetic expression
to assign a value outside of that range. Decimal fractions are ignored.
Taking the \f3bool\fP type from the example above,
if a variable of this type is used in an arithmetic expression,
\f3false\fP translates to 0 and \f3true\fP to 1.
Enumeration values may also be used directly in an arithmetic expression
that refers to a variable of an enumeration type.
To continue our example, for a \f3bool\fP variable \f3v\fP,
\f3((v==true))\fP is the same as \f3((v==1))\fP
and if a variable named \f3true\fP exists, it is ignored.
.RE
.TP
\(dg \f3eval\fP \*(OK \f2arg\^\fP .\|.\|. \*(CK