2018-10-31 16:50:43 +00:00
|
|
|
/*
|
2020-05-29 19:15:08 +00:00
|
|
|
* Copyright (c) 2020, Sergey Bugaev <bugaevc@serenityos.org>
|
2019-09-06 18:03:16 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2018-10-31 16:50:43 +00:00
|
|
|
*/
|
|
|
|
|
2020-05-29 19:15:08 +00:00
|
|
|
#include <AK/StringView.h>
|
|
|
|
#include <AK/Vector.h>
|
2019-09-06 18:03:16 +00:00
|
|
|
#include <getopt.h>
|
|
|
|
#include <stdio.h>
|
2020-05-29 19:15:08 +00:00
|
|
|
#include <string.h>
|
2020-09-05 17:47:40 +00:00
|
|
|
#include <unistd.h>
|
2019-09-06 18:03:16 +00:00
|
|
|
|
2020-05-29 19:15:08 +00:00
|
|
|
int opterr = 1;
|
|
|
|
int optopt = 0;
|
|
|
|
int optind = 1;
|
|
|
|
int optreset = 0;
|
|
|
|
char* optarg = nullptr;
|
2019-09-06 18:03:16 +00:00
|
|
|
|
2020-05-29 19:15:08 +00:00
|
|
|
// POSIX says, "When an element of argv[] contains multiple option characters,
|
|
|
|
// it is unspecified how getopt() determines which options have already been
|
|
|
|
// processed". Well, this is how we do it.
|
|
|
|
static size_t s_index_into_multioption_argument = 0;
|
2019-09-06 18:03:16 +00:00
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
[[gnu::format(printf, 1, 2)]] static inline void report_error(char const* format, ...)
|
2020-05-29 19:15:08 +00:00
|
|
|
{
|
|
|
|
if (!opterr)
|
|
|
|
return;
|
2019-09-06 18:03:16 +00:00
|
|
|
|
2020-05-29 19:15:08 +00:00
|
|
|
fputs("\033[31m", stderr);
|
2019-09-06 18:03:16 +00:00
|
|
|
|
2020-05-29 19:15:08 +00:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
|
|
|
vfprintf(stderr, format, ap);
|
|
|
|
va_end(ap);
|
2019-09-06 18:03:16 +00:00
|
|
|
|
2020-05-29 19:15:08 +00:00
|
|
|
fputs("\033[0m\n", stderr);
|
|
|
|
}
|
2019-09-06 18:03:16 +00:00
|
|
|
|
2020-05-29 19:15:08 +00:00
|
|
|
namespace {
|
2019-09-06 18:03:16 +00:00
|
|
|
|
2020-05-29 19:15:08 +00:00
|
|
|
class OptionParser {
|
|
|
|
public:
|
2022-04-01 17:58:27 +00:00
|
|
|
OptionParser(int argc, char* const* argv, StringView short_options, option const* long_options, int* out_long_option_index = nullptr);
|
2020-05-29 19:15:08 +00:00
|
|
|
int getopt();
|
2019-09-06 18:03:16 +00:00
|
|
|
|
2020-05-29 19:15:08 +00:00
|
|
|
private:
|
|
|
|
bool lookup_short_option(char option, int& needs_value) const;
|
|
|
|
int handle_short_option();
|
2019-09-06 18:03:16 +00:00
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
option const* lookup_long_option(char* raw) const;
|
2020-05-29 19:15:08 +00:00
|
|
|
int handle_long_option();
|
2019-09-06 18:03:16 +00:00
|
|
|
|
2020-05-29 19:15:08 +00:00
|
|
|
void shift_argv();
|
|
|
|
bool find_next_option();
|
2019-09-06 18:03:16 +00:00
|
|
|
|
2022-07-11 20:52:03 +00:00
|
|
|
StringView current_arg() const
|
|
|
|
{
|
|
|
|
auto const* arg_ptr = m_argv[m_arg_index];
|
|
|
|
if (arg_ptr == NULL)
|
|
|
|
return {};
|
|
|
|
return { arg_ptr, strlen(arg_ptr) };
|
|
|
|
}
|
|
|
|
|
2020-05-29 19:15:08 +00:00
|
|
|
size_t m_argc { 0 };
|
2021-06-13 14:35:04 +00:00
|
|
|
char* const* m_argv { nullptr };
|
2020-05-29 19:15:08 +00:00
|
|
|
StringView m_short_options;
|
2022-04-01 17:58:27 +00:00
|
|
|
option const* m_long_options { nullptr };
|
2020-05-29 19:15:08 +00:00
|
|
|
int* m_out_long_option_index { nullptr };
|
|
|
|
bool m_stop_on_first_non_option { false };
|
2019-09-06 18:03:16 +00:00
|
|
|
|
2020-05-29 19:15:08 +00:00
|
|
|
size_t m_arg_index { 0 };
|
|
|
|
size_t m_consumed_args { 0 };
|
|
|
|
};
|
2019-09-06 18:03:16 +00:00
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
OptionParser::OptionParser(int argc, char* const* argv, StringView short_options, option const* long_options, int* out_long_option_index)
|
2020-05-29 19:15:08 +00:00
|
|
|
: m_argc(argc)
|
2021-06-13 14:35:04 +00:00
|
|
|
, m_argv(argv)
|
2020-05-29 19:15:08 +00:00
|
|
|
, m_short_options(short_options)
|
|
|
|
, m_long_options(long_options)
|
|
|
|
, m_out_long_option_index(out_long_option_index)
|
2019-09-06 18:03:16 +00:00
|
|
|
{
|
2020-05-29 19:15:08 +00:00
|
|
|
// In the following case:
|
|
|
|
// $ foo bar -o baz
|
|
|
|
// we want to parse the option (-o baz) first, and leave the argument (bar)
|
|
|
|
// in argv after we return -1 when invoked the second time. So we reorder
|
|
|
|
// argv to put options first and positional arguments next. To turn this
|
|
|
|
// behavior off, start the short options spec with a "+". This is a GNU
|
|
|
|
// extension that we support.
|
|
|
|
m_stop_on_first_non_option = short_options.starts_with('+');
|
|
|
|
|
|
|
|
// See if we should reset the internal state.
|
|
|
|
if (optreset || optind == 0) {
|
|
|
|
optreset = 0;
|
|
|
|
optind = 1;
|
|
|
|
s_index_into_multioption_argument = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
optopt = 0;
|
|
|
|
optarg = nullptr;
|
|
|
|
}
|
2019-09-06 18:03:16 +00:00
|
|
|
|
2020-05-29 19:15:08 +00:00
|
|
|
int OptionParser::getopt()
|
|
|
|
{
|
|
|
|
bool should_reorder_argv = !m_stop_on_first_non_option;
|
|
|
|
int res = -1;
|
|
|
|
|
|
|
|
bool found_an_option = find_next_option();
|
2022-07-11 20:52:03 +00:00
|
|
|
auto arg = current_arg();
|
2020-05-29 19:15:08 +00:00
|
|
|
|
|
|
|
if (!found_an_option) {
|
|
|
|
res = -1;
|
|
|
|
if (arg == "--")
|
|
|
|
m_consumed_args = 1;
|
|
|
|
else
|
|
|
|
m_consumed_args = 0;
|
|
|
|
} else {
|
|
|
|
// Alright, so we have an option on our hands!
|
2022-07-11 17:32:29 +00:00
|
|
|
bool is_long_option = arg.starts_with("--"sv);
|
2020-05-29 19:15:08 +00:00
|
|
|
if (is_long_option)
|
|
|
|
res = handle_long_option();
|
|
|
|
else
|
|
|
|
res = handle_short_option();
|
|
|
|
|
|
|
|
// If we encountered an error, return immediately.
|
|
|
|
if (res == '?')
|
|
|
|
return '?';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (should_reorder_argv)
|
|
|
|
shift_argv();
|
|
|
|
else
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(optind == static_cast<int>(m_arg_index));
|
2020-05-29 19:15:08 +00:00
|
|
|
optind += m_consumed_args;
|
|
|
|
|
|
|
|
return res;
|
2019-09-06 18:03:16 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 19:15:08 +00:00
|
|
|
bool OptionParser::lookup_short_option(char option, int& needs_value) const
|
2019-09-06 18:03:16 +00:00
|
|
|
{
|
2022-10-22 13:38:21 +00:00
|
|
|
Vector<StringView> parts = m_short_options.split_view(option, SplitBehavior::KeepEmpty);
|
2020-05-29 19:15:08 +00:00
|
|
|
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(parts.size() <= 2);
|
2020-05-29 19:15:08 +00:00
|
|
|
if (parts.size() < 2) {
|
|
|
|
// Haven't found the option in the spec.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:32:29 +00:00
|
|
|
if (parts[1].starts_with("::"sv)) {
|
2020-05-29 19:15:08 +00:00
|
|
|
// If an option is followed by two colons, it optionally accepts an
|
|
|
|
// argument.
|
|
|
|
needs_value = optional_argument;
|
|
|
|
} else if (parts[1].starts_with(':')) {
|
|
|
|
// If it's followed by one colon, it requires an argument.
|
|
|
|
needs_value = required_argument;
|
|
|
|
} else {
|
|
|
|
// Otherwise, it doesn't accept arguments.
|
|
|
|
needs_value = no_argument;
|
|
|
|
}
|
|
|
|
return true;
|
2019-09-06 18:03:16 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 19:15:08 +00:00
|
|
|
int OptionParser::handle_short_option()
|
2019-09-06 18:03:16 +00:00
|
|
|
{
|
2022-07-11 20:52:03 +00:00
|
|
|
StringView arg = current_arg();
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(arg.starts_with('-'));
|
2020-05-29 19:15:08 +00:00
|
|
|
|
|
|
|
if (s_index_into_multioption_argument == 0) {
|
|
|
|
// Just starting to parse this argument, skip the "-".
|
|
|
|
s_index_into_multioption_argument = 1;
|
|
|
|
}
|
|
|
|
char option = arg[s_index_into_multioption_argument];
|
|
|
|
s_index_into_multioption_argument++;
|
|
|
|
|
|
|
|
int needs_value = no_argument;
|
|
|
|
bool ok = lookup_short_option(option, needs_value);
|
|
|
|
if (!ok) {
|
|
|
|
optopt = option;
|
2020-06-17 16:20:28 +00:00
|
|
|
report_error("Unrecognized option \033[1m-%c\033[22m", option);
|
2020-05-29 19:15:08 +00:00
|
|
|
return '?';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let's see if we're at the end of this argument already.
|
|
|
|
if (s_index_into_multioption_argument < arg.length()) {
|
|
|
|
// This not yet the end.
|
|
|
|
if (needs_value == no_argument) {
|
|
|
|
optarg = nullptr;
|
|
|
|
m_consumed_args = 0;
|
|
|
|
} else {
|
|
|
|
// Treat the rest of the argument as the value, the "-ovalue"
|
|
|
|
// syntax.
|
|
|
|
optarg = m_argv[m_arg_index] + s_index_into_multioption_argument;
|
|
|
|
// Next time, process the next argument.
|
|
|
|
s_index_into_multioption_argument = 0;
|
|
|
|
m_consumed_args = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
s_index_into_multioption_argument = 0;
|
|
|
|
if (needs_value != required_argument) {
|
|
|
|
optarg = nullptr;
|
|
|
|
m_consumed_args = 1;
|
|
|
|
} else if (m_arg_index + 1 < m_argc) {
|
|
|
|
// Treat the next argument as a value, the "-o value" syntax.
|
|
|
|
optarg = m_argv[m_arg_index + 1];
|
|
|
|
m_consumed_args = 2;
|
|
|
|
} else {
|
2020-06-17 16:20:28 +00:00
|
|
|
report_error("Missing value for option \033[1m-%c\033[22m", option);
|
2020-05-29 19:15:08 +00:00
|
|
|
return '?';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return option;
|
2019-09-06 18:03:16 +00:00
|
|
|
}
|
2018-10-31 16:50:43 +00:00
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
option const* OptionParser::lookup_long_option(char* raw) const
|
2018-10-31 16:50:43 +00:00
|
|
|
{
|
2022-07-11 19:53:29 +00:00
|
|
|
StringView arg { raw, strlen(raw) };
|
2020-05-29 19:15:08 +00:00
|
|
|
|
|
|
|
for (size_t index = 0; m_long_options[index].name; index++) {
|
|
|
|
auto& option = m_long_options[index];
|
2022-07-11 19:53:29 +00:00
|
|
|
StringView name { option.name, strlen(option.name) };
|
2020-05-29 19:15:08 +00:00
|
|
|
|
|
|
|
if (!arg.starts_with(name))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// It would be better to not write out the index at all unless we're
|
|
|
|
// sure we've found the right option, but whatever.
|
|
|
|
if (m_out_long_option_index)
|
|
|
|
*m_out_long_option_index = index;
|
|
|
|
|
|
|
|
// Can either be "--option" or "--option=value".
|
|
|
|
if (arg.length() == name.length()) {
|
|
|
|
optarg = nullptr;
|
|
|
|
return &option;
|
|
|
|
}
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(arg.length() > name.length());
|
2020-05-29 19:15:08 +00:00
|
|
|
if (arg[name.length()] == '=') {
|
|
|
|
optarg = raw + name.length() + 1;
|
|
|
|
return &option;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
2019-09-06 18:03:16 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 19:15:08 +00:00
|
|
|
int OptionParser::handle_long_option()
|
2019-09-06 18:03:16 +00:00
|
|
|
{
|
2022-07-11 20:52:03 +00:00
|
|
|
VERIFY(current_arg().starts_with("--"sv));
|
2020-05-29 19:15:08 +00:00
|
|
|
|
|
|
|
// We cannot set optopt to anything sensible for long options, so set it to 0.
|
|
|
|
optopt = 0;
|
|
|
|
|
|
|
|
auto* option = lookup_long_option(m_argv[m_arg_index] + 2);
|
|
|
|
if (!option) {
|
2020-06-17 16:20:28 +00:00
|
|
|
report_error("Unrecognized option \033[1m%s\033[22m", m_argv[m_arg_index]);
|
2020-05-29 19:15:08 +00:00
|
|
|
return '?';
|
|
|
|
}
|
|
|
|
// lookup_long_option() will also set optarg if the value of the option is
|
|
|
|
// specified using "--option=value" syntax.
|
|
|
|
|
|
|
|
// Figure out whether this option needs and/or has a value (also called "an
|
|
|
|
// argument", but let's not call it that to distinguish it from argv
|
|
|
|
// elements).
|
|
|
|
switch (option->has_arg) {
|
|
|
|
case no_argument:
|
|
|
|
if (optarg) {
|
2020-06-17 16:20:28 +00:00
|
|
|
report_error("Option \033[1m--%s\033[22m doesn't accept an argument", option->name);
|
2020-05-29 19:15:08 +00:00
|
|
|
return '?';
|
|
|
|
}
|
|
|
|
m_consumed_args = 1;
|
|
|
|
break;
|
|
|
|
case optional_argument:
|
|
|
|
m_consumed_args = 1;
|
|
|
|
break;
|
|
|
|
case required_argument:
|
|
|
|
if (optarg) {
|
|
|
|
// Value specified using "--option=value" syntax.
|
|
|
|
m_consumed_args = 1;
|
|
|
|
} else if (m_arg_index + 1 < m_argc) {
|
|
|
|
// Treat the next argument as a value in "--option value" syntax.
|
|
|
|
optarg = m_argv[m_arg_index + 1];
|
|
|
|
m_consumed_args = 2;
|
|
|
|
} else {
|
2020-06-17 16:20:28 +00:00
|
|
|
report_error("Missing value for option \033[1m--%s\033[22m", option->name);
|
2020-05-29 19:15:08 +00:00
|
|
|
return '?';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-05-29 19:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we've figured the value out, see about reporting this option to
|
|
|
|
// our caller.
|
|
|
|
if (option->flag) {
|
|
|
|
*option->flag = option->val;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return option->val;
|
|
|
|
}
|
2019-09-06 18:03:16 +00:00
|
|
|
|
2020-05-29 19:15:08 +00:00
|
|
|
void OptionParser::shift_argv()
|
|
|
|
{
|
|
|
|
// We've just parsed an option (which perhaps has a value).
|
|
|
|
// Put the option (along with it value, if any) in front of other arguments.
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(optind <= static_cast<int>(m_arg_index));
|
2020-05-29 19:15:08 +00:00
|
|
|
|
|
|
|
if (optind == static_cast<int>(m_arg_index) || m_consumed_args == 0) {
|
|
|
|
// Nothing to do!
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-13 14:35:04 +00:00
|
|
|
auto new_argv = const_cast<char**>(m_argv);
|
2020-05-29 19:15:08 +00:00
|
|
|
char* buffer[m_consumed_args];
|
2021-06-13 14:35:04 +00:00
|
|
|
memcpy(buffer, &new_argv[m_arg_index], sizeof(char*) * m_consumed_args);
|
|
|
|
memmove(&new_argv[optind + m_consumed_args], &new_argv[optind], sizeof(char*) * (m_arg_index - optind));
|
|
|
|
memcpy(&new_argv[optind], buffer, sizeof(char*) * m_consumed_args);
|
2019-09-06 18:03:16 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 19:15:08 +00:00
|
|
|
bool OptionParser::find_next_option()
|
2019-09-06 18:03:16 +00:00
|
|
|
{
|
2020-05-29 19:15:08 +00:00
|
|
|
for (m_arg_index = optind; m_arg_index < m_argc && m_argv[m_arg_index]; m_arg_index++) {
|
2022-07-11 20:52:03 +00:00
|
|
|
StringView arg = current_arg();
|
2020-05-29 19:15:08 +00:00
|
|
|
// Anything that doesn't start with a "-" is not an option.
|
2022-11-19 04:39:51 +00:00
|
|
|
// As a special case, a single "-" is not an option either.
|
|
|
|
// (It's typically used by programs to refer to stdin).
|
|
|
|
if (!arg.starts_with('-') || arg == "-") {
|
2020-05-29 19:15:08 +00:00
|
|
|
if (m_stop_on_first_non_option)
|
|
|
|
return false;
|
|
|
|
continue;
|
|
|
|
}
|
2022-11-19 04:39:51 +00:00
|
|
|
|
2020-05-29 19:15:08 +00:00
|
|
|
// As another special case, a "--" is not an option either, and we stop
|
|
|
|
// looking for further options if we encounter it.
|
|
|
|
if (arg == "--")
|
|
|
|
return false;
|
|
|
|
// Otherwise, we have found an option!
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reached the end and still found no options.
|
|
|
|
return false;
|
|
|
|
}
|
2019-09-06 18:03:16 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
int getopt(int argc, char* const* argv, char const* short_options)
|
2019-09-06 18:03:16 +00:00
|
|
|
{
|
2020-05-29 19:15:08 +00:00
|
|
|
option dummy { nullptr, 0, nullptr, 0 };
|
2022-07-11 19:53:29 +00:00
|
|
|
OptionParser parser { argc, argv, { short_options, strlen(short_options) }, &dummy };
|
2020-05-29 19:15:08 +00:00
|
|
|
return parser.getopt();
|
|
|
|
}
|
2019-09-06 18:03:16 +00:00
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
int getopt_long(int argc, char* const* argv, char const* short_options, const struct option* long_options, int* out_long_option_index)
|
2020-05-29 19:15:08 +00:00
|
|
|
{
|
2022-07-11 19:53:29 +00:00
|
|
|
OptionParser parser { argc, argv, { short_options, strlen(short_options) }, long_options, out_long_option_index };
|
2020-05-29 19:15:08 +00:00
|
|
|
return parser.getopt();
|
2018-10-31 16:50:43 +00:00
|
|
|
}
|