|
root / base / usr / src / tools / cscope-fast / find.c
find.c C 1299 lines 27.6 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
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
/*
 * 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) 1988 AT&T	*/
/*	  All Rights Reserved  	*/


/*
 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * 	cscope - interactive C symbol or text cross-reference
 *
 *	searching functions
 */

#include <unistd.h>
#include <stdio.h>
#include <libgen.h>
#include "global.h"
#include "vp.h"

/*
 * most of these functions have been optimized so their innermost loops have
 * only one test for the desired character by putting the char and
 * an end-of-block marker (\0) at the end of the disk block buffer.
 * When the inner loop exits on the char, an outer loop will see if
 * the char is followed by a \0.  If so, it will read the next block
 * and restart the inner loop.
 */

char	block[BUFSIZ + 2];		/* leave room for end-of-block mark */
int	blocklen;			/* length of disk block read */
char	blockmark;			/* mark character to be searched for */
long	blocknumber;			/* block number */
char	*blockp;			/* pointer to current char in block */
char	lastfilepath[PATHLEN + 1];	/* last file that full path was */
					/* computed for */

static	char	cpattern[PATLEN + 1];	/* compressed pattern */
static	long	lastfcnoffset;		/* last function name offset */
static	long	postingsfound;		/* retrieved number of postings */
static	char	*regexp;		/* regular expression */
static	POSTING	*postingp;		/* retrieved posting set pointer */
static	long	searchcount;		/* count of files searched */
static	long	starttime;		/* start time for progress messages */

static POSTING *getposting(void);
static void putsource(FILE *output);
static void putref(char *file, char *function);
static void findcalledbysub(char *file);
static void findterm(void);
static void fileprogress(void);
static void putpostingref(POSTING *p);
static void putline(FILE *output);
static char *strtolower(char *s);
static char *filepath(char *file);

/* find the symbol in the cross-reference */

void
findsymbol(void)
{
	char	file[PATHLEN + 1];	/* source file name */
	char	function[PATLEN + 1];	/* function name */
	char	macro[PATLEN + 1];	/* macro name */
	char	symbol[PATLEN + 1];	/* symbol name */
	char	*cp;
	char	c;
	char	*s;

	if (invertedindex == YES) {
		long	lastline = 0;
		POSTING	*p;

		findterm();
		while ((p = getposting()) != NULL) {
			if (p->type != INCLUDE && p->lineoffset != lastline) {
				putpostingref(p);
				lastline = p->lineoffset;
			}
		}
		return;
	}
	(void) scanpast('\t');		/* find the end of the header */
	skiprefchar();			/* skip the file marker */
	getstring(file);		/* save the file name */
	*function = '\0';
	/* a macro can be inside a function, but not vice versa */
	*macro = '\0';

	/* find the next symbol */
	/* note: this code was expanded in-line for speed */
	/* while (scanpast('\n') != NULL) { */
	/* other macros were replaced by code using cp instead of blockp */
	cp = blockp;
	for (;;) {
		setmark('\n');
		do {	/* innermost loop optimized to only one test */
			while (*cp != '\n') {
				++cp;
			}
		} while (*(cp + 1) == '\0' && (cp = readblock()) != NULL);

		/* skip the found character */
		if (cp != NULL && *(++cp + 1) == '\0') {
			cp = readblock();
		}
		if (cp == NULL) {
			break;
		}
		/* look for a source file or function name */
		if (*cp == '\t') {
			blockp = cp;
			switch (getrefchar()) {

			case NEWFILE:		/* file name */

				/* save the name */
				skiprefchar();
				getstring(file);

				/* check for the end of the symbols */
				if (*file == '\0') {
					return;
				}
				fileprogress();
				/* FALLTHROUGH */

			case FCNEND:		/* function end */
				*function = '\0';
				goto notmatched;	/* don't match name */

			case FCNDEF:		/* function name */
				s = function;
				break;

			case DEFINE:		/* could be a macro */
				if (fileversion >= 10) {
					s = macro;
				} else {
					s = symbol;
				}
				break;

			case DEFINEEND:
				*macro = '\0';
				goto notmatched;	/* don't match name */

			case INCLUDE:		/* #include file */
				goto notmatched;	/* don't match name */
			default:		/* other symbol */
				s = symbol;
			}
			/* save the name */
			skiprefchar();
			getstring(s);

			/* see if this is a regular expression pattern */
			if (regexp != NULL) {
				if (caseless == YES) {
					s = strtolower(s);
				}
				if (*s != '\0' && regex(regexp, s) != NULL) {
					goto matched;
				}
			}
			/* match the symbol to the text pattern */
			else if (strequal(pattern, s)) {
				goto matched;
			}
			goto notmatched;
		}
		/* if this is a regular expression pattern */
		if (regexp != NULL) {
			c = *cp;
			if (c & 0200) {	/* digraph char? */
				c = dichar1[(c & 0177) / 8];
			}
			/* if this is a symbol */
			if (isalpha(c) || c == '_') {
				blockp = cp;
				getstring(symbol);
				s = symbol;
				if (caseless == YES) {
					s = strtolower(s);
				}
				/* match the symbol to the regular expression */
				if (regex(regexp, s) != NULL) {
					goto matched;
				}
				goto notmatched;
			}
		}
		/* match the character to the text pattern */
		else if (*cp == cpattern[0]) {
			blockp = cp;

			/* match the rest of the symbol to the text pattern */
			if (matchrest()) {
				s = NULL;
matched:
				/*
				 * output the file, calling function or macro,
				 * and source line
				 */
				if (*macro != '\0' && s != macro) {
					putref(file, macro);
				} else if (s != function) {
					putref(file, function);
				} else {
					putref(file, "");
				}
				if (blockp == NULL) {
					return;
				}
			}
notmatched:
			cp = blockp;
		}
	}
	blockp = cp;
}

