Skip to content

Commit a34b132

Browse files
committed
scripts: Detect and handle standard input in 'bin-graph-hexdump.sh'
Since the script can't redirect the standard input to both commands without an intermediate file.
1 parent 82be76e commit a34b132

1 file changed

Lines changed: 22 additions & 4 deletions

File tree

scripts/bin-graph-hexdump.sh

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,29 @@ assert_cmd "$HEXDUMP"
4545
assert_cmd "$BIN_GRAPH"
4646
assert_cmd "$PASTE"
4747

48+
# Last argument must be the input file.
4849
input_file="${*: -1}"
49-
if [ ! -f "$input_file" ]; then
50-
echo "$(basename "$0"): Input file '${input_file}' not found." 1>&2
51-
exit 1
50+
51+
# Final string of arguments for 'bin-graph'.
52+
bin_graph_args=("${BIN_GRAPH_ARGS[@]}" "${@:1:$#-1}")
53+
54+
# In order to redirect the standard input to both commands, it needs to be
55+
# buffered in a temporary file.
56+
if [ "$input_file" = "-" ]; then
57+
tmp_file="$(mktemp --tmpdir "bin-graph-hexdump.XXXXX.bin")"
58+
cat "$input_file" > "$tmp_file"
59+
input_file="$tmp_file"
5260
fi
5361

62+
# Merge the output of the hexdump command with the ANSI-encoded output of
63+
# 'bin-graph'.
64+
#
65+
# FIXME: If the last hexdump line is partial, the output of 'bin-graph' is not
66+
# aligned.
5467
"$PASTE" <("$HEXDUMP" "${HEXDUMP_ARGS[@]}" "$input_file") \
55-
<("$BIN_GRAPH" "${BIN_GRAPH_ARGS[@]}" "$@" '-')
68+
<("$BIN_GRAPH" "${bin_graph_args[@]}" "$input_file" '-')
69+
70+
# We are done with the temporary file, delete it.
71+
if [ -n "$tmp_file" ]; then
72+
rm "$tmp_file"
73+
fi

0 commit comments

Comments
 (0)