Add 2019/endoh/try.sh, helper program + formatting

The helper program ascii.c converts args passed to it to char values (if
isascii() returns non-zero) which with try.sh allows one to reconstruct
the code with the backtrace rather than having to memorise ascii(7) (and
even if they did it would be hard to put it together). One should of
course use xargs(1) and try.sh does.

The try.sh script checks that gdb is installed first, telling the user
to install it if they don't have it. The README.md file notes that for
macOS It's not a simple matter of just installing gdb - there are
additional steps.
This commit is contained in:
Cody Boone Ferguson committed 2023-12-18 10:06:39 -08:00
1 parent 88e8d6a83d
commit 40936af68e
6 files changed
+88 -1

No files matched your search

+43
View File
@@ -0,0 +1,43 @@
#!/usr/bin/env bash
#
# try.sh - demonstrate IOCCC winner 2019/endoh
#
# make sure CC is set so that when we do make CC="$CC" it isn't empty. Doing it
# this way allows us to have the user specify a different compiler in an easy
# way.
if [[ -z "$CC" ]]; then
CC="cc"
fi
# Before we do anything we have to make sure we have GDB available
#
GDB="$(type -P gdb)"
if [[ -z "$GDB" ]]; then
echo "No gdb found, install gdb and try again." 1>&2
exit 1
fi
make CC="$CC" all >/dev/null || exit 1
# clear screen after compilation so that only the entry is shown
clear
echo "$ $GDB -q -x prog.c ./prog | head -n 25" 1>&2
read -r -n 1 -p "Press any key to use source code as GDB command file (first 25 lines): "
echo >&2
"$GDB" -q -x prog.c ./prog | head -n 25
echo "$ $GDB -q -x prog.c ./prog | cat | grep : | cut -f 2 -d:|xargs ./ascii"
read -r -n 1 -p "Press any key to use the backtrace to reconstruct prog.c (space = next page, q = quit): "
echo 1>&2
"$GDB" -q -x prog.c ./prog | cat | grep : | cut -f 2 -d: | xargs ./ascii | less -rEXF
echo 1>&2
read -r -n 1 -p "Press any key to compare reconstructed code with original: "
echo 1>&2
echo "$ "$GDB" -q -x prog.c ./prog | cat | grep : | cut -f 2 -d: | xargs ./ascii > prog2.c" 1>&2
"$GDB" -q -x prog.c ./prog | cat | grep : | cut -f 2 -d: | xargs ./ascii > prog2.c
echo "$ diff -s prog.c prog2.c" 1>&2
diff -s prog.c prog2.c