rpc_svcout.c revision 8874
11897Swollman/* @(#)rpc_svcout.c	2.1 88/08/01 4.0 RPCSRC */
21897Swollman/*
31897Swollman * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
41897Swollman * unrestricted use provided that this legend is included on all tape
51897Swollman * media and as a part of the software program in whole or part.  Users
61897Swollman * may copy or modify Sun RPC without charge, but are not authorized
71897Swollman * to license or distribute it to anyone else except as part of a product or
81897Swollman * program developed by the user.
98874Srgrimes *
101897Swollman * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
111897Swollman * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
121897Swollman * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
138874Srgrimes *
141897Swollman * Sun RPC is provided with no support and without any obligation on the
151897Swollman * part of Sun Microsystems, Inc. to assist in its use, correction,
161897Swollman * modification or enhancement.
178874Srgrimes *
181897Swollman * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
191897Swollman * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
201897Swollman * OR ANY PART THEREOF.
218874Srgrimes *
221897Swollman * In no event will Sun Microsystems, Inc. be liable for any lost revenue
231897Swollman * or profits or other special, indirect and consequential damages, even if
241897Swollman * Sun has been advised of the possibility of such damages.
258874Srgrimes *
261897Swollman * Sun Microsystems, Inc.
271897Swollman * 2550 Garcia Avenue
281897Swollman * Mountain View, California  94043
291897Swollman */
301897Swollman#ifndef lint
311897Swollman/*static char sccsid[] = "from: @(#)rpc_svcout.c 1.6 87/06/24 (C) 1987 SMI";*/
328874Srgrimesstatic char rcsid[] = "$Id: rpc_svcout.c,v 1.2 1995/03/04 17:47:50 nate Exp $";
331897Swollman#endif
341897Swollman
351897Swollman/*
361897Swollman * rpc_svcout.c, Server-skeleton outputter for the RPC protocol compiler
378874Srgrimes * Copyright (C) 1987, Sun Microsytsems, Inc.
381897Swollman */
391897Swollman#include <stdio.h>
401897Swollman#include <strings.h>
411897Swollman#include "rpc_parse.h"
421897Swollman#include "rpc_util.h"
431897Swollman
441897Swollmanstatic char RQSTP[] = "rqstp";
451897Swollmanstatic char TRANSP[] = "transp";
461897Swollmanstatic char ARG[] = "argument";
471897Swollmanstatic char RESULT[] = "result";
481897Swollmanstatic char ROUTINE[] = "local";
491897Swollman
501897Swollmanstatic int write_program(), printerr(), printif();
511897Swollman/*
528874Srgrimes * write most of the service, that is, everything but the registrations.
531897Swollman */
541897Swollmanvoid
551897Swollmanwrite_most()
561897Swollman{
571897Swollman	list *l;
581897Swollman	definition *def;
591897Swollman	version_list *vp;
601897Swollman
611897Swollman	for (l = defined; l != NULL; l = l->next) {
621897Swollman		def = (definition *) l->val;
631897Swollman		if (def->def_kind == DEF_PROGRAM) {
641897Swollman			for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
651897Swollman				f_print(fout, "\nstatic void ");
661897Swollman				pvname(def->def_name, vp->vers_num);
671897Swollman				f_print(fout, "();");
681897Swollman			}
691897Swollman		}
701897Swollman	}
711897Swollman	f_print(fout, "\n\n");
721897Swollman	f_print(fout, "main()\n");
731897Swollman	f_print(fout, "{\n");
741897Swollman	f_print(fout, "\tSVCXPRT *%s;\n", TRANSP);
751897Swollman	f_print(fout, "\n");
761897Swollman	for (l = defined; l != NULL; l = l->next) {
771897Swollman		def = (definition *) l->val;
781897Swollman		if (def->def_kind != DEF_PROGRAM) {
791897Swollman			continue;
801897Swollman		}
811897Swollman		for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
821897Swollman 			f_print(fout, "\t(void)pmap_unset(%s, %s);\n", def->def_name, vp->vers_name);
831897Swollman		}
841897Swollman	}
851897Swollman}
861897Swollman
871897Swollman
881897Swollman/*
898874Srgrimes * write a registration for the given transport
901897Swollman */
911897Swollmanvoid
921897Swollmanwrite_register(transp)
931897Swollman	char *transp;
941897Swollman{
951897Swollman	list *l;
961897Swollman	definition *def;
971897Swollman	version_list *vp;
981897Swollman
991897Swollman	f_print(fout, "\n");
1001897Swollman	f_print(fout, "\t%s = svc%s_create(RPC_ANYSOCK", TRANSP, transp);
1011897Swollman	if (streq(transp, "tcp")) {
1021897Swollman		f_print(fout, ", 0, 0");
1031897Swollman	}
1041897Swollman	f_print(fout, ");\n");
1051897Swollman	f_print(fout, "\tif (%s == NULL) {\n", TRANSP);
1061897Swollman 	f_print(fout, "\t\t(void)fprintf(stderr, \"cannot create %s service.\\n\");\n", transp);
1071897Swollman	f_print(fout, "\t\texit(1);\n");
1081897Swollman	f_print(fout, "\t}\n");
1091897Swollman
1101897Swollman	for (l = defined; l != NULL; l = l->next) {
1111897Swollman		def = (definition *) l->val;
1121897Swollman		if (def->def_kind != DEF_PROGRAM) {
1131897Swollman			continue;
1141897Swollman		}
1151897Swollman		for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
1161897Swollman			f_print(fout,
1171897Swollman				"\tif (!svc_register(%s, %s, %s, ",
1181897Swollman				TRANSP, def->def_name, vp->vers_name);
1191897Swollman			pvname(def->def_name, vp->vers_num);
1201897Swollman			f_print(fout, ", IPPROTO_%s)) {\n",
1211897Swollman				streq(transp, "udp") ? "UDP" : "TCP");
1221897Swollman			f_print(fout,
1231897Swollman 				"\t\t(void)fprintf(stderr, \"unable to register (%s, %s, %s).\\n\");\n",
1241897Swollman				def->def_name, vp->vers_name, transp);
1251897Swollman			f_print(fout, "\t\texit(1);\n");
1261897Swollman			f_print(fout, "\t}\n");
1271897Swollman		}
1281897Swollman	}
1291897Swollman}
1301897Swollman
1311897Swollman
1321897Swollman/*
1338874Srgrimes * write the rest of the service
1341897Swollman */
1351897Swollmanvoid
1361897Swollmanwrite_rest()
1371897Swollman{
1381897Swollman	f_print(fout, "\tsvc_run();\n");
1391897Swollman 	f_print(fout, "\t(void)fprintf(stderr, \"svc_run returned\\n\");\n");
1401897Swollman	f_print(fout, "\texit(1);\n");
1411897Swollman	f_print(fout, "}\n");
1421897Swollman}
1431897Swollman
1441897Swollmanvoid
1451897Swollmanwrite_programs(storage)
1461897Swollman	char *storage;
1471897Swollman{
1481897Swollman	list *l;
1491897Swollman	definition *def;
1501897Swollman
1511897Swollman	for (l = defined; l != NULL; l = l->next) {
1521897Swollman		def = (definition *) l->val;
1531897Swollman		if (def->def_kind == DEF_PROGRAM) {
1541897Swollman			write_program(def, storage);
1551897Swollman		}
1561897Swollman	}
1571897Swollman}
1581897Swollman
1591897Swollman
1601897Swollmanstatic
1611897Swollmanwrite_program(def, storage)
1621897Swollman	definition *def;
1631897Swollman	char *storage;
1641897Swollman{
1651897Swollman	version_list *vp;
1661897Swollman	proc_list *proc;
1671897Swollman	int filled;
1681897Swollman
1691897Swollman	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
1701897Swollman		f_print(fout, "\n");
1711897Swollman		if (storage != NULL) {
1721897Swollman			f_print(fout, "%s ", storage);
1731897Swollman		}
1741897Swollman		f_print(fout, "void\n");
1751897Swollman		pvname(def->def_name, vp->vers_num);
1761897Swollman		f_print(fout, "(%s, %s)\n", RQSTP, TRANSP);
1771897Swollman		f_print(fout, "	struct svc_req *%s;\n", RQSTP);
1781897Swollman		f_print(fout, "	SVCXPRT *%s;\n", TRANSP);
1791897Swollman		f_print(fout, "{\n");
1801897Swollman
1811897Swollman		filled = 0;
1821897Swollman		f_print(fout, "\tunion {\n");
1831897Swollman		for (proc = vp->procs; proc != NULL; proc = proc->next) {
1841897Swollman			if (streq(proc->arg_type, "void")) {
1851897Swollman				continue;
1861897Swollman			}
1871897Swollman			filled = 1;
1881897Swollman			f_print(fout, "\t\t");
1891897Swollman			ptype(proc->arg_prefix, proc->arg_type, 0);
1901897Swollman			pvname(proc->proc_name, vp->vers_num);
1911897Swollman			f_print(fout, "_arg;\n");
1921897Swollman		}
1931897Swollman		if (!filled) {
1941897Swollman			f_print(fout, "\t\tint fill;\n");
1951897Swollman		}
1961897Swollman		f_print(fout, "\t} %s;\n", ARG);
1971897Swollman		f_print(fout, "\tchar *%s;\n", RESULT);
1981897Swollman		f_print(fout, "\tbool_t (*xdr_%s)(), (*xdr_%s)();\n", ARG, RESULT);
1991897Swollman		f_print(fout, "\tchar *(*%s)();\n", ROUTINE);
2001897Swollman		f_print(fout, "\n");
2011897Swollman		f_print(fout, "\tswitch (%s->rq_proc) {\n", RQSTP);
2021897Swollman
2031897Swollman		if (!nullproc(vp->procs)) {
2041897Swollman			f_print(fout, "\tcase NULLPROC:\n");
2051897Swollman 			f_print(fout, "\t\t(void)svc_sendreply(%s, xdr_void, (char *)NULL);\n", TRANSP);
2061897Swollman			f_print(fout, "\t\treturn;\n\n");
2071897Swollman		}
2081897Swollman		for (proc = vp->procs; proc != NULL; proc = proc->next) {
2091897Swollman			f_print(fout, "\tcase %s:\n", proc->proc_name);
2108874Srgrimes			f_print(fout, "\t\txdr_%s = xdr_%s;\n", ARG,
2111897Swollman				stringfix(proc->arg_type));
2128874Srgrimes			f_print(fout, "\t\txdr_%s = xdr_%s;\n", RESULT,
2131897Swollman				stringfix(proc->res_type));
2141897Swollman			f_print(fout, "\t\t%s = (char *(*)()) ", ROUTINE);
2151897Swollman			pvname(proc->proc_name, vp->vers_num);
2161897Swollman			f_print(fout, ";\n");
2171897Swollman			f_print(fout, "\t\tbreak;\n\n");
2181897Swollman		}
2191897Swollman		f_print(fout, "\tdefault:\n");
2201897Swollman		printerr("noproc", TRANSP);
2211897Swollman		f_print(fout, "\t\treturn;\n");
2221897Swollman		f_print(fout, "\t}\n");
2231897Swollman
2241897Swollman 		f_print(fout, "\tbzero((char *)&%s, sizeof(%s));\n", ARG, ARG);
2256886Snate		printif("getargs", TRANSP, "(caddr_t)&", ARG);
2261897Swollman		printerr("decode", TRANSP);
2271897Swollman		f_print(fout, "\t\treturn;\n");
2281897Swollman		f_print(fout, "\t}\n");
2291897Swollman
2301897Swollman		f_print(fout, "\t%s = (*%s)(&%s, %s);\n", RESULT, ROUTINE, ARG,
2311897Swollman			RQSTP);
2328874Srgrimes		f_print(fout,
2331897Swollman			"\tif (%s != NULL && !svc_sendreply(%s, xdr_%s, %s)) {\n",
2341897Swollman			RESULT, TRANSP, RESULT, RESULT);
2351897Swollman		printerr("systemerr", TRANSP);
2361897Swollman		f_print(fout, "\t}\n");
2371897Swollman
2386886Snate		printif("freeargs", TRANSP, "(caddr_t)&", ARG);
2391897Swollman 		f_print(fout, "\t\t(void)fprintf(stderr, \"unable to free arguments\\n\");\n");
2401897Swollman		f_print(fout, "\t\texit(1);\n");
2411897Swollman		f_print(fout, "\t}\n");
2421897Swollman
2431897Swollman		f_print(fout, "}\n\n");
2441897Swollman	}
2451897Swollman}
2461897Swollman
2471897Swollmanstatic
2481897Swollmanprinterr(err, transp)
2491897Swollman	char *err;
2501897Swollman	char *transp;
2511897Swollman{
2521897Swollman	f_print(fout, "\t\tsvcerr_%s(%s);\n", err, transp);
2531897Swollman}
2541897Swollman
2551897Swollmanstatic
2561897Swollmanprintif(proc, transp, prefix, arg)
2571897Swollman	char *proc;
2581897Swollman	char *transp;
2591897Swollman	char *prefix;
2601897Swollman	char *arg;
2611897Swollman{
2621897Swollman	f_print(fout, "\tif (!svc_%s(%s, xdr_%s, %s%s)) {\n",
2631897Swollman		proc, transp, arg, prefix, arg);
2641897Swollman}
2651897Swollman
2661897Swollman
2671897Swollmannullproc(proc)
2681897Swollman	proc_list *proc;
2691897Swollman{
2701897Swollman	for (; proc != NULL; proc = proc->next) {
2711897Swollman		if (streq(proc->proc_num, "0")) {
2721897Swollman			return (1);
2731897Swollman		}
2741897Swollman	}
2751897Swollman	return (0);
2761897Swollman}
277