client.c 21.3 KB
Newer Older
1 2 3
/*
 * IDL Compiler
 *
4
 * Copyright 2005-2006 Eric Kohl
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 22
 */

#include "config.h"
#include "wine/port.h"
23

24 25
#include <stdio.h>
#include <stdlib.h>
26 27 28
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
29 30 31 32 33 34 35 36 37
#include <string.h>
#include <ctype.h>

#include "widl.h"
#include "utils.h"
#include "parser.h"
#include "header.h"

#include "widltypes.h"
38
#include "typegen.h"
39
#include "expr.h"
40 41 42 43

static FILE* client;
static int indent = 0;

44
static void print_client( const char *format, ... ) __attribute__((format (printf, 1, 2)));
45
static void print_client( const char *format, ... )
46 47 48
{
    va_list va;
    va_start(va, format);
49
    print(client, indent, format, va);
50 51 52
    va_end(va);
}

53
static void write_client_func_decl( const type_t *iface, const var_t *func )
54
{
55
    const char *callconv = get_attrp(func->declspec.type->attrs, ATTR_CALLCONV);
56
    const var_list_t *args = type_function_get_args(func->declspec.type);
57
    const decl_spec_t *rettype = type_function_get_ret(func->declspec.type);
58

59
    if (!callconv) callconv = "__cdecl";
60
    write_type_decl_left(client, rettype);
61
    fprintf(client, " %s ", callconv);
62 63 64 65 66 67 68 69 70
    fprintf(client, "%s%s(\n", prefix_client, get_name(func));
    indent++;
    if (args)
        write_args(client, args, iface->name, 0, TRUE);
    else
        print_client("void");
    fprintf(client, ")\n");
    indent--;
}
71

