Changeset 6295


Ignore:
Timestamp:
Jul 8, 2008, 3:19:01 AM (14 years ago)
Author:
charles
Message:

cleanup the new getopt code a bit. sync remote's manpage.

Location:
trunk/daemon
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/daemon/getopts.c

    r6294 r6295  
    3838*/
    3939#include <stdio.h>
    40 #include <stdlib.h>
     40#include <stdlib.h> /* exit() */
    4141#include <string.h>
    4242
     
    6666}
    6767
    68 /* int getopts_usage()
    69  *
    70  *  Returns: 1 - Successful
    71  */
    72 int getopts_usage(const char *progName, const char *usage, struct options opts[])
     68void
     69getopts_usage( const char           * progName,
     70               const char           * description,
     71               const struct options   opts[] )
    7372{
    7473  int count;
     
    9291  }
    9392
    94   if( !usage )
    95     usage = "Usage: %s [options]";
    96   printf( usage, progName );
     93  if( !description )
     94    description = "Usage: %s [options]";
     95  printf( description, progName );
    9796  printf( "\n\n" );
    9897  printf( "Usage:\n" );
     
    107106  for( count=0; opts[count].description; ++count )
    108107      getopts_usage_line( &opts[count], nameWidth, shortWidth, argWidth );
    109 
    110   return 1;
    111 }
    112 
    113 /* int getopts()
    114  *
    115  * Returns: -1 - Couldn't allocate memory.  Please handle me.
    116  *          0  - No arguements to parse
     108}
     109
     110static const struct options *
     111findOption( const struct options  * opts,
     112            const char            * str,
     113            const char           ** nested )
     114{
     115    size_t len;
     116    const struct options * o;
     117
     118    for( o=opts; o->number; ++o )
     119    {
     120        if( o->name && (str[0]=='-') && (str[1]=='-') ) {
     121            if( !strcmp( o->name, str+2 ) ) {
     122                if( nested ) *nested = NULL;
     123                return o;
     124            }
     125            len = strlen( o->name );
     126            if( !memcmp( o->name, str+2, len ) && str[len+2]=='=' ) {
     127                if( nested ) *nested = str+len+3;
     128                return o;
     129            }
     130        }
     131       
     132        if( o->shortName && (str[0]=='-') ) {
     133            if( !strcmp( o->shortName, str+1 ) ) {
     134                if( nested ) *nested = NULL;
     135                return o;
     136            }
     137            len = strlen( o->shortName );
     138            if( !memcmp( o->shortName, str+1, len ) && str[len+1]=='=' ) {
     139                if( nested ) *nested = str+len+2;
     140                return o;
     141            }
     142        }
     143    }
     144
     145    return NULL;
     146}
     147
     148static void
     149showUsageAndExit( const char           * appName,
     150                  const char           * description,
     151                  const struct options * opts )
     152{
     153    getopts_usage( appName, description, opts );
     154    exit( 0 );
     155}
     156
     157
     158/*
     159 * Returns: 0  - No arguments to parse
    117160 *          #  - The number in the struct for the matched arg.
    118  *
    119 */
     161 *         -2  - Unrecognized option
     162 */
    120163int
    121 getopts( const char      * usage,
    122          int               argc,
    123          char           ** argv,
    124          struct options  * opts,
    125          char           ** args )
    126 {
    127   int argCounter, sizeOfArgs;
    128   if (argc == 1 || option_index == argc)
    129     return 0;
     164getopts( const char             * usage,
     165         int                      argc,
     166         const char            ** argv,
     167         const struct options   * opts,
     168         const char            ** setme_optarg )
     169{
     170    int i;
     171    const char * nest = NULL;
     172    const struct options * o = NULL;
     173
     174    *setme_optarg = NULL; 
     175
     176    if( argc==1 || argc==option_index )
     177        return 0;
    130178 
    131 /* Search for '-h' or '--help' first.  Then we can just exit */
    132   for (argCounter = 1; argCounter < argc; argCounter++)
    133     {
    134       if (!strcmp(argv[argCounter], "-h") || !strcmp(argv[argCounter], "--help"))
    135         {
    136 useage:
    137 fprintf( stderr, "dkdkdkdkdkd\n" );
    138           getopts_usage(argv[0], usage, opts);
    139           exit(0);
    140         }
    141     }
    142 /* End of -h --help section */
    143  
    144   *args = NULL;
    145   if (option_index <= argc)
    146     {
    147       for (argCounter = 0; opts[argCounter].number!=0; argCounter++)
    148         {
    149           if ((opts[argCounter].name && !strcmp(opts[argCounter].name, (argv[option_index]+2))) ||
    150               (opts[argCounter].shortName && !strcmp(opts[argCounter].shortName, (argv[option_index]+1))))
    151             {
    152               if (opts[argCounter].args)
    153                 {
    154                   option_index++;
    155                   if (option_index >= argc)
    156                     goto useage;
    157 /* This grossness that follows is to support having a '-' in the argument.  */
    158                   if (*argv[option_index] == '-')
    159                     {
    160                       int optionSeeker;
    161                       for (optionSeeker = 0; opts[optionSeeker].description; optionSeeker++)
    162                         {
    163                           if ((opts[optionSeeker].name &&
    164                                !strcmp(opts[optionSeeker].name, (argv[option_index]+2))) ||
    165                                (opts[optionSeeker].shortName &&
    166                                !strcmp(opts[optionSeeker].shortName, (argv[option_index]+1))))
    167                             {
    168                               goto useage;
    169                             }
    170                         }
    171 /* End of gross hack for supporting '-' in arguments. */
    172                     }
    173                   sizeOfArgs = strlen(argv[option_index]);
    174 #ifdef __cplusplus
    175                   if ((*args = (char *)calloc(1, sizeOfArgs+1)) == NULL)
    176 #else
    177                   if ((*args = calloc(1, sizeOfArgs+1)) == NULL)
    178 #endif
    179                     return -1;
    180                   strncpy(*args, argv[option_index], sizeOfArgs);
    181                 }
    182               option_index++;
    183               return opts[argCounter].number;
    184             }
    185         }
    186 /** The Option doesn't exist.  We should warn them. */
    187       //if (*argv[option_index] == '-')
    188         {
    189           sizeOfArgs = strlen(argv[option_index]);
    190 #ifdef __cplusplus
    191           if ((*args = (char *)calloc(1, sizeOfArgs+1)) == NULL)
    192 #else
    193           if ((*args = calloc(1, sizeOfArgs+1)) == NULL)
    194 #endif
    195             return -1;
    196           strncpy(*args, argv[option_index], sizeOfArgs);
    197           option_index++;
    198           return -2;
    199         }
    200     }
    201   return 0;
    202 }
    203 
    204 
    205 
    206 
    207 
    208 
     179    /* handle the builtin 'help' option */
     180    for( i=1; i<argc; ++i )
     181        if( !strcmp(argv[i], "-h") || !strcmp(argv[i], "--help" ) )
     182            showUsageAndExit( argv[0], usage, opts );
     183
     184    /* out of options */
     185    if( option_index >= argc )
     186        return 0;
     187
     188    o = findOption( opts, argv[option_index], &nest );
     189    if( !o ) {
     190        /* let the user know we got an unknown option... */
     191        *setme_optarg = argv[option_index++];
     192        return -2;
     193    }
     194
     195    if( !o->args ) {
     196        /* no argument needed for this option, so we're done */
     197        if( nest )
     198            showUsageAndExit( argv[0], usage, opts );
     199        *setme_optarg = NULL;
     200        option_index++;
     201        return o->number;
     202    }
     203
     204    /* option needed an argument, and it was nested in this string */
     205    if( nest ) {
     206        *setme_optarg = nest;
     207        option_index++;
     208        return o->number;
     209    }
     210
     211    /* throw an error if the option needed an argument but didn't get one */
     212    if( ++option_index >= argc )
     213        showUsageAndExit( argv[0], usage, opts );
     214    if( findOption( opts, argv[option_index], NULL ))
     215        showUsageAndExit( argv[0], usage, opts );
     216
     217    *setme_optarg = argv[option_index++];
     218    return o->number;
     219}
  • trunk/daemon/getopts.h

    r6294 r6295  
    5050};
    5151
    52 int getopts(const char * summary, int argc, char **argv, struct options opts[], char **args);
    53 int getopts_usage(const char *progName, const char * summary, struct options opts[]);
     52int getopts( const char              * summary,
     53             int                       argc,
     54             const char             ** argv,
     55             const struct options    * opts,
     56             const char             ** setme_optarg );
     57
     58void getopts_usage( const char            * appName,
     59                    const char            * description,
     60                    const struct options  * opts );
    5461
    5562#ifdef __cplusplus
  • trunk/daemon/remote.c

    r6294 r6295  
    7777showUsage( void )
    7878{
    79 fprintf( stderr, "asdfasdfasdfasdfasdfasdf\n");
    8079    getopts_usage( MY_NAME, getUsage(), opts );
    8180    exit( 0 );
     
    134133
    135134static void
    136 readargs( int argc, char ** argv )
     135readargs( int argc, const char ** argv )
    137136{
    138137    int c;
    139138    int addingTorrents = 0;
    140     char * optarg;
     139    const char * optarg;
    141140    char id[4096];
    142141
     
    706705        host = tr_strdup( DEFAULT_HOST );
    707706
    708     readargs( argc, argv );
     707    readargs( argc, (const char**)argv );
    709708    if( reqCount )
    710709        processRequests( host, port, (const char**)reqs, reqCount );
  • trunk/daemon/transmission-remote.1

    r6292 r6295  
    1010.Sh SYNOPSIS
    1111.Bk -words
    12 .Nm transmission-remote
    13 .Fl h
    1412.Nm
    1513.Op Ar host:port | host | port
    16 .Op Fl a Ar torrent-file
    17 .Op Fl d Ar download-rate
     14.Op Fl a Ar torrent-files
     15.Op Fl d Ar number
    1816.Op Fl D
    19 .Op Fl e Ar encryption-mode
    20 .Oo
    21 .Fl f Ar id | Ar hash
    22 .Oc
    23 .Oo
    24 .Fl i Ar id | Ar hash
    25 .Oc
     17.Op Fl e Ar mode
     18.Op Fl f
     19.Op Fl g
     20.Op Fl h
     21.Op Fl i
    2622.Op Fl l
    2723.Op Fl m
    2824.Op Fl M
     25.Op Fl n Ar user:pass
    2926.Op Fl p Ar port
    30 .Op Fl t Ar user:pass
    31 .Oo
    32 .Fl r Ar all | Ar id | Ar hash
    33 .Oc
    34 .Oo
    35 .Fl s Ar all | Ar id | Ar hash
    36 .Oc
    37 .Oo
    38 .Fl S Ar all | Ar id | Ar hash
    39 .Oc
    40 .Op Fl u Ar upload-rate
     27.Op Fl r
     28.Op Fl s
     29.Op Fl S
     30.Op Fl t Ar all | Ar id | Ar hash
     31.Op Fl u Ar number
    4132.Op Fl U
    42 .Oo
    43 .Fl v Ar all | Ar id | Ar hash
    44 .Oc
    45 .Op Fl w Ar directory
     33.Op Fl v
     34.Op Fl w Ar download-dir
    4635.Op Fl x
    4736.Op Fl X
     
    6251.Bl -tag -width Ds
    6352.It Fl a Fl -add Ar torrent-file
    64 Add the torrent metainfo file
    65 .Ar torrent-file .
    66 .It Fl d Fl -download-limit Ar download-rate
    67 Set maximum download rate to
    68 .Ar download-rate
    69 in kilobytes per second.
    70 .It Fl D Fl -download-unlimited
     53Add metainfo
     54.Ar torrent-file(s) .
     55
     56.It Fl d Fl -downlimit Ar limit
     57Limit the download speed to
     58.Ar limit
     59kilobytes per second.
     60
     61.It Fl D Fl -no-downlimit
    7162Remove the download limit.
     63
    7264.It Fl e Fl -encryption Ar required
    7365Require all peer connections to be encrypted.
     
    7668.It Fl e Fl -encryption Ar tolerated
    7769Prefer unencrypted peer connections.
    78 .It Fl f Fl -files Ar id | torrent-hash
    79 Get file information for the torrent matching the specified
    80 .Ar id
    81 or
    82 .Ar hash .
     70
     71.It Fl f Fl -files
     72Get a file list for the current torrent(s)
     73
    8374.It Fl h Fl -help
    8475Print command-line option descriptions.
     76
    8577.It Fl i Fl -info Ar id | torrent-hash
    86 Print detailed information for the torrent matching the specified
    87 .Ar id
    88 or
    89 .Ar hash .
     78Show details of the current torrent(s)
     79
    9080.It Fl l Fl -list
    91 List all torrents, their Id numbers, and current status.
    92 .It Fl m Fl -port-mapping
    93 Enable automatic port mapping via NAT-PMP or UPnP IGD.
    94 .It Fl M Fl -no-port-mapping
    95 Disable automatic port mapping.
    96 .It Fl p Fl -port Ar port
    97 Attempt to bind to
    98 .Ar port
    99 for use as a listening port to accept incoming peer connections.
     81List all torrents
    10082
    101 .It Fl r Fl -remove Ar all | id | torrent-hash
    102 Remove all torrents, or the torrent matching the specified
    103 .Ar id
    104 or
    105 .Ar hash .
    106 This does not delete the downloaded data.
     83.It Fl m Fl -portmap
     84Enable portmapping via NAT-PMP or UPnP
     85.It Fl M Fl -no-portmap
     86Disable portmapping
    10787
    108 .It Fl s Fl -start Ar all | id | torrent-hash
    109 Start all torrents downloading or seeding, or the torrent matching the specified
    110 .Ar id
    111 or
    112 .Ar hash .
    113 
    114 .It Fl S Fl -stop Ar all | id | torrent-hash
    115 Stop all torrents from downloading or seeding, or the torrent matching the specified
    116 .Ar id
    117 or
    118 .Ar hash .
    119 
    120 .It Fl t Fl -auth Ar user:pass
     88.It Fl n Fl -auth Ar user:pass
    12189.Ar Username
    12290and
     
    12492for authentication
    12593
    126 .It Fl u Fl -upload-limit Ar upload-rate
    127 Set maximum upload rate to
    128 .Ar upload-rate
    129 in kilobytes per second.
     94.It Fl p Fl -port Ar port
     95Set the
     96.Ar port
     97for use when listening for incoming peer connections
    13098
    131 .It Fl U Fl -upload-unlimited
    132 Remove the upload limit.
     99.It Fl r Fl -remove
     100Remove the current torrent(s).  This does not delete the downloaded data.
    133101
    134 .It Fl v Fl -verify Ar all | id | torrent-hash
    135 Queue all the torrents for verification, or the torrent matching the specified
     102.It Fl s Fl -start
     103Start the current torrent(s)
     104
     105.It Fl S Fl -stop
     106Stop the current torrent(s) from downloading or seeding
     107
     108.It Fl t Fl -torrent Ar all | id | torrent-hash
     109Set the current torrent(s) for use by subsequent options.
     110.Ar all
     111will apply following requests to all torrents, while specific torrents can be chosen by
    136112.Ar id
    137113or
    138114.Ar hash .
     115
     116.It Fl u Fl -uplimit Ar limit
     117Limit the upload speed to
     118.Ar limit
     119kilobytes per second.
     120.It Fl U Fl -no-uplimit
     121Remove the upload limit.
     122
     123.It Fl v Fl -verify
     124Verify the current torrent(s)
    139125
    140126.It Fl w Fl -download-dir Ar directory
     
    156142Pause torrent #12:
    157143.Pp
    158 .Dl transmission-remote -S 12
     144.Dl transmission-remote -t 12 -S
     145.Dl transmission-remote --torrent=12 --stop
     146.Pp
     147Start all torrents:
     148.Pp
     149.Dl transmission-remote -t all -s
     150.Dl transmission-remote --torrent=all --start
    159151.Pp
    160152Set download and upload limits to 100 KiB/sec and 20 KiB/sec:
    161153.Pp
    162154.Dl transmission-remote -d 100 -u 20
     155.Dl transmission-remote -d=100 -u=20
    163156.Pp
    164157Add two torrents:
    165158.Pp
    166 .Dl transmission-remote -a foo.torrent -a bar.torrent
     159.Dl transmission-remote -a one.torrent two.torrent
    167160
    168161.Sh AUTHORS
Note: See TracChangeset for help on using the changeset viewer.