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