* Sync up to trunk head (r65481).
[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 WINAPI Host_QueryInterface(IHost *iface, REFIID riid, void **ppv)
34 {
35 WINE_TRACE("(%s %p)\n", wine_dbgstr_guid(riid), ppv);
36
37 if(IsEqualGUID(&IID_IUnknown, riid)
38 || IsEqualGUID(&IID_IDispatch, riid)
39 || IsEqualGUID(&IID_IHost, riid)) {
40 *ppv = iface;
41 return S_OK;
42 }
43
44 *ppv = NULL;
45 return E_NOINTERFACE;
46 }
47
48 static ULONG WINAPI Host_AddRef(IHost *iface)
49 {
50 return 2;
51 }
52
53 static ULONG WINAPI Host_Release(IHost *iface)
54 {
55 return 1;
56 }
57
58 static HRESULT WINAPI Host_GetTypeInfoCount(IHost *iface, UINT *pctinfo)
59 {
60 WINE_TRACE("(%p)\n", pctinfo);
61 *pctinfo = 1;
62 return S_OK;
63 }
64
65 static HRESULT WINAPI Host_GetTypeInfo(IHost *iface, UINT iTInfo, LCID lcid,
66 ITypeInfo **ppTInfo)
67 {
68 WINE_TRACE("(%x %x %p\n", iTInfo, lcid, ppTInfo);
69
70 ITypeInfo_AddRef(host_ti);
71 *ppTInfo = host_ti;
72 return S_OK;
73 }
74
75 static HRESULT WINAPI Host_GetIDsOfNames(IHost *iface, REFIID riid, LPOLESTR *rgszNames,
76 UINT cNames, LCID lcid, DISPID *rgDispId)
77 {
78 WINE_TRACE("(%s %p %d %x %p)\n", wine_dbgstr_guid(riid), rgszNames,
79 cNames, lcid, rgDispId);
80
81 return ITypeInfo_GetIDsOfNames(host_ti, rgszNames, cNames, rgDispId);
82 }
83
84 static HRESULT WINAPI Host_Invoke(IHost *iface, DISPID dispIdMember, REFIID riid,
85 LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult,
86 EXCEPINFO *pExcepInfo, UINT *puArgErr)
87 {
88 WINE_TRACE("(%d %p %p)\n", dispIdMember, pDispParams, pVarResult);
89
90 return ITypeInfo_Invoke(host_ti, iface, dispIdMember, wFlags, pDispParams,
91 pVarResult, pExcepInfo, puArgErr);
92 }
93
94 static HRESULT WINAPI Host_get_Name(IHost *iface, BSTR *out_Name)
95 {
96 WINE_TRACE("(%p)\n", out_Name);
97
98 if(!(*out_Name = SysAllocString(wshNameW)))
99 return E_OUTOFMEMORY;
100 return S_OK;
101 }
102
103 static HRESULT WINAPI Host_get_Application(IHost *iface, IDispatch **out_Dispatch)
104 {
105 WINE_TRACE("(%p)\n", out_Dispatch);
106
107 *out_Dispatch = (IDispatch*)&host_obj;
108 return S_OK;
109 }
110
111 static HRESULT WINAPI Host_get_FullName(IHost *iface, BSTR *out_Path)
112 {
113 WCHAR fullPath[MAX_PATH];
114
115 WINE_TRACE("(%p)\n", out_Path);
116
117 if(GetModuleFileNameW(NULL, fullPath, sizeof(fullPath)/sizeof(WCHAR)) == 0)
118 return E_FAIL;
119 if(!(*out_Path = SysAllocString(fullPath)))
120 return E_OUTOFMEMORY;
121 return S_OK;
122 }
123
124 static HRESULT WINAPI Host_get_Path(IHost *iface, BSTR *out_Path)
125 {
126 WCHAR path[MAX_PATH];
127 int howMany;
128 WCHAR *pos;
129
130 WINE_TRACE("(%p)\n", out_Path);
131
132 if(GetModuleFileNameW(NULL, path, sizeof(path)/sizeof(WCHAR)) == 0)
133 return E_FAIL;
134 pos = strrchrW(path, '\\');
135 howMany = pos - path;
136 if(!(*out_Path = SysAllocStringLen(path, howMany)))
137 return E_OUTOFMEMORY;
138 return S_OK;
139 }
140
141 static HRESULT WINAPI Host_get_Interactive(IHost *iface, VARIANT_BOOL *out_Interactive)
142 {
143 WINE_TRACE("(%p)\n", out_Interactive);
144
145 *out_Interactive = wshInteractive;
146 return S_OK;
147 }
148
149 static HRESULT WINAPI Host_put_Interactive(IHost *iface, VARIANT_BOOL v)
150 {
151 WINE_TRACE("(%x)\n", v);
152
153 wshInteractive = v;
154 return S_OK;
155 }
156
157 static HRESULT WINAPI Host_Quit(IHost *iface, int ExitCode)
158 {
159 WINE_FIXME("(%d)\n", ExitCode);
160 return E_NOTIMPL;
161 }
162
163 static HRESULT WINAPI Host_get_ScriptName(IHost *iface, BSTR *out_ScriptName)
164 {
165 WCHAR *scriptName;
166
167 WINE_TRACE("(%p)\n", out_ScriptName);
168
169 scriptName = strrchrW(scriptFullName, '\\');
170 ++scriptName;
171 if(!(*out_ScriptName = SysAllocString(scriptName)))
172 return E_OUTOFMEMORY;
173 return S_OK;
174 }
175
176 static HRESULT WINAPI Host_get_ScriptFullName(IHost *iface, BSTR *out_ScriptFullName)
177 {
178 WINE_TRACE("(%p)\n", out_ScriptFullName);
179
180 if(!(*out_ScriptFullName = SysAllocString(scriptFullName)))
181 return E_OUTOFMEMORY;
182 return S_OK;
183 }
184
185 static HRESULT WINAPI Host_get_Arguments(IHost *iface, IArguments2 **out_Arguments)
186 {
187 WINE_TRACE("(%p)\n", out_Arguments);
188
189 *out_Arguments = &arguments_obj;
190 return S_OK;
191 }
192
193 static HRESULT WINAPI Host_get_Version(IHost *iface, BSTR *out_Version)
194 {
195 WINE_TRACE("(%p)\n", out_Version);
196
197 if(!(*out_Version = SysAllocString(wshVersionW)))
198 return E_OUTOFMEMORY;
199 return S_OK;
200 }
201
202 static HRESULT WINAPI Host_get_BuildVersion(IHost *iface, int *out_Build)
203 {
204 WINE_TRACE("(%p)\n", out_Build);
205
206 *out_Build = BUILDVERSION;
207 return S_OK;
208 }
209
210 static HRESULT WINAPI Host_get_Timeout(IHost *iface, LONG *out_Timeout)
211 {
212 WINE_FIXME("(%p)\n", out_Timeout);
213 return E_NOTIMPL;
214 }
215
216 static HRESULT WINAPI Host_put_Timeout(IHost *iface, LONG v)
217 {
218 WINE_FIXME("(%d)\n", v);
219 return E_NOTIMPL;
220 }
221
222 static HRESULT WINAPI Host_CreateObject(IHost *iface, BSTR ProgID, BSTR Prefix,
223 IDispatch **out_Dispatch)
224 {
225 IUnknown *unk;
226 GUID guid;
227 HRESULT hres;
228
229 TRACE("(%s %s %p)\n", wine_dbgstr_w(ProgID), wine_dbgstr_w(Prefix), out_Dispatch);
230
231 if(Prefix && *Prefix) {
232 FIXME("Prefix %s not supported\n", debugstr_w(Prefix));
233 return E_NOTIMPL;
234 }
235
236 hres = CLSIDFromProgID(ProgID, &guid);
237 if(FAILED(hres))
238 return hres;
239
240 hres = CoCreateInstance(&guid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER|CLSCTX_REMOTE_SERVER,
241 &IID_IUnknown, (void**)&unk);
242 if(FAILED(hres))
243 return hres;
244
245 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)out_Dispatch);
246 IUnknown_Release(unk);
247 return hres;
248 }
249
250 static HRESULT WINAPI Host_Echo(IHost *iface, SAFEARRAY *args)
251 {
252 WINE_FIXME("(%p)\n", args);
253 return E_NOTIMPL;
254 }
255
256 static HRESULT WINAPI Host_GetObject(IHost *iface, BSTR Pathname, BSTR ProgID,
257 BSTR Prefix, IDispatch **out_Dispatch)
258 {
259 WINE_FIXME("(%s %s %s %p)\n", wine_dbgstr_w(Pathname), wine_dbgstr_w(ProgID),
260 wine_dbgstr_w(Prefix), out_Dispatch);
261 return E_NOTIMPL;
262 }
263
264 static HRESULT WINAPI Host_DisconnectObject(IHost *iface, IDispatch *Object)
265 {
266 WINE_FIXME("(%p)\n", Object);
267 return E_NOTIMPL;
268 }
269
270 static HRESULT WINAPI Host_Sleep(IHost *iface, LONG Time)
271 {
272 WINE_FIXME("(%d)\n", Time);
273 return E_NOTIMPL;
274 }
275
276 static HRESULT WINAPI Host_ConnectObject(IHost *iface, IDispatch *Object, BSTR Prefix)
277 {
278 WINE_FIXME("(%p %s)\n", Object, wine_dbgstr_w(Prefix));
279 return E_NOTIMPL;
280 }
281
282 static HRESULT WINAPI Host_get_StdIn(IHost *iface, ITextStream **ppts)
283 {
284 WINE_FIXME("(%p)\n", ppts);
285 return E_NOTIMPL;
286 }
287
288 static HRESULT WINAPI Host_get_StdOut(IHost *iface, ITextStream **ppts)
289 {
290 WINE_FIXME("(%p)\n", ppts);
291 return E_NOTIMPL;
292 }
293
294 static HRESULT WINAPI Host_get_StdErr(IHost *iface, ITextStream **ppts)
295 {
296 WINE_FIXME("(%p)\n", ppts);
297 return E_NOTIMPL;
298 }
299
300 static const IHostVtbl HostVtbl = {
301 Host_QueryInterface,
302 Host_AddRef,
303 Host_Release,
304 Host_GetTypeInfoCount,
305 Host_GetTypeInfo,
306 Host_GetIDsOfNames,
307 Host_Invoke,
308 Host_get_Name,
309 Host_get_Application,
310 Host_get_FullName,
311 Host_get_Path,
312 Host_get_Interactive,
313 Host_put_Interactive,
314 Host_Quit,
315 Host_get_ScriptName,
316 Host_get_ScriptFullName,
317 Host_get_Arguments,
318 Host_get_Version,
319 Host_get_BuildVersion,
320 Host_get_Timeout,
321 Host_put_Timeout,
322 Host_CreateObject,
323 Host_Echo,
324 Host_GetObject,
325 Host_DisconnectObject,
326 Host_Sleep,
327 Host_ConnectObject,
328 Host_get_StdIn,
329 Host_get_StdOut,
330 Host_get_StdErr
331 };
332
333 IHost host_obj = { &HostVtbl };