File: dbg-file.inc

package info (click to toggle)
bashdb 3.1.0.9-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 3,252 kB
  • ctags: 117
  • sloc: sh: 4,880; pascal: 3,186; lisp: 484; makefile: 315; ansic: 294
file content (249 lines) | stat: -rw-r--r-- 6,795 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# Things related to file handling.
#
#   Copyright (C) 2002, 2003, 2004, 2006 Rocky Bernstein 
#   rockyb@users.sourceforge.net
#
#   Bash is free software; you can redistribute it and/or modify it under
#   the terms of the GNU General Public License as published by the Free
#   Software Foundation; either version 2, or (at your option) any later
#   version.
#
#   Bash is distributed in the hope that it will be useful, but WITHOUT ANY
#   WARRANTY; without even the implied warranty of MERCHANTABILITY or
#   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
#   for more details.
#   
#   You should have received a copy of the GNU General Public License along
#   with Bash; see the file COPYING.  If not, write to the Free Software
#   Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.

# Directory search patch for unqualified file names
typeset -a _Dbg_dir=('\$cdir' '\$cwd' )

# Directory in which the script is located
typeset -r _Dbg_cdir=${_Dbg_source_file%/*}

#
# Resolve $1 to a full file name which exists. First see if filename has been
# mentioned in a debugger "file" command. If not and the file name
# is a relative name use _Dbg_dir to substitute a relative directory name.
#
_Dbg_resolve_expand_filename() {
  local find_file=$1

  if [[ -z "$find_file" ]] ; then
    _Dbg_msg "Internal debug error: null file to find"
    echo ''
    return 1
  fi

  # Is this one of the files we've that has been specified in a debugger
  # "FILE" command?
  local -r filevar=`_Dbg_file2var $find_file`
  local file_cmd_file=`_Dbg_get_assoc_scalar_entry "_Dbg_file_cmd_" $filevar`
  if [[ -n "$file_cmd_file" ]] ; then
    echo "$file_cmd_file"
    return 0
  fi

  if [[ ${find_file:0:1} == '/' ]] ; then 
    # Absolute file name
    echo "$find_file"
    return 0
  elif [[ ${find_file:0:1} == '.' ]] ; then
    # Relative file name
    full_find_file=$(_Dbg_expand_filename ${_Dbg_init_cwd}/$find_file)
    if [[ -z "$full_find_file" ]] || [[ ! -r $full_find_file ]]; then
      # Try using cwd rather that Dbg_init_cwd
      full_find_file=$(_Dbg_expand_filename $find_file)
    fi
    echo "$full_find_file"
    return 0
  else
    # Resolve file using _Dbg_dir
    local -i n=${#_Dbg_dir[@]}
    local -i i
    for (( i=0 ; i < n; i++ )) ; do
      local basename="${_Dbg_dir[i]}"
      if [[  $basename == '\$cdir' ]] ; then
	basename=$_Dbg_cdir
      elif [[ $basename == '\$cwd' ]] ; then
	basename=$(pwd)
      fi
      if [[ -f "$basename/$find_file" ]] ; then
	echo "$basename/$find_file"
	return 0
      fi
    done
  fi
  echo ""
  return 1
}

# _Dbg_is_file echoes the full filename if $1 is a filename found in files
# '' is echo'd if no file found.
_Dbg_is_file() {
  local find_file=$1

  if [[ -z "$find_file" ]] ; then
    _Dbg_msg "Internal debug error: null file to find"
    echo ''
    return
  fi

  if [[ ${find_file:0:1} == '/' ]] ; then 
    # Absolute file name
    for try_file in ${_Dbg_filenames[@]} ; do 
      if [[ $try_file == $find_file ]] ; then
	echo "$try_file"
	return
      fi
    done
  elif [[ ${find_file:0:1} == '.' ]] ; then
    # Relative file name
    find_file=$(_Dbg_expand_filename ${_Dbg_init_cwd}/$find_file)
    for try_file in ${_Dbg_filenames[@]} ; do 
      if [[ $try_file == $find_file ]] ; then
	echo "$try_file"
	return
      fi
    done
  else
    # Resolve file using _Dbg_dir
    for try_file in ${_Dbg_filenames[@]} ; do 
      local pathname
      local -i n=${#_Dbg_dir[@]}
      local -i i
      for (( i=0 ; i < n; i++ )) ; do
	local basename="${_Dbg_dir[i]}"
	if [[  $basename = '\$cdir' ]] ; then
	  basename=$_Dbg_cdir
	elif [[ $basename = '\$cwd' ]] ; then
	  basename=$(pwd)
	fi
	if [[ "$basename/$find_file" == $try_file ]] ; then
	  echo "$try_file"
	  return
	fi
      done
    done
  fi
  echo ""
}

# Turn filename $1 into something that is safe to use as a variable name
_Dbg_file2var() {
  local filename=$(_Dbg_expand_filename $1)
  local varname=`builtin echo $filename | tr '=~+%* .?/"[]<>-' 'ETPpABDQSqLRlGM'`
  builtin echo $varname
}

# $1 contains the name you want to glob. return 1 if exists and is
# readible or 0 if not. 
# The result will be in variable $filename which is assumed to be 
# local'd by the caller
_Dbg_glob_filename() {
  local cmd="filename=`expr $1`"
  eval $cmd
}

# Either fill out or strip filename as determined by "basename_only"
# and annotate settings
_Dbg_adjust_filename() {
  local -r filename="$1"
  if (( _Dbg_annotate == 1 )) ; then
    echo `_Dbg_resolve_expand_filename $filename`
  elif ((_Dbg_basename_only)) ; then
    echo ${filename##*/}
  else
    echo $filename
  fi
}

