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:
1 parent
88e8d6a83d
commit
40936af68e
6 files changed
+88
-1
No files matched your search
@@ -1,6 +1,7 @@
|
||||
#
|
||||
# sort with: sort -d -u
|
||||
*.dSYM
|
||||
ascii
|
||||
indent
|
||||
indent.c
|
||||
indent.o
|
||||
|
||||
+4
-1
@@ -111,7 +111,7 @@ PROG= prog
|
||||
OBJ= ${PROG}.o
|
||||
CSRC= ${PROG}.o
|
||||
DATA=
|
||||
TARGET= ${PROG}
|
||||
TARGET= ${PROG} ascii
|
||||
#
|
||||
ALT_OBJ=
|
||||
ALT_TARGET=
|
||||
@@ -132,6 +132,9 @@ all: data ${TARGET}
|
||||
${PROG}: ${PROG}.c
|
||||
${CC} ${CFLAGS} -g $< -o $@ ${LDFLAGS}
|
||||
|
||||
ascii: ascii.c
|
||||
${CC} $< -o $@
|
||||
|
||||
# alternative executable
|
||||
#
|
||||
alt: data ${ALT_TARGET}
|
||||
|
||||
@@ -25,10 +25,24 @@ For more detailed information see [2019 endoh in bugs.md](/bugs.md#2019-endoh).
|
||||
|
||||
### Try:
|
||||
|
||||
If you have `gdb(1)` installed:
|
||||
|
||||
```sh
|
||||
./try.sh
|
||||
```
|
||||
|
||||
Note that in macOS installing `gdb(1)` takes multiple steps - it's not just a
|
||||
matter of installing `gdb(1)` and that's that. The [try.sh](try.sh) script has
|
||||
only been tested with linux.
|
||||
|
||||
You might also wish to try:
|
||||
|
||||
```sh
|
||||
gdb ./prog || lldb ./prog
|
||||
```
|
||||
|
||||
and then type in `r` (to run program) and then `bt` (for backtrace).
|
||||
|
||||
|
||||
## Judges' remarks:
|
||||
|
||||
@@ -38,6 +52,7 @@ ascii` when debugging it to reveal its purpose.
|
||||
|
||||
## Author's remarks:
|
||||
|
||||
|
||||
### backtrace quine:
|
||||
|
||||
Compile prog.c with no optimization.
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
* ascii.c - simple program that shows the character value of ASCII values, if
|
||||
* isascii() is true, to help demonstrate IOCCC winner 2019/endoh.
|
||||
*
|
||||
* Will not do anything if no args specified. Use xargs(1) to pass args to
|
||||
* program if it's a lot of characters.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int i;
|
||||
for (argc = 1; argv[argc]; ++argc)
|
||||
{
|
||||
i=atoi(argc[argv]);
|
||||
if (isascii(i)) printf("%c", i);
|
||||
}
|
||||
}
|
||||
Executable
+43
@@ -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
|
||||
@@ -4214,6 +4214,12 @@ As this is a backtrace quine having the optimiser enabled is not a good idea so
|
||||
compilation as debugging symbols might just be useful for an entry that's
|
||||
supposed to segfault :-)
|
||||
|
||||
Cody also added the simple [ascii.c](/2019/endoh/ascii.c) that (while it has an
|
||||
arg) prints out the character of the ASCII value (uses `isascii(3)` first). This
|
||||
combined with the [try.sh](/2019/endoh/try.sh) script that he added allows one
|
||||
to easily reconstruct the source code through GDB by the fact it's a backtrace
|
||||
quine.
|
||||
|
||||
|
||||
## <a name="2019_karns"></a>[2019/karns](/2019/karns/prog.c) ([README.md](/2019/karns/README.md]))
|
||||
|
||||
|
||||
Reference in new issue
Block a user