From 309e38d88995e9ed88c5e18f8bdb05f86701b11f Mon Sep 17 00:00:00 2001 From: Keith Winstein Date: Mon, 13 Feb 2012 06:07:26 -0500 Subject: [PATCH] Use environment variable to convey prediction display preference --- scripts/mosh | 29 +++++++++++++++++++++++++---- src/frontend/mosh-client.cc | 16 +++++++--------- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/scripts/mosh b/scripts/mosh index 6483b61..ea2d217 100755 --- a/scripts/mosh +++ b/scripts/mosh @@ -26,7 +26,8 @@ $|=1; my $client = 'mosh-client'; my $server = 'mosh-server'; -my $predict = 'adaptive'; + +my $predict = undef; my $usage = qq{Usage: $0 [options] [user@]host @@ -40,6 +41,17 @@ qq{Usage: $0 [options] [user@]host Please report bugs to mosh-devel\@mit.edu. Mosh home page: http://mosh.mit.edu\n}; +sub predict_check { + my ( $predict, $env_set ) = @_; + + if ( not exists { adaptive => 0, always => 0, never => 0 }->{ $predict } ) { + my $explanation = $env_set ? " (MOSH_PREDICTION_DISPLAY in environment)" : ""; + print STDERR qq{$0: Unknown mode \"$predict\"$explanation.\n\n}; + + die $usage; + } +} + GetOptions( 'client=s' => \$client, 'server=s' => \$server, 'predict=s' => \$predict, @@ -47,10 +59,18 @@ GetOptions( 'client=s' => \$client, 'n' => sub { $predict = 'never' }, 'fake-proxy!' => \my $fake_proxy ) or die $usage; -if ( not exists { adaptive => 0, always => 0, never => 0 }->{ $predict } ) { - die $usage; +if ( defined $predict ) { + predict_check( $predict, 0 ); +} elsif ( defined $ENV{ 'MOSH_PREDICTION_DISPLAY' } ) { + $predict = $ENV{ 'MOSH_PREDICTION_DISPLAY' }; + predict_check( $predict, 1 ); +} else { + $predict = 'adaptive'; + predict_check( $predict, 0 ); } +delete $ENV{ 'MOSH_PREDICTION_DISPLAY' }; + if ( defined $fake_proxy ) { use Errno qw(EINTR); use IO::Socket::INET; @@ -137,5 +157,6 @@ if ( $pid == 0 ) { # child # Now start real mosh client $ENV{ 'MOSH_KEY' } = $key; - exec {$client} ($client, $ip, $port, $predict); + $ENV{ 'MOSH_PREDICTION_DISPLAY' } = $predict; + exec {$client} ($client, $ip, $port); } diff --git a/src/frontend/mosh-client.cc b/src/frontend/mosh-client.cc index 30141ed..1a1ff19 100644 --- a/src/frontend/mosh-client.cc +++ b/src/frontend/mosh-client.cc @@ -23,7 +23,7 @@ #include "crypto.h" void usage( const char *argv0 ) { - fprintf( stderr, "Usage: %s IP PORT [always|never|adaptive]\n", argv0 ); + fprintf( stderr, "Usage: %s IP PORT\n", argv0 ); } int main( int argc, char *argv[] ) @@ -32,14 +32,8 @@ int main( int argc, char *argv[] ) char *ip; int port; - char *predict_mode = NULL; - - if ( argc == 4 ) { - predict_mode = argv[ 3 ]; - } else if ( argc == 3 ) { - /* do nothing */ - } else { - usage( argv[ 0 ]); + if ( argc != 3 ) { + usage( argv[ 0 ] ); exit( 1 ); } @@ -53,6 +47,10 @@ int main( int argc, char *argv[] ) exit( 1 ); } + /* Read prediction preference */ + char *predict_mode = getenv( "MOSH_PREDICTION_DISPLAY" ); + /* can be NULL */ + char *key = strdup( env_key ); if ( key == NULL ) { perror( "strdup" );