Print this page
rpcgen should only produce ANSI code
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/rpcgen/rpc_sample.c
+++ new/usr/src/cmd/rpcgen/rpc_sample.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
↓ open down ↓ |
12 lines elided |
↑ open up ↑ |
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 + * Copyright 2014 Garrett D'Amore <garrett@damore.org>
24 + *
23 25 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 26 * Use is subject to license terms.
25 27 */
26 28 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 29 /* All Rights Reserved */
28 30 /*
29 31 * University Copyright- Copyright (c) 1982, 1986, 1988
30 32 * The Regents of the University of California
31 33 * All Rights Reserved
32 34 *
33 35 * University Acknowledgment- Portions of this document are derived from
34 36 * software developed by the University of California, Berkeley, and its
35 37 * contributors.
36 38 */
37 39
38 40 /*
39 41 * rpc_sample.c, Sample client-server code outputter
40 42 * for the RPC protocol compiler
41 43 */
42 44
43 45 #include <stdio.h>
44 46 #include <string.h>
45 47 #include "rpc_parse.h"
46 48 #include "rpc_util.h"
47 49
48 50
49 51 static char RQSTP[] = "rqstp";
50 52
51 53 extern void printarglist(proc_list *, char *, char *, char *);
52 54
53 55 static void write_sample_client(char *, version_list *);
54 56 static void write_sample_server(definition *);
55 57 static void return_type(proc_list *);
56 58
57 59 void
58 60 write_sample_svc(definition *def)
59 61 {
60 62 if (def->def_kind != DEF_PROGRAM)
61 63 return;
62 64 write_sample_server(def);
63 65 }
64 66
65 67 int
66 68 write_sample_clnt(definition *def)
67 69 {
68 70 version_list *vp;
69 71 int count = 0;
70 72
71 73 if (def->def_kind != DEF_PROGRAM)
72 74 return (0);
73 75 /* generate sample code for each version */
74 76 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
75 77 write_sample_client(def->def_name, vp);
76 78 ++count;
77 79 }
78 80 return (count);
79 81 }
↓ open down ↓ |
47 lines elided |
↑ open up ↑ |
80 82
81 83 static void
82 84 write_sample_client(char *program_name, version_list *vp)
83 85 {
84 86 proc_list *proc;
85 87 int i;
86 88 decl_list *l;
87 89
88 90 f_print(fout, "\n\nvoid\n");
89 91 pvname(program_name, vp->vers_num);
90 - if (Cflag)
91 - f_print(fout, "(char *host)\n{\n");
92 - else
93 - f_print(fout, "(host)\n\tchar *host;\n{\n");
92 + f_print(fout, "(char *host)\n{\n");
94 93 f_print(fout, "\tCLIENT *clnt;\n");
95 94
96 95 i = 0;
97 96 for (proc = vp->procs; proc != NULL; proc = proc->next) {
98 97 f_print(fout, "\t");
99 98 if (mtflag) {
100 99 f_print(fout, "enum clnt_stat retval_%d;\n", ++i);
101 100 if (!streq(proc->res_type, "oneway")) {
102 101 f_print(fout, "\t");
103 102 if (!streq(proc->res_type, "void"))
104 103 ptype(proc->res_prefix,
105 104 proc->res_type, 1);
106 105 else
107 106 f_print(fout, "void *");
108 107 f_print(fout, "result_%d;\n", i);
109 108 }
110 109 } else {
111 110 ptype(proc->res_prefix, proc->res_type, 1);
112 111 f_print(fout, " *result_%d;\n", ++i);
113 112 }
114 113 /* print out declarations for arguments */
115 114 if (proc->arg_num < 2 && !newstyle) {
116 115 f_print(fout, "\t");
117 116 if (!streq(proc->args.decls->decl.type, "void"))
118 117 ptype(proc->args.decls->decl.prefix,
119 118 proc->args.decls->decl.type, 1);
120 119 else
121 120 /* cannot have "void" type */
122 121 f_print(fout, "char * ");
123 122 f_print(fout, " ");
124 123 pvname(proc->proc_name, vp->vers_num);
125 124 f_print(fout, "_arg;\n");
126 125 } else if (!streq(proc->args.decls->decl.type, "void")) {
127 126 for (l = proc->args.decls; l != NULL; l = l->next) {
128 127 f_print(fout, "\t");
129 128 ptype(l->decl.prefix, l->decl.type, 1);
130 129 if (strcmp(l->decl.type, "string") == 1)
131 130 f_print(fout, " ");
132 131 pvname(proc->proc_name, vp->vers_num);
133 132 f_print(fout, "_%s;\n", l->decl.name);
134 133 }
135 134 }
136 135 }
137 136
138 137 /* generate creation of client handle */
139 138 f_print(fout, "\n#ifndef\tDEBUG\n");
140 139 f_print(fout, "\tclnt = clnt_create(host, %s, %s, \"%s\");\n",
141 140 program_name, vp->vers_name, tirpcflag? "netpath" : "udp");
142 141 f_print(fout, "\tif (clnt == (CLIENT *) NULL) {\n");
143 142 f_print(fout, "\t\tclnt_pcreateerror(host);\n");
144 143 f_print(fout, "\t\texit(1);\n\t}\n");
145 144 f_print(fout, "#endif\t/* DEBUG */\n\n");
146 145
147 146 /* generate calls to procedures */
148 147 i = 0;
149 148 for (proc = vp->procs; proc != NULL; proc = proc->next) {
150 149 if (mtflag)
151 150 f_print(fout, "\tretval_%d = ", ++i);
152 151 else
153 152 f_print(fout, "\tresult_%d = ", ++i);
154 153 pvname(proc->proc_name, vp->vers_num);
155 154 if (proc->arg_num < 2 && !newstyle) {
156 155 f_print(fout, "(");
157 156 if (streq(proc->args.decls->decl.type, "void"))
158 157 /* cast to void * */
159 158 f_print(fout, "(void *)");
160 159 f_print(fout, "&");
161 160 pvname(proc->proc_name, vp->vers_num);
162 161 if (mtflag) {
163 162 if (streq(proc->res_type, "oneway"))
164 163 f_print(fout, "_arg, clnt);\n");
165 164 else
166 165 f_print(fout,
167 166 "_arg, &result_%d, clnt);\n", i);
168 167 } else
169 168 f_print(fout, "_arg, clnt);\n");
170 169
171 170 } else if (streq(proc->args.decls->decl.type, "void")) {
172 171 if (mtflag) {
173 172 if (streq(proc->res_type, "oneway"))
174 173 f_print(fout, "(clnt);\n");
175 174 else
176 175 f_print(fout,
177 176 "(&result_%d, clnt);\n", i);
178 177 } else
179 178 f_print(fout, "(clnt);\n");
180 179 } else {
181 180 f_print(fout, "(");
182 181 for (l = proc->args.decls; l != NULL; l = l->next) {
183 182 pvname(proc->proc_name, vp->vers_num);
184 183 f_print(fout, "_%s, ", l->decl.name);
185 184 }
186 185 if (mtflag) {
187 186 if (!streq(proc->res_type, "oneway"))
188 187 f_print(fout, "&result_%d, ", i);
189 188 }
190 189
191 190 f_print(fout, "clnt);\n");
192 191 }
193 192 if (mtflag) {
194 193 f_print(fout, "\tif (retval_%d != RPC_SUCCESS) {\n", i);
195 194 } else {
196 195 f_print(fout, "\tif (result_%d == (", i);
197 196 ptype(proc->res_prefix, proc->res_type, 1);
198 197 f_print(fout, "*) NULL) {\n");
199 198 }
200 199 f_print(fout, "\t\tclnt_perror(clnt, \"call failed\");\n");
201 200 f_print(fout, "\t}\n");
202 201 }
203 202
204 203 f_print(fout, "#ifndef\tDEBUG\n");
205 204 f_print(fout, "\tclnt_destroy(clnt);\n");
206 205 f_print(fout, "#endif\t /* DEBUG */\n");
207 206 f_print(fout, "}\n");
208 207 }
209 208
210 209 static void
211 210 write_sample_server(definition *def)
212 211 {
213 212 version_list *vp;
214 213 proc_list *proc;
↓ open down ↓ |
111 lines elided |
↑ open up ↑ |
215 214
216 215 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
217 216 for (proc = vp->procs; proc != NULL; proc = proc->next) {
218 217 f_print(fout, "\n");
219 218 if (!mtflag) {
220 219 return_type(proc);
221 220 f_print(fout, "*\n");
222 221 } else {
223 222 f_print(fout, "bool_t\n");
224 223 }
225 - if (Cflag || mtflag)
226 - pvname_svc(proc->proc_name, vp->vers_num);
227 - else
228 - pvname(proc->proc_name, vp->vers_num);
224 + pvname_svc(proc->proc_name, vp->vers_num);
229 225 printarglist(proc, "result", RQSTP, "struct svc_req *");
230 226
231 227 f_print(fout, "{\n");
232 228
233 229 if (!mtflag) {
234 230 f_print(fout, "\tstatic ");
235 231 if ((!streq(proc->res_type, "void")) &&
236 232 (!streq(proc->res_type, "oneway")))
237 233 return_type(proc);
238 234 else
239 235 f_print(fout, "char *");
240 236 /* cannot have void type */
241 237 f_print(fout, " result;\n");
242 238 }
243 239
244 240 f_print(fout, "\n\t/*\n\t * insert server code "
245 241 "here\n\t */\n\n");
246 242
247 243 if (!mtflag)
248 244 if (!streq(proc->res_type, "void"))
249 245 f_print(fout,
250 246 "\treturn (&result);\n}\n");
↓ open down ↓ |
12 lines elided |
↑ open up ↑ |
251 247 else /* cast back to void * */
252 248 f_print(fout, "\treturn((void *) "
253 249 "&result);\n}\n");
254 250 else
255 251 f_print(fout, "\treturn (retval);\n}\n");
256 252 }
257 253 /* put in sample freeing routine */
258 254 if (mtflag) {
259 255 f_print(fout, "\nint\n");
260 256 pvname(def->def_name, vp->vers_num);
261 - if (Cflag)
262 - f_print(fout, "_freeresult(SVCXPRT *transp,"
263 - " xdrproc_t xdr_result,"
264 - " caddr_t result)\n");
265 - else {
266 - f_print(fout, "_freeresult(transp, xdr_result,"
267 - " result)\n");
268 - f_print(fout, "\tSVCXPRT *transp;\n");
269 - f_print(fout, "\txdrproc_t xdr_result;\n");
270 - f_print(fout, "\tcaddr_t result;\n");
271 - }
257 + f_print(fout, "_freeresult(SVCXPRT *transp,"
258 + " xdrproc_t xdr_result,"
259 + " caddr_t result)\n");
272 260 f_print(fout, "{\n"
273 261 "\t(void) xdr_free(xdr_result, result);\n"
274 262 "\n\t/*\n\t * Insert additional freeing"
275 263 " code here, if needed\n\t */\n"
276 264 "\n\n\treturn (TRUE);\n}\n");
277 265 }
278 266 }
279 267 }
280 268
281 269 static void
282 270 return_type(proc_list *plist)
283 271 {
284 272 ptype(plist->res_prefix, plist->res_type, 1);
285 273 }
286 274
287 275 void
288 276 add_sample_msg(void)
289 277 {
290 278 f_print(fout, "/*\n");
291 279 f_print(fout, " * This is sample code generated by rpcgen.\n");
292 280 f_print(fout, " * These are only templates and you can use them\n");
293 281 f_print(fout, " * as a guideline for developing your own functions.\n");
294 282 f_print(fout, " */\n\n");
↓ open down ↓ |
13 lines elided |
↑ open up ↑ |
295 283 }
296 284
297 285 void
298 286 write_sample_clnt_main(void)
299 287 {
300 288 list *l;
301 289 definition *def;
302 290 version_list *vp;
303 291
304 292 f_print(fout, "\n\n");
305 - if (Cflag)
306 - f_print(fout, "int\nmain(int argc, char *argv[])\n{\n");
307 - else
308 - f_print(fout, "int\nmain(argc, argv)\n\tint argc;\n"
309 - "\tchar *argv[];\n{\n");
293 + f_print(fout, "int\nmain(int argc, char *argv[])\n{\n");
310 294
311 295 f_print(fout, "\tchar *host;");
312 296 f_print(fout, "\n\n\tif (argc < 2) {");
313 297 f_print(fout, "\n\t\tprintf(\"usage: %%s server_host\\n\","
314 298 " argv[0]);\n");
315 299 f_print(fout, "\t\texit(1);\n\t}");
316 300 f_print(fout, "\n\thost = argv[1];\n");
317 301
318 302 for (l = defined; l != NULL; l = l->next) {
319 303 def = l->val;
320 304 if (def->def_kind != DEF_PROGRAM)
321 305 continue;
322 306 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
323 307 f_print(fout, "\t");
324 308 pvname(def->def_name, vp->vers_num);
325 309 f_print(fout, "(host);\n");
326 310 }
327 311 }
328 312 f_print(fout, "}\n");
329 313 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX