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>
51 #include "rpc_parse.h"
54 /* Static prototypes */
55 static int findtype(definition * def, char *type);
56 static int undefined(char *type);
57 static void print_header(definition * def);
58 static void print_trailer(void);
59 static void print_ifopen(int indent, char *name);
60 static void print_ifarg(char *arg);
61 static void print_ifsizeof(char *prefix, char *type);
62 static void print_ifclose(int indent);
63 static void space(void);
64 static void print_ifstat(int indent, char *prefix, char *type, relation rel,
65 char *amax, char *objname, char *name);
66 static void emit_enum(definition * def);
67 static void emit_union(definition * def);
68 static void emit_struct(definition * def);
69 static void emit_typedef(definition * def);
70 static void print_stat(declaration * dec);
71 static void print_hout(declaration * dec);
72 static void print_cout(declaration * dec);
73 static void print_rxifopen(char *typename);
74 static void print_rxifarg(char *amp, char *arg, int costant);
75 static void print_rxifsizeof(char *prefix, char *type);
79 * Emit the C-routine for the given definition
82 emit(definition * def)
84 if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) {
88 switch (def->def_kind) {
106 findtype(definition * def, char *type)
108 if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) {
111 return (streq(def->def_name, type));
116 undefined(char *type)
120 def = (definition *) FINDVAL(defined, type, findtype);
121 return (def == NULL);
126 print_header(definition * def)
129 f_print(fout, "bool_t\n");
130 f_print(fout, "xdr_%s(XDR *xdrs, ", def->def_name);
131 f_print(fout, "%s ", def->def_name);
133 if (def->def_kind != DEF_TYPEDEF
134 || !isvectordef(def->def.ty.old_type, def->def.ty.rel)) {
140 f_print(fout, "objp)\n");
141 f_print(fout, "{\n");
147 f_print(fout, "\treturn (TRUE);\n");
148 f_print(fout, "}\n");
154 print_ifopen(int indent, char *name)
156 tabify(fout, indent);
157 f_print(fout, "if (!xdr_%s(xdrs", name);
162 print_ifarg(char *arg)
164 f_print(fout, ", %s", arg);
169 print_ifsizeof(char *prefix, char *type)
171 if (streq(type, "bool")) {
172 f_print(fout, ", sizeof(bool_t), (xdrproc_t) xdr_bool");
174 f_print(fout, ", sizeof(");
175 if (undefined(type) && prefix) {
176 f_print(fout, "%s ", prefix);
178 f_print(fout, "%s), (xdrproc_t) xdr_%s", type, type);
183 print_ifclose(int indent)
185 f_print(fout, ")) {\n");
186 tabify(fout, indent);
187 f_print(fout, "\treturn (FALSE);\n");
188 tabify(fout, indent);
189 f_print(fout, "}\n");
195 f_print(fout, "\n\n");
199 print_ifstat(int indent, char *prefix, char *type, relation rel, char *amax,
200 char *objname, char *name)
206 print_ifopen(indent, "pointer");
207 print_ifarg("(char **)");
208 f_print(fout, "%s", objname);
209 print_ifsizeof(prefix, type);
212 if (streq(type, "string")) {
214 } else if (streq(type, "opaque")) {
218 print_ifopen(indent, alt);
219 print_ifarg(objname);
221 print_ifopen(indent, "vector");
222 print_ifarg("(char *)");
223 f_print(fout, "%s", objname);
227 print_ifsizeof(prefix, type);
231 if (streq(type, "string")) {
233 } else if (streq(type, "opaque")) {
236 if (streq(type, "string")) {
237 print_ifopen(indent, alt);
238 print_ifarg(objname);
241 print_ifopen(indent, alt);
243 print_ifopen(indent, "array");
245 print_ifarg("(char **)");
246 if (*objname == '&') {
247 f_print(fout, "%s.%s_val, (u_int *)%s.%s_len", objname, name,
250 f_print(fout, "&%s->%s_val, (u_int *)&%s->%s_len", objname,
251 name, objname, name);
256 print_ifsizeof(prefix, type);
260 print_ifopen(indent, type);
261 print_ifarg(objname);
264 print_ifclose(indent);
269 emit_enum(definition * def)
271 print_ifopen(1, "enum");
272 print_ifarg("(enum_t *)objp");
278 emit_union(definition * def)
284 char *format = "&objp->%s_u.%s";
286 print_stat(&def->def.un.enum_decl);
287 f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
288 for (cl = def->def.un.cases; cl != NULL; cl = cl->next) {
290 f_print(fout, "\tcase %s:\n", cl->case_name);
291 if (!streq(cs->type, "void")) {
293 alloc(strlen(def->def_name) + strlen(format) +
294 strlen(cs->name) + 1);
295 s_print(object, format, def->def_name, cs->name);
296 print_ifstat(2, cs->prefix, cs->type, cs->rel, cs->array_max,
300 f_print(fout, "\t\tbreak;\n");
302 dflt = def->def.un.default_decl;
304 if (!streq(dflt->type, "void")) {
305 f_print(fout, "\tdefault:\n");
307 alloc(strlen(def->def_name) + strlen(format) +
308 strlen(dflt->name) + 1);
309 s_print(object, format, def->def_name, dflt->name);
310 print_ifstat(2, dflt->prefix, dflt->type, dflt->rel,
311 dflt->array_max, object, dflt->name);
313 f_print(fout, "\t\tbreak;\n");
316 f_print(fout, "\tdefault:\n");
317 f_print(fout, "\t\treturn (FALSE);\n");
319 f_print(fout, "\t}\n");
325 emit_struct(definition * def)
329 for (dl = def->def.st.decls; dl != NULL; dl = dl->next) {
330 print_stat(&dl->decl);
338 emit_typedef(definition * def)
340 char *prefix = def->def.ty.old_prefix;
341 char *type = def->def.ty.old_type;
342 char *amax = def->def.ty.array_max;
343 relation rel = def->def.ty.rel;
345 print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
353 print_stat(declaration * dec)
355 char *prefix = dec->prefix;
356 char *type = dec->type;
357 char *amax = dec->array_max;
358 relation rel = dec->rel;
361 if (isvectordef(type, rel)) {
362 s_print(name, "objp->%s", dec->name);
364 s_print(name, "&objp->%s", dec->name);
366 print_ifstat(1, prefix, type, rel, amax, name, dec->name);
370 print_hout(declaration * dec)
376 s_print(prefix, "%s ", dec->prefix);
379 f_print(fout, "\ntypedef ");
382 f_print(fout, "struct %s {\n", dec->name);
383 f_print(fout, "\tu_int %s_len;\n", dec->name);
384 f_print(fout, "\t%s%s *%s_val;\n", prefix, dec->type, dec->name);
385 f_print(fout, "} %s", dec->name);
388 f_print(fout, ";\n");
389 f_print(fout, "bool_t xdr_%s(XDR *xdrs, %s *objp);\n", dec->name,
396 print_cout(declaration * dec)
400 f_print(fout, "bool_t\n");
401 f_print(fout, "xdr_%s(XDR *xdrs, %s *objp)\n", dec->name, dec->name);
402 f_print(fout, "{\n");
403 print_ifstat(1, dec->prefix, dec->type, dec->rel, dec->array_max,
411 print_rxifopen(char *typename)
413 sprintf(Proc_list->code, "xdr_%s(&z_xdrs", typename);
414 sprintf(Proc_list->scode, "xdr_%s(z_xdrs", typename);
419 print_rxifarg(char *amp, char *arg, int costant)
421 char code[100], scode[100];
423 sprintf(code, ", %s%s", amp, arg);
425 sprintf(scode, ", %s", arg);
427 sprintf(scode, ", &%s", arg);
428 strcat(Proc_list->code, code);
429 strcat(Proc_list->scode, scode);
434 print_rxifsizeof(char *prefix, char *type)
438 if (streq(type, "bool")) {
439 strcat(Proc_list->code, ", sizeof(bool_t), xdr_bool");
440 strcat(Proc_list->scode, ", sizeof(bool_t), xdr_bool");
442 strcat(Proc_list->code, ", sizeof(");
443 strcat(Proc_list->scode, ", sizeof(");
444 if (undefined(type) && prefix) {
445 sprintf(name, "%s ", prefix);
446 strcat(Proc_list->code, name);
447 strcat(Proc_list->scode, name);
449 sprintf(name, "%s), xdr_%s", type, type);
450 strcat(Proc_list->code, name);
451 strcat(Proc_list->scode, name);
457 print_param(declaration * dec)
459 char *prefix = dec->prefix;
460 char *type = dec->type;
461 char *amax = dec->array_max;
462 relation rel = dec->rel;
463 char *name = dec->name;
466 char *objname, *amp = "";
468 if (rel == REL_POINTER)
469 Proc_list->pl.param_flag |= INDIRECT_PARAM;
472 Proc_list->pl.param_flag &= ~INDIRECT_PARAM;
474 objname = Proc_list->pl.param_name;
477 print_rxifopen(type);
478 print_rxifarg(amp, objname, 0);
480 print_rxifopen("pointer");
481 print_rxifarg(amp, "(char **)", 1);
482 sprintf(temp, "%s", objname);
483 strcat(Proc_list->code, temp);
484 strcat(Proc_list->scode, temp);
485 print_rxifsizeof(prefix, type);
489 if (streq(type, "string")) {
491 } else if (streq(type, "opaque")) {
496 print_rxifarg(amp, objname, 0);
498 print_rxifopen("vector");
499 print_rxifarg(amp, "(char *)", 0);
500 sprintf(temp, "%s", objname);
501 strcat(Proc_list->code, temp);
502 strcat(Proc_list->scode, temp);
504 print_rxifarg("", amax, 1);
506 print_rxifsizeof(prefix, type);
510 if (streq(type, "string")) {
512 } else if (streq(type, "opaque")) {
515 if (streq(type, "string")) {
517 if ((Proc_list->pl.param_kind == DEF_OUTPARAM)
518 || (Proc_list->pl.param_kind == DEF_INOUTPARAM)) {
519 Proc_list->pl.param_flag |= OUT_STRING;
520 print_rxifarg("", objname, 0);
522 print_rxifarg("&", objname, 0);
523 /* print_rxifarg(amp, objname, 0); */
524 print_rxifarg("", amax, 1);
526 print_rxifsizeof(prefix, type);
529 char typecontents[100];
533 strcpy(temp, dec->name);
534 strcpy(typecontents, dec->name);
535 strcat(typecontents, " *");
536 strcpy(Proc_list->pl.param_type, typecontents);
537 sprintf(typecontents, "%s_%d", Proc_list->pl.param_name,
539 strcpy(Proc_list->pl.param_name, typecontents);
540 Proc_list->pl.param_flag |= FREETHIS_PARAM;
541 print_rxifopen(temp);
542 print_rxifarg(amp, name, 0);
548 print_rxifopen("array");
550 print_rxifarg(amp, "(char **)", 1);
551 if (*objname == '&') {
552 sprintf(temp, "%s.%s_val, (u_int *)%s.%s_len",
553 objname, name, objname, name);
555 sprintf(temp, "&%s->%s_val, (u_int *)&%s->%s_len",
556 objname, name, objname, name);
558 strcat(Proc_list->code, temp);
559 strcat(Proc_list->scode, temp);
561 print_rxifarg("", amax, 1);
563 print_rxifsizeof(prefix, type);
568 print_rxifopen(type);
569 print_rxifarg(amp, objname, 0);
572 strcat(Proc_list->code, ")");
573 strcat(Proc_list->scode, ")");