Fix typo.
[kopensolaris-gnu/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 char hout_rcsid[] = 
35   "$Id$";
36
37 /*
38  * rpc_hout.c, Header file outputter for the RPC protocol compiler 
39  */
40 #include <stdio.h>
41 #include <ctype.h>
42 #include "rpc_parse.h"
43 #include "rpc_util.h"
44 #include "proto.h"
45
46 static void pconstdef(definition *def);
47 static void pargdef(definition *def);
48 static void pstructdef(definition *def);
49 static void puniondef(definition *def);
50 static void pdefine(const char *name, const char *num);
51 static void puldefine(const char *name, const char *num);
52 static int define_printed(proc_list *stop, version_list *start);
53 static void pprogramdef(definition *def);
54 static void parglist(proc_list *proc, const char *addargtype);
55 static void penumdef(definition *def);
56 static void ptypedef(definition *def);
57 static int undefined2(const char *type, const char *stop);
58
59 /*
60  * Print the C-version of an xdr definition 
61  */
62 void
63 print_datadef(definition *def)
64 {
65
66         if (def->def_kind == DEF_PROGRAM )  /* handle data only */
67                 return;
68
69         if (def->def_kind != DEF_CONST) {
70                 f_print(fout, "\n");
71         }
72         switch (def->def_kind) {
73         case DEF_STRUCT:
74                 pstructdef(def);
75                 break;
76         case DEF_UNION:
77                 puniondef(def);
78                 break;
79         case DEF_ENUM:
80                 penumdef(def);
81                 break;
82         case DEF_TYPEDEF:
83                 ptypedef(def);
84                 break;
85         case DEF_PROGRAM:
86                 pprogramdef(def);
87                 break;
88         case DEF_CONST:
89                 pconstdef(def);
90                 break;
91         }
92         if (def->def_kind != DEF_PROGRAM && def->def_kind != DEF_CONST) {
93           pxdrfuncdecl( def->def_name,
94                        def->def_kind != DEF_TYPEDEF ||
95                        !isvectordef(def->def.ty.old_type, def->def.ty.rel));
96
97         }
98 }
99
100
101 void
102 print_funcdef(definition *def)
103 {
104         switch (def->def_kind) {
105           case DEF_PROGRAM:
106                 f_print(fout, "\n");
107                 pprogramdef(def);
108                 break;
109           default:
110             /* ?... shouldn't happen I guess */
111         }
112 }
113
114 void
115 pxdrfuncdecl(const char *name, int pointerp)
116 {
117
118   f_print(fout,"#ifdef __cplusplus \n");
119     f_print(fout, "extern \"C\" bool_t xdr_%s(XDR *, %s%s);\n", name, name, pointerp ? "*" : "");
120   f_print(fout,"#elif __STDC__ \n");
121     f_print(fout, "extern  bool_t xdr_%s(XDR *, %s%s);\n", name, name, pointerp ? "*" : "");
122   f_print(fout,"#else /* Old Style C */ \n");
123     f_print(fout, "bool_t xdr_%s();\n", name);
124   f_print(fout,"#endif /* Old Style C */ \n\n");
125 }
126
127
128 static void
129 pconstdef(definition *def)
130 {
131         pdefine(def->def_name, def->def.co);
132 }
133
134 /* print out the definitions for the arguments of functions in the 
135    header file 
136 */
137 static void
138 pargdef(definition *def)
139 {
140         decl_list *l;
141         version_list *vers;
142         const char *name;
143         proc_list *plist;
144
145         
146         for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
147                         for(plist = vers->procs; plist != NULL; 
148                             plist = plist->next) {
149                                 
150                                 if (!newstyle || plist->arg_num < 2) {
151                                         continue; /* old style or single args */
152                                 }
153                                 name = plist->args.argname;
154                                 f_print(fout, "struct %s {\n", name);
155                                 for (l = plist->args.decls; 
156                                      l != NULL; l = l->next) {
157                                         pdeclaration(name, &l->decl, 1, ";\n" );
158                                 }
159                                 f_print(fout, "};\n");
160                                 f_print(fout, "typedef struct %s %s;\n", name, name);
161                                 pxdrfuncdecl(name, 0);
162                                 f_print( fout, "\n" );
163                         }
164                 }
165
166 }
167
168
169 static void
170 pstructdef(definition *def)
171 {
172         decl_list *l;
173         const char *name = def->def_name;
174
175         f_print(fout, "struct %s {\n", name);
176         for (l = def->def.st.decls; l != NULL; l = l->next) {
177                 pdeclaration(name, &l->decl, 1, ";\n");
178         }
179         f_print(fout, "};\n");
180         f_print(fout, "typedef struct %s %s;\n", name, name);
181 }
182
183 static void
184 puniondef(definition *def)
185 {
186         case_list *l;
187         const char *name = def->def_name;
188         declaration *decl;
189
190         f_print(fout, "struct %s {\n", name);
191         decl = &def->def.un.enum_decl;
192         if (streq(decl->type, "bool")) {
193                 f_print(fout, "\tbool_t %s;\n", decl->name);
194         } else {
195                 f_print(fout, "\t%s %s;\n", decl->type, decl->name);
196         }
197         f_print(fout, "\tunion {\n");
198         for (l = def->def.un.cases; l != NULL; l = l->next) {
199           if(l->contflag == 0)
200                 pdeclaration(name, &l->case_decl, 2, ";\n" );
201         }
202         decl = def->def.un.default_decl;
203         if (decl && !streq(decl->type, "void")) {
204                 pdeclaration(name, decl, 2, ";\n" );
205         }
206         f_print(fout, "\t} %s_u;\n", name);
207         f_print(fout, "};\n");
208         f_print(fout, "typedef struct %s %s;\n", name, name);
209 }
210
211 static void
212 pdefine(const char *name, const char *num)
213 {
214         f_print(fout, "#define %s %s\n", name, num);
215 }
216
217 static void
218 puldefine(const char *name, const char *num)
219 {
220         f_print(fout, "#define %s ((u_long)%s)\n", name, num);
221 }
222
223 static int
224 define_printed(proc_list *stop, version_list *start)
225 {
226         version_list *vers;
227         proc_list *proc;
228
229         for (vers = start; vers != NULL; vers = vers->next) {
230                 for (proc = vers->procs; proc != NULL; proc = proc->next) {
231                         if (proc == stop) {
232                                 return (0);
233                         } else if (streq(proc->proc_name, stop->proc_name)) {
234                                 return (1);
235                         }
236                 }
237         }
238         abort();
239         /* NOTREACHED */
240 }
241
242 static void
243 pprogramdef(definition *def)
244 {
245         version_list *vers;
246         proc_list *proc;
247         int i;
248         const char *ext;
249         
250         pargdef(def);
251
252         puldefine(def->def_name, def->def.pr.prog_num);
253         for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
254                 if (tblflag) {
255                         f_print(fout, "extern struct rpcgen_table %s_%s_table[];\n",
256                                 locase(def->def_name), vers->vers_num);
257                         f_print(fout, "extern %s_%s_nproc;\n",
258                                 locase(def->def_name), vers->vers_num);
259                 }
260                 puldefine(vers->vers_name, vers->vers_num);
261
262                 /* 
263                  * Print out 3 definitions, one for ANSI-C, another for C++, 
264                  * a third for old style C 
265                  */
266                    
267                 for (i=0;i<3;i++) {
268                         if (i==0) {
269                                 f_print(fout,"\n#ifdef __cplusplus\n");
270                                 ext="extern \"C\" ";
271                         } 
272                         else if (i==1) {
273                                 f_print(fout,"\n#elif __STDC__\n");
274                                 ext="extern  "  ;
275                         } 
276                         else {
277                                 f_print(fout,"\n#else /* Old Style C */ \n");
278                                 ext="extern  ";
279                         }
280                         
281                         
282                         for (proc = vers->procs; proc != NULL; proc = proc->next) {
283                                 if (!define_printed(proc, def->def.pr.versions)) {
284                                         puldefine(proc->proc_name, proc->proc_num);
285                                 }
286                                 f_print(fout,"%s",ext);
287                                 pprocdef(proc, vers, "CLIENT *", 0,i);
288                                 f_print(fout,"%s",ext);
289                                 pprocdef(proc, vers, "struct svc_req *", 1,i);
290                                 
291                         }
292
293                 }
294                 f_print(fout,"#endif /* Old Style C */ \n");
295         }
296 }
297
298 void
299 pprocdef(proc_list *proc, version_list *vp, 
300          const char *addargtype, int server_p, int mode)
301 {
302
303
304
305
306                 ptype( proc->res_prefix, proc->res_type, 1 );
307                 f_print( fout, "* " );
308                 if( server_p )
309                         pvname_svc(proc->proc_name, vp->vers_num);
310                 else
311                         pvname(proc->proc_name, vp->vers_num);
312
313                 /*
314                  * mode  0 == cplusplus, mode  1 = ANSI-C, mode 2 = old style C 
315                  */
316                 if(mode == 0 || mode ==1) 
317                         parglist(proc, addargtype);
318                 else
319                         f_print(fout, "();\n");
320
321
322
323 }
324
325
326
327 /* print out argument list of procedure */
328 static void
329 parglist(proc_list *proc, const char *addargtype)
330 {
331         decl_list *dl;
332
333                 f_print(fout,"(");
334
335                 if( proc->arg_num < 2 && newstyle &&
336                    streq( proc->args.decls->decl.type, "void")) {
337                         /* 0 argument in new style:  do nothing */
338                 } else {
339                         for (dl = proc->args.decls; dl != NULL; dl = dl->next) {
340                                 ptype( dl->decl.prefix, dl->decl.type, 1 );
341                                 if( !newstyle )
342                                         f_print( fout, "*" ); /* old style passes by reference */
343         
344                                 f_print( fout, ", " );
345                         }
346                 }
347     
348                 f_print(fout, "%s);\n", addargtype);
349
350 }
351
352 static void
353 penumdef(definition *def)
354 {
355         const char *name = def->def_name;
356         enumval_list *l;
357         const char *last = NULL;
358         int count = 0;
359
360         f_print(fout, "enum %s {\n", name);
361         for (l = def->def.en.vals; l != NULL; l = l->next) {
362                 f_print(fout, "\t%s", l->name);
363                 if (l->assignment) {
364                         f_print(fout, " = %s", l->assignment);
365                         last = l->assignment;
366                         count = 1;
367                 } else {
368                         if (last == NULL) {
369                                 f_print(fout, " = %d", count++);
370                         } else {
371                                 f_print(fout, " = %s + %d", last, count++);
372                         }
373                 }
374                 f_print(fout, ",\n");
375         }
376         f_print(fout, "};\n");
377         f_print(fout, "typedef enum %s %s;\n", name, name);
378 }
379
380 static void
381 ptypedef(definition *def)
382 {
383         const char *name = def->def_name;
384         const char *old = def->def.ty.old_type;
385         char prefix[8]; /* enough to contain "struct ", including NUL */
386         relation rel = def->def.ty.rel;
387
388
389         if (!streq(name, old)) {
390                 if (streq(old, "string")) {
391                         old = "char";
392                         rel = REL_POINTER;
393                 } else if (streq(old, "opaque")) {
394                         old = "char";
395                 } else if (streq(old, "bool")) {
396                         old = "bool_t";
397                 }
398                 if (undefined2(old, name) && def->def.ty.old_prefix) {
399                         s_print(prefix, "%s ", def->def.ty.old_prefix);
400                 } else {
401                         prefix[0] = 0;
402                 }
403                 f_print(fout, "typedef ");
404                 switch (rel) {
405                 case REL_ARRAY:
406                         f_print(fout, "struct {\n");
407                         f_print(fout, "\tu_int %s_len;\n", name);
408                         f_print(fout, "\t%s%s *%s_val;\n", prefix, old, name);
409                         f_print(fout, "} %s", name);
410                         break;
411                 case REL_POINTER:
412                         f_print(fout, "%s%s *%s", prefix, old, name);
413                         break;
414                 case REL_VECTOR:
415                         f_print(fout, "%s%s %s[%s]", prefix, old, name,
416                                 def->def.ty.array_max);
417                         break;
418                 case REL_ALIAS:
419                         f_print(fout, "%s%s %s", prefix, old, name);
420                         break;
421                 }
422                 f_print(fout, ";\n");
423         }
424 }
425
426 void
427 pdeclaration(const char *name, declaration *dec, int tab, 
428              const char *separator)
429 {
430         char buf[8];    /* enough to hold "struct ", include NUL */
431         const char *prefix;
432         const char *type;
433
434         if (streq(dec->type, "void")) {
435                 return;
436         }
437         tabify(fout, tab);
438         if (streq(dec->type, name) && !dec->prefix) {
439                 f_print(fout, "struct ");
440         }
441         if (streq(dec->type, "string")) {
442                 f_print(fout, "char *%s", dec->name);
443         } else {
444                 prefix = "";
445                 if (streq(dec->type, "bool")) {
446                         type = "bool_t";
447                 } else if (streq(dec->type, "opaque")) {
448                         type = "char";
449                 } else {
450                         if (dec->prefix) {
451                                 s_print(buf, "%s ", dec->prefix);
452                                 prefix = buf;
453                         }
454                         type = dec->type;
455                 }
456                 switch (dec->rel) {
457                 case REL_ALIAS:
458                         f_print(fout, "%s%s %s", prefix, type, dec->name);
459                         break;
460                 case REL_VECTOR:
461                         f_print(fout, "%s%s %s[%s]", prefix, type, dec->name,
462                                 dec->array_max);
463                         break;
464                 case REL_POINTER:
465                         f_print(fout, "%s%s *%s", prefix, type, dec->name);
466                         break;
467                 case REL_ARRAY:
468                         f_print(fout, "struct {\n");
469                         tabify(fout, tab);
470                         f_print(fout, "\tu_int %s_len;\n", dec->name);
471                         tabify(fout, tab);
472                         f_print(fout, "\t%s%s *%s_val;\n", prefix, type, dec->name);
473                         tabify(fout, tab);
474                         f_print(fout, "} %s", dec->name);
475                         break;
476                 }
477         }
478         f_print(fout, separator );
479 }
480
481 static int
482 undefined2(const char *type, const char *stop)
483 {
484         list *l;
485         definition *def;
486
487         for (l = defined; l != NULL; l = l->next) {
488                 def = (definition *) l->val;
489                 if (def->def_kind != DEF_PROGRAM) {
490                         if (streq(def->def_name, stop)) {
491                                 return (1);
492                         } else if (streq(def->def_name, type)) {
493                                 return (0);
494                         }
495                 }
496         }
497         return (1);
498 }