rpc_tblout.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * From: @(#)rpc_tblout.c 1.4 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_tblout.c, Dispatch table outputter for the RPC protocol compiler
  34. */
  35. #include <stdio.h>
  36. #include <string.h>
  37. #include "rpc_parse.h"
  38. #include "rpc_util.h"
  39. #include "proto.h"
  40. #define TABSIZE 8
  41. #define TABCOUNT 5
  42. #define TABSTOP (TABSIZE*TABCOUNT)
  43. static const char tabstr[TABCOUNT + 1] = "\t\t\t\t\t";
  44. static const char tbl_hdr[] = "struct rpcgen_table %s_table[] = {\n";
  45. static const char tbl_end[] = "};\n";
  46. static const char null_entry[] = "\n\t(char *(*)())0,\n\
  47. \t(xdrproc_t) xdr_void,\t\t\t0,\n\
  48. \t(xdrproc_t) xdr_void,\t\t\t0,\n";
  49. static const char tbl_nproc[] = "int %s_nproc =\n\tsizeof(%s_table)/sizeof(%s_table[0]);\n\n";
  50. static void write_table (const definition * def);
  51. static void printit (const char *prefix, const char *type);
  52. void
  53. write_tables (void)
  54. {
  55. list *l;
  56. definition *def;
  57. f_print (fout, "\n");
  58. for (l = defined; l != NULL; l = l->next)
  59. {
  60. def = (definition *) l->val;
  61. if (def->def_kind == DEF_PROGRAM)
  62. {
  63. write_table (def);
  64. }
  65. }
  66. }
  67. static void
  68. write_table (const definition * def)
  69. {
  70. version_list *vp;
  71. proc_list *proc;
  72. int current;
  73. int expected;
  74. char progvers[100];
  75. int warning;
  76. for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
  77. {
  78. warning = 0;
  79. s_print (progvers, "%s_%s",
  80. locase (def->def_name), vp->vers_num);
  81. /* print the table header */
  82. f_print (fout, tbl_hdr, progvers);
  83. if (nullproc (vp->procs))
  84. {
  85. expected = 0;
  86. }
  87. else
  88. {
  89. expected = 1;
  90. f_print (fout, null_entry);
  91. }
  92. for (proc = vp->procs; proc != NULL; proc = proc->next)
  93. {
  94. current = atoi (proc->proc_num);
  95. if (current != expected++)
  96. {
  97. f_print (fout,
  98. "\n/*\n * WARNING: table out of order\n */\n");
  99. if (warning == 0)
  100. {
  101. f_print (stderr,
  102. "WARNING %s table is out of order\n",
  103. progvers);
  104. warning = 1;
  105. nonfatalerrors = 1;
  106. }
  107. expected = current + 1;
  108. }
  109. f_print (fout, "\n\t(char *(*)())RPCGEN_ACTION(");
  110. /* routine to invoke */
  111. if (Cflag && !newstyle)
  112. pvname_svc (proc->proc_name, vp->vers_num);
  113. else
  114. {
  115. if (newstyle)
  116. f_print (fout, "_"); /* calls internal func */
  117. pvname (proc->proc_name, vp->vers_num);
  118. }
  119. f_print (fout, "),\n");
  120. /* argument info */
  121. if (proc->arg_num > 1)
  122. printit ((char *) NULL, proc->args.argname);
  123. else
  124. /* do we have to do something special for newstyle */
  125. printit (proc->args.decls->decl.prefix,
  126. proc->args.decls->decl.type);
  127. /* result info */
  128. printit (proc->res_prefix, proc->res_type);
  129. }
  130. /* print the table trailer */
  131. f_print (fout, tbl_end);
  132. f_print (fout, tbl_nproc, progvers, progvers, progvers);
  133. }
  134. }
  135. static void
  136. printit (const char *prefix, const char *type)
  137. {
  138. int len;
  139. int tabs;
  140. len = fprintf (fout, "\txdr_%s,", stringfix (type));
  141. /* account for leading tab expansion */
  142. len += TABSIZE - 1;
  143. /* round up to tabs required */
  144. tabs = (TABSTOP - len + TABSIZE - 1) / TABSIZE;
  145. f_print (fout, "%s", &tabstr[TABCOUNT - tabs]);
  146. if (streq (type, "void"))
  147. {
  148. f_print (fout, "0");
  149. }
  150. else
  151. {
  152. f_print (fout, "sizeof ( ");
  153. /* XXX: should "follow" be 1 ??? */
  154. ptype (prefix, type, 0);
  155. f_print (fout, ")");
  156. }
  157. f_print (fout, ",\n");
  158. }