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

  1. #ifndef lint
  2. static char    sccsid[] = "@(#)rpc_cout.c 1.1 86/03/26 (C) 1986 SMI";
  3. #endif
  4.  
  5. /*
  6.  * rpc_cout.c, XDR routine outputter for the RPC protocol compiler
  7.  * Copyright (C) 1986, Sun Microsystems, Inc.
  8.  */
  9. #include <stdio.h>
  10. #include <strings.h>
  11. #include "rpc_util.h"
  12. #include "rpc_parse.h"
  13.  
  14. #define SPECIALDEF 1    /* special if definition created by compiler */
  15. #define NORMALDEF 0        /* normal if definition created by human */
  16.  
  17.  
  18. /*
  19.  * Emit the C-routine for the given definition
  20.  */
  21. void
  22. emit(def)
  23.     definition *def;
  24. {
  25.     do_emit(def,NORMALDEF);
  26. }
  27.  
  28.  
  29.  
  30. static
  31. do_emit(def,special)
  32.     definition *def;
  33.     int special;
  34. {
  35.  
  36.     if (def->def_kind == DEF_PROGRAM) {
  37.         return;
  38.     }
  39.     if (isprinted(def->def_name)) {
  40.         return;
  41.     }
  42.     print_undefineds(def);
  43.     print_header(def,special);
  44.     switch (def->def_kind) {
  45.     case DEF_UNION:
  46.         emit_union(def);
  47.         break;
  48.     case DEF_ARRAY:
  49.         emit_array(def);
  50.         break;
  51.     case DEF_ENUM:
  52.         emit_enum(def);
  53.         break;
  54.     case DEF_STRUCT:    
  55.         emit_struct(def);
  56.         break;    
  57.     case DEF_TYPEDEF:
  58.         emit_typedef(def);
  59.         break;
  60.     }
  61.     print_trailer();
  62. }
  63.  
  64. static
  65. findtype(def,type)
  66.     definition *def;
  67.     char *type;
  68. {
  69.     if (def->def_kind == DEF_PROGRAM) {
  70.         return(0);
  71.     } else {
  72.         return(streq(def->def_name,type));
  73.     }
  74. }
  75.  
  76. static
  77. undefined(type)
  78.     char *type;
  79. {
  80.     definition *def;
  81.  
  82.     def = (definition *) FINDVAL(defined,type,findtype);
  83.     return(def == NULL);
  84. }
  85.  
  86. static    
  87. isprinted(name)
  88.     char *name;
  89. {
  90.     char *find;
  91.  
  92.     find = (char *) FINDVAL(printed,name,streq);
  93.     if (find) {
  94.         return(1);
  95.     }
  96.     STOREVAL(&printed, name);
  97.     return(0);
  98. }
  99.  
  100.  
  101.  
  102. static char *
  103. format_funcname(decp)
  104.     declaration *decp;
  105. {
  106.     char buf[256];
  107.     char *p;
  108.  
  109.     switch (decp->rel) {
  110.     case REL_POINTER:
  111.         sprintf(buf,"%s_ptr",decp->type);
  112.         break;
  113.     case REL_VECTOR:
  114.         if (streq(decp->type,"string") && decp->array_max == NULL) {
  115.             sprintf(buf,"wrapstring");
  116.         } else {
  117.             sprintf(buf,"%s_%s",decp->type,decp->array_max);
  118.         }
  119.         break;
  120.     case REL_ALIAS:
  121.         sprintf(buf,"%s",decp->type);
  122.         break;
  123.     }
  124.     p = alloc(strlen(buf) + 1);
  125.     strcpy(p,buf);
  126.     return(p);
  127. }
  128.  
  129.  
  130.  
  131.  
  132. static 
  133. print_undefineds(def)
  134.     definition *def;
  135. {
  136.     case_list *cl;
  137.     declaration *dflt;
  138.  
  139.     if (def->def_kind != DEF_UNION) {
  140.         return;
  141.     }
  142.     for (cl = def->def.un.cases; cl != NULL; cl = cl->next) {
  143.         if (cl->case_decl.rel != REL_ALIAS) {
  144.             emit_new(&cl->case_decl);
  145.         }
  146.     }
  147.     dflt = def->def.un.default_decl;
  148.     if (dflt) {
  149.         if (dflt->rel != REL_ALIAS) {
  150.             emit_new(dflt);
  151.         }
  152.     }
  153. }
  154.  
  155.  
  156. static    
  157. emit_new(dec)
  158.     declaration *dec;
  159. {
  160.     definition *def;
  161.  
  162.     if (dec->rel == REL_VECTOR && streq(dec->type,"string") && 
  163.             dec->array_max == NULL) {
  164.         return;
  165.     }
  166.     def = ALLOC(definition);
  167.     def->def_kind = DEF_TYPEDEF;
  168.     def->def_name = format_funcname(dec);
  169.     def->def.ty.old_prefix = dec->prefix;
  170.     def->def.ty.old_type = dec->type;
  171.     def->def.ty.rel = dec->rel;
  172.     def->def.ty.array_max = dec->array_max;
  173.     do_emit(def, SPECIALDEF);
  174. }
  175.  
  176. static
  177. print_header(def,special)
  178.     definition *def;    
  179.     int special;
  180. {
  181.     space();    
  182.     if (special) {
  183.         fprintf(fout,"static ");
  184.     }
  185.     fprintf(fout,"bool_t\n");
  186.     fprintf(fout,"xdr_%s(xdrs,objp)\n",def->def_name);
  187.     fprintf(fout,"\tXDR *xdrs;\n");
  188.     if (special) {
  189.         if (streq(def->def.ty.old_type,"string")) {
  190.             fprintf(fout,"\tchar *");    
  191.         } else if (streq(def->def.ty.old_type,"opaque")) {
  192.             fprintf(fout,"\tchar *");
  193.         } else if (streq(def->def.ty.old_type,"bool")) {
  194.             fprintf(fout,"\tbool_t *");
  195.         } else {
  196.             fprintf(fout,"\t%s *",def->def.ty.old_type);
  197.         }
  198.     } else {
  199.         fprintf(fout,"\t%s ",def->def_name);
  200.     }
  201.     if (def->def_kind != DEF_TYPEDEF  ||
  202.             ! isvectordef(def->def.ty.old_type,def->def.ty.rel)) {
  203.         fprintf(fout,"*");
  204.     }
  205.     fprintf(fout,"objp;\n");
  206.     fprintf(fout,"{\n");
  207. }
  208.  
  209. static
  210. typedefed(def,type)
  211.     definition *def;
  212.     char *type;
  213. {
  214.     if (def->def_kind != DEF_TYPEDEF || def->def.ty.old_prefix != NULL) {
  215.         return(0);
  216.     } else {
  217.         return (streq(def->def_name,type));
  218.     }
  219. }
  220.  
  221. static
  222. isvectordef(type,rel)
  223.     char *type;
  224.     relation rel;    
  225. {
  226.     definition *def;
  227.  
  228.     for (;;) {
  229.         switch (rel) {
  230.         case REL_VECTOR:
  231.             return(! streq(type,"string"));
  232.         case REL_POINTER:
  233.             return(0);
  234.         case REL_ALIAS:    
  235.             def = (definition *) FINDVAL(defined,type,typedefed);
  236.             if (def == NULL) {
  237.                 return(0);
  238.             }
  239.             type = def->def.ty.old_type;
  240.             rel = def->def.ty.rel;
  241.         }
  242.     }
  243. }
  244.     
  245. static
  246. print_trailer()
  247. {
  248.     fprintf(fout,"\treturn(TRUE);\n");
  249.     fprintf(fout,"}\n");
  250.     space();
  251. }
  252.  
  253. static
  254. print_ifopen(name)
  255.     char *name;
  256. {
  257.     fprintf(fout,"\tif (! xdr_%s(xdrs",name);
  258. }
  259.  
  260.  
  261. static
  262. print_ifarg(arg)
  263.     char *arg;
  264. {
  265.     fprintf(fout,", %s",arg);
  266. }
  267.  
  268.  
  269. static
  270. print_ifsizeof(prefix,type)
  271.     char *prefix;
  272.     char *type;
  273. {
  274.     if (streq(type,"bool")) {
  275.         fprintf(fout,", sizeof(bool_t), xdr_bool");
  276.     } else {
  277.         fprintf(fout,", sizeof(");
  278.         if (undefined(type) && prefix) {
  279.             fprintf(fout,"%s ",prefix);
  280.         }
  281.         fprintf(fout,"%s), xdr_%s",type,type);
  282.     }
  283. }
  284.  
  285. static
  286. print_ifclose()
  287. {
  288.     fprintf(fout,")) {\n");
  289.     fprintf(fout,"\t\treturn(FALSE);\n");
  290.     fprintf(fout,"\t}\n");
  291. }
  292.  
  293. static
  294. space()
  295. {
  296.     fprintf(fout,"\n\n");
  297. }
  298.  
  299. static
  300. print_ifstat(prefix,type,rel,amax,name)
  301.     char *prefix;
  302.     char *type;
  303.     relation rel;
  304.     char *amax;
  305.     char *name;
  306. {
  307.     char *alt = NULL;
  308.  
  309.     switch (rel) {
  310.     case REL_POINTER:
  311.         print_ifopen("pointer");
  312.         print_ifarg("(char *) ");
  313.         fprintf(fout,name);
  314.         print_ifsizeof(prefix,type);
  315.         break;
  316.     case REL_VECTOR:
  317.         if (streq(type,"string")) {
  318.             if (amax) {
  319.                 alt = "string";
  320.             } else {
  321.                 alt = "wrapstring";    
  322.             }
  323.         } else if (streq(type,"opaque")) {
  324.             alt = "opaque";
  325.         }
  326.         if (alt) {
  327.             print_ifopen(alt);
  328.             print_ifarg(name);
  329.         } else {
  330.             print_ifopen("vector");
  331.             print_ifarg("(char *) ");
  332.             fprintf(fout,name);
  333.         }
  334.         if (amax) {
  335.             print_ifarg(amax);
  336.         }
  337.         if (! alt) {
  338.             print_ifsizeof(prefix,type);
  339.         }
  340.         break;
  341.     case REL_ALIAS:
  342.         print_ifopen(type);
  343.         print_ifarg(name);
  344.         break;
  345.     }
  346.     print_ifclose();
  347. }
  348.  
  349.  
  350. /*ARGSUSED*/
  351. static
  352. emit_enum(def)
  353.     definition *def;
  354. {
  355.     print_ifopen("enum");
  356.     print_ifarg("(enum_t *) objp");
  357.     print_ifclose();
  358. }
  359.  
  360.  
  361. static
  362. emit_array(def)
  363.     definition *def;
  364. {
  365.     array_def *ad = &def->def.ar;
  366.     char *prefix = ad->array_prefix;
  367.     char *type = ad->array_type;
  368.     int bytes;
  369.  
  370.     bytes = streq(type,"opaque");
  371.     print_ifopen(bytes ? "bytes" : "array");
  372.     print_ifarg("&objp->");
  373.     fprintf(fout,ad->array_name);
  374.     print_ifarg("(char *) &objp->");
  375.     fprintf(fout,ad->len_name);
  376.     print_ifarg(ad->array_max);
  377.     if (! bytes) {
  378.         print_ifsizeof(prefix,type);
  379.     }
  380.     print_ifclose();
  381. }
  382.  
  383.  
  384.  
  385. static
  386. print_funcname(decp)
  387.     declaration *decp;
  388. {
  389.     char *buf;
  390.  
  391.     buf = format_funcname(decp);
  392.     fprintf(fout,buf);
  393.     free(buf);
  394. }
  395.  
  396.  
  397.     
  398. static
  399. emit_union(def)
  400.     definition *def;
  401. {
  402.     declaration *dflt;
  403.     
  404.  
  405.     print_tags(def);
  406.     print_ifopen("union");
  407.     print_ifarg("(enum_t *) &objp->");
  408.     fprintf(fout,def->def.un.enum_decl.name);
  409.     print_ifarg("(char *) &objp->");
  410.     fprintf(fout,"%s",def->def_name);
  411.     print_ifarg("choices");
  412.     dflt = def->def.un.default_decl;
  413.     if (dflt) {
  414.         print_ifarg("xdr_");
  415.         print_funcname(dflt);
  416.     } else {
  417.         print_ifarg("NULL");
  418.     }
  419.     print_ifclose();
  420. }
  421.  
  422.  
  423.  
  424. static
  425. print_tags(def)
  426.     definition *def;
  427. {
  428.     case_list *cl;
  429.  
  430.     fprintf(fout,"\tstatic struct xdr_discrim choices[] = {\n",def->def_name);
  431.     for (cl = def->def.un.cases; cl != NULL; cl = cl->next) {
  432.         fprintf(fout,"\t\t{ (int) %s, xdr_",cl->case_name);
  433.         print_funcname(&cl->case_decl);
  434.         fprintf(fout," },\n");
  435.     }
  436.     fprintf(fout,"\t\t{ __dontcare__, NULL }\n");
  437.     fprintf(fout,"\t};\n");
  438.     fprintf(fout,"\n");    
  439. }
  440.  
  441.  
  442. static
  443. emit_struct(def)
  444.     definition *def;
  445. {
  446.     decl_list *dl;
  447.  
  448.     for (dl = def->def.st.decls; dl != NULL; dl = dl->next) {
  449.         print_stat(&dl->decl);
  450.     }
  451. }
  452.  
  453.  
  454.  
  455.         
  456. static
  457. emit_typedef(def)
  458.     definition *def;
  459. {
  460.     char *prefix = def->def.ty.old_prefix;
  461.     char *type = def->def.ty.old_type;
  462.     char *amax = def->def.ty.array_max;
  463.     relation rel = def->def.ty.rel;
  464.  
  465.     print_ifstat(prefix,type,rel,amax,"objp");
  466. }
  467.  
  468.  
  469.  
  470.  
  471.  
  472. static
  473. print_stat(dec) 
  474.     declaration *dec;
  475. {
  476.     char *prefix = dec->prefix;
  477.     char *type = dec->type;
  478.     char *amax = dec->array_max;
  479.     relation rel = dec->rel;
  480.     char name[256];
  481.  
  482.     if (isvectordef(type,rel)) {
  483.         sprintf(name,"objp->%s",dec->name);
  484.     } else {
  485.         sprintf(name,"&objp->%s",dec->name);
  486.     }
  487.     print_ifstat(prefix,type,rel,amax,name);
  488. }
  489.