2008-07-08 14:29:05 +00:00
|
|
|
/*
|
2014-01-19 01:09:44 +00:00
|
|
|
* This file Copyright (C) 2008-2014 Mnemosyne LLC
|
2008-07-08 14:29:05 +00:00
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2008-07-09 15:31:35 +00:00
|
|
|
*
|
2008-07-08 14:29:05 +00:00
|
|
|
*/
|
|
|
|
|
2021-09-19 20:41:35 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cctype> /* isspace() */
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib> /* exit() */
|
|
|
|
#include <cstring>
|
2008-07-08 14:29:05 +00:00
|
|
|
|
2019-07-13 08:52:44 +00:00
|
|
|
#include "transmission.h"
|
2008-07-08 14:29:05 +00:00
|
|
|
#include "tr-getopt.h"
|
2017-06-18 12:34:21 +00:00
|
|
|
#include "tr-macros.h"
|
2019-07-13 08:52:44 +00:00
|
|
|
#include "utils.h"
|
2008-07-08 14:29:05 +00:00
|
|
|
|
2008-07-09 15:31:35 +00:00
|
|
|
int tr_optind = 1;
|
2008-07-08 14:29:05 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* getArgName(tr_option const* opt)
|
2008-07-08 14:29:05 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* arg;
|
2008-07-09 15:31:35 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!opt->has_arg)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2008-07-09 15:31:35 +00:00
|
|
|
arg = "";
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-09-15 00:18:09 +00:00
|
|
|
else if (opt->argName != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2008-07-09 15:31:35 +00:00
|
|
|
arg = opt->argName;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-07-09 15:31:35 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2008-07-09 15:31:35 +00:00
|
|
|
arg = "<args>";
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-07-09 15:31:35 +00:00
|
|
|
|
|
|
|
return arg;
|
2008-07-08 14:29:05 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static int get_next_line_len(char const* description, int maxlen)
|
2010-06-16 14:05:18 +00:00
|
|
|
{
|
|
|
|
int end;
|
2017-04-19 12:04:45 +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)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2010-06-16 14:05:18 +00:00
|
|
|
return len;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-06-16 14:05:18 +00:00
|
|
|
|
|
|
|
end = maxlen < len ? maxlen : len;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
while (end > 0 && !isspace(description[end]))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2010-06-16 14:05:18 +00:00
|
|
|
--end;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-06-16 14:05:18 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
return end != 0 ? end : len;
|
2010-06-16 14:05:18 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void getopts_usage_line(tr_option const* opt, int longWidth, int shortWidth, int argWidth)
|
2008-07-08 14:29:05 +00:00
|
|
|
{
|
2010-12-12 16:43:19 +00:00
|
|
|
int len;
|
2021-09-15 00:18:09 +00:00
|
|
|
char const* longName = opt->longName != nullptr ? opt->longName : "";
|
|
|
|
char const* shortName = opt->shortName != nullptr ? opt->shortName : "";
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* arg = getArgName(opt);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
int const d_indent = shortWidth + longWidth + argWidth + 7;
|
|
|
|
int const d_width = 80 - d_indent;
|
|
|
|
char const* d = opt->description;
|
2010-06-16 14:05:18 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
printf(
|
|
|
|
" %s%-*s %s%-*s %-*s ",
|
|
|
|
tr_str_is_empty(shortName) ? " " : "-",
|
|
|
|
TR_ARG_TUPLE(shortWidth, shortName),
|
|
|
|
tr_str_is_empty(longName) ? " " : "--",
|
|
|
|
TR_ARG_TUPLE(longWidth, longName),
|
|
|
|
TR_ARG_TUPLE(argWidth, arg));
|
2017-04-19 12:04:45 +00:00
|
|
|
len = get_next_line_len(d, d_width);
|
2021-08-15 09:41:48 +00:00
|
|
|
printf("%*.*s\n", TR_ARG_TUPLE(len, len, d));
|
2010-06-16 14:05:18 +00:00
|
|
|
|
|
|
|
d += len;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
while (isspace(*d))
|
|
|
|
{
|
|
|
|
++d;
|
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
while ((len = get_next_line_len(d, d_width)) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
printf("%*.*s%*.*s\n", TR_ARG_TUPLE(d_indent, d_indent, ""), TR_ARG_TUPLE(len, len, d));
|
2010-06-16 14:05:18 +00:00
|
|
|
d += len;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
while (isspace(*d))
|
|
|
|
{
|
|
|
|
++d;
|
|
|
|
}
|
2010-06-16 14:05:18 +00:00
|
|
|
}
|
2008-07-08 14:29:05 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void maxWidth(struct tr_option const* o, int* longWidth, int* shortWidth, int* argWidth)
|
2008-07-08 14:29:05 +00:00
|
|
|
{
|
2021-09-19 20:41:35 +00:00
|
|
|
// FIXME: in this function sign bits from int* are lost, then 64-bit result is truncated to 32-bit int
|
|
|
|
// Convert arguments to size_t*
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* arg;
|
2008-07-08 14:29:05 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (o->longName != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-19 20:41:35 +00:00
|
|
|
*longWidth = std::max((size_t)*longWidth, strlen(o->longName));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-07-08 14:29:05 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (o->shortName != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-19 20:41:35 +00:00
|
|
|
*shortWidth = std::max((size_t)*shortWidth, strlen(o->shortName));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-07-08 14:29:05 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if ((arg = getArgName(o)) != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-19 20:41:35 +00:00
|
|
|
*argWidth = std::max((size_t)*argWidth, strlen(arg));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-07-09 15:31:35 +00:00
|
|
|
}
|
2008-07-08 14:29:05 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_getopt_usage(char const* progName, char const* description, struct tr_option const opts[])
|
2008-07-09 15:31:35 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int longWidth = 0;
|
|
|
|
int shortWidth = 0;
|
|
|
|
int argWidth = 0;
|
|
|
|
struct tr_option help;
|
2008-07-08 14:29:05 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (tr_option const* o = opts; o->val != 0; ++o)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
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";
|
2019-07-14 12:40:41 +00:00
|
|
|
help.has_arg = false;
|
2017-04-19 12:04:45 +00:00
|
|
|
maxWidth(&help, &longWidth, &shortWidth, &argWidth);
|
2008-07-09 15:31:35 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (description == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2008-07-09 15:31:35 +00:00
|
|
|
description = "Usage: %s [options]";
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
printf(description, progName);
|
|
|
|
printf("\n\nOptions:\n");
|
|
|
|
getopts_usage_line(&help, longWidth, shortWidth, argWidth);
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (tr_option const* o = opts; o->val != 0; ++o)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
getopts_usage_line(o, longWidth, shortWidth, argWidth);
|
|
|
|
}
|
2008-07-08 14:29:05 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static tr_option const* findOption(tr_option const* opts, char const* str, char const** setme_arg)
|
2008-07-08 14:29:05 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t matchlen = 0;
|
2021-09-15 00:18:09 +00:00
|
|
|
char const* arg = nullptr;
|
|
|
|
tr_option const* match = nullptr;
|
2008-07-08 14:29:05 +00:00
|
|
|
|
2008-07-21 19:24:35 +00:00
|
|
|
/* find the longest matching option */
|
2017-05-13 22:38:31 +00:00
|
|
|
for (tr_option const* o = opts; o->val != 0; ++o)
|
2008-07-09 19:39:40 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
size_t len = o->longName != nullptr ? strlen(o->longName) : 0;
|
2008-07-21 19:24:35 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (matchlen < len && str[0] == '-' && str[1] == '-' && strncmp(str + 2, o->longName, len) == 0 &&
|
2017-04-19 12:04:45 +00:00
|
|
|
(str[len + 2] == '\0' || (o->has_arg && str[len + 2] == '=')))
|
2008-07-21 19:24:35 +00:00
|
|
|
{
|
|
|
|
matchlen = len;
|
|
|
|
match = o;
|
2021-09-15 00:18:09 +00:00
|
|
|
arg = str[len + 2] == '=' ? str + len + 3 : nullptr;
|
2008-07-21 19:24:35 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
len = o->shortName != nullptr ? strlen(o->shortName) : 0;
|
2008-07-21 19:24:35 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (matchlen < len && str[0] == '-' && strncmp(str + 1, o->shortName, len) == 0 && (str[len + 1] == '\0' || o->has_arg))
|
2008-07-21 19:24:35 +00:00
|
|
|
{
|
|
|
|
matchlen = len;
|
|
|
|
match = o;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
switch (str[len + 1])
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case '\0':
|
2021-09-15 00:18:09 +00:00
|
|
|
arg = nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case '=':
|
|
|
|
arg = str + len + 2;
|
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
default:
|
|
|
|
arg = str + len + 1;
|
|
|
|
break;
|
2008-07-08 14:29:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (setme_arg != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2008-07-21 19:24:35 +00:00
|
|
|
*setme_arg = arg;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-07-21 19:24:35 +00:00
|
|
|
|
|
|
|
return match;
|
2008-07-08 14:29:05 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
int tr_getopt(char const* usage, int argc, char const* const* argv, tr_option const* opts, char const** setme_optarg)
|
2008-07-08 14:29:05 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
char const* arg = nullptr;
|
|
|
|
tr_option const* o = nullptr;
|
2008-07-08 14:29:05 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
*setme_optarg = nullptr;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-07-08 14:29:05 +00:00
|
|
|
/* handle the builtin 'help' option */
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 1; i < argc; ++i)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +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)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2008-07-08 14:29:05 +00:00
|
|
|
return TR_OPT_DONE;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
o = findOption(opts, argv[tr_optind], &arg);
|
2008-07-08 14:29:05 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (o == nullptr)
|
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 */
|
2021-09-15 00:18:09 +00:00
|
|
|
if (arg != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2008-07-08 14:29:05 +00:00
|
|
|
return TR_OPT_ERR;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
*setme_optarg = nullptr;
|
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 */
|
2021-09-15 00:18:09 +00:00
|
|
|
if (arg != nullptr)
|
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)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2008-07-08 14:29:05 +00:00
|
|
|
return TR_OPT_ERR;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (findOption(opts, argv[tr_optind], nullptr) != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2008-07-08 14:29:05 +00:00
|
|
|
return TR_OPT_ERR;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-07-08 14:29:05 +00:00
|
|
|
|
2008-07-09 15:31:35 +00:00
|
|
|
*setme_optarg = argv[tr_optind++];
|
2008-07-08 14:29:05 +00:00
|
|
|
return o->val;
|
|
|
|
}
|