rpc_hout.c revision 200462
11590Srgrimes/*
21590Srgrimes * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
31590Srgrimes * unrestricted use provided that this legend is included on all tape
41590Srgrimes * media and as a part of the software program in whole or part.  Users
51590Srgrimes * may copy or modify Sun RPC without charge, but are not authorized
61590Srgrimes * to license or distribute it to anyone else except as part of a product or
71590Srgrimes * program developed by the user.
81590Srgrimes *
91590Srgrimes * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
101590Srgrimes * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
111590Srgrimes * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
121590Srgrimes *
131590Srgrimes * Sun RPC is provided with no support and without any obligation on the
141590Srgrimes * part of Sun Microsystems, Inc. to assist in its use, correction,
151590Srgrimes * modification or enhancement.
161590Srgrimes *
171590Srgrimes * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
181590Srgrimes * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
191590Srgrimes * OR ANY PART THEREOF.
201590Srgrimes *
211590Srgrimes * In no event will Sun Microsystems, Inc. be liable for any lost revenue
221590Srgrimes * or profits or other special, indirect and consequential damages, even if
231590Srgrimes * Sun has been advised of the possibility of such damages.
241590Srgrimes *
251590Srgrimes * Sun Microsystems, Inc.
261590Srgrimes * 2550 Garcia Avenue
271590Srgrimes * Mountain View, California  94043
281590Srgrimes */
291590Srgrimes
301590Srgrimes#if 0
311590Srgrimes#ifndef lint
321590Srgrimes#ident	"@(#)rpc_hout.c	1.16	94/04/25 SMI"
331590Srgrimesstatic char sccsid[] = "@(#)rpc_hout.c 1.12 89/02/22 (C) 1987 SMI";
341590Srgrimes#endif
351590Srgrimes#endif
361590Srgrimes
371590Srgrimes#include <sys/cdefs.h>
381590Srgrimes__FBSDID("$FreeBSD: head/usr.bin/rpcgen/rpc_hout.c 200462 2009-12-13 03:14:06Z delphij $");
391590Srgrimes
401590Srgrimes/*
411590Srgrimes * rpc_hout.c, Header file outputter for the RPC protocol compiler
421590Srgrimes * Copyright (C) 1987, Sun Microsystems, Inc.
431590Srgrimes */
441590Srgrimes#include <stdio.h>
451590Srgrimes#include <ctype.h>
461590Srgrimes#include "rpc_parse.h"
471590Srgrimes#include "rpc_scan.h"
481590Srgrimes#include "rpc_util.h"
491590Srgrimes
501590Srgrimesvoid storexdrfuncdecl(const char *, int );
511590Srgrimesstatic void pconstdef( definition * );
521590Srgrimesstatic void pstructdef( definition * );
531590Srgrimesstatic void puniondef( definition * );
541590Srgrimesstatic void pprogramdef( definition *, int );
551590Srgrimesstatic void penumdef( definition * );
561590Srgrimesstatic void ptypedef( definition * );
571590Srgrimesstatic void pdefine(const char *, const char *);
581590Srgrimesstatic int undefined2(const char *, const char *);
591590Srgrimesstatic void parglist(proc_list *, const char *);
601590Srgrimesstatic void pprocdef(proc_list *, version_list *, const char *, int);
611590Srgrimes
621590Srgrimes/*
631590Srgrimes * Print the C-version of an xdr definition
641590Srgrimes */
651590Srgrimesvoid
661590Srgrimesprint_datadef(definition *def, int headeronly)
671590Srgrimes{
681590Srgrimes
691590Srgrimes	if (def->def_kind == DEF_PROGRAM)  /* handle data only */
701590Srgrimes		return;
711590Srgrimes
721590Srgrimes	if (def->def_kind != DEF_CONST) {
731590Srgrimes		f_print(fout, "\n");
741590Srgrimes	}
751590Srgrimes	switch (def->def_kind) {
761590Srgrimes	case DEF_STRUCT:
771590Srgrimes		pstructdef(def);
781590Srgrimes		break;
791590Srgrimes	case DEF_UNION:
801590Srgrimes		puniondef(def);
811590Srgrimes		break;
821590Srgrimes	case DEF_ENUM:
831590Srgrimes		penumdef(def);
841590Srgrimes		break;
851590Srgrimes	case DEF_TYPEDEF:
861590Srgrimes		ptypedef(def);
871590Srgrimes		break;
881590Srgrimes	case DEF_PROGRAM:
891590Srgrimes		pprogramdef(def, headeronly);
901590Srgrimes		break;
911590Srgrimes	case DEF_CONST:
921590Srgrimes		pconstdef(def);
931590Srgrimes		break;
941590Srgrimes	}
951590Srgrimes	if (def->def_kind != DEF_PROGRAM && def->def_kind != DEF_CONST) {
961590Srgrimes	    storexdrfuncdecl(def->def_name,
971590Srgrimes			     def->def_kind != DEF_TYPEDEF ||
981590Srgrimes			     !isvectordef(def->def.ty.old_type,
991590Srgrimes					  def->def.ty.rel));
1001590Srgrimes	}
1011590Srgrimes}
1021590Srgrimes
1031590Srgrimes
1041590Srgrimesvoid
1051590Srgrimesprint_funcdef(definition *def, int headeronly)
1061590Srgrimes{
1071590Srgrimes	switch (def->def_kind) {
1081590Srgrimes	case DEF_PROGRAM:
1091590Srgrimes		f_print(fout, "\n");
1101590Srgrimes		pprogramdef(def, headeronly);
1111590Srgrimes		break;
1121590Srgrimes	default:
1131590Srgrimes		break;
1141590Srgrimes	}
1151590Srgrimes}
1161590Srgrimes
1171590Srgrimes/* store away enough information to allow the XDR functions to be spat
1181590Srgrimes    out at the end of the file */
1191590Srgrimes
1201590Srgrimesvoid
1211590Srgrimesstorexdrfuncdecl(const char *name, int pointerp)
1221590Srgrimes{
1231590Srgrimes	xdrfunc * xdrptr;
1241590Srgrimes
1251590Srgrimes	xdrptr = XALLOC(struct xdrfunc);
1261590Srgrimes
1271590Srgrimes	xdrptr->name = name;
1281590Srgrimes	xdrptr->pointerp = pointerp;
1291590Srgrimes	xdrptr->next = NULL;
1301590Srgrimes
1311590Srgrimes	if (xdrfunc_tail == NULL){
1321590Srgrimes		xdrfunc_head = xdrptr;
1331590Srgrimes		xdrfunc_tail = xdrptr;
1341590Srgrimes	} else {
1351590Srgrimes		xdrfunc_tail->next = xdrptr;
1361590Srgrimes		xdrfunc_tail = xdrptr;
1371590Srgrimes	}
1381590Srgrimes
1391590Srgrimes
1401590Srgrimes}
1411590Srgrimes
1421590Srgrimesvoid
1431590Srgrimesprint_xdr_func_def(const char *name, int pointerp)
1441590Srgrimes{
1451590Srgrimes	f_print(fout, "extern  bool_t xdr_%s(XDR *, %s%s);\n", name,
1461590Srgrimes		name, pointerp ? "*" : "");
1471590Srgrimes}
1481590Srgrimes
1491590Srgrimes
1501590Srgrimesstatic void
1511590Srgrimespconstdef(def)
1521590Srgrimes	definition *def;
1531590Srgrimes{
1541590Srgrimes	pdefine(def->def_name, def->def.co);
1551590Srgrimes}
1561590Srgrimes
1571590Srgrimes/* print out the definitions for the arguments of functions in the
1581590Srgrimes    header file
1591590Srgrimes*/
1601590Srgrimesstatic void
1611590Srgrimespargdef(definition *def)
1621590Srgrimes{
1631590Srgrimes	decl_list *l;
1641590Srgrimes	version_list *vers;
1651590Srgrimes	char *name;
1661590Srgrimes	proc_list *plist;
1671590Srgrimes
1681590Srgrimes
1691590Srgrimes	for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
1701590Srgrimes			for (plist = vers->procs; plist != NULL;
1711590Srgrimes			    plist = plist->next) {
1721590Srgrimes
1731590Srgrimes				if (!newstyle || plist->arg_num < 2) {
1741590Srgrimes					continue; /* old style or single args */
1751590Srgrimes				}
1761590Srgrimes				name = plist->args.argname;
1771590Srgrimes				f_print(fout, "struct %s {\n", name);
1781590Srgrimes				for (l = plist->args.decls;
1791590Srgrimes				    l != NULL; l = l->next) {
1801590Srgrimes					pdeclaration(name, &l->decl, 1,
1811590Srgrimes						     ";\n");
1821590Srgrimes				}
1831590Srgrimes				f_print(fout, "};\n");
1841590Srgrimes				f_print(fout, "typedef struct %s %s;\n",
1851590Srgrimes					name, name);
1861590Srgrimes				storexdrfuncdecl(name, 1);
1871590Srgrimes				f_print(fout, "\n");
1881590Srgrimes			}
1891590Srgrimes		}
1901590Srgrimes}
1911590Srgrimes
1921590Srgrimes
1931590Srgrimesstatic void
1941590Srgrimespstructdef(definition *def)
1951590Srgrimes{
1961590Srgrimes	decl_list *l;
1971590Srgrimes	const char *name = def->def_name;
1981590Srgrimes
1991590Srgrimes	f_print(fout, "struct %s {\n", name);
2001590Srgrimes	for (l = def->def.st.decls; l != NULL; l = l->next) {
2011590Srgrimes		pdeclaration(name, &l->decl, 1, ";\n");
2021590Srgrimes	}
20317339Sadam	f_print(fout, "};\n");
20417339Sadam	f_print(fout, "typedef struct %s %s;\n", name, name);
20517339Sadam}
20617339Sadam
20717339Sadamstatic void
2081590Srgrimespuniondef(def)
2091590Srgrimes	definition *def;
2101590Srgrimes{
2111590Srgrimes	case_list *l;
2121590Srgrimes	const char *name = def->def_name;
2131590Srgrimes	declaration *decl;
2141590Srgrimes
2151590Srgrimes	f_print(fout, "struct %s {\n", name);
2161590Srgrimes	decl = &def->def.un.enum_decl;
2171590Srgrimes	if (streq(decl->type, "bool")) {
2181590Srgrimes		f_print(fout, "\tbool_t %s;\n", decl->name);
2191590Srgrimes	} else {
2201590Srgrimes		f_print(fout, "\t%s %s;\n", decl->type, decl->name);
2211590Srgrimes	}
2221590Srgrimes	f_print(fout, "\tunion {\n");
2231590Srgrimes	for (l = def->def.un.cases; l != NULL; l = l->next) {
2241590Srgrimes	    if (l->contflag == 0)
2251590Srgrimes		pdeclaration(name, &l->case_decl, 2, ";\n");
2261590Srgrimes	}
2271590Srgrimes	decl = def->def.un.default_decl;
2281590Srgrimes	if (decl && !streq(decl->type, "void")) {
2291590Srgrimes		pdeclaration(name, decl, 2, ";\n");
2301590Srgrimes	}
2311590Srgrimes	f_print(fout, "\t} %s_u;\n", name);
2321590Srgrimes	f_print(fout, "};\n");
2331590Srgrimes	f_print(fout, "typedef struct %s %s;\n", name, name);
2341590Srgrimes}
2351590Srgrimes
2361590Srgrimesstatic void
2371590Srgrimespdefine(const char *name, const char *num)
2381590Srgrimes{
2391590Srgrimes	f_print(fout, "#define\t%s %s\n", name, num);
2401590Srgrimes}
2411590Srgrimes
2421590Srgrimesstatic void
2431590Srgrimespuldefine(const char *name, const char *num)
2441590Srgrimes{
2451590Srgrimes	f_print(fout, "#define\t%s ((unsigned long)(%s))\n", name, num);
2461590Srgrimes}
2471590Srgrimes
2481590Srgrimesstatic int
2491590Srgrimesdefine_printed(proc_list *stop, version_list *start)
2501590Srgrimes{
2511590Srgrimes	version_list *vers;
2521590Srgrimes	proc_list *proc;
2531590Srgrimes
2541590Srgrimes	for (vers = start; vers != NULL; vers = vers->next) {
2551590Srgrimes		for (proc = vers->procs; proc != NULL; proc = proc->next) {
2561590Srgrimes			if (proc == stop) {
2571590Srgrimes				return (0);
2581590Srgrimes			} else if (streq(proc->proc_name, stop->proc_name)) {
2591590Srgrimes				return (1);
2601590Srgrimes			}
2611590Srgrimes		}
2621590Srgrimes	}
2631590Srgrimes	abort();
2641590Srgrimes	/* NOTREACHED */
265}
266
267static void
268pfreeprocdef(const char * name, const char *vers)
269{
270	f_print(fout, "extern int ");
271	pvname(name, vers);
272	f_print(fout, "_freeresult(SVCXPRT *, xdrproc_t, caddr_t);\n");
273}
274
275static void
276pdispatch(const char * name, const char *vers)
277{
278
279	f_print(fout, "void ");
280	pvname(name, vers);
281	f_print(fout, "(struct svc_req *rqstp, SVCXPRT *transp);\n");
282}
283
284static void
285pprogramdef(definition *def, int headeronly)
286{
287	version_list *vers;
288	proc_list *proc;
289	const char *ext;
290
291	pargdef(def);
292
293	puldefine(def->def_name, def->def.pr.prog_num);
294	for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
295		if (tblflag) {
296			f_print(fout,
297				"extern struct rpcgen_table %s_%s_table[];\n",
298				locase(def->def_name), vers->vers_num);
299			f_print(fout,
300				"extern %s_%s_nproc;\n",
301				locase(def->def_name), vers->vers_num);
302		}
303		puldefine(vers->vers_name, vers->vers_num);
304
305		f_print(fout, "\n");
306		ext = "extern  ";
307		if (headeronly) {
308			f_print(fout, "%s", ext);
309			pdispatch(def->def_name, vers->vers_num);
310		}
311		for (proc = vers->procs; proc != NULL; proc = proc->next) {
312			if (!define_printed(proc, def->def.pr.versions)) {
313				puldefine(proc->proc_name, proc->proc_num);
314			}
315			f_print(fout, "%s", ext);
316			pprocdef(proc, vers, "CLIENT *", 0);
317			f_print(fout, "%s", ext);
318			pprocdef(proc, vers, "struct svc_req *", 1);
319		}
320		pfreeprocdef(def->def_name, vers->vers_num);
321	}
322}
323
324static void
325pprocdef(proc_list *proc, version_list *vp, const char *addargtype, int server_p)
326{
327	if (mtflag) {/* Print MT style stubs */
328		if (server_p)
329			f_print(fout, "bool_t ");
330		else
331			f_print(fout, "enum clnt_stat ");
332	} else {
333		ptype(proc->res_prefix, proc->res_type, 1);
334		f_print(fout, "* ");
335	}
336	if (server_p)
337		pvname_svc(proc->proc_name, vp->vers_num);
338	else
339		pvname(proc->proc_name, vp->vers_num);
340
341	parglist(proc, addargtype);
342}
343
344
345
346/* print out argument list of procedure */
347static void
348parglist(proc_list *proc, const char *addargtype)
349{
350	decl_list *dl;
351
352	f_print(fout, "(");
353	if (proc->arg_num < 2 && newstyle &&
354	    streq(proc->args.decls->decl.type, "void")) {
355		/* 0 argument in new style:  do nothing*/
356	}
357	else {
358		for (dl = proc->args.decls; dl != NULL; dl = dl->next) {
359			ptype(dl->decl.prefix, dl->decl.type, 1);
360			if (!newstyle)
361				f_print(fout, "*");
362			/* old style passes by reference */
363			f_print(fout, ", ");
364		}
365	}
366
367	if (mtflag)  {
368		ptype(proc->res_prefix, proc->res_type, 1);
369		f_print(fout, "*, ");
370	}
371
372	f_print(fout, "%s);\n", addargtype);
373
374}
375
376static void
377penumdef(def)
378	definition *def;
379{
380	const char *name = def->def_name;
381	enumval_list *l;
382	const char *last = NULL;
383	int count = 0;
384
385	f_print(fout, "enum %s {\n", name);
386	for (l = def->def.en.vals; l != NULL; l = l->next) {
387		f_print(fout, "\t%s", l->name);
388		if (l->assignment) {
389			f_print(fout, " = %s", l->assignment);
390			last = l->assignment;
391			count = 1;
392		} else {
393			if (last == NULL) {
394				f_print(fout, " = %d", count++);
395			} else {
396				f_print(fout, " = %s + %d", last, count++);
397			}
398		}
399		if (l->next)
400			f_print(fout, ",\n");
401		else
402			f_print(fout, "\n");
403	}
404	f_print(fout, "};\n");
405	f_print(fout, "typedef enum %s %s;\n", name, name);
406}
407
408static void
409ptypedef(def)
410	definition *def;
411{
412	const char *name = def->def_name;
413	const char *old = def->def.ty.old_type;
414	char prefix[8];	/* enough to contain "struct ", including NUL */
415	relation rel = def->def.ty.rel;
416
417
418	if (!streq(name, old)) {
419		if (streq(old, "string")) {
420			old = "char";
421			rel = REL_POINTER;
422		} else if (streq(old, "opaque")) {
423			old = "char";
424		} else 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
455void
456pdeclaration(const char *name, declaration *dec, int tab, const char *separator)
457{
458	char buf[8];	/* enough to hold "struct ", include NUL */
459	const char *prefix;
460	const char *type;
461
462	if (streq(dec->type, "void")) {
463		return;
464	}
465	tabify(fout, tab);
466	if (streq(dec->type, name) && !dec->prefix) {
467		f_print(fout, "struct ");
468	}
469	if (streq(dec->type, "string")) {
470		f_print(fout, "char *%s", dec->name);
471	} else {
472		prefix = "";
473		if (streq(dec->type, "bool")) {
474			type = "bool_t";
475		} else if (streq(dec->type, "opaque")) {
476			type = "char";
477		} else {
478			if (dec->prefix) {
479				s_print(buf, "%s ", dec->prefix);
480				prefix = buf;
481			}
482			type = dec->type;
483		}
484		switch (dec->rel) {
485		case REL_ALIAS:
486			f_print(fout, "%s%s %s", prefix, type, dec->name);
487			break;
488		case REL_VECTOR:
489			f_print(fout, "%s%s %s[%s]", prefix, type, dec->name,
490				dec->array_max);
491			break;
492		case REL_POINTER:
493			f_print(fout, "%s%s *%s", prefix, type, dec->name);
494			break;
495		case REL_ARRAY:
496			f_print(fout, "struct {\n");
497			tabify(fout, tab);
498			f_print(fout, "\tu_int %s_len;\n", dec->name);
499			tabify(fout, tab);
500			f_print(fout,
501				"\t%s%s *%s_val;\n", prefix, type, dec->name);
502			tabify(fout, tab);
503			f_print(fout, "} %s", dec->name);
504			break;
505		}
506	}
507	f_print(fout, separator);
508}
509
510static int
511undefined2(const char *type, const char *stop)
512{
513	list *l;
514	definition *def;
515
516	for (l = defined; l != NULL; l = l->next) {
517		def = (definition *) l->val;
518		if (def->def_kind != DEF_PROGRAM) {
519			if (streq(def->def_name, stop)) {
520				return (1);
521			} else if (streq(def->def_name, type)) {
522				return (0);
523			}
524		}
525	}
526	return (1);
527}
528