From: Oliver Kurth Date: Fri, 15 Sep 2017 18:22:40 +0000 (-0700) Subject: VGAuth cmdline parser improvements X-Git-Tag: stable-10.2.0~742 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=5fdc593389e1ad5a593aad36cbddbb646ed98dc6;p=thirdparty%2Fopen-vm-tools.git VGAuth cmdline parser improvements Restore support for '=' between option and value. --- diff --git a/open-vm-tools/vgauth/cli/main.c b/open-vm-tools/vgauth/cli/main.c index 46df9ff96..b043e5cb8 100644 --- a/open-vm-tools/vgauth/cli/main.c +++ b/open-vm-tools/vgauth/cli/main.c @@ -394,6 +394,12 @@ CliListMapped(VGAuthContext *ctx) ****************************************************************************** */ +#ifdef _WIN32 +#define use_glib_parser 0 +#else +#define use_glib_parser 1 +#endif + static int mainRun(int argc, char *argv[]) @@ -415,8 +421,9 @@ mainRun(int argc, const gchar *lSubject = SU_(cmdline.summary.subject, "subject"); const gchar *lPEMfile = SU_(cmdline.summary.pemfile, "PEM-file"); const gchar *lComm = SU_(cmdline.summary.comm, "comment"); -#ifdef _WIN32 +#if (use_glib_parser == 0) int i; + GOptionEntry *cmdOptions; #else GError *gErr = NULL; #endif @@ -454,7 +461,7 @@ mainRun(int argc, { "global", 'g', 0, G_OPTION_ARG_NONE, &addMapped, SU_(addoptions.global, "Add the certificate to the global mapping file"), NULL }, - { "comment", 'a', 0, G_OPTION_ARG_STRING, &comment, + { "comment", 'c', 0, G_OPTION_ARG_STRING, &comment, SU_(addoptions.comment, "subject comment"), NULL}, { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, SU_(addoptions.verbose, "Verbose operation"), NULL }, @@ -516,63 +523,94 @@ mainRun(int argc, if (strcmp(argvCopy[1], "add") == 0) { doAdd = TRUE; g_option_context_add_main_entries(context, addOptions, NULL); +#if (use_glib_parser == 0) + cmdOptions = addOptions; +#endif } else if (strcmp(argvCopy[1], "remove") == 0) { doRemove = TRUE; g_option_context_add_main_entries(context, removeOptions, NULL); +#if (use_glib_parser == 0) + cmdOptions = removeOptions; +#endif } else if (strcmp(argvCopy[1], "list") == 0) { doList = TRUE; g_option_context_add_main_entries(context, listOptions, NULL); +#if (use_glib_parser == 0) + cmdOptions = listOptions; +#endif } else { Usage(context); } -#ifdef _WIN32 +#if (use_glib_parser == 0) /* * In Windows, g_option_context_parse() does the wrong thing for locale * conversion of the incoming Unicode cmdline. Modern glib (2.40 or - * later) solves this with g_option_context_parse_stv(), but we're stuck + * later) solves this with g_option_context_parse_strv(), but we're stuck * with an older glib for now. * * So instead lets do it all by hand. * + * This does almost everything for the two types of options this code + * cares about. It doesn't handle multiple + * short options behind a single dash, or '--' alone meaning + * to stop parsing. + * * XXX fix this when we upgrade glib. */ for (i = 2; i < argc; i++) { - if (strcmp(argv[i], "--global") == 0 || - strcmp(argv[i], "-g") == 0) { - addMapped = TRUE; - } else if (strcmp(argv[i], "--verbose") == 0 || - strcmp(argv[i], "-v") == 0) { - verbose = TRUE; - } else if (strcmp(argv[i], "--username") == 0 || - strcmp(argv[i], "-u") == 0) { - if ((i + 1) < argc) { - userName = argv[++i]; - } else { - Usage(context); - } - } else if (strcmp(argv[i], "--file") == 0 || - strcmp(argv[i], "-f") == 0) { - if ((i + 1) < argc) { - pemFilename = argv[++i]; - } else { - Usage(context); + GOptionEntry *e; + gboolean match = FALSE; + + e = &cmdOptions[0]; + while (e && e->long_name) { + gsize len; + char longName[32]; + char shortName[3]; + + g_snprintf(longName, sizeof(longName), "--%s", e->long_name); + g_snprintf(shortName, sizeof(shortName), "-%c", e->short_name); + len = strlen(longName); + + // short options don't support '=' + if (strcmp(shortName, argv[i]) == 0) { + if (e->arg == G_OPTION_ARG_STRING) { + if (argv[i+1]) { + *(gchar **) e->arg_data = argv[++i]; + match = TRUE; + } else { + Usage(context); + } + } else if (e->arg == G_OPTION_ARG_NONE) { + *(gboolean *) e->arg_data = TRUE; + match = TRUE; + } + } else if (strncmp(longName, argv[i], len) == 0 && + (argv[i][len] == '=' || argv[i][len] == '\0')) { + gchar *val = NULL; + + if (e->arg == G_OPTION_ARG_STRING) { + if (argv[i][len] == '=') { + val = (gchar *) &(argv[i][len+1]); + } else if (argv[i+1]) { + val = argv[++i]; + } + *(gchar **) e->arg_data = val; + match = TRUE; + } else if ((e->arg == G_OPTION_ARG_NONE) && argv[i][len] == '\0') { + *(gboolean *) e->arg_data = TRUE; + match = TRUE; + } else { + Usage(context); + } } - } else if (strcmp(argv[i], "--comment") == 0 || - strcmp(argv[i], "-c") == 0) { - if ((i + 1) < argc) { - comment = argv[++i]; - } else { - Usage(context); + if (match) { + goto next; } - } else if (strcmp(argv[i], "--subject") == 0 || - strcmp(argv[i], "-s") == 0) { - if ((i + 1) < argc) { - subject = argv[++i]; - } else { - Usage(context); - } - } else { + e++; + } +next: + if (!match) { Usage(context); } }