Project

General

Profile

1
#!/bin/bash -e
2
set -o errexit -o pipefail # errexit in case caller's #! line missing -e
3

    
4
if test ! "$_util_sh_include_guard_utils"; then
5
_util_sh_include_guard_utils=1
6

    
7
isset() { test "${!1+isset}"; }
8

    
9
realpath() { readlink -f -- "$1"; }
10

    
11
str2varname() { echo "${1//[^a-zA-Z0-9_]/_}"; }
12

    
13
include_guard_var() { str2varname "$(realpath "$1")"; }
14

    
15
self_not_included() # usage: if self_not_included; then ... fi
16
{
17
	test $# -ge 1 || set -- "${BASH_SOURCE[1]}"
18
	local include_guard="$(include_guard_var "$1")"
19
	alias self_being_included=false
20
	! isset "$include_guard" && \
21
	{ eval "$include_guard"=1; alias self_being_included=true; }
22
}
23

    
24
# to load newly-defined aliases for use in functions in the same file:
25
## fi # load new aliases
26
## if self_being_included; then
27
# this is needed because aliases defined inside an if statement are not
28
# available inside that if statement
29

    
30
fi
31

    
32

    
33
if self_not_included "${BASH_SOURCE[0]}"; then
34

    
35

    
36
#### options
37

    
38
shopt -s expand_aliases
39

    
40

    
41
#### vars
42

    
43
set_var() { eval "$1"'="$2"'; }
44

    
45
set_default() { if ! isset "$1"; then set_var "$@"; fi; }
46

    
47
set_inv() { set_var no_"$1" "$(test "${!1}" || echo 1)"; }
48

    
49
# usage: local var=...; local_inv
50
alias local_inv='declare "no_$var=$(test "${!var}" || echo 1)"'
51

    
52
unexport() { export -n "$@"; }
53
	# `declare +x` won't work because it defines the var if it isn't set
54

    
55
alias local_export='declare -x' # combines effects of local and export
56

    
57
get_prefix_vars() { : "${prefix:?}"; eval echo '${!'$prefix'*}'; }
58

    
59
# usage: local prefix=..._; import_vars
60
# when used inside another alias 2+ levels deep, *must* be run inside a function
61
alias import_vars="$(cat <<'EOF'
62
: "${prefix:?}"
63
declare src_var dest_var
64
for src_var in $(get_prefix_vars); do
65
	dest_var="${src_var#$prefix}"
66
	declare "$dest_var=${!src_var}"; echo_vars "$dest_var"
