[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [PATCH 3 of 3 V2] xl: SWITCH_FOREACH_OPT handles special options directly



# HG changeset patch
# User Ian Campbell <ijc@xxxxxxxxxxxxxx>
# Date 1355843798 0
# Node ID 03b4c57dd562e5477615f4fd6bc0d78f6227a503
# Parent  4f8b5e25370792c1360dc7b96f769acd0d22d6e9
xl: SWITCH_FOREACH_OPT handles special options directly.

This removes the need for the "case 0: case 2:" boilerplate in every
main_foo() but at the expense of a return in a macro which I find
(mildly) distasteful.

Signed-off-by: Ian Campbell <ian.campbell@xxxxxxxxxx>

diff -r 4f8b5e253707 -r 03b4c57dd562 tools/libxl/xl_cmdimpl.c
--- a/tools/libxl/xl_cmdimpl.c  Tue Dec 18 15:03:40 2012 +0000
+++ b/tools/libxl/xl_cmdimpl.c  Tue Dec 18 15:16:38 2012 +0000
@@ -2396,12 +2396,12 @@ static int def_getopt(int argc, char * c
  * `lopts`) should be handled by a case statement as if it were inside
  * a switch statement.
  *
- * In addition to the options provided in opts callers must handle
- * two additional pseudo options:
- *  0 -- generated if the user passes a -h option. help will be printed,
- *       caller should immediately return 0.
- *  2 -- generated if the user does not provided `num_required_opts`
- *       non-option arguments, caller should immediately return 2.
+ * In addition to the options provided in opts the macro will handle
+ * two special pseudo options:
+ *  -- if the user passes a -h option. help will be printed, and the
+ *     macro will return 0.
+ *  -- if the user does not provided `num_required_opts`
+ *     non-option arguments, the macro will return 2.
  *
  * Example:
  *
@@ -2409,8 +2409,6 @@ static int def_getopt(int argc, char * c
  *     int opt;
  *
  *     SWITCH_FOREACH_OPT(opt, "blah", NULL, "foo", 0) {
- *     case 0: case2:
- *          return opt;
  *      case 'b':
  *          ... handle b option...
  *          break;
@@ -2426,6 +2424,8 @@ static int def_getopt(int argc, char * c
                            commandname, num_required_opts)              \
     while (((opt) = def_getopt(argc, argv, (opts), (longopts),          \
                                 (commandname), (num_required_opts))) != -1) \
+        if (opt == 0) return 0;                                         \
+        if (opt == 2) return 2;                                         \
         switch (opt)
 
 static int set_memory_max(uint32_t domid, const char *mem)
@@ -2452,8 +2452,7 @@ int main_memmax(int argc, char **argv)
     int rc;
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "mem-max", 2) {
-    case 0: case 2:
-        return opt;
+        /* No options */
     }
 
     domid = find_domain(argv[optind]);
@@ -2488,8 +2487,7 @@ int main_memset(int argc, char **argv)
     const char *mem;
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "mem-set", 2) {
-    case 0: case 2:
-        return opt;
+        /* No options */
     }
 
     domid = find_domain(argv[optind]);
@@ -2529,8 +2527,7 @@ int main_cd_eject(int argc, char **argv)
     const char *virtdev;
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "cd-eject", 2) {
-        case 0: case 2:
-            return opt;
+        /* No options */
     }
 
     domid = find_domain(argv[optind]);
@@ -2548,8 +2545,7 @@ int main_cd_insert(int argc, char **argv
     char *file = NULL; /* modified by cd_insert tokenising it */
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "cd-insert", 3) {
-    case 0: case 2:
-        return opt;
+        /* No options */
     }
 
     domid = find_domain(argv[optind]);
