aboutsummaryrefslogtreecommitdiffstats
path: root/bootstrap_cmds/migcom.tproj/header.c
blob: 505cdd3e7896dcd76de5d907e65181578ea2491e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/*
 * Copyright (c) 1999-2018 Apple Inc. All rights reserved.
 *
 * @APPLE_LICENSE_HEADER_START@
 *
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this
 * file.
 *
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 *
 * @APPLE_LICENSE_HEADER_END@
 */
/*
 * Mach Operating System
 * Copyright (c) 1991,1990 Carnegie Mellon University
 * All Rights Reserved.
 *
 * Permission to use, copy, modify and distribute this software and its
 * documentation is hereby granted, provided that both the copyright
 * notice and this permission notice appear in all copies of the
 * software, derivative works or modified versions, and any portions
 * thereof, and that both notices appear in supporting documentation.
 *
 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
 *
 * Carnegie Mellon requests users of this software to return to
 *
 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
 *  School of Computer Science
 *  Carnegie Mellon University
 *  Pittsburgh PA 15213-3890
 *
 * any improvements or extensions that they make and grant Carnegie the
 * rights to redistribute these changes.
 */

#include "write.h"
#include "utils.h"
#include "global.h"
#include "strdefs.h"
#include "error.h"
#include <stdlib.h>

void
WriteIncludes(FILE *file, boolean_t isuser, boolean_t isdef)
{
  if (isdef) {
    fprintf(file, "#include <mach/port.h>\n");
    fprintf(file, "#include <mach/machine/kern_return.h>\n");
    if (!isuser)
      fprintf(file, "#include <mach/mig_errors.h>\n");
  }
  else {
    fprintf(file, "#include <string.h>\n");
    fprintf(file, "#include <mach/ndr.h>\n");
    fprintf(file, "#include <mach/boolean.h>\n");
    fprintf(file, "#include <mach/kern_return.h>\n");
    fprintf(file, "#include <mach/notify.h>\n");
    fprintf(file, "#include <mach/mach_types.h>\n");
    fprintf(file, "#include <mach/message.h>\n");
    fprintf(file, "#include <mach/mig_errors.h>\n");
    fprintf(file, "#include <mach/port.h>\n");

    if (IsVoucherCodeAllowed && !IsKernelUser && !IsKernelServer) {
      fprintf(file, "\t\n/* BEGIN VOUCHER CODE */\n\n");
      fprintf(file, "#ifndef KERNEL\n");
      fprintf(file, "#if defined(__has_include)\n");
      fprintf(file, "#if __has_include(<mach/mig_voucher_support.h>)\n");
      fprintf(file, "#ifndef USING_VOUCHERS\n");
      fprintf(file, "#define USING_VOUCHERS\n");
      fprintf(file, "#endif\n");


      fprintf(file, "#ifndef __VOUCHER_FORWARD_TYPE_DECLS__\n");
      fprintf(file, "#define __VOUCHER_FORWARD_TYPE_DECLS__\n");

      fprintf(file, "#ifdef __cplusplus\n");
      fprintf(file, "extern \"C\" {\n");
      fprintf(file, "#endif\n");

      fprintf(file, "\textern boolean_t voucher_mach_msg_set(mach_msg_header_t *msg) __attribute__((weak_import));\n");

      fprintf(file, "#ifdef __cplusplus\n");
      fprintf(file, "}\n");
      fprintf(file, "#endif\n");

      fprintf(file, "#endif // __VOUCHER_FORWARD_TYPE_DECLS__\n");
      fprintf(file, "#endif // __has_include(<mach/mach_voucher_types.h>)\n");
      fprintf(file, "#endif // __has_include\n");
      fprintf(file, "#endif // !KERNEL\n");

      fprintf(file, "\t\n/* END VOUCHER CODE */\n\n");
    }

    fprintf(file, "\t\n/* BEGIN MIG_STRNCPY_ZEROFILL CODE */\n\n");
    fprintf(file, "#if defined(__has_include)\n");
    fprintf(file, "#if __has_include(<mach/mig_strncpy_zerofill_support.h>)\n");
    fprintf(file, "#ifndef USING_MIG_STRNCPY_ZEROFILL\n");
    fprintf(file, "#define USING_MIG_STRNCPY_ZEROFILL\n");
    fprintf(file, "#endif\n");

    fprintf(file, "#ifndef __MIG_STRNCPY_ZEROFILL_FORWARD_TYPE_DECLS__\n");
    fprintf(file, "#define __MIG_STRNCPY_ZEROFILL_FORWARD_TYPE_DECLS__\n");

    fprintf(file, "#ifdef __cplusplus\n");
    fprintf(file, "extern \"C\" {\n");
    fprintf(file, "#endif\n");

    fprintf(file, "\textern int mig_strncpy_zerofill(char *dest, const char *src, int len) __attribute__((weak_import));\n");

    fprintf(file, "#ifdef __cplusplus\n");
    fprintf(file, "}\n");
    fprintf(file, "#endif\n");

    fprintf(file, "#endif /* __MIG_STRNCPY_ZEROFILL_FORWARD_TYPE_DECLS__ */\n");
    fprintf(file, "#endif /* __has_include(<mach/mig_strncpy_zerofill_support.h>) */\n");
    fprintf(file, "#endif /* __has_include */\n");
    fprintf(file, "\t\n/* END MIG_STRNCPY_ZEROFILL CODE */\n\n");

    if (ShortCircuit)
      fprintf(file, "#include <mach/rpc.h>\n");
    if (isuser && IsKernelUser) {
      fprintf(file, "#if\t(__MigKernelSpecificCode) || (_MIG_KERNEL_SPECIFIC_CODE_)\n");
      fprintf(file, "#include <kern/ipc_mig.h>\n");
      fprintf(file, "#endif /* __MigKernelSpecificCode */\n");
    }
  }
  fprintf(file, "\n");
}

