Skip to content

Commit

Permalink
Fix pollution of global variables
Browse files Browse the repository at this point in the history
  • Loading branch information
jansorg committed Jun 17, 2024
1 parent c9cf5f7 commit 122c3f5
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 61 deletions.
6 changes: 3 additions & 3 deletions bashdb.in
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ typeset _Dbg_libdir="@PKGDATADIR@"
typeset -xa _Dbg_script_args; _Dbg_script_args=("$@")
typeset -i _Dbg_i
for ((_Dbg_i=0; _Dbg_i<${#_Dbg_script_args[@]}-1; _Dbg_i++)) ; do
typeset arg=${_Dbg_script_args[$_Dbg_i]}
if [[ $arg == '-L' || $arg == '--library' ]] ; then
typeset _Dbg_script_arg=${_Dbg_script_args[$_Dbg_i]}
if [[ $_Dbg_script_arg == '-L' || $_Dbg_script_arg == '--library' ]] ; then
((_Dbg_i++))
_Dbg_libdir="${_Dbg_script_args[$_Dbg_i]}"
break
elif [[ $arg == '--' ]] ; then
elif [[ $_Dbg_script_arg == '--' ]] ; then
# We hit the end of bashdb args
break
fi
Expand Down
4 changes: 2 additions & 2 deletions command/help.sh
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ function _Dbg_do_help {
if [[ -n ${_Dbg_command_help[$dbg_cmd]} ]] ; then
_Dbg_msg_rst "${_Dbg_command_help[$dbg_cmd]}"
else
_Dbg_alias_expand $dbg_cmd
dbg_cmd="$expanded_alias"
typeset _Dbg_expanded_alias; _Dbg_alias_expand $dbg_cmd
dbg_cmd="$_Dbg_expanded_alias"
if [[ -n ${_Dbg_command_help[$dbg_cmd]} ]] ; then
_Dbg_msg_rst "${_Dbg_command_help[$dbg_cmd]}"
else
Expand Down
2 changes: 2 additions & 0 deletions command/info.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ _Dbg_do_info() {
typeset subcmd=$1
shift

typeset -i found=0

if [[ -n ${_Dbg_debugger_info_commands[$subcmd]} ]] ; then
${_Dbg_debugger_info_commands[$subcmd]} "$@"
return $?
Expand Down
1 change: 1 addition & 0 deletions command/info_sub/files.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ _Dbg_do_info_files() {
typeset -a list=()
typeset -i i=0
typeset key
typeset file
for key in "${!_Dbg_file2canonic[@]}"; do
list[$i]="$key"
((i++))
Expand Down
10 changes: 5 additions & 5 deletions dbg-main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ if [[ ${_Dbg_libdir:0:1} == '.' ]] ; then
_Dbg_libdir="$(_Dbg_expand_filename "${_Dbg_init_cwd}/${_Dbg_libdir}")"
fi

for source_file in "${_Dbg_o_init_files[@]}" "$DBG_RESTART_FILE"; do
if [[ -n "$source_file" ]] ; then
if [[ -r "$source_file" ]] && [[ -f "$source_file" ]] ; then
source "$source_file"
for _Dbg_source_file in "${_Dbg_o_init_files[@]}" "$DBG_RESTART_FILE"; do
if [[ -n "$_Dbg_source_file" ]] ; then
if [[ -r "$_Dbg_source_file" ]] && [[ -f "$_Dbg_source_file" ]] ; then
source "$_Dbg_source_file"
else
_Dbg_errmsg "Unable to read shell script: ${source_file}"
_Dbg_errmsg "Unable to read shell script: ${_Dbg_source_file}"
fi
fi
done
6 changes: 3 additions & 3 deletions lib/alias.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ _Dbg_alias_remove() {
return 0
}

# Expand alias $1. The result is set in variable expanded_alias which
# Expand alias $1. The result is set in variable _Dbg_expanded_alias which
# could be declared local in the caller.
_Dbg_alias_expand() {
(( $# != 1 )) && return 1
expanded_alias="$1"
_Dbg_expanded_alias="$1"
[[ -z "$1" ]] && return 0
[[ -n ${_Dbg_aliases[$1]} ]] && expanded_alias=${_Dbg_aliases[$1]}
[[ -n ${_Dbg_aliases[$1]} ]] && _Dbg_expanded_alias=${_Dbg_aliases[$1]}
return 0
}

Expand Down
4 changes: 2 additions & 2 deletions lib/complete.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ typeset -a _Dbg_matches; _Dbg_matches=()
# We get the list of completions from _Dbg._*subcmd*_cmds.
# If no completion, we return the empty list.
_Dbg_subcmd_complete() {
subcmd=$1
text=$2
typeset subcmd=$1
typeset text=$2
_Dbg_matches=()
typeset list=''
if [[ $subcmd == 'set' ]] ; then
Expand Down
3 changes: 2 additions & 1 deletion lib/help.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ function _Dbg_help_add {

# Add help text $3 for in subcommand $1 under key $2
function _Dbg_help_add_sub {
add_command=${4:-1}
typeset add_command; add_command=${4:-1}
(($# != 3)) && (($# != 4)) && return 1
typeset -i add_command; add_command=${4:-1}
eval "_Dbg_command_help_$1[$2]=\"$3\""
if (( add_command )) ; then
eval "_Dbg_debugger_${1}_commands[$2]=\"_Dbg_do_${1}_${2}\""
Expand Down
1 change: 1 addition & 0 deletions lib/info.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ _Dbg_info_help() {
typeset -a list
_Dbg_section 'List of info subcommands:'

typeset thing
for thing in $_Dbg_info_cmds ; do
_Dbg_info_help $thing 1
done
Expand Down
74 changes: 37 additions & 37 deletions lib/processor.sh
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ function _Dbg_process_commands {
_Dbg_continue_rc=-1 # Don't continue exectuion unless told to do so.
_Dbg_write_journal "_Dbg_step_ignore=$_Dbg_step_ignore"

typeset key
typeset _Dbg_key

# Evaluate all hooks
for key in ${!_Dbg_cmdloop_hooks[@]} ; do
${_Dbg_cmdloop_hooks[$key]}
for _Dbg_key in ${!_Dbg_cmdloop_hooks[@]} ; do
${_Dbg_cmdloop_hooks[$_Dbg_key]}
done

# Loop over all pending open input file descriptors
Expand Down Expand Up @@ -146,15 +146,15 @@ function _Dbg_process_commands {
_Dbg_preloop

typeset _Dbg_cmd
typeset args
typeset rc
typeset _Dbg_args
typeset _Dbg_rc

while : ; do
set -o history
_Dbg_input_desc=${_Dbg_fd[_Dbg_fd_last]}
if [[ $_Dbg_tty == '&1' ]] ; then
echo -n "$_Dbg_prompt"
if ! read _Dbg_cmd args <&$_Dbg_input_desc 2>&1; then
if ! read _Dbg_cmd _Dbg_args <&$_Dbg_input_desc 2>&1; then
break
fi
else
Expand All @@ -163,7 +163,7 @@ function _Dbg_process_commands {
else
_Dbg_read_fn='read'
fi
if ! $_Dbg_read_fn $_Dbg_edit -p "$_Dbg_prompt" _Dbg_cmd args \
if ! $_Dbg_read_fn $_Dbg_edit -p "$_Dbg_prompt" _Dbg_cmd _Dbg_args \
<&$_Dbg_input_desc 2>>$_Dbg_prompt_output ; then
set +o history
break
Expand All @@ -172,11 +172,11 @@ function _Dbg_process_commands {

# FIXME: until I figure out to fix builtin readc, this happens
# on command completion:
if [[ $_Dbg_cmd =~ ' ' && -z $args ]] ; then
typeset -a ary; IFS=' ' ary=( $_Dbg_cmd )
_Dbg_cmd=${ary[0]}
unset ary[0]
args="${ary[@]}"
if [[ $_Dbg_cmd =~ ' ' && -z $_Dbg_args ]] ; then
typeset -a _Dbg_ary; IFS=' ' _Dbg_ary=( $_Dbg_cmd )
_Dbg_cmd=${_Dbg_ary[0]}
unset _Dbg_ary[0]
_Dbg_args="${_Dbg_ary[@]}"
fi
set +o history
if (( _Dbg_brkpt_commands_defining )) ; then
Expand All @@ -200,14 +200,14 @@ function _Dbg_process_commands {
continue
;;
*)
_Dbg_brkpt_commands[${#_Dbg_brkpt_commands[@]}]="$_Dbg_cmd $args"
_Dbg_brkpt_commands[${#_Dbg_brkpt_commands[@]}]="$_Dbg_cmd $_Dbg_args"
(( _Dbg_brkpt_commands_end[$_Dbg_brkpt_commands_current]++ ))
continue
;;
esac
rc=$?
_Dbg_rc=$?
else
_Dbg_onecmd "$_Dbg_cmd" "$args"
_Dbg_onecmd "$_Dbg_cmd" "$_Dbg_args"
_Dbg_postcmd
fi
((_Dbg_continue_rc >= 0)) && return $_Dbg_continue_rc
Expand Down Expand Up @@ -238,40 +238,40 @@ _Dbg_annotation() {
}

# Run a single command
# Parameters: _Dbg_cmd and args
# Parameters: _Dbg_cmd and _Dbg_args
#
_Dbg_onecmd() {
typeset full_cmd=$@
typeset _Dbg_full_cmd=$@
typeset _Dbg_orig_cmd="$1"
typeset expanded_alias; _Dbg_alias_expand "$_Dbg_orig_cmd"
typeset _Dbg_cmd="$expanded_alias"
typeset _Dbg_expanded_alias; _Dbg_alias_expand "$_Dbg_orig_cmd"
typeset _Dbg_cmd="$_Dbg_expanded_alias"
shift
typeset _Dbg_args="$@"

# Set default next, step or skip command
if [[ -z $_Dbg_cmd ]]; then
_Dbg_cmd=$_Dbg_last_next_step_cmd
_Dbg_args=$_Dbg_last_next_step_args
full_cmd="$_Dbg_cmd $_Dbg_args"
_Dbg_full_cmd="$_Dbg_cmd $_Dbg_args"
fi

# If "set trace-commands" is "on", echo the the command
if [[ $_Dbg_set_trace_commands == 'on' ]] ; then
_Dbg_msg "+$full_cmd"
_Dbg_msg "+$_Dbg_full_cmd"
fi

local dq_cmd=$(_Dbg_esc_dq "$_Dbg_cmd")
local dq_args=$(_Dbg_esc_dq "$_Dbg_args")
local _Dbg_dq_cmd=$(_Dbg_esc_dq "$_Dbg_cmd")
local _Dbg_dq_args=$(_Dbg_esc_dq "$_Dbg_args")

# _Dbg_write_journal_eval doesn't work here. Don't really understand
# how to get it to work. So we do this in two steps.
_Dbg_write_journal \
"_Dbg_history[${#_Dbg_history[@]}]=\"$dq_cmd $dq_args\""
"_Dbg_history[${#_Dbg_history[@]}]=\"$_Dbg_dq_cmd $_Dbg_dq_args\""

_Dbg_history[${#_Dbg_history[@]}]="$_Dbg_cmd $_Dbg_args"

_Dbg_hi=${#_Dbg_history[@]}
history -s -- "$full_cmd"
history -s -- "$_Dbg_full_cmd"

typeset -i _Dbg_redo=1
while (( _Dbg_redo )) ; do
Expand All @@ -280,25 +280,25 @@ _Dbg_onecmd() {

[[ -z $_Dbg_cmd ]] && _Dbg_cmd=$_Dbg_last_cmd
if [[ -n $_Dbg_cmd ]] ; then
typeset -i found=0
typeset found_cmd
typeset -i _Dbg_found=0
typeset _Dbg_found_cmd
if [[ -n ${_Dbg_debugger_commands[$_Dbg_cmd]} ]] ; then
found=1
found_cmd=$_Dbg_cmd
_Dbg_found=1
_Dbg_found_cmd=$_Dbg_cmd
else
# Look for a unique abbreviation
typeset -i count=0
typeset list; list="${!_Dbg_debugger_commands[@]}"
for try in $list ; do
typeset -i _Dbg_count=0
typeset _Dbg_list; _Dbg_list="${!_Dbg_debugger_commands[@]}"
for try in $_Dbg_list ; do
if [[ $try =~ ^$_Dbg_cmd ]] ; then
found_cmd=$try
((count++))
_Dbg_found_cmd=$try
((_Dbg_count++))
fi
done
((found=(count==1)))
((_Dbg_found=(_Dbg_count==1)))
fi
if ((found)); then
${_Dbg_debugger_commands[$found_cmd]} $_Dbg_args
if ((_Dbg_found)); then
${_Dbg_debugger_commands[$_Dbg_found_cmd]} $_Dbg_args
IFS=$_Dbg_space_IFS;
eval "_Dbg_prompt=$_Dbg_prompt_str"
((_Dbg_continue_rc >= 0)) && return $_Dbg_continue_rc
Expand Down
1 change: 1 addition & 0 deletions lib/sig.sh
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ _Dbg_save_handler() {
_Dbg_subst_handler_var() {
typeset -i i
typeset result=''
typeset arg
for arg in $* ; do
case $arg in
'$LINENO' )
Expand Down
16 changes: 8 additions & 8 deletions test/unit/test-alias.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
test_alias()
{
_Dbg_alias_add u up
typeset expanded_alias=''; _Dbg_alias_expand u
assertEquals 'up' $expanded_alias
typeset _Dbg_expanded_alias=''; _Dbg_alias_expand u
assertEquals 'up' $_Dbg_expanded_alias

_Dbg_alias_add q quit
expanded_alias=''; _Dbg_alias_expand q
assertEquals 'quit' $expanded_alias
_Dbg_expanded_alias=''; _Dbg_alias_expand q
assertEquals 'quit' $_Dbg_expanded_alias

typeset aliases_found=''
_Dbg_alias_find_aliased quit
Expand All @@ -19,14 +19,14 @@ test_alias()
assertEquals 'exit, q' "$aliases_found"

_Dbg_alias_remove q
expanded_alias=''; _Dbg_alias_expand q
assertEquals 'q' $expanded_alias
_Dbg_expanded_alias=''; _Dbg_alias_expand q
assertEquals 'q' $_Dbg_expanded_alias

_Dbg_alias_find_aliased quit
assertEquals 'exit' "$aliases_found"

expanded_alias=''; _Dbg_alias_expand u
assertEquals 'up' $expanded_alias
_Dbg_expanded_alias=''; _Dbg_alias_expand u
assertEquals 'up' $_Dbg_expanded_alias
}

if [ '@abs_top_srcdir@' = '' ] ; then
Expand Down

0 comments on commit 122c3f5

Please sign in to comment.