Project

General

Profile

« Previous | Next » 

Revision 9279

lib/sh/util.sh: moved streams section before external commands echoing because command() uses set_fds

View differences:

lib/sh/util.sh
321 321
if self_being_included; then
322 322

  
323 323

  
324
#### streams
325

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

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

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

  
333
fd_set_default() # usage: fd_set_default 'dest[<>]src'
334
{
335
	echo_func
336
	local dest="${1%%[<>]*}"
337
	require_fd_not_exists "$dest" || return 0
338
	set_fds "$1"
339
}
340

  
341
# convention: use fd 10/11/12 for command-specific alternate stdin/stdout/stderr
342

  
343
set_global_fds()
344
# allows commands to access global stdin/stdout/stderr using fd 20/21/22
345
# this works even when /dev/tty isn't available
346
{
347
	log++; echo_func; log++
348
	# ignore errors if a source fd isn't open
349
	fd_set_default '20<&0' || true
350
	fd_set_default '21>&1' || true
351
	fd_set_default '22>&2' || true
352
}
353
set_global_fds
354
err_fd=22 # global stderr
355

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

  
363

  
324 364
#### paths
325 365

  
326 366
top_script_abs="$(realpath "$0")"; echo_vars top_script_abs # outermost script
......
405 445
echo_stdout () { echo_stdin; } # usage: cmd|echo_stdout
406 446

  
407 447

  
408
#### streams
409

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

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

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

  
417
fd_set_default() # usage: fd_set_default 'dest[<>]src'
418
{
419
	echo_func
420
	local dest="${1%%[<>]*}"
421
	require_fd_not_exists "$dest" || return 0
422
	set_fds "$1"
423
}
424

  
425
# convention: use fd 10/11/12 for command-specific alternate stdin/stdout/stderr
426

  
427
set_global_fds()
428
# allows commands to access global stdin/stdout/stderr using fd 20/21/22
429
# this works even when /dev/tty isn't available
430
{
431
	log++; echo_func; log++
432
	# ignore errors if a source fd isn't open
433
	fd_set_default '20<&0' || true
434
	fd_set_default '21>&1' || true
435
	fd_set_default '22>&2' || true
436
}
437
set_global_fds
438
err_fd=22 # global stderr
439

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

  
447

  
448 448
#### commands
449 449

  
450 450
require_not_exists() # usage: require_not_exists file || return 0

Also available in: Unified diff