static void
WriteETAPDefines(FILE *file)
{
  statement_t *stat;
  int fnum;
  char *fname;
  int first = TRUE;

  fprintf(file, "\n#ifndef subsystem_to_name_map_%s\n", SubsystemName);
  fprintf(file, "#define subsystem_to_name_map_%s \\\n", SubsystemName);
  for (stat = stats; stat != stNULL; stat = stat->stNext)
    if (stat->stKind == skRoutine) {
      fnum = SubsystemBase + stat->stRoutine->rtNumber;
      fname = stat->stRoutine->rtName;
      if (! first)
        fprintf(file, ",\\\n");
      fprintf(file, "    { \"%s\", %d }", fname, fnum);
      first = FALSE;
    }
  fprintf(file, "\n#endif\n");
}

static void
WriteProlog(FILE *file, char *protect, boolean_t more, boolean_t isuser)
{
  if (protect != strNULL) {
    fprintf(file, "#ifndef\t_%s\n", protect);
    fprintf(file, "#define\t_%s\n", protect);
    fprintf(file, "\n");
  }

  fprintf(file, "/* Module %s */\n", SubsystemName);
  fprintf(file, "\n");

  if (more) {
    WriteIncludes(file, isuser, UseSplitHeaders);
  }
  fprintf(file, "#ifdef AUTOTEST\n");
  fprintf(file, "#ifndef FUNCTION_PTR_T\n");
  fprintf(file, "#define FUNCTION_PTR_T\n");
  fprintf(file, "typedef void (*function_ptr_t)");
  fprintf(file, "(mach_port_t, char *, mach_msg_type_number_t);\n");
  fprintf(file, "typedef struct {\n");
  fprintf(file, "        char            *name;\n");
  fprintf(file, "        function_ptr_t  function;\n");
  fprintf(file, "} function_table_entry;\n");
  fprintf(file, "typedef function_table_entry   *function_table_t;\n");
  fprintf(file, "#endif /* FUNCTION_PTR_T */\n");
  fprintf(file, "#endif /* AUTOTEST */\n");
  fprintf(file, "\n#ifndef\t%s_MSG_COUNT\n", SubsystemName);
  fprintf(file, "#define\t%s_MSG_COUNT\t%d\n", SubsystemName, rtNumber);
  fprintf(file, "#endif\t/* %s_MSG_COUNT */\n\n", SubsystemName);
}