67
done
68
EOF
69
)"
70

    
71

    
72
#### caching
73

    
74
## shell-variable-based caching
75

    
76
# usage: local cache_key=...; load_cache; \
77
# if ! cached; then save_cache value || return; fi; echo_cached_value
78
# cache_key for function inputs: "$(declare -p kw_param...) $*"
79
alias load_cache='declare cache_var="$(str2varname "${FUNCNAME}___$cache_key")"'
80
alias cached='isset "$cache_var"'
81
alias save_cache='set_var "$cache_var"'
82
alias echo_cached_value='echo "${!cache_var}"'
83

    
84
clear_cache() # usage: func=... clear_cache
85
{ : "${func:?}"; unset $(prefix="${func}___" get_prefix_vars); }
86

    
87
fi # load new aliases
88
if self_being_included; then
89

    
90

    
91
#### aliases
92

    
93
unalias() { builtin unalias "$@" 2>&- || true; } # no error if undefined
94

    
95
# usage: alias alias_='var=value run_cmd '
96
function run_cmd() { "$@"; }
97
alias run_cmd='"run_cmd" ' # last space alias-expands next word
98

    
99
alias_append() { eval $(alias "$1")'"$2"'; } # usage: alias_append alias '; cmd'
100

    
101

    
102
#### functions
103

    
104
kw_params() # usage: func() { kw_params param_var...; }; ...; param_var=... cmd
105
# removes keyword-param-only vars from the environment
106
{ unexport "$@"; }
107

    
108
alias self='command "$FUNCNAME"' # usage: wrapper() { self ...; }
109

    
110
pf() { declare -f "$@"; } # usage: pf function # prints func decl for debugging
111

    
112
all_funcs() # usage: for func in $(all_funcs); do ...; done # all declared funcs
113
{ declare -F|while read -r line; do echo -n "${line#declare -f } "; done; }
114

    
115

    
116
#### exceptions
117

    
118
# usage: cmd || { save_e; ...; rethrow; }
119
alias export_e='e=$?'
120
alias save_e='declare e=$?'
121
alias rethrow='return "$e"'
122
alias rethrow_subshell='exit "$e"'
123

    
124
fi # load new aliases
125
if self_being_included; then
126

    
127
# usage: try cmd...; ignore_e status; if catch status; then ...; fi; end_try
128

    
129
function try() { e=0; benign_error=1 "$@" || { export_e; true; }; }
130
alias try='declare e; "try" ' # last space alias-expands next word
131

    
132
catch() { test "$e" -eq "$1" && e=0; }
133

    
134
ignore_e() { catch "$@" || true; }
135

    
136
alias end_try='rethrow'
137
alias end_try_subshell='rethrow_subshell'
138

    
139
ignore() { save_e; ignore_e "$@"; rethrow; } # usage: try cmd || ignore status
140

    
141
### signals
142

    
143
sig_e() { echo $(( 128+$(kill -l "$1") )); } # usage: sig_e SIGINT, etc.
144

    
145
ignore_sig() { ignore "$(sig_e "$1")"; }
146

    
147
# usage: piped_cmd cmd1...|cmd2... # cmd2 doesn't read all its input
148
function piped_cmd() { "$@" || ignore_sig SIGPIPE; }
149
alias piped_cmd='"piped_cmd" ' # last space alias-expands next word
150

    
151
fi # load new aliases
152
if self_being_included; then
153

    
154

    
155
#### integers
156

    
157
let!() { let "$@" || true; } # always returns true; safe to use for setting
158
	# "If the last ARG evaluates to 0, let returns 1" (`help let`)
