From 43785eb820de0175d0d98ca12abcde86ffb7b740 Mon Sep 17 00:00:00 2001 From: John Hood Date: Mon, 7 Nov 2016 00:37:53 -0500 Subject: [PATCH] Collapse nested conditionals. --- src/frontend/mosh-server.cc | 32 ++++++++---------- src/frontend/stmclient.cc | 28 ++++++---------- src/frontend/terminaloverlay.cc | 59 ++++++++++++++++----------------- src/network/network.cc | 8 ++--- src/terminal/terminal.cc | 7 ++-- 5 files changed, 59 insertions(+), 75 deletions(-) diff --git a/src/frontend/mosh-server.cc b/src/frontend/mosh-server.cc index 33427e0..92756bf 100644 --- a/src/frontend/mosh-server.cc +++ b/src/frontend/mosh-server.cc @@ -826,12 +826,11 @@ static void serve( int host_fd, Terminal::Complete &terminal, ServerConnection & fprintf( stderr, "Network idle for %llu seconds.\n", static_cast( time_since_remote_state / 1000 ) ); } - if ( sel.signal( SIGUSR1 ) ) { - if ( !network_signaled_timeout_ms || network_signaled_timeout_ms <= time_since_remote_state ) { - idle_shutdown = true; - fprintf( stderr, "Network idle for %llu seconds when SIGUSR1 received\n", - static_cast( time_since_remote_state / 1000 ) ); - } + if ( sel.signal( SIGUSR1 ) + && ( !network_signaled_timeout_ms || network_signaled_timeout_ms <= time_since_remote_state ) ) { + idle_shutdown = true; + fprintf( stderr, "Network idle for %llu seconds when SIGUSR1 received\n", + static_cast( time_since_remote_state / 1000 ) ); } if ( sel.any_signal() || idle_shutdown ) { @@ -860,24 +859,21 @@ static void serve( int host_fd, Terminal::Complete &terminal, ServerConnection & #ifdef HAVE_UTEMPTER /* update utmp if has been more than 30 seconds since heard from client */ - if ( connected_utmp ) { - if ( time_since_remote_state > 30000 ) { - utempter_remove_record( host_fd ); + if ( connected_utmp + && time_since_remote_state > 30000 ) { + utempter_remove_record( host_fd ); - char tmp[ 64 ]; - snprintf( tmp, 64, "mosh [%d]", getpid() ); - utempter_add_record( host_fd, tmp ); + char tmp[ 64 ]; + snprintf( tmp, 64, "mosh [%d]", getpid() ); + utempter_add_record( host_fd, tmp ); - connected_utmp = false; - } + connected_utmp = false; } #endif - if ( terminal.set_echo_ack( now ) ) { + if ( terminal.set_echo_ack( now ) && !network.shutdown_in_progress() ) { /* update client with new echo ack */ - if ( !network.shutdown_in_progress() ) { - network.set_current_state( terminal ); - } + network.set_current_state( terminal ); } if ( !network.get_remote_state_num() diff --git a/src/frontend/stmclient.cc b/src/frontend/stmclient.cc index 44611d9..0720b24 100644 --- a/src/frontend/stmclient.cc +++ b/src/frontend/stmclient.cc @@ -217,11 +217,9 @@ void STMClient::shutdown( void ) "Please verify that UDP port %s is not firewalled and can reach the server.\n\n" "(By default, mosh uses a UDP port between 60000 and 61000. The -p option\n" "selects a specific UDP port number.)\n", ip.c_str(), port.c_str(), port.c_str() ); - } else if ( network ) { - if ( !clean_shutdown ) { - fputs( "\n\nmosh did not shut down cleanly. Please note that the\n" - "mosh-server process may still be running on the server.\n", stderr ); - } + } else if ( network && !clean_shutdown ) { + fputs( "\n\nmosh did not shut down cleanly. Please note that the\n" + "mosh-server process may still be running on the server.\n", stderr ); } } @@ -481,21 +479,17 @@ bool STMClient::main( void ) process_network_input(); } - if ( sel.read( STDIN_FILENO ) ) { - /* input from the user needs to be fed to the network */ - if ( !process_user_input( STDIN_FILENO ) ) { - if ( !network->has_remote_addr() ) { - break; - } else if ( !network->shutdown_in_progress() ) { - overlays.get_notification_engine().set_notification_string( wstring( L"Exiting..." ), true ); - network->start_shutdown(); - } + if ( sel.read( STDIN_FILENO ) && !process_user_input( STDIN_FILENO ) ) { /* input from the user needs to be fed to the network */ + if ( !network->has_remote_addr() ) { + break; + } else if ( !network->shutdown_in_progress() ) { + overlays.get_notification_engine().set_notification_string( wstring( L"Exiting..." ), true ); + network->start_shutdown(); } } - if ( sel.signal( SIGWINCH ) ) { - /* resize */ - if ( !process_resize() ) { return false; } + if ( sel.signal( SIGWINCH ) && !process_resize() ) { /* resize */ + return false; } if ( sel.signal( SIGCONT ) ) { diff --git a/src/frontend/terminaloverlay.cc b/src/frontend/terminaloverlay.cc index 9537976..43d13d4 100644 --- a/src/frontend/terminaloverlay.cc +++ b/src/frontend/terminaloverlay.cc @@ -552,11 +552,10 @@ void PredictionEngine::cull( const Framebuffer &fb ) } /* When predictions come in quickly, slowly take away the glitch trigger. */ - if ( (now - j->prediction_time) < GLITCH_THRESHOLD ) { - if ( (glitch_trigger > 0) && (now - GLITCH_REPAIR_MININTERVAL >= last_quick_confirmation) ) { - glitch_trigger--; - last_quick_confirmation = now; - } + if ( now - j->prediction_time < GLITCH_THRESHOLD + && ( glitch_trigger > 0 && now - GLITCH_REPAIR_MININTERVAL >= last_quick_confirmation ) ) { + glitch_trigger--; + last_quick_confirmation = now; } /* match rest of row to the actual renditions */ @@ -594,24 +593,23 @@ void PredictionEngine::cull( const Framebuffer &fb ) } /* go through cursor predictions */ - if ( !cursors.empty() ) { - if ( cursor().get_validity( fb, + if ( !cursors.empty() + && cursor().get_validity( fb, local_frame_acked, local_frame_late_acked ) == IncorrectOrExpired ) { - /* + /* fprintf( stderr, "Sadly, we're predicting (%d,%d) vs. (%d,%d) [tau: %ld, expiration_time=%ld, now=%ld]\n", - cursor().row, cursor().col, - fb.ds.get_cursor_row(), - fb.ds.get_cursor_col(), - cursor().tentative_until_epoch, - cursor().expiration_time, - now ); - */ - if ( display_preference == Experimental ) { - cursors.clear(); - } else { - reset(); - return; - } + cursor().row, cursor().col, + fb.ds.get_cursor_row(), + fb.ds.get_cursor_col(), + cursor().tentative_until_epoch, + cursor().expiration_time, + now ); + */ + if ( display_preference == Experimental ) { + cursors.clear(); + } else { + reset(); + return; } } @@ -635,17 +633,16 @@ ConditionalOverlayRow & PredictionEngine::get_or_make_row( int row_num, int num_ if ( it != overlays.end() ) { return *it; - } else { - /* make row */ - ConditionalOverlayRow r( row_num ); - r.overlay_cells.reserve( num_cols ); - for ( int i = 0; i < num_cols; i++ ) { - r.overlay_cells.push_back( ConditionalOverlayCell( 0, i, prediction_epoch ) ); - assert( r.overlay_cells[ i ].col == i ); - } - overlays.push_back( r ); - return overlays.back(); } + /* make row */ + ConditionalOverlayRow r( row_num ); + r.overlay_cells.reserve( num_cols ); + for ( int i = 0; i < num_cols; i++ ) { + r.overlay_cells.push_back( ConditionalOverlayCell( 0, i, prediction_epoch ) ); + assert( r.overlay_cells[ i ].col == i ); + } + overlays.push_back( r ); + return overlays.back(); } void PredictionEngine::new_user_byte( char the_byte, const Framebuffer &fb ) diff --git a/src/network/network.cc b/src/network/network.cc index 26da603..b7cc79d 100644 --- a/src/network/network.cc +++ b/src/network/network.cc @@ -173,11 +173,9 @@ Connection::Socket::Socket( int family ) /* request explicit congestion notification on received datagrams */ #ifdef HAVE_IP_RECVTOS int tosflag = true; - if ( setsockopt( _fd, IPPROTO_IP, IP_RECVTOS, &tosflag, sizeof tosflag ) < 0 ) { - /* FreeBSD disallows this option on IPv6 sockets. */ - if ( family == IPPROTO_IP ) { - perror( "setsockopt( IP_RECVTOS )" ); - } + if ( setsockopt( _fd, IPPROTO_IP, IP_RECVTOS, &tosflag, sizeof tosflag ) < 0 + && family == IPPROTO_IP ) { /* FreeBSD disallows this option on IPv6 sockets. */ + perror( "setsockopt( IP_RECVTOS )" ); } #endif } diff --git a/src/terminal/terminal.cc b/src/terminal/terminal.cc index 2f9119b..5a010e6 100644 --- a/src/terminal/terminal.cc +++ b/src/terminal/terminal.cc @@ -109,10 +109,9 @@ void Emulator::print( const Parser::Print *act ) this_cell->set_wide( chwidth == 2 ); /* chwidth had better be 1 or 2 here */ fb.apply_renditions_to_cell( this_cell ); - if ( chwidth == 2 ) { /* erase overlapped cell */ - if ( fb.ds.get_cursor_col() + 1 < fb.ds.get_width() ) { - fb.reset_cell( fb.get_mutable_cell( fb.ds.get_cursor_row(), fb.ds.get_cursor_col() + 1 ) ); - } + if ( chwidth == 2 + && fb.ds.get_cursor_col() + 1 < fb.ds.get_width() ) { /* erase overlapped cell */ + fb.reset_cell( fb.get_mutable_cell( fb.ds.get_cursor_row(), fb.ds.get_cursor_col() + 1 ) ); } fb.ds.move_col( chwidth, true, true );