6 #include "NetServerTelnet.hpp"
9 NetServerTelnet::NetServerTelnet(
int priority,
size_t stackSize) :
10 mTask(priority,
"TELNET", stackSize, *this) {
14 NetServerTelnet::~NetServerTelnet() {
17 void NetServerTelnet::ansiCursorUp(
int value) {
18 mSocket.printf(
"\x1B[%uA", value);
21 void NetServerTelnet::ansiCursorDown(
int value) {
22 mSocket.printf(
"\x1B[%uB", value);
25 void NetServerTelnet::ansiCursorForward(
int value) {
26 mSocket.printf(
"\x1B[%uC", value);
29 void NetServerTelnet::ansiCursorBack(
int value) {
30 mSocket.printf(
"\x1B[%uD", value);
33 void NetServerTelnet::ansiCursorPosition(
int x,
int y) {
34 mSocket.printf(
"\x1B[%u;%uH", y, x);
37 void NetServerTelnet::ansiEraseData(
int value) {
38 mSocket.printf(
"\x1B[%uJ", value);
41 void NetServerTelnet::ansiEraseInLine(
int value) {
42 mSocket.printf(
"\x1B[%uK", value);
45 void NetServerTelnet::ansiSaveCursorPosition() {
46 mSocket.printf(
"\x1B[s");
49 void NetServerTelnet::ansiRestoreCursorPosition() {
50 mSocket.printf(
"\x1B[u");
53 void NetServerTelnet::ansiHideCursor() {
54 mSocket.printf(
"\x1B[?25l");
57 void NetServerTelnet::ansiShowCursor() {
58 mSocket.printf(
"\x1B[?25h");
61 void NetServerTelnet::iacNegotiate() {
62 const static char IAC_COMMANDS[] = {
63 IAC, WILL, SUPPRESS_GO_AHEAD,
65 IAC, DO, SUPPRESS_GO_AHEAD,
69 mSocket.printf(IAC_COMMANDS);
72 int NetServerTelnet::getLine(
char** line) {
74 unsigned int index = 0;
75 bool isLineTooLong =
false;
77 char* buffer = (
char*) malloc(MAX_LINE_LEN);
79 return RESULT_ERR_MEMORY;
82 while (mSocket.read(&c, 1)) {
83 if ((c >= 0x20) && (c < 0x7F)) {
84 if (index < (MAX_LINE_LEN - 1)) {
92 else if (c ==
'\r' || c ==
'\n') {
93 if (!mIsLineFeedSeq) {
94 mSocket.printf(
"\r\n");
95 mIsLineFeedSeq =
true;
98 return RESULT_ERR_LINE_TOO_LONG;
102 return RESULT_EMPTY_LINE;
105 buffer = (
char*) realloc(buffer, index + 1);
111 else if (c == 0x1B) {
149 mSocket.printf(
"\b \b");
154 mIsLineFeedSeq =
false;
157 return RESULT_ERR_IO;
160 void NetServerTelnet::freeLine(
char* line) {
166 int NetServerTelnet::getArgc(
char* line) {
168 bool isSpaceFound =
false;
178 else if (isSpaceFound) {
180 isSpaceFound =
false;
187 void NetServerTelnet::getArgV(
char* line,
char* argv[]) {
188 bool isSpaceFound =
false;
199 else if (isSpaceFound) {
202 isSpaceFound =
false;
208 void NetServerTelnet::onConnection() {
212 void NetServerTelnet::onDisconnection() {
216 void NetServerTelnet::run() {
218 mIsLineFeedSeq =
false;
224 while (mSocket.isConnected()) {
226 int len = getLine(&line);
227 bool isEntryFound =
false;
229 int argc = getArgc(line);
232 argv = (
char**) malloc(
sizeof(
char*) * argc);
240 for (
const ENTRY* entry = mEntries; entry->name; entry++) {
241 if (strcmp((
const char*) line, entry->name) == 0) {
242 (this->*entry->handler)(argc, argv);
253 mSocket.printf(
"Invalid command\r\n");