Console work + modem.c

This commit is contained in:
Neale Pickett 2012-05-29 22:45:20 -06:00
parent 63887f7c53
commit 24df97d2f7
3 changed files with 107 additions and 21 deletions

View File

@ -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,15 +65,40 @@ elif ! [ -d $BASE/$hash ]; then
fini
fi
clear
read -r name < $BASE/$hash/.name
printf "%s answer: " "$name"
printf 'Welcome back, %s.\n' "$name"
while true; do
printf "p2> "
read -r answer
echo
case "$answer" in
\?|help)
cat <<EOD
 Help
----------------------------------------------------------
Type "quit" to leave the p2 shell.
Any other string is checked as an answer. If the answer
is correct, you are awarded points and the scoreboard will
update within 10 seconds. Check the puzzles overview to
see if your answer unlocked a new puzzle.
EOD
continue
;;
quit)
break
;;
esac
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
echo 'That is not a correct answer. Type "help" for help.'
continue
fi
cat=${match% *}
@ -86,6 +112,5 @@ else
# run puzzles.cgi
# update scoreboard
fi
fini
done

1
packages/p2/src/Makefile Normal file
View File

@ -0,0 +1 @@
modem:

60
packages/p2/src/modem.c Normal file
View File

@ -0,0 +1,60 @@
#include <unistd.h>
#include <stdlib.h>
#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;
}