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

  1. #ifndef lint 
  2. static char sccsid[] = "@(#)rpc_svcout.c 1.1 86/03/26 (C) 1986 SMI";
  3. #endif
  4.  
  5. /*
  6.  * rpc_svcout.c, Server-skeleton outputter for the RPC protocol compiler
  7.  * Copyright (C) 1986, Sun Microsytsems, Inc.
  8.  */
  9. #include <stdio.h>
  10. #include <strings.h>
  11. #include "rpc_parse.h"
  12. #include "rpc_util.h"
  13.  
  14. static char RQSTP[] = "rqstp";
  15. static char TRANSP[] = "transp";
  16. static char ARG[] = "argument";
  17. static char RESULT[] = "result";
  18. static char ROUTINE[] = "local";
  19.  
  20.  
  21. /*
  22.  * write most of the service, that is,
  23.  * everything but the registrations.
  24.  */
  25. void
  26. write_most()
  27. {
  28.     list *l;
  29.     definition *def;
  30.     version_list *vp;
  31.  
  32.  
  33.     for (l = defined; l != NULL; l = l->next) {
  34.         def = (definition *) l->val;
  35.         if (def->def_kind == DEF_PROGRAM) {
  36.             write_program(def);
  37.         }
  38.     }
  39.     fprintf(fout,"\n\n");
  40.     fprintf(fout,"main()\n");
  41.     fprintf(fout,"{\n");
  42.     fprintf(fout,"\tSVCXPRT *%s;\n",TRANSP);
  43.     fprintf(fout,"\n");
  44.     for (l = defined; l != NULL; l = l->next) {
  45.         def = (definition *) l->val;
  46.         if (def->def_kind != DEF_PROGRAM) {    
  47.             continue;
  48.         }
  49.         for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
  50.              fprintf(fout,"\tpmap_unset(%s, %s);\n",def->def_name,vp->vers_name);
  51.         }
  52.     }
  53. }
  54.  
  55.  
  56. /*
  57.  * write a registration for the given transport
  58.  */
  59. void
  60. write_register(transp)
  61.     char *transp;
  62. {
  63.     list *l;
  64.     definition *def;
  65.     version_list *vp;
  66.  
  67.     fprintf(fout,"\n");
  68.     fprintf(fout,"\t%s = svc%s_create(RPC_ANYSOCK",TRANSP,transp);
  69.     if (streq(transp,"tcp")) {
  70.         fprintf(fout,", 0, 0");
  71.     }
  72.     fprintf(fout,");\n");
  73.     fprintf(fout,"\tif (%s == NULL) {\n",TRANSP);
  74.     fprintf(fout,"\t\tfprintf(stderr,\"cannot create %s service.\\n\");\n",transp);
  75.     fprintf(fout,"\t\texit(1);\n");
  76.     fprintf(fout,"\t}\n");
  77.  
  78.     for (l = defined; l != NULL; l = l->next) {
  79.         def = (definition *) l->val;
  80.         if (def->def_kind != DEF_PROGRAM) {    
  81.             continue;
  82.         }
  83.         for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
  84.             fprintf(fout,
  85.                 "\tif (! svc_register(%s, %s, %s, ",
  86.                 TRANSP,def->def_name,vp->vers_name);
  87.             pvname(def->def_name,vp->vers_num);
  88.             fprintf(fout,", IPPROTO_%s)) {\n",
  89.                 streq(transp,"udp") ? "UDP" : "TCP");
  90.              fprintf(fout,
  91.                 "\t\tfprintf(stderr,\"unable to register (%s, %s, %s).\\n\");\n",
  92.                 def->def_name,vp->vers_name, transp);
  93.             fprintf(fout,"\t\texit(1);\n");
  94.             fprintf(fout,"\t}\n");
  95.         }
  96.     }
  97. }
  98.  
  99.  
  100. /*
  101.  * write the rest of the service
  102.  */
  103. void
  104. write_rest()
  105. {
  106.     fprintf(fout,"\tsvc_run();\n");
  107.     fprintf(fout,"\tfprintf(stderr,\"svc_run returned\\n\");\n");
  108.     fprintf(fout,"\texit(1);\n");
  109.     fprintf(fout,"}\n");
  110. }
  111.  
  112.  
  113.  
  114. static
  115. write_program(def)
  116.     definition *def;
  117. {
  118.     version_list *vp;
  119.     proc_list *proc;
  120.     int filled;
  121.  
  122.     for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
  123.         fprintf(fout,"\n");
  124.         fprintf(fout,"static void\n");
  125.         pvname(def->def_name, vp->vers_num);
  126.         fprintf(fout,"(%s, %s)\n",RQSTP,TRANSP);
  127.         fprintf(fout,"    struct svc_req *%s;\n",RQSTP);
  128.         fprintf(fout,"    SVCXPRT *%s;\n",TRANSP);
  129.         fprintf(fout,"{\n");
  130.  
  131.         filled = 0;    
  132.         fprintf(fout,"\tunion {\n");
  133.         for (proc = vp->procs; proc != NULL; proc = proc->next) {
  134.             if (streq(proc->arg_type,"void")) {
  135.                 continue;
  136.             }
  137.             filled = 1;
  138.             fprintf(fout,"\t\t");    
  139.             if (proc->arg_prefix) {
  140.                 if (streq(proc->arg_prefix,"enum")) {
  141.                     fprintf(fout,"enum ");
  142.                 } else {
  143.                     fprintf(fout,"struct ");
  144.                 }
  145.             }
  146.             fprintf(fout,"%s ",proc->arg_type);
  147.             pvname(proc->proc_name,vp->vers_num);
  148.             fprintf(fout,"_arg;\n");
  149.         }
  150.         if (! filled) {
  151.             fprintf(fout,"\t\tint fill;\n");
  152.         }
  153.         fprintf(fout,"\t} %s;\n",ARG);
  154.         fprintf(fout,"\tchar *%s;\n",RESULT);
  155.         fprintf(fout,"\tbool_t (*xdr_%s)(), (*xdr_%s)();\n",ARG,RESULT);
  156.         fprintf(fout,"\tchar *(*%s)();\n",ROUTINE);
  157.         for (proc = vp->procs; proc != NULL; proc = proc->next) {
  158.             fprintf(fout,"\textern ");
  159.             if (proc->res_prefix) {
  160.                 fprintf(fout,"%s ",proc->res_prefix);
  161.             }
  162.             fprintf(fout,"%s *",proc->res_type);
  163.             pvname(proc->proc_name,vp->vers_num);
  164.             fprintf(fout,"();\n");
  165.         }
  166.         fprintf(fout,"\n");
  167.         fprintf(fout,"\tswitch (%s->rq_proc) {\n",RQSTP);
  168.  
  169.         fprintf(fout,"\tcase NULLPROC:\n");
  170.         fprintf(fout,"\t\tsvc_sendreply(%s, xdr_void, NULL);\n",TRANSP);
  171.         fprintf(fout,"\t\treturn;\n\n");
  172.  
  173.         for (proc = vp->procs; proc != NULL; proc = proc->next) {
  174.             fprintf(fout,"\tcase %s:\n",proc->proc_name);
  175.             fprintf(fout,"\t\txdr_%s = xdr_%s;\n",ARG,proc->arg_type);
  176.             fprintf(fout,"\t\txdr_%s = xdr_%s;\n",RESULT,proc->res_type);
  177.             fprintf(fout,"\t\t%s = (char *(*)()) ",ROUTINE);
  178.             pvname(proc->proc_name,vp->vers_num);
  179.             fprintf(fout,";\n");
  180.             fprintf(fout,"\t\tbreak;\n\n");
  181.         }
  182.         fprintf(fout,"\tdefault:\n");
  183.         printerr("noproc",TRANSP);
  184.         fprintf(fout,"\t\treturn;\n");
  185.         fprintf(fout,"\t}\n");
  186.  
  187.         fprintf(fout,"\tbzero(&%s, sizeof(%s));\n",ARG,ARG);
  188.         printif("getargs",TRANSP,"&",ARG);
  189.         printerr("decode",TRANSP);
  190.         fprintf(fout,"\t\treturn;\n");
  191.         fprintf(fout,"\t}\n");
  192.  
  193.         fprintf(fout,"\t%s = (*%s)(&%s);\n",RESULT,ROUTINE,ARG);
  194.         printif("sendreply",TRANSP,"",RESULT);
  195.         printerr("systemerr",TRANSP);
  196.         fprintf(fout,"\t}\n");
  197.  
  198.         printif("freeargs",TRANSP,"&",ARG);
  199.         fprintf(fout,"\t\tfprintf(stderr,\"unable to free arguments\\n\");\n");
  200.         fprintf(fout,"\t\texit(1);\n");
  201.         fprintf(fout,"\t}\n");
  202.  
  203.         fprintf(fout,"}\n\n");
  204.     }
  205. }
  206.     
  207. static
  208. printerr(err,transp)
  209.     char *err;
  210.     char *transp;
  211. {
  212.     fprintf(fout,"\t\tsvcerr_%s(%s);\n",err,transp);
  213. }
  214.  
  215. static
  216. printif(proc,transp,prefix,arg)
  217.     char *proc;
  218.     char *transp;
  219.     char *prefix;
  220.     char *arg;
  221. {
  222.     fprintf(fout,"\tif (! svc_%s(%s, xdr_%s, %s%s)) {\n",
  223.         proc,transp,arg,prefix,arg);
  224. }
  225.  
  226. static char *
  227. locase(str)
  228.     char *str;
  229. {
  230.     char c;
  231.     static char buf[100];
  232.     char *p = buf;
  233.  
  234.     while (c = *str++) {
  235.         *p++ = (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
  236.     }
  237.     *p = 0;
  238.     return(buf);
  239. }
  240.  
  241.  
  242. static
  243. pvname(pname,vnum)
  244.     char *pname;
  245.     char *vnum;
  246. {
  247.     fprintf(fout,"%s_%s",locase(pname),vnum);
  248. }
  249.  
  250.