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