Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #15515

Feature request: save/restore BASH_REMATCH across debug hook calls

From Rocky Bernstein <rocky@gnu.org>
Newsgroups gnu.bash.bug
Subject Feature request: save/restore BASH_REMATCH across debug hook calls
Date 2019-10-16 13:34 -0400
Message-ID <mailman.884.1571247268.9715.bug-bash@gnu.org> (permalink)
References <CANCp2gZjUeDUbRDVZzjqMVn1eWoRXRK+kTf_y-UHWTs5R6a79Q@mail.gmail.com>

Show all headers | View raw


Current in bash 5.0 and earlier, the value of BASH_REMATCH might chanted
inside a debug hook.

Since BASH_REMATCH is read-only, resetting the value on hook return to the
debugged program is a bit tricky and fragile...

There are way to change a  bash readonly variable but that involve using
either gdb or having a custom plugin. See
https://stackoverflow.com/questions/17397069/unset-readonly-variable-in-bash

The way that bashdb currently resets BASH_REMATCH is to reissue the command
that caused the value to get initially set. That is fragile since this set
on exit between stepping from the time BASH_REMATCH was set until the time
it is last used. In between variables used in the regular expression may
have changed.

Here is the code bashdb currently uses
https://sourceforge.net/p/bashdb/code/ci/bash-5.0/tree/lib/hook.sh#l105 for
saving the value

   if (( ${#BASH_REMATCH[@]} > 0 )) && [[ "${_Dbg_bash_rematch[@]}" !=
"${BASH_REMATCH[@]}" ]]; then
        # Save a copy of the command string to be able to run to
restore read-only
	# variable BASH_REMATCH
	_Dbg_bash_rematch=${BASH_REMATCH[@]}
        _Dbg_last_rematch_args=( "$@" )
        _Dbg_last_rematch_command=$_Dbg_bash_command
        unset _Dbg_last_rematch_args[0]
    elif ((!${#BASH_REMATCH[@]} && ${#_Dbg_bash_rematch[@]})); then
        _Dbg_bash_rematch=()
        _Dbg_last_rematch_command=''
    fi

Restoring it is just as tricky. As I hope you see all of this is a bit fragile.

Back to gnu.bash.bug | Previous | Next | Find similar | Unroll thread


Thread

Feature request: save/restore BASH_REMATCH across debug hook calls Rocky Bernstein <rocky@gnu.org> - 2019-10-16 13:34 -0400

csiph-web