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