Print this page
rpcgen should only produce ANSI code
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/rpcgen/rpc_hout.c
+++ new/usr/src/cmd/rpcgen/rpc_hout.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 */
↓ open down ↓ |
4 lines elided |
↑ open up ↑ |
37 39
38 40 /*
39 41 * rpc_hout.c, Header file outputter for the RPC protocol compiler
40 42 */
41 43 #include <stdio.h>
42 44 #include <stdlib.h>
43 45 #include <ctype.h>
44 46 #include "rpc_parse.h"
45 47 #include "rpc_util.h"
46 48
47 -extern void pprocdef(proc_list *, version_list *, char *, int, int);
49 +extern void pprocdef(proc_list *, version_list *, char *, int);
48 50 extern void pdeclaration(char *, declaration *, int, char *);
49 51
50 52 static void storexdrfuncdecl(char *, int);
51 53 static void pconstdef(definition *);
52 54 static void pstructdef(definition *);
53 55 static void puniondef(definition *);
54 56 static void pdefine(char *, char *);
55 57 static void pprogramdef(definition *);
56 58 static void parglist(proc_list *, char *);
57 59 static void penumdef(definition *);
58 60 static void ptypedef(definition *);
59 61 static uint_t undefined2(char *, char *);
60 62
61 63 enum rpc_gvc {
62 64 PROGRAM,
63 65 VERSION,
64 66 PROCEDURE
65 67 };
66 68
67 69 /*
68 70 * Print the C-version of an xdr definition
69 71 */
70 72 void
71 73 print_datadef(definition *def)
72 74 {
73 75 if (def->def_kind == DEF_PROGRAM) /* handle data only */
74 76 return;
75 77
76 78 if (def->def_kind != DEF_CONST)
77 79 f_print(fout, "\n");
78 80 switch (def->def_kind) {
79 81 case DEF_STRUCT:
80 82 pstructdef(def);
81 83 break;
82 84 case DEF_UNION:
83 85 puniondef(def);
84 86 break;
85 87 case DEF_ENUM:
86 88 penumdef(def);
87 89 break;
88 90 case DEF_TYPEDEF:
89 91 ptypedef(def);
90 92 break;
91 93 case DEF_PROGRAM:
92 94 pprogramdef(def);
93 95 break;
94 96 case DEF_CONST:
95 97 pconstdef(def);
96 98 break;
97 99 }
98 100 if (def->def_kind != DEF_PROGRAM && def->def_kind != DEF_CONST)
99 101 storexdrfuncdecl(def->def_name, def->def_kind != DEF_TYPEDEF ||
100 102 !isvectordef(def->def.ty.old_type, def->def.ty.rel));
101 103 }
102 104
103 105
104 106 void
105 107 print_funcdef(definition *def)
106 108 {
107 109 switch (def->def_kind) {
108 110 case DEF_PROGRAM:
109 111 f_print(fout, "\n");
110 112 pprogramdef(def);
111 113 break;
112 114 }
113 115 }
114 116
115 117 /*
116 118 * store away enough information to allow the XDR functions to be spat
117 119 * out at the end of the file
118 120 */
119 121 static void
120 122 storexdrfuncdecl(char *name, int pointerp)
121 123 {
122 124 xdrfunc *xdrptr;
123 125
124 126 xdrptr = malloc(sizeof (struct xdrfunc));
125 127
126 128 xdrptr->name = name;
127 129 xdrptr->pointerp = pointerp;
128 130 xdrptr->next = NULL;
129 131
130 132 if (xdrfunc_tail == NULL) {
131 133 xdrfunc_head = xdrptr;
↓ open down ↓ |
74 lines elided |
↑ open up ↑ |
132 134 xdrfunc_tail = xdrptr;
133 135 } else {
134 136 xdrfunc_tail->next = xdrptr;
135 137 xdrfunc_tail = xdrptr;
136 138 }
137 139
138 140
139 141 }
140 142
141 143 void
142 -print_xdr_func_def(char *name, int pointerp, int i)
144 +print_xdr_func_def(char *name, int pointerp)
143 145 {
144 - if (i == 2)
145 - f_print(fout, "extern bool_t xdr_%s();\n", name);
146 - else
147 - f_print(fout, "extern bool_t xdr_%s(XDR *, %s%s);\n", name,
148 - name, pointerp ? "*" : "");
146 + f_print(fout, "extern bool_t xdr_%s(XDR *, %s%s);\n", name,
147 + name, pointerp ? "*" : "");
149 148 }
150 149
151 150
152 151 static void
153 152 pconstdef(definition *def)
154 153 {
155 154 pdefine(def->def_name, def->def.co);
156 155 }
157 156
158 157 /*
159 158 * print out the definitions for the arguments of functions in the
160 159 * header file
161 160 */
162 161 static void
163 162 pargdef(definition *def)
164 163 {
165 164 decl_list *l;
166 165 version_list *vers;
167 166 char *name;
168 167 proc_list *plist;
169 168
170 169 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
171 170 for (plist = vers->procs; plist != NULL; plist = plist->next) {
172 171 if (!newstyle || plist->arg_num < 2)
173 172 continue; /* old style or single args */
174 173 name = plist->args.argname;
175 174 f_print(fout, "struct %s {\n", name);
176 175 for (l = plist->args.decls; l != NULL; l = l->next)
177 176 pdeclaration(name, &l->decl, 1, ";\n");
178 177 f_print(fout, "};\n");
179 178 f_print(fout, "typedef struct %s %s;\n", name, name);
180 179 storexdrfuncdecl(name, 1);
181 180 f_print(fout, "\n");
182 181 }
183 182 }
184 183 }
185 184
186 185
187 186 static void
188 187 pstructdef(definition *def)
189 188 {
190 189 decl_list *l;
191 190 char *name = def->def_name;
192 191
193 192 f_print(fout, "struct %s {\n", name);
194 193 for (l = def->def.st.decls; l != NULL; l = l->next)
195 194 pdeclaration(name, &l->decl, 1, ";\n");
196 195 f_print(fout, "};\n");
197 196 f_print(fout, "typedef struct %s %s;\n", name, name);
198 197 }
199 198
200 199 static void
201 200 puniondef(definition *def)
202 201 {
203 202 case_list *l;
204 203 char *name = def->def_name;
205 204 declaration *decl;
206 205
207 206 f_print(fout, "struct %s {\n", name);
208 207 decl = &def->def.un.enum_decl;
209 208 if (streq(decl->type, "bool"))
210 209 f_print(fout, "\tbool_t %s;\n", decl->name);
211 210 else
212 211 f_print(fout, "\t%s %s;\n", decl->type, decl->name);
213 212 f_print(fout, "\tunion {\n");
214 213 for (l = def->def.un.cases; l != NULL; l = l->next) {
215 214 if (l->contflag == 0)
216 215 pdeclaration(name, &l->case_decl, 2, ";\n");
217 216 }
218 217 decl = def->def.un.default_decl;
219 218 if (decl && !streq(decl->type, "void"))
220 219 pdeclaration(name, decl, 2, ";\n");
221 220 f_print(fout, "\t} %s_u;\n", name);
222 221 f_print(fout, "};\n");
223 222 f_print(fout, "typedef struct %s %s;\n", name, name);
224 223 }
225 224
226 225 static void
227 226 pdefine(char *name, char *num)
228 227 {
229 228 f_print(fout, "#define\t%s %s\n", name, num);
230 229 }
231 230
232 231 static void
233 232 puldefine(char *name, char *num, enum rpc_gvc which)
234 233 {
235 234 switch (which) {
236 235 case PROGRAM:
237 236 case VERSION:
238 237 case PROCEDURE:
239 238 f_print(fout, "#define\t%s\t%s\n", name, num);
240 239 break;
241 240 default:
242 241 break;
243 242 }
244 243 }
245 244
246 245 static uint_t
247 246 define_printed(proc_list *stop, version_list *start)
248 247 {
249 248 version_list *vers;
250 249 proc_list *proc;
251 250
252 251 for (vers = start; vers != NULL; vers = vers->next) {
253 252 for (proc = vers->procs; proc != NULL; proc = proc->next) {
254 253 if (proc == stop)
↓ open down ↓ |
96 lines elided |
↑ open up ↑ |
255 254 return (0);
256 255 if (streq(proc->proc_name, stop->proc_name))
257 256 return (1);
258 257 }
259 258 }
260 259 abort();
261 260 /* NOTREACHED */
262 261 }
263 262
264 263 static void
265 -pfreeprocdef(char *name, char *vers, int mode)
264 +pfreeprocdef(char *name, char *vers)
266 265 {
267 266 f_print(fout, "extern int ");
268 267 pvname(name, vers);
269 - if (mode == 1)
270 - f_print(fout, "_freeresult(SVCXPRT *, xdrproc_t, caddr_t);\n");
271 - else
272 - f_print(fout, "_freeresult();\n");
268 + f_print(fout, "_freeresult(SVCXPRT *, xdrproc_t, caddr_t);\n");
273 269 }
274 270
275 271 static void
276 272 pprogramdef(definition *def)
277 273 {
278 274 version_list *vers;
279 275 proc_list *proc;
280 - int i;
281 - char *ext;
282 276
283 277 pargdef(def);
284 278
285 279 puldefine(def->def_name, def->def.pr.prog_num, PROGRAM);
286 280 for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
287 281 if (tblflag) {
288 282 f_print(fout,
289 283 "extern struct rpcgen_table %s_%s_table[];\n",
290 284 locase(def->def_name), vers->vers_num);
291 285 f_print(fout,
292 286 "extern int %s_%s_nproc;\n",
293 287 locase(def->def_name), vers->vers_num);
294 288 }
295 289 puldefine(vers->vers_name, vers->vers_num, VERSION);
296 290
297 - /*
298 - * Print out 2 definitions, one for ANSI-C, another for
299 - * old K & R C
300 - */
301 -
302 - if (!Cflag) {
303 - ext = "extern ";
304 - for (proc = vers->procs; proc != NULL;
305 - proc = proc->next) {
306 - if (!define_printed(proc, def->def.pr.versions))
307 - puldefine(proc->proc_name,
308 - proc->proc_num, PROCEDURE);
309 - f_print(fout, "%s", ext);
310 - pprocdef(proc, vers, NULL, 0, 2);
311 -
312 - if (mtflag) {
313 - f_print(fout, "%s", ext);
314 - pprocdef(proc, vers, NULL, 1, 2);
315 - }
291 + for (proc = vers->procs; proc != NULL;
292 + proc = proc->next) {
293 + if (!define_printed(proc,
294 + def->def.pr.versions)) {
295 + puldefine(proc->proc_name,
296 + proc->proc_num, PROCEDURE);
316 297 }
317 - pfreeprocdef(def->def_name, vers->vers_num, 2);
318 - } else {
319 - for (i = 1; i < 3; i++) {
320 - if (i == 1) {
321 - f_print(fout, "\n#if defined(__STDC__)"
322 - " || defined(__cplusplus)\n");
323 - ext = "extern ";
324 - } else {
325 - f_print(fout, "\n#else /* K&R C */\n");
326 - ext = "extern ";
327 - }
328 -
329 - for (proc = vers->procs; proc != NULL;
330 - proc = proc->next) {
331 - if (!define_printed(proc,
332 - def->def.pr.versions)) {
333 - puldefine(proc->proc_name,
334 - proc->proc_num, PROCEDURE);
335 - }
336 - f_print(fout, "%s", ext);
337 - pprocdef(proc, vers, "CLIENT *", 0, i);
338 - f_print(fout, "%s", ext);
339 - pprocdef(proc, vers,
340 - "struct svc_req *", 1, i);
341 - }
342 - pfreeprocdef(def->def_name, vers->vers_num, i);
343 - }
344 - f_print(fout, "#endif /* K&R C */\n");
298 + f_print(fout, "extern ");
299 + pprocdef(proc, vers, "CLIENT *", 0);
300 + f_print(fout, "extern ");
301 + pprocdef(proc, vers, "struct svc_req *", 1);
345 302 }
303 + pfreeprocdef(def->def_name, vers->vers_num);
346 304 }
347 305 }
348 306
349 307 void
350 -pprocdef(proc_list *proc, version_list *vp, char *addargtype, int server_p,
351 - int mode)
308 +pprocdef(proc_list *proc, version_list *vp, char *addargtype, int server_p)
352 309 {
353 310 if (mtflag) {
354 311 /* Print MT style stubs */
355 312 if (server_p)
356 313 f_print(fout, "bool_t ");
357 314 else
358 315 f_print(fout, "enum clnt_stat ");
359 316 } else {
360 317 ptype(proc->res_prefix, proc->res_type, 1);
361 318 f_print(fout, "* ");
362 319 }
363 320 if (server_p)
364 321 pvname_svc(proc->proc_name, vp->vers_num);
365 322 else
366 323 pvname(proc->proc_name, vp->vers_num);
367 324
368 - /*
369 - * mode 1 = ANSI-C, mode 2 = K&R C
370 - */
371 - if (mode == 1)
372 - parglist(proc, addargtype);
373 - else
374 - f_print(fout, "();\n");
325 + parglist(proc, addargtype);
375 326 }
376 327
377 328 /* print out argument list of procedure */
378 329 static void
379 330 parglist(proc_list *proc, char *addargtype)
380 331 {
381 332 decl_list *dl;
382 333 int oneway = streq(proc->res_type, "oneway");
383 334
384 335 f_print(fout, "(");
385 336 if (proc->arg_num < 2 && newstyle &&
386 337 streq(proc->args.decls->decl.type, "void")) {
387 338 /* 0 argument in new style: do nothing */
388 339 /* EMPTY */
389 340 } else {
390 341 for (dl = proc->args.decls; dl != NULL; dl = dl->next) {
391 342 ptype(dl->decl.prefix, dl->decl.type, 1);
392 343 if (!newstyle || (dl->decl.rel == REL_POINTER))
393 344 f_print(fout, "*");
394 345 /* old style passes by reference */
395 346 f_print(fout, ", ");
396 347 }
397 348 }
398 349
399 350 if (mtflag && !oneway) {
400 351 ptype(proc->res_prefix, proc->res_type, 1);
401 352 f_print(fout, "*, ");
402 353 }
403 354
404 355 f_print(fout, "%s);\n", addargtype);
405 356 }
406 357
407 358 static void
408 359 penumdef(definition *def)
409 360 {
410 361 char *name = def->def_name;
411 362 enumval_list *l;
412 363 char *last = NULL;
413 364 int count = 0;
414 365
415 366 f_print(fout, "enum %s {\n", name);
416 367 for (l = def->def.en.vals; l != NULL; l = l->next) {
417 368 f_print(fout, "\t%s", l->name);
418 369 if (l->assignment) {
419 370 f_print(fout, " = %s", l->assignment);
420 371 last = l->assignment;
421 372 count = 1;
422 373 } else {
423 374 if (last == NULL)
424 375 f_print(fout, " = %d", count++);
425 376 else
426 377 f_print(fout, " = %s + %d", last, count++);
427 378 }
428 379 if (l->next)
429 380 f_print(fout, ",\n");
430 381 else
431 382 f_print(fout, "\n");
432 383 }
433 384 f_print(fout, "};\n");
434 385 f_print(fout, "typedef enum %s %s;\n", name, name);
435 386 }
436 387
437 388 static void
438 389 ptypedef(definition *def)
439 390 {
440 391 char *name = def->def_name;
441 392 char *old = def->def.ty.old_type;
442 393 char prefix[8]; /* enough to contain "struct ", including NUL */
443 394 relation rel = def->def.ty.rel;
444 395
445 396
446 397 if (!streq(name, old)) {
447 398 if (streq(old, "string")) {
448 399 old = "char";
449 400 rel = REL_POINTER;
450 401 } else if (streq(old, "opaque")) {
451 402 old = "char";
452 403 } else if (streq(old, "bool")) {
453 404 old = "bool_t";
454 405 }
455 406 if (undefined2(old, name) && def->def.ty.old_prefix)
456 407 (void) snprintf(prefix, sizeof (prefix), "%s ",
457 408 def->def.ty.old_prefix);
458 409 else
459 410 prefix[0] = 0;
460 411 f_print(fout, "typedef ");
461 412 switch (rel) {
462 413 case REL_ARRAY:
463 414 f_print(fout, "struct {\n");
464 415 f_print(fout, "\tu_int %s_len;\n", name);
465 416 f_print(fout, "\t%s%s *%s_val;\n", prefix, old, name);
466 417 f_print(fout, "} %s", name);
467 418 break;
468 419 case REL_POINTER:
469 420 f_print(fout, "%s%s *%s", prefix, old, name);
470 421 break;
471 422 case REL_VECTOR:
472 423 f_print(fout, "%s%s %s[%s]", prefix, old, name,
473 424 def->def.ty.array_max);
474 425 break;
475 426 case REL_ALIAS:
476 427 f_print(fout, "%s%s %s", prefix, old, name);
477 428 break;
478 429 }
479 430 f_print(fout, ";\n");
480 431 }
481 432 }
482 433
483 434 void
484 435 pdeclaration(char *name, declaration *dec, int tab, char *separator)
485 436 {
486 437 char buf[8]; /* enough to hold "struct ", include NUL */
487 438 char *prefix;
488 439 char *type;
489 440
490 441 if (streq(dec->type, "void"))
491 442 return;
492 443 tabify(fout, tab);
493 444 if (streq(dec->type, name) && !dec->prefix)
494 445 f_print(fout, "struct ");
495 446 if (streq(dec->type, "string")) {
496 447 f_print(fout, "char *%s", dec->name);
497 448 } else {
498 449 prefix = "";
499 450 if (streq(dec->type, "bool")) {
500 451 type = "bool_t";
501 452 } else if (streq(dec->type, "opaque")) {
502 453 type = "char";
503 454 } else {
504 455 if (dec->prefix) {
505 456 (void) snprintf(buf, sizeof (buf),
506 457 "%s ", dec->prefix);
507 458 prefix = buf;
508 459 }
509 460 type = dec->type;
510 461 }
511 462 switch (dec->rel) {
512 463 case REL_ALIAS:
513 464 f_print(fout, "%s%s %s", prefix, type, dec->name);
514 465 break;
515 466 case REL_VECTOR:
516 467 f_print(fout, "%s%s %s[%s]", prefix, type, dec->name,
517 468 dec->array_max);
518 469 break;
519 470 case REL_POINTER:
520 471 f_print(fout, "%s%s *%s", prefix, type, dec->name);
521 472 break;
522 473 case REL_ARRAY:
523 474 f_print(fout, "struct {\n");
524 475 tabify(fout, tab);
525 476 f_print(fout, "\tu_int %s_len;\n", dec->name);
526 477 tabify(fout, tab);
527 478 f_print(fout,
528 479 "\t%s%s *%s_val;\n", prefix, type, dec->name);
529 480 tabify(fout, tab);
530 481 f_print(fout, "} %s", dec->name);
531 482 break;
532 483 }
533 484 }
534 485 /* LINTED variable format */
535 486 f_print(fout, separator);
536 487 }
537 488
538 489 static uint_t
539 490 undefined2(char *type, char *stop)
540 491 {
541 492 list *l;
542 493 definition *def;
543 494
544 495 for (l = defined; l != NULL; l = l->next) {
545 496 def = (definition *) l->val;
546 497 if (def->def_kind != DEF_PROGRAM) {
547 498 if (streq(def->def_name, stop))
548 499 return (1);
549 500 if (streq(def->def_name, type))
550 501 return (0);
551 502 }
552 503 }
553 504 return (1);
554 505 }
↓ open down ↓ |
170 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX