2008-07-08 14:29:05 +00:00
|
|
|
/*
|
2010-01-04 21:00:47 +00:00
|
|
|
* This file Copyright (C) 2008-2010 Mnemosyne LLC
|
2008-07-08 14:29:05 +00:00
|
|
|
*
|
2008-07-22 23:28:28 +00:00
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
|
|
|
* Transmission project are granted a special exemption to clause 2(b)
|
2008-09-23 19:11:04 +00:00
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
2008-07-22 23:28:28 +00:00
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
2008-07-09 15:31:35 +00:00
|
|
|
*
|
2008-07-22 23:28:28 +00:00
|
|
|
* $Id$
|
2008-07-08 14:29:05 +00:00
|
|
|
*/
|
|
|
|
|
2010-06-16 14:05:18 +00:00
|
|
|
#include <ctype.h> /* isspace() */
|
2008-07-08 14:29:05 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h> /* exit() */
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "tr-getopt.h"
|
|
|
|
|
|
|
|
#ifndef MAX
|
2008-09-23 19:11:04 +00:00
|
|
|
#define MAX( a, b ) ( ( ( a ) > ( b ) ) ? ( a ) : ( b ) )
|
2008-07-08 14:29:05 +00:00
|
|
|
#endif
|
|
|
|
|
2008-07-09 15:31:35 +00:00
|
|
|
int tr_optind = 1;
|
2008-07-08 14:29:05 +00:00
|
|
|
|
|
|
|
static const char*
|
|
|
|
getArgName( const tr_option * opt )
|
|
|
|
{
|
2009-01-23 18:44:15 +00:00
|
|
|
const char * arg;
|
2008-07-09 15:31:35 +00:00
|
|
|
|
|
|
|
if( !opt->has_arg )
|
|
|
|
arg = "";
|
|
|
|
else if( opt->argName )
|
|
|
|
arg = opt->argName;
|
|
|
|
else
|
|
|
|
arg = "<args>";
|
|
|
|
|
|
|
|
return arg;
|
2008-07-08 14:29:05 +00:00
|
|
|
}
|
|
|
|
|
2010-06-16 14:05:18 +00:00
|
|
|
static int
|
|
|
|
get_next_line_len( const char * description, int maxlen )
|
|
|
|
{
|
|
|
|
int end;
|
|
|
|
int len = strlen( description );
|
|
|
|
|
|
|
|
if( len < maxlen )
|
|
|
|
return len;
|
|
|
|
|
|
|
|
end = maxlen < len ? maxlen : len;
|
|
|
|
while( ( end > 0 ) && !isspace( description[end] ) )
|
|
|
|
--end;
|
|
|
|
|
|
|
|
return end ? end : len;
|
|
|
|
}
|
|
|
|
|
2008-07-08 14:29:05 +00:00
|
|
|
static void
|
|
|
|
getopts_usage_line( const tr_option * opt,
|
2008-09-23 19:11:04 +00:00
|
|
|
int longWidth,
|
|
|
|
int shortWidth,
|
|
|
|
int argWidth )
|
2008-07-08 14:29:05 +00:00
|
|
|
{
|
2010-06-16 14:05:18 +00:00
|
|
|
int len;
|
2008-07-08 14:29:05 +00:00
|
|
|
const char * longName = opt->longName ? opt->longName : "";
|
|
|
|
const char * shortName = opt->shortName ? opt->shortName : "";
|
|
|
|
const char * arg = getArgName( opt );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2010-06-16 14:05:18 +00:00
|
|
|
const int d_indent = shortWidth + longWidth + argWidth + 7;
|
|
|
|
const int d_width = 80 - d_indent;
|
|
|
|
const char * d = opt->description;
|
|
|
|
|
|
|
|
printf( " %s%-*s %s%-*s %-*s ",
|
2008-12-09 17:01:49 +00:00
|
|
|
(shortName && *shortName ? "-" : " "), shortWidth, shortName,
|
|
|
|
(longName && *longName ? "--" : " "), longWidth, longName,
|
2010-06-16 14:05:18 +00:00
|
|
|
argWidth, arg );
|
|
|
|
len = get_next_line_len( d, d_width );
|
|
|
|
printf( "%*.*s\n", len, len, d );
|
|
|
|
|
|
|
|
d += len;
|
|
|
|
while( isspace( *d ) ) ++d;
|
|
|
|
|
|
|
|
while(( len = get_next_line_len( d, d_width ))) {
|
|
|
|
printf( "%*.*s%*.*s\n", d_indent, d_indent, "", len, len, d );
|
|
|
|
d += len;
|
|
|
|
while( isspace( *d ) ) ++d;
|
|
|
|
}
|
2008-07-08 14:29:05 +00:00
|
|
|
}
|
|
|
|
|
2008-07-09 15:31:35 +00:00
|
|
|
static void
|
|
|
|
maxWidth( const struct tr_option * o,
|
2008-09-23 19:11:04 +00:00
|
|
|
int * longWidth,
|
|
|
|
int * shortWidth,
|
|
|
|
int * argWidth )
|
2008-07-08 14:29:05 +00:00
|
|
|
{
|
|
|
|
const char * arg;
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( o->longName )
|
2008-07-09 15:31:35 +00:00
|
|
|
*longWidth = MAX( *longWidth, (int)strlen( o->longName ) );
|
2008-07-08 14:29:05 +00:00
|
|
|
|
2008-07-09 15:31:35 +00:00
|
|
|
if( o->shortName )
|
|
|
|
*shortWidth = MAX( *shortWidth, (int)strlen( o->shortName ) );
|
2008-07-08 14:29:05 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( ( arg = getArgName( o ) ) )
|
2008-07-09 15:31:35 +00:00
|
|
|
*argWidth = MAX( *argWidth, (int)strlen( arg ) );
|
|
|
|
}
|
2008-07-08 14:29:05 +00:00
|
|
|
|
2008-07-09 15:31:35 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_getopt_usage( const char * progName,
|
|
|
|
const char * description,
|
|
|
|
const struct tr_option opts[] )
|
2008-07-09 15:31:35 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int longWidth = 0;
|
|
|
|
int shortWidth = 0;
|
|
|
|
int argWidth = 0;
|
|
|
|
struct tr_option help;
|
2008-07-09 15:31:35 +00:00
|
|
|
const struct tr_option * o;
|
2008-07-08 14:29:05 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( o = opts; o->val; ++o )
|
2008-07-09 15:31:35 +00:00
|
|
|
maxWidth( o, &longWidth, &shortWidth, &argWidth );
|
|
|
|
|
|
|
|
help.val = -1;
|
|
|
|
help.longName = "help";
|
|
|
|
help.description = "Display this help page and exit";
|
|
|
|
help.shortName = "h";
|
|
|
|
help.has_arg = 0;
|
|
|
|
maxWidth( &help, &longWidth, &shortWidth, &argWidth );
|
|
|
|
|
|
|
|
if( description == NULL )
|
|
|
|
description = "Usage: %s [options]";
|
|
|
|
printf( description, progName );
|
|
|
|
printf( "\n\nOptions:\n" );
|
|
|
|
getopts_usage_line( &help, longWidth, shortWidth, argWidth );
|
2008-09-23 19:11:04 +00:00
|
|
|
for( o = opts; o->val; ++o )
|
2008-07-09 15:31:35 +00:00
|
|
|
getopts_usage_line( o, longWidth, shortWidth, argWidth );
|
2008-07-08 14:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const tr_option *
|
2008-09-23 19:11:04 +00:00
|
|
|
findOption( const tr_option * opts,
|
|
|
|
const char * str,
|
|
|
|
const char ** setme_arg )
|
2008-07-08 14:29:05 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
size_t matchlen = 0;
|
|
|
|
const char * arg = NULL;
|
2008-07-08 14:29:05 +00:00
|
|
|
const tr_option * o;
|
2008-07-21 19:24:35 +00:00
|
|
|
const tr_option * match = NULL;
|
2008-07-08 14:29:05 +00:00
|
|
|
|
2008-07-21 19:24:35 +00:00
|
|
|
/* find the longest matching option */
|
2008-09-23 19:11:04 +00:00
|
|
|
for( o = opts; o->val; ++o )
|
2008-07-09 19:39:40 +00:00
|
|
|
{
|
2008-07-21 19:24:35 +00:00
|
|
|
size_t len = o->longName ? strlen( o->longName ) : 0;
|
|
|
|
|
|
|
|
if( ( matchlen < len ) && !memcmp( str, "--", 2 )
|
2008-09-23 19:11:04 +00:00
|
|
|
&& !memcmp( str + 2, o->longName, len )
|
|
|
|
&& ( str[len + 2] == '\0' || ( o->has_arg && str[len + 2] == '=' ) ) )
|
2008-07-21 19:24:35 +00:00
|
|
|
{
|
|
|
|
matchlen = len;
|
|
|
|
match = o;
|
2008-09-23 19:11:04 +00:00
|
|
|
arg = str[len + 2] == '=' ? str + len + 3 : NULL;
|
2008-07-21 19:24:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
len = o->shortName ? strlen( o->shortName ) : 0;
|
|
|
|
|
|
|
|
if( ( matchlen < len ) && !memcmp( str, "-", 1 )
|
2008-09-23 19:11:04 +00:00
|
|
|
&& !memcmp( str + 1, o->shortName, len )
|
|
|
|
&& ( str[len + 1] == '\0' || o->has_arg ) )
|
2008-07-21 19:24:35 +00:00
|
|
|
{
|
|
|
|
matchlen = len;
|
|
|
|
match = o;
|
2008-09-23 19:11:04 +00:00
|
|
|
switch( str[len + 1] )
|
|
|
|
{
|
|
|
|
case '\0':
|
|
|
|
arg = NULL; break;
|
|
|
|
|
|
|
|
case '=':
|
|
|
|
arg = str + len + 2; break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
arg = str + len + 1; break;
|
2008-07-08 14:29:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-21 19:24:35 +00:00
|
|
|
if( setme_arg )
|
|
|
|
*setme_arg = arg;
|
|
|
|
|
|
|
|
return match;
|
2008-07-08 14:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_getopt( const char * usage,
|
|
|
|
int argc,
|
|
|
|
const char ** argv,
|
|
|
|
const tr_option * opts,
|
|
|
|
const char ** setme_optarg )
|
2008-07-08 14:29:05 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int i;
|
|
|
|
const char * arg = NULL;
|
2008-07-08 14:29:05 +00:00
|
|
|
const tr_option * o = NULL;
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
*setme_optarg = NULL;
|
|
|
|
|
2008-07-08 14:29:05 +00:00
|
|
|
/* handle the builtin 'help' option */
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 1; i < argc; ++i )
|
|
|
|
{
|
|
|
|
if( !strcmp( argv[i], "-h" ) || !strcmp( argv[i], "--help" ) )
|
|
|
|
{
|
2008-07-09 15:31:35 +00:00
|
|
|
tr_getopt_usage( argv[0], usage, opts );
|
|
|
|
exit( 0 );
|
|
|
|
}
|
|
|
|
}
|
2008-07-08 14:29:05 +00:00
|
|
|
|
2008-07-09 15:31:35 +00:00
|
|
|
/* out of options? */
|
2008-09-23 19:11:04 +00:00
|
|
|
if( argc == 1 || tr_optind >= argc )
|
2008-07-08 14:29:05 +00:00
|
|
|
return TR_OPT_DONE;
|
|
|
|
|
2008-07-21 19:24:35 +00:00
|
|
|
o = findOption( opts, argv[tr_optind], &arg );
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !o )
|
|
|
|
{
|
2008-07-08 14:29:05 +00:00
|
|
|
/* let the user know we got an unknown option... */
|
2008-07-09 15:31:35 +00:00
|
|
|
*setme_optarg = argv[tr_optind++];
|
2008-07-08 14:29:05 +00:00
|
|
|
return TR_OPT_UNK;
|
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !o->has_arg )
|
|
|
|
{
|
2008-07-08 14:29:05 +00:00
|
|
|
/* no argument needed for this option, so we're done */
|
2008-07-21 19:24:35 +00:00
|
|
|
if( arg )
|
2008-07-08 14:29:05 +00:00
|
|
|
return TR_OPT_ERR;
|
|
|
|
*setme_optarg = NULL;
|
2008-07-21 19:24:35 +00:00
|
|
|
++tr_optind;
|
2008-07-08 14:29:05 +00:00
|
|
|
return o->val;
|
|
|
|
}
|
|
|
|
|
2008-07-21 19:24:35 +00:00
|
|
|
/* option needed an argument, and it was embedded in this string */
|
2008-09-23 19:11:04 +00:00
|
|
|
if( arg )
|
|
|
|
{
|
2008-07-21 19:24:35 +00:00
|
|
|
*setme_optarg = arg;
|
|
|
|
++tr_optind;
|
2008-07-08 14:29:05 +00:00
|
|
|
return o->val;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* throw an error if the option needed an argument but didn't get one */
|
2008-07-09 15:31:35 +00:00
|
|
|
if( ++tr_optind >= argc )
|
2008-07-08 14:29:05 +00:00
|
|
|
return TR_OPT_ERR;
|
2008-09-23 19:11:04 +00:00
|
|
|
if( findOption( opts, argv[tr_optind], NULL ) )
|
2008-07-08 14:29:05 +00:00
|
|
|
return TR_OPT_ERR;
|
|
|
|
|
2008-07-09 15:31:35 +00:00
|
|
|
*setme_optarg = argv[tr_optind++];
|
2008-07-08 14:29:05 +00:00
|
|
|
return o->val;
|
|
|
|
}
|