richedit: Invalidate without calling UpdateWindow in many cases.
[wine/hramrach.git] / programs / wscript / main.c
blobf94c3aed8027497cf7be12a4f21eb4ac08cf3c57
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
23 #include <windef.h>
24 #include <winbase.h>
25 #include <winreg.h>
26 #include <ole2.h>
27 #include <activscp.h>
29 #include <wine/debug.h>
30 #include <wine/unicode.h>
32 WINE_DEFAULT_DEBUG_CHANNEL(wscript);
34 static HRESULT WINAPI ActiveScriptSite_QueryInterface(IActiveScriptSite *iface,
35 REFIID riid, void **ppv)
37 if(IsEqualGUID(riid, &IID_IUnknown)) {
38 WINE_TRACE("(IID_IUnknown %p)\n", ppv);
39 *ppv = iface;
40 }else if(IsEqualGUID(riid, &IID_IActiveScriptSite)) {
41 WINE_TRACE("(IID_IActiveScriptSite %p)\n", ppv);
42 *ppv = iface;
43 }else {
44 *ppv = NULL;
45 WINE_TRACE("(%s %p)\n", wine_dbgstr_guid(riid), ppv);
46 return E_NOINTERFACE;
49 IUnknown_AddRef((IUnknown*)*ppv);
50 return S_OK;
53 static ULONG WINAPI ActiveScriptSite_AddRef(IActiveScriptSite *iface)
55 return 2;
58 static ULONG WINAPI ActiveScriptSite_Release(IActiveScriptSite *iface)
60 return 1;
63 static HRESULT WINAPI ActiveScriptSite_GetLCID(IActiveScriptSite *iface, LCID *plcid)
65 WINE_TRACE("()\n");
67 *plcid = GetUserDefaultLCID();
68 return S_OK;
71 static HRESULT WINAPI ActiveScriptSite_GetItemInfo(IActiveScriptSite *iface,
72 LPCOLESTR pstrName, DWORD dwReturnMask, IUnknown **ppunkItem, ITypeInfo **ppti)
74 WINE_FIXME("(%s %x %p %p)\n", wine_dbgstr_w(pstrName), dwReturnMask, ppunkItem, ppti);
75 return E_NOTIMPL;
78 static HRESULT WINAPI ActiveScriptSite_GetDocVersionString(IActiveScriptSite *iface,
79 BSTR *pbstrVersion)
81 WINE_FIXME("()\n");
82 return E_NOTIMPL;
85 static HRESULT WINAPI ActiveScriptSite_OnScriptTerminate(IActiveScriptSite *iface,
86 const VARIANT *pvarResult, const EXCEPINFO *pexcepinfo)
88 WINE_FIXME("()\n");
89 return E_NOTIMPL;
92 static HRESULT WINAPI ActiveScriptSite_OnStateChange(IActiveScriptSite *iface,
93 SCRIPTSTATE ssScriptState)
95 WINE_TRACE("(%x)\n", ssScriptState);
96 return S_OK;
99 static HRESULT WINAPI ActiveScriptSite_OnScriptError(IActiveScriptSite *iface,
100 IActiveScriptError *pscripterror)
102 WINE_FIXME("()\n");
103 return E_NOTIMPL;
106 static HRESULT WINAPI ActiveScriptSite_OnEnterScript(IActiveScriptSite *iface)
108 WINE_TRACE("()\n");
109 return S_OK;
112 static HRESULT WINAPI ActiveScriptSite_OnLeaveScript(IActiveScriptSite *iface)
114 WINE_TRACE("()\n");
115 return S_OK;
118 static IActiveScriptSiteVtbl ActiveScriptSiteVtbl = {
119 ActiveScriptSite_QueryInterface,
120 ActiveScriptSite_AddRef,
121 ActiveScriptSite_Release,
122 ActiveScriptSite_GetLCID,
123 ActiveScriptSite_GetItemInfo,
124 ActiveScriptSite_GetDocVersionString,
125 ActiveScriptSite_OnScriptTerminate,
126 ActiveScriptSite_OnStateChange,
127 ActiveScriptSite_OnScriptError,
128 ActiveScriptSite_OnEnterScript,
129 ActiveScriptSite_OnLeaveScript
132 IActiveScriptSite script_site = { &ActiveScriptSiteVtbl };
134 static BOOL get_engine_clsid(const WCHAR *ext, CLSID *clsid)
136 WCHAR fileid[64], progid[64];
137 DWORD res;
138 LONG size;
139 HKEY hkey;
140 HRESULT hres;
142 static const WCHAR script_engineW[] =
143 {'\\','S','c','r','i','p','t','E','n','g','i','n','e',0};
145 res = RegOpenKeyW(HKEY_CLASSES_ROOT, ext, &hkey);
146 if(res != ERROR_SUCCESS)
147 return FALSE;
149 size = sizeof(fileid)/sizeof(WCHAR);
150 res = RegQueryValueW(hkey, NULL, fileid, &size);
151 RegCloseKey(hkey);
152 if(res != ERROR_SUCCESS)
153 return FALSE;
155 WINE_TRACE("fileid is %s\n", wine_dbgstr_w(fileid));
157 strcatW(fileid, script_engineW);
158 res = RegOpenKeyW(HKEY_CLASSES_ROOT, fileid, &hkey);
159 if(res != ERROR_SUCCESS)
160 return FALSE;
162 size = sizeof(progid)/sizeof(WCHAR);
163 res = RegQueryValueW(hkey, NULL, progid, &size);
164 RegCloseKey(hkey);
165 if(res != ERROR_SUCCESS)
166 return FALSE;
168 WINE_TRACE("ProgID is %s\n", wine_dbgstr_w(progid));
170 hres = CLSIDFromProgID(progid, clsid);
171 return SUCCEEDED(hres);
174 static HRESULT create_engine(CLSID *clsid, IActiveScript **script_ret,
175 IActiveScriptParse **parser)
177 IActiveScript *script;
178 IUnknown *unk;
179 HRESULT hres;
181 hres = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
182 &IID_IUnknown, (void**)&unk);
183 if(FAILED(hres))
184 return FALSE;
186 hres = IUnknown_QueryInterface(unk, &IID_IActiveScript, (void**)&script);
187 IUnknown_Release(unk);
188 if(FAILED(hres))
189 return FALSE;
191 hres = IActiveScript_QueryInterface(script, &IID_IActiveScriptParse, (void**)parser);
192 if(FAILED(hres)) {
193 IActiveScript_Release(script);
194 return FALSE;
197 *script_ret = script;
198 return TRUE;
201 static HRESULT init_engine(IActiveScript *script, IActiveScriptParse *parser)
203 HRESULT hres;
205 static const WCHAR wscriptW[] = {'W','S','c','r','i','p','t',0};
206 static const WCHAR wshW[] = {'W','S','H',0};
208 hres = IActiveScript_SetScriptSite(script, &script_site);
209 if(FAILED(hres))
210 return FALSE;
212 hres = IActiveScriptParse64_InitNew(parser);
213 if(FAILED(hres))
214 return FALSE;
216 hres = IActiveScript_AddNamedItem(script, wscriptW, SCRIPTITEM_ISVISIBLE);
217 if(FAILED(hres))
218 return FALSE;
220 hres = IActiveScript_AddNamedItem(script, wshW, SCRIPTITEM_ISVISIBLE);
221 if(FAILED(hres))
222 return FALSE;
224 hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_INITIALIZED);
225 return SUCCEEDED(hres);
228 static BSTR get_script_str(const WCHAR *filename)
230 const char *file_map;
231 HANDLE file, map;
232 DWORD size, len;
233 BSTR ret;
235 file = CreateFileW(filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
236 if(file == INVALID_HANDLE_VALUE)
237 return NULL;
239 size = GetFileSize(file, NULL);
240 map = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, NULL);
241 CloseHandle(file);
242 if(map == INVALID_HANDLE_VALUE)
243 return NULL;
245 file_map = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
246 CloseHandle(map);
247 if(!file_map)
248 return NULL;
250 len = MultiByteToWideChar(CP_ACP, 0, file_map, size, NULL, 0);
251 ret = SysAllocStringLen(NULL, len);
252 MultiByteToWideChar(CP_ACP, 0, file_map, size, ret, len);
254 UnmapViewOfFile(file_map);
255 return ret;
258 static void run_script(const WCHAR *filename, IActiveScript *script, IActiveScriptParse *parser)
260 BSTR text;
261 HRESULT hres;
263 text = get_script_str(filename);
264 if(!text) {
265 WINE_FIXME("Could not get script text\n");
266 return;
269 hres = IActiveScriptParse64_ParseScriptText(parser, text, NULL, NULL, NULL, 1, 1,
270 SCRIPTTEXT_HOSTMANAGESSOURCE|SCRIPTITEM_ISVISIBLE, NULL, NULL);
271 SysFreeString(text);
272 if(FAILED(hres)) {
273 WINE_FIXME("ParseScriptText failed: %08x\n", hres);
274 return;
277 hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_STARTED);
278 if(FAILED(hres))
279 WINE_FIXME("SetScriptState failed: %08x\n", hres);
282 int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPWSTR cmdline, int cmdshow)
284 IActiveScriptParse *parser;
285 IActiveScript *script;
286 const WCHAR *ext;
287 CLSID clsid;
289 WINE_TRACE("(%p %p %s %x)\n", hInst, hPrevInst, wine_dbgstr_w(cmdline), cmdshow);
291 if(!*cmdline)
292 return 1;
294 ext = strchrW(cmdline, '.');
295 if(!ext)
296 ext = cmdline;
297 if(!get_engine_clsid(ext, &clsid)) {
298 WINE_FIXME("Could not fine engine for %s\n", wine_dbgstr_w(ext));
299 return 1;
302 CoInitialize(NULL);
304 if(!create_engine(&clsid, &script, &parser)) {
305 WINE_FIXME("Could not create script engine\n");
306 CoUninitialize();
307 return 1;
310 if(init_engine(script, parser)) {
311 run_script(cmdline, script, parser);
312 IActiveScript_Close(script);
313 }else {
314 WINE_FIXME("Script initialization failed\n");
317 IActiveScript_Release(script);
318 IUnknown_Release(parser);
320 CoUninitialize();
322 return 0;