72 73 74 75 76
static void write_function_stub( const type_t *iface, const var_t *func,
                                 int method_count, unsigned int proc_offset )
{
    unsigned char explicit_fc, implicit_fc;
    int has_full_pointer = is_full_pointer_function(func);
77
    var_t *retval = type_function_get_retval(func->declspec.type);
78
    const var_t *handle_var = get_func_handle_var( iface, func, &explicit_fc, &implicit_fc );
79
    int has_ret = !is_void(retval->declspec.type);
80 81

    if (is_interpreted_func( iface, func ))
82
    {
83
        write_client_func_decl( iface, func );
84
        write_client_call_routine( client, iface, func, iface->name, proc_offset );
85 86
        return;
    }
87

88 89 90 91 92 93
    print_client( "struct __frame_%s%s\n{\n", prefix_client, get_name(func) );
    indent++;
    print_client( "__DECL_EXCEPTION_FRAME\n" );
    print_client("MIDL_STUB_MESSAGE _StubMsg;\n");
    if (handle_var)
    {
94
        if (explicit_fc == FC_BIND_GENERIC)
95 96 97 98
            print_client("%s %s;\n",
                         get_explicit_generic_handle_type(handle_var)->name, handle_var->name );
        print_client("RPC_BINDING_HANDLE _Handle;\n");
    }
99

100
    if (has_ret && decl_indirect(retval->declspec.type))
101
    {
102
        print_client("void *_p_%s;\n", retval->name);
103 104 105
    }
    indent--;
    print_client( "};\n\n" );
106

107 108 109
    print_client( "static void __finally_%s%s(", prefix_client, get_name(func) );
    print_client( " struct __frame_%s%s *__frame )\n{\n", prefix_client, get_name(func) );
    indent++;
110

111 112
    if (has_full_pointer)
        write_full_pointer_free(client, indent, func);
113

114 115
    print_client("NdrFreeBuffer(&__frame->_StubMsg);\n");

116
    if (explicit_fc == FC_BIND_GENERIC)
117 118 119
    {
        fprintf(client, "\n");
        print_client("if (__frame->_Handle)\n");
120
        indent++;
121 122
        print_client("%s_unbind(__frame->%s, __frame->_Handle);\n",
                     get_explicit_generic_handle_type(handle_var)->name, handle_var->name);
123
        indent--;
124 125 126
    }
    indent--;
    print_client( "}\n\n" );
127

128
    write_client_func_decl( iface, func );
129

130 131 132 133
    /* write the functions body */
    fprintf(client, "{\n");
    indent++;
    print_client( "struct __frame_%s%s __f, * const __frame = &__f;\n", prefix_client, get_name(func) );
134

135
    /* declare return value */
136 137 138
    if (has_ret)
    {
        print_client("%s", "");
139
        write_type_decl(client, &retval->declspec, retval->name);
140
        fprintf(client, ";\n");
141 142
    }
    print_client("RPC_MESSAGE _RpcMessage;\n");
143

144 145 146
    if (handle_var)
    {
        print_client( "__frame->_Handle = 0;\n" );
147
        if (explicit_fc == FC_BIND_GENERIC)
148 149
            print_client("__frame->%s = %s;\n", handle_var->name, handle_var->name );
    }
150
    if (has_ret && decl_indirect(retval->declspec.type))
151
    {
152
        print_client("__frame->_p_%s = &%s;\n", retval->name, retval->name);
153 154
    }
    fprintf(client, "\n");
155

156
    print_client( "RpcExceptionInit( 0, __finally_%s%s );\n", prefix_client, get_name(func) );
157

158 159
    if (has_full_pointer)
        write_full_pointer_init(client, indent, func, FALSE);
160

161
    /* check pointers */
162
    write_pointer_checks( client, indent, func );
163

164 165 166
    print_client("RpcTryFinally\n");
    print_client("{\n");
    indent++;
167

168 169
    print_client("NdrClientInitializeNew(&_RpcMessage, &__frame->_StubMsg, &%s_StubDesc, %d);\n",
                 iface->name, method_count);
170

171 172 173 174 175 176 177 178 179 180 181 182
    if (is_attr(func->attrs, ATTR_IDEMPOTENT) || is_attr(func->attrs, ATTR_BROADCAST))
    {
        print_client("_RpcMessage.RpcFlags = ( RPC_NCA_FLAGS_DEFAULT ");
        if (is_attr(func->attrs, ATTR_IDEMPOTENT))
            fprintf(client, "| RPC_NCA_FLAGS_IDEMPOTENT ");
        if (is_attr(func->attrs, ATTR_BROADCAST))
            fprintf(client, "| RPC_NCA_FLAGS_BROADCAST ");
        fprintf(client, ");\n\n");
    }

    switch (explicit_fc)
    {
183
    case FC_BIND_PRIMITIVE:
184 185 186
        print_client("__frame->_Handle = %s;\n", handle_var->name);
        fprintf(client, "\n");
        break;
187
    case FC_BIND_GENERIC:
188 189 190 191
        print_client("__frame->_Handle = %s_bind(%s);\n",
                     get_explicit_generic_handle_type(handle_var)->name, handle_var->name);
        fprintf(client, "\n");
        break;
192
    case FC_BIND_CONTEXT:
193 194 195 196
    {
        /* if the context_handle attribute appears in the chain of types
         * without pointers being followed, then the context handle must
         * be direct, otherwise it is a pointer */
197
        int is_ch_ptr = !is_aliaschain_attr(handle_var->declspec.type, ATTR_CONTEXTHANDLE);
198 199 200 201 202 203
        print_client("if (%s%s != 0)\n", is_ch_ptr ? "*" : "", handle_var->name);
        indent++;
        print_client("__frame->_Handle = NDRCContextBinding(%s%s);\n",
                     is_ch_ptr ? "*" : "", handle_var->name);
        indent--;
        if (is_attr(handle_var->attrs, ATTR_IN) && !is_attr(handle_var->attrs, ATTR_OUT))
204
        {
205
            print_client("else\n");
206
            indent++;
207
            print_client("RpcRaiseException(RPC_X_SS_IN_NULL_CONTEXT);\n");
208 209
            indent--;
        }
210 211 212 213
        fprintf(client, "\n");
        break;
    }
    case 0:  /* implicit handle */
214
        if (handle_var)
215 216 217 218 219 220
        {
            print_client("__frame->_Handle = %s;\n", handle_var->name);
            fprintf(client, "\n");
        }
        break;
    }
221

222
    write_remoting_arguments(client, indent, func, "", PASS_IN, PHASE_BUFFERSIZE);
223

224 225 226 227 228
    print_client("NdrGetBuffer(&__frame->_StubMsg, __frame->_StubMsg.BufferLength, ");
    if (handle_var)
        fprintf(client, "__frame->_Handle);\n\n");
    else
        fprintf(client,"%s__MIDL_AutoBindHandle);\n\n", iface->name);
229

230 231
    /* marshal arguments */
    write_remoting_arguments(client, indent, func, "", PASS_IN, PHASE_MARSHAL);
Robert Shearman's avatar
Robert Shearman committed
232

233 234 235 236 237
    /* send/receive message */
    /* print_client("NdrNsSendReceive(\n"); */
    /* print_client("(unsigned char *)__frame->_StubMsg.Buffer,\n"); */
    /* print_client("(RPC_BINDING_HANDLE *) &%s__MIDL_AutoBindHandle);\n", iface->name); */
    print_client("NdrSendReceive(&__frame->_StubMsg, __frame->_StubMsg.Buffer);\n\n");
238

239 240
    print_client("__frame->_StubMsg.BufferStart = _RpcMessage.Buffer;\n");
    print_client("__frame->_StubMsg.BufferEnd = __frame->_StubMsg.BufferStart + _RpcMessage.BufferLength;\n");
241

242 243
    if (has_out_arg_or_return(func))
    {
244
        fprintf(client, "\n");
245

246
        print_client("if ((_RpcMessage.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
247
        indent++;
248 249
        print_client("NdrConvert(&__frame->_StubMsg, (PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n",
                     proc_offset);
250
        indent--;
251
    }
252

253 254 255
    /* unmarshall arguments */
    fprintf(client, "\n");
    write_remoting_arguments(client, indent, func, "", PASS_OUT, PHASE_UNMARSHAL);
256

257 258 259
    /* unmarshal return value */
    if (has_ret)
    {
260
        if (decl_indirect(retval->declspec.type))
261
            print_client("MIDL_memset(&%s, 0, sizeof(%s));\n", retval->name, retval->name);
262
        else if (is_ptr(retval->declspec.type) || is_array(retval->declspec.type))
263
            print_client("%s = 0;\n", retval->name);
264 265
        write_remoting_arguments(client, indent, func, "", PASS_RETURN, PHASE_UNMARSHAL);
    }
266

267 268 269 270 271 272 273 274 275 276 277 278 279 280
    indent--;
    print_client("}\n");
    print_client("RpcFinally\n");
    print_client("{\n");
    indent++;
    print_client( "__finally_%s%s( __frame );\n", prefix_client, get_name(func) );
    indent--;
    print_client("}\n");
    print_client("RpcEndFinally\n");


    /* emit return code */
    if (has_ret)
    {
281
        fprintf(client, "\n");
282
        print_client("return %s;\n", retval->name);
283
    }
284

285 286 287 288 289
    indent--;
    fprintf(client, "}\n");
    fprintf(client, "\n");
}

290 291 292
static void write_serialize_function(FILE *file, const type_t *type, const type_t *iface,
                                     const char *func_name, const char *ret_type)
{
293
    enum stub_mode mode = get_stub_mode();
294
    static int emitted_pickling_info;
295 296 297 298 299 300 301 302 303

    if (iface && !type->typestring_offset)
    {
        /* FIXME: Those are mostly basic types. They should be implemented
         * using NdrMesSimpleType* functions */
        if (ret_type) warning("Serialization of type %s is not supported\n", type->name);
        return;
    }

304
    if (!emitted_pickling_info && iface && mode != MODE_Os)
305 306 307 308 309 310 311 312 313 314
    {
        fprintf(file, "static const MIDL_TYPE_PICKLING_INFO __MIDL_TypePicklingInfo =\n");
        fprintf(file, "{\n");
        fprintf(file, "    0x33205054,\n");
        fprintf(file, "    0x3,\n");
        fprintf(file, "    0,\n");
        fprintf(file, "    0,\n");
        fprintf(file, "    0\n");
        fprintf(file, "};\n");
        fprintf(file, "\n");
315
        emitted_pickling_info = 1;
316 317
    }

318 319 320 321
    /* FIXME: Assuming explicit handle */

    fprintf(file, "%s __cdecl %s_%s(handle_t IDL_handle, %s *IDL_type)%s\n",
            ret_type ? ret_type : "void", type->name, func_name, type->name, iface ? "" : ";");
322 323 324 325 326 327 328 329 330 331 332 333 334 335
    if (!iface) return; /* declaration only */

    fprintf(file, "{\n");
    fprintf(file, "    %sNdrMesType%s%s(\n", ret_type ? "return " : "", func_name,
            mode != MODE_Os ? "2" : "");
    fprintf(file, "        IDL_handle,\n");
    if (mode != MODE_Os)
        fprintf(file, "        (MIDL_TYPE_PICKLING_INFO*)&__MIDL_TypePicklingInfo,\n");
    fprintf(file, "        &%s_StubDesc,\n", iface->name);
    fprintf(file, "        (PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%u],\n",
            type->typestring_offset);
    fprintf(file, "        IDL_type);\n");
    fprintf(file, "}\n");
    fprintf(file, "\n");
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
}

void write_serialize_functions(FILE *file, const type_t *type, const type_t *iface)
{
    if (is_attr(type->attrs, ATTR_ENCODE))
    {
        write_serialize_function(file, type, iface, "AlignSize", "SIZE_T");
        write_serialize_function(file, type, iface, "Encode", NULL);
    }
    if (is_attr(type->attrs, ATTR_DECODE))
    {
        write_serialize_function(file, type, iface, "Decode", NULL);
        write_serialize_function(file, type, iface, "Free", NULL);
    }
}

352 353 354 355 356 357 358 359 360
static void write_function_stubs(type_t *iface, unsigned int *proc_offset)
{
    const statement_t *stmt;
    const var_t *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
    int method_count = 0;

    if (!implicit_handle)
        print_client("static RPC_BINDING_HANDLE %s__MIDL_AutoBindHandle;\n\n", iface->name);

361
    LIST_FOR_EACH_ENTRY( stmt, type_iface_get_stmts(iface), const statement_t, entry )
362
    {
363 364 365 366 367
        switch (stmt->type)
        {
        case STMT_DECLARATION:
        {
            const var_t *func = stmt->u.var;
368 369
            if (stmt->u.var->declspec.stgclass != STG_NONE
                || type_get_type_detect_alias(stmt->u.var->declspec.type) != TYPE_FUNCTION)
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
                continue;
            write_function_stub( iface, func, method_count++, *proc_offset );
            *proc_offset += get_size_procformatstring_func( iface, func );
            break;
        }
        case STMT_TYPEDEF:
        {
            const type_list_t *type_entry;
            for (type_entry = stmt->u.type_list; type_entry; type_entry = type_entry->next)
                write_serialize_functions(client, type_entry->type, iface);
            break;
        }
        default:
            break;
        }
385 386 387 388 389 390
    }
}


static void write_stubdescdecl(type_t *iface)
{
391
    print_client("static const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
392 393 394 395
    fprintf(client, "\n");
}


396
static void write_stubdescriptor(type_t *iface, int expr_eval_routines)
397
{
398
    const var_t *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
399 400 401 402

    print_client("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
    print_client("{\n");
    indent++;
403
    print_client("(void *)& %s___RpcClientInterface,\n", iface->name);
404 405
    print_client("MIDL_user_allocate,\n");
    print_client("MIDL_user_free,\n");
406 407
    print_client("{\n");
    indent++;
Eric Kohl's avatar
Eric Kohl committed
408
    if (implicit_handle)
409
        print_client("&%s,\n", implicit_handle->name);
410 411
    else
        print_client("&%s__MIDL_AutoBindHandle,\n", iface->name);
412 413
    indent--;
    print_client("},\n");
414
    print_client("0,\n");
415 416 417 418
    if (!list_empty( &generic_handle_list ))
        print_client("BindingRoutines,\n");
    else
        print_client("0,\n");
419 420 421 422
    if (expr_eval_routines)
        print_client("ExprEvalRoutines,\n");
    else
        print_client("0,\n");
423 424 425
    print_client("0,\n");
    print_client("__MIDL_TypeFormatString.Format,\n");
    print_client("1, /* -error bounds_check flag */\n");
426
    print_client("0x%x, /* Ndr library version */\n", get_stub_mode() == MODE_Oif ? 0x50002 : 0x10001);
427
    print_client("0,\n");
428
    print_client("0x50200ca, /* MIDL Version 5.2.202 */\n");
429
    print_client("0,\n");
430
    print_client("%s,\n", list_empty(&user_type_list) ? "0" : "UserMarshalRoutines");
431 432 433 434 435 436 437 438 439 440 441 442 443
    print_client("0,  /* notify & notify_flag routine table */\n");
    print_client("1,  /* Flags */\n");
    print_client("0,  /* Reserved3 */\n");
    print_client("0,  /* Reserved4 */\n");
    print_client("0   /* Reserved5 */\n");
    indent--;
    print_client("};\n");
    fprintf(client, "\n");
}


static void write_clientinterfacedecl(type_t *iface)
{
444
    unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
445
    const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
446 447 448
    const str_list_t *endpoints = get_attrp(iface->attrs, ATTR_ENDPOINT);

    if (endpoints) write_endpoints( client, iface->name, endpoints );
449 450 451 452 453

    print_client("static const RPC_CLIENT_INTERFACE %s___RpcClientInterface =\n", iface->name );
    print_client("{\n");
    indent++;
    print_client("sizeof(RPC_CLIENT_INTERFACE),\n");
454
    print_client("{{0x%08x,0x%04x,0x%04x,{0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x}},{%d,%d}},\n",
455 456
                 uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
                 uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
457
                 uuid->Data4[7], MAJORVERSION(ver), MINORVERSION(ver));
458 459
    print_client("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
    print_client("0,\n");
460 461 462 463 464 465 466 467 468 469
    if (endpoints)
    {
        print_client("%u,\n", list_count(endpoints));
        print_client("(PRPC_PROTSEQ_ENDPOINT)%s__RpcProtseqEndpoint,\n", iface->name);
    }
    else
    {
        print_client("0,\n");
        print_client("0,\n");
    }
470 471 472 473 474
    print_client("0,\n");
    print_client("0,\n");
    print_client("0,\n");
    indent--;
    print_client("};\n");
475
    if (old_names)
476
        print_client("RPC_IF_HANDLE %s_ClientIfHandle DECLSPEC_HIDDEN = (RPC_IF_HANDLE)& %s___RpcClientInterface;\n",
477 478
                     iface->name, iface->name);
    else
479
        print_client("RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec DECLSPEC_HIDDEN = (RPC_IF_HANDLE)& %s___RpcClientInterface;\n",
480
                     prefix_client, iface->name, MAJORVERSION(ver), MINORVERSION(ver), iface->name);
481 482 483 484 485 486
    fprintf(client, "\n");
}


static void write_implicithandledecl(type_t *iface)
{
487
    const var_t *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
488

489
    if (implicit_handle)
490
    {
491
        write_type_decl(client, &implicit_handle->declspec, implicit_handle->name);
492
        fprintf(client, ";\n\n");
493 494 495 496 497 498 499 500 501 502
    }
}


static void init_client(void)
{
    if (client) return;
    if (!(client = fopen(client_name, "w")))
        error("Could not open %s for output\n", client_name);

503
    print_client("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
504
    print_client("#include <string.h>\n");
505
    print_client( "\n");
506
    print_client("#include \"%s\"\n", header_name);
507
    print_client( "\n");
508 509 510 511
    print_client( "#ifndef DECLSPEC_HIDDEN\n");
    print_client( "#define DECLSPEC_HIDDEN\n");
    print_client( "#endif\n");
    print_client( "\n");
512 513 514
}


515 516 517 518 519
static void write_client_ifaces(const statement_list_t *stmts, int expr_eval_routines, unsigned int *proc_offset)
{
    const statement_t *stmt;
    if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
    {
520
        if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
521
        {
522
            int needs_stub = 0;
523
            const statement_t *stmt2;
524 525 526 527 528 529 530 531 532
            type_t *iface = stmt->u.type;
            if (!need_stub(iface))
                return;

            fprintf(client, "/*****************************************************************************\n");
            fprintf(client, " * %s interface\n", iface->name);
            fprintf(client, " */\n");
            fprintf(client, "\n");

533
            LIST_FOR_EACH_ENTRY(stmt2, type_iface_get_stmts(iface), const statement_t, entry)
534
            {
535 536
                if (stmt2->type == STMT_DECLARATION && stmt2->u.var->declspec.stgclass == STG_NONE &&
                    type_get_type_detect_alias(stmt2->u.var->declspec.type) == TYPE_FUNCTION)
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
                {
                    needs_stub = 1;
                    break;
                }
                if (stmt2->type == STMT_TYPEDEF)
                {
                    const type_list_t *type_entry;
                    for (type_entry = stmt2->u.type_list; type_entry; type_entry = type_entry->next)
                    {
                        if (is_attr(type_entry->type->attrs, ATTR_ENCODE)
                            || is_attr(type_entry->type->attrs, ATTR_DECODE))
                        {
                            needs_stub = 1;
                            break;
                        }
                    }
                    if (needs_stub)
                        break;
                }
556
            }
557
            if (needs_stub)
558 559 560 561 562 563 564
            {
                write_implicithandledecl(iface);

                write_clientinterfacedecl(iface);
                write_stubdescdecl(iface);
                write_function_stubs(iface, proc_offset);

565
                print_client("#if !defined(__RPC_WIN%u__)\n", pointer_size == 8 ? 64 : 32);
566 567 568 569 570 571 572 573 574 575
                print_client("#error  Invalid build platform for this stub.\n");
                print_client("#endif\n");

                fprintf(client, "\n");
                write_stubdescriptor(iface, expr_eval_routines);
            }
        }
    }
}

576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
static void write_generic_handle_routine_list(void)
{
    generic_handle_t *gh;

    if (list_empty( &generic_handle_list )) return;
    print_client( "static const GENERIC_BINDING_ROUTINE_PAIR BindingRoutines[] =\n" );
    print_client( "{\n" );
    indent++;
    LIST_FOR_EACH_ENTRY( gh, &generic_handle_list, generic_handle_t, entry )
    {
        print_client( "{ (GENERIC_BINDING_ROUTINE)%s_bind, (GENERIC_UNBIND_ROUTINE)%s_unbind },\n",
                      gh->name, gh->name );
    }
    indent--;
    print_client( "};\n\n" );
}

593
static void write_client_routines(const statement_list_t *stmts)
594
{
595
    unsigned int proc_offset = 0;
596
    int expr_eval_routines;
597

598 599 600 601 602
    if (need_inline_stubs_file( stmts ))
    {
        write_exceptions( client );
        print_client( "\n");
    }
603

604
    write_formatstringsdecl(client, indent, stmts, need_stub);
605 606 607
    expr_eval_routines = write_expr_eval_routines(client, client_token);
    if (expr_eval_routines)
        write_expr_eval_routine_list(client, client_token);
608
    write_generic_handle_routine_list();
609
    write_user_quad_list(client);
610

611
    write_client_ifaces(stmts, expr_eval_routines, &proc_offset);
612

613 614
    fprintf(client, "\n");

615 616
    write_procformatstring(client, stmts, need_stub);
    write_typeformatstring(client, stmts, need_stub);
617 618 619 620 621 622 623 624 625 626 627 628 629
}

void write_client(const statement_list_t *stmts)
{
    if (!do_client)
        return;
    if (do_everything && !need_stub_files(stmts))
        return;

    init_client();
    if (!client)
        return;

630
    write_client_routines( stmts );
631 632
    fclose(client);
}