1 /* $OpenBSD: rpc_clntout.c,v 1.5 2001/07/18 22:26:00 deraadt Exp $ */ 2 /* $NetBSD: rpc_clntout.c,v 1.4 1995/06/11 21:49:52 pk Exp $ */ 3 /* 4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 5 * unrestricted use provided that this legend is included on all tape 6 * media and as a part of the software program in whole or part. Users 7 * may copy or modify Sun RPC without charge, but are not authorized 8 * to license or distribute it to anyone else except as part of a product or 9 * program developed by the user or with the express written consent of 10 * Sun Microsystems, Inc. 11 * 12 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 13 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 14 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 15 * 16 * Sun RPC is provided with no support and without any obligation on the 17 * part of Sun Microsystems, Inc. to assist in its use, correction, 18 * modification or enhancement. 19 * 20 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 21 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 22 * OR ANY PART THEREOF. 23 * 24 * In no event will Sun Microsystems, Inc. be liable for any lost revenue 25 * or profits or other special, indirect and consequential damages, even if 26 * Sun has been advised of the possibility of such damages. 27 * 28 * Sun Microsystems, Inc. 29 * 2550 Garcia Avenue 30 * Mountain View, California 94043 31 */ 32 33 #ifndef lint 34 static char sccsid[] = "@(#)rpc_clntout.c 1.11 89/02/22 (C) 1987 SMI"; 35 #endif 36 37 /* 38 * rpc_clntout.c, Client-stub outputter for the RPC protocol compiler 39 * Copyright (C) 1987, Sun Microsytsems, Inc. 40 */ 41 #include <stdio.h> 42 #include <string.h> 43 #include <rpc/types.h> 44 #include "rpc_parse.h" 45 #include "rpc_util.h" 46 47 static write_program __P((definition *)); 48 static void printbody __P((proc_list *)); 49 50 extern pdeclaration(); 51 52 #define DEFAULT_TIMEOUT 25 /* in seconds */ 53 static char RESULT[] = "clnt_res"; 54 55 56 void 57 write_stubs() 58 { 59 list *l; 60 definition *def; 61 62 f_print(fout, 63 "\n/* Default timeout can be changed using clnt_control() */\n"); 64 f_print(fout, "static struct timeval TIMEOUT = { %d, 0 };\n", 65 DEFAULT_TIMEOUT); 66 for (l = defined; l != NULL; l = l->next) { 67 def = (definition *) l->val; 68 if (def->def_kind == DEF_PROGRAM) { 69 write_program(def); 70 } 71 } 72 } 73 74 static 75 write_program(def) 76 definition *def; 77 { 78 version_list *vp; 79 proc_list *proc; 80 81 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) { 82 for (proc = vp->procs; proc != NULL; proc = proc->next) { 83 f_print(fout, "\n"); 84 ptype(proc->res_prefix, proc->res_type, 1); 85 f_print(fout, "*\n"); 86 pvname(proc->proc_name, vp->vers_num); 87 printarglist( proc, "clnt", "CLIENT *" ); 88 f_print(fout, "{\n"); 89 printbody(proc); 90 f_print(fout, "}\n"); 91 } 92 } 93 } 94 95 /* Writes out declarations of procedure's argument list. 96 In either ANSI C style, in one of old rpcgen style (pass by reference), 97 or new rpcgen style (multiple arguments, pass by value); 98 */ 99 100 /* sample addargname = "clnt"; sample addargtype = "CLIENT * " */ 101 102 void printarglist( proc, addargname, addargtype ) 103 proc_list *proc; 104 char *addargname, *addargtype; 105 { 106 107 decl_list *l; 108 109 if (!newstyle) { /* old style: always pass argument by reference */ 110 if (Cflag) { /* C++ style heading */ 111 f_print(fout, "("); 112 ptype(proc->args.decls->decl.prefix, proc->args.decls->decl.type, 1); 113 f_print(fout, "*argp, %s%s)\n", addargtype, addargname ); 114 } else { 115 f_print(fout, "(argp, %s)\n", addargname); 116 f_print(fout, "\t"); 117 ptype(proc->args.decls->decl.prefix, proc->args.decls->decl.type, 1); 118 f_print(fout, "*argp;\n"); 119 } 120 } else if (streq( proc->args.decls->decl.type, "void")) { 121 /* newstyle, 0 argument */ 122 if( Cflag ) 123 f_print(fout, "(%s%s)\n", addargtype, addargname ); 124 else 125 f_print(fout, "(%s)\n", addargname); 126 } else { 127 /* new style, 1 or multiple arguments */ 128 if( !Cflag ) { 129 f_print(fout, "("); 130 for (l = proc->args.decls; l != NULL; l = l->next) 131 f_print(fout, "%s, ", l->decl.name); 132 f_print(fout, "%s)\n", addargname ); 133 for (l = proc->args.decls; l != NULL; l = l->next) { 134 pdeclaration(proc->args.argname, &l->decl, 1, ";\n" ); 135 } 136 } else { /* C++ style header */ 137 f_print(fout, "("); 138 for(l = proc->args.decls; l != NULL; l = l->next) { 139 pdeclaration(proc->args.argname, &l->decl, 0, ", " ); 140 } 141 f_print(fout, " %s%s)\n", addargtype, addargname ); 142 } 143 } 144 145 if( !Cflag ) 146 f_print(fout, "\t%s%s;\n", addargtype, addargname ); 147 } 148 149 150 151 static char * 152 ampr(type) 153 char *type; 154 { 155 if (isvectordef(type, REL_ALIAS)) { 156 return (""); 157 } else { 158 return ("&"); 159 } 160 } 161 162 static void 163 printbody(proc) 164 proc_list *proc; 165 { 166 decl_list *l; 167 bool_t args2 = (proc->arg_num > 1); 168 int i; 169 170 /* For new style with multiple arguments, need a structure in which 171 to stuff the arguments. */ 172 if ( newstyle && args2) { 173 f_print(fout, "\t%s", proc->args.argname); 174 f_print(fout, " arg;\n"); 175 } 176 f_print(fout, "\tstatic "); 177 if (streq(proc->res_type, "void")) { 178 f_print(fout, "char "); 179 } else { 180 ptype(proc->res_prefix, proc->res_type, 0); 181 } 182 f_print(fout, "%s;\n",RESULT); 183 f_print(fout, "\n"); 184 f_print(fout, "\tmemset((char *)%s%s, 0, sizeof(%s));\n", 185 ampr(proc->res_type ), RESULT, RESULT); 186 if (newstyle && !args2 && (streq( proc->args.decls->decl.type, "void"))) { 187 /* newstyle, 0 arguments */ 188 f_print(fout, 189 "\tif (clnt_call(clnt, %s, xdr_void", proc->proc_name); 190 f_print(fout, 191 ", NULL, xdr_%s, %s%s, TIMEOUT) != RPC_SUCCESS) {\n", 192 stringfix(proc->res_type), ampr(proc->res_type), RESULT); 193 194 } else if ( newstyle && args2) { 195 /* newstyle, multiple arguments: stuff arguments into structure */ 196 for (l = proc->args.decls; l != NULL; l = l->next) { 197 f_print(fout, "\targ.%s = %s;\n", 198 l->decl.name, l->decl.name); 199 } 200 f_print(fout, 201 "\tif (clnt_call(clnt, %s, xdr_%s", proc->proc_name, 202 proc->args.argname); 203 f_print(fout, 204 ", &arg, xdr_%s, %s%s, TIMEOUT) != RPC_SUCCESS) {\n", 205 stringfix(proc->res_type), 206 ampr(proc->res_type), RESULT); 207 } else { /* single argument, new or old style */ 208 f_print(fout, 209 "\tif (clnt_call(clnt, %s, xdr_%s, %s%s, xdr_%s, %s%s, TIMEOUT) != RPC_SUCCESS) {\n", 210 proc->proc_name, 211 stringfix(proc->args.decls->decl.type), 212 (newstyle ? "&" : ""), 213 (newstyle ? proc->args.decls->decl.name : "argp"), 214 stringfix(proc->res_type), 215 ampr(proc->res_type),RESULT); 216 } 217 f_print(fout, "\t\treturn (NULL);\n"); 218 f_print(fout, "\t}\n"); 219 if (streq(proc->res_type, "void")) { 220 f_print(fout, "\treturn ((void *)%s%s);\n", 221 ampr(proc->res_type),RESULT); 222 } else { 223 f_print(fout, "\treturn (%s%s);\n", ampr(proc->res_type),RESULT); 224 } 225 } 226 227