2 * From: @(#)rpc_hout.c 1.12 89/02/22
4 * Copyright (c) 2010, Oracle America, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials
14 * provided with the distribution.
15 * * Neither the name of the "Oracle America, Inc." nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
26 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * rpc_hout.c, Header file outputter for the RPC protocol compiler
38 #include "rpc_parse.h"
42 static void pconstdef (definition
* def
);
43 static void pargdef (definition
* def
);
44 static void pstructdef (definition
* def
);
45 static void puniondef (definition
* def
);
46 static void pdefine (const char *name
, const char *num
);
47 static int define_printed (proc_list
* stop
, version_list
* start
);
48 static void pprogramdef (definition
* def
);
49 static void parglist (proc_list
* proc
, const char *addargtype
);
50 static void penumdef (definition
* def
);
51 static void ptypedef (definition
* def
);
52 static int undefined2 (const char *type
, const char *stop
);
54 /* store away enough information to allow the XDR functions to be spat
55 out at the end of the file */
58 storexdrfuncdecl (const char *name
, int pointerp
)
62 xdrptr
= (xdrfunc
*) malloc(sizeof (struct xdrfunc
));
64 xdrptr
->name
= (char *)name
;
65 xdrptr
->pointerp
= pointerp
;
68 if (xdrfunc_tail
== NULL
)
70 xdrfunc_head
= xdrptr
;
71 xdrfunc_tail
= xdrptr
;
75 xdrfunc_tail
->next
= xdrptr
;
76 xdrfunc_tail
= xdrptr
;
81 * Print the C-version of an xdr definition
84 print_datadef (definition
*def
)
87 if (def
->def_kind
== DEF_PROGRAM
) /* handle data only */
90 if (def
->def_kind
!= DEF_CONST
)
94 switch (def
->def_kind
)
115 if (def
->def_kind
!= DEF_PROGRAM
&& def
->def_kind
!= DEF_CONST
)
117 storexdrfuncdecl(def
->def_name
,
118 def
->def_kind
!= DEF_TYPEDEF
||
119 !isvectordef(def
->def
.ty
.old_type
,
126 print_funcdef (definition
*def
)
128 switch (def
->def_kind
)
131 f_print (fout
, "\n");
136 /* ?... shouldn't happen I guess */
141 print_xdr_func_def (char *name
, int pointerp
, int i
)
145 f_print (fout
, "extern bool_t xdr_%s ();\n", name
);
149 f_print(fout
, "extern bool_t xdr_%s (XDR *, %s%s);\n", name
,
150 name
, pointerp
? "*" : "");
154 pconstdef (definition
*def
)
156 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
)
172 for (plist
= vers
->procs
; plist
!= NULL
;
176 if (!newstyle
|| plist
->arg_num
< 2)
178 continue; /* old style or single args */
180 name
= plist
->args
.argname
;
181 f_print (fout
, "struct %s {\n", name
);
182 for (l
= plist
->args
.decls
;
183 l
!= NULL
; l
= l
->next
)
185 pdeclaration (name
, &l
->decl
, 1, ";\n");
187 f_print (fout
, "};\n");
188 f_print (fout
, "typedef struct %s %s;\n", name
, name
);
189 storexdrfuncdecl (name
, 1);
190 f_print (fout
, "\n");
197 pstructdef (definition
*def
)
200 const char *name
= def
->def_name
;
202 f_print (fout
, "struct %s {\n", name
);
203 for (l
= def
->def
.st
.decls
; l
!= NULL
; l
= l
->next
)
205 pdeclaration (name
, &l
->decl
, 1, ";\n");
207 f_print (fout
, "};\n");
208 f_print (fout
, "typedef struct %s %s;\n", name
, name
);
212 puniondef (definition
*def
)
215 const char *name
= def
->def_name
;
218 f_print (fout
, "struct %s {\n", name
);
219 decl
= &def
->def
.un
.enum_decl
;
220 if (streq (decl
->type
, "bool"))
222 f_print (fout
, "\tbool_t %s;\n", decl
->name
);
226 f_print (fout
, "\t%s %s;\n", decl
->type
, decl
->name
);
228 f_print (fout
, "\tunion {\n");
229 for (l
= def
->def
.un
.cases
; l
!= NULL
; l
= l
->next
)
231 if (l
->contflag
== 0)
232 pdeclaration (name
, &l
->case_decl
, 2, ";\n");
234 decl
= def
->def
.un
.default_decl
;
235 if (decl
&& !streq (decl
->type
, "void"))
237 pdeclaration (name
, decl
, 2, ";\n");
239 f_print (fout
, "\t} %s_u;\n", name
);
240 f_print (fout
, "};\n");
241 f_print (fout
, "typedef struct %s %s;\n", name
, name
);
245 pdefine (const char *name
, const char *num
)
247 f_print (fout
, "#define %s %s\n", name
, num
);
251 define_printed (proc_list
*stop
, version_list
*start
)
256 for (vers
= start
; vers
!= NULL
; vers
= vers
->next
)
258 for (proc
= vers
->procs
; proc
!= NULL
; proc
= proc
->next
)
264 else if (streq (proc
->proc_name
, stop
->proc_name
))
275 pfreeprocdef (const char *name
, const char *vers
, int mode
)
277 f_print (fout
, "extern int ");
280 f_print (fout
,"_freeresult (SVCXPRT *, xdrproc_t, caddr_t);\n");
282 f_print (fout
,"_freeresult ();\n");
286 pprogramdef (definition
*def
)
295 pdefine (def
->def_name
, def
->def
.pr
.prog_num
);
296 for (vers
= def
->def
.pr
.versions
; vers
!= NULL
; vers
= vers
->next
)
300 f_print (fout
, "extern struct rpcgen_table %s_%s_table[];\n",
301 locase (def
->def_name
), vers
->vers_num
);
302 f_print (fout
, "extern %s_%s_nproc;\n",
303 locase (def
->def_name
), vers
->vers_num
);
305 pdefine (vers
->vers_name
, vers
->vers_num
);
308 * Print out 2 definitions, one for ANSI-C, another for
315 for (proc
= vers
->procs
; proc
!= NULL
;
318 if (!define_printed(proc
, def
->def
.pr
.versions
))
320 pdefine (proc
->proc_name
, proc
->proc_num
);
322 f_print (fout
, "%s", ext
);
323 pprocdef (proc
, vers
, NULL
, 0, 2);
327 f_print(fout
, "%s", ext
);
328 pprocdef (proc
, vers
, NULL
, 1, 2);
331 pfreeprocdef (def
->def_name
, vers
->vers_num
, 2);
335 for (i
= 1; i
< 3; i
++)
339 f_print (fout
, "\n#if defined(__STDC__) || defined(__cplusplus)\n");
344 f_print (fout
, "\n#else /* K&R C */\n");
348 for (proc
= vers
->procs
; proc
!= NULL
; proc
= proc
->next
)
350 if (!define_printed(proc
, def
->def
.pr
.versions
))
352 pdefine(proc
->proc_name
, proc
->proc_num
);
354 f_print (fout
, "%s", ext
);
355 pprocdef (proc
, vers
, "CLIENT *", 0, i
);
356 f_print (fout
, "%s", ext
);
357 pprocdef (proc
, vers
, "struct svc_req *", 1, i
);
359 pfreeprocdef (def
->def_name
, vers
->vers_num
, i
);
361 f_print (fout
, "#endif /* K&R C */\n");
367 pprocdef (proc_list
* proc
, version_list
* vp
,
368 const char *addargtype
, int server_p
, int mode
)
371 {/* Print MT style stubs */
373 f_print (fout
, "bool_t ");
375 f_print (fout
, "enum clnt_stat ");
379 ptype (proc
->res_prefix
, proc
->res_type
, 1);
380 f_print (fout
, "* ");
383 pvname_svc (proc
->proc_name
, vp
->vers_num
);
385 pvname (proc
->proc_name
, vp
->vers_num
);
388 * mode 1 = ANSI-C, mode 2 = K&R C
391 parglist (proc
, addargtype
);
393 f_print (fout
, "();\n");
396 /* print out argument list of procedure */
398 parglist (proc_list
*proc
, const char *addargtype
)
403 if (proc
->arg_num
< 2 && newstyle
&&
404 streq (proc
->args
.decls
->decl
.type
, "void"))
406 /* 0 argument in new style: do nothing */
410 for (dl
= proc
->args
.decls
; dl
!= NULL
; dl
= dl
->next
)
412 ptype (dl
->decl
.prefix
, dl
->decl
.type
, 1);
414 f_print (fout
, "*"); /* old style passes by reference */
416 f_print (fout
, ", ");
421 ptype(proc
->res_prefix
, proc
->res_type
, 1);
422 f_print(fout
, "*, ");
425 f_print (fout
, "%s);\n", addargtype
);
429 penumdef (definition
*def
)
431 const char *name
= def
->def_name
;
433 const char *last
= NULL
;
436 f_print (fout
, "enum %s {\n", name
);
437 for (l
= def
->def
.en
.vals
; l
!= NULL
; l
= l
->next
)
439 f_print (fout
, "\t%s", l
->name
);
442 f_print (fout
, " = %s", l
->assignment
);
443 last
= l
->assignment
;
450 f_print (fout
, " = %d", count
++);
454 f_print (fout
, " = %s + %d", last
, count
++);
457 f_print (fout
, ",\n");
459 f_print (fout
, "};\n");
460 f_print (fout
, "typedef enum %s %s;\n", name
, name
);
464 ptypedef (definition
*def
)
466 const char *name
= def
->def_name
;
467 const char *old
= def
->def
.ty
.old_type
;
468 char prefix
[8]; /* enough to contain "struct ", including NUL */
469 relation rel
= def
->def
.ty
.rel
;
471 if (!streq (name
, old
))
473 if (streq (old
, "string"))
478 else if (streq (old
, "opaque"))
482 else if (streq (old
, "bool"))
486 if (undefined2 (old
, name
) && def
->def
.ty
.old_prefix
)
488 s_print (prefix
, "%s ", def
->def
.ty
.old_prefix
);
494 f_print (fout
, "typedef ");
498 f_print (fout
, "struct {\n");
499 f_print (fout
, "\tu_int %s_len;\n", name
);
500 f_print (fout
, "\t%s%s *%s_val;\n", prefix
, old
, name
);
501 f_print (fout
, "} %s", name
);
504 f_print (fout
, "%s%s *%s", prefix
, old
, name
);
507 f_print (fout
, "%s%s %s[%s]", prefix
, old
, name
,
508 def
->def
.ty
.array_max
);
511 f_print (fout
, "%s%s %s", prefix
, old
, name
);
514 f_print (fout
, ";\n");
519 pdeclaration (const char *name
, declaration
* dec
, int tab
,
520 const char *separator
)
522 char buf
[8]; /* enough to hold "struct ", include NUL */
526 if (streq (dec
->type
, "void"))
531 if (streq (dec
->type
, name
) && !dec
->prefix
)
533 f_print (fout
, "struct ");
535 if (streq (dec
->type
, "string"))
537 f_print (fout
, "char *%s", dec
->name
);
542 if (streq (dec
->type
, "bool"))
546 else if (streq (dec
->type
, "opaque"))
554 s_print (buf
, "%s ", dec
->prefix
);
562 f_print (fout
, "%s%s %s", prefix
, type
, dec
->name
);
565 f_print (fout
, "%s%s %s[%s]", prefix
, type
, dec
->name
,
569 f_print (fout
, "%s%s *%s", prefix
, type
, dec
->name
);
572 f_print (fout
, "struct {\n");
574 f_print (fout
, "\tu_int %s_len;\n", dec
->name
);
576 f_print (fout
, "\t%s%s *%s_val;\n", prefix
, type
, dec
->name
);
578 f_print (fout
, "} %s", dec
->name
);
582 f_print (fout
, separator
);
586 undefined2 (const char *type
, const char *stop
)
591 for (l
= defined
; l
!= NULL
; l
= l
->next
)
593 def
= (definition
*) l
->val
;
594 if (def
->def_kind
!= DEF_PROGRAM
)
596 if (streq (def
->def_name
, stop
))
600 else if (streq (def
->def_name
, type
))