summaryrefslogtreecommitdiffstats
path: root/util/argconfig.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2023-12-24 07:57:49 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2023-12-24 07:57:49 +0000
commitfc67d3ad9a2903cc33e5cdaedaad51dd86a42236 (patch)
tree4ef72536a965fe220e71aea78b76b6bbbac2ed59 /util/argconfig.c
parentAdding upstream version 2.5. (diff)
downloadnvme-cli-upstream/2.7.1.tar.xz
nvme-cli-upstream/2.7.1.zip
Adding upstream version 2.7.1.upstream/2.7.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--util/argconfig.c96
1 files changed, 62 insertions, 34 deletions
diff --git a/util/argconfig.c b/util/argconfig.c
index effeea2..5ec3d6f 100644
--- a/util/argconfig.c
+++ b/util/argconfig.c
@@ -41,6 +41,7 @@
#include <stdarg.h>
#include <string.h>
#include <stdbool.h>
+#include <locale.h>
static const char *append_usage_str = "";
@@ -163,6 +164,8 @@ static int argconfig_parse_type(struct argconfig_commandline_options *s, struct
char *endptr;
int ret = 0;
+ errno = 0; /* To distinguish success/failure after strtol/stroul call */
+
switch (s->config_type) {
case CFG_STRING:
*((char **)value) = optarg;
@@ -177,15 +180,6 @@ static int argconfig_parse_type(struct argconfig_commandline_options *s, struct
if (errno || optarg == endptr)
ret = argconfig_error("integer", option[index].name, optarg);
break;
- case CFG_BOOL: {
- int tmp = strtol(optarg, &endptr, 0);
-
- if (errno || tmp < 0 || tmp > 1 || optarg == endptr)
- ret = argconfig_error("0 or 1", option[index].name, optarg);
- else
- *((int *)value) = tmp;
- break;
- }
case CFG_BYTE:
ret = argconfig_parse_byte(option[index].name, optarg, (uint8_t *)value);
break;
@@ -313,23 +307,11 @@ static int argconfig_parse_val(struct argconfig_commandline_options *s, struct o
return argconfig_parse_type(s, option, index);
}
-bool argconfig_output_format_json(bool set)
-{
- static bool output_format_json;
-
- if (set)
- output_format_json = true;
-
- return output_format_json;
-}
-
-static bool argconfig_check_output_format_json(struct argconfig_commandline_options *s)
+static bool argconfig_check_human_readable(struct argconfig_commandline_options *s)
{
for (; s && s->option; s++) {
- if (strcmp(s->option, "output-format") || s->config_type != CFG_STRING)
- continue;
- if (!strcmp(*(char **)s->default_value, "json"))
- return true;
+ if (!strcmp(s->option, "human-readable") && s->config_type == CFG_FLAG)
+ return s->seen;
}
return false;
@@ -375,14 +357,10 @@ int argconfig_parse(int argc, char *argv[], const char *program_desc,
}
long_opts[option_index].name = "help";
- long_opts[option_index++].val = 'h';
-
- long_opts[option_index].name = "json";
- long_opts[option_index].val = 'j';
+ long_opts[option_index].val = 'h';
short_opts[short_index++] = '?';
- short_opts[short_index++] = 'h';
- short_opts[short_index] = 'j';
+ short_opts[short_index] = 'h';
optind = 0;
while ((c = getopt_long_only(argc, argv, short_opts, long_opts, &option_index)) != -1) {
@@ -392,8 +370,6 @@ int argconfig_parse(int argc, char *argv[], const char *program_desc,
ret = -EINVAL;
break;
}
- if (c == 'j')
- argconfig_output_format_json(true);
for (option_index = 0; option_index < options_count; option_index++) {
if (c == options[option_index].short_option)
break;
@@ -416,8 +392,8 @@ int argconfig_parse(int argc, char *argv[], const char *program_desc,
break;
}
- if (argconfig_check_output_format_json(options))
- argconfig_output_format_json(true);
+ if (!argconfig_check_human_readable(options))
+ setlocale(LC_ALL, "C");
out:
free(short_opts);
@@ -549,6 +525,58 @@ int argconfig_parse_comma_sep_array_long(char *string, unsigned long long *val,
}
}
+#define DEFINE_ARGCONFIG_PARSE_COMMA_SEP_ARRAY_UINT_FUNC(size) \
+int argconfig_parse_comma_sep_array_u##size(char *string, \
+ __u##size *val, \
+ unsigned int max_length) \
+{ \
+ int ret = 0; \
+ uintmax_t v; \
+ char *tmp; \
+ char *p; \
+ \
+ if (!string || !strlen(string)) \
+ return 0; \
+ \
+ tmp = strtok(string, ","); \
+ if (!tmp) \
+ return 0; \
+ \
+ v = strtoumax(tmp, &p, 0); \
+ if (*p != 0) \
+ return -1; \
+ if (v > UINT##size##_MAX) { \
+ fprintf(stderr, "%s out of range\n", tmp); \
+ return -1; \
+ } \
+ val[ret] = v; \
+ \
+ ret++; \
+ while (1) { \
+ tmp = strtok(NULL, ","); \
+ \
+ if (tmp == NULL) \
+ return ret; \
+ \
+ if (ret >= max_length) \
+ return -1; \
+ \
+ v = strtoumax(tmp, &p, 0); \
+ if (*p != 0) \
+ return -1; \
+ if (v > UINT##size##_MAX) { \
+ fprintf(stderr, "%s out of range\n", tmp); \
+ return -1; \
+ } \
+ val[ret] = v; \
+ ret++; \
+ } \
+}
+
+DEFINE_ARGCONFIG_PARSE_COMMA_SEP_ARRAY_UINT_FUNC(16);
+DEFINE_ARGCONFIG_PARSE_COMMA_SEP_ARRAY_UINT_FUNC(32);
+DEFINE_ARGCONFIG_PARSE_COMMA_SEP_ARRAY_UINT_FUNC(64);
+
bool argconfig_parse_seen(struct argconfig_commandline_options *s,
const char *option)
{