4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
26 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
29 * University Copyright- Copyright (c) 1982, 1986, 1988
30 * The Regents of the University of California
33 * University Acknowledgment- Portions of this document are derived from
34 * software developed by the University of California, Berkeley, and its
39 * rpc_hout.c, Header file outputter for the RPC protocol compiler
44 #include "rpc_parse.h"
47 extern void pprocdef(proc_list
*, version_list
*, char *, int, int);
48 extern void pdeclaration(char *, declaration
*, int, char *);
50 static void storexdrfuncdecl(char *, int);
51 static void pconstdef(definition
*);
52 static void pstructdef(definition
*);
53 static void puniondef(definition
*);
54 static void pdefine(char *, char *);
55 static void pprogramdef(definition
*);
56 static void parglist(proc_list
*, char *);
57 static void penumdef(definition
*);
58 static void ptypedef(definition
*);
59 static uint_t
undefined2(char *, char *);
68 * Print the C-version of an xdr definition
71 print_datadef(definition
*def
)
73 if (def
->def_kind
== DEF_PROGRAM
) /* handle data only */
76 if (def
->def_kind
!= DEF_CONST
)
78 switch (def
->def_kind
) {
98 if (def
->def_kind
!= DEF_PROGRAM
&& def
->def_kind
!= DEF_CONST
)
99 storexdrfuncdecl(def
->def_name
, def
->def_kind
!= DEF_TYPEDEF
||
100 !isvectordef(def
->def
.ty
.old_type
, def
->def
.ty
.rel
));
105 print_funcdef(definition
*def
)
107 switch (def
->def_kind
) {
116 * store away enough information to allow the XDR functions to be spat
117 * out at the end of the file
120 storexdrfuncdecl(char *name
, int pointerp
)
124 xdrptr
= malloc(sizeof (struct xdrfunc
));
127 xdrptr
->pointerp
= pointerp
;
130 if (xdrfunc_tail
== NULL
) {
131 xdrfunc_head
= xdrptr
;
132 xdrfunc_tail
= xdrptr
;
134 xdrfunc_tail
->next
= xdrptr
;
135 xdrfunc_tail
= xdrptr
;
142 print_xdr_func_def(char *name
, int pointerp
, int i
)
145 f_print(fout
, "extern bool_t xdr_%s();\n", name
);
147 f_print(fout
, "extern bool_t xdr_%s(XDR *, %s%s);\n", name
,
148 name
, pointerp
? "*" : "");
153 pconstdef(definition
*def
)
155 pdefine(def
->def_name
, def
->def
.co
);
159 * print out the definitions for the arguments of functions in the
163 pargdef(definition
*def
)
170 for (vers
= def
->def
.pr
.versions
; vers
!= NULL
; vers
= vers
->next
) {
171 for (plist
= vers
->procs
; plist
!= NULL
; plist
= plist
->next
) {
172 if (!newstyle
|| plist
->arg_num
< 2)
173 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
; l
!= NULL
; l
= l
->next
)
177 pdeclaration(name
, &l
->decl
, 1, ";\n");
178 f_print(fout
, "};\n");
179 f_print(fout
, "typedef struct %s %s;\n", name
, name
);
180 storexdrfuncdecl(name
, 1);
188 pstructdef(definition
*def
)
191 char *name
= def
->def_name
;
193 f_print(fout
, "struct %s {\n", name
);
194 for (l
= def
->def
.st
.decls
; l
!= NULL
; l
= l
->next
)
195 pdeclaration(name
, &l
->decl
, 1, ";\n");
196 f_print(fout
, "};\n");
197 f_print(fout
, "typedef struct %s %s;\n", name
, name
);
201 puniondef(definition
*def
)
204 char *name
= def
->def_name
;
207 f_print(fout
, "struct %s {\n", name
);
208 decl
= &def
->def
.un
.enum_decl
;
209 if (streq(decl
->type
, "bool"))
210 f_print(fout
, "\tbool_t %s;\n", decl
->name
);
212 f_print(fout
, "\t%s %s;\n", decl
->type
, decl
->name
);
213 f_print(fout
, "\tunion {\n");
214 for (l
= def
->def
.un
.cases
; l
!= NULL
; l
= l
->next
) {
215 if (l
->contflag
== 0)
216 pdeclaration(name
, &l
->case_decl
, 2, ";\n");
218 decl
= def
->def
.un
.default_decl
;
219 if (decl
&& !streq(decl
->type
, "void"))
220 pdeclaration(name
, decl
, 2, ";\n");
221 f_print(fout
, "\t} %s_u;\n", name
);
222 f_print(fout
, "};\n");
223 f_print(fout
, "typedef struct %s %s;\n", name
, name
);
227 pdefine(char *name
, char *num
)
229 f_print(fout
, "#define\t%s %s\n", name
, num
);
233 puldefine(char *name
, char *num
, enum rpc_gvc which
)
239 f_print(fout
, "#define\t%s\t%s\n", name
, num
);
247 define_printed(proc_list
*stop
, version_list
*start
)
252 for (vers
= start
; vers
!= NULL
; vers
= vers
->next
) {
253 for (proc
= vers
->procs
; proc
!= NULL
; proc
= proc
->next
) {
256 if (streq(proc
->proc_name
, stop
->proc_name
))
265 pfreeprocdef(char *name
, char *vers
, int mode
)
267 f_print(fout
, "extern int ");
270 f_print(fout
, "_freeresult(SVCXPRT *, xdrproc_t, caddr_t);\n");
272 f_print(fout
, "_freeresult();\n");
276 pprogramdef(definition
*def
)
285 puldefine(def
->def_name
, def
->def
.pr
.prog_num
, PROGRAM
);
286 for (vers
= def
->def
.pr
.versions
; vers
!= NULL
; vers
= vers
->next
) {
289 "extern struct rpcgen_table %s_%s_table[];\n",
290 locase(def
->def_name
), vers
->vers_num
);
292 "extern int %s_%s_nproc;\n",
293 locase(def
->def_name
), vers
->vers_num
);
295 puldefine(vers
->vers_name
, vers
->vers_num
, VERSION
);
298 * Print out 2 definitions, one for ANSI-C, another for
304 for (proc
= vers
->procs
; proc
!= NULL
;
306 if (!define_printed(proc
, def
->def
.pr
.versions
))
307 puldefine(proc
->proc_name
,
308 proc
->proc_num
, PROCEDURE
);
309 f_print(fout
, "%s", ext
);
310 pprocdef(proc
, vers
, NULL
, 0, 2);
313 f_print(fout
, "%s", ext
);
314 pprocdef(proc
, vers
, NULL
, 1, 2);
317 pfreeprocdef(def
->def_name
, vers
->vers_num
, 2);
319 for (i
= 1; i
< 3; i
++) {
321 f_print(fout
, "\n#if defined(__STDC__)"
322 " || defined(__cplusplus)\n");
325 f_print(fout
, "\n#else /* K&R C */\n");
329 for (proc
= vers
->procs
; proc
!= NULL
;
331 if (!define_printed(proc
,
332 def
->def
.pr
.versions
)) {
333 puldefine(proc
->proc_name
,
334 proc
->proc_num
, PROCEDURE
);
336 f_print(fout
, "%s", ext
);
337 pprocdef(proc
, vers
, "CLIENT *", 0, i
);
338 f_print(fout
, "%s", ext
);
340 "struct svc_req *", 1, i
);
342 pfreeprocdef(def
->def_name
, vers
->vers_num
, i
);
344 f_print(fout
, "#endif /* K&R C */\n");
350 pprocdef(proc_list
*proc
, version_list
*vp
, char *addargtype
, int server_p
,
354 /* Print MT style stubs */
356 f_print(fout
, "bool_t ");
358 f_print(fout
, "enum clnt_stat ");
360 ptype(proc
->res_prefix
, proc
->res_type
, 1);
364 pvname_svc(proc
->proc_name
, vp
->vers_num
);
366 pvname(proc
->proc_name
, vp
->vers_num
);
369 * mode 1 = ANSI-C, mode 2 = K&R C
372 parglist(proc
, addargtype
);
374 f_print(fout
, "();\n");
377 /* print out argument list of procedure */
379 parglist(proc_list
*proc
, char *addargtype
)
382 int oneway
= streq(proc
->res_type
, "oneway");
385 if (proc
->arg_num
< 2 && newstyle
&&
386 streq(proc
->args
.decls
->decl
.type
, "void")) {
387 /* 0 argument in new style: do nothing */
390 for (dl
= proc
->args
.decls
; dl
!= NULL
; dl
= dl
->next
) {
391 ptype(dl
->decl
.prefix
, dl
->decl
.type
, 1);
392 if (!newstyle
|| (dl
->decl
.rel
== REL_POINTER
))
394 /* old style passes by reference */
399 if (mtflag
&& !oneway
) {
400 ptype(proc
->res_prefix
, proc
->res_type
, 1);
401 f_print(fout
, "*, ");
404 f_print(fout
, "%s);\n", addargtype
);
408 penumdef(definition
*def
)
410 char *name
= def
->def_name
;
415 f_print(fout
, "enum %s {\n", name
);
416 for (l
= def
->def
.en
.vals
; l
!= NULL
; l
= l
->next
) {
417 f_print(fout
, "\t%s", l
->name
);
419 f_print(fout
, " = %s", l
->assignment
);
420 last
= l
->assignment
;
424 f_print(fout
, " = %d", count
++);
426 f_print(fout
, " = %s + %d", last
, count
++);
429 f_print(fout
, ",\n");
433 f_print(fout
, "};\n");
434 f_print(fout
, "typedef enum %s %s;\n", name
, name
);
438 ptypedef(definition
*def
)
440 char *name
= def
->def_name
;
441 char *old
= def
->def
.ty
.old_type
;
442 char prefix
[8]; /* enough to contain "struct ", including NUL */
443 relation rel
= def
->def
.ty
.rel
;
446 if (!streq(name
, old
)) {
447 if (streq(old
, "string")) {
450 } else if (streq(old
, "opaque")) {
452 } else if (streq(old
, "bool")) {
455 if (undefined2(old
, name
) && def
->def
.ty
.old_prefix
)
456 (void) snprintf(prefix
, sizeof (prefix
), "%s ",
457 def
->def
.ty
.old_prefix
);
460 f_print(fout
, "typedef ");
463 f_print(fout
, "struct {\n");
464 f_print(fout
, "\tu_int %s_len;\n", name
);
465 f_print(fout
, "\t%s%s *%s_val;\n", prefix
, old
, name
);
466 f_print(fout
, "} %s", name
);
469 f_print(fout
, "%s%s *%s", prefix
, old
, name
);
472 f_print(fout
, "%s%s %s[%s]", prefix
, old
, name
,
473 def
->def
.ty
.array_max
);
476 f_print(fout
, "%s%s %s", prefix
, old
, name
);
479 f_print(fout
, ";\n");
484 pdeclaration(char *name
, declaration
*dec
, int tab
, char *separator
)
486 char buf
[8]; /* enough to hold "struct ", include NUL */
490 if (streq(dec
->type
, "void"))
493 if (streq(dec
->type
, name
) && !dec
->prefix
)
494 f_print(fout
, "struct ");
495 if (streq(dec
->type
, "string")) {
496 f_print(fout
, "char *%s", dec
->name
);
499 if (streq(dec
->type
, "bool")) {
501 } else if (streq(dec
->type
, "opaque")) {
505 (void) snprintf(buf
, sizeof (buf
),
513 f_print(fout
, "%s%s %s", prefix
, type
, dec
->name
);
516 f_print(fout
, "%s%s %s[%s]", prefix
, type
, dec
->name
,
520 f_print(fout
, "%s%s *%s", prefix
, type
, dec
->name
);
523 f_print(fout
, "struct {\n");
525 f_print(fout
, "\tu_int %s_len;\n", dec
->name
);
528 "\t%s%s *%s_val;\n", prefix
, type
, dec
->name
);
530 f_print(fout
, "} %s", dec
->name
);
534 /* LINTED variable format */
535 f_print(fout
, separator
);
539 undefined2(char *type
, char *stop
)
544 for (l
= defined
; l
!= NULL
; l
= l
->next
) {
545 def
= (definition
*) l
->val
;
546 if (def
->def_kind
!= DEF_PROGRAM
) {
547 if (streq(def
->def_name
, stop
))
549 if (streq(def
->def_name
, type
))