1# -*- shell-script -*-
2# Things related to variable journaling.
3#
4#   Copyright (C) 2002, 2003, 2004, 2006, 2008, 2009,
5#   2010, 2011 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# We use a journal file to save variable state so that we can pass
23# values set in a subshell or nested shell back. This typically
24# includes debugger information, e.g. breakpoints and state. This file
25# is just code (usually assignment statements) that get eval'd.
26
27# The file to save the journal information.
28typeset _Dbg_journal=$(_Dbg_tempname journal)
29
30# append a command into journal file and then run the command.
31_Dbg_write_journal_eval() {
32    _Dbg_write_journal "$@"
33    eval "$@"
34}
35
36# append a command into journal file and then run the command.
37_Dbg_write_journal_var() {
38    typeset var_name="$1"
39    typeset val
40    typeset val_cmd="val='\${$var_name}'"
41    eval "$val_cmd"
42    _Dbg_write_journal "${var_name}='${val}'"
43}
44
45_Dbg_write_journal_avar() {
46    typeset decl_str; decl_str=$(declare -p $1)
47    typeset -a decl_a
48    decl_a=($decl_str)
49    typeset -a decl_a2
50    decl_a2=${decl_a[@]:2}
51    _Dbg_write_journal ${decl_a2[@]}
52}
53
54# Append a command into journal file. But we only need to do
55# if we are in a subshell.
56_Dbg_write_journal() {
57    if (( BASH_SUBSHELL != 0 )) ; then
58        echo "$@" >> ${_Dbg_journal} 2>/dev/null
59    fi
60    # return $?
61}
62
63# Remove all journal files.
64_Dbg_erase_journals() {
65    [[ -f $_Dbg_journal ]] && rm ${_Dbg_journal} 2>/dev/null
66    return $?
67}
68
69# read in or "source" in journal file which will set variables.
70_Dbg_source_journal() {
71
72  if [ -r $_Dbg_journal ] ; then
73    . $_Dbg_journal
74    (( BASH_SUBSHELL == 0 )) && _Dbg_erase_journals
75  fi
76}
77
78if [ ! -f _Dbg_journal ] ; then
79    typeset -i _Dbg_QUIT_LEVELS=0
80    _Dbg_write_journal "_Dbg_QUIT_LEVELS=0"
81fi
82