// SPDX-License-Identifier: GPL-3.0-or-later // Receives the same byte stream that the stock NitrOS-9 telnet client writes. #include "DriveWireTerminal.h" #include #include #include namespace { using Bytes = std::vector; using Grid = VCC::Mac::TerminalGrid; struct CoWin { Grid lines; int x = 0, y = 0, clears = 0, chimes = 0; CoWin() { for (auto& line : lines) line.fill(' '); } std::string line(int row) const { return std::string(lines[row].begin(), lines[row].end()); } void lf() { if (y < 23) ++y; else { std::move(lines.begin()+1, lines.end(), lines.begin()); lines.back().fill(' '); } } void apply(const Bytes& bytes) { for (std::size_t i = 0; i < bytes.size(); ++i) { switch (int ch = bytes[i]) { case 2: x = bytes.at(++i)-32; y = bytes.at(++i)-32; assert(x >= 0 && x < 80 && y >= 0 && y < 24); break; case 4: std::fill(lines[y].begin()+x, lines[y].end(), ' '); break; case 5: assert(bytes.at(++i) == 0x21); break; case 6: if (x < 79) ++x; break; case 7: ++chimes; break; case 8: if (x > 0) --x; break; case 9: if (y > 0) --y; break; case 10: lf(); break; case 11: std::fill(lines[y].begin()+x, lines[y].end(), ' '); for (int row=y+1; row<24; ++row) lines[row].fill(' '); break; case 12: for (auto& row : lines) row.fill(' '); x=y=0; ++clears; break; case 13: x=0; break; case 0x1f: assert(bytes.at(++i) == 0x30); std::move_backward(lines.begin()+y, lines.end()-1, lines.end()); lines[y].fill(' '); break; default: assert(ch >= 32 && ch <= 126); lines[y][x++] = static_cast(ch); if (x == 80) { x=0; lf(); } } } } }; struct Terminal { VCC::Mac::DriveWireTerminal host; CoWin guest; Bytes send(std::string_view source) { auto bytes = host.consume(reinterpret_cast(source.data()), source.size()); guest.apply(bytes); return bytes; } void fragmented(std::string_view source) { for (char ch : source) send(std::string_view(&ch,1)); } }; void transcript() { Terminal t; t.guest.y = 8; t.guest.lines[3][0] = 'G'; t.fragmented("Mac\x1b[31m shell\x1b[0m\r\n\t> "); assert(t.guest.clears == 0 && t.guest.lines[3][0]=='G'); assert(t.guest.line(8).substr(0,9)=="Mac shell" && t.guest.line(9).substr(8,2)=="> "); t.fragmented("abc\b\b\x1b[Kx"); assert(t.guest.line(9).substr(8,5)=="> ax "); t.fragmented("\x1b]0;private \xe2\x94\x9c title\x1b\\\x1bPignored\x1b\\\x1b[?2004h"); t.fragmented("\xe2\x9e\x9c"); assert(t.guest.line(9)[12]=='?'); assert(t.send("\x1b[?25l").empty()); t.fragmented("\x1b[?1h"); assert(t.host.applicationCursor()); t.fragmented("\x1b[?1l"); assert(!t.host.applicationCursor()); } void editing() { Terminal t; t.fragmented("\x1b[2J\x1b[Habcde\x1b[1;2H\x1b[2@XY"); assert(t.guest.line(0).substr(0,7)=="aXYbcde"); t.send("\x1b[2P\x1b[X"); assert(t.guest.line(0).substr(0,5)=="aXY e"); t.send("\x1b[4h!\x1b[4l"); assert(t.guest.line(0).substr(0,6)=="aXY! e"); t.send("\x1b[1K"); assert(t.guest.line(0).substr(0,6)==" e"); t.send("\x1b[2K\x1b[2;2Hrow2\x1b[3;2Hrow3\x1b[4;2Hrow4"); t.fragmented("\x1b[2;3r\x1b[3;1H\n"); assert(t.guest.line(1).substr(1,4)=="row3" && t.guest.line(2)==std::string(80,' ')); assert(t.guest.line(3).substr(1,4)=="row4"); t.send("\x1b[2;1H\x1b[L"); assert(t.guest.line(1)==std::string(80,' ') && t.guest.line(2).substr(1,4)=="row3"); t.send("\x1b[M\x1b[?6h\x1b[1;1H*\x1b[6n"); assert(t.guest.lines[1][0]=='*' && t.host.takeReply()=="\x1b[1;2R"); t.send("\x1b[?6l\x1b[r\x1b[24;80H!\x1b[1;1HX"); assert(t.guest.lines[0][0]=='X' && t.guest.lines[23][79]=='!'); assert(t.send("\x1b[H" "X").empty()); // same contents and same final cursor } void screens() { Terminal t; t.send("shell$ "); t.fragmented("\x1b[?1049h\x1b[24;1H-- INSERT --\x1b[HTitle\x1b" "7"); assert(t.guest.line(0).substr(0,5)=="Title"); assert(t.guest.line(23).substr(0,12)=="-- INSERT --"); t.fragmented("\x1b[4;80HAZ\x1b" "8"); assert(t.guest.lines[3][79]=='A' && t.guest.lines[4][0]=='Z'); assert(t.guest.x==5 && t.guest.y==0); t.fragmented("\x1b[?7l\x1b[6;80HAB\x1b[?7h"); assert(t.guest.lines[5][79]=='B' && t.guest.lines[6][0]==' '); t.send("\x1b[?1049l"); assert(t.guest.line(0).substr(0,7)=="shell$ "); assert(t.guest.x==7 && t.guest.y==0); assert(t.send("pwd")==Bytes({'p','w','d'})); t.fragmented("\x1b[H\x1b)0\x0elqqk\x0f"); assert(t.guest.line(0).substr(0,4)=="+--+"); } void malformed() { Terminal t; t.send("\x1b[H"); t.send("\x1b[" + std::string(10000,'9') + "Hok"); t.send("\x1b[" + std::string(10000,';') + "J"); assert(t.guest.lines[23][0]=='o'); t.send("\x1bP" + std::string(10000,'x') + "\x18!"); assert(t.guest.lines[23][2]=='!'); for (int i=0; i<1000; ++i) t.send("\x1b[5n"); assert(t.host.takeReply().size() <= 128 && t.host.takeReply().empty()); } } int main() { transcript(); editing(); screens(); malformed(); std::cout << "DriveWire terminal transcript, screen edits, scrolling, alternate buffer, and fragmented input passed.\n"; }