home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / NETKIT-B.05 / NETKIT-B / NetKit-B-0.05 / rpcgen / rpc_cout.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-17  |  7.3 KB  |  356 lines

  1. /* @(#)rpc_cout.c    2.1 88/08/01 4.0 RPCSRC */
  2. /*
  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.
  9.  * 
  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.
  13.  * 
  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.
  17.  * 
  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.
  21.  * 
  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.
  25.  * 
  26.  * Sun Microsystems, Inc.
  27.  * 2550 Garcia Avenue
  28.  * Mountain View, California  94043
  29.  */
  30. #ifndef lint
  31. /*static char sccsid[] = "from: @(#)rpc_cout.c 1.8 87/06/24 (C) 1987 SMI";*/
  32. static char rcsid[] = "$Id: rpc_cout.c,v 1.3 1993/08/01 18:09:22 mycroft Exp $";
  33. #endif
  34.  
  35. /*
  36.  * rpc_cout.c, XDR routine outputter for the RPC protocol compiler 
  37.  * Copyright (C) 1987, Sun Microsystems, Inc. 
  38.  */
  39. #include <stdio.h>
  40. #include <strings.h>
  41. #include "rpc_util.h"
  42. #include "rpc_parse.h"
  43.  
  44. static int print_header(), print_trailer(), space(), emit_enum(),
  45.        emit_union(), emit_struct(), emit_typedef(), print_stat();
  46.  
  47.  
  48. /*
  49.  * Emit the C-routine for the given definition 
  50.  */
  51. void
  52. emit(def)
  53.     definition *def;
  54. {
  55.     if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) {
  56.         return;
  57.     }
  58.     print_header(def);
  59.     switch (def->def_kind) {
  60.     case DEF_UNION:
  61.         emit_union(def);
  62.         break;
  63.     case DEF_ENUM:
  64.         emit_enum(def);
  65.         break;
  66.     case DEF_STRUCT:
  67.         emit_struct(def);
  68.         break;
  69.     case DEF_TYPEDEF:
  70.         emit_typedef(def);
  71.         break;
  72.     }
  73.     print_trailer();
  74. }
  75.  
  76. static
  77. findtype(def, type)
  78.     definition *def;
  79.     char *type;
  80. {
  81.     if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) {
  82.         return (0);
  83.     } else {
  84.         return (streq(def->def_name, type));
  85.     }
  86. }
  87.  
  88. static
  89. undefined(type)
  90.     char *type;
  91. {
  92.     definition *def;
  93.  
  94.     def = (definition *) FINDVAL(defined, type, findtype);
  95.     return (def == NULL);
  96. }
  97.  
  98.  
  99. static
  100. print_header(def)
  101.     definition *def;
  102. {
  103.     space();
  104.     f_print(fout, "bool_t\n");
  105.     f_print(fout, "xdr_%s(xdrs, objp)\n", def->def_name);
  106.     f_print(fout, "\tXDR *xdrs;\n");
  107.     f_print(fout, "\t%s ", def->def_name);
  108.     if (def->def_kind != DEF_TYPEDEF ||
  109.         !isvectordef(def->def.ty.old_type, def->def.ty.rel)) {
  110.         f_print(fout, "*");
  111.     }
  112.     f_print(fout, "objp;\n");
  113.     f_print(fout, "{\n");
  114. }
  115.  
  116. static
  117. print_trailer()
  118. {
  119.     f_print(fout, "\treturn (TRUE);\n");
  120.     f_print(fout, "}\n");
  121.     space();
  122. }
  123.  
  124.  
  125. static
  126. print_ifopen(indent, name)
  127.     int indent;
  128.     char *name;
  129. {
  130.     tabify(fout, indent);
  131.     f_print(fout, "if (!xdr_%s(xdrs", name);
  132. }
  133.  
  134.  
  135. static
  136. print_ifarg(arg)
  137.     char *arg;
  138. {
  139.     f_print(fout, ", %s", arg);
  140. }
  141.  
  142.  
  143. static
  144. print_ifsizeof(prefix, type)
  145.     char *prefix;
  146.     char *type;
  147. {
  148.     if (streq(type, "bool")) {
  149.         f_print(fout, ", sizeof(bool_t), xdr_bool");
  150.     } else {
  151.         f_print(fout, ", sizeof(");
  152.         if (undefined(type) && prefix) {
  153.             f_print(fout, "%s ", prefix);
  154.         }
  155.         f_print(fout, "%s), xdr_%s", type, type);
  156.     }
  157. }
  158.  
  159. static
  160. print_ifclose(indent)
  161.     int indent;
  162. {
  163.     f_print(fout, ")) {\n");
  164.     tabify(fout, indent);
  165.     f_print(fout, "\treturn (FALSE);\n");
  166.     tabify(fout, indent);
  167.     f_print(fout, "}\n");
  168. }
  169.  
  170. static
  171. space()
  172. {
  173.     f_print(fout, "\n\n");
  174. }
  175.  
  176. static
  177. print_ifstat(indent, prefix, type, rel, amax, objname, name)
  178.     int indent;
  179.     char *prefix;
  180.     char *type;
  181.     relation rel;
  182.     char *amax;
  183.     char *objname;
  184.     char *name;
  185. {
  186.     char *alt = NULL;
  187.  
  188.     switch (rel) {
  189.     case REL_POINTER:
  190.         print_ifopen(indent, "pointer");
  191.         print_ifarg("(char **)");
  192.         f_print(fout, "%s", objname);
  193.         print_ifsizeof(prefix, type);
  194.         break;
  195.     case REL_VECTOR:
  196.         if (streq(type, "string")) {
  197.             alt = "string";
  198.         } else if (streq(type, "opaque")) {
  199.             alt = "opaque";
  200.         }
  201.         if (alt) {
  202.             print_ifopen(indent, alt);
  203.             print_ifarg(objname);
  204.         } else {
  205.             print_ifopen(indent, "vector");
  206.             print_ifarg("(char *)");
  207.             f_print(fout, "%s", objname);
  208.         }
  209.         print_ifarg(amax);
  210.         if (!alt) {
  211.             print_ifsizeof(prefix, type);
  212.         }
  213.         break;
  214.     case REL_ARRAY:
  215.         if (streq(type, "string")) {
  216.             alt = "string";
  217.         } else if (streq(type, "opaque")) {
  218.             alt = "bytes";
  219.         }
  220.         if (streq(type, "string")) {
  221.             print_ifopen(indent, alt);
  222.             print_ifarg(objname);
  223.         } else {
  224.             if (alt) {
  225.                 print_ifopen(indent, alt);
  226.             } else {
  227.                 print_ifopen(indent, "array");
  228.             }
  229.             print_ifarg("(char **)");
  230.             if (*objname == '&') {
  231.                 f_print(fout, "%s.%s_val, (u_int *)%s.%s_len",
  232.                     objname, name, objname, name);
  233.             } else {
  234.                 f_print(fout, "&%s->%s_val, (u_int *)&%s->%s_len",
  235.                     objname, name, objname, name);
  236.             }
  237.         }
  238.         print_ifarg(amax);
  239.         if (!alt) {
  240.             print_ifsizeof(prefix, type);
  241.         }
  242.         break;
  243.     case REL_ALIAS:
  244.         print_ifopen(indent, type);
  245.         print_ifarg(objname);
  246.         break;
  247.     }
  248.     print_ifclose(indent);
  249. }
  250.  
  251.  
  252. /* ARGSUSED */
  253. static
  254. emit_enum(def)
  255.     definition *def;
  256. {
  257.     print_ifopen(1, "enum");
  258.     print_ifarg("(enum_t *)objp");
  259.     print_ifclose(1);
  260. }
  261.  
  262.  
  263. static
  264. emit_union(def)
  265.     definition *def;
  266. {
  267.     declaration *dflt;
  268.     case_list *cl;
  269.     declaration *cs;
  270.     char *object;
  271.     char *format = "&objp->%s_u.%s";
  272.  
  273.     print_stat(&def->def.un.enum_decl);
  274.     f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
  275.     for (cl = def->def.un.cases; cl != NULL; cl = cl->next) {
  276.         cs = &cl->case_decl;
  277.         f_print(fout, "\tcase %s:\n", cl->case_name);
  278.         if (!streq(cs->type, "void")) {
  279.             object = alloc(strlen(def->def_name) + strlen(format) +
  280.                        strlen(cs->name) + 1);
  281.             s_print(object, format, def->def_name, cs->name);
  282.             print_ifstat(2, cs->prefix, cs->type, cs->rel, cs->array_max,
  283.                      object, cs->name);
  284.             free(object);
  285.         }
  286.         f_print(fout, "\t\tbreak;\n");
  287.     }
  288.     dflt = def->def.un.default_decl;
  289.     if (dflt != NULL) {
  290.         if (!streq(dflt->type, "void")) {
  291.             f_print(fout, "\tdefault:\n");
  292.             object = alloc(strlen(def->def_name) + strlen(format) +
  293.                        strlen(dflt->name) + 1);
  294.             s_print(object, format, def->def_name, dflt->name);
  295.             print_ifstat(2, dflt->prefix, dflt->type, dflt->rel,
  296.                      dflt->array_max, object, dflt->name);
  297.             free(object);
  298.             f_print(fout, "\t\tbreak;\n");
  299.         }
  300.     } else {
  301.         f_print(fout, "\tdefault:\n");
  302.         f_print(fout, "\t\treturn (FALSE);\n");
  303.     }
  304.     f_print(fout, "\t}\n");
  305. }
  306.  
  307.  
  308.  
  309. static
  310. emit_struct(def)
  311.     definition *def;
  312. {
  313.     decl_list *dl;
  314.  
  315.     for (dl = def->def.st.decls; dl != NULL; dl = dl->next) {
  316.         print_stat(&dl->decl);
  317.     }
  318. }
  319.  
  320.  
  321.  
  322.  
  323. static
  324. emit_typedef(def)
  325.     definition *def;
  326. {
  327.     char *prefix = def->def.ty.old_prefix;
  328.     char *type = def->def.ty.old_type;
  329.     char *amax = def->def.ty.array_max;
  330.     relation rel = def->def.ty.rel;
  331.  
  332.     print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
  333. }
  334.  
  335.  
  336.  
  337.  
  338.  
  339. static
  340. print_stat(dec)
  341.     declaration *dec;
  342. {
  343.     char *prefix = dec->prefix;
  344.     char *type = dec->type;
  345.     char *amax = dec->array_max;
  346.     relation rel = dec->rel;
  347.     char name[256];
  348.  
  349.     if (isvectordef(type, rel)) {
  350.         s_print(name, "objp->%s", dec->name);
  351.     } else {
  352.         s_print(name, "&objp->%s", dec->name);
  353.     }
  354.     print_ifstat(1, prefix, type, rel, amax, name, dec->name);
  355. }
  356.