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