chiark / gitweb /
bin/mdw-build: Use dynamically chosen file descriptors.
authorMark Wooding <mdw@distorted.org.uk>
Sun, 10 May 2020 01:56:28 +0000 (02:56 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Sun, 10 May 2020 01:56:28 +0000 (02:56 +0100)
Having just ranted about a bug caused by shell scripts statically
allocating file descriptors, fix `mdw-build' to allocate descriptors
dynamically, since it's written in Bash, which can apparently do this.

bin/mdw-build

index 7f32c4f3ebd176025b6806e3f7de187b594214d3..27eac50d441c35e076276948afc73056034cf617 100755 (executable)
@@ -188,17 +188,17 @@ case $jobs in 1) ;; *) makeopts="$makeopts -j$jobs" ;; esac
 ##
 ##    0 -- original stdin (never touched)
 ## 1, 2 -- stdout, stderr, redirected to 3 while running comamnds
-##    3 -- original stderr (verbose), or logfile (quiet); captures command
+##  log -- original stderr (verbose), or logfile (quiet); captures command
 ##             output
-##    4 -- null (verbose), or logfile (quiet); primary diagnostic output
-##    5 -- orginal stderr; secondary diagnostic output (with colours)
+## diag -- null (verbose), or logfile (quiet); primary diagnostic output
+## term -- orginal stderr; secondary diagnostic output (with colours)
 
-exec 3>&2 4>/dev/null 5>&2
+exec {log}>&2 {diag}>/dev/null {term}>&2
 
 notify () {
   colour=$1 message=$2
-  echo $message >&4
-  echo "$(tput bold; tput setaf $colour)$message$(tput sgr0; tput op)" >&5
+  echo $message >&$diag
+  echo "$(tput bold; tput setaf $colour)$message$(tput sgr0; tput op)" >&$term
 }
 
 fail () {
@@ -224,18 +224,18 @@ assign () {
 
 runx () {
   notify 2 "+++ $*"
-  "$@" 2>&3 3>&- 4>&- 5>&- || fail "$1: exit $?"
+  "$@" 2>&$log {log}>&- {diag}>&- {term}>&- || fail "$1: exit $?"
 }
 
-run () { runx "$@" >&3; }
+run () { runx "$@" >&$log; }
 
 yesno () {
-  echo -n "(test $*)" >&4
-  if "$@" >&4 2>&4 3>&- 4>&- 5>&-; then
-    echo "(yes)" >&4
+  echo -n "(test $*)" >&$diag
+  if "$@" >&$diag 2>&$diag {log}>&- {diag}>&- {term}>&-; then
+    echo "(yes)" >&$diag
     echo yes
   else
-    echo "(no)" >&4
+    echo "(no)" >&$diag
     echo no
   fi
 }
@@ -270,7 +270,8 @@ rm -rf $releasepath 2>/dev/null || :
 mkdir $releasepath
 case $verbose in
   no)
-    exec 4>$releasepath/mdw-build.log 3>&4 ||
+    exec {log}>&- {diag}>&-
+    exec {diag}>$releasepath/mdw-build.log {log}>&$diag ||
       fail "Failed to create log."
     ;;
 esac
@@ -375,10 +376,10 @@ esac
 cat >find-distdir.mk <<'EOF'
 include Makefile
 print-distdir:
-       @echo >&3 $(distdir)
+       @echo >&$(fd) $(distdir)
 EOF
 assign distdir \
-       $({ $make -f find-distdir.mk print-distdir >/dev/null 2>&1; } 3>&1)
+  $({ $make -f find-distdir.mk print-distdir fd=$t >/dev/null 2>&1; } {t}>&1)
 
 ## Get a tarball distribution.
 case "$distcheck" in