summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlastair Harrison <aharrison24@gmail.com>2020-08-28 11:53:58 +0100
committerAlastair Harrison <aharrison24@gmail.com>2020-08-28 12:18:55 +0100
commit67f960be1be8099ea1727af8d3361d38274b2bd1 (patch)
tree2a7b2a42d9944b59088a24bc6ca122805b8bdb8b
parent5b80abbc729e94abb5f5776a3438ad57d480c097 (diff)
downloadninja-67f960be1be8099ea1727af8d3361d38274b2bd1.tar.gz
Improve error handling in inline.sh
Previously the script would generate some output and return a zero error code, even if the calls to `od` or `sed` failed. This change ensures that: - If `od` or `sed` fail then the script will fail. - Output will only be written on success.
-rwxr-xr-xsrc/inline.sh13
1 files changed, 10 insertions, 3 deletions
diff --git a/src/inline.sh b/src/inline.sh
index b64e8ca..5092fa2 100755
--- a/src/inline.sh
+++ b/src/inline.sh
@@ -19,7 +19,14 @@
# stdin and writes stdout.
varname="$1"
-echo "const char $varname[] ="
-od -t x1 -A n -v | sed -e 's|^[\t ]\{0,\}$||g; s|[\t ]\{1,\}| |g; s| \{1,\}$||g; s| |\\x|g; s|^|"|; s|$|"|'
-echo ";"
+# 'od' and 'sed' may not be available on all platforms, and may not support the
+# flags used here. We must ensure that the script exits with a non-zero exit
+# code in those cases.
+byte_vals=$(od -t x1 -A n -v) || exit 1
+escaped_byte_vals=$(echo "${byte_vals}" \
+ | sed -e 's|^[\t ]\{0,\}$||g; s|[\t ]\{1,\}| |g; s| \{1,\}$||g; s| |\\x|g; s|^|"|; s|$|"|') \
+ || exit 1
+
+# Only write output once we have successfully generated the required data
+printf "const char %s[] = \n%s;" "${varname}" "${escaped_byte_vals}"