]> git.decadent.org.uk Git - nfs-utils.git/blob - tools/rpcgen/rpc_cout.c
9bc20bbd49dfd2a36a2347c249db08e3dc4bc61c
[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 #ifndef lint
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         decl_list      *dl;
159         bas_type       *ptr;
160         int             i;
161
162
163         print_generic_header(def->def_name,
164                 def->def_kind != DEF_TYPEDEF ||
165                 !isvectordef(def->def.ty.old_type, def->def.ty.rel));
166
167         /* Now add Inline support */
168
169
170         if (Inline == 0)
171                 return;
172         /* May cause lint to complain. but  ... */
173         f_print(fout, "\t register long *buf;\n\n");
174 }
175
176 static void
177 print_prog_header(proc_list *plist)
178 {
179         print_generic_header(plist->args.argname, 1);
180 }
181
182 static void
183 print_trailer(void)
184 {
185         f_print(fout, "\treturn (TRUE);\n");
186         f_print(fout, "}\n");
187 }
188
189
190 static void
191 print_ifopen(int indent, char *name)
192 {
193         tabify(fout, indent);
194         f_print(fout, " if (!xdr_%s(xdrs", name);
195 }
196
197 static void
198 print_ifarg(char *arg)
199 {
200         f_print(fout, ", %s", arg);
201 }
202
203 static void
204 print_ifsizeof(char *prefix, char *type)
205 {
206         if (streq(type, "bool")) {
207                 f_print(fout, ", sizeof(bool_t), (xdrproc_t)xdr_bool");
208         } else {
209                 f_print(fout, ", sizeof(");
210                 if (undefined(type) && prefix) {
211                         f_print(fout, "%s ", prefix);
212                 }
213                 f_print(fout, "%s), (xdrproc_t)xdr_%s", type, type);
214         }
215 }
216
217 static void
218 print_ifclose(int indent)
219 {
220         f_print(fout, ")) {\n");
221         tabify(fout, indent);
222         f_print(fout, "\t return (FALSE);\n");
223         tabify(fout, indent);
224         f_print(fout, " }\n");
225 }
226
227 static void
228 print_ifstat(int indent, char *prefix, char *type, relation rel,
229                         char *amax, char *objname, char *name)
230 {
231         char *alt = NULL;
232
233         switch (rel) {
234         case REL_POINTER:
235                 print_ifopen(indent, "pointer");
236                 print_ifarg("(char **)");
237                 f_print(fout, "%s", objname);
238                 print_ifsizeof(prefix, type);
239                 break;
240         case REL_VECTOR:
241                 if (streq(type, "string")) {
242                         alt = "string";
243                 } else if (streq(type, "opaque")) {
244                         alt = "opaque";
245                 }
246                 if (alt) {
247                         print_ifopen(indent, alt);
248                         print_ifarg(objname);
249                 } else {
250                         print_ifopen(indent, "vector");
251                         print_ifarg("(char *)");
252                         f_print(fout, "%s", objname);
253                 }
254                 print_ifarg(amax);
255                 if (!alt) {
256                         print_ifsizeof(prefix, type);
257                 }
258                 break;
259         case REL_ARRAY:
260                 if (streq(type, "string")) {
261                         alt = "string";
262                 } else if (streq(type, "opaque")) {
263                         alt = "bytes";
264                 }
265                 if (streq(type, "string")) {
266                         print_ifopen(indent, alt);
267                         print_ifarg(objname);
268                 } else {
269                         if (alt) {
270                                 print_ifopen(indent, alt);
271                         } else {
272                                 print_ifopen(indent, "array");
273                         }
274                         print_ifarg("(char **)");
275                         if (*objname == '&') {
276                                 f_print(fout, "%s.%s_val, (u_int *)%s.%s_len",
277                                         objname, name, objname, name);
278                         } else {
279                                 f_print(fout, "&%s->%s_val, (u_int *)&%s->%s_len",
280                                         objname, name, objname, name);
281                         }
282                 }
283                 print_ifarg(amax);
284                 if (!alt) {
285                         print_ifsizeof(prefix, type);
286                 }
287                 break;
288         case REL_ALIAS:
289                 print_ifopen(indent, type);
290                 print_ifarg(objname);
291                 break;
292         }
293         print_ifclose(indent);
294 }
295
296 static void
297 emit_enum(definition *def)
298 {
299         print_ifopen(1, "enum");
300         print_ifarg("(enum_t *)objp");
301         print_ifclose(1);
302 }
303
304 static void
305 emit_program(definition *def)
306 {
307         decl_list      *dl;
308         version_list   *vlist;
309         proc_list      *plist;
310
311         for (vlist = def->def.pr.versions; vlist != NULL; vlist = vlist->next)
312                 for (plist = vlist->procs; plist != NULL; plist = plist->next) {
313                         if (!newstyle || plist->arg_num < 2)
314                                 continue;/* old style, or single argument */
315                         print_prog_header(plist);
316                         for (dl = plist->args.decls; dl != NULL; dl = dl->next)
317                                 print_stat(1, &dl->decl);
318                         print_trailer();
319                 }
320 }
321
322
323 static void
324 emit_union(definition *def)
325 {
326   declaration *dflt;
327   case_list *cl;
328   declaration *cs;
329   char *object;
330   char *vecformat = "objp->%s_u.%s";
331   char *format = "&objp->%s_u.%s";
332
333   print_stat(1,&def->def.un.enum_decl);
334   f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
335   for (cl = def->def.un.cases; cl != NULL; cl = cl->next) {
336
337     f_print(fout, "\tcase %s:\n", cl->case_name);
338     if(cl->contflag == 1)       /* a continued case statement */
339       continue;
340     cs = &cl->case_decl;
341     if (!streq(cs->type, "void")) {
342       object = alloc(strlen(def->def_name) + strlen(format) +
343                      strlen(cs->name) + 1);
344       if (isvectordef (cs->type, cs->rel)) {
345         s_print(object, vecformat, def->def_name, 
346                 cs->name);
347       } else {
348         s_print(object, format, def->def_name, 
349                 cs->name);
350       }
351       print_ifstat(2, cs->prefix, cs->type, cs->rel, cs->array_max,
352                    object, cs->name);
353       free(object);
354     }
355     f_print(fout, "\t\tbreak;\n");
356   }
357   dflt = def->def.un.default_decl;
358   if (dflt != NULL) {
359     if (!streq(dflt->type, "void")) {
360       f_print(fout, "\tdefault:\n");
361       object = alloc(strlen(def->def_name) + strlen(format) +
362                      strlen(dflt->name) + 1);
363       if (isvectordef (dflt->type, dflt->rel)) {
364         s_print(object, vecformat, def->def_name, 
365                 dflt->name);
366       } else {
367         s_print(object, format, def->def_name, 
368                 dflt->name);
369       }
370
371       print_ifstat(2, dflt->prefix, dflt->type, dflt->rel,
372                    dflt->array_max, object, dflt->name);
373       free(object);
374       f_print(fout, "\t\tbreak;\n");
375     } else {
376       /* Avoid gcc warnings about `value not handled in switch' */
377       f_print(fout, "\tdefault:\n");
378       f_print(fout, "\t\tbreak;\n");
379     }
380   } else {
381     f_print(fout, "\tdefault:\n");
382     f_print(fout, "\t\treturn (FALSE);\n");
383   }
384
385   f_print(fout, "\t}\n");
386 }
387
388 static void
389 emit_struct(definition *def)
390 {
391         decl_list      *dl;
392         int             i, j, size, flag;
393         decl_list      *cur = NULL, *psav;
394         bas_type       *ptr;
395         char           *sizestr, *plus;
396         char            ptemp[256];
397         int             can_inline;
398
399
400         if (Inline == 0) {
401                 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
402                         print_stat(1, &dl->decl);
403         } else {
404
405                 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
406                         if (dl->decl.rel == REL_VECTOR) {
407                                 f_print(fout, "\t int i;\n");
408                                 break;
409                         }
410                 size = 0;
411                 can_inline = 0;
412                 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
413                         if ((dl->decl.prefix == NULL) && ((ptr = find_type(dl->decl.type)) != NULL) && ((dl->decl.rel == REL_ALIAS) || (dl->decl.rel == REL_VECTOR))) {
414
415                                 if (dl->decl.rel == REL_ALIAS)
416                                         size += ptr->length;
417                                 else {
418                                         can_inline = 1;
419                                         break;  /* can be inlined */
420                                 };
421                         } else {
422                                 if (size >= Inline) {
423                                         can_inline = 1;
424                                         break;  /* can be inlined */
425                                 }
426                                 size = 0;
427                         }
428                 if (size > Inline)
429                         can_inline = 1;
430
431                 if (can_inline == 0) {  /* can not inline, drop back to old mode */
432                         for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
433                                 print_stat(1, &dl->decl);
434                         return;
435                 };
436
437
438
439
440                 flag = PUT;
441                 for (j = 0; j < 2; j++) {
442
443                         if (flag == PUT)
444                                 f_print(fout, "\n\t if (xdrs->x_op == XDR_ENCODE) {\n");
445                         else
446                                 f_print(fout, "\n \t return (TRUE);\n\t} else if (xdrs->x_op == XDR_DECODE) {\n");
447
448
449                         i = 0;
450                         size = 0;
451                         sizestr = NULL;
452                         for (dl = def->def.st.decls; dl != NULL; dl = dl->next) {       /* xxx */
453
454                                 /* now walk down the list and check for basic types */
455                                 if ((dl->decl.prefix == NULL) && ((ptr = find_type(dl->decl.type)) != NULL) && ((dl->decl.rel == REL_ALIAS) || (dl->decl.rel == REL_VECTOR))) {
456                                         if (i == 0)
457                                                 cur = dl;
458                                         i++;
459
460                                         if (dl->decl.rel == REL_ALIAS)
461                                                 size += ptr->length;
462                                         else {
463                                                 /* this is required to handle arrays */
464
465                                                 if (sizestr == NULL)
466                                                         plus = " ";
467                                                 else
468                                                         plus = "+";
469
470                                                 if (ptr->length != 1)
471                                                         s_print(ptemp, " %s %s * %d", plus, dl->decl.array_max, ptr->length);
472                                                 else
473                                                         s_print(ptemp, " %s %s ", plus, dl->decl.array_max);
474
475                                                 /*now concatenate to sizestr !!!! */
476                                                 if (sizestr == NULL)
477                                                         sizestr = strdup(ptemp);
478                                                 else {
479                                                         sizestr = realloc(sizestr, strlen(sizestr) + strlen(ptemp) + 1);
480                                                         if (sizestr == NULL) {
481
482                                                                 f_print(stderr, "Fatal error : no memory \n");
483                                                                 crash();
484                                                         };
485                                                         sizestr = strcat(sizestr, ptemp);       /*build up length of array */
486
487                                                 }
488                                         }
489
490                                 } else {
491                                         if (i > 0)
492                                             {
493                                                 if (sizestr == NULL && size < Inline) {
494                                                         /* don't expand into inline code if size < inline */
495                                                         while (cur != dl) {
496                                                                 print_stat(1, &cur->decl);
497                                                                 cur = cur->next;
498                                                         }
499                                                 } else {
500
501
502
503                                                         /* were already looking at a xdr_inlineable structure */
504                                                         if (sizestr == NULL)
505                                                                 f_print(fout, "\t buf = XDR_INLINE(xdrs,%d * BYTES_PER_XDR_UNIT);",
506                                                                         size);
507                                                         else if (size == 0)
508                                                                 f_print(fout,
509                                                                         "\t buf = XDR_INLINE(xdrs,%s * BYTES_PER_XDR_UNIT);",
510                                                                         sizestr);
511                                                         else
512                                                                 f_print(fout,
513                                                                         "\t buf = XDR_INLINE(xdrs,(%d + %s)* BYTES_PER_XDR_UNIT);",
514                                                                         size, sizestr);
515
516                                                         f_print(fout, "\n\t   if (buf == NULL) {\n");
517
518                                                         psav = cur;
519                                                         while (cur != dl) {
520                                                                 print_stat(2, &cur->decl);
521                                                                 cur = cur->next;
522                                                         }
523
524                                                         f_print(fout, "\n\t  }\n\t  else {\n");
525
526                                                         cur = psav;
527                                                         while (cur != dl) {
528                                                                 emit_inline(&cur->decl, flag);
529                                                                 cur = cur->next;
530                                                         }
531
532                                                         f_print(fout, "\t  }\n");
533                                                 }
534                                             }
535                                         size = 0;
536                                         i = 0;
537                                         sizestr = NULL;
538                                         print_stat(1, &dl->decl);
539                                 }
540
541                         }
542                         if (i > 0)
543                             {
544                                 if (sizestr == NULL && size < Inline) {
545                                         /* don't expand into inline code if size < inline */
546                                         while (cur != dl) {
547                                                 print_stat(1, &cur->decl);
548                                                 cur = cur->next;
549                                         }
550                                 } else {
551
552                                         /* were already looking at a xdr_inlineable structure */
553                                         if (sizestr == NULL)
554                                                 f_print(fout, "\t\tbuf = XDR_INLINE(xdrs,%d * BYTES_PER_XDR_UNIT);",
555                                                         size);
556                                         else if (size == 0)
557                                                 f_print(fout,
558                                                         "\t\tbuf = XDR_INLINE(xdrs,%s * BYTES_PER_XDR_UNIT);",
559                                                         sizestr);
560                                         else
561                                                 f_print(fout,
562                                                         "\t\tbuf = XDR_INLINE(xdrs,(%d + %s)* BYTES_PER_XDR_UNIT);",
563                                                         size, sizestr);
564
565                                         f_print(fout, "\n\t\tif (buf == NULL) {\n");
566
567                                         psav = cur;
568                                         while (cur != NULL) {
569                                                 print_stat(2, &cur->decl);
570                                                 cur = cur->next;
571                                         }
572                                         f_print(fout, "\n\t  }\n\t  else {\n");
573
574                                         cur = psav;
575                                         while (cur != dl) {
576                                                 emit_inline(&cur->decl, flag);
577                                                 cur = cur->next;
578                                         }
579
580                                         f_print(fout, "\t  }\n");
581
582                                 }
583                             }
584                         flag = GET;
585                 }
586                 f_print(fout, "\t return(TRUE);\n\t}\n\n");
587
588                 /* now take care of XDR_FREE case */
589
590                 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
591                         print_stat(1, &dl->decl);
592         }
593 }
594  
595
596
597
598 static void
599 emit_typedef(definition *def)
600 {
601         char *prefix = def->def.ty.old_prefix;
602         char *type = def->def.ty.old_type;
603         char *amax = def->def.ty.array_max;
604         relation rel = def->def.ty.rel;
605
606
607           print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
608 }
609
610 static void
611 print_stat(int indent, declaration *dec)
612 {
613         char *prefix = dec->prefix;
614         char *type = dec->type;
615         char *amax = dec->array_max;
616         relation rel = dec->rel;
617         char name[256];
618
619         if (isvectordef(type, rel)) {
620                 s_print(name, "objp->%s", dec->name);
621         } else {
622                 s_print(name, "&objp->%s", dec->name);
623         }
624         print_ifstat(indent, prefix, type, rel, amax, name, dec->name);
625 }
626
627
628 static void
629 emit_inline(declaration *decl, int flag)
630 {
631
632         /*check whether an array or not */
633
634         switch (decl->rel) {
635         case REL_ALIAS:
636                 emit_single_in_line(decl, flag, REL_ALIAS);
637                 break;
638         case REL_VECTOR:
639                 f_print(fout, "\t\t{ register %s *genp; \n", decl->type);
640                 f_print(fout, "\t\t  for ( i = 0,genp=objp->%s;\n \t\t\ti < %s; i++){\n\t\t",
641                         decl->name, decl->array_max);
642                 emit_single_in_line(decl, flag, REL_VECTOR);
643                 f_print(fout, "\t\t   }\n\t\t };\n");
644                 break;
645         default:
646                 break;
647         }
648 }
649
650 static void
651 emit_single_in_line(declaration *decl, int flag, relation rel)
652 {
653         char *upp_case;
654         int freed=0;
655
656         if(flag == PUT)
657                 f_print(fout,"\t\t IXDR_PUT_");
658         else    
659                 if(rel== REL_ALIAS)
660                         f_print(fout,"\t\t objp->%s = IXDR_GET_",decl->name);
661                 else
662                         f_print(fout,"\t\t *genp++ = IXDR_GET_");
663
664         upp_case=upcase(decl->type);
665
666         /* hack  - XX */
667         if(strcmp(upp_case,"INT") == 0)
668         {
669                 free(upp_case);
670                 freed=1;
671                 upp_case="LONG";
672         }
673
674         if(strcmp(upp_case,"U_INT") == 0)
675         {
676                 free(upp_case);
677                 freed=1;
678                 upp_case="U_LONG";
679         }
680
681
682         if(flag == PUT) 
683                 if(rel== REL_ALIAS)
684                         f_print(fout,"%s(buf,objp->%s);\n",upp_case,decl->name);
685                 else
686                         f_print(fout,"%s(buf,*genp++);\n",upp_case);
687
688         else
689                 f_print(fout,"%s(buf);\n",upp_case);
690         if(!freed)
691                 free(upp_case);
692
693 }
694
695
696 static char *
697 upcase(char *str)
698 {
699         char           *ptr, *hptr;
700
701
702         ptr = (char *) malloc(strlen(str));
703         if (ptr == (char *) NULL) {
704                 f_print(stderr, "malloc failed \n");
705                 exit(1);
706         };
707
708         hptr = ptr;
709         while (*str != '\0')
710                 *ptr++ = toupper(*str++);
711
712         *ptr = '\0';
713         return (hptr);
714
715 }