aboutsummaryrefslogtreecommitdiffstats
path: root/cli_main.c
diff options
context:
space:
mode:
authorGuy Harris <guy@alum.mit.edu>2018-12-12 02:53:08 -0800
committerGuy Harris <guy@alum.mit.edu>2018-12-13 00:52:11 +0000
commita34cc98b2ac5a0ad011a1c3a40ce5e59230a9498 (patch)
tree6cff6069cc4b3925fa4af82ce968959444dadd75 /cli_main.c
parent17b721e45180a9decebb70dab77f41d96d5f7a03 (diff)
Put the main() and wmain() routines for CLI programs into a separate file.
That means that code is only in one place, rather than having copies of it in each of those programs. CLI programs that, on Windows, should get UTF-8 arguments rather than arguments in the local code page should: include the top-level cli_main.h header; define the main function as real_main(); be built with the top-level cli_main.c file. On UN*X, cli_main.c has a main() program, and just passes the arguments on to real_main(). On Windows, cli_main.c has a wmain() function that converts the UTF-16 arguments it's handed to UTF-8 arguments, using WideCharToMultiByte() so that it doesn't use any functions other than those provided by the system, and then calls real_main() with the argument count and UTF-8 arguments. Change-Id: I8b11f01dbc5c63fce599d1bef9ad96cd92c3c01e Reviewed-on: https://code.wireshark.org/review/31017 Petri-Dish: Guy Harris <guy@alum.mit.edu> Tested-by: Petri Dish Buildbot Reviewed-by: Guy Harris <guy@alum.mit.edu>
Diffstat (limited to 'cli_main.c')
-rw-r--r--cli_main.c73
1 files changed, 73 insertions, 0 deletions
diff --git a/cli_main.c b/cli_main.c
new file mode 100644
index 0000000000..ee5f67e93c
--- /dev/null
+++ b/cli_main.c
@@ -0,0 +1,73 @@
+/*
+ * Compile and link this with all CLI programs where the main routine
+ * should get UTF-8 arguments on Windows. In those programs, declare
+ * the main program as real_main() rather than main().
+ *
+ * This is used in software licensed under the GPLv2, and its license MUST
+ * be compatible with that license.
+ *
+ * This is used in software licensed under the Apache 2.0 license, and its
+ * license MUST be compatible with that license.
+ *
+ * For that purpose, we use the MIT (X11) license.
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#include "cli_main.h"
+
+#ifdef _WIN32
+#include <stdlib.h>
+#include <stdio.h>
+#include <windows.h>
+
+int
+wmain(int argc, wchar_t *wc_argv[])
+{
+ char **argv;
+ int i;
+
+ argv = (char **)malloc((argc + 1) * sizeof(char *));
+ if (argv == NULL) {
+ fprintf(stderr, "Out of memory for converted argument list\n");
+ return 2;
+ }
+ for (i = 0; i < argc; i++) {
+ /*
+ * XXX = use WC_ERR_INVALID_CHARS rather than 0, and fail if
+ * the argument isn't valid UTF-16?
+ */
+ int width;
+ char *utf8_string;
+
+ width = WideCharToMultiByte(CP_UTF8, 0, wc_argv[i], -1, NULL, 0,
+ NULL, NULL);
+ if (width == 0) {
+ fprintf(stderr, "WideCharToMultiByte failed: %d\n",
+ width);
+ return 2;
+ }
+ utf8_string = malloc(width);
+ if (utf8_string == NULL) {
+ fprintf(stderr,
+ "Out of memory for converted argument list\n");
+ return 2;
+ }
+ if (WideCharToMultiByte(CP_UTF8, 0, wc_argv[i], -1, utf8_string,
+ width, NULL, NULL) == 0) {
+ fprintf(stderr, "WideCharToMultiByte failed: %d\n",
+ width);
+ return 2;
+ }
+ argv[i] = utf8_string;
+ }
+ argv[i] = NULL;
+ return real_main(argc, argv);
+}
+#else /* _WIN32 */
+int
+main(int argc, char *argv[])
+{
+ return real_main(argc, argv);
+}
+#endif