Now can send debugging info (actions) to separate tty

This commit is contained in:
Keith Winstein
2011-01-22 16:10:23 -05:00
parent 1c6819ae0d
commit bd2e1f99c1
3 changed files with 78 additions and 19 deletions
+26 -10
View File
@@ -13,6 +13,8 @@
#include <wctype.h> #include <wctype.h>
#include <iostream> #include <iostream>
#include <typeinfo> #include <typeinfo>
#include <sys/stat.h>
#include <fcntl.h>
#include "terminal.hpp" #include "terminal.hpp"
@@ -22,14 +24,28 @@
const size_t buf_size = 1024; const size_t buf_size = 1024;
void emulate_terminal( int fd ); void emulate_terminal( int fd, int debug_fd );
int copy( int src, int dest ); int copy( int src, int dest );
int termemu( int fd, Terminal::Emulator *terminal ); int termemu( int fd, Terminal::Emulator *terminal, int debug_fd );
int main( int argc __attribute__((unused)), int main( int argc,
char *argv[] __attribute__((unused)), char *argv[],
char *envp[] ) char *envp[] )
{ {
int debug_fd;
if ( argc == 1 ) {
debug_fd = -1;
} else if ( argc == 2 ) {
debug_fd = open( argv[ 1 ], O_WRONLY );
if ( debug_fd < 0 ) {
perror( "open" );
exit( 1 );
}
} else {
fprintf( stderr, "Usage: %s [debugfd]\n", argv[ 0 ] );
exit( 1 );
}
int master; int master;
struct termios saved_termios, raw_termios, child_termios; struct termios saved_termios, raw_termios, child_termios;
@@ -86,7 +102,7 @@ int main( int argc __attribute__((unused)),
exit( 1 ); exit( 1 );
} }
emulate_terminal( master ); emulate_terminal( master, debug_fd );
if ( tcsetattr( STDIN_FILENO, TCSANOW, &saved_termios ) < 0 ) { if ( tcsetattr( STDIN_FILENO, TCSANOW, &saved_termios ) < 0 ) {
perror( "tcsetattr" ); perror( "tcsetattr" );
@@ -97,7 +113,7 @@ int main( int argc __attribute__((unused)),
return 0; return 0;
} }
void emulate_terminal( int fd ) void emulate_terminal( int fd, int debug_fd )
{ {
Terminal::Emulator terminal( 80, 24 ); Terminal::Emulator terminal( 80, 24 );
struct pollfd pollfds[ 2 ]; struct pollfd pollfds[ 2 ];
@@ -120,7 +136,7 @@ void emulate_terminal( int fd )
return; return;
} }
} else if ( pollfds[ 1 ].revents & POLLIN ) { } else if ( pollfds[ 1 ].revents & POLLIN ) {
if ( termemu( fd, &terminal ) < 0 ) { if ( termemu( fd, &terminal, debug_fd ) < 0 ) {
return; return;
} }
} else if ( (pollfds[ 0 ].revents | pollfds[ 1 ].revents) } else if ( (pollfds[ 0 ].revents | pollfds[ 1 ].revents)
@@ -159,7 +175,7 @@ int copy( int src, int dest )
return 0; return 0;
} }
int termemu( int fd, Terminal::Emulator *terminal ) int termemu( int fd, Terminal::Emulator *terminal, int debug_fd )
{ {
char buf[ buf_size ]; char buf[ buf_size ];
@@ -176,10 +192,10 @@ int termemu( int fd, Terminal::Emulator *terminal )
/* feed to terminal */ /* feed to terminal */
for ( int i = 0; i < bytes_read; i++ ) { for ( int i = 0; i < bytes_read; i++ ) {
terminal_to_host.append( terminal->input( buf[ i ] ) ); terminal_to_host.append( terminal->input( buf[ i ], debug_fd ) );
} }
terminal->debug_printout( stdout ); terminal->debug_printout( STDOUT_FILENO );
/* write writeback */ /* write writeback */
ssize_t total_bytes_written = 0; ssize_t total_bytes_written = 0;
+50 -7
View File
@@ -2,11 +2,14 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <typeinfo>
#include "terminal.hpp" #include "terminal.hpp"
using namespace Terminal; using namespace Terminal;
static void swrite( int fd, const char *str );
Cell::Cell() Cell::Cell()
: overlapping_cell( NULL ), : overlapping_cell( NULL ),
contents(), contents(),
@@ -49,7 +52,7 @@ Emulator::~Emulator()
} }
std::string Emulator::input( char c ) std::string Emulator::input( char c, int actfd )
{ {
terminal_to_host.clear(); terminal_to_host.clear();
@@ -60,6 +63,24 @@ std::string Emulator::input( char c )
i++ ) { i++ ) {
Parser::Action *act = *i; Parser::Action *act = *i;
if ( (actfd > 0) && (typeid(*act) != typeid(Parser::Print)) ) {
char actsum[ 32 ];
if ( act->char_present ) {
if ( isprint( act->ch ) ) {
snprintf( actsum, 32, "%s(0x%02x=%lc) ",
act->name().c_str(), act->ch, act->ch );
} else {
snprintf( actsum, 32, "%s(0x%02x) ",
act->name().c_str(), act->ch );
}
} else {
snprintf( actsum, 32, "[%s] ",
act->name().c_str() );
}
swrite( actfd, actsum );
}
act->act_on_terminal( this ); act->act_on_terminal( this );
delete act; delete act;
@@ -168,26 +189,48 @@ void Emulator::print( Parser::Print *act )
} }
} }
void Emulator::debug_printout( FILE *f ) static void swrite( int fd, const char *str )
{ {
fprintf( f, "\033[H\033[2J" ); ssize_t total_bytes_written = 0;
ssize_t bytes_to_write = strlen( str );
while ( total_bytes_written < bytes_to_write ) {
ssize_t bytes_written = write( fd, str + total_bytes_written,
bytes_to_write - total_bytes_written );
if ( bytes_written <= 0 ) {
perror( "write" );
} else {
total_bytes_written += bytes_written;
}
}
}
void Emulator::debug_printout( int fd )
{
std::string screen;
screen.append( "\033[H\033[2J" );
for ( int y = 0; y < height; y++ ) { for ( int y = 0; y < height; y++ ) {
for ( int x = 0; x < width; x++ ) { for ( int x = 0; x < width; x++ ) {
fprintf( f, "\033[%d;%dH", y + 1, x + 1 ); char curmove[ 32 ];
snprintf( curmove, 32, "\033[%d;%dH", y + 1, x + 1 );
screen.append( curmove );
Cell *cell = &rows[ y ].cells[ x ]; Cell *cell = &rows[ y ].cells[ x ];
if ( cell->overlapping_cell ) continue; if ( cell->overlapping_cell ) continue;
for ( std::vector<wchar_t>::iterator i = cell->contents.begin(); for ( std::vector<wchar_t>::iterator i = cell->contents.begin();
i != cell->contents.end(); i != cell->contents.end();
i++ ) { i++ ) {
fprintf( f, "%lc", *i ); char utf8[ 8 ];
snprintf( utf8, 8, "%lc", *i );
screen.append( utf8 );
} }
} }
} }
fprintf( f, "\033[%d;%dH", cursor_row + 1, cursor_col + 1 ); char curmove[ 32 ];
snprintf( curmove, 32, "\033[%d;%dH", cursor_row + 1, cursor_col + 1 );
screen.append( curmove );
fflush( NULL ); swrite( fd, screen.c_str() );
} }
void Emulator::param( Parser::Param *act ) void Emulator::param( Parser::Param *act )
+2 -2
View File
@@ -77,14 +77,14 @@ namespace Terminal {
Emulator( size_t s_width, size_t s_height ); Emulator( size_t s_width, size_t s_height );
~Emulator(); ~Emulator();
std::string input( char c ); std::string input( char c, int debug_fd );
void resize( size_t s_width, size_t s_height ); void resize( size_t s_width, size_t s_height );
size_t get_width( void ) { return width; } size_t get_width( void ) { return width; }
size_t get_height( void ) { return height; } size_t get_height( void ) { return height; }
void debug_printout( FILE *f ); void debug_printout( int fd );
}; };
} }