Remove unused variables.
[dragonfly/netmp.git] / usr.bin / rpcgen / rpc_cout.c
blob47a63c5b62c395033d59a1de0dc74c5f10bab66a
1 /*
2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
8 *
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
26 * 2550 Garcia Avenue
27 * Mountain View, California 94043
29 * @(#)rpc_cout.c 1.13 89/02/22 (C) 1987 SMI
30 * $FreeBSD: src/usr.bin/rpcgen/rpc_cout.c,v 1.7 1999/08/28 01:05:16 peter Exp $
31 * $DragonFly: src/usr.bin/rpcgen/rpc_cout.c,v 1.4 2004/06/19 16:40:36 joerg Exp $
34 #ident "@(#)rpc_cout.c 1.14 93/07/05 SMI"
37 * rpc_cout.c, XDR routine outputter for the RPC protocol compiler
38 * Copyright (C) 1987, Sun Microsystems, Inc.
40 #include <err.h>
41 #include <ctype.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include "rpc_parse.h"
45 #include "rpc_util.h"
47 static void print_header(definition *);
48 static void print_trailer(void);
49 static void print_stat(int , declaration *);
50 static void emit_enum(definition *);
51 static void emit_program(definition *);
52 static void emit_union(definition *);
53 static void emit_struct(definition *);
54 static void emit_typedef(definition *);
55 static void emit_inline(int, declaration *, int);
56 static void emit_single_in_line(int, declaration *, int, relation);
57 static char *upcase(char *str);
60 * Emit the C-routine for the given definition
62 void
63 emit(definition *def)
65 if (def->def_kind == DEF_CONST)
66 return;
67 if (def->def_kind == DEF_PROGRAM) {
68 emit_program(def);
69 return;
71 if (def->def_kind == DEF_TYPEDEF) {
73 * now we need to handle declarations like
74 * struct typedef foo foo;
75 * since we dont want this to be expanded into 2 calls to xdr_foo
78 if (strcmp(def->def.ty.old_type, def->def_name) == 0)
79 return;
81 print_header(def);
82 switch (def->def_kind) {
83 case DEF_UNION:
84 emit_union(def);
85 break;
86 case DEF_ENUM:
87 emit_enum(def);
88 break;
89 case DEF_STRUCT:
90 emit_struct(def);
91 break;
92 case DEF_TYPEDEF:
93 emit_typedef(def);
94 break;
95 /* DEF_CONST and DEF_PROGRAM have already been handled */
96 default:
97 break;
99 print_trailer();
102 static int
103 findtype(definition *def, char *type)
106 if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST)
107 return(0);
108 else
109 return(streq(def->def_name, type));
112 static int
113 undefined(char *type)
115 definition *def;
117 def = (definition *) FINDVAL(defined, type, findtype);
118 return(def == NULL);
122 static void
123 print_generic_header(char *procname, int pointerp)
125 f_print(fout, "\n");
126 f_print(fout, "bool_t\n");
127 if (Cflag) {
128 f_print(fout, "xdr_%s(", procname);
129 f_print(fout, "register XDR *xdrs, ");
130 f_print(fout, "%s ", procname);
131 if (pointerp)
132 f_print(fout, "*");
133 f_print(fout, "objp)\n{\n\n");
134 } else {
135 f_print(fout, "xdr_%s(xdrs, objp)\n", procname);
136 f_print(fout, "\tregister XDR *xdrs;\n");
137 f_print(fout, "\t%s ", procname);
138 if (pointerp)
139 f_print(fout, "*");
140 f_print(fout, "objp;\n{\n\n");
144 static void
145 print_header(definition *def)
147 print_generic_header(def->def_name,
148 def->def_kind != DEF_TYPEDEF || !isvectordef(def->def.ty.old_type,
149 def->def.ty.rel));
150 /* Now add Inline support */
152 if (rpcgen_inline == 0)
153 return;
154 /* May cause lint to complain. but ... */
155 f_print(fout, "\tregister long *buf;\n\n");
158 static void
159 print_prog_header(proc_list *plist)
161 print_generic_header(plist->args.argname, 1);
164 static void
165 print_trailer(void)
167 f_print(fout, "\treturn (TRUE);\n");
168 f_print(fout, "}\n");
172 static void
173 print_ifopen(int indent, char *name)
175 tabify(fout, indent);
176 f_print(fout, "if (!xdr_%s(xdrs", name);
179 static void
180 print_ifarg(char *arg)
182 f_print(fout, ", %s", arg);
185 static void
186 print_ifsizeof(int indent, char *prefix, char *type)
188 if (indent) {
189 f_print(fout, ",\n");
190 tabify(fout, indent);
191 } else {
192 f_print(fout, ", ");
194 if (streq(type, "bool")) {
195 f_print(fout, "sizeof (bool_t), (xdrproc_t) xdr_bool");
196 } else {
197 f_print(fout, "sizeof (");
198 if (undefined(type) && prefix)
199 f_print(fout, "%s ", prefix);
200 f_print(fout, "%s), (xdrproc_t) xdr_%s", type, type);
204 static void
205 print_ifclose(int indent)
207 f_print(fout, "))\n");
208 tabify(fout, indent);
209 f_print(fout, "\treturn (FALSE);\n");
212 static void
213 print_ifstat(int indent, char *prefix, char *type, relation rel, char *amax,
214 char *objname, char *name)
216 char *alt = NULL;
218 switch (rel) {
219 case REL_POINTER:
220 print_ifopen(indent, "pointer");
221 print_ifarg("(char **)");
222 f_print(fout, "%s", objname);
223 print_ifsizeof(0, prefix, type);
224 break;
225 case REL_VECTOR:
226 if (streq(type, "string"))
227 alt = "string";
228 else if (streq(type, "opaque"))
229 alt = "opaque";
231 if (alt) {
232 print_ifopen(indent, alt);
233 print_ifarg(objname);
234 } else {
235 print_ifopen(indent, "vector");
236 print_ifarg("(char *)");
237 f_print(fout, "%s", objname);
239 print_ifarg(amax);
240 if (!alt)
241 print_ifsizeof(indent + 1, prefix, type);
242 break;
243 case REL_ARRAY:
244 if (streq(type, "string"))
245 alt = "string";
246 else if (streq(type, "opaque"))
247 alt = "bytes";
249 if (streq(type, "string")) {
250 print_ifopen(indent, alt);
251 print_ifarg(objname);
252 } else {
253 if (alt)
254 print_ifopen(indent, alt);
255 else
256 print_ifopen(indent, "array");
257 print_ifarg("(char **)");
258 if (*objname == '&') {
259 f_print(fout, "%s.%s_val, (u_int *) %s.%s_len",
260 objname, name, objname, name);
261 } else {
262 f_print(fout,
263 "&%s->%s_val, (u_int *) &%s->%s_len",
264 objname, name, objname, name);
267 print_ifarg(amax);
268 if (!alt)
269 print_ifsizeof(indent + 1, prefix, type);
270 break;
271 case REL_ALIAS:
272 print_ifopen(indent, type);
273 print_ifarg(objname);
274 break;
276 print_ifclose(indent);
279 /* ARGSUSED */
280 static void
281 emit_enum(definition *def)
283 print_ifopen(1, "enum");
284 print_ifarg("(enum_t *)objp");
285 print_ifclose(1);
288 static void
289 emit_program(definition *def)
291 decl_list *dl;
292 version_list *vlist;
293 proc_list *plist;
295 for (vlist = def->def.pr.versions; vlist != NULL; vlist = vlist->next)
296 for (plist = vlist->procs; plist != NULL; plist = plist->next) {
297 if (!newstyle || plist->arg_num < 2)
298 continue; /* old style, or single argument */
299 print_prog_header(plist);
300 for (dl = plist->args.decls; dl != NULL;
301 dl = dl->next)
302 print_stat(1, &dl->decl);
303 print_trailer();
308 static void
309 emit_union(definition *def)
311 declaration *dflt;
312 case_list *cl;
313 declaration *cs;
314 char *object;
315 char *vecformat = "objp->%s_u.%s";
316 char *format = "&objp->%s_u.%s";
318 print_stat(1, &def->def.un.enum_decl);
319 f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
320 for (cl = def->def.un.cases; cl != NULL; cl = cl->next) {
322 f_print(fout, "\tcase %s:\n", cl->case_name);
323 if (cl->contflag == 1) /* a continued case statement */
324 continue;
325 cs = &cl->case_decl;
326 if (!streq(cs->type, "void")) {
327 object = alloc(strlen(def->def_name) + strlen(format) +
328 strlen(cs->name) + 1);
329 if (isvectordef (cs->type, cs->rel)) {
330 s_print(object, vecformat, def->def_name,
331 cs->name);
332 } else {
333 s_print(object, format, def->def_name,
334 cs->name);
336 print_ifstat(2, cs->prefix, cs->type, cs->rel,
337 cs->array_max, object, cs->name);
338 free(object);
340 f_print(fout, "\t\tbreak;\n");
342 dflt = def->def.un.default_decl;
343 if (dflt != NULL) {
344 if (!streq(dflt->type, "void")) {
345 f_print(fout, "\tdefault:\n");
346 object = alloc(strlen(def->def_name) + strlen(format) +
347 strlen(dflt->name) + 1);
348 if (isvectordef (dflt->type, dflt->rel)) {
349 s_print(object, vecformat, def->def_name,
350 dflt->name);
351 } else {
352 s_print(object, format, def->def_name,
353 dflt->name);
356 print_ifstat(2, dflt->prefix, dflt->type, dflt->rel,
357 dflt->array_max, object, dflt->name);
358 free(object);
359 f_print(fout, "\t\tbreak;\n");
360 } else {
361 f_print(fout, "\tdefault:\n");
362 f_print(fout, "\t\tbreak;\n");
364 } else {
365 f_print(fout, "\tdefault:\n");
366 f_print(fout, "\t\treturn (FALSE);\n");
369 f_print(fout, "\t}\n");
372 static void
373 inline_struct(definition *def, int flag)
375 decl_list *dl;
376 int i, size;
377 decl_list *cur, *psav;
378 bas_type *ptr;
379 char *sizestr, *plus;
380 char ptemp[256];
381 int indent = 1;
383 if (flag == PUT)
384 f_print(fout, "\n\tif (xdrs->x_op == XDR_ENCODE) {\n");
385 else
386 f_print(fout, "\t\treturn (TRUE);\n"
387 "\t} else if (xdrs->x_op == XDR_DECODE) {\n");
389 i = 0;
390 size = 0;
391 sizestr = NULL;
392 for (dl = def->def.st.decls; dl != NULL; dl = dl->next) { /* xxx */
393 /* now walk down the list and check for basic types */
394 if ((dl->decl.prefix == NULL) &&
395 ((ptr = find_type(dl->decl.type)) != NULL) &&
396 ((dl->decl.rel == REL_ALIAS) ||
397 (dl->decl.rel == REL_VECTOR))){
398 if (i == 0)
399 cur = dl;
400 i++;
402 if (dl->decl.rel == REL_ALIAS) {
403 size += ptr->length;
404 } else {
405 /* this code is required to handle arrays */
406 if (sizestr == NULL)
407 plus = "";
408 else
409 plus = " + ";
411 if (ptr->length != 1)
412 s_print(ptemp, "%s%s * %d",
413 plus, dl->decl.array_max,
414 ptr->length);
415 else
416 s_print(ptemp, "%s%s", plus,
417 dl->decl.array_max);
419 /* now concatenate to sizestr !!!! */
420 if (sizestr == NULL) {
421 sizestr = strdup(ptemp);
422 } else {
423 sizestr = realloc(sizestr,
424 strlen(sizestr)
425 +strlen(ptemp)+1);
426 if (sizestr == NULL){
427 warnx("fatal error: no memory");
428 crash();
430 sizestr = strcat(sizestr, ptemp);
431 /* build up length of array */
434 } else {
435 if (i > 0) {
436 if (sizestr == NULL && size < rpcgen_inline){
438 * don't expand into inline code
439 * if size < rpcgen_inline
441 while (cur != dl){
442 print_stat(indent + 1,
443 &cur->decl);
444 cur = cur->next;
446 } else {
447 /* were already looking at a xdr_inlineable structure */
448 tabify(fout, indent + 1);
449 if (sizestr == NULL)
450 f_print(fout, "buf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
451 size);
452 else {
453 if (size == 0)
454 f_print(fout,
455 "buf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
456 sizestr);
457 else
458 f_print(fout,
459 "buf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
460 size, sizestr);
463 f_print(fout, "\n");
464 tabify(fout, indent + 1);
465 f_print(fout,
466 "if (buf == NULL) {\n");
468 psav = cur;
469 while (cur != dl) {
470 print_stat(indent + 2, &cur->decl);
471 cur = cur->next;
474 f_print(fout, "\n\t\t} else {\n");
476 cur = psav;
477 while (cur != dl) {
478 emit_inline(indent + 2, &cur->decl, flag);
479 cur = cur->next;
482 tabify(fout, indent + 1);
483 f_print(fout, "}\n");
486 size = 0;
487 i = 0;
488 sizestr = NULL;
489 print_stat(indent + 1, &dl->decl);
493 if (i > 0)
494 if (sizestr == NULL && size < rpcgen_inline) {
495 /* don't expand into inline code if size < rpcgen_inline */
496 while (cur != dl) {
497 print_stat(indent + 1, &cur->decl);
498 cur = cur->next;
500 } else {
501 /* were already looking at a xdr_inlineable structure */
502 if (sizestr == NULL)
503 f_print(fout, "\t\tbuf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
504 size);
505 else {
506 if (size == 0)
507 f_print(fout,
508 "\t\tbuf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
509 sizestr);
510 else
511 f_print(fout,
512 "\t\tbuf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
513 size, sizestr);
516 f_print(fout, "\n\t\tif (buf == NULL) {\n");
517 psav = cur;
518 while (cur != NULL) {
519 print_stat(indent + 2, &cur->decl);
520 cur = cur->next;
522 f_print(fout, "\t\t} else {\n");
524 cur = psav;
525 while (cur != dl) {
526 emit_inline(indent + 2, &cur->decl, flag);
527 cur = cur->next;
529 f_print(fout, "\t\t}\n");
533 static void
534 emit_struct(def)
535 definition *def;
537 decl_list *dl;
538 int j, size, flag;
539 bas_type *ptr;
540 int can_inline;
542 if (rpcgen_inline == 0) {
543 /* No xdr_inlining at all */
544 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
545 print_stat(1, &dl->decl);
546 return;
549 for (dl = def->def.st.decls; dl != NULL; dl = dl->next) {
550 if (dl->decl.rel == REL_VECTOR){
551 f_print(fout, "\tint i;\n");
552 break;
556 size = 0;
557 can_inline = 0;
559 * Make a first pass and see if inling is possible.
561 for (dl = def->def.st.decls; dl != NULL; dl = dl->next) {
562 if ((dl->decl.prefix == NULL) &&
563 ((ptr = find_type(dl->decl.type)) != NULL) &&
564 ((dl->decl.rel == REL_ALIAS)||
565 (dl->decl.rel == REL_VECTOR))) {
566 if (dl->decl.rel == REL_ALIAS)
567 size += ptr->length;
568 else {
569 can_inline = 1;
570 break; /* can be inlined */
572 } else {
573 if (size >= rpcgen_inline) {
574 can_inline = 1;
575 break; /* can be inlined */
577 size = 0;
580 if (size >= rpcgen_inline)
581 can_inline = 1;
583 if (can_inline == 0) { /* can not inline, drop back to old mode */
584 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
585 print_stat(1, &dl->decl);
586 return;
589 flag = PUT;
590 for (j = 0; j < 2; j++){
591 inline_struct(def, flag);
592 if (flag == PUT)
593 flag = GET;
596 f_print(fout, "\t\treturn (TRUE);\n\t}\n\n");
598 /* now take care of XDR_FREE case */
600 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
601 print_stat(1, &dl->decl);
605 static void
606 emit_typedef(definition *def)
608 char *prefix = def->def.ty.old_prefix;
609 char *type = def->def.ty.old_type;
610 char *amax = def->def.ty.array_max;
611 relation rel = def->def.ty.rel;
613 print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
616 static void
617 print_stat(int indent, declaration *dec)
619 char *prefix = dec->prefix;
620 char *type = dec->type;
621 char *amax = dec->array_max;
622 relation rel = dec->rel;
623 char name[256];
625 if (isvectordef(type, rel))
626 s_print(name, "objp->%s", dec->name);
627 else
628 s_print(name, "&objp->%s", dec->name);
629 print_ifstat(indent, prefix, type, rel, amax, name, dec->name);
634 static void
635 emit_inline(int indent, declaration *decl, int flag)
637 switch (decl->rel) {
638 case REL_ALIAS :
639 emit_single_in_line(indent, decl, flag, REL_ALIAS);
640 break;
641 case REL_VECTOR :
642 tabify(fout, indent);
643 f_print(fout, "{\n");
644 tabify(fout, indent + 1);
645 f_print(fout, "register %s *genp;\n\n", decl->type);
646 tabify(fout, indent + 1);
647 f_print(fout, "for (i = 0, genp = objp->%s;\n", decl->name);
648 tabify(fout, indent + 2);
649 f_print(fout, "i < %s; i++) {\n", decl->array_max);
650 emit_single_in_line(indent + 2, decl, flag, REL_VECTOR);
651 tabify(fout, indent + 1);
652 f_print(fout, "}\n");
653 tabify(fout, indent);
654 f_print(fout, "}\n");
655 default:
656 break;
660 static void
661 emit_single_in_line(int indent, declaration *decl, int flag, relation rel)
663 char *upp_case;
664 int freed = 0;
666 tabify(fout, indent);
667 if (flag == PUT)
668 f_print(fout, "IXDR_PUT_");
669 else if (rel == REL_ALIAS)
670 f_print(fout, "objp->%s = IXDR_GET_", decl->name);
671 else
672 f_print(fout, "*genp++ = IXDR_GET_");
674 upp_case = upcase(decl->type);
676 /* hack - XX */
677 if (strcmp(upp_case, "INT") == 0)
679 free(upp_case);
680 freed = 1;
681 upp_case = "LONG";
684 if (strcmp(upp_case, "U_INT") == 0)
686 free(upp_case);
687 freed = 1;
688 upp_case = "U_LONG";
690 if (flag == PUT) {
691 if (rel == REL_ALIAS)
692 f_print(fout, "%s(buf, objp->%s);\n", upp_case,
693 decl->name);
694 else
695 f_print(fout, "%s(buf, *genp++);\n", upp_case);
696 } else {
697 f_print(fout, "%s(buf);\n", upp_case);
699 if (!freed)
700 free(upp_case);
703 static char *
704 upcase(char *str)
706 char *ptr, *hptr;
708 ptr = malloc(strlen(str)+1);
709 if (ptr == NULL)
710 errx(1, "malloc failed");
712 hptr = ptr;
713 while (*str != '\0')
714 *ptr++ = toupper(*str++);
716 *ptr = '\0';
717 return(hptr);