14e115012SGarrett Wollman /* @(#)rpc_cout.c 2.1 88/08/01 4.0 RPCSRC */ 24e115012SGarrett Wollman /* 34e115012SGarrett Wollman * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 44e115012SGarrett Wollman * unrestricted use provided that this legend is included on all tape 54e115012SGarrett Wollman * media and as a part of the software program in whole or part. Users 64e115012SGarrett Wollman * may copy or modify Sun RPC without charge, but are not authorized 74e115012SGarrett Wollman * to license or distribute it to anyone else except as part of a product or 84e115012SGarrett Wollman * program developed by the user. 94e115012SGarrett Wollman * 104e115012SGarrett Wollman * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 114e115012SGarrett Wollman * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 124e115012SGarrett Wollman * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 134e115012SGarrett Wollman * 144e115012SGarrett Wollman * Sun RPC is provided with no support and without any obligation on the 154e115012SGarrett Wollman * part of Sun Microsystems, Inc. to assist in its use, correction, 164e115012SGarrett Wollman * modification or enhancement. 174e115012SGarrett Wollman * 184e115012SGarrett Wollman * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 194e115012SGarrett Wollman * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 204e115012SGarrett Wollman * OR ANY PART THEREOF. 214e115012SGarrett Wollman * 224e115012SGarrett Wollman * In no event will Sun Microsystems, Inc. be liable for any lost revenue 234e115012SGarrett Wollman * or profits or other special, indirect and consequential damages, even if 244e115012SGarrett Wollman * Sun has been advised of the possibility of such damages. 254e115012SGarrett Wollman * 264e115012SGarrett Wollman * Sun Microsystems, Inc. 274e115012SGarrett Wollman * 2550 Garcia Avenue 284e115012SGarrett Wollman * Mountain View, California 94043 294e115012SGarrett Wollman */ 304e115012SGarrett Wollman #ifndef lint 314e115012SGarrett Wollman /*static char sccsid[] = "from: @(#)rpc_cout.c 1.8 87/06/24 (C) 1987 SMI";*/ 324e115012SGarrett Wollman static char rcsid[] = "$Id: rpc_cout.c,v 1.1 1993/09/13 23:20:13 jtc Exp $"; 334e115012SGarrett Wollman #endif 344e115012SGarrett Wollman 354e115012SGarrett Wollman /* 364e115012SGarrett Wollman * rpc_cout.c, XDR routine outputter for the RPC protocol compiler 374e115012SGarrett Wollman * Copyright (C) 1987, Sun Microsystems, Inc. 384e115012SGarrett Wollman */ 394e115012SGarrett Wollman #include <stdio.h> 404e115012SGarrett Wollman #include <strings.h> 414e115012SGarrett Wollman #include "rpc_util.h" 424e115012SGarrett Wollman #include "rpc_parse.h" 434e115012SGarrett Wollman 444e115012SGarrett Wollman static int print_header(), print_trailer(), space(), emit_enum(), 454e115012SGarrett Wollman emit_union(), emit_struct(), emit_typedef(), print_stat(); 464e115012SGarrett Wollman 474e115012SGarrett Wollman 484e115012SGarrett Wollman /* 494e115012SGarrett Wollman * Emit the C-routine for the given definition 504e115012SGarrett Wollman */ 514e115012SGarrett Wollman void 524e115012SGarrett Wollman emit(def) 534e115012SGarrett Wollman definition *def; 544e115012SGarrett Wollman { 554e115012SGarrett Wollman if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) { 564e115012SGarrett Wollman return; 574e115012SGarrett Wollman } 584e115012SGarrett Wollman print_header(def); 594e115012SGarrett Wollman switch (def->def_kind) { 604e115012SGarrett Wollman case DEF_UNION: 614e115012SGarrett Wollman emit_union(def); 624e115012SGarrett Wollman break; 634e115012SGarrett Wollman case DEF_ENUM: 644e115012SGarrett Wollman emit_enum(def); 654e115012SGarrett Wollman break; 664e115012SGarrett Wollman case DEF_STRUCT: 674e115012SGarrett Wollman emit_struct(def); 684e115012SGarrett Wollman break; 694e115012SGarrett Wollman case DEF_TYPEDEF: 704e115012SGarrett Wollman emit_typedef(def); 714e115012SGarrett Wollman break; 724e115012SGarrett Wollman } 734e115012SGarrett Wollman print_trailer(); 744e115012SGarrett Wollman } 754e115012SGarrett Wollman 764e115012SGarrett Wollman static 774e115012SGarrett Wollman findtype(def, type) 784e115012SGarrett Wollman definition *def; 794e115012SGarrett Wollman char *type; 804e115012SGarrett Wollman { 814e115012SGarrett Wollman if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) { 824e115012SGarrett Wollman return (0); 834e115012SGarrett Wollman } else { 844e115012SGarrett Wollman return (streq(def->def_name, type)); 854e115012SGarrett Wollman } 864e115012SGarrett Wollman } 874e115012SGarrett Wollman 884e115012SGarrett Wollman static 894e115012SGarrett Wollman undefined(type) 904e115012SGarrett Wollman char *type; 914e115012SGarrett Wollman { 924e115012SGarrett Wollman definition *def; 934e115012SGarrett Wollman 944e115012SGarrett Wollman def = (definition *) FINDVAL(defined, type, findtype); 954e115012SGarrett Wollman return (def == NULL); 964e115012SGarrett Wollman } 974e115012SGarrett Wollman 984e115012SGarrett Wollman 994e115012SGarrett Wollman static 1004e115012SGarrett Wollman print_header(def) 1014e115012SGarrett Wollman definition *def; 1024e115012SGarrett Wollman { 1034e115012SGarrett Wollman space(); 1044e115012SGarrett Wollman f_print(fout, "bool_t\n"); 1054e115012SGarrett Wollman f_print(fout, "xdr_%s(xdrs, objp)\n", def->def_name); 1064e115012SGarrett Wollman f_print(fout, "\tXDR *xdrs;\n"); 1074e115012SGarrett Wollman f_print(fout, "\t%s ", def->def_name); 1084e115012SGarrett Wollman if (def->def_kind != DEF_TYPEDEF || 1094e115012SGarrett Wollman !isvectordef(def->def.ty.old_type, def->def.ty.rel)) { 1104e115012SGarrett Wollman f_print(fout, "*"); 1114e115012SGarrett Wollman } 1124e115012SGarrett Wollman f_print(fout, "objp;\n"); 1134e115012SGarrett Wollman f_print(fout, "{\n"); 1144e115012SGarrett Wollman } 1154e115012SGarrett Wollman 1164e115012SGarrett Wollman static 1174e115012SGarrett Wollman print_trailer() 1184e115012SGarrett Wollman { 1194e115012SGarrett Wollman f_print(fout, "\treturn (TRUE);\n"); 1204e115012SGarrett Wollman f_print(fout, "}\n"); 1214e115012SGarrett Wollman space(); 1224e115012SGarrett Wollman } 1234e115012SGarrett Wollman 1244e115012SGarrett Wollman 1254e115012SGarrett Wollman static 1264e115012SGarrett Wollman print_ifopen(indent, name) 1274e115012SGarrett Wollman int indent; 1284e115012SGarrett Wollman char *name; 1294e115012SGarrett Wollman { 1304e115012SGarrett Wollman tabify(fout, indent); 1314e115012SGarrett Wollman f_print(fout, "if (!xdr_%s(xdrs", name); 1324e115012SGarrett Wollman } 1334e115012SGarrett Wollman 1344e115012SGarrett Wollman 1354e115012SGarrett Wollman static 1364e115012SGarrett Wollman print_ifarg(arg) 1374e115012SGarrett Wollman char *arg; 1384e115012SGarrett Wollman { 1394e115012SGarrett Wollman f_print(fout, ", %s", arg); 1404e115012SGarrett Wollman } 1414e115012SGarrett Wollman 1424e115012SGarrett Wollman 1434e115012SGarrett Wollman static 1444e115012SGarrett Wollman print_ifsizeof(prefix, type) 1454e115012SGarrett Wollman char *prefix; 1464e115012SGarrett Wollman char *type; 1474e115012SGarrett Wollman { 1484e115012SGarrett Wollman if (streq(type, "bool")) { 1494e115012SGarrett Wollman f_print(fout, ", sizeof(bool_t), xdr_bool"); 1504e115012SGarrett Wollman } else { 1514e115012SGarrett Wollman f_print(fout, ", sizeof("); 1524e115012SGarrett Wollman if (undefined(type) && prefix) { 1534e115012SGarrett Wollman f_print(fout, "%s ", prefix); 1544e115012SGarrett Wollman } 1554e115012SGarrett Wollman f_print(fout, "%s), xdr_%s", type, type); 1564e115012SGarrett Wollman } 1574e115012SGarrett Wollman } 1584e115012SGarrett Wollman 1594e115012SGarrett Wollman static 1604e115012SGarrett Wollman print_ifclose(indent) 1614e115012SGarrett Wollman int indent; 1624e115012SGarrett Wollman { 1634e115012SGarrett Wollman f_print(fout, ")) {\n"); 1644e115012SGarrett Wollman tabify(fout, indent); 1654e115012SGarrett Wollman f_print(fout, "\treturn (FALSE);\n"); 1664e115012SGarrett Wollman tabify(fout, indent); 1674e115012SGarrett Wollman f_print(fout, "}\n"); 1684e115012SGarrett Wollman } 1694e115012SGarrett Wollman 1704e115012SGarrett Wollman static 1714e115012SGarrett Wollman space() 1724e115012SGarrett Wollman { 1734e115012SGarrett Wollman f_print(fout, "\n\n"); 1744e115012SGarrett Wollman } 1754e115012SGarrett Wollman 1764e115012SGarrett Wollman static 1774e115012SGarrett Wollman print_ifstat(indent, prefix, type, rel, amax, objname, name) 1784e115012SGarrett Wollman int indent; 1794e115012SGarrett Wollman char *prefix; 1804e115012SGarrett Wollman char *type; 1814e115012SGarrett Wollman relation rel; 1824e115012SGarrett Wollman char *amax; 1834e115012SGarrett Wollman char *objname; 1844e115012SGarrett Wollman char *name; 1854e115012SGarrett Wollman { 1864e115012SGarrett Wollman char *alt = NULL; 1874e115012SGarrett Wollman 1884e115012SGarrett Wollman switch (rel) { 1894e115012SGarrett Wollman case REL_POINTER: 1904e115012SGarrett Wollman print_ifopen(indent, "pointer"); 1914e115012SGarrett Wollman print_ifarg("(char **)"); 1924e115012SGarrett Wollman f_print(fout, "%s", objname); 1934e115012SGarrett Wollman print_ifsizeof(prefix, type); 1944e115012SGarrett Wollman break; 1954e115012SGarrett Wollman case REL_VECTOR: 1964e115012SGarrett Wollman if (streq(type, "string")) { 1974e115012SGarrett Wollman alt = "string"; 1984e115012SGarrett Wollman } else if (streq(type, "opaque")) { 1994e115012SGarrett Wollman alt = "opaque"; 2004e115012SGarrett Wollman } 2014e115012SGarrett Wollman if (alt) { 2024e115012SGarrett Wollman print_ifopen(indent, alt); 2034e115012SGarrett Wollman print_ifarg(objname); 2044e115012SGarrett Wollman } else { 2054e115012SGarrett Wollman print_ifopen(indent, "vector"); 2064e115012SGarrett Wollman print_ifarg("(char *)"); 2074e115012SGarrett Wollman f_print(fout, "%s", objname); 2084e115012SGarrett Wollman } 2094e115012SGarrett Wollman print_ifarg(amax); 2104e115012SGarrett Wollman if (!alt) { 2114e115012SGarrett Wollman print_ifsizeof(prefix, type); 2124e115012SGarrett Wollman } 2134e115012SGarrett Wollman break; 2144e115012SGarrett Wollman case REL_ARRAY: 2154e115012SGarrett Wollman if (streq(type, "string")) { 2164e115012SGarrett Wollman alt = "string"; 2174e115012SGarrett Wollman } else if (streq(type, "opaque")) { 2184e115012SGarrett Wollman alt = "bytes"; 2194e115012SGarrett Wollman } 2204e115012SGarrett Wollman if (streq(type, "string")) { 2214e115012SGarrett Wollman print_ifopen(indent, alt); 2224e115012SGarrett Wollman print_ifarg(objname); 2234e115012SGarrett Wollman } else { 2244e115012SGarrett Wollman if (alt) { 2254e115012SGarrett Wollman print_ifopen(indent, alt); 2264e115012SGarrett Wollman } else { 2274e115012SGarrett Wollman print_ifopen(indent, "array"); 2284e115012SGarrett Wollman } 2294e115012SGarrett Wollman print_ifarg("(char **)"); 2304e115012SGarrett Wollman if (*objname == '&') { 2314e115012SGarrett Wollman f_print(fout, "%s.%s_val, (u_int *)%s.%s_len", 2324e115012SGarrett Wollman objname, name, objname, name); 2334e115012SGarrett Wollman } else { 2344e115012SGarrett Wollman f_print(fout, "&%s->%s_val, (u_int *)&%s->%s_len", 2354e115012SGarrett Wollman objname, name, objname, name); 2364e115012SGarrett Wollman } 2374e115012SGarrett Wollman } 2384e115012SGarrett Wollman print_ifarg(amax); 2394e115012SGarrett Wollman if (!alt) { 2404e115012SGarrett Wollman print_ifsizeof(prefix, type); 2414e115012SGarrett Wollman } 2424e115012SGarrett Wollman break; 2434e115012SGarrett Wollman case REL_ALIAS: 2444e115012SGarrett Wollman print_ifopen(indent, type); 2454e115012SGarrett Wollman print_ifarg(objname); 2464e115012SGarrett Wollman break; 2474e115012SGarrett Wollman } 2484e115012SGarrett Wollman print_ifclose(indent); 2494e115012SGarrett Wollman } 2504e115012SGarrett Wollman 2514e115012SGarrett Wollman 2524e115012SGarrett Wollman /* ARGSUSED */ 2534e115012SGarrett Wollman static 2544e115012SGarrett Wollman emit_enum(def) 2554e115012SGarrett Wollman definition *def; 2564e115012SGarrett Wollman { 2574e115012SGarrett Wollman print_ifopen(1, "enum"); 2584e115012SGarrett Wollman print_ifarg("(enum_t *)objp"); 2594e115012SGarrett Wollman print_ifclose(1); 2604e115012SGarrett Wollman } 2614e115012SGarrett Wollman 2624e115012SGarrett Wollman 2634e115012SGarrett Wollman static 2644e115012SGarrett Wollman emit_union(def) 2654e115012SGarrett Wollman definition *def; 2664e115012SGarrett Wollman { 2674e115012SGarrett Wollman declaration *dflt; 2684e115012SGarrett Wollman case_list *cl; 2694e115012SGarrett Wollman declaration *cs; 2704e115012SGarrett Wollman char *object; 2714e115012SGarrett Wollman char *format = "&objp->%s_u.%s"; 2724e115012SGarrett Wollman 2734e115012SGarrett Wollman print_stat(&def->def.un.enum_decl); 2744e115012SGarrett Wollman f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name); 2754e115012SGarrett Wollman for (cl = def->def.un.cases; cl != NULL; cl = cl->next) { 2764e115012SGarrett Wollman cs = &cl->case_decl; 2774e115012SGarrett Wollman f_print(fout, "\tcase %s:\n", cl->case_name); 2784e115012SGarrett Wollman if (!streq(cs->type, "void")) { 2794e115012SGarrett Wollman object = alloc(strlen(def->def_name) + strlen(format) + 2804e115012SGarrett Wollman strlen(cs->name) + 1); 2814e115012SGarrett Wollman s_print(object, format, def->def_name, cs->name); 2824e115012SGarrett Wollman print_ifstat(2, cs->prefix, cs->type, cs->rel, cs->array_max, 2834e115012SGarrett Wollman object, cs->name); 2844e115012SGarrett Wollman free(object); 2854e115012SGarrett Wollman } 2864e115012SGarrett Wollman f_print(fout, "\t\tbreak;\n"); 2874e115012SGarrett Wollman } 2884e115012SGarrett Wollman dflt = def->def.un.default_decl; 2894e115012SGarrett Wollman if (dflt != NULL) { 2904e115012SGarrett Wollman if (!streq(dflt->type, "void")) { 2914e115012SGarrett Wollman f_print(fout, "\tdefault:\n"); 2924e115012SGarrett Wollman object = alloc(strlen(def->def_name) + strlen(format) + 2934e115012SGarrett Wollman strlen(dflt->name) + 1); 2944e115012SGarrett Wollman s_print(object, format, def->def_name, dflt->name); 2954e115012SGarrett Wollman print_ifstat(2, dflt->prefix, dflt->type, dflt->rel, 2964e115012SGarrett Wollman dflt->array_max, object, dflt->name); 2974e115012SGarrett Wollman free(object); 2984e115012SGarrett Wollman f_print(fout, "\t\tbreak;\n"); 2994e115012SGarrett Wollman } 3004e115012SGarrett Wollman } else { 3014e115012SGarrett Wollman f_print(fout, "\tdefault:\n"); 3024e115012SGarrett Wollman f_print(fout, "\t\treturn (FALSE);\n"); 3034e115012SGarrett Wollman } 3044e115012SGarrett Wollman f_print(fout, "\t}\n"); 3054e115012SGarrett Wollman } 3064e115012SGarrett Wollman 3074e115012SGarrett Wollman 3084e115012SGarrett Wollman 3094e115012SGarrett Wollman static 3104e115012SGarrett Wollman emit_struct(def) 3114e115012SGarrett Wollman definition *def; 3124e115012SGarrett Wollman { 3134e115012SGarrett Wollman decl_list *dl; 3144e115012SGarrett Wollman 3154e115012SGarrett Wollman for (dl = def->def.st.decls; dl != NULL; dl = dl->next) { 3164e115012SGarrett Wollman print_stat(&dl->decl); 3174e115012SGarrett Wollman } 3184e115012SGarrett Wollman } 3194e115012SGarrett Wollman 3204e115012SGarrett Wollman 3214e115012SGarrett Wollman 3224e115012SGarrett Wollman 3234e115012SGarrett Wollman static 3244e115012SGarrett Wollman emit_typedef(def) 3254e115012SGarrett Wollman definition *def; 3264e115012SGarrett Wollman { 3274e115012SGarrett Wollman char *prefix = def->def.ty.old_prefix; 3284e115012SGarrett Wollman char *type = def->def.ty.old_type; 3294e115012SGarrett Wollman char *amax = def->def.ty.array_max; 3304e115012SGarrett Wollman relation rel = def->def.ty.rel; 3314e115012SGarrett Wollman 3324e115012SGarrett Wollman print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name); 3334e115012SGarrett Wollman } 3344e115012SGarrett Wollman 3354e115012SGarrett Wollman 3364e115012SGarrett Wollman 3374e115012SGarrett Wollman 3384e115012SGarrett Wollman 3394e115012SGarrett Wollman static 3404e115012SGarrett Wollman print_stat(dec) 3414e115012SGarrett Wollman declaration *dec; 3424e115012SGarrett Wollman { 3434e115012SGarrett Wollman char *prefix = dec->prefix; 3444e115012SGarrett Wollman char *type = dec->type; 3454e115012SGarrett Wollman char *amax = dec->array_max; 3464e115012SGarrett Wollman relation rel = dec->rel; 3474e115012SGarrett Wollman char name[256]; 3484e115012SGarrett Wollman 3494e115012SGarrett Wollman if (isvectordef(type, rel)) { 3504e115012SGarrett Wollman s_print(name, "objp->%s", dec->name); 3514e115012SGarrett Wollman } else { 3524e115012SGarrett Wollman s_print(name, "&objp->%s", dec->name); 3534e115012SGarrett Wollman } 3544e115012SGarrett Wollman print_ifstat(1, prefix, type, rel, amax, name, dec->name); 3554e115012SGarrett Wollman } 356