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