comctl32/tests: Prevent a possible nullptr dereference.
[wine.git] / programs / wscript / main.c
blob3537f9d7241ae6ee82dd919fdef7ca06af19db5f
1 /*
2 * Copyright 2010 Jacek Caban for CodeWeavers
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.
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.
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
19 #include <stdarg.h>
21 #define COBJMACROS
23 #include <windef.h>
24 #include <winbase.h>
25 #include <winreg.h>
26 #include <ole2.h>
27 #include <shellapi.h>
28 #include <activscp.h>
29 #include <initguid.h>
31 #include "wscript.h"
33 #include <wine/debug.h>
34 #include <wine/unicode.h>
36 #ifdef _WIN64
38 #define IActiveScriptParse_Release IActiveScriptParse64_Release
39 #define IActiveScriptParse_InitNew IActiveScriptParse64_InitNew
40 #define IActiveScriptParse_ParseScriptText IActiveScriptParse64_ParseScriptText
42 #else
44 #define IActiveScriptParse_Release IActiveScriptParse32_Release
45 #define IActiveScriptParse_InitNew IActiveScriptParse32_InitNew
46 #define IActiveScriptParse_ParseScriptText IActiveScriptParse32_ParseScriptText
48 #endif
50 WINE_DEFAULT_DEBUG_CHANNEL(wscript);
52 static const WCHAR wscriptW[] = {'W','S','c','r','i','p','t',0};
53 static const WCHAR wshW[] = {'W','S','H',0};
54 WCHAR scriptFullName[MAX_PATH];
56 ITypeInfo *host_ti;
57 ITypeInfo *arguments_ti;
59 static HRESULT query_interface(REFIID,void**);
61 static HRESULT WINAPI ActiveScriptSite_QueryInterface(IActiveScriptSite *iface,
62 REFIID riid, void **ppv)
64 return query_interface(riid, ppv);
67 static ULONG WINAPI ActiveScriptSite_AddRef(IActiveScriptSite *iface)
69 return 2;
72 static ULONG WINAPI ActiveScriptSite_Release(IActiveScriptSite *iface)
74 return 1;
77 static HRESULT WINAPI ActiveScriptSite_GetLCID(IActiveScriptSite *iface, LCID *plcid)
79 WINE_TRACE("()\n");
81 *plcid = GetUserDefaultLCID();
82 return S_OK;
85 static HRESULT WINAPI ActiveScriptSite_GetItemInfo(IActiveScriptSite *iface,
86 LPCOLESTR pstrName, DWORD dwReturnMask, IUnknown **ppunkItem, ITypeInfo **ppti)
88 WINE_TRACE("(%s %x %p %p)\n", wine_dbgstr_w(pstrName), dwReturnMask, ppunkItem, ppti);
90 if(strcmpW(pstrName, wshW) && strcmpW(pstrName, wscriptW))
91 return E_FAIL;
93 if(dwReturnMask & SCRIPTINFO_ITYPEINFO) {
94 ITypeInfo_AddRef(host_ti);
95 *ppti = host_ti;
98 if(dwReturnMask & SCRIPTINFO_IUNKNOWN) {
99 IHost_AddRef(&host_obj);
100 *ppunkItem = (IUnknown*)&host_obj;
103 return S_OK;
106 static HRESULT WINAPI ActiveScriptSite_GetDocVersionString(IActiveScriptSite *iface,
107 BSTR *pbstrVersion)
109 WINE_FIXME("()\n");
110 return E_NOTIMPL;
113 static HRESULT WINAPI ActiveScriptSite_OnScriptTerminate(IActiveScriptSite *iface,
114 const VARIANT *pvarResult, const EXCEPINFO *pexcepinfo)
116 WINE_FIXME("()\n");
117 return E_NOTIMPL;
120 static HRESULT WINAPI ActiveScriptSite_OnStateChange(IActiveScriptSite *iface,
121 SCRIPTSTATE ssScriptState)
123 WINE_TRACE("(%x)\n", ssScriptState);
124 return S_OK;
127 static HRESULT WINAPI ActiveScriptSite_OnScriptError(IActiveScriptSite *iface,
128 IActiveScriptError *pscripterror)
130 WINE_FIXME("()\n");
131 return E_NOTIMPL;
134 static HRESULT WINAPI ActiveScriptSite_OnEnterScript(IActiveScriptSite *iface)
136 WINE_TRACE("()\n");
137 return S_OK;
140 static HRESULT WINAPI ActiveScriptSite_OnLeaveScript(IActiveScriptSite *iface)
142 WINE_TRACE("()\n");
143 return S_OK;
146 static IActiveScriptSiteVtbl ActiveScriptSiteVtbl = {
147 ActiveScriptSite_QueryInterface,
148 ActiveScriptSite_AddRef,
149 ActiveScriptSite_Release,
150 ActiveScriptSite_GetLCID,
151 ActiveScriptSite_GetItemInfo,
152 ActiveScriptSite_GetDocVersionString,
153 ActiveScriptSite_OnScriptTerminate,
154 ActiveScriptSite_OnStateChange,
155 ActiveScriptSite_OnScriptError,
156 ActiveScriptSite_OnEnterScript,
157 ActiveScriptSite_OnLeaveScript
160 static IActiveScriptSite script_site = { &ActiveScriptSiteVtbl };
162 static HRESULT WINAPI ActiveScriptSiteWindow_QueryInterface(IActiveScriptSiteWindow *iface, REFIID riid, void **ppv)
164 return query_interface(riid, ppv);
167 static ULONG WINAPI ActiveScriptSiteWindow_AddRef(IActiveScriptSiteWindow *iface)
169 return 2;
172 static ULONG WINAPI ActiveScriptSiteWindow_Release(IActiveScriptSiteWindow *iface)
174 return 1;
177 static HRESULT WINAPI ActiveScriptSiteWindow_GetWindow(IActiveScriptSiteWindow *iface, HWND *phwnd)
179 TRACE("(%p)\n", phwnd);
181 *phwnd = NULL;
182 return S_OK;
185 static HRESULT WINAPI ActiveScriptSiteWindow_EnableModeless(IActiveScriptSiteWindow *iface, BOOL fEnable)
187 TRACE("(%x)\n", fEnable);
188 return S_OK;
191 static const IActiveScriptSiteWindowVtbl ActiveScriptSiteWindowVtbl = {
192 ActiveScriptSiteWindow_QueryInterface,
193 ActiveScriptSiteWindow_AddRef,
194 ActiveScriptSiteWindow_Release,
195 ActiveScriptSiteWindow_GetWindow,
196 ActiveScriptSiteWindow_EnableModeless
199 static IActiveScriptSiteWindow script_site_window = { &ActiveScriptSiteWindowVtbl };
201 static HRESULT query_interface(REFIID riid, void **ppv)
203 if(IsEqualGUID(riid, &IID_IUnknown)) {
204 TRACE("(IID_IUnknown %p)\n", ppv);
205 *ppv = &script_site;
206 }else if(IsEqualGUID(riid, &IID_IActiveScriptSite)) {
207 TRACE("(IID_IActiveScriptSite %p)\n", ppv);
208 *ppv = &script_site;
209 }else if(IsEqualGUID(riid, &IID_IActiveScriptSiteWindow)) {
210 TRACE("(IID_IActiveScriptSiteWindow %p)\n", ppv);
211 *ppv = &script_site_window;
212 }else {
213 *ppv = NULL;
214 TRACE("(%s %p)\n", wine_dbgstr_guid(riid), ppv);
215 return E_NOINTERFACE;
218 IUnknown_AddRef((IUnknown*)*ppv);
219 return S_OK;
222 static BOOL load_typelib(void)
224 ITypeLib *typelib;
225 HRESULT hres;
227 static const WCHAR wscript_exeW[] = {'w','s','c','r','i','p','t','.','e','x','e',0};
229 hres = LoadTypeLib(wscript_exeW, &typelib);
230 if(FAILED(hres))
231 return FALSE;
233 hres = ITypeLib_GetTypeInfoOfGuid(typelib, &IID_IHost, &host_ti);
234 if(SUCCEEDED(hres))
235 hres = ITypeLib_GetTypeInfoOfGuid(typelib, &IID_IArguments2, &arguments_ti);
237 ITypeLib_Release(typelib);
238 return SUCCEEDED(hres);
241 static BOOL get_engine_clsid(const WCHAR *ext, CLSID *clsid)
243 WCHAR fileid[64], progid[64];
244 DWORD res;
245 LONG size;
246 HKEY hkey;
247 HRESULT hres;
249 static const WCHAR script_engineW[] =
250 {'\\','S','c','r','i','p','t','E','n','g','i','n','e',0};
252 res = RegOpenKeyW(HKEY_CLASSES_ROOT, ext, &hkey);
253 if(res != ERROR_SUCCESS)
254 return FALSE;
256 size = sizeof(fileid)/sizeof(WCHAR);
257 res = RegQueryValueW(hkey, NULL, fileid, &size);
258 RegCloseKey(hkey);
259 if(res != ERROR_SUCCESS)
260 return FALSE;
262 WINE_TRACE("fileid is %s\n", wine_dbgstr_w(fileid));
264 strcatW(fileid, script_engineW);
265 res = RegOpenKeyW(HKEY_CLASSES_ROOT, fileid, &hkey);
266 if(res != ERROR_SUCCESS)
267 return FALSE;
269 size = sizeof(progid)/sizeof(WCHAR);
270 res = RegQueryValueW(hkey, NULL, progid, &size);
271 RegCloseKey(hkey);
272 if(res != ERROR_SUCCESS)
273 return FALSE;
275 WINE_TRACE("ProgID is %s\n", wine_dbgstr_w(progid));
277 hres = CLSIDFromProgID(progid, clsid);
278 return SUCCEEDED(hres);
281 static BOOL create_engine(CLSID *clsid, IActiveScript **script_ret,
282 IActiveScriptParse **parser)
284 IActiveScript *script;
285 IUnknown *unk;
286 HRESULT hres;
288 hres = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
289 &IID_IUnknown, (void**)&unk);
290 if(FAILED(hres))
291 return FALSE;
293 hres = IUnknown_QueryInterface(unk, &IID_IActiveScript, (void**)&script);
294 IUnknown_Release(unk);
295 if(FAILED(hres))
296 return FALSE;
298 hres = IActiveScript_QueryInterface(script, &IID_IActiveScriptParse, (void**)parser);
299 if(FAILED(hres)) {
300 IActiveScript_Release(script);
301 return FALSE;
304 *script_ret = script;
305 return TRUE;
308 static BOOL init_engine(IActiveScript *script, IActiveScriptParse *parser)
310 HRESULT hres;
312 if(!load_typelib())
313 return FALSE;
315 hres = IActiveScript_SetScriptSite(script, &script_site);
316 if(FAILED(hres))
317 return FALSE;
319 hres = IActiveScriptParse_InitNew(parser);
320 if(FAILED(hres))
321 return FALSE;
323 hres = IActiveScript_AddNamedItem(script, wscriptW, SCRIPTITEM_ISVISIBLE);
324 if(FAILED(hres))
325 return FALSE;
327 hres = IActiveScript_AddNamedItem(script, wshW, SCRIPTITEM_ISVISIBLE);
328 if(FAILED(hres))
329 return FALSE;
331 hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_INITIALIZED);
332 return SUCCEEDED(hres);
335 static BSTR get_script_str(const WCHAR *filename)
337 const char *file_map;
338 HANDLE file, map;
339 DWORD size, len;
340 BSTR ret;
342 file = CreateFileW(filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
343 if(file == INVALID_HANDLE_VALUE)
344 return NULL;
346 size = GetFileSize(file, NULL);
347 map = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, NULL);
348 CloseHandle(file);
349 if(map == INVALID_HANDLE_VALUE)
350 return NULL;
352 file_map = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
353 CloseHandle(map);
354 if(!file_map)
355 return NULL;
357 len = MultiByteToWideChar(CP_ACP, 0, file_map, size, NULL, 0);
358 ret = SysAllocStringLen(NULL, len);
359 MultiByteToWideChar(CP_ACP, 0, file_map, size, ret, len);
361 UnmapViewOfFile(file_map);
362 return ret;
365 static void run_script(const WCHAR *filename, IActiveScript *script, IActiveScriptParse *parser)
367 BSTR text;
368 HRESULT hres;
370 text = get_script_str(filename);
371 if(!text) {
372 WINE_FIXME("Could not get script text\n");
373 return;
376 hres = IActiveScriptParse_ParseScriptText(parser, text, NULL, NULL, NULL, 1, 1,
377 SCRIPTTEXT_HOSTMANAGESSOURCE|SCRIPTITEM_ISVISIBLE, NULL, NULL);
378 SysFreeString(text);
379 if(FAILED(hres)) {
380 WINE_FIXME("ParseScriptText failed: %08x\n", hres);
381 return;
384 hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_STARTED);
385 if(FAILED(hres))
386 WINE_FIXME("SetScriptState failed: %08x\n", hres);
389 static BOOL set_host_properties(const WCHAR *prop)
391 static const WCHAR nologoW[] = {'n','o','l','o','g','o',0};
392 static const WCHAR iactive[] = {'i',0};
393 static const WCHAR batch[] = {'b',0};
395 if(*prop == '/') {
396 ++prop;
397 if(*prop == '/')
398 ++prop;
400 else
401 ++prop;
403 if(strcmpiW(prop, iactive) == 0)
404 wshInteractive = VARIANT_TRUE;
405 else if(strcmpiW(prop, batch) == 0)
406 wshInteractive = VARIANT_FALSE;
407 else if(strcmpiW(prop, nologoW) == 0)
408 WINE_FIXME("ignored %s switch\n", debugstr_w(nologoW));
409 else
411 WINE_FIXME("unsupported switch %s\n", debugstr_w(prop));
412 return FALSE;
414 return TRUE;
417 int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPWSTR cmdline, int cmdshow)
419 WCHAR *ext, *filepart, *filename = NULL;
420 IActiveScriptParse *parser;
421 IActiveScript *script;
422 WCHAR **argv;
423 CLSID clsid;
424 int argc, i;
425 DWORD res;
427 WINE_TRACE("(%p %p %s %x)\n", hInst, hPrevInst, wine_dbgstr_w(cmdline), cmdshow);
429 argv = CommandLineToArgvW(GetCommandLineW(), &argc);
430 if(!argv)
431 return 1;
433 for(i=1; i<argc; i++) {
434 if(*argv[i] == '/' || *argv[i] == '-') {
435 if(!set_host_properties(argv[i]))
436 return 1;
437 }else {
438 filename = argv[i];
439 argums = argv+i+1;
440 numOfArgs = argc-i-1;
441 break;
445 if(!filename) {
446 WINE_FIXME("No file name specified\n");
447 return 1;
449 res = GetFullPathNameW(filename, sizeof(scriptFullName)/sizeof(WCHAR), scriptFullName, &filepart);
450 if(!res || res > sizeof(scriptFullName)/sizeof(WCHAR))
451 return 1;
453 ext = strrchrW(filepart, '.');
454 if(!ext || !get_engine_clsid(ext, &clsid)) {
455 WINE_FIXME("Could not find engine for %s\n", wine_dbgstr_w(ext));
456 return 1;
459 CoInitialize(NULL);
461 if(!create_engine(&clsid, &script, &parser)) {
462 WINE_FIXME("Could not create script engine\n");
463 CoUninitialize();
464 return 1;
467 if(init_engine(script, parser)) {
468 run_script(filename, script, parser);
469 IActiveScript_Close(script);
470 ITypeInfo_Release(host_ti);
471 }else {
472 WINE_FIXME("Script initialization failed\n");
475 IActiveScript_Release(script);
476 IActiveScriptParse_Release(parser);
478 CoUninitialize();
480 return 0;