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