Annotation of src/usr.bin/rpcgen/rpc_clntout.c, Revision 1.11
1.11 ! deraadt 1: /* $OpenBSD: rpc_clntout.c,v 1.10 2002/06/01 01:40:38 deraadt Exp $ */
1.1 deraadt 2: /* $NetBSD: rpc_clntout.c,v 1.4 1995/06/11 21:49:52 pk Exp $ */
3: /*
4: * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5: * unrestricted use provided that this legend is included on all tape
6: * media and as a part of the software program in whole or part. Users
7: * may copy or modify Sun RPC without charge, but are not authorized
8: * to license or distribute it to anyone else except as part of a product or
9: * program developed by the user or with the express written consent of
10: * Sun Microsystems, Inc.
11: *
12: * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
13: * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
14: * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
15: *
16: * Sun RPC is provided with no support and without any obligation on the
17: * part of Sun Microsystems, Inc. to assist in its use, correction,
18: * modification or enhancement.
19: *
20: * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
21: * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
22: * OR ANY PART THEREOF.
23: *
24: * In no event will Sun Microsystems, Inc. be liable for any lost revenue
25: * or profits or other special, indirect and consequential damages, even if
26: * Sun has been advised of the possibility of such damages.
27: *
28: * Sun Microsystems, Inc.
29: * 2550 Garcia Avenue
30: * Mountain View, California 94043
31: */
32:
33: #ifndef lint
34: static char sccsid[] = "@(#)rpc_clntout.c 1.11 89/02/22 (C) 1987 SMI";
35: #endif
36:
37: /*
38: * rpc_clntout.c, Client-stub outputter for the RPC protocol compiler
39: * Copyright (C) 1987, Sun Microsytsems, Inc.
40: */
41: #include <stdio.h>
42: #include <string.h>
43: #include <rpc/types.h>
44: #include "rpc_parse.h"
45: #include "rpc_util.h"
46:
1.9 millert 47: static void write_program(definition *);
48: static void printbody(proc_list *);
1.1 deraadt 49:
1.8 deraadt 50: extern void pdeclaration();
1.5 deraadt 51:
1.1 deraadt 52: #define DEFAULT_TIMEOUT 25 /* in seconds */
53: static char RESULT[] = "clnt_res";
54:
55:
56: void
57: write_stubs()
58: {
59: list *l;
60: definition *def;
61:
1.10 deraadt 62: fprintf(fout,
1.1 deraadt 63: "\n/* Default timeout can be changed using clnt_control() */\n");
1.10 deraadt 64: fprintf(fout, "static struct timeval TIMEOUT = { %d, 0 };\n",
1.1 deraadt 65: DEFAULT_TIMEOUT);
66: for (l = defined; l != NULL; l = l->next) {
67: def = (definition *) l->val;
68: if (def->def_kind == DEF_PROGRAM) {
69: write_program(def);
70: }
71: }
72: }
73:
1.8 deraadt 74: static void
1.1 deraadt 75: write_program(def)
76: definition *def;
77: {
78: version_list *vp;
79: proc_list *proc;
80:
81: for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
82: for (proc = vp->procs; proc != NULL; proc = proc->next) {
1.10 deraadt 83: fprintf(fout, "\n");
1.1 deraadt 84: ptype(proc->res_prefix, proc->res_type, 1);
1.10 deraadt 85: fprintf(fout, "*\n");
1.1 deraadt 86: pvname(proc->proc_name, vp->vers_num);
1.11 ! deraadt 87: printarglist(proc, "clnt", "CLIENT *");
1.10 deraadt 88: fprintf(fout, "{\n");
1.1 deraadt 89: printbody(proc);
1.10 deraadt 90: fprintf(fout, "}\n");
1.1 deraadt 91: }
92: }
93: }
94:
1.11 ! deraadt 95: /*
! 96: * Writes out declarations of procedure's argument list.
! 97: * In either ANSI C style, in one of old rpcgen style (pass by reference),
! 98: * or new rpcgen style (multiple arguments, pass by value);
! 99: */
1.1 deraadt 100:
101: /* sample addargname = "clnt"; sample addargtype = "CLIENT * " */
102:
1.11 ! deraadt 103: void printarglist(proc, addargname, addargtype)
! 104: proc_list *proc;
! 105: char *addargname, *addargtype;
1.1 deraadt 106: {
1.11 ! deraadt 107: decl_list *l;
1.1 deraadt 108:
1.11 ! deraadt 109: if (!newstyle) { /* old style: always pass argument by reference */
! 110: if (Cflag) { /* C++ style heading */
! 111: fprintf(fout, "(");
! 112: ptype(proc->args.decls->decl.prefix,
! 113: proc->args.decls->decl.type, 1);
! 114: fprintf(fout, "*argp, %s%s)\n", addargtype, addargname);
! 115: } else {
! 116: fprintf(fout, "(argp, %s)\n", addargname);
! 117: fprintf(fout, "\t");
! 118: ptype(proc->args.decls->decl.prefix,
! 119: proc->args.decls->decl.type, 1);
! 120: fprintf(fout, "*argp;\n");
! 121: }
! 122: } else if (streq(proc->args.decls->decl.type, "void")) {
! 123: /* newstyle, 0 argument */
! 124: if (Cflag)
! 125: fprintf(fout, "(%s%s)\n", addargtype, addargname);
! 126: else
! 127: fprintf(fout, "(%s)\n", addargname);
! 128: } else {
! 129: /* new style, 1 or multiple arguments */
! 130: if (!Cflag) {
! 131: fprintf(fout, "(");
! 132: for (l = proc->args.decls; l != NULL; l = l->next)
! 133: fprintf(fout, "%s, ", l->decl.name);
! 134: fprintf(fout, "%s)\n", addargname);
! 135: for (l = proc->args.decls; l != NULL; l = l->next)
! 136: pdeclaration(proc->args.argname, &l->decl, 1, ";\n");
! 137: } else { /* C++ style header */
! 138: fprintf(fout, "(");
! 139: for (l = proc->args.decls; l != NULL; l = l->next)
! 140: pdeclaration(proc->args.argname, &l->decl, 0, ", ");
! 141: fprintf(fout, " %s%s)\n", addargtype, addargname);
! 142: }
! 143: }
1.1 deraadt 144:
1.11 ! deraadt 145: if (!Cflag)
! 146: fprintf(fout, "\t%s%s;\n", addargtype, addargname);
1.1 deraadt 147: }
148:
149: static char *
150: ampr(type)
151: char *type;
152: {
153: if (isvectordef(type, REL_ALIAS)) {
154: return ("");
155: } else {
156: return ("&");
157: }
158: }
159:
160: static void
161: printbody(proc)
162: proc_list *proc;
163: {
1.11 ! deraadt 164: decl_list *l;
! 165: bool_t args2 = (proc->arg_num > 1);
1.1 deraadt 166:
1.11 ! deraadt 167: /*
! 168: * For new style with multiple arguments, need a structure in which
! 169: * to stuff the arguments.
! 170: */
! 171: if (newstyle && args2) {
1.10 deraadt 172: fprintf(fout, "\t%s", proc->args.argname);
173: fprintf(fout, " arg;\n");
1.1 deraadt 174: }
1.10 deraadt 175: fprintf(fout, "\tstatic ");
1.1 deraadt 176: if (streq(proc->res_type, "void")) {
1.10 deraadt 177: fprintf(fout, "char ");
1.1 deraadt 178: } else {
179: ptype(proc->res_prefix, proc->res_type, 0);
180: }
1.10 deraadt 181: fprintf(fout, "%s;\n",RESULT);
182: fprintf(fout, "\n");
1.11 ! deraadt 183: fprintf(fout, "\tmemset((char *)%s%s, 0, sizeof(%s));\n",
! 184: ampr(proc->res_type), RESULT, RESULT);
! 185: if (newstyle && !args2 && (streq(proc->args.decls->decl.type, "void"))) {
! 186: /* newstyle, 0 arguments */
! 187: fprintf(fout,
1.1 deraadt 188: "\tif (clnt_call(clnt, %s, xdr_void", proc->proc_name);
1.11 ! deraadt 189: fprintf(fout,
! 190: ", NULL, xdr_%s, %s%s, TIMEOUT) != RPC_SUCCESS) {\n",
! 191: stringfix(proc->res_type), ampr(proc->res_type), RESULT);
! 192: } else if (newstyle && args2) {
! 193: /* newstyle, multiple arguments: stuff arguments into structure */
! 194: for (l = proc->args.decls; l != NULL; l = l->next) {
! 195: fprintf(fout, "\targ.%s = %s;\n",
! 196: l->decl.name, l->decl.name);
! 197: }
! 198: fprintf(fout,
! 199: "\tif (clnt_call(clnt, %s, xdr_%s", proc->proc_name,
! 200: proc->args.argname);
! 201: fprintf(fout,
! 202: ", &arg, xdr_%s, %s%s, TIMEOUT) != RPC_SUCCESS) {\n",
! 203: stringfix(proc->res_type),
! 204: ampr(proc->res_type), RESULT);
1.1 deraadt 205: } else { /* single argument, new or old style */
1.11 ! deraadt 206: fprintf(fout,
! 207: "\tif (clnt_call(clnt, %s, xdr_%s, %s%s, xdr_%s, "
! 208: "%s%s, TIMEOUT) != RPC_SUCCESS) {\n",
! 209: proc->proc_name,
! 210: stringfix(proc->args.decls->decl.type),
! 211: (newstyle ? "&" : ""),
! 212: (newstyle ? proc->args.decls->decl.name : "argp"),
! 213: stringfix(proc->res_type),
! 214: ampr(proc->res_type),RESULT);
! 215: }
1.10 deraadt 216: fprintf(fout, "\t\treturn (NULL);\n");
217: fprintf(fout, "\t}\n");
1.1 deraadt 218: if (streq(proc->res_type, "void")) {
1.10 deraadt 219: fprintf(fout, "\treturn ((void *)%s%s);\n",
1.11 ! deraadt 220: ampr(proc->res_type),RESULT);
1.1 deraadt 221: } else {
1.10 deraadt 222: fprintf(fout, "\treturn (%s%s);\n", ampr(proc->res_type),RESULT);
1.1 deraadt 223: }
224: }
225: