Annotation of src/usr.bin/rpcgen/rpc_clntout.c, Revision 1.15
1.15 ! millert 1: /* $OpenBSD: rpc_clntout.c,v 1.14 2009/10/27 23:59:42 deraadt Exp $ */
1.1 deraadt 2: /* $NetBSD: rpc_clntout.c,v 1.4 1995/06/11 21:49:52 pk Exp $ */
1.15 ! millert 3:
1.1 deraadt 4: /*
1.15 ! millert 5: * Copyright (c) 2010, Oracle America, Inc.
1.1 deraadt 6: *
1.15 ! 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.15 ! 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.15 ! 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_clntout.c, Client-stub outputter for the RPC protocol compiler
37: */
38: #include <stdio.h>
39: #include <string.h>
40: #include <rpc/types.h>
41: #include "rpc_parse.h"
42: #include "rpc_util.h"
43:
1.9 millert 44: static void write_program(definition *);
45: static void printbody(proc_list *);
1.1 deraadt 46:
47: #define DEFAULT_TIMEOUT 25 /* in seconds */
48: static char RESULT[] = "clnt_res";
49:
50:
51: void
52: write_stubs()
53: {
54: list *l;
55: definition *def;
56:
1.10 deraadt 57: fprintf(fout,
1.1 deraadt 58: "\n/* Default timeout can be changed using clnt_control() */\n");
1.10 deraadt 59: fprintf(fout, "static struct timeval TIMEOUT = { %d, 0 };\n",
1.1 deraadt 60: DEFAULT_TIMEOUT);
61: for (l = defined; l != NULL; l = l->next) {
62: def = (definition *) l->val;
63: if (def->def_kind == DEF_PROGRAM) {
64: write_program(def);
65: }
66: }
67: }
68:
1.8 deraadt 69: static void
1.1 deraadt 70: write_program(def)
71: definition *def;
72: {
73: version_list *vp;
74: proc_list *proc;
75:
76: for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
77: for (proc = vp->procs; proc != NULL; proc = proc->next) {
1.10 deraadt 78: fprintf(fout, "\n");
1.1 deraadt 79: ptype(proc->res_prefix, proc->res_type, 1);
1.10 deraadt 80: fprintf(fout, "*\n");
1.1 deraadt 81: pvname(proc->proc_name, vp->vers_num);
1.11 deraadt 82: printarglist(proc, "clnt", "CLIENT *");
1.10 deraadt 83: fprintf(fout, "{\n");
1.1 deraadt 84: printbody(proc);
1.10 deraadt 85: fprintf(fout, "}\n");
1.1 deraadt 86: }
87: }
88: }
89:
1.11 deraadt 90: /*
91: * Writes out declarations of procedure's argument list.
92: * In either ANSI C style, in one of old rpcgen style (pass by reference),
93: * or new rpcgen style (multiple arguments, pass by value);
94: */
1.1 deraadt 95:
96: /* sample addargname = "clnt"; sample addargtype = "CLIENT * " */
97:
1.11 deraadt 98: void printarglist(proc, addargname, addargtype)
99: proc_list *proc;
100: char *addargname, *addargtype;
1.1 deraadt 101: {
1.11 deraadt 102: decl_list *l;
1.1 deraadt 103:
1.11 deraadt 104: if (!newstyle) { /* old style: always pass argument by reference */
105: if (Cflag) { /* C++ style heading */
106: fprintf(fout, "(");
107: ptype(proc->args.decls->decl.prefix,
108: proc->args.decls->decl.type, 1);
109: fprintf(fout, "*argp, %s%s)\n", addargtype, addargname);
110: } else {
111: fprintf(fout, "(argp, %s)\n", addargname);
112: fprintf(fout, "\t");
113: ptype(proc->args.decls->decl.prefix,
114: proc->args.decls->decl.type, 1);
115: fprintf(fout, "*argp;\n");
116: }
117: } else if (streq(proc->args.decls->decl.type, "void")) {
118: /* newstyle, 0 argument */
119: if (Cflag)
120: fprintf(fout, "(%s%s)\n", addargtype, addargname);
121: else
122: fprintf(fout, "(%s)\n", addargname);
123: } else {
124: /* new style, 1 or multiple arguments */
125: if (!Cflag) {
126: fprintf(fout, "(");
127: for (l = proc->args.decls; l != NULL; l = l->next)
128: fprintf(fout, "%s, ", l->decl.name);
129: fprintf(fout, "%s)\n", addargname);
130: for (l = proc->args.decls; l != NULL; l = l->next)
131: pdeclaration(proc->args.argname, &l->decl, 1, ";\n");
132: } else { /* C++ style header */
133: fprintf(fout, "(");
134: for (l = proc->args.decls; l != NULL; l = l->next)
135: pdeclaration(proc->args.argname, &l->decl, 0, ", ");
136: fprintf(fout, " %s%s)\n", addargtype, addargname);
137: }
138: }
1.1 deraadt 139:
1.11 deraadt 140: if (!Cflag)
141: fprintf(fout, "\t%s%s;\n", addargtype, addargname);
1.1 deraadt 142: }
143:
144: static char *
1.12 deraadt 145: ampr(char *type)
1.1 deraadt 146: {
147: if (isvectordef(type, REL_ALIAS)) {
148: return ("");
149: } else {
150: return ("&");
151: }
152: }
153:
154: static void
155: printbody(proc)
156: proc_list *proc;
157: {
1.11 deraadt 158: decl_list *l;
159: bool_t args2 = (proc->arg_num > 1);
1.1 deraadt 160:
1.11 deraadt 161: /*
162: * For new style with multiple arguments, need a structure in which
163: * to stuff the arguments.
164: */
165: if (newstyle && args2) {
1.10 deraadt 166: fprintf(fout, "\t%s", proc->args.argname);
167: fprintf(fout, " arg;\n");
1.1 deraadt 168: }
1.10 deraadt 169: fprintf(fout, "\tstatic ");
1.1 deraadt 170: if (streq(proc->res_type, "void")) {
1.10 deraadt 171: fprintf(fout, "char ");
1.1 deraadt 172: } else {
173: ptype(proc->res_prefix, proc->res_type, 0);
174: }
1.10 deraadt 175: fprintf(fout, "%s;\n",RESULT);
176: fprintf(fout, "\n");
1.11 deraadt 177: fprintf(fout, "\tmemset((char *)%s%s, 0, sizeof(%s));\n",
178: ampr(proc->res_type), RESULT, RESULT);
179: if (newstyle && !args2 && (streq(proc->args.decls->decl.type, "void"))) {
180: /* newstyle, 0 arguments */
181: fprintf(fout,
1.1 deraadt 182: "\tif (clnt_call(clnt, %s, xdr_void", proc->proc_name);
1.11 deraadt 183: fprintf(fout,
184: ", NULL, xdr_%s, %s%s, TIMEOUT) != RPC_SUCCESS) {\n",
185: stringfix(proc->res_type), ampr(proc->res_type), RESULT);
186: } else if (newstyle && args2) {
187: /* newstyle, multiple arguments: stuff arguments into structure */
188: for (l = proc->args.decls; l != NULL; l = l->next) {
189: fprintf(fout, "\targ.%s = %s;\n",
190: l->decl.name, l->decl.name);
191: }
192: fprintf(fout,
193: "\tif (clnt_call(clnt, %s, xdr_%s", proc->proc_name,
194: proc->args.argname);
195: fprintf(fout,
196: ", &arg, xdr_%s, %s%s, TIMEOUT) != RPC_SUCCESS) {\n",
197: stringfix(proc->res_type),
198: ampr(proc->res_type), RESULT);
1.1 deraadt 199: } else { /* single argument, new or old style */
1.11 deraadt 200: fprintf(fout,
201: "\tif (clnt_call(clnt, %s, xdr_%s, %s%s, xdr_%s, "
202: "%s%s, TIMEOUT) != RPC_SUCCESS) {\n",
203: proc->proc_name,
204: stringfix(proc->args.decls->decl.type),
205: (newstyle ? "&" : ""),
206: (newstyle ? proc->args.decls->decl.name : "argp"),
207: stringfix(proc->res_type),
208: ampr(proc->res_type),RESULT);
209: }
1.10 deraadt 210: fprintf(fout, "\t\treturn (NULL);\n");
211: fprintf(fout, "\t}\n");
1.1 deraadt 212: if (streq(proc->res_type, "void")) {
1.10 deraadt 213: fprintf(fout, "\treturn ((void *)%s%s);\n",
1.11 deraadt 214: ampr(proc->res_type),RESULT);
1.1 deraadt 215: } else {
1.10 deraadt 216: fprintf(fout, "\treturn (%s%s);\n", ampr(proc->res_type),RESULT);
1.1 deraadt 217: }
218: }
219: