From 2b52cad3b3f02402994eb84089ba84af421f60a9 Mon Sep 17 00:00:00 2001 From: Joachim Ansorg Date: Tue, 8 Oct 2024 16:28:49 +0200 Subject: [PATCH] Avoid pollution with global variables in info commands --- command/info.sh | 84 +++++++++++++++++++++++++------------------------ 1 file changed, 43 insertions(+), 41 deletions(-) diff --git a/command/info.sh b/command/info.sh index 823d2b9..7315e20 100644 --- a/command/info.sh +++ b/command/info.sh @@ -24,58 +24,60 @@ typeset -A _Dbg_command_help_info _Dbg_help_add info '' 1 _Dbg_complete_info # Load in "info" subcommands -for _Dbg_file in "${_Dbg_libdir}/command/info_sub/"*.sh ; do - source "$_Dbg_file" +for _Dbg_file in "${_Dbg_libdir}/command/info_sub/"*.sh; do + source "$_Dbg_file" done # Command completion _Dbg_complete_info() { - _Dbg_complete_subcmd info + _Dbg_complete_subcmd info } _Dbg_do_info() { + if (($# > 0)); then + typeset _Dbg_subcmd="$1" + shift - if (($# > 0)) ; then - typeset subcmd=$1 - shift + if [[ -n ${_Dbg_debugger_info_commands["$_Dbg_subcmd"]} ]]; then + ${_Dbg_debugger_info_commands["$_Dbg_subcmd"]} "$@" + return $? + else + # Look for a unique abbreviation + typeset -i _Dbg_found=0 + typeset -i _Dbg_count=0 + typeset _Dbg_list + _Dbg_list="${!_Dbg_debugger_info_commands[@]}" + for _Dbg_try in $_Dbg_list; do + if [[ $_Dbg_try =~ ^$_Dbg_subcmd ]]; then + _Dbg_subcmd="$_Dbg_try" + ((_Dbg_count++)) + fi + done + ((_Dbg_found = (_Dbg_count == 1))) + fi + if ((_Dbg_found)); then + ${_Dbg_debugger_info_commands["$_Dbg_subcmd"]} "$@" + return $? + fi - typeset -i found=0 - - if [[ -n ${_Dbg_debugger_info_commands[$subcmd]} ]] ; then - ${_Dbg_debugger_info_commands[$subcmd]} "$@" - return $? - else - # Look for a unique abbreviation - typeset -i count=0 - typeset list; list="${!_Dbg_debugger_info_commands[@]}" - for try in $list ; do - if [[ $try =~ ^$subcmd ]] ; then - subcmd=$try - ((count++)) - fi - done - ((found=(count==1))) - fi - if ((found)); then - ${_Dbg_debugger_info_commands[$subcmd]} "$@" - return $? - fi - - _Dbg_errmsg "Unknown info subcommand: $subcmd" - msg=_Dbg_errmsg + _Dbg_errmsg "Unknown info subcommand: $_Dbg_subcmd" + _Dbg_msg=_Dbg_errmsg else - msg=_Dbg_msg + _Dbg_msg=_Dbg_msg fi - typeset -a list - list=(${!_Dbg_debugger_info_commands[@]}) - sort_list 0 ${#list[@]}-1 - typeset columnized='' - typeset -i width; ((width=_Dbg_set_linewidth-5)) - typeset -a columnized; columnize $width - typeset -i i - $msg "Info subcommands are:" - for ((i=0; i<${#columnized[@]}; i++)) ; do - $msg " ${columnized[i]}" + + typeset -a _Dbg_list + _Dbg_list=(${!_Dbg_debugger_info_commands[@]}) + sort_list 0 ${#_Dbg_list[@]}-1 + typeset _Dbg_columnized='' + typeset -i _Dbg_width + ((_Dbg_width = _Dbg_set_linewidth - 5)) + typeset -a _Dbg_columnized + columnize $_Dbg_width + typeset -i _Dbg_i + $_Dbg_msg "Info subcommands are:" + for ((_Dbg_i = 0; _Dbg_i < ${#_Dbg_columnized[@]}; _Dbg_i++)); do + $_Dbg_msg " ${_Dbg_columnized[_Dbg_i]}" done return 1 }