Home | History | Annotate | Line # | Download | only in rpcgen
rpc_hout.c revision 1.1
      1 /* @(#)rpc_hout.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_hout.c 1.6 87/07/28 (C) 1987 SMI";
     32 #endif
     33 
     34 int pconstdef(), pstructdef(), puniondef(), pdefine(), pprogramdef(),
     35     penumdef(), ptypedef(), pdeclaration(), undefined2();
     36 
     37 /*
     38  * rpc_hout.c, Header file outputter for the RPC protocol compiler
     39  * Copyright (C) 1987, Sun Microsystems, Inc.
     40  */
     41 #include <stdio.h>
     42 #include <ctype.h>
     43 #include "rpc_util.h"
     44 #include "rpc_parse.h"
     45 
     46 
     47 /*
     48  * Print the C-version of an xdr definition
     49  */
     50 void
     51 print_datadef(def)
     52 	definition *def;
     53 {
     54 	if (def->def_kind != DEF_CONST) {
     55 		f_print(fout, "\n");
     56 	}
     57 	switch (def->def_kind) {
     58 	case DEF_STRUCT:
     59 		pstructdef(def);
     60 		break;
     61 	case DEF_UNION:
     62 		puniondef(def);
     63 		break;
     64 	case DEF_ENUM:
     65 		penumdef(def);
     66 		break;
     67 	case DEF_TYPEDEF:
     68 		ptypedef(def);
     69 		break;
     70 	case DEF_PROGRAM:
     71 		pprogramdef(def);
     72 		break;
     73 	case DEF_CONST:
     74 		pconstdef(def);
     75 		break;
     76 	}
     77 	if (def->def_kind != DEF_PROGRAM && def->def_kind != DEF_CONST) {
     78 		f_print(fout, "bool_t xdr_%s();\n", def->def_name);
     79 	}
     80 	if (def->def_kind != DEF_CONST) {
     81 		f_print(fout, "\n");
     82 	}
     83 }
     84 
     85 static
     86 pconstdef(def)
     87 	definition *def;
     88 {
     89 	pdefine(def->def_name, def->def.co);
     90 }
     91 
     92 static
     93 pstructdef(def)
     94 	definition *def;
     95 {
     96 	decl_list *l;
     97 	char *name = def->def_name;
     98 
     99 	f_print(fout, "struct %s {\n", name);
    100 	for (l = def->def.st.decls; l != NULL; l = l->next) {
    101 		pdeclaration(name, &l->decl, 1);
    102 	}
    103 	f_print(fout, "};\n");
    104 	f_print(fout, "typedef struct %s %s;\n", name, name);
    105 }
    106 
    107 static
    108 puniondef(def)
    109 	definition *def;
    110 {
    111 	case_list *l;
    112 	char *name = def->def_name;
    113 	declaration *decl;
    114 
    115 	f_print(fout, "struct %s {\n", name);
    116 	decl = &def->def.un.enum_decl;
    117 	if (streq(decl->type, "bool")) {
    118 		f_print(fout, "\tbool_t %s;\n", decl->name);
    119 	} else {
    120 		f_print(fout, "\t%s %s;\n", decl->type, decl->name);
    121 	}
    122 	f_print(fout, "\tunion {\n");
    123 	for (l = def->def.un.cases; l != NULL; l = l->next) {
    124 		pdeclaration(name, &l->case_decl, 2);
    125 	}
    126 	decl = def->def.un.default_decl;
    127 	if (decl && !streq(decl->type, "void")) {
    128 		pdeclaration(name, decl, 2);
    129 	}
    130 	f_print(fout, "\t} %s_u;\n", name);
    131 	f_print(fout, "};\n");
    132 	f_print(fout, "typedef struct %s %s;\n", name, name);
    133 }
    134 
    135 
    136 
    137 static
    138 pdefine(name, num)
    139 	char *name;
    140 	char *num;
    141 {
    142 	f_print(fout, "#define %s %s\n", name, num);
    143 }
    144 
    145 static
    146 puldefine(name, num)
    147 	char *name;
    148 	char *num;
    149 {
    150 	f_print(fout, "#define %s ((u_long)%s)\n", name, num);
    151 }
    152 
    153 static
    154 define_printed(stop, start)
    155 	proc_list *stop;
    156 	version_list *start;
    157 {
    158 	version_list *vers;
    159 	proc_list *proc;
    160 
    161 	for (vers = start; vers != NULL; vers = vers->next) {
    162 		for (proc = vers->procs; proc != NULL; proc = proc->next) {
    163 			if (proc == stop) {
    164 				return (0);
    165 			} else if (streq(proc->proc_name, stop->proc_name)) {
    166 				return (1);
    167 			}
    168 		}
    169 	}
    170 	abort();
    171 	/* NOTREACHED */
    172 }
    173 
    174 
    175 static
    176 pprogramdef(def)
    177 	definition *def;
    178 {
    179 	version_list *vers;
    180 	proc_list *proc;
    181 
    182 	puldefine(def->def_name, def->def.pr.prog_num);
    183 	for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
    184 		puldefine(vers->vers_name, vers->vers_num);
    185 		for (proc = vers->procs; proc != NULL; proc = proc->next) {
    186 			if (!define_printed(proc, def->def.pr.versions)) {
    187 				puldefine(proc->proc_name, proc->proc_num);
    188 			}
    189 			pprocdef(proc, vers);
    190 		}
    191 	}
    192 }
    193 
    194 
    195 pprocdef(proc, vp)
    196 	proc_list *proc;
    197 	version_list *vp;
    198 {
    199 	f_print(fout, "extern ");
    200 	if (proc->res_prefix) {
    201 		if (streq(proc->res_prefix, "enum")) {
    202 			f_print(fout, "enum ");
    203 		} else {
    204 			f_print(fout, "struct ");
    205 		}
    206 	}
    207 	if (streq(proc->res_type, "bool")) {
    208 		f_print(fout, "bool_t *");
    209 	} else if (streq(proc->res_type, "string")) {
    210 		f_print(fout, "char **");
    211 	} else {
    212 		f_print(fout, "%s *", fixtype(proc->res_type));
    213 	}
    214 	pvname(proc->proc_name, vp->vers_num);
    215 	f_print(fout, "();\n");
    216 }
    217 
    218 static
    219 penumdef(def)
    220 	definition *def;
    221 {
    222 	char *name = def->def_name;
    223 	enumval_list *l;
    224 	char *last = NULL;
    225 	int count = 0;
    226 
    227 	f_print(fout, "enum %s {\n", name);
    228 	for (l = def->def.en.vals; l != NULL; l = l->next) {
    229 		f_print(fout, "\t%s", l->name);
    230 		if (l->assignment) {
    231 			f_print(fout, " = %s", l->assignment);
    232 			last = l->assignment;
    233 			count = 1;
    234 		} else {
    235 			if (last == NULL) {
    236 				f_print(fout, " = %d", count++);
    237 			} else {
    238 				f_print(fout, " = %s + %d", last, count++);
    239 			}
    240 		}
    241 		f_print(fout, ",\n");
    242 	}
    243 	f_print(fout, "};\n");
    244 	f_print(fout, "typedef enum %s %s;\n", name, name);
    245 }
    246 
    247 static
    248 ptypedef(def)
    249 	definition *def;
    250 {
    251 	char *name = def->def_name;
    252 	char *old = def->def.ty.old_type;
    253 	char prefix[8];	/* enough to contain "struct ", including NUL */
    254 	relation rel = def->def.ty.rel;
    255 
    256 
    257 	if (!streq(name, old)) {
    258 		if (streq(old, "string")) {
    259 			old = "char";
    260 			rel = REL_POINTER;
    261 		} else if (streq(old, "opaque")) {
    262 			old = "char";
    263 		} else if (streq(old, "bool")) {
    264 			old = "bool_t";
    265 		}
    266 		if (undefined2(old, name) && def->def.ty.old_prefix) {
    267 			s_print(prefix, "%s ", def->def.ty.old_prefix);
    268 		} else {
    269 			prefix[0] = 0;
    270 		}
    271 		f_print(fout, "typedef ");
    272 		switch (rel) {
    273 		case REL_ARRAY:
    274 			f_print(fout, "struct {\n");
    275 			f_print(fout, "\tu_int %s_len;\n", name);
    276 			f_print(fout, "\t%s%s *%s_val;\n", prefix, old, name);
    277 			f_print(fout, "} %s", name);
    278 			break;
    279 		case REL_POINTER:
    280 			f_print(fout, "%s%s *%s", prefix, old, name);
    281 			break;
    282 		case REL_VECTOR:
    283 			f_print(fout, "%s%s %s[%s]", prefix, old, name,
    284 				def->def.ty.array_max);
    285 			break;
    286 		case REL_ALIAS:
    287  			f_print(fout, "%s%s %s", prefix, old, name);
    288 			break;
    289 		}
    290 		f_print(fout, ";\n");
    291 	}
    292 }
    293 
    294 
    295 static
    296 pdeclaration(name, dec, tab)
    297 	char *name;
    298 	declaration *dec;
    299 	int tab;
    300 {
    301 	char buf[8];	/* enough to hold "struct ", include NUL */
    302 	char *prefix;
    303 	char *type;
    304 
    305 	if (streq(dec->type, "void")) {
    306 		return;
    307 	}
    308 	tabify(fout, tab);
    309 	if (streq(dec->type, name) && !dec->prefix) {
    310 		f_print(fout, "struct ");
    311 	}
    312 	if (streq(dec->type, "string")) {
    313 		f_print(fout, "char *%s", dec->name);
    314 	} else {
    315 		prefix = "";
    316 		if (streq(dec->type, "bool")) {
    317 			type = "bool_t";
    318 		} else if (streq(dec->type, "opaque")) {
    319 			type = "char";
    320 		} else {
    321 			if (dec->prefix) {
    322 				s_print(buf, "%s ", dec->prefix);
    323 				prefix = buf;
    324 			}
    325 			type = dec->type;
    326 		}
    327 		switch (dec->rel) {
    328 		case REL_ALIAS:
    329 			f_print(fout, "%s%s %s", prefix, type, dec->name);
    330 			break;
    331 		case REL_VECTOR:
    332 			f_print(fout, "%s%s %s[%s]", prefix, type, dec->name,
    333 				dec->array_max);
    334 			break;
    335 		case REL_POINTER:
    336 			f_print(fout, "%s%s *%s", prefix, type, dec->name);
    337 			break;
    338 		case REL_ARRAY:
    339 			f_print(fout, "struct {\n");
    340 			tabify(fout, tab);
    341 			f_print(fout, "\tu_int %s_len;\n", dec->name);
    342 			tabify(fout, tab);
    343 			f_print(fout, "\t%s%s *%s_val;\n", prefix, type, dec->name);
    344 			tabify(fout, tab);
    345 			f_print(fout, "} %s", dec->name);
    346 			break;
    347 		}
    348 	}
    349 	f_print(fout, ";\n");
    350 }
    351 
    352 
    353 
    354 static
    355 undefined2(type, stop)
    356 	char *type;
    357 	char *stop;
    358 {
    359 	list *l;
    360 	definition *def;
    361 
    362 	for (l = defined; l != NULL; l = l->next) {
    363 		def = (definition *) l->val;
    364 		if (def->def_kind != DEF_PROGRAM) {
    365 			if (streq(def->def_name, stop)) {
    366 				return (1);
    367 			} else if (streq(def->def_name, type)) {
    368 				return (0);
    369 			}
    370 		}
    371 	}
    372 	return (1);
    373 }
    374