2008-07-08 14:29:05 +00:00
|
|
|
/*
|
2011-01-19 13:48:47 +00:00
|
|
|
* This file Copyright (C) Mnemosyne LLC
|
2008-07-08 14:29:05 +00:00
|
|
|
*
|
2010-12-27 19:18:17 +00:00
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
2012-12-05 17:29:46 +00:00
|
|
|
* 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
|
|
|
*/
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
#include <ctype.h> /* isspace () */
|
2008-07-08 14:29:05 +00:00
|
|
|
#include <stdio.h>
|
2012-12-05 17:29:46 +00:00
|
|
|
#include <stdlib.h> /* exit () */
|
2008-07-08 14:29:05 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "tr-getopt.h"
|
|
|
|
|
|
|
|
#ifndef MAX
|
2012-12-05 17:29:46 +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*
|
2012-12-05 17:29:46 +00:00
|
|
|
getArgName (const tr_option * opt)
|
2008-07-08 14:29:05 +00:00
|
|
|
{
|
2009-01-23 18:44:15 +00:00
|
|
|
const char * arg;
|
2008-07-09 15:31:35 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!opt->has_arg)
|
2008-07-09 15:31:35 +00:00
|
|
|
arg = "";
|
2012-12-05 17:29:46 +00:00
|
|
|
else if (opt->argName)
|
2008-07-09 15:31:35 +00:00
|
|
|
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
|
2012-12-05 17:29:46 +00:00
|
|
|
get_next_line_len (const char * description, int maxlen)
|
2010-06-16 14:05:18 +00:00
|
|
|
{
|
|
|
|
int end;
|
2012-12-05 17:29:46 +00:00
|
|
|
int len = strlen (description);
|
2010-06-16 14:05:18 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (len < maxlen)
|
2010-06-16 14:05:18 +00:00
|
|
|
return len;
|
|
|
|
|
|
|
|
end = maxlen < len ? maxlen : len;
|
2012-12-05 17:29:46 +00:00
|
|
|
while ((end > 0) && !isspace (description[end]))
|
2010-06-16 14:05:18 +00:00
|
|
|
--end;
|
|
|
|
|
|
|
|
return end ? end : len;
|
|
|
|
}
|
|
|
|
|
2008-07-08 14:29:05 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
getopts_usage_line (const tr_option * opt,
|
2008-09-23 19:11:04 +00:00
|
|
|
int longWidth,
|
|
|
|
int shortWidth,
|
2012-12-05 17:29:46 +00:00
|
|
|
int argWidth)
|
2008-07-08 14:29:05 +00:00
|
|
|
{
|
2010-12-12 16:43:19 +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 : "";
|
2012-12-05 17:29:46 +00:00
|
|
|
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;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
printf (" %s%-*s %s%-*s %-*s ",
|
|
|
|
(shortName && *shortName ? "-" : " "), shortWidth, shortName,
|
|
|
|
(longName && *longName ? "--" : " "), longWidth, longName,
|
|
|
|
argWidth, arg);
|
|
|
|
len = get_next_line_len (d, d_width);
|
|
|
|
printf ("%*.*s\n", len, len, d);
|
2010-06-16 14:05:18 +00:00
|
|
|
|
|
|
|
d += len;
|
2012-12-05 17:29:46 +00:00
|
|
|
while (isspace (*d)) ++d;
|
2010-06-16 14:05:18 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
while ((len = get_next_line_len (d, d_width))) {
|
|
|
|
printf ("%*.*s%*.*s\n", d_indent, d_indent, "", len, len, d);
|
2010-06-16 14:05:18 +00:00
|
|
|
d += len;
|
2012-12-05 17:29:46 +00:00
|
|
|
while (isspace (*d)) ++d;
|
2010-06-16 14:05:18 +00:00
|
|
|
}
|
2008-07-08 14:29:05 +00:00
|
|
|
}
|
|
|
|
|
2008-07-09 15:31:35 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
maxWidth (const struct tr_option * o,
|
2008-09-23 19:11:04 +00:00
|
|
|
int * longWidth,
|
|
|
|
int * shortWidth,
|
2012-12-05 17:29:46 +00:00
|
|
|
int * argWidth)
|
2008-07-08 14:29:05 +00:00
|
|
|
{
|
|
|
|
const char * arg;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (o->longName)
|
|
|
|
*longWidth = MAX (*longWidth, (int)strlen (o->longName));
|
2008-07-08 14:29:05 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (o->shortName)
|
|
|
|
*shortWidth = MAX (*shortWidth, (int)strlen (o->shortName));
|
2008-07-08 14:29:05 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if ((arg = getArgName (o)))
|
|
|
|
*argWidth = MAX (*argWidth, (int)strlen (arg));
|
2008-07-09 15:31:35 +00:00
|
|
|
}
|
2008-07-08 14:29:05 +00:00
|
|
|
|
2008-07-09 15:31:35 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_getopt_usage (const char * progName,
|
2008-09-23 19:11:04 +00:00
|
|
|
const char * description,
|
2012-12-05 17:29:46 +00:00
|
|
|
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
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
for (o = opts; o->val; ++o)
|
|
|
|
maxWidth (o, &longWidth, &shortWidth, &argWidth);
|
2008-07-09 15:31:35 +00:00
|
|
|
|
|
|
|
help.val = -1;
|
|
|
|
help.longName = "help";
|
|
|
|
help.description = "Display this help page and exit";
|
|
|
|
help.shortName = "h";
|
|
|
|
help.has_arg = 0;
|
2012-12-05 17:29:46 +00:00
|
|
|
maxWidth (&help, &longWidth, &shortWidth, &argWidth);
|
2008-07-09 15:31:35 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (description == NULL)
|
2008-07-09 15:31:35 +00:00
|
|
|
description = "Usage: %s [options]";
|
2012-12-05 17:29:46 +00:00
|
|
|
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);
|
2008-07-08 14:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const tr_option *
|
2012-12-05 17:29:46 +00:00
|
|
|
findOption (const tr_option * opts,
|
2008-09-23 19:11:04 +00:00
|
|
|
const char * str,
|
2012-12-05 17:29:46 +00:00
|
|
|
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 */
|
2012-12-05 17:29:46 +00:00
|
|
|
for (o = opts; o->val; ++o)
|
2008-07-09 19:39:40 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
size_t len = o->longName ? strlen (o->longName) : 0;
|
2008-07-21 19:24:35 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if ((matchlen < len) && !memcmp (str, "--", 2)
|
|
|
|
&& !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
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
len = o->shortName ? strlen (o->shortName) : 0;
|
2008-07-21 19:24:35 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if ((matchlen < len) && !memcmp (str, "-", 1)
|
|
|
|
&& !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;
|
2012-12-05 17:29:46 +00:00
|
|
|
switch (str[len + 1])
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (setme_arg)
|
2008-07-21 19:24:35 +00:00
|
|
|
*setme_arg = arg;
|
|
|
|
|
|
|
|
return match;
|
2008-07-08 14:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_getopt (const char * usage,
|
2008-09-23 19:11:04 +00:00
|
|
|
int argc,
|
|
|
|
const char ** argv,
|
|
|
|
const tr_option * opts,
|
2012-12-05 17:29:46 +00:00
|
|
|
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 */
|
2012-12-05 17:29:46 +00:00
|
|
|
for (i = 1; i < argc; ++i)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!strcmp (argv[i], "-h") || !strcmp (argv[i], "--help"))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_getopt_usage (argv[0], usage, opts);
|
|
|
|
exit (0);
|
2008-07-09 15:31:35 +00:00
|
|
|
}
|
|
|
|
}
|
2008-07-08 14:29:05 +00:00
|
|
|
|
2008-07-09 15:31:35 +00:00
|
|
|
/* out of options? */
|
2012-12-05 17:29:46 +00:00
|
|
|
if (argc == 1 || tr_optind >= argc)
|
2008-07-08 14:29:05 +00:00
|
|
|
return TR_OPT_DONE;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
o = findOption (opts, argv[tr_optind], &arg);
|
|
|
|
if (!o)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!o->has_arg)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2008-07-08 14:29:05 +00:00
|
|
|
/* no argument needed for this option, so we're done */
|
2012-12-05 17:29:46 +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 */
|
2012-12-05 17:29:46 +00:00
|
|
|
if (arg)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
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 */
|
2012-12-05 17:29:46 +00:00
|
|
|
if (++tr_optind >= argc)
|
2008-07-08 14:29:05 +00:00
|
|
|
return TR_OPT_ERR;
|
2012-12-05 17:29:46 +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;
|
|
|
|
}
|