|
root / base / usr / src / cmd / adbgen / common
common Plain Text 1162 lines 23.9 KB
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
#! /bin/sh
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (the "License").  You may not use this file except in compliance
# with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright (c) 1998 by Sun Microsystems, Inc.
# All rights reserved.
#
# ident	"%Z%%M%	%I%	%E% SMI"
#
USAGE="adbgen [-d] [-m ilp32|lp64] [-w] <adb macro file>"
cflags=
mflag=-milp32
subdir=

while getopts dwm: c
do
	case $c in
	d)
		DEBUG=:
		;;
	m)
		case $OPTARG in
		ilp32)
			mflag=-milp32
			;;
		lp64)
			mflag=-mlp64
			cflags=-xarch=v9
			subdir=sparcv9
			/usr/bin/optisa sparcv9 > /dev/null
			if [ $? -ne 0 ]
			then
				echo adbgen -mlp64 must be run on 64-bit system
			fi
			;;
		*)
			echo $USAGE
			exit 2
			;;
		esac
		;;
	w)
		flag=-w
		;;
	\?)
		echo $USAGE
		exit 2
		;;
        esac
done
shift `expr $OPTIND - 1`

ADBDIR=/usr/lib/adb
PATH=$PATH:$ADBDIR
for file in $*
do
	if [ `expr "XX$file" : ".*\.adb"` -eq 0 ]
	then
		echo File $file invalid.
		exit 1
	fi
	if [ $# -gt 1 ]
	then
		echo $file:
	fi
	file=`expr "XX$file" : "XX\(.*\)\.adb"`
	if adbgen1 $flag $mflag < $file.adb > $file.adb.c
	then
		if ${CC:-cc} -w -D${ARCH:-`uname -m`} $cflags \
			-I/usr/share/src/uts/${ARCH:-`uname -m`} \
			-o $file.run $file.adb.c $ADBDIR/$subdir/adbsub.o
		then
			$file.run | adbgen3 | adbgen4 > $file
			$DEBUG rm -f $file.run $file.adb.C $file.adb.c $file.adb.o
		else
			$DEBUG rm -f $file.run $file.adb.C $file.adb.c $file.adb.o
			echo compile failed
			exit 1
		fi
	else
		$DEBUG rm -f $file.adb.C
		echo adbgen1 failed
		exit 1
	fi
done
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * Read in "high-level" adb script and emit C program.
 * The input may have specifications within {} which
 * we analyze and then emit C code to generate the
 * ultimate adb acript.
 * We are just a filter; no arguments are accepted.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define	streq(s1, s2)	(strcmp(s1, s2) == 0)

#define	LINELEN  	1024	/* max line length expected in input */
#define	STRLEN		128	/* for shorter strings */
#define	NARGS		5	/* number of emitted subroutine arguments */

/*
 * Format specifier strings
 * which are recognized by adbgen when surrounded by {}
 */
#define	FSTR_PTR	"POINTER"
#define	FSTR_LONG_DEC	"LONGDEC"
#define	FSTR_LONG_OCT	"LONGOCT"
#define	FSTR_ULONG_DEC	"ULONGDEC"
#define	FSTR_ULONG_HEX	"ULONGHEX"
#define	FSTR_ULONG_OCT	"ULONGOCT"

/*
 * Types of specifications in {}.
 */
#define	PTR_HEX		0	/* emit hex pointer format char */
#define	LONG_DEC	1	/* emit decimal long format char */
#define	LONG_OCT	2	/* emit octal unsigned long format char */
#define	ULONG_DEC	3	/* emit decimal unsigned long format char */
#define	ULONG_HEX	4	/* emit hexadecimal long format char */
#define	ULONG_OCT	5	/* emit octal unsigned long format char */

#define	FMT_ENTRIES	6	/* number of adbgen format specifier strings */

#define	PRINT   	6	/* print member name with format */
#define	INDIRECT	7	/* fetch member value */
#define	OFFSETOK	8	/* insist that the offset is ok */
#define	SIZEOF		9	/* print sizeof struct */
#define	END		10	/* get offset to end of struct */
#define	OFFSET		11	/* just emit offset */
#define	EXPR		12	/* arbitrary C expression */

/*
 * Special return code from nextchar.
 */
#define	CPP		-2	/* cpp line, restart parsing */

typedef struct adbgen_fmt {
	char *f_str;
	char f_char;
} adbgen_fmt_t;

char struct_name[STRLEN];	/* struct name */
char member[STRLEN];		/* member name */
char format[STRLEN];		/* adb format spec */
char arg[NARGS][STRLEN];	/* arg list for called subroutine */
char *ptr_hex_fmt;		/* adb format character for pointer in hex */
char *long_dec_fmt;		/* adb format character for long in decimal */
char *ulong_dec_fmt;		/* adb format character for ulong in decimal */
char *ulong_hex_fmt;		/* adb format character for ulong in hex */
char *long_oct_fmt;		/* adb format character for long in octal */
char *ulong_oct_fmt;		/* adb format character for ulong in octal */

int line_no = 1;		/* input line number - for error messages */
int specsize;			/* size of {} specification - 1 or 2 parts */
int state;			/* XXX 1 = gathering a printf */
				/* This is a kludge so we emit pending */
				/* printf's when we see a CPP line */

adbgen_fmt_t adbgen_fmt_tbl [FMT_ENTRIES] = {
	{FSTR_PTR},
	{FSTR_LONG_DEC},
	{FSTR_LONG_OCT},
	{FSTR_ULONG_DEC},
	{FSTR_ULONG_HEX},
	{FSTR_ULONG_OCT}
};

void emit_call(char *name, int nargs);
void emit_end(void);
void emit_expr(void);
void emit_indirect(void);
void emit_offset(void);
void emit_offsetok(void);
void emit_print(void);
void emit_printf(char *cp);
void emit_sizeof(void);
void generate(void);
int get_type(void);
int nextchar(char *cp);
void read_spec(void);
char *start_printf(void);

int
main(int argc, char **argv)
{
	char *cp;
	int c;
	int warn_flag = 0;
	int is_lp64 = 0;
	char *usage = "adbgen1 [-w] [-m ilp32|lp64] < <macro file>\n";

	while ((c = getopt(argc, argv, "m:w")) != EOF) {
		switch (c) {
		case 'm':
			if (streq(optarg, "ilp32"))
				is_lp64 = 0;
			else if (streq(optarg, "lp64"))
				is_lp64 = 1;
			else
				fprintf(stderr, usage);
			break;
		case 'w':
			warn_flag++;
			break;
		case '?':
			fprintf(stderr, usage);
			break;
		}
	}
	if (is_lp64) {
		adbgen_fmt_tbl[PTR_HEX].f_char = 'J';
		adbgen_fmt_tbl[LONG_DEC].f_char = 'e';
		adbgen_fmt_tbl[LONG_OCT].f_char = 'g';
		adbgen_fmt_tbl[ULONG_DEC].f_char = 'E';
		adbgen_fmt_tbl[ULONG_HEX].f_char = 'J';
		adbgen_fmt_tbl[ULONG_OCT].f_char = 'G';
	} else {
		adbgen_fmt_tbl[PTR_HEX].f_char = 'X';
		adbgen_fmt_tbl[LONG_DEC].f_char = 'D';
		adbgen_fmt_tbl[LONG_OCT].f_char = 'Q';
		adbgen_fmt_tbl[ULONG_DEC].f_char = 'U';
		adbgen_fmt_tbl[ULONG_HEX].f_char = 'X';
		adbgen_fmt_tbl[ULONG_OCT].f_char = 'O';
	}

	/*
	 * Get structure name.
	 */
	cp = struct_name;
	while ((c = nextchar(NULL)) != '\n') {
		if (c == EOF) {
			fprintf(stderr, "Premature EOF\n");
			exit(1);
		}
		if (c == CPP)
			continue;
		*cp++ = (char)c;
	}
	*cp = '\0';
	/*
	 * Basically, the generated program is just an ongoing printf
	 * with breaks for {} format specifications.
	 */
	printf("\n");
	printf("#include <sys/types.h>\n");
	printf("#include <sys/inttypes.h>\n");
	printf("\n\n");
	printf("int do_fmt(char *acp);\n");
	printf("void format(char *name, size_t size, char *fmt);\n");
	printf("void indirect(off_t offset, size_t size, "
	    "char *base, char *member);\n");
	printf("void offset(off_t off);\n");
	printf("void offsetok(void);\n");
	printf("\n\n");
	printf("main(int argc, char *argv[])\n");
	printf("{\n");
	if (warn_flag) {
		printf("\textern int warnings;\n\n\twarnings = 0;\n");
	}
	cp = start_printf();
	while ((c = nextchar(cp)) != EOF) {
		switch (c) {
		case '"':
			*cp++ = '\\';	/* escape ' in string */
			*cp++ = '"';
			break;
		case '\n':
			*cp++ = '\\';	/* escape newline in string */
			*cp++ = 'n';
			break;
		case '{':
			emit_printf(cp);
			read_spec();
			generate();
			cp = start_printf();
			break;
		case CPP:
			/*
			 * Restart printf after cpp line.
			 */
			cp = start_printf();
			break;
		default:
			*cp++ = c;
			break;
		}
		if (cp - arg[1] >= STRLEN - 10) {
			emit_printf(cp);
			cp = start_printf();
		}
	}
	emit_printf(cp);

	/* terminate program, checking for "error" mode */
	printf("\n\tif (argc > 1 && strcmp(argv[1], \"-e\") == 0) {\n");
	printf("\t\textern int warns;\n\n");
	printf("\t\tif (warns)\n");
	printf("\t\t\treturn (1);\n");
	printf("\t}\n");
	printf("\treturn (0);\n");
	printf("}\n");

	return (0);
}

int
nextchar(char *cp)
{
	int c;
	static int newline = 1;

	c = getchar();
	/*
	 * Lines beginning with '#' and blank lines are passed right through.
	 */
	while (newline) {
		switch (c) {
		case '#':
			if (state)
				emit_printf(cp);
			do {
				putchar(c);
				c = getchar();
				if (c == EOF)
					return (c);
			} while (c != '\n');
			putchar(c);
			line_no++;
			return (CPP);
		case '\n':
			if (state)
				emit_printf(cp);
			putchar(c);
			c = getchar();
			line_no++;
			break;
		default:
			newline = 0;
			break;
		}
	}
	if (c == '\n') {
		newline++;
		line_no++;
	}
	return (c);
}

/*
 * Get started on printf of ongoing adb script.
 */
char *
start_printf(void)
{
	char *cp;

	strcpy(arg[0], "\"%s\"");
	cp = arg[1];
	*cp++ = '"';
	state = 1;			/* XXX */
	return (cp);
}

/*
 * Emit call to printf to print part of ongoing adb script.
 */
void
emit_printf(cp)
	char *cp;
{
	*cp++ = '"';
	*cp = '\0';
	emit_call("printf", 2);
	state = 0;			/* XXX */
}

/*
 * Read {} specification.
 * The first part (up to a comma) is put into "member".
 * The second part, if present, is put into "format".
 */
void
read_spec(void)
{
	char *cp;
	int c;
	int nesting;

	cp = member;
	specsize = 1;
	nesting = 0;
	while ((c = nextchar(NULL)) != '}' || (c == '}' && nesting)) {
		switch (c) {
		case EOF:
			fprintf(stderr, "Unexpected EOF inside {}\n");
			exit(1);
		case '\n':
			fprintf(stderr, "Newline not allowed in {}, line %d\n",
				line_no);
			exit(1);
		case '#':
			fprintf(stderr, "# not allowed in {}, line %d\n",
				line_no);
			exit(1);
		case ',':
			if (specsize == 2) {
				fprintf(stderr, "Excessive commas in {}, ");
				fprintf(stderr, "line %d\n", line_no);
				exit(1);
			}
			specsize = 2;
			*cp = '\0';
			cp = format;
			break;
		case '{':
			/*
			 * Allow up to one set of nested {}'s for adbgen
			 * requests of the form {member, {format string}}
			 */
			if (!nesting) {
				nesting = 1;
				*cp++ = c;
			} else {
				fprintf(stderr, "Too many {'s, line %d\n",
					line_no);
				exit(1);
			}
			break;
		case '}':
			*cp++ = c;
			nesting = 0;
			break;
		default:
			*cp++ = c;
			break;
		}
	}
	*cp = '\0';
	if (cp == member) {
		specsize = 0;
	}
}

/*
 * Decide what type of input specification we have.
 */
int
get_type(void)
{
	int i;

	if (specsize == 1) {
		if (streq(member, "SIZEOF")) {
			return (SIZEOF);
		}
		if (streq(member, "OFFSETOK")) {
			return (OFFSETOK);
		}
		if (streq(member, "END")) {
			return (END);
		}
		for (i = 0; i < FMT_ENTRIES; i++)
			if (streq(member, adbgen_fmt_tbl[i].f_str))
				return (i);
		return (OFFSET);
	}
	if (specsize == 2) {
		if (member[0] == '*') {
			return (INDIRECT);
		}
		if (streq(member, "EXPR")) {
			return (EXPR);
		}
		return (PRINT);
	}
	fprintf(stderr, "Invalid specification, line %d\n", line_no);
	exit(1);
}

/*
 * Generate the appropriate output for an input specification.
 */
void
generate(void)
{
	char *cp;
	int type;

	type = get_type();

	switch (type) {
	case PTR_HEX:
	case LONG_DEC:
	case LONG_OCT:
	case ULONG_DEC:
	case ULONG_HEX:
	case ULONG_OCT:
		cp = start_printf();
		*cp++ = adbgen_fmt_tbl[type].f_char;
		emit_printf(cp);
		break;
	case PRINT:
		emit_print();
		break;
	case OFFSET:
		emit_offset();
		break;
	case INDIRECT:
		emit_indirect();
		break;
	case OFFSETOK:
		emit_offsetok();
		break;
	case SIZEOF:
		emit_sizeof();
		break;
	case EXPR:
		emit_expr();
		break;
	case END:
		emit_end();
		break;
	default:
		fprintf(stderr, "Internal error in generate\n");
		exit(1);
	}
}

/*
 * Emit calls to set the offset and print a member.
 */
void
emit_print(void)
{
	char *cp;
	char fmt_request[STRLEN];
	int i;
	char number[STRLEN];

	emit_offset();
	/*
	 * Emit call to "format" subroutine
	 */
	sprintf(arg[0], "\"%s\"", member);
	sprintf(arg[1], "sizeof ((struct %s *)0)->%s",
		struct_name, member);

	/*
	 * Split the format string into <number><format character string>
	 * This is for format strings that contain format specifier requests
	 * like {POINTER_HEX}, {LONG_DEC}, etc. which need to be substituted
	 * with a format character instead.
	 */
	for (cp = format, i = 0; *cp >= '0' && *cp <= '9' && *cp != '\0';
	    cp++, i++)
		number[i] = *cp;
	number[i] = '\0';

	for (i = 0; i < FMT_ENTRIES; i++) {
		(void) sprintf(fmt_request, "{%s}", adbgen_fmt_tbl[i].f_str);
		if (streq(cp, fmt_request)) {
			sprintf(arg[2], "\"%s%c\"",
				number, adbgen_fmt_tbl[i].f_char);
			break;
		}
	}
	if (i == FMT_ENTRIES)
		sprintf(arg[2], "\"%s\"", format);

	emit_call("format", 3);
}

/*
 * Emit calls to set the offset and print a member.
 */
void
emit_offset(void)
{
	/*
	 * Emit call to "offset" subroutine
	 */
	sprintf(arg[0], "(off_t) &(((struct %s *)0)->%s)",
		struct_name, member);
	emit_call("offset", 1);
}

/*
 * Emit call to indirect routine.
 */
void
emit_indirect(void)
{
	sprintf(arg[0], "(off_t) &(((struct %s *)0)->%s)",
		struct_name, member+1);
	sprintf(arg[1], "sizeof ((struct %s *)0)->%s", struct_name, member+1);
	sprintf(arg[2], "\"%s\"", format);	/* adb register name */
	sprintf(arg[3], "\"%s\"", member);
	emit_call("indirect", 4);
}

/*
 * Emit call to "offsetok" routine.
 */
void
emit_offsetok(void)
{
	emit_call("offsetok", 0);
}

/*
 * Emit call to printf the sizeof the structure.
 */
void
emit_sizeof(void)
{
	sprintf(arg[0], "\"0t%%d\"");
	sprintf(arg[1], "sizeof (struct %s)", struct_name);
	emit_call("printf", 2);
}

/*
 * Emit call to printf an arbitrary C expression.
 */
void
emit_expr(void)
{
	sprintf(arg[0], "\"0t%%d\"");
	sprintf(arg[1], "(%s)", format);
	emit_call("printf", 2);
}

/*
 * Emit call to set offset to end of struct.
 */
void
emit_end(void)
{
	sprintf(arg[0], "sizeof (struct %s)", struct_name);
	emit_call("offset", 1);
}

/*
 * Emit call to subroutine name with nargs arguments from arg array.
 */
void
emit_call(char *name, int nargs)
{
	int i;

	printf("\t%s(", name);		/* name of subroutine */
	for (i = 0; i < nargs; i++) {
		if (i > 0) {
			printf(", ");	/* argument separator */
		}
		printf("%s", arg[i]);	/* argument */
	}
	printf(");\n");			/* end of call */
}
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * Post-process adb script.
 * All we do is collapse repeated formats into number*format.
 * E.g. XXX is collapsed to 3X.
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int
main()
{
	int c, quote, paren, savec, count, dispcmd;

	savec = count = 0;
	quote = 0;	/* not in quoted string */
	paren = 0;	/* not in parenthesized string */
	dispcmd = 0;	/* not in display command */
	while ((c = getchar()) != EOF) {
		if (c == '"') {
			quote = !quote;
		} else if (c == '(') {
			paren++;
		} else if (c == ')') {
			paren--;
		} else if (c == '/' || c == '?') {
			dispcmd = 1;
		} else if (c == '\n') {
			dispcmd = 0;
		}
		if (c == savec) {
			count++;
			continue;
		}
		if (savec) {
			if (count > 1) {
				printf("%d", count);
			}
			putchar(savec);
			savec = 0;
		}
		if (quote == 0 && paren == 0 && dispcmd &&
		    strchr("KJFXOQDUfYpPxoqdubcC+IaAtrn-", c)) {
			savec = c;
			count = 1;
		} else {
			putchar(c);
		}
	}
	if (savec)
		putchar(savec);
	return (0);
}
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * Post-process adb scripts to move increment and decrement around.
 * The reason is that at the start of each +/ line, adb prints out
 * the current location.  If the line then increments or decrements
 * dot before printing the user may be confused.  So we move the
 * increment or decrement to the preceeding line.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define	BUFSIZE 1024	/* gross enough to never be exceeded (we hope) */

char buf1[BUFSIZE], buf2[BUFSIZE];

static char *scanpastnum(char *);
static int goodstart(char *);

int
main()
{
	char *cur, *last, *cp1, *cp2, *ep, *t;
	int len;

	gets(buf1);
	last = buf1;
	cur = buf2;
	while (gets(cur) != NULL) {
		if (goodstart(cur) && goodstart(last)) {
			/*
			 * Scan cur for initial increment.
			 * Ignore quoted strings, tabbing, adb newlines.
			 */
			cp1 = cur + 2;
			while (*cp1) {
				if (*cp1 == '"') {
					/* scan past quoted string */
					while (*++cp1 && *cp1 != '"')
						;
					cp1++;
					continue;
				}
				if (*cp1 >= '0' && *cp1 <= '9') {
					cp2 = scanpastnum(cp1);
				} else {
					cp2 = cp1;
				}
				if (*cp2 == 't' || *cp2 == 'n' ||
				    *cp2 == ' ') {
					/* ok to skip over this one */
					cp1 = cp2 + 1;
					continue;
				} else {
					break;
				}
			}
			/*
			 * Cp1 now points at the first non-quoted string and
			 * non adb tab specification.
			 * Now determine if it's an increment or decrement.
			 */
			cp2 = scanpastnum(cp1);
			if (*cp2 == '+' || *cp2 == '-') {
				/*
				 * This is what we were hoping to find.
				 * Move increment or decrement into last.
				 */
				cp2++;
				ep = last + strlen(last);
				len = cp2  - cp1;
				strncpy(ep, cp1, len);
				ep[len] = '\0';
				/*
				 * Remove it from cur.
				 */
				strcpy(cp1, cp2);
			}
		}
		/*
		 * Prepare for next iteration.
		 */
		puts(last);
		t = cur;
		cur = last;
		last = t;
	}
	puts(last);
	return (0);
}

/*
 * Cp points at a digit.
 * Return pointer to next char that isn't a digit.
 */
static char *
scanpastnum(char *cp1)
{
	char *cp2;

	for (cp2 = cp1; isdigit(*cp2); cp2++)
		;
	return (cp2);
}

/*
 * Check whether a line has a good starting string.
 * We need ./ or +/ at the beginning to even think
 * of doing this motion stuff.
 */
static int
goodstart(char *cp)
{
	if (*cp == '.' || *cp == '+') {
		if (*++cp == '/')
			return (1);
	}
	return (0);
}
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright (c) 1983-1998 by Sun Microsystems, Inc.
 * All rights reserved.
 */

/*
 * Subroutines to be called by adbgen2.c, the C program generated
 * by adbgen1.c.
 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>

off_t last_off;
int warnings = 1;
int warns = 0;

/*
 * User claims offset is ok.
 * This usually follows call to another script, which we cannot handle.
 */
void
offsetok(void)
{
	last_off = -1;
}

/*
 * Get adb.s dot to the right offset.
 */
void
offset(off_t off)
{
	off_t off_diff;

	if (last_off == -1) {
		last_off = off;
		return;
	}
	off_diff = off - last_off;
	if (off_diff) {
		if (off_diff > 0) {
			if (off_diff > 1) {
				printf("%ld", off_diff);
			}
			printf("+");
		}
		if (off_diff < 0) {
			if (off_diff < -1) {
				printf("%ld", -off_diff);
			}
			printf("-");
		}
	}
	last_off = off;
}

/*
 * Emit the format command, return the size.
 */
int
do_fmt(char *acp)
{
	int rcount, width, sum, i;
	char *cp;

	cp = acp;
	sum = rcount = 0;
	do {
		while (*cp >= '0' && *cp <= '9') {
			rcount = rcount * 10 + *cp++ - '0';
		}
		if (rcount == 0) {
			rcount = 1;
		}
		switch (*cp) {
		case 'e':
		case 'E':
		case 'F':
		case 'g':
		case 'G':
		case 'J':
			width = 8;
			break;
		case 'K':
#ifdef	_LP64
			width = 8;
#else	/* _LP64 */
			width = 4;
#endif	/* _LP64 */
			break;
		case 'X':
		case 'O':
		case 'Q':
		case 'D':
		case 'U':
		case 'f':
		case 'Y':
		case 'p':
		case 'P':
			width = 4;
			break;
		case 'x':
		case 'o':
		case 'q':
		case 'd':
		case 'u':
			width = 2;
			break;
		case 'v':
		case 'V':
		case 'b':
		case 'B':
		case 'c':
		case 'C':
		case '+':
			width = 1;
			break;
		case 'I':
		case 'a':
		case 'A':
		case 't':
		case 'r':
		case 'n':
			width = 0;
			break;
		case '-':
			width = -1;
			break;
		case 's':
		case 'S':
		case 'i':
			if (warnings) {
				fprintf(stderr,
				"Unknown format size \"%s\", assuming zero\n",
				acp);
				warns++;
			}
			width = 0;
			break;
		default:
			fprintf(stderr, "Unknown format size: %s\n", acp);
			exit(1);
		}
		for (i = 0; i < rcount; i++) {
			putchar(*cp);
		}
		cp++;
		sum += width * rcount;
	} while (*cp);
	return (sum);
}

/*
 * Format struct member, checking size.
 */
void
format(char *name, size_t size, char *fmt)
{
	int fs;

	fs = do_fmt(fmt);
	if (fs != size && warnings) {
		fprintf(stderr,
			"warning: \"%s\" size is %ld, \"%s\" width is %d\n",
			name, size, fmt, fs);
		warns++;
	}
	last_off += fs;
}

/*
 * Get the value at offset based on base.
 */
void
indirect(off_t offset, size_t size, char *base, char *member)
{
	if (size == 8 || size == 4) {
		if (offset == 0) {
			printf("*%s", base);
		} else {
			printf("*(%s+0t%ld)", base, offset);
		}
	} else if (size == 2) {
		if (offset == 2) {
			printf("(*%s&0xffff)", base);
		} else {
			printf("(*(%s+0t%ld)&0xffff)", base, offset - 2);
		}
	} else if (size == 1) {
		if (offset == 3) {
			printf("(*%s&0xff)", base);
		} else {
			if ((offset & 0x1) == 0x1) {
				printf("(*(%s+0t%ld)&0xff)", base, offset - 3);
			} else {
				printf("((*(%s+0t%ld)&0xff00)/0x100)",
				    base, offset - 2);
			}
		}
	} else {
		fprintf(stderr, "Indirect size %ld not 1, 2, or 4: %s\n",
		    size, member);
		exit(1);
	}
}