/* * This file Copyright (C) 2008-2010 Mnemosyne LLC * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 * as published by the Free Software Foundation. * * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html * * $Id$ */ #include #include /* isspace */ #include #include #include #include #include /* strcmp */ #ifdef WIN32 #include /* getcwd */ #else #include /* getcwd */ #endif #include #define CURL_DISABLE_TYPECHECK /* otherwise -Wunreachable-code goes insane */ #include #include #include #include #include #include #include #include #define MY_NAME "transmission-remote" #define DEFAULT_HOST "localhost" #define DEFAULT_PORT atoi(TR_DEFAULT_RPC_PORT_STR) #define ARGUMENTS "arguments" /*** **** **** Display Utilities **** ***/ static void etaToString( char * buf, size_t buflen, int64_t eta ) { if( eta < 0 ) tr_snprintf( buf, buflen, "Unknown" ); else if( eta < 60 ) tr_snprintf( buf, buflen, "%" PRId64 "sec", eta ); else if( eta < ( 60 * 60 ) ) tr_snprintf( buf, buflen, "%" PRId64 " min", eta / 60 ); else if( eta < ( 60 * 60 * 24 ) ) tr_snprintf( buf, buflen, "%" PRId64 " hrs", eta / ( 60 * 60 ) ); else tr_snprintf( buf, buflen, "%" PRId64 " days", eta / ( 60 * 60 * 24 ) ); } static char* tr_strltime( char * buf, int seconds, size_t buflen ) { int days, hours, minutes; char d[128], h[128], m[128], s[128]; if( seconds < 0 ) seconds = 0; days = seconds / 86400; hours = ( seconds % 86400 ) / 3600; minutes = ( seconds % 3600 ) / 60; seconds = ( seconds % 3600 ) % 60; tr_snprintf( d, sizeof( d ), "%'d day%s", days, days==1?"":"s" ); tr_snprintf( h, sizeof( h ), "%'d hour%s", hours, hours==1?"":"s" ); tr_snprintf( m, sizeof( m ), "%'d minute%s", minutes, minutes==1?"":"s" ); tr_snprintf( s, sizeof( s ), "%'d second%s", seconds, seconds==1?"":"s" ); if( days ) { if( days >= 4 || !hours ) tr_strlcpy( buf, d, buflen ); else tr_snprintf( buf, buflen, "%s, %s", d, h ); } else if( hours ) { if( hours >= 4 || !minutes ) tr_strlcpy( buf, h, buflen ); else tr_snprintf( buf, buflen, "%s, %s", h, m ); } else if( minutes ) { if( minutes >= 4 || !seconds ) tr_strlcpy( buf, m, buflen ); else tr_snprintf( buf, buflen, "%s, %s", m, s ); } else tr_strlcpy( buf, s, buflen ); return buf; } static const double KiB = 1024.0; static const double MiB = 1024.0 * 1024.0; static const double GiB = 1024.0 * 1024.0 * 1024.0; static char* strlpercent( char * buf, double x, size_t buflen ) { return tr_strpercent( buf, x, buflen ); } static char* strlratio2( char * buf, double ratio, size_t buflen ) { return tr_strratio( buf, buflen, ratio, "Inf" ); } static char* strlratio( char * buf, double numerator, double denominator, size_t buflen ) { double ratio; if( denominator ) ratio = numerator / denominator; else if( numerator ) ratio = TR_RATIO_INF; else ratio = TR_RATIO_NA; return strlratio2( buf, ratio, buflen ); } static char* strlsize( char * buf, int64_t bytes, size_t buflen ) { if( !bytes ) tr_strlcpy( buf, _( "None" ), buflen ); else tr_formatter_size( buf, bytes, buflen ); return buf; } static char* strlspeed( char * buf, int64_t bytes_per_second, size_t buflen ) { if( !bytes_per_second ) tr_strlcpy( buf, _( "None" ), buflen ); else tr_formatter_speed( buf, bytes_per_second, buflen ); return buf; } enum { TAG_SESSION, TAG_STATS, TAG_LIST, TAG_DETAILS, TAG_FILES, TAG_PEERS, TAG_PORTTEST, TAG_TORRENT_ADD }; static const char* getUsage( void ) { return "Transmission " LONG_VERSION_STRING " http://www.transmissionbt.com/\n" "A fast and easy BitTorrent client\n" "\n" "Usage: " MY_NAME " [host] [options]\n" " " MY_NAME " [port] [options]\n" " " MY_NAME " [host:port] [options]\n" "\n" "See the man page for detailed explanations and many examples."; } /*** **** **** Command-Line Arguments **** ***/ static tr_option opts[] = { { 'a', "add", "Add torrent files by filename or URL", "a", 0, NULL }, { 970, "alt-speed", "Use the alternate Limits", "as", 0, NULL }, { 971, "no-alt-speed", "Don't use the alternate Limits", "AS", 0, NULL }, { 972, "alt-speed-downlimit", "max alternate download speed (in KiB/s)", "asd", 1, "" }, { 973, "alt-speed-uplimit", "max alternate upload speed (in KiB/s)", "asu", 1, "" }, { 974, "alt-speed-scheduler", "Use the scheduled on/off times", "asc", 0, NULL }, { 975, "no-alt-speed-scheduler", "Don't use the scheduled on/off times", "ASC", 0, NULL }, { 976, "alt-speed-time-begin", "Time to start using the alt speed limits (in hhmm)", NULL, 1, "