Replace getopt/ya_getopt with Lyra.

This commit is contained in:
Sadie Powell 2022-04-19 11:37:37 +01:00
parent 0e7883dd96
commit 0667469832
8 changed files with 4507 additions and 453 deletions

View File

@ -36,6 +36,15 @@
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#ifdef __GNUC__
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wshadow"
#endif
#include <lyra/lyra.hpp>
#ifdef __GNUC__
# pragma GCC diagnostic pop
#endif
#include "inspircd.h" #include "inspircd.h"
#include "exitcodes.h" #include "exitcodes.h"
#include "xline.h" #include "xline.h"
@ -43,10 +52,7 @@
// Needs to be included after inspircd.h to avoid reincluding winsock. // Needs to be included after inspircd.h to avoid reincluding winsock.
#include <rang/rang.hpp> #include <rang/rang.hpp>
#ifdef _WIN32 #ifndef _WIN32
# include <ya_getopt/ya_getopt.h>
#else
# include <getopt.h>
# include <grp.h> # include <grp.h>
# include <pwd.h> # include <pwd.h>
# include <sys/resource.h> # include <sys/resource.h>
@ -275,42 +281,52 @@ namespace
// Parses the command line options. // Parses the command line options.
void ParseOptions() void ParseOptions()
{ {
int do_debug = 0, do_nofork = 0, do_nolog = 0; std::string config;
int do_nopid = 0, do_runasroot = 0, do_version = 0; bool do_debug = false;
struct option longopts[] = bool do_help = false;
bool do_nofork = false;
bool do_nolog = false;
bool do_nopid = false;
bool do_runasroot = false;
bool do_version = false;
auto cli = lyra::cli()
| lyra::opt(config, "FILE")
["-c"]["--config"]
("The location of the main config file.")
| lyra::opt(do_debug)
["-d"]["--debug"]
("Start in debug mode.")
| lyra::opt(do_nofork)
["-F"]["--nofork"]
("Disable forking into the background.")
| lyra::opt(do_help)
["-h"]["--help"]
("Show help and exit.")
| lyra::opt(do_nolog)
["-L"]["--nolog"]
("Disable writing logs to disk.")
| lyra::opt(do_nopid)
["-P"]["--nopid"]
("Disable writing the pid file.")
| lyra::opt(do_runasroot)
["-r"]["--runasroot"]
("Allow starting as root (not recommended).")
| lyra::opt(do_version)
["-v"]["--version"]
("Show version and exit.");
auto result = cli.parse({ServerInstance->Config->cmdline.argc, ServerInstance->Config->cmdline.argv});
if (!result)
{ {
{ "config", required_argument, NULL, 'c' }, std::cerr << rang::style::bold << rang::fg::red << "Error: " << rang::style::reset << result.message() << '.' << std::endl;
{ "debug", no_argument, &do_debug, 1 }, ServerInstance->Exit(EXIT_STATUS_ARGV);
{ "nofork", no_argument, &do_nofork, 1 }, }
{ "nolog", no_argument, &do_nolog, 1 },
{ "nopid", no_argument, &do_nopid, 1 },
{ "runasroot", no_argument, &do_runasroot, 1 },
{ "version", no_argument, &do_version, 1 },
{ 0, 0, 0, 0 }
};
char** argv = ServerInstance->Config->cmdline.argv; if (do_help)
int ret;
while ((ret = getopt_long(ServerInstance->Config->cmdline.argc, argv, ":c:", longopts, NULL)) != -1)
{ {
switch (ret) std::cout << cli << std::endl;
{ ServerInstance->Exit(EXIT_STATUS_NOERROR);
case 0:
// A long option was specified.
break;
case 'c':
// The -c option was specified.
ServerInstance->ConfigFileName = ExpandPath(optarg);
break;
default:
// An unknown option was specified.
std::cout << rang::style::bold << rang::fg::red << "Error:" << rang::style::reset << " unknown option '" << argv[optind-1] << "'." << std::endl
<< rang::style::bold << "Usage: " << rang::style::reset << argv[0] << " [--config <file>] [--debug] [--nofork] [--nolog]" << std::endl
<< std::string(strlen(argv[0]) + 8, ' ') << "[--nopid] [--runasroot] [--version]" << std::endl;
ServerInstance->Exit(EXIT_STATUS_ARGV);
}
} }
if (do_version) if (do_version)
@ -320,9 +336,11 @@ namespace
} }
// Store the relevant parsed arguments // Store the relevant parsed arguments
ServerInstance->Config->cmdline.forcedebug = !!do_debug; if (!config.empty())
ServerInstance->Config->cmdline.nofork = !!do_nofork; ServerInstance->ConfigFileName = ExpandPath(config.c_str());
ServerInstance->Config->cmdline.runasroot = !!do_runasroot; ServerInstance->Config->cmdline.forcedebug = do_debug;
ServerInstance->Config->cmdline.nofork = do_nofork;
ServerInstance->Config->cmdline.runasroot = do_runasroot;
ServerInstance->Config->cmdline.writelog = !do_nolog; ServerInstance->Config->cmdline.writelog = !do_nolog;
ServerInstance->Config->cmdline.writepid = !do_nopid; ServerInstance->Config->cmdline.writepid = !do_nopid;
} }

