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