wscript: Use wide-char string literals.
[wine.git] / programs / wscript / host.c
blob9541994d3b0217e055661e4f7a3537ff575a1053
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
22 #define CONST_VTABLE
24 #include <windef.h>
25 #include <winbase.h>
26 #include <ole2.h>
28 #include "wscript.h"
30 #include <wine/debug.h>
31 #include <wine/heap.h>
33 WINE_DEFAULT_DEBUG_CHANNEL(wscript);
35 #define BUILDVERSION 16535
36 static const WCHAR wshVersionW[] = {'5','.','8'};
38 VARIANT_BOOL wshInteractive =
39 #ifndef CSCRIPT_BUILD
40 VARIANT_TRUE;
41 #else
42 VARIANT_FALSE;
43 #endif
45 static HRESULT to_string(VARIANT *src, BSTR *dst)
47 VARIANT v;
48 HRESULT hres;
50 if(V_VT(src) == VT_NULL) {
51 *dst = SysAllocString(L"null");
52 return *dst ? S_OK : E_OUTOFMEMORY;
55 V_VT(&v) = VT_EMPTY;
56 hres = VariantChangeType(&v, src, 0, VT_BSTR);
57 if(FAILED(hres)) {
58 WARN("Could not convert argument %s to string\n", debugstr_variant(src));
59 return hres;
62 *dst = V_BSTR(&v);
63 return S_OK;
66 static void print_string(const WCHAR *string)
68 DWORD count, ret, len, lena;
69 char *buf;
71 if(wshInteractive) {
72 MessageBoxW(NULL, string, L"Windows Script Host", MB_OK);
73 return;
76 len = lstrlenW(string);
77 ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), string, len, &count, NULL);
78 if(ret) {
79 static const WCHAR crnlW[] = {'\r','\n'};
80 WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), crnlW, ARRAY_SIZE(crnlW), &count, NULL);
81 return;
84 lena = WideCharToMultiByte(GetConsoleOutputCP(), 0, string, len, NULL, 0, NULL, NULL);
85 buf = heap_alloc(len);
86 if(!buf)
87 return;
89 WideCharToMultiByte(GetConsoleOutputCP(), 0, string, len, buf, lena, NULL, NULL);
90 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buf, lena, &count, FALSE);
91 heap_free(buf);
92 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), "\r\n", 2, &count, FALSE);
95 static HRESULT WINAPI Host_QueryInterface(IHost *iface, REFIID riid, void **ppv)
97 WINE_TRACE("(%s %p)\n", wine_dbgstr_guid(riid), ppv);
99 if(IsEqualGUID(&IID_IUnknown, riid)
100 || IsEqualGUID(&IID_IDispatch, riid)
101 || IsEqualGUID(&IID_IHost, riid)) {
102 *ppv = iface;
103 return S_OK;
106 *ppv = NULL;
107 return E_NOINTERFACE;
110 static ULONG WINAPI Host_AddRef(IHost *iface)
112 return 2;
115 static ULONG WINAPI Host_Release(IHost *iface)
117 return 1;
120 static HRESULT WINAPI Host_GetTypeInfoCount(IHost *iface, UINT *pctinfo)
122 WINE_TRACE("(%p)\n", pctinfo);
123 *pctinfo = 1;
124 return S_OK;
127 static HRESULT WINAPI Host_GetTypeInfo(IHost *iface, UINT iTInfo, LCID lcid,
128 ITypeInfo **ppTInfo)
130 WINE_TRACE("(%x %x %p\n", iTInfo, lcid, ppTInfo);
132 ITypeInfo_AddRef(host_ti);
133 *ppTInfo = host_ti;
134 return S_OK;
137 static HRESULT WINAPI Host_GetIDsOfNames(IHost *iface, REFIID riid, LPOLESTR *rgszNames,
138 UINT cNames, LCID lcid, DISPID *rgDispId)
140 WINE_TRACE("(%s %p %d %x %p)\n", wine_dbgstr_guid(riid), rgszNames,
141 cNames, lcid, rgDispId);
143 return ITypeInfo_GetIDsOfNames(host_ti, rgszNames, cNames, rgDispId);
146 static HRESULT WINAPI Host_Invoke(IHost *iface, DISPID dispIdMember, REFIID riid,
147 LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult,
148 EXCEPINFO *pExcepInfo, UINT *puArgErr)
150 WINE_TRACE("(%d %p %p)\n", dispIdMember, pDispParams, pVarResult);
152 return ITypeInfo_Invoke(host_ti, iface, dispIdMember, wFlags, pDispParams,
153 pVarResult, pExcepInfo, puArgErr);
156 static HRESULT WINAPI Host_get_Name(IHost *iface, BSTR *out_Name)
158 WINE_TRACE("(%p)\n", out_Name);
160 if(!(*out_Name = SysAllocString(L"Windows Script Host")))
161 return E_OUTOFMEMORY;
162 return S_OK;
165 static HRESULT WINAPI Host_get_Application(IHost *iface, IDispatch **out_Dispatch)
167 WINE_TRACE("(%p)\n", out_Dispatch);
169 *out_Dispatch = (IDispatch*)&host_obj;
170 return S_OK;
173 static HRESULT WINAPI Host_get_FullName(IHost *iface, BSTR *out_Path)
175 WCHAR fullPath[MAX_PATH];
177 WINE_TRACE("(%p)\n", out_Path);
179 if(GetModuleFileNameW(NULL, fullPath, ARRAY_SIZE(fullPath)) == 0)
180 return E_FAIL;
181 if(!(*out_Path = SysAllocString(fullPath)))
182 return E_OUTOFMEMORY;
183 return S_OK;
186 static HRESULT WINAPI Host_get_Path(IHost *iface, BSTR *out_Path)
188 WCHAR path[MAX_PATH];
189 int howMany;
190 WCHAR *pos;
192 WINE_TRACE("(%p)\n", out_Path);
194 if(GetModuleFileNameW(NULL, path, ARRAY_SIZE(path)) == 0)
195 return E_FAIL;
196 pos = wcsrchr(path, '\\');
197 howMany = pos - path;
198 if(!(*out_Path = SysAllocStringLen(path, howMany)))
199 return E_OUTOFMEMORY;
200 return S_OK;
203 static HRESULT WINAPI Host_get_Interactive(IHost *iface, VARIANT_BOOL *out_Interactive)
205 WINE_TRACE("(%p)\n", out_Interactive);
207 *out_Interactive = wshInteractive;
208 return S_OK;
211 static HRESULT WINAPI Host_put_Interactive(IHost *iface, VARIANT_BOOL v)
213 WINE_TRACE("(%x)\n", v);
215 wshInteractive = v;
216 return S_OK;
219 static HRESULT WINAPI Host_Quit(IHost *iface, int ExitCode)
221 FIXME("(%d) semi-stub: no script engine clean up\n", ExitCode);
223 ExitProcess(ExitCode);
224 return S_OK;
227 static HRESULT WINAPI Host_get_ScriptName(IHost *iface, BSTR *out_ScriptName)
229 WCHAR *scriptName;
231 WINE_TRACE("(%p)\n", out_ScriptName);
233 scriptName = wcsrchr(scriptFullName, '\\');
234 ++scriptName;
235 if(!(*out_ScriptName = SysAllocString(scriptName)))
236 return E_OUTOFMEMORY;
237 return S_OK;
240 static HRESULT WINAPI Host_get_ScriptFullName(IHost *iface, BSTR *out_ScriptFullName)
242 WINE_TRACE("(%p)\n", out_ScriptFullName);
244 if(!(*out_ScriptFullName = SysAllocString(scriptFullName)))
245 return E_OUTOFMEMORY;
246 return S_OK;
249 static HRESULT WINAPI Host_get_Arguments(IHost *iface, IArguments2 **out_Arguments)
251 WINE_TRACE("(%p)\n", out_Arguments);
253 *out_Arguments = &arguments_obj;
254 return S_OK;
257 static HRESULT WINAPI Host_get_Version(IHost *iface, BSTR *out_Version)
259 WINE_TRACE("(%p)\n", out_Version);
261 if(!(*out_Version = SysAllocString(wshVersionW)))
262 return E_OUTOFMEMORY;
263 return S_OK;
266 static HRESULT WINAPI Host_get_BuildVersion(IHost *iface, int *out_Build)
268 WINE_TRACE("(%p)\n", out_Build);
270 *out_Build = BUILDVERSION;
271 return S_OK;
274 static HRESULT WINAPI Host_get_Timeout(IHost *iface, LONG *out_Timeout)
276 WINE_FIXME("(%p)\n", out_Timeout);
277 return E_NOTIMPL;
280 static HRESULT WINAPI Host_put_Timeout(IHost *iface, LONG v)
282 WINE_FIXME("(%d)\n", v);
283 return E_NOTIMPL;
286 static HRESULT WINAPI Host_CreateObject(IHost *iface, BSTR ProgID, BSTR Prefix,
287 IDispatch **out_Dispatch)
289 IUnknown *unk;
290 GUID guid;
291 HRESULT hres;
293 TRACE("(%s %s %p)\n", wine_dbgstr_w(ProgID), wine_dbgstr_w(Prefix), out_Dispatch);
295 if(Prefix && *Prefix) {
296 FIXME("Prefix %s not supported\n", debugstr_w(Prefix));
297 return E_NOTIMPL;
300 hres = CLSIDFromProgID(ProgID, &guid);
301 if(FAILED(hres))
302 return hres;
304 hres = CoCreateInstance(&guid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER|CLSCTX_REMOTE_SERVER,
305 &IID_IUnknown, (void**)&unk);
306 if(FAILED(hres))
307 return hres;
309 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)out_Dispatch);
310 IUnknown_Release(unk);
311 return hres;
314 static HRESULT WINAPI Host_Echo(IHost *iface, SAFEARRAY *args)
316 WCHAR *output = NULL, *ptr;
317 unsigned argc, i, len;
318 int ubound, lbound;
319 VARIANT *argv;
320 BSTR *strs;
321 HRESULT hres;
323 TRACE("(%p)\n", args);
325 if(SafeArrayGetDim(args) != 1) {
326 FIXME("Unsupported args dim %d\n", SafeArrayGetDim(args));
327 return E_NOTIMPL;
330 SafeArrayGetLBound(args, 1, &lbound);
331 SafeArrayGetUBound(args, 1, &ubound);
333 hres = SafeArrayAccessData(args, (void**)&argv);
334 if(FAILED(hres))
335 return hres;
337 argc = ubound-lbound+1;
338 strs = heap_alloc_zero(argc*sizeof(*strs));
339 if(!strs) {
340 SafeArrayUnaccessData(args);
341 return E_OUTOFMEMORY;
344 /* Len of spaces between arguments. */
345 len = argc-1;
347 for(i=0; i < argc; i++) {
348 hres = to_string(argv+i, strs+i);
349 if(FAILED(hres))
350 break;
352 len += SysStringLen(strs[i]);
355 SafeArrayUnaccessData(args);
356 if(SUCCEEDED(hres)) {
357 ptr = output = heap_alloc((len+1)*sizeof(WCHAR));
358 if(output) {
359 for(i=0; i < argc; i++) {
360 if(i)
361 *ptr++ = ' ';
362 len = SysStringLen(strs[i]);
363 memcpy(ptr, strs[i], len*sizeof(WCHAR));
364 ptr += len;
366 *ptr = 0;
367 }else {
368 hres = E_OUTOFMEMORY;
372 for(i=0; i < argc; i++)
373 SysFreeString(strs[i]);
374 heap_free(strs);
375 if(FAILED(hres))
376 return hres;
378 print_string(output);
380 heap_free(output);
381 return S_OK;
384 static HRESULT WINAPI Host_GetObject(IHost *iface, BSTR Pathname, BSTR ProgID,
385 BSTR Prefix, IDispatch **out_Dispatch)
387 WINE_FIXME("(%s %s %s %p)\n", wine_dbgstr_w(Pathname), wine_dbgstr_w(ProgID),
388 wine_dbgstr_w(Prefix), out_Dispatch);
389 return E_NOTIMPL;
392 static HRESULT WINAPI Host_DisconnectObject(IHost *iface, IDispatch *Object)
394 WINE_FIXME("(%p)\n", Object);
395 return E_NOTIMPL;
398 static HRESULT WINAPI Host_Sleep(IHost *iface, LONG Time)
400 WINE_FIXME("(%d)\n", Time);
401 return E_NOTIMPL;
404 static HRESULT WINAPI Host_ConnectObject(IHost *iface, IDispatch *Object, BSTR Prefix)
406 WINE_FIXME("(%p %s)\n", Object, wine_dbgstr_w(Prefix));
407 return E_NOTIMPL;
410 static HRESULT WINAPI Host_get_StdIn(IHost *iface, ITextStream **ppts)
412 WINE_FIXME("(%p)\n", ppts);
413 return E_NOTIMPL;
416 static HRESULT WINAPI Host_get_StdOut(IHost *iface, ITextStream **ppts)
418 WINE_FIXME("(%p)\n", ppts);
419 return E_NOTIMPL;
422 static HRESULT WINAPI Host_get_StdErr(IHost *iface, ITextStream **ppts)
424 WINE_FIXME("(%p)\n", ppts);
425 return E_NOTIMPL;
428 static const IHostVtbl HostVtbl = {
429 Host_QueryInterface,
430 Host_AddRef,
431 Host_Release,
432 Host_GetTypeInfoCount,
433 Host_GetTypeInfo,
434 Host_GetIDsOfNames,
435 Host_Invoke,
436 Host_get_Name,
437 Host_get_Application,
438 Host_get_FullName,
439 Host_get_Path,
440 Host_get_Interactive,
441 Host_put_Interactive,
442 Host_Quit,
443 Host_get_ScriptName,
444 Host_get_ScriptFullName,
445 Host_get_Arguments,
446 Host_get_Version,
447 Host_get_BuildVersion,
448 Host_get_Timeout,
449 Host_put_Timeout,
450 Host_CreateObject,
451 Host_Echo,
452 Host_GetObject,
453 Host_DisconnectObject,
454 Host_Sleep,
455 Host_ConnectObject,
456 Host_get_StdIn,
457 Host_get_StdOut,
458 Host_get_StdErr
461 IHost host_obj = { &HostVtbl };