159

    
160
bool2int() { try test ! "$1"; echo "$e"; } # empty->0; non-empty->1
161

    
162
int2exit() { (( "$1" != 0 )); }
163

    
164
exit2bool() { if (( $? == 0 )); then echo 1; fi } # 0->non-empty; !=0->empty
165

    
166

    
167
#### floats
168

    
169
int_part() { echo "${1%%.*}"; }
170

    
171
dec_suffix() { echo "${1#$(int_part "$1")}"; }
172

    
173
round_down() { int_part "$1"; }
174

    
175
float+int() { echo "$(($(int_part "$1")+$2))$(dec_suffix "$1")"; }
176

    
177
float_set_min() { if (($(int_part $1) >= $2)); then echo $1; else echo $2; fi; }
178

    
179

    
180
#### strings
181

    
182
starts_with() { test "${2#$1}" != "$2"; } # usage: starts_with pattern str
183

    
184
repeat() # usage: str=... n=... repeat
185
{
186
	: "${str?}" "${n:?}"; local result= n="$n" # n will be modified in function
187
	for (( ; n > 0; n-- )); do result="$result$str"; done
188
	echo "$result"
189
}
190

    
191
sed_cmd="sed -`case "$(uname)" in Darwin) echo E;; *) echo r;; esac`"
192
alias sed="$sed_cmd"
193

    
194
fi # load new aliases
195
if self_being_included; then
196

    
197
rtrim() { log+ 3; sed 's/[[:space:]]+$//' <<<"$1"; }
198

    
199

    
200
#### arrays
201

    
202
join() { local IFS="$delim"; echo "$*"; } # usage: delim=... join elems...
203

    
204
reverse() # usage: array=($(reverse args...))
205
{
206
	local i
207
	for (( i=$#; i > 0; i-- )); do printf '%q ' "${!i}"; done
208
}
209

    
210
contains() # usage: contains value in_array...
211
{
212
	local value="$1"; shift
213
	local elem
214
	for elem in "$@"; do if test "$elem" = "$value"; then return 0; fi; done
215
	return 1
216
}
217

    
218

    
219
#### verbose output
220

    
221

    
222
err_fd=2 # stderr
223

    
224
usage() { echo "Usage: $1" >&2; return 2; }
225

    
226

    
227
### log++
228

    
229
log_fd=2 # initially stderr
230

    
231
if test "$explicit_errors_only"; then verbosity=0; fi # hide startup logging
232

    
233
# set verbosity
234
if isset verbose; then : "${verbosity:=$(bool2int "$verbose")}"; fi
235
if isset vb; then : "${verbosity:=$vb}"; fi
236
: "${verbosity=1}" # default
237
: "${verbosity:=0}" # ensure non-empty
238
export verbosity # propagate to invoked commands
239
export PS4 # follows verbosity, so also propagate this
240

    
241
# set log_level
242
: "${log_level=$(( ${#PS4}-1 ))}" # defaults to # non-space symbols in PS4
243
export log_level # propagate to invoked commands
244

    
245
verbosity_int() { round_down "$verbosity"; }
246

    
247
# verbosities (and `make` equivalents):
248
# 0: just print errors. useful for cron jobs.
249
#    vs. make: equivalent to --silent, but suppresses external command output
250
# 1: also external commands run. useful for running at the command line.
251
#    vs. make: not provided (but sorely needed to avoid excessive output)
252
# 2: full graphical call tree. useful for determining where error occurred.
253
#    vs. make: equivalent to default verbosity, but with much-needed indents
254
# 3: also values of kw params and variables. useful for low-level debugging.
255
#    vs. make: not provided; need to manually use $(error $(var))
256
# 4: also variables in util.sh commands. useful for debugging util.sh.
257
#    vs. make: somewhat similar to --print-data-base
258
# 5: also variables in logging commands themselves. useful for debugging echo_*.
259
#    vs. make: not provided; need to search Makefile for @ at beginning of cmd
260
# 6+: not currently used (i.e. same as 5)
261

    
262
# definition: the log_level is the minimum verbosity needed to display a message
263
# for messages that use can_log(), the log_level starts with *1*, not 0
264
# for unfiltered messages, the log_level is 0 (i.e. still output at verbosity=0)
265
# to view a message's log_level, count the # of + signs before it in the output
266

    
267
fi # load new aliases
268
if self_being_included; then
269

    
270
# usage: in func:      log++; ...         OR  log_local; "log++"; ...
271
#        outside func: log++; ...; log--
272
#        before cmd:   log++ cmd...
273
# without a cmd, "$@" expands to nothing and assignments are applied to caller
274
# "${@:2}" expands to all of $@ after *1st* arg, not 2nd ($@ indexes start at 1)
275
log+()
276
{
277
	# no local vars because w/o cmd, assignments should be applied to caller
278
	PS4="$(str="${PS4:0:1}" n=$((log_level+$1-1)) repeat)${PS4: -2}"; \
279
	log_level=$((log_level+$1)) \
280
	verbosity="$(float+int "$verbosity" "-$1")" "${@:2}"
281
}
282
log++() { log+  1 "$@"; }
283
log--() { log+ -1 "$@"; }
284
alias log_local=\
285
'declare PS4="$PS4" log_level="$log_level" verbosity="$verbosity"'
286
alias log+='log_local; "log+"' # don't expand next word because it's not a cmd
287
alias log++='log_local; "log++" ' # last space alias-expands next word
288
alias log--='log_local; "log--" ' # last space alias-expands next word
289

    
290
verbosity_min() # usage: verbosity_min min
291
{ if test "$(verbosity_int)" -lt "$1"; then verbosity="$1"; fi; }
292
alias verbosity_min='log_local; "verbosity_min"'
293

    
294

    
295
# indent for call tree. this is *not* the log_level (below).
296
: "${log_indent_step=| }" "${log_indent=}"
297
export log_indent_step log_indent # propagate to invoked commands
298

    
299
__caller_indent='log_indent="$log_indent$log_indent_step"'
300
alias caller_indent="$__caller_indent"
301
alias indent="declare $__caller_indent"
302

    
303

    
304
fi # load new aliases
305
if self_being_included; then
306

    
307
can_log() { test "$(verbosity_int)" -gt 0; }
308
	# verbosity=0 turns off all logging
309

    
310
log() { if can_log; then echo "$log_indent$PS4$1" >&"$log_fd"; fi; }
311

    
312
log_custom() # usage: symbol=... log_custom msg
313
{ log_indent="${log_indent//[^ ]/$symbol}" PS4="${PS4//[^ ]/$symbol}" log "$@";}
314

    
315
log_err() { symbol='#' verbosity=1 log_fd="$err_fd" log_custom "$@"; }
316

    
317
log_info() { symbol=: log_custom "$@"; }
318

    
319
die() # usage: cmd || [type=...] die msg # msg can use $? but not $()
320
{ save_e; kw_params type; "log_${type:-err}" "$1"; rethrow; }
321

    
322
die_e() # usage: cmd || [benign_error=1] die_e [|| handle error]
323
{
324
	save_e; kw_params benign_error
325
	if test "$benign_error"; then log++; fi
326
	type="${benign_error:+info}" die "command exited with \
327
$(if test "$benign_error"; then echo status; else echo error; fi) $e"
328
	rethrow
329
}
330

    
331

    
332
#### functions
333

    
334
func_exists() { declare -f "$1" >/dev/null; }
335

    
336
copy_func() # usage: from=... to=... copy_func
337
# $to must not exist. to get around the no-clobber restriction, use `unset -f`.
338
{
339
	: "${from:?}" "${to:?}"
340
	func_exists "$from" || die "function does not exist: $from"
341
	! func_exists "$to" || die "function already exists: $to"
342
	local from_def="$(declare -f "$from")"
343
	eval "$to${from_def#$from}"
344
}
345

    
346
func_override() # usage: func_override old_name__suffix
347
{ from="${1%__*}" to="$1" copy_func; }
348

    
349
ensure_nested_func() # usage: func__nested_func() { ensure_nested_func; ... }
350
{
351
	local nested_func="${FUNCNAME[1]}"
352
	local func="${nested_func%%__*}"
353
	contains "$func" "${FUNCNAME[@]}" || \
354
		die "$nested_func() must be used by $func()"
355
}
356

    
357

    
358
#### paths
359

    
360
# cache realpath
361
: "${realpath_cache=}" # default off because slower than without
362
if test "$realpath_cache"; then
363
func_override realpath__no_cache
364
realpath() # caches the last result for efficiency
365
{
366
	local cache_key="$*"; load_cache
367
	if ! cached; then save_cache "$(realpath__no_cache "$@")" || return; fi
368
	echo_cached_value
369
}
370
fi
371

    
372
rel_path() # usage: base_dir=... path=... rel_path
373
{
374
	log++; kw_params base_dir path
375
	: "${base_dir:?}" "${path:?}"
376
	
377
	local path="$path/" # add *extra* / to match path when exactly = base_dir
378
	path="${path#$base_dir/}" # remove prefix shared with base_dir
379
	path="${path%/}" # remove any remaining extra trailing /
380
	
381
	if test ! "$path"; then path=.; fi # ensure non-empty
382
	
383
	echo_vars path
384
	echo "$path"
385
}
386

    
387
cd -P . # expand symlinks in $PWD so it matches the output of realpath
388
# do before setting $top_script_abs so realpath has less symlinks to resolve
389

    
390
canon_rel_path()
391
{
392
	local path; path="$(realpath "$1")" || return
393
	base_dir="$PWD" rel_path
394
}
395

    
396
# makes $1 a canon_rel_path if it's a filesystem path
397
alias cmd2rel_path="$(cat <<'EOF'
398
if test "$(type -t "$1")" = file && test -e "$1"; then # not relative to PATH
399
	declare _1="$1"; shift
400
	_1="$(canon_rel_path "$_1")" || return
401
	set -- "$_1" "$@"
402
fi
403
EOF
404
)"
405

    
406

    
407
#### verbose output
408

    
409

    
410
### command echoing
411

    
412
alias echo_params='log "$*"'
413

    
414
fi # load new aliases
415
if self_being_included; then
416

    
417
echo_cmd() { echo_params; }
418

    
419
function echo_run() { echo_params; "$@"; }
420
alias echo_run='"echo_run" ' # last space alias-expands next word
421

    
422
echo_eval() { echo_params; builtin eval "$@"; }
423

    
424
# usage: redirs=(...); echo_redirs_cmd
425
function echo_redirs_cmd()
426
{
427
	# print <>file redirs before cmd, because they introduce it
428
	echo_cmd "$@" $(
429
		set -- "${redirs[@]}" # operate on @redirs
430
		while test "$#" -gt 0 && starts_with '[<>][^&]' "$1"
431
		do log "$1 \\"; shift; done # log() will run *before* echo_cmd itself
432
		echo "$@"
433
	)
434
}
435
alias echo_redirs_cmd='"echo_redirs_cmd" "$@"'
436

    
437
## vars
438

    
439
echo_vars() # usage: echo_vars var...
440
{
441
	log+ 2
442
	if can_log; then
443
		local var
444
		for var in "${@%%=*}"; do
445
			if isset "$var"; then log "$(declare -p "$var")"; fi
446
		done
447
	fi
448
}
449

    
450
echo_export() { builtin export "$@"; echo_vars "$@"; }
451

    
452
alias export="echo_export" # automatically echo env vars when they are set
453

    
454
func_override kw_params__lang
455
kw_params() { kw_params__lang "$@"; echo_vars "$@"; } # echo all keyword params
456

    
457
## functions
458

    
459
# usage: local func=...; set_func_loc; use $file, $line
460
alias set_func_loc="$(cat <<'EOF'
461
: "${func:?}"
462
local func_info="$(shopt -s extdebug; declare -F "$func")" # 'func line file'
463
func_info="${func_info#$func }"
464
local line="${func_info%% *}"
465
local file="${func_info#$line }"
466
EOF
467
)"
468

    
469
fi # load new aliases
470
if self_being_included; then
471

    
472
func_loc() # gets where function declared in the format file:line
473
{
474
	local func="$1"; set_func_loc
475
	file="$(canon_rel_path "$file")" || return
476
	echo "$file:$line"
477
}
478

    
479
# usage: func() { [minor=1] echo_func; ... }
480
function echo_func()
481
# usage: [minor=1] "echo_func" "$FUNCNAME" "$@" && indent || true
482
# exit status: whether function call was echoed
483
{
484
	kw_params minor
485
	local func="$1"; shift
486
	
487
	log++; if test "$minor"; then log++; fi
488
	local loc; loc="$(func_loc "$func")" || return
489
	echo_cmd "$loc" "$func" "$@"
490
	can_log
491
}
492
alias echo_func='"echo_func" "$FUNCNAME" "$@" && indent || true'
493

    
494
fi # load new aliases
495
if self_being_included; then
496

    
497

    
498
#### streams
499

    
500
fd_exists() { (: <&"$1") 2>/dev/null; }
501

    
502
require_fd_not_exists() # usage: require_fd_not_exists fd || return 0
503
{ ! fd_exists "$1" || type=info die "fd $1 already exists, skipping"; }
504

    
505
set_fds() { echo_func; echo_eval exec "$@"; } # usage: set_fds redirect...
506

    
507
fd_set_default() # usage: fd_set_default 'dest[<>]src'
508
{
509
	echo_func
510
	local dest="${1%%[<>]*}"
511
	require_fd_not_exists "$dest" || return 0
512
	set_fds "$1"
513
}
514

    
515
# convention: use fd 40/41/42 for command-specific alternate stdin/stdout/stderr
516
# do NOT use 1x, which are used by eval (which is used by set_fds())
517
# do NOT use 2x, which are used as global stdin/stdout/stderr
518
# do NOT use 3x, which are used for logging
519

    
520
setup_log_fd() # view logging output at verbosity >= 5
521
{
522
	log+ 4; log-- echo_func 
523
	fd_set_default '30>&2' || true # stdlog
524
	log_fd=30 # stdlog
525
}
526
setup_log_fd
527

    
528
set_global_fds()
529
# allows commands to access global stdin/stdout/stderr using fd 20/21/22
530
# this works even when /dev/tty isn't available
531
# view logging output at verbosity >= 3
532
{
533
	log+ 2; log-- echo_func
534
	# ignore errors if a source fd isn't open
535
	fd_set_default '20<&0' || true
536
	fd_set_default '21>&1' || true
537
	fd_set_default '22>&2' || true
538
}
539
set_global_fds
540

    
541
# usage: explicit_errors_only=1 script...
542
# show only explicitly-displayed errors (which have been redirected to fd 22)
543
# most of the time this has the same effect as `verbosity=0 script...`,
544
# which displays everything that isn't explicitly hidden
545
# this option should only be used for testing the explicit error displaying
546
if test "$explicit_errors_only"; then disable_logging; fi
547

    
548

    
549
#### paths
550

    
551
top_script_abs="$(realpath "$0")"; echo_vars top_script_abs # outermost script
552
	# realpath this before doing any cd so this points to the right place
