Annotation of src/usr.bin/rpcgen/rpc_sample.c, Revision 1.17
1.17 ! millert 1: /* $OpenBSD: rpc_sample.c,v 1.16 2009/10/27 23:59:42 deraadt Exp $ */
1.1 deraadt 2: /* $NetBSD: rpc_sample.c,v 1.2 1995/06/11 21:50:01 pk Exp $ */
1.17 ! millert 3:
1.1 deraadt 4: /*
1.17 ! millert 5: * Copyright (c) 2010, Oracle America, Inc.
1.1 deraadt 6: *
1.17 ! millert 7: * Redistribution and use in source and binary forms, with or without
! 8: * modification, are permitted provided that the following conditions are
! 9: * met:
1.1 deraadt 10: *
1.17 ! millert 11: * * Redistributions of source code must retain the above copyright
! 12: * notice, this list of conditions and the following disclaimer.
! 13: * * Redistributions in binary form must reproduce the above
! 14: * copyright notice, this list of conditions and the following
! 15: * disclaimer in the documentation and/or other materials
! 16: * provided with the distribution.
! 17: * * Neither the name of the "Oracle America, Inc." nor the names of its
! 18: * contributors may be used to endorse or promote products derived
! 19: * from this software without specific prior written permission.
1.1 deraadt 20: *
1.17 ! millert 21: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
! 22: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
! 23: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
! 24: * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
! 25: * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
! 26: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
! 27: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
! 28: * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
! 29: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
! 30: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
! 31: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
! 32: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1 deraadt 33: */
34:
35: /*
36: * rpc_sample.c, Sample client-server code outputter for the RPC protocol compiler
37: */
38:
39: #include <sys/cdefs.h>
40: #include <stdio.h>
41: #include <string.h>
42: #include "rpc_parse.h"
43: #include "rpc_util.h"
44:
45: static char RQSTP[] = "rqstp";
46:
1.11 millert 47: static void write_sample_client(char *, version_list *);
48: static void write_sample_server(definition *);
49: static void return_type(proc_list *);
1.1 deraadt 50:
51: void
52: write_sample_svc(def)
1.13 deraadt 53: definition *def;
1.1 deraadt 54: {
55:
1.10 deraadt 56: if (def->def_kind != DEF_PROGRAM)
57: return;
1.1 deraadt 58: write_sample_server(def);
59: }
60:
61:
62: int
63: write_sample_clnt(def)
1.13 deraadt 64: definition *def;
1.1 deraadt 65: {
1.13 deraadt 66: version_list *vp;
1.1 deraadt 67: int count = 0;
68:
1.10 deraadt 69: if (def->def_kind != DEF_PROGRAM)
70: return(0);
1.1 deraadt 71: /* generate sample code for each version */
72: for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
1.10 deraadt 73: write_sample_client(def->def_name, vp);
74: ++count;
1.1 deraadt 75: }
1.10 deraadt 76: return(count);
1.1 deraadt 77: }
78:
79:
1.10 deraadt 80: static void
81: write_sample_client(program_name, vp)
1.13 deraadt 82: char *program_name;
83: version_list *vp;
1.1 deraadt 84: {
1.13 deraadt 85: proc_list *proc;
86: int i;
87: decl_list *l;
88:
89: fprintf(fout, "\n\nvoid\n");
90: pvname(program_name, vp->vers_num);
91: if (Cflag)
92: fprintf(fout,"(char *host)\n{\n");
1.1 deraadt 93: else
1.13 deraadt 94: fprintf(fout, "(host)\nchar *host;\n{\n");
95: fprintf(fout, "\tCLIENT *clnt;\n");
96:
97: i = 0;
98: for (proc = vp->procs; proc != NULL; proc = proc->next) {
99: fprintf(fout, "\t");
100: ptype(proc->res_prefix, proc->res_type, 1);
101: fprintf(fout, " *result_%d;\n",++i);
102: /* print out declarations for arguments */
103: if (proc->arg_num < 2 && !newstyle) {
104: fprintf(fout, "\t");
105: if (!streq(proc->args.decls->decl.type, "void"))
106: ptype(proc->args.decls->decl.prefix,
107: proc->args.decls->decl.type, 1);
108: else
109: fprintf(fout, "char *"); /* cannot have "void" type */
110: fprintf(fout, " ");
111: pvname(proc->proc_name, vp->vers_num);
112: fprintf(fout, "_arg;\n");
113: } else if (!streq(proc->args.decls->decl.type, "void")) {
114: for (l = proc->args.decls; l != NULL; l = l->next) {
115: fprintf(fout, "\t");
116: ptype(l->decl.prefix, l->decl.type, 1);
117: fprintf(fout, " ");
118: pvname(proc->proc_name, vp->vers_num);
119: fprintf(fout, "_%s;\n", l->decl.name);
120: /* pdeclaration(proc->args.argname, &l->decl, 1, ";\n");*/
121: }
122: }
1.1 deraadt 123: }
124:
1.13 deraadt 125: /* generate creation of client handle */
126: fprintf(fout, "\tclnt = clnt_create(host, %s, %s, \"%s\");\n",
127: program_name, vp->vers_name, tirpcflag? "netpath" : "udp");
128: fprintf(fout, "\tif (clnt == NULL) {\n");
129: fprintf(fout, "\t\tclnt_pcreateerror(host);\n");
130: fprintf(fout, "\t\texit(1);\n\t}\n");
131:
132: /* generate calls to procedures */
133: i = 0;
134: for (proc = vp->procs; proc != NULL; proc = proc->next) {
135: fprintf(fout, "\tresult_%d = ",++i);
136: pvname(proc->proc_name, vp->vers_num);
137: if (proc->arg_num < 2 && !newstyle) {
138: fprintf(fout, "(");
139: if (streq(proc->args.decls->decl.type, "void"))
140: fprintf(fout, "(void*)");
141: fprintf(fout, "&");
142: pvname(proc->proc_name, vp->vers_num);
143: fprintf(fout, "_arg, clnt);\n");
144: } else if (streq(proc->args.decls->decl.type, "void")) {
145: fprintf(fout, "(clnt);\n");
146: } else {
147: fprintf(fout, "(");
148: for (l = proc->args.decls; l != NULL; l = l->next) {
149: pvname(proc->proc_name, vp->vers_num);
150: fprintf(fout, "_%s, ", l->decl.name);
151: }
152: fprintf(fout, "clnt);\n");
153: }
154: fprintf(fout, "\tif (result_%d == NULL) {\n", i);
155: fprintf(fout, "\t\tclnt_perror(clnt, \"call failed:\");\n");
156: fprintf(fout, "\t}\n");
1.1 deraadt 157: }
158:
1.13 deraadt 159: fprintf(fout, "\tclnt_destroy(clnt);\n");
160: fprintf(fout, "}\n");
1.1 deraadt 161: }
162:
1.10 deraadt 163: static void
1.1 deraadt 164: write_sample_server(def)
165: definition *def;
166: {
167: version_list *vp;
168: proc_list *proc;
169:
170: for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
171: for (proc = vp->procs; proc != NULL; proc = proc->next) {
1.12 deraadt 172: fprintf(fout, "\n");
1.10 deraadt 173: /* if (Cflag)
1.12 deraadt 174: fprintf(fout, "extern \"C\"{\n");
1.1 deraadt 175: */
176: return_type(proc);
1.12 deraadt 177: fprintf(fout, "* \n");
1.3 deraadt 178: pvname_svc(proc->proc_name, vp->vers_num);
1.10 deraadt 179: printarglist(proc, RQSTP, "struct svc_req *");
1.1 deraadt 180:
1.12 deraadt 181: fprintf(fout, "{\n");
182: fprintf(fout, "\n\tstatic ");
1.10 deraadt 183: if (!streq(proc->res_type, "void"))
184: return_type(proc);
1.4 pvalchev 185: else
1.12 deraadt 186: fprintf(fout, "char*"); /* cannot have void type */
187: fprintf(fout, " result;\n");
188: fprintf(fout,
1.10 deraadt 189: "\n\t/*\n\t * insert server code here\n\t */\n\n");
190: if (!streq(proc->res_type, "void"))
1.12 deraadt 191: fprintf(fout, "\treturn(&result);\n}\n");
1.1 deraadt 192: else /* cast back to void * */
1.12 deraadt 193: fprintf(fout, "\treturn((void*) &result);\n}\n");
1.10 deraadt 194: /* if (Cflag)
1.12 deraadt 195: fprintf(fout, "}\n");
1.1 deraadt 196: */
1.10 deraadt 197: }
1.1 deraadt 198: }
199: }
200:
1.10 deraadt 201: static void
1.1 deraadt 202: return_type(plist)
203: proc_list *plist;
204: {
1.10 deraadt 205: ptype(plist->res_prefix, plist->res_type, 1);
1.1 deraadt 206: }
207:
1.10 deraadt 208: void
1.14 deraadt 209: add_sample_msg(void)
1.1 deraadt 210: {
1.12 deraadt 211: fprintf(fout, "/*\n");
212: fprintf(fout, " * This is sample code generated by rpcgen.\n");
213: fprintf(fout, " * These are only templates and you can use them\n");
214: fprintf(fout, " * as a guideline for developing your own functions.\n");
215: fprintf(fout, " */\n\n");
1.1 deraadt 216: }
217:
218: void
219: write_sample_clnt_main()
220: {
1.10 deraadt 221: list *l;
222: definition *def;
223: version_list *vp;
224:
1.12 deraadt 225: fprintf(fout, "\n\n");
1.10 deraadt 226: if (Cflag)
1.12 deraadt 227: fprintf(fout,"main(int argc, char *argv[])\n{\n");
1.10 deraadt 228: else
1.12 deraadt 229: fprintf(fout, "main(argc, argv)\nint argc;\nchar *argv[];\n{\n");
1.1 deraadt 230:
1.12 deraadt 231: fprintf(fout, "\tchar *host;");
232: fprintf(fout, "\n\n\tif (argc < 2) {");
233: fprintf(fout, "\n\t\tprintf(\"usage: %%s server_host\\n\", argv[0]);\n");
234: fprintf(fout, "\t\texit(1);\n\t}");
235: fprintf(fout, "\n\thost = argv[1];\n");
1.1 deraadt 236:
1.10 deraadt 237: for (l = defined; l != NULL; l = l->next) {
1.1 deraadt 238: def = l->val;
1.10 deraadt 239: if (def->def_kind != DEF_PROGRAM)
1.1 deraadt 240: continue;
241: for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
1.12 deraadt 242: fprintf(fout, "\t");
1.1 deraadt 243: pvname(def->def_name, vp->vers_num);
1.12 deraadt 244: fprintf(fout, "(host);\n");
1.1 deraadt 245: }
1.10 deraadt 246: }
1.12 deraadt 247: fprintf(fout, "}\n");
1.1 deraadt 248: }