From d8c37fba58171629ed01ba81a4e0927ea432af71 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 9 Jul 2008 01:48:29 +0000 Subject: [PATCH] code cleanup --- cli/Makefile.am | 2 +- cli/{transmissioncli.c => cli.c} | 281 ++++++++++++++++--------------- 2 files changed, 144 insertions(+), 139 deletions(-) rename cli/{transmissioncli.c => cli.c} (67%) diff --git a/cli/Makefile.am b/cli/Makefile.am index ca8b21f19..d190b4ab2 100644 --- a/cli/Makefile.am +++ b/cli/Makefile.am @@ -5,7 +5,7 @@ bin_PROGRAMS = transmissioncli dist_man_MANS = transmissioncli.1 -transmissioncli_SOURCES = transmissioncli.c +transmissioncli_SOURCES = cli.c transmissioncli_LDADD = \ $(top_builddir)/libtransmission/libtransmission.a \ diff --git a/cli/transmissioncli.c b/cli/cli.c similarity index 67% rename from cli/transmissioncli.c rename to cli/cli.c index 12db0f9ad..92ce736bc 100644 --- a/cli/transmissioncli.c +++ b/cli/cli.c @@ -36,6 +36,7 @@ #include /* tr_wait */ #include /* tr_webRun */ +#define LINEWIDTH 80 #define MY_NAME "transmission-cli" static int showInfo = 0; @@ -64,15 +65,20 @@ static const char * comment = NULL; static int parseCommandLine ( int argc, const char ** argv ); static void sigHandler ( int signal ); -static char * -getStringRatio( float ratio ) +char* +tr_strlratio( char * buf, double ratio, size_t buflen ) { - static char string[20]; - - if( ratio == TR_RATIO_NA ) - return "n/a"; - snprintf( string, sizeof string, "%.3f", ratio ); - return string; + if( (int)ratio == TR_RATIO_NA ) + tr_strlcpy( buf, _( "None" ), buflen ); + else if( (int)ratio == TR_RATIO_INF ) + tr_strlcpy( buf, "Inf", buflen ); + else if( ratio < 10.0 ) + snprintf( buf, buflen, "%'.2f", ratio ); + else if( ratio < 100.0 ) + snprintf( buf, buflen, "%'.1f", ratio ); + else + snprintf( buf, buflen, "%'.0f", ratio ); + return buf; } static int @@ -95,9 +101,6 @@ escape( char * out, const uint8_t * in, int in_len ) /* rfc2396 */ *out = '\0'; } - -#define LINEWIDTH 80 - static void torrentStateChanged( tr_torrent * torrent UNUSED, cp_status_t status UNUSED, @@ -136,10 +139,90 @@ scrapeDoneFunc( struct tr_handle * session UNUSED, --leftToScrape; } +static void +dumpInfo( FILE * out, const tr_info * inf ) +{ + int i; + int prevTier = -1; + tr_file_index_t ff; + + fprintf( out, "hash:\t" ); + for( i=0; ihash[i] ); + fprintf( out, "\n" ); + + fprintf( out, "name:\t%s\n", inf->name ); + + for( i=0; itrackerCount; ++i ) { + if( prevTier != inf->trackers[i].tier ) { + prevTier = inf->trackers[i].tier; + fprintf( out, "\ntracker tier #%d:\n", (prevTier+1) ); + } + fprintf( out, "\tannounce:\t%s\n", inf->trackers[i].announce ); + } + + fprintf( out, "size:\t%"PRIu64" (%"PRIu64" * %d + %"PRIu64")\n", + inf->totalSize, inf->totalSize / inf->pieceSize, + inf->pieceSize, inf->totalSize % inf->pieceSize ); + + if( inf->comment[0] ) + fprintf( out, "comment:\t%s\n", inf->comment ); + if( inf->creator[0] ) + fprintf( out, "creator:\t%s\n", inf->creator ); + if( inf->isPrivate ) + fprintf( out, "private flag set\n" ); + + fprintf( out, "file(s):\n" ); + for( ff=0; fffileCount; ++ff ) + fprintf( out, "\t%s (%"PRIu64")\n", inf->files[ff].name, inf->files[ff].length ); +} + +static void +getStatusStr( const tr_stat * st, char * buf, size_t buflen ) +{ + if( st->status & TR_STATUS_CHECK_WAIT ) + { + snprintf( buf, buflen, "Waiting to verify local files" ); + } + else if( st->status & TR_STATUS_CHECK ) + { + snprintf( buf, buflen, "Verifying local files (%.2f%%, %.2f%% valid)", + 100 * st->recheckProgress, 100.0 * st->percentDone ); + } + else if( st->status & TR_STATUS_DOWNLOAD ) + { + char ratioStr[80]; + tr_strlratio( ratioStr, st->ratio, sizeof( ratioStr ) ); + snprintf( buf, buflen, + "Progress: %.1f%%, dl from %d of %d peers (%.0f KB/s), " + "ul to %d (%.0f KB/s) [%s]", + st->percentDone * 100.0, + st->peersSendingToUs, + st->peersConnected, + st->rateDownload, + st->peersGettingFromUs, + st->rateUpload, + ratioStr ); + } + else if( st->status & TR_STATUS_SEED ) + { + char ratioStr[80]; + tr_strlratio( ratioStr, st->ratio, sizeof( ratioStr ) ); + snprintf( buf, buflen, + "Seeding, uploading to %d of %d peer(s), %.0f KB/s [%s]", + st->peersGettingFromUs, st->peersConnected, + st->rateUpload, ratioStr ); + } + else if( st->status & TR_STATUS_STOPPED ) + { + *buf = '\0'; + } +} + int main( int argc, char ** argv ) { - int i, error; + int error; tr_handle * h; tr_ctor * ctor; tr_torrent * tor = NULL; @@ -273,39 +356,7 @@ main( int argc, char ** argv ) if( !tr_torrentParse( h, ctor, &info ) ) { - int prevTier = -1; - tr_file_index_t ff; - - printf( "hash:\t" ); - for( i=0; istatus & TR_STATUS_CHECK_WAIT ) - { - chars = snprintf( string, sizeof string, - "Waiting to verify local files..." ); - } - else if( s->status & TR_STATUS_CHECK ) - { - chars = snprintf( string, sizeof string, - "Verifying local files... %.2f%%, found %.2f%% valid", 100 * s->recheckProgress, 100.0 * s->percentDone ); - } - else if( s->status & TR_STATUS_DOWNLOAD ) - { - chars = snprintf( string, sizeof string, - "Progress: %.2f %%, %d peer%s, dl from %d (%.2f KB/s), " - "ul to %d (%.2f KB/s) [%s]", 100.0 * s->percentDone, - s->peersConnected, ( s->peersConnected == 1 ) ? "" : "s", - s->peersSendingToUs, s->rateDownload, - s->peersGettingFromUs, s->rateUpload, - getStringRatio(s->ratio) ); - } - else if( s->status & TR_STATUS_SEED ) - { - chars = snprintf( string, sizeof string, - "Seeding, uploading to %d of %d peer(s), %.2f KB/s [%s]", - s->peersGettingFromUs, s->peersConnected, - s->rateUpload, getStringRatio(s->ratio) ); - } - else if( s->status & TR_STATUS_STOPPED ) - { + st = tr_torrentStat( tor ); + if( st->status & TR_STATUS_STOPPED ) break; - } - if( ( signed )sizeof string > chars ) - { - memset( &string[chars], ' ', sizeof string - 1 - chars ); - } - string[sizeof string - 1] = '\0'; - fprintf( stderr, "\r%s", string ); - if( s->error ) - { - fprintf( stderr, "\n%s\n", s->errorString ); - } - else if( verboseLevel > 0 ) - { - fprintf( stderr, "\n" ); - } + getStatusStr( st, line, sizeof( line ) ); + printf( "%-*s\n", LINEWIDTH, line ); + if( st->error ) + printf( "%s\n", st->errorString ); } - fprintf( stderr, "\n" ); - /* try for 5 seconds to delete any port mappings for nat traversal */ - tr_sessionSetPortForwardingEnabled( h, 0 ); - for( i=0; i<10; ++i ) { - const tr_port_forwarding f = tr_sessionGetPortForwarding( h ); - if( f == TR_PORT_UNMAPPED ) - break; - tr_wait( 500 ); - } - cleanup: + printf( "\n" ); tr_sessionClose( h ); - return EXIT_SUCCESS; } @@ -441,27 +438,35 @@ getUsage( void ) } const struct tr_option options[] = { - { 'a', "announce", "When creating a new torrent, set its announce URL", "a", 1, "" }, - { 'b', "blocklist", "Enable peer blocklists", "b", 0, NULL }, - { 'B', "no-blocklist", "Disable peer blocklists", "B", 0, NULL }, - { 'c', "comment", "When creating a new torrent, set its comment field", "c", 1, "" }, - { 'd', "downlimit", "Set the maxiumum download speed in KB/s", "d", 1, "" }, - { 'D', "no-downlimit", "Don't limit the download speed", "D", 0, NULL }, - { 'e', "encryption", "Set encryption mode [required, preferred, tolerated]", "e", 1, "" }, - { 'f', "finish", "Set a script to run when the torrent finishes", "f", 1, "