clang-format Mosh

Run clang-format over the Mosh source tree. This is a large change and
has been factored into its own commit for auditability. Reproduce it
with

    find . -name \*.cc -or -name \*.h | while read f; do clang-format -i --style=file $f; done
This commit is contained in:
Benjamin Barenblat
2023-08-07 21:53:48 -04:00
committed by Alex Chernyakhovsky
parent 0b15dc94fa
commit 3acaa1c4d3
77 changed files with 4838 additions and 4848 deletions
+34 -36
View File
@@ -41,17 +41,15 @@ using namespace Parser;
using namespace Terminal;
using namespace HostBuffers;
string Complete::act( const string &str )
string Complete::act( const string& str )
{
for ( unsigned int i = 0; i < str.size(); i++ ) {
/* parse octet into up to three actions */
parser.input( str[ i ], actions );
parser.input( str[i], actions );
/* apply actions to terminal and delete them */
for ( Actions::iterator it = actions.begin();
it != actions.end();
it++ ) {
Action &act = **it;
for ( Actions::iterator it = actions.begin(); it != actions.end(); it++ ) {
Action& act = **it;
act.act_on_terminal( &terminal );
}
actions.clear();
@@ -60,7 +58,7 @@ 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 );
@@ -68,39 +66,39 @@ string Complete::act( const Action &act )
}
/* interface for Network::Transport */
string Complete::diff_from( const Complete &existing ) const
string Complete::diff_from( const Complete& existing ) const
{
HostBuffers::HostMessage output;
if ( existing.get_echo_ack() != get_echo_ack() ) {
assert( get_echo_ack() >= existing.get_echo_ack() );
Instruction *new_echo = output.add_instruction();
Instruction* new_echo = output.add_instruction();
new_echo->MutableExtension( echoack )->set_echo_ack_num( get_echo_ack() );
}
if ( !(existing.get_fb() == get_fb()) ) {
if ( (existing.get_fb().ds.get_width() != terminal.get_fb().ds.get_width())
|| (existing.get_fb().ds.get_height() != terminal.get_fb().ds.get_height()) ) {
Instruction *new_res = output.add_instruction();
if ( !( existing.get_fb() == get_fb() ) ) {
if ( ( existing.get_fb().ds.get_width() != terminal.get_fb().ds.get_width() )
|| ( existing.get_fb().ds.get_height() != terminal.get_fb().ds.get_height() ) ) {
Instruction* new_res = output.add_instruction();
new_res->MutableExtension( resize )->set_width( terminal.get_fb().ds.get_width() );
new_res->MutableExtension( resize )->set_height( terminal.get_fb().ds.get_height() );
}
string update = display.new_frame( true, existing.get_fb(), terminal.get_fb() );
if ( !update.empty() ) {
Instruction *new_inst = output.add_instruction();
Instruction* new_inst = output.add_instruction();
new_inst->MutableExtension( hostbytes )->set_hoststring( update );
}
}
return output.SerializeAsString();
}
string Complete::init_diff( void ) const
{
return diff_from( Complete( get_fb().ds.get_width(), get_fb().ds.get_height() ));
return diff_from( Complete( get_fb().ds.get_width(), get_fb().ds.get_height() ) );
}
void Complete::apply_string( const string & diff )
void Complete::apply_string( const string& diff )
{
HostBuffers::HostMessage input;
fatal_assert( input.ParseFromString( diff ) );
@@ -111,7 +109,7 @@ void Complete::apply_string( const string & diff )
assert( terminal_to_host.empty() ); /* server never interrogates client terminal */
} else if ( input.instruction( i ).HasExtension( resize ) ) {
act( Resize( input.instruction( i ).GetExtension( resize ).width(),
input.instruction( i ).GetExtension( resize ).height() ) );
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 );
@@ -120,10 +118,10 @@ void Complete::apply_string( const string & diff )
}
}
bool Complete::operator==( Complete const &x ) const
bool Complete::operator==( Complete const& x ) const
{
// assert( parser == x.parser ); /* parser state is irrelevant for us */
return (terminal == x.terminal) && (echo_ack == x.echo_ack);
return ( terminal == x.terminal ) && ( echo_ack == x.echo_ack );
}
bool Complete::set_echo_ack( uint64_t now )
@@ -131,16 +129,13 @@ bool Complete::set_echo_ack( uint64_t now )
bool ret = false;
uint64_t newest_echo_ack = 0;
for ( input_history_type::const_iterator i = input_history.begin();
i != input_history.end();
i++ ) {
for ( input_history_type::const_iterator i = input_history.begin(); i != input_history.end(); i++ ) {
if ( i->second <= now - ECHO_TIMEOUT ) {
newest_echo_ack = i->first;
}
}
for ( input_history_type::iterator i = input_history.begin();
i != input_history.end(); ) {
for ( input_history_type::iterator i = input_history.begin(); i != input_history.end(); ) {
input_history_type::iterator i_next = i;
i_next++;
if ( i->first < newest_echo_ack ) {
@@ -179,11 +174,11 @@ int Complete::wait_time( uint64_t now ) const
return next_echo_ack_time - now;
}
bool Complete::compare( const Complete &other ) const
bool Complete::compare( const Complete& other ) const
{
bool ret = false;
const Framebuffer &fb = terminal.get_fb();
const Framebuffer &other_fb = other.terminal.get_fb();
const Framebuffer& fb = terminal.get_fb();
const Framebuffer& other_fb = other.terminal.get_fb();
const int height = fb.ds.get_height();
const int other_height = other_fb.ds.get_height();
const int width = fb.ds.get_width();
@@ -197,17 +192,20 @@ bool Complete::compare( const Complete &other ) const
for ( int y = 0; y < height; y++ ) {
for ( int x = 0; x < width; x++ ) {
if ( fb.get_cell( y, x )->compare( *other_fb.get_cell( y, x ) ) ) {
fprintf( stderr, "Cell (%d, %d) differs.\n", y, x );
ret = true;
fprintf( stderr, "Cell (%d, %d) differs.\n", y, x );
ret = true;
}
}
}
if ( (fb.ds.get_cursor_row() != other_fb.ds.get_cursor_row())
|| (fb.ds.get_cursor_col() != other_fb.ds.get_cursor_col()) ) {
fprintf( stderr, "Cursor mismatch: (%d, %d) vs. (%d, %d).\n",
fb.ds.get_cursor_row(), fb.ds.get_cursor_col(),
other_fb.ds.get_cursor_row(), other_fb.ds.get_cursor_col() );
if ( ( fb.ds.get_cursor_row() != other_fb.ds.get_cursor_row() )
|| ( fb.ds.get_cursor_col() != other_fb.ds.get_cursor_col() ) ) {
fprintf( stderr,
"Cursor mismatch: (%d, %d) vs. (%d, %d).\n",
fb.ds.get_cursor_row(),
fb.ds.get_cursor_col(),
other_fb.ds.get_cursor_row(),
other_fb.ds.get_cursor_col() );
ret = true;
}
/* XXX should compare other terminal state too (mouse mode, bell. etc.) */
+35 -33
View File
@@ -42,46 +42,48 @@
/* This class represents the complete terminal -- a UTF8Parser feeding Actions to an Emulator. */
namespace Terminal {
class Complete {
private:
Parser::UTF8Parser parser;
Terminal::Emulator terminal;
Terminal::Display display;
class Complete
{
private:
Parser::UTF8Parser parser;
Terminal::Emulator terminal;
Terminal::Display display;
// Only used locally by act(), but kept here as a performance optimization,
// to avoid construction/destruction. It must always be empty
// outside calls to act() to keep horrible things from happening.
Parser::Actions actions;
// Only used locally by act(), but kept here as a performance optimization,
// to avoid construction/destruction. It must always be empty
// outside calls to act() to keep horrible things from happening.
Parser::Actions actions;
using input_history_type = std::list<std::pair<uint64_t, uint64_t>>;
input_history_type input_history;
uint64_t echo_ack;
using input_history_type = std::list<std::pair<uint64_t, uint64_t>>;
input_history_type input_history;
uint64_t echo_ack;
static const int ECHO_TIMEOUT = 50; /* for late ack */
static const int ECHO_TIMEOUT = 50; /* for late ack */
public:
Complete( size_t width, size_t height ) : parser(), terminal( width, height ), display( false ),
actions(), input_history(), echo_ack( 0 ) {}
std::string act( const std::string &str );
std::string act( const Parser::Action &act );
public:
Complete( size_t width, size_t height )
: parser(), terminal( width, height ), display( false ), actions(), input_history(), echo_ack( 0 )
{}
const Framebuffer & get_fb( void ) const { return terminal.get_fb(); }
void reset_input( void ) { parser.reset_input(); }
uint64_t get_echo_ack( void ) const { return echo_ack; }
bool set_echo_ack( uint64_t now );
void register_input_frame( uint64_t n, uint64_t now );
int wait_time( uint64_t now ) const;
std::string act( const std::string& str );
std::string act( const Parser::Action& act );
/* interface for Network::Transport */
void subtract( const Complete * ) const {}
std::string diff_from( const Complete &existing ) const;
std::string init_diff( void ) const;
void apply_string( const std::string & diff );
bool operator==( const Complete &x ) const;
const Framebuffer& get_fb( void ) const { return terminal.get_fb(); }
void reset_input( void ) { parser.reset_input(); }
uint64_t get_echo_ack( void ) const { return echo_ack; }
bool set_echo_ack( uint64_t now );
void register_input_frame( uint64_t n, uint64_t now );
int wait_time( uint64_t now ) const;
bool compare( const Complete &other ) const;
};
/* interface for Network::Transport */
void subtract( const Complete* ) const {}
std::string diff_from( const Complete& existing ) const;
std::string init_diff( void ) const;
void apply_string( const std::string& diff );
bool operator==( const Complete& x ) const;
bool compare( const Complete& other ) const;
};
}
#endif
+40 -45
View File
@@ -33,24 +33,22 @@
#include <cassert>
#include <typeinfo>
#include "src/protobufs/userinput.pb.h"
#include "src/statesync/user.h"
#include "src/util/fatal_assert.h"
#include "src/protobufs/userinput.pb.h"
using namespace Parser;
using namespace Network;
using namespace ClientBuffers;
void UserStream::subtract( const UserStream *prefix )
void UserStream::subtract( const UserStream* prefix )
{
// if we are subtracting ourself from ourself, just clear the std::deque
if ( this == prefix ) {
actions.clear();
return;
}
for ( std::deque<UserEvent>::const_iterator i = prefix->actions.begin();
i != prefix->actions.end();
i++ ) {
for ( std::deque<UserEvent>::const_iterator i = prefix->actions.begin(); i != prefix->actions.end(); i++ ) {
assert( this != prefix );
assert( !actions.empty() );
assert( *i == actions.front() );
@@ -58,13 +56,11 @@ void UserStream::subtract( const UserStream *prefix )
}
}
std::string UserStream::diff_from( const UserStream &existing ) const
std::string UserStream::diff_from( const UserStream& existing ) const
{
std::deque<UserEvent>::const_iterator my_it = actions.begin();
for ( std::deque<UserEvent>::const_iterator i = existing.actions.begin();
i != existing.actions.end();
i++ ) {
for ( std::deque<UserEvent>::const_iterator i = existing.actions.begin(); i != existing.actions.end(); i++ ) {
assert( my_it != actions.end() );
assert( *i == *my_it );
my_it++;
@@ -74,29 +70,28 @@ std::string UserStream::diff_from( const UserStream &existing ) const
while ( my_it != actions.end() ) {
switch ( my_it->type ) {
case UserByteType:
{
char the_byte = my_it->userbyte.c;
/* can we combine this with a previous Keystroke? */
if ( (output.instruction_size() > 0)
&& (output.instruction( output.instruction_size() - 1 ).HasExtension( keystroke )) ) {
output.mutable_instruction( output.instruction_size() - 1 )->MutableExtension( keystroke )->mutable_keys()->append( std::string( &the_byte, 1 ) );
} else {
Instruction *new_inst = output.add_instruction();
new_inst->MutableExtension( keystroke )->set_keys( &the_byte, 1 );
}
}
break;
case ResizeType:
{
Instruction *new_inst = output.add_instruction();
new_inst->MutableExtension( resize )->set_width( my_it->resize.width );
new_inst->MutableExtension( resize )->set_height( my_it->resize.height );
}
break;
default:
assert( !"unexpected event type" );
break;
case UserByteType: {
char the_byte = my_it->userbyte.c;
/* can we combine this with a previous Keystroke? */
if ( ( output.instruction_size() > 0 )
&& ( output.instruction( output.instruction_size() - 1 ).HasExtension( keystroke ) ) ) {
output.mutable_instruction( output.instruction_size() - 1 )
->MutableExtension( keystroke )
->mutable_keys()
->append( std::string( &the_byte, 1 ) );
} else {
Instruction* new_inst = output.add_instruction();
new_inst->MutableExtension( keystroke )->set_keys( &the_byte, 1 );
}
} break;
case ResizeType: {
Instruction* new_inst = output.add_instruction();
new_inst->MutableExtension( resize )->set_width( my_it->resize.width );
new_inst->MutableExtension( resize )->set_height( my_it->resize.height );
} break;
default:
assert( !"unexpected event type" );
break;
}
my_it++;
@@ -105,7 +100,7 @@ std::string UserStream::diff_from( const UserStream &existing ) const
return output.SerializeAsString();
}
void UserStream::apply_string( const std::string &diff )
void UserStream::apply_string( const std::string& diff )
{
ClientBuffers::UserMessage input;
fatal_assert( input.ParseFromString( diff ) );
@@ -114,25 +109,25 @@ void UserStream::apply_string( const std::string &diff )
if ( input.instruction( i ).HasExtension( keystroke ) ) {
std::string the_bytes = input.instruction( i ).GetExtension( keystroke ).keys();
for ( unsigned int loc = 0; loc < the_bytes.size(); loc++ ) {
actions.push_back( UserEvent( UserByte( the_bytes.at( loc ) ) ) );
actions.push_back( UserEvent( UserByte( the_bytes.at( loc ) ) ) );
}
} else if ( input.instruction( i ).HasExtension( resize ) ) {
actions.push_back( UserEvent( Resize( input.instruction( i ).GetExtension( resize ).width(),
input.instruction( i ).GetExtension( resize ).height() ) ) );
input.instruction( i ).GetExtension( resize ).height() ) ) );
}
}
}
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;
case ResizeType:
return actions[ i ].resize;
default:
assert( !"unexpected action type" );
static const Parser::Ignore nothing = Parser::Ignore();
return nothing;
switch ( actions[i].type ) {
case UserByteType:
return actions[i].userbyte;
case ResizeType:
return actions[i].resize;
default:
assert( !"unexpected action type" );
static const Parser::Ignore nothing = Parser::Ignore();
return nothing;
}
}
+44 -39
View File
@@ -41,52 +41,57 @@
#include "src/terminal/parseraction.h"
namespace Network {
enum UserEventType {
UserByteType = 0,
ResizeType = 1
};
enum UserEventType
{
UserByteType = 0,
ResizeType = 1
};
class UserEvent
class UserEvent
{
public:
UserEventType type;
Parser::UserByte userbyte;
Parser::Resize resize;
UserEvent( const Parser::UserByte& s_userbyte ) : type( UserByteType ), userbyte( s_userbyte ), resize( -1, -1 )
{}
UserEvent( const Parser::Resize& s_resize ) : type( ResizeType ), userbyte( 0 ), resize( s_resize ) {}
private:
UserEvent();
public:
bool operator==( const UserEvent& x ) const
{
public:
UserEventType type;
Parser::UserByte userbyte;
Parser::Resize resize;
return ( type == x.type ) && ( userbyte == x.userbyte ) && ( resize == x.resize );
}
};
UserEvent( const Parser::UserByte & s_userbyte ) : type( UserByteType ), userbyte( s_userbyte ), resize( -1, -1 ) {}
UserEvent( const Parser::Resize & s_resize ) : type( ResizeType ), userbyte( 0 ), resize( s_resize ) {}
class UserStream
{
private:
std::deque<UserEvent> actions;
private:
UserEvent();
public:
UserStream() : actions() {}
public:
bool operator==( const UserEvent &x ) const { return ( type == x.type ) && ( userbyte == x.userbyte ) && ( resize == x.resize ); }
};
void push_back( const Parser::UserByte& s_userbyte ) { actions.push_back( UserEvent( s_userbyte ) ); }
void push_back( const Parser::Resize& s_resize ) { actions.push_back( UserEvent( s_resize ) ); }
class UserStream
{
private:
std::deque<UserEvent> actions;
public:
UserStream() : actions() {}
void push_back( const Parser::UserByte & s_userbyte ) { actions.push_back( UserEvent( s_userbyte ) ); }
void push_back( const Parser::Resize & s_resize ) { actions.push_back( UserEvent( s_resize ) ); }
bool empty( void ) const { return actions.empty(); }
size_t size( void ) const { return actions.size(); }
const Parser::Action &get_action( unsigned int i ) const;
/* interface for Network::Transport */
void subtract( const UserStream *prefix );
std::string diff_from( const UserStream &existing ) const;
std::string init_diff( void ) const { return diff_from( UserStream() ); };
void apply_string( const std::string &diff );
bool operator==( const UserStream &x ) const { return actions == x.actions; }
bool empty( void ) const { return actions.empty(); }
size_t size( void ) const { return actions.size(); }
const Parser::Action& get_action( unsigned int i ) const;
bool compare( const UserStream & ) { return false; }
};
/* interface for Network::Transport */
void subtract( const UserStream* prefix );
std::string diff_from( const UserStream& existing ) const;
std::string init_diff( void ) const { return diff_from( UserStream() ); };
void apply_string( const std::string& diff );
bool operator==( const UserStream& x ) const { return actions == x.actions; }
bool compare( const UserStream& ) { return false; }
};
}
#endif