]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-master: Extend master-service API to register and parse long options
authorKarl Fleischmann <karl.fleischmann@open-xchange.com>
Thu, 22 Jun 2023 09:57:32 +0000 (11:57 +0200)
committerKarl Fleischmann <karl.fleischmann@open-xchange.com>
Mon, 10 Jul 2023 10:10:42 +0000 (12:10 +0200)
src/lib-master/master-service-private.h
src/lib-master/master-service.c
src/lib-master/master-service.h

index 9a3d59fe662b3c6363bf69560d8741a87fe330a8..e2ec19ad0b64615efddbe1b845187a9f4a39f9b7 100644 (file)
@@ -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;
index 4ef0645c03f571d0ad4d78a99786d9cded26f5b5..b88a965618f7b12da3ffa6faf109a0b41c3adf65 100644 (file)
@@ -27,6 +27,7 @@
 #include "master-service-settings.h"
 #include "iostream-ssl.h"
 
+#include <getopt.h>
 #include <unistd.h>
 #include <sys/stat.h>
 #include <syslog.h>
@@ -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;
index 4a10ce5747f5f8ca7b3d5f859532472d6bb1fa6a..ab9874eeb9ed6edcd7bd9764e915cc7c765dd20d 100644 (file)
@@ -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);