Project

General

Profile

« Previous | Next » 

Revision 14711

lib/sh/util.sh: errors, debugging: moved after datatype sections so their functions can use these

View differences:

trunk/lib/sh/util.sh
78 78
function echo_vars() { :; }
79 79

  
80 80

  
81
#### errors
82

  
83
err_fd=2 # stderr
84

  
85
# usage: local func=...; set_func_loc; use $file, $line
86
alias set_func_loc="$(cat <<'EOF'
87
: "${func:?}"
88
local func_info="$(shopt -s extdebug; declare -F "$func")" # 'func line file'
89
func_info="${func_info#$func }"
90
local line="${func_info%% *}"
91
local file="${func_info#$line }"
92
EOF
93
)"
94

  
95
fi # load new aliases
96
if self_being_included; then
97

  
98
func_loc() # gets where function declared in the format file:line
99
{
100
	local func="$1"; set_func_loc
101
	file="$(canon_rel_path "$file")" || return
102
	echo "$file:$line"
103
}
104

  
105

  
106
#### debugging
107

  
108
debug_fd="$err_fd"
109

  
110
ps() { echo "$@" >&"$debug_fd"; } # usage: ps str...
111

  
112
pv() { declare -p "$@" >&"$debug_fd"; } # usage: pv var... # debug-prints vars
113

  
114
pf() { declare -f "$@" >&"$debug_fd"; } # usage: pf func... # prints func decls
115

  
116

  
117 81
#### logic
118 82

  
119 83
not() { ! "$@"; } # usage: wrapper_cmd not wrapped_cmd... # inverts exit status
......
261 225
}
262 226

  
263 227

  
228
#### errors
229

  
230
err_fd=2 # stderr
231

  
232
# usage: local func=...; set_func_loc; use $file, $line
233
alias set_func_loc="$(cat <<'EOF'
234
: "${func:?}"
235
local func_info="$(shopt -s extdebug; declare -F "$func")" # 'func line file'
236
func_info="${func_info#$func }"
237
local line="${func_info%% *}"
238
local file="${func_info#$line }"
239
EOF
240
)"
241

  
242
fi # load new aliases
243
if self_being_included; then
244

  
245
func_loc() # gets where function declared in the format file:line
246
{
247
	local func="$1"; set_func_loc
248
	file="$(canon_rel_path "$file")" || return
249
	echo "$file:$line"
250
}
251

  
252

  
253
#### debugging
254

  
255
debug_fd="$err_fd"
256

  
257
ps() { echo "$@" >&"$debug_fd"; } # usage: ps str...
258

  
259
pv() { declare -p "$@" >&"$debug_fd"; } # usage: pv var... # debug-prints vars
260

  
261
pf() { declare -f "$@" >&"$debug_fd"; } # usage: pf func... # prints func decls
262

  
263

  
264 264
#### caching
265 265

  
266 266
## shell-variable-based caching

Also available in: Unified diff