1 /* @(#)rpc_svcout.c 1.2 87/11/24 3.9 RPCSRC */
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.
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.
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.
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.
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.
26 * Sun Microsystems, Inc.
28 * Mountain View, California 94043
32 * rpc_svcout.c, Server-skeleton outputter for the RPC protocol compiler
33 * Copyright (C) 1987, Sun Microsytsems, Inc.
35 #include <afsconfig.h>
36 #include <afs/param.h>
50 #include "rpc_parse.h"
53 static char RQSTP[] = "rqstp";
54 static char TRANSP[] = "transp";
55 static char ARG[] = "argument";
56 static char RESULT[] = "result";
57 static char ROUTINE[] = "local";
59 /* static prototypes */
60 static void write_program(definition * def, char *storage);
61 static void printerr(char *err, char *transp);
62 static void printif(char *proc, char *transp, char *prefix, char *arg);
66 * write most of the service, that is, everything but the registrations.
75 for (l = defined; l != NULL; l = l->next) {
76 def = (definition *) l->val;
77 if (def->def_kind == DEF_PROGRAM) {
78 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
79 f_print(fout, "\nstatic void ");
80 pvname(def->def_name, vp->vers_num);
85 f_print(fout, "\n\n");
86 f_print(fout, "main()\n");
88 f_print(fout, "\tSVCXPRT *%s;\n", TRANSP);
90 for (l = defined; l != NULL; l = l->next) {
91 def = (definition *) l->val;
92 if (def->def_kind != DEF_PROGRAM) {
95 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
96 f_print(fout, "\tpmap_unset(%s, %s);\n", def->def_name,
104 * write a registration for the given transport
107 write_register(char *transp)
114 f_print(fout, "\t%s = svc%s_create(RPC_ANYSOCK", TRANSP, transp);
115 if (streq(transp, "tcp")) {
116 f_print(fout, ", 0, 0");
118 f_print(fout, ");\n");
119 f_print(fout, "\tif (%s == NULL) {\n", TRANSP);
120 f_print(fout, "\t\tfprintf(stderr, \"cannot create %s service.\\n\");\n",
122 f_print(fout, "\t\texit(1);\n");
123 f_print(fout, "\t}\n");
125 for (l = defined; l != NULL; l = l->next) {
126 def = (definition *) l->val;
127 if (def->def_kind != DEF_PROGRAM) {
130 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
131 f_print(fout, "\tif (!svc_register(%s, %s, %s, ", TRANSP,
132 def->def_name, vp->vers_name);
133 pvname(def->def_name, vp->vers_num);
134 f_print(fout, ", IPPROTO_%s)) {\n",
135 streq(transp, "udp") ? "UDP" : "TCP");
137 "\t\tfprintf(stderr, \"unable to register (%s, %s, %s).\\n\");\n",
138 def->def_name, vp->vers_name, transp);
139 f_print(fout, "\t\texit(1);\n");
140 f_print(fout, "\t}\n");
147 * write the rest of the service
152 f_print(fout, "\tsvc_run();\n");
153 f_print(fout, "\tfprintf(stderr, \"svc_run returned\\n\");\n");
154 f_print(fout, "\texit(1);\n");
155 f_print(fout, "}\n");
159 write_programs(char *storage)
164 for (l = defined; l != NULL; l = l->next) {
165 def = (definition *) l->val;
166 if (def->def_kind == DEF_PROGRAM) {
167 write_program(def, storage);
174 write_program(definition * def, char *storage)
180 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
182 if (storage != NULL) {
183 f_print(fout, "%s ", storage);
185 f_print(fout, "void\n");
186 pvname(def->def_name, vp->vers_num);
187 f_print(fout, "(%s, %s)\n", RQSTP, TRANSP);
188 f_print(fout, " struct svc_req *%s;\n", RQSTP);
189 f_print(fout, " SVCXPRT *%s;\n", TRANSP);
190 f_print(fout, "{\n");
193 f_print(fout, "\tunion {\n");
194 for (proc = vp->procs; proc != NULL; proc = proc->next) {
195 if (streq(proc->arg_type, "void")) {
199 f_print(fout, "\t\t");
200 ptype(proc->arg_prefix, proc->arg_type, 0);
201 pvname(proc->proc_name, vp->vers_num);
202 f_print(fout, "_arg;\n");
205 f_print(fout, "\t\tint fill;\n");
207 f_print(fout, "\t} %s;\n", ARG);
208 f_print(fout, "\tchar *%s;\n", RESULT);
209 f_print(fout, "\tbool_t (*xdr_%s)(), (*xdr_%s)();\n", ARG, RESULT);
210 f_print(fout, "\tchar *(*%s)();\n", ROUTINE);
212 f_print(fout, "\tswitch (%s->rq_proc) {\n", RQSTP);
214 if (!nullproc(vp->procs)) {
215 f_print(fout, "\tcase NULLPROC:\n");
216 f_print(fout, "\t\tsvc_sendreply(%s, xdr_void, NULL);\n", TRANSP);
217 f_print(fout, "\t\treturn;\n\n");
219 for (proc = vp->procs; proc != NULL; proc = proc->next) {
220 f_print(fout, "\tcase %s:\n", proc->proc_name);
221 f_print(fout, "\t\txdr_%s = xdr_%s;\n", ARG,
222 stringfix(proc->arg_type));
223 f_print(fout, "\t\txdr_%s = xdr_%s;\n", RESULT,
224 stringfix(proc->res_type));
225 f_print(fout, "\t\t%s = (char *(*)()) ", ROUTINE);
226 pvname(proc->proc_name, vp->vers_num);
227 f_print(fout, ";\n");
228 f_print(fout, "\t\tbreak;\n\n");
230 f_print(fout, "\tdefault:\n");
231 printerr("noproc", TRANSP);
232 f_print(fout, "\t\treturn;\n");
233 f_print(fout, "\t}\n");
235 f_print(fout, "\tmemset(&%s, 0, sizeof(%s));\n", ARG, ARG);
236 printif("getargs", TRANSP, "&", ARG);
237 printerr("decode", TRANSP);
238 f_print(fout, "\t\treturn;\n");
239 f_print(fout, "\t}\n");
241 f_print(fout, "\t%s = (*%s)(&%s, %s);\n", RESULT, ROUTINE, ARG,
244 "\tif (%s != NULL && !svc_sendreply(%s, xdr_%s, %s)) {\n",
245 RESULT, TRANSP, RESULT, RESULT);
246 printerr("systemerr", TRANSP);
247 f_print(fout, "\t}\n");
249 printif("freeargs", TRANSP, "&", ARG);
251 "\t\tfprintf(stderr, \"unable to free arguments\\n\");\n");
252 f_print(fout, "\t\texit(1);\n");
253 f_print(fout, "\t}\n");
255 f_print(fout, "}\n\n");
260 printerr(char *err, char *transp)
262 f_print(fout, "\t\tsvcerr_%s(%s);\n", err, transp);
266 printif(char *proc, char *transp, char *prefix, char *arg)
268 f_print(fout, "\tif (!svc_%s(%s, xdr_%s, %s%s)) {\n", proc, transp, arg,
274 nullproc(proc_list * proc)
276 for (; proc != NULL; proc = proc->next) {
277 if (streq(proc->proc_num, "0")) {