testexecmgmt/ucc/BinInternal/rpcgen/rpc_svco.c
changeset 0 3da2a79470a7
equal deleted inserted replaced
-1:000000000000 0:3da2a79470a7
       
     1 /*********************************************************************
       
     2  * RPC for the Windows NT Operating System
       
     3  * 1993 by Martin F. Gergeleit
       
     4  * Users may use, copy or modify Sun RPC for the Windows NT Operating 
       
     5  * System according to the Sun copyright below.
       
     6  *
       
     7  * RPC for the Windows NT Operating System COMES WITH ABSOLUTELY NO 
       
     8  * WARRANTY, NOR WILL I BE LIABLE FOR ANY DAMAGES INCURRED FROM THE 
       
     9  * USE OF. USE ENTIRELY AT YOUR OWN RISK!!!
       
    10  *********************************************************************/
       
    11 
       
    12 /* @(#)rpc_svcout.c	2.1 88/08/01 4.0 RPCSRC */
       
    13 /*
       
    14  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
       
    15  * unrestricted use provided that this legend is included on all tape
       
    16  * media and as a part of the software program in whole or part.  Users
       
    17  * may copy or modify Sun RPC without charge, but are not authorized
       
    18  * to license or distribute it to anyone else except as part of a product or
       
    19  * program developed by the user.
       
    20  * 
       
    21  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
       
    22  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
       
    23  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
       
    24  * 
       
    25  * Sun RPC is provided with no support and without any obligation on the
       
    26  * part of Sun Microsystems, Inc. to assist in its use, correction,
       
    27  * modification or enhancement.
       
    28  * 
       
    29  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
       
    30  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
       
    31  * OR ANY PART THEREOF.
       
    32  * 
       
    33  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
       
    34  * or profits or other special, indirect and consequential damages, even if
       
    35  * Sun has been advised of the possibility of such damages.
       
    36  * 
       
    37  * Sun Microsystems, Inc.
       
    38  * 2550 Garcia Avenue
       
    39  * Mountain View, California  94043
       
    40  */
       
    41 //#ifndef lint
       
    42 //static char sccsid[] = "@(#)rpc_svcout.c 1.6 87/06/24 (C) 1987 SMI";
       
    43 //#endif
       
    44 
       
    45 /*
       
    46  * rpc_svcout.c, Server-skeleton outputter for the RPC protocol compiler
       
    47  * Copyright (C) 1987, Sun Microsytsems, Inc. 
       
    48  */
       
    49 #include <stdio.h>
       
    50 #include <stdlib.h>
       
    51 #include <string.h>
       
    52 #include "rpc_pars.h"
       
    53 #include "rpc_util.h"
       
    54 #include "ae_component_rpc.h"
       
    55 
       
    56 static char RQSTP[] = "rqstp";
       
    57 static char TRANSP[] = "transp";
       
    58 static char ARG[] = "argument";
       
    59 static char RESULT[] = "result";
       
    60 static char ROUTINE[] = "local";
       
    61 
       
    62 /* prototype */
       
    63 int nullproc( proc_list *proc );
       
    64 static void printerr( char *err, char *transp );
       
    65 static void printif( char *proc, char *transp, char *prefix, char *arg );
       
    66 
       
    67 /* implementation */
       
    68 static void
       
    69 write_program(def, storage)
       
    70 	definition *def;
       
    71 	char *storage;
       
    72 {
       
    73 	version_list *vp;
       
    74 	proc_list *proc;
       
    75 	int filled;
       
    76 
       
    77 #ifdef WIN32_TARGET
       
    78 	f_print(fout, "\n#ifdef MULTITHREAD\n");
       
    79 	f_print(fout, "\
       
    80 struct call_params {\n\
       
    81 	struct svc_req *rqstp;\n\
       
    82 	SVCXPRT *transp;\n\
       
    83 	void *arg;\n\
       
    84 	char *(*local)();\n\
       
    85 	bool_t (*xdr_argument)(), (*xdr_result)();\n\
       
    86 };\n\
       
    87 ");
       
    88 
       
    89 	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
       
    90 		if (storage != NULL) {
       
    91 			f_print(fout, "%s ", storage);
       
    92 		}
       
    93 		f_print(fout, "void ");
       
    94 		pvname(def->def_name, vp->vers_num);
       
    95 		f_print(fout, "_a ();\n");
       
    96 	}
       
    97 	f_print(fout, "#endif\n");
       
    98 		
       
    99 #endif
       
   100 
       
   101 	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
       
   102 		f_print(fout, "\n");
       
   103 
       
   104 		if (storage != NULL) {
       
   105 			f_print(fout, "%s ", storage);
       
   106 		}
       
   107 		f_print(fout, "void\n");
       
   108 		pvname(def->def_name, vp->vers_num);
       
   109 		f_print(fout, "(%s, %s)\n", RQSTP, TRANSP);
       
   110 		f_print(fout, "	struct svc_req *%s;\n", RQSTP);
       
   111 		f_print(fout, "	SVCXPRT *%s;\n", TRANSP);
       
   112 		f_print(fout, "{\n");
       
   113 
       
   114 		filled = 0;
       
   115 		f_print(fout, "\tunion {\n");
       
   116 		for (proc = vp->procs; proc != NULL; proc = proc->next) {
       
   117 			if (streq(proc->arg_type, "void")) {
       
   118 				continue;
       
   119 			}
       
   120 			filled = 1;
       
   121 			f_print(fout, "\t\t");
       
   122 			ptype(proc->arg_prefix, proc->arg_type, 0);
       
   123 			pvname(proc->proc_name, vp->vers_num);
       
   124 			f_print(fout, "_arg;\n");
       
   125 		}
       
   126 		if (!filled) {
       
   127 			f_print(fout, "\t\tint fill;\n");
       
   128 		}
       
   129 		f_print(fout, "\t} %s;\n", ARG);
       
   130 		f_print(fout, "\tchar *%s;\n", RESULT);
       
   131 		f_print(fout, "\tbool_t (*xdr_%s)(), (*xdr_%s)();\n", ARG, RESULT);
       
   132 		f_print(fout, "\tchar *(*%s)();\n", ROUTINE);
       
   133 		f_print(fout, "\n");
       
   134 
       
   135 #ifdef WIN32_TARGET
       
   136 		f_print(fout, "\n#ifdef MULTITHREAD\n");
       
   137 		f_print(fout, "\
       
   138 	DWORD TID = 0;\n\
       
   139 	HANDLE threadHandle = NULL;\n\
       
   140 	struct call_params	*params;\n");
       
   141 		f_print(fout, "#endif\n");
       
   142 		
       
   143 #endif
       
   144 
       
   145 		f_print(fout, "\tswitch (%s->rq_proc) {\n", RQSTP);
       
   146 
       
   147 		if (!nullproc(vp->procs)) {
       
   148 			f_print(fout, "\tcase NULLPROC:\n");
       
   149  			f_print(fout, "\t\t(void)svc_sendreply(%s, (void*)xdr_void, (char *)NULL);\n", TRANSP);
       
   150 			f_print(fout, "\t\treturn;\n\n");
       
   151 		}
       
   152 		for (proc = vp->procs; proc != NULL; proc = proc->next) {
       
   153 			f_print(fout, "\tcase %s:\n", proc->proc_name);
       
   154 			f_print(fout, "\t\txdr_%s = xdr_%s;\n", ARG, 
       
   155 				ae_extend_custom_types(stringfix(proc->arg_type)));
       
   156 			f_print(fout, "\t\txdr_%s = xdr_%s;\n", RESULT, 
       
   157 				ae_extend_custom_types(stringfix(proc->res_type)));
       
   158 			f_print(fout, "\t\t%s = (char *(*)()) ", ROUTINE);
       
   159 			pvname(proc->proc_name, vp->vers_num);
       
   160 			f_print(fout, ";\n");
       
   161 			f_print(fout, "\t\tbreak;\n\n");
       
   162 		}
       
   163 		f_print(fout, "\tdefault:\n");
       
   164 		printerr("noproc", TRANSP);
       
   165 		f_print(fout, "\t\treturn;\n");
       
   166 		f_print(fout, "\t}\n");
       
   167 
       
   168  		f_print(fout, "\tbzero((char *)&%s, sizeof(%s));\n", ARG, ARG);
       
   169 		printif("getargs", TRANSP, "&", ARG);
       
   170 		printerr("decode", TRANSP);
       
   171 		f_print(fout, "\t\treturn;\n");
       
   172 		f_print(fout, "\t}\n");
       
   173 
       
   174 #ifdef WIN32_TARGET
       
   175 		f_print(fout, "\n#ifdef MULTITHREAD\n");
       
   176 		f_print(fout, "\
       
   177 	params = (struct call_params*) malloc (sizeof(struct call_params));\n\
       
   178 \n\
       
   179 	params->%s = (struct svc_req*) malloc (sizeof(struct svc_req));\n\
       
   180 	bcopy(%s, params->rqstp, sizeof(struct svc_req));\n\
       
   181 \n\
       
   182 	params->transp = %s;\n\
       
   183 \n\
       
   184 	params->arg = malloc (sizeof(%s));\n\
       
   185 	bcopy(&%s, params->arg, sizeof(%s));\n\
       
   186 \n\
       
   187 	params->local = %s;\n\
       
   188 \n\
       
   189 	params->xdr_argument = xdr_%s;\n\
       
   190 	params->xdr_result = xdr_%s;\n\
       
   191 \n", RQSTP, RQSTP, TRANSP, ARG, ARG, ARG, ROUTINE, ARG, RESULT);
       
   192 
       
   193 
       
   194 		//		f_print(fout, "\threadHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)", RQSTP, TRANSP);
       
   195 		f_print(fout, "\tthreadHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)");
       
   196 
       
   197 		pvname(def->def_name, vp->vers_num);
       
   198 	
       
   199 		f_print(fout, "_a, params, 0, &TID);\n\
       
   200 	if (!threadHandle)\n\t\t");
       
   201 
       
   202 		pvname(def->def_name, vp->vers_num);
       
   203 
       
   204 		f_print(fout, "_a(&params);\n\
       
   205 }\n\
       
   206 \n\
       
   207 static void\n");
       
   208 
       
   209 		pvname(def->def_name, vp->vers_num);
       
   210 
       
   211 		f_print(fout, "_a(params)\n\
       
   212 struct call_params *params;\n\
       
   213 {\n\
       
   214 	struct svc_req *%s;\n\
       
   215 	SVCXPRT *%s;\n\
       
   216 	void *%s;\n", RQSTP, TRANSP, ARG);
       
   217 
       
   218 		f_print(fout, "\tvoid (*destroy_proc)();\n\n");
       
   219 		f_print(fout, "\tchar *%s;\n", RESULT);
       
   220 		f_print(fout, "\tbool_t (*xdr_%s)(), (*xdr_%s)();\n", ARG, RESULT);
       
   221 		f_print(fout, "\tchar *(*%s)();\n", ROUTINE);
       
   222 		f_print(fout, "\n\n");
       
   223 
       
   224 		f_print(fout, "\t%s = params->arg;\n", ARG);
       
   225 		f_print(fout, "\t%s = params->rqstp;\n", RQSTP);
       
   226 		f_print(fout, "\t%s = params->transp;\n", TRANSP);
       
   227 		f_print(fout, "\txdr_%s = params->xdr_argument;\n", ARG);
       
   228 		f_print(fout, "\txdr_%s = params->xdr_result;\n", RESULT);
       
   229 		f_print(fout, "\t%s = params->local;\n", ROUTINE);
       
   230 
       
   231 		f_print(fout, "\tdestroy_proc = %s->xp_ops->xp_destroy;\n", TRANSP);
       
   232 		f_print(fout, "\t%s->xp_ops->xp_destroy = xprt_unregister;\n", TRANSP);
       
   233 
       
   234 		f_print(fout, "#endif\n\n");
       
   235 #endif
       
   236 		f_print(fout, "\t%s = (*%s)(&%s, %s);\n", RESULT, ROUTINE, ARG,
       
   237 			RQSTP);
       
   238 		f_print(fout, 
       
   239 			"\tif (%s != NULL && !svc_sendreply(%s, (void*)xdr_%s, %s)) {\n",
       
   240 			RESULT, TRANSP, RESULT, RESULT);
       
   241 		printerr("systemerr", TRANSP);
       
   242 		f_print(fout, "\t}\n");
       
   243 
       
   244 #ifdef WIN32_TARGET
       
   245 		f_print(fout, "#ifdef MULTITHREAD\n");
       
   246 		f_print(fout, "\tfree(params->rqstp);\n");
       
   247 		f_print(fout, "\tfree(params);\n");
       
   248 		f_print(fout, "#endif\n");
       
   249 #endif
       
   250 
       
   251 		printif("freeargs", TRANSP, "&", ARG);
       
   252  		f_print(fout, "\t\t(void)fprintf(stderr, \"unable to free arguments\\n\");\n");
       
   253 #ifdef WIN32_TARGET
       
   254 		f_print(fout, "#ifdef WIN32\n\t\trpc_nt_exit();\n#endif\n");
       
   255 #endif
       
   256 		f_print(fout, "\t\texit(1);\n");
       
   257 		f_print(fout, "\t}\n");
       
   258 
       
   259 #ifdef WIN32_TARGET
       
   260 		f_print(fout, "#ifdef MULTITHREAD\n");
       
   261 		f_print(fout, "\tfree(%s);\n", ARG);
       
   262 		f_print(fout, "\ttransp->xp_ops->xp_destroy = destroy_proc;\n");
       
   263 		f_print(fout, "#endif\n");
       
   264 #endif
       
   265 
       
   266 		f_print(fout, "}\n\n");
       
   267 	}
       
   268 }
       
   269 
       
   270 static void
       
   271 printerr(err, transp)
       
   272 	char *err;
       
   273 	char *transp;
       
   274 {
       
   275 	f_print(fout, "\t\tsvcerr_%s(%s);\n", err, transp);
       
   276 }
       
   277 
       
   278 static void
       
   279 printif(proc, transp, prefix, arg)
       
   280 	char *proc;
       
   281 	char *transp;
       
   282 	char *prefix;
       
   283 	char *arg;
       
   284 {
       
   285 	f_print(fout, "\tif (!svc_%s(%s, (void*)xdr_%s, (void*)%s%s)) {\n",
       
   286 		proc, transp, arg, prefix, arg);
       
   287 }
       
   288 
       
   289 int
       
   290 nullproc(proc)
       
   291 	proc_list *proc;
       
   292 {
       
   293 	for (; proc != NULL; proc = proc->next) {
       
   294 		if (streq(proc->proc_num, "0")) {
       
   295 			return (1);
       
   296 		}
       
   297 	}
       
   298 	return (0);
       
   299 }
       
   300 
       
   301 
       
   302 /*
       
   303  * write most of the service, that is, everything but the registrations. 
       
   304  */
       
   305 void
       
   306 write_most()
       
   307 {
       
   308 	list *l;
       
   309 	definition *def;
       
   310 	version_list *vp;
       
   311 
       
   312 	for (l = defined; l != NULL; l = l->next) {
       
   313 		def = (definition *) l->val;
       
   314 		if (def->def_kind == DEF_PROGRAM) {
       
   315 			for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
       
   316 				f_print(fout, "\nstatic void ");
       
   317 				pvname(def->def_name, vp->vers_num);
       
   318 				f_print(fout, "();");
       
   319 			}
       
   320 		}
       
   321 	}
       
   322 	f_print(fout, "\n\n");
       
   323 	f_print(fout, "int main( void )\n");
       
   324 	f_print(fout, "{\n");
       
   325 	f_print(fout, "\tSVCXPRT *%s;\n", TRANSP);
       
   326 	f_print(fout, "\n");
       
   327 #ifdef WIN32_TARGET
       
   328 	f_print(fout, "#ifdef WIN32\n\trpc_nt_init();\n#endif\n\n");
       
   329 #endif
       
   330 	for (l = defined; l != NULL; l = l->next) {
       
   331 		def = (definition *) l->val;
       
   332 		if (def->def_kind != DEF_PROGRAM) {
       
   333 			continue;
       
   334 		}
       
   335 		for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
       
   336  			f_print(fout, "\t(void)pmap_unset(%s, %s);\n", def->def_name, vp->vers_name);
       
   337 		}
       
   338 	}
       
   339 }
       
   340 
       
   341 
       
   342 /*
       
   343  * write a registration for the given transport 
       
   344  */
       
   345 void
       
   346 write_register(transp)
       
   347 	char *transp;
       
   348 {
       
   349 	list *l;
       
   350 	definition *def;
       
   351 	version_list *vp;
       
   352 
       
   353 	f_print(fout, "\n");
       
   354 	f_print(fout, "\t%s = svc%s_create(RPC_ANYSOCK", TRANSP, transp);
       
   355 	if (streq(transp, "tcp")) {
       
   356 		f_print(fout, ", 0, 0");
       
   357 	}
       
   358 	f_print(fout, ");\n");
       
   359 	f_print(fout, "\tif (%s == NULL) {\n", TRANSP);
       
   360  	f_print(fout, "\t\t(void)fprintf(stderr, \"cannot create %s service.\\n\");\n", transp);
       
   361 #ifdef WIN32_TARGET
       
   362 	f_print(fout, "#ifdef WIN32\n\t\trpc_nt_exit();\n#endif\n");
       
   363 #endif
       
   364 	f_print(fout, "\t\texit(1);\n");
       
   365 	f_print(fout, "\t}\n");
       
   366 
       
   367 	for (l = defined; l != NULL; l = l->next) {
       
   368 		def = (definition *) l->val;
       
   369 		if (def->def_kind != DEF_PROGRAM) {
       
   370 			continue;
       
   371 		}
       
   372 		for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
       
   373 			f_print(fout,
       
   374 				"\tif (!svc_register(%s, %s, %s, ",
       
   375 				TRANSP, def->def_name, vp->vers_name);
       
   376 			pvname(def->def_name, vp->vers_num);
       
   377 			f_print(fout, ", IPPROTO_%s)) {\n",
       
   378 				streq(transp, "udp") ? "UDP" : "TCP");
       
   379 			f_print(fout,
       
   380  				"\t\t(void)fprintf(stderr, \"unable to register (%s, %s, %s).\\n\");\n",
       
   381 				def->def_name, vp->vers_name, transp);
       
   382 #ifdef WIN32_TARGET
       
   383 			f_print(fout, "#ifdef WIN32\n\t\trpc_nt_exit();\n#endif\n");
       
   384 #endif
       
   385 			f_print(fout, "\t\texit(1);\n");
       
   386 			f_print(fout, "\t}\n");
       
   387 		}
       
   388 	}
       
   389 }
       
   390 
       
   391 
       
   392 /*
       
   393  * write the rest of the service 
       
   394  */
       
   395 void
       
   396 write_rest()
       
   397 {
       
   398 	f_print(fout, "\tsvc_run();\n");
       
   399  	f_print(fout, "\t(void)fprintf(stderr, \"svc_run returned\\n\");\n");
       
   400 #ifdef WIN32_TARGET
       
   401 	f_print(fout, "#ifdef WIN32\n\trpc_nt_exit();\n#endif\n");
       
   402 #endif
       
   403 	f_print(fout, "\texit(1);\n");
       
   404 	f_print(fout, "}\n");
       
   405 }
       
   406 
       
   407 void
       
   408 write_programs(storage)
       
   409 	char *storage;
       
   410 {
       
   411 	list *l;
       
   412 	definition *def;
       
   413 
       
   414 	for (l = defined; l != NULL; l = l->next) {
       
   415 		def = (definition *) l->val;
       
   416 		if (def->def_kind == DEF_PROGRAM) {
       
   417 			write_program(def, storage);
       
   418 		}
       
   419 	}
       
   420 }