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) {
100 findtype(definition * def, char *type)
102 if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) {
105 return (streq(def->def_name, type));
110 undefined(char *type)
114 def = (definition *) FINDVAL(defined, type, findtype);
115 return (def == NULL);
120 print_header(definition * def)
123 f_print(fout, "bool_t\n");
124 f_print(fout, "xdr_%s(XDR *xdrs, ", def->def_name);
125 f_print(fout, "%s ", def->def_name);
127 if (def->def_kind != DEF_TYPEDEF
128 || !isvectordef(def->def.ty.old_type, def->def.ty.rel)) {
134 f_print(fout, "objp)\n");
135 f_print(fout, "{\n");
141 f_print(fout, "\treturn (TRUE);\n");
142 f_print(fout, "}\n");
148 print_ifopen(int indent, char *name)
150 tabify(fout, indent);
151 f_print(fout, "if (!xdr_%s(xdrs", name);
156 print_ifarg(char *arg)
158 f_print(fout, ", %s", arg);
163 print_ifsizeof(char *prefix, char *type)
165 if (streq(type, "bool")) {
166 f_print(fout, ", sizeof(bool_t), (xdrproc_t) xdr_bool");
168 f_print(fout, ", sizeof(");
169 if (undefined(type) && prefix) {
170 f_print(fout, "%s ", prefix);
172 f_print(fout, "%s), (xdrproc_t) xdr_%s", type, type);
177 print_ifclose(int indent)
179 f_print(fout, ")) {\n");
180 tabify(fout, indent);
181 f_print(fout, "\treturn (FALSE);\n");
182 tabify(fout, indent);
183 f_print(fout, "}\n");
189 f_print(fout, "\n\n");
193 print_ifstat(int indent, char *prefix, char *type, relation rel, char *amax,
194 char *objname, char *name)
200 print_ifopen(indent, "pointer");
201 print_ifarg("(char **)");
202 f_print(fout, "%s", objname);
203 print_ifsizeof(prefix, type);
206 if (streq(type, "string")) {
208 } else if (streq(type, "opaque")) {
212 print_ifopen(indent, alt);
213 print_ifarg(objname);
215 print_ifopen(indent, "vector");
216 print_ifarg("(char *)");
217 f_print(fout, "%s", objname);
221 print_ifsizeof(prefix, type);
225 if (streq(type, "string")) {
227 } else if (streq(type, "opaque")) {
230 if (streq(type, "string")) {
231 print_ifopen(indent, alt);
232 print_ifarg(objname);
235 print_ifopen(indent, alt);
237 print_ifopen(indent, "array");
239 print_ifarg("(char **)");
240 if (*objname == '&') {
241 f_print(fout, "%s.%s_val, (u_int *)%s.%s_len", objname, name,
244 f_print(fout, "&%s->%s_val, (u_int *)&%s->%s_len", objname,
245 name, objname, name);
250 print_ifsizeof(prefix, type);
254 print_ifopen(indent, type);
255 print_ifarg(objname);
258 print_ifclose(indent);
263 emit_enum(definition * def)
265 print_ifopen(1, "enum");
266 print_ifarg("(enum_t *)objp");
272 emit_union(definition * def)
278 char *format = "&objp->%s_u.%s";
280 print_stat(&def->def.un.enum_decl);
281 f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
282 for (cl = def->def.un.cases; cl != NULL; cl = cl->next) {
284 f_print(fout, "\tcase %s:\n", cl->case_name);
285 if (!streq(cs->type, "void")) {
287 alloc(strlen(def->def_name) + strlen(format) +
288 strlen(cs->name) + 1);
289 s_print(object, format, def->def_name, cs->name);
290 print_ifstat(2, cs->prefix, cs->type, cs->rel, cs->array_max,
294 f_print(fout, "\t\tbreak;\n");
296 dflt = def->def.un.default_decl;
298 if (!streq(dflt->type, "void")) {
299 f_print(fout, "\tdefault:\n");
301 alloc(strlen(def->def_name) + strlen(format) +
302 strlen(dflt->name) + 1);
303 s_print(object, format, def->def_name, dflt->name);
304 print_ifstat(2, dflt->prefix, dflt->type, dflt->rel,
305 dflt->array_max, object, dflt->name);
307 f_print(fout, "\t\tbreak;\n");
310 f_print(fout, "\tdefault:\n");
311 f_print(fout, "\t\treturn (FALSE);\n");
313 f_print(fout, "\t}\n");
319 emit_struct(definition * def)
323 for (dl = def->def.st.decls; dl != NULL; dl = dl->next) {
324 print_stat(&dl->decl);
332 emit_typedef(definition * def)
334 char *prefix = def->def.ty.old_prefix;
335 char *type = def->def.ty.old_type;
336 char *amax = def->def.ty.array_max;
337 relation rel = def->def.ty.rel;
339 print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
347 print_stat(declaration * dec)
349 char *prefix = dec->prefix;
350 char *type = dec->type;
351 char *amax = dec->array_max;
352 relation rel = dec->rel;
355 if (isvectordef(type, rel)) {
356 s_print(name, "objp->%s", dec->name);
358 s_print(name, "&objp->%s", dec->name);
360 print_ifstat(1, prefix, type, rel, amax, name, dec->name);
364 print_hout(declaration * dec)
370 s_print(prefix, "%s ", dec->prefix);
373 f_print(fout, "\ntypedef ");
376 f_print(fout, "struct %s {\n", dec->name);
377 f_print(fout, "\tu_int %s_len;\n", dec->name);
378 f_print(fout, "\t%s%s *%s_val;\n", prefix, dec->type, dec->name);
379 f_print(fout, "} %s", dec->name);
382 f_print(fout, ";\n");
383 f_print(fout, "bool_t xdr_%s(XDR *xdrs, %s *objp);\n", dec->name,
390 print_cout(declaration * dec)
394 f_print(fout, "bool_t\n");
395 f_print(fout, "xdr_%s(XDR *xdrs, %s *objp)\n", dec->name, dec->name);
396 f_print(fout, "{\n");
397 print_ifstat(1, dec->prefix, dec->type, dec->rel, dec->array_max,
405 print_rxifopen(char *typename)
407 sprintf(Proc_list->code, "xdr_%s(&z_xdrs", typename);
408 sprintf(Proc_list->scode, "xdr_%s(z_xdrs", typename);
413 print_rxifarg(char *amp, char *arg, int costant)
415 char code[100], scode[100];
417 sprintf(code, ", %s%s", amp, arg);
419 sprintf(scode, ", %s", arg);
421 sprintf(scode, ", &%s", arg);
422 strcat(Proc_list->code, code);
423 strcat(Proc_list->scode, scode);
428 print_rxifsizeof(char *prefix, char *type)
432 if (streq(type, "bool")) {
433 strcat(Proc_list->code, ", sizeof(bool_t), xdr_bool");
434 strcat(Proc_list->scode, ", sizeof(bool_t), xdr_bool");
436 strcat(Proc_list->code, ", sizeof(");
437 strcat(Proc_list->scode, ", sizeof(");
438 if (undefined(type) && prefix) {
439 sprintf(name, "%s ", prefix);
440 strcat(Proc_list->code, name);
441 strcat(Proc_list->scode, name);
443 sprintf(name, "%s), xdr_%s", type, type);
444 strcat(Proc_list->code, name);
445 strcat(Proc_list->scode, name);
451 print_param(declaration * dec)
453 char *prefix = dec->prefix;
454 char *type = dec->type;
455 char *amax = dec->array_max;
456 relation rel = dec->rel;
457 char *name = dec->name;
460 char *objname, *amp = "";
462 if (rel == REL_POINTER)
463 Proc_list->pl.param_flag |= INDIRECT_PARAM;
466 Proc_list->pl.param_flag &= ~INDIRECT_PARAM;
468 objname = Proc_list->pl.param_name;
471 print_rxifopen(type);
472 print_rxifarg(amp, objname, 0);
474 print_rxifopen("pointer");
475 print_rxifarg(amp, "(char **)", 1);
476 sprintf(temp, "%s", objname);
477 strcat(Proc_list->code, temp);
478 strcat(Proc_list->scode, temp);
479 print_rxifsizeof(prefix, type);
483 if (streq(type, "string")) {
485 } else if (streq(type, "opaque")) {
490 print_rxifarg(amp, objname, 0);
492 print_rxifopen("vector");
493 print_rxifarg(amp, "(char *)", 0);
494 sprintf(temp, "%s", objname);
495 strcat(Proc_list->code, temp);
496 strcat(Proc_list->scode, temp);
498 print_rxifarg("", amax, 1);
500 print_rxifsizeof(prefix, type);
504 if (streq(type, "string")) {
506 } else if (streq(type, "opaque")) {
509 if (streq(type, "string")) {
511 if ((Proc_list->pl.param_kind == DEF_OUTPARAM)
512 || (Proc_list->pl.param_kind == DEF_INOUTPARAM)) {
513 Proc_list->pl.param_flag |= OUT_STRING;
514 print_rxifarg("", objname, 0);
516 print_rxifarg("&", objname, 0);
517 /* print_rxifarg(amp, objname, 0); */
518 print_rxifarg("", amax, 1);
520 print_rxifsizeof(prefix, type);
523 char typecontents[100];
527 strcpy(temp, dec->name);
528 strcpy(typecontents, dec->name);
529 strcat(typecontents, " *");
530 strcpy(Proc_list->pl.param_type, typecontents);
531 sprintf(typecontents, "%s_%d", Proc_list->pl.param_name,
533 strcpy(Proc_list->pl.param_name, typecontents);
534 Proc_list->pl.param_flag |= FREETHIS_PARAM;
535 print_rxifopen(temp);
536 print_rxifarg(amp, name, 0);
542 print_rxifopen("array");
544 print_rxifarg(amp, "(char **)", 1);
545 if (*objname == '&') {
546 sprintf(temp, "%s.%s_val, (u_int *)%s.%s_len",
547 objname, name, objname, name);
549 sprintf(temp, "&%s->%s_val, (u_int *)&%s->%s_len",
550 objname, name, objname, name);
552 strcat(Proc_list->code, temp);
553 strcat(Proc_list->scode, temp);
555 print_rxifarg("", amax, 1);
557 print_rxifsizeof(prefix, type);
562 print_rxifopen(type);
563 print_rxifarg(amp, objname, 0);
566 strcat(Proc_list->code, ")");
567 strcat(Proc_list->scode, ")");