1 /* $OpenBSD: rpc_clntout.c,v 1.13 2007/10/03 14:35:48 weingart 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 const 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 void write_program(definition *); 48 static void printbody(proc_list *); 49 50 #define DEFAULT_TIMEOUT 25 /* in seconds */ 51 static char RESULT[] = "clnt_res"; 52 53 54 void 55 write_stubs() 56 { 57 list *l; 58 definition *def; 59 60 fprintf(fout, 61 "\n/* Default timeout can be changed using clnt_control() */\n"); 62 fprintf(fout, "static struct timeval TIMEOUT = { %d, 0 };\n", 63 DEFAULT_TIMEOUT); 64 for (l = defined; l != NULL; l = l->next) { 65 def = (definition *) l->val; 66 if (def->def_kind == DEF_PROGRAM) { 67 write_program(def); 68 } 69 } 70 } 71 72 static void 73 write_program(def) 74 definition *def; 75 { 76 version_list *vp; 77 proc_list *proc; 78 79 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) { 80 for (proc = vp->procs; proc != NULL; proc = proc->next) { 81 fprintf(fout, "\n"); 82 ptype(proc->res_prefix, proc->res_type, 1); 83 fprintf(fout, "*\n"); 84 pvname(proc->proc_name, vp->vers_num); 85 printarglist(proc, "clnt", "CLIENT *"); 86 fprintf(fout, "{\n"); 87 printbody(proc); 88 fprintf(fout, "}\n"); 89 } 90 } 91 } 92 93 /* 94 * Writes out declarations of procedure's argument list. 95 * In either ANSI C style, in one of old rpcgen style (pass by reference), 96 * or new rpcgen style (multiple arguments, pass by value); 97 */ 98 99 /* sample addargname = "clnt"; sample addargtype = "CLIENT * " */ 100 101 void printarglist(proc, addargname, addargtype) 102 proc_list *proc; 103 char *addargname, *addargtype; 104 { 105 decl_list *l; 106 107 if (!newstyle) { /* old style: always pass argument by reference */ 108 if (Cflag) { /* C++ style heading */ 109 fprintf(fout, "("); 110 ptype(proc->args.decls->decl.prefix, 111 proc->args.decls->decl.type, 1); 112 fprintf(fout, "*argp, %s%s)\n", addargtype, addargname); 113 } else { 114 fprintf(fout, "(argp, %s)\n", addargname); 115 fprintf(fout, "\t"); 116 ptype(proc->args.decls->decl.prefix, 117 proc->args.decls->decl.type, 1); 118 fprintf(fout, "*argp;\n"); 119 } 120 } else if (streq(proc->args.decls->decl.type, "void")) { 121 /* newstyle, 0 argument */ 122 if (Cflag) 123 fprintf(fout, "(%s%s)\n", addargtype, addargname); 124 else 125 fprintf(fout, "(%s)\n", addargname); 126 } else { 127 /* new style, 1 or multiple arguments */ 128 if (!Cflag) { 129 fprintf(fout, "("); 130 for (l = proc->args.decls; l != NULL; l = l->next) 131 fprintf(fout, "%s, ", l->decl.name); 132 fprintf(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 } else { /* C++ style header */ 136 fprintf(fout, "("); 137 for (l = proc->args.decls; l != NULL; l = l->next) 138 pdeclaration(proc->args.argname, &l->decl, 0, ", "); 139 fprintf(fout, " %s%s)\n", addargtype, addargname); 140 } 141 } 142 143 if (!Cflag) 144 fprintf(fout, "\t%s%s;\n", addargtype, addargname); 145 } 146 147 static char * 148 ampr(char *type) 149 { 150 if (isvectordef(type, REL_ALIAS)) { 151 return (""); 152 } else { 153 return ("&"); 154 } 155 } 156 157 static void 158 printbody(proc) 159 proc_list *proc; 160 { 161 decl_list *l; 162 bool_t args2 = (proc->arg_num > 1); 163 164 /* 165 * For new style with multiple arguments, need a structure in which 166 * to stuff the arguments. 167 */ 168 if (newstyle && args2) { 169 fprintf(fout, "\t%s", proc->args.argname); 170 fprintf(fout, " arg;\n"); 171 } 172 fprintf(fout, "\tstatic "); 173 if (streq(proc->res_type, "void")) { 174 fprintf(fout, "char "); 175 } else { 176 ptype(proc->res_prefix, proc->res_type, 0); 177 } 178 fprintf(fout, "%s;\n",RESULT); 179 fprintf(fout, "\n"); 180 fprintf(fout, "\tmemset((char *)%s%s, 0, sizeof(%s));\n", 181 ampr(proc->res_type), RESULT, RESULT); 182 if (newstyle && !args2 && (streq(proc->args.decls->decl.type, "void"))) { 183 /* newstyle, 0 arguments */ 184 fprintf(fout, 185 "\tif (clnt_call(clnt, %s, xdr_void", proc->proc_name); 186 fprintf(fout, 187 ", NULL, xdr_%s, %s%s, TIMEOUT) != RPC_SUCCESS) {\n", 188 stringfix(proc->res_type), ampr(proc->res_type), RESULT); 189 } else if (newstyle && args2) { 190 /* newstyle, multiple arguments: stuff arguments into structure */ 191 for (l = proc->args.decls; l != NULL; l = l->next) { 192 fprintf(fout, "\targ.%s = %s;\n", 193 l->decl.name, l->decl.name); 194 } 195 fprintf(fout, 196 "\tif (clnt_call(clnt, %s, xdr_%s", proc->proc_name, 197 proc->args.argname); 198 fprintf(fout, 199 ", &arg, xdr_%s, %s%s, TIMEOUT) != RPC_SUCCESS) {\n", 200 stringfix(proc->res_type), 201 ampr(proc->res_type), RESULT); 202 } else { /* single argument, new or old style */ 203 fprintf(fout, 204 "\tif (clnt_call(clnt, %s, xdr_%s, %s%s, xdr_%s, " 205 "%s%s, TIMEOUT) != RPC_SUCCESS) {\n", 206 proc->proc_name, 207 stringfix(proc->args.decls->decl.type), 208 (newstyle ? "&" : ""), 209 (newstyle ? proc->args.decls->decl.name : "argp"), 210 stringfix(proc->res_type), 211 ampr(proc->res_type),RESULT); 212 } 213 fprintf(fout, "\t\treturn (NULL);\n"); 214 fprintf(fout, "\t}\n"); 215 if (streq(proc->res_type, "void")) { 216 fprintf(fout, "\treturn ((void *)%s%s);\n", 217 ampr(proc->res_type),RESULT); 218 } else { 219 fprintf(fout, "\treturn (%s%s);\n", ampr(proc->res_type),RESULT); 220 } 221 } 222 223