static void
WriteEpilog(FILE *file, char *protect, boolean_t isuser)
{
  char *defname = isuser ? "__AfterMigUserHeader" : "__AfterMigServerHeader";

  WriteETAPDefines(file);
  fprintf(file, "\n#ifdef %s\n%s\n#endif /* %s */\n", defname, defname, defname);
  if (protect != strNULL) {
    fprintf(file, "\n");
    fprintf(file, "#endif\t /* _%s */\n", protect);
  }
}

static void
WriteUserRoutine(FILE *file, routine_t *rt)
{
  fprintf(file, "\n");
  fprintf(file, "/* %s %s */\n", rtRoutineKindToStr(rt->rtKind), rt->rtName);
  WriteMigExternal(file);
  fprintf(file, "%s %s\n", ReturnTypeStr(rt), rt->rtUserName);
  if (BeLint) {
    fprintf(file, "#if\t%s\n", LintLib);
    fprintf(file, "    (");
    WriteList(file, rt->rtArgs, WriteNameDecl, akbUserArg, ", " , "");
    fprintf(file, ")\n");
    WriteList(file, rt->rtArgs, WriteUserVarDecl, akbUserArg, ";\n", ";\n");
    fprintf(file, "{ ");
    fprintf(file, "return ");
    fprintf(file, "%s(", rt->rtUserName);
    WriteList(file, rt->rtArgs, WriteNameDecl, akbUserArg, ", ", "");
    fprintf(file, "); }\n");
    fprintf(file, "#else\n");
  }
  if (BeAnsiC) {
    fprintf(file, "(\n");
    WriteList(file, rt->rtArgs, WriteUserVarDecl, akbUserArg, ",\n", "\n");
    fprintf(file, ");\n");
  }
  else {
    fprintf(file, "#if\t%s\n", NewCDecl);
    fprintf(file, "(\n");
    WriteList(file, rt->rtArgs, WriteUserVarDecl, akbUserArg, ",\n", "\n");
    fprintf(file, ");\n");
    fprintf(file, "#else\n");

    fprintf(file, "    ();\n");
    fprintf(file, "#endif\t/* %s */\n", NewCDecl);
  }
  if (BeLint) {
    fprintf(file, "#endif\t/* %s */\n",  LintLib);
  }
}

void
WriteUserRequestUnion(FILE *file, statement_t *stats)
{
  statement_t *stat;

  fprintf(file, "/* union of all requests */\n\n");
  fprintf(file, "#ifndef __RequestUnion__%s%s_subsystem__defined\n", UserPrefix, SubsystemName);
  fprintf(file, "#define __RequestUnion__%s%s_subsystem__defined\n", UserPrefix, SubsystemName);
  fprintf(file, "union __RequestUnion__%s%s_subsystem {\n", UserPrefix, SubsystemName);
  for (stat = stats; stat != stNULL; stat = stat->stNext) {
    if (stat->stKind == skRoutine) {
      routine_t *rt;

      rt = stat->stRoutine;
      fprintf(file, "\t__Request__%s_t Request_%s;\n", rt->rtName, rt->rtUserName);
    }
  }
  fprintf(file, "};\n");
  fprintf(file, "#endif /* !__RequestUnion__%s%s_subsystem__defined */\n", UserPrefix, SubsystemName);
}

