Suggest make uninstall in case the user tried make install.
[wine] / tools / widl / server.c
1 /*
2  * IDL Compiler
3  *
4  * Copyright 2005 Eric Kohl
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 <assert.h>
31 #include <ctype.h>
32 #include <signal.h>
33
34 #include "widl.h"
35 #include "utils.h"
36 #include "parser.h"
37 #include "header.h"
38 #include "windef.h"
39
40 #include "widl.h"
41 #include "typelib.h"
42 #include "typelib_struct.h"
43
44 #define END_OF_LIST(list)       \
45   do {                          \
46     if (list) {                 \
47       while (NEXT_LINK(list))   \
48         list = NEXT_LINK(list); \
49     }                           \
50   } while(0)
51
52 static FILE* server;
53 static int indent = 0;
54
55
56 static int print_server(const char *format, ...)
57 {
58     va_list va;
59     int i, r;
60
61     va_start(va, format);
62     for (i = 0; i < indent; i++)
63         fprintf(server, "    ");
64     r = vfprintf(server, format, va);
65     va_end(va);
66     return r;
67 }
68
69
70 static void write_procformatstring(type_t *iface)
71 {
72     func_t *cur = iface->funcs;
73
74     print_server("static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
75     print_server("{\n");
76     indent++;
77     print_server("0,\n");
78     print_server("{\n");
79     indent++;
80
81     while (NEXT_LINK(cur)) cur = NEXT_LINK(cur);
82     while (cur)
83     {
84         var_t *def = cur->def;
85
86         if (is_void(def->type, NULL))
87         {
88             print_server("0x5b,    /* FC_END */\n");
89             print_server("0x5c,    /* FC_PAD */\n");
90         }
91         else
92         {
93             print_server("0x53,    /* FC_RETURN_PARAM_BASETYPE */\n");
94             print_server("0x%02x,    /* <type> */\n", def->type->type);
95         }
96
97         cur = PREV_LINK(cur);
98     }
99
100     print_server("0x0\n");
101     indent--;
102     print_server("}\n");
103     indent--;
104     print_server("};\n");
105     print_server("\n");
106 }
107
108
109 static void write_typeformatstring(void)
110 {
111     print_server("static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
112     print_server("{\n");
113     indent++;
114     print_server("0,\n");
115     print_server("{\n");
116     indent++;
117     print_server("NdrFcShort(0x0),\n");
118     print_server("0x0\n");
119     indent--;
120     print_server("}\n");
121     indent--;
122     print_server("};\n");
123     print_server("\n");
124 }
125
126
127 unsigned int get_required_stack_size(type_t *type)
128 {
129     switch(type->type)
130     {
131         case RPC_FC_BYTE:
132         case RPC_FC_CHAR:
133         case RPC_FC_WCHAR:
134         case RPC_FC_USHORT:
135         case RPC_FC_SHORT:
136         case RPC_FC_ULONG:
137         case RPC_FC_LONG:
138             return 4;
139
140       case RPC_FC_HYPER:
141             return 8;
142
143         default:
144             error("Unknown/unsupported type: %s\n", type->name);
145     }
146 }
147
148
149 static void write_function_stubs(type_t *iface)
150 {
151     func_t *cur = iface->funcs;
152     while (NEXT_LINK(cur)) cur = NEXT_LINK(cur);
153     while (cur)
154     {
155         var_t *def = cur->def;
156
157         write_type(server, def->type, def, def->tname);
158         fprintf(server, " __RPC_STUB\n");
159         fprintf(server, "%s_", iface->name);
160         write_name(server, def);
161         fprintf(server, "(\n");
162         indent++;
163         print_server("PRPC_MESSAGE _pRpcMessage)\n");
164         indent--;
165
166         /* write the functions body */
167         fprintf(server, "{\n");
168         indent++;
169
170         /* declare return value '_RetVal' */
171         if (!is_void(def->type, NULL))
172         {
173             print_server("");
174             write_type(server, def->type, def, def->tname);
175             fprintf(server, " _RetVal;\n");
176         }
177
178         print_server("MIDL_STUB_MESSAGE _StubMsg;\n");
179         print_server("RPC_STATUS _Status;\n");
180         fprintf(server, "\n");
181         print_server("((void)(_Status));\n");
182         print_server("NdrServerInitializeNew(\n");
183         indent++;
184         print_server("_pRpcMessage,\n");
185         print_server("&_StubMsg,\n");
186         print_server("&%s_StubDesc);\n", iface->name);
187         indent--;
188         fprintf(server, "\n");
189
190         print_server("RpcTryFinally\n");
191         print_server("{\n");
192         indent++;
193         print_server("RpcTryExcept\n");
194         print_server("{\n");
195         indent++;
196         print_server("if (_StubMsg.Buffer > _StubMsg.BufferEnd)\n");
197         print_server("{\n");
198         indent++;
199         print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
200         indent--;
201         print_server("}\n");
202         indent--;
203         print_server("}\n");
204         print_server("RpcExcept(RPC_BAD_STUB_DATA_EXCEPTION_FILTER)\n");
205         print_server("{\n");
206         indent++;
207         print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
208         indent--;
209         print_server("}\n");
210         print_server("RpcEndExcept\n");
211         fprintf(server, "\n");
212
213
214         /* Call the real server function */
215         if (!is_void(def->type, NULL))
216             print_server("_RetVal = ");
217         else
218             print_server("");
219         write_name(server, def);
220
221         /* FIXME: handle argument list */
222         fprintf(server, "();\n");
223
224         /* FIXME: Marshall the return value */
225         if (!is_void(def->type, NULL))
226         {
227             fprintf(server, "\n");
228             print_server("_StubMsg.BufferLength = %uU;\n", get_required_stack_size(def->type));
229             print_server("_pRpcMessage->BufferLength = _StubMsg.BufferLength;\n");
230             fprintf(server, "\n");
231             print_server("_Status = I_RpcGetBuffer(_pRpcMessage);\n");
232             print_server("if (_Status)\n");
233             indent++;
234             print_server("RpcRaiseException(_Status);\n");
235             indent--;
236             fprintf(server, "\n");
237             print_server("_StubMsg.Buffer = (unsigned char __RPC_FAR *)_pRpcMessage->Buffer;\n");
238             fprintf(server, "\n");
239
240             print_server("*((");
241             write_type(server, def->type, def, def->tname);
242             fprintf(server, " __RPC_FAR *)_StubMsg.Buffer)++ = _RetVal;\n");
243         }
244
245         indent--;
246         print_server("}\n");
247         print_server("RpcFinally\n");
248         print_server("{\n");
249         print_server("}\n");
250         print_server("RpcEndFinally\n");
251
252         /* calculate buffer length */
253         fprintf(server, "\n");
254         print_server("_pRpcMessage->BufferLength =\n");
255         indent++;
256         print_server("(unsigned int)((long)_StubMsg.Buffer - (long)_pRpcMessage->Buffer);\n");
257         indent--;
258         indent--;
259         fprintf(server, "}\n");
260         fprintf(server, "\n");
261
262         cur = PREV_LINK(cur);
263     }
264 }
265
266
267 static void write_dispatchtable(type_t *iface)
268 {
269     unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
270     unsigned long method_count = 0;
271     func_t *cur = iface->funcs;
272
273     print_server("static RPC_DISPATCH_FUNCTION %s_table[] =\n", iface->name);
274     print_server("{\n");
275     indent++;
276     while (NEXT_LINK(cur)) cur = NEXT_LINK(cur);
277     while (cur)
278     {
279         var_t *def = cur->def;
280
281         print_server("%s_", iface->name);
282         write_name(server, def);
283         fprintf(server, ",\n");
284
285         method_count++;
286         cur = PREV_LINK(cur);
287     }
288     print_server("0\n");
289     indent--;
290     print_server("};\n");
291     print_server("RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable =\n", iface->name, LOWORD(ver), HIWORD(ver));
292     print_server("{\n");
293     indent++;
294     print_server("%u,\n", method_count);
295     print_server("%s_table\n", iface->name);
296     indent--;
297     print_server("};\n");
298     fprintf(server, "\n");
299 }
300
301
302 static void write_stubdescdecl(type_t *iface)
303 {
304     print_server("extern const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
305     fprintf(server, "\n");
306 }
307
308
309 static void write_stubdescriptor(type_t *iface)
310 {
311     print_server("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
312     print_server("{\n");
313     indent++;
314     print_server("(void __RPC_FAR *)& %s___RpcServerInterface,\n", iface->name);
315     print_server("MIDL_user_allocate,\n");
316     print_server("MIDL_user_free,\n");
317     print_server("0,\n");
318     print_server("0,\n");
319     print_server("0,\n");
320     print_server("0,\n");
321     print_server("0,\n");
322     print_server("__MIDL_TypeFormatString.Format,\n");
323     print_server("1, /* -error bounds_check flag */\n");
324     print_server("0x10001, /* Ndr library version */\n");
325     print_server("0,\n");
326     print_server("0x50100a4, /* MIDL Version 5.1.164 */\n");
327     print_server("0,\n");
328     print_server("0,\n");
329     print_server("0,  /* notify & notify_flag routine table */\n");
330     print_server("1,  /* Flags */\n");
331     print_server("0,  /* Reserved3 */\n");
332     print_server("0,  /* Reserved4 */\n");
333     print_server("0   /* Reserved5 */\n");
334     indent--;
335     print_server("};\n");
336     fprintf(server, "\n");
337 }
338
339
340 static void write_serverinterfacedecl(type_t *iface)
341 {
342     unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
343     UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
344
345     print_server("extern RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable;\n", iface->name, LOWORD(ver), HIWORD(ver));
346     fprintf(server, "\n");
347     print_server("static const RPC_SERVER_INTERFACE %s___RpcServerInterface =\n", iface->name );
348     print_server("{\n");
349     indent++;
350     print_server("sizeof(RPC_SERVER_INTERFACE),\n");
351     print_server("{{0x%08lx,0x%04x,0x%04x,{0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x}},{%d,%d}},\n",
352                  uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
353                  uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
354                  uuid->Data4[7], LOWORD(ver), HIWORD(ver));
355     print_server("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
356     print_server("&%s_v%d_%d_DispatchTable,\n", iface->name, LOWORD(ver), HIWORD(ver));
357     print_server("0,\n");
358     print_server("0,\n");
359     print_server("0,\n");
360     print_server("0,\n");
361     print_server("0,\n");
362     indent--;
363     print_server("};\n");
364     print_server("RPC_IF_HANDLE %s_v%d_%d_s_ifspec = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
365                  iface->name, LOWORD(ver), HIWORD(ver), iface->name);
366     fprintf(server, "\n");
367 }
368
369 static void write_formatdesc( const char *str )
370 {
371     print_server("typedef struct _MIDL_%s_FORMAT_STRING\n", str );
372     print_server("{\n");
373     indent++;
374     print_server("short Pad;\n");
375     print_server("unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
376     indent--;
377     print_server("} MIDL_%s_FORMAT_STRING;\n", str);
378     print_server("\n");
379 }
380
381
382 static void write_formatstringsdecl(type_t *iface)
383 {
384     func_t *cur;
385     int byte_count = 1;
386
387     print_server("#define TYPE_FORMAT_STRING_SIZE %d\n", 3); /* FIXME */
388
389     /* determine the proc format string size */
390     cur = iface->funcs;
391     while (NEXT_LINK(cur)) cur = NEXT_LINK(cur);
392     while (cur)
393     {
394         byte_count += 2; /* FIXME: determine real size */
395         cur = PREV_LINK(cur);
396     }
397     print_server("#define PROC_FORMAT_STRING_SIZE %d\n", byte_count);
398
399     fprintf(server, "\n");
400     write_formatdesc("TYPE");
401     write_formatdesc("PROC");
402     fprintf(server, "\n");
403     print_server("extern const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
404     print_server("extern const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
405     print_server("\n");
406 }
407
408
409 static void init_server(void)
410 {
411     if (server)
412         return;
413     if (!(server = fopen(server_name, "w")))
414         error("Could not open %s for output\n", server_name);
415
416     print_server("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", WIDL_FULLVERSION, input_name);
417     print_server("#include<string.h>\n");
418     fprintf(server, "\n");
419     print_server("#include\"%s\"\n", header_name);
420     fprintf(server, "\n");
421 }
422
423
424 void write_server(ifref_t *ifaces)
425 {
426     ifref_t *lcur = ifaces;
427     char *file_id = server_token;
428     int c;
429
430     if (!do_server)
431         return;
432     if (!lcur)
433         return;
434     END_OF_LIST(lcur);
435
436     init_server();
437     if (!server)
438         return;
439
440     write_formatstringsdecl(lcur->iface);
441     write_serverinterfacedecl(lcur->iface);
442     write_stubdescdecl(lcur->iface);
443
444     write_function_stubs(lcur->iface);
445
446     write_stubdescriptor(lcur->iface);
447     write_dispatchtable(lcur->iface);
448
449     print_server("#if !defined(__RPC_WIN32__)\n");
450     print_server("#error  Invalid build platform for this stub.\n");
451     print_server("#endif\n");
452     fprintf(server, "\n");
453
454     write_procformatstring(lcur->iface);
455     write_typeformatstring();
456
457     fprintf(server, "\n");
458
459     fclose(server);
460 }