1 /* @(#)rpc_cout.c 2.1 88/08/01 4.0 RPCSRC */ 2 /* 3 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 4 * unrestricted use provided that this legend is included on all tape 5 * media and as a part of the software program in whole or part. Users 6 * may copy or modify Sun RPC without charge, but are not authorized 7 * to license or distribute it to anyone else except as part of a product or 8 * program developed by the user. 9 * 10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 13 * 14 * Sun RPC is provided with no support and without any obligation on the 15 * part of Sun Microsystems, Inc. to assist in its use, correction, 16 * modification or enhancement. 17 * 18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 20 * OR ANY PART THEREOF. 21 * 22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue 23 * or profits or other special, indirect and consequential damages, even if 24 * Sun has been advised of the possibility of such damages. 25 * 26 * Sun Microsystems, Inc. 27 * 2550 Garcia Avenue 28 * Mountain View, California 94043 29 */ 30 #ifndef lint 31 static char sccsid[] = "@(#)rpc_cout.c 1.8 87/06/24 (C) 1987 SMI"; 32 #endif 33 34 /* 35 * rpc_cout.c, XDR routine outputter for the RPC protocol compiler 36 * Copyright (C) 1987, Sun Microsystems, Inc. 37 */ 38 #include <stdio.h> 39 #include <strings.h> 40 #include "rpc_util.h" 41 #include "rpc_parse.h" 42 43 static int print_header(), print_trailer(), space(), emit_enum(), 44 emit_union(), emit_struct(), emit_typedef(), print_stat(); 45 46 47 /* 48 * Emit the C-routine for the given definition 49 */ 50 void 51 emit(def) 52 definition *def; 53 { 54 if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) { 55 return; 56 } 57 print_header(def); 58 switch (def->def_kind) { 59 case DEF_UNION: 60 emit_union(def); 61 break; 62 case DEF_ENUM: 63 emit_enum(def); 64 break; 65 case DEF_STRUCT: 66 emit_struct(def); 67 break; 68 case DEF_TYPEDEF: 69 emit_typedef(def); 70 break; 71 } 72 print_trailer(); 73 } 74 75 static 76 findtype(def, type) 77 definition *def; 78 char *type; 79 { 80 if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) { 81 return (0); 82 } else { 83 return (streq(def->def_name, type)); 84 } 85 } 86 87 static 88 undefined(type) 89 char *type; 90 { 91 definition *def; 92 93 def = (definition *) FINDVAL(defined, type, findtype); 94 return (def == NULL); 95 } 96 97 98 static 99 print_header(def) 100 definition *def; 101 { 102 space(); 103 f_print(fout, "bool_t\n"); 104 f_print(fout, "xdr_%s(xdrs, objp)\n", def->def_name); 105 f_print(fout, "\tXDR *xdrs;\n"); 106 f_print(fout, "\t%s ", def->def_name); 107 if (def->def_kind != DEF_TYPEDEF || 108 !isvectordef(def->def.ty.old_type, def->def.ty.rel)) { 109 f_print(fout, "*"); 110 } 111 f_print(fout, "objp;\n"); 112 f_print(fout, "{\n"); 113 } 114 115 static 116 print_trailer() 117 { 118 f_print(fout, "\treturn (TRUE);\n"); 119 f_print(fout, "}\n"); 120 space(); 121 } 122 123 124 static 125 print_ifopen(indent, name) 126 int indent; 127 char *name; 128 { 129 tabify(fout, indent); 130 f_print(fout, "if (!xdr_%s(xdrs", name); 131 } 132 133 134 static 135 print_ifarg(arg) 136 char *arg; 137 { 138 f_print(fout, ", %s", arg); 139 } 140 141 142 static 143 print_ifsizeof(prefix, type) 144 char *prefix; 145 char *type; 146 { 147 if (streq(type, "bool")) { 148 f_print(fout, ", sizeof(bool_t), xdr_bool"); 149 } else { 150 f_print(fout, ", sizeof("); 151 if (undefined(type) && prefix) { 152 f_print(fout, "%s ", prefix); 153 } 154 f_print(fout, "%s), xdr_%s", type, type); 155 } 156 } 157 158 static 159 print_ifclose(indent) 160 int indent; 161 { 162 f_print(fout, ")) {\n"); 163 tabify(fout, indent); 164 f_print(fout, "\treturn (FALSE);\n"); 165 tabify(fout, indent); 166 f_print(fout, "}\n"); 167 } 168 169 static 170 space() 171 { 172 f_print(fout, "\n\n"); 173 } 174 175 static 176 print_ifstat(indent, prefix, type, rel, amax, objname, name) 177 int indent; 178 char *prefix; 179 char *type; 180 relation rel; 181 char *amax; 182 char *objname; 183 char *name; 184 { 185 char *alt = NULL; 186 187 switch (rel) { 188 case REL_POINTER: 189 print_ifopen(indent, "pointer"); 190 print_ifarg("(char **)"); 191 f_print(fout, "%s", objname); 192 print_ifsizeof(prefix, type); 193 break; 194 case REL_VECTOR: 195 if (streq(type, "string")) { 196 alt = "string"; 197 } else if (streq(type, "opaque")) { 198 alt = "opaque"; 199 } 200 if (alt) { 201 print_ifopen(indent, alt); 202 print_ifarg(objname); 203 } else { 204 print_ifopen(indent, "vector"); 205 print_ifarg("(char *)"); 206 f_print(fout, "%s", objname); 207 } 208 print_ifarg(amax); 209 if (!alt) { 210 print_ifsizeof(prefix, type); 211 } 212 break; 213 case REL_ARRAY: 214 if (streq(type, "string")) { 215 alt = "string"; 216 } else if (streq(type, "opaque")) { 217 alt = "bytes"; 218 } 219 if (streq(type, "string")) { 220 print_ifopen(indent, alt); 221 print_ifarg(objname); 222 } else { 223 if (alt) { 224 print_ifopen(indent, alt); 225 } else { 226 print_ifopen(indent, "array"); 227 } 228 print_ifarg("(char **)"); 229 if (*objname == '&') { 230 f_print(fout, "%s.%s_val, (u_int *)%s.%s_len", 231 objname, name, objname, name); 232 } else { 233 f_print(fout, "&%s->%s_val, (u_int *)&%s->%s_len", 234 objname, name, objname, name); 235 } 236 } 237 print_ifarg(amax); 238 if (!alt) { 239 print_ifsizeof(prefix, type); 240 } 241 break; 242 case REL_ALIAS: 243 print_ifopen(indent, type); 244 print_ifarg(objname); 245 break; 246 } 247 print_ifclose(indent); 248 } 249 250 251 /* ARGSUSED */ 252 static 253 emit_enum(def) 254 definition *def; 255 { 256 print_ifopen(1, "enum"); 257 print_ifarg("(enum_t *)objp"); 258 print_ifclose(1); 259 } 260 261 262 static 263 emit_union(def) 264 definition *def; 265 { 266 declaration *dflt; 267 case_list *cl; 268 declaration *cs; 269 char *object; 270 char *format = "&objp->%s_u.%s"; 271 272 print_stat(&def->def.un.enum_decl); 273 f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name); 274 for (cl = def->def.un.cases; cl != NULL; cl = cl->next) { 275 cs = &cl->case_decl; 276 f_print(fout, "\tcase %s:\n", cl->case_name); 277 if (!streq(cs->type, "void")) { 278 object = alloc(strlen(def->def_name) + strlen(format) + 279 strlen(cs->name) + 1); 280 s_print(object, format, def->def_name, cs->name); 281 print_ifstat(2, cs->prefix, cs->type, cs->rel, cs->array_max, 282 object, cs->name); 283 free(object); 284 } 285 f_print(fout, "\t\tbreak;\n"); 286 } 287 dflt = def->def.un.default_decl; 288 if (dflt != NULL) { 289 if (!streq(dflt->type, "void")) { 290 f_print(fout, "\tdefault:\n"); 291 object = alloc(strlen(def->def_name) + strlen(format) + 292 strlen(dflt->name) + 1); 293 s_print(object, format, def->def_name, dflt->name); 294 print_ifstat(2, dflt->prefix, dflt->type, dflt->rel, 295 dflt->array_max, object, dflt->name); 296 free(object); 297 f_print(fout, "\t\tbreak;\n"); 298 } 299 } else { 300 f_print(fout, "\tdefault:\n"); 301 f_print(fout, "\t\treturn (FALSE);\n"); 302 } 303 f_print(fout, "\t}\n"); 304 } 305 306 307 308 static 309 emit_struct(def) 310 definition *def; 311 { 312 decl_list *dl; 313 314 for (dl = def->def.st.decls; dl != NULL; dl = dl->next) { 315 print_stat(&dl->decl); 316 } 317 } 318 319 320 321 322 static 323 emit_typedef(def) 324 definition *def; 325 { 326 char *prefix = def->def.ty.old_prefix; 327 char *type = def->def.ty.old_type; 328 char *amax = def->def.ty.array_max; 329 relation rel = def->def.ty.rel; 330 331 print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name); 332 } 333 334 335 336 337 338 static 339 print_stat(dec) 340 declaration *dec; 341 { 342 char *prefix = dec->prefix; 343 char *type = dec->type; 344 char *amax = dec->array_max; 345 relation rel = dec->rel; 346 char name[256]; 347 348 if (isvectordef(type, rel)) { 349 s_print(name, "objp->%s", dec->name); 350 } else { 351 s_print(name, "&objp->%s", dec->name); 352 } 353 print_ifstat(1, prefix, type, rel, amax, name, dec->name); 354 } 355