~ubuntu-branches/ubuntu/jaunty/transmission/jaunty-security

« back to all changes in this revision

Viewing changes to libtransmission/tr-getopt.c

  • Committer: Bazaar Package Importer
  • Author(s): Chris Coulson
  • Date: 2008-11-28 15:33:48 UTC
  • mfrom: (1.1.19 upstream)
  • Revision ID: james.westby@ubuntu.com-20081128153348-it70trfnxiroblmc
Tags: 1.40-0ubuntu1
* New upstream release (LP: #302672)
  - Tracker communication uses fewer resources
  - More accurate bandwidth limits
  - Reduce disk fragmentation by preallocating files (LP: #287726)
  - Stability, security and performance improvements to the RPC /
    Web UI server (closes LP: #290423)
  - Support compression when serving Web UI and RPC responses
  - Simplify the RPC whitelist
  - Fix bug that prevented handshakes with encrypted BitComet peers
  - Fix 1.3x bug that could re-download some data unnecessarily
    (LP: #295040)
  - Option to automatically update the blocklist weekly
  - Added off-hour bandwidth scheduling
  - Simplify file/priority selection in the details dialog
  - Fix a couple of crashes
  - New / updated translations
  - Don't inhibit hibernation by default (LP: #292929)
  - Use "close" animation when sending to notification area (LP: #130811)
  - Fix resize problems (LP: #269872)
  - Support "--version" option when launching from command line
    (LP: #292011)
  - Correctly parse announce URLs that have leading or trailing
    spaces (LP: #262411)
  - Display an error when "Open Torrent" fails (LP: #281463)
* Dropped 10_fix_crasher_from_upstream.dpatch: Fix is in this
  upstream release.
* debian/control: Don't just build-depend on libcurl-dev, which is
  a virtual package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
 *
4
4
 * This file is licensed by the GPL version 2.  Works owned by the
5
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. 
 
6
 * so that the bulk of its code can remain under the MIT license.
7
7
 * This exemption does not extend to derived works not owned by
8
8
 * the Transmission project.
9
9
 *
17
17
#include "tr-getopt.h"
18
18
 
19
19
#ifndef MAX
20
 
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
 
20
 #define MAX( a, b ) ( ( ( a ) > ( b ) ) ? ( a ) : ( b ) )
21
21
#endif
22
22
 
23
23
int tr_optind = 1;
39
39
 
40
40
static void
41
41
getopts_usage_line( const tr_option * opt,
42
 
                    int longWidth, int shortWidth, int argWidth )
 
42
                    int               longWidth,
 
43
                    int               shortWidth,
 
44
                    int               argWidth )
43
45
{
44
46
    const char * longName   = opt->longName ? opt->longName : "";
45
47
    const char * shortName  = opt->shortName ? opt->shortName : "";
46
48
    const char * arg        = getArgName( opt );
 
49
 
47
50
    printf( "  -%-*s --%-*s %-*s  %s\n", shortWidth, shortName,
48
 
                                         longWidth, longName,
49
 
                                         argWidth, arg,
50
 
                                         opt->description );
 
51
            longWidth, longName,
 
52
            argWidth, arg,
 
53
            opt->description );
51
54
}
52
55
 
53
56
static void
54
57
maxWidth( const struct tr_option * o,
55
 
          int * longWidth, int * shortWidth, int * argWidth )
 
58
          int *                    longWidth,
 
59
          int *                    shortWidth,
 
60
          int *                    argWidth )
56
61
{
57
62
    const char * arg;
58
63
 
59
 
    if( o->longName ) 
 
64
    if( o->longName )
60
65
        *longWidth = MAX( *longWidth, (int)strlen( o->longName ) );
61
66
 
62
67
    if( o->shortName )
63
68
        *shortWidth = MAX( *shortWidth, (int)strlen( o->shortName ) );
64
69
 
65
 
    if(( arg = getArgName( o )))
 
70
    if( ( arg = getArgName( o ) ) )
66
71
        *argWidth = MAX( *argWidth, (int)strlen( arg ) );
67
72
}
68
73
 
69
74
void
70
 
tr_getopt_usage( const char              * progName,
71
 
                 const char              * description,
72
 
                 const struct tr_option    opts[] )
 
75
tr_getopt_usage( const char *           progName,
 
76
                 const char *           description,
 
77
                 const struct tr_option opts[] )
73
78
{
74
 
    int longWidth = 0;
75
 
    int shortWidth = 0;
76
 
    int argWidth = 0;
77
 
    struct tr_option help;
 
79
    int                      longWidth = 0;
 
80
    int                      shortWidth = 0;
 
81
    int                      argWidth = 0;
 
82
    struct tr_option         help;
78
83
    const struct tr_option * o;
79
84
 
80
 
    for( o=opts; o->val; ++o )
 
85
    for( o = opts; o->val; ++o )
81
86
        maxWidth( o, &longWidth, &shortWidth, &argWidth );
82
87
 
83
88
    help.val = -1;
92
97
    printf( description, progName );
93
98
    printf( "\n\nOptions:\n" );
94
99
    getopts_usage_line( &help, longWidth, shortWidth, argWidth );
95
 
    for( o=opts; o->val; ++o )
 
100
    for( o = opts; o->val; ++o )
96
101
        getopts_usage_line( o, longWidth, shortWidth, argWidth );
97
102
}
98
103
 
99
104
static const tr_option *
100
 
findOption( const tr_option   * opts,
101
 
            const char        * str,
102
 
            const char       ** setme_arg )
 
105
findOption( const tr_option * opts,
 
106
            const char *      str,
 
107
            const char **     setme_arg )
103
108
{
104
 
    size_t matchlen = 0;
105
 
    const char * arg = NULL;
 
109
    size_t            matchlen = 0;
 
110
    const char *      arg = NULL;
106
111
    const tr_option * o;
107
112
    const tr_option * match = NULL;
108
113
 
109
114
    /* find the longest matching option */
110
 
    for( o=opts; o->val; ++o )
 
115
    for( o = opts; o->val; ++o )
111
116
    {
112
117
        size_t len = o->longName ? strlen( o->longName ) : 0;
113
118
 
114
119
        if( ( matchlen < len ) && !memcmp( str, "--", 2 )
115
 
                               && !memcmp( str+2, o->longName, len ) 
116
 
                               && ( str[len+2]=='\0' || ( o->has_arg && str[len+2]=='=' ) ) )
 
120
          && !memcmp( str + 2, o->longName, len )
 
121
          && ( str[len + 2] == '\0' || ( o->has_arg && str[len + 2] == '=' ) ) )
117
122
        {
118
123
            matchlen = len;
119
124
            match = o;
120
 
            arg = str[len+2]=='=' ? str+len+3 : NULL;
 
125
            arg = str[len + 2] == '=' ? str + len + 3 : NULL;
121
126
        }
122
127
 
123
128
        len = o->shortName ? strlen( o->shortName ) : 0;
124
129
 
125
130
        if( ( matchlen < len ) && !memcmp( str, "-", 1 )
126
 
                               && !memcmp( str+1, o->shortName, len ) 
127
 
                               && ( str[len+1]=='\0' || o->has_arg ) )
 
131
          && !memcmp( str + 1, o->shortName, len )
 
132
          && ( str[len + 1] == '\0' || o->has_arg ) )
128
133
        {
129
134
            matchlen = len;
130
135
            match = o;
131
 
            switch( str[len+1] ) {
132
 
                case '\0': arg = NULL;          break;
133
 
                case '=':  arg = str + len + 2; break;
134
 
                default:   arg = str + len + 1; break;
 
136
            switch( str[len + 1] )
 
137
            {
 
138
                case '\0':
 
139
                    arg = NULL;          break;
 
140
 
 
141
                case '=':
 
142
                    arg = str + len + 2; break;
 
143
 
 
144
                default:
 
145
                    arg = str + len + 1; break;
135
146
            }
136
147
        }
137
148
    }
143
154
}
144
155
 
145
156
int
146
 
tr_getopt( const char        * usage,
147
 
           int                 argc,
148
 
           const char       ** argv,
149
 
           const tr_option   * opts,
150
 
           const char       ** setme_optarg )
 
157
tr_getopt( const char *      usage,
 
158
           int               argc,
 
159
           const char **     argv,
 
160
           const tr_option * opts,
 
161
           const char **     setme_optarg )
151
162
{
152
 
    int i;
153
 
    const char * arg = NULL;
 
163
    int               i;
 
164
    const char *      arg = NULL;
154
165
    const tr_option * o = NULL;
155
166
 
156
 
    *setme_optarg = NULL;  
157
 
  
 
167
    *setme_optarg = NULL;
 
168
 
158
169
    /* handle the builtin 'help' option */
159
 
    for( i=1; i<argc; ++i ) {
160
 
        if( !strcmp(argv[i], "-h") || !strcmp(argv[i], "--help" ) ) {
 
170
    for( i = 1; i < argc; ++i )
 
171
    {
 
172
        if( !strcmp( argv[i], "-h" ) || !strcmp( argv[i], "--help" ) )
 
173
        {
161
174
            tr_getopt_usage( argv[0], usage, opts );
162
175
            exit( 0 );
163
176
        }
164
177
    }
165
178
 
166
179
    /* out of options? */
167
 
    if( argc==1 || tr_optind>=argc )
 
180
    if( argc == 1 || tr_optind >= argc )
168
181
        return TR_OPT_DONE;
169
182
 
170
183
    o = findOption( opts, argv[tr_optind], &arg );
171
 
    if( !o ) {
 
184
    if( !o )
 
185
    {
172
186
        /* let the user know we got an unknown option... */
173
187
        *setme_optarg = argv[tr_optind++];
174
188
        return TR_OPT_UNK;
175
189
    }
176
190
 
177
 
    if( !o->has_arg ) {
 
191
    if( !o->has_arg )
 
192
    {
178
193
        /* no argument needed for this option, so we're done */
179
194
        if( arg )
180
195
            return TR_OPT_ERR;
184
199
    }
185
200
 
186
201
    /* option needed an argument, and it was embedded in this string */
187
 
    if( arg ) {
 
202
    if( arg )
 
203
    {
188
204
        *setme_optarg = arg;
189
205
        ++tr_optind;
190
206
        return o->val;
193
209
    /* throw an error if the option needed an argument but didn't get one */
194
210
    if( ++tr_optind >= argc )
195
211
        return TR_OPT_ERR;
196
 
    if( findOption( opts, argv[tr_optind], NULL ))
 
212
    if( findOption( opts, argv[tr_optind], NULL ) )
197
213
        return TR_OPT_ERR;
198
214
 
199
215
    *setme_optarg = argv[tr_optind++];
200
216
    return o->val;
201
217
}
 
218