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