# Return the maximum line in $1
_Dbg_get_maxline() {
  # set -x
  local -r filename=$1
  local -r filevar=`_Dbg_file2var $filename`
  local is_read=`_Dbg_get_assoc_scalar_entry "_Dbg_read_" $filevar`
  [ $is_read ] || _Dbg_readin $filename 
  echo `_Dbg_get_assoc_scalar_entry "_Dbg_maxline_" $filevar`
  # set +x
}

# Check that line $2 is not greater than the number of lines in 
# file $1
_Dbg_check_line() {
  local -ir line_number=$1
  local filename=$2
  local -i max_line=`_Dbg_get_maxline $filename`
  if (( $line_number >  max_line )) ; then 
    (( _Dbg_basename_only )) && filename=${filename##*/}
    _Dbg_msg "Line $line_number is too large." \
      "File $filename has only $max_line lines."
    return 1
  fi
  return 0
}

# Create temporary file based on $1
# file $1
_Dbg_tempname() {
  echo "$_Dbg_tmpdir/bashdb$1$$"
}

# append a command into journal file and then run the command.
_Dbg_write_journal_eval() {
  _Dbg_write_journal "$*" 
  eval "$*"
}

# append a command into journal file and then run the command.
_Dbg_write_journal_var() {
  local var_name=$1
  local val
  local val_cmd="$val=\${$var_name}"
  eval $val_cmd
  _Dbg_write_journal "${var_name}=${val}" 
}

_Dbg_write_journal_avar() {
  local decl_str=$(declare -p $1)
  local -a decl_a
  decl_a=($decl_str)
  local -a decl_a2
  decl_a2=${decl_a[@]:2}
  _Dbg_write_journal ${decl_a2[@]}
}

# Append a command into journal file. But we only need to do
# if we are in a subshell.
_Dbg_write_journal() {
  if (( $BASH_SUBSHELL != 0 )) ; then
    echo "$@" >> ${_Dbg_journal} 2>/dev/null
  fi
  # return $?
}

# Remove all journal files.
_Dbg_erase_journals() {
  rm ${_Dbg_journal} 2>/dev/null
}

# read in or "source" in journal file which will set variables.
_Dbg_source_journal() {

  if [ -r $_Dbg_journal ] ; then 
    . $_Dbg_journal
    (( BASH_SUBSHELL == 0 )) && _Dbg_erase_journals
  fi
}

# This is put at the so we have something at the end when we debug this.
typeset -r _Dbg_file_ver=\
'$Id: dbg-file.inc,v 1.5 2006/12/19 20:06:46 rockyb Exp $'

#;;; Local Variables: ***
#;;; mode:shell-script ***
#;;; eval: (sh-set-shell "bash") ***
#;;; End: ***