xref: /onnv-gate/usr/src/cmd/adbgen/common/adbsub.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
23*0Sstevel@tonic-gate 
24*0Sstevel@tonic-gate /*
25*0Sstevel@tonic-gate  * Copyright (c) 1983-1998 by Sun Microsystems, Inc.
26*0Sstevel@tonic-gate  * All rights reserved.
27*0Sstevel@tonic-gate  */
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * Subroutines to be called by adbgen2.c, the C program generated
31*0Sstevel@tonic-gate  * by adbgen1.c.
32*0Sstevel@tonic-gate  */
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #include <stdio.h>
35*0Sstevel@tonic-gate #include <stdlib.h>
36*0Sstevel@tonic-gate #include <sys/types.h>
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate off_t last_off;
39*0Sstevel@tonic-gate int warnings = 1;
40*0Sstevel@tonic-gate int warns = 0;
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate /*
43*0Sstevel@tonic-gate  * User claims offset is ok.
44*0Sstevel@tonic-gate  * This usually follows call to another script, which we cannot handle.
45*0Sstevel@tonic-gate  */
46*0Sstevel@tonic-gate void
offsetok(void)47*0Sstevel@tonic-gate offsetok(void)
48*0Sstevel@tonic-gate {
49*0Sstevel@tonic-gate 	last_off = -1;
50*0Sstevel@tonic-gate }
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate /*
53*0Sstevel@tonic-gate  * Get adb.s dot to the right offset.
54*0Sstevel@tonic-gate  */
55*0Sstevel@tonic-gate void
offset(off_t off)56*0Sstevel@tonic-gate offset(off_t off)
57*0Sstevel@tonic-gate {
58*0Sstevel@tonic-gate 	off_t off_diff;
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate 	if (last_off == -1) {
61*0Sstevel@tonic-gate 		last_off = off;
62*0Sstevel@tonic-gate 		return;
63*0Sstevel@tonic-gate 	}
64*0Sstevel@tonic-gate 	off_diff = off - last_off;
65*0Sstevel@tonic-gate 	if (off_diff) {
66*0Sstevel@tonic-gate 		if (off_diff > 0) {
67*0Sstevel@tonic-gate 			if (off_diff > 1) {
68*0Sstevel@tonic-gate 				printf("%ld", off_diff);
69*0Sstevel@tonic-gate 			}
70*0Sstevel@tonic-gate 			printf("+");
71*0Sstevel@tonic-gate 		}
72*0Sstevel@tonic-gate 		if (off_diff < 0) {
73*0Sstevel@tonic-gate 			if (off_diff < -1) {
74*0Sstevel@tonic-gate 				printf("%ld", -off_diff);
75*0Sstevel@tonic-gate 			}
76*0Sstevel@tonic-gate 			printf("-");
77*0Sstevel@tonic-gate 		}
78*0Sstevel@tonic-gate 	}
79*0Sstevel@tonic-gate 	last_off = off;
80*0Sstevel@tonic-gate }
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate /*
83*0Sstevel@tonic-gate  * Emit the format command, return the size.
84*0Sstevel@tonic-gate  */
85*0Sstevel@tonic-gate int
do_fmt(char * acp)86*0Sstevel@tonic-gate do_fmt(char *acp)
87*0Sstevel@tonic-gate {
88*0Sstevel@tonic-gate 	int rcount, width, sum, i;
89*0Sstevel@tonic-gate 	char *cp;
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 	cp = acp;
92*0Sstevel@tonic-gate 	sum = rcount = 0;
93*0Sstevel@tonic-gate 	do {
94*0Sstevel@tonic-gate 		while (*cp >= '0' && *cp <= '9') {
95*0Sstevel@tonic-gate 			rcount = rcount * 10 + *cp++ - '0';
96*0Sstevel@tonic-gate 		}
97*0Sstevel@tonic-gate 		if (rcount == 0) {
98*0Sstevel@tonic-gate 			rcount = 1;
99*0Sstevel@tonic-gate 		}
100*0Sstevel@tonic-gate 		switch (*cp) {
101*0Sstevel@tonic-gate 		case 'e':
102*0Sstevel@tonic-gate 		case 'E':
103*0Sstevel@tonic-gate 		case 'F':
104*0Sstevel@tonic-gate 		case 'g':
105*0Sstevel@tonic-gate 		case 'G':
106*0Sstevel@tonic-gate 		case 'J':
107*0Sstevel@tonic-gate 			width = 8;
108*0Sstevel@tonic-gate 			break;
109*0Sstevel@tonic-gate 		case 'K':
110*0Sstevel@tonic-gate #ifdef	_LP64
111*0Sstevel@tonic-gate 			width = 8;
112*0Sstevel@tonic-gate #else	/* _LP64 */
113*0Sstevel@tonic-gate 			width = 4;
114*0Sstevel@tonic-gate #endif	/* _LP64 */
115*0Sstevel@tonic-gate 			break;
116*0Sstevel@tonic-gate 		case 'X':
117*0Sstevel@tonic-gate 		case 'O':
118*0Sstevel@tonic-gate 		case 'Q':
119*0Sstevel@tonic-gate 		case 'D':
120*0Sstevel@tonic-gate 		case 'U':
121*0Sstevel@tonic-gate 		case 'f':
122*0Sstevel@tonic-gate 		case 'Y':
123*0Sstevel@tonic-gate 		case 'p':
124*0Sstevel@tonic-gate 		case 'P':
125*0Sstevel@tonic-gate 			width = 4;
126*0Sstevel@tonic-gate 			break;
127*0Sstevel@tonic-gate 		case 'x':
128*0Sstevel@tonic-gate 		case 'o':
129*0Sstevel@tonic-gate 		case 'q':
130*0Sstevel@tonic-gate 		case 'd':
131*0Sstevel@tonic-gate 		case 'u':
132*0Sstevel@tonic-gate 			width = 2;
133*0Sstevel@tonic-gate 			break;
134*0Sstevel@tonic-gate 		case 'v':
135*0Sstevel@tonic-gate 		case 'V':
136*0Sstevel@tonic-gate 		case 'b':
137*0Sstevel@tonic-gate 		case 'B':
138*0Sstevel@tonic-gate 		case 'c':
139*0Sstevel@tonic-gate 		case 'C':
140*0Sstevel@tonic-gate 		case '+':
141*0Sstevel@tonic-gate 			width = 1;
142*0Sstevel@tonic-gate 			break;
143*0Sstevel@tonic-gate 		case 'I':
144*0Sstevel@tonic-gate 		case 'a':
145*0Sstevel@tonic-gate 		case 'A':
146*0Sstevel@tonic-gate 		case 't':
147*0Sstevel@tonic-gate 		case 'r':
148*0Sstevel@tonic-gate 		case 'n':
149*0Sstevel@tonic-gate 			width = 0;
150*0Sstevel@tonic-gate 			break;
151*0Sstevel@tonic-gate 		case '-':
152*0Sstevel@tonic-gate 			width = -1;
153*0Sstevel@tonic-gate 			break;
154*0Sstevel@tonic-gate 		case 's':
155*0Sstevel@tonic-gate 		case 'S':
156*0Sstevel@tonic-gate 		case 'i':
157*0Sstevel@tonic-gate 			if (warnings) {
158*0Sstevel@tonic-gate 				fprintf(stderr,
159*0Sstevel@tonic-gate 				"Unknown format size \"%s\", assuming zero\n",
160*0Sstevel@tonic-gate 				acp);
161*0Sstevel@tonic-gate 				warns++;
162*0Sstevel@tonic-gate 			}
163*0Sstevel@tonic-gate 			width = 0;
164*0Sstevel@tonic-gate 			break;
165*0Sstevel@tonic-gate 		default:
166*0Sstevel@tonic-gate 			fprintf(stderr, "Unknown format size: %s\n", acp);
167*0Sstevel@tonic-gate 			exit(1);
168*0Sstevel@tonic-gate 		}
169*0Sstevel@tonic-gate 		for (i = 0; i < rcount; i++) {
170*0Sstevel@tonic-gate 			putchar(*cp);
171*0Sstevel@tonic-gate 		}
172*0Sstevel@tonic-gate 		cp++;
173*0Sstevel@tonic-gate 		sum += width * rcount;
174*0Sstevel@tonic-gate 	} while (*cp);
175*0Sstevel@tonic-gate 	return (sum);
176*0Sstevel@tonic-gate }
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate /*
179*0Sstevel@tonic-gate  * Format struct member, checking size.
180*0Sstevel@tonic-gate  */
181*0Sstevel@tonic-gate void
format(char * name,size_t size,char * fmt)182*0Sstevel@tonic-gate format(char *name, size_t size, char *fmt)
183*0Sstevel@tonic-gate {
184*0Sstevel@tonic-gate 	int fs;
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate 	fs = do_fmt(fmt);
187*0Sstevel@tonic-gate 	if (fs != size && warnings) {
188*0Sstevel@tonic-gate 		fprintf(stderr,
189*0Sstevel@tonic-gate 			"warning: \"%s\" size is %ld, \"%s\" width is %d\n",
190*0Sstevel@tonic-gate 			name, size, fmt, fs);
191*0Sstevel@tonic-gate 		warns++;
192*0Sstevel@tonic-gate 	}
193*0Sstevel@tonic-gate 	last_off += fs;
194*0Sstevel@tonic-gate }
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate /*
197*0Sstevel@tonic-gate  * Get the value at offset based on base.
198*0Sstevel@tonic-gate  */
199*0Sstevel@tonic-gate void
indirect(off_t offset,size_t size,char * base,char * member)200*0Sstevel@tonic-gate indirect(off_t offset, size_t size, char *base, char *member)
201*0Sstevel@tonic-gate {
202*0Sstevel@tonic-gate 	if (size == 8 || size == 4) {
203*0Sstevel@tonic-gate 		if (offset == 0) {
204*0Sstevel@tonic-gate 			printf("*%s", base);
205*0Sstevel@tonic-gate 		} else {
206*0Sstevel@tonic-gate 			printf("*(%s+0t%ld)", base, offset);
207*0Sstevel@tonic-gate 		}
208*0Sstevel@tonic-gate 	} else if (size == 2) {
209*0Sstevel@tonic-gate 		if (offset == 2) {
210*0Sstevel@tonic-gate 			printf("(*%s&0xffff)", base);
211*0Sstevel@tonic-gate 		} else {
212*0Sstevel@tonic-gate 			printf("(*(%s+0t%ld)&0xffff)", base, offset - 2);
213*0Sstevel@tonic-gate 		}
214*0Sstevel@tonic-gate 	} else if (size == 1) {
215*0Sstevel@tonic-gate 		if (offset == 3) {
216*0Sstevel@tonic-gate 			printf("(*%s&0xff)", base);
217*0Sstevel@tonic-gate 		} else {
218*0Sstevel@tonic-gate 			if ((offset & 0x1) == 0x1) {
219*0Sstevel@tonic-gate 				printf("(*(%s+0t%ld)&0xff)", base, offset - 3);
220*0Sstevel@tonic-gate 			} else {
221*0Sstevel@tonic-gate 				printf("((*(%s+0t%ld)&0xff00)/0x100)",
222*0Sstevel@tonic-gate 				    base, offset - 2);
223*0Sstevel@tonic-gate 			}
224*0Sstevel@tonic-gate 		}
225*0Sstevel@tonic-gate 	} else {
226*0Sstevel@tonic-gate 		fprintf(stderr, "Indirect size %ld not 1, 2, or 4: %s\n",
227*0Sstevel@tonic-gate 		    size, member);
228*0Sstevel@tonic-gate 		exit(1);
229*0Sstevel@tonic-gate 	}
230*0Sstevel@tonic-gate }
231