568d090ebcd541f00dcfe383bf3bed36d79d653f
[reactos.git] / base / applications / cmdutils / wscript / main.c
1 /*
2 * Copyright 2010 Jacek Caban for CodeWeavers
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 */
18
19 #include "wscript.h"
20
21 #include <shellapi.h>
22 #include <activscp.h>
23
24 #ifdef _WIN64
25
26 #define IActiveScriptParse_Release IActiveScriptParse64_Release
27 #define IActiveScriptParse_InitNew IActiveScriptParse64_InitNew
28 #define IActiveScriptParse_ParseScriptText IActiveScriptParse64_ParseScriptText
29
30 #else
31
32 #define IActiveScriptParse_Release IActiveScriptParse32_Release
33 #define IActiveScriptParse_InitNew IActiveScriptParse32_InitNew
34 #define IActiveScriptParse_ParseScriptText IActiveScriptParse32_ParseScriptText
35
36 #endif
37
38 static const WCHAR wscriptW[] = {'W','S','c','r','i','p','t',0};
39 static const WCHAR wshW[] = {'W','S','H',0};
40 WCHAR scriptFullName[MAX_PATH];
41
42 ITypeInfo *host_ti;
43 ITypeInfo *arguments_ti;
44
45 static HRESULT query_interface(REFIID,void**);
46
47 static HRESULT WINAPI ActiveScriptSite_QueryInterface(IActiveScriptSite *iface,
48 REFIID riid, void **ppv)
49 {
50 return query_interface(riid, ppv);
51 }
52
53 static ULONG WINAPI ActiveScriptSite_AddRef(IActiveScriptSite *iface)
54 {
55 return 2;
56 }
57
58 static ULONG WINAPI ActiveScriptSite_Release(IActiveScriptSite *iface)
59 {
60 return 1;
61 }
62
63 static HRESULT WINAPI ActiveScriptSite_GetLCID(IActiveScriptSite *iface, LCID *plcid)
64 {
65 WINE_TRACE("()\n");
66
67 *plcid = GetUserDefaultLCID();
68 return S_OK;
69 }
70
71 static HRESULT WINAPI ActiveScriptSite_GetItemInfo(IActiveScriptSite *iface,
72 LPCOLESTR pstrName, DWORD dwReturnMask, IUnknown **ppunkItem, ITypeInfo **ppti)
73 {
74 WINE_TRACE("(%s %x %p %p)\n", wine_dbgstr_w(pstrName), dwReturnMask, ppunkItem, ppti);
75
76 if(strcmpW(pstrName, wshW) && strcmpW(pstrName, wscriptW))
77 return E_FAIL;
78
79 if(dwReturnMask & SCRIPTINFO_ITYPEINFO) {
80 ITypeInfo_AddRef(host_ti);
81 *ppti = host_ti;
82 }
83
84 if(dwReturnMask & SCRIPTINFO_IUNKNOWN) {
85 IHost_AddRef(&host_obj);
86 *ppunkItem = (IUnknown*)&host_obj;
87 }
88
89 return S_OK;
90 }
91
92 static HRESULT WINAPI ActiveScriptSite_GetDocVersionString(IActiveScriptSite *iface,
93 BSTR *pbstrVersion)
94 {
95 WINE_FIXME("()\n");
96 return E_NOTIMPL;
97 }
98
99 static HRESULT WINAPI ActiveScriptSite_OnScriptTerminate(IActiveScriptSite *iface,
100 const VARIANT *pvarResult, const EXCEPINFO *pexcepinfo)
101 {
102 WINE_FIXME("()\n");
103 return E_NOTIMPL;
104 }
105
106 static HRESULT WINAPI ActiveScriptSite_OnStateChange(IActiveScriptSite *iface,
107 SCRIPTSTATE ssScriptState)
108 {
109 WINE_TRACE("(%x)\n", ssScriptState);
110 return S_OK;
111 }
112
113 static HRESULT WINAPI ActiveScriptSite_OnScriptError(IActiveScriptSite *iface,
114 IActiveScriptError *pscripterror)
115 {
116 WINE_FIXME("()\n");
117 return E_NOTIMPL;
118 }
119
120 static HRESULT WINAPI ActiveScriptSite_OnEnterScript(IActiveScriptSite *iface)
121 {
122 WINE_TRACE("()\n");
123 return S_OK;
124 }
125
126 static HRESULT WINAPI ActiveScriptSite_OnLeaveScript(IActiveScriptSite *iface)
127 {
128 WINE_TRACE("()\n");
129 return S_OK;
130 }
131
132 static IActiveScriptSiteVtbl ActiveScriptSiteVtbl = {
133 ActiveScriptSite_QueryInterface,
134 ActiveScriptSite_AddRef,
135 ActiveScriptSite_Release,
136 ActiveScriptSite_GetLCID,
137 ActiveScriptSite_GetItemInfo,
138 ActiveScriptSite_GetDocVersionString,
139 ActiveScriptSite_OnScriptTerminate,
140 ActiveScriptSite_OnStateChange,
141 ActiveScriptSite_OnScriptError,
142 ActiveScriptSite_OnEnterScript,
143 ActiveScriptSite_OnLeaveScript
144 };
145
146 static IActiveScriptSite script_site = { &ActiveScriptSiteVtbl };
147
148 static HRESULT WINAPI ActiveScriptSiteWindow_QueryInterface(IActiveScriptSiteWindow *iface, REFIID riid, void **ppv)
149 {
150 return query_interface(riid, ppv);
151 }
152
153 static ULONG WINAPI ActiveScriptSiteWindow_AddRef(IActiveScriptSiteWindow *iface)
154 {
155 return 2;
156 }
157
158 static ULONG WINAPI ActiveScriptSiteWindow_Release(IActiveScriptSiteWindow *iface)
159 {
160 return 1;
161 }
162
163 static HRESULT WINAPI ActiveScriptSiteWindow_GetWindow(IActiveScriptSiteWindow *iface, HWND *phwnd)
164 {
165 TRACE("(%p)\n", phwnd);
166
167 *phwnd = NULL;
168 return S_OK;
169 }
170
171 static HRESULT WINAPI ActiveScriptSiteWindow_EnableModeless(IActiveScriptSiteWindow *iface, BOOL fEnable)
172 {
173 TRACE("(%x)\n", fEnable);
174 return S_OK;
175 }
176
177 static const IActiveScriptSiteWindowVtbl ActiveScriptSiteWindowVtbl = {
178 ActiveScriptSiteWindow_QueryInterface,
179 ActiveScriptSiteWindow_AddRef,
180 ActiveScriptSiteWindow_Release,
181 ActiveScriptSiteWindow_GetWindow,
182 ActiveScriptSiteWindow_EnableModeless
183 };
184
185 static IActiveScriptSiteWindow script_site_window = { &ActiveScriptSiteWindowVtbl };
186
187 static HRESULT query_interface(REFIID riid, void **ppv)
188 {
189 if(IsEqualGUID(riid, &IID_IUnknown)) {
190 TRACE("(IID_IUnknown %p)\n", ppv);
191 *ppv = &script_site;
192 }else if(IsEqualGUID(riid, &IID_IActiveScriptSite)) {
193 TRACE("(IID_IActiveScriptSite %p)\n", ppv);
194 *ppv = &script_site;
195 }else if(IsEqualGUID(riid, &IID_IActiveScriptSiteWindow)) {
196 TRACE("(IID_IActiveScriptSiteWindow %p)\n", ppv);
197 *ppv = &script_site_window;
198 }else {
199 *ppv = NULL;
200 TRACE("(%s %p)\n", wine_dbgstr_guid(riid), ppv);
201 return E_NOINTERFACE;
202 }
203
204 IUnknown_AddRef((IUnknown*)*ppv);
205 return S_OK;
206 }
207
208 static BOOL load_typelib(void)
209 {
210 ITypeLib *typelib;
211 HRESULT hres;
212
213 static const WCHAR wscript_exeW[] = {'w','s','c','r','i','p','t','.','e','x','e',0};
214
215 hres = LoadTypeLib(wscript_exeW, &typelib);
216 if(FAILED(hres))
217 return FALSE;
218
219 hres = ITypeLib_GetTypeInfoOfGuid(typelib, &IID_IHost, &host_ti);
220 if(SUCCEEDED(hres))
221 hres = ITypeLib_GetTypeInfoOfGuid(typelib, &IID_IArguments2, &arguments_ti);
222
223 ITypeLib_Release(typelib);
224 return SUCCEEDED(hres);
225 }
226
227 static BOOL get_engine_clsid(const WCHAR *ext, CLSID *clsid)
228 {
229 WCHAR fileid[64], progid[64];
230 DWORD res;
231 LONG size;
232 HKEY hkey;
233 HRESULT hres;
234
235 static const WCHAR script_engineW[] =
236 {'\\','S','c','r','i','p','t','E','n','g','i','n','e',0};
237
238 res = RegOpenKeyW(HKEY_CLASSES_ROOT, ext, &hkey);
239 if(res != ERROR_SUCCESS)
240 return FALSE;
241
242 size = sizeof(fileid)/sizeof(WCHAR);
243 res = RegQueryValueW(hkey, NULL, fileid, &size);
244 RegCloseKey(hkey);
245 if(res != ERROR_SUCCESS)
246 return FALSE;
247
248 WINE_TRACE("fileid is %s\n", wine_dbgstr_w(fileid));
249
250 strcatW(fileid, script_engineW);
251 res = RegOpenKeyW(HKEY_CLASSES_ROOT, fileid, &hkey);
252 if(res != ERROR_SUCCESS)
253 return FALSE;
254
255 size = sizeof(progid)/sizeof(WCHAR);
256 res = RegQueryValueW(hkey, NULL, progid, &size);
257 RegCloseKey(hkey);
258 if(res != ERROR_SUCCESS)
259 return FALSE;
260
261 WINE_TRACE("ProgID is %s\n", wine_dbgstr_w(progid));
262
263 hres = CLSIDFromProgID(progid, clsid);
264 return SUCCEEDED(hres);
265 }
266
267 static BOOL create_engine(CLSID *clsid, IActiveScript **script_ret,
268 IActiveScriptParse **parser)
269 {
270 IActiveScript *script;
271 IUnknown *unk;
272 HRESULT hres;
273
274 hres = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
275 &IID_IUnknown, (void**)&unk);
276 if(FAILED(hres))
277 return FALSE;
278
279 hres = IUnknown_QueryInterface(unk, &IID_IActiveScript, (void**)&script);
280 IUnknown_Release(unk);
281 if(FAILED(hres))
282 return FALSE;
283
284 hres = IActiveScript_QueryInterface(script, &IID_IActiveScriptParse, (void**)parser);
285 if(FAILED(hres)) {
286 IActiveScript_Release(script);
287 return FALSE;
288 }
289
290 *script_ret = script;
291 return TRUE;
292 }
293
294 static BOOL init_engine(IActiveScript *script, IActiveScriptParse *parser)
295 {
296 HRESULT hres;
297
298 if(!load_typelib())
299 return FALSE;
300
301 hres = IActiveScript_SetScriptSite(script, &script_site);
302 if(FAILED(hres))
303 return FALSE;
304
305 hres = IActiveScriptParse_InitNew(parser);
306 if(FAILED(hres))
307 return FALSE;
308
309 hres = IActiveScript_AddNamedItem(script, wscriptW, SCRIPTITEM_ISVISIBLE);
310 if(FAILED(hres))
311 return FALSE;
312
313 hres = IActiveScript_AddNamedItem(script, wshW, SCRIPTITEM_ISVISIBLE);
314 if(FAILED(hres))
315 return FALSE;
316
317 hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_INITIALIZED);
318 return SUCCEEDED(hres);
319 }
320
321 static BSTR get_script_str(const WCHAR *filename)
322 {
323 const char *file_map;
324 HANDLE file, map;
325 DWORD size, len;
326 BSTR ret;
327
328 file = CreateFileW(filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
329 if(file == INVALID_HANDLE_VALUE)
330 return NULL;
331
332 size = GetFileSize(file, NULL);
333 map = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, NULL);
334 CloseHandle(file);
335 if(map == INVALID_HANDLE_VALUE)
336 return NULL;
337
338 file_map = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
339 CloseHandle(map);
340 if(!file_map)
341 return NULL;
342
343 len = MultiByteToWideChar(CP_ACP, 0, file_map, size, NULL, 0);
344 ret = SysAllocStringLen(NULL, len);
345 MultiByteToWideChar(CP_ACP, 0, file_map, size, ret, len);
346
347 UnmapViewOfFile(file_map);
348 return ret;
349 }
350
351 static void run_script(const WCHAR *filename, IActiveScript *script, IActiveScriptParse *parser)
352 {
353 BSTR text;
354 HRESULT hres;
355
356 text = get_script_str(filename);
357 if(!text) {
358 WINE_FIXME("Could not get script text\n");
359 return;
360 }
361
362 hres = IActiveScriptParse_ParseScriptText(parser, text, NULL, NULL, NULL, 1, 1,
363 SCRIPTTEXT_HOSTMANAGESSOURCE|SCRIPTITEM_ISVISIBLE, NULL, NULL);
364 SysFreeString(text);
365 if(FAILED(hres)) {
366 WINE_FIXME("ParseScriptText failed: %08x\n", hres);
367 return;
368 }
369
370 hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_STARTED);
371 if(FAILED(hres))
372 WINE_FIXME("SetScriptState failed: %08x\n", hres);
373 }
374
375 static BOOL set_host_properties(const WCHAR *prop)
376 {
377 static const WCHAR nologoW[] = {'n','o','l','o','g','o',0};
378 static const WCHAR iactive[] = {'i',0};
379 static const WCHAR batch[] = {'b',0};
380
381 if(*prop == '/') {
382 ++prop;
383 if(*prop == '/')
384 ++prop;
385 }
386 else
387 ++prop;
388
389 if(strcmpiW(prop, iactive) == 0)
390 wshInteractive = VARIANT_TRUE;
391 else if(strcmpiW(prop, batch) == 0)
392 wshInteractive = VARIANT_FALSE;
393 else if(strcmpiW(prop, nologoW) == 0)
394 WINE_FIXME("ignored %s switch\n", debugstr_w(nologoW));
395 else
396 {
397 WINE_FIXME("unsupported switch %s\n", debugstr_w(prop));
398 return FALSE;
399 }
400 return TRUE;
401 }
402
403 int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPWSTR cmdline, int cmdshow)
404 {
405 WCHAR *ext, *filepart, *filename = NULL;
406 IActiveScriptParse *parser;
407 IActiveScript *script;
408 WCHAR **argv;
409 CLSID clsid;
410 int argc, i;
411 DWORD res;
412
413 WINE_TRACE("(%p %p %s %x)\n", hInst, hPrevInst, wine_dbgstr_w(cmdline), cmdshow);
414
415 argv = CommandLineToArgvW(GetCommandLineW(), &argc);
416 if(!argv)
417 return 1;
418
419 for(i=1; i<argc; i++) {
420 if(*argv[i] == '/' || *argv[i] == '-') {
421 if(!set_host_properties(argv[i]))
422 return 1;
423 }else {
424 filename = argv[i];
425 argums = argv+i+1;
426 numOfArgs = argc-i-1;
427 break;
428 }
429 }
430
431 if(!filename) {
432 WINE_FIXME("No file name specified\n");
433 return 1;
434 }
435 res = GetFullPathNameW(filename, sizeof(scriptFullName)/sizeof(WCHAR), scriptFullName, &filepart);
436 if(!res || res > sizeof(scriptFullName)/sizeof(WCHAR))
437 return 1;
438
439 ext = strrchrW(filepart, '.');
440 if(!ext || !get_engine_clsid(ext, &clsid)) {
441 WINE_FIXME("Could not find engine for %s\n", wine_dbgstr_w(ext));
442 return 1;
443 }
444
445 CoInitialize(NULL);
446
447 if(!create_engine(&clsid, &script, &parser)) {
448 WINE_FIXME("Could not create script engine\n");
449 CoUninitialize();
450 return 1;
451 }
452
453 if(init_engine(script, parser)) {
454 run_script(filename, script, parser);
455 IActiveScript_Close(script);
456 ITypeInfo_Release(host_ti);
457 }else {
458 WINE_FIXME("Script initialization failed\n");
459 }
460
461 IActiveScript_Release(script);
462 IActiveScriptParse_Release(parser);
463
464 CoUninitialize();
465
466 return 0;
467 }