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