/* find the function definition or #define */

void
finddef(void)
{
	char	file[PATHLEN + 1];	/* source file name */
	char	function[PATLEN + 1];	/* function name */
	char	macro[PATLEN + 1];	/* macro name */
	char	symbol[PATLEN + 1];	/* symbol name */
	char	*s;

	if (invertedindex == YES) {
		POSTING	*p;

		findterm();
		while ((p = getposting()) != NULL) {
			switch (p->type) {
			case DEFINE:		/* could be a macro */
			case FCNDEF:
			case CLASSDEF:
			case ENUMDEF:
			case MEMBERDEF:
			case STRUCTDEF:
			case TYPEDEF:
			case UNIONDEF:
			case GLOBALDEF:		/* other global definition */
			case LOCALDEF:		/* other local definition */
			case PARAMETER:
				putpostingref(p);
			}
		}
		return;
	}
	/* find the next file name or definition */
	*function = '\0';
	/* a macro can be inside a function, but not vice versa */
	*macro = '\0';

	while (scanpast('\t') != NULL) {
		switch (*blockp) {

		case NEWFILE:
			skiprefchar();	/* save file name */
			getstring(file);
			if (*file == '\0') {	/* if end of symbols */
				return;
			}
			fileprogress();
			/* FALLTHROUGH */

		case FCNEND:		/* function end */
			*function = '\0';
			break;

		case FCNDEF:		/* function name */
			s = function;
			goto def;

		case DEFINE:		/* could be a macro */
			if (fileversion >= 10) {
				s = macro;
			} else {
				s = symbol;
			}
			goto def;

		case DEFINEEND:
			*macro = '\0';
			break;

		case CLASSDEF:
		case ENUMDEF:
		case MEMBERDEF:
		case STRUCTDEF:
		case TYPEDEF:
		case UNIONDEF:
		case GLOBALDEF:		/* other global definition */
		case LOCALDEF:		/* other local definition */
		case PARAMETER:
			s = symbol;
		def:
			/* save the name */
			skiprefchar();
			getstring(s);

			/* see if this is a regular expression pattern */
			if (regexp != NULL) {
				if (caseless == YES) {
					s = strtolower(s);
				}
				if (*s != '\0' && regex(regexp, s) != NULL) {
					goto matched;
				}
			} else if (strequal(pattern, s)) {
				/* match the symbol to the text pattern */
matched:
				/*
				 * output the file, calling function or macro,
				 * and source line
				 */
				if (*macro != '\0' && s != macro) {
					putref(file, macro);
				} else if (s != function) {
					putref(file, function);
				} else {
					putref(file, "");
				}
			}
		}
	}
}

