rpc_cout.c revision 1.24 1 1.24 itojun /* $NetBSD: rpc_cout.c,v 1.24 2002/06/11 06:06:19 itojun Exp $ */
2 1.1 glass /*
3 1.1 glass * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4 1.1 glass * unrestricted use provided that this legend is included on all tape
5 1.1 glass * media and as a part of the software program in whole or part. Users
6 1.1 glass * may copy or modify Sun RPC without charge, but are not authorized
7 1.1 glass * to license or distribute it to anyone else except as part of a product or
8 1.4 pk * program developed by the user or with the express written consent of
9 1.4 pk * Sun Microsystems, Inc.
10 1.4 pk *
11 1.1 glass * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 1.1 glass * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 1.1 glass * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 1.4 pk *
15 1.1 glass * Sun RPC is provided with no support and without any obligation on the
16 1.1 glass * part of Sun Microsystems, Inc. to assist in its use, correction,
17 1.1 glass * modification or enhancement.
18 1.4 pk *
19 1.1 glass * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 1.1 glass * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 1.1 glass * OR ANY PART THEREOF.
22 1.4 pk *
23 1.1 glass * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 1.1 glass * or profits or other special, indirect and consequential damages, even if
25 1.1 glass * Sun has been advised of the possibility of such damages.
26 1.4 pk *
27 1.1 glass * Sun Microsystems, Inc.
28 1.1 glass * 2550 Garcia Avenue
29 1.1 glass * Mountain View, California 94043
30 1.1 glass */
31 1.4 pk
32 1.11 christos #include <sys/cdefs.h>
33 1.19 tv #if defined(__RCSID) && !defined(lint)
34 1.11 christos #if 0
35 1.4 pk static char sccsid[] = "@(#)rpc_cout.c 1.13 89/02/22 (C) 1987 SMI";
36 1.11 christos #else
37 1.24 itojun __RCSID("$NetBSD: rpc_cout.c,v 1.24 2002/06/11 06:06:19 itojun Exp $");
38 1.11 christos #endif
39 1.1 glass #endif
40 1.1 glass
41 1.1 glass /*
42 1.4 pk * rpc_cout.c, XDR routine outputter for the RPC protocol compiler
43 1.1 glass */
44 1.19 tv #include <ctype.h>
45 1.19 tv #include <err.h>
46 1.1 glass #include <stdio.h>
47 1.5 cgd #include <stdlib.h>
48 1.4 pk #include <string.h>
49 1.11 christos #include "rpc_scan.h"
50 1.4 pk #include "rpc_parse.h"
51 1.1 glass #include "rpc_util.h"
52 1.1 glass
53 1.11 christos static int findtype __P((definition *, char *));
54 1.11 christos static int undefined __P((char *));
55 1.11 christos static void print_generic_header __P((char *, int));
56 1.11 christos static void print_header __P((definition *));
57 1.11 christos static void print_prog_header __P((proc_list *));
58 1.11 christos static void print_trailer __P((void));
59 1.11 christos static void print_ifopen __P((int, char *));
60 1.11 christos static void print_ifarg __P((char *));
61 1.11 christos static void print_ifsizeof __P((char *, char *));
62 1.11 christos static void print_ifclose __P((int));
63 1.11 christos static void print_ifstat __P((int, char *, char *, relation, char *, char *, char *));
64 1.11 christos static void emit_enum __P((definition *));
65 1.11 christos static void emit_program __P((definition *));
66 1.11 christos static void emit_union __P((definition *));
67 1.11 christos static void emit_struct __P((definition *));
68 1.11 christos static void emit_typedef __P((definition *));
69 1.11 christos static void print_stat __P((int, declaration *));
70 1.1 glass
71 1.1 glass /*
72 1.4 pk * Emit the C-routine for the given definition
73 1.1 glass */
74 1.1 glass void
75 1.1 glass emit(def)
76 1.1 glass definition *def;
77 1.1 glass {
78 1.4 pk if (def->def_kind == DEF_CONST) {
79 1.4 pk return;
80 1.4 pk }
81 1.4 pk if (def->def_kind == DEF_PROGRAM) {
82 1.4 pk emit_program(def);
83 1.1 glass return;
84 1.1 glass }
85 1.4 pk if (def->def_kind == DEF_TYPEDEF) {
86 1.4 pk /* now we need to handle declarations like struct typedef foo
87 1.4 pk * foo; since we dont want this to be expanded into 2 calls to
88 1.4 pk * xdr_foo */
89 1.4 pk
90 1.4 pk if (strcmp(def->def.ty.old_type, def->def_name) == 0)
91 1.4 pk return;
92 1.4 pk };
93 1.4 pk
94 1.1 glass print_header(def);
95 1.7 mycroft
96 1.1 glass switch (def->def_kind) {
97 1.1 glass case DEF_UNION:
98 1.1 glass emit_union(def);
99 1.1 glass break;
100 1.1 glass case DEF_ENUM:
101 1.1 glass emit_enum(def);
102 1.1 glass break;
103 1.1 glass case DEF_STRUCT:
104 1.1 glass emit_struct(def);
105 1.1 glass break;
106 1.1 glass case DEF_TYPEDEF:
107 1.1 glass emit_typedef(def);
108 1.1 glass break;
109 1.11 christos case DEF_PROGRAM:
110 1.11 christos case DEF_CONST:
111 1.24 itojun errx(1, "Internal error %s, %d: Case %d not handled",
112 1.11 christos __FILE__, __LINE__, def->def_kind);
113 1.11 christos break;
114 1.1 glass }
115 1.1 glass print_trailer();
116 1.1 glass }
117 1.1 glass
118 1.11 christos static int
119 1.1 glass findtype(def, type)
120 1.1 glass definition *def;
121 1.4 pk char *type;
122 1.1 glass {
123 1.4 pk
124 1.1 glass if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) {
125 1.1 glass return (0);
126 1.1 glass } else {
127 1.1 glass return (streq(def->def_name, type));
128 1.1 glass }
129 1.1 glass }
130 1.1 glass
131 1.11 christos static int
132 1.1 glass undefined(type)
133 1.4 pk char *type;
134 1.1 glass {
135 1.1 glass definition *def;
136 1.1 glass
137 1.1 glass def = (definition *) FINDVAL(defined, type, findtype);
138 1.4 pk
139 1.4 pk
140 1.1 glass return (def == NULL);
141 1.1 glass }
142 1.1 glass
143 1.11 christos static void
144 1.4 pk print_generic_header(procname, pointerp)
145 1.4 pk char *procname;
146 1.4 pk int pointerp;
147 1.4 pk {
148 1.4 pk f_print(fout, "\n");
149 1.4 pk f_print(fout, "bool_t\n");
150 1.4 pk if (Cflag) {
151 1.4 pk f_print(fout, "xdr_%s(", procname);
152 1.4 pk f_print(fout, "XDR *xdrs, ");
153 1.4 pk f_print(fout, "%s ", procname);
154 1.4 pk if (pointerp)
155 1.4 pk f_print(fout, "*");
156 1.7 mycroft f_print(fout, "objp)\n{\n");
157 1.4 pk } else {
158 1.4 pk f_print(fout, "xdr_%s(xdrs, objp)\n", procname);
159 1.4 pk f_print(fout, "\tXDR *xdrs;\n");
160 1.4 pk f_print(fout, "\t%s ", procname);
161 1.4 pk if (pointerp)
162 1.4 pk f_print(fout, "*");
163 1.7 mycroft f_print(fout, "objp;\n{\n");
164 1.4 pk }
165 1.4 pk }
166 1.1 glass
167 1.11 christos static void
168 1.1 glass print_header(def)
169 1.1 glass definition *def;
170 1.1 glass {
171 1.4 pk print_generic_header(def->def_name,
172 1.4 pk def->def_kind != DEF_TYPEDEF ||
173 1.4 pk !isvectordef(def->def.ty.old_type, def->def.ty.rel));
174 1.4 pk }
175 1.4 pk
176 1.11 christos static void
177 1.4 pk print_prog_header(plist)
178 1.4 pk proc_list *plist;
179 1.4 pk {
180 1.4 pk print_generic_header(plist->args.argname, 1);
181 1.1 glass }
182 1.1 glass
183 1.11 christos static void
184 1.1 glass print_trailer()
185 1.1 glass {
186 1.1 glass f_print(fout, "\treturn (TRUE);\n");
187 1.1 glass f_print(fout, "}\n");
188 1.1 glass }
189 1.1 glass
190 1.1 glass
191 1.11 christos static void
192 1.1 glass print_ifopen(indent, name)
193 1.4 pk int indent;
194 1.4 pk char *name;
195 1.1 glass {
196 1.16 fvdl char _t_kludge[32];
197 1.16 fvdl /*
198 1.16 fvdl * XXX Solaris seems to strip the _t. No idea why.
199 1.16 fvdl */
200 1.16 fvdl if (!strcmp(name, "rpcprog_t") || !strcmp(name, "rpcvers_t") ||
201 1.16 fvdl !strcmp(name, "rpcproc_t") || !strcmp(name, "rpcprot_t") ||
202 1.16 fvdl !strcmp(name, "rpcport_t") || !strcmp(name, "rpcpinline_t")) {
203 1.16 fvdl strncpy(_t_kludge, name, strlen(name) - 2);
204 1.16 fvdl name = _t_kludge;
205 1.16 fvdl }
206 1.1 glass tabify(fout, indent);
207 1.7 mycroft f_print(fout, "if (!xdr_%s(xdrs", name);
208 1.1 glass }
209 1.1 glass
210 1.11 christos static void
211 1.1 glass print_ifarg(arg)
212 1.4 pk char *arg;
213 1.1 glass {
214 1.1 glass f_print(fout, ", %s", arg);
215 1.1 glass }
216 1.1 glass
217 1.11 christos static void
218 1.1 glass print_ifsizeof(prefix, type)
219 1.4 pk char *prefix;
220 1.4 pk char *type;
221 1.1 glass {
222 1.1 glass if (streq(type, "bool")) {
223 1.21 christos f_print(fout, ", (u_int)sizeof(bool_t), (xdrproc_t)xdr_bool");
224 1.1 glass } else {
225 1.21 christos f_print(fout, ", (u_int)sizeof(");
226 1.1 glass if (undefined(type) && prefix) {
227 1.1 glass f_print(fout, "%s ", prefix);
228 1.1 glass }
229 1.4 pk f_print(fout, "%s), (xdrproc_t)xdr_%s", type, type);
230 1.1 glass }
231 1.1 glass }
232 1.1 glass
233 1.11 christos static void
234 1.1 glass print_ifclose(indent)
235 1.4 pk int indent;
236 1.1 glass {
237 1.7 mycroft f_print(fout, "))\n");
238 1.1 glass tabify(fout, indent);
239 1.7 mycroft f_print(fout, "\treturn (FALSE);\n");
240 1.1 glass }
241 1.1 glass
242 1.11 christos static void
243 1.1 glass print_ifstat(indent, prefix, type, rel, amax, objname, name)
244 1.4 pk int indent;
245 1.4 pk char *prefix;
246 1.4 pk char *type;
247 1.1 glass relation rel;
248 1.4 pk char *amax;
249 1.4 pk char *objname;
250 1.4 pk char *name;
251 1.1 glass {
252 1.4 pk char *alt = NULL;
253 1.1 glass
254 1.1 glass switch (rel) {
255 1.1 glass case REL_POINTER:
256 1.1 glass print_ifopen(indent, "pointer");
257 1.1 glass print_ifarg("(char **)");
258 1.1 glass f_print(fout, "%s", objname);
259 1.1 glass print_ifsizeof(prefix, type);
260 1.1 glass break;
261 1.1 glass case REL_VECTOR:
262 1.1 glass if (streq(type, "string")) {
263 1.1 glass alt = "string";
264 1.4 pk } else
265 1.4 pk if (streq(type, "opaque")) {
266 1.4 pk alt = "opaque";
267 1.4 pk }
268 1.1 glass if (alt) {
269 1.1 glass print_ifopen(indent, alt);
270 1.1 glass print_ifarg(objname);
271 1.1 glass } else {
272 1.1 glass print_ifopen(indent, "vector");
273 1.20 christos print_ifarg("(char *)(void *)");
274 1.1 glass f_print(fout, "%s", objname);
275 1.1 glass }
276 1.1 glass print_ifarg(amax);
277 1.1 glass if (!alt) {
278 1.1 glass print_ifsizeof(prefix, type);
279 1.1 glass }
280 1.1 glass break;
281 1.1 glass case REL_ARRAY:
282 1.1 glass if (streq(type, "string")) {
283 1.1 glass alt = "string";
284 1.4 pk } else
285 1.4 pk if (streq(type, "opaque")) {
286 1.4 pk alt = "bytes";
287 1.4 pk }
288 1.1 glass if (streq(type, "string")) {
289 1.1 glass print_ifopen(indent, alt);
290 1.1 glass print_ifarg(objname);
291 1.1 glass } else {
292 1.1 glass if (alt) {
293 1.1 glass print_ifopen(indent, alt);
294 1.1 glass } else {
295 1.1 glass print_ifopen(indent, "array");
296 1.1 glass }
297 1.1 glass print_ifarg("(char **)");
298 1.1 glass if (*objname == '&') {
299 1.1 glass f_print(fout, "%s.%s_val, (u_int *)%s.%s_len",
300 1.4 pk objname, name, objname, name);
301 1.1 glass } else {
302 1.1 glass f_print(fout, "&%s->%s_val, (u_int *)&%s->%s_len",
303 1.4 pk objname, name, objname, name);
304 1.1 glass }
305 1.1 glass }
306 1.1 glass print_ifarg(amax);
307 1.1 glass if (!alt) {
308 1.1 glass print_ifsizeof(prefix, type);
309 1.1 glass }
310 1.1 glass break;
311 1.1 glass case REL_ALIAS:
312 1.1 glass print_ifopen(indent, type);
313 1.1 glass print_ifarg(objname);
314 1.1 glass break;
315 1.1 glass }
316 1.1 glass print_ifclose(indent);
317 1.1 glass }
318 1.1 glass /* ARGSUSED */
319 1.11 christos static void
320 1.1 glass emit_enum(def)
321 1.1 glass definition *def;
322 1.1 glass {
323 1.20 christos tabify(fout, 1);
324 1.20 christos f_print(fout, "{\n");
325 1.20 christos tabify(fout, 2);
326 1.20 christos f_print(fout, "enum_t et = (enum_t)*objp;\n");
327 1.20 christos print_ifopen(2, "enum");
328 1.20 christos print_ifarg("&et");
329 1.20 christos print_ifclose(2);
330 1.22 christos tabify(fout, 2);
331 1.22 christos f_print(fout, "*objp = (%s)et;\n", def->def_name);
332 1.20 christos tabify(fout, 1);
333 1.20 christos f_print(fout, "}\n");
334 1.1 glass }
335 1.1 glass
336 1.11 christos static void
337 1.4 pk emit_program(def)
338 1.4 pk definition *def;
339 1.4 pk {
340 1.4 pk decl_list *dl;
341 1.4 pk version_list *vlist;
342 1.4 pk proc_list *plist;
343 1.4 pk
344 1.4 pk for (vlist = def->def.pr.versions; vlist != NULL; vlist = vlist->next)
345 1.4 pk for (plist = vlist->procs; plist != NULL; plist = plist->next) {
346 1.4 pk if (!newstyle || plist->arg_num < 2)
347 1.4 pk continue; /* old style, or single
348 1.4 pk * argument */
349 1.4 pk print_prog_header(plist);
350 1.4 pk for (dl = plist->args.decls; dl != NULL;
351 1.4 pk dl = dl->next)
352 1.4 pk print_stat(1, &dl->decl);
353 1.4 pk print_trailer();
354 1.4 pk }
355 1.4 pk }
356 1.4 pk
357 1.1 glass
358 1.11 christos static void
359 1.1 glass emit_union(def)
360 1.1 glass definition *def;
361 1.1 glass {
362 1.1 glass declaration *dflt;
363 1.1 glass case_list *cl;
364 1.1 glass declaration *cs;
365 1.4 pk char *object;
366 1.23 yamt static const char vecformat[] = "objp->%s_u.%s";
367 1.23 yamt static const char format[] = "&objp->%s_u.%s";
368 1.1 glass
369 1.7 mycroft f_print(fout, "\n");
370 1.4 pk print_stat(1, &def->def.un.enum_decl);
371 1.1 glass f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
372 1.1 glass for (cl = def->def.un.cases; cl != NULL; cl = cl->next) {
373 1.4 pk f_print(fout, "\tcase %s:\n", cl->case_name);
374 1.4 pk if (cl->contflag == 1) /* a continued case statement */
375 1.4 pk continue;
376 1.1 glass cs = &cl->case_decl;
377 1.1 glass if (!streq(cs->type, "void")) {
378 1.1 glass object = alloc(strlen(def->def_name) + strlen(format) +
379 1.4 pk strlen(cs->name) + 1);
380 1.4 pk if (isvectordef(cs->type, cs->rel)) {
381 1.4 pk s_print(object, vecformat, def->def_name,
382 1.4 pk cs->name);
383 1.4 pk } else {
384 1.4 pk s_print(object, format, def->def_name,
385 1.4 pk cs->name);
386 1.4 pk }
387 1.7 mycroft print_ifstat(2, cs->prefix, cs->type, cs->rel,
388 1.7 mycroft cs->array_max, object, cs->name);
389 1.1 glass free(object);
390 1.1 glass }
391 1.1 glass f_print(fout, "\t\tbreak;\n");
392 1.1 glass }
393 1.1 glass dflt = def->def.un.default_decl;
394 1.7 mycroft f_print(fout, "\tdefault:\n");
395 1.1 glass if (dflt != NULL) {
396 1.1 glass if (!streq(dflt->type, "void")) {
397 1.1 glass object = alloc(strlen(def->def_name) + strlen(format) +
398 1.4 pk strlen(dflt->name) + 1);
399 1.4 pk if (isvectordef(dflt->type, dflt->rel)) {
400 1.4 pk s_print(object, vecformat, def->def_name,
401 1.4 pk dflt->name);
402 1.4 pk } else {
403 1.4 pk s_print(object, format, def->def_name,
404 1.4 pk dflt->name);
405 1.4 pk }
406 1.1 glass print_ifstat(2, dflt->prefix, dflt->type, dflt->rel,
407 1.4 pk dflt->array_max, object, dflt->name);
408 1.1 glass free(object);
409 1.1 glass }
410 1.7 mycroft f_print(fout, "\t\tbreak;\n");
411 1.1 glass } else {
412 1.1 glass f_print(fout, "\t\treturn (FALSE);\n");
413 1.1 glass }
414 1.4 pk
415 1.1 glass f_print(fout, "\t}\n");
416 1.1 glass }
417 1.1 glass
418 1.11 christos static void
419 1.1 glass emit_struct(def)
420 1.1 glass definition *def;
421 1.1 glass {
422 1.1 glass decl_list *dl;
423 1.4 pk int i, j, size, flag;
424 1.11 christos decl_list *cur = NULL, *psav;
425 1.4 pk bas_type *ptr;
426 1.4 pk char *sizestr, *plus;
427 1.4 pk char ptemp[256];
428 1.4 pk int can_inline;
429 1.1 glass
430 1.4 pk
431 1.11 christos if (doinline == 0) {
432 1.20 christos f_print(fout, "\n");
433 1.4 pk for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
434 1.4 pk print_stat(1, &dl->decl);
435 1.4 pk return;
436 1.1 glass }
437 1.4 pk size = 0;
438 1.4 pk can_inline = 0;
439 1.4 pk for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
440 1.4 pk if ((dl->decl.prefix == NULL) &&
441 1.4 pk ((ptr = find_type(dl->decl.type)) != NULL) &&
442 1.4 pk ((dl->decl.rel == REL_ALIAS) || (dl->decl.rel == REL_VECTOR))) {
443 1.4 pk
444 1.4 pk if (dl->decl.rel == REL_ALIAS)
445 1.4 pk size += ptr->length;
446 1.4 pk else {
447 1.4 pk can_inline = 1;
448 1.4 pk break; /* can be inlined */
449 1.4 pk };
450 1.4 pk } else {
451 1.11 christos if (size >= doinline) {
452 1.4 pk can_inline = 1;
453 1.4 pk break; /* can be inlined */
454 1.4 pk }
455 1.4 pk size = 0;
456 1.4 pk }
457 1.11 christos if (size > doinline)
458 1.4 pk can_inline = 1;
459 1.4 pk
460 1.4 pk if (can_inline == 0) { /* can not inline, drop back to old mode */
461 1.20 christos f_print(fout, "\n");
462 1.4 pk for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
463 1.4 pk print_stat(1, &dl->decl);
464 1.4 pk return;
465 1.4 pk };
466 1.4 pk
467 1.7 mycroft /* May cause lint to complain. but ... */
468 1.12 lukem f_print(fout, "\tint32_t *buf;\n");
469 1.4 pk
470 1.7 mycroft flag = PUT;
471 1.7 mycroft f_print(fout, "\n\tif (xdrs->x_op == XDR_ENCODE) {\n");
472 1.4 pk
473 1.4 pk for (j = 0; j < 2; j++) {
474 1.4 pk i = 0;
475 1.4 pk size = 0;
476 1.4 pk sizestr = NULL;
477 1.4 pk for (dl = def->def.st.decls; dl != NULL; dl = dl->next) { /* xxx */
478 1.4 pk
479 1.4 pk /* now walk down the list and check for basic types */
480 1.4 pk if ((dl->decl.prefix == NULL) && ((ptr = find_type(dl->decl.type)) != NULL) && ((dl->decl.rel == REL_ALIAS) || (dl->decl.rel == REL_VECTOR))) {
481 1.4 pk if (i == 0)
482 1.4 pk cur = dl;
483 1.4 pk i++;
484 1.4 pk
485 1.4 pk if (dl->decl.rel == REL_ALIAS)
486 1.4 pk size += ptr->length;
487 1.4 pk else {
488 1.4 pk /* this is required to handle arrays */
489 1.4 pk
490 1.4 pk if (sizestr == NULL)
491 1.10 mycroft plus = "";
492 1.4 pk else
493 1.10 mycroft plus = " + ";
494 1.4 pk
495 1.4 pk if (ptr->length != 1)
496 1.10 mycroft s_print(ptemp, "%s%s * %d", plus, dl->decl.array_max, ptr->length);
497 1.4 pk else
498 1.10 mycroft s_print(ptemp, "%s%s", plus, dl->decl.array_max);
499 1.4 pk
500 1.4 pk /* now concatenate to sizestr !!!! */
501 1.4 pk if (sizestr == NULL)
502 1.4 pk sizestr = strdup(ptemp);
503 1.4 pk else {
504 1.13 lukem sizestr = (char *) realloc(sizestr, strlen(sizestr) + strlen(ptemp) + 1);
505 1.4 pk if (sizestr == NULL) {
506 1.4 pk
507 1.7 mycroft f_print(stderr, "Fatal error : no memory\n");
508 1.4 pk crash();
509 1.4 pk };
510 1.4 pk sizestr = strcat(sizestr, ptemp); /* build up length of
511 1.4 pk * array */
512 1.4 pk
513 1.4 pk }
514 1.4 pk }
515 1.4 pk
516 1.4 pk } else {
517 1.14 ross if (i > 0) {
518 1.11 christos if (sizestr == NULL && size < doinline) {
519 1.4 pk /* don't expand into inline
520 1.11 christos * code if size < doinline */
521 1.4 pk while (cur != dl) {
522 1.7 mycroft print_stat(2, &cur->decl);
523 1.4 pk cur = cur->next;
524 1.4 pk }
525 1.4 pk } else {
526 1.4 pk
527 1.4 pk
528 1.4 pk
529 1.4 pk /* were already looking at a
530 1.4 pk * xdr_inlineable structure */
531 1.4 pk if (sizestr == NULL)
532 1.7 mycroft f_print(fout, "\t\tbuf = (int32_t *)XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);\n",
533 1.4 pk size);
534 1.4 pk else
535 1.4 pk if (size == 0)
536 1.4 pk f_print(fout,
537 1.7 mycroft "\t\tbuf = (int32_t *)XDR_INLINE(xdrs, %s * BYTES_PER_XDR_UNIT);\n",
538 1.4 pk sizestr);
539 1.4 pk else
540 1.4 pk f_print(fout,
541 1.7 mycroft "\t\tbuf = (int32_t *)XDR_INLINE(xdrs, (%d + %s) * BYTES_PER_XDR_UNIT);\n",
542 1.4 pk size, sizestr);
543 1.4 pk
544 1.7 mycroft f_print(fout, "\t\tif (buf == NULL) {\n");
545 1.4 pk
546 1.4 pk psav = cur;
547 1.4 pk while (cur != dl) {
548 1.7 mycroft print_stat(3, &cur->decl);
549 1.4 pk cur = cur->next;
550 1.4 pk }
551 1.4 pk
552 1.7 mycroft f_print(fout, "\t\t} else {\n");
553 1.4 pk
554 1.4 pk cur = psav;
555 1.4 pk while (cur != dl) {
556 1.4 pk emit_inline(&cur->decl, flag);
557 1.4 pk cur = cur->next;
558 1.4 pk }
559 1.4 pk
560 1.7 mycroft f_print(fout, "\t\t}\n");
561 1.4 pk }
562 1.14 ross }
563 1.4 pk size = 0;
564 1.4 pk i = 0;
565 1.4 pk sizestr = NULL;
566 1.7 mycroft print_stat(2, &dl->decl);
567 1.4 pk }
568 1.4 pk
569 1.4 pk }
570 1.14 ross if (i > 0) {
571 1.11 christos if (sizestr == NULL && size < doinline) {
572 1.4 pk /* don't expand into inline code if size <
573 1.11 christos * doinline */
574 1.4 pk while (cur != dl) {
575 1.7 mycroft print_stat(2, &cur->decl);
576 1.4 pk cur = cur->next;
577 1.4 pk }
578 1.4 pk } else {
579 1.4 pk
580 1.4 pk /* were already looking at a xdr_inlineable
581 1.4 pk * structure */
582 1.4 pk if (sizestr == NULL)
583 1.7 mycroft f_print(fout, "\t\tbuf = (int32_t *)XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);\n",
584 1.4 pk size);
585 1.4 pk else
586 1.4 pk if (size == 0)
587 1.4 pk f_print(fout,
588 1.7 mycroft "\t\tbuf = (int32_t *)XDR_INLINE(xdrs, %s * BYTES_PER_XDR_UNIT);\n",
589 1.4 pk sizestr);
590 1.4 pk else
591 1.4 pk f_print(fout,
592 1.7 mycroft "\t\tbuf = (int32_t *)XDR_INLINE(xdrs, (%d + %s) * BYTES_PER_XDR_UNIT);\n",
593 1.4 pk size, sizestr);
594 1.4 pk
595 1.7 mycroft f_print(fout, "\t\tif (buf == NULL) {\n");
596 1.4 pk
597 1.4 pk psav = cur;
598 1.4 pk while (cur != NULL) {
599 1.7 mycroft print_stat(3, &cur->decl);
600 1.4 pk cur = cur->next;
601 1.4 pk }
602 1.7 mycroft f_print(fout, "\t\t} else {\n");
603 1.4 pk
604 1.4 pk cur = psav;
605 1.4 pk while (cur != dl) {
606 1.4 pk emit_inline(&cur->decl, flag);
607 1.4 pk cur = cur->next;
608 1.4 pk }
609 1.4 pk
610 1.7 mycroft f_print(fout, "\t\t}\n");
611 1.1 glass
612 1.4 pk }
613 1.14 ross }
614 1.7 mycroft if (flag == PUT) {
615 1.7 mycroft flag = GET;
616 1.7 mycroft f_print(fout, "\t} else if (xdrs->x_op == XDR_DECODE) {\n");
617 1.7 mycroft }
618 1.4 pk }
619 1.7 mycroft
620 1.7 mycroft f_print(fout, "\t} else {\n");
621 1.1 glass
622 1.4 pk /* now take care of XDR_FREE case */
623 1.1 glass
624 1.4 pk for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
625 1.7 mycroft print_stat(2, &dl->decl);
626 1.7 mycroft
627 1.7 mycroft f_print(fout, "\t}\n");
628 1.4 pk }
629 1.1 glass
630 1.11 christos static void
631 1.1 glass emit_typedef(def)
632 1.1 glass definition *def;
633 1.1 glass {
634 1.4 pk char *prefix = def->def.ty.old_prefix;
635 1.4 pk char *type = def->def.ty.old_type;
636 1.4 pk char *amax = def->def.ty.array_max;
637 1.1 glass relation rel = def->def.ty.rel;
638 1.1 glass
639 1.20 christos f_print(fout, "\n");
640 1.1 glass print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
641 1.1 glass }
642 1.1 glass
643 1.11 christos static void
644 1.4 pk print_stat(indent, dec)
645 1.1 glass declaration *dec;
646 1.4 pk int indent;
647 1.1 glass {
648 1.4 pk char *prefix = dec->prefix;
649 1.4 pk char *type = dec->type;
650 1.4 pk char *amax = dec->array_max;
651 1.1 glass relation rel = dec->rel;
652 1.4 pk char name[256];
653 1.1 glass
654 1.1 glass if (isvectordef(type, rel)) {
655 1.1 glass s_print(name, "objp->%s", dec->name);
656 1.1 glass } else {
657 1.1 glass s_print(name, "&objp->%s", dec->name);
658 1.1 glass }
659 1.4 pk print_ifstat(indent, prefix, type, rel, amax, name, dec->name);
660 1.4 pk }
661 1.4 pk
662 1.4 pk
663 1.11 christos void
664 1.4 pk emit_inline(decl, flag)
665 1.4 pk declaration *decl;
666 1.4 pk int flag;
667 1.4 pk {
668 1.4 pk
669 1.4 pk /*check whether an array or not */
670 1.4 pk
671 1.4 pk switch (decl->rel) {
672 1.4 pk case REL_ALIAS:
673 1.4 pk emit_single_in_line(decl, flag, REL_ALIAS);
674 1.4 pk break;
675 1.4 pk case REL_VECTOR:
676 1.7 mycroft f_print(fout, "\t\t\t{\n");
677 1.12 lukem f_print(fout, "\t\t\t\tint i;\n");
678 1.12 lukem f_print(fout, "\t\t\t\t%s *genp;\n", decl->type);
679 1.9 mycroft f_print(fout, "\n");
680 1.7 mycroft f_print(fout, "\t\t\t\tfor (i = 0, genp = objp->%s;\n",
681 1.7 mycroft decl->name);
682 1.7 mycroft f_print(fout, "\t\t\t\t i < %s; i++) {\n\t\t",
683 1.7 mycroft decl->array_max);
684 1.4 pk emit_single_in_line(decl, flag, REL_VECTOR);
685 1.7 mycroft f_print(fout, "\t\t\t\t}\n\t\t\t}\n");
686 1.11 christos break;
687 1.11 christos case REL_ARRAY:
688 1.11 christos case REL_POINTER:
689 1.24 itojun errx(1, "Internal error %s, %d: Case %d not handled",
690 1.11 christos __FILE__, __LINE__, decl->rel);
691 1.4 pk }
692 1.4 pk }
693 1.4 pk
694 1.11 christos void
695 1.4 pk emit_single_in_line(decl, flag, rel)
696 1.4 pk declaration *decl;
697 1.4 pk int flag;
698 1.4 pk relation rel;
699 1.4 pk {
700 1.4 pk char *upp_case;
701 1.4 pk int freed = 0;
702 1.4 pk
703 1.4 pk if (flag == PUT)
704 1.7 mycroft f_print(fout, "\t\t\tIXDR_PUT_");
705 1.4 pk else
706 1.4 pk if (rel == REL_ALIAS)
707 1.7 mycroft f_print(fout, "\t\t\tobjp->%s = IXDR_GET_", decl->name);
708 1.4 pk else
709 1.7 mycroft f_print(fout, "\t\t\t*genp++ = IXDR_GET_");
710 1.4 pk
711 1.4 pk upp_case = upcase(decl->type);
712 1.4 pk
713 1.4 pk /* hack - XX */
714 1.4 pk if (strcmp(upp_case, "INT") == 0) {
715 1.4 pk free(upp_case);
716 1.4 pk freed = 1;
717 1.20 christos upp_case = "INT32";
718 1.4 pk }
719 1.4 pk if (strcmp(upp_case, "U_INT") == 0) {
720 1.4 pk free(upp_case);
721 1.4 pk freed = 1;
722 1.20 christos upp_case = "U_INT32";
723 1.4 pk }
724 1.15 christos if (flag == PUT) {
725 1.4 pk if (rel == REL_ALIAS)
726 1.8 mycroft f_print(fout, "%s(buf, objp->%s);\n", upp_case, decl->name);
727 1.4 pk else
728 1.8 mycroft f_print(fout, "%s(buf, *genp++);\n", upp_case);
729 1.4 pk
730 1.15 christos } else
731 1.4 pk f_print(fout, "%s(buf);\n", upp_case);
732 1.4 pk if (!freed)
733 1.4 pk free(upp_case);
734 1.4 pk
735 1.4 pk }
736 1.4 pk
737 1.4 pk
738 1.13 lukem char *
739 1.4 pk upcase(str)
740 1.4 pk char *str;
741 1.4 pk {
742 1.4 pk char *ptr, *hptr;
743 1.4 pk
744 1.4 pk
745 1.13 lukem ptr = (char *) malloc(strlen(str) + 1);
746 1.4 pk if (ptr == (char *) NULL) {
747 1.7 mycroft f_print(stderr, "malloc failed\n");
748 1.4 pk exit(1);
749 1.4 pk };
750 1.4 pk
751 1.4 pk hptr = ptr;
752 1.4 pk while (*str != '\0')
753 1.4 pk *ptr++ = toupper(*str++);
754 1.4 pk
755 1.4 pk *ptr = '\0';
756 1.4 pk return (hptr);
757 1.4 pk
758 1.1 glass }
759