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