makefiles: Generate explicit build rules for bison files from makedep.
[wine.git] / programs / wscript / main.c
blob3d97bdef650579ab48ca272d0d4115a6f8e8c925
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 WINAPI ActiveScriptSite_QueryInterface(IActiveScriptSite *iface,
60 REFIID riid, void **ppv)
62 if(IsEqualGUID(riid, &IID_IUnknown)) {
63 WINE_TRACE("(IID_IUnknown %p)\n", ppv);
64 *ppv = iface;
65 }else if(IsEqualGUID(riid, &IID_IActiveScriptSite)) {
66 WINE_TRACE("(IID_IActiveScriptSite %p)\n", ppv);
67 *ppv = iface;
68 }else {
69 *ppv = NULL;
70 WINE_TRACE("(%s %p)\n", wine_dbgstr_guid(riid), ppv);
71 return E_NOINTERFACE;
74 IUnknown_AddRef((IUnknown*)*ppv);
75 return S_OK;
78 static ULONG WINAPI ActiveScriptSite_AddRef(IActiveScriptSite *iface)
80 return 2;
83 static ULONG WINAPI ActiveScriptSite_Release(IActiveScriptSite *iface)
85 return 1;
88 static HRESULT WINAPI ActiveScriptSite_GetLCID(IActiveScriptSite *iface, LCID *plcid)
90 WINE_TRACE("()\n");
92 *plcid = GetUserDefaultLCID();
93 return S_OK;
96 static HRESULT WINAPI ActiveScriptSite_GetItemInfo(IActiveScriptSite *iface,
97 LPCOLESTR pstrName, DWORD dwReturnMask, IUnknown **ppunkItem, ITypeInfo **ppti)
99 WINE_TRACE("(%s %x %p %p)\n", wine_dbgstr_w(pstrName), dwReturnMask, ppunkItem, ppti);
101 if(strcmpW(pstrName, wshW) && strcmpW(pstrName, wscriptW))
102 return E_FAIL;
104 if(dwReturnMask & SCRIPTINFO_ITYPEINFO) {
105 ITypeInfo_AddRef(host_ti);
106 *ppti = host_ti;
109 if(dwReturnMask & SCRIPTINFO_IUNKNOWN) {
110 IHost_AddRef(&host_obj);
111 *ppunkItem = (IUnknown*)&host_obj;
114 return S_OK;
117 static HRESULT WINAPI ActiveScriptSite_GetDocVersionString(IActiveScriptSite *iface,
118 BSTR *pbstrVersion)
120 WINE_FIXME("()\n");
121 return E_NOTIMPL;
124 static HRESULT WINAPI ActiveScriptSite_OnScriptTerminate(IActiveScriptSite *iface,
125 const VARIANT *pvarResult, const EXCEPINFO *pexcepinfo)
127 WINE_FIXME("()\n");
128 return E_NOTIMPL;
131 static HRESULT WINAPI ActiveScriptSite_OnStateChange(IActiveScriptSite *iface,
132 SCRIPTSTATE ssScriptState)
134 WINE_TRACE("(%x)\n", ssScriptState);
135 return S_OK;
138 static HRESULT WINAPI ActiveScriptSite_OnScriptError(IActiveScriptSite *iface,
139 IActiveScriptError *pscripterror)
141 WINE_FIXME("()\n");
142 return E_NOTIMPL;
145 static HRESULT WINAPI ActiveScriptSite_OnEnterScript(IActiveScriptSite *iface)
147 WINE_TRACE("()\n");
148 return S_OK;
151 static HRESULT WINAPI ActiveScriptSite_OnLeaveScript(IActiveScriptSite *iface)
153 WINE_TRACE("()\n");
154 return S_OK;
157 static IActiveScriptSiteVtbl ActiveScriptSiteVtbl = {
158 ActiveScriptSite_QueryInterface,
159 ActiveScriptSite_AddRef,
160 ActiveScriptSite_Release,
161 ActiveScriptSite_GetLCID,
162 ActiveScriptSite_GetItemInfo,
163 ActiveScriptSite_GetDocVersionString,
164 ActiveScriptSite_OnScriptTerminate,
165 ActiveScriptSite_OnStateChange,
166 ActiveScriptSite_OnScriptError,
167 ActiveScriptSite_OnEnterScript,
168 ActiveScriptSite_OnLeaveScript
171 IActiveScriptSite script_site = { &ActiveScriptSiteVtbl };
173 static BOOL load_typelib(void)
175 ITypeLib *typelib;
176 HRESULT hres;
178 static const WCHAR wscript_exeW[] = {'w','s','c','r','i','p','t','.','e','x','e',0};
180 hres = LoadTypeLib(wscript_exeW, &typelib);
181 if(FAILED(hres))
182 return FALSE;
184 hres = ITypeLib_GetTypeInfoOfGuid(typelib, &IID_IHost, &host_ti);
185 if(SUCCEEDED(hres))
186 hres = ITypeLib_GetTypeInfoOfGuid(typelib, &IID_IArguments2, &arguments_ti);
188 ITypeLib_Release(typelib);
189 return SUCCEEDED(hres);
192 static BOOL get_engine_clsid(const WCHAR *ext, CLSID *clsid)
194 WCHAR fileid[64], progid[64];
195 DWORD res;
196 LONG size;
197 HKEY hkey;
198 HRESULT hres;
200 static const WCHAR script_engineW[] =
201 {'\\','S','c','r','i','p','t','E','n','g','i','n','e',0};
203 res = RegOpenKeyW(HKEY_CLASSES_ROOT, ext, &hkey);
204 if(res != ERROR_SUCCESS)
205 return FALSE;
207 size = sizeof(fileid)/sizeof(WCHAR);
208 res = RegQueryValueW(hkey, NULL, fileid, &size);
209 RegCloseKey(hkey);
210 if(res != ERROR_SUCCESS)
211 return FALSE;
213 WINE_TRACE("fileid is %s\n", wine_dbgstr_w(fileid));
215 strcatW(fileid, script_engineW);
216 res = RegOpenKeyW(HKEY_CLASSES_ROOT, fileid, &hkey);
217 if(res != ERROR_SUCCESS)
218 return FALSE;
220 size = sizeof(progid)/sizeof(WCHAR);
221 res = RegQueryValueW(hkey, NULL, progid, &size);
222 RegCloseKey(hkey);
223 if(res != ERROR_SUCCESS)
224 return FALSE;
226 WINE_TRACE("ProgID is %s\n", wine_dbgstr_w(progid));
228 hres = CLSIDFromProgID(progid, clsid);
229 return SUCCEEDED(hres);
232 static HRESULT create_engine(CLSID *clsid, IActiveScript **script_ret,
233 IActiveScriptParse **parser)
235 IActiveScript *script;
236 IUnknown *unk;
237 HRESULT hres;
239 hres = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
240 &IID_IUnknown, (void**)&unk);
241 if(FAILED(hres))
242 return FALSE;
244 hres = IUnknown_QueryInterface(unk, &IID_IActiveScript, (void**)&script);
245 IUnknown_Release(unk);
246 if(FAILED(hres))
247 return FALSE;
249 hres = IActiveScript_QueryInterface(script, &IID_IActiveScriptParse, (void**)parser);
250 if(FAILED(hres)) {
251 IActiveScript_Release(script);
252 return FALSE;
255 *script_ret = script;
256 return TRUE;
259 static HRESULT init_engine(IActiveScript *script, IActiveScriptParse *parser)
261 HRESULT hres;
263 if(!load_typelib())
264 return FALSE;
266 hres = IActiveScript_SetScriptSite(script, &script_site);
267 if(FAILED(hres))
268 return FALSE;
270 hres = IActiveScriptParse_InitNew(parser);
271 if(FAILED(hres))
272 return FALSE;
274 hres = IActiveScript_AddNamedItem(script, wscriptW, SCRIPTITEM_ISVISIBLE);
275 if(FAILED(hres))
276 return FALSE;
278 hres = IActiveScript_AddNamedItem(script, wshW, SCRIPTITEM_ISVISIBLE);
279 if(FAILED(hres))
280 return FALSE;
282 hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_INITIALIZED);
283 return SUCCEEDED(hres);
286 static BSTR get_script_str(const WCHAR *filename)
288 const char *file_map;
289 HANDLE file, map;
290 DWORD size, len;
291 BSTR ret;
293 file = CreateFileW(filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
294 if(file == INVALID_HANDLE_VALUE)
295 return NULL;
297 size = GetFileSize(file, NULL);
298 map = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, NULL);
299 CloseHandle(file);
300 if(map == INVALID_HANDLE_VALUE)
301 return NULL;
303 file_map = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
304 CloseHandle(map);
305 if(!file_map)
306 return NULL;
308 len = MultiByteToWideChar(CP_ACP, 0, file_map, size, NULL, 0);
309 ret = SysAllocStringLen(NULL, len);
310 MultiByteToWideChar(CP_ACP, 0, file_map, size, ret, len);
312 UnmapViewOfFile(file_map);
313 return ret;
316 static void run_script(const WCHAR *filename, IActiveScript *script, IActiveScriptParse *parser)
318 BSTR text;
319 HRESULT hres;
321 text = get_script_str(filename);
322 if(!text) {
323 WINE_FIXME("Could not get script text\n");
324 return;
327 hres = IActiveScriptParse_ParseScriptText(parser, text, NULL, NULL, NULL, 1, 1,
328 SCRIPTTEXT_HOSTMANAGESSOURCE|SCRIPTITEM_ISVISIBLE, NULL, NULL);
329 SysFreeString(text);
330 if(FAILED(hres)) {
331 WINE_FIXME("ParseScriptText failed: %08x\n", hres);
332 return;
335 hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_STARTED);
336 if(FAILED(hres))
337 WINE_FIXME("SetScriptState failed: %08x\n", hres);
340 static BOOL set_host_properties(const WCHAR *prop)
342 static const WCHAR iactive[] = {'i',0};
343 static const WCHAR batch[] = {'b',0};
345 if(*prop == '/') {
346 ++prop;
347 if(*prop == '/')
348 ++prop;
350 else
351 ++prop;
353 if(strcmpiW(prop, iactive) == 0)
354 wshInteractive = VARIANT_TRUE;
355 else if(strcmpiW(prop, batch) == 0)
356 wshInteractive = VARIANT_FALSE;
357 else
358 return FALSE;
359 return TRUE;
362 int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPWSTR cmdline, int cmdshow)
364 const WCHAR *ext, *filename = NULL;
365 IActiveScriptParse *parser;
366 IActiveScript *script;
367 WCHAR **argv;
368 CLSID clsid;
369 int argc, i;
370 DWORD res;
372 WINE_TRACE("(%p %p %s %x)\n", hInst, hPrevInst, wine_dbgstr_w(cmdline), cmdshow);
374 argv = CommandLineToArgvW(cmdline, &argc);
375 if(!argv)
376 return 1;
378 for(i=0; i<argc; i++) {
379 if(*argv[i] == '/' || *argv[i] == '-') {
380 if(!set_host_properties(argv[i]))
381 return 1;
382 }else {
383 filename = argv[i];
384 argums = argv+i+1;
385 numOfArgs = argc-i-1;
386 break;
390 if(!filename) {
391 WINE_FIXME("No file name specified\n");
392 return 1;
394 res = GetFullPathNameW(filename, sizeof(scriptFullName)/sizeof(WCHAR), scriptFullName, NULL);
395 if(!res || res > sizeof(scriptFullName)/sizeof(WCHAR))
396 return 1;
398 ext = strchrW(filename, '.');
399 if(!ext)
400 ext = filename;
401 if(!get_engine_clsid(ext, &clsid)) {
402 WINE_FIXME("Could not find engine for %s\n", wine_dbgstr_w(ext));
403 return 1;
406 CoInitialize(NULL);
408 if(!create_engine(&clsid, &script, &parser)) {
409 WINE_FIXME("Could not create script engine\n");
410 CoUninitialize();
411 return 1;
414 if(init_engine(script, parser)) {
415 run_script(filename, script, parser);
416 IActiveScript_Close(script);
417 ITypeInfo_Release(host_ti);
418 }else {
419 WINE_FIXME("Script initialization failed\n");
422 IActiveScript_Release(script);
423 IActiveScriptParse_Release(parser);
425 CoUninitialize();
427 return 0;