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