553

    
554
set_paths()
555
{
556
	top_script="$(canon_rel_path "$top_script_abs")" || return
557
		echo_vars top_script
558
	top_dir="$(dirname "$top_script")" || return; echo_vars top_dir
559
}
560
set_paths
561

    
562

    
563
#### verbose output
564

    
565

    
566
## internal commands
567

    
568
.()
569
{
570
	cmd2rel_path; set -- "$FUNCNAME" "$@"
571
	if (log++; echo_params; can_log); then indent; fi
572
	builtin "$@"
573
}
574

    
575
cd() # indent is permanent within subshell cd was used in
576
{
577
	log++ echo_func
578
	cmd2rel_path; echo_cmd "$FUNCNAME" "$@"
579
	if can_log; then caller_indent; fi
580
	# -P: expand symlinks so $PWD matches the output of realpath
581
	builtin "$FUNCNAME" -P "$@"
582
	
583
	func=realpath clear_cache
584
	set_paths
585
}
586

    
587
## external commands
588

    
589
disable_logging() { set_fds "$log_fd>/dev/null"; }
590

    
591
function redir() # usage: local redirs=(#<>...); redir cmd...; unset redirs
592
# to view only explicitly-displayed errors: explicit_errors_only=1 script...
593
{
594
	echo_func; kw_params redirs
595
	
596
	case "$1" in redir|command) "$@"; return;; esac # redir will be run later
