1# Reply server mig-output massager
2#
3#   Copyright (C) 1995, 1996, 1999, 2007 Free Software Foundation, Inc.
4#
5#   Written by Miles Bader <miles@gnu.ai.mit.edu>
6#
7#   This program is free software; you can redistribute it and/or
8#   modify it under the terms of the GNU General Public License as
9#   published by the Free Software Foundation; either version 2, or (at
10#   your option) any later version.
11#
12#   This program is distributed in the hope that it will be useful, but
13#   WITHOUT ANY WARRANTY; without even the implied warranty of
14#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15#   General Public License for more details.
16#
17#   You should have received a copy of the GNU General Public License
18#   along with this program; if not, write to the Free Software
19#   Foundation, Inc., 51 Franklin Street, Fifth Floor,
20#   Boston, MA 02110-1301, USA.
21#
22# This awk script hacks the output of mig-generated reply server code
23# so that it allows replies with just the error-code in them (as this is
24# how mig returns errors).
25#
26# It is highly, highly, dependent on the exact format of mig output.  Ick.
27#
28
29BEGIN { parse_phase = 0; }
30
31/^}/ { parse_phase = 0; }
32
33parse_phase == 0 && /^mig_internal void _X[a-zA-Z0-9_]*_reply/ {
34  # The start of a mig server routine.  Reset everything.  Note that we only
35  # mess with rpcs that have the suffix `_reply'.
36  num_args = 0;
37  num_checks = 0;
38  parse_phase = 1;
39  print; next;
40}
41
42parse_phase == 1 && /^[\t ]*typedef struct/ {
43  # The first structure in the server routine should describe the arguments
44  parse_phase = 2;
45  print; next;
46}
47
48parse_phase == 2 {
49  # The message header field in the args structure, which skip.
50  parse_phase = 3;
51  print; next;
52}
53
54parse_phase == 3 && /}/ {
55  # The args structure is over.
56  if (num_args > 1)
57    parse_phase = 5;
58  else
59    # There's no extra args that could screw up the normal mechanism for
60    # error returns, so we don't have to insert any new code.
61    parse_phase = 0;
62  print; next;
63}
64
65parse_phase == 3 {
66  # The type field for an argument.
67  arg_type_code_name[num_args] = $2;
68  sub (/;$/, "", arg_type_code_name[num_args]) # Get rid of the semi-colon
69  parse_phase = 4;
70  print; next;
71}
72
73parse_phase == 4 {
74  # The value field for an argument.
75  arg_name[num_args] = $2;
76  sub (/;$/, "", arg_name[num_args]) # Get rid of the semi-colon
77  arg_type[num_args] = $1;
78  num_args++;
79  parse_phase = 3;
80  print; next;
81}
82
83parse_phase == 5 && /^[ \t]*(auto|static) const mach_msg_type_t/ {
84  # The type check structure for an argument.
85  arg_check_name[num_checks] = $4;
86  num_checks++;
87  print; next;
88}
89
90parse_phase == 5 && /^[ \t]*mig_external kern_return_t/ {
91  # The declaration of the user server function for this rpc.
92  user_function_name = $3;
93  print; next;
94}
95
96parse_phase == 5 && /^#if[ \t]TypeCheck/ {
97  # The first args type checking statement; we need to insert our chunk of
98  # code that bypasses all the type checks if this is an error return, after
99  # which we're done until we get to the next function.  Handily, the size
100  # of mig's Reply structure is also the size of the alternate Request 
101  # structure that we want to check for.
102  print "\tif (In0P->Head.msgh_size == sizeof (Reply)";
103  print "\t    && ! (In0P->Head.msgh_bits & MACH_MSGH_BITS_COMPLEX)";
104  print "\t    && *(int *)&In0P->" arg_type_code_name[0] " == *(int *)&" arg_check_name[0];
105  print "\t    && In0P->" arg_name[0] " != 0)";
106  print "\t  /* Error return, only the error code argument is passed.  */";
107  print "\t  {";
108  # Force the function into a type that only takes the first two args, via
109  # the temp variable SFUN (is there another way to correctly do this cast?).
110  # This is possibly bogus, but easier than supplying bogus values for all
111  # the other args (we can't just pass 0 for them, as they might not be scalar).
112  printf ("\t    kern_return_t (*sfun)(mach_port_t");
113  for (i = 0; i < num_args; i++)
114    printf (", %s", arg_type[i]);
115  printf (") = %s;\n", user_function_name);
116  print "\t    OutP->RetCode = (*(kern_return_t (*)(mach_port_t, kern_return_t))sfun) (In0P->Head.msgh_request_port, In0P->" arg_name[0] ");";
117  print "\t    return;";
118  print "\t  }";
119  print "";
120  parse_phase = 0;
121  print; next;
122}
123
124{ print; }
125