1 /* $NetBSD: rpc_hout.c,v 1.19 2002/06/11 06:06:19 itojun Exp $ */
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 or with the express written consent of
9 * Sun Microsystems, Inc.
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
27 * Sun Microsystems, Inc.
29 * Mountain View, California 94043
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
36 #include <sys/cdefs.h>
37 #if defined(__RCSID) && !defined(lint)
39 static char sccsid
[] = "@(#)rpc_hout.c 1.12 89/02/22 (C) 1987 SMI";
41 __RCSID("$NetBSD: rpc_hout.c,v 1.19 2002/06/11 06:06:19 itojun Exp $");
46 * rpc_hout.c, Header file outputter for the RPC protocol compiler
52 #include "rpc_parse.h"
55 static void pconstdef
__P((definition
*));
56 static void pargdef
__P((definition
*));
57 static void pstructdef
__P((definition
*));
58 static void puniondef
__P((definition
*));
59 static void pdefine
__P((char *, char *));
60 static void puldefine
__P((char *, char *));
61 static int define_printed
__P((proc_list
*, version_list
*));
62 static void pprogramdef
__P((definition
*));
63 static void penumdef
__P((definition
*));
64 static void ptypedef
__P((definition
*));
65 static int undefined2
__P((char *, char *));
68 * Print the C-version of an xdr definition
75 if (def
->def_kind
== DEF_PROGRAM
) /* handle data only */
78 if (def
->def_kind
!= DEF_CONST
) {
81 switch (def
->def_kind
) {
101 if (def
->def_kind
!= DEF_PROGRAM
&& def
->def_kind
!= DEF_CONST
) {
102 pxdrfuncdecl(def
->def_name
,
103 def
->def_kind
!= DEF_TYPEDEF
||
104 !isvectordef(def
->def
.ty
.old_type
, def
->def
.ty
.rel
));
114 switch (def
->def_kind
) {
129 pxdrfuncdecl(name
, pointerp
)
134 f_print(fout
, "#ifdef __cplusplus\n");
135 f_print(fout
, "extern \"C\" bool_t xdr_%s(XDR *, %s%s);\n",
137 name
, pointerp
? (" *") : "");
138 f_print(fout
, "#elif __STDC__\n");
139 f_print(fout
, "extern bool_t xdr_%s(XDR *, %s%s);\n",
141 name
, pointerp
? (" *") : "");
142 f_print(fout
, "#else /* Old Style C */\n");
143 f_print(fout
, "bool_t xdr_%s();\n", name
);
144 f_print(fout
, "#endif /* Old Style C */\n\n");
152 pdefine(def
->def_name
, def
->def
.co
);
154 /* print out the definitions for the arguments of functions in the
167 for (vers
= def
->def
.pr
.versions
; vers
!= NULL
; vers
= vers
->next
) {
168 for (plist
= vers
->procs
; plist
!= NULL
;
169 plist
= plist
->next
) {
171 if (!newstyle
|| plist
->arg_num
< 2) {
172 continue; /* old style or single args */
174 name
= plist
->args
.argname
;
175 f_print(fout
, "struct %s {\n", name
);
176 for (l
= plist
->args
.decls
;
177 l
!= NULL
; l
= l
->next
) {
178 pdeclaration(name
, &l
->decl
, 1, ";\n");
180 f_print(fout
, "};\n");
181 f_print(fout
, "typedef struct %s %s;\n", name
, name
);
182 pxdrfuncdecl(name
, 1);
195 char *name
= def
->def_name
;
197 f_print(fout
, "struct %s {\n", name
);
198 for (l
= def
->def
.st
.decls
; l
!= NULL
; l
= l
->next
) {
199 pdeclaration(name
, &l
->decl
, 1, ";\n");
201 f_print(fout
, "};\n");
202 f_print(fout
, "typedef struct %s %s;\n", name
, name
);
210 char *name
= def
->def_name
;
213 f_print(fout
, "struct %s {\n", name
);
214 decl
= &def
->def
.un
.enum_decl
;
215 if (streq(decl
->type
, "bool")) {
216 f_print(fout
, "\tbool_t %s;\n", decl
->name
);
218 f_print(fout
, "\t%s %s;\n", decl
->type
, decl
->name
);
220 f_print(fout
, "\tunion {\n");
221 for (l
= def
->def
.un
.cases
; l
!= NULL
; l
= l
->next
) {
222 if (l
->contflag
== 0)
223 pdeclaration(name
, &l
->case_decl
, 2, ";\n");
225 decl
= def
->def
.un
.default_decl
;
226 if (decl
&& !streq(decl
->type
, "void")) {
227 pdeclaration(name
, decl
, 2, ";\n");
229 f_print(fout
, "\t} %s_u;\n", name
);
230 f_print(fout
, "};\n");
231 f_print(fout
, "typedef struct %s %s;\n", name
, name
);
239 f_print(fout
, "#define %s %s\n", name
, num
);
247 f_print(fout
, "#define %s %s\n", name
, num
);
251 define_printed(stop
, start
)
258 for (vers
= start
; vers
!= NULL
; vers
= vers
->next
) {
259 for (proc
= vers
->procs
; proc
!= NULL
; proc
= proc
->next
) {
263 if (streq(proc
->proc_name
, stop
->proc_name
)) {
268 errx(1, "Internal error %s, %d: procedure not found",
284 puldefine(def
->def_name
, def
->def
.pr
.prog_num
);
285 for (vers
= def
->def
.pr
.versions
; vers
!= NULL
; vers
= vers
->next
) {
287 f_print(fout
, "extern struct rpcgen_table %s_%s_table[];\n",
288 locase(def
->def_name
), vers
->vers_num
);
289 f_print(fout
, "extern %s_%s_nproc;\n",
290 locase(def
->def_name
), vers
->vers_num
);
292 puldefine(vers
->vers_name
, vers
->vers_num
);
294 /* Print out 3 definitions, one for ANSI-C, another for C++, a
295 * third for old style C */
297 for (i
= 0; i
< 3; i
++) {
299 f_print(fout
, "\n#ifdef __cplusplus\n");
300 ext
= "extern \"C\" ";
303 f_print(fout
, "\n#elif __STDC__\n");
306 f_print(fout
, "\n#else /* Old Style C */\n");
311 for (proc
= vers
->procs
; proc
!= NULL
; proc
= proc
->next
) {
312 if (!define_printed(proc
, def
->def
.pr
.versions
)) {
313 puldefine(proc
->proc_name
, proc
->proc_num
);
315 f_print(fout
, "%s", ext
);
316 pprocdef(proc
, vers
, "CLIENT *", 0, i
);
317 f_print(fout
, "%s", ext
);
318 pprocdef(proc
, vers
, "struct svc_req *", 1, i
);
323 f_print(fout
, "#endif /* Old Style C */\n");
328 pprocdef(proc
, vp
, addargtype
, server_p
, mode
)
339 f_print(fout
, "bool_t ");
341 f_print(fout
, "enum clnt_stat ");
343 ptype(proc
->res_prefix
, proc
->res_type
, 1);
347 pvname_svc(proc
->proc_name
, vp
->vers_num
);
349 pvname(proc
->proc_name
, vp
->vers_num
);
352 * mode 0 == cplusplus, mode 1 = ANSI-C, mode 2 = old style C
354 if (mode
== 0 || mode
== 1) {
356 if (proc
->arg_num
< 2 && newstyle
&&
357 streq(proc
->args
.decls
->decl
.type
, "void")) {
358 /* 0 argument in new style: do nothing */
360 for (dl
= proc
->args
.decls
; dl
!= NULL
; dl
= dl
->next
) {
361 ptype(dl
->decl
.prefix
, dl
->decl
.type
, 1);
368 if (streq(proc
->res_type
, "void"))
369 f_print(fout
, "char");
371 ptype(proc
->res_prefix
, proc
->res_type
, 0);
372 if (!isvectordef(proc
->res_type
, REL_ALIAS
))
376 f_print(fout
, "%s);\n", addargtype
);
379 f_print(fout
, "();\n");
387 char *name
= def
->def_name
;
393 f_print(fout
, "enum %s {\n", name
);
394 for (l
= def
->def
.en
.vals
; l
!= NULL
; l
= l
->next
) {
395 f_print(fout
, "%s\t%s", first
, l
->name
);
397 f_print(fout
, " = %s", l
->assignment
);
398 last
= l
->assignment
;
402 f_print(fout
, " = %d", count
++);
404 f_print(fout
, " = %s + %d", last
, count
++);
409 f_print(fout
, "\n};\n");
410 f_print(fout
, "typedef enum %s %s;\n", name
, name
);
417 char *name
= def
->def_name
;
418 char *old
= def
->def
.ty
.old_type
;
419 char prefix
[8]; /* enough to contain "struct ", including NUL */
420 relation rel
= def
->def
.ty
.rel
;
423 if (!streq(name
, old
)) {
424 if (streq(old
, "string")) {
428 if (streq(old
, "opaque")) {
431 if (streq(old
, "bool")) {
434 if (undefined2(old
, name
) && def
->def
.ty
.old_prefix
) {
435 s_print(prefix
, "%s ", def
->def
.ty
.old_prefix
);
439 f_print(fout
, "typedef ");
442 f_print(fout
, "struct {\n");
443 f_print(fout
, "\tu_int %s_len;\n", name
);
444 f_print(fout
, "\t%s%s *%s_val;\n", prefix
, old
, name
);
445 f_print(fout
, "} %s", name
);
448 f_print(fout
, "%s%s *%s", prefix
, old
, name
);
451 f_print(fout
, "%s%s %s[%s]", prefix
, old
, name
,
452 def
->def
.ty
.array_max
);
455 f_print(fout
, "%s%s %s", prefix
, old
, name
);
458 f_print(fout
, ";\n");
463 pdeclaration(name
, dec
, tab
, separator
)
469 char buf
[8]; /* enough to hold "struct ", include NUL */
473 if (streq(dec
->type
, "void")) {
477 if (streq(dec
->type
, name
) && !dec
->prefix
) {
478 f_print(fout
, "struct ");
480 if (streq(dec
->type
, "string")) {
481 f_print(fout
, "char *%s", dec
->name
);
484 if (streq(dec
->type
, "bool")) {
487 if (streq(dec
->type
, "opaque")) {
491 s_print(buf
, "%s ", dec
->prefix
);
498 f_print(fout
, "%s%s %s", prefix
, type
, dec
->name
);
501 f_print(fout
, "%s%s %s[%s]", prefix
, type
, dec
->name
,
505 f_print(fout
, "%s%s *%s", prefix
, type
, dec
->name
);
508 f_print(fout
, "struct {\n");
510 f_print(fout
, "\tu_int %s_len;\n", dec
->name
);
512 f_print(fout
, "\t%s%s *%s_val;\n", prefix
, type
, dec
->name
);
514 f_print(fout
, "} %s", dec
->name
);
518 f_print(fout
, "%s", separator
);
522 undefined2(type
, stop
)
529 for (l
= defined
; l
!= NULL
; l
= l
->next
) {
530 def
= (definition
*) l
->val
;
531 if (def
->def_kind
!= DEF_PROGRAM
) {
532 if (streq(def
->def_name
, stop
)) {
535 if (streq(def
->def_name
, type
)) {