Home | History | Annotate | Line # | Download | only in rpcgen
rpc_clntout.c revision 1.4
      1  1.4     pk /*	$NetBSD: rpc_clntout.c,v 1.4 1995/06/11 21:49:52 pk Exp $	*/
      2  1.1  glass /*
      3  1.1  glass  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
      4  1.1  glass  * unrestricted use provided that this legend is included on all tape
      5  1.1  glass  * media and as a part of the software program in whole or part.  Users
      6  1.1  glass  * may copy or modify Sun RPC without charge, but are not authorized
      7  1.1  glass  * to license or distribute it to anyone else except as part of a product or
      8  1.4     pk  * program developed by the user or with the express written consent of
      9  1.4     pk  * Sun Microsystems, Inc.
     10  1.4     pk  *
     11  1.1  glass  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
     12  1.1  glass  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
     13  1.1  glass  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
     14  1.4     pk  *
     15  1.1  glass  * Sun RPC is provided with no support and without any obligation on the
     16  1.1  glass  * part of Sun Microsystems, Inc. to assist in its use, correction,
     17  1.1  glass  * modification or enhancement.
     18  1.4     pk  *
     19  1.1  glass  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
     20  1.1  glass  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
     21  1.1  glass  * OR ANY PART THEREOF.
     22  1.4     pk  *
     23  1.1  glass  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
     24  1.1  glass  * or profits or other special, indirect and consequential damages, even if
     25  1.1  glass  * Sun has been advised of the possibility of such damages.
     26  1.4     pk  *
     27  1.1  glass  * Sun Microsystems, Inc.
     28  1.1  glass  * 2550 Garcia Avenue
     29  1.1  glass  * Mountain View, California  94043
     30  1.1  glass  */
     31  1.4     pk 
     32  1.1  glass #ifndef lint
     33  1.4     pk static char sccsid[] = "@(#)rpc_clntout.c 1.11 89/02/22 (C) 1987 SMI";
     34  1.1  glass #endif
     35  1.1  glass 
     36  1.1  glass /*
     37  1.1  glass  * rpc_clntout.c, Client-stub outputter for the RPC protocol compiler
     38  1.1  glass  * Copyright (C) 1987, Sun Microsytsems, Inc.
     39  1.1  glass  */
     40  1.1  glass #include <stdio.h>
     41  1.4     pk #include <string.h>
     42  1.4     pk #include <rpc/types.h>
     43  1.1  glass #include "rpc_parse.h"
     44  1.1  glass #include "rpc_util.h"
     45  1.1  glass 
     46  1.4     pk static write_program __P((definition *));
     47  1.4     pk static void printbody __P((proc_list *));
     48  1.4     pk 
     49  1.4     pk extern pdeclaration();
     50  1.4     pk 
     51  1.1  glass #define DEFAULT_TIMEOUT 25	/* in seconds */
     52  1.4     pk static char RESULT[] = "clnt_res";
     53  1.1  glass 
     54  1.1  glass 
     55  1.1  glass void
     56  1.1  glass write_stubs()
     57  1.1  glass {
     58  1.1  glass 	list *l;
     59  1.1  glass 	definition *def;
     60  1.1  glass 
     61  1.4     pk 	f_print(fout,
     62  1.4     pk 		"\n/* Default timeout can be changed using clnt_control() */\n");
     63  1.4     pk 	f_print(fout, "static struct timeval TIMEOUT = { %d, 0 };\n",
     64  1.1  glass 		DEFAULT_TIMEOUT);
     65  1.1  glass 	for (l = defined; l != NULL; l = l->next) {
     66  1.1  glass 		def = (definition *) l->val;
     67  1.1  glass 		if (def->def_kind == DEF_PROGRAM) {
     68  1.1  glass 			write_program(def);
     69  1.1  glass 		}
     70  1.1  glass 	}
     71  1.1  glass }
     72  1.1  glass 
     73  1.1  glass static
     74  1.1  glass write_program(def)
     75  1.1  glass 	definition *def;
     76  1.1  glass {
     77  1.1  glass 	version_list *vp;
     78  1.1  glass 	proc_list *proc;
     79  1.1  glass 
     80  1.1  glass 	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
     81  1.1  glass 		for (proc = vp->procs; proc != NULL; proc = proc->next) {
     82  1.1  glass 			f_print(fout, "\n");
     83  1.1  glass 			ptype(proc->res_prefix, proc->res_type, 1);
     84  1.1  glass 			f_print(fout, "*\n");
     85  1.1  glass 			pvname(proc->proc_name, vp->vers_num);
     86  1.4     pk 			printarglist( proc, "clnt", "CLIENT *" );
     87  1.1  glass 			f_print(fout, "{\n");
     88  1.1  glass 			printbody(proc);
     89  1.4     pk 			f_print(fout, "}\n");
     90  1.1  glass 		}
     91  1.1  glass 	}
     92  1.1  glass }
     93  1.1  glass 
     94  1.4     pk /* Writes out declarations of procedure's argument list.
     95  1.4     pk    In either ANSI C style, in one of old rpcgen style (pass by reference),
     96  1.4     pk    or new rpcgen style (multiple arguments, pass by value);
     97  1.4     pk    */
     98  1.4     pk 
     99  1.4     pk /* sample addargname = "clnt"; sample addargtype = "CLIENT * " */
    100  1.4     pk 
    101  1.4     pk void printarglist( proc, addargname, addargtype )
    102  1.4     pk      proc_list *proc;
    103  1.4     pk      char *addargname, *addargtype;
    104  1.4     pk {
    105  1.4     pk 
    106  1.4     pk   decl_list *l;
    107  1.4     pk 
    108  1.4     pk   if (!newstyle) {    /* old style: always pass argument by reference */
    109  1.4     pk     if (Cflag) {      /* C++ style heading */
    110  1.4     pk       f_print(fout, "(");
    111  1.4     pk       ptype(proc->args.decls->decl.prefix, proc->args.decls->decl.type, 1);
    112  1.4     pk       f_print(fout, "*argp, %s%s)\n", addargtype, addargname );
    113  1.4     pk     } else {
    114  1.4     pk       f_print(fout, "(argp, %s)\n", addargname);
    115  1.4     pk       f_print(fout, "\t");
    116  1.4     pk       ptype(proc->args.decls->decl.prefix, proc->args.decls->decl.type, 1);
    117  1.4     pk       f_print(fout, "*argp;\n");
    118  1.4     pk     }
    119  1.4     pk   } else if (streq( proc->args.decls->decl.type, "void")) {
    120  1.4     pk     /* newstyle, 0 argument */
    121  1.4     pk     if( Cflag )
    122  1.4     pk       f_print(fout, "(%s%s)\n", addargtype, addargname );
    123  1.4     pk     else
    124  1.4     pk       f_print(fout, "(%s)\n", addargname);
    125  1.4     pk   } else {
    126  1.4     pk     /* new style, 1 or multiple arguments */
    127  1.4     pk     if( !Cflag ) {
    128  1.4     pk       f_print(fout, "(");
    129  1.4     pk       for (l = proc->args.decls;  l != NULL; l = l->next)
    130  1.4     pk 	f_print(fout, "%s, ", l->decl.name);
    131  1.4     pk       f_print(fout, "%s)\n", addargname );
    132  1.4     pk       for (l = proc->args.decls; l != NULL; l = l->next) {
    133  1.4     pk 	pdeclaration(proc->args.argname, &l->decl, 1, ";\n" );
    134  1.4     pk       }
    135  1.4     pk     } else {  /* C++ style header */
    136  1.4     pk       f_print(fout, "(");
    137  1.4     pk       for(l = proc->args.decls; l != NULL; l = l->next) {
    138  1.4     pk 	pdeclaration(proc->args.argname, &l->decl, 0, ", " );
    139  1.4     pk       }
    140  1.4     pk       f_print(fout, " %s%s)\n", addargtype, addargname );
    141  1.4     pk     }
    142  1.4     pk   }
    143  1.4     pk 
    144  1.4     pk   if( !Cflag )
    145  1.4     pk     f_print(fout, "\t%s%s;\n", addargtype, addargname );
    146  1.4     pk }
    147  1.4     pk 
    148  1.4     pk 
    149  1.4     pk 
    150  1.1  glass static char *
    151  1.1  glass ampr(type)
    152  1.1  glass 	char *type;
    153  1.1  glass {
    154  1.1  glass 	if (isvectordef(type, REL_ALIAS)) {
    155  1.1  glass 		return ("");
    156  1.1  glass 	} else {
    157  1.1  glass 		return ("&");
    158  1.1  glass 	}
    159  1.1  glass }
    160  1.1  glass 
    161  1.4     pk static void
    162  1.1  glass printbody(proc)
    163  1.1  glass 	proc_list *proc;
    164  1.1  glass {
    165  1.4     pk   decl_list *l;
    166  1.4     pk   bool_t args2 = (proc->arg_num > 1);
    167  1.4     pk   int i;
    168  1.4     pk 
    169  1.4     pk   /* For new style with multiple arguments, need a structure in which
    170  1.4     pk      to stuff the arguments. */
    171  1.4     pk 	if ( newstyle && args2) {
    172  1.4     pk 		f_print(fout, "\t%s", proc->args.argname);
    173  1.4     pk 		f_print(fout, " arg;\n");
    174  1.4     pk 	}
    175  1.1  glass 	f_print(fout, "\tstatic ");
    176  1.1  glass 	if (streq(proc->res_type, "void")) {
    177  1.1  glass 		f_print(fout, "char ");
    178  1.1  glass 	} else {
    179  1.1  glass 		ptype(proc->res_prefix, proc->res_type, 0);
    180  1.1  glass 	}
    181  1.4     pk 	f_print(fout, "%s;\n",RESULT);
    182  1.1  glass 	f_print(fout, "\n");
    183  1.4     pk         f_print(fout, "\tmemset((char *)%s%s, 0, sizeof(%s));\n",
    184  1.4     pk 		ampr(proc->res_type ), RESULT, RESULT);
    185  1.4     pk 	if (newstyle && !args2 && (streq( proc->args.decls->decl.type, "void"))) {
    186  1.4     pk 	  /* newstyle, 0 arguments */
    187  1.4     pk 	  f_print(fout,
    188  1.4     pk 		    "\tif (clnt_call(clnt, %s, xdr_void", proc->proc_name);
    189  1.4     pk 	  f_print(fout,
    190  1.4     pk  		  ", NULL, xdr_%s, %s,%s, TIMEOUT) != RPC_SUCCESS) {\n",
    191  1.4     pk  		  stringfix(proc->res_type), ampr(proc->res_type), RESULT);
    192  1.4     pk 
    193  1.4     pk 	} else if ( newstyle && args2) {
    194  1.4     pk 	  /* newstyle, multiple arguments:  stuff arguments into structure */
    195  1.4     pk 	  for (l = proc->args.decls;  l != NULL; l = l->next) {
    196  1.4     pk 	    f_print(fout, "\targ.%s = %s;\n",
    197  1.4     pk 		    l->decl.name, l->decl.name);
    198  1.4     pk 	  }
    199  1.4     pk 	  f_print(fout,
    200  1.4     pk 		  "\tif (clnt_call(clnt, %s, xdr_%s", proc->proc_name,
    201  1.4     pk 		  proc->args.argname);
    202  1.4     pk 	  f_print(fout,
    203  1.4     pk  		  ", &arg, xdr_%s, %s%s, TIMEOUT) != RPC_SUCCESS) {\n",
    204  1.4     pk  		  stringfix(proc->res_type),
    205  1.4     pk 		  ampr(proc->res_type), RESULT);
    206  1.4     pk 	} else {  /* single argument, new or old style */
    207  1.4     pk 	      f_print(fout,
    208  1.4     pk  		      "\tif (clnt_call(clnt, %s, xdr_%s, %s%s, xdr_%s, %s%s, TIMEOUT) != RPC_SUCCESS) {\n",
    209  1.4     pk 		      proc->proc_name,
    210  1.4     pk 		      stringfix(proc->args.decls->decl.type),
    211  1.4     pk 		      (newstyle ? "&" : ""),
    212  1.4     pk 		      (newstyle ? proc->args.decls->decl.name : "argp"),
    213  1.4     pk 		      stringfix(proc->res_type),
    214  1.4     pk 		      ampr(proc->res_type),RESULT);
    215  1.4     pk 	    }
    216  1.1  glass 	f_print(fout, "\t\treturn (NULL);\n");
    217  1.1  glass 	f_print(fout, "\t}\n");
    218  1.1  glass 	if (streq(proc->res_type, "void")) {
    219  1.4     pk 		f_print(fout, "\treturn ((void *)%s%s);\n",
    220  1.4     pk 			ampr(proc->res_type),RESULT);
    221  1.1  glass 	} else {
    222  1.4     pk 		f_print(fout, "\treturn (%s%s);\n", ampr(proc->res_type),RESULT);
    223  1.1  glass 	}
    224  1.1  glass }
    225  1.4     pk 
    226