1# -*- shell-script -*-
2# condition.sh - gdb-like "condition" debugger command
3#
4#   Copyright (C) 2002-2006, 2008, 2009, 2011, 2016, 2019
5#   Rocky Bernstein rocky@gnu.org
6#
7#   This program is free software; you can redistribute it and/or
8#   modify it under the terms of the GNU General Public License as
9#   published by the Free Software Foundation; either version 2, or
10#   (at your option) any later version.
11#
12#   This program is distributed in the hope that it will be useful,
13#   but WITHOUT ANY WARRANTY; without even the implied warranty of
14#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15#   General Public License for more details.
16#
17#   You should have received a copy of the GNU General Public License
18#   along with this program; see the file COPYING.  If not, write to
19#   the Free Software Foundation, 59 Temple Place, Suite 330, Boston,
20#   MA 02111 USA.
21
22_Dbg_help_add condition \
23"**condition** *bp_number* *bash-cond*
24
25Break only if *bash-cond* is true in breakpoint number *bp_number*.
26
27*bp_number* is an integer and *cond* is an zsh expression to be evaluated whenever
28breakpoint *bp_number* is reached.
29
30Examples:
31---------
32
33   condition 5 x > 10  # Breakpoint 5 now has condition x > 10
34   condition 5         # Remove above condition
35
36See also:
37---------
38
39*break*, *tbreak*.
40" 1 _Dbg_complete_brkpt_range
41
42# Set a condition for a given breakpoint $1 is a breakpoint number
43# $2 is a condition. If not given, set "unconditional" or 1.
44# returns 0 if success or 1 if fail.
45function _Dbg_do_condition {
46
47  if (( $# < 1 )) ; then
48    _Dbg_errmsg 'condition: Argument required (breakpoint number).'
49    return 1
50  fi
51
52  typeset -r n=$1
53  shift
54
55  eval "$_seteglob"
56  if [[ $n != $int_pat ]]; then
57    eval "$_resteglob"
58    _Dbg_errmsg "condition: Bad breakpoint number: $n"
59    return 2
60  fi
61  eval "$_resteglob"
62
63  if [[ -z "${_Dbg_brkpt_file[$n]}" ]] ; then
64    _Dbg_msg "condition: Breakpoint entry $n is not set. Condition not changed."
65    return 3
66  fi
67
68  if [[ -z $condition ]] ; then
69    condition=1
70    _Dbg_msg "Breakpoint $n now unconditional."
71  fi
72  _Dbg_brkpt_cond[$n]="$condition"
73  return 0
74}
75