mirror of
https://github.com/transmission/transmission.git
synced 2025-12-21 19:08:36 +00:00
code cleanup
This commit is contained in:
33
cli/cli.c
33
cli/cli.c
@@ -165,16 +165,17 @@ dumpInfo( FILE * out, const tr_info * inf )
|
|||||||
inf->totalSize, inf->totalSize / inf->pieceSize,
|
inf->totalSize, inf->totalSize / inf->pieceSize,
|
||||||
inf->pieceSize, inf->totalSize % inf->pieceSize );
|
inf->pieceSize, inf->totalSize % inf->pieceSize );
|
||||||
|
|
||||||
if( inf->comment[0] )
|
if( inf->comment && *inf->comment )
|
||||||
fprintf( out, "comment:\t%s\n", inf->comment );
|
fprintf( out, "comment:\t%s\n", inf->comment );
|
||||||
if( inf->creator[0] )
|
if( inf->creator && *inf->creator )
|
||||||
fprintf( out, "creator:\t%s\n", inf->creator );
|
fprintf( out, "creator:\t%s\n", inf->creator );
|
||||||
if( inf->isPrivate )
|
if( inf->isPrivate )
|
||||||
fprintf( out, "private flag set\n" );
|
fprintf( out, "private flag set\n" );
|
||||||
|
|
||||||
fprintf( out, "file(s):\n" );
|
fprintf( out, "file(s):\n" );
|
||||||
for( ff=0; ff<inf->fileCount; ++ff )
|
for( ff=0; ff<inf->fileCount; ++ff )
|
||||||
fprintf( out, "\t%s (%"PRIu64")\n", inf->files[ff].name, inf->files[ff].length );
|
fprintf( out, "\t%s (%"PRIu64")\n", inf->files[ff].name,
|
||||||
|
inf->files[ff].length );
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@@ -213,10 +214,7 @@ getStatusStr( const tr_stat * st, char * buf, size_t buflen )
|
|||||||
st->peersGettingFromUs, st->peersConnected,
|
st->peersGettingFromUs, st->peersConnected,
|
||||||
st->rateUpload, ratioStr );
|
st->rateUpload, ratioStr );
|
||||||
}
|
}
|
||||||
else if( st->status & TR_STATUS_STOPPED )
|
else *buf = '\0';
|
||||||
{
|
|
||||||
*buf = '\0';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
@@ -296,17 +294,17 @@ main( int argc, char ** argv )
|
|||||||
if( sourceFile && *sourceFile ) /* creating a torrent */
|
if( sourceFile && *sourceFile ) /* creating a torrent */
|
||||||
{
|
{
|
||||||
int err;
|
int err;
|
||||||
tr_metainfo_builder * builder = tr_metaInfoBuilderCreate( h, sourceFile );
|
tr_metainfo_builder * b = tr_metaInfoBuilderCreate( h, sourceFile );
|
||||||
tr_tracker_info ti;
|
tr_tracker_info ti;
|
||||||
ti.tier = 0;
|
ti.tier = 0;
|
||||||
ti.announce = (char*) announce;
|
ti.announce = (char*) announce;
|
||||||
tr_makeMetaInfo( builder, torrentPath, &ti, 1, comment, isPrivate );
|
tr_makeMetaInfo( b, torrentPath, &ti, 1, comment, isPrivate );
|
||||||
while( !builder->isDone ) {
|
while( !b->isDone ) {
|
||||||
tr_wait( 1000 );
|
tr_wait( 1000 );
|
||||||
printf( "." );
|
printf( "." );
|
||||||
}
|
}
|
||||||
err = builder->result;
|
err = b->result;
|
||||||
tr_metaInfoBuilderFree( builder );
|
tr_metaInfoBuilderFree( b );
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -338,6 +336,7 @@ main( int argc, char ** argv )
|
|||||||
tr_httpParseURL( scrape, -1, &host, NULL, NULL );
|
tr_httpParseURL( scrape, -1, &host, NULL, NULL );
|
||||||
++leftToScrape;
|
++leftToScrape;
|
||||||
tr_webRun( h, url, NULL, scrapeDoneFunc, host );
|
tr_webRun( h, url, NULL, scrapeDoneFunc, host );
|
||||||
|
tr_free( host );
|
||||||
tr_free( url );
|
tr_free( url );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -393,16 +392,16 @@ main( int argc, char ** argv )
|
|||||||
|
|
||||||
if( gotsig ) {
|
if( gotsig ) {
|
||||||
gotsig = 0;
|
gotsig = 0;
|
||||||
fprintf( stderr, "stopping torrent...\n" );
|
printf( "stopping torrent...\n" );
|
||||||
tr_torrentStop( tor );
|
tr_torrentStop( tor );
|
||||||
}
|
}
|
||||||
|
|
||||||
if( manualUpdate ) {
|
if( manualUpdate ) {
|
||||||
manualUpdate = 0;
|
manualUpdate = 0;
|
||||||
if ( !tr_torrentCanManualUpdate( tor ) )
|
if ( !tr_torrentCanManualUpdate( tor ) )
|
||||||
fprintf( stderr, "\rReceived SIGHUP, but can't send a manual update now\n" );
|
fprintf( stderr, "\nReceived SIGHUP, but can't send a manual update now\n" );
|
||||||
else {
|
else {
|
||||||
fprintf( stderr, "\rReceived SIGHUP: manual update scheduled\n" );
|
fprintf( stderr, "\nReceived SIGHUP: manual update scheduled\n" );
|
||||||
tr_torrentManualUpdate( tor );
|
tr_torrentManualUpdate( tor );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -412,9 +411,9 @@ fprintf( stderr, "stopping torrent...\n" );
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
getStatusStr( st, line, sizeof( line ) );
|
getStatusStr( st, line, sizeof( line ) );
|
||||||
printf( "%-*s\n", LINEWIDTH, line );
|
printf( "\r%-*s", LINEWIDTH, line );
|
||||||
if( st->error )
|
if( st->error )
|
||||||
printf( "%s\n", st->errorString );
|
printf( "\n%s\n", st->errorString );
|
||||||
}
|
}
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
|
|||||||
@@ -1,11 +1,23 @@
|
|||||||
/*
|
/*
|
||||||
* This file Copyright (C) 2008 Charles Kerr <charles@rebelbase.com>
|
* This file Copyright (C) 2008 Charles Kerr <charles@rebelbase.com>
|
||||||
*
|
*
|
||||||
* This file is licensed by the GPL version 2. Works owned by the
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
* Transmission project are granted a special exemption to clause 2(b)
|
* copy of this software and associated documentation files (the "Software"),
|
||||||
* so that the bulk of its code can remain under the MIT license.
|
* to deal in the Software without restriction, including without limitation
|
||||||
* This exemption does not extend to derived works not owned by
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||||
* the Transmission project.
|
* and/or sell copies of the Software, and to permit persons to whom the
|
||||||
|
* Software is furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||||
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
|
* DEALINGS IN THE SOFTWARE.
|
||||||
*
|
*
|
||||||
* $Id:$
|
* $Id:$
|
||||||
*/
|
*/
|
||||||
@@ -20,14 +32,21 @@
|
|||||||
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
|
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int option_index = 1;
|
int tr_optind = 1;
|
||||||
|
|
||||||
static const char*
|
static const char*
|
||||||
getArgName( const tr_option * opt )
|
getArgName( const tr_option * opt )
|
||||||
{
|
{
|
||||||
if( !opt->has_arg ) return "";
|
char * arg;
|
||||||
if( opt->argName ) return opt->argName;
|
|
||||||
return "<args>";
|
if( !opt->has_arg )
|
||||||
|
arg = "";
|
||||||
|
else if( opt->argName )
|
||||||
|
arg = opt->argName;
|
||||||
|
else
|
||||||
|
arg = "<args>";
|
||||||
|
|
||||||
|
return arg;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@@ -43,46 +62,50 @@ getopts_usage_line( const tr_option * opt,
|
|||||||
opt->description );
|
opt->description );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
maxWidth( const struct tr_option * o,
|
||||||
|
int * longWidth, int * shortWidth, int * argWidth )
|
||||||
|
{
|
||||||
|
const char * arg;
|
||||||
|
|
||||||
|
if( o->longName )
|
||||||
|
*longWidth = MAX( *longWidth, (int)strlen( o->longName ) );
|
||||||
|
|
||||||
|
if( o->shortName )
|
||||||
|
*shortWidth = MAX( *shortWidth, (int)strlen( o->shortName ) );
|
||||||
|
|
||||||
|
if(( arg = getArgName( o )))
|
||||||
|
*argWidth = MAX( *argWidth, (int)strlen( arg ) );
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
tr_getopt_usage( const char * progName,
|
tr_getopt_usage( const char * progName,
|
||||||
const char * description,
|
const char * description,
|
||||||
const struct tr_option opts[] )
|
const struct tr_option opts[] )
|
||||||
{
|
{
|
||||||
int count;
|
|
||||||
int longWidth = 0;
|
int longWidth = 0;
|
||||||
int shortWidth = 0;
|
int shortWidth = 0;
|
||||||
int argWidth = 0;
|
int argWidth = 0;
|
||||||
struct tr_option help;
|
struct tr_option help;
|
||||||
|
const struct tr_option * o;
|
||||||
|
|
||||||
for( count=0; opts[count].description; ++count )
|
for( o=opts; o->val; ++o )
|
||||||
{
|
maxWidth( o, &longWidth, &shortWidth, &argWidth );
|
||||||
const char * arg;
|
|
||||||
|
|
||||||
if( opts[count].longName )
|
|
||||||
longWidth = MAX( longWidth, (int)strlen( opts[count].longName ) );
|
|
||||||
|
|
||||||
if( opts[count].shortName )
|
|
||||||
shortWidth = MAX( shortWidth, (int)strlen( opts[count].shortName ) );
|
|
||||||
|
|
||||||
if(( arg = getArgName( &opts[count] )))
|
|
||||||
argWidth = MAX( argWidth, (int)strlen( arg ) );
|
|
||||||
}
|
|
||||||
|
|
||||||
if( !description )
|
|
||||||
description = "Usage: %s [options]";
|
|
||||||
printf( description, progName );
|
|
||||||
printf( "\n\n" );
|
|
||||||
printf( "Options:\n" );
|
|
||||||
|
|
||||||
help.val = -1;
|
help.val = -1;
|
||||||
help.longName = "help";
|
help.longName = "help";
|
||||||
help.description = "Display this help page and exit";
|
help.description = "Display this help page and exit";
|
||||||
help.shortName = "h";
|
help.shortName = "h";
|
||||||
help.has_arg = 0;
|
help.has_arg = 0;
|
||||||
getopts_usage_line( &help, longWidth, shortWidth, argWidth );
|
maxWidth( &help, &longWidth, &shortWidth, &argWidth );
|
||||||
|
|
||||||
for( count=0; opts[count].description; ++count )
|
if( description == NULL )
|
||||||
getopts_usage_line( &opts[count], longWidth, shortWidth, argWidth );
|
description = "Usage: %s [options]";
|
||||||
|
printf( description, progName );
|
||||||
|
printf( "\n\nOptions:\n" );
|
||||||
|
getopts_usage_line( &help, longWidth, shortWidth, argWidth );
|
||||||
|
for( o=opts; o->val; ++o )
|
||||||
|
getopts_usage_line( o, longWidth, shortWidth, argWidth );
|
||||||
}
|
}
|
||||||
|
|
||||||
static const tr_option *
|
static const tr_option *
|
||||||
@@ -123,16 +146,6 @@ findOption( const tr_option * opts,
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
|
||||||
showUsageAndExit( const char * appName,
|
|
||||||
const char * description,
|
|
||||||
const tr_option * opts )
|
|
||||||
{
|
|
||||||
tr_getopt_usage( appName, description, opts );
|
|
||||||
exit( 0 );
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
tr_getopt( const char * usage,
|
tr_getopt( const char * usage,
|
||||||
int argc,
|
int argc,
|
||||||
@@ -146,22 +159,22 @@ tr_getopt( const char * usage,
|
|||||||
|
|
||||||
*setme_optarg = NULL;
|
*setme_optarg = NULL;
|
||||||
|
|
||||||
if( argc==1 || argc==option_index )
|
|
||||||
return TR_OPT_DONE;
|
|
||||||
|
|
||||||
/* handle the builtin 'help' option */
|
/* handle the builtin 'help' option */
|
||||||
for( i=1; i<argc; ++i )
|
for( i=1; i<argc; ++i ) {
|
||||||
if( !strcmp(argv[i], "-h") || !strcmp(argv[i], "--help" ) )
|
if( !strcmp(argv[i], "-h") || !strcmp(argv[i], "--help" ) ) {
|
||||||
showUsageAndExit( argv[0], usage, opts );
|
tr_getopt_usage( argv[0], usage, opts );
|
||||||
|
exit( 0 );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* out of options */
|
/* out of options? */
|
||||||
if( option_index >= argc )
|
if( argc==1 || tr_optind>=argc )
|
||||||
return TR_OPT_DONE;
|
return TR_OPT_DONE;
|
||||||
|
|
||||||
o = findOption( opts, argv[option_index], &nest );
|
o = findOption( opts, argv[tr_optind], &nest );
|
||||||
if( !o ) {
|
if( !o ) {
|
||||||
/* let the user know we got an unknown option... */
|
/* let the user know we got an unknown option... */
|
||||||
*setme_optarg = argv[option_index++];
|
*setme_optarg = argv[tr_optind++];
|
||||||
return TR_OPT_UNK;
|
return TR_OPT_UNK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -170,23 +183,23 @@ tr_getopt( const char * usage,
|
|||||||
if( nest )
|
if( nest )
|
||||||
return TR_OPT_ERR;
|
return TR_OPT_ERR;
|
||||||
*setme_optarg = NULL;
|
*setme_optarg = NULL;
|
||||||
option_index++;
|
tr_optind++;
|
||||||
return o->val;
|
return o->val;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* option needed an argument, and it was nested in this string */
|
/* option needed an argument, and it was nested in this string */
|
||||||
if( nest ) {
|
if( nest ) {
|
||||||
*setme_optarg = nest;
|
*setme_optarg = nest;
|
||||||
option_index++;
|
tr_optind++;
|
||||||
return o->val;
|
return o->val;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* throw an error if the option needed an argument but didn't get one */
|
/* throw an error if the option needed an argument but didn't get one */
|
||||||
if( ++option_index >= argc )
|
if( ++tr_optind >= argc )
|
||||||
return TR_OPT_ERR;
|
return TR_OPT_ERR;
|
||||||
if( findOption( opts, argv[option_index], NULL ))
|
if( findOption( opts, argv[tr_optind], NULL ))
|
||||||
return TR_OPT_ERR;
|
return TR_OPT_ERR;
|
||||||
|
|
||||||
*setme_optarg = argv[option_index++];
|
*setme_optarg = argv[tr_optind++];
|
||||||
return o->val;
|
return o->val;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,23 @@
|
|||||||
/*
|
/*
|
||||||
* This file Copyright (C) 2008 Charles Kerr <charles@rebelbase.com>
|
* This file Copyright (C) 2008 Charles Kerr <charles@rebelbase.com>
|
||||||
*
|
*
|
||||||
* This file is licensed by the GPL version 2. Works owned by the
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
* Transmission project are granted a special exemption to clause 2(b)
|
* copy of this software and associated documentation files (the "Software"),
|
||||||
* so that the bulk of its code can remain under the MIT license.
|
* to deal in the Software without restriction, including without limitation
|
||||||
* This exemption does not extend to derived works not owned by
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||||
* the Transmission project.
|
* and/or sell copies of the Software, and to permit persons to whom the
|
||||||
|
* Software is furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||||
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
|
* DEALINGS IN THE SOFTWARE.
|
||||||
*
|
*
|
||||||
* $Id:$
|
* $Id:$
|
||||||
*/
|
*/
|
||||||
@@ -41,7 +53,7 @@ enum
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @return TR_GETOPT_DONE, TR_GETOPT_ERR, TR_GETOPT_UNK,
|
* @return TR_GETOPT_DONE, TR_GETOPT_ERR, TR_GETOPT_UNK,
|
||||||
* or the matching tr_option's `value' field
|
* or the matching tr_option's `val' field
|
||||||
*/
|
*/
|
||||||
int tr_getopt( const char * summary,
|
int tr_getopt( const char * summary,
|
||||||
int argc,
|
int argc,
|
||||||
|
|||||||
Reference in New Issue
Block a user