Print this page
rpcgen should only produce ANSI code
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/rpcgen/rpc_cout.c
+++ new/usr/src/cmd/rpcgen/rpc_cout.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_cout.c, XDR routine outputter for the RPC protocol compiler
40 42 */
41 43 #include <stdio.h>
42 44 #include <stdlib.h>
43 45 #include <string.h>
44 46 #include <ctype.h>
45 47 #include "rpc_parse.h"
46 48 #include "rpc_util.h"
47 49
48 50 extern void crash(void);
49 51
50 52 static void print_header(definition *);
51 53 static void print_trailer(void);
52 54 static void emit_enum(definition *);
53 55 static void emit_program(definition *);
54 56 static void emit_union(definition *);
55 57 static void emit_struct(definition *);
56 58 static void emit_typedef(definition *);
57 59 static void print_stat(int, declaration *);
58 60 static void emit_inline(int, declaration *, int);
59 61 static void emit_inline64(int, declaration *, int);
60 62 static void emit_single_in_line(int, declaration *, int, relation);
61 63 static void emit_single_in_line64(int, declaration *, int, relation);
62 64 static char *upcase(char *);
63 65
64 66 /*
65 67 * Emit the C-routine for the given definition
66 68 */
67 69 void
68 70 emit(definition *def)
69 71 {
70 72 if (def->def_kind == DEF_CONST)
71 73 return;
72 74 if (def->def_kind == DEF_PROGRAM) {
73 75 emit_program(def);
74 76 return;
75 77 }
76 78 if (def->def_kind == DEF_TYPEDEF) {
77 79 /*
78 80 * now we need to handle declarations like
79 81 * struct typedef foo foo;
80 82 * since we dont want this to be expanded into 2 calls
81 83 * to xdr_foo
82 84 */
83 85
84 86 if (strcmp(def->def.ty.old_type, def->def_name) == 0)
85 87 return;
86 88 };
87 89 print_header(def);
88 90 switch (def->def_kind) {
89 91 case DEF_UNION:
90 92 emit_union(def);
91 93 break;
92 94 case DEF_ENUM:
93 95 emit_enum(def);
94 96 break;
95 97 case DEF_STRUCT:
96 98 emit_struct(def);
97 99 break;
98 100 case DEF_TYPEDEF:
99 101 emit_typedef(def);
100 102 break;
101 103 }
102 104 print_trailer();
103 105 }
104 106
105 107 static int
106 108 findtype(definition *def, char *type)
107 109 {
108 110
109 111 if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST)
110 112 return (0);
111 113 return (streq(def->def_name, type));
112 114 }
113 115
114 116 static int
115 117 undefined(char *type)
116 118 {
117 119 definition *def;
118 120
↓ open down ↓ |
86 lines elided |
↑ open up ↑ |
119 121 def = (definition *)FINDVAL(defined, type, findtype);
120 122 return (def == NULL);
121 123 }
122 124
123 125
124 126 static void
125 127 print_generic_header(char *procname, int pointerp)
126 128 {
127 129 f_print(fout, "\n");
128 130 f_print(fout, "bool_t\n");
129 - if (Cflag) {
130 - f_print(fout, "xdr_%s(", procname);
131 - f_print(fout, "XDR *xdrs, ");
132 - f_print(fout, "%s ", procname);
133 - if (pointerp)
134 - f_print(fout, "*");
135 - f_print(fout, "objp)\n{\n\n");
136 - } else {
137 - f_print(fout, "xdr_%s(xdrs, objp)\n", procname);
138 - f_print(fout, "\tXDR *xdrs;\n");
139 - f_print(fout, "\t%s ", procname);
140 - if (pointerp)
141 - f_print(fout, "*");
142 - f_print(fout, "objp;\n{\n\n");
143 - }
131 + f_print(fout, "xdr_%s(", procname);
132 + f_print(fout, "XDR *xdrs, ");
133 + f_print(fout, "%s ", procname);
134 + if (pointerp)
135 + f_print(fout, "*");
136 + f_print(fout, "objp)\n{\n\n");
144 137 }
145 138
146 139 static void
147 140 print_header(definition *def)
148 141 {
149 142 print_generic_header(def->def_name,
150 143 def->def_kind != DEF_TYPEDEF ||
151 144 !isvectordef(def->def.ty.old_type, def->def.ty.rel));
152 145 /* Now add Inline support */
153 146
154 147 if (inlinelen == 0)
155 148 return;
156 149 /* May cause lint to complain. but ... */
157 150 f_print(fout, "\trpc_inline_t *buf;\n\n");
158 151 }
159 152
160 153 static void
161 154 print_prog_header(proc_list *plist)
162 155 {
163 156 print_generic_header(plist->args.argname, 1);
164 157 }
165 158
166 159 static void
167 160 print_trailer(void)
168 161 {
169 162 f_print(fout, "\treturn (TRUE);\n");
170 163 f_print(fout, "}\n");
171 164 }
172 165
173 166
174 167 static void
175 168 print_ifopen(int indent, char *name)
176 169 {
177 170 tabify(fout, indent);
178 171 if (streq(name, "rpcprog_t") ||
179 172 streq(name, "rpcvers_t") ||
180 173 streq(name, "rpcproc_t") ||
181 174 streq(name, "rpcprot_t") ||
182 175 streq(name, "rpcport_t"))
183 176 (void) strtok(name, "_");
184 177 f_print(fout, "if (!xdr_%s(xdrs", name);
185 178 }
186 179
187 180 static void
188 181 print_ifarg(char *arg)
189 182 {
190 183 f_print(fout, ", %s", arg);
191 184 }
192 185
193 186 static void
194 187 print_ifsizeof(int indent, char *prefix, char *type)
195 188 {
196 189 if (indent) {
197 190 f_print(fout, ",\n");
198 191 tabify(fout, indent);
199 192 } else {
200 193 f_print(fout, ", ");
201 194 }
202 195 if (streq(type, "bool")) {
203 196 f_print(fout, "sizeof (bool_t), (xdrproc_t)xdr_bool");
204 197 } else {
205 198 f_print(fout, "sizeof (");
206 199 if (undefined(type) && prefix) {
207 200 f_print(fout, "%s ", prefix);
208 201 }
209 202 f_print(fout, "%s), (xdrproc_t)xdr_%s", type, type);
210 203 }
211 204 }
212 205
213 206 static void
214 207 print_ifclose(int indent)
215 208 {
216 209 f_print(fout, "))\n");
217 210 tabify(fout, indent);
218 211 f_print(fout, "\treturn (FALSE);\n");
219 212 }
220 213
221 214 static void
222 215 print_ifstat(int indent, char *prefix, char *type, relation rel,
223 216 char *amax, char *objname, char *name)
224 217 {
225 218 char *alt = NULL;
226 219
227 220 switch (rel) {
228 221 case REL_POINTER:
229 222 print_ifopen(indent, "pointer");
230 223 print_ifarg("(char **)");
231 224 f_print(fout, "%s", objname);
232 225 print_ifsizeof(0, prefix, type);
233 226 break;
234 227 case REL_VECTOR:
235 228 if (streq(type, "string"))
236 229 alt = "string";
237 230 else if (streq(type, "opaque"))
238 231 alt = "opaque";
239 232 if (alt) {
240 233 print_ifopen(indent, alt);
241 234 print_ifarg(objname);
242 235 } else {
243 236 print_ifopen(indent, "vector");
244 237 print_ifarg("(char *)");
245 238 f_print(fout, "%s", objname);
246 239 }
247 240 print_ifarg(amax);
248 241 if (!alt)
249 242 print_ifsizeof(indent + 1, prefix, type);
250 243 break;
251 244 case REL_ARRAY:
252 245 if (streq(type, "string"))
253 246 alt = "string";
254 247 else if (streq(type, "opaque"))
255 248 alt = "bytes";
256 249 if (streq(type, "string")) {
257 250 print_ifopen(indent, alt);
258 251 print_ifarg(objname);
259 252 } else {
260 253 if (alt)
261 254 print_ifopen(indent, alt);
262 255 else
263 256 print_ifopen(indent, "array");
264 257 print_ifarg("(char **)");
265 258 if (*objname == '&')
266 259 f_print(fout, "%s.%s_val, (u_int *) %s.%s_len",
267 260 objname, name, objname, name);
268 261 else
269 262 f_print(fout,
270 263 "&%s->%s_val, (u_int *) &%s->%s_len",
271 264 objname, name, objname, name);
272 265 }
273 266 print_ifarg(amax);
274 267 if (!alt)
275 268 print_ifsizeof(indent + 1, prefix, type);
276 269 break;
277 270 case REL_ALIAS:
278 271 print_ifopen(indent, type);
279 272 print_ifarg(objname);
280 273 break;
281 274 }
282 275 print_ifclose(indent);
283 276 }
284 277
285 278 /* ARGSUSED */
286 279 static void
287 280 emit_enum(definition *def)
288 281 {
289 282 print_ifopen(1, "enum");
290 283 print_ifarg("(enum_t *)objp");
291 284 print_ifclose(1);
292 285 }
293 286
294 287 static void
295 288 emit_program(definition *def)
296 289 {
297 290 decl_list *dl;
298 291 version_list *vlist;
299 292 proc_list *plist;
300 293
301 294 for (vlist = def->def.pr.versions; vlist != NULL; vlist = vlist->next)
302 295 for (plist = vlist->procs; plist != NULL; plist = plist->next) {
303 296 if (!newstyle || plist->arg_num < 2)
304 297 continue; /* old style, or single argument */
305 298 print_prog_header(plist);
306 299 for (dl = plist->args.decls; dl != NULL;
307 300 dl = dl->next)
308 301 print_stat(1, &dl->decl);
309 302 print_trailer();
310 303 }
311 304 }
312 305
313 306
314 307 static void
315 308 emit_union(definition *def)
316 309 {
317 310 declaration *dflt;
318 311 case_list *cl;
319 312 declaration *cs;
320 313 char *object;
321 314
322 315 print_stat(1, &def->def.un.enum_decl);
323 316 f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
324 317 for (cl = def->def.un.cases; cl != NULL; cl = cl->next) {
325 318
326 319 f_print(fout, "\tcase %s:\n", cl->case_name);
327 320 if (cl->contflag == 1) /* a continued case statement */
328 321 continue;
329 322 cs = &cl->case_decl;
330 323 if (!streq(cs->type, "void")) {
331 324 size_t len = strlen(def->def_name) +
332 325 strlen("&objp->%s_u.%s") +
333 326 strlen(cs->name) + 1;
334 327 object = malloc(len);
335 328 if (isvectordef(cs->type, cs->rel))
336 329 (void) snprintf(object, len, "objp->%s_u.%s",
337 330 def->def_name, cs->name);
338 331 else
339 332 (void) snprintf(object, len, "&objp->%s_u.%s",
340 333 def->def_name, cs->name);
341 334 print_ifstat(2, cs->prefix, cs->type, cs->rel,
342 335 cs->array_max, object, cs->name);
343 336 free(object);
344 337 }
345 338 f_print(fout, "\t\tbreak;\n");
346 339 }
347 340 dflt = def->def.un.default_decl;
348 341 if (dflt != NULL) {
349 342 if (!streq(dflt->type, "void")) {
350 343 size_t len = strlen(def->def_name) +
351 344 strlen("&objp->%s_u.%s") +
352 345 strlen(dflt->name) + 1;
353 346 f_print(fout, "\tdefault:\n");
354 347 object = malloc(len);
355 348 if (isvectordef(dflt->type, dflt->rel))
356 349 (void) snprintf(object, len, "objp->%s_u.%s",
357 350 def->def_name, dflt->name);
358 351 else
359 352 (void) snprintf(object, len, "&objp->%s_u.%s",
360 353 def->def_name, dflt->name);
361 354
362 355 print_ifstat(2, dflt->prefix, dflt->type, dflt->rel,
363 356 dflt->array_max, object, dflt->name);
364 357 free(object);
365 358 f_print(fout, "\t\tbreak;\n");
366 359 }
367 360 } else {
368 361 f_print(fout, "\tdefault:\n");
369 362 f_print(fout, "\t\treturn (FALSE);\n");
370 363 }
371 364
372 365 f_print(fout, "\t}\n");
373 366 }
374 367
375 368 static void
376 369 expand_inline(int indent, const char *sizestr,
377 370 int size, int flag, decl_list *dl, decl_list *cur)
378 371 {
379 372 decl_list *psav;
380 373
381 374 /*
382 375 * were already looking at a xdr_inlineable structure
383 376 */
384 377 tabify(fout, indent + 1);
385 378 if (sizestr == NULL)
386 379 f_print(fout,
387 380 "buf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
388 381 size);
389 382 else if (size == 0)
390 383 f_print(fout,
391 384 "buf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
392 385 sizestr);
393 386 else
394 387 f_print(fout,
395 388 "buf = XDR_INLINE(xdrs, (%d + (%s)) "
396 389 "* BYTES_PER_XDR_UNIT);", size, sizestr);
397 390
398 391 f_print(fout, "\n");
399 392 tabify(fout, indent + 1);
400 393 f_print(fout, "if (buf == NULL) {\n");
401 394
402 395 psav = cur;
403 396 while (cur != dl) {
404 397 print_stat(indent + 2,
405 398 &cur->decl);
406 399 cur = cur->next;
407 400 }
408 401
409 402 tabify(fout, indent+1);
410 403 f_print(fout, "} else {\n");
411 404
412 405 f_print(fout, "#if defined(_LP64) || defined(_KERNEL)\n");
413 406 cur = psav;
414 407 while (cur != dl) {
415 408 emit_inline64(indent + 2, &cur->decl, flag);
416 409 cur = cur->next;
417 410 }
418 411 f_print(fout, "#else\n");
419 412 cur = psav;
420 413 while (cur != dl) {
421 414 emit_inline(indent + 2, &cur->decl, flag);
422 415 cur = cur->next;
423 416 }
424 417 f_print(fout, "#endif\n");
425 418
426 419 tabify(fout, indent + 1);
427 420 f_print(fout, "}\n");
428 421 }
429 422
430 423 /*
431 424 * An inline type is a base type (interger type) or a vector of base types.
432 425 */
433 426 static int
434 427 inline_type(declaration *dc, int *size)
435 428 {
436 429 bas_type *ptr;
437 430
438 431 *size = 0;
439 432
440 433 if (dc->prefix == NULL &&
441 434 (dc->rel == REL_ALIAS || dc->rel == REL_VECTOR)) {
442 435 ptr = find_type(dc->type);
443 436 if (ptr != NULL) {
444 437 *size = ptr->length;
445 438 return (1);
446 439 }
447 440 }
448 441
449 442 return (0);
450 443 }
451 444
452 445 static char *
453 446 arraysize(char *sz, declaration *dc, int elsize)
454 447 {
455 448 int len;
456 449 int elsz = elsize;
457 450 int digits;
458 451 int slen = 0;
459 452 char *plus = "";
460 453 char *tmp;
461 454 size_t tlen;
462 455
463 456 /*
464 457 * Calculate the size of a string to hold the size of all arrays
465 458 * to be inlined.
466 459 *
467 460 * We have the string representation of the total size that has already
468 461 * been seen. (Null if this is the first array).
469 462 * We have the string representation of array max from the declaration,
470 463 * optionally the plus string, " + ", if this is not the first array,
471 464 * and the number of digits for the element size for this declaration.
472 465 */
473 466 if (sz != NULL) {
474 467 plus = " + ";
475 468 slen = strlen(sz);
476 469 }
477 470
478 471 /* Calculate the number of digits to hold the element size */
479 472 for (digits = 1; elsz >= 10; digits++)
480 473 elsz /= 10;
481 474
482 475 /*
483 476 * If elsize != 1 the allocate 3 extra bytes for the times
484 477 * string, " * ", the "()" below, and the digits. One extra
485 478 * for the trailing NULL
486 479 */
487 480 len = strlen(dc->array_max) + (elsize == 1 ? 0 : digits + 5) + 1;
488 481 tlen = slen + len + strlen(plus);
489 482 tmp = realloc(sz, tlen);
490 483 if (tmp == NULL) {
491 484 f_print(stderr, "Fatal error : no memory\n");
492 485 crash();
493 486 }
494 487
495 488 if (elsize == 1)
496 489 (void) snprintf(tmp + slen, tlen - slen, "%s%s",
497 490 plus, dc->array_max);
498 491 else
499 492 (void) snprintf(tmp + slen, tlen - slen, "%s(%s) * %d",
500 493 plus, dc->array_max, elsize);
501 494
502 495 return (tmp);
503 496 }
504 497
505 498 static void
506 499 inline_struct(decl_list *dl, decl_list *last, int flag, int indent)
507 500 {
508 501 int size, tsize;
509 502 decl_list *cur;
510 503 char *sizestr;
511 504
512 505 cur = NULL;
513 506 tsize = 0;
514 507 sizestr = NULL;
515 508 for (; dl != last; dl = dl->next) {
516 509 if (inline_type(&dl->decl, &size)) {
517 510 if (cur == NULL)
518 511 cur = dl;
519 512
520 513 if (dl->decl.rel == REL_ALIAS)
521 514 tsize += size;
522 515 else {
523 516 /* this code is required to handle arrays */
524 517 sizestr = arraysize(sizestr, &dl->decl, size);
525 518 }
526 519 } else {
527 520 if (cur != NULL)
528 521 if (sizestr == NULL && tsize < inlinelen) {
529 522 /*
530 523 * don't expand into inline code
531 524 * if tsize < inlinelen
532 525 */
533 526 while (cur != dl) {
534 527 print_stat(indent + 1,
535 528 &cur->decl);
536 529 cur = cur->next;
537 530 }
538 531 } else {
539 532 expand_inline(indent, sizestr,
540 533 tsize, flag, dl, cur);
541 534 }
542 535 tsize = 0;
543 536 cur = NULL;
544 537 sizestr = NULL;
545 538 print_stat(indent + 1, &dl->decl);
546 539 }
547 540 }
548 541
549 542 if (cur == NULL)
550 543 return;
551 544 if (sizestr == NULL && tsize < inlinelen) {
552 545 /* don't expand into inline code if tsize < inlinelen */
553 546 while (cur != dl) {
554 547 print_stat(indent + 1, &cur->decl);
555 548 cur = cur->next;
556 549 }
557 550 } else {
558 551 expand_inline(indent, sizestr, tsize, flag, dl, cur);
559 552 }
560 553 }
561 554
562 555 /*
563 556 * Check if we can inline this structure. While we are at it check if the
564 557 * declaration list has any vectors defined of "basic" types.
565 558 */
566 559 static int
567 560 check_inline(decl_list *dl, int inlinelen, int *have_vector)
568 561 {
569 562 int tsize = 0;
570 563 int size;
571 564 int doinline = 0;
572 565
573 566 *have_vector = 0;
574 567 if (inlinelen == 0)
575 568 return (0);
576 569
577 570 for (; dl != NULL; dl = dl->next) {
578 571 if (!inline_type(&dl->decl, &size)) {
579 572 tsize = 0;
580 573 continue;
581 574 }
582 575 if (dl->decl.rel == REL_VECTOR) {
583 576 *have_vector = 1;
584 577 return (1);
585 578 }
586 579 tsize += size;
587 580 if (tsize >= inlinelen)
588 581 doinline = 1;
589 582 }
590 583
591 584 return (doinline);
592 585 }
593 586
594 587
595 588 static void
596 589 emit_struct_tail_recursion(definition *defp, int can_inline)
597 590 {
598 591 int indent = 3;
599 592 struct_def *sp = &defp->def.st;
600 593 decl_list *dl;
601 594
602 595
603 596 f_print(fout, "\t%s *tmp_%s;\n",
604 597 defp->def_name, defp->def_name);
605 598
606 599 f_print(fout, "\tbool_t more_data = TRUE;\n");
607 600 f_print(fout, "\tbool_t first_objp = TRUE;\n\n");
608 601
609 602 f_print(fout, "\n\tif (xdrs->x_op == XDR_DECODE) {\n");
610 603 f_print(fout, "\n\t\twhile (more_data) {\n");
611 604 f_print(fout, "\n\t\t\tvoid bzero();\n\n");
612 605
613 606 if (can_inline)
614 607 inline_struct(sp->decls, sp->tail, GET, indent);
615 608 else
616 609 for (dl = sp->decls; dl != NULL && dl != sp->tail;
617 610 dl = dl->next)
618 611 print_stat(indent, &dl->decl);
619 612
620 613 f_print(fout, "\t\t\tif (!xdr_bool(xdrs, "
621 614 "&more_data))\n\t\t\t\treturn (FALSE);\n");
622 615
623 616 f_print(fout, "\n\t\t\tif (!more_data) {\n");
624 617 f_print(fout, "\t\t\t\tobjp->%s = NULL;\n", sp->tail->decl.name);
625 618 f_print(fout, "\t\t\t\tbreak;\n");
626 619 f_print(fout, "\t\t\t}\n\n");
627 620 f_print(fout, "\t\t\tif (objp->%s == NULL) {\n", sp->tail->decl.name);
628 621 f_print(fout, "\t\t\t\tobjp->%s = "
629 622 "(%s *)\n\t\t\t\t\tmem_alloc(sizeof (%s));\n",
630 623 sp->tail->decl.name, defp->def_name, defp->def_name);
631 624
632 625 f_print(fout, "\t\t\t\tif (objp->%s == NULL)\n"
633 626 "\t\t\t\t\treturn (FALSE);\n", sp->tail->decl.name);
634 627 f_print(fout, "\t\t\t\tbzero(objp->%s, sizeof (%s));\n",
635 628 sp->tail->decl.name, defp->def_name);
636 629 f_print(fout, "\t\t\t}\n");
637 630 f_print(fout, "\t\t\tobjp = objp->%s;\n", sp->tail->decl.name);
638 631 f_print(fout, "\t\t}\n");
639 632
640 633 f_print(fout, "\n\t} else if (xdrs->x_op == XDR_ENCODE) {\n");
641 634 f_print(fout, "\n\t\twhile (more_data) {\n");
642 635
643 636 if (can_inline)
644 637 inline_struct(sp->decls, sp->tail, PUT, indent);
645 638 else
646 639 for (dl = sp->decls; dl != NULL && dl != sp->tail;
647 640 dl = dl->next)
648 641 print_stat(indent, &dl->decl);
649 642
650 643 f_print(fout, "\t\t\tobjp = objp->%s;\n", sp->tail->decl.name);
651 644 f_print(fout, "\t\t\tif (objp == NULL)\n");
652 645 f_print(fout, "\t\t\t\tmore_data = FALSE;\n");
653 646
654 647 f_print(fout, "\t\t\tif (!xdr_bool(xdrs, &more_data))\n"
655 648 "\t\t\t\treturn (FALSE);\n");
656 649
657 650 f_print(fout, "\t\t}\n");
658 651
659 652 f_print(fout, "\n\t} else {\n");
660 653 f_print(fout, "\n\t\twhile (more_data) {\n");
661 654
662 655 for (dl = sp->decls; dl != NULL && dl != sp->tail; dl = dl->next)
663 656 print_stat(indent, &dl->decl);
664 657
665 658 f_print(fout, "\t\t\ttmp_%s = objp;\n", defp->def_name);
666 659 f_print(fout, "\t\t\tobjp = objp->%s;\n", sp->tail->decl.name);
667 660
668 661 f_print(fout, "\t\t\tif (objp == NULL)\n");
669 662 f_print(fout, "\t\t\t\tmore_data = FALSE;\n");
670 663
671 664 f_print(fout, "\t\t\tif (!first_objp)\n");
672 665
673 666 f_print(fout, "\t\t\t\tmem_free(tmp_%s, sizeof (%s));\n",
674 667 defp->def_name, defp->def_name);
675 668
676 669 f_print(fout, "\t\t\telse\n\t\t\t\tfirst_objp = FALSE;\n\t\t}\n");
677 670
678 671 f_print(fout, "\n\t}\n");
679 672 }
680 673
681 674 static void
682 675 emit_struct(definition *def)
683 676 {
684 677 decl_list *dl = def->def.st.decls;
685 678 int can_inline, have_vector;
686 679
687 680
688 681 can_inline = check_inline(dl, inlinelen, &have_vector);
689 682 if (have_vector)
690 683 f_print(fout, "\tint i;\n");
691 684
692 685
693 686 if (rflag && def->def.st.self_pointer) {
694 687 /* Handle tail recursion elimination */
695 688 emit_struct_tail_recursion(def, can_inline);
696 689 return;
697 690 }
698 691
699 692
700 693 if (can_inline) {
701 694 f_print(fout, "\n\tif (xdrs->x_op == XDR_ENCODE) {\n");
702 695 inline_struct(dl, NULL, PUT, 1);
703 696
704 697 f_print(fout, "\t\treturn (TRUE);\n\t}"
705 698 " else if (xdrs->x_op == XDR_DECODE) {\n");
706 699
707 700 inline_struct(dl, NULL, GET, 1);
708 701 f_print(fout, "\t\treturn (TRUE);\n\t}\n\n");
709 702 }
710 703
711 704 /* now take care of XDR_FREE inline case or the non-inline cases */
712 705
713 706 for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
714 707 print_stat(1, &dl->decl);
715 708
716 709 }
717 710
718 711 static void
719 712 emit_typedef(definition *def)
720 713 {
721 714 char *prefix = def->def.ty.old_prefix;
722 715 char *type = def->def.ty.old_type;
723 716 char *amax = def->def.ty.array_max;
724 717 relation rel = def->def.ty.rel;
725 718
726 719 print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
727 720 }
728 721
729 722 static void
730 723 print_stat(int indent, declaration *dec)
731 724 {
732 725 char *prefix = dec->prefix;
733 726 char *type = dec->type;
734 727 char *amax = dec->array_max;
735 728 relation rel = dec->rel;
736 729 char name[256];
737 730
738 731 if (isvectordef(type, rel))
739 732 (void) snprintf(name, sizeof (name), "objp->%s", dec->name);
740 733 else
741 734 (void) snprintf(name, sizeof (name), "&objp->%s", dec->name);
742 735 print_ifstat(indent, prefix, type, rel, amax, name, dec->name);
743 736 }
744 737
745 738
746 739 static void
747 740 emit_inline(int indent, declaration *decl, int flag)
748 741 {
749 742 switch (decl->rel) {
750 743 case REL_ALIAS :
751 744 emit_single_in_line(indent, decl, flag, REL_ALIAS);
752 745 break;
753 746 case REL_VECTOR :
754 747 tabify(fout, indent);
755 748 f_print(fout, "{\n");
756 749 tabify(fout, indent + 1);
757 750 f_print(fout, "%s *genp;\n\n", decl->type);
758 751 tabify(fout, indent + 1);
759 752 f_print(fout,
760 753 "for (i = 0, genp = objp->%s;\n", decl->name);
761 754 tabify(fout, indent + 2);
762 755 f_print(fout, "i < %s; i++) {\n", decl->array_max);
763 756 emit_single_in_line(indent + 2, decl, flag, REL_VECTOR);
764 757 tabify(fout, indent + 1);
765 758 f_print(fout, "}\n");
766 759 tabify(fout, indent);
767 760 f_print(fout, "}\n");
768 761 }
769 762 }
770 763
771 764 static void
772 765 emit_inline64(int indent, declaration *decl, int flag)
773 766 {
774 767 switch (decl->rel) {
775 768 case REL_ALIAS :
776 769 emit_single_in_line64(indent, decl, flag, REL_ALIAS);
777 770 break;
778 771 case REL_VECTOR :
779 772 tabify(fout, indent);
780 773 f_print(fout, "{\n");
781 774 tabify(fout, indent + 1);
782 775 f_print(fout, "%s *genp;\n\n", decl->type);
783 776 tabify(fout, indent + 1);
784 777 f_print(fout,
785 778 "for (i = 0, genp = objp->%s;\n", decl->name);
786 779 tabify(fout, indent + 2);
787 780 f_print(fout, "i < %s; i++) {\n", decl->array_max);
788 781 emit_single_in_line64(indent + 2, decl, flag, REL_VECTOR);
789 782 tabify(fout, indent + 1);
790 783 f_print(fout, "}\n");
791 784 tabify(fout, indent);
792 785 f_print(fout, "}\n");
793 786 }
794 787 }
795 788
796 789 static void
797 790 emit_single_in_line(int indent, declaration *decl, int flag, relation rel)
798 791 {
799 792 char *upp_case;
800 793 int freed = 0;
801 794
802 795 tabify(fout, indent);
803 796 if (flag == PUT)
804 797 f_print(fout, "IXDR_PUT_");
805 798 else
806 799 if (rel == REL_ALIAS)
807 800 f_print(fout, "objp->%s = IXDR_GET_", decl->name);
808 801 else
809 802 f_print(fout, "*genp++ = IXDR_GET_");
810 803
811 804 upp_case = upcase(decl->type);
812 805
813 806 /* hack - XX */
814 807 if (strcmp(upp_case, "INT") == 0) {
815 808 free(upp_case);
816 809 freed = 1;
817 810 upp_case = "LONG";
818 811 }
819 812 if ((strcmp(upp_case, "U_INT") == 0) ||
820 813 (strcmp(upp_case, "RPCPROG") == 0) ||
821 814 (strcmp(upp_case, "RPCVERS") == 0) ||
822 815 (strcmp(upp_case, "RPCPROC") == 0) ||
823 816 (strcmp(upp_case, "RPCPROT") == 0) ||
824 817 (strcmp(upp_case, "RPCPORT") == 0)) {
825 818 free(upp_case);
826 819 freed = 1;
827 820 upp_case = "U_LONG";
828 821 }
829 822
830 823 if (flag == PUT)
831 824 if (rel == REL_ALIAS)
832 825 f_print(fout,
833 826 "%s(buf, objp->%s);\n", upp_case, decl->name);
834 827 else
835 828 f_print(fout, "%s(buf, *genp++);\n", upp_case);
836 829
837 830 else
838 831 f_print(fout, "%s(buf);\n", upp_case);
839 832 if (!freed)
840 833 free(upp_case);
841 834 }
842 835
843 836 static void
844 837 emit_single_in_line64(int indent, declaration *decl, int flag, relation rel)
845 838 {
846 839 char *upp_case;
847 840 int freed = 0;
848 841
849 842 tabify(fout, indent);
850 843 if (flag == PUT)
851 844 f_print(fout, "IXDR_PUT_");
852 845 else
853 846 if (rel == REL_ALIAS)
854 847 f_print(fout, "objp->%s = IXDR_GET_", decl->name);
855 848 else
856 849 f_print(fout, "*genp++ = IXDR_GET_");
857 850
858 851 upp_case = upcase(decl->type);
859 852
860 853 /* hack - XX */
861 854 if ((strcmp(upp_case, "INT") == 0)||(strcmp(upp_case, "LONG") == 0)) {
862 855 free(upp_case);
863 856 freed = 1;
864 857 upp_case = "INT32";
865 858 }
866 859 if ((strcmp(upp_case, "U_INT") == 0) ||
867 860 (strcmp(upp_case, "U_LONG") == 0) ||
868 861 (strcmp(upp_case, "RPCPROG") == 0) ||
869 862 (strcmp(upp_case, "RPCVERS") == 0) ||
870 863 (strcmp(upp_case, "RPCPROC") == 0) ||
871 864 (strcmp(upp_case, "RPCPROT") == 0) ||
872 865 (strcmp(upp_case, "RPCPORT") == 0)) {
873 866 free(upp_case);
874 867 freed = 1;
875 868 upp_case = "U_INT32";
876 869 }
877 870
878 871 if (flag == PUT)
879 872 if (rel == REL_ALIAS)
880 873 f_print(fout,
881 874 "%s(buf, objp->%s);\n", upp_case, decl->name);
882 875 else
883 876 f_print(fout, "%s(buf, *genp++);\n", upp_case);
884 877
885 878 else
886 879 f_print(fout, "%s(buf);\n", upp_case);
887 880 if (!freed)
888 881 free(upp_case);
889 882 }
890 883
891 884 static char *
892 885 upcase(char *str)
893 886 {
894 887 char *ptr, *hptr;
895 888
896 889 ptr = malloc(strlen(str)+1);
897 890 if (ptr == NULL) {
898 891 f_print(stderr, "malloc failed\n");
899 892 exit(1);
900 893 };
901 894
902 895 hptr = ptr;
903 896 while (*str != '\0')
904 897 *ptr++ = toupper(*str++);
905 898
906 899 *ptr = '\0';
907 900 return (hptr);
908 901 }
↓ open down ↓ |
755 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX