From 262a19e5d92229cbfecedbf36c12b040f15cb34c Mon Sep 17 00:00:00 2001 From: Keith Winstein Date: Sun, 18 Mar 2012 04:53:14 -0400 Subject: [PATCH] Give mosh-client option to print number of colors --- man/mosh-client.1 | 5 +++++ src/frontend/mosh-client.cc | 42 +++++++++++++++++++++++++++++++++---- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/man/mosh-client.1 b/man/mosh-client.1 index f9bef79..8179f3a 100644 --- a/man/mosh-client.1 +++ b/man/mosh-client.1 @@ -21,6 +21,8 @@ mosh-client \- client-side helper for mosh MOSH_KEY=KEY .B mosh-client IP PORT +.B mosh-client +-c .br .SH DESCRIPTION \fBmosh-client\fP is a helper program for the @@ -43,6 +45,9 @@ For constructing new setup wrappers for remote execution facilities other than SSH, it may be necessary to invoke \fBmosh-client\fP directly. +With the -c option, \fBmosh-client\fP instead prints the number of colors +of the terminal given by the TERM environment variable. + .SH SEE ALSO .BR mosh (1), .BR mosh-server (1). diff --git a/src/frontend/mosh-client.cc b/src/frontend/mosh-client.cc index c97ff45..44dda34 100644 --- a/src/frontend/mosh-client.cc +++ b/src/frontend/mosh-client.cc @@ -20,31 +20,65 @@ #include #include +#include #include "stmclient.h" #include "crypto.h" +/* these need to be included last because of conflicting defines */ +#include +#include + void usage( const char *argv0 ) { fprintf( stderr, "mosh-client (%s)\n", PACKAGE_STRING ); fprintf( stderr, "Copyright 2012 Keith Winstein \n" ); fprintf( stderr, "License GPLv3+: GNU GPL version 3 or later .\nThis is free software: you are free to change and redistribute it.\nThere is NO WARRANTY, to the extent permitted by law.\n\n" ); - fprintf( stderr, "Usage: %s IP PORT\n", argv0 ); + fprintf( stderr, "Usage: %s IP PORT\n %s -c\n", argv0, argv0 ); +} + +void print_colorcount( void ) +{ + /* check colors */ + setupterm((char *)0, 1, (int *)0); + + char colors_name[] = "colors"; + int color_val = tigetnum( colors_name ); + if ( color_val == -2 ) { + fprintf( stderr, "Invalid terminfo numeric capability: %s\n", + colors_name ); + } + + printf( "%d\n", color_val ); } int main( int argc, char *argv[] ) { /* Get arguments */ + int opt; + while ( (opt = getopt( argc, argv, "c" )) != -1 ) { + switch ( opt ) { + case 'c': + print_colorcount(); + exit( 0 ); + break; + default: + usage( argv[ 0 ] ); + exit( 1 ); + break; + } + } + char *ip; int port; - if ( argc != 3 ) { + if ( argc - optind != 2 ) { usage( argv[ 0 ] ); exit( 1 ); } - ip = argv[ 1 ]; - port = myatoi( argv[ 2 ] ); + ip = argv[ optind ]; + port = myatoi( argv[ optind + 1 ] ); /* Read key from environment */ char *env_key = getenv( "MOSH_KEY" );