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_PROGRAM || def->def_kind == DEF_CONST) {
79 switch (def->def_kind) {
99 findtype(definition * def, char *type)
101 if (def->def_kind == DEF_PROGRAM || 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 f_print(fout, ptr_to ? ", (%s *) %s" : ", (%s) %s", type, arg);
168 print_ifsizeof(char *prefix, char *type)
170 if (streq(type, "bool")) {
171 f_print(fout, ", sizeof(bool_t), (xdrproc_t) xdr_bool");
173 f_print(fout, ", sizeof(");
174 if (undefined(type) && prefix) {
175 f_print(fout, "%s ", prefix);
177 f_print(fout, "%s), (xdrproc_t) xdr_%s", type, type);
182 print_ifclose(int indent)
184 f_print(fout, ")) {\n");
185 tabify(fout, indent);
186 f_print(fout, "\treturn (FALSE);\n");
187 tabify(fout, indent);
188 f_print(fout, "}\n");
194 f_print(fout, "\n\n");
198 print_ifstat(int indent, char *prefix, char *type, relation rel, char *amax,
199 char *objname, char *name)
202 char *altcast = NULL;
206 print_ifopen(indent, "pointer");
207 print_ifarg_with_cast(1, "char *", objname);
208 print_ifsizeof(prefix, type);
211 if (streq(type, "string")) {
213 } else if (streq(type, "opaque")) {
218 print_ifopen(indent, alt);
220 print_ifarg_with_cast(0, altcast, objname);
222 print_ifarg(objname);
225 print_ifopen(indent, "vector");
226 print_ifarg_with_cast(1, "char", objname);
230 print_ifsizeof(prefix, type);
234 if (streq(type, "string")) {
236 } else if (streq(type, "opaque")) {
239 if (streq(type, "string")) {
240 print_ifopen(indent, alt);
241 print_ifarg(objname);
244 print_ifopen(indent, alt);
245 print_ifarg("(char **)");
247 print_ifopen(indent, "array");
248 print_ifarg("(caddr_t *)");
250 if (*objname == '&') {
252 f_print(fout, "%s.val, (u_int *)%s.len", objname, objname);
254 f_print(fout, "%s.%s_val, (u_int *)%s.%s_len",
255 objname, name, objname, name);
259 f_print(fout, "&%s->val, (u_int *)&%s->len",
262 f_print(fout, "&%s->%s_val, (u_int *)&%s->%s_len",
263 objname, name, objname, name);
269 print_ifsizeof(prefix, type);
273 print_ifopen(indent, type);
274 print_ifarg_with_cast(1, type, objname);
277 print_ifclose(indent);
282 emit_enum(definition * def)
284 print_ifopen(1, "enum");
285 print_ifarg("(enum_t *)objp");
291 emit_union(definition * def)
297 char *format = "&objp->%s_u.%s";
298 char *briefformat = "&objp->u.%s";
300 print_stat(&def->def.un.enum_decl);
301 f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
302 for (cl = def->def.un.cases; cl != NULL; cl = cl->next) {
304 f_print(fout, "\tcase %s:\n", cl->case_name);
305 if (!streq(cs->type, "void")) {
307 alloc(strlen(def->def_name) + strlen(format) +
308 strlen(cs->name) + 1);
311 s_print(object, briefformat, cs->name);
313 s_print(object, format, def->def_name, cs->name);
315 print_ifstat(2, cs->prefix, cs->type, cs->rel, cs->array_max,
319 f_print(fout, "\t\tbreak;\n");
321 dflt = def->def.un.default_decl;
323 if (!streq(dflt->type, "void")) {
324 f_print(fout, "\tdefault:\n");
326 alloc(strlen(def->def_name) + strlen(format) +
327 strlen(dflt->name) + 1);
328 s_print(object, format, def->def_name, dflt->name);
329 print_ifstat(2, dflt->prefix, dflt->type, dflt->rel,
330 dflt->array_max, object, dflt->name);
332 f_print(fout, "\t\tbreak;\n");
335 f_print(fout, "\tdefault:\n");
336 f_print(fout, "\t\treturn (FALSE);\n");
338 f_print(fout, "\t}\n");
344 emit_struct(definition * def)
348 for (dl = def->def.st.decls; dl != NULL; dl = dl->next) {
349 print_stat(&dl->decl);
357 emit_typedef(definition * def)
359 char *prefix = def->def.ty.old_prefix;
360 char *type = def->def.ty.old_type;
361 char *amax = def->def.ty.array_max;
362 relation rel = def->def.ty.rel;
364 print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
372 print_stat(declaration * dec)
374 char *prefix = dec->prefix;
375 char *type = dec->type;
376 char *amax = dec->array_max;
377 relation rel = dec->rel;
380 if (isvectordef(type, rel)) {
381 s_print(name, "objp->%s", dec->name);
383 s_print(name, "&objp->%s", dec->name);
385 print_ifstat(1, prefix, type, rel, amax, name, dec->name);
389 print_hout(declaration * dec)
395 s_print(prefix, "%s ", dec->prefix);
398 f_print(fout, "\ntypedef ");
401 f_print(fout, "struct %s {\n", dec->name);
403 f_print(fout, "\tu_int %s_len;\n", dec->name);
404 f_print(fout, "\t%s%s *%s_val;\n", prefix,
405 dec->type, dec->name);
407 f_print(fout, "\tu_int %s_len;\n", dec->name);
408 f_print(fout, "\t%s%s *%s_val;\n", prefix,
409 dec->type, dec->name);
411 f_print(fout, "} %s", dec->name);
416 f_print(fout, ";\n");
417 f_print(fout, "bool_t xdr_%s(XDR *xdrs, %s *objp);\n", dec->name,
424 print_cout(declaration * dec)
428 f_print(fout, "bool_t\n");
429 f_print(fout, "xdr_%s(XDR *xdrs, %s *objp)\n", dec->name, dec->name);
430 f_print(fout, "{\n");
431 print_ifstat(1, dec->prefix, dec->type, dec->rel, dec->array_max,
439 print_rxifopen(char *typename)
441 sprintf(Proc_list->code, "xdr_%s(&z_xdrs", typename);
442 sprintf(Proc_list->scode, "xdr_%s(z_xdrs", typename);
447 print_rxifarg(char *amp, char *arg, int costant)
449 char code[100], scode[100];
451 sprintf(code, ", %s%s", amp, arg);
453 sprintf(scode, ", %s", arg);
455 sprintf(scode, ", &%s", arg);
456 strcat(Proc_list->code, code);
457 strcat(Proc_list->scode, scode);
462 print_rxifsizeof(char *prefix, char *type)
466 if (streq(type, "bool")) {
467 strcat(Proc_list->code, ", sizeof(bool_t), xdr_bool");
468 strcat(Proc_list->scode, ", sizeof(bool_t), xdr_bool");
470 strcat(Proc_list->code, ", sizeof(");
471 strcat(Proc_list->scode, ", sizeof(");
472 if (undefined(type) && prefix) {
473 sprintf(name, "%s ", prefix);
474 strcat(Proc_list->code, name);
475 strcat(Proc_list->scode, name);
477 sprintf(name, "%s), xdr_%s", type, type);
478 strcat(Proc_list->code, name);
479 strcat(Proc_list->scode, name);
485 print_param(declaration * dec)
487 char *prefix = dec->prefix;
488 char *type = dec->type;
489 char *amax = dec->array_max;
490 relation rel = dec->rel;
491 char *name = dec->name;
494 char *objname, *amp = "";
496 if (rel == REL_POINTER)
497 Proc_list->pl.param_flag |= INDIRECT_PARAM;
500 Proc_list->pl.param_flag &= ~INDIRECT_PARAM;
502 objname = Proc_list->pl.param_name;
505 print_rxifopen(type);
506 print_rxifarg(amp, objname, 0);
508 print_rxifopen("pointer");
509 print_rxifarg(amp, "(char **)", 1);
510 sprintf(temp, "%s", objname);
511 strcat(Proc_list->code, temp);
512 strcat(Proc_list->scode, temp);
513 print_rxifsizeof(prefix, type);
517 if (streq(type, "string")) {
519 } else if (streq(type, "opaque")) {
525 print_rxifopen("vector");
527 print_rxifarg(amp, "(char *)", 0);
528 sprintf(temp, "%s", objname);
529 strcat(Proc_list->code, temp);
530 strcat(Proc_list->scode, temp);
531 print_rxifarg("", amax, 1);
533 print_rxifsizeof(prefix, type);
537 if (streq(type, "string")) {
539 } else if (streq(type, "opaque")) {
542 if (streq(type, "string")) {
544 if ((Proc_list->pl.param_kind == DEF_OUTPARAM)
545 || (Proc_list->pl.param_kind == DEF_INOUTPARAM)) {
546 Proc_list->pl.param_flag |= OUT_STRING;
547 print_rxifarg("", objname, 0);
549 print_rxifarg("(char **) &", objname, 0);
550 /* print_rxifarg(amp, objname, 0); */
551 print_rxifarg("", amax, 1);
553 print_rxifsizeof(prefix, type);
556 char typecontents[100];
560 strcpy(temp, dec->name);
561 strcpy(typecontents, dec->name);
562 strcat(typecontents, " *");
563 strcpy(Proc_list->pl.param_type, typecontents);
564 sprintf(typecontents, "%s_%d", Proc_list->pl.param_name,
566 strcpy(Proc_list->pl.param_name, typecontents);
567 Proc_list->pl.param_flag |= FREETHIS_PARAM;
568 print_rxifopen(temp);
569 print_rxifarg(amp, name, 0);
575 print_rxifopen("array");
577 print_rxifarg(amp, "(char **)", 1);
578 if (*objname == '&') {
579 sprintf(temp, "%s.%s_val, (u_int *)%s.%s_len",
580 objname, name, objname, name);
582 sprintf(temp, "&%s->%s_val, (u_int *)&%s->%s_len",
583 objname, name, objname, name);
585 strcat(Proc_list->code, temp);
586 strcat(Proc_list->scode, temp);
588 print_rxifarg("", amax, 1);
590 print_rxifsizeof(prefix, type);
595 print_rxifopen(type);
596 print_rxifarg(amp, objname, 0);
599 strcat(Proc_list->code, ")");
600 strcat(Proc_list->scode, ")");