/* find all function definitions (used by samuel only) */

void
findallfcns(void)
{
	char	file[PATHLEN + 1];	/* source file name */
	char	function[PATLEN + 1];	/* function name */

	/* find the next file name or definition */
	while (scanpast('\t') != NULL) {
		switch (*blockp) {
		case NEWFILE:
			skiprefchar();	/* save file name */
			getstring(file);
			if (*file == '\0') {	/* if end of symbols */
				return;
			}
			fileprogress();
			break;

		case FCNDEF:
		case CLASSDEF:
			skiprefchar();	/* save function name */
			getstring(function);

			/* output the file, function and source line */
			putref(file, function);
			break;
		}
	}
}

/* find the functions called by this function */

void
findcalledby(void)
{
	char	file[PATHLEN + 1];	/* source file name */

	if (invertedindex == YES) {
		POSTING	*p;

		findterm();
		while ((p = getposting()) != NULL) {
			switch (p->type) {
			case DEFINE:		/* could be a macro */
			case FCNDEF:
				if (dbseek(p->lineoffset) != -1 &&
				    scanpast('\t') != NULL) {	/* skip def */
					findcalledbysub(srcfiles[p->fileindex]);
				}
			}
		}
		return;
	}
	/* find the function definition(s) */
	while (scanpast('\t') != NULL) {
		switch (*blockp) {
		case NEWFILE:
			skiprefchar();	/* save file name */
			getstring(file);
			if (*file == '\0') {	/* if end of symbols */
				return;
			}
			fileprogress();
			break;

		case DEFINE:		/* could be a macro */
			if (fileversion < 10) {
				break;
			}
			/* FALLTHROUGH */

		case FCNDEF:
			skiprefchar();	/* match name to pattern */
			if (match()) {
				findcalledbysub(file);
			}
			break;
		}
	}
}

static void
findcalledbysub(char *file)
{
	/* find the next function call or the end of this function */
	while (scanpast('\t') != NULL) {
		switch (*blockp) {
		case DEFINE:		/* #define inside a function */
			if (fileversion >= 10) {	/* skip it */
				while (scanpast('\t') != NULL &&
				    *blockp != DEFINEEND)
					;
			}
			break;
		case FCNCALL:		/* function call */

			/* output the file name */
			(void) fprintf(refsfound, "%s ", filepath(file));

			/* output the function name */
			skiprefchar();
			putline(refsfound);
			(void) putc(' ', refsfound);

			/* output the source line */
			putsource(refsfound);
			break;

		case DEFINEEND:		/* #define end */
		case FCNEND:		/* function end */
		case FCNDEF:		/* function end (pre 9.5) */
		case NEWFILE:		/* file end */
			return;
		}
	}
}

/* find the functions calling this function */

void
findcalling(void)
{
	char	file[PATHLEN + 1];	/* source file name */
	char	function[PATLEN + 1];	/* function name */
	char	macro[PATLEN + 1];	/* macro name */

	if (invertedindex == YES) {
		POSTING	*p;

		findterm();
		while ((p = getposting()) != NULL) {
			if (p->type == FCNCALL) {
				putpostingref(p);
			}
		}
		return;
	}
	/* find the next file name or function definition */
	/* a macro can be inside a function, but not vice versa */
	*macro = '\0';

	while (scanpast('\t') != NULL) {
		switch (*blockp) {
		case NEWFILE:		/* save file name */
			skiprefchar();
			getstring(file);
			if (*file == '\0') {	/* if end of symbols */
				return;
			}
			fileprogress();
			/* FALLTHROUGH */
		case FCNEND:		/* function end */
			*function = '\0';
			break;
		case DEFINE:		/* could be a macro */
			if (fileversion >= 10) {
				skiprefchar();
				getstring(macro);
			}
			break;

		case DEFINEEND:
			*macro = '\0';
			break;

		case FCNDEF:		/* save calling function name */
			skiprefchar();
			getstring(function);
			break;
		case FCNCALL:		/* match function called to pattern */
			skiprefchar();
			if (match()) {
				/* output the file, calling function or */
				/* macro, and source */
				if (*macro != '\0') {
					putref(file, macro);
				} else {
					putref(file, function);
				}
			}
		}
	}
}

/* find direct assignment to, and increment and decrement of, this variable */

void
findassignments(void)
{
	char	file[PATHLEN + 1];	/* source file name */
	char	function[PATLEN + 1];	/* function name */
	char	macro[PATLEN + 1];	/* macro name */

	if (fileversion < 13) {
		putmsg("Database built with cscope version < 13 does not "
		    "have assignment information");
		(void) sleep(3);
		return;
	}
#if CTRACE
	ctroff();
#endif
	if (invertedindex == YES) {
		POSTING	*p;

		findterm();
		while ((p = getposting()) != NULL) {
			switch (p->type) {
			case ASSIGNMENT:
			case GLOBALDEF:		/* can have initializer */
			case LOCALDEF:		/* can have initializer */
			case PARAMETER:		/* initial value */
				putpostingref(p);
			}
		}
		return;
	}
	/* find the next file name or function definition */
	/* a macro can be inside a function, but not vice versa */
	*macro = '\0';

	while (scanpast('\t') != NULL) {
		switch (*blockp) {
		case NEWFILE:		/* save file name */
			skiprefchar();
			getstring(file);
			if (*file == '\0') {	/* if end of symbols */
				return;
			}
			fileprogress();
			/* FALLTHROUGH */
		case FCNEND:		/* function end */
			*function = '\0';
			break;
		case DEFINE:		/* could be a macro */
			if (fileversion >= 10) {
				skiprefchar();
				getstring(macro);
			}
			break;

		case DEFINEEND:
			*macro = '\0';
			break;

		case FCNDEF:		/* save calling function name */
			skiprefchar();
			getstring(function);
			break;
		case ASSIGNMENT:	/* match assignment to pattern */
		case GLOBALDEF:		/* can have initializer */
		case LOCALDEF:		/* can have initializer */
		case PARAMETER:		/* initial value */
			skiprefchar();
			if (match()) {
				/* output the file, calling function or */
				/* macro, and source */
				if (*macro != '\0') {
					putref(file, macro);
				} else {
					putref(file, function);
				}
			}
		}
	}
}

/* find the grep pattern in the source files */

char *
findgreppat(void)
{
	char	egreppat[2 * PATLEN];
	char	*cp, *pp;

	/* translate egrep special characters in the regular expression */
	cp = egreppat;
	for (pp = pattern; *pp != '\0'; ++pp) {
		if (strchr("+?|()", *pp) != NULL) {
			*cp++ = '\\';
		}
		*cp++ = *pp;
	}
	*cp = '\0';

	/* search the source files */
	return (findegreppat(egreppat));
}

/* find this regular expression in the source files */

char *
findegreppat(char *egreppat)
{
	int	i;
	char	*egreperror;
	char	msg[MSGLEN + 1];

	/* compile the pattern */
	if ((egreperror = egrepinit(egreppat)) == NULL) {

		/* search the files */
		for (i = 0; i < nsrcfiles; ++i) {
			char *file = filepath(srcfiles[i]);
			fileprogress();
			if (egrep(file, refsfound, "%s <unknown> %ld ") < 0) {
				(void) sprintf(msg, "Cannot open file %s",
				    file);
				putmsg2(msg);
			}
		}
	}
	return (egreperror);
}

/* find matching file names */

void
findfile(void)
{
	int	i;
	char	*s;

	for (i = 0; i < nsrcfiles; ++i) {
		s = srcfiles[i];
		if (caseless == YES) {
			s = strtolower(s);
		}
		if (regex(regexp, s) != NULL) {
			(void) fprintf(refsfound, "%s <unknown> 1 <unknown>\n",
				filepath(srcfiles[i]));
		}
	}
}

/* find files #including this file */

void
findinclude(void)
{
	char	file[PATHLEN + 1];	/* source file name */

	if (invertedindex == YES) {
		POSTING	*p;

		findterm();
		while ((p = getposting()) != NULL) {
			if (p->type == INCLUDE) {
				putpostingref(p);
			}
		}
		return;
	}
	/* find the next file name or function definition */
	while (scanpast('\t') != NULL) {
		switch (*blockp) {

		case NEWFILE:		/* save file name */
			skiprefchar();
			getstring(file);
			if (*file == '\0') {	/* if end of symbols */
				return;
			}
			fileprogress();
			break;

		case INCLUDE:		/* match function called to pattern */
			skiprefchar();
			/* skip global or local #include marker */
			skiprefchar();
			if (match()) {
				/* output the file and source line */
				putref(file, "");
			}
		}
	}
}

/* initialize */

FINDINIT
findinit(void)
{
	char	buf[PATLEN + 3];
	BOOL	isregexp = NO;
	int	i;
	char	*s;
	unsigned c;

	/* remove trailing white space */
	for (s = pattern + strlen(pattern) - 1; isspace(*s); --s) {
		*s = '\0';
	}
	/* allow a partial match for a file name */
	if (field == FILENAME || field == INCLUDES) {
		/* allow types.h to match #include <sys/types.h> */
		if (invertedindex == YES && field == INCLUDES &&
		    strncmp(pattern, ".*", 2) != 0) {
			(void) sprintf(pattern, ".*%s", strcpy(buf, pattern));
		}
		if ((regexp = regcmp(pattern, (char *)NULL)) == NULL) {
			return (REGCMPERROR);
		}
		return (NOERROR);
	}
	/* see if the pattern is a regular expression */
	if (strpbrk(pattern, "^.[{*+$") != NULL) {
		isregexp = YES;
	} else {
		/* check for a valid C symbol */
		s = pattern;
		if (!isalpha(*s) && *s != '_') {
			return (NOTSYMBOL);
		}
		while (*++s != '\0') {
			if (!isalnum(*s) && *s != '_') {
				return (NOTSYMBOL);
			}
		}
		/*
		 * look for use of the -T option (truncate symbol to 8
		 * characters) on a database not built with -T
		 */
		if (truncatesyms == YES && isuptodate == YES &&
		    dbtruncated == NO && s - pattern >= 8) {
			(void) strcpy(pattern + 8, ".*");
			isregexp = YES;
		}
	}
	/* if this is a regular expression or letter case is to be ignored */
	/* or there is an inverted index */
	if (isregexp == YES || caseless == YES || invertedindex == YES) {

		/* remove a leading ^ */
		s = pattern;
		if (*s == '^') {
			(void) strcpy(newpat, s + 1);
			(void) strcpy(s, newpat);
		}
		/* remove a trailing $ */
		i = strlen(s) - 1;
		if (s[i] == '$') {
			s[i] = '\0';
		}
		/* if requested, try to truncate a C symbol pattern */
		if (truncatesyms == YES && strpbrk(s, "[{*+") == NULL) {
			s[8] = '\0';
		}
		/* must be an exact match */
		/*
		 * note: regcmp doesn't recognize ^*keypad$ as an syntax error
		 * unless it is given as a single arg
		 */
		(void) sprintf(buf, "^%s$", s);
		if ((regexp = regcmp(buf, (char *)NULL)) == NULL) {
			return (REGCMPERROR);
		}
	} else {
		/* if requested, truncate a C symbol pattern */
		if (truncatesyms == YES && field <= CALLING) {
			pattern[8] = '\0';
		}
		/* compress the string pattern for matching */
		s = cpattern;
		for (i = 0; (c = pattern[i]) != '\0'; ++i) {
			if (dicode1[c] && dicode2[(unsigned)pattern[i + 1]]) {
				c = (0200 - 2) + dicode1[c] +
				    dicode2[(unsigned)pattern[i + 1]];
				++i;
			}
			*s++ = (char)c;
		}
		*s = '\0';
	}
	return (NOERROR);
}

