rpc_tblout.c revision 146833
168673Sobrien/*
2218822Sdim * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
378828Sobrien * unrestricted use provided that this legend is included on all tape
468673Sobrien * media and as a part of the software program in whole or part.  Users
5218822Sdim * may copy or modify Sun RPC without charge, but are not authorized
668673Sobrien * to license or distribute it to anyone else except as part of a product or
7218822Sdim * program developed by the user.
8218822Sdim *
9218822Sdim * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10218822Sdim * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
1168673Sobrien * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12218822Sdim *
13218822Sdim * Sun RPC is provided with no support and without any obligation on the
14218822Sdim * part of Sun Microsystems, Inc. to assist in its use, correction,
15218822Sdim * modification or enhancement.
1668673Sobrien *
17218822Sdim * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18218822Sdim * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19218822Sdim * OR ANY PART THEREOF.
2068673Sobrien *
2168673Sobrien * In no event will Sun Microsystems, Inc. be liable for any lost revenue
2268673Sobrien * or profits or other special, indirect and consequential damages, even if
2368673Sobrien * Sun has been advised of the possibility of such damages.
2468673Sobrien *
2568673Sobrien * Sun Microsystems, Inc.
2668673Sobrien * 2550 Garcia Avenue
2768673Sobrien * Mountain View, California  94043
2868673Sobrien */
2968673Sobrien
3068673Sobrien#if 0
3168673Sobrien#ifndef lint
3268673Sobrien#ident	"@(#)rpc_tblout.c	1.11	93/07/05 SMI"
3368673Sobrienstatic char sccsid[] = "@(#)rpc_tblout.c 1.4 89/02/22 (C) 1988 SMI";
3468673Sobrien#endif
3568673Sobrien#endif
3668673Sobrien
3768673Sobrien#include <sys/cdefs.h>
3868673Sobrien__FBSDID("$FreeBSD: head/usr.bin/rpcgen/rpc_tblout.c 146833 2005-05-31 20:00:29Z stefanf $");
3968673Sobrien
4068673Sobrien/*
4168673Sobrien * rpc_tblout.c, Dispatch table outputter for the RPC protocol compiler
4268673Sobrien * Copyright (C) 1989, Sun Microsystems, Inc.
4368673Sobrien */
4468673Sobrien#include <err.h>
4568673Sobrien#include <stdio.h>
4668673Sobrien#include <string.h>
4768673Sobrien#include "rpc_parse.h"
4868673Sobrien#include "rpc_util.h"
4968673Sobrien
5068673Sobrien#define TABSIZE		8
5168673Sobrien#define TABCOUNT	5
5268673Sobrien#define TABSTOP		(TABSIZE*TABCOUNT)
5368673Sobrien
5468673Sobrienstatic char tabstr[TABCOUNT+1] = "\t\t\t\t\t";
5568673Sobrien
5668673Sobrienstatic char tbl_hdr[] = "struct rpcgen_table %s_table[] = {\n";
5768673Sobrienstatic char tbl_end[] = "};\n";
5868673Sobrien
5968673Sobrienstatic char null_entry[] = "\n\t(char *(*)())0,\n\
6068673Sobrien \t(xdrproc_t) xdr_void,\t\t\t0,\n\
6168673Sobrien \t(xdrproc_t) xdr_void,\t\t\t0,\n";
6268673Sobrien
6368673Sobrien
6468673Sobrienstatic char tbl_nproc[] = "int %s_nproc =\n\tsizeof(%s_table)/sizeof(%s_table[0]);\n\n";
6568673Sobrien
6668673Sobrienextern int nullproc( proc_list * );
6768673Sobrienstatic void write_table( definition * );
6868673Sobrienstatic void printit( char *, char * );
6968673Sobrien
7068673Sobrienvoid
7168673Sobrienwrite_tables()
7268673Sobrien{
7368673Sobrien	list *l;
7468673Sobrien	definition *def;
7568673Sobrien
7668673Sobrien	f_print(fout, "\n");
77218822Sdim	for (l = defined; l != NULL; l = l->next) {
78218822Sdim		def = (definition *) l->val;
79218822Sdim		if (def->def_kind == DEF_PROGRAM) {
8068673Sobrien			write_table(def);
8168673Sobrien		}
8268673Sobrien	}
83218822Sdim}
8468673Sobrien
8568673Sobrienstatic void
86218822Sdimwrite_table(def)
8768673Sobrien	definition *def;
8868673Sobrien{
8968673Sobrien	version_list *vp;
9068673Sobrien	proc_list *proc;
9168673Sobrien	int current;
9268673Sobrien	int expected;
9368673Sobrien	char progvers[100];
9468673Sobrien	int warning;
9568673Sobrien
9668673Sobrien	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
9768673Sobrien		warning = 0;
9868673Sobrien		s_print(progvers, "%s_%s",
99218822Sdim		    locase(def->def_name), vp->vers_num);
100218822Sdim		/* print the table header */
10168673Sobrien		f_print(fout, tbl_hdr, progvers);
10268673Sobrien
10368673Sobrien		if (nullproc(vp->procs)) {
10468673Sobrien			expected = 0;
10568673Sobrien		} else {
10668673Sobrien			expected = 1;
10768673Sobrien			f_print(fout, null_entry);
10868673Sobrien		}
10968673Sobrien		for (proc = vp->procs; proc != NULL; proc = proc->next) {
11068673Sobrien			current = atoi(proc->proc_num);
11177298Sobrien			if (current != expected++) {
11268673Sobrien				f_print(fout,
11377298Sobrien			"\n/*\n * WARNING: table out of order\n */\n");
11468673Sobrien				if (warning == 0) {
11577298Sobrien					warnx("WARNING %s table is out of order", progvers);
11677298Sobrien					warning = 1;
11768673Sobrien					nonfatalerrors = 1;
11877298Sobrien				}
11977298Sobrien				expected = current + 1;
12068673Sobrien			}
12177298Sobrien			f_print(fout, "\n\t(char *(*)())RPCGEN_ACTION(");
12277298Sobrien
12368673Sobrien			/* routine to invoke */
12477298Sobrien			if( Cflag && !newstyle )
12577298Sobrien			  pvname_svc(proc->proc_name, vp->vers_num);
12668673Sobrien			else {
12777298Sobrien			  if( newstyle )
12877298Sobrien			    f_print( fout, "_");   /* calls internal func */
12968673Sobrien			  pvname(proc->proc_name, vp->vers_num);
13077298Sobrien			}
13177298Sobrien			f_print(fout, "),\n");
13268673Sobrien
13377298Sobrien			/* argument info */
13477298Sobrien			if( proc->arg_num > 1 )
13577298Sobrien			  printit((char*) NULL, proc->args.argname );
13677298Sobrien			else
13768673Sobrien			  /* do we have to do something special for newstyle */
13877298Sobrien			  printit( proc->args.decls->decl.prefix,
13977298Sobrien				  proc->args.decls->decl.type );
14077298Sobrien			/* result info */
14177298Sobrien			printit(proc->res_prefix, proc->res_type);
14268673Sobrien		}
14377298Sobrien
14477298Sobrien		/* print the table trailer */
14577298Sobrien		f_print(fout, tbl_end);
14668673Sobrien		f_print(fout, tbl_nproc, progvers, progvers, progvers);
14777298Sobrien	}
14877298Sobrien}
14968673Sobrien
15077298Sobrienstatic void
15177298Sobrienprintit(prefix, type)
15268673Sobrien	char *prefix;
15377298Sobrien	char *type;
15477298Sobrien{
15568673Sobrien	int len;
15677298Sobrien	int tabs;
15777298Sobrien
15868673Sobrien
15977298Sobrien 	len = fprintf(fout, "\txdr_%s,", stringfix(type));
16077298Sobrien	/* account for leading tab expansion */
16168673Sobrien	len += TABSIZE - 1;
16277298Sobrien	/* round up to tabs required */
16377298Sobrien	tabs = (TABSTOP - len + TABSIZE - 1)/TABSIZE;
16468673Sobrien	f_print(fout, "%s", &tabstr[TABCOUNT-tabs]);
16577298Sobrien
16677298Sobrien	if (streq(type, "void")) {
16768673Sobrien		f_print(fout, "0");
16877298Sobrien	} else {
16977298Sobrien		f_print(fout, "sizeof ( ");
17068673Sobrien		/* XXX: should "follow" be 1 ??? */
17168673Sobrien		ptype(prefix, type, 0);
17277298Sobrien		f_print(fout, ")");
17377298Sobrien	}
17477298Sobrien	f_print(fout, ",\n");
17568673Sobrien}
17677298Sobrien