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.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
29 * @(#)rpc_hout.c 1.12 89/02/22 (C) 1987 SMI
30 * $DragonFly: src/usr.bin/rpcgen/rpc_hout.c,v 1.5 2004/06/19 16:40:36 joerg Exp $
33 #ident "@(#)rpc_hout.c 1.16 94/04/25 SMI"
36 * rpc_hout.c, Header file outputter for the RPC protocol compiler
37 * Copyright (C) 1987, Sun Microsystems, Inc.
41 #include "rpc_parse.h"
44 void storexdrfuncdecl(char *, int);
45 static void pconstdef(definition
*);
46 static void pstructdef(definition
*);
47 static void puniondef(definition
*);
48 static void pprogramdef(definition
*);
49 static void pstructdef(definition
*);
50 static void penumdef(definition
*);
51 static void ptypedef(definition
*);
52 static void pdefine(char *, char *);
53 static int undefined2(char *, char *);
54 static void parglist(proc_list
*, char *);
55 static void pprocdef(proc_list
*, version_list
*, char *, int, int);
56 void pdeclaration(char *, declaration
*, int, char *);
58 static char RESULT
[] = "clnt_res";
61 * Print the C-version of an xdr definition
64 print_datadef(definition
*def
)
66 if (def
->def_kind
== DEF_PROGRAM
) /* handle data only */
69 if (def
->def_kind
!= DEF_CONST
)
72 switch (def
->def_kind
) {
92 if (def
->def_kind
!= DEF_PROGRAM
&& def
->def_kind
!= DEF_CONST
) {
93 if (def
->def_kind
!= DEF_TYPEDEF
||
94 !isvectordef(def
->def
.ty
.old_type
, def
->def
.ty
.rel
))
95 storexdrfuncdecl(def
->def_name
, 1);
97 storexdrfuncdecl(def
->def_name
, 0);
103 print_funcdef(definition
*def
)
105 switch (def
->def_kind
) {
115 /* store away enough information to allow the XDR functions to be spat
116 out at the end of the file */
119 storexdrfuncdecl(char *name
, int pointerp
)
123 xdrptr
= malloc(sizeof(struct xdrfunc
));
126 xdrptr
->pointerp
= pointerp
;
129 if (xdrfunc_tail
== NULL
)
130 xdrfunc_head
= xdrptr
;
132 xdrfunc_tail
->next
= xdrptr
;
133 xdrfunc_tail
= xdrptr
;
138 print_xdr_func_def(char *name
, int pointerp
, int i
)
141 f_print(fout
, "extern bool_t xdr_%s();\n", name
);
143 f_print(fout
, "extern bool_t xdr_%s(XDR *, %s%s);\n", name
,
144 name
, pointerp
? "*" : "");
149 pconstdef(definition
*def
)
151 pdefine(def
->def_name
, def
->def
.co
);
154 /* print out the definitions for the arguments of functions in the
158 pargdef(definition
*def
)
165 for (vers
= def
->def
.pr
.versions
; vers
!= NULL
; vers
= vers
->next
) {
166 for (plist
= vers
->procs
; plist
!= NULL
; plist
= plist
->next
) {
167 if (!newstyle
|| plist
->arg_num
< 2)
168 continue; /* old style or single args */
170 name
= plist
->args
.argname
;
171 f_print(fout
, "struct %s {\n", name
);
172 for (l
= plist
->args
.decls
; l
!= NULL
; l
= l
->next
)
173 pdeclaration(name
, &l
->decl
, 1, ";\n");
174 f_print(fout
, "};\n");
175 f_print(fout
, "typedef struct %s %s;\n", name
, name
);
176 storexdrfuncdecl(name
, 1);
184 pstructdef(definition
*def
)
187 char *name
= def
->def_name
;
189 f_print(fout
, "struct %s {\n", name
);
190 for (l
= def
->def
.st
.decls
; l
!= NULL
; l
= l
->next
)
191 pdeclaration(name
, &l
->decl
, 1, ";\n");
193 f_print(fout
, "};\n");
194 f_print(fout
, "typedef struct %s %s;\n", name
, name
);
198 puniondef(definition
*def
)
201 char *name
= def
->def_name
;
204 f_print(fout
, "struct %s {\n", name
);
205 decl
= &def
->def
.un
.enum_decl
;
206 if (streq(decl
->type
, "bool"))
207 f_print(fout
, "\tbool_t %s;\n", decl
->name
);
209 f_print(fout
, "\t%s %s;\n", decl
->type
, decl
->name
);
211 f_print(fout
, "\tunion {\n");
212 for (l
= def
->def
.un
.cases
; l
!= NULL
; l
= l
->next
) {
213 if (l
->contflag
== 0)
214 pdeclaration(name
, &l
->case_decl
, 2, ";\n");
216 decl
= def
->def
.un
.default_decl
;
217 if (decl
&& !streq(decl
->type
, "void")) {
218 pdeclaration(name
, decl
, 2, ";\n");
220 f_print(fout
, "\t} %s_u;\n", name
);
221 f_print(fout
, "};\n");
222 f_print(fout
, "typedef struct %s %s;\n", name
, name
);
226 pdefine(char *name
, char *num
)
228 f_print(fout
, "#define\t%s %s\n", name
, num
);
232 puldefine(char *name
, char *num
)
234 f_print(fout
, "#define\t%s ((unsigned long)(%s))\n", name
, num
);
238 define_printed(proc_list
*stop
, version_list
*start
)
243 for (vers
= start
; vers
!= NULL
; vers
= vers
->next
) {
244 for (proc
= vers
->procs
; proc
!= NULL
; proc
= proc
->next
) {
247 else if (streq(proc
->proc_name
, stop
->proc_name
))
256 pfreeprocdef(char * name
, char *vers
, int mode
)
258 f_print(fout
, "extern int ");
261 f_print(fout
,"_freeresult(SVCXPRT *, xdrproc_t, caddr_t);\n");
263 f_print(fout
,"_freeresult();\n");
267 pprogramdef(definition
*def
)
276 puldefine(def
->def_name
, def
->def
.pr
.prog_num
);
277 for (vers
= def
->def
.pr
.versions
; vers
!= NULL
; vers
= vers
->next
) {
280 "extern struct rpcgen_table %s_%s_table[];\n",
281 locase(def
->def_name
), vers
->vers_num
);
282 f_print(fout
, "extern %s_%s_nproc;\n",
283 locase(def
->def_name
), vers
->vers_num
);
285 puldefine(vers
->vers_name
, vers
->vers_num
);
288 * Print out 2 definitions, one for ANSI-C, another for
294 for (proc
= vers
->procs
; proc
!= NULL
;
296 if (!define_printed(proc
,
297 def
->def
.pr
.versions
)) {
298 puldefine(proc
->proc_name
,
301 f_print(fout
, "%s", ext
);
302 pprocdef(proc
, vers
, NULL
, 0, 2);
305 f_print(fout
, "%s", ext
);
306 pprocdef(proc
, vers
, NULL
, 1, 2);
309 pfreeprocdef(def
->def_name
, vers
->vers_num
, 2);
311 for (i
= 1; i
< 3; i
++) {
313 f_print(fout
, "\n#if defined(__STDC__) || defined(__cplusplus)\n");
316 f_print(fout
, "\n#else /* K&R C */\n");
320 for (proc
= vers
->procs
; proc
!= NULL
;
322 if (!define_printed(proc
,
323 def
->def
.pr
.versions
)) {
324 puldefine(proc
->proc_name
,
327 f_print(fout
, "%s", ext
);
328 pprocdef(proc
, vers
, "CLIENT *", 0, i
);
329 f_print(fout
, "%s", ext
);
330 pprocdef(proc
, vers
, "struct svc_req *", 1, i
);
332 pfreeprocdef(def
->def_name
, vers
->vers_num
, i
);
334 f_print(fout
, "#endif /* K&R C */\n");
340 pprocdef(proc_list
*proc
, version_list
*vp
, char* addargtype
, int server_p
,
344 /* Print MT style stubs */
346 f_print(fout
, "bool_t ");
348 f_print(fout
, "enum clnt_stat ");
350 ptype(proc
->res_prefix
, proc
->res_type
, 1);
354 pvname_svc(proc
->proc_name
, vp
->vers_num
);
356 pvname(proc
->proc_name
, vp
->vers_num
);
359 * mode 1 = ANSI-C, mode 2 = K&R C
362 parglist(proc
, addargtype
);
364 f_print(fout
, "();\n");
369 /* print out argument list of procedure */
371 parglist(proc_list
*proc
, char* addargtype
)
376 if (proc
->arg_num
< 2 && newstyle
&&
377 streq(proc
->args
.decls
->decl
.type
, "void")) {
378 /* 0 argument in new style: do nothing*/
381 for (dl
= proc
->args
.decls
; dl
!= NULL
; dl
= dl
->next
) {
382 ptype(dl
->decl
.prefix
, dl
->decl
.type
, 1);
385 /* old style passes by reference */
391 ptype(proc
->res_prefix
, proc
->res_type
, 1);
392 f_print(fout
, "*, ");
395 f_print(fout
, "%s);\n", addargtype
);
399 penumdef(definition
*def
)
401 char *name
= def
->def_name
;
406 f_print(fout
, "enum %s {\n", name
);
407 for (l
= def
->def
.en
.vals
; l
!= NULL
; l
= l
->next
) {
408 f_print(fout
, "\t%s", l
->name
);
410 f_print(fout
, " = %s", l
->assignment
);
411 last
= l
->assignment
;
415 f_print(fout
, " = %d", count
++);
417 f_print(fout
, " = %s + %d", last
, count
++);
420 f_print(fout
, ",\n");
424 f_print(fout
, "};\n");
425 f_print(fout
, "typedef enum %s %s;\n", name
, name
);
429 ptypedef(definition
*def
)
431 char *name
= def
->def_name
;
432 char *old
= def
->def
.ty
.old_type
;
433 char prefix
[8]; /* enough to contain "struct ", including NUL */
434 relation rel
= def
->def
.ty
.rel
;
436 if (!streq(name
, old
)) {
437 if (streq(old
, "string")) {
440 } else if (streq(old
, "opaque")) {
442 } else if (streq(old
, "bool")) {
445 if (undefined2(old
, name
) && def
->def
.ty
.old_prefix
)
446 s_print(prefix
, "%s ", def
->def
.ty
.old_prefix
);
449 f_print(fout
, "typedef ");
452 f_print(fout
, "struct {\n");
453 f_print(fout
, "\tu_int %s_len;\n", name
);
454 f_print(fout
, "\t%s%s *%s_val;\n", prefix
, old
, name
);
455 f_print(fout
, "} %s", name
);
458 f_print(fout
, "%s%s *%s", prefix
, old
, name
);
461 f_print(fout
, "%s%s %s[%s]", prefix
, old
, name
,
462 def
->def
.ty
.array_max
);
465 f_print(fout
, "%s%s %s", prefix
, old
, name
);
468 f_print(fout
, ";\n");
473 pdeclaration(char *name
, declaration
*dec
, int tab
, char *separator
)
475 char buf
[8]; /* enough to hold "struct ", include NUL */
479 if (streq(dec
->type
, "void"))
483 if (streq(dec
->type
, name
) && !dec
->prefix
)
484 f_print(fout
, "struct ");
486 if (streq(dec
->type
, "string")) {
487 f_print(fout
, "char *%s", dec
->name
);
490 if (streq(dec
->type
, "bool")) {
492 } else if (streq(dec
->type
, "opaque")) {
496 s_print(buf
, "%s ", dec
->prefix
);
503 f_print(fout
, "%s%s %s", prefix
, type
, dec
->name
);
506 f_print(fout
, "%s%s %s[%s]", prefix
, type
, dec
->name
,
510 f_print(fout
, "%s%s *%s", prefix
, type
, dec
->name
);
513 f_print(fout
, "struct {\n");
515 f_print(fout
, "\tu_int %s_len;\n", dec
->name
);
518 "\t%s%s *%s_val;\n", prefix
, type
, dec
->name
);
520 f_print(fout
, "} %s", dec
->name
);
524 f_print(fout
, separator
);
528 undefined2(char *type
, char *stop
)
533 for (l
= defined
; l
!= NULL
; l
= l
->next
) {
534 def
= (definition
*) l
->val
;
535 if (def
->def_kind
!= DEF_PROGRAM
) {
536 if (streq(def
->def_name
, stop
))
538 else if (streq(def
->def_name
, type
))