1 /* $OpenBSD: rpc_sample.c,v 1.15 2007/10/03 14:35:48 weingart Exp $ */ 2 /* $NetBSD: rpc_sample.c,v 1.2 1995/06/11 21:50:01 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_sample.c 1.1 90/08/30 (C) 1987 SMI"; 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 static void write_sample_client(char *, version_list *); 50 static void write_sample_server(definition *); 51 static void return_type(proc_list *); 52 53 void 54 write_sample_svc(def) 55 definition *def; 56 { 57 58 if (def->def_kind != DEF_PROGRAM) 59 return; 60 write_sample_server(def); 61 } 62 63 64 int 65 write_sample_clnt(def) 66 definition *def; 67 { 68 version_list *vp; 69 int count = 0; 70 71 if (def->def_kind != DEF_PROGRAM) 72 return(0); 73 /* generate sample code for each version */ 74 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) { 75 write_sample_client(def->def_name, vp); 76 ++count; 77 } 78 return(count); 79 } 80 81 82 static void 83 write_sample_client(program_name, vp) 84 char *program_name; 85 version_list *vp; 86 { 87 proc_list *proc; 88 int i; 89 decl_list *l; 90 91 fprintf(fout, "\n\nvoid\n"); 92 pvname(program_name, vp->vers_num); 93 if (Cflag) 94 fprintf(fout,"(char *host)\n{\n"); 95 else 96 fprintf(fout, "(host)\nchar *host;\n{\n"); 97 fprintf(fout, "\tCLIENT *clnt;\n"); 98 99 i = 0; 100 for (proc = vp->procs; proc != NULL; proc = proc->next) { 101 fprintf(fout, "\t"); 102 ptype(proc->res_prefix, proc->res_type, 1); 103 fprintf(fout, " *result_%d;\n",++i); 104 /* print out declarations for arguments */ 105 if (proc->arg_num < 2 && !newstyle) { 106 fprintf(fout, "\t"); 107 if (!streq(proc->args.decls->decl.type, "void")) 108 ptype(proc->args.decls->decl.prefix, 109 proc->args.decls->decl.type, 1); 110 else 111 fprintf(fout, "char *"); /* cannot have "void" type */ 112 fprintf(fout, " "); 113 pvname(proc->proc_name, vp->vers_num); 114 fprintf(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 fprintf(fout, "\t"); 118 ptype(l->decl.prefix, l->decl.type, 1); 119 fprintf(fout, " "); 120 pvname(proc->proc_name, vp->vers_num); 121 fprintf(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 fprintf(fout, "\tclnt = clnt_create(host, %s, %s, \"%s\");\n", 129 program_name, vp->vers_name, tirpcflag? "netpath" : "udp"); 130 fprintf(fout, "\tif (clnt == NULL) {\n"); 131 fprintf(fout, "\t\tclnt_pcreateerror(host);\n"); 132 fprintf(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 fprintf(fout, "\tresult_%d = ",++i); 138 pvname(proc->proc_name, vp->vers_num); 139 if (proc->arg_num < 2 && !newstyle) { 140 fprintf(fout, "("); 141 if (streq(proc->args.decls->decl.type, "void")) 142 fprintf(fout, "(void*)"); 143 fprintf(fout, "&"); 144 pvname(proc->proc_name, vp->vers_num); 145 fprintf(fout, "_arg, clnt);\n"); 146 } else if (streq(proc->args.decls->decl.type, "void")) { 147 fprintf(fout, "(clnt);\n"); 148 } else { 149 fprintf(fout, "("); 150 for (l = proc->args.decls; l != NULL; l = l->next) { 151 pvname(proc->proc_name, vp->vers_num); 152 fprintf(fout, "_%s, ", l->decl.name); 153 } 154 fprintf(fout, "clnt);\n"); 155 } 156 fprintf(fout, "\tif (result_%d == NULL) {\n", i); 157 fprintf(fout, "\t\tclnt_perror(clnt, \"call failed:\");\n"); 158 fprintf(fout, "\t}\n"); 159 } 160 161 fprintf(fout, "\tclnt_destroy(clnt);\n"); 162 fprintf(fout, "}\n"); 163 } 164 165 static void 166 write_sample_server(def) 167 definition *def; 168 { 169 version_list *vp; 170 proc_list *proc; 171 172 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) { 173 for (proc = vp->procs; proc != NULL; proc = proc->next) { 174 fprintf(fout, "\n"); 175 /* if (Cflag) 176 fprintf(fout, "extern \"C\"{\n"); 177 */ 178 return_type(proc); 179 fprintf(fout, "* \n"); 180 pvname_svc(proc->proc_name, vp->vers_num); 181 printarglist(proc, RQSTP, "struct svc_req *"); 182 183 fprintf(fout, "{\n"); 184 fprintf(fout, "\n\tstatic "); 185 if (!streq(proc->res_type, "void")) 186 return_type(proc); 187 else 188 fprintf(fout, "char*"); /* cannot have void type */ 189 fprintf(fout, " result;\n"); 190 fprintf(fout, 191 "\n\t/*\n\t * insert server code here\n\t */\n\n"); 192 if (!streq(proc->res_type, "void")) 193 fprintf(fout, "\treturn(&result);\n}\n"); 194 else /* cast back to void * */ 195 fprintf(fout, "\treturn((void*) &result);\n}\n"); 196 /* if (Cflag) 197 fprintf(fout, "}\n"); 198 */ 199 } 200 } 201 } 202 203 static void 204 return_type(plist) 205 proc_list *plist; 206 { 207 ptype(plist->res_prefix, plist->res_type, 1); 208 } 209 210 void 211 add_sample_msg(void) 212 { 213 fprintf(fout, "/*\n"); 214 fprintf(fout, " * This is sample code generated by rpcgen.\n"); 215 fprintf(fout, " * These are only templates and you can use them\n"); 216 fprintf(fout, " * as a guideline for developing your own functions.\n"); 217 fprintf(fout, " */\n\n"); 218 } 219 220 void 221 write_sample_clnt_main() 222 { 223 list *l; 224 definition *def; 225 version_list *vp; 226 227 fprintf(fout, "\n\n"); 228 if (Cflag) 229 fprintf(fout,"main(int argc, char *argv[])\n{\n"); 230 else 231 fprintf(fout, "main(argc, argv)\nint argc;\nchar *argv[];\n{\n"); 232 233 fprintf(fout, "\tchar *host;"); 234 fprintf(fout, "\n\n\tif (argc < 2) {"); 235 fprintf(fout, "\n\t\tprintf(\"usage: %%s server_host\\n\", argv[0]);\n"); 236 fprintf(fout, "\t\texit(1);\n\t}"); 237 fprintf(fout, "\n\thost = argv[1];\n"); 238 239 for (l = defined; l != NULL; l = l->next) { 240 def = l->val; 241 if (def->def_kind != DEF_PROGRAM) 242 continue; 243 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) { 244 fprintf(fout, "\t"); 245 pvname(def->def_name, vp->vers_num); 246 fprintf(fout, "(host);\n"); 247 } 248 } 249 fprintf(fout, "}\n"); 250 } 251