1 /* $OpenBSD: rpc_tblout.c,v 1.15 2013/10/27 18:31:24 guenther Exp $ */ 2 /* $NetBSD: rpc_tblout.c,v 1.3 1995/06/24 15:00:15 pk Exp $ */ 3 4 /* 5 * Copyright (c) 2010, Oracle America, Inc. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions are 9 * met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above 14 * copyright notice, this list of conditions and the following 15 * disclaimer in the documentation and/or other materials 16 * provided with the distribution. 17 * * Neither the name of the "Oracle America, Inc." nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 26 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 28 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 31 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 /* 36 * rpc_tblout.c, Dispatch table outputter for the RPC protocol compiler 37 */ 38 #include <stdio.h> 39 #include <stdlib.h> 40 #include <string.h> 41 #include "rpc_parse.h" 42 #include "rpc_util.h" 43 44 #define TABSIZE 8 45 #define TABCOUNT 5 46 #define TABSTOP (TABSIZE*TABCOUNT) 47 48 static char tabstr[TABCOUNT+1] = "\t\t\t\t\t"; 49 50 static const char tbl_hdr[] = "struct rpcgen_table %s_table[] = {\n"; 51 static const char tbl_end[] = "};\n"; 52 53 static const char null_entry[] = "\n\t(char *(*)())0,\n\ 54 \t(xdrproc_t) xdr_void,\t\t\t0,\n\ 55 \t(xdrproc_t) xdr_void,\t\t\t0,\n"; 56 57 static const char tbl_nproc[] = "int %s_nproc =\n\tsizeof(%s_table)/sizeof(%s_table[0]);\n\n"; 58 59 static void write_table(definition *); 60 static void printit(char *, char *); 61 62 void 63 write_tables() 64 { 65 definition *def; 66 list *l; 67 68 fprintf(fout, "\n"); 69 for (l = defined; l != NULL; l = l->next) { 70 def = (definition *) l->val; 71 if (def->def_kind == DEF_PROGRAM) 72 write_table(def); 73 } 74 } 75 76 static void 77 write_table(def) 78 definition *def; 79 { 80 version_list *vp; 81 proc_list *proc; 82 int current; 83 int expected; 84 char progvers[100]; 85 int warning; 86 87 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) { 88 warning = 0; 89 snprintf(progvers, sizeof progvers, "%s_%s", 90 locase(def->def_name), vp->vers_num); 91 /* print the table header */ 92 fprintf(fout, tbl_hdr, progvers); 93 94 if (nullproc(vp->procs)) { 95 expected = 0; 96 } else { 97 expected = 1; 98 fprintf(fout, null_entry); 99 } 100 for (proc = vp->procs; proc != NULL; proc = proc->next) { 101 current = atoi(proc->proc_num); 102 if (current != expected++) { 103 fprintf(fout, 104 "\n/*\n * WARNING: table out of order\n */\n"); 105 if (warning == 0) { 106 fprintf(stderr, 107 "WARNING %s table is out of order\n", 108 progvers); 109 warning = 1; 110 nonfatalerrors = 1; 111 } 112 expected = current + 1; 113 } 114 fprintf(fout, "\n\t(char *(*)())RPCGEN_ACTION("); 115 116 /* routine to invoke */ 117 if (!newstyle) 118 pvname_svc(proc->proc_name, vp->vers_num); 119 else { 120 if (newstyle) 121 fprintf(fout, "_"); /* calls internal func */ 122 pvname(proc->proc_name, vp->vers_num); 123 } 124 fprintf(fout, "),\n"); 125 126 /* argument info */ 127 if (proc->arg_num > 1) 128 printit((char*) NULL, proc->args.argname); 129 else 130 /* do we have to do something special for newstyle */ 131 printit(proc->args.decls->decl.prefix, 132 proc->args.decls->decl.type); 133 /* result info */ 134 printit(proc->res_prefix, proc->res_type); 135 } 136 137 /* print the table trailer */ 138 fprintf(fout, tbl_end); 139 fprintf(fout, tbl_nproc, progvers, progvers, progvers); 140 } 141 } 142 143 static void 144 printit(prefix, type) 145 char *prefix; 146 char *type; 147 { 148 int len, tabs; 149 150 len = fprintf(fout, "\txdr_%s,", stringfix(type)); 151 /* account for leading tab expansion */ 152 len += TABSIZE - 1; 153 /* round up to tabs required */ 154 tabs = (TABSTOP - len + TABSIZE - 1)/TABSIZE; 155 fprintf(fout, "%s", &tabstr[TABCOUNT-tabs]); 156 157 if (streq(type, "void")) { 158 fprintf(fout, "0"); 159 } else { 160 fprintf(fout, "sizeof ("); 161 /* XXX: should "follow" be 1 ??? */ 162 ptype(prefix, type, 0); 163 fprintf(fout, ")"); 164 } 165 fprintf(fout, ",\n"); 166 } 167