void
findcleanup(void)
{
	/* discard any regular expression */
	if (regexp != NULL) {
		free(regexp);
		regexp = NULL;
	}
}

/* find this term, which can be a regular expression */

static void
findterm(void)
{
	char	*s;
	int	len;
	char	prefix[PATLEN + 1];
	char	term[PATLEN + 1];

	npostings = 0;		/* will be non-zero after database built */
	lastfcnoffset = 0;	/* clear the last function name found */
	boolclear();		/* clear the posting set */

	/* get the string prefix (if any) of the regular expression */
	(void) strcpy(prefix, pattern);
	if ((s = strpbrk(prefix, ".[{*+")) != NULL) {
		*s = '\0';
	}
	/* if letter case is to be ignored */
	if (caseless == YES) {

		/*
		 * convert the prefix to upper case because it is lexically
		 * less than lower case
		 */
		s = prefix;
		while (*s != '\0') {
			*s = toupper(*s);
			++s;
		}
	}
	/* find the term lexically >= the prefix */
	(void) invfind(&invcontrol, prefix);
	if (caseless == YES) {	/* restore lower case */
		(void) strcpy(prefix, strtolower(prefix));
	}
	/*
	 * a null prefix matches the null term in the inverted index,
	 * so move to the first real term
	 */
	if (*prefix == '\0') {
		(void) invforward(&invcontrol);
	}
	len = strlen(prefix);
	do {
		(void) invterm(&invcontrol, term);	/* get the term */
		s = term;
		if (caseless == YES) {
			s = strtolower(s);	/* make it lower case */
		}
		/* if it matches */
		if (regex(regexp, s) != NULL) {
			/* add it's postings to the set */
			if ((postingp = boolfile(&invcontrol,
			    &npostings, OR)) == NULL) {
				break;
			}
		} else if (len > 0) {
			/* if there is a prefix */

			/*
			 * if ignoring letter case and the term is out of the
			 * range of possible matches
			 */
			if (caseless == YES) {
				if (strncmp(term, prefix, len) > 0) {
					break;	/* stop searching */
				}
			}
			/* if using letter case and the prefix doesn't match */
			else if (strncmp(term, prefix, len) != 0) {
				break;	/* stop searching */
			}
		}
		/* display progress about every three seconds */
		if (++searchcount % 50 == 0) {
			progress("%ld of %ld symbols matched",
			    searchcount, totalterms);
		}
	} while (invforward(&invcontrol));	/* while didn't wrap around */

	/* initialize the progress message for retrieving the references */
	initprogress();
	postingsfound = npostings;
}

/* display the file search progress about every three seconds */

static void
fileprogress(void)
{
	if (++searchcount % 10 == 0) {
		progress("%ld of %ld files searched", searchcount,
		    (long)nsrcfiles);
	}
}

/* initialize the progress message */

void
initprogress(void)
{
	searchcount = 0;
	starttime = time((long *)NULL);
}

/* display the progress every three seconds */

void
progress(char *format, long n1, long n2)
{
	char	msg[MSGLEN + 1];
	long	now;

	/* print after 2 seconds so the average is nearer 3 seconds */
	if (linemode == NO && (now = time((long *)NULL)) - starttime >= 2) {
		starttime = now;
		(void) sprintf(msg, format, n1, n2);
		putmsg(msg);
	}
}

