From 4671a56f593f608e10cca39552923078743a9709 Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Tue, 29 May 2012 22:45:20 -0600 Subject: [PATCH] Console work + modem.c --- packages/p2/bin/p2console | 67 +++++++++++++++++++++++++++------------ packages/p2/src/Makefile | 1 + packages/p2/src/modem.c | 60 +++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 21 deletions(-) create mode 100644 packages/p2/src/Makefile create mode 100644 packages/p2/src/modem.c diff --git a/packages/p2/bin/p2console b/packages/p2/bin/p2console index 6cf4d9b..f4d5a8d 100755 --- a/packages/p2/bin/p2console +++ b/packages/p2/bin/p2console @@ -8,6 +8,7 @@ if ! [ -f $BASE/salt ]; then dd if=/dev/urandom count=1 | md5sum - > $BASE/salt fi clear +printf '\017' read salt < $BASE/salt esc () { @@ -64,28 +65,52 @@ elif ! [ -d $BASE/$hash ]; then fini fi +clear + read -r name < $BASE/$hash/.name -printf "%s answer: " "$name" -read -r answer -echo -match=$(awk -v ans="$answer" '(substr($0, length($1)+2) == ans) { print substr(FILENAME, 6, length(FILENAME)-17) " " $1; }' /opt/*/answers.txt) -if [ -z "$match" ]; then - echo "That is not a correct answer." - fini -fi +printf 'Welcome back, %s.\n' "$name" + +while true; do + printf "p2> " + read -r answer + + case "$answer" in + \?|help) + cat < $fn - echo "You get $points more points in the $cat category." - # run puzzles.cgi - # update scoreboard -fi - -fini + cat=${match% *} + points=${match#* } + fn=$BASE/$hash/$cat.$points + if [ -f $fn ]; then + echo "You've already received points for this answer." + else + > $fn + echo "You get $points more points in the $cat category." + # run puzzles.cgi + # update scoreboard + fi +done diff --git a/packages/p2/src/Makefile b/packages/p2/src/Makefile new file mode 100644 index 0000000..3ca13ff --- /dev/null +++ b/packages/p2/src/Makefile @@ -0,0 +1 @@ +modem: diff --git a/packages/p2/src/modem.c b/packages/p2/src/modem.c new file mode 100644 index 0000000..e8a74f5 --- /dev/null +++ b/packages/p2/src/modem.c @@ -0,0 +1,60 @@ +#include +#include + +#define NOISE_PROB 300 +#define NOISE_BITS 16 + +int badbits = 0; + +char +line_noise(char c) +{ + int i = 7; + + while (badbits && (i >= 0)) { + c = c ^ ((rand() % 2) << i); + badbits -= 1; + i -= 1; + } + + if (rand() % NOISE_PROB == 0) { + badbits = rand() % NOISE_BITS; + } + + return c; +} + +int +main(int argc, char *argv[]) +{ + char c; + ssize_t ret; + int baud = 0; + useconds_t usec; + + if (argv[1]) { + baud = atoi(argv[1]); + } + if (! baud) { + baud = 1200; + } + + srandom(getpid()); + + /* + N81 uses 1 stop bit, and 1 parity bit. That works out to + exactly 10 bits per byte. + */ + usec = 10000000 / baud; + + while (1) { + ret = read(0, &c, 1); + if (ret != 1) { + break; + } + c = line_noise(c); + write(1, &c, 1); + usleep(usec); + } + return 0; +}