diff options
author | Alastair Harrison <aharrison24@gmail.com> | 2020-08-28 10:53:58 (GMT) |
---|---|---|
committer | Alastair Harrison <aharrison24@gmail.com> | 2020-08-28 11:18:55 (GMT) |
commit | 67f960be1be8099ea1727af8d3361d38274b2bd1 (patch) | |
tree | 2a7b2a42d9944b59088a24bc6ca122805b8bdb8b /src | |
parent | 5b80abbc729e94abb5f5776a3438ad57d480c097 (diff) | |
download | Ninja-67f960be1be8099ea1727af8d3361d38274b2bd1.zip Ninja-67f960be1be8099ea1727af8d3361d38274b2bd1.tar.gz Ninja-67f960be1be8099ea1727af8d3361d38274b2bd1.tar.bz2 |
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.
Diffstat (limited to 'src')
-rwxr-xr-x | src/inline.sh | 13 |
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}" |