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