@@ -2567,8 +2563,6 @@ int main_console(int argc, char **argv)
     libxl_console_type type = 0;
 
     SWITCH_FOREACH_OPT(opt, "n:t:", NULL, "console", 1) {
-    case 0: case 2:
-        return opt;
     case 't':
         if (!strcmp(optarg, "pv"))
             type = LIBXL_CONSOLE_TYPE_PV;
@@ -2605,8 +2599,6 @@ int main_vncviewer(int argc, char **argv
     int opt, autopass = 0;
 
     SWITCH_FOREACH_OPT(opt, "ah", opts, "vncviewer", 1) {
-    case 0: case 2:
-        return opt;
     case 'a':
         autopass = 1;
         break;
@@ -2643,8 +2635,7 @@ int main_pcilist(int argc, char **argv)
     int opt;
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "pci-list", 1) {
-    case 0: case 2:
-        return opt;
+        /* No options */
     }
 
     domid = find_domain(argv[optind]);
@@ -2684,8 +2675,6 @@ int main_pcidetach(int argc, char **argv
     const char *bdf = NULL;
 
     SWITCH_FOREACH_OPT(opt, "f", NULL, "pci-detach", 2) {
-    case 0: case 2:
-        return opt;
     case 'f':
         force = 1;
         break;
@@ -2724,8 +2713,7 @@ int main_pciattach(int argc, char **argv
     const char *bdf = NULL, *vs = NULL;
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "pci-attach", 2) {
-    case 0: case 2:
-        return opt;
+        /* No options */
     }
 
     domid = find_domain(argv[optind]);
@@ -2760,8 +2748,7 @@ int main_pciassignable_list(int argc, ch
     int opt;
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "pci-assignable-list", 0) {
-    case 0: case 2:
-        return opt;
+        /* No options */
     }
 
     pciassignable_list();
@@ -2794,8 +2781,7 @@ int main_pciassignable_add(int argc, cha
     const char *bdf = NULL;
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "pci-assignable-add", 1) {
-    case 0: case 2:
-        return opt;
+        /* No options */
     }
 
     bdf = argv[optind];
@@ -2831,8 +2817,6 @@ int main_pciassignable_remove(int argc, 
     int rebind = 0;
 
     SWITCH_FOREACH_OPT(opt, "r", NULL, "pci-assignable-remove", 1) {
-    case 0: case 2:
-        return opt;
     case 'r':
         rebind=1;
         break;
@@ -3647,8 +3631,6 @@ int main_restore(int argc, char **argv)
     };
 
     SWITCH_FOREACH_OPT(opt, "FhcpdeVA", opts, "restore", 1) {
-    case 0: case 2:
-        return opt;
     case 'c':
         console_autoconnect = 1;
         break;
@@ -3708,8 +3690,6 @@ int main_migrate_receive(int argc, char 
     int opt;
 
     SWITCH_FOREACH_OPT(opt, "Fedr", NULL, "migrate-receive", 0) {
-    case 0: case 2:
-        return opt;
     case 'F':
         daemonize = 0;
         break;
@@ -3745,8 +3725,6 @@ int main_save(int argc, char **argv)
     int opt;
 
     SWITCH_FOREACH_OPT(opt, "c", NULL, "save", 2) {
-    case 0: case 2:
-        return opt;
     case 'c':
         checkpoint = 1;
         break;
@@ -3776,8 +3754,6 @@ int main_migrate(int argc, char **argv)
     int opt, daemonize = 1, monitor = 1, debug = 0;
 
     SWITCH_FOREACH_OPT(opt, "FC:s:ed", NULL, "migrate", 2) {
-    case 0: case 2:
-        return opt;
     case 'C':
         config_filename = optarg;
         break;
@@ -3818,8 +3794,7 @@ int main_dump_core(int argc, char **argv
     int opt;
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "dump-core", 2) {
-    case 0: case 2:
-        return opt;
+        /* No options */
     }
 
     core_dump_domain(find_domain(argv[optind]), argv[optind + 1]);
@@ -3831,8 +3806,7 @@ int main_pause(int argc, char **argv)
     int opt;
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "pause", 1) {
-    case 0: case 2:
-        return opt;
+        /* No options */
     }
 
     pause_domain(find_domain(argv[optind]));
@@ -3845,8 +3819,7 @@ int main_unpause(int argc, char **argv)
     int opt;
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "unpause", 1) {
-    case 0: case 2:
-        return opt;
+        /* No options */
     }
 
     unpause_domain(find_domain(argv[optind]));
@@ -3859,8 +3832,7 @@ int main_destroy(int argc, char **argv)
     int opt;
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "destroy", 1) {
-    case 0: case 2:
-        return opt;
+        /* No options */
     }
 
     destroy_domain(find_domain(argv[optind]));
@@ -3884,8 +3856,6 @@ static int main_shutdown_or_reboot(int d
     };
 
     SWITCH_FOREACH_OPT(opt, "awF", opts, what, 0) {
-    case 0: case 2:
-        return opt;
     case 'a':
         all = 1;
         break;
@@ -3966,8 +3936,6 @@ int main_list(int argc, char **argv)
     int nb_domain, rc;
 
     SWITCH_FOREACH_OPT(opt, "lvhZ", opts, "list", 0) {
-    case 0: case 2:
-        return opt;
     case 'l':
         details = 1;
         break;
@@ -4023,8 +3991,7 @@ int main_vm_list(int argc, char **argv)
     int opt;
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "vm-list", 0) {
-    case 0: case 2:
-        return opt;
+        /* No options */
     }
 
     list_vm();
@@ -4056,8 +4023,6 @@ int main_create(int argc, char **argv)
     }
 
     SWITCH_FOREACH_OPT(opt, "Fhnqf:pcdeVA", opts, "create", 0) {
-    case 0: case 2:
-        return opt;
     case 'f':
         filename = optarg;
         break;
@@ -4157,8 +4122,6 @@ int main_config_update(int argc, char **
     }
 
     SWITCH_FOREACH_OPT(opt, "dhqf:", opts, "config_update", 0) {
-    case 0: case 2:
-        return opt;
     case 'd':
         debug = 1;
         break;
@@ -4254,8 +4217,7 @@ int main_button_press(int argc, char **a
 
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "button-press", 2) {
-    case 0: case 2:
-        return opt;
+        /* No options */
     }
 
     button_press(find_domain(argv[optind]), argv[optind + 1]);
@@ -4397,8 +4359,7 @@ int main_vcpulist(int argc, char **argv)
     int opt;
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "cpu-list", 0) {
-    case 0: case 2:
-        return opt;
+        /* No options */
     }
 
     vcpulist(argc - optind, argv + optind);
@@ -4460,8 +4421,7 @@ int main_vcpupin(int argc, char **argv)
     int opt;
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "vcpu-pin", 3) {
-    case 0: case 2:
-        return opt;
+        /* No options */
     }
 
     vcpupin(find_domain(argv[optind]), argv[optind+1] , argv[optind+2]);
@@ -4498,8 +4458,7 @@ int main_vcpuset(int argc, char **argv)
     int opt;
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "vcpu-set", 2) {
-    case 0: case 2:
-        return opt;
+        /* No options */
     }
 
     vcpuset(find_domain(argv[optind]), argv[optind+1]);
@@ -4683,8 +4642,6 @@ int main_info(int argc, char **argv)
     int numa = 0;
 
     SWITCH_FOREACH_OPT(opt, "hn", opts, "info", 0) {
-    case 0: case 2:
-        return opt;
     case 'n':
         numa = 1;
         break;
@@ -4722,8 +4679,7 @@ int main_sharing(int argc, char **argv)
     int nb_domain, rc;
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "sharing", 0) {
-    case 0: case 2:
-        return opt;
+        /* No options */
     }
 
     if (optind >= argc) {
@@ -5005,8 +4961,6 @@ int main_sched_credit(int argc, char **a
     };
 
     SWITCH_FOREACH_OPT(opt, "d:w:c:p:t:r:hs", opts, "sched-credit", 0) {
-    case 0: case 2:
-        return opt;
     case 'd':
         dom = optarg;
         break;
@@ -5122,8 +5076,6 @@ int main_sched_credit2(int argc, char **
     };
 
     SWITCH_FOREACH_OPT(opt, "d:w:p:h", opts, "sched-credit2", 0) {
-    case 0: case 2:
-        return opt;
     case 'd':
         dom = optarg;
         break;
@@ -5195,8 +5147,6 @@ int main_sched_sedf(int argc, char **arg
     };
 
     SWITCH_FOREACH_OPT(opt, "d:p:s:l:e:w:c:h", opts, "sched-sedf", 0) {
-    case 0: case 2:
-        return opt;
     case 'd':
         dom = optarg;
         break;
@@ -5290,8 +5240,7 @@ int main_domid(int argc, char **argv)
     const char *domname = NULL;
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "domid", 1) {
-    case 0: case 2:
-        return opt;
+        /* No options */
     }
 
     domname = argv[optind];
@@ -5314,8 +5263,7 @@ int main_domname(int argc, char **argv)
     char *endptr = NULL;
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "domname", 1) {
-    case 0: case 2:
-        return opt;
+        /* No options */
     }
 
     domid = strtol(argv[optind], &endptr, 10);
@@ -5344,8 +5292,7 @@ int main_rename(int argc, char **argv)
     const char *dom, *new_name;
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "rename", 2) {
-    case 0: case 2:
-        return opt;
+        /* No options */
     }
 
     dom = argv[optind++];
@@ -5370,8 +5317,7 @@ int main_trigger(int argc, char **argv)
     libxl_trigger trigger;
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "trigger", 2) {
-    case 0: case 2:
-        return opt;
+        /* No options */
     }
 
     domid = find_domain(argv[optind++]);
@@ -5402,8 +5348,7 @@ int main_sysrq(int argc, char **argv)
     const char *sysrq = NULL;
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "sysrq", 2) {
-    case 0: case 2:
-        return opt;
+        /* No options */
     }
 
     domid = find_domain(argv[optind++]);
@@ -5427,8 +5372,7 @@ int main_debug_keys(int argc, char **arg
     char *keys;
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "debug-keys", 1) {
-    case 0: case 2:
-        return opt;
+        /* No options */
     }
 
     keys = argv[optind];
@@ -5449,8 +5393,6 @@ int main_dmesg(int argc, char **argv)
     int opt, ret = 1;
 
     SWITCH_FOREACH_OPT(opt, "c", NULL, "dmesg", 0) {
-    case 0: case 2:
-        return opt;
     case 'c':
         clear = 1;
         break;
@@ -5473,8 +5415,7 @@ int main_top(int argc, char **argv)
     int opt;
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "top", 0) {
-    case 0: case 2:
-        return opt;
+        /* No options */
     }
 
     return system("xentop");
@@ -5492,8 +5433,7 @@ int main_networkattach(int argc, char **
     unsigned int val;
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "network-attach", 1) {
-    case 0: case 2:
-        return opt;
+        /* No options */
     }
 
     if (argc-optind > 11) {
@@ -5581,8 +5521,7 @@ int main_networklist(int argc, char **ar
     int nb, i;
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "network-list", 1) {
-    case 0: case 2:
-        return opt;
+        /* No options */
     }
 
     /*      Idx  BE   MAC   Hdl  Sta  evch txr/rxr  BE-path */
@@ -5620,8 +5559,7 @@ int main_networkdetach(int argc, char **
     libxl_device_nic nic;
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "network-detach", 2) {
-    case 0: case 2:
-        return opt;
+        /* No options */
     }
 
     domid = find_domain(argv[optind]);
@@ -5653,8 +5591,7 @@ int main_blockattach(int argc, char **ar
     XLU_Config *config = 0;
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "block-attach", 2) {
-    case 0: case 2:
-        return opt;
+        /* No options */
     }
 
     if (domain_qualifier_to_domid(argv[optind], &fe_domid, 0) < 0) {
@@ -5690,8 +5627,7 @@ int main_blocklist(int argc, char **argv
     libxl_diskinfo diskinfo;
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "block-list", 1) {
-    case 0: case 2:
-        return opt;
+        /* No options */
     }
 
     printf("%-5s %-3s %-6s %-5s %-6s %-8s %-30s\n",
@@ -5728,8 +5664,7 @@ int main_blockdetach(int argc, char **ar
     libxl_device_disk disk;
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "block-detach", 2) {
-    case 0: case 2:
-        return opt;
+        /* No options */
     }
 
     domid = find_domain(argv[optind]);
@@ -5755,8 +5690,7 @@ int main_vtpmattach(int argc, char **arg
     uint32_t domid;
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "vtpm-attach", 1) {
-    case 0: case 2:
-        return opt;
+        /* No options */
     }
 
     if (domain_qualifier_to_domid(argv[optind], &domid, 0) < 0) {
@@ -5810,8 +5744,7 @@ int main_vtpmlist(int argc, char **argv)
     int nb, i;
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "vtpm-list", 1) {
-    case 0: case 2:
-        return opt;
+        /* No options */
     }
 
     /*      Idx  BE   UUID   Hdl  Sta  evch rref  BE-path */
@@ -5852,8 +5785,7 @@ int main_vtpmdetach(int argc, char **arg
     libxl_uuid uuid;
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "vtpm-detach", 2) {
-    case 0: case 2:
-        return opt;
+        /* No options */
     }
 
     domid = find_domain(argv[optind]);
@@ -6046,8 +5978,6 @@ int main_uptime(int argc, char **argv)
     int opt;
 
     SWITCH_FOREACH_OPT(opt, "s", NULL, "uptime", 1) {
-    case 0: case 2:
-        return opt;
     case 's':
         short_mode = 1;
         break;
@@ -6071,8 +6001,6 @@ int main_tmem_list(int argc, char **argv
     int opt;
 
     SWITCH_FOREACH_OPT(opt, "al", NULL, "tmem-list", 0) {
-    case 0: case 2:
-        return opt;
     case 'l':
         use_long = 1;
         break;
@@ -6110,8 +6038,6 @@ int main_tmem_freeze(int argc, char **ar
     int opt;
 
     SWITCH_FOREACH_OPT(opt, "a", NULL, "tmem-freeze", 0) {
-    case 0: case 2:
-        return opt;
     case 'a':
         all = 1;
         break;
@@ -6141,8 +6067,6 @@ int main_tmem_thaw(int argc, char **argv
     int opt;
 
     SWITCH_FOREACH_OPT(opt, "a", NULL, "tmem-thaw", 0) {
-    case 0: case 2:
-        return opt;
     case 'a':
         all = 1;
         break;
@@ -6174,8 +6098,6 @@ int main_tmem_set(int argc, char **argv)
     int opt;
 
     SWITCH_FOREACH_OPT(opt, "aw:c:p:", NULL, "tmem-set", 0) {
-    case 0: case 2:
-        return opt;
     case 'a':
         all = 1;
         break;
@@ -6233,8 +6155,6 @@ int main_tmem_shared_auth(int argc, char
     int opt;
 
     SWITCH_FOREACH_OPT(opt, "au:A:", NULL, "tmem-shared-auth", 0) {
-    case 0: case 2:
-        return opt;
     case 'a':
         all = 1;
         break;
@@ -6281,8 +6201,7 @@ int main_tmem_freeable(int argc, char **
     int mb;
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "tmem-freeale", 0) {
-    case 0: case 2:
-        return opt;
+        /* No options */
     }
 
     mb = libxl_tmem_freeable(ctx);
@@ -6323,8 +6242,6 @@ int main_cpupoolcreate(int argc, char **
     int rc = -ERROR_FAIL;
 
     SWITCH_FOREACH_OPT(opt, "hnf:", opts, "cpupool-create", 0) {
-    case 0: case 2:
-        return opt;
     case 'f':
         filename = optarg;
         break;
@@ -6506,8 +6423,6 @@ int main_cpupoollist(int argc, char **ar
     int ret = 0;
 
     SWITCH_FOREACH_OPT(opt, "hc", opts, "cpupool-list", 1) {
-    case 0: case 2:
-        break;
     case 'c':
         opt_cpus = 1;
         break;
@@ -6571,8 +6486,7 @@ int main_cpupooldestroy(int argc, char *
     uint32_t poolid;
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "cpupool-destroy", 1) {
-    case 0: case 2:
-        return opt;
+        /* No options */
     }
 
     pool = argv[optind];
@@ -6594,8 +6508,7 @@ int main_cpupoolrename(int argc, char **
     uint32_t poolid;
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "cpupool-rename", 2) {
-    case 0: case 2:
-        return opt;
+        /* No options */
     }
 
     pool = argv[optind++];
@@ -6626,8 +6539,7 @@ int main_cpupoolcpuadd(int argc, char **
     int n;
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "cpupool-cpu-add", 2) {
-    case 0: case 2:
-        return opt;
+        /* No options */
     }
 
     pool = argv[optind++];
@@ -6672,8 +6584,7 @@ int main_cpupoolcpuremove(int argc, char
     int n;
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "cpupool-cpu-remove", 2) {
-    case 0: case 2:
-        return opt;
+        /* No options */
     }
 
     pool = argv[optind++];
@@ -6717,8 +6628,7 @@ int main_cpupoolmigrate(int argc, char *
     uint32_t domid;
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "cpupool-migrate", 2) {
-    case 0: case 2:
-        return opt;
+        /* No options */
     }
 
     dom = argv[optind++];
@@ -6759,8 +6669,7 @@ int main_cpupoolnumasplit(int argc, char
     libxl_dominfo info;
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "cpupool-numa-split", 0) {
-    case 0: case 2:
-        return opt;
+        /* No options */
     }
 
     ret = 0;
@@ -7015,8 +6924,6 @@ int main_remus(int argc, char **argv)
     r_info.compression = 1;
 
     SWITCH_FOREACH_OPT(opt, "bui:s:e", NULL, "remus", 2) {
-    case 0: case 2:
-        return opt;
     case 'i':
         r_info.interval = atoi(optarg);
         break;

_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxx
http://lists.xen.org/xen-devel


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.