Blame sunrpc/rpc_sample.c

Packit 6c4009
/*
Packit 6c4009
 * From: @(#)rpc_sample.c  1.1  90/08/30
Packit 6c4009
 *
Packit 6c4009
 * Copyright (c) 2010, Oracle America, Inc.
Packit 6c4009
 * Redistribution and use in source and binary forms, with or without
Packit 6c4009
 * modification, are permitted provided that the following conditions are
Packit 6c4009
 * met:
Packit 6c4009
 *
Packit 6c4009
 *     * Redistributions of source code must retain the above copyright
Packit 6c4009
 *       notice, this list of conditions and the following disclaimer.
Packit 6c4009
 *     * Redistributions in binary form must reproduce the above
Packit 6c4009
 *       copyright notice, this list of conditions and the following
Packit 6c4009
 *       disclaimer in the documentation and/or other materials
Packit 6c4009
 *       provided with the distribution.
Packit 6c4009
 *     * Neither the name of the "Oracle America, Inc." nor the names of its
Packit 6c4009
 *       contributors may be used to endorse or promote products derived
Packit 6c4009
 *       from this software without specific prior written permission.
Packit 6c4009
 *
Packit 6c4009
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit 6c4009
 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit 6c4009
 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
Packit 6c4009
 *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
Packit 6c4009
 *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
Packit 6c4009
 *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 6c4009
 *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
Packit 6c4009
 *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit 6c4009
 *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
Packit 6c4009
 *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
Packit 6c4009
 *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 6c4009
 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * rpc_sample.c, Sample client-server code outputter for the RPC protocol compiler
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include "rpc_parse.h"
Packit 6c4009
#include "rpc_util.h"
Packit 6c4009
#include "proto.h"
Packit 6c4009
Packit 6c4009
Packit 6c4009
static const char RQSTP[] = "rqstp";
Packit 6c4009
Packit 6c4009
static void write_sample_client (const char *program_name, version_list * vp);
Packit 6c4009
static void write_sample_server (definition * def);
Packit 6c4009
static void return_type (proc_list * plist);
Packit 6c4009
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
write_sample_svc (definition * def)
Packit 6c4009
{
Packit 6c4009
Packit 6c4009
  if (def->def_kind != DEF_PROGRAM)
Packit 6c4009
    return;
Packit 6c4009
  write_sample_server (def);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
write_sample_clnt (definition * def)
Packit 6c4009
{
Packit 6c4009
  version_list *vp;
Packit 6c4009
  int count = 0;
Packit 6c4009
Packit 6c4009
  if (def->def_kind != DEF_PROGRAM)
Packit 6c4009
    return 0;
Packit 6c4009
  /* generate sample code for each version */
Packit 6c4009
  for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
Packit 6c4009
    {
Packit 6c4009
      write_sample_client (def->def_name, vp);
Packit 6c4009
      ++count;
Packit 6c4009
    }
Packit 6c4009
  return count;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
