rpc_svcout.c revision 1897
11573Srgrimes/* @(#)rpc_svcout.c	2.1 88/08/01 4.0 RPCSRC */
21573Srgrimes/*
31573Srgrimes * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
41573Srgrimes * unrestricted use provided that this legend is included on all tape
51573Srgrimes * media and as a part of the software program in whole or part.  Users
61573Srgrimes * may copy or modify Sun RPC without charge, but are not authorized
71573Srgrimes * to license or distribute it to anyone else except as part of a product or
81573Srgrimes * program developed by the user.
91573Srgrimes *
101573Srgrimes * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
111573Srgrimes * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
121573Srgrimes * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
131573Srgrimes *
141573Srgrimes * Sun RPC is provided with no support and without any obligation on the
151573Srgrimes * part of Sun Microsystems, Inc. to assist in its use, correction,
16251672Semaste * modification or enhancement.
171573Srgrimes *
181573Srgrimes * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
191573Srgrimes * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
201573Srgrimes * OR ANY PART THEREOF.
211573Srgrimes *
221573Srgrimes * In no event will Sun Microsystems, Inc. be liable for any lost revenue
231573Srgrimes * or profits or other special, indirect and consequential damages, even if
241573Srgrimes * Sun has been advised of the possibility of such damages.
251573Srgrimes *
261573Srgrimes * Sun Microsystems, Inc.
271573Srgrimes * 2550 Garcia Avenue
281573Srgrimes * Mountain View, California  94043
291573Srgrimes */
301573Srgrimes#ifndef lint
311573Srgrimes/*static char sccsid[] = "from: @(#)rpc_svcout.c 1.6 87/06/24 (C) 1987 SMI";*/
321573Srgrimesstatic char rcsid[] = "$Id: rpc_svcout.c,v 1.1 1993/09/13 23:20:19 jtc Exp $";
331573Srgrimes#endif
341573Srgrimes
351573Srgrimes/*
3692986Sobrien * rpc_svcout.c, Server-skeleton outputter for the RPC protocol compiler
3792986Sobrien * Copyright (C) 1987, Sun Microsytsems, Inc.
381573Srgrimes */
391573Srgrimes#include <stdio.h>
401573Srgrimes#include <strings.h>
411573Srgrimes#include "rpc_parse.h"
421573Srgrimes#include "rpc_util.h"
431573Srgrimes
441573Srgrimesstatic char RQSTP[] = "rqstp";
451573Srgrimesstatic char TRANSP[] = "transp";
461573Srgrimesstatic char ARG[] = "argument";
471573Srgrimesstatic char RESULT[] = "result";
481573Srgrimesstatic char ROUTINE[] = "local";
491573Srgrimes
501573Srgrimesstatic int write_program(), printerr(), printif();
511573Srgrimes/*
521573Srgrimes * write most of the service, that is, everything but the registrations.
531573Srgrimes */
541573Srgrimesvoid
551573Srgrimeswrite_most()
561573Srgrimes{
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 */
91void
92write_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 */
135void
136write_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
144void
145write_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
160static
161write_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
247static
248printerr(err, transp)
249	char *err;
250	char *transp;
251{
252	f_print(fout, "\t\tsvcerr_%s(%s);\n", err, transp);
253}
254
255static
256printif(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
267nullproc(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