Home | History | Annotate | Line # | Download | only in rpcgen
rpc_hout.c revision 1.7
      1 /*	$NetBSD: rpc_hout.c,v 1.7 1997/10/18 10:53:48 lukem Exp $	*/
      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 or with the express written consent of
      9  * Sun Microsystems, Inc.
     10  *
     11  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
     12  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
     13  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
     14  *
     15  * Sun RPC is provided with no support and without any obligation on the
     16  * part of Sun Microsystems, Inc. to assist in its use, correction,
     17  * modification or enhancement.
     18  *
     19  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
     20  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
     21  * OR ANY PART THEREOF.
     22  *
     23  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
     24  * or profits or other special, indirect and consequential damages, even if
     25  * Sun has been advised of the possibility of such damages.
     26  *
     27  * Sun Microsystems, Inc.
     28  * 2550 Garcia Avenue
     29  * Mountain View, California  94043
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 #if 0
     35 static char sccsid[] = "@(#)rpc_hout.c 1.12 89/02/22 (C) 1987 SMI";
     36 #else
     37 __RCSID("$NetBSD: rpc_hout.c,v 1.7 1997/10/18 10:53:48 lukem Exp $");
     38 #endif
     39 #endif
     40 
     41 /*
     42  * rpc_hout.c, Header file outputter for the RPC protocol compiler
     43  */
     44 #include <stdio.h>
     45 #include <ctype.h>
     46 #include <err.h>
     47 #include "rpc_scan.h"
     48 #include "rpc_parse.h"
     49 #include "rpc_util.h"
     50 
     51 static void pconstdef __P((definition *));
     52 static void pargdef __P((definition *));
     53 static void pstructdef __P((definition *));
     54 static void puniondef __P((definition *));
     55 static void pdefine __P((char *, char *));
     56 static void puldefine __P((char *, char *));
     57 static int define_printed __P((proc_list *, version_list *));
     58 static void pprogramdef __P((definition *));
     59 static void parglist __P((proc_list *, char *));
     60 static void penumdef __P((definition *));
     61 static void ptypedef __P((definition *));
     62 static int undefined2 __P((char *, char *));
     63 
     64 /*
     65  * Print the C-version of an xdr definition
     66  */
     67 void
     68 print_datadef(def)
     69 	definition *def;
     70 {
     71 
     72 	if (def->def_kind == DEF_PROGRAM)	/* handle data only */
     73 		return;
     74 
     75 	if (def->def_kind != DEF_CONST) {
     76 		f_print(fout, "\n");
     77 	}
     78 	switch (def->def_kind) {
     79 	case DEF_STRUCT:
     80 		pstructdef(def);
     81 		break;
     82 	case DEF_UNION:
     83 		puniondef(def);
     84 		break;
     85 	case DEF_ENUM:
     86 		penumdef(def);
     87 		break;
     88 	case DEF_TYPEDEF:
     89 		ptypedef(def);
     90 		break;
     91 	case DEF_PROGRAM:
     92 		pprogramdef(def);
     93 		break;
     94 	case DEF_CONST:
     95 		pconstdef(def);
     96 		break;
     97 	}
     98 	if (def->def_kind != DEF_PROGRAM && def->def_kind != DEF_CONST) {
     99 		pxdrfuncdecl(def->def_name,
    100 		    def->def_kind != DEF_TYPEDEF ||
    101 		    !isvectordef(def->def.ty.old_type, def->def.ty.rel));
    102 
    103 	}
    104 }
    105 
    106 
    107 void
    108 print_funcdef(def)
    109 	definition *def;
    110 {
    111 	switch (def->def_kind) {
    112 	case DEF_PROGRAM:
    113 		f_print(fout, "\n");
    114 		pprogramdef(def);
    115 		break;
    116 	case DEF_CONST:
    117 	case DEF_TYPEDEF:
    118 	case DEF_ENUM:
    119 	case DEF_UNION:
    120 	case DEF_STRUCT:
    121 		break;
    122 	}
    123 }
    124 
    125 void
    126 pxdrfuncdecl(name, pointerp)
    127 	char   *name;
    128 	int     pointerp;
    129 {
    130 
    131 	f_print(fout, "#ifdef __cplusplus\n");
    132 	f_print(fout, "extern \"C\" bool_t xdr_%s(XDR *, %s%s);\n",
    133 	    name,
    134 	    name, pointerp ? ("*") : "");
    135 	f_print(fout, "#elif __STDC__\n");
    136 	f_print(fout, "extern  bool_t xdr_%s(XDR *, %s%s);\n",
    137 	    name,
    138 	    name, pointerp ? ("*") : "");
    139 	f_print(fout, "#else /* Old Style C */\n");
    140 	f_print(fout, "bool_t xdr_%s();\n", name);
    141 	f_print(fout, "#endif /* Old Style C */\n\n");
    142 }
    143 
    144 
    145 static void
    146 pconstdef(def)
    147 	definition *def;
    148 {
    149 	pdefine(def->def_name, def->def.co);
    150 }
    151 /* print out the definitions for the arguments of functions in the
    152    header file
    153 */
    154 static void
    155 pargdef(def)
    156 	definition *def;
    157 {
    158 	decl_list *l;
    159 	version_list *vers;
    160 	char   *name;
    161 	proc_list *plist;
    162 
    163 
    164 	for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
    165 		for (plist = vers->procs; plist != NULL;
    166 		    plist = plist->next) {
    167 
    168 			if (!newstyle || plist->arg_num < 2) {
    169 				continue;	/* old style or single args */
    170 			}
    171 			name = plist->args.argname;
    172 			f_print(fout, "struct %s {\n", name);
    173 			for (l = plist->args.decls;
    174 			    l != NULL; l = l->next) {
    175 				pdeclaration(name, &l->decl, 1, ";\n");
    176 			}
    177 			f_print(fout, "};\n");
    178 			f_print(fout, "typedef struct %s %s;\n", name, name);
    179 			pxdrfuncdecl(name, NULL);
    180 			f_print(fout, "\n");
    181 		}
    182 	}
    183 
    184 }
    185 
    186 
    187 static void
    188 pstructdef(def)
    189 	definition *def;
    190 {
    191 	decl_list *l;
    192 	char   *name = def->def_name;
    193 
    194 	f_print(fout, "struct %s {\n", name);
    195 	for (l = def->def.st.decls; l != NULL; l = l->next) {
    196 		pdeclaration(name, &l->decl, 1, ";\n");
    197 	}
    198 	f_print(fout, "};\n");
    199 	f_print(fout, "typedef struct %s %s;\n", name, name);
    200 }
    201 
    202 static void
    203 puniondef(def)
    204 	definition *def;
    205 {
    206 	case_list *l;
    207 	char   *name = def->def_name;
    208 	declaration *decl;
    209 
    210 	f_print(fout, "struct %s {\n", name);
    211 	decl = &def->def.un.enum_decl;
    212 	if (streq(decl->type, "bool")) {
    213 		f_print(fout, "\tbool_t %s;\n", decl->name);
    214 	} else {
    215 		f_print(fout, "\t%s %s;\n", decl->type, decl->name);
    216 	}
    217 	f_print(fout, "\tunion {\n");
    218 	for (l = def->def.un.cases; l != NULL; l = l->next) {
    219 		if (l->contflag == 0)
    220 			pdeclaration(name, &l->case_decl, 2, ";\n");
    221 	}
    222 	decl = def->def.un.default_decl;
    223 	if (decl && !streq(decl->type, "void")) {
    224 		pdeclaration(name, decl, 2, ";\n");
    225 	}
    226 	f_print(fout, "\t} %s_u;\n", name);
    227 	f_print(fout, "};\n");
    228 	f_print(fout, "typedef struct %s %s;\n", name, name);
    229 }
    230 
    231 static void
    232 pdefine(name, num)
    233 	char   *name;
    234 	char   *num;
    235 {
    236 	f_print(fout, "#define %s %s\n", name, num);
    237 }
    238 
    239 static void
    240 puldefine(name, num)
    241 	char   *name;
    242 	char   *num;
    243 {
    244 	f_print(fout, "#define %s ((u_long)%s)\n", name, num);
    245 }
    246 
    247 static int
    248 define_printed(stop, start)
    249 	proc_list *stop;
    250 	version_list *start;
    251 {
    252 	version_list *vers;
    253 	proc_list *proc;
    254 
    255 	for (vers = start; vers != NULL; vers = vers->next) {
    256 		for (proc = vers->procs; proc != NULL; proc = proc->next) {
    257 			if (proc == stop) {
    258 				return (0);
    259 			} else
    260 				if (streq(proc->proc_name, stop->proc_name)) {
    261 					return (1);
    262 				}
    263 		}
    264 	}
    265 	errx(1, "Internal error %s, %d: procedure not found\n",
    266 	    __FILE__, __LINE__);
    267 	/* NOTREACHED */
    268 }
    269 
    270 static void
    271 pprogramdef(def)
    272 	definition *def;
    273 {
    274 	version_list *vers;
    275 	proc_list *proc;
    276 	int     i;
    277 	char   *ext;
    278 
    279 	pargdef(def);
    280 
    281 	puldefine(def->def_name, def->def.pr.prog_num);
    282 	for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
    283 		if (tblflag) {
    284 			f_print(fout, "extern struct rpcgen_table %s_%s_table[];\n",
    285 			    locase(def->def_name), vers->vers_num);
    286 			f_print(fout, "extern %s_%s_nproc;\n",
    287 			    locase(def->def_name), vers->vers_num);
    288 		}
    289 		puldefine(vers->vers_name, vers->vers_num);
    290 
    291 		/* Print out 3 definitions, one for ANSI-C, another for C++, a
    292 		 * third for old style C */
    293 
    294 		for (i = 0; i < 3; i++) {
    295 			if (i == 0) {
    296 				f_print(fout, "\n#ifdef __cplusplus\n");
    297 				ext = "extern \"C\" ";
    298 			} else
    299 				if (i == 1) {
    300 					f_print(fout, "\n#elif __STDC__\n");
    301 					ext = "extern  ";
    302 				} else {
    303 					f_print(fout, "\n#else /* Old Style C */\n");
    304 					ext = "extern  ";
    305 				}
    306 
    307 
    308 			for (proc = vers->procs; proc != NULL; proc = proc->next) {
    309 				if (!define_printed(proc, def->def.pr.versions)) {
    310 					puldefine(proc->proc_name, proc->proc_num);
    311 				}
    312 				f_print(fout, "%s", ext);
    313 				pprocdef(proc, vers, "CLIENT *", 0, i);
    314 				f_print(fout, "%s", ext);
    315 				pprocdef(proc, vers, "struct svc_req *", 1, i);
    316 
    317 			}
    318 
    319 		}
    320 		f_print(fout, "#endif /* Old Style C */\n");
    321 	}
    322 }
    323 
    324 void
    325 pprocdef(proc, vp, addargtype, server_p, mode)
    326 	proc_list *proc;
    327 	version_list *vp;
    328 	char   *addargtype;
    329 	int     server_p;
    330 	int     mode;
    331 {
    332 
    333 	ptype(proc->res_prefix, proc->res_type, 1);
    334 	f_print(fout, "* ");
    335 	if (server_p)
    336 		pvname_svc(proc->proc_name, vp->vers_num);
    337 	else
    338 		pvname(proc->proc_name, vp->vers_num);
    339 
    340 	/*
    341 	 * mode  0 == cplusplus, mode  1 = ANSI-C, mode 2 = old style C
    342 	 */
    343 	if (mode == 0 || mode == 1)
    344 		parglist(proc, addargtype);
    345 	else
    346 		f_print(fout, "();\n");
    347 }
    348 
    349 
    350 /* print out argument list of procedure */
    351 static void
    352 parglist(proc, addargtype)
    353 	proc_list *proc;
    354 	char   *addargtype;
    355 {
    356 	decl_list *dl;
    357 
    358 	f_print(fout, "(");
    359 
    360 	if (proc->arg_num < 2 && newstyle &&
    361 	    streq(proc->args.decls->decl.type, "void")) {
    362 		/* 0 argument in new style:  do nothing */
    363 	} else {
    364 		for (dl = proc->args.decls; dl != NULL; dl = dl->next) {
    365 			ptype(dl->decl.prefix, dl->decl.type, 1);
    366 			if (!newstyle)
    367 				f_print(fout, "*");	/* old style passes by
    368 							 * reference */
    369 
    370 			f_print(fout, ", ");
    371 		}
    372 	}
    373 
    374 	f_print(fout, "%s);\n", addargtype);
    375 }
    376 
    377 static void
    378 penumdef(def)
    379 	definition *def;
    380 {
    381 	char   *name = def->def_name;
    382 	enumval_list *l;
    383 	char   *last = NULL;
    384 	int     count = 0;
    385 
    386 	f_print(fout, "enum %s {\n", name);
    387 	for (l = def->def.en.vals; l != NULL; l = l->next) {
    388 		f_print(fout, "\t%s", l->name);
    389 		if (l->assignment) {
    390 			f_print(fout, " = %s", l->assignment);
    391 			last = l->assignment;
    392 			count = 1;
    393 		} else {
    394 			if (last == NULL) {
    395 				f_print(fout, " = %d", count++);
    396 			} else {
    397 				f_print(fout, " = %s + %d", last, count++);
    398 			}
    399 		}
    400 		f_print(fout, ",\n");
    401 	}
    402 	f_print(fout, "};\n");
    403 	f_print(fout, "typedef enum %s %s;\n", name, name);
    404 }
    405 
    406 static void
    407 ptypedef(def)
    408 	definition *def;
    409 {
    410 	char   *name = def->def_name;
    411 	char   *old = def->def.ty.old_type;
    412 	char    prefix[8];	/* enough to contain "struct ", including NUL */
    413 	relation rel = def->def.ty.rel;
    414 
    415 
    416 	if (!streq(name, old)) {
    417 		if (streq(old, "string")) {
    418 			old = "char";
    419 			rel = REL_POINTER;
    420 		} else
    421 			if (streq(old, "opaque")) {
    422 				old = "char";
    423 			} else
    424 				if (streq(old, "bool")) {
    425 					old = "bool_t";
    426 				}
    427 		if (undefined2(old, name) && def->def.ty.old_prefix) {
    428 			s_print(prefix, "%s ", def->def.ty.old_prefix);
    429 		} else {
    430 			prefix[0] = 0;
    431 		}
    432 		f_print(fout, "typedef ");
    433 		switch (rel) {
    434 		case REL_ARRAY:
    435 			f_print(fout, "struct {\n");
    436 			f_print(fout, "\tu_int %s_len;\n", name);
    437 			f_print(fout, "\t%s%s *%s_val;\n", prefix, old, name);
    438 			f_print(fout, "} %s", name);
    439 			break;
    440 		case REL_POINTER:
    441 			f_print(fout, "%s%s *%s", prefix, old, name);
    442 			break;
    443 		case REL_VECTOR:
    444 			f_print(fout, "%s%s %s[%s]", prefix, old, name,
    445 			    def->def.ty.array_max);
    446 			break;
    447 		case REL_ALIAS:
    448 			f_print(fout, "%s%s %s", prefix, old, name);
    449 			break;
    450 		}
    451 		f_print(fout, ";\n");
    452 	}
    453 }
    454 
    455 void
    456 pdeclaration(name, dec, tab, separator)
    457 	char   *name;
    458 	declaration *dec;
    459 	int     tab;
    460 	char   *separator;
    461 {
    462 	char    buf[8];		/* enough to hold "struct ", include NUL */
    463 	char   *prefix;
    464 	char   *type;
    465 
    466 	if (streq(dec->type, "void")) {
    467 		return;
    468 	}
    469 	tabify(fout, tab);
    470 	if (streq(dec->type, name) && !dec->prefix) {
    471 		f_print(fout, "struct ");
    472 	}
    473 	if (streq(dec->type, "string")) {
    474 		f_print(fout, "char *%s", dec->name);
    475 	} else {
    476 		prefix = "";
    477 		if (streq(dec->type, "bool")) {
    478 			type = "bool_t";
    479 		} else
    480 			if (streq(dec->type, "opaque")) {
    481 				type = "char";
    482 			} else {
    483 				if (dec->prefix) {
    484 					s_print(buf, "%s ", dec->prefix);
    485 					prefix = buf;
    486 				}
    487 				type = dec->type;
    488 			}
    489 		switch (dec->rel) {
    490 		case REL_ALIAS:
    491 			f_print(fout, "%s%s %s", prefix, type, dec->name);
    492 			break;
    493 		case REL_VECTOR:
    494 			f_print(fout, "%s%s %s[%s]", prefix, type, dec->name,
    495 			    dec->array_max);
    496 			break;
    497 		case REL_POINTER:
    498 			f_print(fout, "%s%s *%s", prefix, type, dec->name);
    499 			break;
    500 		case REL_ARRAY:
    501 			f_print(fout, "struct {\n");
    502 			tabify(fout, tab);
    503 			f_print(fout, "\tu_int %s_len;\n", dec->name);
    504 			tabify(fout, tab);
    505 			f_print(fout, "\t%s%s *%s_val;\n", prefix, type, dec->name);
    506 			tabify(fout, tab);
    507 			f_print(fout, "} %s", dec->name);
    508 			break;
    509 		}
    510 	}
    511 	f_print(fout, separator);
    512 }
    513 
    514 static int
    515 undefined2(type, stop)
    516 	char   *type;
    517 	char   *stop;
    518 {
    519 	list   *l;
    520 	definition *def;
    521 
    522 	for (l = defined; l != NULL; l = l->next) {
    523 		def = (definition *) l->val;
    524 		if (def->def_kind != DEF_PROGRAM) {
    525 			if (streq(def->def_name, stop)) {
    526 				return (1);
    527 			} else
    528 				if (streq(def->def_name, type)) {
    529 					return (0);
    530 				}
    531 		}
    532 	}
    533 	return (1);
    534 }
    535