void
WriteUserReplyUnion(FILE *file, statement_t *stats)
{
  statement_t *stat;

  fprintf(file, "/* union of all replies */\n\n");
  fprintf(file, "#ifndef __ReplyUnion__%s%s_subsystem__defined\n", UserPrefix, SubsystemName);
  fprintf(file, "#define __ReplyUnion__%s%s_subsystem__defined\n", UserPrefix, SubsystemName);
  fprintf(file, "union __ReplyUnion__%s%s_subsystem {\n", UserPrefix, SubsystemName);
  for (stat = stats; stat != stNULL; stat = stat->stNext) {
    if (stat->stKind == skRoutine) {
      routine_t *rt;

      rt = stat->stRoutine;
      fprintf(file, "\t__Reply__%s_t Reply_%s;\n", rt->rtName, rt->rtUserName);
    }
  }
  fprintf(file, "};\n");
  fprintf(file, "#endif /* !__RequestUnion__%s%s_subsystem__defined */\n", UserPrefix, SubsystemName);
}

void
WriteUserHeader(FILE *file, statement_t *stats)
{
  statement_t *stat;
  char *protect = strconcat(SubsystemName, "_user_");

  WriteProlog(file, protect, TRUE, TRUE);
  for (stat = stats; stat != stNULL; stat = stat->stNext)
    switch (stat->stKind) {

      case skImport:
      case skUImport:
      case skDImport:
        WriteImport(file, stat->stFileName);
        break;

      case skRoutine:
      case skSImport:
      case skIImport:
        break;

      default:
        fatal("WriteHeader(): bad statement_kind_t (%d)", (int) stat->stKind);
  }
  fprintf(file, "\n");
  fprintf(file, "#ifdef __BeforeMigUserHeader\n");
  fprintf(file, "__BeforeMigUserHeader\n");
  fprintf(file, "#endif /* __BeforeMigUserHeader */\n");
  fprintf(file, "\n");
  fprintf(file, "#include <sys/cdefs.h>\n");
  fprintf(file, "__BEGIN_DECLS\n");
  fprintf(file, "\n");
  for (stat = stats; stat != stNULL; stat = stat->stNext) {
    if (stat->stKind == skRoutine)
      WriteUserRoutine(file, stat->stRoutine);
  }
  fprintf(file, "\n");
  fprintf(file, "__END_DECLS\n");

  fprintf(file, "\n");
  fprintf(file, "/********************** Caution **************************/\n");
  fprintf(file, "/* The following data types should be used to calculate  */\n");
  fprintf(file, "/* maximum message sizes only. The actual message may be */\n");
  fprintf(file, "/* smaller, and the position of the arguments within the */\n");
  fprintf(file, "/* message layout may vary from what is presented here.  */\n");
  fprintf(file, "/* For example, if any of the arguments are variable-    */\n");
  fprintf(file, "/* sized, and less than the maximum is sent, the data    */\n");
  fprintf(file, "/* will be packed tight in the actual message to reduce  */\n");
  fprintf(file, "/* the presence of holes.                                */\n");
  fprintf(file, "/********************** Caution **************************/\n");
  fprintf(file, "\n");

  WriteRequestTypes(file, stats);
  WriteUserRequestUnion(file, stats);

  WriteReplyTypes(file, stats);
  WriteUserReplyUnion(file, stats);

  WriteEpilog(file, protect, TRUE);
}

static void
WriteDefinesRoutine(FILE *file, routine_t *rt)
{
  char *up = (char *)malloc(strlen(rt->rtName)+1);

  up = toupperstr(strcpy(up, rt->rtName));
  fprintf(file, "#define\tMACH_ID_%s\t\t%d\t/* %s() */\n", up, rt->rtNumber + SubsystemBase, rt->rtName);
  if (rt->rtKind == rkRoutine)
    fprintf(file, "#define\tMACH_ID_%s_REPLY\t\t%d\t/* %s() */\n", up, rt->rtNumber + SubsystemBase + 100, rt->rtName);
  fprintf(file, "\n");
}

