chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / sunrpc / rpc_hout.c
1 /*
2  * From: @(#)rpc_hout.c 1.12 89/02/22 (C) 1987 SMI
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  *       notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  *       copyright notice, this list of conditions and the following
12  *       disclaimer in the documentation and/or other materials
13  *       provided with the distribution.
14  *     * Neither the name of Sun Microsystems, Inc. nor the names of its
15  *       contributors may be used to endorse or promote products derived
16  *       from this software without specific prior written permission.
17  *
18  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 /*
33  * rpc_hout.c, Header file outputter for the RPC protocol compiler
34  */
35 #include <stdio.h>
36 #include <ctype.h>
37 #include "rpc_parse.h"
38 #include "rpc_util.h"
39 #include "proto.h"
40
41 static void pconstdef (definition * def);
42 static void pargdef (definition * def);
43 static void pstructdef (definition * def);
44 static void puniondef (definition * def);
45 static void pdefine (const char *name, const char *num);
46 static int define_printed (proc_list * stop, version_list * start);
47 static void pprogramdef (definition * def);
48 static void parglist (proc_list * proc, const char *addargtype);
49 static void penumdef (definition * def);
50 static void ptypedef (definition * def);
51 static int undefined2 (const char *type, const char *stop);
52
53 /* store away enough information to allow the XDR functions to be spat
54     out at the end of the file */
55
56 static void
57 storexdrfuncdecl (const char *name, int pointerp)
58 {
59   xdrfunc * xdrptr;
60
61   xdrptr = (xdrfunc *) malloc(sizeof (struct xdrfunc));
62
63   xdrptr->name = (char *)name;
64   xdrptr->pointerp = pointerp;
65   xdrptr->next = NULL;
66
67   if (xdrfunc_tail == NULL)
68     {
69       xdrfunc_head = xdrptr;
70       xdrfunc_tail = xdrptr;
71     }
72   else
73     {
74       xdrfunc_tail->next = xdrptr;
75       xdrfunc_tail = xdrptr;
76     }
77 }
78
79 /*
80  * Print the C-version of an xdr definition
81  */
82 void
83 print_datadef (definition *def)
84 {
85
86   if (def->def_kind == DEF_PROGRAM)     /* handle data only */
87     return;
88
89   if (def->def_kind != DEF_CONST)
90     {
91       f_print (fout, "\n");
92     }
93   switch (def->def_kind)
94     {
95     case DEF_STRUCT:
96       pstructdef (def);
97       break;
98     case DEF_UNION:
99       puniondef (def);
100       break;
101     case DEF_ENUM:
102       penumdef (def);
103       break;
104     case DEF_TYPEDEF:
105       ptypedef (def);
106       break;
107     case DEF_PROGRAM:
108       pprogramdef (def);
109       break;
110     case DEF_CONST:
111       pconstdef (def);
112       break;
113     }
114   if (def->def_kind != DEF_PROGRAM && def->def_kind != DEF_CONST)
115     {
116       storexdrfuncdecl(def->def_name,
117                        def->def_kind != DEF_TYPEDEF ||
118                        !isvectordef(def->def.ty.old_type,
119                                     def->def.ty.rel));
120     }
121 }
122
123
124 void
125 print_funcdef (definition *def)
126 {
127   switch (def->def_kind)
128     {
129     case DEF_PROGRAM:
130       f_print (fout, "\n");
131       pprogramdef (def);
132       break;
133     default:
134       break;
135       /* ?... shouldn't happen I guess */
136     }
137 }
138
139 void
140 print_xdr_func_def (char *name, int pointerp, int i)
141 {
142   if (i == 2)
143     {
144       f_print (fout, "extern bool_t xdr_%s ();\n", name);
145       return;
146     }
147   else
148     f_print(fout, "extern  bool_t xdr_%s (XDR *, %s%s);\n", name,
149             name, pointerp ? "*" : "");
150 }
151
152 static void
153 pconstdef (definition *def)
154 {
155   pdefine (def->def_name, def->def.co);
156 }
157
158 /* print out the definitions for the arguments of functions in the
159    header file
160  */
161 static void
162 pargdef (definition * def)
163 {
164   decl_list *l;
165   version_list *vers;
166   const char *name;
167   proc_list *plist;
168
169   for (vers = def->def.pr.versions; vers != NULL; vers = vers->next)
170     {
171       for (plist = vers->procs; plist != NULL;
172            plist = plist->next)
173         {
174
175           if (!newstyle || plist->arg_num < 2)
176             {
177               continue;         /* old style or single args */
178             }
179           name = plist->args.argname;
180           f_print (fout, "struct %s {\n", name);
181           for (l = plist->args.decls;
182                l != NULL; l = l->next)
183             {
184               pdeclaration (name, &l->decl, 1, ";\n");
185             }
186           f_print (fout, "};\n");
187           f_print (fout, "typedef struct %s %s;\n", name, name);
188           storexdrfuncdecl (name, 1);
189           f_print (fout, "\n");
190         }
191     }
192
193 }
194
195 static void
196 pstructdef (definition *def)
197 {
198   decl_list *l;
199   const char *name = def->def_name;
200
201   f_print (fout, "struct %s {\n", name);
202   for (l = def->def.st.decls; l != NULL; l = l->next)
203     {
204       pdeclaration (name, &l->decl, 1, ";\n");
205     }
206   f_print (fout, "};\n");
207   f_print (fout, "typedef struct %s %s;\n", name, name);
208 }
209
210 static void
211 puniondef (definition *def)
212 {
213   case_list *l;
214   const char *name = def->def_name;
215   declaration *decl;
216
217   f_print (fout, "struct %s {\n", name);
218   decl = &def->def.un.enum_decl;
219   if (streq (decl->type, "bool"))
220     {
221       f_print (fout, "\tbool_t %s;\n", decl->name);
222     }
223   else
224     {
225       f_print (fout, "\t%s %s;\n", decl->type, decl->name);
226     }
227   f_print (fout, "\tunion {\n");
228   for (l = def->def.un.cases; l != NULL; l = l->next)
229     {
230       if (l->contflag == 0)
231         pdeclaration (name, &l->case_decl, 2, ";\n");
232     }
233   decl = def->def.un.default_decl;
234   if (decl && !streq (decl->type, "void"))
235     {
236       pdeclaration (name, decl, 2, ";\n");
237     }
238   f_print (fout, "\t} %s_u;\n", name);
239   f_print (fout, "};\n");
240   f_print (fout, "typedef struct %s %s;\n", name, name);
241 }
242
243 static void
244 pdefine (const char *name, const char *num)
245 {
246   f_print (fout, "#define %s %s\n", name, num);
247 }
248
249 static int
250 define_printed (proc_list *stop, version_list *start)
251 {
252   version_list *vers;
253   proc_list *proc;
254
255   for (vers = start; vers != NULL; vers = vers->next)
256     {
257       for (proc = vers->procs; proc != NULL; proc = proc->next)
258         {
259           if (proc == stop)
260             {
261               return 0;
262             }
263           else if (streq (proc->proc_name, stop->proc_name))
264             {
265               return 1;
266             }
267         }
268     }
269   abort ();
270   /* NOTREACHED */
271 }
272
273 static void
274 pfreeprocdef (const char *name, const char *vers, int mode)
275 {
276   f_print (fout, "extern int ");
277   pvname (name, vers);
278   if (mode == 1)
279     f_print (fout,"_freeresult (SVCXPRT *, xdrproc_t, caddr_t);\n");
280   else
281     f_print (fout,"_freeresult ();\n");
282 }
283
284 static void
285 pprogramdef (definition *def)
286 {
287   version_list *vers;
288   proc_list *proc;
289   int i;
290   const char *ext;
291
292   pargdef (def);
293
294   pdefine (def->def_name, def->def.pr.prog_num);
295   for (vers = def->def.pr.versions; vers != NULL; vers = vers->next)
296     {
297       if (tblflag)
298         {
299           f_print (fout, "extern struct rpcgen_table %s_%s_table[];\n",
300                    locase (def->def_name), vers->vers_num);
301           f_print (fout, "extern %s_%s_nproc;\n",
302                    locase (def->def_name), vers->vers_num);
303         }
304       pdefine (vers->vers_name, vers->vers_num);
305
306       /*
307        * Print out 2 definitions, one for ANSI-C, another for
308        * old K & R C
309        */
310
311       if(!Cflag)
312         {
313           ext = "extern  ";
314           for (proc = vers->procs; proc != NULL;
315                proc = proc->next)
316             {
317               if (!define_printed(proc, def->def.pr.versions))
318                 {
319                   pdefine (proc->proc_name, proc->proc_num);
320                 }
321               f_print (fout, "%s", ext);
322               pprocdef (proc, vers, NULL, 0, 2);
323
324               if (mtflag)
325                 {
326                   f_print(fout, "%s", ext);
327                   pprocdef (proc, vers, NULL, 1, 2);
328                 }
329             }
330           pfreeprocdef (def->def_name, vers->vers_num, 2);
331         }
332       else
333         {
334           for (i = 1; i < 3; i++)
335             {
336               if (i == 1)
337                 {
338                   f_print (fout, "\n#if defined(__STDC__) || defined(__cplusplus)\n");
339                   ext = "extern  ";
340                 }
341               else
342                 {
343                   f_print (fout, "\n#else /* K&R C */\n");
344                   ext = "extern  ";
345                 }
346
347               for (proc = vers->procs; proc != NULL; proc = proc->next)
348                 {
349                   if (!define_printed(proc, def->def.pr.versions))
350                     {
351                       pdefine(proc->proc_name, proc->proc_num);
352                     }
353                   f_print (fout, "%s", ext);
354                   pprocdef (proc, vers, "CLIENT *", 0, i);
355                   f_print (fout, "%s", ext);
356                   pprocdef (proc, vers, "struct svc_req *", 1, i);
357                 }
358               pfreeprocdef (def->def_name, vers->vers_num, i);
359             }
360           f_print (fout, "#endif /* K&R C */\n");
361         }
362     }
363 }
364
365 void
366 pprocdef (proc_list * proc, version_list * vp,
367           const char *addargtype, int server_p, int mode)
368 {
369   if (mtflag)
370     {/* Print MT style stubs */
371       if (server_p)
372         f_print (fout, "bool_t ");
373       else
374         f_print (fout, "enum clnt_stat ");
375     }
376   else
377     {
378       ptype (proc->res_prefix, proc->res_type, 1);
379       f_print (fout, "* ");
380     }
381   if (server_p)
382     pvname_svc (proc->proc_name, vp->vers_num);
383   else
384     pvname (proc->proc_name, vp->vers_num);
385
386   /*
387    * mode  1 = ANSI-C, mode 2 = K&R C
388    */
389   if (mode == 1)
390     parglist (proc, addargtype);
391   else
392     f_print (fout, "();\n");
393 }
394
395 /* print out argument list of procedure */
396 static void
397 parglist (proc_list *proc, const char *addargtype)
398 {
399   decl_list *dl;
400
401   f_print(fout,"(");
402   if (proc->arg_num < 2 && newstyle &&
403       streq (proc->args.decls->decl.type, "void"))
404     {
405       /* 0 argument in new style:  do nothing */
406     }
407   else
408     {
409       for (dl = proc->args.decls; dl != NULL; dl = dl->next)
410         {
411           ptype (dl->decl.prefix, dl->decl.type, 1);
412           if (!newstyle)
413             f_print (fout, "*");        /* old style passes by reference */
414
415           f_print (fout, ", ");
416         }
417     }
418   if (mtflag)
419     {
420       ptype(proc->res_prefix, proc->res_type, 1);
421       f_print(fout, "*, ");
422     }
423
424   f_print (fout, "%s);\n", addargtype);
425 }
426
427 static void
428 penumdef (definition *def)
429 {
430   const char *name = def->def_name;
431   enumval_list *l;
432   const char *last = NULL;
433   int count = 0;
434
435   f_print (fout, "enum %s {\n", name);
436   for (l = def->def.en.vals; l != NULL; l = l->next)
437     {
438       f_print (fout, "\t%s", l->name);
439       if (l->assignment)
440         {
441           f_print (fout, " = %s", l->assignment);
442           last = l->assignment;
443           count = 1;
444         }
445       else
446         {
447           if (last == NULL)
448             {
449               f_print (fout, " = %d", count++);
450             }
451           else
452             {
453               f_print (fout, " = %s + %d", last, count++);
454             }
455         }
456       f_print (fout, ",\n");
457     }
458   f_print (fout, "};\n");
459   f_print (fout, "typedef enum %s %s;\n", name, name);
460 }
461
462 static void
463 ptypedef (definition *def)
464 {
465   const char *name = def->def_name;
466   const char *old = def->def.ty.old_type;
467   char prefix[8];         /* enough to contain "struct ", including NUL */
468   relation rel = def->def.ty.rel;
469
470   if (!streq (name, old))
471     {
472       if (streq (old, "string"))
473         {
474           old = "char";
475           rel = REL_POINTER;
476         }
477       else if (streq (old, "opaque"))
478         {
479           old = "char";
480         }
481       else if (streq (old, "bool"))
482         {
483           old = "bool_t";
484         }
485       if (undefined2 (old, name) && def->def.ty.old_prefix)
486         {
487           s_print (prefix, "%s ", def->def.ty.old_prefix);
488         }
489       else
490         {
491           prefix[0] = 0;
492         }
493       f_print (fout, "typedef ");
494       switch (rel)
495         {
496         case REL_ARRAY:
497           f_print (fout, "struct {\n");
498           f_print (fout, "\tu_int %s_len;\n", name);
499           f_print (fout, "\t%s%s *%s_val;\n", prefix, old, name);
500           f_print (fout, "} %s", name);
501           break;
502         case REL_POINTER:
503           f_print (fout, "%s%s *%s", prefix, old, name);
504           break;
505         case REL_VECTOR:
506           f_print (fout, "%s%s %s[%s]", prefix, old, name,
507                    def->def.ty.array_max);
508           break;
509         case REL_ALIAS:
510           f_print (fout, "%s%s %s", prefix, old, name);
511           break;
512         }
513       f_print (fout, ";\n");
514     }
515 }
516
517 void
518 pdeclaration (const char *name, declaration * dec, int tab,
519               const char *separator)
520 {
521   char buf[8];                  /* enough to hold "struct ", include NUL */
522   const char *prefix;
523   const char *type;
524
525   if (streq (dec->type, "void"))
526     {
527       return;
528     }
529   tabify (fout, tab);
530   if (streq (dec->type, name) && !dec->prefix)
531     {
532       f_print (fout, "struct ");
533     }
534   if (streq (dec->type, "string"))
535     {
536       f_print (fout, "char *%s", dec->name);
537     }
538   else
539     {
540       prefix = "";
541       if (streq (dec->type, "bool"))
542         {
543           type = "bool_t";
544         }
545       else if (streq (dec->type, "opaque"))
546         {
547           type = "char";
548         }
549       else
550         {
551           if (dec->prefix)
552             {
553               s_print (buf, "%s ", dec->prefix);
554               prefix = buf;
555             }
556           type = dec->type;
557         }
558       switch (dec->rel)
559         {
560         case REL_ALIAS:
561           f_print (fout, "%s%s %s", prefix, type, dec->name);
562           break;
563         case REL_VECTOR:
564           f_print (fout, "%s%s %s[%s]", prefix, type, dec->name,
565                    dec->array_max);
566           break;
567         case REL_POINTER:
568           f_print (fout, "%s%s *%s", prefix, type, dec->name);
569           break;
570         case REL_ARRAY:
571           f_print (fout, "struct {\n");
572           tabify (fout, tab);
573           f_print (fout, "\tu_int %s_len;\n", dec->name);
574           tabify (fout, tab);
575           f_print (fout, "\t%s%s *%s_val;\n", prefix, type, dec->name);
576           tabify (fout, tab);
577           f_print (fout, "} %s", dec->name);
578           break;
579         }
580     }
581   f_print (fout, separator);
582 }
583
584 static int
585 undefined2 (const char *type, const char *stop)
586 {
587   list *l;
588   definition *def;
589
590   for (l = defined; l != NULL; l = l->next)
591     {
592       def = (definition *) l->val;
593       if (def->def_kind != DEF_PROGRAM)
594         {
595           if (streq (def->def_name, stop))
596             {
597               return 1;
598             }
599           else if (streq (def->def_name, type))
600             {
601               return 0;
602             }
603         }
604     }
605   return 1;
606 }