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>
|
2021-10-01 17:47:23 +00:00
|
|
|
#include <string_view>
|
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
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!opt->has_arg)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-21 17:40:36 +00:00
|
|
|
return "";
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-10-21 17:40:36 +00:00
|
|
|
|
|
|
|
if (opt->argName != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-21 17:40:36 +00:00
|
|
|
return opt->argName;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-07-09 15:31:35 +00:00
|
|
|
|
2021-10-21 17:40:36 +00:00
|
|
|
return "<args>";
|
2008-07-08 14:29:05 +00:00
|
|
|
}
|
|
|
|
|
2021-10-01 17:47:23 +00:00
|
|
|
static size_t get_next_line_len(std::string_view description, size_t maxlen)
|
2010-06-16 14:05:18 +00:00
|
|
|
{
|
2021-10-01 17:47:23 +00:00
|
|
|
auto len = std::size(description);
|
|
|
|
if (len > maxlen)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-01 17:47:23 +00:00
|
|
|
description.remove_suffix(len - maxlen);
|
|
|
|
auto const pos = description.rfind(' ');
|
|
|
|
len = pos != std::string_view::npos ? pos : maxlen;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-10-01 17:47:23 +00:00
|
|
|
return 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
|
|
|
{
|
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;
|
2021-10-01 17:47:23 +00:00
|
|
|
auto d = std::string_view{ 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));
|
2010-06-16 14:05:18 +00:00
|
|
|
|
2021-10-01 17:47:23 +00:00
|
|
|
auto const strip_leading_whitespace = [](std::string_view text)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-01 17:47:23 +00:00
|
|
|
auto pos = text.find_first_not_of(' ');
|
|
|
|
if (pos != std::string_view::npos)
|
|
|
|
{
|
|
|
|
text.remove_prefix(pos);
|
|
|
|
}
|
|
|
|
return text;
|
|
|
|
};
|
|
|
|
|
|
|
|
int len = get_next_line_len(d, d_width);
|
|
|
|
printf("%*.*s\n", TR_ARG_TUPLE(len, len, std::data(d)));
|
|
|
|
d.remove_prefix(len);
|
|
|
|
d = strip_leading_whitespace(d);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
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-10-01 17:47:23 +00:00
|
|
|
printf("%*.*s%*.*s\n", TR_ARG_TUPLE(d_indent, d_indent, ""), TR_ARG_TUPLE(len, len, std::data(d)));
|
|
|
|
d.remove_prefix(len);
|
|
|
|
d = strip_leading_whitespace(d);
|
2010-06-16 14:05:18 +00:00
|
|
|
}
|
2008-07-08 14:29:05 +00:00
|
|
|
}
|
|
|
|
|
2021-10-01 17:47:23 +00:00
|
|
|
static void maxWidth(struct tr_option const* o, size_t& longWidth, size_t& shortWidth, size_t& argWidth)
|
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-10-01 17:47:23 +00:00
|
|
|
longWidth = std::max(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-10-01 17:47:23 +00:00
|
|
|
shortWidth = std::max(shortWidth, strlen(o->shortName));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-07-08 14:29:05 +00:00
|
|
|
|
2021-10-21 17:40:36 +00:00
|
|
|
char const* const arg = getArgName(o);
|
|
|
|
if (arg != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-01 17:47:23 +00:00
|
|
|
argWidth = std::max(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
|
|
|
{
|
2021-10-01 17:47:23 +00:00
|
|
|
auto longWidth = size_t{ 0 };
|
|
|
|
auto shortWidth = size_t{ 0 };
|
|
|
|
auto argWidth = size_t{ 0 };
|
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
|
|
|
{
|
2021-10-01 17:47:23 +00:00
|
|
|
maxWidth(o, longWidth, shortWidth, argWidth);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-07-09 15:31:35 +00:00
|
|
|
|
2021-10-01 17:47:23 +00:00
|
|
|
auto const help = tr_option{ -1, "help", "Display this help page and exit", "h", false, nullptr };
|
|
|
|
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;
|
|
|
|
}
|