From 8c8daff1e3661cfe427f194c38c6a1b452cd1456 Mon Sep 17 00:00:00 2001 From: Chet Ramey Date: Mon, 22 Apr 2024 09:19:14 -0400 Subject: [PATCH] minor updates to several tests --- MANIFEST | 1 + examples/functions/setalrm | 66 ++++++++++++++++++++++++++++++++++++++ examples/scripts/bcalc | 2 +- subst.c | 60 +++++++++++++++++----------------- tests/printf.right | 34 ++++++++++---------- tests/printf7.sub | 3 +- tests/unicode1.sub | 21 +++++++++--- 7 files changed, 133 insertions(+), 54 deletions(-) create mode 100644 examples/functions/setalrm diff --git a/MANIFEST b/MANIFEST index 1514c3e92..196ff62f3 100644 --- a/MANIFEST +++ b/MANIFEST @@ -860,6 +860,7 @@ examples/functions/notify.bash f #examples/functions/repeat3 f examples/functions/seq f examples/functions/seq2 f +examples/functions/setalrm f examples/functions/shcat f examples/functions/shcat2 f examples/functions/sort-pos-params f diff --git a/examples/functions/setalrm b/examples/functions/setalrm new file mode 100644 index 000000000..b84beb157 --- /dev/null +++ b/examples/functions/setalrm @@ -0,0 +1,66 @@ +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +# setalrm - set a timer to fire in N seconds and send SIGALRM to the shell +# +# usage: setalrm [-c] N +# +# -c means to reset the trap on ALRM if the timer is canceled +# +# If N = 0, we cancel any pending alarm by killing the background timeout +# process. Any value greater than 0 sets a timeout for N seconds. Values +# of N less than zero are errors. + +declare -i alrmpid= + +setalrm() +{ + local untrap= + local setalrm_usage="setalrm: usage: setalrm [-c] N" + + while getopts c opt; do + case $opt in + c) untrap=1 ;; + *) echo "$setalrm_usage" >&2 ; return 2;; + esac + done + + shift $(( $OPTIND - 1 )) + + if [[ -z $1 ]]; then + echo "$setalrm_usage" >&2 + return 2 + fi + + if (( $1 < 0 )); then + echo "setalrm: timeout must be greater than zero" >& 2 + return 2 + fi + + if [[ $1 -eq 0 ]] && [[ -n "$alrmpid" ]]; then + kill -TERM $alrmpid ; es=$? + alrmpid= + if [ -n "$untrap" ]; then + trap - ALRM # caller saves if desired + fi + return $es + fi + + # setting alarm + { trap - ALRM ; sleep $1; kill -ALRM $$; } & + alrmpid=$! + return 0 +} + + diff --git a/examples/scripts/bcalc b/examples/scripts/bcalc index bc7e2b40e..826eca4f3 100644 --- a/examples/scripts/bcalc +++ b/examples/scripts/bcalc @@ -91,7 +91,7 @@ do esac # save to the history list - history -s "$EQN" + history -s -- "$EQN" # run it through bc calc "$EQN" diff --git a/subst.c b/subst.c index a29eb5beb..73a4eb679 100644 --- a/subst.c +++ b/subst.c @@ -378,8 +378,8 @@ static WORD_LIST *expand_declaration_argument (WORD_LIST *, WORD_LIST *); static WORD_LIST *shell_expand_word_list (WORD_LIST *, int); static WORD_LIST *expand_word_list_internal (WORD_LIST *, int); -static void posix_variable_assignment_error (int); -static void bash_variable_assignment_error (int); +static inline void posix_variable_assignment_error (int); +static inline void bash_variable_assignment_error (int); static int do_assignment_statements (WORD_LIST *, char *, int); @@ -11147,6 +11147,34 @@ expand_array_subscript (const char *string, size_t *sindex, int quoted, int flag } #endif +/* Handle a variable assignment error in default mode. */ +static inline void +bash_variable_assignment_error (int force_exit) +{ + if (interactive_shell == 0 && force_exit) + exp_jump_to_top_level (FORCE_EOF); + else if (interactive_shell == 0) + exp_jump_to_top_level (DISCARD); /* XXX - maybe change later */ + else + exp_jump_to_top_level (DISCARD); +} + +/* Handle a variable assignment error in posix mode. */ +static inline void +posix_variable_assignment_error (int force_exit) +{ +#if defined (STRICT_POSIX) + if (posixly_correct && interactive_shell == 0) +#else + if (posixly_correct && interactive_shell == 0 && executing_command_builtin == 0) +#endif + exp_jump_to_top_level (FORCE_EOF); + else if (force_exit) + exp_jump_to_top_level (FORCE_EOF); + else + exp_jump_to_top_level (DISCARD); +} + void invalidate_cached_quoted_dollar_at (void) { @@ -13083,34 +13111,6 @@ shell_expand_word_list (WORD_LIST *tlist, int eflags) return (new_list); } -/* Handle a variable assignment error in default mode. */ -static inline void -bash_variable_assignment_error (int force_exit) -{ - if (interactive_shell == 0 && force_exit) - exp_jump_to_top_level (FORCE_EOF); - else if (interactive_shell == 0) - exp_jump_to_top_level (DISCARD); /* XXX - maybe change later */ - else - exp_jump_to_top_level (DISCARD); -} - -/* Handle a variable assignment error in posix mode. */ -static inline void -posix_variable_assignment_error (int force_exit) -{ -#if defined (STRICT_POSIX) - if (posixly_correct && interactive_shell == 0) -#else - if (posixly_correct && interactive_shell == 0 && executing_command_builtin == 0) -#endif - exp_jump_to_top_level (FORCE_EOF); - else if (force_exit) - exp_jump_to_top_level (FORCE_EOF); - else - exp_jump_to_top_level (DISCARD); -} - /* Perform assignment statements optionally preceding a command name COMMAND. If COMMAND == NULL, is_nullcmd usually == 1. Follow the POSIX rules for variable assignment errors. */ diff --git a/tests/printf.right b/tests/printf.right index 8af40aa71..7c2e19a56 100644 --- a/tests/printf.right +++ b/tests/printf.right @@ -368,38 +368,38 @@ hello -- 0000000 340 262 207 040 040 040 055 055 055 012 000000a [][] -./printf7.sub: line 19: printf: 21474836470: Result too large -[] ./printf7.sub: line 20: printf: 21474836470: Result too large +[] +./printf7.sub: line 21: printf: 21474836470: Result too large [X] -./printf7.sub: line 22: printf: 21474836470: Result too large +./printf7.sub: line 23: printf: 21474836470: Result too large VAR=[] -./printf7.sub: line 25: printf: 21474836470: Result too large +./printf7.sub: line 26: printf: 21474836470: Result too large VAR=[X] -./printf7.sub: line 31: printf: 9223372036854775825: Result too large -[] ./printf7.sub: line 32: printf: 9223372036854775825: Result too large +[] +./printf7.sub: line 33: printf: 9223372036854775825: Result too large [X] -./printf7.sub: line 34: printf: 9223372036854775825: Result too large +./printf7.sub: line 35: printf: 9223372036854775825: Result too large VAR=[] -./printf7.sub: line 37: printf: 9223372036854775825: Result too large +./printf7.sub: line 38: printf: 9223372036854775825: Result too large VAR=[X] -./printf7.sub: line 43: printf: 21474836470: Result too large -[] ./printf7.sub: line 44: printf: 21474836470: Result too large +[] +./printf7.sub: line 45: printf: 21474836470: Result too large [X] -./printf7.sub: line 46: printf: 21474836470: Result too large +./printf7.sub: line 47: printf: 21474836470: Result too large VAR=[] -./printf7.sub: line 49: printf: 21474836470: Result too large +./printf7.sub: line 50: printf: 21474836470: Result too large VAR=[X] -./printf7.sub: line 55: printf: 9223372036854775825: Result too large -[] ./printf7.sub: line 56: printf: 9223372036854775825: Result too large +[] +./printf7.sub: line 57: printf: 9223372036854775825: Result too large [X] -./printf7.sub: line 58: printf: 9223372036854775825: Result too large +./printf7.sub: line 59: printf: 9223372036854775825: Result too large VAR=[] -./printf7.sub: line 61: printf: 9223372036854775825: Result too large +./printf7.sub: line 62: printf: 9223372036854775825: Result too large VAR=[X] XY -./printf7.sub: line 71: printf: 9223372036854775825: Result too large +./printf7.sub: line 72: printf: 9223372036854775825: Result too large XY diff --git a/tests/printf7.sub b/tests/printf7.sub index 471a6ab04..8ca55171a 100644 --- a/tests/printf7.sub +++ b/tests/printf7.sub @@ -13,7 +13,8 @@ # # tests of integer overflow for field width and precision arguments -INT_MAX=$(getconf INT_MAX) +INT_MAX=$(getconf INT_MAX 2>/dev/null) +[ -z "$INT_MAX" ] && INT_MAX=2147483647 # assume 32 bits TOOBIG=$(( $INT_MAX * 10 )) printf '[%*s]\n' "${TOOBIG}" diff --git a/tests/unicode1.sub b/tests/unicode1.sub index efd704df7..2a70247b6 100644 --- a/tests/unicode1.sub +++ b/tests/unicode1.sub @@ -19,6 +19,12 @@ unset LC_ALL ErrorCnt=0 TestCnt=0 +localewarn() +{ + echo "unicode1.sub: warning: you do not have the $1 locale installed;" >&2 + echo "unicode1.sub: that will cause some of these tests to be skipped." >&2 +} + function TestCodePage { local TargetCharset="${1:?Missing Test charset}" local EChar RChar Uval x @@ -95,8 +101,8 @@ fr_FR_ISO_8859_1=( if locale -a | grep -i '^fr_FR\.ISO8859.*1$' >/dev/null ; then TestCodePage fr_FR.ISO8859-1 fr_FR_ISO_8859_1 else - echo "unicode1.sub: warning: you do not have the fr_FR.ISO8859-1 locale installed;" >&2 - echo "unicode1.sub: that will cause some of these tests to be skipped." >&2 + localewarn fr_FR.ISO8859-1 + fi zh_TW_BIG5=( @@ -110,7 +116,13 @@ zh_TW_BIG5=( # can't use these any more; macos 14 (sonoma) broke libiconv # [0x00fb]='\u00FB' [0x00fc]='\u00FC' [0x00fd]='\u00FD' [0x00fe]='\u00FE' ) -TestCodePage zh_TW.BIG5 zh_TW_BIG5 + +# this locale causes problems all over the place +if locale -a | grep -i '^zh_TW\.big5' >/dev/null ; then + TestCodePage zh_TW.BIG5 zh_TW_BIG5 +else + localewarn zh_TW.BIG5 +fi jp_JP_SHIFT_JIS=( [0x0001]=$'\x01' # START OF HEADING @@ -309,8 +321,7 @@ jp_JP_SHIFT_JIS=( if locale -a | grep -i '^ja_JP.SJIS' >/dev/null ; then TestCodePage ja_JP.SJIS jp_JP_SHIFT_JIS else - echo "unicode1.sub: warning: you do not have the ja_JP.SJIS locale installed;" >&2 - echo "unicode1.sub: that will cause some of these tests to be skipped." >&2 + localewarn ja_JP.SJIS fi #for ((x=1;x<1000;x++)); do printf -v u '\\U%08x' "$x"; printf ' [0x%04x]=%-11q' "$x" "${u@E}"; [ $(($x%5)) = 0 ] && echo; done -- 2.47.2