]> gitweb.factorcode.org Git - factor.git/blob - vm/main-windows-ce.cpp
Use ParseCommandLineArgvw() on Windows again, instead of hand-rolled parser. Update...
[factor.git] / vm / main-windows-ce.cpp
1 #include "master.hpp"
2
3 /* 
4         Windows argument parsing ported to work on
5         int main(int argc, wchar_t **argv).
6
7         Based on MinGW's public domain char** version.
8 */
9
10 VM_C_API int parse_tokens(wchar_t *string, wchar_t ***tokens, int length)
11 {
12         /* Extract whitespace- and quotes- delimited tokens from the given string
13            and put them into the tokens array. Returns number of tokens
14            extracted. Length specifies the current size of tokens[].
15            THIS METHOD MODIFIES string.  */
16
17         const wchar_t *whitespace = L" \t\r\n";
18         wchar_t *tokenEnd = 0;
19         const wchar_t *quoteCharacters = L"\"\'";
20         wchar_t *end = string + wcslen(string);
21
22         if (string == NULL)
23                 return length;
24
25         while (1)
26         {
27                 const wchar_t *q;
28                 /* Skip over initial whitespace.  */
29                 string += wcsspn(string, whitespace);
30                 if (*string == '\0')
31                         break;
32
33                 for (q = quoteCharacters; *q; ++q)
34                 {
35                         if (*string == *q)
36                                 break;
37                 }
38                 if (*q)
39                 {
40                         /* Token is quoted.  */
41                         wchar_t quote = *string++;
42                         tokenEnd = wcschr(string, quote);
43                         /* If there is no endquote, the token is the rest of the string.  */
44                         if (!tokenEnd)
45                                 tokenEnd = end;
46                 }
47                 else
48                 {
49                         tokenEnd = string + wcscspn(string, whitespace);
50                 }
51
52                 *tokenEnd = '\0';
53
54                 {
55                         wchar_t **new_tokens;
56                         int newlen = length + 1;
57                         new_tokens = (wchar_t **)realloc (*tokens, sizeof (wchar_t**) * newlen);
58                         if (!new_tokens)
59                         {
60                                 /* Out of memory.  */
61                                 return -1;
62                         }
63
64                         *tokens = new_tokens;
65                         (*tokens)[length] = string;
66                         length = newlen;
67                 }
68                 if (tokenEnd == end)
69                         break;
70                 string = tokenEnd + 1;
71         }
72         return length;
73 }
74
75 VM_C_API void parse_args(int *argc, wchar_t ***argv, wchar_t *cmdlinePtrW)
76 {
77         int cmdlineLen = 0;
78
79         if (!cmdlinePtrW)
80                 cmdlineLen = 0;
81         else
82                 cmdlineLen = wcslen(cmdlinePtrW);
83
84         /* gets realloc()'d later */
85         *argc = 0;
86         *argv = (wchar_t **)malloc (sizeof (wchar_t**));
87
88         if (!*argv)
89                 ExitProcess(1);
90
91 #ifdef WINCE
92         wchar_t cmdnameBufW[MAX_UNICODE_PATH];
93
94         /* argv[0] is the path of invoked program - get this from CE.  */
95         cmdnameBufW[0] = 0;
96         GetModuleFileNameW(NULL, cmdnameBufW, sizeof (cmdnameBufW)/sizeof (cmdnameBufW[0]));
97
98         (*argv)[0] = wcsdup(cmdnameBufW);
99         if(!(*argv[0]))
100                 ExitProcess(1);
101         /* Add one to account for argv[0] */
102         (*argc)++;
103 #endif
104
105         if (cmdlineLen > 0)
106         {
107                 wchar_t *string = wcsdup(cmdlinePtrW);
108                 if(!string)
109                         ExitProcess(1);
110                 *argc = parse_tokens(string, argv, *argc);
111                 if (*argc < 0)
112                         ExitProcess(1);
113         }
114         (*argv)[*argc] = 0;
115         return;
116 }
117
118 int WINAPI WinMain(
119         HINSTANCE hInstance,
120         HINSTANCE hPrevInstance,
121         LPWSTR lpCmdLine,
122         int nCmdShow)
123 {
124         int __argc;
125         wchar_t **__argv;
126         factor::parse_args(&__argc, &__argv, lpCmdLine);
127         factor::init_globals();
128         factor::start_standalone_factor(__argc,(LPWSTR*)__argv);
129
130         // memory leak from malloc, wcsdup
131         return 0;
132 }