/* match the pattern to the string */

BOOL
match(void)
{
	char	string[PATLEN + 1];
	char	*s;

	/* see if this is a regular expression pattern */
	if (regexp != NULL) {
		getstring(string);
		if (*string == '\0') {
			return (NO);
		}
		s = string;
		if (caseless == YES) {
			s = strtolower(s);
		}
		return (regex(regexp, s) ? YES : NO);
	}
	/* it is a string pattern */
	return ((BOOL)(*blockp == cpattern[0] && matchrest()));
}

/* match the rest of the pattern to the name */

BOOL
matchrest(void)
{
	int	i = 1;

	skiprefchar();
	do {
		while (*blockp == cpattern[i]) {
			++blockp;
			++i;
		}
	} while (*(blockp + 1) == '\0' && readblock() != NULL);

	if (*blockp == '\n' && cpattern[i] == '\0') {
		return (YES);
	}
	return (NO);
}

/* get the next posting for this term */

static POSTING *
getposting(void)
{
	if (npostings-- <= 0) {
		return (NULL);
	}
	/* display progress about every three seconds */
	if (++searchcount % 100 == 0) {
		progress("%ld of %ld possible references retrieved",
		    searchcount, postingsfound);
	}
	return (postingp++);
}

/* put the posting reference into the file */

static void
putpostingref(POSTING *p)
{
	static	char	function[PATLEN + 1];	/* function name */

	if (p->fcnoffset == 0) {
		*function = '\0';
	} else if (p->fcnoffset != lastfcnoffset) {
		if (dbseek(p->fcnoffset) != -1) {
			getstring(function);
			lastfcnoffset = p->fcnoffset;
		}
	}
	if (dbseek(p->lineoffset) != -1) {
		putref(srcfiles[p->fileindex], function);
	}
}

/* put the reference into the file */

static void
putref(char *file, char *function)
{
	FILE	*output;

	/* put global references first */
	if (*function == '\0') {
		function = "<global>";
		output = refsfound;
	} else {
		output = nonglobalrefs;
	}
	if (fprintf(output, "%s %s ", filepath(file), function) == EOF) {
		cannotwrite(temp1);
		/* NOTREACHED */
	}
	putsource(output);
}

/* put the source line into the file */

static void
putsource(FILE *output)
{
	char	*cp, nextc = '\0';

	if (fileversion <= 5) {
		(void) scanpast(' ');
		putline(output);
		(void) putc('\n', output);
		return;
	}
	/* scan back to the beginning of the source line */
	cp = blockp;
	while (*cp != '\n' || nextc != '\n') {
		nextc = *cp;
		if (--cp < block) {
			/* read the previous block */
			(void) dbseek((blocknumber - 1) * BUFSIZ);
			cp = &block[BUFSIZ - 1];
		}
	}
	/* there must be a double newline followed by a line number */
	blockp = cp;
	setmark(' ');	/* so getrefchar doesn't skip the last block char */
	if (*blockp != '\n' || getrefchar() != '\n' ||
	    !isdigit(getrefchar()) && fileversion >= 12) {
		putmsg("Internal error: cannot get source line from database");
		myexit(1);
	}
	/* until a double newline is found */
	do {
		/* skip a symbol type */
		if (*blockp == '\t') {
			skiprefchar();
			skiprefchar();
		}
		/* output a piece of the source line */
		putline(output);
	} while (blockp != NULL && getrefchar() != '\n');
	(void) putc('\n', output);
}

/* put the rest of the cross-reference line into the file */

