b867fae65c30e0d1a5485c57a6d92b6d68e2d82c
[reactos.git] / base / applications / cmdutils / wscript / host.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 #define BUILDVERSION 16535
22
23 static const WCHAR wshNameW[] = {'W','i','n','d','o','w','s',' ','S','c','r','i','p','t',' ','H','o','s','t',0};
24 static const WCHAR wshVersionW[] = {'5','.','8'};
25
26 VARIANT_BOOL wshInteractive =
27 #ifndef CSCRIPT_BUILD
28 VARIANT_TRUE;
29 #else
30 VARIANT_FALSE;
31 #endif
32
33 static HRESULT to_string(VARIANT *src, BSTR *dst)
34 {
35 VARIANT v;
36 HRESULT hres;
37
38 static const WCHAR nullW[] = {'n','u','l','l',0};
39
40 if(V_VT(src) == VT_NULL) {
41 *dst = SysAllocString(nullW);
42 return *dst ? S_OK : E_OUTOFMEMORY;
43 }
44
45 V_VT(&v) = VT_EMPTY;
46 hres = VariantChangeType(&v, src, 0, VT_BSTR);
47 if(FAILED(hres)) {
48 WARN("Could not convert argument %s to string\n", debugstr_variant(src));
49 return hres;
50 }
51
52 *dst = V_BSTR(&v);
53 return S_OK;
54 }
55
56 static void print_string(const WCHAR *string)
57 {
58 DWORD count, ret, len, lena;
59 char *buf;
60
61 if(wshInteractive) {
62 static const WCHAR windows_script_hostW[] =
63 {'W','i','n','d','o','w','s',' ','S','c','r','i','p','t',' ','H','o','s','t',0};
64 MessageBoxW(NULL, string, windows_script_hostW, MB_OK);
65 return;
66 }
67
68 len = strlenW(string);
69 ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), string, len, &count, NULL);
70 if(ret) {
71 static const WCHAR crnlW[] = {'\r','\n'};
72 WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), crnlW, sizeof(crnlW)/sizeof(*crnlW), &count, NULL);
73 return;
74 }
75
76 lena = WideCharToMultiByte(GetConsoleOutputCP(), 0, string, len, NULL, 0, NULL, NULL);
77 buf = heap_alloc(len);
78 if(!buf)
79 return;
80
81 WideCharToMultiByte(GetConsoleOutputCP(), 0, string, len, buf, lena, NULL, NULL);
82 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buf, lena, &count, FALSE);
83 heap_free(buf);
84 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), "\r\n", 2, &count, FALSE);
85 }
86
87 static HRESULT WINAPI Host_QueryInterface(IHost *iface, REFIID riid, void **ppv)
88 {
89 WINE_TRACE("(%s %p)\n", wine_dbgstr_guid(riid), ppv);
90
91 if(IsEqualGUID(&IID_IUnknown, riid)
92 || IsEqualGUID(&IID_IDispatch, riid)
93 || IsEqualGUID(&IID_IHost, riid)) {
94 *ppv = iface;
95 return S_OK;
96 }
97
98 *ppv = NULL;
99 return E_NOINTERFACE;
100 }
101
102 static ULONG WINAPI Host_AddRef(IHost *iface)
103 {
104 return 2;
105 }
106
107 static ULONG WINAPI Host_Release(IHost *iface)
108 {
109 return 1;
110 }
111
112 static HRESULT WINAPI Host_GetTypeInfoCount(IHost *iface, UINT *pctinfo)
113 {
114 WINE_TRACE("(%p)\n", pctinfo);
115 *pctinfo = 1;
116 return S_OK;
117 }
118
119 static HRESULT WINAPI Host_GetTypeInfo(IHost *iface, UINT iTInfo, LCID lcid,
120 ITypeInfo **ppTInfo)
121 {
122 WINE_TRACE("(%x %x %p\n", iTInfo, lcid, ppTInfo);
123
124 ITypeInfo_AddRef(host_ti);
125 *ppTInfo = host_ti;
126 return S_OK;
127 }
128
129 static HRESULT WINAPI Host_GetIDsOfNames(IHost *iface, REFIID riid, LPOLESTR *rgszNames,
130 UINT cNames, LCID lcid, DISPID *rgDispId)
131 {
132 WINE_TRACE("(%s %p %d %x %p)\n", wine_dbgstr_guid(riid), rgszNames,
133 cNames, lcid, rgDispId);
134
135 return ITypeInfo_GetIDsOfNames(host_ti, rgszNames, cNames, rgDispId);
136 }
137
138 static HRESULT WINAPI Host_Invoke(IHost *iface, DISPID dispIdMember, REFIID riid,
139 LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult,
140 EXCEPINFO *pExcepInfo, UINT *puArgErr)
141 {
142 WINE_TRACE("(%d %p %p)\n", dispIdMember, pDispParams, pVarResult);
143
144 return ITypeInfo_Invoke(host_ti, iface, dispIdMember, wFlags, pDispParams,
145 pVarResult, pExcepInfo, puArgErr);
146 }
147
148 static HRESULT WINAPI Host_get_Name(IHost *iface, BSTR *out_Name)
149 {
150 WINE_TRACE("(%p)\n", out_Name);
151
152 if(!(*out_Name = SysAllocString(wshNameW)))
153 return E_OUTOFMEMORY;
154 return S_OK;
155 }
156
157 static HRESULT WINAPI Host_get_Application(IHost *iface, IDispatch **out_Dispatch)
158 {
159 WINE_TRACE("(%p)\n", out_Dispatch);
160
161 *out_Dispatch = (IDispatch*)&host_obj;
162 return S_OK;
163 }
164
165 static HRESULT WINAPI Host_get_FullName(IHost *iface, BSTR *out_Path)
166 {
167 WCHAR fullPath[MAX_PATH];
168
169 WINE_TRACE("(%p)\n", out_Path);
170
171 if(GetModuleFileNameW(NULL, fullPath, sizeof(fullPath)/sizeof(WCHAR)) == 0)
172 return E_FAIL;
173 if(!(*out_Path = SysAllocString(fullPath)))
174 return E_OUTOFMEMORY;
175 return S_OK;
176 }
177
178 static HRESULT WINAPI Host_get_Path(IHost *iface, BSTR *out_Path)
179 {
180 WCHAR path[MAX_PATH];
181 int howMany;
182 WCHAR *pos;
183
184 WINE_TRACE("(%p)\n", out_Path);
185
186 if(GetModuleFileNameW(NULL, path, sizeof(path)/sizeof(WCHAR)) == 0)
187 return E_FAIL;
188 pos = strrchrW(path, '\\');
189 howMany = pos - path;
190 if(!(*out_Path = SysAllocStringLen(path, howMany)))
191 return E_OUTOFMEMORY;
192 return S_OK;
193 }
194
195 static HRESULT WINAPI Host_get_Interactive(IHost *iface, VARIANT_BOOL *out_Interactive)
196 {
197 WINE_TRACE("(%p)\n", out_Interactive);
198
199 *out_Interactive = wshInteractive;
200 return S_OK;
201 }
202
203 static HRESULT WINAPI Host_put_Interactive(IHost *iface, VARIANT_BOOL v)
204 {
205 WINE_TRACE("(%x)\n", v);
206
207 wshInteractive = v;
208 return S_OK;
209 }
210
211 static HRESULT WINAPI Host_Quit(IHost *iface, int ExitCode)
212 {
213 FIXME("(%d) semi-stub: no script engine clean up\n", ExitCode);
214
215 ExitProcess(ExitCode);
216 return S_OK;
217 }
218
219 static HRESULT WINAPI Host_get_ScriptName(IHost *iface, BSTR *out_ScriptName)
220 {
221 WCHAR *scriptName;
222
223 WINE_TRACE("(%p)\n", out_ScriptName);
224
225 scriptName = strrchrW(scriptFullName, '\\');
226 ++scriptName;
227 if(!(*out_ScriptName = SysAllocString(scriptName)))
228 return E_OUTOFMEMORY;
229 return S_OK;
230 }
231
232 static HRESULT WINAPI Host_get_ScriptFullName(IHost *iface, BSTR *out_ScriptFullName)
233 {
234 WINE_TRACE("(%p)\n", out_ScriptFullName);
235
236 if(!(*out_ScriptFullName = SysAllocString(scriptFullName)))
237 return E_OUTOFMEMORY;
238 return S_OK;
239 }
240
241 static HRESULT WINAPI Host_get_Arguments(IHost *iface, IArguments2 **out_Arguments)
242 {
243 WINE_TRACE("(%p)\n", out_Arguments);
244
245 *out_Arguments = &arguments_obj;
246 return S_OK;
247 }
248
249 static HRESULT WINAPI Host_get_Version(IHost *iface, BSTR *out_Version)
250 {
251 WINE_TRACE("(%p)\n", out_Version);
252
253 if(!(*out_Version = SysAllocString(wshVersionW)))
254 return E_OUTOFMEMORY;
255 return S_OK;
256 }
257
258 static HRESULT WINAPI Host_get_BuildVersion(IHost *iface, int *out_Build)
259 {
260 WINE_TRACE("(%p)\n", out_Build);
261
262 *out_Build = BUILDVERSION;
263 return S_OK;
264 }
265
266 static HRESULT WINAPI Host_get_Timeout(IHost *iface, LONG *out_Timeout)
267 {
268 WINE_FIXME("(%p)\n", out_Timeout);
269 return E_NOTIMPL;
270 }
271
272 static HRESULT WINAPI Host_put_Timeout(IHost *iface, LONG v)
273 {
274 WINE_FIXME("(%d)\n", v);
275 return E_NOTIMPL;
276 }
277
278 static HRESULT WINAPI Host_CreateObject(IHost *iface, BSTR ProgID, BSTR Prefix,
279 IDispatch **out_Dispatch)
280 {
281 IUnknown *unk;
282 GUID guid;
283 HRESULT hres;
284
285 TRACE("(%s %s %p)\n", wine_dbgstr_w(ProgID), wine_dbgstr_w(Prefix), out_Dispatch);
286
287 if(Prefix && *Prefix) {
288 FIXME("Prefix %s not supported\n", debugstr_w(Prefix));
289 return E_NOTIMPL;
290 }
291
292 hres = CLSIDFromProgID(ProgID, &guid);
293 if(FAILED(hres))
294 return hres;
295
296 hres = CoCreateInstance(&guid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER|CLSCTX_REMOTE_SERVER,
297 &IID_IUnknown, (void**)&unk);
298 if(FAILED(hres))
299 return hres;
300
301 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)out_Dispatch);
302 IUnknown_Release(unk);
303 return hres;
304 }
305
306 static HRESULT WINAPI Host_Echo(IHost *iface, SAFEARRAY *args)
307 {
308 WCHAR *output = NULL, *ptr;
309 unsigned argc, i, len;
310 LONG ubound, lbound;
311 VARIANT *argv;
312 BSTR *strs;
313 HRESULT hres;
314
315 TRACE("(%p)\n", args);
316
317 if(SafeArrayGetDim(args) != 1) {
318 FIXME("Unsupported args dim %d\n", SafeArrayGetDim(args));
319 return E_NOTIMPL;
320 }
321
322 SafeArrayGetLBound(args, 1, &lbound);
323 SafeArrayGetUBound(args, 1, &ubound);
324
325 hres = SafeArrayAccessData(args, (void**)&argv);
326 if(FAILED(hres))
327 return hres;
328
329 argc = ubound-lbound+1;
330 strs = heap_alloc_zero(argc*sizeof(*strs));
331 if(!strs) {
332 SafeArrayUnaccessData(args);
333 return E_OUTOFMEMORY;
334 }
335
336 /* Len of spaces between arguments. */
337 len = argc-1;
338
339 for(i=0; i < argc; i++) {
340 hres = to_string(argv+i, strs+i);
341 if(FAILED(hres))
342 break;
343
344 len += SysStringLen(strs[i]);
345 }
346
347 SafeArrayUnaccessData(args);
348 if(SUCCEEDED(hres)) {
349 ptr = output = heap_alloc((len+1)*sizeof(WCHAR));
350 if(output) {
351 for(i=0; i < argc; i++) {
352 if(i)
353 *ptr++ = ' ';
354 len = SysStringLen(strs[i]);
355 memcpy(ptr, strs[i], len*sizeof(WCHAR));
356 ptr += len;
357 }
358 *ptr = 0;
359 }else {
360 hres = E_OUTOFMEMORY;
361 }
362 }
363
364 for(i=0; i < argc; i++)
365 SysFreeString(strs[i]);
366 heap_free(strs);
367 if(FAILED(hres))
368 return hres;
369
370 print_string(output);
371
372 heap_free(output);
373 return S_OK;
374 }
375
376 static HRESULT WINAPI Host_GetObject(IHost *iface, BSTR Pathname, BSTR ProgID,
377 BSTR Prefix, IDispatch **out_Dispatch)
378 {
379 WINE_FIXME("(%s %s %s %p)\n", wine_dbgstr_w(Pathname), wine_dbgstr_w(ProgID),
380 wine_dbgstr_w(Prefix), out_Dispatch);
381 return E_NOTIMPL;
382 }
383
384 static HRESULT WINAPI Host_DisconnectObject(IHost *iface, IDispatch *Object)
385 {
386 WINE_FIXME("(%p)\n", Object);
387 return E_NOTIMPL;
388 }
389
390 static HRESULT WINAPI Host_Sleep(IHost *iface, LONG Time)
391 {
392 WINE_FIXME("(%d)\n", Time);
393 return E_NOTIMPL;
394 }
395
396 static HRESULT WINAPI Host_ConnectObject(IHost *iface, IDispatch *Object, BSTR Prefix)
397 {
398 WINE_FIXME("(%p %s)\n", Object, wine_dbgstr_w(Prefix));
399 return E_NOTIMPL;
400 }
401
402 static HRESULT WINAPI Host_get_StdIn(IHost *iface, ITextStream **ppts)
403 {
404 WINE_FIXME("(%p)\n", ppts);
405 return E_NOTIMPL;
406 }
407
408 static HRESULT WINAPI Host_get_StdOut(IHost *iface, ITextStream **ppts)
409 {
410 WINE_FIXME("(%p)\n", ppts);
411 return E_NOTIMPL;
412 }
413
414 static HRESULT WINAPI Host_get_StdErr(IHost *iface, ITextStream **ppts)
415 {
416 WINE_FIXME("(%p)\n", ppts);
417 return E_NOTIMPL;
418 }
419
420 static const IHostVtbl HostVtbl = {
421 Host_QueryInterface,
422 Host_AddRef,
423 Host_Release,
424 Host_GetTypeInfoCount,
425 Host_GetTypeInfo,
426 Host_GetIDsOfNames,
427 Host_Invoke,
428 Host_get_Name,
429 Host_get_Application,
430 Host_get_FullName,
431 Host_get_Path,
432 Host_get_Interactive,
433 Host_put_Interactive,
434 Host_Quit,
435 Host_get_ScriptName,
436 Host_get_ScriptFullName,
437 Host_get_Arguments,
438 Host_get_Version,
439 Host_get_BuildVersion,
440 Host_get_Timeout,
441 Host_put_Timeout,
442 Host_CreateObject,
443 Host_Echo,
444 Host_GetObject,
445 Host_DisconnectObject,
446 Host_Sleep,
447 Host_ConnectObject,
448 Host_get_StdIn,
449 Host_get_StdOut,
450 Host_get_StdErr
451 };
452
453 IHost host_obj = { &HostVtbl };