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