rpc_sample.c revision 149709
112795Swpaul/* 212795Swpaul * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 312795Swpaul * unrestricted use provided that this legend is included on all tape 412795Swpaul * media and as a part of the software program in whole or part. Users 512795Swpaul * may copy or modify Sun RPC without charge, but are not authorized 612795Swpaul * to license or distribute it to anyone else except as part of a product or 712795Swpaul * program developed by the user. 8100441Scharnier * 912795Swpaul * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 1012795Swpaul * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 1112795Swpaul * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 12100441Scharnier * 1312795Swpaul * Sun RPC is provided with no support and without any obligation on the 1412795Swpaul * part of Sun Microsystems, Inc. to assist in its use, correction, 1512795Swpaul * modification or enhancement. 16100441Scharnier * 1712795Swpaul * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 1812795Swpaul * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 1912795Swpaul * OR ANY PART THEREOF. 20100441Scharnier * 2112795Swpaul * In no event will Sun Microsystems, Inc. be liable for any lost revenue 2212795Swpaul * or profits or other special, indirect and consequential damages, even if 2312795Swpaul * Sun has been advised of the possibility of such damages. 24100441Scharnier * 2512795Swpaul * Sun Microsystems, Inc. 2612795Swpaul * 2550 Garcia Avenue 2712795Swpaul * Mountain View, California 94043 2812795Swpaul */ 2912795Swpaul 3099979Salfred/* #pragma ident "@(#)rpc_sample.c 1.9 94/04/25 SMI" */ 3112795Swpaul 32100441Scharnier#include <sys/cdefs.h> 33100441Scharnier__FBSDID("$FreeBSD: head/usr.bin/rpcgen/rpc_sample.c 149709 2005-09-02 10:23:26Z stefanf $"); 34100441Scharnier 3512795Swpaul/* 3612795Swpaul * rpc_sample.c, Sample client-server code outputter for the RPC protocol compiler 3712795Swpaul * Copyright (C) 1987, Sun Microsystems, Inc. 3812795Swpaul */ 3912795Swpaul 4012795Swpaul#include <stdio.h> 4112795Swpaul#include <string.h> 4212795Swpaul#include "rpc_parse.h" 43149682Sstefanf#include "rpc_scan.h" 4412795Swpaul#include "rpc_util.h" 4512795Swpaul 4612795Swpaul 4712795Swpaulstatic char RQSTP[] = "rqstp"; 4812795Swpaul 4992921Simpextern void printarglist( proc_list *, char *, char *, char *); 5092921Simpstatic void write_sample_client( char *, version_list * ); 5192921Simpstatic void write_sample_server( definition * ); 5292921Simpstatic void return_type( proc_list * ); 5312795Swpaul 5412795Swpaulvoid 5512795Swpaulwrite_sample_svc(def) 5612795Swpaul definition *def; 5712795Swpaul{ 5812795Swpaul 59100441Scharnier if (def->def_kind != DEF_PROGRAM) 6012795Swpaul return; 6112795Swpaul write_sample_server(def); 6212795Swpaul} 6312795Swpaul 6412795Swpaul 6512795Swpaulint 6612795Swpaulwrite_sample_clnt(def) 6712795Swpaul definition *def; 6812795Swpaul{ 6912795Swpaul version_list *vp; 7012795Swpaul int count = 0; 7112795Swpaul 72100441Scharnier if (def->def_kind != DEF_PROGRAM) 7312795Swpaul return(0); 7412795Swpaul /* generate sample code for each version */ 7512795Swpaul for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) { 7612795Swpaul write_sample_client(def->def_name, vp); 7712795Swpaul ++count; 7812795Swpaul } 7912795Swpaul return(count); 8012795Swpaul} 8112795Swpaul 8212795Swpaul 8317142Sjkhstatic void 8412795Swpaulwrite_sample_client(program_name, vp) 8512795Swpaul char *program_name; 8612795Swpaul version_list *vp; 8712795Swpaul{ 8812795Swpaul proc_list *proc; 8912795Swpaul int i; 9012795Swpaul decl_list *l; 9112795Swpaul 9212795Swpaul f_print(fout, "\n\nvoid\n"); 9312795Swpaul pvname(program_name, vp->vers_num); 94149709Sstefanf f_print(fout, "(char *host)\n{\n"); 9512795Swpaul f_print(fout, "\tCLIENT *clnt;\n"); 9612795Swpaul 9712795Swpaul i = 0; 9812795Swpaul for (proc = vp->procs; proc != NULL; proc = proc->next) { 9912795Swpaul f_print(fout, "\t"); 10012795Swpaul if (mtflag) { 10112795Swpaul f_print(fout, "enum clnt_stat retval_%d;\n\t", ++i); 10212795Swpaul ptype(proc->res_prefix, proc->res_type, 1); 10312795Swpaul f_print(fout, "result_%d;\n", i); 10412795Swpaul } else { 10512795Swpaul ptype(proc->res_prefix, proc->res_type, 1); 10612795Swpaul f_print(fout, " *result_%d;\n",++i); 10712795Swpaul } 10812795Swpaul /* print out declarations for arguments */ 10912795Swpaul if(proc->arg_num < 2 && !newstyle) { 11012795Swpaul f_print(fout, "\t"); 11112795Swpaul if(!streq(proc->args.decls->decl.type, "void")) 112100441Scharnier ptype(proc->args.decls->decl.prefix, 11312795Swpaul proc->args.decls->decl.type, 1); 11412795Swpaul else 11512795Swpaul f_print(fout, "char * "); /* cannot have "void" type */ 11612795Swpaul f_print(fout, " "); 11712795Swpaul pvname(proc->proc_name, vp->vers_num); 11812795Swpaul f_print(fout, "_arg;\n"); 11912795Swpaul } else if (!streq(proc->args.decls->decl.type, "void")) { 12012795Swpaul for (l = proc->args.decls; l != NULL; l = l->next) { 12112795Swpaul f_print(fout, "\t"); 12212795Swpaul ptype(l->decl.prefix, l->decl.type, 1); 12312795Swpaul if (strcmp(l->decl.type,"string") == 1) 12412795Swpaul f_print(fout, " "); 12512795Swpaul pvname(proc->proc_name, vp->vers_num); 12612795Swpaul f_print(fout, "_%s;\n", l->decl.name); 12712795Swpaul } 12812795Swpaul } 12912795Swpaul } 13012795Swpaul 13112795Swpaul /* generate creation of client handle */ 13212795Swpaul f_print(fout, "\n#ifndef\tDEBUG\n"); 133100441Scharnier f_print(fout, "\tclnt = clnt_create(host, %s, %s, \"%s\");\n", 13412795Swpaul program_name, vp->vers_name, tirpcflag? "netpath" : "udp"); 13512795Swpaul f_print(fout, "\tif (clnt == (CLIENT *) NULL) {\n"); 13612795Swpaul f_print(fout, "\t\tclnt_pcreateerror(host);\n"); 13712795Swpaul f_print(fout, "\t\texit(1);\n\t}\n"); 13812795Swpaul f_print(fout, "#endif\t/* DEBUG */\n\n"); 13912795Swpaul 14012795Swpaul /* generate calls to procedures */ 14112795Swpaul i = 0; 14212795Swpaul for (proc = vp->procs; proc != NULL; proc = proc->next) { 14312795Swpaul if (mtflag) 144100441Scharnier f_print(fout, "\tretval_%d = ",++i); 14512795Swpaul else 146100441Scharnier f_print(fout, "\tresult_%d = ",++i); 14712795Swpaul pvname(proc->proc_name, vp->vers_num); 14812795Swpaul if (proc->arg_num < 2 && !newstyle) { 14912795Swpaul f_print(fout, "("); 150100441Scharnier if(streq(proc->args.decls->decl.type, "void")) 15112795Swpaul /* cast to void * */ 15212795Swpaul f_print(fout, "(void *)"); 15312795Swpaul f_print(fout, "&"); 15412795Swpaul pvname(proc->proc_name, vp->vers_num); 15512795Swpaul if (mtflag) 15612795Swpaul f_print(fout, "_arg, &result_%d, clnt);\n", 15712795Swpaul i); 15812795Swpaul else 15912795Swpaul f_print(fout, "_arg, clnt);\n"); 16012795Swpaul 16112795Swpaul } else if (streq(proc->args.decls->decl.type, "void")) { 16212795Swpaul if (mtflag) 16312795Swpaul f_print(fout, "(&result_%d, clnt);\n", i); 16412795Swpaul else 16512795Swpaul f_print(fout, "(clnt);\n"); 16612795Swpaul } 16712795Swpaul else { 16812795Swpaul f_print(fout, "("); 16912795Swpaul for (l = proc->args.decls; l != NULL; l = l->next) { 17012795Swpaul pvname(proc->proc_name, vp->vers_num); 17112795Swpaul f_print(fout, "_%s, ", l->decl.name); 17212795Swpaul } 17312795Swpaul if (mtflag) 17412795Swpaul f_print(fout, "&result_%d, ", i); 17512795Swpaul 17612795Swpaul f_print(fout, "clnt);\n"); 17712795Swpaul } 17812795Swpaul if (mtflag) { 17912795Swpaul f_print(fout, "\tif (retval_%d != RPC_SUCCESS) {\n", i); 18012795Swpaul 18112795Swpaul } else { 18212795Swpaul f_print(fout, "\tif (result_%d == (", i); 18312795Swpaul ptype(proc->res_prefix, proc->res_type, 1); 18412795Swpaul f_print(fout, "*) NULL) {\n"); 18512795Swpaul } 18612795Swpaul f_print(fout, "\t\tclnt_perror(clnt, \"call failed\");\n"); 18712795Swpaul f_print(fout, "\t}\n"); 18812795Swpaul } 18912795Swpaul 19012795Swpaul f_print(fout, "#ifndef\tDEBUG\n"); 19112795Swpaul f_print(fout, "\tclnt_destroy(clnt);\n"); 19212795Swpaul f_print(fout, "#endif\t /* DEBUG */\n"); 19312795Swpaul f_print(fout, "}\n"); 19412795Swpaul} 19512795Swpaul 19617142Sjkhstatic void 19712795Swpaulwrite_sample_server(def) 19812795Swpaul definition *def; 19912795Swpaul{ 20012795Swpaul version_list *vp; 20112795Swpaul proc_list *proc; 20212795Swpaul 20312795Swpaul for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) { 20412795Swpaul for (proc = vp->procs; proc != NULL; proc = proc->next) { 20512795Swpaul f_print(fout, "\n"); 20612795Swpaul if (!mtflag) { 20712795Swpaul return_type(proc); 20812795Swpaul f_print(fout, "*\n"); 20912795Swpaul } else 21012795Swpaul f_print(fout, "bool_t\n"); 211149709Sstefanf pvname_svc(proc->proc_name, vp->vers_num); 21212795Swpaul printarglist(proc, "result", RQSTP, "struct svc_req *"); 21312795Swpaul 21412795Swpaul f_print(fout, "{\n"); 21512795Swpaul if (!mtflag) { 21612795Swpaul f_print(fout, "\tstatic "); 21712795Swpaul if(!streq(proc->res_type, "void")) 21812795Swpaul return_type(proc); 21912795Swpaul else 22012795Swpaul f_print(fout, "char *"); 22112795Swpaul /* cannot have void type */ 22238022Sbde f_print(fout, " result;\n"); 22312795Swpaul } 22412795Swpaul else 22512795Swpaul f_print(fout, "\tbool_t retval;\n"); 226100441Scharnier f_print(fout, 22712795Swpaul "\n\t/*\n\t * insert server code here\n\t */\n\n"); 22812795Swpaul 22912795Swpaul if (!mtflag) 23012795Swpaul if(!streq(proc->res_type, "void")) 23112795Swpaul f_print(fout, "\treturn (&result);\n}\n"); 23212795Swpaul else /* cast back to void * */ 233100441Scharnier f_print(fout, "\treturn((void *) &result);\n}\n"); 23412795Swpaul else 23512795Swpaul f_print(fout, "\treturn (retval);\n}\n"); 23612795Swpaul } 23712795Swpaul /* put in sample freeing routine */ 23812795Swpaul if (mtflag) { 23912795Swpaul f_print(fout, "\nint\n"); 24012795Swpaul pvname(def->def_name, vp->vers_num); 241149709Sstefanf f_print(fout,"_freeresult(SVCXPRT *transp, xdrproc_t xdr_result, caddr_t result)\n"); 24212795Swpaul f_print(fout, "{\n"); 24312795Swpaul f_print(fout, "\t(void) xdr_free(xdr_result, result);\n"); 244100441Scharnier f_print(fout, 24512795Swpaul "\n\t/*\n\t * Insert additional freeing code here, if needed\n\t */\n"); 24612795Swpaul f_print(fout, "\n}\n"); 24712795Swpaul 24812795Swpaul 24912795Swpaul } 25012795Swpaul } 25112795Swpaul} 25212795Swpaul 25312795Swpaul 25412795Swpaul 25517142Sjkhstatic void 25612795Swpaulreturn_type(plist) 25712795Swpaul proc_list *plist; 25812795Swpaul{ 25912795Swpaul ptype(plist->res_prefix, plist->res_type, 1); 26012795Swpaul} 26112795Swpaul 26217142Sjkhvoid 26312795Swpauladd_sample_msg() 26412795Swpaul{ 26512795Swpaul f_print(fout, "/*\n"); 26612795Swpaul f_print(fout, " * This is sample code generated by rpcgen.\n"); 26712795Swpaul f_print(fout, " * These are only templates and you can use them\n"); 26812795Swpaul f_print(fout, " * as a guideline for developing your own functions.\n"); 26912795Swpaul f_print(fout, " */\n\n"); 27012795Swpaul} 27112795Swpaul 27212795Swpaulvoid 27312795Swpaulwrite_sample_clnt_main() 27412795Swpaul{ 27512795Swpaul list *l; 27612795Swpaul definition *def; 27712795Swpaul version_list *vp; 27812795Swpaul 27912795Swpaul f_print(fout, "\n\n"); 280149709Sstefanf f_print(fout, "main(int argc, char *argv[])\n{\n"); 28112795Swpaul 28212795Swpaul f_print(fout, "\tchar *host;"); 28312795Swpaul f_print(fout, "\n\n\tif (argc < 2) {"); 28412795Swpaul f_print(fout, "\n\t\tprintf(\"usage: %%s server_host\\n\", argv[0]);\n"); 28512795Swpaul f_print(fout, "\t\texit(1);\n\t}"); 28612795Swpaul f_print(fout, "\n\thost = argv[1];\n"); 28712795Swpaul 28812795Swpaul for (l = defined; l != NULL; l = l->next) { 28912795Swpaul def = l->val; 29012795Swpaul if (def->def_kind != DEF_PROGRAM) { 29112795Swpaul continue; 29212795Swpaul } 29312795Swpaul for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) { 29412795Swpaul f_print(fout, "\t"); 29512795Swpaul pvname(def->def_name, vp->vers_num); 29612795Swpaul f_print(fout, "(host);\n"); 29712795Swpaul } 29812795Swpaul } 29912795Swpaul f_print(fout, "}\n"); 30012795Swpaul} 301