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 <info@metux.net>
This commit is contained in:
Enrico Weigelt, metux IT consult 2024-09-11 19:09:40 +02:00
parent 945edb0186
commit e8d8047fd9
3 changed files with 48 additions and 0 deletions

32
os/cmdline.c Normal file
View File

@ -0,0 +1,32 @@
/* SPDX-License-Identifier: MIT OR X11
*
* Copyright © 2024 Enrico Weigelt, metux IT consult <info@metux.net>
*
* @brief command line helper functions
*/
#include <dix-config.h>
#include <string.h>
#include <stdlib.h>
#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;
}

View File

@ -17,4 +17,19 @@ void UseMsg(void);
void ProcessCommandLine(int argc, char * argv[]); void ProcessCommandLine(int argc, char * argv[]);
void CheckUserParameters(int argc, char **argv, char **envp); 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 */ #endif /* _XSERVER_OS_CMELINE_H */

View File

@ -5,6 +5,7 @@ srcs_os = [
'auth.c', 'auth.c',
'backtrace.c', 'backtrace.c',
'client.c', 'client.c',
'cmdline.c',
'connection.c', 'connection.c',
'fmt.c', 'fmt.c',
'inputthread.c', 'inputthread.c',