597
	(
598
		log++ set_fds "${redirs[@]}"
599
		(case "$1" in command__exec) shift;; esac; echo_redirs_cmd)
600
		"$@"
601
	) || return
602
}
603
alias redir='"redir" ' # last space alias-expands next word
604

    
605
alias_append save_e '; unset redirs' # don't redirect error handlers
606

    
607
command() # usage: [cmd_log_fd=|1|2|#] [verbosity_min=] command extern_cmd...
608
{
609
	echo_func; kw_params cmd_log_fd redirs verbosity_min
610
	# if no cmd_log_fd, limit log_fd in case command uses util.sh
611
	local cmd_log_fd="${cmd_log_fd-$log_fd}"
612
	local redirs=("${redirs[@]}")
613
	
614
	# determine redirections
615
	if test "$cmd_log_fd"; then
616
		if can_log; then
617
			if test "$cmd_log_fd" != "$log_fd"; then
618
				redirs+=("$cmd_log_fd>&$log_fd")
619
			fi # else no redir needed
620
		else redirs+=("$cmd_log_fd>/dev/null");
621
		fi
622
	fi
623
	
624
	cmd2rel_path
625
	redir command__exec "$@" || die_e
626
}
627
command__exec()
628
{
629
	ensure_nested_func
630
	if can_log; then indent; fi
631
	if test "$verbosity_min"; then verbosity_min "$verbosity_min"; fi
632
	exec -- "$@" # -- so cmd name not treated as `exec` option
633
}
634

    
635
# auto-echo common external commands
636
for cmd in env rm; do alias "$cmd=command $cmd"; done; unset cmd
637

    
638

    
639
### external command input/output
640

    
641
pipe_delay() # usage: cmd1 | { pipe_delay; cmd2; }
642
{ sleep 0.1; } # s; display after leading output of cmd1
643

    
644
fi # load new aliases
645
if self_being_included; then
646

    
647
echo_stdin() # usage: input|echo_stdin|cmd
648
{
649
	if can_log; then
650
		pipe_delay
651
		echo ----- >&"$log_fd"
652
		tee -a /dev/fd/"$log_fd";
653
		echo ----- >&"$log_fd"
654
	else cat
655
	fi
656
}
657

    
658
echo_stdout() { echo_stdin; } # usage: cmd|echo_stdout
659

    
660

    
661
#### commands
662

    
663
already_exists_msg() # usage: cond || what=... already_exists_msg || return 0
664
{ type=info die "$what already exists, skipping"; }
665

    
666
require_not_exists() # usage: require_not_exists file || return 0
667
{ test ! -e "$1" || what="file \"$1\"" already_exists_msg; }
668

    
669
function to_file() # usage: stdout=... [if_not_exists=1] [del=] to_file cmd...
670
# auto-removes a command's output file on error (like make's .DELETE_ON_ERROR)
671
{
672
	echo_func; kw_params stdout if_not_exists del
673
	: "${stdout?}"; local del="${del-1}"
674
	if test "$if_not_exists"; then require_not_exists "$stdout" || return 0; fi
675
	
676
	local redirs=("${redirs[@]}" ">$stdout")
677
	redir "$@" || { save_e; test ! "$del" || rm "$stdout"; rethrow; }
678
}
679
alias to_file='"to_file" ' # last space alias-expands next word
680

    
681
run_args_cmd() # runs the command line args command
682
{
683
	eval set -- "$(reverse "${BASH_ARGV[@]}")"
684
	test $# -ge 1 || set -- all
685
	echo_cmd "$top_script" "$@"; "$@"
686
}
687

    
688
fwd() # usage: subdirs=(...); fwd "$FUNCNAME" "$@"
689
{
690
	echo_func
691
	: "${subdirs?}"
692
	
693
	for subdir in "${subdirs[@]}"; do "$top_dir"/"$subdir"/run "$@"; done
694
}
695

    
696

    
697
#### filesystem
698

    
699
alias file_size=\
700
"stat `case "$(uname)" in Darwin) echo -f %z;; *) echo --format=%s;; esac`"
701

    
702

    
703
#### URLs
704

    
705
localize_url() { test _"$1" = _"$(hostname -f)" || echo "$1"; }
706

    
707
fi
(8-8/8)