nyancat-server/src/nyancat.c

640 lines
21 KiB
C
Raw Normal View History

2011-11-30 06:43:24 +00:00
/*
2011-12-01 08:29:45 +00:00
* Copyright (c) 2011 Kevin Lange. All rights reserved.
*
2011-12-05 17:25:53 +00:00
* Developed by: Kevin Lange
* http://github.com/klange/nyancat
* http://miku.acm.uiuc.edu
*
* 40-column support by: Peter Hazenberg
* http://github.com/Peetz0r/nyancat
* http://peter.haas-en-berg.nl
2011-12-01 08:29:45 +00:00
*
2012-02-23 02:53:39 +00:00
* Build tools unifed by: Aaron Peschel
* https://github.com/apeschel
*
* For a complete listing of contributers, please see the git commit history.
*
* This is a simple telnet server / standalone application which renders the
* classic Nyan Cat (or "poptart cat") to your terminal.
*
* It makes use of various ANSI escape sequences to render color, or in the case
* of a VT220, simply dumps text to the screen.
*
* For more information, please see:
*
* http://miku.acm.uiuc.edu
*
2011-12-01 08:29:45 +00:00
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal with the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimers.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimers in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the names of the Association for Computing Machinery, Kevin
* Lange, nor the names of its contributors may be used to endorse
* or promote products derived from this Software without specific prior
* written permission.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* WITH THE SOFTWARE.
2011-11-30 06:43:24 +00:00
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
2011-12-02 06:52:30 +00:00
#include <signal.h>
2011-12-04 07:12:11 +00:00
#include <time.h>
#include <setjmp.h>
2011-12-05 06:10:56 +00:00
#include <sys/ioctl.h>
2011-12-04 10:03:30 +00:00
/*
* telnet.h contains some #defines for the various
* commands, escape characters, and modes for telnet.
* (it surprises some people that telnet is, really,
* a protocol, and not just raw text transmission)
*/
2011-12-04 01:00:34 +00:00
#include "telnet.h"
2011-11-30 06:43:24 +00:00
2011-12-04 10:03:30 +00:00
/*
* The animation frames are stored separately in
* this header so they don't clutter the core source
*/
2011-12-04 01:00:34 +00:00
#include "animation.h"
2011-12-04 10:03:30 +00:00
/*
* Color palette to use for final output
* Specifically, this should be either control sequences
* or raw characters (ie, for vt220 mode)
*/
2011-11-30 06:43:24 +00:00
char * colors[256] = {NULL};
2011-12-04 10:03:30 +00:00
/*
* For most modes, we ouput spaces, but for some
2011-12-04 10:03:30 +00:00
* we will use block characters (or even nothing)
*/
2011-12-04 01:00:34 +00:00
char * output = " ";
2011-11-30 06:43:24 +00:00
2011-12-04 10:03:30 +00:00
/*
* Are we currently in telnet mode?
*/
2011-12-04 01:57:59 +00:00
int telnet = 0;
2011-12-04 10:03:30 +00:00
/*
* Environment to use for setjmp/longjmp
* when breaking out of options handler
*/
jmp_buf environment;
/*
* I refuse to include libm to keep this low
* on external dependencies.
2011-12-04 10:05:52 +00:00
*
* Count the number of digits in a number for
* use with string output.
*/
2011-12-04 10:03:30 +00:00
int digits(int val) {
2011-12-04 07:12:11 +00:00
int d = 1, c;
if (val >= 0) for (c = 10; c <= val; c *= 10) d++;
else for (c = -10 ; c >= val; c *= 10) d++;
2011-12-04 10:03:30 +00:00
return (c < 0) ? ++d : d;
2011-12-04 07:12:11 +00:00
}
/*
* These values crop the animation, as we have a full 64x64 stored,
2011-12-04 10:05:52 +00:00
* but we only want to display 40x24 (double width).
*/
2011-12-04 01:00:34 +00:00
#define MIN_ROW 20
2011-12-04 07:12:11 +00:00
#define MAX_ROW 43
2011-12-04 01:00:34 +00:00
#define MIN_COL 10
#define MAX_COL 50
2011-11-30 06:43:24 +00:00
/*
* In the standalone mode, we want to handle an interrupt signal
* (^C) so that we can restore the cursor and clear the terminal.
*/
2011-12-04 01:00:34 +00:00
void SIGINT_handler(int sig){
printf("\033[?25h\033[0m\033[H\033[2J");
2011-12-04 01:00:34 +00:00
exit(0);
}
2011-11-30 06:43:24 +00:00
/*
* Handle the alarm which breaks us off of options
* handling if we didn't receive a terminal
*/
void SIGALRM_handler(int sig) {
alarm(0);
longjmp(environment, 1);
/* Unreachable */
}
2011-12-04 10:03:30 +00:00
/*
* Telnet requires us to send a specific sequence
* for a line break (\r\000\n), so let's make it happy.
*/
2011-12-04 01:57:59 +00:00
void newline(int n) {
int i = 0;
for (i = 0; i < n; ++i) {
2011-12-04 10:03:30 +00:00
/* We will send `n` linefeeds to the client */
if (telnet) {
/* Send the telnet newline sequence */
putc('\r', stdout);
putc(0, stdout);
putc('\n', stdout);
} else {
/* Send a regular line feed */
putc('\n', stdout);
}
2011-12-04 01:57:59 +00:00
}
}
/*
* These are the options we want to use as
* a telnet server. These are set in set_options()
*/
2011-12-04 01:00:34 +00:00
unsigned char telnet_options[256] = { 0 };
unsigned char telnet_willack[256] = { 0 };
2011-11-30 06:43:24 +00:00
/*
* These are the values we have set or
* agreed to during our handshake.
* These are set in send_command(...)
*/
2011-12-04 01:00:34 +00:00
unsigned char telnet_do_set[256] = { 0 };
unsigned char telnet_will_set[256]= { 0 };
2011-11-30 06:43:24 +00:00
/*
* Set the default options for the telnet server.
*/
2011-12-04 01:00:34 +00:00
void set_options() {
/* We will not echo input */
2011-12-04 01:00:34 +00:00
telnet_options[ECHO] = WONT;
/* We will set graphics modes */
2011-12-04 01:00:34 +00:00
telnet_options[SGA] = WILL;
/* We will not set new environments */
2011-12-04 01:00:34 +00:00
telnet_options[NEW_ENVIRON] = WONT;
2011-11-30 06:43:24 +00:00
/* The client should echo its own input */
2011-12-04 01:00:34 +00:00
telnet_willack[ECHO] = DO;
/* The client can set a graphics mode */
2011-12-04 01:00:34 +00:00
telnet_willack[SGA] = DO;
/* The client should not change, but it should tell us its window size */
telnet_willack[NAWS] = DO;
/* The client should tell us its terminal type (very important) */
2011-12-04 01:00:34 +00:00
telnet_willack[TTYPE] = DO;
/* No linemode */
2011-12-04 01:00:34 +00:00
telnet_willack[LINEMODE] = DONT;
/* And the client can set a new environment */
2011-12-04 01:00:34 +00:00
telnet_willack[NEW_ENVIRON] = DO;
}
2011-11-30 06:43:24 +00:00
2011-12-04 10:03:30 +00:00
/*
* Send a command (cmd) to the telnet client
* Also does special handling for DO/DONT/WILL/WONT
*/
2011-12-04 01:00:34 +00:00
void send_command(int cmd, int opt) {
/* Send a command to the telnet client */
2011-12-04 01:00:34 +00:00
if (cmd == DO || cmd == DONT) {
/* DO commands say what the client should do. */
2011-12-04 01:00:34 +00:00
if (((cmd == DO) && (telnet_do_set[opt] != DO)) ||
((cmd == DONT) && (telnet_do_set[opt] != DONT))) {
/* And we only send them if there is a disagreement */
2011-12-04 01:00:34 +00:00
telnet_do_set[opt] = cmd;
printf("%c%c%c", IAC, cmd, opt);
}
} else if (cmd == WILL || cmd == WONT) {
/* Similarly, WILL commands say what the server will do. */
2011-12-04 01:00:34 +00:00
if (((cmd == WILL) && (telnet_will_set[opt] != WILL)) ||
((cmd == WONT) && (telnet_will_set[opt] != WONT))) {
/* And we only send them during disagreements */
2011-12-04 01:00:34 +00:00
telnet_will_set[opt] = cmd;
printf("%c%c%c", IAC, cmd, opt);
}
} else {
/* Other commands are sent raw */
2011-12-04 01:00:34 +00:00
printf("%c%c", IAC, cmd);
}
}
2011-11-30 06:43:24 +00:00
2011-12-04 01:00:34 +00:00
int main(int argc, char ** argv) {
2011-11-30 06:43:24 +00:00
/* I have a bad habit of being very C99, so this may not be everything */
/* The default terminal is ANSI */
2011-12-04 10:05:52 +00:00
char term[1024] = {'a','n','s','i', 0};
2011-12-05 06:10:56 +00:00
int terminal_width = 80;
2011-12-04 01:00:34 +00:00
int k, ttype;
uint32_t option = 0, done = 0, sb_mode = 0, do_echo = 0;
/* Various pieces for the telnet communication */
2011-12-04 01:00:34 +00:00
char sb[1024] = {0};
char sb_len = 0;
2011-11-30 06:43:24 +00:00
2011-12-04 01:00:34 +00:00
if (argc > 1) {
if (!strcmp(argv[1], "-t")) {
/* Yes, I know, lame way to get arguments, whatever, we only want one of them. */
2011-12-04 01:57:59 +00:00
telnet = 1;
2011-11-30 06:43:24 +00:00
/* Set the default options */
2011-12-04 01:00:34 +00:00
set_options();
2011-11-30 06:43:24 +00:00
/* Let the client know what we're using */
2011-12-04 01:00:34 +00:00
for (option = 0; option < 256; option++) {
if (telnet_options[option]) {
send_command(telnet_options[option], option);
fflush(stdout);
}
}
for (option = 0; option < 256; option++) {
if (telnet_willack[option]) {
send_command(telnet_willack[option], option);
fflush(stdout);
}
}
/* Set the alarm handler to execute the longjmp */
signal(SIGALRM, SIGALRM_handler);
/* Negotiate options */
if (!setjmp(environment)) {
/* We will stop handling options after one second */
alarm(1);
/* Let's do this */
while (!feof(stdin) && done < 2) {
/* Get either IAC (start command) or a regular character (break, unless in SB mode) */
unsigned char i = getchar();
unsigned char opt = 0;
if (i == IAC) {
/* If IAC, get the command */
i = getchar();
switch (i) {
case SE:
/* End of extended option mode */
sb_mode = 0;
if (sb[0] == TTYPE) {
2011-12-04 10:03:30 +00:00
/* This was a response to the TTYPE command, meaning
* that this should be a terminal type */
alarm(0);
strcpy(term, &sb[2]);
done++;
}
else if (sb[0] == NAWS) {
/* This was a response to the NAWS command, meaning
* that this should be a window size */
alarm(0);
terminal_width = sb[2];
done++;
}
break;
case NOP:
/* No Op */
send_command(NOP, 0);
2011-12-04 01:00:34 +00:00
fflush(stdout);
break;
case WILL:
case WONT:
/* Will / Won't Negotiation */
opt = getchar();
if (!telnet_willack[opt]) {
2011-12-04 10:03:30 +00:00
/* We default to WONT */
telnet_willack[opt] = WONT;
}
send_command(telnet_willack[opt], opt);
fflush(stdout);
if ((i == WILL) && (opt == TTYPE)) {
/* WILL TTYPE? Great, let's do that now! */
printf("%c%c%c%c%c%c", IAC, SB, TTYPE, SEND, IAC, SE);
fflush(stdout);
}
break;
case DO:
case DONT:
/* Do / Don't Negotation */
opt = getchar();
if (!telnet_options[opt]) {
2011-12-04 10:03:30 +00:00
/* We default to DONT */
telnet_options[opt] = DONT;
}
send_command(telnet_options[opt], opt);
if (opt == ECHO) {
/* We don't really need this, as we don't accept input, but,
* in case we do in the future, set our echo mode */
do_echo = (i == DO);
}
fflush(stdout);
break;
case SB:
/* Begin Extended Option Mode */
sb_mode = 1;
sb_len = 0;
memset(sb, 0, 1024);
break;
case IAC:
2011-12-04 10:03:30 +00:00
/* IAC IAC? That's probably not right. */
done = 2;
break;
default:
break;
}
} else if (sb_mode) {
/* Extended Option Mode -> Accept character */
if (sb_len < 1023) {
2011-12-04 10:03:30 +00:00
/* Append this character to the SB string,
* but only if it doesn't put us over
* our limit; honestly, we shouldn't hit
* the limit, as we're only collecting characters
* for a terminal type or window size, but better safe than
2011-12-04 10:03:30 +00:00
* sorry (and vulernable).
*/
sb[sb_len] = i;
sb_len++;
}
}
2011-12-04 01:00:34 +00:00
}
}
}
} else {
2011-12-04 10:03:30 +00:00
/* We are running standalone, retrieve the
* terminal type from the environment. */
2011-12-04 01:00:34 +00:00
char * nterm = getenv("TERM");
if (nterm)
strcpy(term, nterm);
2011-12-05 06:10:56 +00:00
/* Also get the number of columns */
2011-12-05 06:10:56 +00:00
struct winsize w;
ioctl(0, TIOCGWINSZ, &w);
terminal_width = w.ws_col;
2011-12-04 01:00:34 +00:00
}
2011-11-30 06:43:24 +00:00
/* Convert the entire terminal string to lower case */
2011-12-04 01:00:34 +00:00
for (k = 0; k < strlen(term); ++k) {
term[k] = tolower(term[k]);
}
/* We don't want terminals wider than 80 columns */
if(terminal_width > 80) terminal_width = 80;
2011-12-04 01:00:34 +00:00
/* Do our terminal detection */
2011-12-04 01:00:34 +00:00
if (strstr(term, "xterm")) {
ttype = 1; /* 256-color, spaces */
2011-12-04 01:00:34 +00:00
} else if (strstr(term, "linux")) {
ttype = 3; /* Spaces and blink attribute */
2011-12-04 01:00:34 +00:00
} else if (strstr(term, "vtnt")) {
ttype = 5; /* Extended ASCII fallback == Windows */
2011-12-04 01:00:34 +00:00
} else if (strstr(term, "cygwin")) {
ttype = 5; /* Extended ASCII fallback == Windows */
2011-12-04 01:00:34 +00:00
} else if (strstr(term, "vt220")) {
ttype = 6; /* No color support */
2011-12-04 01:00:34 +00:00
} else if (strstr(term, "fallback")) {
ttype = 4; /* Unicode fallback */
2011-12-04 01:00:34 +00:00
} else if (strstr(term, "rxvt")) {
ttype = 3; /* Accepts LINUX mode */
2011-12-05 06:10:56 +00:00
} else if (strstr(term, "vt100") && terminal_width == 40) {
ttype = 7; /* No color support, only 40 columns */
2011-12-04 01:00:34 +00:00
} else {
2011-12-04 23:01:18 +00:00
ttype = 2; /* Everything else */
2011-12-04 01:00:34 +00:00
}
int always_escape = 0; /* Used for text mode */
/* Accept ^C -> restore cursor */
signal(SIGINT, SIGINT_handler);
2011-12-04 01:00:34 +00:00
switch (ttype) {
case 1:
colors[','] = "\033[48;5;17m"; /* Blue background */
colors['.'] = "\033[48;5;15m"; /* White stars */
colors['\''] = "\033[48;5;0m"; /* Black border */
colors['@'] = "\033[48;5;230m"; /* Tan poptart */
colors['$'] = "\033[48;5;175m"; /* Pink poptart */
colors['-'] = "\033[48;5;162m"; /* Red poptart */
colors['>'] = "\033[48;5;9m"; /* Red rainbow */
colors['&'] = "\033[48;5;202m"; /* Orange rainbow */
colors['+'] = "\033[48;5;11m"; /* Yellow Rainbow */
colors['#'] = "\033[48;5;10m"; /* Green rainbow */
colors['='] = "\033[48;5;33m"; /* Light blue rainbow */
colors[';'] = "\033[48;5;19m"; /* Dark blue rainbow */
colors['*'] = "\033[48;5;8m"; /* Gray cat face */
colors['%'] = "\033[48;5;175m"; /* Pink cheeks */
break;
case 2:
colors[','] = "\033[104m"; /* Blue background */
colors['.'] = "\033[107m"; /* White stars */
colors['\''] = "\033[40m"; /* Black border */
colors['@'] = "\033[47m"; /* Tan poptart */
colors['$'] = "\033[105m"; /* Pink poptart */
colors['-'] = "\033[101m"; /* Red poptart */
colors['>'] = "\033[101m"; /* Red rainbow */
colors['&'] = "\033[43m"; /* Orange rainbow */
colors['+'] = "\033[103m"; /* Yellow Rainbow */
colors['#'] = "\033[102m"; /* Green rainbow */
colors['='] = "\033[104m"; /* Light blue rainbow */
colors[';'] = "\033[44m"; /* Dark blue rainbow */
colors['*'] = "\033[100m"; /* Gray cat face */
colors['%'] = "\033[105m"; /* Pink cheeks */
break;
case 3:
colors[','] = "\033[25;44m"; /* Blue background */
colors['.'] = "\033[5;47m"; /* White stars */
colors['\''] = "\033[25;40m"; /* Black border */
colors['@'] = "\033[5;47m"; /* Tan poptart */
colors['$'] = "\033[5;45m"; /* Pink poptart */
colors['-'] = "\033[5;41m"; /* Red poptart */
colors['>'] = "\033[5;41m"; /* Red rainbow */
colors['&'] = "\033[25;43m"; /* Orange rainbow */
colors['+'] = "\033[5;43m"; /* Yellow Rainbow */
colors['#'] = "\033[5;42m"; /* Green rainbow */
colors['='] = "\033[25;44m"; /* Light blue rainbow */
colors[';'] = "\033[5;44m"; /* Dark blue rainbow */
colors['*'] = "\033[5;40m"; /* Gray cat face */
colors['%'] = "\033[5;45m"; /* Pink cheeks */
break;
case 4:
colors[','] = "\033[0;34;44m"; /* Blue background */
colors['.'] = "\033[1;37;47m"; /* White stars */
colors['\''] = "\033[0;30;40m"; /* Black border */
colors['@'] = "\033[1;37;47m"; /* Tan poptart */
colors['$'] = "\033[1;35;45m"; /* Pink poptart */
colors['-'] = "\033[1;31;41m"; /* Red poptart */
colors['>'] = "\033[1;31;41m"; /* Red rainbow */
colors['&'] = "\033[0;33;43m"; /* Orange rainbow */
colors['+'] = "\033[1;33;43m"; /* Yellow Rainbow */
colors['#'] = "\033[1;32;42m"; /* Green rainbow */
colors['='] = "\033[1;34;44m"; /* Light blue rainbow */
colors[';'] = "\033[0;34;44m"; /* Dark blue rainbow */
colors['*'] = "\033[1;30;40m"; /* Gray cat face */
colors['%'] = "\033[1;35;45m"; /* Pink cheeks */
2011-12-04 01:00:34 +00:00
output = "██";
break;
case 5:
colors[','] = "\033[0;34;44m"; /* Blue background */
colors['.'] = "\033[1;37;47m"; /* White stars */
colors['\''] = "\033[0;30;40m"; /* Black border */
colors['@'] = "\033[1;37;47m"; /* Tan poptart */
colors['$'] = "\033[1;35;45m"; /* Pink poptart */
colors['-'] = "\033[1;31;41m"; /* Red poptart */
colors['>'] = "\033[1;31;41m"; /* Red rainbow */
colors['&'] = "\033[0;33;43m"; /* Orange rainbow */
colors['+'] = "\033[1;33;43m"; /* Yellow Rainbow */
colors['#'] = "\033[1;32;42m"; /* Green rainbow */
colors['='] = "\033[1;34;44m"; /* Light blue rainbow */
colors[';'] = "\033[0;34;44m"; /* Dark blue rainbow */
colors['*'] = "\033[1;30;40m"; /* Gray cat face */
colors['%'] = "\033[1;35;45m"; /* Pink cheeks */
2011-12-04 01:00:34 +00:00
output = "\333\333";
break;
case 6:
colors[','] = "::"; /* Blue background */
colors['.'] = "@@"; /* White stars */
colors['\''] = " "; /* Black border */
colors['@'] = "##"; /* Tan poptart */
colors['$'] = "??"; /* Pink poptart */
colors['-'] = "<>"; /* Red poptart */
colors['>'] = "##"; /* Red rainbow */
colors['&'] = "=="; /* Orange rainbow */
colors['+'] = "--"; /* Yellow Rainbow */
colors['#'] = "++"; /* Green rainbow */
colors['='] = "~~"; /* Light blue rainbow */
colors[';'] = "$$"; /* Dark blue rainbow */
colors['*'] = ";;"; /* Gray cat face */
colors['%'] = "()"; /* Pink cheeks */
2011-12-04 01:00:34 +00:00
always_escape = 1;
break;
2011-12-05 06:10:56 +00:00
case 7:
colors[','] = "."; /* Blue background */
colors['.'] = "@"; /* White stars */
colors['\''] = " "; /* Black border */
colors['@'] = "#"; /* Tan poptart */
colors['$'] = "?"; /* Pink poptart */
colors['-'] = "O"; /* Red poptart */
colors['>'] = "#"; /* Red rainbow */
colors['&'] = "="; /* Orange rainbow */
colors['+'] = "-"; /* Yellow Rainbow */
colors['#'] = "+"; /* Green rainbow */
colors['='] = "~"; /* Light blue rainbow */
colors[';'] = "$"; /* Dark blue rainbow */
colors['*'] = ";"; /* Gray cat face */
colors['%'] = "o"; /* Pink cheeks */
always_escape = 1;
terminal_width = 40;
break;
2011-12-04 01:00:34 +00:00
default:
break;
2011-11-30 07:13:20 +00:00
}
2011-11-30 06:43:24 +00:00
/* Attempt to set terminal title */
printf("\033kNyanyanyanyanyanyanya...\033\134");
printf("\033]1;Nyanyanyanyanyanyanya...\007");
printf("\033]2;Nyanyanyanyanyanyanya...\007");
/* Clear the screen */
2011-11-30 23:24:56 +00:00
printf("\033[H\033[2J\033[?25l");
2011-11-30 06:43:24 +00:00
/* Display the MOTD */
2011-12-04 01:00:34 +00:00
int countdown_clock = 5;
2011-12-02 01:15:38 +00:00
for (k = 0; k < countdown_clock; ++k) {
2011-12-04 01:57:59 +00:00
newline(3);
2011-12-04 10:16:31 +00:00
printf(" \033[1mNyancat Telnet Server\033[0m");
2011-12-04 01:57:59 +00:00
newline(2);
2011-12-04 10:16:31 +00:00
printf(" written and run by \033[1;32mKevin Lange\033[1;34m @kevinlange\033[0m");
2011-12-04 01:57:59 +00:00
newline(2);
2011-12-04 10:16:31 +00:00
printf(" If things don't look right, try:");
2011-12-04 01:57:59 +00:00
newline(1);
2011-12-04 10:16:31 +00:00
printf(" TERM=fallback telnet ...");
2011-12-04 01:57:59 +00:00
newline(2);
2011-12-04 10:16:31 +00:00
printf(" Or on Windows:");
2011-12-04 01:57:59 +00:00
newline(1);
2011-12-04 10:16:31 +00:00
printf(" telnet -t vtnt ...");
2011-12-04 18:30:24 +00:00
newline(2);
2011-12-04 10:16:31 +00:00
printf(" Problems? I am also a webserver:");
newline(1);
printf(" \033[1;34mhttp://miku.acm.uiuc.edu\033[0m");
2011-12-04 18:30:24 +00:00
newline(2);
printf(" This is a telnet server, remember your escape keys!");
newline(1);
printf(" \033[1;31m^]quit\033[0m to exit");
newline(2);
2011-12-04 10:16:31 +00:00
printf(" Starting in %d... \n", countdown_clock-k);
2011-12-02 01:15:38 +00:00
2011-12-04 01:00:34 +00:00
fflush(stdout);
usleep(400000);
printf("\033[H"); /* Reset cursor */
2011-12-02 01:15:38 +00:00
}
/* Clear the screen again */
2011-12-02 01:15:38 +00:00
printf("\033[H\033[2J\033[?25l");
2011-12-04 07:12:11 +00:00
/* Store the start time */
time_t start, current;
time(&start);
2011-12-04 10:03:30 +00:00
int playing = 1; /* Animation should continue [left here for modifications] */
size_t i = 0; /* Current frame # */
char last = 0; /* Last color index rendered */
size_t y, x; /* x/y coordinates of what we're drawing */
2011-11-30 06:43:24 +00:00
while (playing) {
/* Render the frame */
2011-11-30 06:43:24 +00:00
for (y = MIN_ROW; y < MAX_ROW; ++y) {
for (x = MIN_COL; x < MAX_COL; ++x) {
2011-12-02 01:15:38 +00:00
if (always_escape) {
/* Text mode (or "Always Send Color Escapse") */
2011-12-02 01:15:38 +00:00
printf("%s", colors[frames[i][y][x]]);
2011-11-30 06:43:24 +00:00
} else {
2011-12-02 01:15:38 +00:00
if (frames[i][y][x] != last && colors[frames[i][y][x]]) {
/* Normal Mode, send escape (because the color changed) */
2011-12-02 01:15:38 +00:00
last = frames[i][y][x];
printf("%s%s", colors[frames[i][y][x]], output);
} else {
/* Same color, just send the output characters */
2011-12-04 01:00:34 +00:00
printf("%s", output);
2011-12-02 01:15:38 +00:00
}
2011-11-30 06:43:24 +00:00
}
}
/* End of row, send newline */
2011-12-04 07:12:11 +00:00
newline(1);
}
2011-12-04 10:03:30 +00:00
/* Get the current time for the "You have nyaned..." string */
2011-12-04 07:12:11 +00:00
time(&current);
double diff = difftime(current, start);
2011-12-04 10:03:30 +00:00
/* Now count the length of the time difference so we can center */
2011-12-04 20:06:35 +00:00
int nLen = digits((int)diff);
2011-12-05 06:10:56 +00:00
int width = (terminal_width - 29 - nLen) / 2;
2011-12-04 10:03:30 +00:00
/* Spit out some spaces so that we're actually centered */
2011-12-04 20:06:35 +00:00
while (width > 0) {
2011-12-04 07:12:11 +00:00
printf(" ");
width--;
2011-11-30 06:43:24 +00:00
}
2011-12-04 10:03:30 +00:00
/* You have nyaned for [n] seconds!
* The \033[J ensures that the rest of the line has the dark blue
2011-12-05 06:10:56 +00:00
* background, and the \033[1;37m ensures that our text is bright white.
* The \033[0m prevents the Apple ][ from flipping everything, but
* makes the whole nyancat less bright on the vt220
2011-12-04 10:03:30 +00:00
*/
2011-12-05 06:10:56 +00:00
printf("\033[1;37mYou have nyaned for %0.0f seconds!\033[J\033[0m", diff);
2011-12-04 10:03:30 +00:00
/* Reset the last color so that the escape sequences rewrite */
2011-12-04 07:19:16 +00:00
last = 0;
/* Update frame crount */
2011-11-30 06:43:24 +00:00
++i;
if (!frames[i]) {
/* Loop animation */
2011-11-30 06:43:24 +00:00
i = 0;
}
/* Reset cursor */
2011-11-30 06:43:24 +00:00
printf("\033[H");
/* Wait */
2011-11-30 06:43:24 +00:00
usleep(90000);
}
return 0;
}