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
38 #define END_OF_LIST(list) \
41 while (NEXT_LINK(list)) \
42 list = NEXT_LINK(list); \
47 static int indent
= 0;
50 static int print_server(const char *format
, ...)
56 for (i
= 0; i
< indent
; i
++)
58 r
= vfprintf(server
, format
, va
);
64 static void write_procformatstring(type_t
*iface
)
66 func_t
*func
= iface
->funcs
;
69 print_server("static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
76 while (NEXT_LINK(func
)) func
= NEXT_LINK(func
);
79 /* emit argument data */
83 while (NEXT_LINK(var
)) var
= NEXT_LINK(var
);
86 switch(var
->type
->type
)
89 print_server("0x4e, /* FC_IN_PARAM_BASETYPE */\n");
90 print_server("0x%02x, /* FC_BYTE */\n", var
->type
->type
);
93 print_server("0x4e, /* FC_IN_PARAM_BASETYPE */\n");
94 print_server("0x%02x, /* FC_CHAR */\n", var
->type
->type
);
97 print_server("0x4e, /* FC_IN_PARAM_BASETYPE */\n");
98 print_server("0x%02x, /* FC_WCHAR */\n", var
->type
->type
);
101 print_server("0x4e, /* FC_IN_PARAM_BASETYPE */\n");
102 print_server("0x%02x, /* FC_USHORT */\n", var
->type
->type
);
105 print_server("0x4e, /* FC_IN_PARAM_BASETYPE */\n");
106 print_server("0x%02x, /* FC_SHORT */\n", var
->type
->type
);
109 print_server("0x4e, /* FC_IN_PARAM_BASETYPE */\n");
110 print_server("0x%02x, /* FC_ULONG */\n", var
->type
->type
);
113 print_server("0x4e, /* FC_IN_PARAM_BASETYPE */\n");
114 print_server("0x%02x, /* FC_LONG */\n", var
->type
->type
);
117 print_server("0x4e, /* FC_IN_PARAM_BASETYPE */\n");
118 print_server("0x%02x, /* FC_HYPER */\n", var
->type
->type
);
121 print_server("0x4e, /* FC_IN_PARAM_BASETYPE */\n");
122 print_server("0x%02x, /* FC_IGNORE */\n", var
->type
->type
);
125 print_server("0x4e, /* FC_IN_PARAM_BASETYPE */\n");
126 print_server("0x%02x, /* FC_SMALL */\n", RPC_FC_SMALL
);
129 print_server("0x4e, /* FC_IN_PARAM_BASETYPE */\n");
130 print_server("0x%02x, /* FC_FLOAT */\n", RPC_FC_FLOAT
);
133 print_server("0x4e, /* FC_IN_PARAM_BASETYPE */\n");
134 print_server("0x%02x, /* FC_DOUBLE */\n", RPC_FC_DOUBLE
);
137 error("Unknown/unsupported type\n");
140 var
= PREV_LINK(var
);
144 /* emit return value data */
146 if (is_void(var
->type
, NULL
))
148 print_server("0x5b, /* FC_END */\n");
149 print_server("0x5c, /* FC_PAD */\n");
153 switch(var
->type
->type
)
156 print_server("0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
157 print_server("0x%02x, /* FC_BYTE */\n", var
->type
->type
);
160 print_server("0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
161 print_server("0x%02x, /* FC_CHAR */\n", var
->type
->type
);
164 print_server("0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
165 print_server("0x%02x, /* FC_WCHAR */\n", var
->type
->type
);
168 print_server("0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
169 print_server("0x%02x, /* FC_USHORT */\n", var
->type
->type
);
172 print_server("0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
173 print_server("0x%02x, /* FC_SHORT */\n", var
->type
->type
);
176 print_server("0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
177 print_server("0x%02x, /* FC_ULONG */\n", var
->type
->type
);
180 print_server("0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
181 print_server("0x%02x, /* FC_LONG */\n", var
->type
->type
);
184 print_server("0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
185 print_server("0x%02x, /* FC_HYPER */\n", var
->type
->type
);
188 print_server("0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
189 print_server("0x%02x, /* FC_SMALL */\n", RPC_FC_SMALL
);
192 print_server("0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
193 print_server("0x%02x, /* FC_FLOAT */\n", RPC_FC_FLOAT
);
196 print_server("0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
197 print_server("0x%02x, /* FC_DOUBLE */\n", RPC_FC_DOUBLE
);
200 error("Unknown/unsupported type\n");
204 func
= PREV_LINK(func
);
207 print_server("0x0\n");
211 print_server("};\n");
216 static void write_typeformatstring(void)
218 print_server("static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
221 print_server("0,\n");
224 print_server("NdrFcShort(0x0),\n");
225 print_server("0x0\n");
229 print_server("};\n");
234 unsigned int get_required_buffer_size(type_t
*type
)
257 error("Unknown/unsupported type: %s\n", type
->name
);
262 static void unmarshall_arguments(func_t
*func
)
264 unsigned int alignment
;
266 unsigned int last_size
= 0;
273 while (NEXT_LINK(var
)) var
= NEXT_LINK(var
);
277 switch (var
->type
->type
)
290 if (last_size
!= 0 && last_size
< 2)
291 alignment
= (2 - last_size
);
298 if (last_size
!= 0 && last_size
< 4)
299 alignment
= (4 - last_size
);
305 if (last_size
!= 0 && last_size
< 4)
306 alignment
= (4 - last_size
);
314 error("Unknown/unsupported type!");
320 print_server("_StubMsg.Buffer += %u;\n", alignment
);
323 write_name(server
, var
);
324 fprintf(server
, " = *((");
325 write_type(server
, var
->type
, var
, var
->tname
);
326 fprintf(server
, " __RPC_FAR*)_StubMsg.Buffer)++;\n");
327 fprintf(server
, "\n");
332 var
= PREV_LINK(var
);
337 static void write_function_stubs(type_t
*iface
)
339 int explicit_handle
= is_attr(iface
->attrs
, ATTR_EXPLICIT_HANDLE
);
340 func_t
*func
= iface
->funcs
;
342 var_t
* explicit_handle_var
;
343 unsigned int proc_offset
= 0;
345 while (NEXT_LINK(func
)) func
= NEXT_LINK(func
);
348 var_t
*def
= func
->def
;
350 /* check for a defined binding handle */
351 explicit_handle_var
= get_explicit_handle_var(func
);
354 if (!explicit_handle_var
)
356 error("%s() does not define an explicit binding handle!\n", def
->name
);
362 if (explicit_handle_var
)
364 error("%s() must not define a binding handle!\n", def
->name
);
369 write_type(server
, def
->type
, def
, def
->tname
);
370 fprintf(server
, " __RPC_STUB\n");
371 fprintf(server
, "%s_", iface
->name
);
372 write_name(server
, def
);
373 fprintf(server
, "(\n");
375 print_server("PRPC_MESSAGE _pRpcMessage)\n");
378 /* write the functions body */
379 fprintf(server
, "{\n");
382 /* declare return value '_RetVal' */
383 if (!is_void(def
->type
, NULL
))
386 write_type(server
, def
->type
, def
, def
->tname
);
387 fprintf(server
, " _RetVal;\n");
390 /* declare arguments */
394 while (NEXT_LINK(var
)) var
= NEXT_LINK(var
);
398 write_type(server
, var
->type
, var
, var
->tname
);
399 fprintf(server
, " ");
400 write_name(server
, var
);
401 fprintf(server
, ";\n");
403 var
= PREV_LINK(var
);
407 print_server("MIDL_STUB_MESSAGE _StubMsg;\n");
408 print_server("RPC_STATUS _Status;\n");
409 fprintf(server
, "\n");
412 print_server("((void)(_Status));\n");
413 print_server("NdrServerInitializeNew(\n");
415 print_server("_pRpcMessage,\n");
416 print_server("&_StubMsg,\n");
417 print_server("&%s_StubDesc);\n", iface
->name
);
419 fprintf(server
, "\n");
423 print_server("%s = _pRpcMessage->Handle;\n", explicit_handle_var
->name
);
424 fprintf(server
, "\n");
427 print_server("RpcTryFinally\n");
430 print_server("RpcTryExcept\n");
436 print_server("if ((_pRpcMessage->DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
438 print_server("NdrConvert(\n");
440 print_server("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
441 print_server("(PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n", proc_offset
);
443 fprintf(server
, "\n");
445 unmarshall_arguments(func
);
448 print_server("if (_StubMsg.Buffer > _StubMsg.BufferEnd)\n");
451 print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
456 print_server("RpcExcept(RPC_BAD_STUB_DATA_EXCEPTION_FILTER)\n");
459 print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
462 print_server("RpcEndExcept\n");
463 fprintf(server
, "\n");
466 /* Call the real server function */
467 if (!is_void(def
->type
, NULL
))
468 print_server("_RetVal = ");
471 write_name(server
, def
);
477 fprintf(server
, "(\n");
480 while (NEXT_LINK(var
)) var
= NEXT_LINK(var
);
486 fprintf(server
, ",\n");
488 write_name(server
, var
);
489 var
= PREV_LINK(var
);
491 fprintf(server
, ");\n");
496 fprintf(server
, "();\n");
499 /* marshall the return value */
500 if (!is_void(def
->type
, NULL
))
502 fprintf(server
, "\n");
503 print_server("_StubMsg.BufferLength = %uU;\n", get_required_buffer_size(def
->type
));
504 print_server("_pRpcMessage->BufferLength = _StubMsg.BufferLength;\n");
505 fprintf(server
, "\n");
506 print_server("_Status = I_RpcGetBuffer(_pRpcMessage);\n");
507 print_server("if (_Status)\n");
509 print_server("RpcRaiseException(_Status);\n");
511 fprintf(server
, "\n");
512 print_server("_StubMsg.Buffer = (unsigned char __RPC_FAR *)_pRpcMessage->Buffer;\n");
513 fprintf(server
, "\n");
516 write_type(server
, def
->type
, def
, def
->tname
);
517 fprintf(server
, " __RPC_FAR *)_StubMsg.Buffer)++ = _RetVal;\n");
522 print_server("RpcFinally\n");
525 print_server("RpcEndFinally\n");
527 /* calculate buffer length */
528 fprintf(server
, "\n");
529 print_server("_pRpcMessage->BufferLength =\n");
531 print_server("(unsigned int)((long)_StubMsg.Buffer - (long)_pRpcMessage->Buffer);\n");
534 fprintf(server
, "}\n");
535 fprintf(server
, "\n");
537 /* update proc_offset */
541 while (NEXT_LINK(var
)) var
= NEXT_LINK(var
);
544 proc_offset
+= 2; /* FIXME */
545 var
= PREV_LINK(var
);
548 proc_offset
+= 2; /* FIXME */
550 func
= PREV_LINK(func
);
555 static void write_dispatchtable(type_t
*iface
)
557 unsigned long ver
= get_attrv(iface
->attrs
, ATTR_VERSION
);
558 unsigned long method_count
= 0;
559 func_t
*cur
= iface
->funcs
;
561 print_server("static RPC_DISPATCH_FUNCTION %s_table[] =\n", iface
->name
);
564 while (NEXT_LINK(cur
)) cur
= NEXT_LINK(cur
);
567 var_t
*def
= cur
->def
;
569 print_server("%s_", iface
->name
);
570 write_name(server
, def
);
571 fprintf(server
, ",\n");
574 cur
= PREV_LINK(cur
);
578 print_server("};\n");
579 print_server("RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable =\n", iface
->name
, LOWORD(ver
), HIWORD(ver
));
582 print_server("%u,\n", method_count
);
583 print_server("%s_table\n", iface
->name
);
585 print_server("};\n");
586 fprintf(server
, "\n");
590 static void write_stubdescdecl(type_t
*iface
)
592 print_server("extern const MIDL_STUB_DESC %s_StubDesc;\n", iface
->name
);
593 fprintf(server
, "\n");
597 static void write_stubdescriptor(type_t
*iface
)
599 print_server("static const MIDL_STUB_DESC %s_StubDesc =\n", iface
->name
);
602 print_server("(void __RPC_FAR *)& %s___RpcServerInterface,\n", iface
->name
);
603 print_server("MIDL_user_allocate,\n");
604 print_server("MIDL_user_free,\n");
605 print_server("0,\n");
606 print_server("0,\n");
607 print_server("0,\n");
608 print_server("0,\n");
609 print_server("0,\n");
610 print_server("__MIDL_TypeFormatString.Format,\n");
611 print_server("1, /* -error bounds_check flag */\n");
612 print_server("0x10001, /* Ndr library version */\n");
613 print_server("0,\n");
614 print_server("0x50100a4, /* MIDL Version 5.1.164 */\n");
615 print_server("0,\n");
616 print_server("0,\n");
617 print_server("0, /* notify & notify_flag routine table */\n");
618 print_server("1, /* Flags */\n");
619 print_server("0, /* Reserved3 */\n");
620 print_server("0, /* Reserved4 */\n");
621 print_server("0 /* Reserved5 */\n");
623 print_server("};\n");
624 fprintf(server
, "\n");
628 static void write_serverinterfacedecl(type_t
*iface
)
630 unsigned long ver
= get_attrv(iface
->attrs
, ATTR_VERSION
);
631 UUID
*uuid
= get_attrp(iface
->attrs
, ATTR_UUID
);
633 print_server("extern RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable;\n", iface
->name
, LOWORD(ver
), HIWORD(ver
));
634 fprintf(server
, "\n");
635 print_server("static const RPC_SERVER_INTERFACE %s___RpcServerInterface =\n", iface
->name
);
638 print_server("sizeof(RPC_SERVER_INTERFACE),\n");
639 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",
640 uuid
->Data1
, uuid
->Data2
, uuid
->Data3
, uuid
->Data4
[0], uuid
->Data4
[1],
641 uuid
->Data4
[2], uuid
->Data4
[3], uuid
->Data4
[4], uuid
->Data4
[5], uuid
->Data4
[6],
642 uuid
->Data4
[7], LOWORD(ver
), HIWORD(ver
));
643 print_server("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
644 print_server("&%s_v%d_%d_DispatchTable,\n", iface
->name
, LOWORD(ver
), HIWORD(ver
));
645 print_server("0,\n");
646 print_server("0,\n");
647 print_server("0,\n");
648 print_server("0,\n");
649 print_server("0,\n");
651 print_server("};\n");
652 print_server("RPC_IF_HANDLE %s_v%d_%d_s_ifspec = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
653 iface
->name
, LOWORD(ver
), HIWORD(ver
), iface
->name
);
654 fprintf(server
, "\n");
657 static void write_formatdesc( const char *str
)
659 print_server("typedef struct _MIDL_%s_FORMAT_STRING\n", str
);
662 print_server("short Pad;\n");
663 print_server("unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str
);
665 print_server("} MIDL_%s_FORMAT_STRING;\n", str
);
670 static void write_formatstringsdecl(type_t
*iface
)
676 print_server("#define TYPE_FORMAT_STRING_SIZE %d\n", 3); /* FIXME */
678 /* determine the proc format string size */
680 while (NEXT_LINK(func
)) func
= NEXT_LINK(func
);
683 /* argument list size */
687 while (NEXT_LINK(var
)) var
= NEXT_LINK(var
);
690 byte_count
+= 2; /* FIXME: determine real size */
691 var
= PREV_LINK(var
);
695 /* return value size */
696 byte_count
+= 2; /* FIXME: determine real size */
697 func
= PREV_LINK(func
);
699 print_server("#define PROC_FORMAT_STRING_SIZE %d\n", byte_count
);
701 fprintf(server
, "\n");
702 write_formatdesc("TYPE");
703 write_formatdesc("PROC");
704 fprintf(server
, "\n");
705 print_server("extern const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
706 print_server("extern const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
711 static void init_server(void)
715 if (!(server
= fopen(server_name
, "w")))
716 error("Could not open %s for output\n", server_name
);
718 print_server("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", WIDL_FULLVERSION
, input_name
);
719 print_server("#include<string.h>\n");
720 fprintf(server
, "\n");
721 print_server("#include\"%s\"\n", header_name
);
722 fprintf(server
, "\n");
726 void write_server(ifref_t
*ifaces
)
728 ifref_t
*iface
= ifaces
;
742 fprintf(server
, "/*****************************************************************************\n");
743 fprintf(server
, " * %s interface\n", iface
->iface
->name
);
744 fprintf(server
, " */\n");
745 fprintf(server
, "\n");
747 write_formatstringsdecl(iface
->iface
);
748 write_serverinterfacedecl(iface
->iface
);
749 write_stubdescdecl(iface
->iface
);
751 write_function_stubs(iface
->iface
);
753 write_stubdescriptor(iface
->iface
);
754 write_dispatchtable(iface
->iface
);
756 print_server("#if !defined(__RPC_WIN32__)\n");
757 print_server("#error Invalid build platform for this stub.\n");
758 print_server("#endif\n");
759 fprintf(server
, "\n");
761 write_procformatstring(iface
->iface
);
762 write_typeformatstring();
764 fprintf(server
, "\n");
766 iface
= PREV_LINK(iface
);