Print this page
rpcgen should only produce ANSI code


   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*


  23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
  27 /* All Rights Reserved */
  28 /*
  29  * University Copyright- Copyright (c) 1982, 1986, 1988
  30  * The Regents of the University of California
  31  * All Rights Reserved
  32  *
  33  * University Acknowledgment- Portions of this document are derived from
  34  * software developed by the University of California, Berkeley, and its
  35  * contributors.
  36  */
  37 
  38 /*
  39  * rpc_sample.c, Sample client-server code outputter
  40  * for the RPC protocol compiler
  41  */
  42 


  70 
  71         if (def->def_kind != DEF_PROGRAM)
  72                 return (0);
  73         /* generate sample code for each version */
  74         for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
  75                 write_sample_client(def->def_name, vp);
  76                 ++count;
  77         }
  78         return (count);
  79 }
  80 
  81 static void
  82 write_sample_client(char *program_name, version_list *vp)
  83 {
  84         proc_list *proc;
  85         int i;
  86         decl_list *l;
  87 
  88         f_print(fout, "\n\nvoid\n");
  89         pvname(program_name, vp->vers_num);
  90         if (Cflag)
  91                 f_print(fout, "(char *host)\n{\n");
  92         else
  93                 f_print(fout, "(host)\n\tchar *host;\n{\n");
  94         f_print(fout, "\tCLIENT *clnt;\n");
  95 
  96         i = 0;
  97         for (proc = vp->procs; proc != NULL; proc = proc->next) {
  98                 f_print(fout, "\t");
  99                 if (mtflag) {
 100                         f_print(fout, "enum clnt_stat retval_%d;\n", ++i);
 101                         if (!streq(proc->res_type, "oneway")) {
 102                                 f_print(fout, "\t");
 103                                 if (!streq(proc->res_type, "void"))
 104                                         ptype(proc->res_prefix,
 105                                             proc->res_type, 1);
 106                                 else
 107                                         f_print(fout, "void *");
 108                                 f_print(fout, "result_%d;\n", i);
 109                         }
 110                 } else {
 111                         ptype(proc->res_prefix, proc->res_type, 1);
 112                         f_print(fout, " *result_%d;\n", ++i);
 113                 }


 205         f_print(fout, "\tclnt_destroy(clnt);\n");
 206         f_print(fout, "#endif\t /* DEBUG */\n");
 207         f_print(fout, "}\n");
 208 }
 209 
 210 static void
 211 write_sample_server(definition *def)
 212 {
 213         version_list *vp;
 214         proc_list *proc;
 215 
 216         for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
 217                 for (proc = vp->procs; proc != NULL; proc = proc->next) {
 218                         f_print(fout, "\n");
 219                         if (!mtflag) {
 220                                 return_type(proc);
 221                                 f_print(fout, "*\n");
 222                         } else {
 223                                 f_print(fout, "bool_t\n");
 224                         }
 225                         if (Cflag || mtflag)
 226                                 pvname_svc(proc->proc_name, vp->vers_num);
 227                         else
 228                                 pvname(proc->proc_name, vp->vers_num);
 229                         printarglist(proc, "result", RQSTP, "struct svc_req *");
 230 
 231                         f_print(fout, "{\n");
 232 
 233                         if (!mtflag) {
 234                                 f_print(fout, "\tstatic ");
 235                                 if ((!streq(proc->res_type, "void")) &&
 236                                     (!streq(proc->res_type, "oneway")))
 237                                         return_type(proc);
 238                                 else
 239                                         f_print(fout, "char *");
 240                                 /* cannot have void type */
 241                                 f_print(fout, " result;\n");
 242                         }
 243 
 244                         f_print(fout, "\n\t/*\n\t * insert server code "
 245                             "here\n\t */\n\n");
 246 
 247                         if (!mtflag)
 248                                 if (!streq(proc->res_type, "void"))
 249                                         f_print(fout,
 250                                             "\treturn (&result);\n}\n");
 251                                 else /* cast back to void * */
 252                                         f_print(fout, "\treturn((void *) "
 253                                             "&result);\n}\n");
 254                         else
 255                                 f_print(fout, "\treturn (retval);\n}\n");
 256                 }
 257                 /* put in sample freeing routine */
 258                 if (mtflag) {
 259                         f_print(fout, "\nint\n");
 260                         pvname(def->def_name, vp->vers_num);
 261                         if (Cflag)
 262                                 f_print(fout, "_freeresult(SVCXPRT *transp,"
 263                                     " xdrproc_t xdr_result,"
 264                                     " caddr_t result)\n");
 265                         else {
 266                                 f_print(fout, "_freeresult(transp, xdr_result,"
 267                                     " result)\n");
 268                                 f_print(fout, "\tSVCXPRT *transp;\n");
 269                                 f_print(fout, "\txdrproc_t xdr_result;\n");
 270                                 f_print(fout, "\tcaddr_t result;\n");
 271                         }
 272                         f_print(fout, "{\n"
 273                             "\t(void) xdr_free(xdr_result, result);\n"
 274                             "\n\t/*\n\t * Insert additional freeing"
 275                             " code here, if needed\n\t */\n"
 276                             "\n\n\treturn (TRUE);\n}\n");
 277                 }
 278         }
 279 }
 280 
 281 static void
 282 return_type(proc_list *plist)
 283 {
 284         ptype(plist->res_prefix, plist->res_type, 1);
 285 }
 286 
 287 void
 288 add_sample_msg(void)
 289 {
 290         f_print(fout, "/*\n");
 291         f_print(fout, " * This is sample code generated by rpcgen.\n");
 292         f_print(fout, " * These are only templates and you can use them\n");
 293         f_print(fout, " * as a guideline for developing your own functions.\n");
 294         f_print(fout, " */\n\n");
 295 }
 296 
 297 void
 298 write_sample_clnt_main(void)
 299 {
 300         list *l;
 301         definition *def;
 302         version_list *vp;
 303 
 304         f_print(fout, "\n\n");
 305         if (Cflag)
 306                 f_print(fout, "int\nmain(int argc, char *argv[])\n{\n");
 307         else
 308                 f_print(fout, "int\nmain(argc, argv)\n\tint argc;\n"
 309                     "\tchar *argv[];\n{\n");
 310 
 311         f_print(fout, "\tchar *host;");
 312         f_print(fout, "\n\n\tif (argc < 2) {");
 313         f_print(fout, "\n\t\tprintf(\"usage:  %%s server_host\\n\","
 314             " argv[0]);\n");
 315         f_print(fout, "\t\texit(1);\n\t}");
 316         f_print(fout, "\n\thost = argv[1];\n");
 317 
 318         for (l = defined; l != NULL; l = l->next) {
 319                 def = l->val;
 320                 if (def->def_kind != DEF_PROGRAM)
 321                         continue;
 322                 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
 323                         f_print(fout, "\t");
 324                         pvname(def->def_name, vp->vers_num);
 325                         f_print(fout, "(host);\n");
 326                 }
 327         }
 328         f_print(fout, "}\n");
 329 }


   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright 2014 Garrett D'Amore <garrett@damore.org>
  24  *
  25  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  26  * Use is subject to license terms.
  27  */
  28 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
  29 /* All Rights Reserved */
  30 /*
  31  * University Copyright- Copyright (c) 1982, 1986, 1988
  32  * The Regents of the University of California
  33  * All Rights Reserved
  34  *
  35  * University Acknowledgment- Portions of this document are derived from
  36  * software developed by the University of California, Berkeley, and its
  37  * contributors.
  38  */
  39 
  40 /*
  41  * rpc_sample.c, Sample client-server code outputter
  42  * for the RPC protocol compiler
  43  */
  44 


  72 
  73         if (def->def_kind != DEF_PROGRAM)
  74                 return (0);
  75         /* generate sample code for each version */
  76         for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
  77                 write_sample_client(def->def_name, vp);
  78                 ++count;
  79         }
  80         return (count);
  81 }
  82 
  83 static void
  84 write_sample_client(char *program_name, version_list *vp)
  85 {
  86         proc_list *proc;
  87         int i;
  88         decl_list *l;
  89 
  90         f_print(fout, "\n\nvoid\n");
  91         pvname(program_name, vp->vers_num);

  92         f_print(fout, "(char *host)\n{\n");


  93         f_print(fout, "\tCLIENT *clnt;\n");
  94 
  95         i = 0;
  96         for (proc = vp->procs; proc != NULL; proc = proc->next) {
  97                 f_print(fout, "\t");
  98                 if (mtflag) {
  99                         f_print(fout, "enum clnt_stat retval_%d;\n", ++i);
 100                         if (!streq(proc->res_type, "oneway")) {
 101                                 f_print(fout, "\t");
 102                                 if (!streq(proc->res_type, "void"))
 103                                         ptype(proc->res_prefix,
 104                                             proc->res_type, 1);
 105                                 else
 106                                         f_print(fout, "void *");
 107                                 f_print(fout, "result_%d;\n", i);
 108                         }
 109                 } else {
 110                         ptype(proc->res_prefix, proc->res_type, 1);
 111                         f_print(fout, " *result_%d;\n", ++i);
 112                 }


 204         f_print(fout, "\tclnt_destroy(clnt);\n");
 205         f_print(fout, "#endif\t /* DEBUG */\n");
 206         f_print(fout, "}\n");
 207 }
 208 
 209 static void
 210 write_sample_server(definition *def)
 211 {
 212         version_list *vp;
 213         proc_list *proc;
 214 
 215         for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
 216                 for (proc = vp->procs; proc != NULL; proc = proc->next) {
 217                         f_print(fout, "\n");
 218                         if (!mtflag) {
 219                                 return_type(proc);
 220                                 f_print(fout, "*\n");
 221                         } else {
 222                                 f_print(fout, "bool_t\n");
 223                         }

 224                         pvname_svc(proc->proc_name, vp->vers_num);


 225                         printarglist(proc, "result", RQSTP, "struct svc_req *");
 226 
 227                         f_print(fout, "{\n");
 228 
 229                         if (!mtflag) {
 230                                 f_print(fout, "\tstatic ");
 231                                 if ((!streq(proc->res_type, "void")) &&
 232                                     (!streq(proc->res_type, "oneway")))
 233                                         return_type(proc);
 234                                 else
 235                                         f_print(fout, "char *");
 236                                 /* cannot have void type */
 237                                 f_print(fout, " result;\n");
 238                         }
 239 
 240                         f_print(fout, "\n\t/*\n\t * insert server code "
 241                             "here\n\t */\n\n");
 242 
 243                         if (!mtflag)
 244                                 if (!streq(proc->res_type, "void"))
 245                                         f_print(fout,
 246                                             "\treturn (&result);\n}\n");
 247                                 else /* cast back to void * */
 248                                         f_print(fout, "\treturn((void *) "
 249                                             "&result);\n}\n");
 250                         else
 251                                 f_print(fout, "\treturn (retval);\n}\n");
 252                 }
 253                 /* put in sample freeing routine */
 254                 if (mtflag) {
 255                         f_print(fout, "\nint\n");
 256                         pvname(def->def_name, vp->vers_num);

 257                         f_print(fout, "_freeresult(SVCXPRT *transp,"
 258                             " xdrproc_t xdr_result,"
 259                             " caddr_t result)\n");







 260                         f_print(fout, "{\n"
 261                             "\t(void) xdr_free(xdr_result, result);\n"
 262                             "\n\t/*\n\t * Insert additional freeing"
 263                             " code here, if needed\n\t */\n"
 264                             "\n\n\treturn (TRUE);\n}\n");
 265                 }
 266         }
 267 }
 268 
 269 static void
 270 return_type(proc_list *plist)
 271 {
 272         ptype(plist->res_prefix, plist->res_type, 1);
 273 }
 274 
 275 void
 276 add_sample_msg(void)
 277 {
 278         f_print(fout, "/*\n");
 279         f_print(fout, " * This is sample code generated by rpcgen.\n");
 280         f_print(fout, " * These are only templates and you can use them\n");
 281         f_print(fout, " * as a guideline for developing your own functions.\n");
 282         f_print(fout, " */\n\n");
 283 }
 284 
 285 void
 286 write_sample_clnt_main(void)
 287 {
 288         list *l;
 289         definition *def;
 290         version_list *vp;
 291 
 292         f_print(fout, "\n\n");

 293         f_print(fout, "int\nmain(int argc, char *argv[])\n{\n");



 294 
 295         f_print(fout, "\tchar *host;");
 296         f_print(fout, "\n\n\tif (argc < 2) {");
 297         f_print(fout, "\n\t\tprintf(\"usage:  %%s server_host\\n\","
 298             " argv[0]);\n");
 299         f_print(fout, "\t\texit(1);\n\t}");
 300         f_print(fout, "\n\thost = argv[1];\n");
 301 
 302         for (l = defined; l != NULL; l = l->next) {
 303                 def = l->val;
 304                 if (def->def_kind != DEF_PROGRAM)
 305                         continue;
 306                 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
 307                         f_print(fout, "\t");
 308                         pvname(def->def_name, vp->vers_num);
 309                         f_print(fout, "(host);\n");
 310                 }
 311         }
 312         f_print(fout, "}\n");
 313 }