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