kernelbase: Allow the two initial slashes to be backslashes in UrlGetPart().
[wine.git] / programs / wscript / host.c
blobbd16b7b4cbe758bff68230e6c8ab0c98029e832d
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[] = L"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 WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), L"\r\n", lstrlenW(L"\r\n"), &count, NULL);
80 return;
83 lena = WideCharToMultiByte(GetConsoleOutputCP(), 0, string, len, NULL, 0, NULL, NULL);
84 buf = heap_alloc(len);
85 if(!buf)
86 return;
88 WideCharToMultiByte(GetConsoleOutputCP(), 0, string, len, buf, lena, NULL, NULL);
89 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buf, lena, &count, FALSE);
90 heap_free(buf);
91 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), "\r\n", 2, &count, FALSE);
94 static HRESULT WINAPI Host_QueryInterface(IHost *iface, REFIID riid, void **ppv)
96 WINE_TRACE("(%s %p)\n", wine_dbgstr_guid(riid), ppv);
98 if(IsEqualGUID(&IID_IUnknown, riid)
99 || IsEqualGUID(&IID_IDispatch, riid)
100 || IsEqualGUID(&IID_IHost, riid)) {
101 *ppv = iface;
102 return S_OK;
105 *ppv = NULL;
106 return E_NOINTERFACE;
109 static ULONG WINAPI Host_AddRef(IHost *iface)
111 return 2;
114 static ULONG WINAPI Host_Release(IHost *iface)
116 return 1;
119 static HRESULT WINAPI Host_GetTypeInfoCount(IHost *iface, UINT *pctinfo)
121 WINE_TRACE("(%p)\n", pctinfo);
122 *pctinfo = 1;
123 return S_OK;
126 static HRESULT WINAPI Host_GetTypeInfo(IHost *iface, UINT iTInfo, LCID lcid,
127 ITypeInfo **ppTInfo)
129 WINE_TRACE("(%x %lx %p\n", iTInfo, lcid, ppTInfo);
131 ITypeInfo_AddRef(host_ti);
132 *ppTInfo = host_ti;
133 return S_OK;
136 static HRESULT WINAPI Host_GetIDsOfNames(IHost *iface, REFIID riid, LPOLESTR *rgszNames,
137 UINT cNames, LCID lcid, DISPID *rgDispId)
139 WINE_TRACE("(%s %p %d %lx %p)\n", wine_dbgstr_guid(riid), rgszNames,
140 cNames, lcid, rgDispId);
142 return ITypeInfo_GetIDsOfNames(host_ti, rgszNames, cNames, rgDispId);
145 static HRESULT WINAPI Host_Invoke(IHost *iface, DISPID dispIdMember, REFIID riid,
146 LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult,
147 EXCEPINFO *pExcepInfo, UINT *puArgErr)
149 WINE_TRACE("(%ld %p %p)\n", dispIdMember, pDispParams, pVarResult);
151 return ITypeInfo_Invoke(host_ti, iface, dispIdMember, wFlags, pDispParams,
152 pVarResult, pExcepInfo, puArgErr);
155 static HRESULT WINAPI Host_get_Name(IHost *iface, BSTR *out_Name)
157 WINE_TRACE("(%p)\n", out_Name);
159 if(!(*out_Name = SysAllocString(L"Windows Script Host")))
160 return E_OUTOFMEMORY;
161 return S_OK;
164 static HRESULT WINAPI Host_get_Application(IHost *iface, IDispatch **out_Dispatch)
166 WINE_TRACE("(%p)\n", out_Dispatch);
168 *out_Dispatch = (IDispatch*)&host_obj;
169 return S_OK;
172 static HRESULT WINAPI Host_get_FullName(IHost *iface, BSTR *out_Path)
174 WCHAR fullPath[MAX_PATH];
176 WINE_TRACE("(%p)\n", out_Path);
178 if(GetModuleFileNameW(NULL, fullPath, ARRAY_SIZE(fullPath)) == 0)
179 return E_FAIL;
180 if(!(*out_Path = SysAllocString(fullPath)))
181 return E_OUTOFMEMORY;
182 return S_OK;
185 static HRESULT WINAPI Host_get_Path(IHost *iface, BSTR *out_Path)
187 WCHAR path[MAX_PATH];
188 int howMany;
189 WCHAR *pos;
191 WINE_TRACE("(%p)\n", out_Path);
193 if(GetModuleFileNameW(NULL, path, ARRAY_SIZE(path)) == 0)
194 return E_FAIL;
195 pos = wcsrchr(path, '\\');
196 howMany = pos - path;
197 if(!(*out_Path = SysAllocStringLen(path, howMany)))
198 return E_OUTOFMEMORY;
199 return S_OK;
202 static HRESULT WINAPI Host_get_Interactive(IHost *iface, VARIANT_BOOL *out_Interactive)
204 WINE_TRACE("(%p)\n", out_Interactive);
206 *out_Interactive = wshInteractive;
207 return S_OK;
210 static HRESULT WINAPI Host_put_Interactive(IHost *iface, VARIANT_BOOL v)
212 WINE_TRACE("(%x)\n", v);
214 wshInteractive = v;
215 return S_OK;
218 static HRESULT WINAPI Host_Quit(IHost *iface, int ExitCode)
220 FIXME("(%d) semi-stub: no script engine clean up\n", ExitCode);
222 ExitProcess(ExitCode);
223 return S_OK;
226 static HRESULT WINAPI Host_get_ScriptName(IHost *iface, BSTR *out_ScriptName)
228 WCHAR *scriptName;
230 WINE_TRACE("(%p)\n", out_ScriptName);
232 scriptName = wcsrchr(scriptFullName, '\\');
233 ++scriptName;
234 if(!(*out_ScriptName = SysAllocString(scriptName)))
235 return E_OUTOFMEMORY;
236 return S_OK;
239 static HRESULT WINAPI Host_get_ScriptFullName(IHost *iface, BSTR *out_ScriptFullName)
241 WINE_TRACE("(%p)\n", out_ScriptFullName);
243 if(!(*out_ScriptFullName = SysAllocString(scriptFullName)))
244 return E_OUTOFMEMORY;
245 return S_OK;
248 static HRESULT WINAPI Host_get_Arguments(IHost *iface, IArguments2 **out_Arguments)
250 WINE_TRACE("(%p)\n", out_Arguments);
252 *out_Arguments = &arguments_obj;
253 return S_OK;
256 static HRESULT WINAPI Host_get_Version(IHost *iface, BSTR *out_Version)
258 WINE_TRACE("(%p)\n", out_Version);
260 if(!(*out_Version = SysAllocString(wshVersionW)))
261 return E_OUTOFMEMORY;
262 return S_OK;
265 static HRESULT WINAPI Host_get_BuildVersion(IHost *iface, int *out_Build)
267 WINE_TRACE("(%p)\n", out_Build);
269 *out_Build = BUILDVERSION;
270 return S_OK;
273 static HRESULT WINAPI Host_get_Timeout(IHost *iface, LONG *out_Timeout)
275 WINE_FIXME("(%p)\n", out_Timeout);
276 return E_NOTIMPL;
279 static HRESULT WINAPI Host_put_Timeout(IHost *iface, LONG v)
281 WINE_FIXME("(%ld)\n", v);
282 return E_NOTIMPL;
285 static HRESULT WINAPI Host_CreateObject(IHost *iface, BSTR ProgID, BSTR Prefix,
286 IDispatch **out_Dispatch)
288 IUnknown *unk;
289 GUID guid;
290 HRESULT hres;
292 TRACE("(%s %s %p)\n", wine_dbgstr_w(ProgID), wine_dbgstr_w(Prefix), out_Dispatch);
294 if(Prefix && *Prefix) {
295 FIXME("Prefix %s not supported\n", debugstr_w(Prefix));
296 return E_NOTIMPL;
299 hres = CLSIDFromProgID(ProgID, &guid);
300 if(FAILED(hres))
301 return hres;
303 hres = CoCreateInstance(&guid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER|CLSCTX_REMOTE_SERVER,
304 &IID_IUnknown, (void**)&unk);
305 if(FAILED(hres))
306 return hres;
308 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)out_Dispatch);
309 IUnknown_Release(unk);
310 return hres;
313 static HRESULT WINAPI Host_Echo(IHost *iface, SAFEARRAY *args)
315 WCHAR *output = NULL, *ptr;
316 unsigned argc, i, len;
317 LONG ubound, lbound;
318 VARIANT *argv;
319 BSTR *strs;
320 HRESULT hres;
322 TRACE("(%p)\n", args);
324 if(SafeArrayGetDim(args) != 1) {
325 FIXME("Unsupported args dim %d\n", SafeArrayGetDim(args));
326 return E_NOTIMPL;
329 SafeArrayGetLBound(args, 1, &lbound);
330 SafeArrayGetUBound(args, 1, &ubound);
332 hres = SafeArrayAccessData(args, (void**)&argv);
333 if(FAILED(hres))
334 return hres;
336 argc = ubound-lbound+1;
337 strs = heap_alloc_zero(argc*sizeof(*strs));
338 if(!strs) {
339 SafeArrayUnaccessData(args);
340 return E_OUTOFMEMORY;
343 /* Len of spaces between arguments. */
344 len = argc-1;
346 for(i=0; i < argc; i++) {
347 hres = to_string(argv+i, strs+i);
348 if(FAILED(hres))
349 break;
351 len += SysStringLen(strs[i]);
354 SafeArrayUnaccessData(args);
355 if(SUCCEEDED(hres)) {
356 ptr = output = heap_alloc((len+1)*sizeof(WCHAR));
357 if(output) {
358 for(i=0; i < argc; i++) {
359 if(i)
360 *ptr++ = ' ';
361 len = SysStringLen(strs[i]);
362 memcpy(ptr, strs[i], len*sizeof(WCHAR));
363 ptr += len;
365 *ptr = 0;
366 }else {
367 hres = E_OUTOFMEMORY;
371 for(i=0; i < argc; i++)
372 SysFreeString(strs[i]);
373 heap_free(strs);
374 if(FAILED(hres))
375 return hres;
377 print_string(output);
379 heap_free(output);
380 return S_OK;
383 static HRESULT WINAPI Host_GetObject(IHost *iface, BSTR Pathname, BSTR ProgID,
384 BSTR Prefix, IDispatch **out_Dispatch)
386 WINE_FIXME("(%s %s %s %p)\n", wine_dbgstr_w(Pathname), wine_dbgstr_w(ProgID),
387 wine_dbgstr_w(Prefix), out_Dispatch);
388 return E_NOTIMPL;
391 static HRESULT WINAPI Host_DisconnectObject(IHost *iface, IDispatch *Object)
393 WINE_FIXME("(%p)\n", Object);
394 return E_NOTIMPL;
397 static HRESULT WINAPI Host_Sleep(IHost *iface, LONG Time)
399 WINE_FIXME("(%ld)\n", Time);
400 return E_NOTIMPL;
403 static HRESULT WINAPI Host_ConnectObject(IHost *iface, IDispatch *Object, BSTR Prefix)
405 WINE_FIXME("(%p %s)\n", Object, wine_dbgstr_w(Prefix));
406 return E_NOTIMPL;
409 static HRESULT WINAPI Host_get_StdIn(IHost *iface, ITextStream **ppts)
411 WINE_FIXME("(%p)\n", ppts);
412 return E_NOTIMPL;
415 static HRESULT WINAPI Host_get_StdOut(IHost *iface, ITextStream **ppts)
417 WINE_FIXME("(%p)\n", ppts);
418 return E_NOTIMPL;
421 static HRESULT WINAPI Host_get_StdErr(IHost *iface, ITextStream **ppts)
423 WINE_FIXME("(%p)\n", ppts);
424 return E_NOTIMPL;
427 static const IHostVtbl HostVtbl = {
428 Host_QueryInterface,
429 Host_AddRef,
430 Host_Release,
431 Host_GetTypeInfoCount,
432 Host_GetTypeInfo,
433 Host_GetIDsOfNames,
434 Host_Invoke,
435 Host_get_Name,
436 Host_get_Application,
437 Host_get_FullName,
438 Host_get_Path,
439 Host_get_Interactive,
440 Host_put_Interactive,
441 Host_Quit,
442 Host_get_ScriptName,
443 Host_get_ScriptFullName,
444 Host_get_Arguments,
445 Host_get_Version,
446 Host_get_BuildVersion,
447 Host_get_Timeout,
448 Host_put_Timeout,
449 Host_CreateObject,
450 Host_Echo,
451 Host_GetObject,
452 Host_DisconnectObject,
453 Host_Sleep,
454 Host_ConnectObject,
455 Host_get_StdIn,
456 Host_get_StdOut,
457 Host_get_StdErr
460 IHost host_obj = { &HostVtbl };