rpc_sample.c revision 92921
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. 812795Swpaul * 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. 1212795Swpaul * 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. 1612795Swpaul * 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. 2012795Swpaul * 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. 2412795Swpaul * 2512795Swpaul * Sun Microsystems, Inc. 2612795Swpaul * 2550 Garcia Avenue 2712795Swpaul * Mountain View, California 94043 2892921Simp * 2992921Simp * $FreeBSD: head/usr.bin/rpcgen/rpc_sample.c 92921 2002-03-22 01:33:25Z imp $ 3012795Swpaul */ 3112795Swpaul 3212795Swpaul#pragma ident "@(#)rpc_sample.c 1.9 94/04/25 SMI" 3312795Swpaul 3412795Swpaul/* 3512795Swpaul * rpc_sample.c, Sample client-server code outputter for the RPC protocol compiler 3612795Swpaul * Copyright (C) 1987, Sun Microsystems, Inc. 3712795Swpaul */ 3812795Swpaul 3912795Swpaul#include <stdio.h> 4012795Swpaul#include <string.h> 4112795Swpaul#include "rpc_parse.h" 4212795Swpaul#include "rpc_util.h" 4312795Swpaul 4412795Swpaul 4512795Swpaulstatic char RQSTP[] = "rqstp"; 4612795Swpaul 4792921Simpextern void printarglist( proc_list *, char *, char *, char *); 4892921Simpstatic void write_sample_client( char *, version_list * ); 4992921Simpstatic void write_sample_server( definition * ); 5092921Simpstatic void return_type( proc_list * ); 5112795Swpaul 5212795Swpaulvoid 5312795Swpaulwrite_sample_svc(def) 5412795Swpaul definition *def; 5512795Swpaul{ 5612795Swpaul 5712795Swpaul if (def->def_kind != DEF_PROGRAM) 5812795Swpaul return; 5912795Swpaul write_sample_server(def); 6012795Swpaul} 6112795Swpaul 6212795Swpaul 6312795Swpaulint 6412795Swpaulwrite_sample_clnt(def) 6512795Swpaul definition *def; 6612795Swpaul{ 6712795Swpaul version_list *vp; 6812795Swpaul int count = 0; 6912795Swpaul 7012795Swpaul if (def->def_kind != DEF_PROGRAM) 7112795Swpaul return(0); 7212795Swpaul /* generate sample code for each version */ 7312795Swpaul for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) { 7412795Swpaul write_sample_client(def->def_name, vp); 7512795Swpaul ++count; 7612795Swpaul } 7712795Swpaul return(count); 7812795Swpaul} 7912795Swpaul 8012795Swpaul 8117142Sjkhstatic void 8212795Swpaulwrite_sample_client(program_name, vp) 8312795Swpaul char *program_name; 8412795Swpaul version_list *vp; 8512795Swpaul{ 8612795Swpaul proc_list *proc; 8712795Swpaul int i; 8812795Swpaul decl_list *l; 8912795Swpaul 9012795Swpaul f_print(fout, "\n\nvoid\n"); 9112795Swpaul pvname(program_name, vp->vers_num); 9212795Swpaul if(Cflag) 9312795Swpaul f_print(fout,"(char *host)\n{\n"); 9412795Swpaul else 9512795Swpaul f_print(fout, "(host)\n\tchar *host;\n{\n"); 9612795Swpaul f_print(fout, "\tCLIENT *clnt;\n"); 9712795Swpaul 9812795Swpaul i = 0; 9912795Swpaul for (proc = vp->procs; proc != NULL; proc = proc->next) { 10012795Swpaul f_print(fout, "\t"); 10112795Swpaul if (mtflag) { 10212795Swpaul f_print(fout, "enum clnt_stat retval_%d;\n\t", ++i); 10312795Swpaul ptype(proc->res_prefix, proc->res_type, 1); 10412795Swpaul f_print(fout, "result_%d;\n", i); 10512795Swpaul } else { 10612795Swpaul ptype(proc->res_prefix, proc->res_type, 1); 10712795Swpaul f_print(fout, " *result_%d;\n",++i); 10812795Swpaul } 10912795Swpaul /* print out declarations for arguments */ 11012795Swpaul if(proc->arg_num < 2 && !newstyle) { 11112795Swpaul f_print(fout, "\t"); 11212795Swpaul if(!streq(proc->args.decls->decl.type, "void")) 11312795Swpaul ptype(proc->args.decls->decl.prefix, 11412795Swpaul proc->args.decls->decl.type, 1); 11512795Swpaul else 11612795Swpaul f_print(fout, "char * "); /* cannot have "void" type */ 11712795Swpaul f_print(fout, " "); 11812795Swpaul pvname(proc->proc_name, vp->vers_num); 11912795Swpaul f_print(fout, "_arg;\n"); 12012795Swpaul } else if (!streq(proc->args.decls->decl.type, "void")) { 12112795Swpaul for (l = proc->args.decls; l != NULL; l = l->next) { 12212795Swpaul f_print(fout, "\t"); 12312795Swpaul ptype(l->decl.prefix, l->decl.type, 1); 12412795Swpaul if (strcmp(l->decl.type,"string") == 1) 12512795Swpaul f_print(fout, " "); 12612795Swpaul pvname(proc->proc_name, vp->vers_num); 12712795Swpaul f_print(fout, "_%s;\n", l->decl.name); 12812795Swpaul } 12912795Swpaul } 13012795Swpaul } 13112795Swpaul 13212795Swpaul /* generate creation of client handle */ 13312795Swpaul f_print(fout, "\n#ifndef\tDEBUG\n"); 13412795Swpaul f_print(fout, "\tclnt = clnt_create(host, %s, %s, \"%s\");\n", 13512795Swpaul program_name, vp->vers_name, tirpcflag? "netpath" : "udp"); 13612795Swpaul f_print(fout, "\tif (clnt == (CLIENT *) NULL) {\n"); 13712795Swpaul f_print(fout, "\t\tclnt_pcreateerror(host);\n"); 13812795Swpaul f_print(fout, "\t\texit(1);\n\t}\n"); 13912795Swpaul f_print(fout, "#endif\t/* DEBUG */\n\n"); 14012795Swpaul 14112795Swpaul /* generate calls to procedures */ 14212795Swpaul i = 0; 14312795Swpaul for (proc = vp->procs; proc != NULL; proc = proc->next) { 14412795Swpaul if (mtflag) 14512795Swpaul f_print(fout, "\tretval_%d = ",++i); 14612795Swpaul else 14712795Swpaul f_print(fout, "\tresult_%d = ",++i); 14812795Swpaul pvname(proc->proc_name, vp->vers_num); 14912795Swpaul if (proc->arg_num < 2 && !newstyle) { 15012795Swpaul f_print(fout, "("); 15112795Swpaul if(streq(proc->args.decls->decl.type, "void")) 15212795Swpaul /* cast to void * */ 15312795Swpaul f_print(fout, "(void *)"); 15412795Swpaul f_print(fout, "&"); 15512795Swpaul pvname(proc->proc_name, vp->vers_num); 15612795Swpaul if (mtflag) 15712795Swpaul f_print(fout, "_arg, &result_%d, clnt);\n", 15812795Swpaul i); 15912795Swpaul else 16012795Swpaul f_print(fout, "_arg, clnt);\n"); 16112795Swpaul 16212795Swpaul } else if (streq(proc->args.decls->decl.type, "void")) { 16312795Swpaul if (mtflag) 16412795Swpaul f_print(fout, "(&result_%d, clnt);\n", i); 16512795Swpaul else 16612795Swpaul f_print(fout, "(clnt);\n"); 16712795Swpaul } 16812795Swpaul else { 16912795Swpaul f_print(fout, "("); 17012795Swpaul for (l = proc->args.decls; l != NULL; l = l->next) { 17112795Swpaul pvname(proc->proc_name, vp->vers_num); 17212795Swpaul f_print(fout, "_%s, ", l->decl.name); 17312795Swpaul } 17412795Swpaul if (mtflag) 17512795Swpaul f_print(fout, "&result_%d, ", i); 17612795Swpaul 17712795Swpaul f_print(fout, "clnt);\n"); 17812795Swpaul } 17912795Swpaul if (mtflag) { 18012795Swpaul f_print(fout, "\tif (retval_%d != RPC_SUCCESS) {\n", i); 18112795Swpaul 18212795Swpaul } else { 18312795Swpaul f_print(fout, "\tif (result_%d == (", i); 18412795Swpaul ptype(proc->res_prefix, proc->res_type, 1); 18512795Swpaul f_print(fout, "*) NULL) {\n"); 18612795Swpaul } 18712795Swpaul f_print(fout, "\t\tclnt_perror(clnt, \"call failed\");\n"); 18812795Swpaul f_print(fout, "\t}\n"); 18912795Swpaul } 19012795Swpaul 19112795Swpaul f_print(fout, "#ifndef\tDEBUG\n"); 19212795Swpaul f_print(fout, "\tclnt_destroy(clnt);\n"); 19312795Swpaul f_print(fout, "#endif\t /* DEBUG */\n"); 19412795Swpaul f_print(fout, "}\n"); 19512795Swpaul} 19612795Swpaul 19717142Sjkhstatic void 19812795Swpaulwrite_sample_server(def) 19912795Swpaul definition *def; 20012795Swpaul{ 20112795Swpaul version_list *vp; 20212795Swpaul proc_list *proc; 20312795Swpaul 20412795Swpaul for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) { 20512795Swpaul for (proc = vp->procs; proc != NULL; proc = proc->next) { 20612795Swpaul f_print(fout, "\n"); 20712795Swpaul if (!mtflag) { 20812795Swpaul return_type(proc); 20912795Swpaul f_print(fout, "*\n"); 21012795Swpaul } else 21112795Swpaul f_print(fout, "bool_t\n"); 21212795Swpaul if (Cflag || mtflag) 21312795Swpaul pvname_svc(proc->proc_name, vp->vers_num); 21412795Swpaul else 21512795Swpaul pvname(proc->proc_name, vp->vers_num); 21612795Swpaul printarglist(proc, "result", RQSTP, "struct svc_req *"); 21712795Swpaul 21812795Swpaul f_print(fout, "{\n"); 21912795Swpaul if (!mtflag) { 22012795Swpaul f_print(fout, "\tstatic "); 22112795Swpaul if(!streq(proc->res_type, "void")) 22212795Swpaul return_type(proc); 22312795Swpaul else 22412795Swpaul f_print(fout, "char *"); 22512795Swpaul /* cannot have void type */ 22638022Sbde f_print(fout, " result;\n"); 22712795Swpaul } 22812795Swpaul else 22912795Swpaul f_print(fout, "\tbool_t retval;\n"); 23012795Swpaul f_print(fout, 23112795Swpaul "\n\t/*\n\t * insert server code here\n\t */\n\n"); 23212795Swpaul 23312795Swpaul if (!mtflag) 23412795Swpaul if(!streq(proc->res_type, "void")) 23512795Swpaul f_print(fout, "\treturn (&result);\n}\n"); 23612795Swpaul else /* cast back to void * */ 23712795Swpaul f_print(fout, "\treturn((void *) &result);\n}\n"); 23812795Swpaul else 23912795Swpaul f_print(fout, "\treturn (retval);\n}\n"); 24012795Swpaul } 24112795Swpaul /* put in sample freeing routine */ 24212795Swpaul if (mtflag) { 24312795Swpaul f_print(fout, "\nint\n"); 24412795Swpaul pvname(def->def_name, vp->vers_num); 24512795Swpaul if (Cflag) 24612795Swpaul f_print(fout,"_freeresult(SVCXPRT *transp, xdrproc_t xdr_result, caddr_t result)\n"); 24712795Swpaul else { 24812795Swpaul f_print(fout,"_freeresult(transp, xdr_result, result)\n"); 24912795Swpaul f_print(fout,"\tSVCXPRT *transp;\n"); 25012795Swpaul f_print(fout,"\txdrproc_t xdr_result;\n"); 25112795Swpaul f_print(fout,"\tcaddr_t result;\n"); 25212795Swpaul } 25312795Swpaul f_print(fout, "{\n"); 25412795Swpaul f_print(fout, "\t(void) xdr_free(xdr_result, result);\n"); 25512795Swpaul f_print(fout, 25612795Swpaul "\n\t/*\n\t * Insert additional freeing code here, if needed\n\t */\n"); 25712795Swpaul f_print(fout, "\n}\n"); 25812795Swpaul 25912795Swpaul 26012795Swpaul } 26112795Swpaul } 26212795Swpaul} 26312795Swpaul 26412795Swpaul 26512795Swpaul 26617142Sjkhstatic void 26712795Swpaulreturn_type(plist) 26812795Swpaul proc_list *plist; 26912795Swpaul{ 27012795Swpaul ptype(plist->res_prefix, plist->res_type, 1); 27112795Swpaul} 27212795Swpaul 27317142Sjkhvoid 27412795Swpauladd_sample_msg() 27512795Swpaul{ 27612795Swpaul f_print(fout, "/*\n"); 27712795Swpaul f_print(fout, " * This is sample code generated by rpcgen.\n"); 27812795Swpaul f_print(fout, " * These are only templates and you can use them\n"); 27912795Swpaul f_print(fout, " * as a guideline for developing your own functions.\n"); 28012795Swpaul f_print(fout, " */\n\n"); 28112795Swpaul} 28212795Swpaul 28312795Swpaulvoid 28412795Swpaulwrite_sample_clnt_main() 28512795Swpaul{ 28612795Swpaul list *l; 28712795Swpaul definition *def; 28812795Swpaul version_list *vp; 28912795Swpaul 29012795Swpaul f_print(fout, "\n\n"); 29112795Swpaul if(Cflag) 29212795Swpaul f_print(fout,"main(int argc, char *argv[])\n{\n"); 29312795Swpaul else 29412795Swpaul f_print(fout, "main(argc, argv)\n\tint argc;\n\tchar *argv[];\n{\n"); 29512795Swpaul 29612795Swpaul f_print(fout, "\tchar *host;"); 29712795Swpaul f_print(fout, "\n\n\tif (argc < 2) {"); 29812795Swpaul f_print(fout, "\n\t\tprintf(\"usage: %%s server_host\\n\", argv[0]);\n"); 29912795Swpaul f_print(fout, "\t\texit(1);\n\t}"); 30012795Swpaul f_print(fout, "\n\thost = argv[1];\n"); 30112795Swpaul 30212795Swpaul for (l = defined; l != NULL; l = l->next) { 30312795Swpaul def = l->val; 30412795Swpaul if (def->def_kind != DEF_PROGRAM) { 30512795Swpaul continue; 30612795Swpaul } 30712795Swpaul for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) { 30812795Swpaul f_print(fout, "\t"); 30912795Swpaul pvname(def->def_name, vp->vers_num); 31012795Swpaul f_print(fout, "(host);\n"); 31112795Swpaul } 31212795Swpaul } 31312795Swpaul f_print(fout, "}\n"); 31412795Swpaul} 315