mirror of
git://git.code.sf.net/p/cdesktopenv/code
synced 2025-03-09 15:50:02 +00:00
test/[: binary operators: fix '<' and add '=~'; some more cleanups
In ksh88, the test/[ built-in supported both the '<' and '>'
lexical sorting comparison operators, same as in [[. However, in
every version of ksh93, '<' does not work though '>' still does!
Still, the code for both is present in test_binop():
src/cmd/ksh93/bltins/test.c
548: case TEST_SGT:
549: return(strcoll(left, right)>0);
550: case TEST_SLT:
551: return(strcoll(left, right)<0);
Analysis: The binary operators are looked up in shtab_testops[] in
data/testops.c using a macro called sh_lookup, which expands to a
sh_locate() call. If we examine that function in sh/string.c, it's
easy to see that on systems using ASCII (i.e. all except IBM
mainframes), it assumes the table is sorted in ASCII order.
src/cmd/ksh93/sh/string.c
64: while((c= *tp->sh_name) && (CC_NATIVE!=CC_ASCII || c <= first))
The problem was that the '<' operator was not correctly sorted in
shtab_testops[]; it was sorted immediately before '>', but after
'='. The ASCII order is: < (60), = (61), > (62). This caused '<' to
never be found in the table.
The test_binop() function is also used by [[, yet '<' always worked
in that. This is because the parser has code that directly checks
for '<' and '>' within [[ (in sh/parse.c, lines 1949-1952).
This commit also adds '=~' to 'test', which took three lines of
code and allowed eliminating error handling in test_binop() as
test/[ and [[ now support the same binary ops. (re: fc2d5a60)
src/cmd/ksh93/*/*.[ch]:
- Rename a couple of very misleadingly named macros in test.h:
. For == and !=, the TEST_PATTERN bit is off for pattern compares
and on for literal string compares! Rename to TEST_STRCMP.
. The TEST_BINOP bit does not denote all binary operators, but
only the logical -a/-o ops in test/[. Rename to TEST_ANDOR.
src/cmd/ksh93/bltins/test.c: test_binop():
- Add support for =~. This is only used by test/[. The method is
implemented in two lines that convert the ERE to a shell pattern
by prefixing it with ~(E), then call test_strmatch with that
temporary string to match the ERE and update ${.sh.match}.
- Since all binary ops from shtab_testops[] are now accounted for,
remove unknown op error handling from this function.
src/cmd/ksh93/data/testops.c:
- shtab_testops[]:
. Correctly sort the '<' (TEST_SLT) entry.
. Remove ']]' (TEST_END). It's not an op and doesn't belong here.
- Update sh_opttest[] documentation with =~, \<, \>.
- Remove now-unused e_unsupported_op[] error message.
src/cmd/ksh93/sh/lex.c: sh_lex():
- Check for ']]' directly instead of relying on the removed
TEST_END entry from shtab_testops[].
src/cmd/ksh93/tests/bracket.sh:
- Add relevant tests.
src/cmd/ksh93/tests/builtins.sh:
- Fix an old test that globally deleted the 'test' builtin. Delete
it within the command substitution subshell only.
- Remove the test for non-support of =~ in test/[.
- Update the test for invalid test/[ op to use test directly.
This commit is contained in:
parent
6f5c9fea93
commit
c81473061a
9 changed files with 58 additions and 48 deletions
|
|
@ -31,6 +31,7 @@
|
|||
|
||||
/*
|
||||
* This is the list of binary test and [[ ... ]] operators
|
||||
* It must be sorted in ascending ASCII order
|
||||
*/
|
||||
|
||||
const Shtable_t shtab_testops[] =
|
||||
|
|
@ -47,12 +48,11 @@ const Shtable_t shtab_testops[] =
|
|||
"-nt", TEST_NT,
|
||||
"-o", TEST_OR,
|
||||
"-ot", TEST_OT,
|
||||
"<", TEST_SLT,
|
||||
"=", TEST_SEQ,
|
||||
"==", TEST_SEQ,
|
||||
"=~", TEST_REP,
|
||||
"<", TEST_SLT,
|
||||
">", TEST_SGT,
|
||||
"]]", TEST_END,
|
||||
"", 0
|
||||
};
|
||||
|
||||
|
|
@ -134,6 +134,9 @@ const char sh_opttest[] =
|
|||
"[+\astring1\a = \astring2\a?\astring1\a is equal to \astring2\a.]"
|
||||
"[+\astring1\a == \astring2\a *?Same as \b=\b.]"
|
||||
"[+\astring1\a != \astring2\a?\astring1\a is not equal to \astring2\a.]"
|
||||
"[+\astring1\a =~ \aere\a *?\astring1\a matches the extended regular expression \aere\a.]"
|
||||
"[+\astring1\a \\< \astring2\a *?\astring1\a lexically sorts before \astring2\a.]"
|
||||
"[+\astring1\a \\> \astring2\a *?\astring1\a lexically sorts after \astring2\a.]"
|
||||
"[+\anum1\a -eq \anum2\a?\anum1\a is numerically equal to \anum2\a.]"
|
||||
"[+\anum1\a -ne \anum2\a?\anum1\a is not numerically equal to \anum2\a.]"
|
||||
"[+\anum1\a -lt \anum2\a?\anum1\a is less than \anum2\a.]"
|
||||
|
|
@ -167,6 +170,5 @@ const char test_opchars[] = "HLNRSVOGCaeohrwxdcbfugkv"
|
|||
const char e_argument[] = "argument expected";
|
||||
const char e_missing[] = "%s missing";
|
||||
const char e_badop[] = "%s: unknown operator";
|
||||
const char e_unsupported_op[] = "%s: operator not supported; use [[ ... ]]";
|
||||
const char e_tstbegin[] = "[[ ! ";
|
||||
const char e_tstend[] = " ]]\n";
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue