Comments? In /my/ code? It's more likely than you think.
This commit is contained in:
parent
93fcebb156
commit
cedbee69a2
113
src/nyancat.c
113
src/nyancat.c
@ -4,6 +4,16 @@
|
|||||||
* Developed by: Kevin Lange
|
* Developed by: Kevin Lange
|
||||||
* http://github.com/klange/nyancat
|
* http://github.com/klange/nyancat
|
||||||
*
|
*
|
||||||
|
* 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
|
||||||
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to
|
* of this software and associated documentation files (the "Software"), to
|
||||||
* deal with the Software without restriction, including without limitation the
|
* deal with the Software without restriction, including without limitation the
|
||||||
@ -37,54 +47,95 @@
|
|||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include "telnet.h"
|
#include "telnet.h"
|
||||||
|
|
||||||
|
/* The animation frames are stored separately. */
|
||||||
#include "animation.h"
|
#include "animation.h"
|
||||||
|
|
||||||
|
/* The color palette is not yet set */
|
||||||
char * colors[256] = {NULL};
|
char * colors[256] = {NULL};
|
||||||
|
/*
|
||||||
|
* For most modes, we ouput spaces, but for some
|
||||||
|
* we will use block characters.
|
||||||
|
*/
|
||||||
char * output = " ";
|
char * output = " ";
|
||||||
|
|
||||||
|
/*
|
||||||
|
* These values crop the animation, as we have a full 64x64 stored,
|
||||||
|
* but we only want to display 80x24.
|
||||||
|
*/
|
||||||
#define MIN_ROW 20
|
#define MIN_ROW 20
|
||||||
#define MAX_ROW 44
|
#define MAX_ROW 44
|
||||||
|
|
||||||
#define MIN_COL 10
|
#define MIN_COL 10
|
||||||
#define MAX_COL 50
|
#define MAX_COL 50
|
||||||
|
|
||||||
|
/*
|
||||||
|
* In the standalone mode, we want to handle an interrupt signal
|
||||||
|
* (^C) so that we can restore the cursor.
|
||||||
|
*/
|
||||||
void SIGINT_handler(int sig){
|
void SIGINT_handler(int sig){
|
||||||
printf("\033[?25h");
|
printf("\033[?25h");
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* These are the options we want to use as
|
||||||
|
* a telnet server. These are set in set_options()
|
||||||
|
*/
|
||||||
unsigned char telnet_options[256] = { 0 };
|
unsigned char telnet_options[256] = { 0 };
|
||||||
unsigned char telnet_willack[256] = { 0 };
|
unsigned char telnet_willack[256] = { 0 };
|
||||||
|
|
||||||
|
/*
|
||||||
|
* These are the values we have set or
|
||||||
|
* agreed to during our handshake.
|
||||||
|
* These are set in send_command(...)
|
||||||
|
*/
|
||||||
unsigned char telnet_do_set[256] = { 0 };
|
unsigned char telnet_do_set[256] = { 0 };
|
||||||
unsigned char telnet_will_set[256]= { 0 };
|
unsigned char telnet_will_set[256]= { 0 };
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Set the default options for the telnet server.
|
||||||
|
*/
|
||||||
void set_options() {
|
void set_options() {
|
||||||
|
/* We will not echo input */
|
||||||
telnet_options[ECHO] = WONT;
|
telnet_options[ECHO] = WONT;
|
||||||
|
/* We will set graphics modes */
|
||||||
telnet_options[SGA] = WILL;
|
telnet_options[SGA] = WILL;
|
||||||
|
/* We will not set new environments */
|
||||||
telnet_options[NEW_ENVIRON] = WONT;
|
telnet_options[NEW_ENVIRON] = WONT;
|
||||||
|
|
||||||
|
/* The client should echo its own input */
|
||||||
telnet_willack[ECHO] = DO;
|
telnet_willack[ECHO] = DO;
|
||||||
|
/* The client can set a graphics mode */
|
||||||
telnet_willack[SGA] = DO;
|
telnet_willack[SGA] = DO;
|
||||||
|
/* The client should not change the window size */
|
||||||
telnet_willack[NAWS] = DONT;
|
telnet_willack[NAWS] = DONT;
|
||||||
|
/* The client should tell us its terminal type (very important) */
|
||||||
telnet_willack[TTYPE] = DO;
|
telnet_willack[TTYPE] = DO;
|
||||||
|
/* No linemode */
|
||||||
telnet_willack[LINEMODE] = DONT;
|
telnet_willack[LINEMODE] = DONT;
|
||||||
|
/* And the client can set a new environment */
|
||||||
telnet_willack[NEW_ENVIRON] = DO;
|
telnet_willack[NEW_ENVIRON] = DO;
|
||||||
}
|
}
|
||||||
|
|
||||||
void send_command(int cmd, int opt) {
|
void send_command(int cmd, int opt) {
|
||||||
|
/* Send a command to the telnet client */
|
||||||
if (cmd == DO || cmd == DONT) {
|
if (cmd == DO || cmd == DONT) {
|
||||||
|
/* DO commands say what the client should do. */
|
||||||
if (((cmd == DO) && (telnet_do_set[opt] != DO)) ||
|
if (((cmd == DO) && (telnet_do_set[opt] != DO)) ||
|
||||||
((cmd == DONT) && (telnet_do_set[opt] != DONT))) {
|
((cmd == DONT) && (telnet_do_set[opt] != DONT))) {
|
||||||
|
/* And we only send them if there is a disagreement */
|
||||||
telnet_do_set[opt] = cmd;
|
telnet_do_set[opt] = cmd;
|
||||||
printf("%c%c%c", IAC, cmd, opt);
|
printf("%c%c%c", IAC, cmd, opt);
|
||||||
}
|
}
|
||||||
} else if (cmd == WILL || cmd == WONT) {
|
} else if (cmd == WILL || cmd == WONT) {
|
||||||
|
/* Similarly, WILL commands say what the server will do. */
|
||||||
if (((cmd == WILL) && (telnet_will_set[opt] != WILL)) ||
|
if (((cmd == WILL) && (telnet_will_set[opt] != WILL)) ||
|
||||||
((cmd == WONT) && (telnet_will_set[opt] != WONT))) {
|
((cmd == WONT) && (telnet_will_set[opt] != WONT))) {
|
||||||
|
/* And we only send them during disagreements */
|
||||||
telnet_will_set[opt] = cmd;
|
telnet_will_set[opt] = cmd;
|
||||||
printf("%c%c%c", IAC, cmd, opt);
|
printf("%c%c%c", IAC, cmd, opt);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
/* Other commands are sent raw */
|
||||||
printf("%c%c", IAC, cmd);
|
printf("%c%c", IAC, cmd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -92,17 +143,23 @@ void send_command(int cmd, int opt) {
|
|||||||
|
|
||||||
int main(int argc, char ** argv) {
|
int main(int argc, char ** argv) {
|
||||||
|
|
||||||
|
/* I have a bad habit of being very C99, so this may not be everything */
|
||||||
|
/* The default terminal is ANSI */
|
||||||
|
char term[1024] = {'a','n','s','i'};
|
||||||
int k, ttype;
|
int k, ttype;
|
||||||
uint32_t option = 0, done = 0, sb_mode = 0, do_echo = 0;
|
uint32_t option = 0, done = 0, sb_mode = 0, do_echo = 0;
|
||||||
char term[1024] = {'a','n','s','i'};
|
/* Various pieces for the telnet communication */
|
||||||
char sb[1024] = {0};
|
char sb[1024] = {0};
|
||||||
char sb_len = 0;
|
char sb_len = 0;
|
||||||
|
|
||||||
if (argc > 1) {
|
if (argc > 1) {
|
||||||
if (!strcmp(argv[1], "-t")) {
|
if (!strcmp(argv[1], "-t")) {
|
||||||
|
/* Yes, I know, lame way to get arguments, whatever, we only want one of them. */
|
||||||
|
|
||||||
|
/* Set the default options */
|
||||||
set_options();
|
set_options();
|
||||||
|
|
||||||
|
/* Let the client know what we're using */
|
||||||
for (option = 0; option < 256; option++) {
|
for (option = 0; option < 256; option++) {
|
||||||
if (telnet_options[option]) {
|
if (telnet_options[option]) {
|
||||||
send_command(telnet_options[option], option);
|
send_command(telnet_options[option], option);
|
||||||
@ -116,13 +173,15 @@ int main(int argc, char ** argv) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Negotiate options */
|
||||||
while (!feof(stdin) && !done) {
|
while (!feof(stdin) && !done) {
|
||||||
unsigned char i = getchar();
|
unsigned char i = getchar();
|
||||||
unsigned char opt = 0;
|
unsigned char opt = 0;
|
||||||
if (i == 255) {
|
if (i == IAC) {
|
||||||
i = getchar();
|
i = getchar();
|
||||||
switch (i) {
|
switch (i) {
|
||||||
case SE:
|
case SE:
|
||||||
|
/* End of extended option mode */
|
||||||
sb_mode = 0;
|
sb_mode = 0;
|
||||||
if (sb[0] == TTYPE) {
|
if (sb[0] == TTYPE) {
|
||||||
strcpy(term, &sb[2]);
|
strcpy(term, &sb[2]);
|
||||||
@ -130,11 +189,13 @@ int main(int argc, char ** argv) {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case NOP:
|
case NOP:
|
||||||
|
/* No Op */
|
||||||
send_command(NOP, 0);
|
send_command(NOP, 0);
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
break;
|
break;
|
||||||
case WILL:
|
case WILL:
|
||||||
case WONT:
|
case WONT:
|
||||||
|
/* Will / Won't Negotiation */
|
||||||
opt = getchar();
|
opt = getchar();
|
||||||
if (!telnet_willack[opt]) {
|
if (!telnet_willack[opt]) {
|
||||||
telnet_willack[opt] = WONT;
|
telnet_willack[opt] = WONT;
|
||||||
@ -142,70 +203,82 @@ int main(int argc, char ** argv) {
|
|||||||
send_command(telnet_willack[opt], opt);
|
send_command(telnet_willack[opt], opt);
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
if ((i == WILL) && (opt == TTYPE)) {
|
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);
|
printf("%c%c%c%c%c%c", IAC, SB, TTYPE, SEND, IAC, SE);
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case DO:
|
case DO:
|
||||||
case DONT:
|
case DONT:
|
||||||
|
/* Do / Don't Negotation */
|
||||||
opt = getchar();
|
opt = getchar();
|
||||||
if (!telnet_options[opt]) {
|
if (!telnet_options[opt]) {
|
||||||
telnet_options[opt] = DONT;
|
telnet_options[opt] = DONT;
|
||||||
}
|
}
|
||||||
send_command(telnet_options[opt], opt);
|
send_command(telnet_options[opt], opt);
|
||||||
if (opt == ECHO) {
|
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);
|
do_echo = (i == DO);
|
||||||
}
|
}
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
break;
|
break;
|
||||||
case SB:
|
case SB:
|
||||||
|
/* Begin Extended Option Mode */
|
||||||
sb_mode = 1;
|
sb_mode = 1;
|
||||||
sb_len = 0;
|
sb_len = 0;
|
||||||
memset(sb, 0, 1024);
|
memset(sb, 0, 1024);
|
||||||
break;
|
break;
|
||||||
case IAC:
|
case IAC:
|
||||||
/* Connection Closed */
|
/* Connection Closed During Negotiation */
|
||||||
done = 1;
|
done = 1;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else if (sb_mode) {
|
} else if (sb_mode) {
|
||||||
|
/* Extended Option Mode -> Accept character */
|
||||||
|
if (sb_len < 1023) {
|
||||||
sb[sb_len] = i;
|
sb[sb_len] = i;
|
||||||
sb_len++;
|
sb_len++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
|
/* Otherwise, we were run standalone, find the terminal type from the environement */
|
||||||
char * nterm = getenv("TERM");
|
char * nterm = getenv("TERM");
|
||||||
strcpy(term, nterm);
|
strcpy(term, nterm);
|
||||||
}
|
}
|
||||||
|
|
||||||
ready:
|
ready:
|
||||||
|
|
||||||
|
/* Convert the entire terminal string to lower case */
|
||||||
for (k = 0; k < strlen(term); ++k) {
|
for (k = 0; k < strlen(term); ++k) {
|
||||||
term[k] = tolower(term[k]);
|
term[k] = tolower(term[k]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Do our terminal detection */
|
||||||
if (strstr(term, "xterm")) {
|
if (strstr(term, "xterm")) {
|
||||||
ttype = 1;
|
ttype = 1; /* 256-color, spaces */
|
||||||
} else if (strstr(term, "linux")) {
|
} else if (strstr(term, "linux")) {
|
||||||
ttype = 3;
|
ttype = 3; /* Spaces and blink attribute */
|
||||||
} else if (strstr(term, "vtnt")) {
|
} else if (strstr(term, "vtnt")) {
|
||||||
ttype = 5;
|
ttype = 5; /* Extended ASCII fallback == Windows */
|
||||||
} else if (strstr(term, "cygwin")) {
|
} else if (strstr(term, "cygwin")) {
|
||||||
ttype = 5;
|
ttype = 5; /* Extended ASCII fallback == Windows */
|
||||||
} else if (strstr(term, "vt220")) {
|
} else if (strstr(term, "vt220")) {
|
||||||
ttype = 6;
|
ttype = 6; /* No color support */
|
||||||
} else if (strstr(term, "fallback")) {
|
} else if (strstr(term, "fallback")) {
|
||||||
ttype = 4;
|
ttype = 4; /* Unicode fallback */
|
||||||
} else if (strstr(term, "rxvt")) {
|
} else if (strstr(term, "rxvt")) {
|
||||||
ttype = 3;
|
ttype = 3; /* Accepts LINUX mode */
|
||||||
} else {
|
} else {
|
||||||
ttype = 2;
|
ttype = 2; /* Verything else */
|
||||||
}
|
}
|
||||||
|
|
||||||
int always_escape = 0;
|
int always_escape = 0; /* Used for text mode */
|
||||||
|
/* Accept ^C -> restore cursor */
|
||||||
signal(SIGINT, SIGINT_handler);
|
signal(SIGINT, SIGINT_handler);
|
||||||
switch (ttype) {
|
switch (ttype) {
|
||||||
case 1:
|
case 1:
|
||||||
@ -311,9 +384,11 @@ ready:
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Clear the screen */
|
||||||
printf("\033[H\033[2J\033[?25l");
|
printf("\033[H\033[2J\033[?25l");
|
||||||
|
|
||||||
|
|
||||||
|
/* Display the MOTD */
|
||||||
int countdown_clock = 5;
|
int countdown_clock = 5;
|
||||||
for (k = 0; k < countdown_clock; ++k) {
|
for (k = 0; k < countdown_clock; ++k) {
|
||||||
printf("\n\n\n");
|
printf("\n\n\n");
|
||||||
@ -333,9 +408,10 @@ ready:
|
|||||||
|
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
usleep(400000);
|
usleep(400000);
|
||||||
printf("\033[H");
|
printf("\033[H"); /* Reset cursor */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Clear the screen again */
|
||||||
printf("\033[H\033[2J\033[?25l");
|
printf("\033[H\033[2J\033[?25l");
|
||||||
|
|
||||||
int playing = 1;
|
int playing = 1;
|
||||||
@ -343,27 +419,36 @@ ready:
|
|||||||
char last = 0;
|
char last = 0;
|
||||||
size_t y, x;
|
size_t y, x;
|
||||||
while (playing) {
|
while (playing) {
|
||||||
|
/* Render the frame */
|
||||||
for (y = MIN_ROW; y < MAX_ROW; ++y) {
|
for (y = MIN_ROW; y < MAX_ROW; ++y) {
|
||||||
for (x = MIN_COL; x < MAX_COL; ++x) {
|
for (x = MIN_COL; x < MAX_COL; ++x) {
|
||||||
if (always_escape) {
|
if (always_escape) {
|
||||||
|
/* Text mode (or "Always Send Color Escapse") */
|
||||||
printf("%s", colors[frames[i][y][x]]);
|
printf("%s", colors[frames[i][y][x]]);
|
||||||
} else {
|
} else {
|
||||||
if (frames[i][y][x] != last && colors[frames[i][y][x]]) {
|
if (frames[i][y][x] != last && colors[frames[i][y][x]]) {
|
||||||
|
/* Normal Mode, send escape (because the color changed) */
|
||||||
last = frames[i][y][x];
|
last = frames[i][y][x];
|
||||||
printf("%s%s", colors[frames[i][y][x]], output);
|
printf("%s%s", colors[frames[i][y][x]], output);
|
||||||
} else {
|
} else {
|
||||||
|
/* Same color, just send the output characters */
|
||||||
printf("%s", output);
|
printf("%s", output);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/* End of row, send newline */
|
||||||
if (y != MAX_ROW - 1)
|
if (y != MAX_ROW - 1)
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
/* Update frame crount */
|
||||||
++i;
|
++i;
|
||||||
if (!frames[i]) {
|
if (!frames[i]) {
|
||||||
|
/* Loop animation */
|
||||||
i = 0;
|
i = 0;
|
||||||
}
|
}
|
||||||
|
/* Reset cursor */
|
||||||
printf("\033[H");
|
printf("\033[H");
|
||||||
|
/* Wait */
|
||||||
usleep(90000);
|
usleep(90000);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user