From 29febcdc125353b32999fb64eb71c05a10a4bcbf Mon Sep 17 00:00:00 2001 From: ReDTerraN Date: Fri, 14 Apr 2023 21:04:53 +0200 Subject: [PATCH 1/5] Save current setup --- bin/xpanes | 132 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 84 insertions(+), 48 deletions(-) diff --git a/bin/xpanes b/bin/xpanes index e08ca4f..63457c9 100755 --- a/bin/xpanes +++ b/bin/xpanes @@ -1840,44 +1840,82 @@ xpns_warning_before_extra() { fi } -xpns_opt_check_num() { - local _option="$1" - shift - local _arg="$1" - if [[ -n "$_arg" ]] && [[ -z "${_arg//[0-9]/}" ]]; then - return 0 - fi - xpns_msg_error "Invalid argument '$_arg' for $_option option" - exit ${XP_EINVAL} -} - -xpns_opt_check_float() { - local _option="$1" - shift - local _arg="$1" - if [[ -n "$_arg" ]] && [[ -z "${_arg//[0-9.]/}" ]]; then - return 0 - fi - xpns_msg_error "Invalid argument '$_arg' for $_option option" - exit ${XP_EINVAL} -} - -xpns_opt_check_str() { +#xpns_opt_check_number() { +# local _option="$1" +# shift +# local _arg="$1" +# if [[ -n "$_arg" ]] && [[ -z "${_arg//[0-9]/}" ]]; then +# return 0 +# fi +# xpns_msg_error "Invalid argument '$_arg' for $_option option" +# exit ${XP_EINVAL} +#} + +#xpns_opt_check_float() { +# local _option="$1" +# shift +# local _arg="$1" +# if [[ -n "$_arg" ]] && [[ -z "${_arg//[0-9.]/}" ]]; then +# return 0 +# fi +# xpns_msg_error "Invalid argument '$_arg' for $_option option" +# exit ${XP_EINVAL} +#} +#xpns_opt_check_number() { +# local _option="$1" +# shift +# local _arg="$1" +# local _check_float="$2" +# local _pattern="[0-9]" +# if [[ "$_check_float" == "float_true" ]]; then +# _pattern="[0-9.]" +# fi +# if [[ -n "$_arg" ]] && [[ -z "${_arg//$_pattern/}" ]]; then +# return 0 +# fi +# xpns_msg_error "Invalid argument '$_arg' for $_option option" +# exit ${XP_EINVAL} +#} +# +#xpns_opt_check_str() { +# local _option="$1" +# shift +# local _arg="$1" +# if [[ -n "$_arg" ]]; then +# return 0 +# fi +# xpns_msg_error "Invalid argument '$_arg' for $_option option" +# exit ${XP_EINVAL} +#} +# +#xpns_opt_check_num_csv() { +# local _option="$1" +# shift +# local _arg="$1" +# if [[ "$1" =~ ^([0-9][0-9]*,?)+$ ]]; then +# return 0 +# fi +# xpns_msg_error "Invalid argument '$_arg' for $_option option" +# exit ${XP_EINVAL} +#} + +xpns_opt_checker() { local _option="$1" shift local _arg="$1" - if [[ -n "$_arg" ]]; then - return 0 + local _check_float="${2:-default}" + #if we do not define a positional parameter, we expect an integer + local _pattern="[0-9]" + if [[ "$_check_float" == "float" ]]; then + _pattern="[0-9.]" + elif [[ "$_check_float" == "csv" ]]; then + _pattern="^[0-9]+(,[0-9]+)*$" + elif [[ "$_check_float" == "string" ]]; then + if [[ -n "$_arg" ]]; then + return 0 + fi fi - xpns_msg_error "Invalid argument '$_arg' for $_option option" - exit ${XP_EINVAL} -} - -xpns_opt_check_num_csv() { - local _option="$1" - shift - local _arg="$1" - if [[ "$1" =~ ^([0-9][0-9]*,?)+$ ]]; then + if [[ -n "$_arg" ]] && [[ "$_arg" =~ $_pattern ]]; then return 0 fi xpns_msg_error "Invalid argument '$_arg' for $_option option" @@ -1932,7 +1970,7 @@ xpns_parse_options() { XP_OPT_IS_SYNC=0 ;; --log-format) - xpns_opt_check_str "$opt" "$1" + xpns_opt_checker "$opt" "$1" "string" XP_OPTIONS+=("$opt") XP_OPT_LOG_STORE=1 TMUX_XPANES_LOG_FORMAT="$1" @@ -1943,7 +1981,7 @@ xpns_parse_options() { XP_OPT_LOG_STORE=1 XP_OPTIONS+=("$opt") TMUX_XPANES_LOG_DIRECTORY="${opt#--log=}" - xpns_opt_check_str "${opt%=}" "$TMUX_XPANES_LOG_DIRECTORY" + xpns_opt_checker "${opt%=}" "$TMUX_XPANES_LOG_DIRECTORY" "string" ;; --log) XP_OPT_LOG_STORE=1 @@ -1964,28 +2002,28 @@ xpns_parse_options() { XP_OPT_ATTACH=0 ;; --cols) - xpns_opt_check_num "$opt" "$1" + xpns_opt_checker "$opt" "$1" XP_OPTIONS+=("$opt") XP_OPT_CUSTOM_SIZE_COLS="$1" XP_OPTIONS+=("$1") shift ;; --rows) - xpns_opt_check_num "$opt" "$1" + xpns_opt_checker "$opt" "$1" XP_OPTIONS+=("$opt") XP_OPT_CUSTOM_SIZE_ROWS="$1" XP_OPTIONS+=("$1") shift ;; --bulk-cols) - xpns_opt_check_num_csv "$opt" "$1" + xpns_opt_checker "$opt" "$1" "csv" XP_OPTIONS+=("$opt") XP_OPT_BULK_COLS="$1" XP_OPTIONS+=("$1") shift ;; --interval) - xpns_opt_check_float "$opt" "$1" + xpns_opt_checker "$opt" "$1" "float" XP_OPTIONS+=("$opt") XP_OPT_INTERVAL="$1" XP_OPTIONS+=("$1") @@ -2055,14 +2093,14 @@ xpns_parse_options() { # Short options with argument ## ---------------- -I) - xpns_opt_check_str "$opt" "$1" + xpns_opt_checker "$opt" "$1" "string" XP_OPTIONS+=("$opt") XP_REPSTR="$1" XP_OPTIONS+=("$1") shift ;; -l) - xpns_opt_check_str "$opt" "$1" + xpns_opt_checker "$opt" "$1" "string" XP_OPTIONS+=("$opt") XP_OPT_USE_PRESET_LAYOUT=1 XP_LAYOUT="$(printf '%s\n' "$1" | xpns_layout_short2long)" @@ -2072,7 +2110,6 @@ xpns_parse_options() { ;; -c) # -c allows empty - # xpns_opt_check_str "$opt" "$1" XP_OPTIONS+=("$opt") XP_CMD_UTILITY="$1" XP_OPT_CMD_UTILITY=1 @@ -2080,28 +2117,28 @@ xpns_parse_options() { shift ;; -n) - xpns_opt_check_num "$opt" "$1" + xpns_opt_checker "$opt" "$1" XP_OPTIONS+=("$opt") XP_MAX_PANE_ARGS="$1" XP_OPTIONS+=("$1") shift ;; -S) - xpns_opt_check_str "$opt" "$1" + xpns_opt_checker "$opt" "$1" "string" XP_OPTIONS+=("$opt") XP_SOCKET_PATH="$1" XP_OPTIONS+=("$1") shift ;; -C) - xpns_opt_check_num "$opt" "$1" + xpns_opt_checker "$opt" "$1" XP_OPTIONS+=("$opt") XP_OPT_CUSTOM_SIZE_COLS="$1" XP_OPTIONS+=("$1") shift ;; -R) - xpns_opt_check_num "$opt" "$1" + xpns_opt_checker "$opt" "$1" XP_OPTIONS+=("$opt") XP_OPT_CUSTOM_SIZE_ROWS="$1" XP_OPTIONS+=("$1") @@ -2109,7 +2146,6 @@ xpns_parse_options() { ;; -B) # -B allows empty - # xpns_opt_check_str "$opt" "$1" XP_OPTIONS+=("$opt") XP_BEGIN_ARGS+=("$1") XP_OPTIONS+=("$1") From e73fc64bf852cbc492cc863a1dc9b7e996b35a00 Mon Sep 17 00:00:00 2001 From: ReDTerraN Date: Fri, 14 Apr 2023 21:21:02 +0200 Subject: [PATCH 2/5] adjust positional params and variables --- bin/xpanes | 72 +++++------------------------------------------------- 1 file changed, 6 insertions(+), 66 deletions(-) diff --git a/bin/xpanes b/bin/xpanes index 63457c9..d97505f 100755 --- a/bin/xpanes +++ b/bin/xpanes @@ -1840,77 +1840,17 @@ xpns_warning_before_extra() { fi } -#xpns_opt_check_number() { -# local _option="$1" -# shift -# local _arg="$1" -# if [[ -n "$_arg" ]] && [[ -z "${_arg//[0-9]/}" ]]; then -# return 0 -# fi -# xpns_msg_error "Invalid argument '$_arg' for $_option option" -# exit ${XP_EINVAL} -#} - -#xpns_opt_check_float() { -# local _option="$1" -# shift -# local _arg="$1" -# if [[ -n "$_arg" ]] && [[ -z "${_arg//[0-9.]/}" ]]; then -# return 0 -# fi -# xpns_msg_error "Invalid argument '$_arg' for $_option option" -# exit ${XP_EINVAL} -#} -#xpns_opt_check_number() { -# local _option="$1" -# shift -# local _arg="$1" -# local _check_float="$2" -# local _pattern="[0-9]" -# if [[ "$_check_float" == "float_true" ]]; then -# _pattern="[0-9.]" -# fi -# if [[ -n "$_arg" ]] && [[ -z "${_arg//$_pattern/}" ]]; then -# return 0 -# fi -# xpns_msg_error "Invalid argument '$_arg' for $_option option" -# exit ${XP_EINVAL} -#} -# -#xpns_opt_check_str() { -# local _option="$1" -# shift -# local _arg="$1" -# if [[ -n "$_arg" ]]; then -# return 0 -# fi -# xpns_msg_error "Invalid argument '$_arg' for $_option option" -# exit ${XP_EINVAL} -#} -# -#xpns_opt_check_num_csv() { -# local _option="$1" -# shift -# local _arg="$1" -# if [[ "$1" =~ ^([0-9][0-9]*,?)+$ ]]; then -# return 0 -# fi -# xpns_msg_error "Invalid argument '$_arg' for $_option option" -# exit ${XP_EINVAL} -#} - xpns_opt_checker() { local _option="$1" - shift - local _arg="$1" - local _check_float="${2:-default}" - #if we do not define a positional parameter, we expect an integer + local _arg="$2" + local _check_value="${3:-default}" + # if we do not define a positional parameter in options, we expect an integer local _pattern="[0-9]" - if [[ "$_check_float" == "float" ]]; then + if [[ "$_check_value" == "float" ]]; then _pattern="[0-9.]" - elif [[ "$_check_float" == "csv" ]]; then + elif [[ "$_check_value" == "csv" ]]; then _pattern="^[0-9]+(,[0-9]+)*$" - elif [[ "$_check_float" == "string" ]]; then + elif [[ "$_check_value" == "string" ]]; then if [[ -n "$_arg" ]]; then return 0 fi From 4df0cc3263ce24bbc8cb20c0c669264a0ccf5133 Mon Sep 17 00:00:00 2001 From: ReDTerraN Date: Fri, 14 Apr 2023 22:21:33 +0200 Subject: [PATCH 3/5] bulletproof the float regex --- bin/xpanes | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/xpanes b/bin/xpanes index d97505f..18718a4 100755 --- a/bin/xpanes +++ b/bin/xpanes @@ -1847,7 +1847,7 @@ xpns_opt_checker() { # if we do not define a positional parameter in options, we expect an integer local _pattern="[0-9]" if [[ "$_check_value" == "float" ]]; then - _pattern="[0-9.]" + _pattern="^[0-9]*\.?[0-9]*$" elif [[ "$_check_value" == "csv" ]]; then _pattern="^[0-9]+(,[0-9]+)*$" elif [[ "$_check_value" == "string" ]]; then From eab98518aaae24c1aa72470aa614cc0d80658986 Mon Sep 17 00:00:00 2001 From: Yasuhiro Yamada Date: Sat, 15 Apr 2023 13:16:11 +0000 Subject: [PATCH 4/5] Add unit tests for xpns_opt_checker --- test/unit.sh | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/unit.sh b/test/unit.sh index f9cda1c..7656dba 100755 --- a/test/unit.sh +++ b/test/unit.sh @@ -512,5 +512,22 @@ test_xpns_parse_options_interval () { assertEquals "4" "$?" } +test_xpns_opt_checker () { + ( xpns_opt_checker --bulk-cols 1,2,3,4 csv ) + assertEquals "0" "$?" + ( xpns_opt_checker -n 1 ) + assertEquals "0" "$?" + ( xpns_opt_checker -S ~/dummy string ) + assertEquals "0" "$?" + ( xpns_opt_checker --interval 1.1 float ) + assertEquals "0" "$?" + ( xpns_opt_checker --bulk-cols "invalid" csv ) + assertEquals "4" "$?" + ( xpns_opt_checker -n "invalid" ) + assertEquals "4" "$?" + ( xpns_opt_checker --interval "invalid" float ) + assertEquals "4" "$?" +} + # shellcheck source=/dev/null . "${THIS_DIR}/shunit2/shunit2" From 0b220f33aec0526bde992864364f424ddae3f47a Mon Sep 17 00:00:00 2001 From: ReDTerraN Date: Sat, 15 Apr 2023 20:35:12 +0200 Subject: [PATCH 5/5] Fixed int regex and applied additional unit tests --- bin/xpanes | 2 +- test/unit.sh | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/bin/xpanes b/bin/xpanes index 18718a4..592c346 100755 --- a/bin/xpanes +++ b/bin/xpanes @@ -1845,7 +1845,7 @@ xpns_opt_checker() { local _arg="$2" local _check_value="${3:-default}" # if we do not define a positional parameter in options, we expect an integer - local _pattern="[0-9]" + local _pattern="^[0-9]+$" if [[ "$_check_value" == "float" ]]; then _pattern="^[0-9]*\.?[0-9]*$" elif [[ "$_check_value" == "csv" ]]; then diff --git a/test/unit.sh b/test/unit.sh index 7656dba..1b891b0 100755 --- a/test/unit.sh +++ b/test/unit.sh @@ -523,10 +523,16 @@ test_xpns_opt_checker () { assertEquals "0" "$?" ( xpns_opt_checker --bulk-cols "invalid" csv ) assertEquals "4" "$?" + ( xpns_opt_checker --bulk-cols "1,2,a" csv ) + assertEquals "4" "$?" ( xpns_opt_checker -n "invalid" ) assertEquals "4" "$?" + ( xpns_opt_checker -n "1.1.1.1" ) + assertEquals "4" "$?" ( xpns_opt_checker --interval "invalid" float ) assertEquals "4" "$?" + ( xpns_opt_checker --interval "1.1.1.1" float ) + assertEquals "4" "$?" } # shellcheck source=/dev/null