14e115012SGarrett Wollman /*
24e115012SGarrett Wollman * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
34e115012SGarrett Wollman * unrestricted use provided that this legend is included on all tape
44e115012SGarrett Wollman * media and as a part of the software program in whole or part. Users
54e115012SGarrett Wollman * may copy or modify Sun RPC without charge, but are not authorized
64e115012SGarrett Wollman * to license or distribute it to anyone else except as part of a product or
74e115012SGarrett Wollman * program developed by the user.
84e115012SGarrett Wollman *
94e115012SGarrett Wollman * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
104e115012SGarrett Wollman * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
114e115012SGarrett Wollman * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
124e115012SGarrett Wollman *
134e115012SGarrett Wollman * Sun RPC is provided with no support and without any obligation on the
144e115012SGarrett Wollman * part of Sun Microsystems, Inc. to assist in its use, correction,
154e115012SGarrett Wollman * modification or enhancement.
164e115012SGarrett Wollman *
174e115012SGarrett Wollman * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
184e115012SGarrett Wollman * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
194e115012SGarrett Wollman * OR ANY PART THEREOF.
204e115012SGarrett Wollman *
214e115012SGarrett Wollman * In no event will Sun Microsystems, Inc. be liable for any lost revenue
224e115012SGarrett Wollman * or profits or other special, indirect and consequential damages, even if
234e115012SGarrett Wollman * Sun has been advised of the possibility of such damages.
244e115012SGarrett Wollman *
254e115012SGarrett Wollman * Sun Microsystems, Inc.
264e115012SGarrett Wollman * 2550 Garcia Avenue
274e115012SGarrett Wollman * Mountain View, California 94043
284e115012SGarrett Wollman */
29ff49530fSBill Paul
304e115012SGarrett Wollman /*
314e115012SGarrett Wollman * rpc_cout.c, XDR routine outputter for the RPC protocol compiler
324e115012SGarrett Wollman * Copyright (C) 1987, Sun Microsystems, Inc.
334e115012SGarrett Wollman */
340e76f40dSPhilippe Charnier #include <ctype.h>
354e115012SGarrett Wollman #include <stdio.h>
36ff49530fSBill Paul #include <string.h>
374e115012SGarrett Wollman #include "rpc_parse.h"
38d0cc804bSStefan Farfeleder #include "rpc_scan.h"
39ff49530fSBill Paul #include "rpc_util.h"
404e115012SGarrett Wollman
41d3cb5dedSWarner Losh static void print_header( definition * );
42d3cb5dedSWarner Losh static void print_trailer( void );
43d3cb5dedSWarner Losh static void print_stat( int , declaration * );
44d3cb5dedSWarner Losh static void emit_enum( definition * );
45d3cb5dedSWarner Losh static void emit_program( definition * );
46d3cb5dedSWarner Losh static void emit_union( definition * );
47d3cb5dedSWarner Losh static void emit_struct( definition * );
48d3cb5dedSWarner Losh static void emit_typedef( definition * );
49d3cb5dedSWarner Losh static void emit_inline( int, declaration *, int );
50d3cb5dedSWarner Losh static void emit_single_in_line( int, declaration *, int, relation );
514e115012SGarrett Wollman
524e115012SGarrett Wollman /*
534e115012SGarrett Wollman * Emit the C-routine for the given definition
544e115012SGarrett Wollman */
554e115012SGarrett Wollman void
emit(definition * def)56e390e3afSDavid Malone emit(definition *def)
574e115012SGarrett Wollman {
58ff49530fSBill Paul if (def->def_kind == DEF_CONST) {
594e115012SGarrett Wollman return;
604e115012SGarrett Wollman }
61ff49530fSBill Paul if (def->def_kind == DEF_PROGRAM) {
62ff49530fSBill Paul emit_program(def);
63ff49530fSBill Paul return;
64ff49530fSBill Paul }
65ff49530fSBill Paul if (def->def_kind == DEF_TYPEDEF) {
66ff49530fSBill Paul /*
67ff49530fSBill Paul * now we need to handle declarations like
68ff49530fSBill Paul * struct typedef foo foo;
69ff49530fSBill Paul * since we dont want this to be expanded into 2 calls to xdr_foo
70ff49530fSBill Paul */
71ff49530fSBill Paul
72ff49530fSBill Paul if (strcmp(def->def.ty.old_type, def->def_name) == 0)
73ff49530fSBill Paul return;
7480c7cc1cSPedro F. Giffuni }
754e115012SGarrett Wollman print_header(def);
764e115012SGarrett Wollman switch (def->def_kind) {
774e115012SGarrett Wollman case DEF_UNION:
784e115012SGarrett Wollman emit_union(def);
794e115012SGarrett Wollman break;
804e115012SGarrett Wollman case DEF_ENUM:
814e115012SGarrett Wollman emit_enum(def);
824e115012SGarrett Wollman break;
834e115012SGarrett Wollman case DEF_STRUCT:
844e115012SGarrett Wollman emit_struct(def);
854e115012SGarrett Wollman break;
864e115012SGarrett Wollman case DEF_TYPEDEF:
874e115012SGarrett Wollman emit_typedef(def);
884e115012SGarrett Wollman break;
89526195adSJordan K. Hubbard /* DEF_CONST and DEF_PROGRAM have already been handled */
90526195adSJordan K. Hubbard default:
9140ad8885SAlfred Perlstein break;
924e115012SGarrett Wollman }
934e115012SGarrett Wollman print_trailer();
944e115012SGarrett Wollman }
954e115012SGarrett Wollman
96526195adSJordan K. Hubbard static int
findtype(definition * def,const char * type)97e390e3afSDavid Malone findtype(definition *def, const char *type)
984e115012SGarrett Wollman {
99ff49530fSBill Paul
1004e115012SGarrett Wollman if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) {
1014e115012SGarrett Wollman return (0);
1024e115012SGarrett Wollman } else {
1034e115012SGarrett Wollman return (streq(def->def_name, type));
1044e115012SGarrett Wollman }
1054e115012SGarrett Wollman }
1064e115012SGarrett Wollman
107526195adSJordan K. Hubbard static int
undefined(const char * type)108e390e3afSDavid Malone undefined(const char *type)
1094e115012SGarrett Wollman {
1104e115012SGarrett Wollman definition *def;
1114e115012SGarrett Wollman
1124e115012SGarrett Wollman def = (definition *) FINDVAL(defined, type, findtype);
1134e115012SGarrett Wollman return (def == NULL);
1144e115012SGarrett Wollman }
1154e115012SGarrett Wollman
1164e115012SGarrett Wollman
117526195adSJordan K. Hubbard static void
print_generic_header(const char * procname,int pointerp)118e390e3afSDavid Malone print_generic_header(const char *procname, int pointerp)
119ff49530fSBill Paul {
120ff49530fSBill Paul f_print(fout, "\n");
121ff49530fSBill Paul f_print(fout, "bool_t\n");
122ff49530fSBill Paul f_print(fout, "xdr_%s(", procname);
123896cdc31SStefan Farfeleder f_print(fout, "XDR *xdrs, ");
124ff49530fSBill Paul f_print(fout, "%s ", procname);
125ff49530fSBill Paul if (pointerp)
126ff49530fSBill Paul f_print(fout, "*");
127ff49530fSBill Paul f_print(fout, "objp)\n{\n\n");
128ff49530fSBill Paul }
129ff49530fSBill Paul
130526195adSJordan K. Hubbard static void
print_header(definition * def)131e390e3afSDavid Malone print_header(definition *def)
1324e115012SGarrett Wollman {
133ff49530fSBill Paul print_generic_header(def->def_name,
134ff49530fSBill Paul def->def_kind != DEF_TYPEDEF ||
135ff49530fSBill Paul !isvectordef(def->def.ty.old_type,
136ff49530fSBill Paul def->def.ty.rel));
137ff49530fSBill Paul /* Now add Inline support */
138ff49530fSBill Paul
139122562cdSStefan Farfeleder if (inline_size == 0)
140ff49530fSBill Paul return;
141ff49530fSBill Paul /* May cause lint to complain. but ... */
142ff49530fSBill Paul f_print(fout, "\tregister long *buf;\n\n");
1434e115012SGarrett Wollman }
144ff49530fSBill Paul
145526195adSJordan K. Hubbard static void
print_prog_header(proc_list * plist)146e390e3afSDavid Malone print_prog_header(proc_list *plist)
147ff49530fSBill Paul {
148ff49530fSBill Paul print_generic_header(plist->args.argname, 1);
1494e115012SGarrett Wollman }
1504e115012SGarrett Wollman
151526195adSJordan K. Hubbard static void
print_trailer(void)152e390e3afSDavid Malone print_trailer(void)
1534e115012SGarrett Wollman {
1544e115012SGarrett Wollman f_print(fout, "\treturn (TRUE);\n");
1554e115012SGarrett Wollman f_print(fout, "}\n");
1564e115012SGarrett Wollman }
1574e115012SGarrett Wollman
1584e115012SGarrett Wollman
159526195adSJordan K. Hubbard static void
print_ifopen(int indent,const char * name)160e390e3afSDavid Malone print_ifopen(int indent, const char *name)
1614e115012SGarrett Wollman {
1624e115012SGarrett Wollman tabify(fout, indent);
1634e115012SGarrett Wollman f_print(fout, "if (!xdr_%s(xdrs", name);
1644e115012SGarrett Wollman }
1654e115012SGarrett Wollman
166526195adSJordan K. Hubbard static void
print_ifarg(const char * arg)167e390e3afSDavid Malone print_ifarg(const char *arg)
1684e115012SGarrett Wollman {
1694e115012SGarrett Wollman f_print(fout, ", %s", arg);
1704e115012SGarrett Wollman }
1714e115012SGarrett Wollman
172526195adSJordan K. Hubbard static void
print_ifsizeof(int indent,const char * prefix,const char * type)173e390e3afSDavid Malone print_ifsizeof(int indent, const char *prefix, const char *type)
1744e115012SGarrett Wollman {
175ff49530fSBill Paul if (indent) {
176ff49530fSBill Paul f_print(fout, ",\n");
177ff49530fSBill Paul tabify(fout, indent);
1784e115012SGarrett Wollman } else {
179ff49530fSBill Paul f_print(fout, ", ");
180ff49530fSBill Paul }
181ff49530fSBill Paul if (streq(type, "bool")) {
182ff49530fSBill Paul f_print(fout, "sizeof (bool_t), (xdrproc_t) xdr_bool");
183ff49530fSBill Paul } else {
184ff49530fSBill Paul f_print(fout, "sizeof (");
1854e115012SGarrett Wollman if (undefined(type) && prefix) {
1864e115012SGarrett Wollman f_print(fout, "%s ", prefix);
1874e115012SGarrett Wollman }
188ff49530fSBill Paul f_print(fout, "%s), (xdrproc_t) xdr_%s", type, type);
1894e115012SGarrett Wollman }
1904e115012SGarrett Wollman }
1914e115012SGarrett Wollman
192526195adSJordan K. Hubbard static void
print_ifclose(int indent,int brace)193844b7833SJohn Birrell print_ifclose(int indent, int brace)
1944e115012SGarrett Wollman {
195ff49530fSBill Paul f_print(fout, "))\n");
1964e115012SGarrett Wollman tabify(fout, indent);
1974e115012SGarrett Wollman f_print(fout, "\treturn (FALSE);\n");
198844b7833SJohn Birrell if (brace)
199844b7833SJohn Birrell f_print(fout, "\t}\n");
2004e115012SGarrett Wollman }
2014e115012SGarrett Wollman
202526195adSJordan K. Hubbard static void
print_ifstat(int indent,const char * prefix,const char * type,relation rel,const char * amax,const char * objname,const char * name)203e390e3afSDavid Malone print_ifstat(int indent, const char *prefix, const char *type, relation rel,
204e390e3afSDavid Malone const char *amax, const char *objname, const char *name)
2054e115012SGarrett Wollman {
206e390e3afSDavid Malone const char *alt = NULL;
207844b7833SJohn Birrell int brace = 0;
2084e115012SGarrett Wollman
2094e115012SGarrett Wollman switch (rel) {
2104e115012SGarrett Wollman case REL_POINTER:
211844b7833SJohn Birrell brace = 1;
212844b7833SJohn Birrell f_print(fout, "\t{\n");
213844b7833SJohn Birrell f_print(fout, "\t%s **pp = %s;\n", type, objname);
2144e115012SGarrett Wollman print_ifopen(indent, "pointer");
2154e115012SGarrett Wollman print_ifarg("(char **)");
216844b7833SJohn Birrell f_print(fout, "pp");
217ff49530fSBill Paul print_ifsizeof(0, prefix, type);
2184e115012SGarrett Wollman break;
2194e115012SGarrett Wollman case REL_VECTOR:
2204e115012SGarrett Wollman if (streq(type, "string")) {
2214e115012SGarrett Wollman alt = "string";
2224e115012SGarrett Wollman } else if (streq(type, "opaque")) {
2234e115012SGarrett Wollman alt = "opaque";
2244e115012SGarrett Wollman }
2254e115012SGarrett Wollman if (alt) {
2264e115012SGarrett Wollman print_ifopen(indent, alt);
2274e115012SGarrett Wollman print_ifarg(objname);
2284e115012SGarrett Wollman } else {
2294e115012SGarrett Wollman print_ifopen(indent, "vector");
2304e115012SGarrett Wollman print_ifarg("(char *)");
2314e115012SGarrett Wollman f_print(fout, "%s", objname);
2324e115012SGarrett Wollman }
2334e115012SGarrett Wollman print_ifarg(amax);
2344e115012SGarrett Wollman if (!alt) {
235ff49530fSBill Paul print_ifsizeof(indent + 1, prefix, type);
2364e115012SGarrett Wollman }
2374e115012SGarrett Wollman break;
2384e115012SGarrett Wollman case REL_ARRAY:
2394e115012SGarrett Wollman if (streq(type, "string")) {
2404e115012SGarrett Wollman alt = "string";
2414e115012SGarrett Wollman } else if (streq(type, "opaque")) {
2424e115012SGarrett Wollman alt = "bytes";
2434e115012SGarrett Wollman }
2444e115012SGarrett Wollman if (streq(type, "string")) {
2454e115012SGarrett Wollman print_ifopen(indent, alt);
2464e115012SGarrett Wollman print_ifarg(objname);
2474e115012SGarrett Wollman } else {
2484e115012SGarrett Wollman if (alt) {
2494e115012SGarrett Wollman print_ifopen(indent, alt);
2504e115012SGarrett Wollman } else {
2514e115012SGarrett Wollman print_ifopen(indent, "array");
2524e115012SGarrett Wollman }
2534e115012SGarrett Wollman print_ifarg("(char **)");
2544e115012SGarrett Wollman if (*objname == '&') {
2554e115012SGarrett Wollman f_print(fout, "%s.%s_val, (u_int *) %s.%s_len",
2564e115012SGarrett Wollman objname, name, objname, name);
2574e115012SGarrett Wollman } else {
258ff49530fSBill Paul f_print(fout,
259ff49530fSBill Paul "&%s->%s_val, (u_int *) &%s->%s_len",
2604e115012SGarrett Wollman objname, name, objname, name);
2614e115012SGarrett Wollman }
2624e115012SGarrett Wollman }
2634e115012SGarrett Wollman print_ifarg(amax);
2644e115012SGarrett Wollman if (!alt) {
265ff49530fSBill Paul print_ifsizeof(indent + 1, prefix, type);
2664e115012SGarrett Wollman }
2674e115012SGarrett Wollman break;
2684e115012SGarrett Wollman case REL_ALIAS:
2694e115012SGarrett Wollman print_ifopen(indent, type);
2704e115012SGarrett Wollman print_ifarg(objname);
2714e115012SGarrett Wollman break;
2724e115012SGarrett Wollman }
273844b7833SJohn Birrell print_ifclose(indent, brace);
2744e115012SGarrett Wollman }
2754e115012SGarrett Wollman
2764e115012SGarrett Wollman /* ARGSUSED */
277526195adSJordan K. Hubbard static void
emit_enum(definition * def __unused)278e390e3afSDavid Malone emit_enum(definition *def __unused)
2794e115012SGarrett Wollman {
2804e115012SGarrett Wollman print_ifopen(1, "enum");
2814e115012SGarrett Wollman print_ifarg("(enum_t *)objp");
282844b7833SJohn Birrell print_ifclose(1, 0);
2834e115012SGarrett Wollman }
2844e115012SGarrett Wollman
285526195adSJordan K. Hubbard static void
emit_program(definition * def)286e390e3afSDavid Malone emit_program(definition *def)
287ff49530fSBill Paul {
288ff49530fSBill Paul decl_list *dl;
289ff49530fSBill Paul version_list *vlist;
290ff49530fSBill Paul proc_list *plist;
291ff49530fSBill Paul
292ff49530fSBill Paul for (vlist = def->def.pr.versions; vlist != NULL; vlist = vlist->next)
293ff49530fSBill Paul for (plist = vlist->procs; plist != NULL; plist = plist->next) {
294ff49530fSBill Paul if (!newstyle || plist->arg_num < 2)
295ff49530fSBill Paul continue; /* old style, or single argument */
296ff49530fSBill Paul print_prog_header(plist);
297ff49530fSBill Paul for (dl = plist->args.decls; dl != NULL;
298ff49530fSBill Paul dl = dl->next)
299ff49530fSBill Paul print_stat(1, &dl->decl);
300ff49530fSBill Paul print_trailer();
301ff49530fSBill Paul }
302ff49530fSBill Paul }
303ff49530fSBill Paul
3044e115012SGarrett Wollman
305526195adSJordan K. Hubbard static void
emit_union(definition * def)306e390e3afSDavid Malone emit_union(definition *def)
3074e115012SGarrett Wollman {
3084e115012SGarrett Wollman declaration *dflt;
3094e115012SGarrett Wollman case_list *cl;
3104e115012SGarrett Wollman declaration *cs;
3114e115012SGarrett Wollman char *object;
312e390e3afSDavid Malone const char *vecformat = "objp->%s_u.%s";
313e390e3afSDavid Malone const char *format = "&objp->%s_u.%s";
3144e115012SGarrett Wollman
315ff49530fSBill Paul print_stat(1, &def->def.un.enum_decl);
3164e115012SGarrett Wollman f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
3174e115012SGarrett Wollman for (cl = def->def.un.cases; cl != NULL; cl = cl->next) {
318ff49530fSBill Paul
3194e115012SGarrett Wollman f_print(fout, "\tcase %s:\n", cl->case_name);
320ff49530fSBill Paul if (cl->contflag == 1) /* a continued case statement */
321ff49530fSBill Paul continue;
322ff49530fSBill Paul cs = &cl->case_decl;
3234e115012SGarrett Wollman if (!streq(cs->type, "void")) {
32475863a6dSPhilippe Charnier object = xmalloc(strlen(def->def_name) +
32575863a6dSPhilippe Charnier strlen(format) + strlen(cs->name) + 1);
326ff49530fSBill Paul if (isvectordef (cs->type, cs->rel)) {
327ff49530fSBill Paul s_print(object, vecformat, def->def_name,
328ff49530fSBill Paul cs->name);
329ff49530fSBill Paul } else {
330ff49530fSBill Paul s_print(object, format, def->def_name,
331ff49530fSBill Paul cs->name);
332ff49530fSBill Paul }
333ff49530fSBill Paul print_ifstat(2, cs->prefix, cs->type, cs->rel,
334ff49530fSBill Paul cs->array_max, object, cs->name);
3354e115012SGarrett Wollman free(object);
3364e115012SGarrett Wollman }
3374e115012SGarrett Wollman f_print(fout, "\t\tbreak;\n");
3384e115012SGarrett Wollman }
3394e115012SGarrett Wollman dflt = def->def.un.default_decl;
3404e115012SGarrett Wollman if (dflt != NULL) {
3414e115012SGarrett Wollman if (!streq(dflt->type, "void")) {
3424e115012SGarrett Wollman f_print(fout, "\tdefault:\n");
34375863a6dSPhilippe Charnier object = xmalloc(strlen(def->def_name) +
34475863a6dSPhilippe Charnier strlen(format) + strlen(dflt->name) + 1);
345ff49530fSBill Paul if (isvectordef (dflt->type, dflt->rel)) {
346ff49530fSBill Paul s_print(object, vecformat, def->def_name,
347ff49530fSBill Paul dflt->name);
348ff49530fSBill Paul } else {
349ff49530fSBill Paul s_print(object, format, def->def_name,
350ff49530fSBill Paul dflt->name);
351ff49530fSBill Paul }
352ff49530fSBill Paul
3534e115012SGarrett Wollman print_ifstat(2, dflt->prefix, dflt->type, dflt->rel,
3544e115012SGarrett Wollman dflt->array_max, object, dflt->name);
3554e115012SGarrett Wollman free(object);
3564e115012SGarrett Wollman f_print(fout, "\t\tbreak;\n");
357526195adSJordan K. Hubbard } else {
358526195adSJordan K. Hubbard f_print(fout, "\tdefault:\n");
359526195adSJordan K. Hubbard f_print(fout, "\t\tbreak;\n");
3604e115012SGarrett Wollman }
3614e115012SGarrett Wollman } else {
3624e115012SGarrett Wollman f_print(fout, "\tdefault:\n");
3634e115012SGarrett Wollman f_print(fout, "\t\treturn (FALSE);\n");
3644e115012SGarrett Wollman }
365ff49530fSBill Paul
3664e115012SGarrett Wollman f_print(fout, "\t}\n");
3674e115012SGarrett Wollman }
3684e115012SGarrett Wollman
369ff49530fSBill Paul static void
inline_struct(definition * def,int flag)370e390e3afSDavid Malone inline_struct(definition *def, int flag)
371ff49530fSBill Paul {
372ff49530fSBill Paul decl_list *dl;
373ff49530fSBill Paul int i, size;
374ff49530fSBill Paul decl_list *cur, *psav;
375ff49530fSBill Paul bas_type *ptr;
376e390e3afSDavid Malone char *sizestr;
377e390e3afSDavid Malone const char *plus;
378ff49530fSBill Paul char ptemp[256];
379ff49530fSBill Paul int indent = 1;
3804e115012SGarrett Wollman
38140ad8885SAlfred Perlstein cur = NULL;
382ff49530fSBill Paul if (flag == PUT)
383ff49530fSBill Paul f_print(fout, "\n\tif (xdrs->x_op == XDR_ENCODE) {\n");
384ff49530fSBill Paul else
385ff49530fSBill Paul f_print(fout, "\t\treturn (TRUE);\n\t} else if (xdrs->x_op == XDR_DECODE) {\n");
386ff49530fSBill Paul
387ff49530fSBill Paul i = 0;
388ff49530fSBill Paul size = 0;
389ff49530fSBill Paul sizestr = NULL;
390ff49530fSBill Paul for (dl = def->def.st.decls; dl != NULL; dl = dl->next) { /* xxx */
391ff49530fSBill Paul /* now walk down the list and check for basic types */
392ff49530fSBill Paul if ((dl->decl.prefix == NULL) &&
393ff49530fSBill Paul ((ptr = find_type(dl->decl.type)) != NULL) &&
394ff49530fSBill Paul ((dl->decl.rel == REL_ALIAS) ||
395ff49530fSBill Paul (dl->decl.rel == REL_VECTOR))){
396ff49530fSBill Paul if (i == 0)
397ff49530fSBill Paul cur = dl;
398ff49530fSBill Paul i++;
399ff49530fSBill Paul
400ff49530fSBill Paul if (dl->decl.rel == REL_ALIAS)
401ff49530fSBill Paul size += ptr->length;
402ff49530fSBill Paul else {
403ff49530fSBill Paul /* this code is required to handle arrays */
404ff49530fSBill Paul if (sizestr == NULL)
405ff49530fSBill Paul plus = "";
406ff49530fSBill Paul else
407ff49530fSBill Paul plus = " + ";
408ff49530fSBill Paul
409ff49530fSBill Paul if (ptr->length != 1)
410ff49530fSBill Paul s_print(ptemp, "%s%s * %d",
411ff49530fSBill Paul plus, dl->decl.array_max,
412ff49530fSBill Paul ptr->length);
413ff49530fSBill Paul else
414ff49530fSBill Paul s_print(ptemp, "%s%s", plus,
415ff49530fSBill Paul dl->decl.array_max);
416ff49530fSBill Paul
417ff49530fSBill Paul /* now concatenate to sizestr !!!! */
41875863a6dSPhilippe Charnier if (sizestr == NULL) {
41975863a6dSPhilippe Charnier sizestr = xstrdup(ptemp);
42075863a6dSPhilippe Charnier }
421ff49530fSBill Paul else{
42275863a6dSPhilippe Charnier sizestr = xrealloc(sizestr,
423ff49530fSBill Paul strlen(sizestr)
424ff49530fSBill Paul +strlen(ptemp)+1);
425ff49530fSBill Paul sizestr = strcat(sizestr, ptemp);
426ff49530fSBill Paul /* build up length of array */
427ff49530fSBill Paul }
428ff49530fSBill Paul }
429ff49530fSBill Paul } else {
4309ef5c48bSBill Fumerola if (i > 0) {
431122562cdSStefan Farfeleder if (sizestr == NULL && size < inline_size){
432ff49530fSBill Paul /*
433ff49530fSBill Paul * don't expand into inline code
434122562cdSStefan Farfeleder * if size < inline_size
435ff49530fSBill Paul */
436ff49530fSBill Paul while (cur != dl){
437ff49530fSBill Paul print_stat(indent + 1, &cur->decl);
438ff49530fSBill Paul cur = cur->next;
439ff49530fSBill Paul }
440ff49530fSBill Paul } else {
441ff49530fSBill Paul /* were already looking at a xdr_inlineable structure */
442ff49530fSBill Paul tabify(fout, indent + 1);
443ff49530fSBill Paul if (sizestr == NULL)
444ff49530fSBill Paul f_print(fout, "buf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
445ff49530fSBill Paul size);
4469ef5c48bSBill Fumerola else {
447ff49530fSBill Paul if (size == 0)
448ff49530fSBill Paul f_print(fout,
449ff49530fSBill Paul "buf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
450ff49530fSBill Paul sizestr);
451ff49530fSBill Paul else
452ff49530fSBill Paul f_print(fout,
453ff49530fSBill Paul "buf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
454ff49530fSBill Paul size, sizestr);
455ff49530fSBill Paul
4569ef5c48bSBill Fumerola }
457ff49530fSBill Paul f_print(fout, "\n");
458ff49530fSBill Paul tabify(fout, indent + 1);
459ff49530fSBill Paul f_print(fout,
460ff49530fSBill Paul "if (buf == NULL) {\n");
461ff49530fSBill Paul
462ff49530fSBill Paul psav = cur;
463ff49530fSBill Paul while (cur != dl){
464ff49530fSBill Paul print_stat(indent + 2, &cur->decl);
465ff49530fSBill Paul cur = cur->next;
466ff49530fSBill Paul }
467ff49530fSBill Paul
468ff49530fSBill Paul f_print(fout, "\n\t\t} else {\n");
469ff49530fSBill Paul
470ff49530fSBill Paul cur = psav;
471ff49530fSBill Paul while (cur != dl){
472ff49530fSBill Paul emit_inline(indent + 2, &cur->decl, flag);
473ff49530fSBill Paul cur = cur->next;
474ff49530fSBill Paul }
475ff49530fSBill Paul
476ff49530fSBill Paul tabify(fout, indent + 1);
477ff49530fSBill Paul f_print(fout, "}\n");
478ff49530fSBill Paul }
4799ef5c48bSBill Fumerola }
480ff49530fSBill Paul size = 0;
481ff49530fSBill Paul i = 0;
4823f65dafdSXin LI free(sizestr);
483ff49530fSBill Paul sizestr = NULL;
484ff49530fSBill Paul print_stat(indent + 1, &dl->decl);
485ff49530fSBill Paul }
486ff49530fSBill Paul }
487ff49530fSBill Paul
48840ad8885SAlfred Perlstein if (i > 0) {
489122562cdSStefan Farfeleder if (sizestr == NULL && size < inline_size){
490122562cdSStefan Farfeleder /* don't expand into inline code if size < inline_size */
491ff49530fSBill Paul while (cur != dl){
492ff49530fSBill Paul print_stat(indent + 1, &cur->decl);
493ff49530fSBill Paul cur = cur->next;
494ff49530fSBill Paul }
495ff49530fSBill Paul } else {
496ff49530fSBill Paul /* were already looking at a xdr_inlineable structure */
497ff49530fSBill Paul if (sizestr == NULL)
498ff49530fSBill Paul f_print(fout, "\t\tbuf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);",
499ff49530fSBill Paul size);
500ff49530fSBill Paul else
501ff49530fSBill Paul if (size == 0)
502ff49530fSBill Paul f_print(fout,
503ff49530fSBill Paul "\t\tbuf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);",
504ff49530fSBill Paul sizestr);
505ff49530fSBill Paul else
506ff49530fSBill Paul f_print(fout,
507ff49530fSBill Paul "\t\tbuf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);",
508ff49530fSBill Paul size, sizestr);
509ff49530fSBill Paul
510ff49530fSBill Paul f_print(fout, "\n\t\tif (buf == NULL) {\n");
511ff49530fSBill Paul psav = cur;
512ff49530fSBill Paul while (cur != NULL){
513ff49530fSBill Paul print_stat(indent + 2, &cur->decl);
514ff49530fSBill Paul cur = cur->next;
515ff49530fSBill Paul }
516ff49530fSBill Paul f_print(fout, "\t\t} else {\n");
517ff49530fSBill Paul
518ff49530fSBill Paul cur = psav;
519ff49530fSBill Paul while (cur != dl){
520ff49530fSBill Paul emit_inline(indent + 2, &cur->decl, flag);
521ff49530fSBill Paul cur = cur->next;
522ff49530fSBill Paul }
523ff49530fSBill Paul f_print(fout, "\t\t}\n");
524ff49530fSBill Paul }
525ff49530fSBill Paul }
52640ad8885SAlfred Perlstein }
5274e115012SGarrett Wollman
528526195adSJordan K. Hubbard static void
emit_struct(definition * def)529e390e3afSDavid Malone emit_struct(definition *def)
5304e115012SGarrett Wollman {
5314e115012SGarrett Wollman decl_list *dl;
532526195adSJordan K. Hubbard int j, size, flag;
533ff49530fSBill Paul bas_type *ptr;
534ff49530fSBill Paul int can_inline;
5354e115012SGarrett Wollman
536122562cdSStefan Farfeleder if (inline_size == 0) {
537ff49530fSBill Paul /* No xdr_inlining at all */
538ff49530fSBill Paul for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
539ff49530fSBill Paul print_stat(1, &dl->decl);
540ff49530fSBill Paul return;
5414e115012SGarrett Wollman }
5424e115012SGarrett Wollman
543ff49530fSBill Paul for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
544*e9ed334fSPedro F. Giffuni if (dl->decl.rel == REL_VECTOR &&
545*e9ed334fSPedro F. Giffuni strcmp(dl->decl.type, "opaque") != 0){
546ff49530fSBill Paul f_print(fout, "\tint i;\n");
547ff49530fSBill Paul break;
548ff49530fSBill Paul }
5494e115012SGarrett Wollman
550ff49530fSBill Paul size = 0;
551ff49530fSBill Paul can_inline = 0;
552ff49530fSBill Paul /*
553ff49530fSBill Paul * Make a first pass and see if inling is possible.
554ff49530fSBill Paul */
555ff49530fSBill Paul for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
556ff49530fSBill Paul if ((dl->decl.prefix == NULL) &&
557ff49530fSBill Paul ((ptr = find_type(dl->decl.type)) != NULL) &&
558ff49530fSBill Paul ((dl->decl.rel == REL_ALIAS)||
559ff49530fSBill Paul (dl->decl.rel == REL_VECTOR))){
560ff49530fSBill Paul if (dl->decl.rel == REL_ALIAS)
561ff49530fSBill Paul size += ptr->length;
562ff49530fSBill Paul else {
563ff49530fSBill Paul can_inline = 1;
564ff49530fSBill Paul break; /* can be inlined */
565ff49530fSBill Paul }
566ff49530fSBill Paul } else {
567122562cdSStefan Farfeleder if (size >= inline_size){
568ff49530fSBill Paul can_inline = 1;
569ff49530fSBill Paul break; /* can be inlined */
570ff49530fSBill Paul }
571ff49530fSBill Paul size = 0;
572ff49530fSBill Paul }
573122562cdSStefan Farfeleder if (size >= inline_size)
574ff49530fSBill Paul can_inline = 1;
5754e115012SGarrett Wollman
576ff49530fSBill Paul if (can_inline == 0){ /* can not inline, drop back to old mode */
577ff49530fSBill Paul for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
578ff49530fSBill Paul print_stat(1, &dl->decl);
579ff49530fSBill Paul return;
580ff49530fSBill Paul }
581ff49530fSBill Paul
582ff49530fSBill Paul flag = PUT;
583ff49530fSBill Paul for (j = 0; j < 2; j++){
584ff49530fSBill Paul inline_struct(def, flag);
585ff49530fSBill Paul if (flag == PUT)
586ff49530fSBill Paul flag = GET;
587ff49530fSBill Paul }
588ff49530fSBill Paul
589ff49530fSBill Paul f_print(fout, "\t\treturn (TRUE);\n\t}\n\n");
590ff49530fSBill Paul
591ff49530fSBill Paul /* now take care of XDR_FREE case */
592ff49530fSBill Paul
593ff49530fSBill Paul for (dl = def->def.st.decls; dl != NULL; dl = dl->next)
594ff49530fSBill Paul print_stat(1, &dl->decl);
595ff49530fSBill Paul
596ff49530fSBill Paul }
5974e115012SGarrett Wollman
598526195adSJordan K. Hubbard static void
emit_typedef(definition * def)599e390e3afSDavid Malone emit_typedef(definition *def)
6004e115012SGarrett Wollman {
601e390e3afSDavid Malone const char *prefix = def->def.ty.old_prefix;
602e390e3afSDavid Malone const char *type = def->def.ty.old_type;
603e390e3afSDavid Malone const char *amax = def->def.ty.array_max;
6044e115012SGarrett Wollman relation rel = def->def.ty.rel;
6054e115012SGarrett Wollman
6064e115012SGarrett Wollman print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
6074e115012SGarrett Wollman }
6084e115012SGarrett Wollman
609526195adSJordan K. Hubbard static void
print_stat(int indent,declaration * dec)610e390e3afSDavid Malone print_stat(int indent, declaration *dec)
6114e115012SGarrett Wollman {
612e390e3afSDavid Malone const char *prefix = dec->prefix;
613e390e3afSDavid Malone const char *type = dec->type;
614e390e3afSDavid Malone const char *amax = dec->array_max;
6154e115012SGarrett Wollman relation rel = dec->rel;
6164e115012SGarrett Wollman char name[256];
6174e115012SGarrett Wollman
6184e115012SGarrett Wollman if (isvectordef(type, rel)) {
6194e115012SGarrett Wollman s_print(name, "objp->%s", dec->name);
6204e115012SGarrett Wollman } else {
6214e115012SGarrett Wollman s_print(name, "&objp->%s", dec->name);
6224e115012SGarrett Wollman }
623ff49530fSBill Paul print_ifstat(indent, prefix, type, rel, amax, name, dec->name);
624ff49530fSBill Paul }
625ff49530fSBill Paul
626ff49530fSBill Paul
627e390e3afSDavid Malone char *upcase(const char *);
628ff49530fSBill Paul
629526195adSJordan K. Hubbard static void
emit_inline(int indent,declaration * decl,int flag)630e390e3afSDavid Malone emit_inline(int indent, declaration *decl, int flag)
631ff49530fSBill Paul {
632ff49530fSBill Paul switch (decl->rel) {
633ff49530fSBill Paul case REL_ALIAS :
634ff49530fSBill Paul emit_single_in_line(indent, decl, flag, REL_ALIAS);
635ff49530fSBill Paul break;
636ff49530fSBill Paul case REL_VECTOR :
637ff49530fSBill Paul tabify(fout, indent);
638ff49530fSBill Paul f_print(fout, "{\n");
639ff49530fSBill Paul tabify(fout, indent + 1);
640896cdc31SStefan Farfeleder f_print(fout, "%s *genp;\n\n", decl->type);
641ff49530fSBill Paul tabify(fout, indent + 1);
642ff49530fSBill Paul f_print(fout,
643ff49530fSBill Paul "for (i = 0, genp = objp->%s;\n", decl->name);
644ff49530fSBill Paul tabify(fout, indent + 2);
645ff49530fSBill Paul f_print(fout, "i < %s; i++) {\n", decl->array_max);
646ff49530fSBill Paul emit_single_in_line(indent + 2, decl, flag, REL_VECTOR);
647ff49530fSBill Paul tabify(fout, indent + 1);
648ff49530fSBill Paul f_print(fout, "}\n");
649ff49530fSBill Paul tabify(fout, indent);
650ff49530fSBill Paul f_print(fout, "}\n");
65140ad8885SAlfred Perlstein break;
652526195adSJordan K. Hubbard default:
65340ad8885SAlfred Perlstein break;
654ff49530fSBill Paul }
655ff49530fSBill Paul }
656ff49530fSBill Paul
657526195adSJordan K. Hubbard static void
emit_single_in_line(int indent,declaration * decl,int flag,relation rel)658e390e3afSDavid Malone emit_single_in_line(int indent, declaration *decl, int flag, relation rel)
659ff49530fSBill Paul {
660ff49530fSBill Paul char *upp_case;
661ff49530fSBill Paul
662ff49530fSBill Paul tabify(fout, indent);
663ff49530fSBill Paul if (flag == PUT)
664ff49530fSBill Paul f_print(fout, "IXDR_PUT_");
665ff49530fSBill Paul else
666ff49530fSBill Paul if (rel == REL_ALIAS)
667ff49530fSBill Paul f_print(fout, "objp->%s = IXDR_GET_", decl->name);
668ff49530fSBill Paul else
669ff49530fSBill Paul f_print(fout, "*genp++ = IXDR_GET_");
670ff49530fSBill Paul
671ff49530fSBill Paul upp_case = upcase(decl->type);
672ff49530fSBill Paul
673ff49530fSBill Paul /* hack - XX */
674ff49530fSBill Paul if (strcmp(upp_case, "INT") == 0)
675ff49530fSBill Paul {
676ff49530fSBill Paul free(upp_case);
677e390e3afSDavid Malone upp_case = strdup("LONG");
678ff49530fSBill Paul }
679ff49530fSBill Paul
680ff49530fSBill Paul if (strcmp(upp_case, "U_INT") == 0)
681ff49530fSBill Paul {
682ff49530fSBill Paul free(upp_case);
683e390e3afSDavid Malone upp_case = strdup("U_LONG");
684ff49530fSBill Paul }
685ff49530fSBill Paul if (flag == PUT)
686ff49530fSBill Paul if (rel == REL_ALIAS)
687ff49530fSBill Paul f_print(fout,
688ff49530fSBill Paul "%s(buf, objp->%s);\n", upp_case, decl->name);
689ff49530fSBill Paul else
690ff49530fSBill Paul f_print(fout, "%s(buf, *genp++);\n", upp_case);
691ff49530fSBill Paul
692ff49530fSBill Paul else
693ff49530fSBill Paul f_print(fout, "%s(buf);\n", upp_case);
694ff49530fSBill Paul free(upp_case);
695ff49530fSBill Paul }
696ff49530fSBill Paul
697e390e3afSDavid Malone char *
upcase(const char * str)698e390e3afSDavid Malone upcase(const char *str)
699ff49530fSBill Paul {
700ff49530fSBill Paul char *ptr, *hptr;
701ff49530fSBill Paul
70275863a6dSPhilippe Charnier ptr = (char *)xmalloc(strlen(str)+1);
703ff49530fSBill Paul
704ff49530fSBill Paul hptr = ptr;
705ff49530fSBill Paul while (*str != '\0')
706ff49530fSBill Paul *ptr++ = toupper(*str++);
707ff49530fSBill Paul
708ff49530fSBill Paul *ptr = '\0';
709ff49530fSBill Paul return (hptr);
7104e115012SGarrett Wollman }
711