home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume6 / rpc2 / part10 / rpc / rpcgen / rpc_hout.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  5.5 KB  |  303 lines

  1. #ifndef lint 
  2. static char sccsid[] = "@(#)rpc_hout.c 1.1 86/03/26 (C) 1986 SMI";
  3. #endif
  4.  
  5. /*
  6.  * rpc_hout.c, Header file outputter for the RPC protocol compiler
  7.  * Copyright (C) 1986, Sun Microsystems, Inc.
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <ctype.h>
  12. #include "rpc_util.h"
  13. #include "rpc_parse.h"
  14.  
  15.  
  16. /*
  17.  * Print the C-version of an xdr definition
  18.  */
  19. void
  20. print_datadef(def)
  21.     definition *def;
  22. {
  23.     
  24.     fprintf(fout,"\n");
  25.     switch (def->def_kind) {
  26.     case DEF_STRUCT:
  27.         pstructdef(def);
  28.         break;
  29.     case DEF_UNION:
  30.         puniondef(def);
  31.         break;
  32.     case DEF_ARRAY:
  33.         parraydef(def);
  34.         break;
  35.     case DEF_ENUM:
  36.         penumdef(def);
  37.         break;
  38.     case DEF_TYPEDEF:
  39.         ptypedef(def);
  40.         break;
  41.     case DEF_PROGRAM:
  42.         pprogramdef(def);
  43.         break;
  44.     }
  45.     fprintf(fout,"\n");
  46. }
  47.  
  48. /*
  49.  * Declare all of the functions that were written
  50.  */
  51. void
  52. print_funcdefs()
  53. {
  54.     list *l;
  55.     definition *def;
  56.  
  57.     for (l = defined; l != NULL; l = l->next) {
  58.         def = (definition *) l->val;
  59.         if (def->def_kind != DEF_PROGRAM) {
  60.             fprintf(fout,"bool_t xdr_%s();\n",def->def_name);
  61.         }
  62.     }
  63. }
  64.  
  65. static
  66. pstructdef(def)
  67.     definition *def;
  68. {
  69.     decl_list *l;
  70.     char *name = def->def_name;
  71.  
  72.     fprintf(fout,"struct %s {\n",name);
  73.     for (l = def->def.st.decls; l != NULL; l = l->next) {
  74.         pdeclaration(name,&l->decl,1);
  75.     }
  76.     fprintf(fout,"};\n");
  77.     fprintf(fout,"typedef struct %s %s;\n",name,name);
  78. }
  79.  
  80. static
  81. puniondef(def)
  82.     definition *def;
  83. {
  84.     case_list *l;
  85.     char *name = def->def_name;
  86.     declaration *decl;
  87.  
  88.     fprintf(fout,"struct %s {\n",name);
  89.     decl = &def->def.un.enum_decl;
  90.     fprintf(fout,"\t%s %s;\n",decl->type,decl->name);
  91.     fprintf(fout,"\tunion {\n");
  92.     for (l = def->def.un.cases; l != NULL; l = l->next) {
  93.         pdeclaration(name,&l->case_decl,2);
  94.     }
  95.     decl = def->def.un.default_decl;
  96.     if (decl && ! streq(decl->type,"void")) {
  97.         pdeclaration(name,decl,2);
  98.     }
  99.     fprintf(fout,"\t} %s;\n",name);
  100.     fprintf(fout,"};\n");
  101.     fprintf(fout,"typedef struct %s %s;\n",name,name);
  102. }
  103.  
  104.  
  105.  
  106. static
  107. pdefine(name,num)
  108.     char *name;
  109.     char *num;    
  110. {
  111.     
  112.     fprintf(fout,"#define %s %s\n",name,num);
  113. }
  114.  
  115. static
  116. dprinted(stop,start)
  117.     proc_list *stop;
  118.     version_list *start;
  119. {
  120.     version_list *vers;
  121.     proc_list *proc;
  122.  
  123.     for (vers = start; vers != NULL; vers = vers->next) {
  124.         for (proc = vers->procs; proc != NULL; proc = proc->next) {
  125.             if (proc == stop) {
  126.                 return(0);
  127.             } else if (streq(proc->proc_name,stop->proc_name)) {
  128.                 return(1);
  129.             }
  130.         }
  131.     }
  132.     abort();    
  133.     /* NOTREACHED */
  134. }
  135.     
  136.     
  137. static
  138. pprogramdef(def)
  139.     definition *def;
  140. {
  141.     version_list *vers;
  142.     proc_list *proc;
  143.  
  144.     pdefine(def->def_name,def->def.pr.prog_num);
  145.     for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
  146.         pdefine(vers->vers_name,vers->vers_num);
  147.         for (proc = vers->procs; proc != NULL; proc = proc->next) {
  148.             if (! dprinted(proc,def->def.pr.versions)) {
  149.                 pdefine(proc->proc_name,proc->proc_num);
  150.             }
  151.         }
  152.     }
  153. }
  154.  
  155.  
  156. static
  157. penumdef(def)
  158.     definition *def;
  159. {
  160.     char *name = def->def_name;
  161.     enumval_list *l;
  162.     char *last = NULL;
  163.     int count = 0;
  164.  
  165.     fprintf(fout,"enum %s {\n",name);
  166.     for (l = def->def.en.vals; l != NULL; l = l->next) {
  167.         fprintf(fout,"\t%s",l->name);
  168.         if (l->assignment) {
  169.             fprintf(fout," = %s",l->assignment);
  170.             last = l->assignment;
  171.             count = 1;
  172.         } else {
  173.             if (last == NULL) {
  174.                 fprintf(fout," = %d",count++);
  175.             } else {
  176.                 fprintf(fout," = %s + %d",last,count++);
  177.             }
  178.         }    
  179.         fprintf(fout,",\n");
  180.     }
  181.     fprintf(fout,"};\n");
  182.     fprintf(fout,"typedef enum %s %s;\n",name,name);
  183. }
  184.  
  185. static
  186. parraydef(def)
  187.     definition *def;
  188. {
  189.     char *name = def->def_name;
  190.     char *atype = def->def.ar.array_type;
  191.     char *aname = def->def.ar.array_name;
  192.  
  193.     fprintf(fout,"struct %s {\n",name);
  194.     fprintf(fout,"\tu_int %s;\n",def->def.ar.len_name);
  195.     if (streq(atype,"opaque")) {
  196.         atype = "char";
  197.     }
  198.     fprintf(fout,"\t%s *%s;\n",atype,aname);
  199.     fprintf(fout,"};\n");
  200.     fprintf(fout,"typedef struct %s %s;\n",name,name);
  201. }
  202.     
  203.  
  204. static
  205. ptypedef(def)
  206.     definition *def;
  207. {
  208.     char *name = def->def_name;
  209.     char *old = def->def.ty.old_type;
  210.     char *prefix = def->def.ty.old_prefix;
  211.     relation rel = def->def.ty.rel;
  212.  
  213.     if (! streq(name,old)) {
  214.         if (streq(old,"string")) {
  215.             old = "char";
  216.             rel = REL_POINTER;
  217.         } else if (streq(old,"opaque")) {
  218.             old = "char";
  219.         } else if (streq(old,"bool")) {
  220.             old = "bool_t";
  221.         }    
  222.         fprintf(fout,"typedef ");
  223.         if (undefined2(old,name) && prefix) {
  224.             fprintf(fout,"%s ",prefix);
  225.         }
  226.         fprintf(fout,"%s ",old);
  227.         if (rel == REL_POINTER) {
  228.             fprintf(fout,"*");
  229.         }
  230.         fprintf(fout,name);
  231.         if (rel == REL_VECTOR) {
  232.             fprintf(fout,"[%s]",def->def.ty.array_max);
  233.         }
  234.         fprintf(fout,";\n");
  235.     }
  236. }
  237.  
  238. static
  239. pdeclaration(name,dec,tab)
  240.     char *name;
  241.     declaration *dec;
  242.     int tab;
  243. {    
  244.     if (streq(dec->type,"void")) {
  245.         return;
  246.     }
  247.     while (tab--) {    
  248.         fprintf(fout,"\t");
  249.     }
  250.     if (streq(dec->type,name) && ! dec->prefix) {
  251.         fprintf(fout,"struct ");
  252.     }
  253.     if (streq(dec->type,"string")) {
  254.         fprintf(fout,"char *%s",dec->name);
  255.     } else {
  256.         if (streq(dec->type,"bool")) {
  257.             fprintf(fout,"bool_t ");
  258.         } else if (streq(dec->type,"opaque")) {
  259.             fprintf(fout,"char ");
  260.         } else {
  261.             if (dec->prefix) {
  262.                 if (streq(dec->prefix,"enum")) {
  263.                     fprintf(fout,"enum ");
  264.                 } else {
  265.                     fprintf(fout,"struct ");
  266.                 }
  267.             }
  268.             fprintf(fout,"%s ",dec->type);
  269.         }
  270.         if (dec->rel == REL_POINTER) {
  271.             fprintf(fout,"*");
  272.         }
  273.         fprintf(fout,dec->name);
  274.         if (dec->rel == REL_VECTOR) {
  275.             fprintf(fout,"[%s]",dec->array_max);
  276.         }
  277.     }
  278.     fprintf(fout,";\n");
  279. }
  280.  
  281.  
  282.  
  283. static
  284. undefined2(type,stop)
  285.     char *type;
  286.     char *stop;
  287. {
  288.     list *l;
  289.     definition *def;
  290.  
  291.     for (l = defined; l != NULL; l = l->next) {
  292.         def = (definition *) l->val;
  293.         if (def->def_kind != DEF_PROGRAM) {
  294.             if (streq(def->def_name,stop)) {
  295.                 return(1);
  296.             } else if (streq(def->def_name,type)) {
  297.                 return(0);
  298.             }
  299.         }
  300.     }
  301.     return(1);
  302. }
  303.