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>
45 #include "rpc_parse.h"
48 /* Static prototypes */
49 static int findtype(definition * def, char *type);
50 static int undefined(char *type);
51 static void print_header(definition * def);
52 static void print_trailer(void);
53 static void print_ifopen(int indent, char *name);
54 static void print_ifarg(char *arg);
55 static void print_ifsizeof(char *prefix, char *type);
56 static void print_ifclose(int indent);
57 static void space(void);
58 static void print_ifstat(int indent, char *prefix, char *type, relation rel,
59 char *amax, char *objname, char *name);
60 static void emit_enum(definition * def);
61 static void emit_union(definition * def);
62 static void emit_struct(definition * def);
63 static void emit_typedef(definition * def);
64 static void print_stat(declaration * dec);
65 static void print_hout(declaration * dec);
66 static void print_cout(declaration * dec);
67 static void print_rxifopen(char *typename);
68 static void print_rxifarg(char *amp, char *arg, int costant);
69 static void print_rxifsizeof(char *prefix, char *type);
73 * Emit the C-routine for the given definition
76 emit(definition * def)
78 if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) {
82 switch (def->def_kind) {
102 findtype(definition * def, char *type)
104 if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) {
107 return (streq(def->def_name, type));
112 undefined(char *type)
116 def = (definition *) FINDVAL(defined, type, findtype);
117 return (def == NULL);
122 print_header(definition * def)
125 f_print(fout, "bool_t\n");
126 f_print(fout, "xdr_%s(XDR *xdrs, ", def->def_name);
127 f_print(fout, "%s ", def->def_name);
129 if (def->def_kind != DEF_TYPEDEF
130 || !isvectordef(def->def.ty.old_type, def->def.ty.rel)) {
136 f_print(fout, "objp)\n");
137 f_print(fout, "{\n");
143 f_print(fout, "\treturn (TRUE);\n");
144 f_print(fout, "}\n");
150 print_ifopen(int indent, char *name)
152 tabify(fout, indent);
153 f_print(fout, "if (!xdr_%s(xdrs", name);
158 print_ifarg(char *arg)
160 f_print(fout, ", %s", arg);
165 print_ifsizeof(char *prefix, char *type)
167 if (streq(type, "bool")) {
168 f_print(fout, ", sizeof(bool_t), (xdrproc_t) xdr_bool");
170 f_print(fout, ", sizeof(");
171 if (undefined(type) && prefix) {
172 f_print(fout, "%s ", prefix);
174 f_print(fout, "%s), (xdrproc_t) xdr_%s", type, type);
179 print_ifclose(int indent)
181 f_print(fout, ")) {\n");
182 tabify(fout, indent);
183 f_print(fout, "\treturn (FALSE);\n");
184 tabify(fout, indent);
185 f_print(fout, "}\n");
191 f_print(fout, "\n\n");
195 print_ifstat(int indent, char *prefix, char *type, relation rel, char *amax,
196 char *objname, char *name)
202 print_ifopen(indent, "pointer");
203 print_ifarg("(char **)");
204 f_print(fout, "%s", objname);
205 print_ifsizeof(prefix, type);
208 if (streq(type, "string")) {
210 } else if (streq(type, "opaque")) {
214 print_ifopen(indent, alt);
215 print_ifarg(objname);
217 print_ifopen(indent, "vector");
218 print_ifarg("(char *)");
219 f_print(fout, "%s", objname);
223 print_ifsizeof(prefix, type);
227 if (streq(type, "string")) {
229 } else if (streq(type, "opaque")) {
232 if (streq(type, "string")) {
233 print_ifopen(indent, alt);
234 print_ifarg(objname);
237 print_ifopen(indent, alt);
239 print_ifopen(indent, "array");
241 print_ifarg("(char **)");
242 if (*objname == '&') {
243 f_print(fout, "%s.%s_val, (u_int *)%s.%s_len", objname, name,
246 f_print(fout, "&%s->%s_val, (u_int *)&%s->%s_len", objname,
247 name, objname, name);
252 print_ifsizeof(prefix, type);
256 print_ifopen(indent, type);
257 print_ifarg(objname);
260 print_ifclose(indent);
265 emit_enum(definition * def)
267 print_ifopen(1, "enum");
268 print_ifarg("(enum_t *)objp");
274 emit_union(definition * def)
280 char *format = "&objp->%s_u.%s";
282 print_stat(&def->def.un.enum_decl);
283 f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
284 for (cl = def->def.un.cases; cl != NULL; cl = cl->next) {
286 f_print(fout, "\tcase %s:\n", cl->case_name);
287 if (!streq(cs->type, "void")) {
289 alloc(strlen(def->def_name) + strlen(format) +
290 strlen(cs->name) + 1);
291 s_print(object, format, def->def_name, cs->name);
292 print_ifstat(2, cs->prefix, cs->type, cs->rel, cs->array_max,
296 f_print(fout, "\t\tbreak;\n");
298 dflt = def->def.un.default_decl;
300 if (!streq(dflt->type, "void")) {
301 f_print(fout, "\tdefault:\n");
303 alloc(strlen(def->def_name) + strlen(format) +
304 strlen(dflt->name) + 1);
305 s_print(object, format, def->def_name, dflt->name);
306 print_ifstat(2, dflt->prefix, dflt->type, dflt->rel,
307 dflt->array_max, object, dflt->name);
309 f_print(fout, "\t\tbreak;\n");
312 f_print(fout, "\tdefault:\n");
313 f_print(fout, "\t\treturn (FALSE);\n");
315 f_print(fout, "\t}\n");
321 emit_struct(definition * def)
325 for (dl = def->def.st.decls; dl != NULL; dl = dl->next) {
326 print_stat(&dl->decl);
334 emit_typedef(definition * def)
336 char *prefix = def->def.ty.old_prefix;
337 char *type = def->def.ty.old_type;
338 char *amax = def->def.ty.array_max;
339 relation rel = def->def.ty.rel;
341 print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
349 print_stat(declaration * dec)
351 char *prefix = dec->prefix;
352 char *type = dec->type;
353 char *amax = dec->array_max;
354 relation rel = dec->rel;
357 if (isvectordef(type, rel)) {
358 s_print(name, "objp->%s", dec->name);
360 s_print(name, "&objp->%s", dec->name);
362 print_ifstat(1, prefix, type, rel, amax, name, dec->name);
366 print_hout(declaration * dec)
372 s_print(prefix, "%s ", dec->prefix);
375 f_print(fout, "\ntypedef ");
378 f_print(fout, "struct %s {\n", dec->name);
379 f_print(fout, "\tu_int %s_len;\n", dec->name);
380 f_print(fout, "\t%s%s *%s_val;\n", prefix, dec->type, dec->name);
381 f_print(fout, "} %s", dec->name);
386 f_print(fout, ";\n");
387 f_print(fout, "bool_t xdr_%s(XDR *xdrs, %s *objp);\n", dec->name,
394 print_cout(declaration * dec)
398 f_print(fout, "bool_t\n");
399 f_print(fout, "xdr_%s(XDR *xdrs, %s *objp)\n", dec->name, dec->name);
400 f_print(fout, "{\n");
401 print_ifstat(1, dec->prefix, dec->type, dec->rel, dec->array_max,
409 print_rxifopen(char *typename)
411 sprintf(Proc_list->code, "xdr_%s(&z_xdrs", typename);
412 sprintf(Proc_list->scode, "xdr_%s(z_xdrs", typename);
417 print_rxifarg(char *amp, char *arg, int costant)
419 char code[100], scode[100];
421 sprintf(code, ", %s%s", amp, arg);
423 sprintf(scode, ", %s", arg);
425 sprintf(scode, ", &%s", arg);
426 strcat(Proc_list->code, code);
427 strcat(Proc_list->scode, scode);
432 print_rxifsizeof(char *prefix, char *type)
436 if (streq(type, "bool")) {
437 strcat(Proc_list->code, ", sizeof(bool_t), xdr_bool");
438 strcat(Proc_list->scode, ", sizeof(bool_t), xdr_bool");
440 strcat(Proc_list->code, ", sizeof(");
441 strcat(Proc_list->scode, ", sizeof(");
442 if (undefined(type) && prefix) {
443 sprintf(name, "%s ", prefix);
444 strcat(Proc_list->code, name);
445 strcat(Proc_list->scode, name);
447 sprintf(name, "%s), xdr_%s", type, type);
448 strcat(Proc_list->code, name);
449 strcat(Proc_list->scode, name);
455 print_param(declaration * dec)
457 char *prefix = dec->prefix;
458 char *type = dec->type;
459 char *amax = dec->array_max;
460 relation rel = dec->rel;
461 char *name = dec->name;
464 char *objname, *amp = "";
466 if (rel == REL_POINTER)
467 Proc_list->pl.param_flag |= INDIRECT_PARAM;
470 Proc_list->pl.param_flag &= ~INDIRECT_PARAM;
472 objname = Proc_list->pl.param_name;
475 print_rxifopen(type);
476 print_rxifarg(amp, objname, 0);
478 print_rxifopen("pointer");
479 print_rxifarg(amp, "(char **)", 1);
480 sprintf(temp, "%s", objname);
481 strcat(Proc_list->code, temp);
482 strcat(Proc_list->scode, temp);
483 print_rxifsizeof(prefix, type);
487 if (streq(type, "string")) {
489 } else if (streq(type, "opaque")) {
494 print_rxifarg(amp, objname, 0);
496 print_rxifopen("vector");
497 print_rxifarg(amp, "(char *)", 0);
498 sprintf(temp, "%s", objname);
499 strcat(Proc_list->code, temp);
500 strcat(Proc_list->scode, temp);
502 print_rxifarg("", amax, 1);
504 print_rxifsizeof(prefix, type);
508 if (streq(type, "string")) {
510 } else if (streq(type, "opaque")) {
513 if (streq(type, "string")) {
515 if ((Proc_list->pl.param_kind == DEF_OUTPARAM)
516 || (Proc_list->pl.param_kind == DEF_INOUTPARAM)) {
517 Proc_list->pl.param_flag |= OUT_STRING;
518 print_rxifarg("", objname, 0);
520 print_rxifarg("&", objname, 0);
521 /* print_rxifarg(amp, objname, 0); */
522 print_rxifarg("", amax, 1);
524 print_rxifsizeof(prefix, type);
527 char typecontents[100];
531 strcpy(temp, dec->name);
532 strcpy(typecontents, dec->name);
533 strcat(typecontents, " *");
534 strcpy(Proc_list->pl.param_type, typecontents);
535 sprintf(typecontents, "%s_%d", Proc_list->pl.param_name,
537 strcpy(Proc_list->pl.param_name, typecontents);
538 Proc_list->pl.param_flag |= FREETHIS_PARAM;
539 print_rxifopen(temp);
540 print_rxifarg(amp, name, 0);
546 print_rxifopen("array");
548 print_rxifarg(amp, "(char **)", 1);
549 if (*objname == '&') {
550 sprintf(temp, "%s.%s_val, (u_int *)%s.%s_len",
551 objname, name, objname, name);
553 sprintf(temp, "&%s->%s_val, (u_int *)&%s->%s_len",
554 objname, name, objname, name);
556 strcat(Proc_list->code, temp);
557 strcat(Proc_list->scode, temp);
559 print_rxifarg("", amax, 1);
561 print_rxifsizeof(prefix, type);
566 print_rxifopen(type);
567 print_rxifarg(amp, objname, 0);
570 strcat(Proc_list->code, ")");
571 strcat(Proc_list->scode, ")");