1 /*        $NetBSD: rpc_hout.c,v 1.25 2016/01/23 02:33:09 dholland Exp $         */
2 /*
3  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4  * unrestricted use provided that this legend is included on all tape
5  * media and as a part of the software program in whole or part.  Users
6  * may copy or modify Sun RPC without charge, but are not authorized
7  * to license or distribute it to anyone else except as part of a product or
8  * program developed by the user or with the express written consent of
9  * Sun Microsystems, Inc.
10  *
11  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14  *
15  * Sun RPC is provided with no support and without any obligation on the
16  * part of Sun Microsystems, Inc. to assist in its use, correction,
17  * modification or enhancement.
18  *
19  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21  * OR ANY PART THEREOF.
22  *
23  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24  * or profits or other special, indirect and consequential damages, even if
25  * Sun has been advised of the possibility of such damages.
26  *
27  * Sun Microsystems, Inc.
28  * 2550 Garcia Avenue
29  * Mountain View, California  94043
30  */
31 
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
35 
36 #include <sys/cdefs.h>
37 #if defined(__RCSID) && !defined(lint)
38 #if 0
39 static char sccsid[] = "@(#)rpc_hout.c 1.12 89/02/22 (C) 1987 SMI";
40 #else
41 __RCSID("$NetBSD: rpc_hout.c,v 1.25 2016/01/23 02:33:09 dholland Exp $");
42 #endif
43 #endif
44 
45 /*
46  * rpc_hout.c, Header file outputter for the RPC protocol compiler
47  */
48 #include <ctype.h>
49 #include <err.h>
50 #include <stdio.h>
51 #include "rpc_scan.h"
52 #include "rpc_parse.h"
53 #include "rpc_util.h"
54 
55 static void pconstdef(definition *);
56 static void pargdef(definition *);
57 static void pstructdef(definition *);
58 static void puniondef(definition *);
59 static void pdefine(const char *, const char *);
60 static void puldefine(const char *, const char *);
61 static int define_printed(proc_list *, version_list *);
62 static void pprogramdef(definition *);
63 static void penumdef(definition *);
64 static void ptypedef(definition *);
65 static int undefined2(const char *, const char *);
66 static void cplusplusstart(void);
67 static void cplusplusend(void);
68 
69 /*
70  * Print the C-version of an xdr definition
71  */
72 void
print_datadef(definition * def)73 print_datadef(definition *def)
74 {
75 
76           if (def->def_kind == DEF_PROGRAM)       /* handle data only */
77                     return;
78 
79           if (def->def_kind != DEF_CONST) {
80                     f_print(fout, "\n");
81           }
82           switch (def->def_kind) {
83           case DEF_STRUCT:
84                     pstructdef(def);
85                     break;
86           case DEF_UNION:
87                     puniondef(def);
88                     break;
89           case DEF_ENUM:
90                     penumdef(def);
91                     break;
92           case DEF_TYPEDEF:
93                     ptypedef(def);
94                     break;
95           case DEF_PROGRAM:
96                     pprogramdef(def);
97                     break;
98           case DEF_CONST:
99                     pconstdef(def);
100                     break;
101           }
102 }
103 
104 void
print_progdef(definition * def)105 print_progdef(definition *def)
106 {
107           switch (def->def_kind) {
108           case DEF_PROGRAM:
109                     f_print(fout, "\n");
110                     pprogramdef(def);
111                     break;
112           case DEF_CONST:
113           case DEF_TYPEDEF:
114           case DEF_ENUM:
115           case DEF_UNION:
116           case DEF_STRUCT:
117                     break;
118           }
119 }
120 
121 void
print_funcdef(definition * def,int * did)122 print_funcdef(definition *def, int *did)
123 {
124           switch (def->def_kind) {
125           case DEF_PROGRAM:
126           case DEF_CONST:
127                     break;
128           case DEF_TYPEDEF:
129           case DEF_ENUM:
130           case DEF_UNION:
131           case DEF_STRUCT:
132                     if (!*did) {
133                         f_print(fout, "\n");
134                         cplusplusstart();
135                         *did = 1;
136                     }
137                     pxdrfuncdecl(def->def_name,
138                         def->def_kind != DEF_TYPEDEF ||
139                         !isvectordef(def->def.ty.old_type, def->def.ty.rel));
140                     break;
141           }
142 }
143 
144 void
print_funcend(int did)145 print_funcend(int did) {
146           if (did) {
147                     cplusplusend();
148           }
149 }
150 
151 void
pxdrfuncdecl(const char * name,int pointerp)152 pxdrfuncdecl(const char *name, int pointerp)
153 {
154 
155           f_print(fout, "bool_t xdr_%s(XDR *, %s%s);\n", name,
156               name, pointerp ? (" *") : "");
157 }
158 
159 
160 static void
pconstdef(definition * def)161 pconstdef(definition *def)
162 {
163           pdefine(def->def_name, def->def.co);
164 }
165 
166 /* print out the definitions for the arguments of functions in the
167    header file
168 */
169 static void
pargdef(definition * def)170 pargdef(definition *def)
171 {
172           decl_list *l;
173           version_list *vers;
174           char   *name;
175           proc_list *plist;
176           int did;
177 
178 
179           for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
180                     for (plist = vers->procs; plist != NULL; plist = plist->next) {
181                               if (!newstyle || plist->arg_num < 2) {
182                                         continue; /* old style or single args */
183                               }
184                               name = plist->args.argname;
185                               f_print(fout, "struct %s {\n", name);
186                               for (l = plist->args.decls;
187                                   l != NULL; l = l->next) {
188                                         pdeclaration(name, &l->decl, 1, ";\n");
189                               }
190                               f_print(fout, "};\n");
191                               f_print(fout, "typedef struct %s %s;\n", name, name);
192                     }
193           }
194           did = 0;
195           for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
196                     for (plist = vers->procs; plist != NULL; plist = plist->next) {
197                               if (!newstyle || plist->arg_num < 2) {
198                                         continue; /* old style or single args */
199                               }
200 
201                               if (!did) {
202                                         cplusplusstart();
203                                         did = 1;
204                               }
205                               pxdrfuncdecl(plist->args.argname, 1);
206                     }
207           }
208           if (did) {
209                     cplusplusend();
210           }
211 
212 }
213 
214 
215 static void
pstructdef(definition * def)216 pstructdef(definition *def)
217 {
218           decl_list *l;
219           const char *name = def->def_name;
220 
221           f_print(fout, "struct %s {\n", name);
222           for (l = def->def.st.decls; l != NULL; l = l->next) {
223                     pdeclaration(name, &l->decl, 1, ";\n");
224           }
225           f_print(fout, "};\n");
226           f_print(fout, "typedef struct %s %s;\n", name, name);
227 }
228 
229 static void
puniondef(definition * def)230 puniondef(definition *def)
231 {
232           case_list *l;
233           const char *name = def->def_name;
234           declaration *decl;
235 
236           f_print(fout, "struct %s {\n", name);
237           decl = &def->def.un.enum_decl;
238           if (streq(decl->type, "bool")) {
239                     f_print(fout, "\tbool_t %s;\n", decl->name);
240           } else {
241                     f_print(fout, "\t%s %s;\n", decl->type, decl->name);
242           }
243           f_print(fout, "\tunion {\n");
244           for (l = def->def.un.cases; l != NULL; l = l->next) {
245                     if (l->contflag == 0)
246                               pdeclaration(name, &l->case_decl, 2, ";\n");
247           }
248           decl = def->def.un.default_decl;
249           if (decl && !streq(decl->type, "void")) {
250                     pdeclaration(name, decl, 2, ";\n");
251           }
252           f_print(fout, "\t} %s_u;\n", name);
253           f_print(fout, "};\n");
254           f_print(fout, "typedef struct %s %s;\n", name, name);
255 }
256 
257 static void
pdefine(const char * name,const char * num)258 pdefine(const char *name, const char *num)
259 {
260           f_print(fout, "#define %s %s\n", name, num);
261 }
262 
263 static void
puldefine(const char * name,const char * num)264 puldefine(const char *name, const char *num)
265 {
266           f_print(fout, "#define %s %s\n", name, num);
267 }
268 
269 static int
define_printed(proc_list * stop,version_list * start)270 define_printed(proc_list *stop, version_list *start)
271 {
272           version_list *vers;
273           proc_list *proc;
274 
275           for (vers = start; vers != NULL; vers = vers->next) {
276                     for (proc = vers->procs; proc != NULL; proc = proc->next) {
277                               if (proc == stop) {
278                                         return (0);
279                               } else
280                                         if (streq(proc->proc_name, stop->proc_name)) {
281                                                   return (1);
282                                         }
283                     }
284           }
285           errx(1, "Internal error at %s:%d: procedure not found",
286               __FILE__, __LINE__);
287           /* NOTREACHED */
288 }
289 
290 static void
cplusplusstart(void)291 cplusplusstart(void)
292 {
293           if (BSDflag)
294                     f_print(fout, "__BEGIN_DECLS\n");
295           else
296                     f_print(fout, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n");
297 }
298 
299 static void
cplusplusend(void)300 cplusplusend(void)
301 {
302           if (BSDflag)
303                     f_print(fout, "__END_DECLS\n");
304           else
305                     f_print(fout, "#ifdef __cplusplus\n};\n#endif\n");
306 }
307 
308 static void
pprogramdef(definition * def)309 pprogramdef(definition *def)
310 {
311           version_list *vers;
312           proc_list *proc;
313 
314           pargdef(def);
315 
316           puldefine(def->def_name, def->def.pr.prog_num);
317           for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
318                     if (tblflag) {
319                               f_print(fout, "extern struct rpcgen_table %s_%s_table[];\n",
320                                   locase(def->def_name), vers->vers_num);
321                               f_print(fout, "extern %s_%s_nproc;\n",
322                                   locase(def->def_name), vers->vers_num);
323                     }
324                     puldefine(vers->vers_name, vers->vers_num);
325                     for (proc = vers->procs; proc != NULL; proc = proc->next) {
326                               if (!define_printed(proc, def->def.pr.versions)) {
327                                         puldefine(proc->proc_name, proc->proc_num);
328                               }
329                     }
330           }
331 
332           /*
333            * Print out 3 definitions, one for ANSI-C, another for C++, a
334            * third for old style C
335            */
336           f_print(fout, "\n");
337           cplusplusstart();
338           for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
339                     for (proc = vers->procs; proc != NULL; proc = proc->next) {
340                               pprocdef(proc, vers, "CLIENT *", 0);
341                               pprocdef(proc, vers, "struct svc_req *", 1);
342                     }
343           }
344           cplusplusend();
345 }
346 
347 void
pprocdef(proc_list * proc,version_list * vp,const char * addargtype,int server_p)348 pprocdef(proc_list *proc, version_list *vp, const char *addargtype,
349      int server_p)
350 {
351           decl_list *dl;
352 
353           if (Mflag) {
354                     if (server_p)
355                               f_print(fout, "bool_t ");
356                     else
357                               f_print(fout, "enum clnt_stat ");
358           } else {
359                     ptype(proc->res_prefix, proc->res_type, 1);
360                     f_print(fout, "*");
361           }
362           if (server_p)
363                     pvname_svc(proc->proc_name, vp->vers_num);
364           else
365                     pvname(proc->proc_name, vp->vers_num);
366 
367           f_print(fout, "(");
368           if (proc->arg_num < 2 && newstyle &&
369               streq(proc->args.decls->decl.type, "void")) {
370                     /* 0 argument in new style:  do nothing */
371           } else {
372                     for (dl = proc->args.decls; dl != NULL; dl = dl->next) {
373                               ptype(dl->decl.prefix, dl->decl.type, 1);
374                               if (!newstyle)
375                                         f_print(fout, "*");
376                               f_print(fout, ", ");
377                     }
378           }
379           if (Mflag) {
380                     if (streq(proc->res_type, "void"))
381                               f_print(fout, "char");
382                     else
383                               ptype(proc->res_prefix, proc->res_type, 0);
384                     if (!isvectordef(proc->res_type, REL_ALIAS))
385                               f_print(fout, "*");
386                     f_print(fout, ", ");
387           }
388           f_print(fout, "%s);\n", addargtype);
389 }
390 
391 
392 static void
penumdef(definition * def)393 penumdef(definition *def)
394 {
395           const char *name = def->def_name;
396           enumval_list *l;
397           const char *last = NULL;
398           int count = 0;
399           const char *first = "";
400 
401           f_print(fout, "enum %s {\n", name);
402           for (l = def->def.en.vals; l != NULL; l = l->next) {
403                     f_print(fout, "%s\t%s", first, l->name);
404                     if (l->assignment) {
405                               f_print(fout, " = %s", l->assignment);
406                               last = l->assignment;
407                               count = 1;
408                     } else {
409                               if (last == NULL) {
410                                         f_print(fout, " = %d", count++);
411                               } else {
412                                         f_print(fout, " = %s + %d", last, count++);
413                               }
414                     }
415                     first = ",\n";
416           }
417           f_print(fout, "\n};\n");
418           f_print(fout, "typedef enum %s %s;\n", name, name);
419 }
420 
421 static void
ptypedef(definition * def)422 ptypedef(definition *def)
423 {
424           const char *name = def->def_name;
425           const char *old = def->def.ty.old_type;
426           char    prefix[8];  /* enough to contain "struct ", including NUL */
427           relation rel = def->def.ty.rel;
428 
429 
430           if (!streq(name, old)) {
431                     if (streq(old, "string")) {
432                               old = "char";
433                               rel = REL_POINTER;
434                     } else
435                               if (streq(old, "opaque")) {
436                                         old = "char";
437                               } else
438                                         if (streq(old, "bool")) {
439                                                   old = "bool_t";
440                                         }
441                     if (undefined2(old, name) && def->def.ty.old_prefix) {
442                               s_print(prefix, "%s ", def->def.ty.old_prefix);
443                     } else {
444                               prefix[0] = 0;
445                     }
446                     f_print(fout, "typedef ");
447                     switch (rel) {
448                     case REL_ARRAY:
449                               f_print(fout, "struct {\n");
450                               f_print(fout, "\tunsigned int %s_len;\n", name);
451                               f_print(fout, "\t%s%s *%s_val;\n", prefix, old, name);
452                               f_print(fout, "} %s", name);
453                               break;
454                     case REL_POINTER:
455                               f_print(fout, "%s%s *%s", prefix, old, name);
456                               break;
457                     case REL_VECTOR:
458                               f_print(fout, "%s%s %s[%s]", prefix, old, name,
459                                   def->def.ty.array_max);
460                               break;
461                     case REL_ALIAS:
462                               f_print(fout, "%s%s %s", prefix, old, name);
463                               break;
464                     }
465                     f_print(fout, ";\n");
466           }
467 }
468 
469 void
pdeclaration(const char * name,declaration * dec,int tab,const char * separator)470 pdeclaration(const char *name, declaration *dec, int tab,
471                const char *separator)
472 {
473           char    buf[8];               /* enough to hold "struct ", include NUL */
474           const char *prefix;
475           const char *type;
476 
477           if (streq(dec->type, "void")) {
478                     return;
479           }
480           tabify(fout, tab);
481           if (streq(dec->type, name) && !dec->prefix) {
482                     f_print(fout, "struct ");
483           }
484           if (streq(dec->type, "string")) {
485                     f_print(fout, "char *%s", dec->name);
486           } else {
487                     prefix = "";
488                     if (streq(dec->type, "bool")) {
489                               type = "bool_t";
490                     } else
491                               if (streq(dec->type, "opaque")) {
492                                         type = "char";
493                               } else {
494                                         if (dec->prefix) {
495                                                   s_print(buf, "%s ", dec->prefix);
496                                                   prefix = buf;
497                                         }
498                                         type = dec->type;
499                               }
500                     switch (dec->rel) {
501                     case REL_ALIAS:
502                               f_print(fout, "%s%s %s", prefix, type, dec->name);
503                               break;
504                     case REL_VECTOR:
505                               f_print(fout, "%s%s %s[%s]", prefix, type, dec->name,
506                                   dec->array_max);
507                               break;
508                     case REL_POINTER:
509                               f_print(fout, "%s%s *%s", prefix, type, dec->name);
510                               break;
511                     case REL_ARRAY:
512                               f_print(fout, "struct {\n");
513                               tabify(fout, tab);
514                               f_print(fout, "\tunsigned int %s_len;\n", dec->name);
515                               tabify(fout, tab);
516                               f_print(fout, "\t%s%s *%s_val;\n", prefix, type, dec->name);
517                               tabify(fout, tab);
518                               f_print(fout, "} %s", dec->name);
519                               break;
520                     }
521           }
522           f_print(fout, "%s", separator);
523 }
524 
525 static int
undefined2(const char * type,const char * stop)526 undefined2(const char *type, const char *stop)
527 {
528           list   *l;
529           definition *def;
530 
531           for (l = defined; l != NULL; l = l->next) {
532                     def = (definition *) l->val;
533                     if (def->def_kind != DEF_PROGRAM) {
534                               if (streq(def->def_name, stop)) {
535                                         return (1);
536                               } else
537                                         if (streq(def->def_name, type)) {
538                                                   return (0);
539                                         }
540                     }
541           }
542           return (1);
543 }
544