make-image can now generate images with Unicode strings
[factor/jcg.git] / vm / main-windows-ce.c
blobfc04d455dbe2c4f51739dc022594f167ac0524fd
1 #include "master.h"
3 /*
4 Windows CE argument parsing ported to work on
5 int main(int argc, wchar_t **argv).
7 This would not be necessary if Windows CE had CommandLineToArgvW.
9 Based on MinGW's public domain char** version.
13 int __argc;
14 wchar_t **__argv;
16 static int
17 parse_tokens(wchar_t* string, wchar_t*** tokens, int length)
19 /* Extract whitespace- and quotes- delimited tokens from the given string
20 and put them into the tokens array. Returns number of tokens
21 extracted. Length specifies the current size of tokens[].
22 THIS METHOD MODIFIES string. */
24 const wchar_t* whitespace = L" \t\r\n";
25 wchar_t* tokenEnd = 0;
26 const wchar_t* quoteCharacters = L"\"\'";
27 wchar_t *end = string + wcslen(string);
29 if (string == NULL)
30 return length;
32 while (1)
34 const wchar_t* q;
35 /* Skip over initial whitespace. */
36 string += wcsspn(string, whitespace);
37 if (*string == '\0')
38 break;
40 for (q = quoteCharacters; *q; ++q)
42 if (*string == *q)
43 break;
45 if (*q)
47 /* Token is quoted. */
48 wchar_t quote = *string++;
49 tokenEnd = wcschr(string, quote);
50 /* If there is no endquote, the token is the rest of the string. */
51 if (!tokenEnd)
52 tokenEnd = end;
54 else
56 tokenEnd = string + wcscspn(string, whitespace);
59 *tokenEnd = '\0';
62 wchar_t** new_tokens;
63 int newlen = length + 1;
64 new_tokens = realloc (*tokens, sizeof (wchar_t**) * newlen);
65 if (!new_tokens)
67 /* Out of memory. */
68 return -1;
71 *tokens = new_tokens;
72 (*tokens)[length] = string;
73 length = newlen;
75 if (tokenEnd == end)
76 break;
77 string = tokenEnd + 1;
79 return length;
82 static void
83 parse_args(int *argc, wchar_t ***argv, wchar_t *cmdlinePtrW)
85 wchar_t cmdnameBufW[MAX_UNICODE_PATH];
86 int cmdlineLen = 0;
87 int modlen;
89 /* argv[0] is the path of invoked program - get this from CE. */
90 cmdnameBufW[0] = 0;
91 modlen = GetModuleFileNameW(NULL, cmdnameBufW, sizeof (cmdnameBufW)/sizeof (cmdnameBufW[0]));
93 if (!cmdlinePtrW)
94 cmdlineLen = 0;
95 else
96 cmdlineLen = wcslen(cmdlinePtrW);
98 /* gets realloc()'d later */
99 *argv = malloc (sizeof (wchar_t**) * 1);
100 if (!*argv)
101 ExitProcess(-1);
103 (*argv)[0] = wcsdup(cmdnameBufW);
104 if(!(*argv[0]))
105 ExitProcess(-1);
106 /* Add one to account for argv[0] */
107 (*argc)++;
109 if (cmdlineLen > 0)
111 wchar_t* argv1 = (*argv)[0] + wcslen((*argv)[0]) + 1;
112 argv1 = wcsdup(cmdlinePtrW);
113 if(!argv1)
114 ExitProcess(-1);
115 *argc = parse_tokens(argv1, argv, 1);
116 if (*argc < 0)
117 ExitProcess(-1);
119 (*argv)[*argc] = 0;
120 return;
123 int WINAPI
124 WinMain(
125 HINSTANCE hInstance,
126 HINSTANCE hPrevInstance,
127 LPWSTR lpCmdLine,
128 int nCmdShow)
130 parse_args(&__argc, &__argv, lpCmdLine);
131 start_standalone_factor(__argc,(LPWSTR*)__argv);
132 // memory leak from malloc, wcsdup
133 return 0;