[Devel] [PATCH] RFC: rewrite vzctl acction strcmp()s as array lookup

Igor Podlesny openvz at poige.ru
Mon May 13 05:52:11 PDT 2013


---
 src/vzctl.c |  149 ++++++++++++++++++++++++++++++++---------------------------
 1 file changed, 80 insertions(+), 69 deletions(-)

diff --git a/src/vzctl.c b/src/vzctl.c
index 359bcde..37fda15 100644
--- a/src/vzctl.c
+++ b/src/vzctl.c
@@ -123,18 +123,79 @@ static void usage(int rc)
 	exit(rc);
 }
 
+static void cb_init_modules(char const *arg_name, act_t arg_action)
+{
+	init_modules(&g_action, arg_name);
+}
+
+static void cb_help(char const *arg_name, act_t arg_action)
+{
+	usage(0);
+}
+
+static int quiet = 0;
+static void cb_set_quiet(char const *arg_name, act_t arg_action)
+{
+	quiet = 1;
+}
+
 int main(int argc, char *argv[], char *envp[])
 {
 	act_t action = -1;
 	int verbose = 0;
 	int verbose_custom = 0;
-	int quiet = 0;
 	int veid, ret, skiplock = 0;
 	char buf[256];
 	vps_param *gparam = NULL, *vps_p = NULL, *cmd_p = NULL;
 	const char *action_nm;
 	struct sigaction act;
 	char *name = NULL, *opt;
+	unsigned int i = 0;
+
+	struct {
+		char const *const name;
+		act_t const action;
+		void (*const cb)(char const *, act_t const);
+	} const name_action_cb[] = {
+		{ "set",	ACTION_SET,	cb_init_modules	},
+		{ "create",	ACTION_CREATE,	cb_init_modules	},
+		{ "start",	ACTION_START,	cb_init_modules	},
+		{ "stop",	ACTION_STOP,	cb_init_modules	},
+		{ "restart",	ACTION_RESTART,		NULL	},
+		{ "destroy",	ACTION_DESTROY,		NULL	},
+		{ "delete",	ACTION_DESTROY,		NULL	},
+		{ "mount",	ACTION_MOUNT,		NULL	},
+		{ "umount",	ACTION_UMOUNT,		NULL	},
+		{ "exec3",	ACTION_EXEC3,		NULL	},
+		{ "exec2",	ACTION_EXEC2,		NULL	},
+		{ "exec",	ACTION_EXEC,		NULL	},
+		{ "runscript",	ACTION_RUNSCRIPT,	NULL	},
+		{ "enter",	ACTION_ENTER,		NULL	},
+		{ "console",	ACTION_CONSOLE,		NULL	},
+#ifdef HAVE_PLOOP
+		{ "convert",	ACTION_CONVERT,		NULL	},
+		{ "compact",	ACTION_COMPACT,		NULL	},
+#endif
+		{ "status",	ACTION_STATUS,	cb_set_quiet	},
+		{ "suspend",	ACTION_SUSPEND,		NULL	},
+		{ "chkpnt",	ACTION_SUSPEND,		NULL	},
+		{ "resume",	ACTION_RESUME,		NULL	},
+		{ "restore",	ACTION_RESUME,		NULL	},
+		{ "quotaon",	ACTION_QUOTAON,		NULL	},
+		{ "quotaoff",	ACTION_QUOTAOFF,	NULL	},
+		{ "quotainit",	ACTION_QUOTAINIT,	NULL	},
+
+#ifdef HAVE_PLOOP
+		{ "snapshot",		ACTION_SNAPSHOT_CREATE,	NULL },
+		{ "snapshot-switch",	ACTION_SNAPSHOT_SWITCH,	NULL },
+		{ "snapshot-delete",	ACTION_SNAPSHOT_DELETE,	NULL },
+		{ "snapshot-list",	ACTION_SNAPSHOT_LIST,	NULL },
+		{ "snapshot-mount",	ACTION_SNAPSHOT_MOUNT,	NULL },
+		{ "snapshot-umount",	ACTION_SNAPSHOT_UMOUNT,	NULL },
+#endif
+		{ "--help",	0,	cb_help	},
+		{ NULL, 0, NULL }
+	};
 
 	_proc_title = argv[0];
 	_proc_title_len = envp[0] - argv[0];
@@ -169,74 +230,20 @@ int main(int argc, char *argv[], char *envp[])
 		usage(VZ_INVALID_PARAMETER_SYNTAX);
 	action_nm = argv[1];
 	init_log(NULL, 0, 1, verbose, quiet, NULL);
-	if (!strcmp(argv[1], "set")) {
-		init_modules(&g_action, "set");
-		action = ACTION_SET;
-	} else if (!strcmp(argv[1], "create")) {
-		init_modules(&g_action, "create");
-		action = ACTION_CREATE;
-	} else if (!strcmp(argv[1], "start")) {
-		init_modules(&g_action, "set");
-		action = ACTION_START;
-	} else if (!strcmp(argv[1], "stop")) {
-		init_modules(&g_action, "set");
-		action = ACTION_STOP;
-	} else if (!strcmp(argv[1], "restart")) {
-		action = ACTION_RESTART;
-	} else if (!strcmp(argv[1], "destroy") || !strcmp(argv[1], "delete")) {
-		action = ACTION_DESTROY;
-	} else if (!strcmp(argv[1], "mount")) {
-		action = ACTION_MOUNT;
-	} else if (!strcmp(argv[1], "umount")) {
-		action = ACTION_UMOUNT;
-	} else if (!strcmp(argv[1], "exec3")) {
-		action = ACTION_EXEC3;
-	} else if (!strcmp(argv[1], "exec2")) {
-		action = ACTION_EXEC2;
-	} else if (!strcmp(argv[1], "exec")) {
-		action = ACTION_EXEC;
-	} else if (!strcmp(argv[1], "runscript")) {
-		action = ACTION_RUNSCRIPT;
-	} else if (!strcmp(argv[1], "enter")) {
-		action = ACTION_ENTER;
-	} else if (!strcmp(argv[1], "console")) {
-		action = ACTION_CONSOLE;
-#ifdef HAVE_PLOOP
-	} else if (!strcmp(argv[1], "convert")) {
-		action = ACTION_CONVERT;
-	} else if (!strcmp(argv[1], "compact")) {
-		action = ACTION_COMPACT;
-#endif
-	} else if (!strcmp(argv[1], "status")) {
-		action = ACTION_STATUS;
-		quiet = 1;
-	} else if (!strcmp(argv[1], "suspend") || !strcmp(argv[1], "chkpnt")) {
-		action = ACTION_SUSPEND;
-	} else if (!strcmp(argv[1], "resume") || !strcmp(argv[1], "restore")) {
-		action = ACTION_RESUME;
-	} else if (!strcmp(argv[1], "quotaon")) {
-		action = ACTION_QUOTAON;
-	} else if (!strcmp(argv[1], "quotaoff")) {
-		action = ACTION_QUOTAOFF;
-	} else if (!strcmp(argv[1], "quotainit")) {
-		action = ACTION_QUOTAINIT;
-#ifdef HAVE_PLOOP
-	} else if (!strcmp(argv[1], "snapshot")) {
-		action = ACTION_SNAPSHOT_CREATE;
-	} else if (!strcmp(argv[1], "snapshot-switch")) {
-		action = ACTION_SNAPSHOT_SWITCH;
-	} else if (!strcmp(argv[1], "snapshot-delete")) {
-		action = ACTION_SNAPSHOT_DELETE;
-	} else if (!strcmp(argv[1], "snapshot-list")) {
-		action = ACTION_SNAPSHOT_LIST;
-	} else if (!strcmp(argv[1], "snapshot-mount")) {
-		action = ACTION_SNAPSHOT_MOUNT;
-	} else if (!strcmp(argv[1], "snapshot-umount")) {
-		action = ACTION_SNAPSHOT_UMOUNT;
-#endif
-	} else if (!strcmp(argv[1], "--help")) {
-		usage(0);
-	} else {
+
+	for (i = 0; (name = name_action_cb[i].name) != NULL; ++i) {
+		if (!strncmp(argv[1], name, strlen(name))) {
+			void (*const cb)(char const *, act_t const) =
+				name_action_cb[i].cb;
+			action = name_action_cb[i].action;
+			if (cb != NULL) {
+				(*cb)(name, action);
+			}
+			break;
+		}
+	}
+
+	if (!name) {
 		init_modules(&g_action, action_nm);
 		action = ACTION_CUSTOM;
 		if (!g_action.mod_count) {
@@ -244,7 +251,11 @@ int main(int argc, char *argv[], char *envp[])
 			ret = VZ_INVALID_PARAMETER_SYNTAX;
 			goto error;
 		}
+	} else {
+		/* We have to clean it before free() */
+		name = NULL;
 	}
+
 	if (argc < 3) {
 		fprintf(stderr, "CT ID missing\n");
 		ret = VZ_INVALID_PARAMETER_VALUE;
-- 
1.7.9.5




More information about the Devel mailing list