rpc_cout.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. /*
  2. * From: @(#)rpc_cout.c 1.13 89/02/22
  3. *
  4. * Copyright (c) 2010, Oracle America, Inc.
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following
  13. * disclaimer in the documentation and/or other materials
  14. * provided with the distribution.
  15. * * Neither the name of the "Oracle America, Inc." nor the names of its
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  24. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  26. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. /*
  33. * rpc_cout.c, XDR routine outputter for the RPC protocol compiler
  34. */
  35. #include <ctype.h>
  36. #include <stdio.h>
  37. #include <string.h>
  38. #include "rpc_parse.h"
  39. #include "rpc_util.h"
  40. #include "proto.h"
  41. static void emit_enum (definition * def);
  42. static void emit_program (const definition * def);
  43. static void emit_union (const definition * def);
  44. static void emit_struct (definition * def);
  45. static void emit_typedef (const definition * def);
  46. static void emit_inline (int indent, declaration * decl, int flag);
  47. static void emit_single_in_line (int indent, declaration *decl, int flag,
  48. relation rel);
  49. static int findtype (const definition * def, const char *type);
  50. static int undefined (const char *type);
  51. static void print_generic_header (const char *procname, int pointerp);
  52. static void print_ifopen (int indent, const char *name);
  53. static void print_ifarg (const char *arg);
  54. static void print_ifsizeof (int indent, const char *prefix, const char *type);
  55. static void print_ifclose (int indent);
  56. static void print_ifstat (int indent, const char *prefix, const char *type,
  57. relation rel, const char *amax,
  58. const char *objname, const char *name);
  59. static void print_stat (int indent, const declaration * dec);
  60. static void print_header (const definition * def);
  61. static void print_trailer (void);
  62. static char *upcase (const char *str);
  63. /*
  64. * Emit the C-routine for the given definition
  65. */
  66. void
  67. emit (definition * def)
  68. {
  69. if (def->def_kind == DEF_CONST)
  70. {
  71. return;
  72. }
  73. if (def->def_kind == DEF_PROGRAM)
  74. {
  75. emit_program (def);
  76. return;
  77. }
  78. if (def->def_kind == DEF_TYPEDEF)
  79. {
  80. /* now we need to handle declarations like
  81. struct typedef foo foo;
  82. since we don't want this to be expanded
  83. into 2 calls to xdr_foo */
  84. if (strcmp (def->def.ty.old_type, def->def_name) == 0)
  85. return;
  86. };
  87. print_header (def);
  88. switch (def->def_kind)
  89. {
  90. case DEF_UNION:
  91. emit_union (def);
  92. break;
  93. case DEF_ENUM:
  94. emit_enum (def);
  95. break;
  96. case DEF_STRUCT:
  97. emit_struct (def);
  98. break;
  99. case DEF_TYPEDEF:
  100. emit_typedef (def);
  101. break;
  102. default:
  103. /* can't happen */
  104. break;
  105. }
  106. print_trailer ();
  107. }
  108. static int
  109. findtype (const definition * def, const char *type)
  110. {
  111. if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST)
  112. {
  113. return 0;
  114. }
  115. else
  116. {
  117. return (streq (def->def_name, type));
  118. }
  119. }
  120. static int
  121. undefined (const char *type)
  122. {
  123. definition *def;
  124. def = (definition *) FINDVAL (defined, type, findtype);
  125. return (def == NULL);
  126. }
  127. static void
  128. print_generic_header (const char *procname, int pointerp)
  129. {
  130. f_print (fout, "\n");
  131. f_print (fout, "bool_t\n");
  132. if (Cflag)
  133. {
  134. f_print (fout, "xdr_%s (", procname);
  135. f_print (fout, "XDR *xdrs, ");
  136. f_print (fout, "%s ", procname);
  137. if (pointerp)
  138. f_print (fout, "*");
  139. f_print (fout, "objp)\n{\n");
  140. }
  141. else
  142. {
  143. f_print (fout, "xdr_%s (xdrs, objp)\n", procname);
  144. f_print (fout, "\tXDR *xdrs;\n");
  145. f_print (fout, "\t%s ", procname);
  146. if (pointerp)
  147. f_print (fout, "*");
  148. f_print (fout, "objp;\n{\n");
  149. }
  150. }
  151. static void
  152. print_header (const definition * def)
  153. {
  154. print_generic_header (def->def_name,
  155. def->def_kind != DEF_TYPEDEF ||
  156. !isvectordef (def->def.ty.old_type,
  157. def->def.ty.rel));
  158. /* Now add Inline support */
  159. if (inlineflag == 0)
  160. return;
  161. /*May cause lint to complain. but ... */
  162. f_print (fout, "\tregister int32_t *buf;\n\n");
  163. }
  164. static void
  165. print_prog_header (const proc_list * plist)
  166. {
  167. print_generic_header (plist->args.argname, 1);
  168. }
  169. static void
  170. print_trailer (void)
  171. {
  172. f_print (fout, "\treturn TRUE;\n");
  173. f_print (fout, "}\n");
  174. }
  175. static void
  176. print_ifopen (int indent, const char *name)
  177. {
  178. tabify (fout, indent);
  179. f_print (fout, " if (!xdr_%s (xdrs", name);
  180. }
  181. static void
  182. print_ifarg (const char *arg)
  183. {
  184. f_print (fout, ", %s", arg);
  185. }
  186. static void
  187. print_ifsizeof (int indent, const char *prefix, const char *type)
  188. {
  189. if (indent)
  190. {
  191. fprintf (fout, ",\n");
  192. tabify (fout, indent);
  193. }
  194. else
  195. fprintf (fout, ", ");
  196. if (streq (type, "bool"))
  197. fprintf (fout, "sizeof (bool_t), (xdrproc_t) xdr_bool");
  198. else
  199. {
  200. fprintf (fout, "sizeof (");
  201. if (undefined (type) && prefix)
  202. {
  203. f_print (fout, "%s ", prefix);
  204. }
  205. fprintf (fout, "%s), (xdrproc_t) xdr_%s", type, type);
  206. }
  207. }
  208. static void
  209. print_ifclose (int indent)
  210. {
  211. f_print (fout, "))\n");
  212. tabify (fout, indent);
  213. f_print (fout, "\t return FALSE;\n");
  214. }
  215. static void
  216. print_ifstat (int indent, const char *prefix, const char *type, relation rel,
  217. const char *amax, const char *objname, const char *name)
  218. {
  219. const char *alt = NULL;
  220. switch (rel)
  221. {
  222. case REL_POINTER:
  223. print_ifopen (indent, "pointer");
  224. print_ifarg ("(char **)");
  225. f_print (fout, "%s", objname);
  226. print_ifsizeof (0, prefix, type);
  227. break;
  228. case REL_VECTOR:
  229. if (streq (type, "string"))
  230. {
  231. alt = "string";
  232. }
  233. else if (streq (type, "opaque"))
  234. {
  235. alt = "opaque";
  236. }
  237. if (alt)
  238. {
  239. print_ifopen (indent, alt);
  240. print_ifarg (objname);
  241. }
  242. else
  243. {
  244. print_ifopen (indent, "vector");
  245. print_ifarg ("(char *)");
  246. f_print (fout, "%s", objname);
  247. }
  248. print_ifarg (amax);
  249. if (!alt)
  250. {
  251. print_ifsizeof (indent + 1, prefix, type);
  252. }
  253. break;
  254. case REL_ARRAY:
  255. if (streq (type, "string"))
  256. {
  257. alt = "string";
  258. }
  259. else if (streq (type, "opaque"))
  260. {
  261. alt = "bytes";
  262. }
  263. if (streq (type, "string"))
  264. {
  265. print_ifopen (indent, alt);
  266. print_ifarg (objname);
  267. }
  268. else
  269. {
  270. if (alt)
  271. {
  272. print_ifopen (indent, alt);
  273. }
  274. else
  275. {
  276. print_ifopen (indent, "array");
  277. }
  278. print_ifarg ("(char **)");
  279. if (*objname == '&')
  280. {
  281. f_print (fout, "%s.%s_val, (u_int *) %s.%s_len",
  282. objname, name, objname, name);
  283. }
  284. else
  285. {
  286. f_print (fout, "&%s->%s_val, (u_int *) &%s->%s_len",
  287. objname, name, objname, name);
  288. }
  289. }
  290. print_ifarg (amax);
  291. if (!alt)
  292. {
  293. print_ifsizeof (indent + 1, prefix, type);
  294. }
  295. break;
  296. case REL_ALIAS:
  297. print_ifopen (indent, type);
  298. print_ifarg (objname);
  299. break;
  300. }
  301. print_ifclose (indent);
  302. }
  303. static void
  304. emit_enum (definition * def)
  305. {
  306. (void) def;
  307. print_ifopen (1, "enum");
  308. print_ifarg ("(enum_t *) objp");
  309. print_ifclose (1);
  310. }
  311. static void
  312. emit_program (const definition * def)
  313. {
  314. decl_list *dl;
  315. version_list *vlist;
  316. proc_list *plist;
  317. for (vlist = def->def.pr.versions; vlist != NULL; vlist = vlist->next)
  318. for (plist = vlist->procs; plist != NULL; plist = plist->next)
  319. {
  320. if (!newstyle || plist->arg_num < 2)
  321. continue; /* old style, or single argument */
  322. print_prog_header (plist);
  323. for (dl = plist->args.decls; dl != NULL;
  324. dl = dl->next)
  325. print_stat (1, &dl->decl);
  326. print_trailer ();
  327. }
  328. }
  329. static void
  330. emit_union (const definition * def)
  331. {
  332. declaration *dflt;
  333. case_list *cl;
  334. declaration *cs;
  335. char *object;
  336. const char *vecformat = "objp->%s_u.%s";
  337. const char *format = "&objp->%s_u.%s";
  338. print_stat (1, &def->def.un.enum_decl);
  339. f_print (fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
  340. for (cl = def->def.un.cases; cl != NULL; cl = cl->next)
  341. {
  342. f_print (fout, "\tcase %s:\n", cl->case_name);
  343. if (cl->contflag == 1) /* a continued case statement */
  344. continue;
  345. cs = &cl->case_decl;
  346. if (!streq (cs->type, "void"))
  347. {
  348. object = alloc (strlen (def->def_name) + strlen (format) +
  349. strlen (cs->name) + 1);
  350. if (isvectordef (cs->type, cs->rel))
  351. {
  352. s_print (object, vecformat, def->def_name,
  353. cs->name);
  354. }
  355. else
  356. {
  357. s_print (object, format, def->def_name,
  358. cs->name);
  359. }
  360. print_ifstat (2, cs->prefix, cs->type, cs->rel, cs->array_max,
  361. object, cs->name);
  362. free (object);
  363. }
  364. f_print (fout, "\t\tbreak;\n");
  365. }
  366. dflt = def->def.un.default_decl;
  367. if (dflt != NULL)
  368. {
  369. if (!streq (dflt->type, "void"))
  370. {
  371. f_print (fout, "\tdefault:\n");
  372. object = alloc (strlen (def->def_name) + strlen (format) +
  373. strlen (dflt->name) + 1);
  374. if (isvectordef (dflt->type, dflt->rel))
  375. {
  376. s_print (object, vecformat, def->def_name,
  377. dflt->name);
  378. }
  379. else
  380. {
  381. s_print (object, format, def->def_name,
  382. dflt->name);
  383. }
  384. print_ifstat (2, dflt->prefix, dflt->type, dflt->rel,
  385. dflt->array_max, object, dflt->name);
  386. free (object);
  387. f_print (fout, "\t\tbreak;\n");
  388. }
  389. else
  390. {
  391. f_print (fout, "\tdefault:\n");
  392. f_print (fout, "\t\tbreak;\n");
  393. }
  394. }
  395. else
  396. {
  397. f_print (fout, "\tdefault:\n");
  398. f_print (fout, "\t\treturn FALSE;\n");
  399. }
  400. f_print (fout, "\t}\n");
  401. }
  402. static void
  403. inline_struct (definition *def, int flag)
  404. {
  405. decl_list *dl;
  406. int i, size;
  407. decl_list *cur = NULL;
  408. decl_list *psav;
  409. bas_type *ptr;
  410. char *sizestr;
  411. const char *plus;
  412. char ptemp[256];
  413. int indent = 1;
  414. if (flag == PUT)
  415. f_print (fout, "\n\tif (xdrs->x_op == XDR_ENCODE) {\n");
  416. else
  417. f_print (fout,
  418. "\t\treturn TRUE;\n\t} else if (xdrs->x_op == XDR_DECODE) {\n");
  419. i = 0;
  420. size = 0;
  421. sizestr = NULL;
  422. for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
  423. { /* xxx */
  424. /* now walk down the list and check for basic types */
  425. if ((dl->decl.prefix == NULL) &&
  426. ((ptr = find_type (dl->decl.type)) != NULL) &&
  427. ((dl->decl.rel == REL_ALIAS) || (dl->decl.rel == REL_VECTOR)))
  428. {
  429. if (i == 0)
  430. cur = dl;
  431. ++i;
  432. if (dl->decl.rel == REL_ALIAS)
  433. size += ptr->length;
  434. else
  435. {
  436. /* this is required to handle arrays */
  437. if (sizestr == NULL)
  438. plus = "";
  439. else
  440. plus = "+ ";
  441. if (ptr->length != 1)
  442. s_print (ptemp, " %s %s * %d", plus, dl->decl.array_max,
  443. ptr->length);
  444. else
  445. s_print (ptemp, " %s%s ", plus, dl->decl.array_max);
  446. /*now concatenate to sizestr !!!! */
  447. if (sizestr == NULL)
  448. sizestr = strdup (ptemp);
  449. else
  450. {
  451. sizestr = realloc (sizestr, strlen (sizestr) +
  452. strlen (ptemp) + 1);
  453. if (sizestr == NULL)
  454. {
  455. f_print (stderr, "Fatal error : no memory \n");
  456. crash ();
  457. };
  458. sizestr = strcat (sizestr, ptemp);
  459. /*build up length of array */
  460. }
  461. }
  462. }
  463. else
  464. {
  465. if (i > 0)
  466. {
  467. if (sizestr == NULL && size < inlineflag)
  468. {
  469. /* don't expand into inline code if size < inlineflag */
  470. while (cur != dl)
  471. {
  472. print_stat (indent + 1, &cur->decl);
  473. cur = cur->next;
  474. }
  475. }
  476. else
  477. {
  478. /* were already looking at a xdr_inlineable structure */
  479. tabify (fout, indent + 1);
  480. if (sizestr == NULL)
  481. f_print (fout, "buf = XDR_INLINE (xdrs, %d * BYTES_PER_XDR_UNIT);", size);
  482. else if (size == 0)
  483. f_print (fout,
  484. "buf = XDR_INLINE (xdrs, (%s) * BYTES_PER_XDR_UNIT);",
  485. sizestr);
  486. else
  487. f_print (fout,
  488. "buf = XDR_INLINE (xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
  489. size, sizestr);
  490. f_print (fout, "\n");
  491. tabify (fout, indent + 1);
  492. fprintf (fout, "if (buf == NULL) {\n");
  493. psav = cur;
  494. while (cur != dl)
  495. {
  496. print_stat (indent + 2, &cur->decl);
  497. cur = cur->next;
  498. }
  499. f_print (fout, "\n\t\t} else {\n");
  500. cur = psav;
  501. while (cur != dl)
  502. {
  503. emit_inline (indent + 1, &cur->decl, flag);
  504. cur = cur->next;
  505. }
  506. tabify (fout, indent + 1);
  507. f_print (fout, "}\n");
  508. }
  509. }
  510. size = 0;
  511. i = 0;
  512. free (sizestr);
  513. sizestr = NULL;
  514. print_stat (indent + 1, &dl->decl);
  515. }
  516. }
  517. if (i > 0)
  518. {
  519. if (sizestr == NULL && size < inlineflag)
  520. {
  521. /* don't expand into inline code if size < inlineflag */
  522. while (cur != dl)
  523. {
  524. print_stat (indent + 1, &cur->decl);
  525. cur = cur->next;
  526. }
  527. }
  528. else
  529. {
  530. /* were already looking at a xdr_inlineable structure */
  531. if (sizestr == NULL)
  532. f_print (fout,
  533. "\t\tbuf = XDR_INLINE (xdrs, %d * BYTES_PER_XDR_UNIT);",
  534. size);
  535. else if (size == 0)
  536. f_print (fout,
  537. "\t\tbuf = XDR_INLINE (xdrs, (%s) * BYTES_PER_XDR_UNIT);",
  538. sizestr);
  539. else
  540. f_print (fout,
  541. "\t\tbuf = XDR_INLINE (xdrs, (%d + %s)* BYTES_PER_XDR_UNIT);",
  542. size, sizestr);
  543. f_print (fout, "\n\t\tif (buf == NULL) {\n");
  544. psav = cur;
  545. while (cur != NULL)
  546. {
  547. print_stat (indent + 2, &cur->decl);
  548. cur = cur->next;
  549. }
  550. f_print (fout, "\t\t} else {\n");
  551. cur = psav;
  552. while (cur != dl)
  553. {
  554. emit_inline (indent + 2, &cur->decl, flag);
  555. cur = cur->next;
  556. }
  557. f_print (fout, "\t\t}\n");
  558. }
  559. }
  560. }
  561. /* this may be const. i haven't traced this one through yet. */
  562. static void
  563. emit_struct (definition * def)
  564. {
  565. decl_list *dl;
  566. int j, size, flag;
  567. bas_type *ptr;
  568. int can_inline;
  569. if (inlineflag == 0)
  570. {
  571. /* No xdr_inlining at all */
  572. for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
  573. print_stat (1, &dl->decl);
  574. return;
  575. }
  576. for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
  577. if (dl->decl.rel == REL_VECTOR)
  578. {
  579. f_print (fout, "\tint i;\n");
  580. break;
  581. }
  582. size = 0;
  583. can_inline = 0;
  584. /*
  585. * Make a first pass and see if inling is possible.
  586. */
  587. for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
  588. if ((dl->decl.prefix == NULL) &&
  589. ((ptr = find_type (dl->decl.type)) != NULL) &&
  590. ((dl->decl.rel == REL_ALIAS) || (dl->decl.rel == REL_VECTOR)))
  591. {
  592. if (dl->decl.rel == REL_ALIAS)
  593. size += ptr->length;
  594. else
  595. {
  596. can_inline = 1;
  597. break; /* can be inlined */
  598. }
  599. }
  600. else
  601. {
  602. if (size >= inlineflag)
  603. {
  604. can_inline = 1;
  605. break; /* can be inlined */
  606. }
  607. size = 0;
  608. }
  609. if (size > inlineflag)
  610. can_inline = 1;
  611. if (can_inline == 0)
  612. { /* can not inline, drop back to old mode */
  613. for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
  614. print_stat (1, &dl->decl);
  615. return;
  616. };
  617. flag = PUT;
  618. for (j = 0; j < 2; j++)
  619. {
  620. inline_struct (def, flag);
  621. if (flag == PUT)
  622. flag = GET;
  623. }
  624. f_print (fout, "\t return TRUE;\n\t}\n\n");
  625. /* now take care of XDR_FREE case */
  626. for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
  627. print_stat (1, &dl->decl);
  628. }
  629. static void
  630. emit_typedef (const definition * def)
  631. {
  632. const char *prefix = def->def.ty.old_prefix;
  633. const char *type = def->def.ty.old_type;
  634. const char *amax = def->def.ty.array_max;
  635. relation rel = def->def.ty.rel;
  636. print_ifstat (1, prefix, type, rel, amax, "objp", def->def_name);
  637. }
  638. static void
  639. print_stat (int indent, const declaration * dec)
  640. {
  641. const char *prefix = dec->prefix;
  642. const char *type = dec->type;
  643. const char *amax = dec->array_max;
  644. relation rel = dec->rel;
  645. char name[256];
  646. if (isvectordef (type, rel))
  647. {
  648. s_print (name, "objp->%s", dec->name);
  649. }
  650. else
  651. {
  652. s_print (name, "&objp->%s", dec->name);
  653. }
  654. print_ifstat (indent, prefix, type, rel, amax, name, dec->name);
  655. }
  656. static void
  657. emit_inline (int indent, declaration * decl, int flag)
  658. {
  659. switch (decl->rel)
  660. {
  661. case REL_ALIAS:
  662. emit_single_in_line (indent, decl, flag, REL_ALIAS);
  663. break;
  664. case REL_VECTOR:
  665. tabify (fout, indent);
  666. f_print (fout, "{\n");
  667. tabify (fout, indent + 1);
  668. f_print (fout, "register %s *genp;\n\n", decl->type);
  669. tabify (fout, indent + 1);
  670. f_print (fout,
  671. "for (i = 0, genp = objp->%s;\n", decl->name);
  672. tabify (fout, indent + 2);
  673. f_print (fout, "i < %s; ++i) {\n", decl->array_max);
  674. emit_single_in_line (indent + 2, decl, flag, REL_VECTOR);
  675. tabify (fout, indent + 1);
  676. f_print (fout, "}\n");
  677. tabify (fout, indent);
  678. f_print (fout, "}\n");
  679. break;
  680. default:
  681. break;
  682. /* ?... do nothing I guess */
  683. }
  684. }
  685. static void
  686. emit_single_in_line (int indent, declaration *decl, int flag, relation rel)
  687. {
  688. char *upp_case;
  689. int freed = 0;
  690. tabify (fout, indent);
  691. if (flag == PUT)
  692. f_print (fout, "IXDR_PUT_");
  693. else
  694. {
  695. if (rel == REL_ALIAS)
  696. f_print (fout, "objp->%s = IXDR_GET_", decl->name);
  697. else
  698. f_print (fout, "*genp++ = IXDR_GET_");
  699. }
  700. upp_case = upcase (decl->type);
  701. /* hack - XX */
  702. if (!strcmp (upp_case, "INT"))
  703. {
  704. free (upp_case);
  705. freed = 1;
  706. /* Casting is safe since the `freed' flag is set. */
  707. upp_case = (char *) "LONG";
  708. }
  709. if (!strcmp (upp_case, "U_INT"))
  710. {
  711. free (upp_case);
  712. freed = 1;
  713. /* Casting is safe since the `freed' flag is set. */
  714. upp_case = (char *) "U_LONG";
  715. }
  716. if (flag == PUT)
  717. {
  718. if (rel == REL_ALIAS)
  719. f_print (fout, "%s(buf, objp->%s);\n", upp_case, decl->name);
  720. else
  721. f_print (fout, "%s(buf, *genp++);\n", upp_case);
  722. }
  723. else
  724. {
  725. f_print (fout, "%s(buf);\n", upp_case);
  726. }
  727. if (!freed)
  728. free (upp_case);
  729. }
  730. static char *
  731. upcase (const char *str)
  732. {
  733. char *ptr, *hptr;
  734. ptr = malloc (strlen (str) + 1);
  735. if (ptr == NULL)
  736. {
  737. f_print (stderr, "malloc failed\n");
  738. exit (1);
  739. }
  740. hptr = ptr;
  741. while (*str != '\0')
  742. *ptr++ = toupper (*str++);
  743. *ptr = '\0';
  744. return hptr;
  745. }