-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcop_main.h
86 lines (81 loc) · 2.68 KB
/
cop_main.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* Copyright (c) 2020 Nick Appleton
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE. */
/* Portable implementaiton of main()
*
* This header defines the following function-like macro which expands to a
* C main() entry point:
*
* COP_MAIN(main_fn_)
*
* main_fn_ must be a name of a function which has the prototype:
*
* int main_fn(int argc, char *argv[]);
*
* The value of argc is the number of entries in argv. Each entry of argv is
* a UTF-8 encoded Unicode string with the command line argument.
*/
#ifndef COP_MAIN_H
#define COP_MAIN_H
#ifdef WIN32
#include <windows.h>
#define COP_MAIN(main_fn_) \
int main(int argc, char *argv[]) { \
LPWSTR cmdline = GetCommandLineW(); \
char **p_args; \
char **p_args2; \
int nb_args, i, ret; \
(void)argc; \
(void)argv; \
if (cmdline == NULL) \
abort(); \
p_args = (char **)CommandLineToArgvW(cmdline, &nb_args); \
if (p_args == NULL || nb_args < 1) \
abort(); \
p_args2 = (char **)malloc(sizeof(char *) * nb_args); \
if (p_args2 == NULL) \
abort(); \
for (i = 0; i < nb_args; i++) { \
int x = WideCharToMultiByte(CP_UTF8, 0, (LPCWCH)(p_args[i]), -1, NULL, 0, NULL, NULL); \
if (x <= 0) \
abort(); \
char *m = (char *)malloc(x); \
if (m == NULL) \
abort(); \
if (x != WideCharToMultiByte(CP_UTF8, 0, (LPCWCH)(p_args[i]), -1, m, x, NULL, NULL)) \
abort(); \
p_args[i] = m; \
p_args2[i] = m; \
} \
ret = main_fn_(nb_args, p_args2); \
for (i = 0; i < nb_args; i++) { \
free(p_args[i]); \
} \
free(p_args2); \
LocalFree(p_args); \
return ret; \
}
#endif
#ifndef COP_MAIN
#define COP_MAIN(main_fn_) \
int main(int argc, char *argv[]) { \
return main_fn_(argc, argv); \
}
#endif /* COP_MAIN */
#endif /* COP_MAIN_H */