Here is the code for _setbp:
# Set breakpoint(s) at given line numbers or strings
# by appending patterns to breakpoint variables
function _setbp {
if [[ -z $1 ]]; then
_listbp
elif [[ $1 == +([0-9]) ]]; then # number, set bp at that line
_linebp="${_linebp}$1|"
_msg "Breakpoint at line " $1
else # string, set bp at next line w/string
_stringbp="${_stringbp}$@|"
_msg "Breakpoint at next line containing '$@'."
fi
}
_setbp sets the breakpoints by storing them
in the variables _linebp (line number breakpoints) and
_stringbp (string breakpoints). Both have breakpoints separated by
pipe character delimiters, for reasons that will become clear
shortly. This implies that breakpoints are cumulative; setting new
breakpoints does not erase the old ones.
The only way to remove breakpoints is with the command
#cb,
which (in function _clearbp) clears all
of them at once by simply resetting the two variables to null.
If you don't remember what breakpoints you have set,
the command #bp without arguments lists them.
The functions _at_linenumbp and _at_stringbp are called
by _steptrap after every statement; they check whether the
shell has arrived at a line number or string breakpoint, respectively.
Here is _at_linenumbp:
# See if next line no. is a breakpoint.
function _at_linenumbp {
[[ $_curline == @(${_linebp%\|}) ]]
}
_at_linenumbp takes advantage of the pipe character as the
separator between line numbers: it constructs a regular expression
of the form
@(N1|N2|...)
by taking
the list of line numbers _linebp,
removing the trailing |,
and surrounding it with @( and ). For example, if
$_linebp is 3|15|19|, the resulting expression is
@(3|15|19).
If the current line is any of these numbers, the conditional
becomes true, and _at_linenumbp also returns a "true"
(0) exit status.
The check for a string breakpoint works on the same principle,
but it's slightly more complicated; here is _at_stringbp:
# Search string breakpoints to see if next line in script matches.
function _at_stringbp {
[[ -n $_stringbp && ${_lines[$_curline]} == *@(${_stringbp%\|})* ]]
}
The conditional first checks if $_stringbp is non-null
(meaning that string breakpoints have been defined).
If not, the conditional evaluates to false, but if so, its
value depends on the pattern match after the && -- which
tests the current line to see if it contains any of the
breakpoint strings.
The expression on the right side of the double equal sign is similar
to the one in _at_linenumbp above, except that it has
* before and after it. This gives expressions of the form
*@(S1|S2|...)*, where the Ss
are the string breakpoints. This expression matches any line
that contains any one of the possibilities in the parentheses.
The left side of the double equal sign is the text of the current line
in the guinea pig. So, if this text matches the regular
expression, we've reached a string breakpoint; accordingly,
the conditional expression and _at_stringbp return exit
status 0.