rpc_sample.c revision 149682
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 149682 2005-08-31 20:45:15Z 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); 9412795Swpaul if(Cflag) 9512795Swpaul f_print(fout,"(char *host)\n{\n"); 9612795Swpaul else 9712795Swpaul f_print(fout, "(host)\n\tchar *host;\n{\n"); 9812795Swpaul f_print(fout, "\tCLIENT *clnt;\n"); 9912795Swpaul 10012795Swpaul i = 0; 10112795Swpaul for (proc = vp->procs; proc != NULL; proc = proc->next) { 10212795Swpaul f_print(fout, "\t"); 10312795Swpaul if (mtflag) { 10412795Swpaul f_print(fout, "enum clnt_stat retval_%d;\n\t", ++i); 10512795Swpaul ptype(proc->res_prefix, proc->res_type, 1); 10612795Swpaul f_print(fout, "result_%d;\n", i); 10712795Swpaul } else { 10812795Swpaul ptype(proc->res_prefix, proc->res_type, 1); 10912795Swpaul f_print(fout, " *result_%d;\n",++i); 11012795Swpaul } 11112795Swpaul /* print out declarations for arguments */ 11212795Swpaul if(proc->arg_num < 2 && !newstyle) { 11312795Swpaul f_print(fout, "\t"); 11412795Swpaul if(!streq(proc->args.decls->decl.type, "void")) 115100441Scharnier ptype(proc->args.decls->decl.prefix, 11612795Swpaul proc->args.decls->decl.type, 1); 11712795Swpaul else 11812795Swpaul f_print(fout, "char * "); /* cannot have "void" type */ 11912795Swpaul f_print(fout, " "); 12012795Swpaul pvname(proc->proc_name, vp->vers_num); 12112795Swpaul f_print(fout, "_arg;\n"); 12212795Swpaul } else if (!streq(proc->args.decls->decl.type, "void")) { 12312795Swpaul for (l = proc->args.decls; l != NULL; l = l->next) { 12412795Swpaul f_print(fout, "\t"); 12512795Swpaul ptype(l->decl.prefix, l->decl.type, 1); 12612795Swpaul if (strcmp(l->decl.type,"string") == 1) 12712795Swpaul f_print(fout, " "); 12812795Swpaul pvname(proc->proc_name, vp->vers_num); 12912795Swpaul f_print(fout, "_%s;\n", l->decl.name); 13012795Swpaul } 13112795Swpaul } 13212795Swpaul } 13312795Swpaul 13412795Swpaul /* generate creation of client handle */ 13512795Swpaul f_print(fout, "\n#ifndef\tDEBUG\n"); 136100441Scharnier f_print(fout, "\tclnt = clnt_create(host, %s, %s, \"%s\");\n", 13712795Swpaul program_name, vp->vers_name, tirpcflag? "netpath" : "udp"); 13812795Swpaul f_print(fout, "\tif (clnt == (CLIENT *) NULL) {\n"); 13912795Swpaul f_print(fout, "\t\tclnt_pcreateerror(host);\n"); 14012795Swpaul f_print(fout, "\t\texit(1);\n\t}\n"); 14112795Swpaul f_print(fout, "#endif\t/* DEBUG */\n\n"); 14212795Swpaul 14312795Swpaul /* generate calls to procedures */ 14412795Swpaul i = 0; 14512795Swpaul for (proc = vp->procs; proc != NULL; proc = proc->next) { 14612795Swpaul if (mtflag) 147100441Scharnier f_print(fout, "\tretval_%d = ",++i); 14812795Swpaul else 149100441Scharnier f_print(fout, "\tresult_%d = ",++i); 15012795Swpaul pvname(proc->proc_name, vp->vers_num); 15112795Swpaul if (proc->arg_num < 2 && !newstyle) { 15212795Swpaul f_print(fout, "("); 153100441Scharnier if(streq(proc->args.decls->decl.type, "void")) 15412795Swpaul /* cast to void * */ 15512795Swpaul f_print(fout, "(void *)"); 15612795Swpaul f_print(fout, "&"); 15712795Swpaul pvname(proc->proc_name, vp->vers_num); 15812795Swpaul if (mtflag) 15912795Swpaul f_print(fout, "_arg, &result_%d, clnt);\n", 16012795Swpaul i); 16112795Swpaul else 16212795Swpaul f_print(fout, "_arg, clnt);\n"); 16312795Swpaul 16412795Swpaul } else if (streq(proc->args.decls->decl.type, "void")) { 16512795Swpaul if (mtflag) 16612795Swpaul f_print(fout, "(&result_%d, clnt);\n", i); 16712795Swpaul else 16812795Swpaul f_print(fout, "(clnt);\n"); 16912795Swpaul } 17012795Swpaul else { 17112795Swpaul f_print(fout, "("); 17212795Swpaul for (l = proc->args.decls; l != NULL; l = l->next) { 17312795Swpaul pvname(proc->proc_name, vp->vers_num); 17412795Swpaul f_print(fout, "_%s, ", l->decl.name); 17512795Swpaul } 17612795Swpaul if (mtflag) 17712795Swpaul f_print(fout, "&result_%d, ", i); 17812795Swpaul 17912795Swpaul f_print(fout, "clnt);\n"); 18012795Swpaul } 18112795Swpaul if (mtflag) { 18212795Swpaul f_print(fout, "\tif (retval_%d != RPC_SUCCESS) {\n", i); 18312795Swpaul 18412795Swpaul } else { 18512795Swpaul f_print(fout, "\tif (result_%d == (", i); 18612795Swpaul ptype(proc->res_prefix, proc->res_type, 1); 18712795Swpaul f_print(fout, "*) NULL) {\n"); 18812795Swpaul } 18912795Swpaul f_print(fout, "\t\tclnt_perror(clnt, \"call failed\");\n"); 19012795Swpaul f_print(fout, "\t}\n"); 19112795Swpaul } 19212795Swpaul 19312795Swpaul f_print(fout, "#ifndef\tDEBUG\n"); 19412795Swpaul f_print(fout, "\tclnt_destroy(clnt);\n"); 19512795Swpaul f_print(fout, "#endif\t /* DEBUG */\n"); 19612795Swpaul f_print(fout, "}\n"); 19712795Swpaul} 19812795Swpaul 19917142Sjkhstatic void 20012795Swpaulwrite_sample_server(def) 20112795Swpaul definition *def; 20212795Swpaul{ 20312795Swpaul version_list *vp; 20412795Swpaul proc_list *proc; 20512795Swpaul 20612795Swpaul for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) { 20712795Swpaul for (proc = vp->procs; proc != NULL; proc = proc->next) { 20812795Swpaul f_print(fout, "\n"); 20912795Swpaul if (!mtflag) { 21012795Swpaul return_type(proc); 21112795Swpaul f_print(fout, "*\n"); 21212795Swpaul } else 21312795Swpaul f_print(fout, "bool_t\n"); 21412795Swpaul if (Cflag || mtflag) 21512795Swpaul pvname_svc(proc->proc_name, vp->vers_num); 21612795Swpaul else 21712795Swpaul pvname(proc->proc_name, vp->vers_num); 21812795Swpaul printarglist(proc, "result", RQSTP, "struct svc_req *"); 21912795Swpaul 22012795Swpaul f_print(fout, "{\n"); 22112795Swpaul if (!mtflag) { 22212795Swpaul f_print(fout, "\tstatic "); 22312795Swpaul if(!streq(proc->res_type, "void")) 22412795Swpaul return_type(proc); 22512795Swpaul else 22612795Swpaul f_print(fout, "char *"); 22712795Swpaul /* cannot have void type */ 22838022Sbde f_print(fout, " result;\n"); 22912795Swpaul } 23012795Swpaul else 23112795Swpaul f_print(fout, "\tbool_t retval;\n"); 232100441Scharnier f_print(fout, 23312795Swpaul "\n\t/*\n\t * insert server code here\n\t */\n\n"); 23412795Swpaul 23512795Swpaul if (!mtflag) 23612795Swpaul if(!streq(proc->res_type, "void")) 23712795Swpaul f_print(fout, "\treturn (&result);\n}\n"); 23812795Swpaul else /* cast back to void * */ 239100441Scharnier f_print(fout, "\treturn((void *) &result);\n}\n"); 24012795Swpaul else 24112795Swpaul f_print(fout, "\treturn (retval);\n}\n"); 24212795Swpaul } 24312795Swpaul /* put in sample freeing routine */ 24412795Swpaul if (mtflag) { 24512795Swpaul f_print(fout, "\nint\n"); 24612795Swpaul pvname(def->def_name, vp->vers_num); 247100441Scharnier if (Cflag) 24812795Swpaul f_print(fout,"_freeresult(SVCXPRT *transp, xdrproc_t xdr_result, caddr_t result)\n"); 24912795Swpaul else { 25012795Swpaul f_print(fout,"_freeresult(transp, xdr_result, result)\n"); 25112795Swpaul f_print(fout,"\tSVCXPRT *transp;\n"); 25212795Swpaul f_print(fout,"\txdrproc_t xdr_result;\n"); 25312795Swpaul f_print(fout,"\tcaddr_t result;\n"); 25412795Swpaul } 25512795Swpaul f_print(fout, "{\n"); 25612795Swpaul f_print(fout, "\t(void) xdr_free(xdr_result, result);\n"); 257100441Scharnier f_print(fout, 25812795Swpaul "\n\t/*\n\t * Insert additional freeing code here, if needed\n\t */\n"); 25912795Swpaul f_print(fout, "\n}\n"); 26012795Swpaul 26112795Swpaul 26212795Swpaul } 26312795Swpaul } 26412795Swpaul} 26512795Swpaul 26612795Swpaul 26712795Swpaul 26817142Sjkhstatic void 26912795Swpaulreturn_type(plist) 27012795Swpaul proc_list *plist; 27112795Swpaul{ 27212795Swpaul ptype(plist->res_prefix, plist->res_type, 1); 27312795Swpaul} 27412795Swpaul 27517142Sjkhvoid 27612795Swpauladd_sample_msg() 27712795Swpaul{ 27812795Swpaul f_print(fout, "/*\n"); 27912795Swpaul f_print(fout, " * This is sample code generated by rpcgen.\n"); 28012795Swpaul f_print(fout, " * These are only templates and you can use them\n"); 28112795Swpaul f_print(fout, " * as a guideline for developing your own functions.\n"); 28212795Swpaul f_print(fout, " */\n\n"); 28312795Swpaul} 28412795Swpaul 28512795Swpaulvoid 28612795Swpaulwrite_sample_clnt_main() 28712795Swpaul{ 28812795Swpaul list *l; 28912795Swpaul definition *def; 29012795Swpaul version_list *vp; 29112795Swpaul 29212795Swpaul f_print(fout, "\n\n"); 29312795Swpaul if(Cflag) 29412795Swpaul f_print(fout,"main(int argc, char *argv[])\n{\n"); 29512795Swpaul else 29612795Swpaul f_print(fout, "main(argc, argv)\n\tint argc;\n\tchar *argv[];\n{\n"); 29712795Swpaul 29812795Swpaul f_print(fout, "\tchar *host;"); 29912795Swpaul f_print(fout, "\n\n\tif (argc < 2) {"); 30012795Swpaul f_print(fout, "\n\t\tprintf(\"usage: %%s server_host\\n\", argv[0]);\n"); 30112795Swpaul f_print(fout, "\t\texit(1);\n\t}"); 30212795Swpaul f_print(fout, "\n\thost = argv[1];\n"); 30312795Swpaul 30412795Swpaul for (l = defined; l != NULL; l = l->next) { 30512795Swpaul def = l->val; 30612795Swpaul if (def->def_kind != DEF_PROGRAM) { 30712795Swpaul continue; 30812795Swpaul } 30912795Swpaul for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) { 31012795Swpaul f_print(fout, "\t"); 31112795Swpaul pvname(def->def_name, vp->vers_num); 31212795Swpaul f_print(fout, "(host);\n"); 31312795Swpaul } 31412795Swpaul } 31512795Swpaul f_print(fout, "}\n"); 31612795Swpaul} 317