From 74730c8ac77d931f88c394118dddc0055faa2b96 Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Mon, 22 Nov 2021 06:13:25 +0100 Subject: [PATCH] test/[: Improve error status > 1 (re: 7003aba4, cd2cf236, ef1f53b5) As I got to know the code better, it now seems painfully obvious that getting test/[ to issue an exit status >= 2 on error only requires a simple check in sh_exit() in fault.c, which is called whenever the shell issues an error message. --- src/cmd/ksh93/bltins/test.c | 14 -------------- src/cmd/ksh93/include/defs.h | 2 -- src/cmd/ksh93/sh/arith.c | 4 ---- src/cmd/ksh93/sh/fault.c | 6 +++++- src/cmd/ksh93/sh/streval.c | 4 ---- 5 files changed, 5 insertions(+), 25 deletions(-) diff --git a/src/cmd/ksh93/bltins/test.c b/src/cmd/ksh93/bltins/test.c index 146f06c6c..87e4c5338 100644 --- a/src/cmd/ksh93/bltins/test.c +++ b/src/cmd/ksh93/bltins/test.c @@ -83,20 +83,6 @@ static char *nxtarg(struct test*,int); static int expr(struct test*,int); static int e3(struct test*); -/* - * POSIX requires error status > 1 for test builtin. - * Since ksh 'test' can parse arithmetic expressions, the #define - * override is also needed in sh/arith.c and sh/streval.c - */ -int _ERROR_exit_b_test(int exitval) -{ - if((sh.bltindata.bnode==SYSTEST || sh.bltindata.bnode==SYSBRACKET) && exitval < 2) - exitval = 2; - return(ERROR_exit(exitval)); -} -#undef ERROR_exit -#define ERROR_exit(n) _ERROR_exit_b_test(n) - static int test_strmatch(Shell_t *shp,const char *str, const char *pat) { regoff_t match[2*(MATCH_MAX+1)],n; diff --git a/src/cmd/ksh93/include/defs.h b/src/cmd/ksh93/include/defs.h index bef174792..32e09b4e7 100644 --- a/src/cmd/ksh93/include/defs.h +++ b/src/cmd/ksh93/include/defs.h @@ -462,6 +462,4 @@ extern const char e_dict[]; # define sh_stats(x) #endif /* SHOPT_STATS */ -extern int _ERROR_exit_b_test(int); - #endif diff --git a/src/cmd/ksh93/sh/arith.c b/src/cmd/ksh93/sh/arith.c index b9e6d7dbb..3f76b06ed 100644 --- a/src/cmd/ksh93/sh/arith.c +++ b/src/cmd/ksh93/sh/arith.c @@ -32,10 +32,6 @@ #include "variables.h" #include "builtins.h" -/* POSIX requires error status > 1 if called from test builtin */ -#undef ERROR_exit -#define ERROR_exit(n) _ERROR_exit_b_test(n) - #ifndef LLONG_MAX #define LLONG_MAX LONG_MAX #endif diff --git a/src/cmd/ksh93/sh/fault.c b/src/cmd/ksh93/sh/fault.c index 65384677f..972bcfd77 100644 --- a/src/cmd/ksh93/sh/fault.c +++ b/src/cmd/ksh93/sh/fault.c @@ -513,7 +513,11 @@ void sh_exit(register int xno) register struct checkpt *pp = (struct checkpt*)shp->jmplist; register int sig=0; register Sfio_t* pool; - shp->exitval=xno; + /* POSIX requires exit status >= 2 for error in 'test'/'[' */ + if(xno == 1 && (shp->bltindata.bnode==SYSTEST || shp->bltindata.bnode==SYSBRACKET)) + shp->exitval = 2; + else + shp->exitval = xno; if(xno==SH_EXITSIG) shp->exitval |= (sig=shp->lastsig); if(pp && pp->mode>1) diff --git a/src/cmd/ksh93/sh/streval.c b/src/cmd/ksh93/sh/streval.c index 4d8e97876..084771bff 100644 --- a/src/cmd/ksh93/sh/streval.c +++ b/src/cmd/ksh93/sh/streval.c @@ -37,10 +37,6 @@ #include "FEATURE/externs" #include "defs.h" /* for sh.decomma */ -/* POSIX requires error status > 1 if called from test builtin */ -#undef ERROR_exit -#define ERROR_exit(n) _ERROR_exit_b_test(n) - #ifndef ERROR_dictionary # define ERROR_dictionary(s) (s) #endif