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