msi: Add an implementation of MsiGetDatabaseState.
[wine/gsoc_dplay.git] / tools / widl / server.c
blob6d6d0ccc1c5f14c392c3f204c308d03d87179f61
1 /*
2 * IDL Compiler
4 * Copyright 2005 Eric Kohl
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.
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.
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
21 #include "config.h"
22 #include "wine/port.h"
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>
34 #include "widl.h"
35 #include "utils.h"
36 #include "parser.h"
37 #include "header.h"
38 #include "windef.h"
40 #include "widl.h"
41 #include "typelib.h"
42 #include "typelib_struct.h"
43 #include "typegen.h"
45 #define END_OF_LIST(list) \
46 do { \
47 if (list) { \
48 while (NEXT_LINK(list)) \
49 list = NEXT_LINK(list); \
50 } \
51 } while(0)
53 static FILE* server;
54 static int indent = 0;
57 static int print_server(const char *format, ...)
59 va_list va;
60 int i, r;
62 va_start(va, format);
63 for (i = 0; i < indent; i++)
64 fprintf(server, " ");
65 r = vfprintf(server, format, va);
66 va_end(va);
67 return r;
71 static void write_parameters_init(func_t *func)
73 var_t *var;
75 if (!func->args)
76 return;
78 var = func->args;
79 while (NEXT_LINK(var)) var = NEXT_LINK(var);
80 while (var)
82 print_server("%s = 0;\n", var->name);
84 var = PREV_LINK(var);
86 fprintf(server, "\n");
90 static void write_function_stubs(type_t *iface)
92 char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
93 int explicit_handle = is_attr(iface->attrs, ATTR_EXPLICIT_HANDLE);
94 func_t *func = iface->funcs;
95 var_t *var;
96 var_t* explicit_handle_var;
97 unsigned int proc_offset = 0;
98 unsigned int type_offset = 2;
100 while (NEXT_LINK(func)) func = NEXT_LINK(func);
101 while (func)
103 var_t *def = func->def;
104 unsigned long buffer_size = 0;
106 /* check for a defined binding handle */
107 explicit_handle_var = get_explicit_handle_var(func);
108 if (explicit_handle)
110 if (!explicit_handle_var)
112 error("%s() does not define an explicit binding handle!\n", def->name);
113 return;
116 else if (implicit_handle)
118 if (explicit_handle_var)
120 error("%s() must not define a binding handle!\n", def->name);
121 return;
125 fprintf(server, "void __RPC_STUB\n");
126 fprintf(server, "%s_", iface->name);
127 write_name(server, def);
128 fprintf(server, "(\n");
129 indent++;
130 print_server("PRPC_MESSAGE _pRpcMessage)\n");
131 indent--;
133 /* write the functions body */
134 fprintf(server, "{\n");
135 indent++;
137 /* declare return value '_RetVal' */
138 if (!is_void(def->type, NULL))
140 print_server("");
141 write_type(server, def->type, def, def->tname);
142 fprintf(server, " _RetVal;\n");
145 /* declare arguments */
146 if (func->args)
148 var = func->args;
149 while (NEXT_LINK(var)) var = NEXT_LINK(var);
150 while (var)
152 print_server("");
153 write_type(server, var->type, var, var->tname);
154 fprintf(server, " ");
155 write_name(server, var);
156 write_array(server, var->array, 0);
157 fprintf(server, ";\n");
159 var = PREV_LINK(var);
163 print_server("MIDL_STUB_MESSAGE _StubMsg;\n");
164 print_server("RPC_STATUS _Status;\n");
165 fprintf(server, "\n");
168 print_server("((void)(_Status));\n");
169 print_server("NdrServerInitializeNew(\n");
170 indent++;
171 print_server("_pRpcMessage,\n");
172 print_server("&_StubMsg,\n");
173 print_server("&%s_StubDesc);\n", iface->name);
174 indent--;
175 fprintf(server, "\n");
177 write_parameters_init(func);
179 if (explicit_handle_var)
181 print_server("%s = _pRpcMessage->Handle;\n", explicit_handle_var->name);
182 fprintf(server, "\n");
185 print_server("RpcTryFinally\n");
186 print_server("{\n");
187 indent++;
188 print_server("RpcTryExcept\n");
189 print_server("{\n");
190 indent++;
192 if (func->args)
194 print_server("if ((_pRpcMessage->DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
195 indent++;
196 print_server("NdrConvert(\n");
197 indent++;
198 print_server("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
199 print_server("(PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n", proc_offset);
200 indent -= 2;
201 fprintf(server, "\n");
203 unmarshall_arguments(server, indent, func, &type_offset, PASS_IN);
206 print_server("if (_StubMsg.Buffer > _StubMsg.BufferEnd)\n");
207 print_server("{\n");
208 indent++;
209 print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
210 indent--;
211 print_server("}\n");
212 indent--;
213 print_server("}\n");
214 print_server("RpcExcept(RPC_BAD_STUB_DATA_EXCEPTION_FILTER)\n");
215 print_server("{\n");
216 indent++;
217 print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
218 indent--;
219 print_server("}\n");
220 print_server("RpcEndExcept\n");
221 fprintf(server, "\n");
224 /* Call the real server function */
225 if (!is_void(def->type, NULL))
226 print_server("_RetVal = ");
227 else
228 print_server("");
229 write_name(server, def);
231 if (func->args)
233 int first_arg = 1;
235 fprintf(server, "(\n");
236 indent++;
237 var = func->args;
238 while (NEXT_LINK(var)) var = NEXT_LINK(var);
239 while (var)
241 if (first_arg)
242 first_arg = 0;
243 else
244 fprintf(server, ",\n");
245 print_server("");
246 write_name(server, var);
247 var = PREV_LINK(var);
249 fprintf(server, ");\n");
250 indent--;
252 else
254 fprintf(server, "();\n");
257 if (func->args)
259 var_t *var = func->args;
260 while (NEXT_LINK(var)) var = NEXT_LINK(var);
261 while (var)
263 if (is_attr(var->attrs, ATTR_OUT))
265 unsigned int alignment;
266 buffer_size += get_required_buffer_size(var, &alignment);
267 buffer_size += alignment;
270 var = PREV_LINK(var);
274 if (!is_void(def->type, NULL))
276 unsigned int alignment;
277 buffer_size += get_required_buffer_size(def, &alignment);
278 buffer_size += alignment;
281 if (buffer_size)
283 fprintf(server, "\n");
284 print_server("_StubMsg.BufferLength = %uU;\n", buffer_size);
285 print_server("_pRpcMessage->BufferLength = _StubMsg.BufferLength;\n");
286 fprintf(server, "\n");
287 print_server("_Status = I_RpcGetBuffer(_pRpcMessage);\n");
288 print_server("if (_Status)\n");
289 indent++;
290 print_server("RpcRaiseException(_Status);\n");
291 indent--;
292 fprintf(server, "\n");
293 print_server("_StubMsg.Buffer = (unsigned char *)_pRpcMessage->Buffer;\n");
294 fprintf(server, "\n");
297 marshall_arguments(server, indent, func, &type_offset, PASS_OUT);
299 /* marshall the return value */
300 if (!is_void(def->type, NULL))
302 print_server("*(");
303 write_type(server, def->type, def, def->tname);
304 fprintf(server, " *)_StubMsg.Buffer = _RetVal;\n");
305 fprintf(server, "_StubMsg.Buffer += sizeof(");
306 write_type(server, def->type, def, def->tname);
307 fprintf(server, ");\n");
310 indent--;
311 print_server("}\n");
312 print_server("RpcFinally\n");
313 print_server("{\n");
314 print_server("}\n");
315 print_server("RpcEndFinally\n");
317 /* calculate buffer length */
318 fprintf(server, "\n");
319 print_server("_pRpcMessage->BufferLength =\n");
320 indent++;
321 print_server("(unsigned int)(_StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer);\n");
322 indent--;
323 indent--;
324 fprintf(server, "}\n");
325 fprintf(server, "\n");
327 /* update proc_offset */
328 if (func->args)
330 var = func->args;
331 while (NEXT_LINK(var)) var = NEXT_LINK(var);
332 while (var)
334 proc_offset += get_size_procformatstring_var(var);
335 var = PREV_LINK(var);
338 if (!is_void(def->type, NULL))
339 proc_offset += get_size_procformatstring_var(def);
341 func = PREV_LINK(func);
346 static void write_dispatchtable(type_t *iface)
348 unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
349 unsigned long method_count = 0;
350 func_t *func = iface->funcs;
352 print_server("static RPC_DISPATCH_FUNCTION %s_table[] =\n", iface->name);
353 print_server("{\n");
354 indent++;
355 while (NEXT_LINK(func)) func = NEXT_LINK(func);
356 while (func)
358 var_t *def = func->def;
360 print_server("%s_", iface->name);
361 write_name(server, def);
362 fprintf(server, ",\n");
364 method_count++;
365 func = PREV_LINK(func);
367 print_server("0\n");
368 indent--;
369 print_server("};\n");
370 print_server("RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable =\n", iface->name, LOWORD(ver), HIWORD(ver));
371 print_server("{\n");
372 indent++;
373 print_server("%u,\n", method_count);
374 print_server("%s_table\n", iface->name);
375 indent--;
376 print_server("};\n");
377 fprintf(server, "\n");
381 static void write_stubdescdecl(type_t *iface)
383 print_server("extern const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
384 fprintf(server, "\n");
388 static void write_stubdescriptor(type_t *iface, int expr_eval_routines)
390 print_server("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
391 print_server("{\n");
392 indent++;
393 print_server("(void *)& %s___RpcServerInterface,\n", iface->name);
394 print_server("MIDL_user_allocate,\n");
395 print_server("MIDL_user_free,\n");
396 print_server("0,\n");
397 print_server("0,\n");
398 print_server("0,\n");
399 if (expr_eval_routines)
400 print_server("ExprEvalRoutines,\n");
401 else
402 print_server("0,\n");
403 print_server("0,\n");
404 print_server("__MIDL_TypeFormatString.Format,\n");
405 print_server("1, /* -error bounds_check flag */\n");
406 print_server("0x10001, /* Ndr library version */\n");
407 print_server("0,\n");
408 print_server("0x50100a4, /* MIDL Version 5.1.164 */\n");
409 print_server("0,\n");
410 print_server("0,\n");
411 print_server("0, /* notify & notify_flag routine table */\n");
412 print_server("1, /* Flags */\n");
413 print_server("0, /* Reserved3 */\n");
414 print_server("0, /* Reserved4 */\n");
415 print_server("0 /* Reserved5 */\n");
416 indent--;
417 print_server("};\n");
418 fprintf(server, "\n");
422 static void write_serverinterfacedecl(type_t *iface)
424 unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
425 UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
427 print_server("extern RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable;\n", iface->name, LOWORD(ver), HIWORD(ver));
428 fprintf(server, "\n");
429 print_server("static const RPC_SERVER_INTERFACE %s___RpcServerInterface =\n", iface->name );
430 print_server("{\n");
431 indent++;
432 print_server("sizeof(RPC_SERVER_INTERFACE),\n");
433 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",
434 uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
435 uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
436 uuid->Data4[7], LOWORD(ver), HIWORD(ver));
437 print_server("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
438 print_server("&%s_v%d_%d_DispatchTable,\n", iface->name, LOWORD(ver), HIWORD(ver));
439 print_server("0,\n");
440 print_server("0,\n");
441 print_server("0,\n");
442 print_server("0,\n");
443 print_server("0,\n");
444 indent--;
445 print_server("};\n");
446 print_server("RPC_IF_HANDLE %s_v%d_%d_s_ifspec = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
447 iface->name, LOWORD(ver), HIWORD(ver), iface->name);
448 fprintf(server, "\n");
451 static void write_formatdesc( const char *str )
453 print_server("typedef struct _MIDL_%s_FORMAT_STRING\n", str );
454 print_server("{\n");
455 indent++;
456 print_server("short Pad;\n");
457 print_server("unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
458 indent--;
459 print_server("} MIDL_%s_FORMAT_STRING;\n", str);
460 print_server("\n");
464 static void write_formatstringsdecl(type_t *iface)
466 int byte_count = 1;
468 print_server("#define TYPE_FORMAT_STRING_SIZE %d\n", 3); /* FIXME */
470 /* determine the proc format string size */
471 if (iface->funcs)
473 func_t *func = iface->funcs;
474 while (NEXT_LINK(func)) func = NEXT_LINK(func);
475 while (func)
477 /* argument list size */
478 if (func->args)
480 var_t *var = func->args;
481 while (NEXT_LINK(var)) var = NEXT_LINK(var);
482 while (var)
484 byte_count += 2; /* FIXME: determine real size */
485 var = PREV_LINK(var);
489 /* return value size */
490 byte_count += 2; /* FIXME: determine real size */
491 func = PREV_LINK(func);
494 print_server("#define PROC_FORMAT_STRING_SIZE %d\n", byte_count);
496 fprintf(server, "\n");
497 write_formatdesc("TYPE");
498 write_formatdesc("PROC");
499 fprintf(server, "\n");
500 print_server("extern const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
501 print_server("extern const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
502 print_server("\n");
506 static void init_server(void)
508 if (server)
509 return;
510 if (!(server = fopen(server_name, "w")))
511 error("Could not open %s for output\n", server_name);
513 print_server("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", WIDL_FULLVERSION, input_name);
514 print_server("#include <string.h>\n");
515 fprintf(server, "\n");
516 print_server("#include \"%s\"\n", header_name);
517 fprintf(server, "\n");
521 void write_server(ifref_t *ifaces)
523 ifref_t *iface = ifaces;
525 if (!do_server)
526 return;
527 if (!iface)
528 return;
529 END_OF_LIST(iface);
531 init_server();
532 if (!server)
533 return;
535 while (iface)
537 fprintf(server, "/*****************************************************************************\n");
538 fprintf(server, " * %s interface\n", iface->iface->name);
539 fprintf(server, " */\n");
540 fprintf(server, "\n");
542 if (iface->iface->funcs)
544 int expr_eval_routines;
546 write_formatstringsdecl(iface->iface);
547 write_serverinterfacedecl(iface->iface);
548 write_stubdescdecl(iface->iface);
550 write_function_stubs(iface->iface);
552 print_server("#if !defined(__RPC_WIN32__)\n");
553 print_server("#error Invalid build platform for this stub.\n");
554 print_server("#endif\n");
555 fprintf(server, "\n");
557 write_procformatstring(server, iface->iface);
558 write_typeformatstring(server, iface->iface);
560 fprintf(server, "\n");
562 expr_eval_routines = write_expr_eval_routines(server, iface->iface->name);
563 if (expr_eval_routines)
564 write_expr_eval_routine_list(server, iface->iface->name);
566 write_stubdescriptor(iface->iface, expr_eval_routines);
567 write_dispatchtable(iface->iface);
570 iface = PREV_LINK(iface);
573 fclose(server);