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