112795Swpaul/*
212795Swpaul * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
312795Swpaul * unrestricted use provided that this legend is included on all tape
412795Swpaul * media and as a part of the software program in whole or part.  Users
512795Swpaul * may copy or modify Sun RPC without charge, but are not authorized
612795Swpaul * to license or distribute it to anyone else except as part of a product or
712795Swpaul * program developed by the user.
8100441Scharnier *
912795Swpaul * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
1012795Swpaul * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
1112795Swpaul * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12100441Scharnier *
1312795Swpaul * Sun RPC is provided with no support and without any obligation on the
1412795Swpaul * part of Sun Microsystems, Inc. to assist in its use, correction,
1512795Swpaul * modification or enhancement.
16100441Scharnier *
1712795Swpaul * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
1812795Swpaul * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
1912795Swpaul * OR ANY PART THEREOF.
20100441Scharnier *
2112795Swpaul * In no event will Sun Microsystems, Inc. be liable for any lost revenue
2212795Swpaul * or profits or other special, indirect and consequential damages, even if
2312795Swpaul * Sun has been advised of the possibility of such damages.
24100441Scharnier *
2512795Swpaul * Sun Microsystems, Inc.
2612795Swpaul * 2550 Garcia Avenue
2712795Swpaul * Mountain View, California  94043
2812795Swpaul */
2912795Swpaul
30100441Scharnier#if 0
3112795Swpaul#ifndef lint
32146833Sstefanf#ident	"@(#)rpc_tblout.c	1.11	93/07/05 SMI"
3312795Swpaulstatic char sccsid[] = "@(#)rpc_tblout.c 1.4 89/02/22 (C) 1988 SMI";
3412795Swpaul#endif
3527935Scharnier#endif
3612795Swpaul
37100441Scharnier#include <sys/cdefs.h>
38100441Scharnier__FBSDID("$FreeBSD$");
39100441Scharnier
4012795Swpaul/*
4112795Swpaul * rpc_tblout.c, Dispatch table outputter for the RPC protocol compiler
4212795Swpaul * Copyright (C) 1989, Sun Microsystems, Inc.
4312795Swpaul */
4427935Scharnier#include <err.h>
4512795Swpaul#include <stdio.h>
46200462Sdelphij#include <string.h>
4712795Swpaul#include "rpc_parse.h"
48149682Sstefanf#include "rpc_scan.h"
4912795Swpaul#include "rpc_util.h"
5012795Swpaul
5112795Swpaul#define TABSIZE		8
5212795Swpaul#define TABCOUNT	5
5312795Swpaul#define TABSTOP		(TABSIZE*TABCOUNT)
5412795Swpaul
5512795Swpaulstatic char tabstr[TABCOUNT+1] = "\t\t\t\t\t";
5612795Swpaul
5712795Swpaulstatic char tbl_hdr[] = "struct rpcgen_table %s_table[] = {\n";
58141614Salfredstatic char tbl_end[] = "};\n";
5912795Swpaul
60141614Salfredstatic char null_entry[] = "\n\t(char *(*)())0,\n\
6112795Swpaul \t(xdrproc_t) xdr_void,\t\t\t0,\n\
62141614Salfred \t(xdrproc_t) xdr_void,\t\t\t0,\n";
6312795Swpaul
6412795Swpaul
6512795Swpaulstatic char tbl_nproc[] = "int %s_nproc =\n\tsizeof(%s_table)/sizeof(%s_table[0]);\n\n";
6612795Swpaul
6792921Simpstatic void write_table( definition * );
68152398Sdwmalonestatic void printit(const  char *, const char *);
6912795Swpaul
7012795Swpaulvoid
71152398Sdwmalonewrite_tables(void)
7212795Swpaul{
7312795Swpaul	list *l;
7412795Swpaul	definition *def;
7512795Swpaul
7612795Swpaul	f_print(fout, "\n");
7712795Swpaul	for (l = defined; l != NULL; l = l->next) {
7812795Swpaul		def = (definition *) l->val;
7912795Swpaul		if (def->def_kind == DEF_PROGRAM) {
8012795Swpaul			write_table(def);
8112795Swpaul		}
8212795Swpaul	}
8312795Swpaul}
8412795Swpaul
8517142Sjkhstatic void
86152398Sdwmalonewrite_table(definition *def)
8712795Swpaul{
8812795Swpaul	version_list *vp;
8912795Swpaul	proc_list *proc;
9012795Swpaul	int current;
9112795Swpaul	int expected;
9212795Swpaul	char progvers[100];
9312795Swpaul	int warning;
9412795Swpaul
9512795Swpaul	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
9612795Swpaul		warning = 0;
9712795Swpaul		s_print(progvers, "%s_%s",
9812795Swpaul		    locase(def->def_name), vp->vers_num);
9912795Swpaul		/* print the table header */
10012795Swpaul		f_print(fout, tbl_hdr, progvers);
10112795Swpaul
10212795Swpaul		if (nullproc(vp->procs)) {
10312795Swpaul			expected = 0;
10412795Swpaul		} else {
10512795Swpaul			expected = 1;
106222080Sbenl			fputs(null_entry, fout);
10712795Swpaul		}
10812795Swpaul		for (proc = vp->procs; proc != NULL; proc = proc->next) {
10912795Swpaul			current = atoi(proc->proc_num);
11012795Swpaul			if (current != expected++) {
11112795Swpaul				f_print(fout,
11212795Swpaul			"\n/*\n * WARNING: table out of order\n */\n");
11312795Swpaul				if (warning == 0) {
11427935Scharnier					warnx("WARNING %s table is out of order", progvers);
11512795Swpaul					warning = 1;
11612795Swpaul					nonfatalerrors = 1;
11712795Swpaul				}
11812795Swpaul				expected = current + 1;
11912795Swpaul			}
120141614Salfred			f_print(fout, "\n\t(char *(*)())RPCGEN_ACTION(");
12112795Swpaul
12212795Swpaul			/* routine to invoke */
123149709Sstefanf			if( !newstyle )
12412795Swpaul			  pvname_svc(proc->proc_name, vp->vers_num);
12512795Swpaul			else {
12612795Swpaul			  if( newstyle )
12712795Swpaul			    f_print( fout, "_");   /* calls internal func */
12812795Swpaul			  pvname(proc->proc_name, vp->vers_num);
12912795Swpaul			}
13012795Swpaul			f_print(fout, "),\n");
13112795Swpaul
13212795Swpaul			/* argument info */
13312795Swpaul			if( proc->arg_num > 1 )
13412795Swpaul			  printit((char*) NULL, proc->args.argname );
135100441Scharnier			else
13612795Swpaul			  /* do we have to do something special for newstyle */
13712795Swpaul			  printit( proc->args.decls->decl.prefix,
13812795Swpaul				  proc->args.decls->decl.type );
13912795Swpaul			/* result info */
14012795Swpaul			printit(proc->res_prefix, proc->res_type);
14112795Swpaul		}
14212795Swpaul
14312795Swpaul		/* print the table trailer */
144222080Sbenl		fputs(tbl_end, fout);
14512795Swpaul		f_print(fout, tbl_nproc, progvers, progvers, progvers);
14612795Swpaul	}
14712795Swpaul}
14812795Swpaul
14917142Sjkhstatic void
150152398Sdwmaloneprintit(const char *prefix, const char *type)
15112795Swpaul{
15212795Swpaul	int len;
15312795Swpaul	int tabs;
15412795Swpaul
15512795Swpaul
156141614Salfred 	len = fprintf(fout, "\txdr_%s,", stringfix(type));
15712795Swpaul	/* account for leading tab expansion */
15812795Swpaul	len += TABSIZE - 1;
15912795Swpaul	/* round up to tabs required */
16012795Swpaul	tabs = (TABSTOP - len + TABSIZE - 1)/TABSIZE;
16112795Swpaul	f_print(fout, "%s", &tabstr[TABCOUNT-tabs]);
16212795Swpaul
16312795Swpaul	if (streq(type, "void")) {
16412795Swpaul		f_print(fout, "0");
16512795Swpaul	} else {
166141614Salfred		f_print(fout, "sizeof ( ");
16712795Swpaul		/* XXX: should "follow" be 1 ??? */
16812795Swpaul		ptype(prefix, type, 0);
16912795Swpaul		f_print(fout, ")");
17012795Swpaul	}
17112795Swpaul	f_print(fout, ",\n");
17212795Swpaul}
173