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.
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.
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.
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.
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.
26 * Sun Microsystems, Inc.
28 * Mountain View, California 94043
32 static char sccsid
[] = "@(#)rpc_hout.c 1.12 89/02/22 (C) 1987 SMI";
36 * rpc_hout.c, Header file outputter for the RPC protocol compiler
40 #include "rpc_parse.h"
42 #include "rpc_output.h"
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
);
62 * Print the C-version of an xdr definition
65 print_datadef(definition
*def
)
68 if (def
->def_kind
== DEF_PROGRAM
) /* handle data only */
71 if (def
->def_kind
!= DEF_CONST
) {
74 switch (def
->def_kind
) {
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
));
104 print_funcdef(definition
*def
)
106 switch (def
->def_kind
) {
117 pxdrfuncdecl(char *name
, int pointerp
)
120 "#ifdef __cplusplus \n"
121 "extern \"C\" bool_t xdr_%s(XDR *, %s%s);\n"
123 "extern bool_t xdr_%s(XDR *, %s%s);\n"
124 "#else /* Old Style C */ \n"
126 "#endif /* Old Style C */ \n\n",
127 name
, name
, pointerp
? "*" : "",
128 name
, name
, pointerp
? "*" : "",
134 pconstdef(definition
*def
)
136 pdefine(def
->def_name
, def
->def
.co
);
139 /* print out the definitions for the arguments of functions in the
143 pargdef(definition
*def
)
151 for (vers
= def
->def
.pr
.versions
; vers
!= NULL
; vers
= vers
->next
) {
152 for(plist
= vers
->procs
; plist
!= NULL
;
153 plist
= plist
->next
) {
155 if (!newstyle
|| plist
->arg_num
< 2) {
156 continue; /* old style or single args */
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" );
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" );
175 pstructdef(definition
*def
)
178 char *name
= def
->def_name
;
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");
184 f_print(fout
, "};\n");
185 f_print(fout
, "typedef struct %s %s;\n", name
, name
);
189 puniondef(definition
*def
)
192 char *name
= def
->def_name
;
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
);
200 f_print(fout
, "\t%s %s;\n", decl
->type
, decl
->name
);
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");
207 decl
= def
->def
.un
.default_decl
;
208 if (decl
&& !streq(decl
->type
, "void")) {
209 pdeclaration(name
, decl
, 2, ";\n");
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
);
217 pdefine(char *name
, char *num
)
219 f_print(fout
, "#define %s %s\n", name
, num
);
223 puldefine(char *name
, char *num
)
225 f_print(fout
, "#define %s ((u_int32_t)%s)\n", name
, num
);
229 define_printed(proc_list
*stop
, version_list
*start
)
234 for (vers
= start
; vers
!= NULL
; vers
= vers
->next
) {
235 for (proc
= vers
->procs
; proc
!= NULL
; proc
= proc
->next
) {
238 } else if (streq(proc
->proc_name
, stop
->proc_name
)) {
248 pprogramdef(definition
*def
)
257 puldefine(def
->def_name
, def
->def
.pr
.prog_num
);
258 for (vers
= def
->def
.pr
.versions
; vers
!= NULL
; vers
= vers
->next
) {
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
);
265 puldefine(vers
->vers_name
, vers
->vers_num
);
268 * Print out 3 definitions, one for ANSI-C, another for C++,
269 * a third for old style C
272 for (i
= 0; i
< 3; i
++) {
274 f_print(fout
, "\n#ifdef __cplusplus\n");
275 ext
= "extern \"C\" ";
277 f_print(fout
, "\n#elif __STDC__\n");
280 f_print(fout
, "\n#else /* Old Style C */ \n");
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
);
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
);
297 f_print(fout
, "#endif /* Old Style C */ \n");
302 pprocdef(proc_list
*proc
, version_list
*vp
, char *addargtype
,
303 int server_p
, int mode
)
305 ptype(proc
->res_prefix
, proc
->res_type
, 1);
308 pvname_svc(proc
->proc_name
, vp
->vers_num
);
310 pvname(proc
->proc_name
, vp
->vers_num
);
313 * mode 0 == cplusplus, mode 1 = ANSI-C, mode 2 = old style C
315 if (mode
== 0 || mode
== 1)
316 parglist(proc
, addargtype
);
318 f_print(fout
, "();\n");
323 /* print out argument list of procedure */
325 parglist(proc_list
*proc
, char *addargtype
)
331 if (proc
->arg_num
< 2 && newstyle
&&
332 streq(proc
->args
.decls
->decl
.type
, "void")) {
333 /* 0 argument in new style: do nothing */
335 for (dl
= proc
->args
.decls
; dl
!= NULL
; dl
= dl
->next
) {
336 ptype(dl
->decl
.prefix
, dl
->decl
.type
, 1);
338 f_print(fout
, "*"); /* old style passes by reference */
344 f_print(fout
, "%s);\n", addargtype
);
348 penumdef(definition
*def
)
350 char *name
= def
->def_name
;
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
);
359 f_print(fout
, " = %s", l
->assignment
);
360 last
= l
->assignment
;
364 f_print(fout
, " = %d", count
++);
366 f_print(fout
, " = %s + %d", last
, count
++);
369 f_print(fout
, ",\n");
371 f_print(fout
, "};\n");
372 f_print(fout
, "typedef enum %s %s;\n", name
, name
);
376 ptypedef(definition
*def
)
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
;
384 if (!streq(name
, old
)) {
385 if (streq(old
, "string")) {
388 } else if (streq(old
, "opaque")) {
390 } else if (streq(old
, "bool")) {
393 if (undefined2(old
, name
) && def
->def
.ty
.old_prefix
) {
394 s_print(prefix
, "%s ", def
->def
.ty
.old_prefix
);
398 f_print(fout
, "typedef ");
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
);
407 f_print(fout
, "%s%s *%s", prefix
, old
, name
);
410 f_print(fout
, "%s%s %s[%s]", prefix
, old
, name
,
411 def
->def
.ty
.array_max
);
414 f_print(fout
, "%s%s %s", prefix
, old
, name
);
417 f_print(fout
, ";\n");
422 pdeclaration(char *name
, declaration
*dec
, int tab
, char *separator
)
424 char buf
[8]; /* enough to hold "struct ", include NUL */
428 if (streq(dec
->type
, "void")) {
432 if (streq(dec
->type
, name
) && !dec
->prefix
) {
433 f_print(fout
, "struct ");
435 if (streq(dec
->type
, "string")) {
436 f_print(fout
, "char *%s", dec
->name
);
439 if (streq(dec
->type
, "bool")) {
441 } else if (streq(dec
->type
, "opaque")) {
445 s_print(buf
, "%s ", dec
->prefix
);
452 f_print(fout
, "%s%s %s", prefix
, type
, dec
->name
);
455 f_print(fout
, "%s%s %s[%s]", prefix
, type
, dec
->name
,
459 f_print(fout
, "%s%s *%s", prefix
, type
, dec
->name
);
462 f_print(fout
, "struct {\n");
464 f_print(fout
, "\tu_int %s_len;\n", dec
->name
);
466 f_print(fout
, "\t%s%s *%s_val;\n", prefix
, type
, dec
->name
);
468 f_print(fout
, "} %s", dec
->name
);
472 f_print(fout
, separator
);
476 undefined2(char *type
, char *stop
)
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
)) {
486 } else if (streq(def
->def_name
, type
)) {