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 fprintf(server
, "void __RPC_STUB\n");
370 fprintf(server
, "%s_", iface
->name
);
371 write_name(server
, def
);
372 fprintf(server
, "(\n");
374 print_server("PRPC_MESSAGE _pRpcMessage)\n");
377 /* write the functions body */
378 fprintf(server
, "{\n");
381 /* declare return value '_RetVal' */
382 if (!is_void(def
->type
, NULL
))
385 write_type(server
, def
->type
, def
, def
->tname
);
386 fprintf(server
, " _RetVal;\n");
389 /* declare arguments */
393 while (NEXT_LINK(var
)) var
= NEXT_LINK(var
);
397 write_type(server
, var
->type
, var
, var
->tname
);
398 fprintf(server
, " ");
399 write_name(server
, var
);
400 fprintf(server
, ";\n");
402 var
= PREV_LINK(var
);
406 print_server("MIDL_STUB_MESSAGE _StubMsg;\n");
407 print_server("RPC_STATUS _Status;\n");
408 fprintf(server
, "\n");
411 print_server("((void)(_Status));\n");
412 print_server("NdrServerInitializeNew(\n");
414 print_server("_pRpcMessage,\n");
415 print_server("&_StubMsg,\n");
416 print_server("&%s_StubDesc);\n", iface
->name
);
418 fprintf(server
, "\n");
422 print_server("%s = _pRpcMessage->Handle;\n", explicit_handle_var
->name
);
423 fprintf(server
, "\n");
426 print_server("RpcTryFinally\n");
429 print_server("RpcTryExcept\n");
435 print_server("if ((_pRpcMessage->DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
437 print_server("NdrConvert(\n");
439 print_server("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
440 print_server("(PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n", proc_offset
);
442 fprintf(server
, "\n");
444 unmarshall_arguments(func
);
447 print_server("if (_StubMsg.Buffer > _StubMsg.BufferEnd)\n");
450 print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
455 print_server("RpcExcept(RPC_BAD_STUB_DATA_EXCEPTION_FILTER)\n");
458 print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
461 print_server("RpcEndExcept\n");
462 fprintf(server
, "\n");
465 /* Call the real server function */
466 if (!is_void(def
->type
, NULL
))
467 print_server("_RetVal = ");
470 write_name(server
, def
);
476 fprintf(server
, "(\n");
479 while (NEXT_LINK(var
)) var
= NEXT_LINK(var
);
485 fprintf(server
, ",\n");
487 write_name(server
, var
);
488 var
= PREV_LINK(var
);
490 fprintf(server
, ");\n");
495 fprintf(server
, "();\n");
498 /* marshall the return value */
499 if (!is_void(def
->type
, NULL
))
501 fprintf(server
, "\n");
502 print_server("_StubMsg.BufferLength = %uU;\n", get_required_buffer_size(def
->type
));
503 print_server("_pRpcMessage->BufferLength = _StubMsg.BufferLength;\n");
504 fprintf(server
, "\n");
505 print_server("_Status = I_RpcGetBuffer(_pRpcMessage);\n");
506 print_server("if (_Status)\n");
508 print_server("RpcRaiseException(_Status);\n");
510 fprintf(server
, "\n");
511 print_server("_StubMsg.Buffer = (unsigned char __RPC_FAR *)_pRpcMessage->Buffer;\n");
512 fprintf(server
, "\n");
515 write_type(server
, def
->type
, def
, def
->tname
);
516 fprintf(server
, " __RPC_FAR *)_StubMsg.Buffer)++ = _RetVal;\n");
521 print_server("RpcFinally\n");
524 print_server("RpcEndFinally\n");
526 /* calculate buffer length */
527 fprintf(server
, "\n");
528 print_server("_pRpcMessage->BufferLength =\n");
530 print_server("(unsigned int)((long)_StubMsg.Buffer - (long)_pRpcMessage->Buffer);\n");
533 fprintf(server
, "}\n");
534 fprintf(server
, "\n");
536 /* update proc_offset */
540 while (NEXT_LINK(var
)) var
= NEXT_LINK(var
);
543 proc_offset
+= 2; /* FIXME */
544 var
= PREV_LINK(var
);
547 proc_offset
+= 2; /* FIXME */
549 func
= PREV_LINK(func
);
554 static void write_dispatchtable(type_t
*iface
)
556 unsigned long ver
= get_attrv(iface
->attrs
, ATTR_VERSION
);
557 unsigned long method_count
= 0;
558 func_t
*cur
= iface
->funcs
;
560 print_server("static RPC_DISPATCH_FUNCTION %s_table[] =\n", iface
->name
);
563 while (NEXT_LINK(cur
)) cur
= NEXT_LINK(cur
);
566 var_t
*def
= cur
->def
;
568 print_server("%s_", iface
->name
);
569 write_name(server
, def
);
570 fprintf(server
, ",\n");
573 cur
= PREV_LINK(cur
);
577 print_server("};\n");
578 print_server("RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable =\n", iface
->name
, LOWORD(ver
), HIWORD(ver
));
581 print_server("%u,\n", method_count
);
582 print_server("%s_table\n", iface
->name
);
584 print_server("};\n");
585 fprintf(server
, "\n");
589 static void write_stubdescdecl(type_t
*iface
)
591 print_server("extern const MIDL_STUB_DESC %s_StubDesc;\n", iface
->name
);
592 fprintf(server
, "\n");
596 static void write_stubdescriptor(type_t
*iface
)
598 print_server("static const MIDL_STUB_DESC %s_StubDesc =\n", iface
->name
);
601 print_server("(void __RPC_FAR *)& %s___RpcServerInterface,\n", iface
->name
);
602 print_server("MIDL_user_allocate,\n");
603 print_server("MIDL_user_free,\n");
604 print_server("{NULL},\n");
605 print_server("0,\n");
606 print_server("0,\n");
607 print_server("0,\n");
608 print_server("0,\n");
609 print_server("__MIDL_TypeFormatString.Format,\n");
610 print_server("1, /* -error bounds_check flag */\n");
611 print_server("0x10001, /* Ndr library version */\n");
612 print_server("0,\n");
613 print_server("0x50100a4, /* MIDL Version 5.1.164 */\n");
614 print_server("0,\n");
615 print_server("0,\n");
616 print_server("0, /* notify & notify_flag routine table */\n");
617 print_server("1, /* Flags */\n");
618 print_server("0, /* Reserved3 */\n");
619 print_server("0, /* Reserved4 */\n");
620 print_server("0 /* Reserved5 */\n");
622 print_server("};\n");
623 fprintf(server
, "\n");
627 static void write_serverinterfacedecl(type_t
*iface
)
629 unsigned long ver
= get_attrv(iface
->attrs
, ATTR_VERSION
);
630 UUID
*uuid
= get_attrp(iface
->attrs
, ATTR_UUID
);
632 print_server("extern RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable;\n", iface
->name
, LOWORD(ver
), HIWORD(ver
));
633 fprintf(server
, "\n");
634 print_server("static const RPC_SERVER_INTERFACE %s___RpcServerInterface =\n", iface
->name
);
637 print_server("sizeof(RPC_SERVER_INTERFACE),\n");
638 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",
639 uuid
->Data1
, uuid
->Data2
, uuid
->Data3
, uuid
->Data4
[0], uuid
->Data4
[1],
640 uuid
->Data4
[2], uuid
->Data4
[3], uuid
->Data4
[4], uuid
->Data4
[5], uuid
->Data4
[6],
641 uuid
->Data4
[7], LOWORD(ver
), HIWORD(ver
));
642 print_server("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
643 print_server("&%s_v%d_%d_DispatchTable,\n", iface
->name
, LOWORD(ver
), HIWORD(ver
));
644 print_server("0,\n");
645 print_server("0,\n");
646 print_server("0,\n");
647 print_server("0,\n");
648 print_server("0,\n");
650 print_server("};\n");
651 print_server("RPC_IF_HANDLE %s_v%d_%d_s_ifspec = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
652 iface
->name
, LOWORD(ver
), HIWORD(ver
), iface
->name
);
653 fprintf(server
, "\n");
656 static void write_formatdesc( const char *str
)
658 print_server("typedef struct _MIDL_%s_FORMAT_STRING\n", str
);
661 print_server("short Pad;\n");
662 print_server("unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str
);
664 print_server("} MIDL_%s_FORMAT_STRING;\n", str
);
669 static void write_formatstringsdecl(type_t
*iface
)
675 print_server("#define TYPE_FORMAT_STRING_SIZE %d\n", 3); /* FIXME */
677 /* determine the proc format string size */
679 while (NEXT_LINK(func
)) func
= NEXT_LINK(func
);
682 /* argument list size */
686 while (NEXT_LINK(var
)) var
= NEXT_LINK(var
);
689 byte_count
+= 2; /* FIXME: determine real size */
690 var
= PREV_LINK(var
);
694 /* return value size */
695 byte_count
+= 2; /* FIXME: determine real size */
696 func
= PREV_LINK(func
);
698 print_server("#define PROC_FORMAT_STRING_SIZE %d\n", byte_count
);
700 fprintf(server
, "\n");
701 write_formatdesc("TYPE");
702 write_formatdesc("PROC");
703 fprintf(server
, "\n");
704 print_server("extern const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
705 print_server("extern const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
710 static void init_server(void)
714 if (!(server
= fopen(server_name
, "w")))
715 error("Could not open %s for output\n", server_name
);
717 print_server("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", WIDL_FULLVERSION
, input_name
);
718 print_server("#include<string.h>\n");
719 fprintf(server
, "\n");
720 print_server("#include\"%s\"\n", header_name
);
721 fprintf(server
, "\n");
725 void write_server(ifref_t
*ifaces
)
727 ifref_t
*iface
= ifaces
;
741 fprintf(server
, "/*****************************************************************************\n");
742 fprintf(server
, " * %s interface\n", iface
->iface
->name
);
743 fprintf(server
, " */\n");
744 fprintf(server
, "\n");
746 write_formatstringsdecl(iface
->iface
);
747 write_serverinterfacedecl(iface
->iface
);
748 write_stubdescdecl(iface
->iface
);
750 write_function_stubs(iface
->iface
);
752 write_stubdescriptor(iface
->iface
);
753 write_dispatchtable(iface
->iface
);
755 print_server("#if !defined(__RPC_WIN32__)\n");
756 print_server("#error Invalid build platform for this stub.\n");
757 print_server("#endif\n");
758 fprintf(server
, "\n");
760 write_procformatstring(iface
->iface
);
761 write_typeformatstring();
763 fprintf(server
, "\n");
765 iface
= PREV_LINK(iface
);