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