Move parser outside of terminal emulator -- prepare for keyboard input

This commit is contained in:
Keith Winstein
2011-02-03 01:59:55 -05:00
parent 7989265fa0
commit 9bca84b3c6
7 changed files with 48 additions and 61 deletions
+2 -2
View File
@@ -159,8 +159,8 @@ int vt_parser( int fd, Parser::UTF8Parser *parser )
/* feed to parser */
for ( int i = 0; i < bytes_read; i++ ) {
std::vector<Parser::Action *> actions = parser->input( buf[ i ] );
for ( std::vector<Parser::Action *>::iterator j = actions.begin();
std::list<Parser::Action *> actions = parser->input( buf[ i ] );
for ( std::list<Parser::Action *>::iterator j = actions.begin();
j != actions.end();
j++ ) {
+7 -11
View File
@@ -5,7 +5,7 @@
#include "parser.hpp"
static void append_or_delete( Parser::Action *act,
std::vector<Parser::Action *>&vec )
std::list<Parser::Action *>&vec )
{
assert( act );
@@ -16,9 +16,9 @@ static void append_or_delete( Parser::Action *act,
}
}
std::vector<Parser::Action *> Parser::Parser::input( wchar_t ch )
std::list<Parser::Action *> Parser::Parser::input( wchar_t ch )
{
std::vector<Action *> ret;
std::list<Action *> ret;
Transition tx = state->input( ch );
@@ -47,7 +47,7 @@ Parser::UTF8Parser::UTF8Parser()
assert( BUF_SIZE >= MB_CUR_MAX );
}
std::vector<Parser::Action *> Parser::UTF8Parser::input( char c )
std::list<Parser::Action *> Parser::UTF8Parser::input( char c )
{
assert( buf_len < BUF_SIZE );
@@ -62,7 +62,7 @@ std::vector<Parser::Action *> Parser::UTF8Parser::input( char c )
size_t total_bytes_parsed = 0;
size_t orig_buf_len = buf_len;
std::vector<Action *> ret;
std::list<Action *> ret;
/* this routine is somewhat complicated in order to comply with
Unicode 6.0, section 3.9, "Best Practices for using U+FFFD" */
@@ -112,12 +112,8 @@ std::vector<Parser::Action *> Parser::UTF8Parser::input( char c )
pwc = (wchar_t) 0xFFFD;
}
std::vector<Action *> vec = parser.input( pwc );
for ( std::vector<Action *>::iterator i = vec.begin();
i != vec.end();
i++ ) {
ret.push_back( *i );
}
std::list<Action *> vec = parser.input( pwc );
ret.insert( ret.end(), vec.begin(), vec.end() );
total_bytes_parsed += bytes_parsed;
}
+3 -3
View File
@@ -5,7 +5,7 @@
http://www.vt100.net/emu/dec_ansi_parser */
#include <wchar.h>
#include <vector>
#include <list>
#include <string.h>
#include "parsertransition.hpp"
@@ -30,7 +30,7 @@ namespace Parser {
Parser & operator=( const Parser & );
~Parser() {}
std::vector<Action *> input( wchar_t ch );
std::list<Action *> input( wchar_t ch );
};
static const size_t BUF_SIZE = 8;
@@ -45,7 +45,7 @@ namespace Parser {
public:
UTF8Parser();
std::vector<Action *> input( char c );
std::list<Action *> input( char c );
};
}
+13 -8
View File
@@ -1,3 +1,4 @@
#include <list>
#include <vector>
#include <deque>
#include <wchar.h>
@@ -9,11 +10,15 @@ namespace Parser {
class Action;
}
template class std::vector<Parser::Action *>;
template class std::vector<Terminal::Cell>;
template class std::deque<Terminal::Row>;
template class std::vector<Terminal::Cell *>;
template class std::vector<wchar_t>;
template class std::vector<int>;
template class std::map<std::string, Terminal::Function>;
template class std::vector<bool>;
using namespace std;
using namespace Terminal;
template class list<Parser::Action *>;
template class vector<Cell>;
template class deque<Row>;
template class vector<Cell *>;
template class vector<wchar_t>;
template class vector<int>;
template class map<string, Function>;
template class vector<bool>;
+18 -7
View File
@@ -23,7 +23,8 @@ const size_t buf_size = 1024;
void emulate_terminal( int fd, int debug_fd );
int copy( int src, int dest );
int termemu( int fd, Terminal::Emulator *terminal, int debug_fd );
int termemu( int fd, Parser::UTF8Parser *parser,
Terminal::Emulator *terminal, int debug_fd );
int main( int argc,
char *argv[],
@@ -107,11 +108,14 @@ int main( int argc,
}
}
printf( "[rtm is exiting.]\n" );
return 0;
}
void emulate_terminal( int fd, int debug_fd )
{
Parser::UTF8Parser parser;
Terminal::Emulator terminal( 80, 24 );
struct pollfd pollfds[ 2 ];
@@ -133,7 +137,7 @@ void emulate_terminal( int fd, int debug_fd )
return;
}
} else if ( pollfds[ 1 ].revents & POLLIN ) {
if ( termemu( fd, &terminal, debug_fd ) < 0 ) {
if ( termemu( fd, &parser, &terminal, debug_fd ) < 0 ) {
return;
}
} else if ( (pollfds[ 0 ].revents | pollfds[ 1 ].revents)
@@ -160,7 +164,8 @@ int copy( int src, int dest )
return swrite( dest, buf, bytes_read );
}
int termemu( int fd, Terminal::Emulator *terminal, int debug_fd )
int termemu( int fd, Parser::UTF8Parser *parser,
Terminal::Emulator *terminal, int debug_fd )
{
char buf[ buf_size ];
@@ -173,15 +178,21 @@ int termemu( int fd, Terminal::Emulator *terminal, int debug_fd )
return -1;
}
std::string terminal_to_host;
/* feed to terminal */
/* feed bytes to parser, and actions to terminal */
for ( int i = 0; i < bytes_read; i++ ) {
terminal_to_host.append( terminal->input( buf[ i ], debug_fd ) );
std::list<Parser::Action *> actions = parser->input( buf[ i ] );
for ( std::list<Parser::Action *>::iterator i = actions.begin();
i != actions.end();
i++ ) {
(*i)->act_on_terminal( terminal );
}
}
terminal->debug_printout( STDOUT_FILENO );
debug_fd = debug_fd;
/* write writeback */
std::string terminal_to_host = terminal->read_octets_to_host();
return swrite( fd, terminal_to_host.c_str(), terminal_to_host.length() );
}
+4 -28
View File
@@ -10,38 +10,14 @@
using namespace Terminal;
Emulator::Emulator( size_t s_width, size_t s_height )
: parser(), fb( s_width, s_height ), dispatch()
: fb( s_width, s_height ), dispatch()
{}
std::string Emulator::input( char c, int actfd )
std::string Emulator::read_octets_to_host( void )
{
std::string ret = dispatch.terminal_to_host;
dispatch.terminal_to_host.clear();
std::vector<Parser::Action *> vec = parser.input( c );
for ( std::vector<Parser::Action *>::iterator i = vec.begin();
i != vec.end();
i++ ) {
Parser::Action *act = *i;
act->act_on_terminal( this );
/* print out debugging information */
if ( (actfd > 0) && ( !act->handled ) ) {
char actsum[ 64 ];
if ( (typeid( *act ) == typeid( Parser::CSI_Dispatch ))
|| (typeid( *act ) == typeid( Parser::Esc_Dispatch )) ) {
snprintf( actsum, 64, "%s%s ", act->str().c_str(), dispatch.str().c_str() );
} else {
snprintf( actsum, 64, "%s ", act->str().c_str() );
}
swrite( actfd, actsum );
}
delete act;
}
/* the user is supposed to send this string to the host, as if typed */
return dispatch.terminal_to_host;
return ret;
}
void Emulator::execute( Parser::Execute *act )
+1 -2
View File
@@ -24,7 +24,6 @@ namespace Terminal {
friend void Parser::OSC_End::act_on_terminal( Emulator * );
private:
Parser::UTF8Parser parser;
Framebuffer fb;
Dispatcher dispatch;
@@ -38,7 +37,7 @@ namespace Terminal {
public:
Emulator( size_t s_width, size_t s_height );
std::string input( char c, int debug_fd );
std::string read_octets_to_host( void );
void debug_printout( int fd );
};