From a327f4c94cdc44c208ec10a735f5d212a302384d Mon Sep 17 00:00:00 2001 From: Karl Fleischmann Date: Thu, 22 Jun 2023 11:57:32 +0200 Subject: [PATCH] lib-master: Extend master-service API to register and parse long options --- src/lib-master/master-service-private.h | 1 + src/lib-master/master-service.c | 29 +++++++++++++++++++++++++ src/lib-master/master-service.h | 6 +++++ 3 files changed, 36 insertions(+) diff --git a/src/lib-master/master-service-private.h b/src/lib-master/master-service-private.h index 9a3d59fe66..e2ec19ad0b 100644 --- a/src/lib-master/master-service-private.h +++ b/src/lib-master/master-service-private.h @@ -30,6 +30,7 @@ struct master_service { char *name; char *configured_name; char *getopt_str; + const struct option *longopts; enum master_service_flags flags; int argc; diff --git a/src/lib-master/master-service.c b/src/lib-master/master-service.c index 4ef0645c03..b88a965618 100644 --- a/src/lib-master/master-service.c +++ b/src/lib-master/master-service.c @@ -27,6 +27,7 @@ #include "master-service-settings.h" #include "iostream-ssl.h" +#include #include #include #include @@ -627,6 +628,13 @@ master_service_init(const char *name, enum master_service_flags flags, return service; } +void +master_service_register_long_options(struct master_service *service, + const struct option *longopts) +{ + service->longopts = longopts; +} + int master_getopt(struct master_service *service) { int c; @@ -641,6 +649,27 @@ int master_getopt(struct master_service *service) return c; } +int +master_getopt_long(struct master_service *service, const char **longopt_r) +{ + if (service->longopts == NULL) + return master_getopt(service); + + i_assert(master_getopt_str_is_valid(service->getopt_str)); + + int c; + int longopt_idx = -1; + while ((c = getopt_long(service->argc, service->argv, + service->getopt_str, service->longopts, + &longopt_idx)) > 0) { + if (!master_service_parse_option(service, c, optarg)) + break; + } + if (longopt_idx >= 0) + *longopt_r = service->longopts[longopt_idx].name; + return c; +} + bool master_getopt_str_is_valid(const char *str) { unsigned int i, j; diff --git a/src/lib-master/master-service.h b/src/lib-master/master-service.h index 4a10ce5747..ab9874eeb9 100644 --- a/src/lib-master/master-service.h +++ b/src/lib-master/master-service.h @@ -116,9 +116,15 @@ const char *master_service_getopt_string(void); struct master_service * master_service_init(const char *name, enum master_service_flags flags, int *argc, char **argv[], const char *getopt_str); +void master_service_register_long_options(struct master_service *service, + const struct option *longopts); /* Call getopt() and handle internal parameters. Return values are the same as getopt()'s. */ int master_getopt(struct master_service *service); +/* Call getopt_long() and handle internal parameters. Return values are the + same as getopt_long()'s. Additionally if a long option was encountered, its + name is written to longopt_r. */ +int master_getopt_long(struct master_service *service, const char **longopt_r); /* Returns TRUE if str is a valid getopt_str. Currently this only checks for duplicate args so they aren't accidentally added. */ bool master_getopt_str_is_valid(const char *str); -- 2.47.3