From e8d8047fd93bf7feeb416e8e3a3f519f03026b63 Mon Sep 17 00:00:00 2001 From: "Enrico Weigelt, metux IT consult" Date: Wed, 11 Sep 2024 19:09:40 +0200 Subject: [PATCH] os: helper for parsing an counting-flag or value-flag option Parses an option that may either be used for setting an integer value or given one or multiple times (without argument) to increase an value Signed-off-by: Enrico Weigelt, metux IT consult --- os/cmdline.c | 32 ++++++++++++++++++++++++++++++++ os/cmdline.h | 15 +++++++++++++++ os/meson.build | 1 + 3 files changed, 48 insertions(+) create mode 100644 os/cmdline.c diff --git a/os/cmdline.c b/os/cmdline.c new file mode 100644 index 000000000..9e3034566 --- /dev/null +++ b/os/cmdline.c @@ -0,0 +1,32 @@ +/* SPDX-License-Identifier: MIT OR X11 + * + * Copyright © 2024 Enrico Weigelt, metux IT consult + * + * @brief command line helper functions + */ + +#include + +#include +#include + +#include "os/cmdline.h" + +int ProcessCmdLineMultiInt(int argc, char *argv[], int *idx, const char* name, int *value) +{ + if (strcmp(argv[*idx], name)) + return 0; + + int i2 = *idx+1; + if (i2 < argc && argv[i2]) { + char *end; + long val = strtol(argv[i2], &end, 0); + if (*end == '\0') { + (*idx)++; + (*value) = val; + return 1; + } + } + (*value)++; + return 1; +} diff --git a/os/cmdline.h b/os/cmdline.h index 9a4a19467..eba96746f 100644 --- a/os/cmdline.h +++ b/os/cmdline.h @@ -17,4 +17,19 @@ void UseMsg(void); void ProcessCommandLine(int argc, char * argv[]); void CheckUserParameters(int argc, char **argv, char **envp); +/* + * @brief check for and parse an counting-flag or value-flag option + * + * Parses an option that may either be used for setting an integer value or + * given one or multiple times (without argument) to increase an value + * + * @param argc total number of elements in argv + * @param argv array of pointers to cmdline argument strings + * @param idx pointer to current index in argv -- eventually will be modified + * @param name the command line argument name + * @param value pointer to the field holding the setting value + * @return non-zero if the flag was found and parsed + */ +int ProcessCmdLineMultiInt(int argc, char *argv[], int *idx, const char* name, int *value); + #endif /* _XSERVER_OS_CMELINE_H */ diff --git a/os/meson.build b/os/meson.build index 44acba6ba..8b1255349 100644 --- a/os/meson.build +++ b/os/meson.build @@ -5,6 +5,7 @@ srcs_os = [ 'auth.c', 'backtrace.c', 'client.c', + 'cmdline.c', 'connection.c', 'fmt.c', 'inputthread.c',