home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 3 / goldfish_volume_3.bin / files / comm / tcp / amitcp / src / devtools / rpcgen / rpc_clntout.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-09  |  6.8 KB  |  232 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_clntout.c 1.11 89/02/22 (C) 1987 SMI";
  33. #endif
  34.  
  35. /*
  36.  * rpc_clntout.c, Client-stub outputter for the RPC protocol compiler
  37.  * Copyright (C) 1987, Sun Microsytsems, Inc.
  38.  */
  39. #include <stdio.h>
  40. #include <string.h>
  41. #include <rpc/types.h>
  42. #include "rpc_parse.h"
  43. #include "rpc_util.h"
  44.  
  45. #ifdef __STDC__
  46. #include "rpc_scan.h"
  47. #include "protos.h"
  48.  
  49. void write_stubs ( void );
  50. static void write_program ( definition *def );
  51. void printarglist ( proc_list *proc , char *addargname , char *addargtype );
  52. static char *ampr ( char *type );
  53. static void printbody ( proc_list *proc );
  54.  
  55. #else
  56. extern pdeclaration();
  57. void printarglist();
  58.  
  59. #endif
  60.  
  61. #define DEFAULT_TIMEOUT 25    /* in seconds */
  62. static char RESULT[] = "clnt_res";
  63.  
  64.  
  65. void
  66. write_stubs()
  67. {
  68.     list *l;
  69.     definition *def;
  70.  
  71.     f_print(fout, 
  72.         "\n/* Default timeout can be changed using clnt_control() */\n");
  73.     f_print(fout, "static struct timeval TIMEOUT = { %d, 0 };\n",
  74.         DEFAULT_TIMEOUT);
  75.     for (l = defined; l != NULL; l = l->next) {
  76.         def = (definition *) l->val;
  77.         if (def->def_kind == DEF_PROGRAM) {
  78.             write_program(def);
  79.         }
  80.     }
  81. }
  82.  
  83. static void
  84. write_program(definition *def)
  85. {
  86.     version_list *vp;
  87.     proc_list *proc;
  88.  
  89.     for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
  90.         for (proc = vp->procs; proc != NULL; proc = proc->next) {
  91.             f_print(fout, "\n");
  92.             ptype(proc->res_prefix, proc->res_type, 1);
  93.             f_print(fout, "*\n");
  94.             pvname(proc->proc_name, vp->vers_num);
  95.             printarglist( proc, "clnt", "CLIENT *" );
  96.             f_print(fout, "{\n");
  97.             printbody(proc);
  98.             f_print(fout, "}\n");
  99.         }
  100.     }
  101. }
  102.  
  103. /* Writes out declarations of procedure's argument list.
  104.    In either ANSI C style, in one of old rpcgen style (pass by reference),
  105.    or new rpcgen style (multiple arguments, pass by value);
  106.    */
  107.  
  108. /* sample addargname = "clnt"; sample addargtype = "CLIENT * " */
  109.  
  110. void printarglist(proc_list *proc, char* addargname, char* addargtype)
  111. {
  112.  
  113.   decl_list *l;
  114.  
  115.   if (!newstyle) {    /* old style: always pass argument by reference */
  116.     if (Cflag) {      /* C++ style heading */
  117.       f_print(fout, "(");
  118.       ptype(proc->args.decls->decl.prefix, proc->args.decls->decl.type, 1);
  119.       f_print(fout, "*argp, %s%s)\n", addargtype, addargname );
  120.     } else {
  121.       f_print(fout, "(argp, %s)\n", addargname);
  122.       f_print(fout, "\t");
  123.       ptype(proc->args.decls->decl.prefix, proc->args.decls->decl.type, 1);
  124.       f_print(fout, "*argp;\n");
  125.     }
  126.   } else if (streq( proc->args.decls->decl.type, "void")) { 
  127.     /* newstyle, 0 argument */
  128.     if( Cflag ) 
  129.       f_print(fout, "(%s%s)\n", addargtype, addargname );
  130.     else
  131.       f_print(fout, "(%s)\n", addargname);
  132.   } else {
  133.     /* new style, 1 or multiple arguments */
  134.     if( !Cflag ) {
  135.       f_print(fout, "(");
  136.       for (l = proc->args.decls;  l != NULL; l = l->next) 
  137.     f_print(fout, "%s, ", l->decl.name);
  138.       f_print(fout, "%s)\n", addargname );
  139.       for (l = proc->args.decls; l != NULL; l = l->next) {
  140.     pdeclaration(proc->args.argname, &l->decl, 1, ";\n" );
  141.       }
  142.     } else {  /* C++ style header */
  143.       f_print(fout, "(");
  144.       for(l = proc->args.decls; l != NULL; l = l->next) {
  145.     pdeclaration(proc->args.argname, &l->decl, 0, ", " );
  146.       }
  147.       f_print(fout, " %s%s)\n", addargtype, addargname );
  148.     }
  149.   }
  150.  
  151.   if( !Cflag ) 
  152.     f_print(fout, "\t%s%s;\n", addargtype, addargname );
  153. }
  154.  
  155.  
  156.  
  157. static char *
  158. ampr(type)
  159.     char *type;
  160. {
  161.     if (isvectordef(type, REL_ALIAS)) {
  162.         return ("");
  163.     } else {
  164.         return ("&");
  165.     }
  166. }
  167.  
  168. static void
  169. printbody(proc_list *proc)
  170. {
  171.   decl_list *l;
  172.   bool_t args2 = (proc->arg_num > 1);
  173. #ifndef amigados
  174.   int i;
  175. #endif
  176.  
  177.   /* For new style with multiple arguments, need a structure in which
  178.      to stuff the arguments. */
  179.     if ( newstyle && args2) {
  180.         f_print(fout, "\t%s", proc->args.argname);
  181.         f_print(fout, " arg;\n");    
  182.     }
  183.     f_print(fout, "\tstatic ");
  184.     if (streq(proc->res_type, "void")) {
  185.         f_print(fout, "char ");
  186.     } else {
  187.         ptype(proc->res_prefix, proc->res_type, 0);
  188.     }
  189.     f_print(fout, "%s;\n",RESULT);
  190.     f_print(fout, "\n");
  191.         f_print(fout, "\tmemset((char *)%s%s, 0, sizeof(%s));\n",
  192.         ampr(proc->res_type ), RESULT, RESULT);
  193.     if (newstyle && !args2 && (streq( proc->args.decls->decl.type, "void"))) {
  194.       /* newstyle, 0 arguments */
  195.       f_print(fout,
  196.             "\tif (clnt_call(clnt, %s, xdr_void", proc->proc_name);
  197.       f_print(fout, 
  198.            ", NULL, xdr_%s, %s,%s, TIMEOUT) != RPC_SUCCESS) {\n",
  199.            stringfix(proc->res_type), ampr(proc->res_type), RESULT);
  200.  
  201.     } else if ( newstyle && args2) {
  202.       /* newstyle, multiple arguments:  stuff arguments into structure */
  203.       for (l = proc->args.decls;  l != NULL; l = l->next) {
  204.         f_print(fout, "\targ.%s = %s;\n",
  205.             l->decl.name, l->decl.name);
  206.       }
  207.       f_print(fout,
  208.           "\tif (clnt_call(clnt, %s, xdr_%s", proc->proc_name,
  209.           proc->args.argname);
  210.       f_print(fout, 
  211.                ", &arg, xdr_%s, %s%s, TIMEOUT) != RPC_SUCCESS) {\n",
  212.            stringfix(proc->res_type), ampr(proc->res_type), RESULT);
  213.     } else {  /* single argument, new or old style */
  214.           f_print(fout,
  215.                "\tif (clnt_call(clnt, %s, xdr_%s, %s%s, xdr_%s, %s%s, TIMEOUT) != RPC_SUCCESS) {\n",
  216.               proc->proc_name, 
  217.               stringfix(proc->args.decls->decl.type), 
  218.               (newstyle ? "&" : ""),
  219.               (newstyle ? proc->args.decls->decl.name : "argp"),
  220.               stringfix(proc->res_type), ampr(proc->res_type),RESULT);
  221.         }
  222.     f_print(fout, "\t\treturn (NULL);\n");
  223.     f_print(fout, "\t}\n");
  224.     if (streq(proc->res_type, "void")) {
  225.         f_print(fout, "\treturn ((void *)%s%s);\n", 
  226.             ampr(proc->res_type),RESULT);
  227.     } else {
  228.         f_print(fout, "\treturn (%s%s);\n", ampr(proc->res_type),RESULT);
  229.     }
  230. }
  231.  
  232.