po: The start /L option is gone so remove it from the Hungarian translation.
[wine/multimedia.git] / programs / wscript / main.c
blobd456e98ad8fdfdf8202045753761f6e78277e967
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 WINE_DEFAULT_DEBUG_CHANNEL(wscript);
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];
42 ITypeInfo *host_ti;
43 ITypeInfo *arguments_ti;
45 static HRESULT WINAPI ActiveScriptSite_QueryInterface(IActiveScriptSite *iface,
46 REFIID riid, void **ppv)
48 if(IsEqualGUID(riid, &IID_IUnknown)) {
49 WINE_TRACE("(IID_IUnknown %p)\n", ppv);
50 *ppv = iface;
51 }else if(IsEqualGUID(riid, &IID_IActiveScriptSite)) {
52 WINE_TRACE("(IID_IActiveScriptSite %p)\n", ppv);
53 *ppv = iface;
54 }else {
55 *ppv = NULL;
56 WINE_TRACE("(%s %p)\n", wine_dbgstr_guid(riid), ppv);
57 return E_NOINTERFACE;
60 IUnknown_AddRef((IUnknown*)*ppv);
61 return S_OK;
64 static ULONG WINAPI ActiveScriptSite_AddRef(IActiveScriptSite *iface)
66 return 2;
69 static ULONG WINAPI ActiveScriptSite_Release(IActiveScriptSite *iface)
71 return 1;
74 static HRESULT WINAPI ActiveScriptSite_GetLCID(IActiveScriptSite *iface, LCID *plcid)
76 WINE_TRACE("()\n");
78 *plcid = GetUserDefaultLCID();
79 return S_OK;
82 static HRESULT WINAPI ActiveScriptSite_GetItemInfo(IActiveScriptSite *iface,
83 LPCOLESTR pstrName, DWORD dwReturnMask, IUnknown **ppunkItem, ITypeInfo **ppti)
85 WINE_TRACE("(%s %x %p %p)\n", wine_dbgstr_w(pstrName), dwReturnMask, ppunkItem, ppti);
87 if(strcmpW(pstrName, wshW) && strcmpW(pstrName, wscriptW))
88 return E_FAIL;
90 if(dwReturnMask & SCRIPTINFO_ITYPEINFO) {
91 ITypeInfo_AddRef(host_ti);
92 *ppti = host_ti;
95 if(dwReturnMask & SCRIPTINFO_IUNKNOWN) {
96 IHost_AddRef(&host_obj);
97 *ppunkItem = (IUnknown*)&host_obj;
100 return S_OK;
103 static HRESULT WINAPI ActiveScriptSite_GetDocVersionString(IActiveScriptSite *iface,
104 BSTR *pbstrVersion)
106 WINE_FIXME("()\n");
107 return E_NOTIMPL;
110 static HRESULT WINAPI ActiveScriptSite_OnScriptTerminate(IActiveScriptSite *iface,
111 const VARIANT *pvarResult, const EXCEPINFO *pexcepinfo)
113 WINE_FIXME("()\n");
114 return E_NOTIMPL;
117 static HRESULT WINAPI ActiveScriptSite_OnStateChange(IActiveScriptSite *iface,
118 SCRIPTSTATE ssScriptState)
120 WINE_TRACE("(%x)\n", ssScriptState);
121 return S_OK;
124 static HRESULT WINAPI ActiveScriptSite_OnScriptError(IActiveScriptSite *iface,
125 IActiveScriptError *pscripterror)
127 WINE_FIXME("()\n");
128 return E_NOTIMPL;
131 static HRESULT WINAPI ActiveScriptSite_OnEnterScript(IActiveScriptSite *iface)
133 WINE_TRACE("()\n");
134 return S_OK;
137 static HRESULT WINAPI ActiveScriptSite_OnLeaveScript(IActiveScriptSite *iface)
139 WINE_TRACE("()\n");
140 return S_OK;
143 static IActiveScriptSiteVtbl ActiveScriptSiteVtbl = {
144 ActiveScriptSite_QueryInterface,
145 ActiveScriptSite_AddRef,
146 ActiveScriptSite_Release,
147 ActiveScriptSite_GetLCID,
148 ActiveScriptSite_GetItemInfo,
149 ActiveScriptSite_GetDocVersionString,
150 ActiveScriptSite_OnScriptTerminate,
151 ActiveScriptSite_OnStateChange,
152 ActiveScriptSite_OnScriptError,
153 ActiveScriptSite_OnEnterScript,
154 ActiveScriptSite_OnLeaveScript
157 IActiveScriptSite script_site = { &ActiveScriptSiteVtbl };
159 static BOOL load_typelib(void)
161 ITypeLib *typelib;
162 HRESULT hres;
164 static const WCHAR wscript_exeW[] = {'w','s','c','r','i','p','t','.','e','x','e',0};
166 hres = LoadTypeLib(wscript_exeW, &typelib);
167 if(FAILED(hres))
168 return FALSE;
170 hres = ITypeLib_GetTypeInfoOfGuid(typelib, &IID_IHost, &host_ti);
171 if(SUCCEEDED(hres))
172 hres = ITypeLib_GetTypeInfoOfGuid(typelib, &IID_IArguments2, &arguments_ti);
174 ITypeLib_Release(typelib);
175 return SUCCEEDED(hres);
178 static BOOL get_engine_clsid(const WCHAR *ext, CLSID *clsid)
180 WCHAR fileid[64], progid[64];
181 DWORD res;
182 LONG size;
183 HKEY hkey;
184 HRESULT hres;
186 static const WCHAR script_engineW[] =
187 {'\\','S','c','r','i','p','t','E','n','g','i','n','e',0};
189 res = RegOpenKeyW(HKEY_CLASSES_ROOT, ext, &hkey);
190 if(res != ERROR_SUCCESS)
191 return FALSE;
193 size = sizeof(fileid)/sizeof(WCHAR);
194 res = RegQueryValueW(hkey, NULL, fileid, &size);
195 RegCloseKey(hkey);
196 if(res != ERROR_SUCCESS)
197 return FALSE;
199 WINE_TRACE("fileid is %s\n", wine_dbgstr_w(fileid));
201 strcatW(fileid, script_engineW);
202 res = RegOpenKeyW(HKEY_CLASSES_ROOT, fileid, &hkey);
203 if(res != ERROR_SUCCESS)
204 return FALSE;
206 size = sizeof(progid)/sizeof(WCHAR);
207 res = RegQueryValueW(hkey, NULL, progid, &size);
208 RegCloseKey(hkey);
209 if(res != ERROR_SUCCESS)
210 return FALSE;
212 WINE_TRACE("ProgID is %s\n", wine_dbgstr_w(progid));
214 hres = CLSIDFromProgID(progid, clsid);
215 return SUCCEEDED(hres);
218 static HRESULT create_engine(CLSID *clsid, IActiveScript **script_ret,
219 IActiveScriptParse **parser)
221 IActiveScript *script;
222 IUnknown *unk;
223 HRESULT hres;
225 hres = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
226 &IID_IUnknown, (void**)&unk);
227 if(FAILED(hres))
228 return FALSE;
230 hres = IUnknown_QueryInterface(unk, &IID_IActiveScript, (void**)&script);
231 IUnknown_Release(unk);
232 if(FAILED(hres))
233 return FALSE;
235 hres = IActiveScript_QueryInterface(script, &IID_IActiveScriptParse, (void**)parser);
236 if(FAILED(hres)) {
237 IActiveScript_Release(script);
238 return FALSE;
241 *script_ret = script;
242 return TRUE;
245 static HRESULT init_engine(IActiveScript *script, IActiveScriptParse *parser)
247 HRESULT hres;
249 if(!load_typelib())
250 return FALSE;
252 hres = IActiveScript_SetScriptSite(script, &script_site);
253 if(FAILED(hres))
254 return FALSE;
256 hres = IActiveScriptParse64_InitNew(parser);
257 if(FAILED(hres))
258 return FALSE;
260 hres = IActiveScript_AddNamedItem(script, wscriptW, SCRIPTITEM_ISVISIBLE);
261 if(FAILED(hres))
262 return FALSE;
264 hres = IActiveScript_AddNamedItem(script, wshW, SCRIPTITEM_ISVISIBLE);
265 if(FAILED(hres))
266 return FALSE;
268 hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_INITIALIZED);
269 return SUCCEEDED(hres);
272 static BSTR get_script_str(const WCHAR *filename)
274 const char *file_map;
275 HANDLE file, map;
276 DWORD size, len;
277 BSTR ret;
279 file = CreateFileW(filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
280 if(file == INVALID_HANDLE_VALUE)
281 return NULL;
283 size = GetFileSize(file, NULL);
284 map = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, NULL);
285 CloseHandle(file);
286 if(map == INVALID_HANDLE_VALUE)
287 return NULL;
289 file_map = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
290 CloseHandle(map);
291 if(!file_map)
292 return NULL;
294 len = MultiByteToWideChar(CP_ACP, 0, file_map, size, NULL, 0);
295 ret = SysAllocStringLen(NULL, len);
296 MultiByteToWideChar(CP_ACP, 0, file_map, size, ret, len);
298 UnmapViewOfFile(file_map);
299 return ret;
302 static void run_script(const WCHAR *filename, IActiveScript *script, IActiveScriptParse *parser)
304 BSTR text;
305 HRESULT hres;
307 text = get_script_str(filename);
308 if(!text) {
309 WINE_FIXME("Could not get script text\n");
310 return;
313 hres = IActiveScriptParse64_ParseScriptText(parser, text, NULL, NULL, NULL, 1, 1,
314 SCRIPTTEXT_HOSTMANAGESSOURCE|SCRIPTITEM_ISVISIBLE, NULL, NULL);
315 SysFreeString(text);
316 if(FAILED(hres)) {
317 WINE_FIXME("ParseScriptText failed: %08x\n", hres);
318 return;
321 hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_STARTED);
322 if(FAILED(hres))
323 WINE_FIXME("SetScriptState failed: %08x\n", hres);
326 static BOOL set_host_properties(const WCHAR *prop)
328 static const WCHAR iactive[] = {'i',0};
329 static const WCHAR batch[] = {'b',0};
331 if(*prop == '/') {
332 ++prop;
333 if(*prop == '/')
334 ++prop;
336 else
337 ++prop;
339 if(strcmpiW(prop, iactive) == 0)
340 wshInteractive = VARIANT_TRUE;
341 else if(strcmpiW(prop, batch) == 0)
342 wshInteractive = VARIANT_FALSE;
343 else
344 return FALSE;
345 return TRUE;
348 int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPWSTR cmdline, int cmdshow)
350 const WCHAR *ext, *filename = NULL;
351 IActiveScriptParse *parser;
352 IActiveScript *script;
353 WCHAR **argv;
354 CLSID clsid;
355 int argc, i;
356 DWORD res;
358 WINE_TRACE("(%p %p %s %x)\n", hInst, hPrevInst, wine_dbgstr_w(cmdline), cmdshow);
360 argv = CommandLineToArgvW(cmdline, &argc);
361 if(!argv)
362 return 1;
364 for(i=0; i<argc; i++) {
365 if(*argv[i] == '/' || *argv[i] == '-') {
366 if(!set_host_properties(argv[i]))
367 return 1;
368 }else {
369 filename = argv[i];
370 argums = argv+i+1;
371 numOfArgs = argc-i-1;
372 break;
376 if(!filename) {
377 WINE_FIXME("No file name specified\n");
378 return 1;
380 res = GetFullPathNameW(filename, sizeof(scriptFullName)/sizeof(WCHAR), scriptFullName, NULL);
381 if(!res || res > sizeof(scriptFullName)/sizeof(WCHAR))
382 return 1;
384 ext = strchrW(filename, '.');
385 if(!ext)
386 ext = filename;
387 if(!get_engine_clsid(ext, &clsid)) {
388 WINE_FIXME("Could not find engine for %s\n", wine_dbgstr_w(ext));
389 return 1;
392 CoInitialize(NULL);
394 if(!create_engine(&clsid, &script, &parser)) {
395 WINE_FIXME("Could not create script engine\n");
396 CoUninitialize();
397 return 1;
400 if(init_engine(script, parser)) {
401 run_script(filename, script, parser);
402 IActiveScript_Close(script);
403 ITypeInfo_Release(host_ti);
404 }else {
405 WINE_FIXME("Script initialization failed\n");
408 IActiveScript_Release(script);
409 IUnknown_Release(parser);
411 CoUninitialize();
413 return 0;