Use shared_ptr and references for Actions.

This slows terminal emulation slightly.
This commit is contained in:
John Hood
2017-05-11 01:22:12 -04:00
parent c6c7f09954
commit 42d2b5d029
13 changed files with 104 additions and 113 deletions
+7 -9
View File
@@ -199,21 +199,19 @@ static int vt_parser( int fd, Parser::UTF8Parser *parser )
j != actions.end();
j++ ) {
Parser::Action *act = *j;
assert( act );
assert( *j );
Parser::Action &act = **j;
if ( act->char_present ) {
if ( iswprint( act->ch ) ) {
printf( "%s(0x%02x=%lc) ", act->name().c_str(), (unsigned int)act->ch, (wint_t)act->ch );
if ( act.char_present ) {
if ( iswprint( act.ch ) ) {
printf( "%s(0x%02x=%lc) ", act.name().c_str(), (unsigned int)act.ch, (wint_t)act.ch );
} else {
printf( "%s(0x%02x) ", act->name().c_str(), (unsigned int)act->ch );
printf( "%s(0x%02x) ", act.name().c_str(), (unsigned int)act.ch );
}
} else {
printf( "[%s] ", act->name().c_str() );
printf( "[%s] ", act.name().c_str() );
}
delete act;
fflush( stdout );
}
actions.clear();
+2 -4
View File
@@ -270,8 +270,7 @@ static void emulate_terminal( int fd )
std::string terminal_to_host;
for ( int i = 0; i < bytes_read; i++ ) {
Parser::UserByte ub( buf[ i ] );
terminal_to_host += complete.act( &ub );
terminal_to_host += complete.act( Parser::UserByte( buf[ i ] ) );
}
if ( swrite( fd, terminal_to_host.c_str(), terminal_to_host.length() ) < 0 ) {
@@ -302,8 +301,7 @@ static void emulate_terminal( int fd )
}
/* tell emulator */
Parser::Resize r( window_size.ws_col, window_size.ws_row );
complete.act( &r );
complete.act( Parser::Resize( window_size.ws_col, window_size.ws_row ) );
/* tell child process */
if ( ioctl( fd, TIOCSWINSZ, &window_size ) < 0 ) {
+10 -7
View File
@@ -719,22 +719,25 @@ static void serve( int host_fd, Terminal::Complete &terminal, ServerConnection &
us.apply_string( network.get_remote_diff() );
/* apply userstream to terminal */
for ( size_t i = 0; i < us.size(); i++ ) {
const Parser::Action *action = us.get_action( i );
if ( typeid( *action ) == typeid( Parser::Resize ) ) {
const Parser::Action &action = us.get_action( i );
if ( typeid( action ) == typeid( Parser::Resize ) ) {
/* apply only the last consecutive Resize action */
while ( i < us.size() - 1 &&
typeid( us.get_action( i + 1 ) ) == typeid( Parser::Resize ) ) {
while ( i < us.size() - 1 ) {
const Parser::Action &next = us.get_action( i + 1 );
if ( typeid( next ) != typeid( Parser::Resize ) ) {
break;
}
i++;
}
/* tell child process of resize */
const Parser::Resize *res = static_cast<const Parser::Resize *>( action );
const Parser::Resize &res = static_cast<const Parser::Resize &>( action );
struct winsize window_size;
if ( ioctl( host_fd, TIOCGWINSZ, &window_size ) < 0 ) {
perror( "ioctl TIOCGWINSZ" );
network.start_shutdown();
}
window_size.ws_col = res->width;
window_size.ws_row = res->height;
window_size.ws_col = res.width;
window_size.ws_row = res.height;
if ( ioctl( host_fd, TIOCSWINSZ, &window_size ) < 0 ) {
perror( "ioctl TIOCSWINSZ" );
network.start_shutdown();
+9 -11
View File
@@ -672,22 +672,22 @@ void PredictionEngine::new_user_byte( char the_byte, const Framebuffer &fb )
for ( Parser::Actions::iterator it = actions.begin();
it != actions.end();
it++ ) {
Parser::Action *act = *it;
Parser::Action& act = **it;
/*
fprintf( stderr, "Action: %s (%lc)\n",
act->name().c_str(), act->char_present ? act->ch : L'_' );
*/
const std::type_info& type_act = typeid( *act );
const std::type_info& type_act = typeid( act );
if ( type_act == typeid( Parser::Print ) ) {
/* make new prediction */
init_cursor( fb );
assert( act->char_present );
assert( act.char_present );
wchar_t ch = act->ch;
wchar_t ch = act.ch;
/* XXX handle wide characters */
if ( ch == 0x7f ) { /* backspace */
@@ -826,24 +826,24 @@ void PredictionEngine::new_user_byte( char the_byte, const Framebuffer &fb )
}
}
} else if ( type_act == typeid( Parser::Execute ) ) {
if ( act->char_present && (act->ch == 0x0d) /* CR */ ) {
if ( act.char_present && (act.ch == 0x0d) /* CR */ ) {
become_tentative();
newline_carriage_return( fb );
} else {
// fprintf( stderr, "Execute 0x%x\n", act->ch );
// fprintf( stderr, "Execute 0x%x\n", act.ch );
become_tentative();
}
} else if ( type_act == typeid( Parser::Esc_Dispatch ) ) {
// fprintf( stderr, "Escape sequence\n" );
become_tentative();
} else if ( type_act == typeid( Parser::CSI_Dispatch ) ) {
if ( act->char_present && (act->ch == L'C') ) { /* right arrow */
if ( act.char_present && (act.ch == L'C') ) { /* right arrow */
init_cursor( fb );
if ( cursor().col < fb.ds.get_width() - 1 ) {
cursor().col++;
cursor().expire( local_frame_sent + 1, now );
}
} else if ( act->char_present && (act->ch == L'D') ) { /* left arrow */
} else if ( act.char_present && (act.ch == L'D') ) { /* left arrow */
init_cursor( fb );
if ( cursor().col > 0 ) {
@@ -851,12 +851,10 @@ void PredictionEngine::new_user_byte( char the_byte, const Framebuffer &fb )
cursor().expire( local_frame_sent + 1, now );
}
} else {
// fprintf( stderr, "CSI sequence %lc\n", act->ch );
// fprintf( stderr, "CSI sequence %lc\n", act.ch );
become_tentative();
}
}
delete act;
}
}
+6 -8
View File
@@ -52,9 +52,8 @@ string Complete::act( const string &str )
for ( Actions::iterator it = actions.begin();
it != actions.end();
it++ ) {
Action *act = *it;
act->act_on_terminal( &terminal );
delete act;
Action &act = **it;
act.act_on_terminal( &terminal );
}
actions.clear();
}
@@ -62,10 +61,10 @@ string Complete::act( const string &str )
return terminal.read_octets_to_host();
}
string Complete::act( const Action *act )
string Complete::act( const Action &act )
{
/* apply action to terminal */
act->act_on_terminal( &terminal );
act.act_on_terminal( &terminal );
return terminal.read_octets_to_host();
}
@@ -112,9 +111,8 @@ void Complete::apply_string( const string & diff )
string terminal_to_host = act( input.instruction( i ).GetExtension( hostbytes ).hoststring() );
assert( terminal_to_host.empty() ); /* server never interrogates client terminal */
} else if ( input.instruction( i ).HasExtension( resize ) ) {
Resize new_size( input.instruction( i ).GetExtension( resize ).width(),
input.instruction( i ).GetExtension( resize ).height() );
act( &new_size );
act( Resize( input.instruction( i ).GetExtension( resize ).width(),
input.instruction( i ).GetExtension( resize ).height() ) );
} else if ( input.instruction( i ).HasExtension( echoack ) ) {
uint64_t inst_echo_ack_num = input.instruction( i ).GetExtension( echoack ).echo_ack_num();
assert( inst_echo_ack_num >= echo_ack );
+1 -1
View File
@@ -64,7 +64,7 @@ namespace Terminal {
actions(), input_history(), echo_ack( 0 ) {}
std::string act( const std::string &str );
std::string act( const Parser::Action *act );
std::string act( const Parser::Action &act );
const Framebuffer & get_fb( void ) const { return terminal.get_fb(); }
void reset_input( void ) { parser.reset_input(); }
+5 -4
View File
@@ -123,15 +123,16 @@ void UserStream::apply_string( const string &diff )
}
}
const Parser::Action *UserStream::get_action( unsigned int i ) const
const Parser::Action &UserStream::get_action( unsigned int i ) const
{
switch( actions[ i ].type ) {
case UserByteType:
return &( actions[ i ].userbyte );
return actions[ i ].userbyte;
case ResizeType:
return &( actions[ i ].resize );
return actions[ i ].resize;
default:
assert( false );
return NULL;
static const Parser::Ignore nothing = Parser::Ignore();
return nothing;
}
}
+1 -1
View File
@@ -84,7 +84,7 @@ namespace Network {
bool empty( void ) const { return actions.empty(); }
size_t size( void ) const { return actions.size(); }
const Parser::Action *get_action( unsigned int i ) const;
const Parser::Action &get_action( unsigned int i ) const;
/* interface for Network::Transport */
void subtract( const UserStream *prefix );
+1 -4
View File
@@ -40,15 +40,13 @@
const Parser::StateFamily Parser::family;
static void append_or_delete( Parser::Action *act,
static void append_or_delete( Parser::ActionPointer act,
Parser::Actions &vec )
{
assert( act );
if ( !act->ignore() ) {
vec.push_back( act );
} else {
delete act;
}
}
@@ -61,7 +59,6 @@ void Parser::Parser::input( wchar_t ch, Actions &ret )
}
append_or_delete( tx.action, ret );
tx.action = NULL;
if ( tx.next_state != NULL ) {
append_or_delete( tx.next_state->enter(), ret );
+4 -1
View File
@@ -36,6 +36,8 @@
#include <string>
#include <vector>
#include "shared.h"
namespace Terminal {
class Emulator;
}
@@ -59,7 +61,8 @@ namespace Parser {
virtual bool operator==( const Action &other ) const;
};
typedef std::vector<Action *> Actions;
typedef shared::shared_ptr<Action> ActionPointer;
typedef std::vector<ActionPointer> Actions;
class Ignore : public Action {
public:
+46 -45
View File
@@ -32,6 +32,7 @@
#include "parserstate.h"
#include "parserstatefamily.h"
#include "shared.h"
using namespace Parser;
@@ -41,7 +42,7 @@ Transition State::anywhere_rule( wchar_t ch ) const
|| ((0x80 <= ch) && (ch <= 0x8F))
|| ((0x91 <= ch) && (ch <= 0x97))
|| (ch == 0x99) || (ch == 0x9A) ) {
return Transition( new Execute, &family->s_Ground );
return Transition( shared::make_shared<Execute>(), &family->s_Ground );
} else if ( ch == 0x9C ) {
return Transition( &family->s_Ground );
} else if ( ch == 0x1B ) {
@@ -56,7 +57,7 @@ Transition State::anywhere_rule( wchar_t ch ) const
return Transition( &family->s_CSI_Entry );
}
return Transition(( State * )NULL, NULL ); /* don't allocate an Ignore action */
return Transition(( State * )NULL, ActionPointer() ); /* don't allocate an Ignore action */
}
Transition State::input( wchar_t ch ) const
@@ -92,29 +93,29 @@ static bool GLGR ( wchar_t ch )
Transition Ground::input_state_rule( wchar_t ch ) const
{
if ( C0_prime( ch ) ) {
return Transition( new Execute );
return Transition( shared::make_shared< Execute >() );
}
if ( GLGR( ch ) ) {
return Transition( new Print );
return Transition( shared::make_shared< Print >() );
}
return Transition();
}
Action *Escape::enter( void ) const
ActionPointer Escape::enter( void ) const
{
return new Clear;
return shared::make_shared< Clear >();
}
Transition Escape::input_state_rule( wchar_t ch ) const
{
if ( C0_prime( ch ) ) {
return Transition( new Execute );
return Transition( shared::make_shared< Execute >() );
}
if ( (0x20 <= ch) && (ch <= 0x2F) ) {
return Transition( new Collect, &family->s_Escape_Intermediate );
return Transition( shared::make_shared< Collect >(), &family->s_Escape_Intermediate );
}
if ( ( (0x30 <= ch) && (ch <= 0x4F) )
@@ -123,7 +124,7 @@ Transition Escape::input_state_rule( wchar_t ch ) const
|| ( ch == 0x5A )
|| ( ch == 0x5C )
|| ( (0x60 <= ch) && (ch <= 0x7E) ) ) {
return Transition( new Esc_Dispatch, &family->s_Ground );
return Transition( shared::make_shared< Esc_Dispatch >(), &family->s_Ground );
}
if ( ch == 0x5B ) {
@@ -148,42 +149,42 @@ Transition Escape::input_state_rule( wchar_t ch ) const
Transition Escape_Intermediate::input_state_rule( wchar_t ch ) const
{
if ( C0_prime( ch ) ) {
return Transition( new Execute );
return Transition( shared::make_shared< Execute >() );
}
if ( (0x20 <= ch) && (ch <= 0x2F) ) {
return Transition( new Collect );
return Transition( shared::make_shared< Collect >() );
}
if ( (0x30 <= ch) && (ch <= 0x7E) ) {
return Transition( new Esc_Dispatch, &family->s_Ground );
return Transition( shared::make_shared< Esc_Dispatch >(), &family->s_Ground );
}
return Transition();
}
Action *CSI_Entry::enter( void ) const
ActionPointer CSI_Entry::enter( void ) const
{
return new Clear;
return shared::make_shared< Clear >();
}
Transition CSI_Entry::input_state_rule( wchar_t ch ) const
{
if ( C0_prime( ch ) ) {
return Transition( new Execute );
return Transition( shared::make_shared< Execute >() );
}
if ( (0x40 <= ch) && (ch <= 0x7E) ) {
return Transition( new CSI_Dispatch, &family->s_Ground );
return Transition( shared::make_shared< CSI_Dispatch >(), &family->s_Ground );
}
if ( ( (0x30 <= ch) && (ch <= 0x39) )
|| ( ch == 0x3B ) ) {
return Transition( new Param, &family->s_CSI_Param );
return Transition( shared::make_shared< Param >(), &family->s_CSI_Param );
}
if ( (0x3C <= ch) && (ch <= 0x3F) ) {
return Transition( new Collect, &family->s_CSI_Param );
return Transition( shared::make_shared< Collect >(), &family->s_CSI_Param );
}
if ( ch == 0x3A ) {
@@ -191,7 +192,7 @@ Transition CSI_Entry::input_state_rule( wchar_t ch ) const
}
if ( (0x20 <= ch) && (ch <= 0x2F) ) {
return Transition( new Collect, &family->s_CSI_Intermediate );
return Transition( shared::make_shared< Collect >(), &family->s_CSI_Intermediate );
}
return Transition();
@@ -200,11 +201,11 @@ Transition CSI_Entry::input_state_rule( wchar_t ch ) const
Transition CSI_Param::input_state_rule( wchar_t ch ) const
{
if ( C0_prime( ch ) ) {
return Transition( new Execute );
return Transition( shared::make_shared< Execute >() );
}
if ( ( (0x30 <= ch) && (ch <= 0x39) ) || ( ch == 0x3B ) ) {
return Transition( new Param );
return Transition( shared::make_shared< Param >() );
}
if ( ( ch == 0x3A ) || ( (0x3C <= ch) && (ch <= 0x3F) ) ) {
@@ -212,11 +213,11 @@ Transition CSI_Param::input_state_rule( wchar_t ch ) const
}
if ( (0x20 <= ch) && (ch <= 0x2F) ) {
return Transition( new Collect, &family->s_CSI_Intermediate );
return Transition( shared::make_shared< Collect >(), &family->s_CSI_Intermediate );
}
if ( (0x40 <= ch) && (ch <= 0x7E) ) {
return Transition( new CSI_Dispatch, &family->s_Ground );
return Transition( shared::make_shared< CSI_Dispatch >(), &family->s_Ground );
}
return Transition();
@@ -225,15 +226,15 @@ Transition CSI_Param::input_state_rule( wchar_t ch ) const
Transition CSI_Intermediate::input_state_rule( wchar_t ch ) const
{
if ( C0_prime( ch ) ) {
return Transition( new Execute );
return Transition( shared::make_shared< Execute >() );
}
if ( (0x20 <= ch) && (ch <= 0x2F) ) {
return Transition( new Collect );
return Transition( shared::make_shared< Collect >() );
}
if ( (0x40 <= ch) && (ch <= 0x7E) ) {
return Transition( new CSI_Dispatch, &family->s_Ground );
return Transition( shared::make_shared< CSI_Dispatch >(), &family->s_Ground );
}
if ( (0x30 <= ch) && (ch <= 0x3F) ) {
@@ -246,7 +247,7 @@ Transition CSI_Intermediate::input_state_rule( wchar_t ch ) const
Transition CSI_Ignore::input_state_rule( wchar_t ch ) const
{
if ( C0_prime( ch ) ) {
return Transition( new Execute );
return Transition( shared::make_shared< Execute >() );
}
if ( (0x40 <= ch) && (ch <= 0x7E) ) {
@@ -256,15 +257,15 @@ Transition CSI_Ignore::input_state_rule( wchar_t ch ) const
return Transition();
}
Action *DCS_Entry::enter( void ) const
ActionPointer DCS_Entry::enter( void ) const
{
return new Clear;
return shared::make_shared< Clear >();
}
Transition DCS_Entry::input_state_rule( wchar_t ch ) const
{
if ( (0x20 <= ch) && (ch <= 0x2F) ) {
return Transition( new Collect, &family->s_DCS_Intermediate );
return Transition( shared::make_shared< Collect >(), &family->s_DCS_Intermediate );
}
if ( ch == 0x3A ) {
@@ -272,11 +273,11 @@ Transition DCS_Entry::input_state_rule( wchar_t ch ) const
}
if ( ( (0x30 <= ch) && (ch <= 0x39) ) || ( ch == 0x3B ) ) {
return Transition( new Param, &family->s_DCS_Param );
return Transition( shared::make_shared< Param >(), &family->s_DCS_Param );
}
if ( (0x3C <= ch) && (ch <= 0x3F) ) {
return Transition( new Collect, &family->s_DCS_Param );
return Transition( shared::make_shared< Collect >(), &family->s_DCS_Param );
}
if ( (0x40 <= ch) && (ch <= 0x7E) ) {
@@ -289,7 +290,7 @@ Transition DCS_Entry::input_state_rule( wchar_t ch ) const
Transition DCS_Param::input_state_rule( wchar_t ch ) const
{
if ( ( (0x30 <= ch) && (ch <= 0x39) ) || ( ch == 0x3B ) ) {
return Transition( new Param );
return Transition( shared::make_shared< Param >() );
}
if ( ( ch == 0x3A ) || ( (0x3C <= ch) && (ch <= 0x3F) ) ) {
@@ -297,7 +298,7 @@ Transition DCS_Param::input_state_rule( wchar_t ch ) const
}
if ( (0x20 <= ch) && (ch <= 0x2F) ) {
return Transition( new Collect, &family->s_DCS_Intermediate );
return Transition( shared::make_shared< Collect >(), &family->s_DCS_Intermediate );
}
if ( (0x40 <= ch) && (ch <= 0x7E) ) {
@@ -310,7 +311,7 @@ Transition DCS_Param::input_state_rule( wchar_t ch ) const
Transition DCS_Intermediate::input_state_rule( wchar_t ch ) const
{
if ( (0x20 <= ch) && (ch <= 0x2F) ) {
return Transition( new Collect );
return Transition( shared::make_shared< Collect >() );
}
if ( (0x40 <= ch) && (ch <= 0x7E) ) {
@@ -324,20 +325,20 @@ Transition DCS_Intermediate::input_state_rule( wchar_t ch ) const
return Transition();
}
Action *DCS_Passthrough::enter( void ) const
ActionPointer DCS_Passthrough::enter( void ) const
{
return new Hook;
return shared::make_shared< Hook >();
}
Action *DCS_Passthrough::exit( void ) const
ActionPointer DCS_Passthrough::exit( void ) const
{
return new Unhook;
return shared::make_shared< Unhook >();
}
Transition DCS_Passthrough::input_state_rule( wchar_t ch ) const
{
if ( C0_prime( ch ) || ( (0x20 <= ch) && (ch <= 0x7E) ) ) {
return Transition( new Put );
return Transition( shared::make_shared< Put >() );
}
if ( ch == 0x9C ) {
@@ -356,20 +357,20 @@ Transition DCS_Ignore::input_state_rule( wchar_t ch ) const
return Transition();
}
Action *OSC_String::enter( void ) const
ActionPointer OSC_String::enter( void ) const
{
return new OSC_Start;
return shared::make_shared< OSC_Start >();
}
Action *OSC_String::exit( void ) const
ActionPointer OSC_String::exit( void ) const
{
return new OSC_End;
return shared::make_shared< OSC_End >();
}
Transition OSC_String::input_state_rule( wchar_t ch ) const
{
if ( (0x20 <= ch) && (ch <= 0x7F) ) {
return Transition( new OSC_Put );
return Transition( shared::make_shared< OSC_Put >() );
}
if ( (ch == 0x9C) || (ch == 0x07) ) { /* 0x07 is xterm non-ANSI variant */
+9 -9
View File
@@ -50,8 +50,8 @@ namespace Parser {
public:
void setfamily( StateFamily *s_family ) { family = s_family; }
Transition input( wchar_t ch ) const;
virtual Action *enter( void ) const { return new Ignore; }
virtual Action *exit( void ) const { return new Ignore; }
virtual ActionPointer enter( void ) const { return shared::make_shared< Ignore >(); }
virtual ActionPointer exit( void ) const { return shared::make_shared< Ignore >(); }
State() : family( NULL ) {};
virtual ~State() {};
@@ -65,7 +65,7 @@ namespace Parser {
};
class Escape : public State {
Action *enter( void ) const;
ActionPointer enter( void ) const;
Transition input_state_rule( wchar_t ch ) const;
};
@@ -74,7 +74,7 @@ namespace Parser {
};
class CSI_Entry : public State {
Action *enter( void ) const;
ActionPointer enter( void ) const;
Transition input_state_rule( wchar_t ch ) const;
};
class CSI_Param : public State {
@@ -88,7 +88,7 @@ namespace Parser {
};
class DCS_Entry : public State {
Action *enter( void ) const;
ActionPointer enter( void ) const;
Transition input_state_rule( wchar_t ch ) const;
};
class DCS_Param : public State {
@@ -98,18 +98,18 @@ namespace Parser {
Transition input_state_rule( wchar_t ch ) const;
};
class DCS_Passthrough : public State {
Action *enter( void ) const;
ActionPointer enter( void ) const;
Transition input_state_rule( wchar_t ch ) const;
Action *exit( void ) const;
ActionPointer exit( void ) const;
};
class DCS_Ignore : public State {
Transition input_state_rule( wchar_t ch ) const;
};
class OSC_String : public State {
Action *enter( void ) const;
ActionPointer enter( void ) const;
Transition input_state_rule( wchar_t ch ) const;
Action *exit( void ) const;
ActionPointer exit( void ) const;
};
class SOS_PM_APC_String : public State {
Transition input_state_rule( wchar_t ch ) const;
+3 -9
View File
@@ -45,7 +45,7 @@ namespace Parser {
public:
// Transition is only a courier for an Action; it should
// never create/delete one on its own.
Action *action;
ActionPointer action;
State *next_state;
Transition( const Transition &x )
@@ -58,20 +58,14 @@ namespace Parser {
return *this;
}
virtual ~Transition()
{
// Indicate to checkers that we don't own *action anymore
action = NULL;
}
Transition( Action *s_action=new Ignore, State *s_next_state=NULL )
Transition( ActionPointer s_action=shared::make_shared< Ignore >(), State *s_next_state=NULL )
: action( s_action ), next_state( s_next_state )
{}
// This is only ever used in the 1-argument form;
// we use this instead of an initializer to
// tell Coverity the object never owns *action.
Transition( State *s_next_state, Action *s_action=new Ignore )
Transition( State *s_next_state, ActionPointer s_action=shared::make_shared< Ignore >() )
: action( s_action ), next_state( s_next_state )
{}
};