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>
43 #include "rpc_parse.h"
46 /* Static prototypes */
47 static int findtype(definition * def, char *type);
48 static int undefined(char *type);
49 static void print_header(definition * def);
50 static void print_trailer(void);
51 static void print_ifopen(int indent, char *name);
52 static void print_ifarg(char *arg);
53 static void print_ifarg_with_cast(int ptr_to, char *type, char *arg);
54 static void print_ifsizeof(char *prefix, char *type);
55 static void print_ifclose(int indent);
56 static void space(void);
57 static void print_ifstat(int indent, char *prefix, char *type, relation rel,
58 char *amax, char *objname, char *name);
59 static void emit_enum(definition * def);
60 static void emit_union(definition * def);
61 static void emit_struct(definition * def);
62 static void emit_typedef(definition * def);
63 static void print_stat(declaration * dec);
64 static void print_hout(declaration * dec);
65 static void print_cout(declaration * dec);
66 static void print_rxifopen(char *typename);
67 static void print_rxifarg(char *amp, char *arg, int costant);
68 static void print_rxifsizeof(char *prefix, char *type);
72 * Emit the C-routine for the given definition
75 emit(definition * def)
77 if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) {
81 switch (def->def_kind) {
101 findtype(definition * def, char *type)
103 if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) {
106 return (streq(def->def_name, type));
111 undefined(char *type)
115 def = (definition *) FINDVAL(defined, type, findtype);
116 return (def == NULL);
121 print_header(definition * def)
124 f_print(fout, "bool_t\n");
125 f_print(fout, "xdr_%s(XDR *xdrs, ", def->def_name);
126 f_print(fout, "%s ", def->def_name);
128 if (def->def_kind != DEF_TYPEDEF
129 || !isvectordef(def->def.ty.old_type, def->def.ty.rel)) {
135 f_print(fout, "objp)\n");
136 f_print(fout, "{\n");
142 f_print(fout, "\treturn (TRUE);\n");
143 f_print(fout, "}\n");
149 print_ifopen(int indent, char *name)
151 tabify(fout, indent);
152 f_print(fout, "if (!xdr_%s(xdrs", name);
157 print_ifarg(char *arg)
159 f_print(fout, ", %s", arg);
164 print_ifarg_with_cast(int ptr_to, char *type, char *arg)
166 f_print(fout, ptr_to ? ", (%s *) %s" : ", (%s) %s", type, arg);
170 print_ifsizeof(char *prefix, char *type)
172 if (streq(type, "bool")) {
173 f_print(fout, ", sizeof(bool_t), (xdrproc_t) xdr_bool");
175 f_print(fout, ", sizeof(");
176 if (undefined(type) && prefix) {
177 f_print(fout, "%s ", prefix);
179 f_print(fout, "%s), (xdrproc_t) xdr_%s", type, type);
184 print_ifclose(int indent)
186 f_print(fout, ")) {\n");
187 tabify(fout, indent);
188 f_print(fout, "\treturn (FALSE);\n");
189 tabify(fout, indent);
190 f_print(fout, "}\n");
196 f_print(fout, "\n\n");
200 print_ifstat(int indent, char *prefix, char *type, relation rel, char *amax,
201 char *objname, char *name)
204 char *altcast = NULL;
208 print_ifopen(indent, "pointer");
209 print_ifarg_with_cast(1, "char *", objname);
210 print_ifsizeof(prefix, type);
213 if (streq(type, "string")) {
215 } else if (streq(type, "opaque")) {
220 print_ifopen(indent, alt);
222 print_ifarg_with_cast(0, altcast, objname);
224 print_ifarg(objname);
227 print_ifopen(indent, "vector");
228 print_ifarg_with_cast(1, "char", objname);
232 print_ifsizeof(prefix, type);
236 if (streq(type, "string")) {
238 } else if (streq(type, "opaque")) {
241 if (streq(type, "string")) {
242 print_ifopen(indent, alt);
243 print_ifarg(objname);
246 print_ifopen(indent, alt);
247 print_ifarg("(char **)");
249 print_ifopen(indent, "array");
250 print_ifarg("(caddr_t *)");
252 if (*objname == '&') {
253 f_print(fout, "%s.%s_val, (u_int *)%s.%s_len", objname, name,
256 f_print(fout, "&%s->%s_val, (u_int *)&%s->%s_len", objname,
257 name, objname, name);
262 print_ifsizeof(prefix, type);
266 print_ifopen(indent, type);
267 print_ifarg_with_cast(1, type, objname);
270 print_ifclose(indent);
275 emit_enum(definition * def)
277 print_ifopen(1, "enum");
278 print_ifarg("(enum_t *)objp");
284 emit_union(definition * def)
290 char *format = "&objp->%s_u.%s";
292 print_stat(&def->def.un.enum_decl);
293 f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
294 for (cl = def->def.un.cases; cl != NULL; cl = cl->next) {
296 f_print(fout, "\tcase %s:\n", cl->case_name);
297 if (!streq(cs->type, "void")) {
299 alloc(strlen(def->def_name) + strlen(format) +
300 strlen(cs->name) + 1);
301 s_print(object, format, def->def_name, cs->name);
302 print_ifstat(2, cs->prefix, cs->type, cs->rel, cs->array_max,
306 f_print(fout, "\t\tbreak;\n");
308 dflt = def->def.un.default_decl;
310 if (!streq(dflt->type, "void")) {
311 f_print(fout, "\tdefault:\n");
313 alloc(strlen(def->def_name) + strlen(format) +
314 strlen(dflt->name) + 1);
315 s_print(object, format, def->def_name, dflt->name);
316 print_ifstat(2, dflt->prefix, dflt->type, dflt->rel,
317 dflt->array_max, object, dflt->name);
319 f_print(fout, "\t\tbreak;\n");
322 f_print(fout, "\tdefault:\n");
323 f_print(fout, "\t\treturn (FALSE);\n");
325 f_print(fout, "\t}\n");
331 emit_struct(definition * def)
335 for (dl = def->def.st.decls; dl != NULL; dl = dl->next) {
336 print_stat(&dl->decl);
344 emit_typedef(definition * def)
346 char *prefix = def->def.ty.old_prefix;
347 char *type = def->def.ty.old_type;
348 char *amax = def->def.ty.array_max;
349 relation rel = def->def.ty.rel;
351 print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
359 print_stat(declaration * dec)
361 char *prefix = dec->prefix;
362 char *type = dec->type;
363 char *amax = dec->array_max;
364 relation rel = dec->rel;
367 if (isvectordef(type, rel)) {
368 s_print(name, "objp->%s", dec->name);
370 s_print(name, "&objp->%s", dec->name);
372 print_ifstat(1, prefix, type, rel, amax, name, dec->name);
376 print_hout(declaration * dec)
382 s_print(prefix, "%s ", dec->prefix);
385 f_print(fout, "\ntypedef ");
388 f_print(fout, "struct %s {\n", dec->name);
389 f_print(fout, "\tu_int %s_len;\n", dec->name);
390 f_print(fout, "\t%s%s *%s_val;\n", prefix, dec->type, dec->name);
391 f_print(fout, "} %s", dec->name);
396 f_print(fout, ";\n");
397 f_print(fout, "bool_t xdr_%s(XDR *xdrs, %s *objp);\n", dec->name,
404 print_cout(declaration * dec)
408 f_print(fout, "bool_t\n");
409 f_print(fout, "xdr_%s(XDR *xdrs, %s *objp)\n", dec->name, dec->name);
410 f_print(fout, "{\n");
411 print_ifstat(1, dec->prefix, dec->type, dec->rel, dec->array_max,
419 print_rxifopen(char *typename)
421 sprintf(Proc_list->code, "xdr_%s(&z_xdrs", typename);
422 sprintf(Proc_list->scode, "xdr_%s(z_xdrs", typename);
427 print_rxifarg(char *amp, char *arg, int costant)
429 char code[100], scode[100];
431 sprintf(code, ", %s%s", amp, arg);
433 sprintf(scode, ", %s", arg);
435 sprintf(scode, ", &%s", arg);
436 strcat(Proc_list->code, code);
437 strcat(Proc_list->scode, scode);
442 print_rxifsizeof(char *prefix, char *type)
446 if (streq(type, "bool")) {
447 strcat(Proc_list->code, ", sizeof(bool_t), xdr_bool");
448 strcat(Proc_list->scode, ", sizeof(bool_t), xdr_bool");
450 strcat(Proc_list->code, ", sizeof(");
451 strcat(Proc_list->scode, ", sizeof(");
452 if (undefined(type) && prefix) {
453 sprintf(name, "%s ", prefix);
454 strcat(Proc_list->code, name);
455 strcat(Proc_list->scode, name);
457 sprintf(name, "%s), xdr_%s", type, type);
458 strcat(Proc_list->code, name);
459 strcat(Proc_list->scode, name);
465 print_param(declaration * dec)
467 char *prefix = dec->prefix;
468 char *type = dec->type;
469 char *amax = dec->array_max;
470 relation rel = dec->rel;
471 char *name = dec->name;
474 char *objname, *amp = "";
476 if (rel == REL_POINTER)
477 Proc_list->pl.param_flag |= INDIRECT_PARAM;
480 Proc_list->pl.param_flag &= ~INDIRECT_PARAM;
482 objname = Proc_list->pl.param_name;
485 print_rxifopen(type);
486 print_rxifarg(amp, objname, 0);
488 print_rxifopen("pointer");
489 print_rxifarg(amp, "(char **)", 1);
490 sprintf(temp, "%s", objname);
491 strcat(Proc_list->code, temp);
492 strcat(Proc_list->scode, temp);
493 print_rxifsizeof(prefix, type);
497 if (streq(type, "string")) {
499 } else if (streq(type, "opaque")) {
504 print_rxifarg(amp, objname, 0);
506 print_rxifopen("vector");
507 print_rxifarg(amp, "(char *)", 0);
508 sprintf(temp, "%s", objname);
509 strcat(Proc_list->code, temp);
510 strcat(Proc_list->scode, temp);
512 print_rxifarg("", amax, 1);
514 print_rxifsizeof(prefix, type);
518 if (streq(type, "string")) {
520 } else if (streq(type, "opaque")) {
523 if (streq(type, "string")) {
525 if ((Proc_list->pl.param_kind == DEF_OUTPARAM)
526 || (Proc_list->pl.param_kind == DEF_INOUTPARAM)) {
527 Proc_list->pl.param_flag |= OUT_STRING;
528 print_rxifarg("", objname, 0);
530 print_rxifarg("&", objname, 0);
531 /* print_rxifarg(amp, objname, 0); */
532 print_rxifarg("", amax, 1);
534 print_rxifsizeof(prefix, type);
537 char typecontents[100];
541 strcpy(temp, dec->name);
542 strcpy(typecontents, dec->name);
543 strcat(typecontents, " *");
544 strcpy(Proc_list->pl.param_type, typecontents);
545 sprintf(typecontents, "%s_%d", Proc_list->pl.param_name,
547 strcpy(Proc_list->pl.param_name, typecontents);
548 Proc_list->pl.param_flag |= FREETHIS_PARAM;
549 print_rxifopen(temp);
550 print_rxifarg(amp, name, 0);
556 print_rxifopen("array");
558 print_rxifarg(amp, "(char **)", 1);
559 if (*objname == '&') {
560 sprintf(temp, "%s.%s_val, (u_int *)%s.%s_len",
561 objname, name, objname, name);
563 sprintf(temp, "&%s->%s_val, (u_int *)&%s->%s_len",
564 objname, name, objname, name);
566 strcat(Proc_list->code, temp);
567 strcat(Proc_list->scode, temp);
569 print_rxifarg("", amax, 1);
571 print_rxifsizeof(prefix, type);
576 print_rxifopen(type);
577 print_rxifarg(amp, objname, 0);
580 strcat(Proc_list->code, ")");
581 strcat(Proc_list->scode, ")");