write_sample_client (const char *program_name, version_list * vp)
Packit 6c4009
{
Packit 6c4009
  proc_list *proc;
Packit 6c4009
  int i;
Packit 6c4009
  decl_list *l;
Packit 6c4009
Packit 6c4009
  f_print (fout, "\n\nvoid\n");
Packit 6c4009
  pvname (program_name, vp->vers_num);
Packit 6c4009
  if (Cflag)
Packit 6c4009
    f_print (fout, "(char *host)\n{\n");
Packit 6c4009
  else
Packit 6c4009
    f_print (fout, "(host)\nchar *host;\n{\n");
Packit 6c4009
  f_print (fout, "\tCLIENT *clnt;\n");
Packit 6c4009
Packit 6c4009
  i = 0;
Packit 6c4009
  for (proc = vp->procs; proc != NULL; proc = proc->next)
Packit 6c4009
    {
Packit 6c4009
      f_print (fout, "\t");
Packit 6c4009
      ++i;
Packit 6c4009
      if (mtflag)
Packit 6c4009
	{
Packit 6c4009
	  f_print (fout, "enum clnt_stat retval_%d;\n\t", i);
Packit 6c4009
	  ptype (proc->res_prefix, proc->res_type, 1);
Packit 6c4009
	  if (!streq (proc->res_type, "void"))
Packit 6c4009
	    f_print (fout, "result_%d;\n", i);
Packit 6c4009
	  else
Packit 6c4009
	    fprintf (fout, "*result_%d;\n", i);
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  ptype (proc->res_prefix, proc->res_type, 1);
Packit 6c4009
	  f_print (fout, " *result_%d;\n", i);
Packit 6c4009
	}
Packit 6c4009
      /* print out declarations for arguments */
Packit 6c4009
      if (proc->arg_num < 2 && !newstyle)
Packit 6c4009
	{
Packit 6c4009
	  f_print (fout, "\t");
Packit 6c4009
	  if (!streq (proc->args.decls->decl.type, "void"))
Packit 6c4009
	    {
Packit 6c4009
	      ptype (proc->args.decls->decl.prefix,
Packit 6c4009
		     proc->args.decls->decl.type, 1);
Packit 6c4009
	      f_print (fout, " ");
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    f_print (fout, "char *");	/* cannot have "void" type */
Packit 6c4009
	  pvname (proc->proc_name, vp->vers_num);
Packit 6c4009
	  f_print (fout, "_arg;\n");
Packit 6c4009
	}
Packit 6c4009
      else if (!streq (proc->args.decls->decl.type, "void"))
Packit 6c4009
	{
Packit 6c4009
	  for (l = proc->args.decls; l != NULL; l = l->next)
Packit 6c4009
	    {
Packit 6c4009
	      f_print (fout, "\t");
Packit 6c4009
	      ptype (l->decl.prefix, l->decl.type, 1);
Packit 6c4009
	      if (strcmp (l->decl.type, "string") == 1)
Packit 6c4009
		f_print (fout, " ");
Packit 6c4009
	      pvname (proc->proc_name, vp->vers_num);
Packit 6c4009
	      f_print (fout, "_%s;\n", l->decl.name);
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* generate creation of client handle */
Packit 6c4009
  f_print(fout, "\n#ifndef\tDEBUG\n");
Packit 6c4009
  f_print (fout, "\tclnt = clnt_create (host, %s, %s, \"%s\");\n",
Packit 6c4009
	   program_name, vp->vers_name, tirpcflag ? "netpath" : "udp");
Packit 6c4009
  f_print (fout, "\tif (clnt == NULL) {\n");
Packit 6c4009
  f_print (fout, "\t\tclnt_pcreateerror (host);\n");
Packit 6c4009
  f_print (fout, "\t\texit (1);\n\t}\n");
Packit 6c4009
  f_print(fout, "#endif\t/* DEBUG */\n\n");
Packit 6c4009
Packit 6c4009
  /* generate calls to procedures */
Packit 6c4009
  i = 0;
Packit 6c4009
  for (proc = vp->procs; proc != NULL; proc = proc->next)
Packit 6c4009
    {
Packit 6c4009
      if (mtflag)
Packit 6c4009
	f_print(fout, "\tretval_%d = ",++i);
Packit 6c4009
      else
Packit 6c4009
	f_print (fout, "\tresult_%d = ", ++i);
Packit 6c4009
      pvname (proc->proc_name, vp->vers_num);
Packit 6c4009
      if (proc->arg_num < 2 && !newstyle)
Packit 6c4009
	{
Packit 6c4009
	  f_print (fout, "(");
Packit 6c4009
	  if (streq (proc->args.decls->decl.type, "void"))/* cast to void* */
Packit 6c4009
	    f_print (fout, "(void*)");
Packit 6c4009
	  f_print (fout, "&";;
Packit 6c4009
	  pvname (proc->proc_name, vp->vers_num);
Packit 6c4009
	  if (mtflag)
Packit 6c4009
	    f_print(fout, "_arg, &result_%d, clnt);\n", i);
Packit 6c4009
	  else
Packit 6c4009
	    f_print (fout, "_arg, clnt);\n");
Packit 6c4009
	}
Packit 6c4009
      else if (streq (proc->args.decls->decl.type, "void"))
Packit 6c4009
	{
Packit 6c4009
	  if (mtflag)
Packit 6c4009
	    f_print (fout, "(&result_%d, clnt);\n", i);
Packit 6c4009
	  else
Packit 6c4009
	    f_print (fout, "(clnt);\n");
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  f_print (fout, "(");
Packit 6c4009
	  for (l = proc->args.decls; l != NULL; l = l->next)
Packit 6c4009
	    {
Packit 6c4009
	      pvname (proc->proc_name, vp->vers_num);
Packit 6c4009
	      f_print (fout, "_%s, ", l->decl.name);
Packit 6c4009
	    }
Packit 6c4009
	  if (mtflag)
Packit 6c4009
	    f_print(fout, "&result_%d, ", i);
Packit 6c4009
	  f_print (fout, "clnt);\n");
Packit 6c4009
	}
Packit 6c4009
      if (mtflag)
Packit 6c4009
	{
Packit 6c4009
	  f_print(fout, "\tif (retval_%d != RPC_SUCCESS) {\n", i);
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  f_print(fout, "\tif (result_%d == (", i);
Packit 6c4009
	  ptype(proc->res_prefix, proc->res_type, 1);
Packit 6c4009
	  f_print(fout, "*) NULL) {\n");
Packit 6c4009
	}
Packit 6c4009
      f_print(fout, "\t\tclnt_perror (clnt, \"call failed\");\n");
Packit 6c4009
      f_print(fout, "\t}\n");
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  f_print (fout, "#ifndef\tDEBUG\n");
Packit 6c4009
  f_print (fout, "\tclnt_destroy (clnt);\n");
Packit 6c4009
  f_print (fout, "#endif\t /* DEBUG */\n");
Packit 6c4009
  f_print (fout, "}\n");
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
write_sample_server (definition * def)
Packit 6c4009
{
Packit 6c4009
  version_list *vp;
Packit 6c4009
  proc_list *proc;
Packit 6c4009
Packit 6c4009
  for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
Packit 6c4009
    {
Packit 6c4009
      for (proc = vp->procs; proc != NULL; proc = proc->next)
Packit 6c4009
	{
Packit 6c4009
	  f_print (fout, "\n");
Packit 6c4009
	  if (!mtflag)
Packit 6c4009
	    {
Packit 6c4009
	      return_type (proc);
Packit 6c4009
	      f_print (fout, "*\n");
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    f_print (fout, "bool_t\n");
Packit 6c4009
	  if (Cflag || mtflag)
Packit 6c4009
	    pvname_svc (proc->proc_name, vp->vers_num);
Packit 6c4009
	  else
Packit 6c4009
	    pvname(proc->proc_name, vp->vers_num);
Packit 6c4009
	  printarglist(proc, "result", RQSTP, "struct svc_req *");
Packit 6c4009
	  f_print(fout, "{\n");
Packit 6c4009
	  if (!mtflag)
Packit 6c4009
	    {
Packit 6c4009
	      f_print(fout, "\tstatic ");
Packit 6c4009
	      if(!streq(proc->res_type, "void"))
Packit 6c4009
		return_type(proc);
Packit 6c4009
	      else
Packit 6c4009
		f_print(fout, "char *");
Packit 6c4009
				/* cannot have void type */
Packit 6c4009
	      /* f_print(fout, " result;\n", proc->res_type); */
Packit 6c4009
	      f_print(fout, " result;\n");
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    f_print(fout, "\tbool_t retval;\n");
Packit 6c4009
	  fprintf (fout, "\n\t/*\n\t * insert server code here\n\t */\n\n");
Packit 6c4009
Packit 6c4009
	  if (!mtflag)
Packit 6c4009
	    {
Packit 6c4009
	      if (!streq(proc->res_type, "void"))
Packit 6c4009
		f_print(fout, "\treturn &result;\n}\n");
Packit 6c4009
	      else /* cast back to void * */
Packit 6c4009
		f_print(fout, "\treturn (void *) &result;\n}\n");
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    f_print(fout, "\treturn retval;\n}\n");
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      /* put in sample freeing routine */
Packit 6c4009
      if (mtflag)
Packit 6c4009
	{
Packit 6c4009
	  f_print(fout, "\nint\n");
Packit 6c4009
	  pvname(def->def_name, vp->vers_num);
Packit 6c4009
	  if (Cflag)
Packit 6c4009
	    f_print(fout,"_freeresult (SVCXPRT *transp, xdrproc_t xdr_result, caddr_t result)\n");
Packit 6c4009
	  else
Packit 6c4009
	    {
Packit 6c4009
	      f_print(fout,"_freeresult (transp, xdr_result, result)\n");
Packit 6c4009
	      f_print(fout,"\tSVCXPRT *transp;\n");
Packit 6c4009
	      f_print(fout,"\txdrproc_t xdr_result;\n");
Packit 6c4009
	      f_print(fout,"\tcaddr_t result;\n");
Packit 6c4009
	    }
Packit 6c4009
	  f_print(fout, "{\n");
Packit 6c4009
	  f_print(fout, "\txdr_free (xdr_result, result);\n");
Packit 6c4009
	  f_print(fout,
Packit 6c4009
		  "\n\t/*\n\t * Insert additional freeing code here, if needed\n\t */\n");
Packit 6c4009
	  f_print(fout, "\n\treturn 1;\n}\n");
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
return_type (proc_list * plist)
Packit 6c4009
{
Packit 6c4009
  ptype (plist->res_prefix, plist->res_type, 1);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
add_sample_msg (void)
Packit 6c4009
{
Packit 6c4009
  f_print (fout, "/*\n");
Packit 6c4009
  f_print (fout, " * This is sample code generated by rpcgen.\n");
Packit 6c4009
  f_print (fout, " * These are only templates and you can use them\n");
Packit 6c4009
  f_print (fout, " * as a guideline for developing your own functions.\n");
Packit 6c4009
  f_print (fout, " */\n\n");
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
write_sample_clnt_main (void)
Packit 6c4009
{
Packit 6c4009
  list *l;
Packit 6c4009
  definition *def;
Packit 6c4009
  version_list *vp;
Packit 6c4009
Packit 6c4009
  f_print (fout, "\n\n");
Packit 6c4009
  if (Cflag)
Packit 6c4009
    f_print (fout, "int\nmain (int argc, char *argv[])\n{\n");
Packit 6c4009
  else
Packit 6c4009
    f_print (fout, "int\nmain (argc, argv)\nint argc;\nchar *argv[];\n{\n");
Packit 6c4009
Packit 6c4009
  f_print (fout, "\tchar *host;");
Packit 6c4009
  f_print (fout, "\n\n\tif (argc < 2) {");
Packit 6c4009
  f_print (fout, "\n\t\tprintf (\"usage: %%s server_host\\n\", argv[0]);\n");
Packit 6c4009
  f_print (fout, "\t\texit (1);\n\t}");
Packit 6c4009
  f_print (fout, "\n\thost = argv[1];\n");
Packit 6c4009
Packit 6c4009
  for (l = defined; l != NULL; l = l->next)
Packit 6c4009
    {
Packit 6c4009
      def = l->val;
Packit 6c4009
      if (def->def_kind != DEF_PROGRAM)
Packit 6c4009
	{
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
      for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
Packit 6c4009
	{
Packit 6c4009
	  f_print (fout, "\t");
Packit 6c4009
	  pvname (def->def_name, vp->vers_num);
Packit 6c4009
	  f_print (fout, " (host);\n");
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  f_print (fout, "exit (0);\n}\n");
Packit 6c4009
}