Commit | Line | Data |
---|---|---|
c906108c SS |
1 | # Reply server mig-output massager |
2 | # | |
ecd75fc8 | 3 | # Copyright (C) 1995-2014 Free Software Foundation, Inc. |
c906108c SS |
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 | |
5b1ba0e5 | 9 | # published by the Free Software Foundation; either version 3, or (at |
c906108c SS |
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 | |
5b1ba0e5 | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
c906108c SS |
19 | # |
20 | # This awk script hacks the output of mig-generated reply server code | |
21 | # so that it allows replies with just the error-code in them (as this is | |
22 | # how mig returns errors). | |
23 | # | |
24 | # It is highly, highly, dependent on the exact format of mig output. Ick. | |
25 | # | |
26 | ||
27 | BEGIN { parse_phase = 0; } | |
28 | ||
29 | /^}/ { parse_phase = 0; } | |
30 | ||
31 | parse_phase == 0 && /^mig_internal void _X[a-zA-Z0-9_]*_reply/ { | |
32 | # The start of a mig server routine. Reset everything. Note that we only | |
33 | # mess with rpcs that have the suffix `_reply'. | |
34 | num_args = 0; | |
35 | num_checks = 0; | |
36 | parse_phase = 1; | |
37 | print; next; | |
38 | } | |
39 | ||
40 | parse_phase == 1 && /^[\t ]*typedef struct/ { | |
41 | # The first structure in the server routine should describe the arguments | |
42 | parse_phase = 2; | |
43 | print; next; | |
44 | } | |
45 | ||
46 | parse_phase == 2 { | |
47 | # The message header field in the args structure, which skip. | |
48 | parse_phase = 3; | |
49 | print; next; | |
50 | } | |
51 | ||
52 | parse_phase == 3 && /}/ { | |
53 | # The args structure is over. | |
54 | if (num_args > 1) | |
55 | parse_phase = 5; | |
56 | else | |
57 | # There's no extra args that could screw up the normal mechanism for | |
58 | # error returns, so we don't have to insert any new code. | |
59 | parse_phase = 0; | |
60 | print; next; | |
61 | } | |
62 | ||
63 | parse_phase == 3 { | |
64 | # The type field for an argument. | |
65 | arg_type_code_name[num_args] = $2; | |
66 | sub (/;$/, "", arg_type_code_name[num_args]) # Get rid of the semi-colon | |
67 | parse_phase = 4; | |
68 | print; next; | |
69 | } | |
70 | ||
71 | parse_phase == 4 { | |
72 | # The value field for an argument. | |
73 | arg_name[num_args] = $2; | |
74 | sub (/;$/, "", arg_name[num_args]) # Get rid of the semi-colon | |
75 | arg_type[num_args] = $1; | |
76 | num_args++; | |
77 | parse_phase = 3; | |
78 | print; next; | |
79 | } | |
80 | ||
bae8023e | 81 | parse_phase == 5 && /^[ \t]*(auto |static |)const mach_msg_type_t/ { |
c906108c | 82 | # The type check structure for an argument. |
bae8023e | 83 | arg_check_name[num_checks] = $(NF - 2); |
c906108c SS |
84 | num_checks++; |
85 | print; next; | |
86 | } | |
87 | ||
88 | parse_phase == 5 && /^[ \t]*mig_external kern_return_t/ { | |
89 | # The declaration of the user server function for this rpc. | |
90 | user_function_name = $3; | |
91 | print; next; | |
92 | } | |
93 | ||
94 | parse_phase == 5 && /^#if[ \t]TypeCheck/ { | |
d47642c9 TS |
95 | # Keep going if we have not yet collected the type check structures. |
96 | if (num_checks == 0) | |
97 | { | |
98 | print; next; | |
99 | } | |
100 | ||
c906108c SS |
101 | # The first args type checking statement; we need to insert our chunk of |
102 | # code that bypasses all the type checks if this is an error return, after | |
103 | # which we're done until we get to the next function. Handily, the size | |
104 | # of mig's Reply structure is also the size of the alternate Request | |
105 | # structure that we want to check for. | |
106 | print "\tif (In0P->Head.msgh_size == sizeof (Reply)"; | |
107 | print "\t && ! (In0P->Head.msgh_bits & MACH_MSGH_BITS_COMPLEX)"; | |
20249808 | 108 | print "\t && ! BAD_TYPECHECK(&In0P->" arg_type_code_name[0] ", &" arg_check_name[0] ")"; |
c906108c SS |
109 | print "\t && In0P->" arg_name[0] " != 0)"; |
110 | print "\t /* Error return, only the error code argument is passed. */"; | |
111 | print "\t {"; | |
112 | # Force the function into a type that only takes the first two args, via | |
113 | # the temp variable SFUN (is there another way to correctly do this cast?). | |
114 | # This is possibly bogus, but easier than supplying bogus values for all | |
115 | # the other args (we can't just pass 0 for them, as they might not be scalar). | |
116 | printf ("\t kern_return_t (*sfun)(mach_port_t"); | |
117 | for (i = 0; i < num_args; i++) | |
118 | printf (", %s", arg_type[i]); | |
119 | printf (") = %s;\n", user_function_name); | |
120 | print "\t OutP->RetCode = (*(kern_return_t (*)(mach_port_t, kern_return_t))sfun) (In0P->Head.msgh_request_port, In0P->" arg_name[0] ");"; | |
121 | print "\t return;"; | |
122 | print "\t }"; | |
123 | print ""; | |
124 | parse_phase = 0; | |
125 | print; next; | |
126 | } | |
127 | ||
128 | { print; } |