interactivity

This commit is contained in:
y4my4my4m 2023-04-29 19:41:09 +09:00
parent 447bbcd16d
commit 81bf25c5ba

View File

@ -1,4 +1,4 @@
// Telnet client for ZealOS by y4my4m 2023
// Telnet client for ZealOS by y4my4m
// Public Domain
#define TELNET_PORT 23
@ -20,9 +20,35 @@ I64 TelnetOpen(U8 *host, U16 port) {
return sock;
}
// U0 GetColorString(I64 color_code, U8 *buf) {
// static U8 *color_strings[] = {
// "$$BLACK$$", "$$RED$$", "$$GREEN$$", "$$YELLOW$$",
// "$$BLUE$$", "$$MAGENTA$$", "$$CYAN$$", "$$WHITE$$"
// };
// if (color_code >= 0 && color_code < 8) {
// StrCopy(buf, color_strings[color_code]);
// } else {
// StrCopy(buf, ""); // Default to empty string if an invalid color code is given
// }
// }
U0 GetColorString(I64 color_code, U8 *buf) {
switch (color_code) {
case 0: StrCopy(buf, "$$BLACK$$"); break;
case 1: StrCopy(buf, "$$RED$$"); break;
case 2: StrCopy(buf, "$$GREEN$$"); break;
case 3: StrCopy(buf, "$$YELLOW$$"); break;
case 4: StrCopy(buf, "$$BLUE$$"); break;
case 5: StrCopy(buf, "$$MAGENTA$$"); break;
case 6: StrCopy(buf, "$$CYAN$$"); break;
case 7: StrCopy(buf, "$$WHITE$$"); break;
default: StrCopy(buf, ""); break; // Default to empty string if an invalid color code is given
}
}
U0 TelnetClient(U8 *host, U16 port) {
I64 sock, bytes_received;
U8 buffer[BUF_SIZE], input_buffer[BUF_SIZE];
I64 sock, bytes_received, input_len;
U8 buffer[BUF_SIZE], input_buffer[BUF_SIZE], *ptr, ch;
sock = TelnetOpen(host, port);
if (sock <= 0) {
@ -35,14 +61,55 @@ U0 TelnetClient(U8 *host, U16 port) {
bytes_received = TCPSocketReceive(sock, buffer, BUF_SIZE - 1);
if (bytes_received > 0) {
buffer[bytes_received] = '\0';
"Received: %s\n", buffer;
// Basic Telnet protocol parser: Ignore negotiation sequences
ptr = buffer;
while (*ptr) {
if (*ptr == 0xFF) {
// Skip Telnet negotiation sequence (3 bytes)
ptr += 3;
} else if (*ptr == 0x1B) {
// ANSI escape sequence
ptr++;
if (*ptr == '[') {
ptr++;
I64 ansi_code = 0;
while (*ptr >= '0' && *ptr <= '9') {
ansi_code = ansi_code * 10 + (*ptr - '0');
ptr++;
}
// Convert ANSI color code to ZealOS color
I64 templeos_color = 7; // Default color
if (ansi_code >= 30 && ansi_code <= 37) {
templeos_color = ansi_code - 30;
}
// GrColor(templeos_color);
}
} else {
// Print the received character
"%c", *ptr;
ptr++;
}
}
"\n";
// Prompt user for input and send it to the remote host
"Enter your choice: ";
StrGet(input_buffer, BUF_SIZE - 1);
input_buffer[StrLen(input_buffer)] = '\r';
input_buffer[StrLen(input_buffer) + 1] = '\n';
TCPSocketSend(sock, input_buffer, StrLen(input_buffer) + 2);
input_len = 0;
while (1) {
ch = CharGet;
if (ch == '\r' || ch == '\n') {
break;
}
input_buffer[input_len++] = ch;
}
input_buffer[input_len] = '\0';
"Sending: %s\n", input_buffer; // Debugging line
input_buffer[input_len++] = '\r';
input_buffer[input_len++] = '\n';
TCPSocketSend(sock, input_buffer, input_len);
} else {
"Error: Connection closed by the remote host.\n";
break;