1 /* @(#)rpc_cout.c 1.1 87/11/04 3.9 RPCSRC */
3 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4 * unrestricted use provided that this legend is included on all tape
5 * media and as a part of the software program in whole or part. Users
6 * may copy or modify Sun RPC without charge, but are not authorized
7 * to license or distribute it to anyone else except as part of a product or
8 * program developed by the user.
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.
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.
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.
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.
26 * Sun Microsystems, Inc.
28 * Mountain View, California 94043
32 * rpc_cout.c, XDR routine outputter for the RPC protocol compiler
33 * Copyright (C) 1987, Sun Microsystems, Inc.
35 #include <afsconfig.h>
36 #include <afs/param.h>
41 #include "rpc_parse.h"
44 /* Static prototypes */
45 static int findtype(definition
* def
, char *type
);
46 static int undefined(char *type
);
47 static void print_header(definition
* def
);
48 static void print_trailer(void);
49 static void print_ifopen(int indent
, char *name
);
50 static void print_ifarg(char *arg
);
51 static void print_ifarg_with_cast(int ptr_to
, char *type
, char *arg
);
52 static void print_ifsizeof(char *prefix
, char *type
);
53 static void print_ifclose(int indent
);
54 static void space(void);
55 static void print_ifstat(int indent
, char *prefix
, char *type
, relation rel
,
56 char *amax
, char *objname
, char *name
);
57 static void emit_enum(definition
* def
);
58 static void emit_union(definition
* def
);
59 static void emit_struct(definition
* def
);
60 static void emit_typedef(definition
* def
);
61 static void print_stat(declaration
* dec
);
62 static void print_hout(declaration
* dec
);
63 static void print_cout(declaration
* dec
);
64 static void print_rxifopen(char *typename
);
65 static void print_rxifarg(char *amp
, char *arg
, int costant
);
66 static void print_rxifsizeof(char *prefix
, char *type
);
70 * Emit the C-routine for the given definition
73 emit(definition
* def
)
75 if (def
->def_kind
== DEF_CONST
) {
79 switch (def
->def_kind
) {
99 findtype(definition
* def
, char *type
)
101 if (def
->def_kind
== DEF_CONST
) {
104 return (streq(def
->def_name
, type
));
109 undefined(char *type
)
113 def
= (definition
*) FINDVAL(defined
, type
, findtype
);
114 return (def
== NULL
);
119 print_header(definition
* def
)
122 f_print(fout
, "bool_t\n");
123 f_print(fout
, "xdr_%s(XDR *xdrs, ", def
->def_name
);
124 f_print(fout
, "%s ", def
->def_name
);
126 if (def
->def_kind
!= DEF_TYPEDEF
127 || !isvectordef(def
->def
.ty
.old_type
, def
->def
.ty
.rel
)) {
133 f_print(fout
, "objp)\n");
134 f_print(fout
, "{\n");
140 f_print(fout
, "\treturn (TRUE);\n");
141 f_print(fout
, "}\n");
147 print_ifopen(int indent
, char *name
)
149 tabify(fout
, indent
);
150 f_print(fout
, "if (!xdr_%s(xdrs", name
);
155 print_ifarg(char *arg
)
157 f_print(fout
, ", %s", arg
);
162 print_ifarg_with_cast(int ptr_to
, char *type
, char *arg
)
164 if (streq(type
, "bool")) {
165 f_print(fout
, ptr_to
? ", (bool_t *) %s" : ", (bool_t) %s", arg
);
167 f_print(fout
, ptr_to
? ", (%s *) %s" : ", (%s) %s", type
, arg
);
172 print_ifsizeof(char *prefix
, char *type
)
174 if (streq(type
, "bool")) {
175 f_print(fout
, ", sizeof(bool_t), (xdrproc_t) xdr_bool");
177 f_print(fout
, ", sizeof(");
178 if (undefined(type
) && prefix
) {
179 f_print(fout
, "%s ", prefix
);
181 f_print(fout
, "%s), (xdrproc_t) xdr_%s", type
, type
);
186 print_ifclose(int indent
)
188 f_print(fout
, ")) {\n");
189 tabify(fout
, indent
);
190 f_print(fout
, "\treturn (FALSE);\n");
191 tabify(fout
, indent
);
192 f_print(fout
, "}\n");
198 f_print(fout
, "\n\n");
202 print_ifarg_val(char *objname
, char *name
)
204 if (*objname
== '&') {
206 f_print(fout
, "%s.val", objname
);
208 f_print(fout
, "%s.%s_val", objname
, name
);
212 f_print(fout
, "&%s->val", objname
);
214 f_print(fout
, "&%s->%s_val", objname
, name
);
220 print_ifarg_len(char *objname
, char *name
)
222 if (*objname
== '&') {
224 f_print(fout
, "%s.len", objname
);
226 f_print(fout
, "%s.%s_len", objname
, name
);
230 f_print(fout
, "&%s->len", objname
);
232 f_print(fout
, "&%s->%s_len", objname
, name
);
238 print_ifstat(int indent
, char *prefix
, char *type
, relation rel
, char *amax
,
239 char *objname
, char *name
)
242 char *altcast
= NULL
;
246 print_ifopen(indent
, "pointer");
247 print_ifarg_with_cast(1, "char *", objname
);
248 print_ifsizeof(prefix
, type
);
251 if (streq(type
, "string")) {
253 } else if (streq(type
, "opaque")) {
258 print_ifopen(indent
, alt
);
260 print_ifarg_with_cast(0, altcast
, objname
);
262 print_ifarg(objname
);
265 print_ifopen(indent
, "vector");
266 print_ifarg_with_cast(1, "char", objname
);
270 print_ifsizeof(prefix
, type
);
274 if (streq(type
, "string")) {
276 } else if (streq(type
, "opaque")) {
278 tabify(fout
, indent
);
279 f_print(fout
, "{\n");
281 tabify(fout
, indent
);
282 f_print(fout
, "u_int __len = (u_int) ");
284 print_ifarg_len(objname
, name
);
285 f_print(fout
, ");\n");
287 if (streq(type
, "string")) {
288 print_ifopen(indent
, alt
);
289 print_ifarg(objname
);
292 print_ifopen(indent
, alt
);
293 print_ifarg("(char **)");
295 print_ifopen(indent
, "array");
296 print_ifarg("(caddr_t *)");
298 print_ifarg_val(objname
, name
);
300 if (streq(type
, "opaque")) {
301 f_print(fout
, "&__len");
303 print_ifarg_len(objname
, name
);
308 print_ifsizeof(prefix
, type
);
312 print_ifopen(indent
, type
);
313 print_ifarg(objname
);
316 print_ifclose(indent
);
317 if (rel
== REL_ARRAY
&& streq(type
, "opaque")) {
318 tabify(fout
, indent
);
320 print_ifarg_len(objname
, name
);
322 f_print(fout
, " = __len;\n");
324 tabify(fout
, indent
);
325 f_print(fout
, "}\n");
332 emit_enum(definition
* def
)
334 print_ifopen(1, "enum");
335 print_ifarg("(enum_t *)objp");
341 emit_union(definition
* def
)
347 char *format
= "&objp->%s_u.%s";
348 char *briefformat
= "&objp->u.%s";
350 print_stat(&def
->def
.un
.enum_decl
);
351 f_print(fout
, "\tswitch (objp->%s) {\n", def
->def
.un
.enum_decl
.name
);
352 for (cl
= def
->def
.un
.cases
; cl
!= NULL
; cl
= cl
->next
) {
354 f_print(fout
, "\tcase %s:\n", cl
->case_name
);
355 if (!streq(cs
->type
, "void")) {
357 alloc(strlen(def
->def_name
) + strlen(format
) +
358 strlen(cs
->name
) + 1);
361 s_print(object
, briefformat
, cs
->name
);
363 s_print(object
, format
, def
->def_name
, cs
->name
);
365 print_ifstat(2, cs
->prefix
, cs
->type
, cs
->rel
, cs
->array_max
,
369 f_print(fout
, "\t\tbreak;\n");
371 dflt
= def
->def
.un
.default_decl
;
373 if (!streq(dflt
->type
, "void")) {
374 f_print(fout
, "\tdefault:\n");
376 alloc(strlen(def
->def_name
) + strlen(format
) +
377 strlen(dflt
->name
) + 1);
378 s_print(object
, format
, def
->def_name
, dflt
->name
);
379 print_ifstat(2, dflt
->prefix
, dflt
->type
, dflt
->rel
,
380 dflt
->array_max
, object
, dflt
->name
);
382 f_print(fout
, "\t\tbreak;\n");
385 f_print(fout
, "\tdefault:\n");
386 f_print(fout
, "\t\treturn (FALSE);\n");
388 f_print(fout
, "\t}\n");
394 emit_struct(definition
* def
)
398 for (dl
= def
->def
.st
.decls
; dl
!= NULL
; dl
= dl
->next
) {
399 print_stat(&dl
->decl
);
407 emit_typedef(definition
* def
)
409 char *prefix
= def
->def
.ty
.old_prefix
;
410 char *type
= def
->def
.ty
.old_type
;
411 char *amax
= def
->def
.ty
.array_max
;
412 relation rel
= def
->def
.ty
.rel
;
414 print_ifstat(1, prefix
, type
, rel
, amax
, "objp", def
->def_name
);
422 print_stat(declaration
* dec
)
424 char *prefix
= dec
->prefix
;
425 char *type
= dec
->type
;
426 char *amax
= dec
->array_max
;
427 relation rel
= dec
->rel
;
430 if (isvectordef(type
, rel
)) {
431 s_print(name
, "objp->%s", dec
->name
);
433 s_print(name
, "&objp->%s", dec
->name
);
435 print_ifstat(1, prefix
, type
, rel
, amax
, name
, dec
->name
);
439 print_hout(declaration
* dec
)
445 s_print(prefix
, "%s ", dec
->prefix
);
448 f_print(fout
, "\ntypedef ");
451 f_print(fout
, "struct %s {\n", dec
->name
);
453 f_print(fout
, "\tu_int %s_len;\n", dec
->name
);
454 f_print(fout
, "\t%s%s *%s_val;\n", prefix
,
455 dec
->type
, dec
->name
);
457 f_print(fout
, "\tu_int %s_len;\n", dec
->name
);
458 f_print(fout
, "\t%s%s *%s_val;\n", prefix
,
459 dec
->type
, dec
->name
);
461 f_print(fout
, "} %s", dec
->name
);
466 f_print(fout
, ";\n");
467 f_print(fout
, "bool_t xdr_%s(XDR *xdrs, %s *objp);\n", dec
->name
,
474 print_cout(declaration
* dec
)
478 f_print(fout
, "bool_t\n");
479 f_print(fout
, "xdr_%s(XDR *xdrs, %s *objp)\n", dec
->name
, dec
->name
);
480 f_print(fout
, "{\n");
481 print_ifstat(1, dec
->prefix
, dec
->type
, dec
->rel
, dec
->array_max
,
489 print_rxifopen(char *typename
)
491 sprintf(Proc_list
->code
, "xdr_%s(&z_xdrs", typename
);
492 sprintf(Proc_list
->scode
, "xdr_%s(z_xdrs", typename
);
497 print_rxifarg(char *amp
, char *arg
, int costant
)
499 char code
[100], scode
[100];
501 sprintf(code
, ", %s%s", amp
, arg
);
503 sprintf(scode
, ", %s", arg
);
505 sprintf(scode
, ", &%s", arg
);
506 strcat(Proc_list
->code
, code
);
507 strcat(Proc_list
->scode
, scode
);
512 print_rxifsizeof(char *prefix
, char *type
)
516 if (streq(type
, "bool")) {
517 strcat(Proc_list
->code
, ", sizeof(bool_t), xdr_bool");
518 strcat(Proc_list
->scode
, ", sizeof(bool_t), xdr_bool");
520 strcat(Proc_list
->code
, ", sizeof(");
521 strcat(Proc_list
->scode
, ", sizeof(");
522 if (undefined(type
) && prefix
) {
523 sprintf(name
, "%s ", prefix
);
524 strcat(Proc_list
->code
, name
);
525 strcat(Proc_list
->scode
, name
);
527 sprintf(name
, "%s), xdr_%s", type
, type
);
528 strcat(Proc_list
->code
, name
);
529 strcat(Proc_list
->scode
, name
);
535 print_param(declaration
* dec
)
537 char *prefix
= dec
->prefix
;
538 char *type
= dec
->type
;
539 char *amax
= dec
->array_max
;
540 relation rel
= dec
->rel
;
541 char *name
= dec
->name
;
544 char *objname
, *amp
= "";
546 if (rel
== REL_POINTER
)
547 Proc_list
->pl
.param_flag
|= INDIRECT_PARAM
;
550 Proc_list
->pl
.param_flag
&= ~INDIRECT_PARAM
;
552 objname
= Proc_list
->pl
.param_name
;
555 print_rxifopen(type
);
556 print_rxifarg(amp
, objname
, 0);
558 print_rxifopen("pointer");
559 print_rxifarg(amp, "(char **)", 1);
560 sprintf(temp, "%s", objname);
561 strcat(Proc_list->code, temp);
562 strcat(Proc_list->scode, temp);
563 print_rxifsizeof(prefix, type);
567 if (streq(type
, "string")) {
569 } else if (streq(type
, "opaque")) {
575 print_rxifopen("vector");
577 print_rxifarg(amp
, "(char *)", 0);
578 sprintf(temp
, "%s", objname
);
579 strcat(Proc_list
->code
, temp
);
580 strcat(Proc_list
->scode
, temp
);
581 print_rxifarg("", amax
, 1);
583 print_rxifsizeof(prefix
, type
);
587 if (streq(type
, "string")) {
589 } else if (streq(type
, "opaque")) {
592 if (streq(type
, "string")) {
594 if ((Proc_list
->pl
.param_kind
== DEF_OUTPARAM
)
595 || (Proc_list
->pl
.param_kind
== DEF_INOUTPARAM
)) {
596 Proc_list
->pl
.param_flag
|= OUT_STRING
;
597 print_rxifarg("", objname
, 0);
599 print_rxifarg("(char **) &", objname
, 0);
600 /* print_rxifarg(amp, objname, 0); */
601 print_rxifarg("", amax
, 1);
603 print_rxifsizeof(prefix
, type
);
606 char typecontents
[100];
610 strcpy(temp
, dec
->name
);
611 strcpy(typecontents
, dec
->name
);
612 strcat(typecontents
, " *");
613 strcpy(Proc_list
->pl
.param_type
, typecontents
);
614 sprintf(typecontents
, "%s_%d", Proc_list
->pl
.param_name
,
616 strcpy(Proc_list
->pl
.param_name
, typecontents
);
617 Proc_list
->pl
.param_flag
|= FREETHIS_PARAM
;
618 print_rxifopen(temp
);
619 print_rxifarg(amp
, name
, 0);
625 print_rxifopen("array");
627 print_rxifarg(amp, "(char **)", 1);
628 if (*objname == '&') {
629 sprintf(temp, "%s.%s_val, (u_int *)%s.%s_len",
630 objname, name, objname, name);
632 sprintf(temp, "&%s->%s_val, (u_int *)&%s->%s_len",
633 objname, name, objname, name);
635 strcat(Proc_list->code, temp);
636 strcat(Proc_list->scode, temp);
638 print_rxifarg("", amax, 1);
640 print_rxifsizeof(prefix, type);
645 print_rxifopen(type
);
646 print_rxifarg(amp
, objname
, 0);
649 strcat(Proc_list
->code
, ")");
650 strcat(Proc_list
->scode
, ")");