Home | History | Annotate | Line # | Download | only in rpcgen
rpc_clntout.c revision 1.10
      1  1.10   mycroft /*	$NetBSD: rpc_clntout.c,v 1.10 2001/03/21 20:11:01 mycroft 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.7  christos #include <sys/cdefs.h>
     33   1.1     glass #ifndef lint
     34   1.7  christos #if 0
     35   1.4        pk static char sccsid[] = "@(#)rpc_clntout.c 1.11 89/02/22 (C) 1987 SMI";
     36   1.7  christos #else
     37  1.10   mycroft __RCSID("$NetBSD: rpc_clntout.c,v 1.10 2001/03/21 20:11:01 mycroft Exp $");
     38   1.7  christos #endif
     39   1.1     glass #endif
     40   1.1     glass 
     41   1.1     glass /*
     42   1.1     glass  * rpc_clntout.c, Client-stub outputter for the RPC protocol compiler
     43   1.1     glass  * Copyright (C) 1987, Sun Microsytsems, Inc.
     44   1.1     glass  */
     45   1.1     glass #include <stdio.h>
     46   1.4        pk #include <string.h>
     47   1.4        pk #include <rpc/types.h>
     48   1.7  christos #include "rpc_scan.h"
     49   1.1     glass #include "rpc_parse.h"
     50   1.1     glass #include "rpc_util.h"
     51   1.1     glass 
     52   1.7  christos static void write_program __P((definition *));
     53   1.7  christos static char *ampr __P((char *));
     54   1.9   mycroft static char *aster __P((char *));
     55   1.4        pk static void printbody __P((proc_list *));
     56   1.4        pk 
     57   1.1     glass #define DEFAULT_TIMEOUT 25	/* in seconds */
     58   1.4        pk static char RESULT[] = "clnt_res";
     59   1.1     glass 
     60   1.1     glass 
     61   1.1     glass void
     62   1.1     glass write_stubs()
     63   1.1     glass {
     64   1.8     lukem 	list   *l;
     65   1.1     glass 	definition *def;
     66   1.1     glass 
     67   1.8     lukem 	f_print(fout,
     68   1.8     lukem 	    "\n/* Default timeout can be changed using clnt_control() */\n");
     69   1.4        pk 	f_print(fout, "static struct timeval TIMEOUT = { %d, 0 };\n",
     70   1.8     lukem 	    DEFAULT_TIMEOUT);
     71   1.1     glass 	for (l = defined; l != NULL; l = l->next) {
     72   1.1     glass 		def = (definition *) l->val;
     73   1.1     glass 		if (def->def_kind == DEF_PROGRAM) {
     74   1.1     glass 			write_program(def);
     75   1.1     glass 		}
     76   1.1     glass 	}
     77   1.1     glass }
     78   1.1     glass 
     79   1.7  christos static void
     80   1.1     glass write_program(def)
     81   1.1     glass 	definition *def;
     82   1.1     glass {
     83   1.1     glass 	version_list *vp;
     84   1.1     glass 	proc_list *proc;
     85   1.1     glass 
     86   1.1     glass 	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
     87   1.1     glass 		for (proc = vp->procs; proc != NULL; proc = proc->next) {
     88   1.1     glass 			f_print(fout, "\n");
     89   1.9   mycroft 			if (Mflag)
     90   1.9   mycroft 				f_print(fout, "enum clnt_stat\n");
     91   1.9   mycroft 			else {
     92   1.9   mycroft 				ptype(proc->res_prefix, proc->res_type, 1);
     93   1.9   mycroft 				f_print(fout, "*\n");
     94   1.9   mycroft 			}
     95   1.1     glass 			pvname(proc->proc_name, vp->vers_num);
     96   1.9   mycroft 			printarglist(proc, RESULT, "clnt", "CLIENT *");
     97   1.1     glass 			f_print(fout, "{\n");
     98   1.1     glass 			printbody(proc);
     99   1.4        pk 			f_print(fout, "}\n");
    100   1.1     glass 		}
    101   1.1     glass 	}
    102   1.1     glass }
    103   1.4        pk /* Writes out declarations of procedure's argument list.
    104   1.4        pk    In either ANSI C style, in one of old rpcgen style (pass by reference),
    105   1.4        pk    or new rpcgen style (multiple arguments, pass by value);
    106   1.4        pk    */
    107   1.4        pk 
    108   1.4        pk /* sample addargname = "clnt"; sample addargtype = "CLIENT * " */
    109   1.4        pk 
    110   1.8     lukem void
    111   1.9   mycroft printarglist(proc, result, addargname, addargtype)
    112   1.8     lukem 	proc_list *proc;
    113   1.9   mycroft 	char   *result, *addargname, *addargtype;
    114   1.4        pk {
    115   1.4        pk 
    116   1.8     lukem 	decl_list *l;
    117   1.4        pk 
    118   1.8     lukem 	if (!newstyle) {	/* old style: always pass argument by
    119   1.8     lukem 				 * reference */
    120   1.8     lukem 		if (Cflag) {	/* C++ style heading */
    121   1.8     lukem 			f_print(fout, "(");
    122   1.8     lukem 			ptype(proc->args.decls->decl.prefix, proc->args.decls->decl.type, 1);
    123   1.9   mycroft 			f_print(fout, "*argp, ");
    124   1.9   mycroft 			if (Mflag) {
    125   1.9   mycroft 				if (streq(proc->res_type, "void"))
    126   1.9   mycroft 					f_print(fout, "char ");
    127   1.9   mycroft 				else
    128   1.9   mycroft 					ptype(proc->res_prefix, proc->res_type, 0);
    129   1.9   mycroft 				f_print(fout, "%s%s, ", aster(proc->res_type),
    130   1.9   mycroft 				    result);
    131   1.9   mycroft 			}
    132   1.9   mycroft 			f_print(fout, "%s%s)\n", addargtype, addargname);
    133   1.8     lukem 		} else {
    134   1.9   mycroft 			f_print(fout, "(argp, ");
    135   1.9   mycroft 			if (Mflag)
    136   1.9   mycroft 				f_print(fout, "%s, ", result);
    137   1.9   mycroft 			f_print(fout, "%s)\n", addargname);
    138   1.8     lukem 			f_print(fout, "\t");
    139   1.8     lukem 			ptype(proc->args.decls->decl.prefix, proc->args.decls->decl.type, 1);
    140   1.8     lukem 			f_print(fout, "*argp;\n");
    141   1.9   mycroft 			if (Mflag) {
    142   1.9   mycroft 				f_print(fout, "\t");
    143   1.9   mycroft 				if (streq(proc->res_type, "void"))
    144   1.9   mycroft 					f_print(fout, "char ");
    145   1.9   mycroft 				else
    146   1.9   mycroft 					ptype(proc->res_prefix, proc->res_type, 0);
    147   1.9   mycroft 				f_print(fout, "%s%s;\n", aster(proc->res_type),
    148   1.9   mycroft 				    result);
    149   1.9   mycroft 			}
    150   1.8     lukem 		}
    151  1.10   mycroft 	} else {
    152  1.10   mycroft 		f_print(fout, "(");
    153  1.10   mycroft 		if (!streq(proc->args.decls->decl.type, "void")) {
    154   1.8     lukem 			/* new style, 1 or multiple arguments */
    155   1.8     lukem 			if (!Cflag) {
    156  1.10   mycroft 				for (l = proc->args.decls; l != NULL;
    157  1.10   mycroft 				    l = l->next)
    158   1.8     lukem 					f_print(fout, "%s, ", l->decl.name);
    159   1.8     lukem 			} else {/* C++ style header */
    160  1.10   mycroft 				for (l = proc->args.decls; l != NULL;
    161  1.10   mycroft 				    l = l->next)
    162  1.10   mycroft 					pdeclaration(proc->args.argname,
    163  1.10   mycroft 					    &l->decl, 0, ", ");
    164  1.10   mycroft 			}
    165  1.10   mycroft 		}
    166  1.10   mycroft 		if (!Cflag) {
    167  1.10   mycroft 			if (Mflag) {
    168  1.10   mycroft 				f_print(fout, "\t");
    169  1.10   mycroft 				if (streq(proc->res_type, "void"))
    170  1.10   mycroft 					f_print(fout, "char ");
    171  1.10   mycroft 				else
    172  1.10   mycroft 					ptype(proc->res_prefix, proc->res_type, 0);
    173  1.10   mycroft 				f_print(fout, "%s%s;\n", aster(proc->res_type),
    174  1.10   mycroft 				    result);
    175  1.10   mycroft 			}
    176  1.10   mycroft 			f_print(fout, "%s)\n", addargname);
    177  1.10   mycroft 			if (!streq(proc->args.decls->decl.type, "void")) {
    178  1.10   mycroft 				for (l = proc->args.decls; l != NULL;
    179  1.10   mycroft 				    l = l->next)
    180  1.10   mycroft 					pdeclaration(proc->args.argname,
    181  1.10   mycroft 					    &l->decl, 1, ";\n");
    182  1.10   mycroft 			}
    183  1.10   mycroft 		} else {
    184  1.10   mycroft 			if (Mflag) {
    185  1.10   mycroft 				if (streq(proc->res_type, "void"))
    186  1.10   mycroft 					f_print(fout, "char ");
    187  1.10   mycroft 				else
    188  1.10   mycroft 					ptype(proc->res_prefix, proc->res_type, 0);
    189  1.10   mycroft 				f_print(fout, "%s%s, ", aster(proc->res_type),
    190  1.10   mycroft 				    result);
    191   1.8     lukem 			}
    192  1.10   mycroft 			f_print(fout, "%s%s)\n", addargtype, addargname);
    193   1.8     lukem 		}
    194  1.10   mycroft 	}
    195   1.4        pk 
    196   1.8     lukem 	if (!Cflag)
    197   1.8     lukem 		f_print(fout, "\t%s%s;\n", addargtype, addargname);
    198   1.4        pk }
    199   1.4        pk 
    200   1.4        pk 
    201   1.1     glass static char *
    202   1.1     glass ampr(type)
    203   1.8     lukem 	char   *type;
    204   1.1     glass {
    205   1.1     glass 	if (isvectordef(type, REL_ALIAS)) {
    206   1.1     glass 		return ("");
    207   1.1     glass 	} else {
    208   1.1     glass 		return ("&");
    209   1.1     glass 	}
    210   1.1     glass }
    211   1.1     glass 
    212   1.9   mycroft static char *
    213   1.9   mycroft aster(type)
    214   1.9   mycroft 	char   *type;
    215   1.9   mycroft {
    216   1.9   mycroft 	if (isvectordef(type, REL_ALIAS)) {
    217   1.9   mycroft 		return ("");
    218   1.9   mycroft 	} else {
    219   1.9   mycroft 		return ("*");
    220   1.9   mycroft 	}
    221   1.9   mycroft }
    222   1.9   mycroft 
    223   1.4        pk static void
    224   1.1     glass printbody(proc)
    225   1.1     glass 	proc_list *proc;
    226   1.1     glass {
    227   1.8     lukem 	decl_list *l;
    228   1.8     lukem 	bool_t  args2 = (proc->arg_num > 1);
    229   1.4        pk 
    230   1.8     lukem 	/* For new style with multiple arguments, need a structure in which to
    231   1.8     lukem 	 * stuff the arguments. */
    232   1.8     lukem 	if (newstyle && args2) {
    233   1.4        pk 		f_print(fout, "\t%s", proc->args.argname);
    234   1.8     lukem 		f_print(fout, " arg;\n");
    235   1.4        pk 	}
    236   1.9   mycroft 	if (!Mflag) {
    237   1.9   mycroft 		f_print(fout, "\tstatic ");
    238   1.9   mycroft 		if (streq(proc->res_type, "void"))
    239   1.9   mycroft 			f_print(fout, "char ");
    240   1.9   mycroft 		else
    241   1.9   mycroft 			ptype(proc->res_prefix, proc->res_type, 0);
    242   1.9   mycroft 		f_print(fout, "%s;\n", RESULT);
    243   1.1     glass 	}
    244   1.1     glass 	f_print(fout, "\n");
    245   1.9   mycroft 	if (!Mflag)
    246   1.9   mycroft 		f_print(fout, "\tmemset((char *)%s%s, 0, sizeof(%s));\n",
    247   1.9   mycroft 		    ampr(proc->res_type), RESULT, RESULT);
    248   1.8     lukem 	if (newstyle && !args2 && (streq(proc->args.decls->decl.type, "void"))) {
    249   1.8     lukem 		/* newstyle, 0 arguments */
    250   1.9   mycroft 		if (Mflag) {
    251   1.9   mycroft 			f_print(fout, "\treturn (clnt_call(clnt, %s, xdr_void",
    252   1.9   mycroft 			    proc->proc_name);
    253   1.9   mycroft 			f_print(fout, ", NULL, xdr_%s, %s, TIMEOUT));\n",
    254   1.9   mycroft 			    stringfix(proc->res_type), RESULT);
    255   1.9   mycroft 		} else {
    256   1.9   mycroft 			f_print(fout, "\tif (clnt_call(clnt, %s, xdr_void, ",
    257   1.9   mycroft 			    proc->proc_name);
    258   1.9   mycroft 			f_print(fout,
    259   1.9   mycroft 			    "NULL, xdr_%s, %s%s, TIMEOUT) != RPC_SUCCESS) {\n",
    260   1.9   mycroft 			    stringfix(proc->res_type), ampr(proc->res_type),
    261   1.9   mycroft 			    RESULT);
    262   1.9   mycroft 		}
    263   1.9   mycroft 	} else {
    264   1.8     lukem 		if (newstyle && args2) {
    265   1.8     lukem 			/* newstyle, multiple arguments:  stuff arguments into
    266   1.8     lukem 			 * structure */
    267   1.8     lukem 			for (l = proc->args.decls; l != NULL; l = l->next) {
    268   1.8     lukem 				f_print(fout, "\targ.%s = %s;\n",
    269   1.8     lukem 				    l->decl.name, l->decl.name);
    270   1.8     lukem 			}
    271   1.9   mycroft 			if (Mflag) {
    272   1.9   mycroft 				f_print(fout,
    273   1.9   mycroft 				    "\treturn (clnt_call(clnt, %s, xdr_%s, &arg, xdr_%s, %s, TIMEOUT));\n",
    274   1.9   mycroft 				    proc->proc_name, proc->args.argname,
    275   1.9   mycroft 				    stringfix(proc->res_type), RESULT);
    276   1.9   mycroft 			} else {
    277   1.9   mycroft 				f_print(fout,
    278   1.9   mycroft 				    "\tif (clnt_call(clnt, %s, xdr_%s, &arg, xdr_%s, %s%s, TIMEOUT) != RPC_SUCCESS)\n",
    279   1.9   mycroft 				    proc->proc_name, proc->args.argname,
    280   1.9   mycroft 				    stringfix(proc->res_type),
    281   1.9   mycroft 				    ampr(proc->res_type), RESULT);
    282   1.9   mycroft 			}
    283   1.9   mycroft 		} else {	/* single argument, new or old style */
    284   1.9   mycroft 			if (Mflag) {
    285   1.9   mycroft 				f_print(fout,
    286   1.9   mycroft 				    "\treturn (clnt_call(clnt, %s, xdr_%s, %s%s, xdr_%s, %s, TIMEOUT));\n",
    287   1.9   mycroft 				    proc->proc_name,
    288   1.9   mycroft 				    stringfix(proc->args.decls->decl.type),
    289   1.9   mycroft 				    (newstyle ? "&" : ""),
    290   1.9   mycroft 				    (newstyle ? proc->args.decls->decl.name : "argp"),
    291   1.9   mycroft 				    stringfix(proc->res_type), RESULT);
    292   1.9   mycroft 			} else {
    293   1.9   mycroft 				f_print(fout,
    294   1.9   mycroft 				    "\tif (clnt_call(clnt, %s, xdr_%s, %s%s, xdr_%s, %s%s, TIMEOUT) != RPC_SUCCESS)\n",
    295   1.9   mycroft 				    proc->proc_name,
    296   1.9   mycroft 				    stringfix(proc->args.decls->decl.type),
    297   1.9   mycroft 				    (newstyle ? "&" : ""),
    298   1.9   mycroft 				    (newstyle ? proc->args.decls->decl.name : "argp"),
    299   1.9   mycroft 				    stringfix(proc->res_type),
    300   1.9   mycroft 				    ampr(proc->res_type), RESULT);
    301   1.9   mycroft 			}
    302   1.9   mycroft 		}
    303   1.9   mycroft 	}
    304   1.9   mycroft 	if (!Mflag) {
    305   1.9   mycroft 		f_print(fout, "\t\treturn (NULL);\n");
    306   1.9   mycroft 		if (streq(proc->res_type, "void"))
    307   1.9   mycroft 			f_print(fout, "\treturn ((void *)%s%s);\n",
    308   1.8     lukem 			    ampr(proc->res_type), RESULT);
    309   1.9   mycroft 		else
    310   1.9   mycroft 			f_print(fout, "\treturn (%s%s);\n",
    311   1.8     lukem 			    ampr(proc->res_type), RESULT);
    312   1.1     glass 	}
    313   1.1     glass }
    314