xref: /freebsd/usr.bin/rpcgen/rpc_clntout.c (revision 8e6b01171e30297084bb0b4457c4183c2746aacc)
1 /* @(#)rpc_clntout.c	2.1 88/08/01 4.0 RPCSRC */
2 /*
3  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4  * unrestricted use provided that this legend is included on all tape
5  * media and as a part of the software program in whole or part.  Users
6  * may copy or modify Sun RPC without charge, but are not authorized
7  * to license or distribute it to anyone else except as part of a product or
8  * program developed by the user.
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 #ifndef lint
31 /*static char sccsid[] = "from: @(#)rpc_clntout.c 1.2 87/06/24 (C) 1987 SMI";*/
32 static char rcsid[] = "$Id: rpc_clntout.c,v 1.1 1994/08/07 18:01:28 wollman Exp $";
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 <strings.h>
41 #include "rpc_parse.h"
42 #include "rpc_util.h"
43 
44 #define DEFAULT_TIMEOUT 25	/* in seconds */
45 
46 static int write_program(), printbody();
47 
48 
49 void
50 write_stubs()
51 {
52 	list *l;
53 	definition *def;
54 
55  	f_print(fout,
56  		"\n/* Default timeout can be changed using clnt_control() */\n");
57  	f_print(fout, "static struct timeval TIMEOUT = { %d, 0 };\n",
58 		DEFAULT_TIMEOUT);
59 	for (l = defined; l != NULL; l = l->next) {
60 		def = (definition *) l->val;
61 		if (def->def_kind == DEF_PROGRAM) {
62 			write_program(def);
63 		}
64 	}
65 }
66 
67 
68 static
69 write_program(def)
70 	definition *def;
71 {
72 	version_list *vp;
73 	proc_list *proc;
74 
75 	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
76 		for (proc = vp->procs; proc != NULL; proc = proc->next) {
77 			f_print(fout, "\n");
78 			ptype(proc->res_prefix, proc->res_type, 1);
79 			f_print(fout, "*\n");
80 			pvname(proc->proc_name, vp->vers_num);
81 			f_print(fout, "(argp, clnt)\n");
82 			f_print(fout, "\t");
83 			ptype(proc->arg_prefix, proc->arg_type, 1);
84 			f_print(fout, "*argp;\n");
85 			f_print(fout, "\tCLIENT *clnt;\n");
86 			f_print(fout, "{\n");
87 			printbody(proc);
88 			f_print(fout, "}\n\n");
89 		}
90 	}
91 }
92 
93 static char *
94 ampr(type)
95 	char *type;
96 {
97 	if (isvectordef(type, REL_ALIAS)) {
98 		return ("");
99 	} else {
100 		return ("&");
101 	}
102 }
103 
104 static
105 printbody(proc)
106 	proc_list *proc;
107 {
108 	f_print(fout, "\tstatic ");
109 	if (streq(proc->res_type, "void")) {
110 		f_print(fout, "char ");
111 	} else {
112 		ptype(proc->res_prefix, proc->res_type, 0);
113 	}
114 	f_print(fout, "res;\n");
115 	f_print(fout, "\n");
116  	f_print(fout, "\tbzero((char *)%sres, sizeof(res));\n",
117  		ampr(proc->res_type));
118 	f_print(fout,
119 		"\tif (clnt_call(clnt, %s, xdr_%s, argp, xdr_%s, %sres, TIMEOUT) != RPC_SUCCESS) {\n",
120 		proc->proc_name, stringfix(proc->arg_type),
121 		stringfix(proc->res_type), ampr(proc->res_type));
122 	f_print(fout, "\t\treturn (NULL);\n");
123 	f_print(fout, "\t}\n");
124 	if (streq(proc->res_type, "void")) {
125 		f_print(fout, "\treturn ((void *)%sres);\n",
126 			ampr(proc->res_type));
127 	} else {
128 		f_print(fout, "\treturn (%sres);\n", ampr(proc->res_type));
129 	}
130 }
131