xref: /freebsd/usr.bin/rpcgen/rpc_svcout.c (revision 17ee9d00bc1ae1e598c38f25826f861e4bc6c3ce)
1 /* @(#)rpc_svcout.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_svcout.c 1.6 87/06/24 (C) 1987 SMI";*/
32 static char rcsid[] = "$Id: rpc_svcout.c,v 1.1 1993/09/13 23:20:19 jtc Exp $";
33 #endif
34 
35 /*
36  * rpc_svcout.c, Server-skeleton 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 static char RQSTP[] = "rqstp";
45 static char TRANSP[] = "transp";
46 static char ARG[] = "argument";
47 static char RESULT[] = "result";
48 static char ROUTINE[] = "local";
49 
50 static int write_program(), printerr(), printif();
51 /*
52  * write most of the service, that is, everything but the registrations.
53  */
54 void
55 write_most()
56 {
57 	list *l;
58 	definition *def;
59 	version_list *vp;
60 
61 	for (l = defined; l != NULL; l = l->next) {
62 		def = (definition *) l->val;
63 		if (def->def_kind == DEF_PROGRAM) {
64 			for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
65 				f_print(fout, "\nstatic void ");
66 				pvname(def->def_name, vp->vers_num);
67 				f_print(fout, "();");
68 			}
69 		}
70 	}
71 	f_print(fout, "\n\n");
72 	f_print(fout, "main()\n");
73 	f_print(fout, "{\n");
74 	f_print(fout, "\tSVCXPRT *%s;\n", TRANSP);
75 	f_print(fout, "\n");
76 	for (l = defined; l != NULL; l = l->next) {
77 		def = (definition *) l->val;
78 		if (def->def_kind != DEF_PROGRAM) {
79 			continue;
80 		}
81 		for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
82  			f_print(fout, "\t(void)pmap_unset(%s, %s);\n", def->def_name, vp->vers_name);
83 		}
84 	}
85 }
86 
87 
88 /*
89  * write a registration for the given transport
90  */
91 void
92 write_register(transp)
93 	char *transp;
94 {
95 	list *l;
96 	definition *def;
97 	version_list *vp;
98 
99 	f_print(fout, "\n");
100 	f_print(fout, "\t%s = svc%s_create(RPC_ANYSOCK", TRANSP, transp);
101 	if (streq(transp, "tcp")) {
102 		f_print(fout, ", 0, 0");
103 	}
104 	f_print(fout, ");\n");
105 	f_print(fout, "\tif (%s == NULL) {\n", TRANSP);
106  	f_print(fout, "\t\t(void)fprintf(stderr, \"cannot create %s service.\\n\");\n", transp);
107 	f_print(fout, "\t\texit(1);\n");
108 	f_print(fout, "\t}\n");
109 
110 	for (l = defined; l != NULL; l = l->next) {
111 		def = (definition *) l->val;
112 		if (def->def_kind != DEF_PROGRAM) {
113 			continue;
114 		}
115 		for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
116 			f_print(fout,
117 				"\tif (!svc_register(%s, %s, %s, ",
118 				TRANSP, def->def_name, vp->vers_name);
119 			pvname(def->def_name, vp->vers_num);
120 			f_print(fout, ", IPPROTO_%s)) {\n",
121 				streq(transp, "udp") ? "UDP" : "TCP");
122 			f_print(fout,
123  				"\t\t(void)fprintf(stderr, \"unable to register (%s, %s, %s).\\n\");\n",
124 				def->def_name, vp->vers_name, transp);
125 			f_print(fout, "\t\texit(1);\n");
126 			f_print(fout, "\t}\n");
127 		}
128 	}
129 }
130 
131 
132 /*
133  * write the rest of the service
134  */
135 void
136 write_rest()
137 {
138 	f_print(fout, "\tsvc_run();\n");
139  	f_print(fout, "\t(void)fprintf(stderr, \"svc_run returned\\n\");\n");
140 	f_print(fout, "\texit(1);\n");
141 	f_print(fout, "}\n");
142 }
143 
144 void
145 write_programs(storage)
146 	char *storage;
147 {
148 	list *l;
149 	definition *def;
150 
151 	for (l = defined; l != NULL; l = l->next) {
152 		def = (definition *) l->val;
153 		if (def->def_kind == DEF_PROGRAM) {
154 			write_program(def, storage);
155 		}
156 	}
157 }
158 
159 
160 static
161 write_program(def, storage)
162 	definition *def;
163 	char *storage;
164 {
165 	version_list *vp;
166 	proc_list *proc;
167 	int filled;
168 
169 	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
170 		f_print(fout, "\n");
171 		if (storage != NULL) {
172 			f_print(fout, "%s ", storage);
173 		}
174 		f_print(fout, "void\n");
175 		pvname(def->def_name, vp->vers_num);
176 		f_print(fout, "(%s, %s)\n", RQSTP, TRANSP);
177 		f_print(fout, "	struct svc_req *%s;\n", RQSTP);
178 		f_print(fout, "	SVCXPRT *%s;\n", TRANSP);
179 		f_print(fout, "{\n");
180 
181 		filled = 0;
182 		f_print(fout, "\tunion {\n");
183 		for (proc = vp->procs; proc != NULL; proc = proc->next) {
184 			if (streq(proc->arg_type, "void")) {
185 				continue;
186 			}
187 			filled = 1;
188 			f_print(fout, "\t\t");
189 			ptype(proc->arg_prefix, proc->arg_type, 0);
190 			pvname(proc->proc_name, vp->vers_num);
191 			f_print(fout, "_arg;\n");
192 		}
193 		if (!filled) {
194 			f_print(fout, "\t\tint fill;\n");
195 		}
196 		f_print(fout, "\t} %s;\n", ARG);
197 		f_print(fout, "\tchar *%s;\n", RESULT);
198 		f_print(fout, "\tbool_t (*xdr_%s)(), (*xdr_%s)();\n", ARG, RESULT);
199 		f_print(fout, "\tchar *(*%s)();\n", ROUTINE);
200 		f_print(fout, "\n");
201 		f_print(fout, "\tswitch (%s->rq_proc) {\n", RQSTP);
202 
203 		if (!nullproc(vp->procs)) {
204 			f_print(fout, "\tcase NULLPROC:\n");
205  			f_print(fout, "\t\t(void)svc_sendreply(%s, xdr_void, (char *)NULL);\n", TRANSP);
206 			f_print(fout, "\t\treturn;\n\n");
207 		}
208 		for (proc = vp->procs; proc != NULL; proc = proc->next) {
209 			f_print(fout, "\tcase %s:\n", proc->proc_name);
210 			f_print(fout, "\t\txdr_%s = xdr_%s;\n", ARG,
211 				stringfix(proc->arg_type));
212 			f_print(fout, "\t\txdr_%s = xdr_%s;\n", RESULT,
213 				stringfix(proc->res_type));
214 			f_print(fout, "\t\t%s = (char *(*)()) ", ROUTINE);
215 			pvname(proc->proc_name, vp->vers_num);
216 			f_print(fout, ";\n");
217 			f_print(fout, "\t\tbreak;\n\n");
218 		}
219 		f_print(fout, "\tdefault:\n");
220 		printerr("noproc", TRANSP);
221 		f_print(fout, "\t\treturn;\n");
222 		f_print(fout, "\t}\n");
223 
224  		f_print(fout, "\tbzero((char *)&%s, sizeof(%s));\n", ARG, ARG);
225 		printif("getargs", TRANSP, "&", ARG);
226 		printerr("decode", TRANSP);
227 		f_print(fout, "\t\treturn;\n");
228 		f_print(fout, "\t}\n");
229 
230 		f_print(fout, "\t%s = (*%s)(&%s, %s);\n", RESULT, ROUTINE, ARG,
231 			RQSTP);
232 		f_print(fout,
233 			"\tif (%s != NULL && !svc_sendreply(%s, xdr_%s, %s)) {\n",
234 			RESULT, TRANSP, RESULT, RESULT);
235 		printerr("systemerr", TRANSP);
236 		f_print(fout, "\t}\n");
237 
238 		printif("freeargs", TRANSP, "&", ARG);
239  		f_print(fout, "\t\t(void)fprintf(stderr, \"unable to free arguments\\n\");\n");
240 		f_print(fout, "\t\texit(1);\n");
241 		f_print(fout, "\t}\n");
242 
243 		f_print(fout, "}\n\n");
244 	}
245 }
246 
247 static
248 printerr(err, transp)
249 	char *err;
250 	char *transp;
251 {
252 	f_print(fout, "\t\tsvcerr_%s(%s);\n", err, transp);
253 }
254 
255 static
256 printif(proc, transp, prefix, arg)
257 	char *proc;
258 	char *transp;
259 	char *prefix;
260 	char *arg;
261 {
262 	f_print(fout, "\tif (!svc_%s(%s, xdr_%s, %s%s)) {\n",
263 		proc, transp, arg, prefix, arg);
264 }
265 
266 
267 nullproc(proc)
268 	proc_list *proc;
269 {
270 	for (; proc != NULL; proc = proc->next) {
271 		if (streq(proc->proc_num, "0")) {
272 			return (1);
273 		}
274 	}
275 	return (0);
276 }
277