1
0
Fork 0
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:
Martijn Dekker 2021-11-13 23:57:15 +01:00
parent 6f5c9fea93
commit c81473061a
9 changed files with 58 additions and 48 deletions

View file

@ -31,12 +31,13 @@
#include "defs.h"
#include "shtable.h"
/*
* These are the valid test operators
* These are the binary test operators
* See also shtab_testops[] in data/testops.c
*/
#define TEST_ARITH 040 /* arithmetic operators */
#define TEST_BINOP 0200 /* binary operator */
#define TEST_PATTERN 0100 /* turn off bit for pattern compares */
#define TEST_ANDOR 0200 /* logical operators: -a, -o */
#define TEST_STRCMP 0100 /* literal string comparison; turn off bit for pattern matching */
#define TEST_NE (TEST_ARITH|9)
#define TEST_EQ (TEST_ARITH|4)
@ -44,10 +45,10 @@
#define TEST_GT (TEST_ARITH|6)
#define TEST_LE (TEST_ARITH|7)
#define TEST_LT (TEST_ARITH|8)
#define TEST_OR (TEST_BINOP|1)
#define TEST_AND (TEST_BINOP|2)
#define TEST_SNE (TEST_PATTERN|1)
#define TEST_SEQ (TEST_PATTERN|14)
#define TEST_OR (TEST_ANDOR|1)
#define TEST_AND (TEST_ANDOR|2)
#define TEST_SNE (TEST_STRCMP|TEST_PNE)
#define TEST_SEQ (TEST_STRCMP|TEST_PEQ)
#define TEST_PNE 1
#define TEST_PEQ 14
#define TEST_EF 3
@ -55,7 +56,6 @@
#define TEST_OT 12
#define TEST_SLT 16
#define TEST_SGT 17
#define TEST_END 8
#define TEST_REP 20
extern int test_unop(Shell_t*,int, const char*);
@ -67,7 +67,6 @@ extern const char test_opchars[];
extern const char e_argument[];
extern const char e_missing[];
extern const char e_badop[];
extern const char e_unsupported_op[];
extern const char e_tstbegin[];
extern const char e_tstend[];