summaryrefslogtreecommitdiffstats
path: root/util/argconfig.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2023-01-31 04:13:00 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2023-01-31 04:13:00 +0000
commitdc3f3471f8a00ce0c8fb4cbf2a31e299696f3bbc (patch)
treee97b4f25c511372d73bdd96c389c5f468d99138a /util/argconfig.c
parentAdding upstream version 2.2.1. (diff)
downloadnvme-cli-upstream/2.3.tar.xz
nvme-cli-upstream/2.3.zip
Adding upstream version 2.3.upstream/2.3
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--util/argconfig.c50
1 files changed, 31 insertions, 19 deletions
diff --git a/util/argconfig.c b/util/argconfig.c
index 2c328ff..231a704 100644
--- a/util/argconfig.c
+++ b/util/argconfig.c
@@ -53,7 +53,7 @@ void argconfig_append_usage(const char *str)
append_usage_str = str;
}
-void print_word_wrapped(const char *s, int indent, int start)
+void print_word_wrapped(const char *s, int indent, int start, FILE *stream)
{
const int width = 76;
const char *c, *t;
@@ -61,7 +61,7 @@ void print_word_wrapped(const char *s, int indent, int start)
int last_line = indent;
while (start < indent) {
- putc(' ', stderr);
+ putc(' ', stream);
start++;
}
@@ -78,14 +78,14 @@ void print_word_wrapped(const char *s, int indent, int start)
int i;
new_line:
last_line = (int) (c-s) + start;
- putc('\n', stderr);
+ putc('\n', stream);
for (i = 0; i < indent; i++)
- putc(' ', stderr);
+ putc(' ', stream);
start = indent;
continue;
}
}
- putc(*c, stderr);
+ putc(*c, stream);
}
}
@@ -115,8 +115,8 @@ static void show_option(const struct argconfig_commandline_options *option)
fprintf(stderr, "%s", buffer);
if (option->help) {
- print_word_wrapped("--- ", 40, b - buffer);
- print_word_wrapped(option->help, 44, 44);
+ print_word_wrapped("--- ", 40, b - buffer, stderr);
+ print_word_wrapped(option->help, 44, 44, stderr);
}
fprintf(stderr, "\n");
}
@@ -126,20 +126,37 @@ void argconfig_print_help(const char *program_desc,
{
const struct argconfig_commandline_options *s;
- printf("\033[1mUsage: %s\033[0m\n\n",
- append_usage_str);
+ fprintf(stderr, "\033[1mUsage: %s\033[0m\n\n",
+ append_usage_str);
- print_word_wrapped(program_desc, 0, 0);
- printf("\n");
+ print_word_wrapped(program_desc, 0, 0, stderr);
+ fprintf(stderr, "\n");
if (!options || !options->option)
return;
- printf("\n\033[1mOptions:\033[0m\n");
+ fprintf(stderr, "\n\033[1mOptions:\033[0m\n");
for (s = options; (s != NULL) && (s->option != NULL); s++)
show_option(s);
}
+int argconfig_parse_byte(const char *opt, const char *str, unsigned char *val)
+{
+ char *endptr;
+ unsigned long tmp = strtoul(str, &endptr, 0);
+
+ if (errno || tmp >= 1 << 8 || str == endptr) {
+ fprintf(stderr,
+ "Expected byte argument for '%s' but got '%s'!\n", opt,
+ str);
+ return -EINVAL;
+ }
+
+ *val = tmp;
+
+ return 0;
+}
+
int argconfig_parse(int argc, char *argv[], const char *program_desc,
const struct argconfig_commandline_options *options)
{
@@ -244,14 +261,9 @@ int argconfig_parse(int argc, char *argv[], const char *program_desc,
}
*((int *)value_addr) = tmp;
} else if (s->config_type == CFG_BYTE) {
- unsigned long tmp = strtoul(optarg, &endptr, 0);
- if (errno || tmp >= (1 << 8) || optarg == endptr) {
- fprintf(stderr,
- "Expected byte argument for '%s' but got '%s'!\n",
- long_opts[option_index].name, optarg);
+ if (argconfig_parse_byte(long_opts[option_index].name,
+ optarg, (uint8_t *)value_addr))
goto out;
- }
- *((uint8_t *) value_addr) = tmp;
} else if (s->config_type == CFG_SHORT) {
unsigned long tmp = strtoul(optarg, &endptr, 0);
if (errno || tmp >= (1 << 16) || optarg == endptr) {