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