]> git.decadent.org.uk Git - nfs-utils.git/blob - tools/rpcgen/rpc_hout.c
Imported Debian patch 1.1.1-11
[nfs-utils.git] / tools / rpcgen / rpc_hout.c
1 /*
2  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3  * unrestricted use provided that this legend is included on all tape
4  * media and as a part of the software program in whole or part.  Users
5  * may copy or modify Sun RPC without charge, but are not authorized
6  * to license or distribute it to anyone else except as part of a product or
7  * program developed by the user or with the express written consent of
8  * Sun Microsystems, Inc.
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 #if 0
32 static char sccsid[] = "@(#)rpc_hout.c 1.12 89/02/22 (C) 1987 SMI";
33 #endif
34
35 /*
36  * rpc_hout.c, Header file outputter for the RPC protocol compiler 
37  */
38 #include <stdio.h>
39 #include <ctype.h>
40 #include "rpc_parse.h"
41 #include "rpc_util.h"
42 #include "rpc_output.h"
43
44
45 static int      undefined2(char *type, char *stop);
46 static void     pxdrfuncdecl(char *name, int pointerp);
47 static void     pconstdef(definition *def);
48 static void     pargdef(definition *def);
49 static void     pstructdef(definition *def);
50 static void     puniondef(definition *def);
51 static void     pdefine(char *name, char *num);
52 static void     puldefine(char *name, char *num);
53 static int      define_printed(proc_list *stop, version_list *start);
54 static void     pprogramdef(definition *def);
55 static void     pprocdef(proc_list *proc, version_list *vp,
56                                 char *addargtype, int server_p, int mode);
57 static void     parglist(proc_list *proc, char *addargtype);
58 static void     penumdef(definition *def);
59 static void     ptypedef(definition *def);
60
61 /*
62  * Print the C-version of an xdr definition 
63  */
64 void
65 print_datadef(definition *def)
66 {
67
68         if (def->def_kind == DEF_PROGRAM )  /* handle data only */
69                 return;
70
71         if (def->def_kind != DEF_CONST) {
72                 f_print(fout, "\n");
73         }
74         switch (def->def_kind) {
75         case DEF_STRUCT:
76                 pstructdef(def);
77                 break;
78         case DEF_UNION:
79                 puniondef(def);
80                 break;
81         case DEF_ENUM:
82                 penumdef(def);
83                 break;
84         case DEF_TYPEDEF:
85                 ptypedef(def);
86                 break;
87         case DEF_PROGRAM:
88                 pprogramdef(def);
89                 break;
90         case DEF_CONST:
91                 pconstdef(def);
92                 break;
93         }
94         if (def->def_kind != DEF_PROGRAM && def->def_kind != DEF_CONST) {
95           pxdrfuncdecl( def->def_name,
96                        def->def_kind != DEF_TYPEDEF ||
97                        !isvectordef(def->def.ty.old_type, def->def.ty.rel));
98
99         }
100 }
101
102
103 void
104 print_funcdef(definition *def)
105 {
106         switch (def->def_kind) {
107         case DEF_PROGRAM:
108                 f_print(fout, "\n");
109                 pprogramdef(def);
110                 break;
111         default:
112                 break;
113         }
114 }
115
116 static void
117 pxdrfuncdecl(char *name, int pointerp)
118 {
119         f_print(fout,
120         "#ifdef __cplusplus \n"
121         "extern \"C\" bool_t xdr_%s(XDR *, %s%s);\n"
122         "#elif __STDC__ \n"
123         "extern  bool_t xdr_%s(XDR *, %s%s);\n"
124         "#else /* Old Style C */ \n"
125         "bool_t xdr_%s();\n"
126         "#endif /* Old Style C */ \n\n",
127         name, name, pointerp ? "*" : "",
128         name, name, pointerp ? "*" : "",
129         name);
130 }
131
132
133 static void
134 pconstdef(definition *def)
135 {
136         pdefine(def->def_name, def->def.co);
137 }
138
139 /* print out the definitions for the arguments of functions in the 
140    header file 
141 */
142 static  void
143 pargdef(definition *def)
144 {
145         decl_list *l;
146         version_list *vers;
147         char *name;
148         proc_list *plist;
149
150         
151         for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
152                         for(plist = vers->procs; plist != NULL; 
153                             plist = plist->next) {
154                                 
155                                 if (!newstyle || plist->arg_num < 2) {
156                                         continue; /* old style or single args */
157                                 }
158                                 name = plist->args.argname;
159                                 f_print(fout, "struct %s {\n", name);
160                                 for (l = plist->args.decls; 
161                                      l != NULL; l = l->next) {
162                                         pdeclaration(name, &l->decl, 1, ";\n" );
163                                 }
164                                 f_print(fout, "};\n");
165                                 f_print(fout, "typedef struct %s %s;\n", name, name);
166                                 pxdrfuncdecl(name, 0);
167                                 f_print( fout, "\n" );
168                         }
169                 }
170
171 }
172
173
174 static void
175 pstructdef(definition *def)
176 {
177         decl_list *l;
178         char *name = def->def_name;
179
180         f_print(fout, "struct %s {\n", name);
181         for (l = def->def.st.decls; l != NULL; l = l->next) {
182                 pdeclaration(name, &l->decl, 1, ";\n");
183         }
184         f_print(fout, "};\n");
185         f_print(fout, "typedef struct %s %s;\n", name, name);
186 }
187
188 static void
189 puniondef(definition *def)
190 {
191         case_list      *l;
192         char           *name = def->def_name;
193         declaration    *decl;
194
195         f_print(fout, "struct %s {\n", name);
196         decl = &def->def.un.enum_decl;
197         if (streq(decl->type, "bool")) {
198                 f_print(fout, "\tbool_t %s;\n", decl->name);
199         } else {
200                 f_print(fout, "\t%s %s;\n", decl->type, decl->name);
201         }
202         f_print(fout, "\tunion {\n");
203         for (l = def->def.un.cases; l != NULL; l = l->next) {
204                 if (l->contflag == 0)
205                         pdeclaration(name, &l->case_decl, 2, ";\n");
206         }
207         decl = def->def.un.default_decl;
208         if (decl && !streq(decl->type, "void")) {
209                 pdeclaration(name, decl, 2, ";\n");
210         }
211         f_print(fout, "\t} %s_u;\n", name);
212         f_print(fout, "};\n");
213         f_print(fout, "typedef struct %s %s;\n", name, name);
214 }
215
216 static void
217 pdefine(char *name, char *num)
218 {
219         f_print(fout, "#define %s %s\n", name, num);
220 }
221
222 static void
223 puldefine(char *name, char *num)
224 {
225         f_print(fout, "#define %s ((u_int32_t)%s)\n", name, num);
226 }
227
228 static int
229 define_printed(proc_list *stop, version_list *start)
230 {
231         version_list *vers;
232         proc_list *proc;
233
234         for (vers = start; vers != NULL; vers = vers->next) {
235                 for (proc = vers->procs; proc != NULL; proc = proc->next) {
236                         if (proc == stop) {
237                                 return (0);
238                         } else if (streq(proc->proc_name, stop->proc_name)) {
239                                 return (1);
240                         }
241                 }
242         }
243         abort();
244         /* NOTREACHED */
245 }
246
247 static void
248 pprogramdef(definition *def)
249 {
250         version_list   *vers;
251         proc_list      *proc;
252         int             i;
253         char           *ext;
254
255         pargdef(def);
256
257         puldefine(def->def_name, def->def.pr.prog_num);
258         for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
259                 if (tblflag) {
260                         f_print(fout, "extern struct rpcgen_table %s_%s_table[];\n",
261                                 locase(def->def_name), vers->vers_num);
262                         f_print(fout, "extern %s_%s_nproc;\n",
263                                 locase(def->def_name), vers->vers_num);
264                 }
265                 puldefine(vers->vers_name, vers->vers_num);
266
267                 /*
268                  * Print out 3 definitions, one for ANSI-C, another for C++,
269                  * a third for old style C
270                  */
271
272                 for (i = 0; i < 3; i++) {
273                         if (i == 0) {
274                                 f_print(fout, "\n#ifdef __cplusplus\n");
275                                 ext = "extern \"C\" ";
276                         } else if (i == 1) {
277                                 f_print(fout, "\n#elif __STDC__\n");
278                                 ext = "extern  ";
279                         } else {
280                                 f_print(fout, "\n#else /* Old Style C */ \n");
281                                 ext = "extern  ";
282                         }
283
284
285                         for (proc = vers->procs; proc != NULL; proc = proc->next) {
286                                 if (!define_printed(proc, def->def.pr.versions)) {
287                                         puldefine(proc->proc_name, proc->proc_num);
288                                 }
289                                 f_print(fout, "%s", ext);
290                                 pprocdef(proc, vers, "CLIENT *", 0, i);
291                                 f_print(fout, "%s", ext);
292                                 pprocdef(proc, vers, "struct svc_req *", 1, i);
293
294                         }
295
296                 }
297                 f_print(fout, "#endif /* Old Style C */ \n");
298         }
299 }
300
301 static void
302 pprocdef(proc_list *proc, version_list *vp, char *addargtype,
303                                 int server_p, int mode)
304 {
305         ptype(proc->res_prefix, proc->res_type, 1);
306         f_print(fout, "* ");
307         if (server_p)
308                 pvname_svc(proc->proc_name, vp->vers_num);
309         else
310                 pvname(proc->proc_name, vp->vers_num);
311
312         /*
313          * mode  0 == cplusplus, mode  1 = ANSI-C, mode 2 = old style C
314          */
315         if (mode == 0 || mode == 1)
316                 parglist(proc, addargtype);
317         else
318                 f_print(fout, "();\n");
319 }
320
321
322
323 /* print out argument list of procedure */
324 static void
325 parglist(proc_list *proc, char *addargtype)
326 {
327         decl_list      *dl;
328
329         f_print(fout, "(");
330
331         if (proc->arg_num < 2 && newstyle &&
332                 streq(proc->args.decls->decl.type, "void")) {
333                 /* 0 argument in new style:  do nothing */
334         } else {
335                 for (dl = proc->args.decls; dl != NULL; dl = dl->next) {
336                         ptype(dl->decl.prefix, dl->decl.type, 1);
337                         if (!newstyle)
338                                 f_print(fout, "*");     /* old style passes by reference */
339
340                         f_print(fout, ", ");
341                 }
342         }
343
344         f_print(fout, "%s);\n", addargtype);
345 }
346
347 static void
348 penumdef(definition *def)
349 {
350         char *name = def->def_name;
351         enumval_list *l;
352         char *last = NULL;
353         int count = 0;
354
355         f_print(fout, "enum %s {\n", name);
356         for (l = def->def.en.vals; l != NULL; l = l->next) {
357                 f_print(fout, "\t%s", l->name);
358                 if (l->assignment) {
359                         f_print(fout, " = %s", l->assignment);
360                         last = l->assignment;
361                         count = 1;
362                 } else {
363                         if (last == NULL) {
364                                 f_print(fout, " = %d", count++);
365                         } else {
366                                 f_print(fout, " = %s + %d", last, count++);
367                         }
368                 }
369                 f_print(fout, ",\n");
370         }
371         f_print(fout, "};\n");
372         f_print(fout, "typedef enum %s %s;\n", name, name);
373 }
374
375 static void
376 ptypedef(definition *def)
377 {
378         char *name = def->def_name;
379         char *old = def->def.ty.old_type;
380         char prefix[8]; /* enough to contain "struct ", including NUL */
381         relation rel = def->def.ty.rel;
382
383
384         if (!streq(name, old)) {
385                 if (streq(old, "string")) {
386                         old = "char";
387                         rel = REL_POINTER;
388                 } else if (streq(old, "opaque")) {
389                         old = "char";
390                 } else if (streq(old, "bool")) {
391                         old = "bool_t";
392                 }
393                 if (undefined2(old, name) && def->def.ty.old_prefix) {
394                         s_print(prefix, "%s ", def->def.ty.old_prefix);
395                 } else {
396                         prefix[0] = 0;
397                 }
398                 f_print(fout, "typedef ");
399                 switch (rel) {
400                 case REL_ARRAY:
401                         f_print(fout, "struct {\n");
402                         f_print(fout, "\tu_int %s_len;\n", name);
403                         f_print(fout, "\t%s%s *%s_val;\n", prefix, old, name);
404                         f_print(fout, "} %s", name);
405                         break;
406                 case REL_POINTER:
407                         f_print(fout, "%s%s *%s", prefix, old, name);
408                         break;
409                 case REL_VECTOR:
410                         f_print(fout, "%s%s %s[%s]", prefix, old, name,
411                                 def->def.ty.array_max);
412                         break;
413                 case REL_ALIAS:
414                         f_print(fout, "%s%s %s", prefix, old, name);
415                         break;
416                 }
417                 f_print(fout, ";\n");
418         }
419 }
420
421 void
422 pdeclaration(char *name, declaration *dec, int tab, char *separator)
423 {
424         char buf[8];    /* enough to hold "struct ", include NUL */
425         char *prefix;
426         char *type;
427
428         if (streq(dec->type, "void")) {
429                 return;
430         }
431         tabify(fout, tab);
432         if (streq(dec->type, name) && !dec->prefix) {
433                 f_print(fout, "struct ");
434         }
435         if (streq(dec->type, "string")) {
436                 f_print(fout, "char *%s", dec->name);
437         } else {
438                 prefix = "";
439                 if (streq(dec->type, "bool")) {
440                         type = "bool_t";
441                 } else if (streq(dec->type, "opaque")) {
442                         type = "char";
443                 } else {
444                         if (dec->prefix) {
445                                 s_print(buf, "%s ", dec->prefix);
446                                 prefix = buf;
447                         }
448                         type = dec->type;
449                 }
450                 switch (dec->rel) {
451                 case REL_ALIAS:
452                         f_print(fout, "%s%s %s", prefix, type, dec->name);
453                         break;
454                 case REL_VECTOR:
455                         f_print(fout, "%s%s %s[%s]", prefix, type, dec->name,
456                                 dec->array_max);
457                         break;
458                 case REL_POINTER:
459                         f_print(fout, "%s%s *%s", prefix, type, dec->name);
460                         break;
461                 case REL_ARRAY:
462                         f_print(fout, "struct {\n");
463                         tabify(fout, tab);
464                         f_print(fout, "\tu_int %s_len;\n", dec->name);
465                         tabify(fout, tab);
466                         f_print(fout, "\t%s%s *%s_val;\n", prefix, type, dec->name);
467                         tabify(fout, tab);
468                         f_print(fout, "} %s", dec->name);
469                         break;
470                 }
471         }
472         f_print(fout, separator );
473 }
474
475 static int
476 undefined2(char *type, char *stop)
477 {
478         list *l;
479         definition *def;
480
481         for (l = defined; l != NULL; l = l->next) {
482                 def = (definition *) l->val;
483                 if (def->def_kind != DEF_PROGRAM) {
484                         if (streq(def->def_name, stop)) {
485                                 return (1);
486                         } else if (streq(def->def_name, type)) {
487                                 return (0);
488                         }
489                 }
490         }
491         return (1);
492 }