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