17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*a2f144d1SJordan Brown * Common Development and Distribution License (the "License").
6*a2f144d1SJordan Brown * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
2061961e0fSrobinson */
2161961e0fSrobinson
2261961e0fSrobinson /*
23*a2f144d1SJordan Brown * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
277c478bd9Sstevel@tonic-gate /* All Rights Reserved */
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
307c478bd9Sstevel@tonic-gate * The Regents of the University of California
317c478bd9Sstevel@tonic-gate * All Rights Reserved
327c478bd9Sstevel@tonic-gate *
337c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
347c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
357c478bd9Sstevel@tonic-gate * contributors.
367c478bd9Sstevel@tonic-gate */
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate * rpc_sample.c, Sample client-server code outputter
407c478bd9Sstevel@tonic-gate * for the RPC protocol compiler
417c478bd9Sstevel@tonic-gate */
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gate #include <stdio.h>
447c478bd9Sstevel@tonic-gate #include <string.h>
457c478bd9Sstevel@tonic-gate #include "rpc_parse.h"
467c478bd9Sstevel@tonic-gate #include "rpc_util.h"
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate static char RQSTP[] = "rqstp";
507c478bd9Sstevel@tonic-gate
5161961e0fSrobinson extern void printarglist(proc_list *, char *, char *, char *);
5261961e0fSrobinson
5361961e0fSrobinson static void write_sample_client(char *, version_list *);
5461961e0fSrobinson static void write_sample_server(definition *);
5561961e0fSrobinson static void return_type(proc_list *);
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate void
write_sample_svc(definition * def)5861961e0fSrobinson write_sample_svc(definition *def)
597c478bd9Sstevel@tonic-gate {
607c478bd9Sstevel@tonic-gate if (def->def_kind != DEF_PROGRAM)
617c478bd9Sstevel@tonic-gate return;
627c478bd9Sstevel@tonic-gate write_sample_server(def);
637c478bd9Sstevel@tonic-gate }
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate int
write_sample_clnt(definition * def)6661961e0fSrobinson write_sample_clnt(definition *def)
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate version_list *vp;
697c478bd9Sstevel@tonic-gate int count = 0;
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate if (def->def_kind != DEF_PROGRAM)
727c478bd9Sstevel@tonic-gate return (0);
737c478bd9Sstevel@tonic-gate /* generate sample code for each version */
747c478bd9Sstevel@tonic-gate for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
757c478bd9Sstevel@tonic-gate write_sample_client(def->def_name, vp);
767c478bd9Sstevel@tonic-gate ++count;
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate return (count);
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate
8161961e0fSrobinson static void
write_sample_client(char * program_name,version_list * vp)8261961e0fSrobinson write_sample_client(char *program_name, version_list *vp)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate proc_list *proc;
857c478bd9Sstevel@tonic-gate int i;
867c478bd9Sstevel@tonic-gate decl_list *l;
877c478bd9Sstevel@tonic-gate
887c478bd9Sstevel@tonic-gate f_print(fout, "\n\nvoid\n");
897c478bd9Sstevel@tonic-gate pvname(program_name, vp->vers_num);
907c478bd9Sstevel@tonic-gate if (Cflag)
917c478bd9Sstevel@tonic-gate f_print(fout, "(char *host)\n{\n");
927c478bd9Sstevel@tonic-gate else
937c478bd9Sstevel@tonic-gate f_print(fout, "(host)\n\tchar *host;\n{\n");
947c478bd9Sstevel@tonic-gate f_print(fout, "\tCLIENT *clnt;\n");
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate i = 0;
977c478bd9Sstevel@tonic-gate for (proc = vp->procs; proc != NULL; proc = proc->next) {
987c478bd9Sstevel@tonic-gate f_print(fout, "\t");
997c478bd9Sstevel@tonic-gate if (mtflag) {
1007c478bd9Sstevel@tonic-gate f_print(fout, "enum clnt_stat retval_%d;\n", ++i);
1017c478bd9Sstevel@tonic-gate if (!streq(proc->res_type, "oneway")) {
1027c478bd9Sstevel@tonic-gate f_print(fout, "\t");
1037c478bd9Sstevel@tonic-gate if (!streq(proc->res_type, "void"))
1047c478bd9Sstevel@tonic-gate ptype(proc->res_prefix,
1057c478bd9Sstevel@tonic-gate proc->res_type, 1);
1067c478bd9Sstevel@tonic-gate else
1077c478bd9Sstevel@tonic-gate f_print(fout, "void *");
1087c478bd9Sstevel@tonic-gate f_print(fout, "result_%d;\n", i);
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate } else {
1117c478bd9Sstevel@tonic-gate ptype(proc->res_prefix, proc->res_type, 1);
1127c478bd9Sstevel@tonic-gate f_print(fout, " *result_%d;\n", ++i);
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate /* print out declarations for arguments */
1157c478bd9Sstevel@tonic-gate if (proc->arg_num < 2 && !newstyle) {
1167c478bd9Sstevel@tonic-gate f_print(fout, "\t");
1177c478bd9Sstevel@tonic-gate if (!streq(proc->args.decls->decl.type, "void"))
1187c478bd9Sstevel@tonic-gate ptype(proc->args.decls->decl.prefix,
1197c478bd9Sstevel@tonic-gate proc->args.decls->decl.type, 1);
1207c478bd9Sstevel@tonic-gate else
1217c478bd9Sstevel@tonic-gate /* cannot have "void" type */
1227c478bd9Sstevel@tonic-gate f_print(fout, "char * ");
1237c478bd9Sstevel@tonic-gate f_print(fout, " ");
1247c478bd9Sstevel@tonic-gate pvname(proc->proc_name, vp->vers_num);
1257c478bd9Sstevel@tonic-gate f_print(fout, "_arg;\n");
1267c478bd9Sstevel@tonic-gate } else if (!streq(proc->args.decls->decl.type, "void")) {
1277c478bd9Sstevel@tonic-gate for (l = proc->args.decls; l != NULL; l = l->next) {
1287c478bd9Sstevel@tonic-gate f_print(fout, "\t");
1297c478bd9Sstevel@tonic-gate ptype(l->decl.prefix, l->decl.type, 1);
1307c478bd9Sstevel@tonic-gate if (strcmp(l->decl.type, "string") == 1)
1317c478bd9Sstevel@tonic-gate f_print(fout, " ");
1327c478bd9Sstevel@tonic-gate pvname(proc->proc_name, vp->vers_num);
1337c478bd9Sstevel@tonic-gate f_print(fout, "_%s;\n", l->decl.name);
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate /* generate creation of client handle */
1397c478bd9Sstevel@tonic-gate f_print(fout, "\n#ifndef\tDEBUG\n");
1407c478bd9Sstevel@tonic-gate f_print(fout, "\tclnt = clnt_create(host, %s, %s, \"%s\");\n",
1417c478bd9Sstevel@tonic-gate program_name, vp->vers_name, tirpcflag? "netpath" : "udp");
1427c478bd9Sstevel@tonic-gate f_print(fout, "\tif (clnt == (CLIENT *) NULL) {\n");
1437c478bd9Sstevel@tonic-gate f_print(fout, "\t\tclnt_pcreateerror(host);\n");
1447c478bd9Sstevel@tonic-gate f_print(fout, "\t\texit(1);\n\t}\n");
1457c478bd9Sstevel@tonic-gate f_print(fout, "#endif\t/* DEBUG */\n\n");
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate /* generate calls to procedures */
1487c478bd9Sstevel@tonic-gate i = 0;
1497c478bd9Sstevel@tonic-gate for (proc = vp->procs; proc != NULL; proc = proc->next) {
1507c478bd9Sstevel@tonic-gate if (mtflag)
1517c478bd9Sstevel@tonic-gate f_print(fout, "\tretval_%d = ", ++i);
1527c478bd9Sstevel@tonic-gate else
1537c478bd9Sstevel@tonic-gate f_print(fout, "\tresult_%d = ", ++i);
1547c478bd9Sstevel@tonic-gate pvname(proc->proc_name, vp->vers_num);
1557c478bd9Sstevel@tonic-gate if (proc->arg_num < 2 && !newstyle) {
1567c478bd9Sstevel@tonic-gate f_print(fout, "(");
1577c478bd9Sstevel@tonic-gate if (streq(proc->args.decls->decl.type, "void"))
1587c478bd9Sstevel@tonic-gate /* cast to void * */
1597c478bd9Sstevel@tonic-gate f_print(fout, "(void *)");
1607c478bd9Sstevel@tonic-gate f_print(fout, "&");
1617c478bd9Sstevel@tonic-gate pvname(proc->proc_name, vp->vers_num);
1627c478bd9Sstevel@tonic-gate if (mtflag) {
16361961e0fSrobinson if (streq(proc->res_type, "oneway"))
1647c478bd9Sstevel@tonic-gate f_print(fout, "_arg, clnt);\n");
16561961e0fSrobinson else
1667c478bd9Sstevel@tonic-gate f_print(fout,
1677c478bd9Sstevel@tonic-gate "_arg, &result_%d, clnt);\n", i);
1687c478bd9Sstevel@tonic-gate } else
1697c478bd9Sstevel@tonic-gate f_print(fout, "_arg, clnt);\n");
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate } else if (streq(proc->args.decls->decl.type, "void")) {
1727c478bd9Sstevel@tonic-gate if (mtflag) {
17361961e0fSrobinson if (streq(proc->res_type, "oneway"))
1747c478bd9Sstevel@tonic-gate f_print(fout, "(clnt);\n");
17561961e0fSrobinson else
1767c478bd9Sstevel@tonic-gate f_print(fout,
1777c478bd9Sstevel@tonic-gate "(&result_%d, clnt);\n", i);
1787c478bd9Sstevel@tonic-gate } else
1797c478bd9Sstevel@tonic-gate f_print(fout, "(clnt);\n");
1807c478bd9Sstevel@tonic-gate } else {
1817c478bd9Sstevel@tonic-gate f_print(fout, "(");
1827c478bd9Sstevel@tonic-gate for (l = proc->args.decls; l != NULL; l = l->next) {
1837c478bd9Sstevel@tonic-gate pvname(proc->proc_name, vp->vers_num);
1847c478bd9Sstevel@tonic-gate f_print(fout, "_%s, ", l->decl.name);
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate if (mtflag) {
18761961e0fSrobinson if (!streq(proc->res_type, "oneway"))
1887c478bd9Sstevel@tonic-gate f_print(fout, "&result_%d, ", i);
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate f_print(fout, "clnt);\n");
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate if (mtflag) {
1947c478bd9Sstevel@tonic-gate f_print(fout, "\tif (retval_%d != RPC_SUCCESS) {\n", i);
1957c478bd9Sstevel@tonic-gate } else {
1967c478bd9Sstevel@tonic-gate f_print(fout, "\tif (result_%d == (", i);
1977c478bd9Sstevel@tonic-gate ptype(proc->res_prefix, proc->res_type, 1);
1987c478bd9Sstevel@tonic-gate f_print(fout, "*) NULL) {\n");
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate f_print(fout, "\t\tclnt_perror(clnt, \"call failed\");\n");
2017c478bd9Sstevel@tonic-gate f_print(fout, "\t}\n");
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate
2047c478bd9Sstevel@tonic-gate f_print(fout, "#ifndef\tDEBUG\n");
2057c478bd9Sstevel@tonic-gate f_print(fout, "\tclnt_destroy(clnt);\n");
2067c478bd9Sstevel@tonic-gate f_print(fout, "#endif\t /* DEBUG */\n");
2077c478bd9Sstevel@tonic-gate f_print(fout, "}\n");
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate
21061961e0fSrobinson static void
write_sample_server(definition * def)21161961e0fSrobinson write_sample_server(definition *def)
2127c478bd9Sstevel@tonic-gate {
2137c478bd9Sstevel@tonic-gate version_list *vp;
2147c478bd9Sstevel@tonic-gate proc_list *proc;
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
2177c478bd9Sstevel@tonic-gate for (proc = vp->procs; proc != NULL; proc = proc->next) {
2187c478bd9Sstevel@tonic-gate f_print(fout, "\n");
2197c478bd9Sstevel@tonic-gate if (!mtflag) {
2207c478bd9Sstevel@tonic-gate return_type(proc);
2217c478bd9Sstevel@tonic-gate f_print(fout, "*\n");
2227c478bd9Sstevel@tonic-gate } else {
2237c478bd9Sstevel@tonic-gate f_print(fout, "bool_t\n");
2247c478bd9Sstevel@tonic-gate }
2257c478bd9Sstevel@tonic-gate if (Cflag || mtflag)
2267c478bd9Sstevel@tonic-gate pvname_svc(proc->proc_name, vp->vers_num);
2277c478bd9Sstevel@tonic-gate else
2287c478bd9Sstevel@tonic-gate pvname(proc->proc_name, vp->vers_num);
2297c478bd9Sstevel@tonic-gate printarglist(proc, "result", RQSTP, "struct svc_req *");
2307c478bd9Sstevel@tonic-gate
2317c478bd9Sstevel@tonic-gate f_print(fout, "{\n");
2327c478bd9Sstevel@tonic-gate
2337c478bd9Sstevel@tonic-gate if (!mtflag) {
2347c478bd9Sstevel@tonic-gate f_print(fout, "\tstatic ");
2357c478bd9Sstevel@tonic-gate if ((!streq(proc->res_type, "void")) &&
2367c478bd9Sstevel@tonic-gate (!streq(proc->res_type, "oneway")))
2377c478bd9Sstevel@tonic-gate return_type(proc);
2387c478bd9Sstevel@tonic-gate else
2397c478bd9Sstevel@tonic-gate f_print(fout, "char *");
2407c478bd9Sstevel@tonic-gate /* cannot have void type */
24161961e0fSrobinson f_print(fout, " result;\n");
2427c478bd9Sstevel@tonic-gate }
2437c478bd9Sstevel@tonic-gate
2447c478bd9Sstevel@tonic-gate f_print(fout, "\n\t/*\n\t * insert server code "
2457c478bd9Sstevel@tonic-gate "here\n\t */\n\n");
2467c478bd9Sstevel@tonic-gate
2477c478bd9Sstevel@tonic-gate if (!mtflag)
2487c478bd9Sstevel@tonic-gate if (!streq(proc->res_type, "void"))
2497c478bd9Sstevel@tonic-gate f_print(fout,
2507c478bd9Sstevel@tonic-gate "\treturn (&result);\n}\n");
2517c478bd9Sstevel@tonic-gate else /* cast back to void * */
2527c478bd9Sstevel@tonic-gate f_print(fout, "\treturn((void *) "
2537c478bd9Sstevel@tonic-gate "&result);\n}\n");
2547c478bd9Sstevel@tonic-gate else
2557c478bd9Sstevel@tonic-gate f_print(fout, "\treturn (retval);\n}\n");
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate /* put in sample freeing routine */
2587c478bd9Sstevel@tonic-gate if (mtflag) {
2597c478bd9Sstevel@tonic-gate f_print(fout, "\nint\n");
2607c478bd9Sstevel@tonic-gate pvname(def->def_name, vp->vers_num);
2617c478bd9Sstevel@tonic-gate if (Cflag)
2627c478bd9Sstevel@tonic-gate f_print(fout, "_freeresult(SVCXPRT *transp,"
2637c478bd9Sstevel@tonic-gate " xdrproc_t xdr_result,"
2647c478bd9Sstevel@tonic-gate " caddr_t result)\n");
2657c478bd9Sstevel@tonic-gate else {
2667c478bd9Sstevel@tonic-gate f_print(fout, "_freeresult(transp, xdr_result,"
2677c478bd9Sstevel@tonic-gate " result)\n");
2687c478bd9Sstevel@tonic-gate f_print(fout, "\tSVCXPRT *transp;\n");
2697c478bd9Sstevel@tonic-gate f_print(fout, "\txdrproc_t xdr_result;\n");
2707c478bd9Sstevel@tonic-gate f_print(fout, "\tcaddr_t result;\n");
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate f_print(fout, "{\n"
2737c478bd9Sstevel@tonic-gate "\t(void) xdr_free(xdr_result, result);\n"
2747c478bd9Sstevel@tonic-gate "\n\t/*\n\t * Insert additional freeing"
2757c478bd9Sstevel@tonic-gate " code here, if needed\n\t */\n"
2767c478bd9Sstevel@tonic-gate "\n\n\treturn (TRUE);\n}\n");
2777c478bd9Sstevel@tonic-gate }
2787c478bd9Sstevel@tonic-gate }
2797c478bd9Sstevel@tonic-gate }
2807c478bd9Sstevel@tonic-gate
28161961e0fSrobinson static void
return_type(proc_list * plist)28261961e0fSrobinson return_type(proc_list *plist)
2837c478bd9Sstevel@tonic-gate {
2847c478bd9Sstevel@tonic-gate ptype(plist->res_prefix, plist->res_type, 1);
2857c478bd9Sstevel@tonic-gate }
2867c478bd9Sstevel@tonic-gate
28761961e0fSrobinson void
add_sample_msg(void)28861961e0fSrobinson add_sample_msg(void)
2897c478bd9Sstevel@tonic-gate {
2907c478bd9Sstevel@tonic-gate f_print(fout, "/*\n");
2917c478bd9Sstevel@tonic-gate f_print(fout, " * This is sample code generated by rpcgen.\n");
2927c478bd9Sstevel@tonic-gate f_print(fout, " * These are only templates and you can use them\n");
2937c478bd9Sstevel@tonic-gate f_print(fout, " * as a guideline for developing your own functions.\n");
2947c478bd9Sstevel@tonic-gate f_print(fout, " */\n\n");
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate
2977c478bd9Sstevel@tonic-gate void
write_sample_clnt_main(void)29861961e0fSrobinson write_sample_clnt_main(void)
2997c478bd9Sstevel@tonic-gate {
3007c478bd9Sstevel@tonic-gate list *l;
3017c478bd9Sstevel@tonic-gate definition *def;
3027c478bd9Sstevel@tonic-gate version_list *vp;
3037c478bd9Sstevel@tonic-gate
3047c478bd9Sstevel@tonic-gate f_print(fout, "\n\n");
3057c478bd9Sstevel@tonic-gate if (Cflag)
30661961e0fSrobinson f_print(fout, "int\nmain(int argc, char *argv[])\n{\n");
3077c478bd9Sstevel@tonic-gate else
30861961e0fSrobinson f_print(fout, "int\nmain(argc, argv)\n\tint argc;\n"
3097c478bd9Sstevel@tonic-gate "\tchar *argv[];\n{\n");
3107c478bd9Sstevel@tonic-gate
3117c478bd9Sstevel@tonic-gate f_print(fout, "\tchar *host;");
3127c478bd9Sstevel@tonic-gate f_print(fout, "\n\n\tif (argc < 2) {");
3137c478bd9Sstevel@tonic-gate f_print(fout, "\n\t\tprintf(\"usage: %%s server_host\\n\","
3147c478bd9Sstevel@tonic-gate " argv[0]);\n");
3157c478bd9Sstevel@tonic-gate f_print(fout, "\t\texit(1);\n\t}");
3167c478bd9Sstevel@tonic-gate f_print(fout, "\n\thost = argv[1];\n");
3177c478bd9Sstevel@tonic-gate
3187c478bd9Sstevel@tonic-gate for (l = defined; l != NULL; l = l->next) {
3197c478bd9Sstevel@tonic-gate def = l->val;
32061961e0fSrobinson if (def->def_kind != DEF_PROGRAM)
3217c478bd9Sstevel@tonic-gate continue;
3227c478bd9Sstevel@tonic-gate for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
3237c478bd9Sstevel@tonic-gate f_print(fout, "\t");
3247c478bd9Sstevel@tonic-gate pvname(def->def_name, vp->vers_num);
3257c478bd9Sstevel@tonic-gate f_print(fout, "(host);\n");
3267c478bd9Sstevel@tonic-gate }
3277c478bd9Sstevel@tonic-gate }
3287c478bd9Sstevel@tonic-gate f_print(fout, "}\n");
3297c478bd9Sstevel@tonic-gate }
330