1 /*
2 * Generation of dll registration scripts
3 *
4 * Copyright 2010 Alexandre Julliard
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
29 #include <string.h>
30 #include <ctype.h>
31
32 #include "widl.h"
33 #include "utils.h"
34 #include "parser.h"
35 #include "header.h"
36 #include "typegen.h"
37 #include "typelib.h"
38
39 static int indent;
40
format_uuid(const UUID * uuid)41 static const char *format_uuid( const UUID *uuid )
42 {
43 static char buffer[40];
44 sprintf( buffer, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
45 uuid->Data1, uuid->Data2, uuid->Data3,
46 uuid->Data4[0], uuid->Data4[1], uuid->Data4[2], uuid->Data4[3],
47 uuid->Data4[4], uuid->Data4[5], uuid->Data4[6], uuid->Data4[7] );
48 return buffer;
49 }
50
get_coclass_threading(const type_t * class)51 static const char *get_coclass_threading( const type_t *class )
52 {
53 static const char * const models[] =
54 {
55 NULL,
56 "Apartment", /* THREADING_APARTMENT */
57 "Neutral", /* THREADING_NEUTRAL */
58 "Single", /* THREADING_SINGLE */
59 "Free", /* THREADING_FREE */
60 "Both", /* THREADING_BOTH */
61 };
62 return models[get_attrv( class->attrs, ATTR_THREADING )];
63 }
64
find_ps_factory(const statement_list_t * stmts)65 static const type_t *find_ps_factory( const statement_list_t *stmts )
66 {
67 const statement_t *stmt;
68
69 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
70 {
71 if (stmt->type == STMT_TYPE)
72 {
73 const type_t *type = stmt->u.type;
74 if (type_get_type(type) == TYPE_COCLASS && !strcmp( type->name, "PSFactoryBuffer" ))
75 return type;
76 }
77 }
78 return NULL;
79 }
80
write_interface(const type_t * iface,const type_t * ps_factory)81 static void write_interface( const type_t *iface, const type_t *ps_factory )
82 {
83 const UUID *uuid = get_attrp( iface->attrs, ATTR_UUID );
84 const UUID *ps_uuid = get_attrp( ps_factory->attrs, ATTR_UUID );
85
86 if (!uuid) return;
87 if (!is_object( iface )) return;
88 if (!type_iface_get_inherit(iface)) /* special case for IUnknown */
89 {
90 put_str( indent, "'%s' = s '%s'\n", format_uuid( uuid ), iface->name );
91 return;
92 }
93 if (is_local( iface->attrs )) return;
94 put_str( indent, "'%s' = s '%s'\n", format_uuid( uuid ), iface->name );
95 put_str( indent, "{\n" );
96 indent++;
97 put_str( indent, "NumMethods = s %u\n", count_methods( iface ));
98 put_str( indent, "ProxyStubClsid32 = s '%s'\n", format_uuid( ps_uuid ));
99 indent--;
100 put_str( indent, "}\n" );
101 }
102
write_interfaces(const statement_list_t * stmts,const type_t * ps_factory)103 static void write_interfaces( const statement_list_t *stmts, const type_t *ps_factory )
104 {
105 const statement_t *stmt;
106
107 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
108 {
109 if (stmt->type == STMT_TYPE && type_get_type( stmt->u.type ) == TYPE_INTERFACE)
110 write_interface( stmt->u.type, ps_factory );
111 }
112 }
113
write_typelib_interface(const type_t * iface,const typelib_t * typelib)114 static void write_typelib_interface( const type_t *iface, const typelib_t *typelib )
115 {
116 const UUID *typelib_uuid = get_attrp( typelib->attrs, ATTR_UUID );
117 const UUID *uuid = get_attrp( iface->attrs, ATTR_UUID );
118 unsigned int version = get_attrv( typelib->attrs, ATTR_VERSION );
119
120 if (!uuid) return;
121 if (!is_object( iface )) return;
122 if (!is_attr( iface->attrs, ATTR_OLEAUTOMATION ) && !is_attr( iface->attrs, ATTR_DISPINTERFACE ))
123 return;
124 put_str( indent, "'%s' = s '%s'\n", format_uuid( uuid ), iface->name );
125 put_str( indent, "{\n" );
126 indent++;
127 put_str( indent, "ProxyStubClsid = s '{00020424-0000-0000-C000-000000000046}'\n" );
128 put_str( indent, "ProxyStubClsid32 = s '{00020424-0000-0000-C000-000000000046}'\n" );
129 if (version)
130 put_str( indent, "TypeLib = s '%s' { val Version = s '%u.%u' }\n",
131 format_uuid( typelib_uuid ), MAJORVERSION(version), MINORVERSION(version) );
132 else
133 put_str( indent, "TypeLib = s '%s'", format_uuid( typelib_uuid ));
134 indent--;
135 put_str( indent, "}\n" );
136 }
137
write_typelib_interfaces(const typelib_t * typelib)138 static void write_typelib_interfaces( const typelib_t *typelib )
139 {
140 const statement_t *stmt;
141
142 if (typelib->stmts) LIST_FOR_EACH_ENTRY( stmt, typelib->stmts, const statement_t, entry )
143 {
144 if (stmt->type == STMT_TYPE && type_get_type( stmt->u.type ) == TYPE_INTERFACE)
145 write_typelib_interface( stmt->u.type, typelib );
146 }
147 }
148
write_coclass(const type_t * class,const typelib_t * typelib)149 static int write_coclass( const type_t *class, const typelib_t *typelib )
150 {
151 const UUID *uuid = get_attrp( class->attrs, ATTR_UUID );
152 const char *descr = get_attrp( class->attrs, ATTR_HELPSTRING );
153 const char *progid = get_attrp( class->attrs, ATTR_PROGID );
154 const char *vi_progid = get_attrp( class->attrs, ATTR_VIPROGID );
155 const char *threading = get_coclass_threading( class );
156 unsigned int version = get_attrv( class->attrs, ATTR_VERSION );
157
158 if (!uuid) return 0;
159 if (typelib && !threading && !progid) return 0;
160 if (!descr) descr = class->name;
161
162 put_str( indent, "'%s' = s '%s'\n", format_uuid( uuid ), descr );
163 put_str( indent++, "{\n" );
164 if (threading) put_str( indent, "InprocServer32 = s '%%MODULE%%' { val ThreadingModel = s '%s' }\n",
165 threading );
166 if (progid) put_str( indent, "ProgId = s '%s'\n", progid );
167 if (typelib)
168 {
169 const UUID *typelib_uuid = get_attrp( typelib->attrs, ATTR_UUID );
170 put_str( indent, "TypeLib = s '%s'\n", format_uuid( typelib_uuid ));
171 if (!version) version = get_attrv( typelib->attrs, ATTR_VERSION );
172 }
173 if (version) put_str( indent, "Version = s '%u.%u'\n", MAJORVERSION(version), MINORVERSION(version) );
174 if (vi_progid) put_str( indent, "VersionIndependentProgId = s '%s'\n", vi_progid );
175 put_str( --indent, "}\n" );
176 return 1;
177 }
178
write_coclasses(const statement_list_t * stmts,const typelib_t * typelib)179 static void write_coclasses( const statement_list_t *stmts, const typelib_t *typelib )
180 {
181 const statement_t *stmt;
182
183 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
184 {
185 if (stmt->type == STMT_TYPE)
186 {
187 const type_t *type = stmt->u.type;
188 if (type_get_type(type) == TYPE_COCLASS) write_coclass( type, typelib );
189 }
190 }
191 }
192
write_progid(const type_t * class)193 static int write_progid( const type_t *class )
194 {
195 const UUID *uuid = get_attrp( class->attrs, ATTR_UUID );
196 const char *descr = get_attrp( class->attrs, ATTR_HELPSTRING );
197 const char *progid = get_attrp( class->attrs, ATTR_PROGID );
198 const char *vi_progid = get_attrp( class->attrs, ATTR_VIPROGID );
199
200 if (!uuid) return 0;
201 if (!descr) descr = class->name;
202
203 if (progid)
204 {
205 put_str( indent, "'%s' = s '%s'\n", progid, descr );
206 put_str( indent++, "{\n" );
207 put_str( indent, "CLSID = s '%s'\n", format_uuid( uuid ) );
208 put_str( --indent, "}\n" );
209 }
210 if (vi_progid)
211 {
212 put_str( indent, "'%s' = s '%s'\n", vi_progid, descr );
213 put_str( indent++, "{\n" );
214 put_str( indent, "CLSID = s '%s'\n", format_uuid( uuid ) );
215 if (progid && strcmp( progid, vi_progid )) put_str( indent, "CurVer = s '%s'\n", progid );
216 put_str( --indent, "}\n" );
217 }
218 return 1;
219 }
220
write_progids(const statement_list_t * stmts)221 static void write_progids( const statement_list_t *stmts )
222 {
223 const statement_t *stmt;
224
225 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
226 {
227 if (stmt->type == STMT_TYPE)
228 {
229 const type_t *type = stmt->u.type;
230 if (type_get_type(type) == TYPE_COCLASS) write_progid( type );
231 }
232 }
233 }
234
write_regscript(const statement_list_t * stmts)235 void write_regscript( const statement_list_t *stmts )
236 {
237 const type_t *ps_factory;
238
239 if (!do_regscript) return;
240 if (do_everything && !need_proxy_file( stmts )) return;
241
242 init_output_buffer();
243
244 put_str( indent, "HKCR\n" );
245 put_str( indent++, "{\n" );
246
247 put_str( indent, "NoRemove Interface\n" );
248 put_str( indent++, "{\n" );
249 ps_factory = find_ps_factory( stmts );
250 if (ps_factory) write_interfaces( stmts, ps_factory );
251 put_str( --indent, "}\n" );
252
253 put_str( indent, "NoRemove CLSID\n" );
254 put_str( indent++, "{\n" );
255 write_coclasses( stmts, NULL );
256 put_str( --indent, "}\n" );
257
258 write_progids( stmts );
259 put_str( --indent, "}\n" );
260
261 if (strendswith( regscript_name, ".res" )) /* create a binary resource file */
262 {
263 add_output_to_resources( "WINE_REGISTRY", regscript_token );
264 flush_output_resources( regscript_name );
265 }
266 else
267 {
268 FILE *f = fopen( regscript_name, "w" );
269 if (!f) error( "Could not open %s for output\n", regscript_name );
270 if (fwrite( output_buffer, 1, output_buffer_pos, f ) != output_buffer_pos)
271 error( "Failed to write to %s\n", regscript_name );
272 if (fclose( f ))
273 error( "Failed to write to %s\n", regscript_name );
274 }
275 }
276
write_typelib_regscript(const statement_list_t * stmts)277 void write_typelib_regscript( const statement_list_t *stmts )
278 {
279 const statement_t *stmt;
280 unsigned int count = 0;
281
282 if (!do_typelib) return;
283 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
284 {
285 if (stmt->type != STMT_LIBRARY) continue;
286 if (count && !strendswith( typelib_name, ".res" ))
287 error( "Cannot store multiple typelibs into %s\n", typelib_name );
288 else
289 create_msft_typelib( stmt->u.lib );
290 count++;
291 }
292 if (count && strendswith( typelib_name, ".res" )) flush_output_resources( typelib_name );
293 }
294
output_typelib_regscript(const typelib_t * typelib)295 void output_typelib_regscript( const typelib_t *typelib )
296 {
297 const UUID *typelib_uuid = get_attrp( typelib->attrs, ATTR_UUID );
298 const char *descr = get_attrp( typelib->attrs, ATTR_HELPSTRING );
299 const expr_t *lcid_expr = get_attrp( typelib->attrs, ATTR_LIBLCID );
300 unsigned int version = get_attrv( typelib->attrs, ATTR_VERSION );
301 unsigned int flags = 0;
302 char id_part[12] = "";
303 char *resname = typelib_name;
304 expr_t *expr;
305
306 if (is_attr( typelib->attrs, ATTR_RESTRICTED )) flags |= 1; /* LIBFLAG_FRESTRICTED */
307 if (is_attr( typelib->attrs, ATTR_CONTROL )) flags |= 2; /* LIBFLAG_FCONTROL */
308 if (is_attr( typelib->attrs, ATTR_HIDDEN )) flags |= 4; /* LIBFLAG_FHIDDEN */
309
310 put_str( indent, "HKCR\n" );
311 put_str( indent++, "{\n" );
312
313 put_str( indent, "NoRemove Typelib\n" );
314 put_str( indent++, "{\n" );
315 put_str( indent, "NoRemove '%s'\n", format_uuid( typelib_uuid ));
316 put_str( indent++, "{\n" );
317 put_str( indent, "'%u.%u' = s '%s'\n",
318 MAJORVERSION(version), MINORVERSION(version), descr ? descr : typelib->name );
319 put_str( indent++, "{\n" );
320 expr = get_attrp( typelib->attrs, ATTR_ID );
321 if (expr)
322 {
323 sprintf(id_part, "\\%d", expr->cval);
324 #ifndef __REACTOS__
325 resname = xmalloc( strlen(typelib_name) + 20 );
326 sprintf(resname, "%s\\%d", typelib_name, expr->cval);
327 #endif
328 }
329 put_str( indent, "'%x' { %s = s '%%MODULE%%%s' }\n",
330 lcid_expr ? lcid_expr->cval : 0, pointer_size == 8 ? "win64" : "win32", id_part );
331 put_str( indent, "FLAGS = s '%u'\n", flags );
332 put_str( --indent, "}\n" );
333 put_str( --indent, "}\n" );
334 put_str( --indent, "}\n" );
335
336 put_str( indent, "NoRemove Interface\n" );
337 put_str( indent++, "{\n" );
338 write_typelib_interfaces( typelib );
339 put_str( --indent, "}\n" );
340
341 put_str( indent, "NoRemove CLSID\n" );
342 put_str( indent++, "{\n" );
343 write_coclasses( typelib->stmts, typelib );
344 put_str( --indent, "}\n" );
345
346 write_progids( typelib->stmts );
347 put_str( --indent, "}\n" );
348
349 add_output_to_resources( "WINE_REGISTRY", resname );
350 }
351