aboutsummaryrefslogtreecommitdiff
path: root/src/supplemental/util/options.c
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2025-01-04 17:57:28 -0800
committerGarrett D'Amore <garrett@damore.org>2025-01-04 18:00:09 -0800
commitf77e5a5ec7f8b1373eeda0ea56f47137daf40330 (patch)
tree9f2f10860a70765924dba9231bf3e2e4650f8b82 /src/supplemental/util/options.c
parenta40b421d39a64c285a4ac23304538690aaa90739 (diff)
downloadnng-f77e5a5ec7f8b1373eeda0ea56f47137daf40330.tar.gz
nng-f77e5a5ec7f8b1373eeda0ea56f47137daf40330.tar.bz2
nng-f77e5a5ec7f8b1373eeda0ea56f47137daf40330.zip
args: Convert nng_opts_parse into a header only library using `nng_args_parse`.
The API is identical, except that some names have changed, and this is now a header library in `nng/args.h` - so the core library does not need to carry this code in binaries. Being a header library also means it is not necessary to link against NNG, and it does not include any parts of NNG; it only depends on a standard C99 or C11 environment.
Diffstat (limited to 'src/supplemental/util/options.c')
-rw-r--r--src/supplemental/util/options.c127
1 files changed, 0 insertions, 127 deletions
diff --git a/src/supplemental/util/options.c b/src/supplemental/util/options.c
deleted file mode 100644
index 961e0bb2..00000000
--- a/src/supplemental/util/options.c
+++ /dev/null
@@ -1,127 +0,0 @@
-//
-// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech>
-// Copyright 2018 Capitar IT Group BV <info@capitar.com>
-//
-// This software is supplied under the terms of the MIT License, a
-// copy of which should be located in the distribution where this
-// file was obtained (LICENSE.txt). A copy of the license may also be
-// found online at https://opensource.org/licenses/MIT.
-//
-
-#include <stdlib.h>
-#include <string.h>
-
-#include <nng/nng.h>
-#include <nng/supplemental/util/options.h>
-
-#include "core/nng_impl.h"
-
-// Call with optidx set to 1 to start parsing.
-int
-nng_opts_parse(int argc, char *const *argv, const nng_optspec *opts, int *val,
- char **optarg, int *optidx)
-{
- const nng_optspec *opt;
- int matches;
- bool shortopt;
- size_t l;
- char * arg;
- int i;
-
- if ((i = *optidx) >= argc) {
- return (-1);
- }
- arg = argv[*optidx];
-
- if (arg[0] != '-') {
- return (-1);
- }
- if (arg[1] == '\0') {
- *optidx = i + 1;
- return (-1);
- }
-
- if ((arg[0] == '-') && (arg[1] == '-')) {
- arg += 2;
- shortopt = false;
- for (l = 0; arg[l] != '\0'; l++) {
- if ((arg[l] == '=') || (arg[l] == ':')) {
- break;
- }
- }
- } else {
- arg++;
- shortopt = true;
- l = 1;
- }
-
- matches = 0;
- opt = NULL;
-
- for (int x = 0; opts[x].o_val != 0; x++) {
-
- if (shortopt) {
- if (arg[0] == opts[x].o_short) {
- matches = 1;
- opt = &opts[x];
- break;
- }
- continue;
- }
-
- if ((opts[x].o_name == NULL) ||
- (strncmp(arg, opts[x].o_name, l) != 0)) {
- continue;
- }
- matches++;
- opt = &opts[x];
-
- if (strlen(opts[x].o_name) == l) {
- // Perfect match.
- matches = 1;
- break;
- }
- }
-
- switch (matches) {
- case 1:
- // Exact match
- break;
- case 0:
- // No such option
- return (NNG_EINVAL);
- break;
- default:
- // Ambiguous (not match)
- return (NNG_EAMBIGUOUS);
- break;
- }
-
- if (!opt->o_arg) {
- // No option clustering for short options yet.
- if (arg[l] != '\0') {
- return (NNG_EINVAL);
- }
- *val = opt->o_val;
- *optidx = i + 1;
- return (0);
- }
-
- if (arg[l] != '\0') {
- if (shortopt) {
- *optarg = arg + l;
- } else {
- *optarg = arg + l + 1;
- }
- } else {
- i++;
- if (i >= argc) {
- return (NNG_ENOARG);
- }
- *optarg = argv[i];
- }
- *optidx = ++i;
- *val = opt->o_val;
-
- return (0);
-}