home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 3 / goldfish_volume_3.bin / files / comm / tcp / amitcp / src / devtools / rpcgen / rpc_hout.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-09  |  12.2 KB  |  513 lines

  1. /*
  2.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  3.  * unrestricted use provided that this legend is included on all tape
  4.  * media and as a part of the software program in whole or part.  Users
  5.  * may copy or modify Sun RPC without charge, but are not authorized
  6.  * to license or distribute it to anyone else except as part of a product or
  7.  * program developed by the user or with the express written consent of
  8.  * Sun Microsystems, Inc.
  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.  
  31. #ifndef lint
  32. static char sccsid[] = "@(#)rpc_hout.c 1.12 89/02/22 (C) 1987 SMI";
  33. #endif
  34.  
  35. /*
  36.  * rpc_hout.c, Header file outputter for the RPC protocol compiler 
  37.  */
  38. #include <stdio.h>
  39. #include <ctype.h>
  40. #include "rpc_parse.h"
  41. #include "rpc_util.h"
  42.  
  43. #ifdef __STDC__
  44. #include "rpc_scan.h"
  45. #include "protos.h"
  46.  
  47. void print_datadef ( definition *def );
  48. void print_funcdef ( definition *def );
  49. void pxdrfuncdecl ( char *name , int pointerp );
  50. static void pconstdef ( definition *def );
  51. static void pargdef ( definition *def );
  52. static void pstructdef ( definition *def );
  53. static void puniondef ( definition *def );
  54. static void pdefine ( char *name , char *num );
  55. static void puldefine ( char *name , char *num );
  56. static int define_printed ( proc_list *stop , version_list *start );
  57. static void pprogramdef ( definition *def );
  58. void pprocdef ( proc_list *proc , version_list *vp , char *addargtype , int server_p , int mode );
  59. static void parglist ( proc_list *proc , char *addargtype );
  60. static void penumdef ( definition *def );
  61. static void ptypedef ( definition *def );
  62. void pdeclaration ( char *name , declaration *dec , int tab , char *separator );
  63. static int undefined2 ( char *type , char *stop );
  64. #endif
  65.  
  66. /*
  67.  * Print the C-version of an xdr definition 
  68.  */
  69.  
  70. void
  71. print_datadef(definition *def)
  72. {
  73.  
  74.     if (def->def_kind == DEF_PROGRAM )  /* handle data only */
  75.             return;
  76.  
  77.     if (def->def_kind != DEF_CONST) {
  78.         f_print(fout, "\n");
  79.     }
  80.     switch (def->def_kind) {
  81.     case DEF_STRUCT:
  82.         pstructdef(def);
  83.         break;
  84.     case DEF_UNION:
  85.         puniondef(def);
  86.         break;
  87.     case DEF_ENUM:
  88.         penumdef(def);
  89.         break;
  90.     case DEF_TYPEDEF:
  91.         ptypedef(def);
  92.         break;
  93.     case DEF_PROGRAM:
  94.         pprogramdef(def);
  95.         break;
  96.     case DEF_CONST:
  97.         pconstdef(def);
  98.         break;
  99.     }
  100.     if (def->def_kind != DEF_PROGRAM && def->def_kind != DEF_CONST) {
  101.       pxdrfuncdecl( def->def_name,
  102.                def->def_kind != DEF_TYPEDEF ||
  103.                !isvectordef(def->def.ty.old_type, def->def.ty.rel));
  104.  
  105.     }
  106. }
  107.  
  108.  
  109. void
  110. print_funcdef(definition *def)
  111. {
  112.     switch (def->def_kind) {
  113.     case DEF_PROGRAM:
  114.         f_print(fout, "\n");
  115.         pprogramdef(def);
  116.         break;
  117.           }
  118. }
  119.  
  120. void
  121. pxdrfuncdecl(char *name, int pointerp)
  122. {
  123. #ifdef amigados
  124.   f_print(fout,"#if defined(__cplusplus) \n");
  125.     f_print(fout, "extern \"C\" bool_t XDRFUN xdr_%s(XDR *, %s%s);\n", name, name, pointerp ? "*" : "");
  126.   f_print(fout,"#elif __STDC__ \n");
  127.     f_print(fout, "extern  bool_t XDRFUN xdr_%s(XDR *, %s%s);\n", name, name, pointerp ? "*" : "");
  128.   f_print(fout,"#else /* Old Style C */ \n");
  129.     f_print(fout, "bool_t XDRFUN xdr_%s();\n", name);
  130. #else
  131.   f_print(fout,"#ifdef __cplusplus \n");
  132.     f_print(fout, "extern \"C\" bool_t xdr_%s(XDR *, %s%s);\n", name, name, pointerp ? "*" : "");
  133.   f_print(fout,"#elif __STDC__ \n");
  134.     f_print(fout, "extern  bool_t xdr_%s(XDR *, %s%s);\n", name, name, pointerp ? "*" : "");
  135.   f_print(fout,"#else /* Old Style C */ \n");
  136.     f_print(fout, "bool_t xdr_%s();\n", name);
  137. #endif
  138.   f_print(fout,"#endif /* Old Style C */ \n\n");
  139. }
  140.  
  141.  
  142. static void
  143. pconstdef(definition *def)
  144. {
  145.     pdefine(def->def_name, def->def.co);
  146. }
  147.  
  148. /* print out the definitions for the arguments of functions in the 
  149.    header file 
  150. */
  151. static void
  152. pargdef(definition *def)
  153. {
  154.     decl_list *l;
  155.     version_list *vers;
  156.     char *name;
  157.     proc_list *plist;
  158.  
  159.     
  160.     for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
  161.             for(plist = vers->procs; plist != NULL; 
  162.                 plist = plist->next) {
  163.                 
  164.                 if (!newstyle || plist->arg_num < 2) {
  165.                     continue; /* old style or single args */
  166.                 }
  167.                 name = plist->args.argname;
  168.                 f_print(fout, "struct %s {\n", name);
  169.                 for (l = plist->args.decls; 
  170.                      l != NULL; l = l->next) {
  171.                     pdeclaration(name, &l->decl, 1, ";\n" );
  172.                 }
  173.                 f_print(fout, "};\n");
  174.                 f_print(fout, "typedef struct %s %s;\n", name, name);
  175.                 pxdrfuncdecl( name,NULL );
  176.                 f_print( fout, "\n" );
  177.             }
  178.         }
  179.  
  180. }
  181.  
  182.  
  183. static void
  184. pstructdef(definition *def)
  185. {
  186.     decl_list *l;
  187.     char *name = def->def_name;
  188.  
  189.     f_print(fout, "struct %s {\n", name);
  190.     for (l = def->def.st.decls; l != NULL; l = l->next) {
  191.         pdeclaration(name, &l->decl, 1, ";\n");
  192.     }
  193.     f_print(fout, "};\n");
  194.     f_print(fout, "typedef struct %s %s;\n", name, name);
  195. }
  196.  
  197. static void
  198. puniondef(definition *def)
  199. {
  200.     case_list *l;
  201.     char *name = def->def_name;
  202.     declaration *decl;
  203.  
  204.     f_print(fout, "struct %s {\n", name);
  205.     decl = &def->def.un.enum_decl;
  206.     if (streq(decl->type, "bool")) {
  207.         f_print(fout, "\tbool_t %s;\n", decl->name);
  208.     } else {
  209.         f_print(fout, "\t%s %s;\n", decl->type, decl->name);
  210.     }
  211.     f_print(fout, "\tunion {\n");
  212.     for (l = def->def.un.cases; l != NULL; l = l->next) {
  213.       if(l->contflag == 0)
  214.         pdeclaration(name, &l->case_decl, 2, ";\n" );
  215.     }
  216.     decl = def->def.un.default_decl;
  217.     if (decl && !streq(decl->type, "void")) {
  218.         pdeclaration(name, decl, 2, ";\n" );
  219.     }
  220.     f_print(fout, "\t} %s_u;\n", name);
  221.     f_print(fout, "};\n");
  222.     f_print(fout, "typedef struct %s %s;\n", name, name);
  223. }
  224.  
  225. static void
  226. pdefine(char *name, char *num)
  227. {
  228.     f_print(fout, "#define %s %s\n", name, num);
  229. }
  230.  
  231. static void
  232. puldefine(char *name, char *num)
  233. {
  234.     f_print(fout, "#define %s ((u_long)%s)\n", name, num);
  235. }
  236.  
  237. static int
  238. define_printed(proc_list *stop, version_list *start)
  239. {
  240.     version_list *vers;
  241.     proc_list *proc;
  242.  
  243.     for (vers = start; vers != NULL; vers = vers->next) {
  244.         for (proc = vers->procs; proc != NULL; proc = proc->next) {
  245.             if (proc == stop) {
  246.                 return (0);
  247.             } else if (streq(proc->proc_name, stop->proc_name)) {
  248.                 return (1);
  249.             }
  250.         }
  251.     }
  252.     abort();
  253.     /* NOTREACHED */
  254. }
  255.  
  256. static void
  257. pprogramdef(definition *def)
  258. {
  259.     version_list *vers;
  260.     proc_list *proc;
  261.     int i;
  262.     char *ext;
  263.     
  264.     pargdef(def);
  265.  
  266.     puldefine(def->def_name, def->def.pr.prog_num);
  267.     for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
  268.         if (tblflag) {
  269.             f_print(fout, "extern struct rpcgen_table %s_%s_table[];\n",
  270.                 locase(def->def_name), vers->vers_num);
  271.             f_print(fout, "extern %s_%s_nproc;\n",
  272.                 locase(def->def_name), vers->vers_num);
  273.         }
  274.         puldefine(vers->vers_name, vers->vers_num);
  275.  
  276.         /* 
  277.          * Print out 3 definitions, one for ANSI-C, another for C++, 
  278.          * a third for old style C 
  279.          */
  280.            
  281.         for(i=0;i<3;i++){
  282.             if(i==0){
  283. #ifdef amigados
  284.                 f_print(fout,"\n#if defined(__cplusplus)\n");
  285. #else
  286.                 f_print(fout,"\n#ifdef __cplusplus\n");
  287. #endif
  288.                 ext="extern \"C\" ";
  289.             }else if ( i== 1){
  290.                 f_print(fout,"\n#elif __STDC__\n");
  291.                 ext="extern  "  ;
  292.             }else{
  293.                 f_print(fout,"\n#else /* Old Style C */ \n");
  294.                 ext="extern  ";
  295.             }
  296.             
  297.             
  298.             for (proc = vers->procs; proc != NULL; proc = proc->next) {
  299.                 if (!define_printed(proc, def->def.pr.versions)) {
  300.                     puldefine(proc->proc_name, proc->proc_num);
  301.                 }
  302.                 f_print(fout,"%s",ext);
  303.                 pprocdef(proc, vers, "CLIENT *", 0,i);
  304.                 f_print(fout,"%s",ext);
  305.                 pprocdef(proc, vers, "struct svc_req *", 1,i);
  306.                 
  307.             }
  308.  
  309.         }
  310.         f_print(fout,"#endif /* Old Style C */ \n");
  311.     }
  312. }
  313.  
  314. void
  315. pprocdef(proc_list *proc, version_list *vp, char *addargtype, int server_p, int mode)
  316. {
  317.  
  318.  
  319.  
  320.  
  321.         ptype( proc->res_prefix, proc->res_type, 1 );
  322.         f_print( fout, "* " );
  323.         if( server_p )
  324.             pvname_svc(proc->proc_name, vp->vers_num);
  325.         else
  326.             pvname(proc->proc_name, vp->vers_num);
  327.  
  328.         /*
  329.          * mode  0 == cplusplus, mode  1 = ANSI-C, mode 2 = old style C 
  330.          */
  331.         if(mode == 0 || mode ==1) 
  332.             parglist( proc, addargtype );
  333.         else
  334.             f_print(fout, "();\n");
  335.  
  336.  
  337.  
  338. }
  339.  
  340.  
  341.  
  342. /* print out argument list of procedure */
  343. static void 
  344. parglist(proc_list *proc, char *addargtype)
  345. {
  346.     decl_list *dl;
  347.  
  348.         f_print(fout,"(");
  349.  
  350.         if( proc->arg_num < 2 && newstyle &&
  351.            streq( proc->args.decls->decl.type, "void")) {
  352.             /* 0 argument in new style:  do nothing */
  353.         } else {
  354.             for (dl = proc->args.decls; dl != NULL; dl = dl->next) {
  355.                 ptype( dl->decl.prefix, dl->decl.type, 1 );
  356.                 if( !newstyle )
  357.                     f_print( fout, "*" ); /* old style passes by reference */
  358.     
  359.                 f_print( fout, ", " );
  360.             }
  361.         }
  362.     
  363.         f_print(fout, "%s);\n", addargtype);
  364.  
  365. }
  366.  
  367. static void
  368. penumdef(definition *def)
  369. {
  370.     char *name = def->def_name;
  371.     enumval_list *l;
  372.     char *last = NULL;
  373.     int count = 0;
  374.  
  375.     f_print(fout, "enum %s {\n", name);
  376.     for (l = def->def.en.vals; l != NULL; l = l->next) {
  377.         f_print(fout, "\t%s", l->name);
  378.         if (l->assignment) {
  379.             f_print(fout, " = %s", l->assignment);
  380.             last = l->assignment;
  381.             count = 1;
  382.         } else {
  383.             if (last == NULL) {
  384.                 f_print(fout, " = %d", count++);
  385.             } else {
  386.                 f_print(fout, " = %s + %d", last, count++);
  387.             }
  388.         }
  389.         f_print(fout, ",\n");
  390.     }
  391.     f_print(fout, "};\n");
  392.     f_print(fout, "typedef enum %s %s;\n", name, name);
  393. }
  394.  
  395. static void
  396. ptypedef(definition *def)
  397. {
  398.     char *name = def->def_name;
  399.     char *old = def->def.ty.old_type;
  400.     char prefix[8];    /* enough to contain "struct ", including NUL */
  401.     relation rel = def->def.ty.rel;
  402.  
  403.  
  404.     if (!streq(name, old)) {
  405.         if (streq(old, "string")) {
  406.             old = "char";
  407.             rel = REL_POINTER;
  408.         } else if (streq(old, "opaque")) {
  409.             old = "char";
  410.         } else if (streq(old, "bool")) {
  411.             old = "bool_t";
  412.         }
  413.         if (undefined2(old, name) && def->def.ty.old_prefix) {
  414.             s_print(prefix, "%s ", def->def.ty.old_prefix);
  415.         } else {
  416.             prefix[0] = 0;
  417.         }
  418.         f_print(fout, "typedef ");
  419.         switch (rel) {
  420.         case REL_ARRAY:
  421.             f_print(fout, "struct {\n");
  422.             f_print(fout, "\tu_int %s_len;\n", name);
  423.             f_print(fout, "\t%s%s *%s_val;\n", prefix, old, name);
  424.             f_print(fout, "} %s", name);
  425.             break;
  426.         case REL_POINTER:
  427.             f_print(fout, "%s%s *%s", prefix, old, name);
  428.             break;
  429.         case REL_VECTOR:
  430.             f_print(fout, "%s%s %s[%s]", prefix, old, name,
  431.                 def->def.ty.array_max);
  432.             break;
  433.         case REL_ALIAS:
  434.             f_print(fout, "%s%s %s", prefix, old, name);
  435.             break;
  436.         }
  437.         f_print(fout, ";\n");
  438.     }
  439. }
  440.  
  441. void
  442. pdeclaration(char *name, declaration *dec, int tab, char *separator)
  443. {
  444.     char buf[8];    /* enough to hold "struct ", include NUL */
  445.     char *prefix;
  446.     char *type;
  447.  
  448.     if (streq(dec->type, "void")) {
  449.         return;
  450.     }
  451.     tabify(fout, tab);
  452.     if (streq(dec->type, name) && !dec->prefix) {
  453.         f_print(fout, "struct ");
  454.     }
  455.     if (streq(dec->type, "string")) {
  456.         f_print(fout, "char *%s", dec->name);
  457.     } else {
  458.         prefix = "";
  459.         if (streq(dec->type, "bool")) {
  460.             type = "bool_t";
  461.         } else if (streq(dec->type, "opaque")) {
  462.             type = "char";
  463.         } else {
  464.             if (dec->prefix) {
  465.                 s_print(buf, "%s ", dec->prefix);
  466.                 prefix = buf;
  467.             }
  468.             type = dec->type;
  469.         }
  470.         switch (dec->rel) {
  471.         case REL_ALIAS:
  472.             f_print(fout, "%s%s %s", prefix, type, dec->name);
  473.             break;
  474.         case REL_VECTOR:
  475.             f_print(fout, "%s%s %s[%s]", prefix, type, dec->name,
  476.                 dec->array_max);
  477.             break;
  478.         case REL_POINTER:
  479.             f_print(fout, "%s%s *%s", prefix, type, dec->name);
  480.             break;
  481.         case REL_ARRAY:
  482.             f_print(fout, "struct {\n");
  483.             tabify(fout, tab);
  484.             f_print(fout, "\tu_int %s_len;\n", dec->name);
  485.             tabify(fout, tab);
  486.             f_print(fout, "\t%s%s *%s_val;\n", prefix, type, dec->name);
  487.             tabify(fout, tab);
  488.             f_print(fout, "} %s", dec->name);
  489.             break;
  490.         }
  491.     }
  492.     f_print(fout, separator );
  493. }
  494.  
  495. static int
  496. undefined2(char *type, char *stop)
  497. {
  498.     list *l;
  499.     definition *def;
  500.  
  501.     for (l = defined; l != NULL; l = l->next) {
  502.         def = (definition *) l->val;
  503.         if (def->def_kind != DEF_PROGRAM) {
  504.             if (streq(def->def_name, stop)) {
  505.                 return (1);
  506.             } else if (streq(def->def_name, type)) {
  507.                 return (0);
  508.             }
  509.         }
  510.     }
  511.     return (1);
  512. }
  513.