|
1 /********************************************************************* |
|
2 * RPC for the Windows NT Operating System |
|
3 * 1993 by Martin F. Gergeleit |
|
4 * Users may use, copy or modify Sun RPC for the Windows NT Operating |
|
5 * System according to the Sun copyright below. |
|
6 * |
|
7 * RPC for the Windows NT Operating System COMES WITH ABSOLUTELY NO |
|
8 * WARRANTY, NOR WILL I BE LIABLE FOR ANY DAMAGES INCURRED FROM THE |
|
9 * USE OF. USE ENTIRELY AT YOUR OWN RISK!!! |
|
10 *********************************************************************/ |
|
11 |
|
12 /* @(#)rpc_cout.c 2.1 88/08/01 4.0 RPCSRC */ |
|
13 /* |
|
14 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for |
|
15 * unrestricted use provided that this legend is included on all tape |
|
16 * media and as a part of the software program in whole or part. Users |
|
17 * may copy or modify Sun RPC without charge, but are not authorized |
|
18 * to license or distribute it to anyone else except as part of a product or |
|
19 * program developed by the user. |
|
20 * |
|
21 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE |
|
22 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR |
|
23 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. |
|
24 * |
|
25 * Sun RPC is provided with no support and without any obligation on the |
|
26 * part of Sun Microsystems, Inc. to assist in its use, correction, |
|
27 * modification or enhancement. |
|
28 * |
|
29 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE |
|
30 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC |
|
31 * OR ANY PART THEREOF. |
|
32 * |
|
33 * In no event will Sun Microsystems, Inc. be liable for any lost revenue |
|
34 * or profits or other special, indirect and consequential damages, even if |
|
35 * Sun has been advised of the possibility of such damages. |
|
36 * |
|
37 * Sun Microsystems, Inc. |
|
38 * 2550 Garcia Avenue |
|
39 * Mountain View, California 94043 |
|
40 */ |
|
41 //#ifndef lint |
|
42 //static char sccsid[] = "@(#)rpc_cout.c 1.8 87/06/24 (C) 1987 SMI"; |
|
43 //#endif |
|
44 |
|
45 /* |
|
46 * rpc_cout.c, XDR routine outputter for the RPC protocol compiler |
|
47 * Copyright (C) 1987, Sun Microsystems, Inc. |
|
48 */ |
|
49 #include <stdio.h> |
|
50 #include <stdlib.h> |
|
51 #ifdef WIN32 |
|
52 #include <string.h> |
|
53 #include "rpc_util.h" |
|
54 #include "rpc_pars.h" |
|
55 #else |
|
56 #include <string.h> |
|
57 #include "rpc_util.h" |
|
58 #include "rpc_pars.h" |
|
59 #endif |
|
60 #include "ae_component_rpc.h" |
|
61 |
|
62 /* prototypes */ |
|
63 static void print_ifstat( int indent, char *prefix, char *type, relation rel, char *amax, char *objname, char *name ); |
|
64 |
|
65 |
|
66 /* implementation */ |
|
67 static void |
|
68 print_stat(dec) |
|
69 declaration *dec; |
|
70 { |
|
71 char *prefix = dec->prefix; |
|
72 char *type = dec->type; |
|
73 char *amax = dec->array_max; |
|
74 relation rel = dec->rel; |
|
75 char name[256]; |
|
76 |
|
77 if (isvectordef(type, rel)) { |
|
78 s_print(name, "objp->%s", dec->name); |
|
79 } else { |
|
80 s_print(name, "&objp->%s", dec->name); |
|
81 } |
|
82 print_ifstat(1, prefix, type, rel, amax, name, dec->name); |
|
83 } |
|
84 |
|
85 |
|
86 static void |
|
87 space() |
|
88 { |
|
89 f_print(fout, "\n\n"); |
|
90 } |
|
91 |
|
92 static int |
|
93 findtype(def, type) |
|
94 definition *def; |
|
95 char *type; |
|
96 { |
|
97 if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) { |
|
98 return (0); |
|
99 } else { |
|
100 return (streq(def->def_name, type)); |
|
101 } |
|
102 } |
|
103 |
|
104 static int |
|
105 undefined(type) |
|
106 char *type; |
|
107 { |
|
108 definition *def; |
|
109 |
|
110 def = (definition *) FINDVAL(defined, type, findtype); |
|
111 return (def == NULL); |
|
112 } |
|
113 |
|
114 |
|
115 static void |
|
116 print_header(def) |
|
117 definition *def; |
|
118 { |
|
119 space(); |
|
120 f_print(fout, "bool_t\n"); |
|
121 f_print(fout, "xdr_%s(xdrs, objp)\n", ae_extend_custom_types(def->def_name)); |
|
122 f_print(fout, "\tXDR *xdrs;\n"); |
|
123 f_print(fout, "\t%s ", def->def_name); |
|
124 if (def->def_kind != DEF_TYPEDEF || |
|
125 !isvectordef(def->def.ty.old_type, def->def.ty.rel)) { |
|
126 f_print(fout, "*"); |
|
127 } |
|
128 f_print(fout, "objp;\n"); |
|
129 f_print(fout, "{\n"); |
|
130 } |
|
131 |
|
132 static void |
|
133 print_trailer() |
|
134 { |
|
135 f_print(fout, "\treturn (TRUE);\n"); |
|
136 f_print(fout, "}\n"); |
|
137 space(); |
|
138 } |
|
139 |
|
140 |
|
141 static void |
|
142 print_ifopen(indent, name) |
|
143 int indent; |
|
144 char *name; |
|
145 { |
|
146 tabify(fout, indent); |
|
147 f_print(fout, "if (!xdr_%s(xdrs", ae_extend_custom_types(name)); |
|
148 } |
|
149 |
|
150 |
|
151 static void |
|
152 print_ifarg(arg) |
|
153 char *arg; |
|
154 { |
|
155 f_print(fout, ", %s", arg); |
|
156 } |
|
157 |
|
158 |
|
159 static void |
|
160 print_ifsizeof(prefix, type) |
|
161 char *prefix; |
|
162 char *type; |
|
163 { |
|
164 if (streq(type, "bool")) { |
|
165 f_print(fout, ", sizeof(bool_t), xdr_bool"); |
|
166 } else { |
|
167 f_print(fout, ", sizeof("); |
|
168 if (undefined(type) && prefix) { |
|
169 f_print(fout, "%s ", prefix); |
|
170 } |
|
171 f_print(fout, "%s), (void*)xdr_%s", type, ae_extend_custom_types(type)); |
|
172 } |
|
173 } |
|
174 |
|
175 static void |
|
176 print_ifclose(indent) |
|
177 int indent; |
|
178 { |
|
179 f_print(fout, ")) {\n"); |
|
180 tabify(fout, indent); |
|
181 f_print(fout, "\treturn (FALSE);\n"); |
|
182 tabify(fout, indent); |
|
183 f_print(fout, "}\n"); |
|
184 } |
|
185 |
|
186 static void |
|
187 print_ifstat(indent, prefix, type, rel, amax, objname, name) |
|
188 int indent; |
|
189 char *prefix; |
|
190 char *type; |
|
191 relation rel; |
|
192 char *amax; |
|
193 char *objname; |
|
194 char *name; |
|
195 { |
|
196 char *alt = NULL; |
|
197 |
|
198 switch (rel) { |
|
199 case REL_POINTER: |
|
200 { |
|
201 int ischar; |
|
202 ischar = strcmp( type, "char" ); |
|
203 if( (ischar == 0) && (command_line.wrapstring_flag) ) { |
|
204 print_ifopen( indent, "wrapstring" ); |
|
205 f_print(fout, ", %s", objname); |
|
206 } else { |
|
207 print_ifopen(indent, "pointer"); |
|
208 print_ifarg("(char **)"); |
|
209 f_print(fout, "%s", objname); |
|
210 print_ifsizeof(prefix, type); |
|
211 } |
|
212 break; |
|
213 } |
|
214 case REL_VECTOR: |
|
215 if (streq(type, "string")) { |
|
216 alt = "string"; |
|
217 } else if (streq(type, "opaque")) { |
|
218 alt = "opaque"; |
|
219 } |
|
220 if (alt) { |
|
221 print_ifopen(indent, alt); |
|
222 print_ifarg(objname); |
|
223 } else { |
|
224 print_ifopen(indent, "vector"); |
|
225 print_ifarg("(char *)"); |
|
226 f_print(fout, "%s", objname); |
|
227 } |
|
228 print_ifarg(amax); |
|
229 if (!alt) { |
|
230 print_ifsizeof(prefix, type); |
|
231 } |
|
232 break; |
|
233 case REL_ARRAY: |
|
234 if (streq(type, "string")) { |
|
235 alt = "string"; |
|
236 } else if (streq(type, "opaque")) { |
|
237 alt = "bytes"; |
|
238 } |
|
239 if (streq(type, "string")) { |
|
240 print_ifopen(indent, alt); |
|
241 print_ifarg(objname); |
|
242 } else { |
|
243 if (alt) { |
|
244 print_ifopen(indent, alt); |
|
245 } else { |
|
246 print_ifopen(indent, "array"); |
|
247 } |
|
248 print_ifarg("(char **)"); |
|
249 if (*objname == '&') { |
|
250 f_print(fout, "%s.%s_val, (u_int *)%s.%s_len", |
|
251 objname, name, objname, name); |
|
252 } else { |
|
253 f_print(fout, "&%s->%s_val, (u_int *)&%s->%s_len", |
|
254 objname, name, objname, name); |
|
255 } |
|
256 } |
|
257 print_ifarg(amax); |
|
258 if (!alt) { |
|
259 print_ifsizeof(prefix, type); |
|
260 } |
|
261 break; |
|
262 case REL_ALIAS: |
|
263 print_ifopen(indent, type); |
|
264 print_ifarg(objname); |
|
265 break; |
|
266 } |
|
267 print_ifclose(indent); |
|
268 } |
|
269 |
|
270 |
|
271 /* ARGSUSED */ |
|
272 static void |
|
273 emit_enum(def) |
|
274 definition *def; |
|
275 { |
|
276 print_ifopen(1, "enum"); |
|
277 print_ifarg("(enum_t *)objp"); |
|
278 print_ifclose(1); |
|
279 } |
|
280 |
|
281 |
|
282 static void |
|
283 emit_union(def) |
|
284 definition *def; |
|
285 { |
|
286 declaration *dflt; |
|
287 case_list *cl; |
|
288 declaration *cs; |
|
289 char *object; |
|
290 char *format = "&objp->%s_u.%s"; |
|
291 |
|
292 print_stat(&def->def.un.enum_decl); |
|
293 f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name); |
|
294 for (cl = def->def.un.cases; cl != NULL; cl = cl->next) { |
|
295 cs = &cl->case_decl; |
|
296 f_print(fout, "\tcase %s:\n", cl->case_name); |
|
297 if (!streq(cs->type, "void")) { |
|
298 object = alloc(strlen(def->def_name) + strlen(format) + |
|
299 strlen(cs->name) + 1); |
|
300 s_print(object, format, def->def_name, cs->name); |
|
301 print_ifstat(2, cs->prefix, cs->type, cs->rel, cs->array_max, |
|
302 object, cs->name); |
|
303 free(object); |
|
304 } |
|
305 f_print(fout, "\t\tbreak;\n"); |
|
306 } |
|
307 dflt = def->def.un.default_decl; |
|
308 if (dflt != NULL) { |
|
309 if (!streq(dflt->type, "void")) { |
|
310 f_print(fout, "\tdefault:\n"); |
|
311 object = alloc(strlen(def->def_name) + strlen(format) + |
|
312 strlen(dflt->name) + 1); |
|
313 s_print(object, format, def->def_name, dflt->name); |
|
314 print_ifstat(2, dflt->prefix, dflt->type, dflt->rel, |
|
315 dflt->array_max, object, dflt->name); |
|
316 free(object); |
|
317 f_print(fout, "\t\tbreak;\n"); |
|
318 } |
|
319 } else { |
|
320 f_print(fout, "\tdefault:\n"); |
|
321 f_print(fout, "\t\treturn (FALSE);\n"); |
|
322 } |
|
323 f_print(fout, "\t}\n"); |
|
324 } |
|
325 |
|
326 |
|
327 |
|
328 static void |
|
329 emit_struct(def) |
|
330 definition *def; |
|
331 { |
|
332 decl_list *dl; |
|
333 |
|
334 for (dl = def->def.st.decls; dl != NULL; dl = dl->next) { |
|
335 print_stat(&dl->decl); |
|
336 } |
|
337 } |
|
338 |
|
339 |
|
340 |
|
341 |
|
342 static void |
|
343 emit_typedef(def) |
|
344 definition *def; |
|
345 { |
|
346 char *prefix = def->def.ty.old_prefix; |
|
347 char *type = def->def.ty.old_type; |
|
348 char *amax = def->def.ty.array_max; |
|
349 relation rel = def->def.ty.rel; |
|
350 |
|
351 print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name); |
|
352 } |
|
353 |
|
354 |
|
355 |
|
356 |
|
357 /* |
|
358 * Emit the C-routine for the given definition |
|
359 */ |
|
360 void |
|
361 emit(def) |
|
362 definition *def; |
|
363 { |
|
364 if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) { |
|
365 return; |
|
366 } |
|
367 print_header(def); |
|
368 switch (def->def_kind) { |
|
369 case DEF_UNION: |
|
370 emit_union(def); |
|
371 break; |
|
372 case DEF_ENUM: |
|
373 emit_enum(def); |
|
374 break; |
|
375 case DEF_STRUCT: |
|
376 emit_struct(def); |
|
377 break; |
|
378 case DEF_TYPEDEF: |
|
379 emit_typedef(def); |
|
380 break; |
|
381 case DEF_CONST: |
|
382 case DEF_PROGRAM: |
|
383 break; |
|
384 } |
|
385 print_trailer(); |
|
386 } |