20
vendor/README.md vendored
View File

@ -22,6 +22,16 @@ This directory contains vendored dependencies that are shipped with InspIRCd to
**Website** &mdash; [https://github.com/nodejs/http-parser](https://github.com/nodejs/http-parser) **Website** &mdash; [https://github.com/nodejs/http-parser](https://github.com/nodejs/http-parser)
## lyra
**Author** &mdash; Build Frameworks Group
**License** &mdash; Boost Software License
**Version** &mdash; 1.6
**Website** &mdash; [https://github.com/bfgroup/Lyra](https://github.com/bfgroup/Lyra)
## md5 ## md5
**Author** &mdash; [Solar Designer](mailto:solar@openwall.com) **Author** &mdash; [Solar Designer](mailto:solar@openwall.com)
@ -61,13 +71,3 @@ This directory contains vendored dependencies that are shipped with InspIRCd to
**Version** &mdash; v3.2.1 **Version** &mdash; v3.2.1
**Website** &mdash; [https://github.com/nemtrif/utfcpp](https://github.com/nemtrif/utfcpp) **Website** &mdash; [https://github.com/nemtrif/utfcpp](https://github.com/nemtrif/utfcpp)
## ya_getopt
**Author** &mdash; Kubo Takehiro
**License** &mdash; 2-clause BSD License
**Version** &mdash; 6ce4310
**Website** &mdash; [https://github.com/kubo/ya_getopt](https://github.com/kubo/ya_getopt)

23
vendor/lyra/LICENSE.txt vendored Normal file
View File

@ -0,0 +1,23 @@
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

4409
vendor/lyra/lyra.hpp vendored Normal file

File diff suppressed because it is too large Load Diff

13
vendor/update.toml vendored
View File

@ -13,6 +13,13 @@ files = "{http_parser.[ch],LICENSE-MIT}"
git = "https://github.com/nodejs/http-parser" git = "https://github.com/nodejs/http-parser"
license = "MIT License" license = "MIT License"
[lyra]
author = "Build Frameworks Group"
depth = 3
files = "{data/single_include/lyra/lyra.hpp,LICENSE.txt}"
git = "https://github.com/bfgroup/Lyra"
license = "Boost Software License"
[md5] [md5]
author = "Solar Designer" author = "Solar Designer"
depth = 1 depth = 1
@ -45,9 +52,3 @@ depth = 2
files = "{LICENSE,source/utf8/{core,unchecked}.h}" files = "{LICENSE,source/utf8/{core,unchecked}.h}"
git = "https://github.com/nemtrif/utfcpp" git = "https://github.com/nemtrif/utfcpp"
license = "Boost Software License" license = "Boost Software License"
[ya_getopt]
author = "Kubo Takehiro"
files = "ya_getopt.[ch]"
git = "https://github.com/kubo/ya_getopt"
license = "2-clause BSD License"

View File

@ -1,318 +0,0 @@
/* -*- indent-tabs-mode: nil -*-
*
* ya_getopt - Yet another getopt
* https://github.com/kubo/ya_getopt
*
* Copyright 2015 Kubo Takehiro <kubo@jiubao.org>
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of the authors.
*
*/
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "ya_getopt.h"
char *ya_optarg = NULL;
int ya_optind = 1;
int ya_opterr = 1;
int ya_optopt = '?';
static char *ya_optnext = NULL;
static int posixly_correct = -1;
static int handle_nonopt_argv = 0;
static void ya_getopt_error(const char *optstring, const char *format, ...);
static void check_gnu_extension(const char *optstring);
static int ya_getopt_internal(int argc, char * const argv[], const char *optstring, const struct option *longopts, int *longindex, int long_only);
static int ya_getopt_shortopts(int argc, char * const argv[], const char *optstring, int long_only);
static int ya_getopt_longopts(int argc, char * const argv[], char *arg, const char *optstring, const struct option *longopts, int *longindex, int *long_only_flag);
static void ya_getopt_error(const char *optstring, const char *format, ...)
{
if (ya_opterr && optstring[0] != ':') {
va_list ap;
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
}
}
static void check_gnu_extension(const char *optstring)
{
if (optstring[0] == '+' || getenv("POSIXLY_CORRECT") != NULL) {
posixly_correct = 1;
} else {
posixly_correct = 0;
}
if (optstring[0] == '-') {
handle_nonopt_argv = 1;
} else {
handle_nonopt_argv = 0;
}
}
static int is_option(const char *arg)
{
return arg[0] == '-' && arg[1] != '\0';
}
int ya_getopt(int argc, char * const argv[], const char *optstring)
{
return ya_getopt_internal(argc, argv, optstring, NULL, NULL, 0);
}
int ya_getopt_long(int argc, char * const argv[], const char *optstring, const struct option *longopts, int *longindex)
{
return ya_getopt_internal(argc, argv, optstring, longopts, longindex, 0);
}
int ya_getopt_long_only(int argc, char * const argv[], const char *optstring, const struct option *longopts, int *longindex)
{
return ya_getopt_internal(argc, argv, optstring, longopts, longindex, 1);
}
static int ya_getopt_internal(int argc, char * const argv[], const char *optstring, const struct option *longopts, int *longindex, int long_only)
{
static int start, end;
if (ya_optopt == '?') {
ya_optopt = 0;
}
if (posixly_correct == -1) {
check_gnu_extension(optstring);
}
if (ya_optind == 0) {
check_gnu_extension(optstring);
ya_optind = 1;
ya_optnext = NULL;
}
switch (optstring[0]) {
case '+':
case '-':
optstring++;
}
if (ya_optnext == NULL && start != 0) {
int last_pos = ya_optind - 1;
ya_optind -= end - start;
if (ya_optind <= 0) {
ya_optind = 1;
}
while (start < end--) {
int i;
char *arg = argv[end];
for (i = end; i < last_pos; i++) {
((char **)argv)[i] = argv[i + 1];
}
((char const **)argv)[i] = arg;
last_pos--;
}
start = 0;
}
if (ya_optind >= argc) {
ya_optarg = NULL;
return -1;
}
if (ya_optnext == NULL) {
const char *arg = argv[ya_optind];
if (!is_option(arg)) {
if (handle_nonopt_argv) {
ya_optarg = argv[ya_optind++];
start = 0;
return 1;
} else if (posixly_correct) {
ya_optarg = NULL;
return -1;
} else {
int i;
start = ya_optind;
for (i = ya_optind + 1; i < argc; i++) {
if (is_option(argv[i])) {
end = i;
break;
}
}
if (i == argc) {
ya_optarg = NULL;
return -1;
}
ya_optind = i;
arg = argv[ya_optind];
}
}
if (strcmp(arg, "--") == 0) {
ya_optind++;
return -1;
}
if (longopts != NULL && arg[1] == '-') {
return ya_getopt_longopts(argc, argv, argv[ya_optind] + 2, optstring, longopts, longindex, NULL);
}
}
if (ya_optnext == NULL) {
ya_optnext = argv[ya_optind] + 1;
}
if (long_only) {
int long_only_flag = 0;
int rv = ya_getopt_longopts(argc, argv, ya_optnext, optstring, longopts, longindex, &long_only_flag);
if (!long_only_flag) {
ya_optnext = NULL;
return rv;
}
}
return ya_getopt_shortopts(argc, argv, optstring, long_only);
}
static int ya_getopt_shortopts(int argc, char * const argv[], const char *optstring, int long_only)
{
int opt = *ya_optnext;
const char *os = strchr(optstring, opt);
if (os == NULL) {
ya_optarg = NULL;
if (long_only) {
ya_getopt_error(optstring, "%s: unrecognized option '-%s'\n", argv[0], ya_optnext);
ya_optind++;
ya_optnext = NULL;
} else {
ya_optopt = opt;
ya_getopt_error(optstring, "%s: invalid option -- '%c'\n", argv[0], opt);
if (*(++ya_optnext) == 0) {
ya_optind++;
ya_optnext = NULL;
}
}
return '?';
}
if (os[1] == ':') {
if (ya_optnext[1] == 0) {
ya_optind++;
ya_optnext = NULL;
if (os[2] == ':') {
/* optional argument */
ya_optarg = NULL;
} else {
if (ya_optind == argc) {
ya_optarg = NULL;
ya_optopt = opt;
ya_getopt_error(optstring, "%s: option requires an argument -- '%c'\n", argv[0], opt);
if (optstring[0] == ':') {
return ':';
} else {
return '?';
}
}
ya_optarg = argv[ya_optind];
ya_optind++;
}
} else {
ya_optarg = ya_optnext + 1;
ya_optind++;
}
ya_optnext = NULL;
} else {
ya_optarg = NULL;
if (ya_optnext[1] == 0) {
ya_optnext = NULL;
ya_optind++;
} else {
ya_optnext++;
}
}
return opt;
}
static int ya_getopt_longopts(int argc, char * const argv[], char *arg, const char *optstring, const struct option *longopts, int *longindex, int *long_only_flag)
{
char *val = NULL;
const struct option *opt;
size_t namelen;
int idx;
for (idx = 0; longopts[idx].name != NULL; idx++) {
opt = &longopts[idx];
namelen = strlen(opt->name);
if (strncmp(arg, opt->name, namelen) == 0) {
switch (arg[namelen]) {
case '\0':
switch (opt->has_arg) {
case ya_required_argument:
ya_optind++;
if (ya_optind == argc) {
ya_optarg = NULL;
ya_optopt = opt->val;
ya_getopt_error(optstring, "%s: option '--%s' requires an argument\n", argv[0], opt->name);
if (optstring[0] == ':') {
return ':';
} else {
return '?';
}
}
val = argv[ya_optind];
break;
}
goto found;
case '=':
if (opt->has_arg == ya_no_argument) {
const char *hyphens = (argv[ya_optind][1] == '-') ? "--" : "-";
ya_optind++;
ya_optarg = NULL;
ya_optopt = opt->val;
ya_getopt_error(optstring, "%s: option '%s%s' doesn't allow an argument\n", argv[0], hyphens, opt->name);
return '?';
}
val = arg + namelen + 1;
goto found;
}
}
}
if (long_only_flag) {
*long_only_flag = 1;
} else {
ya_getopt_error(optstring, "%s: unrecognized option '%s'\n", argv[0], argv[ya_optind]);
ya_optind++;
}
return '?';
found:
ya_optarg = val;
ya_optind++;
if (opt->flag) {
*opt->flag = opt->val;
}
if (longindex) {
*longindex = idx;
}
return opt->flag ? 0 : opt->val;
}

View File

@ -1,77 +0,0 @@
/* -*- indent-tabs-mode: nil -*-
*
* ya_getopt - Yet another getopt
* https://github.com/kubo/ya_getopt
*
* Copyright 2015 Kubo Takehiro <kubo@jiubao.org>
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of the authors.
*
*/
#ifndef YA_GETOPT_H
#define YA_GETOPT_H 1
#if defined(__cplusplus)
extern "C" {
#endif
#define ya_no_argument 0
#define ya_required_argument 1
#define ya_optional_argument 2
struct option {
const char *name;
int has_arg;
int *flag;
int val;
};
int ya_getopt(int argc, char * const argv[], const char *optstring);
int ya_getopt_long(int argc, char * const argv[], const char *optstring,
const struct option *longopts, int *longindex);
int ya_getopt_long_only(int argc, char * const argv[], const char *optstring,
const struct option *longopts, int *longindex);
extern char *ya_optarg;
extern int ya_optind, ya_opterr, ya_optopt;
#ifndef YA_GETOPT_NO_COMPAT_MACRO
#define getopt ya_getopt
#define getopt_long ya_getopt_long
#define getopt_long_only ya_getopt_long_only
#define optarg ya_optarg
#define optind ya_optind
#define opterr ya_opterr
#define optopt ya_optopt
#define no_argument ya_no_argument
#define required_argument ya_required_argument
#define optional_argument ya_optional_argument
#endif
#if defined(__cplusplus)
}
#endif
#endif

View File

@ -27,8 +27,6 @@
#include "inspircd_win32wrapper.h" #include "inspircd_win32wrapper.h"
#include "inspircd.h" #include "inspircd.h"
#include <ya_getopt/ya_getopt.c>
CWin32Exception::CWin32Exception() : exception() CWin32Exception::CWin32Exception() : exception()
{ {
dwErrorCode = GetLastError(); dwErrorCode = GetLastError();