void
WriteServerRoutine(FILE *file, routine_t *rt)
{
  fprintf(file, "\n");
  fprintf(file, "/* %s %s */\n", rtRoutineKindToStr(rt->rtKind), rt->rtName);
  WriteMigExternal(file);

  // MIG_SERVER_ROUTINE can be defined by system headers to resolve to an attribute that
  // tells the compiler that this is a MIG server routine. Useful for static analysis.
  fprintf(file, "MIG_SERVER_ROUTINE\n%s %s\n", ReturnTypeStr(rt), rt->rtServerName);

  if (BeLint) {
    fprintf(file, "#if\t%s\n", LintLib);
    fprintf(file, "    (");
    WriteList(file, rt->rtArgs, WriteNameDecl, akbServerArg, ", " , "");
    fprintf(file, ")\n");
    WriteList(file, rt->rtArgs, WriteServerVarDecl, akbServerArg, ";\n", ";\n");
    fprintf(file, "{ ");
    fprintf(file, "return ");
    fprintf(file, "%s(", rt->rtServerName);
    WriteList(file, rt->rtArgs, WriteNameDecl, akbServerArg, ", ", "");
    fprintf(file, "); }\n");
    fprintf(file, "#else /* %s */\n",LintLib);
  }
  if (BeAnsiC) {
    fprintf(file, "(\n");
    WriteList(file, rt->rtArgs, WriteServerVarDecl, akbServerArg, ",\n", "\n");
    fprintf(file, ");\n");
  }
  else {
    fprintf(file, "#if\t%s\n", NewCDecl);
    fprintf(file, "(\n");
    WriteList(file, rt->rtArgs, WriteServerVarDecl, akbServerArg, ",\n", "\n");
    fprintf(file, ");\n");
    fprintf(file, "#else\n");

    fprintf(file, "    ();\n");
    fprintf(file, "#endif\t/* %s */\n", NewCDecl);
  }
  if (BeLint) {
    fprintf(file, "#endif\t/* %s */\n",  LintLib);
  }
}

static void
WriteDispatcher(FILE *file)
{
  statement_t *stat;
  int descr_count = 0;

  for (stat = stats; stat != stNULL; stat = stat->stNext)
    if (stat->stKind == skRoutine) {
      routine_t *rt = stat->stRoutine;
      descr_count += rtCountArgDescriptors(rt->rtArgs, (int *) 0);
    }
  fprintf(file, "\n");

  WriteMigExternal(file);
  fprintf(file, "boolean_t %s(\n", ServerDemux);
  fprintf(file, "\t\tmach_msg_header_t *InHeadP,\n");
  fprintf(file, "\t\tmach_msg_header_t *OutHeadP);\n\n");

  WriteMigExternal(file);
  fprintf(file, "mig_routine_t %s_routine(\n", ServerDemux);
  fprintf(file, "\t\tmach_msg_header_t *InHeadP);\n\n");

  fprintf(file, "\n/* Description of this subsystem, for use in direct RPC */\n");
  fprintf(file, "extern const struct %s {\n", ServerSubsys);
  if (UseRPCTrap) {
    fprintf(file, "\tstruct subsystem *\tsubsystem;\t/* Reserved for system use */\n");
  }
  else {
    fprintf(file, "\tmig_server_routine_t\tserver;\t/* Server routine */\n");
  }
  fprintf(file, "\tmach_msg_id_t\tstart;\t/* Min routine number */\n");
  fprintf(file, "\tmach_msg_id_t\tend;\t/* Max routine number + 1 */\n");
  fprintf(file, "\tunsigned int\tmaxsize;\t/* Max msg size */\n");
  if (UseRPCTrap) {
    fprintf(file, "\tvm_address_t\tbase_addr;\t/* Base address */\n");
    fprintf(file, "\tstruct rpc_routine_descriptor\t/*Array of routine descriptors */\n");
  }
  else {
    fprintf(file, "\tvm_address_t\treserved;\t/* Reserved */\n");
    fprintf(file, "\tstruct routine_descriptor\t/*Array of routine descriptors */\n");
  }
  fprintf(file, "\t\troutine[%d];\n", rtNumber);
  if (UseRPCTrap) {
    fprintf(file, "\tstruct rpc_routine_arg_descriptor\t/*Array of arg descriptors */\n");
    fprintf(file, "\t\targ_descriptor[%d];\n", descr_count);
  }
  fprintf(file, "} %s;\n", ServerSubsys);
  fprintf(file, "\n");
}

