112795Swpaul/*
212795Swpaul * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
312795Swpaul * unrestricted use provided that this legend is included on all tape
412795Swpaul * media and as a part of the software program in whole or part.  Users
512795Swpaul * may copy or modify Sun RPC without charge, but are not authorized
612795Swpaul * to license or distribute it to anyone else except as part of a product or
712795Swpaul * program developed by the user.
8100441Scharnier *
912795Swpaul * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
1012795Swpaul * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
1112795Swpaul * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12100441Scharnier *
1312795Swpaul * Sun RPC is provided with no support and without any obligation on the
1412795Swpaul * part of Sun Microsystems, Inc. to assist in its use, correction,
1512795Swpaul * modification or enhancement.
16100441Scharnier *
1712795Swpaul * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
1812795Swpaul * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
1912795Swpaul * OR ANY PART THEREOF.
20100441Scharnier *
2112795Swpaul * In no event will Sun Microsystems, Inc. be liable for any lost revenue
2212795Swpaul * or profits or other special, indirect and consequential damages, even if
2312795Swpaul * Sun has been advised of the possibility of such damages.
24100441Scharnier *
2512795Swpaul * Sun Microsystems, Inc.
2612795Swpaul * 2550 Garcia Avenue
2712795Swpaul * Mountain View, California  94043
2812795Swpaul */
2912795Swpaul
3099979Salfred/* #pragma ident	"@(#)rpc_sample.c	1.9	94/04/25 SMI" */
3112795Swpaul
32100441Scharnier#include <sys/cdefs.h>
33100441Scharnier__FBSDID("$FreeBSD$");
34100441Scharnier
3512795Swpaul/*
3612795Swpaul * rpc_sample.c, Sample client-server code outputter for the RPC protocol compiler
3712795Swpaul * Copyright (C) 1987, Sun Microsystems, Inc.
3812795Swpaul */
3912795Swpaul
4012795Swpaul#include <stdio.h>
4112795Swpaul#include <string.h>
4212795Swpaul#include "rpc_parse.h"
43149682Sstefanf#include "rpc_scan.h"
4412795Swpaul#include "rpc_util.h"
4512795Swpaul
4612795Swpaul
4712795Swpaulstatic char RQSTP[] = "rqstp";
4812795Swpaul
49152398Sdwmalonestatic void write_sample_client(const char *, version_list * );
5092921Simpstatic void write_sample_server( definition * );
5192921Simpstatic void return_type( proc_list * );
5212795Swpaul
5312795Swpaulvoid
54152398Sdwmalonewrite_sample_svc(definition *def)
5512795Swpaul{
5612795Swpaul
57100441Scharnier	if (def->def_kind != DEF_PROGRAM)
5812795Swpaul	  return;
5912795Swpaul	write_sample_server(def);
6012795Swpaul}
6112795Swpaul
6212795Swpaul
6312795Swpaulint
64152398Sdwmalonewrite_sample_clnt(definition *def)
6512795Swpaul{
6612795Swpaul        version_list *vp;
6712795Swpaul	int count = 0;
6812795Swpaul
69100441Scharnier	if (def->def_kind != DEF_PROGRAM)
7012795Swpaul	  return(0);
7112795Swpaul	/* generate sample code for each version */
7212795Swpaul	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
7312795Swpaul	  write_sample_client(def->def_name, vp);
7412795Swpaul	  ++count;
7512795Swpaul	}
7612795Swpaul	return(count);
7712795Swpaul}
7812795Swpaul
7912795Swpaul
8017142Sjkhstatic void
81152398Sdwmalonewrite_sample_client(const char *program_name, version_list *vp)
8212795Swpaul{
8312795Swpaul	proc_list *proc;
8412795Swpaul	int i;
8512795Swpaul	decl_list *l;
8612795Swpaul
8712795Swpaul	f_print(fout, "\n\nvoid\n");
8812795Swpaul	pvname(program_name, vp->vers_num);
89149709Sstefanf	f_print(fout, "(char *host)\n{\n");
9012795Swpaul	f_print(fout, "\tCLIENT *clnt;\n");
9112795Swpaul
9212795Swpaul	i = 0;
9312795Swpaul	for (proc = vp->procs; proc != NULL; proc = proc->next) {
9412795Swpaul		f_print(fout, "\t");
9512795Swpaul		if (mtflag) {
9612795Swpaul			f_print(fout, "enum clnt_stat retval_%d;\n\t", ++i);
9712795Swpaul			ptype(proc->res_prefix, proc->res_type, 1);
9812795Swpaul			f_print(fout, "result_%d;\n", i);
9912795Swpaul		} else {
10012795Swpaul			ptype(proc->res_prefix, proc->res_type, 1);
10112795Swpaul			f_print(fout, " *result_%d;\n",++i);
10212795Swpaul		}
10312795Swpaul		/* print out declarations for arguments */
10412795Swpaul		if(proc->arg_num < 2 && !newstyle) {
10512795Swpaul			f_print(fout, "\t");
10612795Swpaul			if(!streq(proc->args.decls->decl.type, "void"))
107100441Scharnier				ptype(proc->args.decls->decl.prefix,
10812795Swpaul				      proc->args.decls->decl.type, 1);
10912795Swpaul			else
11012795Swpaul				f_print(fout, "char * "); /* cannot have "void" type */
11112795Swpaul			f_print(fout, " ");
11212795Swpaul			pvname(proc->proc_name, vp->vers_num);
11312795Swpaul			f_print(fout, "_arg;\n");
11412795Swpaul		} else if (!streq(proc->args.decls->decl.type, "void")) {
11512795Swpaul			for (l = proc->args.decls; l != NULL; l = l->next) {
11612795Swpaul				f_print(fout, "\t");
11712795Swpaul				ptype(l->decl.prefix, l->decl.type, 1);
11812795Swpaul				if (strcmp(l->decl.type,"string") == 1)
11912795Swpaul				    f_print(fout, " ");
12012795Swpaul				pvname(proc->proc_name, vp->vers_num);
12112795Swpaul				f_print(fout, "_%s;\n", l->decl.name);
12212795Swpaul			}
12312795Swpaul		}
12412795Swpaul	}
12512795Swpaul
12612795Swpaul	/* generate creation of client handle */
12712795Swpaul	f_print(fout, "\n#ifndef\tDEBUG\n");
128100441Scharnier	f_print(fout, "\tclnt = clnt_create(host, %s, %s, \"%s\");\n",
12912795Swpaul		program_name, vp->vers_name, tirpcflag? "netpath" : "udp");
13012795Swpaul	f_print(fout, "\tif (clnt == (CLIENT *) NULL) {\n");
13112795Swpaul	f_print(fout, "\t\tclnt_pcreateerror(host);\n");
13212795Swpaul	f_print(fout, "\t\texit(1);\n\t}\n");
13312795Swpaul	f_print(fout, "#endif\t/* DEBUG */\n\n");
13412795Swpaul
13512795Swpaul	/* generate calls to procedures */
13612795Swpaul	i = 0;
13712795Swpaul	for (proc = vp->procs; proc != NULL; proc = proc->next) {
13812795Swpaul		if (mtflag)
139100441Scharnier			f_print(fout, "\tretval_%d = ",++i);
14012795Swpaul		else
141100441Scharnier			f_print(fout, "\tresult_%d = ",++i);
14212795Swpaul		pvname(proc->proc_name, vp->vers_num);
14312795Swpaul		if (proc->arg_num < 2 && !newstyle) {
14412795Swpaul			f_print(fout, "(");
145100441Scharnier			if(streq(proc->args.decls->decl.type, "void"))
14612795Swpaul				/* cast to void * */
14712795Swpaul				f_print(fout, "(void *)");
14812795Swpaul			f_print(fout, "&");
14912795Swpaul			pvname(proc->proc_name, vp->vers_num);
15012795Swpaul			if (mtflag)
15112795Swpaul				f_print(fout, "_arg, &result_%d, clnt);\n",
15212795Swpaul					i);
15312795Swpaul			else
15412795Swpaul				f_print(fout, "_arg, clnt);\n");
15512795Swpaul
15612795Swpaul		} else if (streq(proc->args.decls->decl.type, "void")) {
15712795Swpaul			if (mtflag)
15812795Swpaul				f_print(fout, "(&result_%d, clnt);\n", i);
15912795Swpaul			else
16012795Swpaul				f_print(fout, "(clnt);\n");
16112795Swpaul		}
16212795Swpaul		else {
16312795Swpaul			f_print(fout, "(");
16412795Swpaul			for (l = proc->args.decls;  l != NULL; l = l->next) {
16512795Swpaul				pvname(proc->proc_name, vp->vers_num);
16612795Swpaul				f_print(fout, "_%s, ", l->decl.name);
16712795Swpaul			}
16812795Swpaul			if (mtflag)
16912795Swpaul				f_print(fout, "&result_%d, ", i);
17012795Swpaul
17112795Swpaul			f_print(fout, "clnt);\n");
17212795Swpaul		}
17312795Swpaul		if (mtflag) {
17412795Swpaul			f_print(fout, "\tif (retval_%d != RPC_SUCCESS) {\n", i);
17512795Swpaul
17612795Swpaul		} else {
17712795Swpaul			f_print(fout, "\tif (result_%d == (", i);
17812795Swpaul			ptype(proc->res_prefix, proc->res_type, 1);
17912795Swpaul			f_print(fout, "*) NULL) {\n");
18012795Swpaul		}
18112795Swpaul		f_print(fout, "\t\tclnt_perror(clnt, \"call failed\");\n");
18212795Swpaul		f_print(fout, "\t}\n");
18312795Swpaul	}
18412795Swpaul
18512795Swpaul	f_print(fout, "#ifndef\tDEBUG\n");
18612795Swpaul	f_print(fout, "\tclnt_destroy(clnt);\n");
18712795Swpaul	f_print(fout, "#endif\t	/* DEBUG */\n");
18812795Swpaul	f_print(fout, "}\n");
18912795Swpaul}
19012795Swpaul
19117142Sjkhstatic void
192152398Sdwmalonewrite_sample_server(definition *def)
19312795Swpaul{
19412795Swpaul	version_list *vp;
19512795Swpaul	proc_list *proc;
19612795Swpaul
19712795Swpaul	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
19812795Swpaul		for (proc = vp->procs; proc != NULL; proc = proc->next) {
19912795Swpaul			f_print(fout, "\n");
20012795Swpaul			if (!mtflag) {
20112795Swpaul				return_type(proc);
20212795Swpaul				f_print(fout, "*\n");
20312795Swpaul			} else
20412795Swpaul				f_print(fout, "bool_t\n");
205149709Sstefanf			pvname_svc(proc->proc_name, vp->vers_num);
20612795Swpaul			printarglist(proc, "result", RQSTP, "struct svc_req *");
20712795Swpaul
20812795Swpaul			f_print(fout, "{\n");
20912795Swpaul			if (!mtflag) {
21012795Swpaul				f_print(fout, "\tstatic ");
21112795Swpaul				if(!streq(proc->res_type, "void"))
21212795Swpaul					return_type(proc);
21312795Swpaul				else
21412795Swpaul					f_print(fout, "char *");
21512795Swpaul				/* cannot have void type */
21638022Sbde				f_print(fout, " result;\n");
21712795Swpaul			}
21812795Swpaul			else
21912795Swpaul				f_print(fout, "\tbool_t retval;\n");
220100441Scharnier			f_print(fout,
22112795Swpaul				"\n\t/*\n\t * insert server code here\n\t */\n\n");
22212795Swpaul
22312795Swpaul			if (!mtflag)
22412795Swpaul				if(!streq(proc->res_type, "void"))
22512795Swpaul					f_print(fout, "\treturn (&result);\n}\n");
22612795Swpaul				else /* cast back to void * */
227100441Scharnier					f_print(fout, "\treturn((void *) &result);\n}\n");
22812795Swpaul			else
22912795Swpaul				f_print(fout, "\treturn (retval);\n}\n");
23012795Swpaul		}
23112795Swpaul		/* put in sample freeing routine */
23212795Swpaul		if (mtflag) {
23312795Swpaul		f_print(fout, "\nint\n");
23412795Swpaul		pvname(def->def_name, vp->vers_num);
235149709Sstefanf		f_print(fout,"_freeresult(SVCXPRT *transp, xdrproc_t xdr_result, caddr_t result)\n");
23612795Swpaul		f_print(fout, "{\n");
23712795Swpaul		f_print(fout, "\t(void) xdr_free(xdr_result, result);\n");
238100441Scharnier		f_print(fout,
23912795Swpaul			"\n\t/*\n\t * Insert additional freeing code here, if needed\n\t */\n");
24012795Swpaul		f_print(fout, "\n}\n");
24112795Swpaul
24212795Swpaul
24312795Swpaul	}
24412795Swpaul	}
24512795Swpaul}
24612795Swpaul
24712795Swpaul
24812795Swpaul
24917142Sjkhstatic void
250152398Sdwmalonereturn_type(proc_list *plist)
25112795Swpaul{
25212795Swpaul  ptype(plist->res_prefix, plist->res_type, 1);
25312795Swpaul}
25412795Swpaul
25517142Sjkhvoid
256152398Sdwmaloneadd_sample_msg(void)
25712795Swpaul{
25812795Swpaul	f_print(fout, "/*\n");
25912795Swpaul	f_print(fout, " * This is sample code generated by rpcgen.\n");
26012795Swpaul	f_print(fout, " * These are only templates and you can use them\n");
26112795Swpaul	f_print(fout, " * as a guideline for developing your own functions.\n");
26212795Swpaul	f_print(fout, " */\n\n");
26312795Swpaul}
26412795Swpaul
26512795Swpaulvoid
266152398Sdwmalonewrite_sample_clnt_main(void)
26712795Swpaul{
26812795Swpaul	list *l;
26912795Swpaul	definition *def;
27012795Swpaul	version_list *vp;
27112795Swpaul
27212795Swpaul	f_print(fout, "\n\n");
273267444Sbdrewery	f_print(fout, "int\n");
274149709Sstefanf	f_print(fout, "main(int argc, char *argv[])\n{\n");
27512795Swpaul
27612795Swpaul	f_print(fout, "\tchar *host;");
27712795Swpaul	f_print(fout, "\n\n\tif (argc < 2) {");
27812795Swpaul	f_print(fout, "\n\t\tprintf(\"usage:  %%s server_host\\n\", argv[0]);\n");
27912795Swpaul	f_print(fout, "\t\texit(1);\n\t}");
28012795Swpaul	f_print(fout, "\n\thost = argv[1];\n");
28112795Swpaul
28212795Swpaul	for (l = defined; l != NULL; l = l->next) {
28312795Swpaul		def = l->val;
28412795Swpaul		if (def->def_kind != DEF_PROGRAM) {
28512795Swpaul			continue;
28612795Swpaul		}
28712795Swpaul		for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
28812795Swpaul		        f_print(fout, "\t");
28912795Swpaul			pvname(def->def_name, vp->vers_num);
29012795Swpaul			f_print(fout, "(host);\n");
29112795Swpaul		}
29212795Swpaul	}
29312795Swpaul	f_print(fout, "}\n");
29412795Swpaul}
295