Annotation of src/usr.bin/rpcgen/rpc_sample.c, Revision 1.1
1.1 ! deraadt 1: /* $NetBSD: rpc_sample.c,v 1.2 1995/06/11 21:50:01 pk Exp $ */
! 2: /*
! 3: * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
! 4: * unrestricted use provided that this legend is included on all tape
! 5: * media and as a part of the software program in whole or part. Users
! 6: * may copy or modify Sun RPC without charge, but are not authorized
! 7: * to license or distribute it to anyone else except as part of a product or
! 8: * program developed by the user or with the express written consent of
! 9: * Sun Microsystems, Inc.
! 10: *
! 11: * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
! 12: * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
! 13: * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
! 14: *
! 15: * Sun RPC is provided with no support and without any obligation on the
! 16: * part of Sun Microsystems, Inc. to assist in its use, correction,
! 17: * modification or enhancement.
! 18: *
! 19: * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
! 20: * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
! 21: * OR ANY PART THEREOF.
! 22: *
! 23: * In no event will Sun Microsystems, Inc. be liable for any lost revenue
! 24: * or profits or other special, indirect and consequential damages, even if
! 25: * Sun has been advised of the possibility of such damages.
! 26: *
! 27: * Sun Microsystems, Inc.
! 28: * 2550 Garcia Avenue
! 29: * Mountain View, California 94043
! 30: */
! 31:
! 32: #ifndef lint
! 33: static char sccsid[] = "@(#)rpc_sample.c 1.1 90/08/30 (C) 1987 SMI";
! 34:
! 35: #endif
! 36:
! 37: /*
! 38: * rpc_sample.c, Sample client-server code outputter for the RPC protocol compiler
! 39: */
! 40:
! 41: #include <sys/cdefs.h>
! 42: #include <stdio.h>
! 43: #include <string.h>
! 44: #include "rpc_parse.h"
! 45: #include "rpc_util.h"
! 46:
! 47: static char RQSTP[] = "rqstp";
! 48:
! 49: void printarglist();
! 50: static write_sample_client __P((char *, version_list *));
! 51: static write_sample_server __P((definition *));
! 52: static return_type __P((proc_list *));
! 53:
! 54: void
! 55: write_sample_svc(def)
! 56: definition *def;
! 57: {
! 58:
! 59: if (def->def_kind != DEF_PROGRAM)
! 60: return;
! 61: write_sample_server(def);
! 62: }
! 63:
! 64:
! 65: int
! 66: write_sample_clnt(def)
! 67: definition *def;
! 68: {
! 69: version_list *vp;
! 70: int count = 0;
! 71:
! 72: if (def->def_kind != DEF_PROGRAM)
! 73: return( 0 );
! 74: /* generate sample code for each version */
! 75: for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
! 76: write_sample_client(def->def_name, vp );
! 77: ++count;
! 78: }
! 79: return( count );
! 80: }
! 81:
! 82:
! 83: static
! 84: write_sample_client(program_name, vp )
! 85: char* program_name;
! 86: version_list *vp;
! 87: {
! 88: proc_list *proc;
! 89: int i;
! 90: decl_list *l;
! 91:
! 92: f_print(fout, "\n\nvoid\n" );
! 93: pvname( program_name, vp->vers_num );
! 94: if( Cflag )
! 95: f_print(fout,"( char* host )\n{\n" );
! 96: else
! 97: f_print(fout, "(host)\nchar *host;\n{\n" );
! 98: f_print(fout, "\tCLIENT *clnt;\n");
! 99:
! 100: i = 0;
! 101: for (proc = vp->procs; proc != NULL; proc = proc->next) {
! 102: f_print(fout, "\t");
! 103: ptype(proc->res_prefix, proc->res_type, 1);
! 104: f_print(fout, " *result_%d;\n",++i);
! 105: /* print out declarations for arguments */
! 106: if( proc->arg_num < 2 && !newstyle) {
! 107: f_print( fout, "\t" );
! 108: if( !streq( proc->args.decls->decl.type, "void") )
! 109: ptype(proc->args.decls->decl.prefix, proc->args.decls->decl.type, 1);
! 110: else
! 111: f_print(fout, "char* "); /* cannot have "void" type */
! 112: f_print(fout, " ");
! 113: pvname( proc->proc_name, vp->vers_num );
! 114: f_print(fout, "_arg;\n");
! 115: } else if (!streq( proc->args.decls->decl.type, "void")) {
! 116: for (l = proc->args.decls; l != NULL; l = l->next) {
! 117: f_print( fout, "\t" );
! 118: ptype(l->decl.prefix, l->decl.type, 1);
! 119: f_print( fout, " ");
! 120: pvname( proc->proc_name, vp->vers_num );
! 121: f_print(fout, "_%s;\n", l->decl.name );
! 122: /* pdeclaration(proc->args.argname, &l->decl, 1, ";\n" );*/
! 123: }
! 124: }
! 125: }
! 126:
! 127: /* generate creation of client handle */
! 128: f_print(fout, "\tclnt = clnt_create(host, %s, %s, \"%s\");\n",
! 129: program_name, vp->vers_name, tirpcflag? "netpath" : "udp");
! 130: f_print(fout, "\tif (clnt == NULL) {\n");
! 131: f_print(fout, "\t\tclnt_pcreateerror(host);\n");
! 132: f_print(fout, "\t\texit(1);\n\t}\n");
! 133:
! 134: /* generate calls to procedures */
! 135: i = 0;
! 136: for (proc = vp->procs; proc != NULL; proc = proc->next) {
! 137: f_print(fout, "\tresult_%d = ",++i);
! 138: pvname(proc->proc_name, vp->vers_num);
! 139: if (proc->arg_num < 2 && !newstyle) {
! 140: f_print(fout, "(" );
! 141: if( streq( proc->args.decls->decl.type, "void") ) /* cast to void* */
! 142: f_print(fout, "(void*)");
! 143: f_print(fout, "&" );
! 144: pvname(proc->proc_name, vp->vers_num );
! 145: f_print(fout, "_arg, clnt);\n");
! 146: } else if (streq( proc->args.decls->decl.type, "void")) {
! 147: f_print(fout, "(clnt);\n");
! 148: }
! 149: else {
! 150: f_print(fout, "(");
! 151: for (l = proc->args.decls; l != NULL; l = l->next) {
! 152: pvname( proc->proc_name, vp->vers_num );
! 153: f_print(fout, "_%s, ", l->decl.name);
! 154: }
! 155: f_print(fout, "clnt);\n");
! 156: }
! 157: f_print(fout, "\tif (result_%d == NULL) {\n", i);
! 158: f_print(fout, "\t\tclnt_perror(clnt, \"call failed:\");\n");
! 159: f_print(fout, "\t}\n");
! 160: }
! 161:
! 162: f_print(fout, "\tclnt_destroy( clnt );\n" );
! 163: f_print(fout, "}\n");
! 164: }
! 165:
! 166: static
! 167: write_sample_server(def)
! 168: definition *def;
! 169: {
! 170: version_list *vp;
! 171: proc_list *proc;
! 172:
! 173: for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
! 174: for (proc = vp->procs; proc != NULL; proc = proc->next) {
! 175: f_print(fout, "\n");
! 176: /* if( Cflag )
! 177: f_print( fout, "extern \"C\"{\n");
! 178: */
! 179: return_type(proc);
! 180: f_print(fout, "* \n");
! 181: if( Cflag )
! 182: pvname_svc(proc->proc_name, vp->vers_num);
! 183: else
! 184: pvname(proc->proc_name, vp->vers_num);
! 185: printarglist( proc, RQSTP, "struct svc_req *" );
! 186:
! 187: f_print(fout, "{\n");
! 188: f_print(fout, "\n\tstatic ");
! 189: if( !streq( proc->res_type, "void") )
! 190: return_type(proc);
! 191: else
! 192: f_print(fout, "char*" ); /* cannot have void type */
! 193: f_print(fout, " result;\n", proc->res_type);
! 194: f_print(fout,
! 195: "\n\t/*\n\t * insert server code here\n\t */\n\n");
! 196: if( !streq( proc->res_type, "void") )
! 197: f_print(fout, "\treturn(&result);\n}\n");
! 198: else /* cast back to void * */
! 199: f_print(fout, "\treturn((void*) &result);\n}\n");
! 200: /* if( Cflag)
! 201: f_print( fout, "};\n");
! 202: */
! 203:
! 204: }
! 205: }
! 206: }
! 207:
! 208: static
! 209: return_type(plist)
! 210: proc_list *plist;
! 211: {
! 212: ptype( plist->res_prefix, plist->res_type, 1 );
! 213: }
! 214:
! 215: add_sample_msg()
! 216: {
! 217: f_print(fout, "/*\n");
! 218: f_print(fout, " * This is sample code generated by rpcgen.\n");
! 219: f_print(fout, " * These are only templates and you can use them\n");
! 220: f_print(fout, " * as a guideline for developing your own functions.\n");
! 221: f_print(fout, " */\n\n");
! 222: }
! 223:
! 224: void
! 225: write_sample_clnt_main()
! 226: {
! 227: list *l;
! 228: definition *def;
! 229: version_list *vp;
! 230:
! 231: f_print(fout, "\n\n" );
! 232: if( Cflag )
! 233: f_print(fout,"main( int argc, char* argv[] )\n{\n" );
! 234: else
! 235: f_print(fout, "main(argc, argv)\nint argc;\nchar *argv[];\n{\n" );
! 236:
! 237: f_print(fout, "\tchar *host;");
! 238: f_print(fout, "\n\n\tif(argc < 2) {");
! 239: f_print(fout, "\n\t\tprintf(\"usage: %%s server_host\\n\", argv[0]);\n" );
! 240: f_print(fout, "\t\texit(1);\n\t}");
! 241: f_print(fout, "\n\thost = argv[1];\n");
! 242:
! 243: for (l = defined; l != NULL; l = l->next) {
! 244: def = l->val;
! 245: if (def->def_kind != DEF_PROGRAM) {
! 246: continue;
! 247: }
! 248: for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
! 249: f_print( fout, "\t" );
! 250: pvname(def->def_name, vp->vers_num);
! 251: f_print( fout, "( host );\n" );
! 252: }
! 253: }
! 254: f_print(fout, "}\n");
! 255: }