Release 1.4-rc5.
[wine] / programs / 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 <stdarg.h>
20
21 #define COBJMACROS
22
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>
30
31 #include "wscript.h"
32
33 #include <wine/debug.h>
34 #include <wine/unicode.h>
35
36 WINE_DEFAULT_DEBUG_CHANNEL(wscript);
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 WINAPI ActiveScriptSite_QueryInterface(IActiveScriptSite *iface,
46                                                       REFIID riid, void **ppv)
47 {
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;
58     }
59
60     IUnknown_AddRef((IUnknown*)*ppv);
61     return S_OK;
62 }
63
64 static ULONG WINAPI ActiveScriptSite_AddRef(IActiveScriptSite *iface)
65 {
66     return 2;
67 }
68
69 static ULONG WINAPI ActiveScriptSite_Release(IActiveScriptSite *iface)
70 {
71     return 1;
72 }
73
74 static HRESULT WINAPI ActiveScriptSite_GetLCID(IActiveScriptSite *iface, LCID *plcid)
75 {
76     WINE_TRACE("()\n");
77
78     *plcid = GetUserDefaultLCID();
79     return S_OK;
80 }
81
82 static HRESULT WINAPI ActiveScriptSite_GetItemInfo(IActiveScriptSite *iface,
83         LPCOLESTR pstrName, DWORD dwReturnMask, IUnknown **ppunkItem, ITypeInfo **ppti)
84 {
85     WINE_TRACE("(%s %x %p %p)\n", wine_dbgstr_w(pstrName), dwReturnMask, ppunkItem, ppti);
86
87     if(strcmpW(pstrName, wshW) && strcmpW(pstrName, wscriptW))
88         return E_FAIL;
89
90     if(dwReturnMask & SCRIPTINFO_ITYPEINFO) {
91         ITypeInfo_AddRef(host_ti);
92         *ppti = host_ti;
93     }
94
95     if(dwReturnMask & SCRIPTINFO_IUNKNOWN) {
96         IHost_AddRef(&host_obj);
97         *ppunkItem = (IUnknown*)&host_obj;
98     }
99
100     return S_OK;
101 }
102
103 static HRESULT WINAPI ActiveScriptSite_GetDocVersionString(IActiveScriptSite *iface,
104         BSTR *pbstrVersion)
105 {
106     WINE_FIXME("()\n");
107     return E_NOTIMPL;
108 }
109
110 static HRESULT WINAPI ActiveScriptSite_OnScriptTerminate(IActiveScriptSite *iface,
111         const VARIANT *pvarResult, const EXCEPINFO *pexcepinfo)
112 {
113     WINE_FIXME("()\n");
114     return E_NOTIMPL;
115 }
116
117 static HRESULT WINAPI ActiveScriptSite_OnStateChange(IActiveScriptSite *iface,
118         SCRIPTSTATE ssScriptState)
119 {
120     WINE_TRACE("(%x)\n", ssScriptState);
121     return S_OK;
122 }
123
124 static HRESULT WINAPI ActiveScriptSite_OnScriptError(IActiveScriptSite *iface,
125         IActiveScriptError *pscripterror)
126 {
127     WINE_FIXME("()\n");
128     return E_NOTIMPL;
129 }
130
131 static HRESULT WINAPI ActiveScriptSite_OnEnterScript(IActiveScriptSite *iface)
132 {
133     WINE_TRACE("()\n");
134     return S_OK;
135 }
136
137 static HRESULT WINAPI ActiveScriptSite_OnLeaveScript(IActiveScriptSite *iface)
138 {
139     WINE_TRACE("()\n");
140     return S_OK;
141 }
142
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
155 };
156
157 IActiveScriptSite script_site = { &ActiveScriptSiteVtbl };
158
159 static BOOL load_typelib(void)
160 {
161     ITypeLib *typelib;
162     HRESULT hres;
163
164     static const WCHAR wscript_exeW[] = {'w','s','c','r','i','p','t','.','e','x','e',0};
165
166     hres = LoadTypeLib(wscript_exeW, &typelib);
167     if(FAILED(hres))
168         return FALSE;
169
170     hres = ITypeLib_GetTypeInfoOfGuid(typelib, &IID_IHost, &host_ti);
171     if(SUCCEEDED(hres))
172         hres = ITypeLib_GetTypeInfoOfGuid(typelib, &IID_IArguments2, &arguments_ti);
173
174     ITypeLib_Release(typelib);
175     return SUCCEEDED(hres);
176 }
177
178 static BOOL get_engine_clsid(const WCHAR *ext, CLSID *clsid)
179 {
180     WCHAR fileid[64], progid[64];
181     DWORD res;
182     LONG size;
183     HKEY hkey;
184     HRESULT hres;
185
186     static const WCHAR script_engineW[] =
187         {'\\','S','c','r','i','p','t','E','n','g','i','n','e',0};
188
189     res = RegOpenKeyW(HKEY_CLASSES_ROOT, ext, &hkey);
190     if(res != ERROR_SUCCESS)
191         return FALSE;
192
193     size = sizeof(fileid)/sizeof(WCHAR);
194     res = RegQueryValueW(hkey, NULL, fileid, &size);
195     RegCloseKey(hkey);
196     if(res != ERROR_SUCCESS)
197         return FALSE;
198
199     WINE_TRACE("fileid is %s\n", wine_dbgstr_w(fileid));
200
201     strcatW(fileid, script_engineW);
202     res = RegOpenKeyW(HKEY_CLASSES_ROOT, fileid, &hkey);
203     if(res != ERROR_SUCCESS)
204         return FALSE;
205
206     size = sizeof(progid)/sizeof(WCHAR);
207     res = RegQueryValueW(hkey, NULL, progid, &size);
208     RegCloseKey(hkey);
209     if(res != ERROR_SUCCESS)
210         return FALSE;
211
212     WINE_TRACE("ProgID is %s\n", wine_dbgstr_w(progid));
213
214     hres = CLSIDFromProgID(progid, clsid);
215     return SUCCEEDED(hres);
216 }
217
218 static HRESULT create_engine(CLSID *clsid, IActiveScript **script_ret,
219         IActiveScriptParse **parser)
220 {
221     IActiveScript *script;
222     IUnknown *unk;
223     HRESULT hres;
224
225     hres = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
226             &IID_IUnknown, (void**)&unk);
227     if(FAILED(hres))
228         return FALSE;
229
230     hres = IUnknown_QueryInterface(unk, &IID_IActiveScript, (void**)&script);
231     IUnknown_Release(unk);
232     if(FAILED(hres))
233         return FALSE;
234
235     hres = IActiveScript_QueryInterface(script, &IID_IActiveScriptParse, (void**)parser);
236     if(FAILED(hres)) {
237         IActiveScript_Release(script);
238         return FALSE;
239     }
240
241     *script_ret = script;
242     return TRUE;
243 }
244
245 static HRESULT init_engine(IActiveScript *script, IActiveScriptParse *parser)
246 {
247     HRESULT hres;
248
249     if(!load_typelib())
250         return FALSE;
251
252     hres = IActiveScript_SetScriptSite(script, &script_site);
253     if(FAILED(hres))
254         return FALSE;
255
256     hres = IActiveScriptParse64_InitNew(parser);
257     if(FAILED(hres))
258         return FALSE;
259
260     hres = IActiveScript_AddNamedItem(script, wscriptW, SCRIPTITEM_ISVISIBLE);
261     if(FAILED(hres))
262         return FALSE;
263
264     hres = IActiveScript_AddNamedItem(script, wshW, SCRIPTITEM_ISVISIBLE);
265     if(FAILED(hres))
266         return FALSE;
267
268     hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_INITIALIZED);
269     return SUCCEEDED(hres);
270 }
271
272 static BSTR get_script_str(const WCHAR *filename)
273 {
274     const char *file_map;
275     HANDLE file, map;
276     DWORD size, len;
277     BSTR ret;
278
279     file = CreateFileW(filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
280     if(file == INVALID_HANDLE_VALUE)
281         return NULL;
282
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;
288
289     file_map = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
290     CloseHandle(map);
291     if(!file_map)
292         return NULL;
293
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);
297
298     UnmapViewOfFile(file_map);
299     return ret;
300 }
301
302 static void run_script(const WCHAR *filename, IActiveScript *script, IActiveScriptParse *parser)
303 {
304     BSTR text;
305     HRESULT hres;
306
307     text = get_script_str(filename);
308     if(!text) {
309         WINE_FIXME("Could not get script text\n");
310         return;
311     }
312
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;
319     }
320
321     hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_STARTED);
322     if(FAILED(hres))
323         WINE_FIXME("SetScriptState failed: %08x\n", hres);
324 }
325
326 static BOOL set_host_properties(const WCHAR *prop)
327 {
328     static const WCHAR iactive[] = {'i',0};
329     static const WCHAR batch[] = {'b',0};
330
331     if(*prop == '/') {
332         ++prop;
333         if(*prop == '/')
334             ++prop;
335     }
336     else
337         ++prop;
338
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;
346 }
347
348 int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPWSTR cmdline, int cmdshow)
349 {
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;
357
358     WINE_TRACE("(%p %p %s %x)\n", hInst, hPrevInst, wine_dbgstr_w(cmdline), cmdshow);
359
360     argv = CommandLineToArgvW(cmdline, &argc);
361     if(!argv)
362         return 1;
363
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;
373         }
374     }
375
376     if(!filename) {
377         WINE_FIXME("No file name specified\n");
378         return 1;
379     }
380     res = GetFullPathNameW(filename, sizeof(scriptFullName)/sizeof(WCHAR), scriptFullName, NULL);
381     if(!res || res > sizeof(scriptFullName)/sizeof(WCHAR))
382         return 1;
383
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;
390     }
391
392     CoInitialize(NULL);
393
394     if(!create_engine(&clsid, &script, &parser)) {
395         WINE_FIXME("Could not create script engine\n");
396         CoUninitialize();
397         return 1;
398     }
399
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");
406     }
407
408     IActiveScript_Release(script);
409     IUnknown_Release(parser);
410
411     CoUninitialize();
412
413     return 0;
414 }