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