static void
putline(FILE *output)
{
	char	*cp;
	unsigned c;

	setmark('\n');
	cp = blockp;
	do {
		while ((c = *cp) != '\n') {
			/* check for a compressed digraph */
			if (c & 0200) {
				c &= 0177;
				(void) putc(dichar1[c / 8], output);
				(void) putc(dichar2[c & 7], output);
			} else if (c < ' ') {
				/* a compressed keyword */
				(void) fputs(keyword[c].text, output);
				if (keyword[c].delim != '\0') {
					(void) putc(' ', output);
				}
				if (keyword[c].delim == '(') {
					(void) putc('(', output);
				}
			} else {
				(void) putc((int)c, output);
			}
			++cp;
		}
	} while (*(cp + 1) == '\0' && (cp = readblock()) != NULL);
	blockp = cp;
}

/* put the rest of the cross-reference line into the string */

void
getstring(char *s)
{
	char	*cp;
	unsigned c;

	setmark('\n');
	cp = blockp;
	do {
		while ((c = *cp) != '\n') {
			if (c & 0200) {
				c &= 0177;
				*s++ = dichar1[c / 8];
				*s++ = dichar2[c & 7];
			} else {
				*s++ = (char)c;
			}
			++cp;
		}
	} while (*(cp + 1) == '\0' && (cp = readblock()) != NULL);
	blockp = cp;
	*s = '\0';
}

/* scan past the next occurence of this character in the cross-reference */

char *
scanpast(int c)
{
	char	*cp;

	setmark(c);
	cp = blockp;
	do {	/* innermost loop optimized to only one test */
		while (*cp != c) {
			++cp;
		}
	} while (*(cp + 1) == '\0' && (cp = readblock()) != NULL);
	blockp = cp;
	if (cp != NULL) {
		skiprefchar();	/* skip the found character */
	}
	return (blockp);
}

/* read a block of the cross-reference */

char *
readblock(void)
{
	/* read the next block */
	blocklen = read(symrefs, block, BUFSIZ);
	blockp = block;

	/* add the search character and end-of-block mark */
	block[blocklen] = blockmark;
	block[blocklen + 1] = '\0';

	/* return NULL on end-of-file */
	if (blocklen == 0) {
		blockp = NULL;
	} else {
		++blocknumber;
	}
	return (blockp);
}

/* seek to the database offset */

long
dbseek(long offset)
{
	long	n;
	int	rc = 0;

	if ((n = offset / BUFSIZ) != blocknumber) {
		if ((rc = lseek(symrefs, n * BUFSIZ, 0)) == -1) {
			myperror("Lseek failed");
			(void) sleep(3);
			return (rc);
		}
		(void) readblock();
		blocknumber = n;
	}
	blockp = block + offset % BUFSIZ;
	return (rc);
}

/* convert the string to lower case */

static char *
strtolower(char *s)
{
	static char buf[PATLEN + 1];
	char *lp = buf;

	while (*s != '\0') {
		/*
		 * note: s in not incremented in this line because the BSD
		 * compatibility tolower macro evaluates its argument twice
		 */
		*lp++ = tolower(*s);
		++s;
	}
	*lp = '\0';
	return (buf);
}

/* if needed, convert a relative path to a full path */

static char *
filepath(char *file)
{
	static	char	path[PATHLEN + 1];
	int	i;

	if (*file != '/') {

		/* if same file as last time, return the same path */
		if (strequal(file, lastfilepath)) {
			return (path);
		}
		(void) strcpy(lastfilepath, file);

		/* if requested, prepend a path to a relative file path */
		if (prependpath != NULL) {
			(void) sprintf(path, "%s/%s", prependpath, file);
			return (path);
		}
		/*
		 * if the database was built with a view path, return a
		 * full path so "cscope -d -f" does not have to be called
		 * from the build directory with the same view path
		 */
		if (dbvpndirs > 1) {
			for (i = 0; i < dbvpndirs; i++) {
				(void) sprintf(path,
				    "%s/%s", dbvpdirs[i], file);
				if (access(path, READ) != -1) {
					return (path);
				}
			}
		}
		(void) strcpy(path, file);	/* for lastfilepath check */
	}
	return (file);
}