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