Changeset: 453fcb7a3a7f for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/453fcb7a3a7f
Modified Files:
        tools/mserver/mserver5.c
Branch: default
Log Message:

Make better use of the possibilities of getopt_long().


diffs (truncated from 581 to 300 lines):

diff --git a/tools/mserver/mserver5.c b/tools/mserver/mserver5.c
--- a/tools/mserver/mserver5.c
+++ b/tools/mserver/mserver5.c
@@ -330,44 +330,84 @@ wmain(int argc, wchar_t **argv)
        bool inmemory = false;
        bool readpwdxit = false;
        static const char in_memory[] = "in-memory";
-       static const struct option long_options[] = {
-               {"config", required_argument, NULL, 'c'},
-               {"dbextra", required_argument, NULL, 0},
-               {"dbtemp", required_argument, NULL, 0},
-               {"dbpath", required_argument, NULL, 0},
-               {"dbtrace", required_argument, NULL, 0},
-               {"debug", optional_argument, NULL, 'd'},
-               {"help", no_argument, NULL, '?'},
-               {"in-memory", no_argument, NULL, 0},
-               {"readonly", no_argument, NULL, 'r'},
-               {"set", required_argument, NULL, 's'},
-               {"single-user", no_argument, NULL, 0},
-               {"version", no_argument, NULL, 0},
-
-               {"logging", required_argument, NULL, 0},
+       enum options {
+               /* options that correspond to short options */
+               opt_CONFIG = 'c',
+               opt_DEBUG = 'd',
+               opt_READONLY = 'r',
+               opt_SET = 's',
 
-               {"algorithms", no_argument, NULL, 0},
-               {"forcemito", no_argument, NULL, 0},
-               {"heaps", no_argument, NULL, 0},
-               {"io", no_argument, NULL, 0},
-               {"memory", no_argument, NULL, 0},
-               {"modules", no_argument, NULL, 0},
-               {"performance", no_argument, NULL, 0},
-               {"properties", no_argument, NULL, 0},
-               {"threads", no_argument, NULL, 0},
-               {"transactions", no_argument, NULL, 0},
+               /* options only available as long options */
+               opt_DBEXTRA = 1000,
+               opt_DBTEMP,
+               opt_DBPATH,
+               opt_DBTRACE,
+               opt_IN_MEMORY,
+               opt_SINGLE_USER,
+               opt_LOGGING,
+               opt_ALGORITHMS,
+               opt_FORCEMITO,
+               opt_HEAPS,
+               opt_IO,
+               opt_MEMORY,
+               opt_MODULES,
+               opt_PERFORMANCE,
+               opt_PROPERTIES,
+               opt_THREADS,
+               opt_TRANSACTIONS,
+               opt_LOADMODULE,
+               opt_WITHOUT_GEOM,
+               opt_PIPELINE,
+               opt_READ_PASSWORD_INITIALIZE_AND_EXIT,
+               opt_PROCESS_WAL_AND_EXIT,
+               opt_CLEAN_BBP,
+               opt_ACCEPT_THE_RISKS_RUNNING_AS_ROOT,
+               opt_VERSION,
+               opt_HELP,
+       };
+       static const struct option long_options[] = {
+               /* last field uniquely identifies option
+                * if letter, option is equivalent to short form option */
+               {"config", required_argument, NULL, opt_CONFIG},
+               {"dbextra", required_argument, NULL, opt_DBEXTRA},
+               {"dbtemp", required_argument, NULL, opt_DBTEMP},
+               {"dbpath", required_argument, NULL, opt_DBPATH},
+               {"dbtrace", required_argument, NULL, opt_DBTRACE},
+               {"debug", optional_argument, NULL, opt_DEBUG},
+               {"in-memory", no_argument, NULL, opt_IN_MEMORY},
+               {"readonly", no_argument, NULL, opt_READONLY},
+               {"set", required_argument, NULL, opt_SET},
+               {"single-user", no_argument, NULL, opt_SINGLE_USER},
 
-               {"loadmodule", required_argument, NULL, 0},
-               {"without-geom", no_argument, NULL, 0},
-               {"pipeline", no_argument, NULL, 0},
+               {"logging", required_argument, NULL, opt_LOGGING},
 
-               {"read-password-initialize-and-exit", no_argument, NULL, 0},
-               {"process-wal-and-exit", no_argument, NULL, 0},
-               {"clean-BBP", no_argument, NULL, 0},
+               {"algorithms", no_argument, NULL, opt_ALGORITHMS},
+               {"forcemito", no_argument, NULL, opt_FORCEMITO},
+               {"heaps", no_argument, NULL, opt_HEAPS},
+               {"io", no_argument, NULL, opt_IO},
+               {"memory", no_argument, NULL, opt_MEMORY},
+               {"modules", no_argument, NULL, opt_MODULES},
+               {"performance", no_argument, NULL, opt_PERFORMANCE},
+               {"properties", no_argument, NULL, opt_PROPERTIES},
+               {"threads", no_argument, NULL, opt_THREADS},
+               {"transactions", no_argument, NULL, opt_TRANSACTIONS},
+
+               {"loadmodule", required_argument, NULL, opt_LOADMODULE},
+               {"without-geom", no_argument, NULL, opt_WITHOUT_GEOM},
+               {"pipeline", no_argument, NULL, opt_PIPELINE},
+
+               {"read-password-initialize-and-exit", no_argument, NULL,
+                opt_READ_PASSWORD_INITIALIZE_AND_EXIT},
+               {"process-wal-and-exit", no_argument, NULL,
+                opt_PROCESS_WAL_AND_EXIT},
+               {"clean-BBP", no_argument, NULL, opt_CLEAN_BBP},
 
 #ifdef HAVE_GETUID
-               {"accept-the-risks-running-as-root", no_argument, NULL, 0},
+               {"accept-the-risks-running-as-root", no_argument, NULL,
+                opt_ACCEPT_THE_RISKS_RUNNING_AS_ROOT},
 #endif
+               {"version", no_argument, NULL, opt_VERSION},
+               {"help", no_argument, NULL, opt_HELP},
 
                {NULL, 0, NULL, 0}
        };
@@ -429,233 +469,18 @@ wmain(int argc, wchar_t **argv)
 #endif
 
        for (;;) {
-               int option_index = 0;
-
                int c = getopt_long(argc, av, "c:d::rs:?",
-                                   long_options, &option_index);
+                                   long_options, NULL);
 
                if (c == -1)
                        break;
 
                switch (c) {
-               case 0:
-#ifdef HAVE_GETUID
-                       if (strcmp(long_options[option_index].name, 
"accept-the-risks-running-as-root") == 0) {
-                               allow_root = true;
-                               break;
-                       }
-#endif
-                       if (strcmp(long_options[option_index].name, in_memory) 
== 0) {
-                               inmemory = true;
-                               break;
-                       }
-                       if (strcmp(long_options[option_index].name, "dbpath") 
== 0) {
-                               size_t optarglen = strlen(optarg);
-                               /* remove trailing directory separator */
-                               while (optarglen > 0
-                                      && (optarg[optarglen - 1] == '/'
-                                          || optarg[optarglen - 1] == '\\'))
-                                       optarg[--optarglen] = '\0';
-                               dbpath = absolute_path(optarg);
-                               if (dbpath == NULL) {
-                                       fprintf(stderr,
-                                               "#error: can not allocate 
memory for dbpath\n");
-                                       exit(1);
-                               }
-                               if (strlen(dbpath) >= FILENAME_MAX - 45) {
-                                       fprintf(stderr, "#error: dbpath name 
too long\n");
-                                       exit(1);
-                               }
-                               setlen = mo_add_option(&set, setlen, 
opt_cmdline,
-                                                      "gdk_dbpath", dbpath);
-                               break;
-                       }
-                       if (strcmp(long_options[option_index].name, "dbextra") 
== 0) {
-                               if (dbextra) {
-                                       fprintf(stderr,
-                                               "#warning: ignoring multiple 
--dbextra arguments\n");
-                                       break;
-                               }
-                               if (strcmp(optarg, in_memory) == 0) {
-                                       dbextra = (char *) in_memory;
-                               } else {
-                                       size_t optarglen = strlen(optarg);
-                                       /* remove trailing directory separator 
*/
-                                       while (optarglen > 0
-                                              && (optarg[optarglen - 1] == '/'
-                                                  || optarg[optarglen - 1] == 
'\\'))
-                                               optarg[--optarglen] = '\0';
-                                       dbextra = absolute_path(optarg);
-                                       if (dbextra == NULL) {
-                                               fprintf(stderr,
-                                                       "#error: can not 
allocate memory for dbextra\n");
-                                               exit(1);
-                                       }
-                                       if (strlen(dbextra) >= FILENAME_MAX - 
45) {
-                                               fprintf(stderr, "#error: 
dbextra name too long\n");
-                                               exit(1);
-                                       }
-                               }
-                               break;
-                       }
-                       if (strcmp(long_options[option_index].name, "dbtemp") 
== 0) {
-                               if (dbtemp) {
-                                       fprintf(stderr,
-                                               "#warning: ignoring multiple 
--dbtemp arguments\n");
-                                       break;
-                               }
-                               if (strcmp(optarg, in_memory) == 0) {
-                                       dbtemp = (char *) in_memory;
-                               } else {
-                                       size_t optarglen = strlen(optarg);
-                                       /* remove trailing directory separator 
*/
-                                       while (optarglen > 0
-                                              && (optarg[optarglen - 1] == '/'
-                                                  || optarg[optarglen - 1] == 
'\\'))
-                                               optarg[--optarglen] = '\0';
-                                       dbtemp = absolute_path(optarg);
-                                       if (dbtemp == NULL) {
-                                               fprintf(stderr,
-                                                       "#error: can not 
allocate memory for dbtemp\n");
-                                               exit(1);
-                                       }
-                                       if (strlen(dbtemp) >= FILENAME_MAX - 
45) {
-                                               fprintf(stderr, "#error: dbtemp 
name too long\n");
-                                               exit(1);
-                                       }
-                               }
-                               break;
-                       }
-
-                       if (strcmp(long_options[option_index].name, "dbtrace") 
== 0) {
-                               size_t optarglen = strlen(optarg);
-                               /* remove trailing directory separator */
-                               while (optarglen > 0
-                                      && (optarg[optarglen - 1] == '/'
-                                          || optarg[optarglen - 1] == '\\'))
-                                       optarg[--optarglen] = '\0';
-                               if (strcmp(optarg, "stdout") == 0)
-                                       dbtrace = optarg;
-                               else
-                                       dbtrace = absolute_path(optarg);
-                               if (dbtrace == NULL)
-                                       fprintf(stderr,
-                                               "#error: can not allocate 
memory for dbtrace\n");
-                               else
-                                       setlen = mo_add_option(&set, setlen, 
opt_cmdline,
-                                                              "gdk_dbtrace", 
dbtrace);
-                               break;
-                       }
-
-                       if (strcmp(long_options[option_index].name, 
"single-user") == 0) {
-                               setlen = mo_add_option(&set, setlen, 
opt_cmdline,
-                                                      "gdk_single_user", 
"yes");
-                               break;
-                       }
-                       if (strcmp(long_options[option_index].name, "version") 
== 0) {
-                               monet_version();
-                               exit(0);
-                       }
-                       if (strcmp(long_options[option_index].name, "logging") 
== 0) {
-                               char *tmp = strchr(optarg, '=');
-                               if (tmp) {
-                                       *tmp = 0;
-                                       if 
(GDKtracer_set_component_level(optarg, tmp + 1) != GDK_SUCCEED) {
-                                               fprintf(stderr, "WARNING: could 
not set logging component %s to %s\n",
-                                                       optarg, tmp + 1);
-                                       }
-                               } else {
-                                       fprintf(stderr, "ERROR: --logging flag 
requires component=level argument\n");
-                               }
-                               break;
-                       }
-                       /* debugging options */
-                       if (strcmp(long_options[option_index].name, "pipeline") 
== 0) {
-                               default_pipeline_mode = true;
-                               break;
-                       }
-                       if (strcmp(long_options[option_index].name, 
"algorithms") == 0) {
-                               grpdebug |= GRPalgorithms;
-                               break;
-                       }
-                       if (strcmp(long_options[option_index].name, 
"forcemito") == 0) {
-                               grpdebug |= GRPforcemito;
-                               break;
-                       }
-                       if (strcmp(long_options[option_index].name, "heaps") == 
0) {
-                               grpdebug |= GRPheaps;
-                               break;
-                       }
-                       if (strcmp(long_options[option_index].name, "io") == 0) 
{
-                               grpdebug |= GRPio;
-                               break;
-                       }
-                       if (strcmp(long_options[option_index].name, "memory") 
== 0) {
-                               grpdebug |= GRPmemory;
-                               break;
-                       }
-                       if (strcmp(long_options[option_index].name, "modules") 
== 0) {
-                               grpdebug |= GRPmodules;
-                               break;
-                       }
-                       if (strcmp(long_options[option_index].name, 
"performance") == 0) {
-                               grpdebug |= GRPperformance;
-                               break;
-                       }
-                       if (strcmp(long_options[option_index].name, 
"properties") == 0) {
-                               grpdebug |= GRPproperties;
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to