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