void
WriteBogusServerRoutineAnnotationDefine(FILE *file) {
  // MIG_SERVER_ROUTINE can be defined by system headers to resolve to
  // an attribute that tells the compiler that this is a MIG server routine.
  // Useful for static analysis.
  fprintf(file, "#ifndef MIG_SERVER_ROUTINE\n");
  fprintf(file, "#define MIG_SERVER_ROUTINE\n");
  fprintf(file, "#endif\n");
  fprintf(file, "\n");
}

void
WriteServerHeader(FILE *file, statement_t *stats)
{
  statement_t *stat;
  char *protect = strconcat(SubsystemName, "_server_");

  WriteProlog(file, protect, TRUE, FALSE);
  for (stat = stats; stat != stNULL; stat = stat->stNext)
    switch (stat->stKind) {

      case skImport:
      case skSImport:
      case skDImport:
        WriteImport(file, stat->stFileName);
        break;

      case skRoutine:
      case skUImport:
      case skIImport:
        break;

      default:
        fatal("WriteServerHeader(): bad statement_kind_t (%d)", (int) stat->stKind);
  }
  fprintf(file, "\n#ifdef __BeforeMigServerHeader\n");
  fprintf(file, "__BeforeMigServerHeader\n");
  fprintf(file, "#endif /* __BeforeMigServerHeader */\n\n");

  WriteBogusServerRoutineAnnotationDefine(file);

  for (stat = stats; stat != stNULL; stat = stat->stNext) {
    if (stat->stKind == skRoutine)
      WriteServerRoutine(file, stat->stRoutine);
  }
  WriteDispatcher(file);

  WriteRequestTypes(file, stats);
  WriteServerRequestUnion(file, stats);

  WriteReplyTypes(file, stats);
  WriteServerReplyUnion(file, stats);

  WriteEpilog(file, protect, FALSE);
}

static void
WriteInternalRedefine(FILE *file, routine_t *rt)
{
  fprintf(file, "#define %s %s_external\n", rt->rtUserName, rt->rtUserName);
}

void
WriteInternalHeader(FILE *file, statement_t *stats)
{
  statement_t *stat;

  for (stat = stats; stat != stNULL; stat = stat->stNext)
    switch (stat->stKind) {

      case skRoutine:
        WriteInternalRedefine(file, stat->stRoutine);
        break;

      case skImport:
      case skUImport:
      case skSImport:
      case skDImport:
      case skIImport:
        break;

      default:
        fatal("WriteInternalHeader(): bad statement_kind_t (%d)", (int) stat->stKind);
  }
}

void
WriteDefinesHeader(FILE *file, statement_t *stats)
{
  statement_t *stat;
  char *protect = strconcat(SubsystemName, "_defines");

  WriteProlog(file, protect, FALSE, FALSE);
  fprintf(file, "\n/*\tDefines related to the Subsystem %s\t*/\n\n", SubsystemName);
  for (stat = stats; stat != stNULL; stat = stat->stNext)
    switch (stat->stKind) {

      case skRoutine:
        WriteDefinesRoutine(file, stat->stRoutine);
        break;

      case skImport:
      case skSImport:
      case skUImport:
        break;

      default:
        fatal("WriteDefinesHeader(): bad statement_kind_t (%d)", (int) stat->stKind);
  }
  WriteEpilog(file, protect, FALSE);
}