Home | History | Annotate | Line # | Download | only in rpcgen
rpc_svcout.c revision 1.5
      1 /* @(#)rpc_svcout.c	2.1 88/08/01 4.0 RPCSRC */
      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.
      9  *
     10  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
     11  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
     12  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
     13  *
     14  * Sun RPC is provided with no support and without any obligation on the
     15  * part of Sun Microsystems, Inc. to assist in its use, correction,
     16  * modification or enhancement.
     17  *
     18  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
     19  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
     20  * OR ANY PART THEREOF.
     21  *
     22  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
     23  * or profits or other special, indirect and consequential damages, even if
     24  * Sun has been advised of the possibility of such damages.
     25  *
     26  * Sun Microsystems, Inc.
     27  * 2550 Garcia Avenue
     28  * Mountain View, California  94043
     29  */
     30 #ifndef lint
     31 /*static char sccsid[] = "from: @(#)rpc_svcout.c 1.6 87/06/24 (C) 1987 SMI";*/
     32 static char rcsid[] = "$Id: rpc_svcout.c,v 1.5 1995/03/06 04:59:27 cgd Exp $";
     33 #endif
     34 
     35 /*
     36  * rpc_svcout.c, Server-skeleton outputter for the RPC protocol compiler
     37  * Copyright (C) 1987, Sun Microsytsems, Inc.
     38  */
     39 #include <stdio.h>
     40 #include <strings.h>
     41 #include "rpc_parse.h"
     42 #include "rpc_util.h"
     43 
     44 static char RQSTP[] = "rqstp";
     45 static char TRANSP[] = "transp";
     46 static char ARG[] = "argument";
     47 static char RESULT[] = "result";
     48 static char ROUTINE[] = "local";
     49 
     50 static int write_program(), printerr(), printif();
     51 /*
     52  * write most of the service, that is, everything but the registrations.
     53  */
     54 void
     55 write_most()
     56 {
     57 	list *l;
     58 	definition *def;
     59 	version_list *vp;
     60 
     61 	for (l = defined; l != NULL; l = l->next) {
     62 		def = (definition *) l->val;
     63 		if (def->def_kind == DEF_PROGRAM) {
     64 			for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
     65 				f_print(fout, "\nstatic void ");
     66 				pvname(def->def_name, vp->vers_num);
     67 				f_print(fout, "();");
     68 			}
     69 		}
     70 	}
     71 	f_print(fout, "\n\n");
     72 	f_print(fout, "main()\n");
     73 	f_print(fout, "{\n");
     74 	f_print(fout, "\tSVCXPRT *%s;\n", TRANSP);
     75 	f_print(fout, "\n");
     76 	for (l = defined; l != NULL; l = l->next) {
     77 		def = (definition *) l->val;
     78 		if (def->def_kind != DEF_PROGRAM) {
     79 			continue;
     80 		}
     81 		for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
     82  			f_print(fout, "\t(void)pmap_unset(%s, %s);\n", def->def_name, vp->vers_name);
     83 		}
     84 	}
     85 }
     86 
     87 
     88 /*
     89  * write a registration for the given transport
     90  */
     91 void
     92 write_register(transp)
     93 	char *transp;
     94 {
     95 	list *l;
     96 	definition *def;
     97 	version_list *vp;
     98 
     99 	f_print(fout, "\n");
    100 	f_print(fout, "\t%s = svc%s_create(RPC_ANYSOCK", TRANSP, transp);
    101 	if (streq(transp, "tcp")) {
    102 		f_print(fout, ", 0, 0");
    103 	}
    104 	f_print(fout, ");\n");
    105 	f_print(fout, "\tif (%s == NULL) {\n", TRANSP);
    106  	f_print(fout, "\t\t(void)fprintf(stderr, \"cannot create %s service.\\n\");\n", transp);
    107 	f_print(fout, "\t\texit(1);\n");
    108 	f_print(fout, "\t}\n");
    109 
    110 	for (l = defined; l != NULL; l = l->next) {
    111 		def = (definition *) l->val;
    112 		if (def->def_kind != DEF_PROGRAM) {
    113 			continue;
    114 		}
    115 		for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
    116 			f_print(fout,
    117 				"\tif (!svc_register(%s, %s, %s, ",
    118 				TRANSP, def->def_name, vp->vers_name);
    119 			pvname(def->def_name, vp->vers_num);
    120 			f_print(fout, ", IPPROTO_%s)) {\n",
    121 				streq(transp, "udp") ? "UDP" : "TCP");
    122 			f_print(fout,
    123  				"\t\t(void)fprintf(stderr, \"unable to register (%s, %s, %s).\\n\");\n",
    124 				def->def_name, vp->vers_name, transp);
    125 			f_print(fout, "\t\texit(1);\n");
    126 			f_print(fout, "\t}\n");
    127 		}
    128 	}
    129 }
    130 
    131 
    132 /*
    133  * write the rest of the service
    134  */
    135 void
    136 write_rest()
    137 {
    138 	f_print(fout, "\tsvc_run();\n");
    139  	f_print(fout, "\t(void)fprintf(stderr, \"svc_run returned\\n\");\n");
    140 	f_print(fout, "\texit(1);\n");
    141 	f_print(fout, "}\n");
    142 }
    143 
    144 void
    145 write_programs(storage)
    146 	char *storage;
    147 {
    148 	list *l;
    149 	definition *def;
    150 
    151 	for (l = defined; l != NULL; l = l->next) {
    152 		def = (definition *) l->val;
    153 		if (def->def_kind == DEF_PROGRAM) {
    154 			write_program(def, storage);
    155 		}
    156 	}
    157 }
    158 
    159 
    160 static
    161 write_program(def, storage)
    162 	definition *def;
    163 	char *storage;
    164 {
    165 	version_list *vp;
    166 	proc_list *proc;
    167 	int filled;
    168 
    169 	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
    170 		f_print(fout, "\n");
    171 		if (storage != NULL) {
    172 			f_print(fout, "%s ", storage);
    173 		}
    174 		f_print(fout, "void\n");
    175 		pvname(def->def_name, vp->vers_num);
    176 		f_print(fout, "(%s, %s)\n", RQSTP, TRANSP);
    177 		f_print(fout, "	struct svc_req *%s;\n", RQSTP);
    178 		f_print(fout, "	SVCXPRT *%s;\n", TRANSP);
    179 		f_print(fout, "{\n");
    180 
    181 		filled = 0;
    182 		f_print(fout, "\tunion {\n");
    183 		for (proc = vp->procs; proc != NULL; proc = proc->next) {
    184 			if (streq(proc->arg_type, "void")) {
    185 				continue;
    186 			}
    187 			filled = 1;
    188 			f_print(fout, "\t\t");
    189 			ptype(proc->arg_prefix, proc->arg_type, 0);
    190 			pvname(proc->proc_name, vp->vers_num);
    191 			f_print(fout, "_arg;\n");
    192 		}
    193 		if (!filled) {
    194 			f_print(fout, "\t\tint fill;\n");
    195 		}
    196 		f_print(fout, "\t} %s;\n", ARG);
    197 		f_print(fout, "\tchar *%s;\n", RESULT);
    198 		f_print(fout, "\tbool_t (*xdr_%s)(), (*xdr_%s)();\n", ARG, RESULT);
    199 		f_print(fout, "\tchar *(*%s)();\n", ROUTINE);
    200 		f_print(fout, "\n");
    201 		f_print(fout, "\tswitch (%s->rq_proc) {\n", RQSTP);
    202 
    203 		if (!nullproc(vp->procs)) {
    204 			f_print(fout, "\tcase NULLPROC:\n");
    205  			f_print(fout, "\t\t(void)svc_sendreply(%s, xdr_void, (char *)NULL);\n", TRANSP);
    206 			f_print(fout, "\t\treturn;\n\n");
    207 		}
    208 		for (proc = vp->procs; proc != NULL; proc = proc->next) {
    209 			f_print(fout, "\tcase %s:\n", proc->proc_name);
    210 			f_print(fout, "\t\txdr_%s = xdr_%s;\n", ARG,
    211 				stringfix(proc->arg_type));
    212 			f_print(fout, "\t\txdr_%s = xdr_%s;\n", RESULT,
    213 				stringfix(proc->res_type));
    214 			f_print(fout, "\t\t%s = (char *(*)()) ", ROUTINE);
    215 			pvname(proc->proc_name, vp->vers_num);
    216 			f_print(fout, ";\n");
    217 			f_print(fout, "\t\tbreak;\n\n");
    218 		}
    219 		f_print(fout, "\tdefault:\n");
    220 		printerr("noproc", TRANSP);
    221 		f_print(fout, "\t\treturn;\n");
    222 		f_print(fout, "\t}\n");
    223 
    224  		f_print(fout, "\tmemset((char *)&%s, 0, sizeof(%s));\n", ARG,
    225 			ARG);
    226 		printif("getargs", TRANSP, "(caddr_t)&", ARG);
    227 		printerr("decode", TRANSP);
    228 		f_print(fout, "\t\treturn;\n");
    229 		f_print(fout, "\t}\n");
    230 
    231 		f_print(fout, "\t%s = (*%s)(&%s, %s);\n", RESULT, ROUTINE, ARG,
    232 			RQSTP);
    233 		f_print(fout,
    234 			"\tif (%s != NULL && !svc_sendreply(%s, xdr_%s, %s)) {\n",
    235 			RESULT, TRANSP, RESULT, RESULT);
    236 		printerr("systemerr", TRANSP);
    237 		f_print(fout, "\t}\n");
    238 
    239 		printif("freeargs", TRANSP, "(caddr_t)&", ARG);
    240  		f_print(fout, "\t\t(void)fprintf(stderr, \"unable to free arguments\\n\");\n");
    241 		f_print(fout, "\t\texit(1);\n");
    242 		f_print(fout, "\t}\n");
    243 
    244 		f_print(fout, "}\n\n");
    245 	}
    246 }
    247 
    248 static
    249 printerr(err, transp)
    250 	char *err;
    251 	char *transp;
    252 {
    253 	f_print(fout, "\t\tsvcerr_%s(%s);\n", err, transp);
    254 }
    255 
    256 static
    257 printif(proc, transp, prefix, arg)
    258 	char *proc;
    259 	char *transp;
    260 	char *prefix;
    261 	char *arg;
    262 {
    263 	f_print(fout, "\tif (!svc_%s(%s, xdr_%s, %s%s)) {\n",
    264 		proc, transp, arg, prefix, arg);
    265 }
    266 
    267 
    268 nullproc(proc)
    269 	proc_list *proc;
    270 {
    271 	for (; proc != NULL; proc = proc->next) {
    272 		if (streq(proc->proc_num, "0")) {
    273 			return (1);
    274 		}
    275 	}
    276 	return (0);
    277 }
    278