Back to index

glibc  2.9
rpc_clntout.c
Go to the documentation of this file.
00001 /*
00002  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
00003  * unrestricted use provided that this legend is included on all tape
00004  * media and as a part of the software program in whole or part.  Users
00005  * may copy or modify Sun RPC without charge, but are not authorized
00006  * to license or distribute it to anyone else except as part of a product or
00007  * program developed by the user or with the express written consent of
00008  * Sun Microsystems, Inc.
00009  *
00010  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
00011  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
00012  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
00013  *
00014  * Sun RPC is provided with no support and without any obligation on the
00015  * part of Sun Microsystems, Inc. to assist in its use, correction,
00016  * modification or enhancement.
00017  *
00018  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
00019  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
00020  * OR ANY PART THEREOF.
00021  *
00022  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
00023  * or profits or other special, indirect and consequential damages, even if
00024  * Sun has been advised of the possibility of such damages.
00025  *
00026  * Sun Microsystems, Inc.
00027  * 2550 Garcia Avenue
00028  * Mountain View, California  94043
00029  */
00030 
00031 /*
00032  * From: @(#)rpc_clntout.c 1.11 89/02/22 (C) 1987 SMI
00033  */
00034 
00035 /*
00036  * rpc_clntout.c, Client-stub outputter for the RPC protocol compiler
00037  * Copyright (C) 1987, Sun Microsystems, Inc.
00038  */
00039 #include <stdio.h>
00040 #include <string.h>
00041 #include <rpc/types.h>
00042 #include "rpc_parse.h"
00043 #include "rpc_util.h"
00044 #include "proto.h"
00045 
00046 #define DEFAULT_TIMEOUT 25  /* in seconds */
00047 static const char RESULT[] = "clnt_res";
00048 
00049 static void write_program (definition * def);
00050 static void printbody (proc_list * proc);
00051 static const char *ampr (const char *type);
00052 static void printbody (proc_list * proc);
00053 
00054 
00055 void
00056 write_stubs (void)
00057 {
00058   list *l;
00059   definition *def;
00060 
00061   fprintf (fout,
00062           "\n/* Default timeout can be changed using clnt_control() */\n");
00063   fprintf (fout, "static struct timeval TIMEOUT = { %d, 0 };\n",
00064           DEFAULT_TIMEOUT);
00065   for (l = defined; l != NULL; l = l->next)
00066     {
00067       def = (definition *) l->val;
00068       if (def->def_kind == DEF_PROGRAM)
00069        {
00070          write_program (def);
00071        }
00072     }
00073 }
00074 
00075 static void
00076 write_program (definition * def)
00077 {
00078   version_list *vp;
00079   proc_list *proc;
00080 
00081   for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
00082     {
00083       for (proc = vp->procs; proc != NULL; proc = proc->next)
00084        {
00085          fprintf (fout, "\n");
00086          if (mtflag == 0)
00087            {
00088              ptype (proc->res_prefix, proc->res_type, 1);
00089              fprintf (fout, "*\n");
00090              pvname (proc->proc_name, vp->vers_num);
00091              printarglist (proc, RESULT, "clnt", "CLIENT *");
00092            }
00093          else
00094            {
00095              fprintf (fout, "enum clnt_stat \n");
00096              pvname (proc->proc_name, vp->vers_num);
00097              printarglist (proc, RESULT, "clnt", "CLIENT *");
00098            }
00099          fprintf (fout, "{\n");
00100          printbody (proc);
00101          fprintf (fout, "}\n");
00102        }
00103     }
00104 }
00105 
00106 /* Writes out declarations of procedure's argument list.
00107    In either ANSI C style, in one of old rpcgen style (pass by reference),
00108    or new rpcgen style (multiple arguments, pass by value);
00109  */
00110 
00111 /* sample addargname = "clnt"; sample addargtype = "CLIENT * " */
00112 
00113 void
00114 printarglist (proc_list * proc,  const char *result,
00115              const char *addargname, const char *addargtype)
00116 {
00117 
00118   decl_list *l;
00119 
00120   if (!newstyle)
00121     { /* old style: always pass argument by reference */
00122       if (Cflag)
00123        {                    /* C++ style heading */
00124          fprintf (fout, "(");
00125          ptype (proc->args.decls->decl.prefix,
00126                proc->args.decls->decl.type, 1);
00127 
00128          if (mtflag)
00129            {/* Generate result field */
00130              fprintf (fout, "*argp, ");
00131              ptype(proc->res_prefix, proc->res_type, 1);
00132              fprintf (fout, "*%s, %s%s)\n", result, addargtype, addargname);
00133            }
00134          else
00135            fprintf (fout, "*argp, %s%s)\n", addargtype, addargname);
00136        }
00137       else
00138        {
00139          if (!mtflag)
00140            fprintf (fout, "(argp, %s)\n", addargname);
00141          else
00142            fprintf (fout, "(argp, %s, %s)\n", result, addargname);
00143          fprintf (fout, "\t");
00144          ptype (proc->args.decls->decl.prefix,
00145                proc->args.decls->decl.type, 1);
00146          fprintf (fout, "*argp;\n");
00147          if (mtflag)
00148            {
00149              fprintf (fout, "\t");
00150              ptype (proc->res_prefix, proc->res_type, 1);
00151              fprintf (fout, "*%s;\n", result);
00152            }
00153        }
00154     }
00155   else if (streq (proc->args.decls->decl.type, "void"))
00156     {
00157       /* newstyle, 0 argument */
00158       if (mtflag)
00159        {
00160          fprintf (fout, "(");
00161          if (Cflag)
00162            {
00163              ptype(proc->res_prefix, proc->res_type, 1);
00164              fprintf (fout, "*%s, %s%s)\n", result, addargtype, addargname);
00165            }
00166          else
00167            fprintf (fout, "(%s)\n", addargname);
00168        }
00169       else if (Cflag)
00170        fprintf (fout, "(%s%s)\n", addargtype, addargname);
00171       else
00172        fprintf (fout, "(%s)\n", addargname);
00173     }
00174   else
00175     {
00176       /* new style, 1 or multiple arguments */
00177       if (!Cflag)
00178        {
00179          fprintf (fout, "(");
00180          for (l = proc->args.decls; l != NULL; l = l->next)
00181            fprintf (fout, "%s, ", l->decl.name);
00182          if (mtflag)
00183            fprintf (fout, "%s, ", result);
00184          fprintf (fout, "%s)\n", addargname);
00185          for (l = proc->args.decls; l != NULL; l = l->next)
00186            {
00187              pdeclaration (proc->args.argname, &l->decl, 1, ";\n");
00188            }
00189          if (mtflag)
00190            {
00191              fprintf (fout, "\t");
00192              ptype (proc->res_prefix, proc->res_type, 1);
00193              fprintf (fout, "*%s;\n", result);
00194            }
00195        }
00196       else
00197        {                    /* C++ style header */
00198          fprintf (fout, "(");
00199          for (l = proc->args.decls; l != NULL; l = l->next)
00200            {
00201              pdeclaration (proc->args.argname, &l->decl, 0, ", ");
00202            }
00203          if (mtflag)
00204            {
00205              ptype (proc->res_prefix, proc->res_type, 1);
00206              fprintf (fout, "*%s, ", result);
00207            }
00208          fprintf (fout, " %s%s)\n", addargtype, addargname);
00209        }
00210     }
00211 
00212   if (!Cflag)
00213     fprintf (fout, "\t%s%s;\n", addargtype, addargname);
00214 }
00215 
00216 
00217 static
00218 const char *
00219 ampr (const char *type)
00220 {
00221   if (isvectordef (type, REL_ALIAS))
00222     {
00223       return "";
00224     }
00225   else
00226     {
00227       return "&";
00228     }
00229 }
00230 
00231 static void
00232 printbody (proc_list * proc)
00233 {
00234   decl_list *l;
00235   bool_t args2 = (proc->arg_num > 1);
00236 /*  int i; */
00237 
00238   /* For new style with multiple arguments, need a structure in which
00239      to stuff the arguments. */
00240   if (newstyle && args2)
00241     {
00242       fprintf (fout, "\t%s", proc->args.argname);
00243       fprintf (fout, " arg;\n");
00244     }
00245   if (!mtflag)
00246     {
00247       fprintf (fout, "\tstatic ");
00248       if (streq (proc->res_type, "void"))
00249        {
00250          fprintf (fout, "char ");
00251        }
00252       else
00253        {
00254          ptype (proc->res_prefix, proc->res_type, 0);
00255        }
00256       fprintf (fout, "%s;\n", RESULT);
00257       fprintf (fout, "\n");
00258       fprintf (fout, "\tmemset((char *)%s%s, 0, sizeof(%s));\n",
00259               ampr (proc->res_type), RESULT, RESULT);
00260     }
00261   if (newstyle && !args2 && (streq (proc->args.decls->decl.type, "void")))
00262     {
00263       /* newstyle, 0 arguments */
00264       if (mtflag)
00265        fprintf (fout, "\t return ");
00266       else
00267        fprintf (fout, "\t if ");
00268       fprintf (fout,
00269               "(clnt_call (clnt, %s, (xdrproc_t) xdr_void, ", proc->proc_name);
00270 
00271       fprintf (fout,
00272               "(caddr_t) NULL,\n\t\t(xdrproc_t) xdr_%s, (caddr_t) %s%s,",
00273               stringfix(proc->res_type), (mtflag)?"":ampr(proc->res_type),
00274               RESULT);
00275       if (mtflag)
00276        fprintf (fout, "\n\t\tTIMEOUT));\n\n");
00277       else
00278        fprintf (fout, "\n\t\tTIMEOUT) != RPC_SUCCESS) {\n");
00279     }
00280   else if (newstyle && args2)
00281     {
00282       /* newstyle, multiple arguments:  stuff arguments into structure */
00283       for (l = proc->args.decls; l != NULL; l = l->next)
00284        {
00285          fprintf (fout, "\targ.%s = %s;\n",
00286                  l->decl.name, l->decl.name);
00287        }
00288       if (mtflag)
00289        fprintf (fout, "\treturn ");
00290       else
00291        fprintf (fout, "\tif ");
00292 
00293       fprintf (fout,
00294               "(clnt_call (clnt, %s, (xdrproc_t) xdr_%s", proc->proc_name,
00295               proc->args.argname);
00296       fprintf (fout,
00297               ", (caddr_t) &arg,\n\t\t(xdrproc_t) xdr_%s, (caddr_t) %s%s,",
00298               stringfix(proc->res_type), (mtflag)?"":ampr(proc->res_type),
00299               RESULT);
00300       if (mtflag)
00301        fprintf (fout, "\n\t\tTIMEOUT));\n");
00302       else
00303        fprintf (fout, "\n\t\tTIMEOUT) != RPC_SUCCESS) {\n");
00304     }
00305   else
00306     {                       /* single argument, new or old style */
00307       if (!mtflag)
00308        fprintf (fout,
00309                "\tif (clnt_call (clnt, %s,\n\t\t(xdrproc_t) xdr_%s, (caddr_t) %s%s,\n\t\t(xdrproc_t) xdr_%s, (caddr_t) %s%s,\n\t\tTIMEOUT) != RPC_SUCCESS) {\n",
00310                proc->proc_name,
00311                stringfix (proc->args.decls->decl.type),
00312                (newstyle ? "&" : ""),
00313                (newstyle ? proc->args.decls->decl.name : "argp"),
00314                stringfix (proc->res_type), ampr (proc->res_type),
00315                RESULT);
00316       else
00317        fprintf(fout,
00318               "\treturn (clnt_call(clnt, %s,\n\t\t(xdrproc_t) xdr_%s, (caddr_t) %s%s,\n\t\t(xdrproc_t) xdr_%s, (caddr_t) %s%s,\n\t\tTIMEOUT));\n",
00319               proc->proc_name,
00320               stringfix (proc->args.decls->decl.type),
00321               (newstyle ? "&" : ""),
00322               (newstyle ? proc->args.decls->decl.name : "argp"),
00323               stringfix (proc->res_type), "",
00324               RESULT);
00325     }
00326   if (!mtflag)
00327     {
00328       fprintf (fout, "\t\treturn (NULL);\n");
00329       fprintf (fout, "\t}\n");
00330       if (streq (proc->res_type, "void"))
00331        {
00332          fprintf (fout, "\treturn ((void *)%s%s);\n",
00333                  ampr (proc->res_type), RESULT);
00334        }
00335       else
00336        {
00337          fprintf (fout, "\treturn (%s%s);\n", ampr (proc->res_type), RESULT);
00338        }
00339     }
00340 }