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