xref: /titanic_53/usr/src/tools/ndrgen/ndr_print.c (revision d0e518695adc90b82233b99af7dffbb3d3f92c00)
1*d0e51869Samw /*
2*d0e51869Samw  * CDDL HEADER START
3*d0e51869Samw  *
4*d0e51869Samw  * The contents of this file are subject to the terms of the
5*d0e51869Samw  * Common Development and Distribution License (the "License").
6*d0e51869Samw  * You may not use this file except in compliance with the License.
7*d0e51869Samw  *
8*d0e51869Samw  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*d0e51869Samw  * or http://www.opensolaris.org/os/licensing.
10*d0e51869Samw  * See the License for the specific language governing permissions
11*d0e51869Samw  * and limitations under the License.
12*d0e51869Samw  *
13*d0e51869Samw  * When distributing Covered Code, include this CDDL HEADER in each
14*d0e51869Samw  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*d0e51869Samw  * If applicable, add the following below this CDDL HEADER, with the
16*d0e51869Samw  * fields enclosed by brackets "[]" replaced with your own identifying
17*d0e51869Samw  * information: Portions Copyright [yyyy] [name of copyright owner]
18*d0e51869Samw  *
19*d0e51869Samw  * CDDL HEADER END
20*d0e51869Samw  */
21*d0e51869Samw 
22*d0e51869Samw /*
23*d0e51869Samw  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24*d0e51869Samw  * Use is subject to license terms.
25*d0e51869Samw  */
26*d0e51869Samw 
27*d0e51869Samw #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*d0e51869Samw 
29*d0e51869Samw #include "ndrgen.h"
30*d0e51869Samw #include "y.tab.h"
31*d0e51869Samw 
32*d0e51869Samw 
33*d0e51869Samw static void print_declaration(ndr_node_t *);
34*d0e51869Samw static void print_advice_list(ndr_node_t *);
35*d0e51869Samw static void print_node_list(ndr_node_t *);
36*d0e51869Samw 
37*d0e51869Samw 
38*d0e51869Samw void
39*d0e51869Samw tdata_dump(void)
40*d0e51869Samw {
41*d0e51869Samw 	print_node_list(construct_list);
42*d0e51869Samw }
43*d0e51869Samw 
44*d0e51869Samw void
45*d0e51869Samw print_node(ndr_node_t *np)
46*d0e51869Samw {
47*d0e51869Samw 	char		*nm;
48*d0e51869Samw 
49*d0e51869Samw 	if (!np) {
50*d0e51869Samw 		(void) printf("<null>");
51*d0e51869Samw 		return;
52*d0e51869Samw 	}
53*d0e51869Samw 
54*d0e51869Samw 	switch (np->label) {
55*d0e51869Samw 	case ALIGN_KW:		nm = "align";		break;
56*d0e51869Samw 	case STRUCT_KW:		nm = "struct";		break;
57*d0e51869Samw 	case UNION_KW:		nm = "union";		break;
58*d0e51869Samw 	case TYPEDEF_KW:	nm = "typedef";		break;
59*d0e51869Samw 	case INTERFACE_KW:	nm = "interface";	break;
60*d0e51869Samw 	case IN_KW:		nm = "in";		break;
61*d0e51869Samw 	case OUT_KW:		nm = "out";		break;
62*d0e51869Samw 	case SIZE_IS_KW:	nm = "size_is";		break;
63*d0e51869Samw 	case LENGTH_IS_KW:	nm = "length_is";	break;
64*d0e51869Samw 	case STRING_KW:		nm = "string";		break;
65*d0e51869Samw 	case TRANSMIT_AS_KW:	nm = "transmit_as";	break;
66*d0e51869Samw 	case OPERATION_KW:	nm = "operation";	break;
67*d0e51869Samw 	case UUID_KW:		nm = "uuid";		break;
68*d0e51869Samw 	case _NO_REORDER_KW:	nm = "_no_reorder";	break;
69*d0e51869Samw 	case EXTERN_KW:		nm = "extern";		break;
70*d0e51869Samw 	case ARG_IS_KW:		nm = "arg_is";		break;
71*d0e51869Samw 	case CASE_KW:		nm = "case";		break;
72*d0e51869Samw 	case DEFAULT_KW:	nm = "default";		break;
73*d0e51869Samw 	case BASIC_TYPE:	nm = "<btype>";		break;
74*d0e51869Samw 	case TYPENAME:		nm = "<tname>";		break;
75*d0e51869Samw 	case IDENTIFIER:	nm = "<ident>";		break;
76*d0e51869Samw 	case INTEGER:		nm = "<intg>";		break;
77*d0e51869Samw 	case STRING:		nm = "<string>";	break;
78*d0e51869Samw 	case STAR:		nm = "<*>";		break;
79*d0e51869Samw 	case LB:		nm = "<[>";		break;
80*d0e51869Samw 	case LP:		nm = "<(>";		break;
81*d0e51869Samw 	case L_MEMBER:		nm = "<member>";	break;
82*d0e51869Samw 	default:
83*d0e51869Samw 		(void) printf("<<lab=%d>>", np->label);
84*d0e51869Samw 		return;
85*d0e51869Samw 	}
86*d0e51869Samw 
87*d0e51869Samw 	switch (np->label) {
88*d0e51869Samw 	case STRUCT_KW:
89*d0e51869Samw 	case UNION_KW:
90*d0e51869Samw 	case TYPEDEF_KW:
91*d0e51869Samw 		(void) printf("\n");
92*d0e51869Samw 		if (np->n_c_advice) {
93*d0e51869Samw 			print_advice_list(np->n_c_advice);
94*d0e51869Samw 			(void) printf("\n");
95*d0e51869Samw 		}
96*d0e51869Samw 		(void) printf("%s ", nm);
97*d0e51869Samw 		print_node(np->n_c_typename);
98*d0e51869Samw 		(void) printf(" {\n");
99*d0e51869Samw 		print_node_list(np->n_c_members);
100*d0e51869Samw 		(void) printf("};\n");
101*d0e51869Samw 		break;
102*d0e51869Samw 
103*d0e51869Samw 	case IN_KW:
104*d0e51869Samw 	case OUT_KW:
105*d0e51869Samw 	case STRING_KW:
106*d0e51869Samw 	case DEFAULT_KW:
107*d0e51869Samw 	case _NO_REORDER_KW:
108*d0e51869Samw 	case EXTERN_KW:
109*d0e51869Samw 		(void) printf("%s", nm);
110*d0e51869Samw 		break;
111*d0e51869Samw 
112*d0e51869Samw 	case ALIGN_KW:
113*d0e51869Samw 		/*
114*d0e51869Samw 		 * Don't output anything for default alignment.
115*d0e51869Samw 		 */
116*d0e51869Samw 		if ((np->n_a_arg == NULL) || (np->n_a_arg->n_int == 0))
117*d0e51869Samw 			break;
118*d0e51869Samw 		(void) printf("%s(", nm);
119*d0e51869Samw 		print_node(np->n_a_arg);
120*d0e51869Samw 		(void) printf(")");
121*d0e51869Samw 		break;
122*d0e51869Samw 
123*d0e51869Samw 	case INTERFACE_KW:
124*d0e51869Samw 	case SIZE_IS_KW:
125*d0e51869Samw 	case LENGTH_IS_KW:
126*d0e51869Samw 	case TRANSMIT_AS_KW:
127*d0e51869Samw 	case ARG_IS_KW:
128*d0e51869Samw 	case CASE_KW:
129*d0e51869Samw 	case OPERATION_KW:
130*d0e51869Samw 	case UUID_KW:
131*d0e51869Samw 		(void) printf("%s(", nm);
132*d0e51869Samw 		print_node(np->n_a_arg);
133*d0e51869Samw 		(void) printf(")");
134*d0e51869Samw 		break;
135*d0e51869Samw 
136*d0e51869Samw 	case BASIC_TYPE:
137*d0e51869Samw 	case TYPENAME:
138*d0e51869Samw 	case IDENTIFIER:
139*d0e51869Samw 		(void) printf("%s", np->n_sym->name);
140*d0e51869Samw 		break;
141*d0e51869Samw 
142*d0e51869Samw 	case INTEGER:
143*d0e51869Samw 		(void) printf("%ld", np->n_int);
144*d0e51869Samw 		break;
145*d0e51869Samw 
146*d0e51869Samw 	case STRING:
147*d0e51869Samw 		(void) printf("\"%s\"", np->n_str);
148*d0e51869Samw 		break;
149*d0e51869Samw 
150*d0e51869Samw 	case STAR:
151*d0e51869Samw 		(void) printf("*");
152*d0e51869Samw 		print_node(np->n_d_descend);
153*d0e51869Samw 		break;
154*d0e51869Samw 
155*d0e51869Samw 	case LB:
156*d0e51869Samw 		print_node(np->n_d_descend);
157*d0e51869Samw 		(void) printf("[");
158*d0e51869Samw 		if (np->n_d_dim)
159*d0e51869Samw 			print_node(np->n_d_dim);
160*d0e51869Samw 		(void) printf("]");
161*d0e51869Samw 		break;
162*d0e51869Samw 
163*d0e51869Samw 	case LP:
164*d0e51869Samw 		(void) printf("(");
165*d0e51869Samw 		print_node(np->n_d_descend);
166*d0e51869Samw 		(void) printf(")");
167*d0e51869Samw 		break;
168*d0e51869Samw 
169*d0e51869Samw 	case L_MEMBER:
170*d0e51869Samw 		if (np->n_m_advice) {
171*d0e51869Samw 			(void) printf("    ");
172*d0e51869Samw 			print_advice_list(np->n_m_advice);
173*d0e51869Samw 			(void) printf("\n");
174*d0e51869Samw 		}
175*d0e51869Samw 		(void) printf("\t");
176*d0e51869Samw 		print_declaration(np);
177*d0e51869Samw 		(void) printf(";\n");
178*d0e51869Samw 		break;
179*d0e51869Samw 
180*d0e51869Samw 	default:
181*d0e51869Samw 		return;
182*d0e51869Samw 	}
183*d0e51869Samw }
184*d0e51869Samw 
185*d0e51869Samw static void
186*d0e51869Samw print_declaration(ndr_node_t *np)
187*d0e51869Samw {
188*d0e51869Samw 	ndr_node_t	*dnp = np->n_m_decl;
189*d0e51869Samw 	char		buf[NDLBUFSZ];
190*d0e51869Samw 	char		*p = buf;
191*d0e51869Samw 
192*d0e51869Samw 	if (np->n_m_type &&
193*d0e51869Samw 	    (np->n_m_type->label == IDENTIFIER ||
194*d0e51869Samw 	    np->n_m_type->label == TYPENAME)) {
195*d0e51869Samw 		(void) snprintf(buf, NDLBUFSZ, "%s", np->n_m_type->n_sym->name);
196*d0e51869Samw 
197*d0e51869Samw 		while (*p)
198*d0e51869Samw 			p++;
199*d0e51869Samw 
200*d0e51869Samw 		if (dnp && dnp->label == STAR) {
201*d0e51869Samw 			*p++ = ' ';
202*d0e51869Samw 			while (dnp && dnp->label == STAR) {
203*d0e51869Samw 				*p++ = '*';
204*d0e51869Samw 				dnp = dnp->n_d_descend;
205*d0e51869Samw 			}
206*d0e51869Samw 		}
207*d0e51869Samw 		*p = 0;
208*d0e51869Samw 		(void) printf("%-23s ", buf);
209*d0e51869Samw 	} else {
210*d0e51869Samw 		print_node(np->n_m_type);
211*d0e51869Samw 		(void) printf(" ");
212*d0e51869Samw 	}
213*d0e51869Samw 
214*d0e51869Samw 	print_node(dnp);
215*d0e51869Samw }
216*d0e51869Samw 
217*d0e51869Samw static void
218*d0e51869Samw print_advice_list(ndr_node_t *np)
219*d0e51869Samw {
220*d0e51869Samw 	if (!np)
221*d0e51869Samw 		return;
222*d0e51869Samw 
223*d0e51869Samw 	(void) printf("[");
224*d0e51869Samw 	for (; np; np = np->n_next) {
225*d0e51869Samw 		print_node(np);
226*d0e51869Samw 		if (np->n_next)
227*d0e51869Samw 			(void) printf(" ");
228*d0e51869Samw 	}
229*d0e51869Samw 	(void) printf("]");
230*d0e51869Samw }
231*d0e51869Samw 
232*d0e51869Samw static void
233*d0e51869Samw print_node_list(ndr_node_t *np)
234*d0e51869Samw {
235*d0e51869Samw 	for (; np; np = np->n_next) {
236*d0e51869Samw 		print_node(np);
237*d0e51869Samw 	}
238*d0e51869Samw }
239