Changeset 6306


Ignore:
Timestamp:
Jul 9, 2008, 3:31:35 PM (15 years ago)
Author:
charles
Message:

code cleanup

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/cli/cli.c

    r6302 r6306  
    5555static sig_atomic_t  manualUpdate     = 0;
    5656
    57 static const char   * torrentPath   = NULL;
    58 static const char   * downloadDir   = NULL;
     57static const char   * torrentPath  = NULL;
     58static const char   * downloadDir  = NULL;
    5959static const char   * finishCall   = NULL;
    6060static const char   * announce     = NULL;
     
    166166                  inf->pieceSize, inf->totalSize % inf->pieceSize );
    167167
    168     if( inf->comment[0] )
     168    if( inf->comment && *inf->comment )
    169169        fprintf( out, "comment:\t%s\n", inf->comment );
    170     if( inf->creator[0] )
     170    if( inf->creator && *inf->creator )
    171171        fprintf( out, "creator:\t%s\n", inf->creator );
    172172    if( inf->isPrivate )
     
    175175    fprintf( out, "file(s):\n" );
    176176    for( ff=0; ff<inf->fileCount; ++ff )
    177         fprintf( out, "\t%s (%"PRIu64")\n", inf->files[ff].name, inf->files[ff].length );
     177        fprintf( out, "\t%s (%"PRIu64")\n", inf->files[ff].name,
     178                                            inf->files[ff].length );
    178179}
    179180
     
    214215                  st->rateUpload, ratioStr );
    215216    }
    216     else if( st->status & TR_STATUS_STOPPED )
    217     {
    218         *buf = '\0';
    219     }
     217    else *buf = '\0';
    220218}
    221219
     
    297295    {
    298296        int err;
    299         tr_metainfo_builder * builder = tr_metaInfoBuilderCreate( h, sourceFile );
     297        tr_metainfo_builder * b = tr_metaInfoBuilderCreate( h, sourceFile );
    300298        tr_tracker_info ti;
    301299        ti.tier = 0;
    302300        ti.announce = (char*) announce;
    303         tr_makeMetaInfo( builder, torrentPath, &ti, 1, comment, isPrivate );
    304         while( !builder->isDone ) {
     301        tr_makeMetaInfo( b, torrentPath, &ti, 1, comment, isPrivate );
     302        while( !b->isDone ) {
    305303            tr_wait( 1000 );
    306304            printf( "." );
    307305        }
    308         err = builder->result;
    309         tr_metaInfoBuilderFree( builder );
     306        err = b->result;
     307        tr_metaInfoBuilderFree( b );
    310308        return err;
    311309    }
     
    339337                    ++leftToScrape;
    340338                    tr_webRun( h, url, NULL, scrapeDoneFunc, host );
     339                    tr_free( host );
    341340                    tr_free( url );
    342341                }
     
    394393        if( gotsig ) {
    395394            gotsig = 0;
    396 fprintf( stderr, "stopping torrent...\n" );
     395            printf( "stopping torrent...\n" );
    397396            tr_torrentStop( tor );
    398397        }
     
    401400            manualUpdate = 0;
    402401            if ( !tr_torrentCanManualUpdate( tor ) )
    403                 fprintf( stderr, "\rReceived SIGHUP, but can't send a manual update now\n" );
     402                fprintf( stderr, "\nReceived SIGHUP, but can't send a manual update now\n" );
    404403            else {
    405                 fprintf( stderr, "\rReceived SIGHUP: manual update scheduled\n" );
     404                fprintf( stderr, "\nReceived SIGHUP: manual update scheduled\n" );
    406405                tr_torrentManualUpdate( tor );
    407406            }
     
    413412
    414413        getStatusStr( st, line, sizeof( line ) );
    415         printf( "%-*s\n", LINEWIDTH, line );
     414        printf( "\r%-*s", LINEWIDTH, line );
    416415        if( st->error )
    417             printf( "%s\n", st->errorString );
     416            printf( "\n%s\n", st->errorString );
    418417    }
    419418
  • trunk/libtransmission/tr-getopt.c

    r6298 r6306  
    22 * This file Copyright (C) 2008 Charles Kerr <charles@rebelbase.com>
    33 *
    4  * This file is licensed by the GPL version 2.  Works owned by the
    5  * Transmission project are granted a special exemption to clause 2(b)
    6  * so that the bulk of its code can remain under the MIT license.
    7  * This exemption does not extend to derived works not owned by
    8  * the Transmission project.
     4 * Permission is hereby granted, free of charge, to any person obtaining a
     5 * copy of this software and associated documentation files (the "Software"),
     6 * to deal in the Software without restriction, including without limitation
     7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     8 * and/or sell copies of the Software, and to permit persons to whom the
     9 * Software is furnished to do so, subject to the following conditions:
     10 *
     11 * The above copyright notice and this permission notice shall be included in
     12 * all copies or substantial portions of the Software.
     13 *
     14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
     20 * DEALINGS IN THE SOFTWARE.
    921 *
    1022 * $Id:$
     
    2133#endif
    2234
    23 int option_index = 1;
     35int tr_optind = 1;
    2436
    2537static const char*
    2638getArgName( const tr_option * opt )
    2739{
    28     if( !opt->has_arg )   return "";
    29     if( opt->argName ) return opt->argName;
    30     return "<args>";
     40    char * arg;
     41
     42    if( !opt->has_arg )
     43        arg = "";
     44    else if( opt->argName )
     45        arg = opt->argName;
     46    else
     47        arg = "<args>";
     48
     49    return arg;
    3150}
    3251
     
    4463}
    4564
     65static void
     66maxWidth( const struct tr_option * o,
     67          int * longWidth, int * shortWidth, int * argWidth )
     68{
     69    const char * arg;
     70
     71    if( o->longName )
     72        *longWidth = MAX( *longWidth, (int)strlen( o->longName ) );
     73
     74    if( o->shortName )
     75        *shortWidth = MAX( *shortWidth, (int)strlen( o->shortName ) );
     76
     77    if(( arg = getArgName( o )))
     78        *argWidth = MAX( *argWidth, (int)strlen( arg ) );
     79}
     80
    4681void
    47 tr_getopt_usage( const char             * progName,
    48                  const char             * description,
     82tr_getopt_usage( const char              * progName,
     83                 const char              * description,
    4984                 const struct tr_option    opts[] )
    5085{
    51   int count;
    52   int longWidth = 0;
    53   int shortWidth = 0;
    54   int argWidth = 0;
    55   struct tr_option help;
    56 
    57   for( count=0; opts[count].description; ++count )
    58   {
    59     const char * arg;
    60 
    61     if( opts[count].longName )
    62       longWidth = MAX( longWidth, (int)strlen( opts[count].longName ) );
    63 
    64     if( opts[count].shortName )
    65       shortWidth = MAX( shortWidth, (int)strlen( opts[count].shortName ) );
    66 
    67     if(( arg = getArgName( &opts[count] )))
    68       argWidth = MAX( argWidth, (int)strlen( arg ) );
    69   }
    70 
    71   if( !description )
    72     description = "Usage: %s [options]";
    73   printf( description, progName );
    74   printf( "\n\n" );
    75   printf( "Options:\n" );
    76 
    77   help.val = -1;
    78   help.longName = "help";
    79   help.description = "Display this help page and exit";
    80   help.shortName = "h";
    81   help.has_arg = 0;
    82   getopts_usage_line( &help, longWidth, shortWidth, argWidth );
    83  
    84   for( count=0; opts[count].description; ++count )
    85       getopts_usage_line( &opts[count], longWidth, shortWidth, argWidth );
     86    int longWidth = 0;
     87    int shortWidth = 0;
     88    int argWidth = 0;
     89    struct tr_option help;
     90    const struct tr_option * o;
     91
     92    for( o=opts; o->val; ++o )
     93        maxWidth( o, &longWidth, &shortWidth, &argWidth );
     94
     95    help.val = -1;
     96    help.longName = "help";
     97    help.description = "Display this help page and exit";
     98    help.shortName = "h";
     99    help.has_arg = 0;
     100    maxWidth( &help, &longWidth, &shortWidth, &argWidth );
     101
     102    if( description == NULL )
     103        description = "Usage: %s [options]";
     104    printf( description, progName );
     105    printf( "\n\nOptions:\n" );
     106    getopts_usage_line( &help, longWidth, shortWidth, argWidth );
     107    for( o=opts; o->val; ++o )
     108        getopts_usage_line( o, longWidth, shortWidth, argWidth );
    86109}
    87110
     
    123146    return NULL;
    124147}
    125 
    126 static void
    127 showUsageAndExit( const char        * appName,
    128                   const char        * description,
    129                   const tr_option   * opts )
    130 {
    131     tr_getopt_usage( appName, description, opts );
    132     exit( 0 );
    133 }
    134 
    135148
    136149int
     
    146159
    147160    *setme_optarg = NULL; 
    148 
    149     if( argc==1 || argc==option_index )
    150         return TR_OPT_DONE;
    151161 
    152162    /* handle the builtin 'help' option */
    153     for( i=1; i<argc; ++i )
    154         if( !strcmp(argv[i], "-h") || !strcmp(argv[i], "--help" ) )
    155             showUsageAndExit( argv[0], usage, opts );
    156 
    157     /* out of options */
    158     if( option_index >= argc )
     163    for( i=1; i<argc; ++i ) {
     164        if( !strcmp(argv[i], "-h") || !strcmp(argv[i], "--help" ) ) {
     165            tr_getopt_usage( argv[0], usage, opts );
     166            exit( 0 );
     167        }
     168    }
     169
     170    /* out of options? */
     171    if( argc==1 || tr_optind>=argc )
    159172        return TR_OPT_DONE;
    160173
    161     o = findOption( opts, argv[option_index], &nest );
     174    o = findOption( opts, argv[tr_optind], &nest );
    162175    if( !o ) {
    163176        /* let the user know we got an unknown option... */
    164         *setme_optarg = argv[option_index++];
     177        *setme_optarg = argv[tr_optind++];
    165178        return TR_OPT_UNK;
    166179    }
     
    171184            return TR_OPT_ERR;
    172185        *setme_optarg = NULL;
    173         option_index++;
     186        tr_optind++;
    174187        return o->val;
    175188    }
     
    178191    if( nest ) {
    179192        *setme_optarg = nest;
    180         option_index++;
     193        tr_optind++;
    181194        return o->val;
    182195    }
    183196
    184197    /* throw an error if the option needed an argument but didn't get one */
    185     if( ++option_index >= argc )
     198    if( ++tr_optind >= argc )
    186199        return TR_OPT_ERR;
    187     if( findOption( opts, argv[option_index], NULL ))
     200    if( findOption( opts, argv[tr_optind], NULL ))
    188201        return TR_OPT_ERR;
    189202
    190     *setme_optarg = argv[option_index++];
     203    *setme_optarg = argv[tr_optind++];
    191204    return o->val;
    192205}
  • trunk/libtransmission/tr-getopt.h

    r6297 r6306  
    22 * This file Copyright (C) 2008 Charles Kerr <charles@rebelbase.com>
    33 *
    4  * This file is licensed by the GPL version 2.  Works owned by the
    5  * Transmission project are granted a special exemption to clause 2(b)
    6  * so that the bulk of its code can remain under the MIT license.
    7  * This exemption does not extend to derived works not owned by
    8  * the Transmission project.
     4 * Permission is hereby granted, free of charge, to any person obtaining a
     5 * copy of this software and associated documentation files (the "Software"),
     6 * to deal in the Software without restriction, including without limitation
     7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     8 * and/or sell copies of the Software, and to permit persons to whom the
     9 * Software is furnished to do so, subject to the following conditions:
     10 *
     11 * The above copyright notice and this permission notice shall be included in
     12 * all copies or substantial portions of the Software.
     13 *
     14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
     20 * DEALINGS IN THE SOFTWARE.
    921 *
    1022 * $Id:$
     
    4254/**
    4355 * @return TR_GETOPT_DONE, TR_GETOPT_ERR, TR_GETOPT_UNK,
    44  *         or the matching tr_option's `value' field
     56 *         or the matching tr_option's `val' field
    4557 */
    4658int tr_getopt( const char        * summary,
Note: See TracChangeset for help on using the changeset viewer.