xref: /onnv-gate/usr/src/tools/cscope-fast/find.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 /*	Copyright (c) 1988 AT&T	*/
23*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate /*
27*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
28*0Sstevel@tonic-gate  * Use is subject to license terms.
29*0Sstevel@tonic-gate  */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate /*
34*0Sstevel@tonic-gate  * 	cscope - interactive C symbol or text cross-reference
35*0Sstevel@tonic-gate  *
36*0Sstevel@tonic-gate  *	searching functions
37*0Sstevel@tonic-gate  */
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate #include <unistd.h>
40*0Sstevel@tonic-gate #include <stdio.h>
41*0Sstevel@tonic-gate #include <libgen.h>
42*0Sstevel@tonic-gate #include "global.h"
43*0Sstevel@tonic-gate #include "vp.h"
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate /*
46*0Sstevel@tonic-gate  * most of these functions have been optimized so their innermost loops have
47*0Sstevel@tonic-gate  * only one test for the desired character by putting the char and
48*0Sstevel@tonic-gate  * an end-of-block marker (\0) at the end of the disk block buffer.
49*0Sstevel@tonic-gate  * When the inner loop exits on the char, an outer loop will see if
50*0Sstevel@tonic-gate  * the char is followed by a \0.  If so, it will read the next block
51*0Sstevel@tonic-gate  * and restart the inner loop.
52*0Sstevel@tonic-gate  */
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate char	block[BUFSIZ + 2];		/* leave room for end-of-block mark */
55*0Sstevel@tonic-gate int	blocklen;			/* length of disk block read */
56*0Sstevel@tonic-gate char	blockmark;			/* mark character to be searched for */
57*0Sstevel@tonic-gate long	blocknumber;			/* block number */
58*0Sstevel@tonic-gate char	*blockp;			/* pointer to current char in block */
59*0Sstevel@tonic-gate char	lastfilepath[PATHLEN + 1];	/* last file that full path was */
60*0Sstevel@tonic-gate 					/* computed for */
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate static	char	cpattern[PATLEN + 1];	/* compressed pattern */
63*0Sstevel@tonic-gate static	long	lastfcnoffset;		/* last function name offset */
64*0Sstevel@tonic-gate static	long	postingsfound;		/* retrieved number of postings */
65*0Sstevel@tonic-gate static	char	*regexp;		/* regular expression */
66*0Sstevel@tonic-gate static	POSTING	*postingp;		/* retrieved posting set pointer */
67*0Sstevel@tonic-gate static	long	searchcount;		/* count of files searched */
68*0Sstevel@tonic-gate static	long	starttime;		/* start time for progress messages */
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate static POSTING *getposting(void);
71*0Sstevel@tonic-gate static void putsource(FILE *output);
72*0Sstevel@tonic-gate static void putref(char *file, char *function);
73*0Sstevel@tonic-gate static void findcalledbysub(char *file);
74*0Sstevel@tonic-gate static void findterm(void);
75*0Sstevel@tonic-gate static void fileprogress(void);
76*0Sstevel@tonic-gate static void putpostingref(POSTING *p);
77*0Sstevel@tonic-gate static void putline(FILE *output);
78*0Sstevel@tonic-gate static char *strtolower(char *s);
79*0Sstevel@tonic-gate static char *filepath(char *file);
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate /* find the symbol in the cross-reference */
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate void
findsymbol(void)84*0Sstevel@tonic-gate findsymbol(void)
85*0Sstevel@tonic-gate {
86*0Sstevel@tonic-gate 	char	file[PATHLEN + 1];	/* source file name */
87*0Sstevel@tonic-gate 	char	function[PATLEN + 1];	/* function name */
88*0Sstevel@tonic-gate 	char	macro[PATLEN + 1];	/* macro name */
89*0Sstevel@tonic-gate 	char	symbol[PATLEN + 1];	/* symbol name */
90*0Sstevel@tonic-gate 	char	*cp;
91*0Sstevel@tonic-gate 	char	c;
92*0Sstevel@tonic-gate 	char	*s;
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate 	if (invertedindex == YES) {
95*0Sstevel@tonic-gate 		long	lastline = 0;
96*0Sstevel@tonic-gate 		POSTING	*p;
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate 		findterm();
99*0Sstevel@tonic-gate 		while ((p = getposting()) != NULL) {
100*0Sstevel@tonic-gate 			if (p->type != INCLUDE && p->lineoffset != lastline) {
101*0Sstevel@tonic-gate 				putpostingref(p);
102*0Sstevel@tonic-gate 				lastline = p->lineoffset;
103*0Sstevel@tonic-gate 			}
104*0Sstevel@tonic-gate 		}
105*0Sstevel@tonic-gate 		return;
106*0Sstevel@tonic-gate 	}
107*0Sstevel@tonic-gate 	(void) scanpast('\t');		/* find the end of the header */
108*0Sstevel@tonic-gate 	skiprefchar();			/* skip the file marker */
109*0Sstevel@tonic-gate 	getstring(file);		/* save the file name */
110*0Sstevel@tonic-gate 	*function = '\0';
111*0Sstevel@tonic-gate 	/* a macro can be inside a function, but not vice versa */
112*0Sstevel@tonic-gate 	*macro = '\0';
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 	/* find the next symbol */
115*0Sstevel@tonic-gate 	/* note: this code was expanded in-line for speed */
116*0Sstevel@tonic-gate 	/* while (scanpast('\n') != NULL) { */
117*0Sstevel@tonic-gate 	/* other macros were replaced by code using cp instead of blockp */
118*0Sstevel@tonic-gate 	cp = blockp;
119*0Sstevel@tonic-gate 	for (;;) {
120*0Sstevel@tonic-gate 		setmark('\n');
121*0Sstevel@tonic-gate 		do {	/* innermost loop optimized to only one test */
122*0Sstevel@tonic-gate 			while (*cp != '\n') {
123*0Sstevel@tonic-gate 				++cp;
124*0Sstevel@tonic-gate 			}
125*0Sstevel@tonic-gate 		} while (*(cp + 1) == '\0' && (cp = readblock()) != NULL);
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate 		/* skip the found character */
128*0Sstevel@tonic-gate 		if (cp != NULL && *(++cp + 1) == '\0') {
129*0Sstevel@tonic-gate 			cp = readblock();
130*0Sstevel@tonic-gate 		}
131*0Sstevel@tonic-gate 		if (cp == NULL) {
132*0Sstevel@tonic-gate 			break;
133*0Sstevel@tonic-gate 		}
134*0Sstevel@tonic-gate 		/* look for a source file or function name */
135*0Sstevel@tonic-gate 		if (*cp == '\t') {
136*0Sstevel@tonic-gate 			blockp = cp;
137*0Sstevel@tonic-gate 			switch (getrefchar()) {
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate 			case NEWFILE:		/* file name */
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate 				/* save the name */
142*0Sstevel@tonic-gate 				skiprefchar();
143*0Sstevel@tonic-gate 				getstring(file);
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 				/* check for the end of the symbols */
146*0Sstevel@tonic-gate 				if (*file == '\0') {
147*0Sstevel@tonic-gate 					return;
148*0Sstevel@tonic-gate 				}
149*0Sstevel@tonic-gate 				fileprogress();
150*0Sstevel@tonic-gate 				/* FALLTHROUGH */
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 			case FCNEND:		/* function end */
153*0Sstevel@tonic-gate 				*function = '\0';
154*0Sstevel@tonic-gate 				goto notmatched;	/* don't match name */
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 			case FCNDEF:		/* function name */
157*0Sstevel@tonic-gate 				s = function;
158*0Sstevel@tonic-gate 				break;
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 			case DEFINE:		/* could be a macro */
161*0Sstevel@tonic-gate 				if (fileversion >= 10) {
162*0Sstevel@tonic-gate 					s = macro;
163*0Sstevel@tonic-gate 				} else {
164*0Sstevel@tonic-gate 					s = symbol;
165*0Sstevel@tonic-gate 				}
166*0Sstevel@tonic-gate 				break;
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate 			case DEFINEEND:
169*0Sstevel@tonic-gate 				*macro = '\0';
170*0Sstevel@tonic-gate 				goto notmatched;	/* don't match name */
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate 			case INCLUDE:		/* #include file */
173*0Sstevel@tonic-gate 				goto notmatched;	/* don't match name */
174*0Sstevel@tonic-gate 			default:		/* other symbol */
175*0Sstevel@tonic-gate 				s = symbol;
176*0Sstevel@tonic-gate 			}
177*0Sstevel@tonic-gate 			/* save the name */
178*0Sstevel@tonic-gate 			skiprefchar();
179*0Sstevel@tonic-gate 			getstring(s);
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 			/* see if this is a regular expression pattern */
182*0Sstevel@tonic-gate 			if (regexp != NULL) {
183*0Sstevel@tonic-gate 				if (caseless == YES) {
184*0Sstevel@tonic-gate 					s = strtolower(s);
185*0Sstevel@tonic-gate 				}
186*0Sstevel@tonic-gate 				if (*s != '\0' && regex(regexp, s) != NULL) {
187*0Sstevel@tonic-gate 					goto matched;
188*0Sstevel@tonic-gate 				}
189*0Sstevel@tonic-gate 			}
190*0Sstevel@tonic-gate 			/* match the symbol to the text pattern */
191*0Sstevel@tonic-gate 			else if (strequal(pattern, s)) {
192*0Sstevel@tonic-gate 				goto matched;
193*0Sstevel@tonic-gate 			}
194*0Sstevel@tonic-gate 			goto notmatched;
195*0Sstevel@tonic-gate 		}
196*0Sstevel@tonic-gate 		/* if this is a regular expression pattern */
197*0Sstevel@tonic-gate 		if (regexp != NULL) {
198*0Sstevel@tonic-gate 			c = *cp;
199*0Sstevel@tonic-gate 			if (c & 0200) {	/* digraph char? */
200*0Sstevel@tonic-gate 				c = dichar1[(c & 0177) / 8];
201*0Sstevel@tonic-gate 			}
202*0Sstevel@tonic-gate 			/* if this is a symbol */
203*0Sstevel@tonic-gate 			if (isalpha(c) || c == '_') {
204*0Sstevel@tonic-gate 				blockp = cp;
205*0Sstevel@tonic-gate 				getstring(symbol);
206*0Sstevel@tonic-gate 				s = symbol;
207*0Sstevel@tonic-gate 				if (caseless == YES) {
208*0Sstevel@tonic-gate 					s = strtolower(s);
209*0Sstevel@tonic-gate 				}
210*0Sstevel@tonic-gate 				/* match the symbol to the regular expression */
211*0Sstevel@tonic-gate 				if (regex(regexp, s) != NULL) {
212*0Sstevel@tonic-gate 					goto matched;
213*0Sstevel@tonic-gate 				}
214*0Sstevel@tonic-gate 				goto notmatched;
215*0Sstevel@tonic-gate 			}
216*0Sstevel@tonic-gate 		}
217*0Sstevel@tonic-gate 		/* match the character to the text pattern */
218*0Sstevel@tonic-gate 		else if (*cp == cpattern[0]) {
219*0Sstevel@tonic-gate 			blockp = cp;
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate 			/* match the rest of the symbol to the text pattern */
222*0Sstevel@tonic-gate 			if (matchrest()) {
223*0Sstevel@tonic-gate 				s = NULL;
224*0Sstevel@tonic-gate matched:
225*0Sstevel@tonic-gate 				/*
226*0Sstevel@tonic-gate 				 * output the file, calling function or macro,
227*0Sstevel@tonic-gate 				 * and source line
228*0Sstevel@tonic-gate 				 */
229*0Sstevel@tonic-gate 				if (*macro != '\0' && s != macro) {
230*0Sstevel@tonic-gate 					putref(file, macro);
231*0Sstevel@tonic-gate 				} else if (s != function) {
232*0Sstevel@tonic-gate 					putref(file, function);
233*0Sstevel@tonic-gate 				} else {
234*0Sstevel@tonic-gate 					putref(file, "");
235*0Sstevel@tonic-gate 				}
236*0Sstevel@tonic-gate 				if (blockp == NULL) {
237*0Sstevel@tonic-gate 					return;
238*0Sstevel@tonic-gate 				}
239*0Sstevel@tonic-gate 			}
240*0Sstevel@tonic-gate notmatched:
241*0Sstevel@tonic-gate 			cp = blockp;
242*0Sstevel@tonic-gate 		}
243*0Sstevel@tonic-gate 	}
244*0Sstevel@tonic-gate 	blockp = cp;
245*0Sstevel@tonic-gate }
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate /* find the function definition or #define */
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate void
finddef(void)250*0Sstevel@tonic-gate finddef(void)
251*0Sstevel@tonic-gate {
252*0Sstevel@tonic-gate 	char	file[PATHLEN + 1];	/* source file name */
253*0Sstevel@tonic-gate 	char	function[PATLEN + 1];	/* function name */
254*0Sstevel@tonic-gate 	char	macro[PATLEN + 1];	/* macro name */
255*0Sstevel@tonic-gate 	char	symbol[PATLEN + 1];	/* symbol name */
256*0Sstevel@tonic-gate 	char	*s;
257*0Sstevel@tonic-gate 
258*0Sstevel@tonic-gate 	if (invertedindex == YES) {
259*0Sstevel@tonic-gate 		POSTING	*p;
260*0Sstevel@tonic-gate 
261*0Sstevel@tonic-gate 		findterm();
262*0Sstevel@tonic-gate 		while ((p = getposting()) != NULL) {
263*0Sstevel@tonic-gate 			switch (p->type) {
264*0Sstevel@tonic-gate 			case DEFINE:		/* could be a macro */
265*0Sstevel@tonic-gate 			case FCNDEF:
266*0Sstevel@tonic-gate 			case CLASSDEF:
267*0Sstevel@tonic-gate 			case ENUMDEF:
268*0Sstevel@tonic-gate 			case MEMBERDEF:
269*0Sstevel@tonic-gate 			case STRUCTDEF:
270*0Sstevel@tonic-gate 			case TYPEDEF:
271*0Sstevel@tonic-gate 			case UNIONDEF:
272*0Sstevel@tonic-gate 			case GLOBALDEF:		/* other global definition */
273*0Sstevel@tonic-gate 			case LOCALDEF:		/* other local definition */
274*0Sstevel@tonic-gate 			case PARAMETER:
275*0Sstevel@tonic-gate 				putpostingref(p);
276*0Sstevel@tonic-gate 			}
277*0Sstevel@tonic-gate 		}
278*0Sstevel@tonic-gate 		return;
279*0Sstevel@tonic-gate 	}
280*0Sstevel@tonic-gate 	/* find the next file name or definition */
281*0Sstevel@tonic-gate 	*function = '\0';
282*0Sstevel@tonic-gate 	/* a macro can be inside a function, but not vice versa */
283*0Sstevel@tonic-gate 	*macro = '\0';
284*0Sstevel@tonic-gate 
285*0Sstevel@tonic-gate 	while (scanpast('\t') != NULL) {
286*0Sstevel@tonic-gate 		switch (*blockp) {
287*0Sstevel@tonic-gate 
288*0Sstevel@tonic-gate 		case NEWFILE:
289*0Sstevel@tonic-gate 			skiprefchar();	/* save file name */
290*0Sstevel@tonic-gate 			getstring(file);
291*0Sstevel@tonic-gate 			if (*file == '\0') {	/* if end of symbols */
292*0Sstevel@tonic-gate 				return;
293*0Sstevel@tonic-gate 			}
294*0Sstevel@tonic-gate 			fileprogress();
295*0Sstevel@tonic-gate 			/* FALLTHROUGH */
296*0Sstevel@tonic-gate 
297*0Sstevel@tonic-gate 		case FCNEND:		/* function end */
298*0Sstevel@tonic-gate 			*function = '\0';
299*0Sstevel@tonic-gate 			break;
300*0Sstevel@tonic-gate 
301*0Sstevel@tonic-gate 		case FCNDEF:		/* function name */
302*0Sstevel@tonic-gate 			s = function;
303*0Sstevel@tonic-gate 			goto def;
304*0Sstevel@tonic-gate 
305*0Sstevel@tonic-gate 		case DEFINE:		/* could be a macro */
306*0Sstevel@tonic-gate 			if (fileversion >= 10) {
307*0Sstevel@tonic-gate 				s = macro;
308*0Sstevel@tonic-gate 			} else {
309*0Sstevel@tonic-gate 				s = symbol;
310*0Sstevel@tonic-gate 			}
311*0Sstevel@tonic-gate 			goto def;
312*0Sstevel@tonic-gate 
313*0Sstevel@tonic-gate 		case DEFINEEND:
314*0Sstevel@tonic-gate 			*macro = '\0';
315*0Sstevel@tonic-gate 			break;
316*0Sstevel@tonic-gate 
317*0Sstevel@tonic-gate 		case CLASSDEF:
318*0Sstevel@tonic-gate 		case ENUMDEF:
319*0Sstevel@tonic-gate 		case MEMBERDEF:
320*0Sstevel@tonic-gate 		case STRUCTDEF:
321*0Sstevel@tonic-gate 		case TYPEDEF:
322*0Sstevel@tonic-gate 		case UNIONDEF:
323*0Sstevel@tonic-gate 		case GLOBALDEF:		/* other global definition */
324*0Sstevel@tonic-gate 		case LOCALDEF:		/* other local definition */
325*0Sstevel@tonic-gate 		case PARAMETER:
326*0Sstevel@tonic-gate 			s = symbol;
327*0Sstevel@tonic-gate 		def:
328*0Sstevel@tonic-gate 			/* save the name */
329*0Sstevel@tonic-gate 			skiprefchar();
330*0Sstevel@tonic-gate 			getstring(s);
331*0Sstevel@tonic-gate 
332*0Sstevel@tonic-gate 			/* see if this is a regular expression pattern */
333*0Sstevel@tonic-gate 			if (regexp != NULL) {
334*0Sstevel@tonic-gate 				if (caseless == YES) {
335*0Sstevel@tonic-gate 					s = strtolower(s);
336*0Sstevel@tonic-gate 				}
337*0Sstevel@tonic-gate 				if (*s != '\0' && regex(regexp, s) != NULL) {
338*0Sstevel@tonic-gate 					goto matched;
339*0Sstevel@tonic-gate 				}
340*0Sstevel@tonic-gate 			} else if (strequal(pattern, s)) {
341*0Sstevel@tonic-gate 				/* match the symbol to the text pattern */
342*0Sstevel@tonic-gate matched:
343*0Sstevel@tonic-gate 				/*
344*0Sstevel@tonic-gate 				 * output the file, calling function or macro,
345*0Sstevel@tonic-gate 				 * and source line
346*0Sstevel@tonic-gate 				 */
347*0Sstevel@tonic-gate 				if (*macro != '\0' && s != macro) {
348*0Sstevel@tonic-gate 					putref(file, macro);
349*0Sstevel@tonic-gate 				} else if (s != function) {
350*0Sstevel@tonic-gate 					putref(file, function);
351*0Sstevel@tonic-gate 				} else {
352*0Sstevel@tonic-gate 					putref(file, "");
353*0Sstevel@tonic-gate 				}
354*0Sstevel@tonic-gate 			}
355*0Sstevel@tonic-gate 		}
356*0Sstevel@tonic-gate 	}
357*0Sstevel@tonic-gate }
358*0Sstevel@tonic-gate 
359*0Sstevel@tonic-gate /* find all function definitions (used by samuel only) */
360*0Sstevel@tonic-gate 
361*0Sstevel@tonic-gate void
findallfcns(void)362*0Sstevel@tonic-gate findallfcns(void)
363*0Sstevel@tonic-gate {
364*0Sstevel@tonic-gate 	char	file[PATHLEN + 1];	/* source file name */
365*0Sstevel@tonic-gate 	char	function[PATLEN + 1];	/* function name */
366*0Sstevel@tonic-gate 
367*0Sstevel@tonic-gate 	/* find the next file name or definition */
368*0Sstevel@tonic-gate 	while (scanpast('\t') != NULL) {
369*0Sstevel@tonic-gate 		switch (*blockp) {
370*0Sstevel@tonic-gate 		case NEWFILE:
371*0Sstevel@tonic-gate 			skiprefchar();	/* save file name */
372*0Sstevel@tonic-gate 			getstring(file);
373*0Sstevel@tonic-gate 			if (*file == '\0') {	/* if end of symbols */
374*0Sstevel@tonic-gate 				return;
375*0Sstevel@tonic-gate 			}
376*0Sstevel@tonic-gate 			fileprogress();
377*0Sstevel@tonic-gate 			break;
378*0Sstevel@tonic-gate 
379*0Sstevel@tonic-gate 		case FCNDEF:
380*0Sstevel@tonic-gate 		case CLASSDEF:
381*0Sstevel@tonic-gate 			skiprefchar();	/* save function name */
382*0Sstevel@tonic-gate 			getstring(function);
383*0Sstevel@tonic-gate 
384*0Sstevel@tonic-gate 			/* output the file, function and source line */
385*0Sstevel@tonic-gate 			putref(file, function);
386*0Sstevel@tonic-gate 			break;
387*0Sstevel@tonic-gate 		}
388*0Sstevel@tonic-gate 	}
389*0Sstevel@tonic-gate }
390*0Sstevel@tonic-gate 
391*0Sstevel@tonic-gate /* find the functions called by this function */
392*0Sstevel@tonic-gate 
393*0Sstevel@tonic-gate void
findcalledby(void)394*0Sstevel@tonic-gate findcalledby(void)
395*0Sstevel@tonic-gate {
396*0Sstevel@tonic-gate 	char	file[PATHLEN + 1];	/* source file name */
397*0Sstevel@tonic-gate 
398*0Sstevel@tonic-gate 	if (invertedindex == YES) {
399*0Sstevel@tonic-gate 		POSTING	*p;
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate 		findterm();
402*0Sstevel@tonic-gate 		while ((p = getposting()) != NULL) {
403*0Sstevel@tonic-gate 			switch (p->type) {
404*0Sstevel@tonic-gate 			case DEFINE:		/* could be a macro */
405*0Sstevel@tonic-gate 			case FCNDEF:
406*0Sstevel@tonic-gate 				if (dbseek(p->lineoffset) != -1 &&
407*0Sstevel@tonic-gate 				    scanpast('\t') != NULL) {	/* skip def */
408*0Sstevel@tonic-gate 					findcalledbysub(srcfiles[p->fileindex]);
409*0Sstevel@tonic-gate 				}
410*0Sstevel@tonic-gate 			}
411*0Sstevel@tonic-gate 		}
412*0Sstevel@tonic-gate 		return;
413*0Sstevel@tonic-gate 	}
414*0Sstevel@tonic-gate 	/* find the function definition(s) */
415*0Sstevel@tonic-gate 	while (scanpast('\t') != NULL) {
416*0Sstevel@tonic-gate 		switch (*blockp) {
417*0Sstevel@tonic-gate 		case NEWFILE:
418*0Sstevel@tonic-gate 			skiprefchar();	/* save file name */
419*0Sstevel@tonic-gate 			getstring(file);
420*0Sstevel@tonic-gate 			if (*file == '\0') {	/* if end of symbols */
421*0Sstevel@tonic-gate 				return;
422*0Sstevel@tonic-gate 			}
423*0Sstevel@tonic-gate 			fileprogress();
424*0Sstevel@tonic-gate 			break;
425*0Sstevel@tonic-gate 
426*0Sstevel@tonic-gate 		case DEFINE:		/* could be a macro */
427*0Sstevel@tonic-gate 			if (fileversion < 10) {
428*0Sstevel@tonic-gate 				break;
429*0Sstevel@tonic-gate 			}
430*0Sstevel@tonic-gate 			/* FALLTHROUGH */
431*0Sstevel@tonic-gate 
432*0Sstevel@tonic-gate 		case FCNDEF:
433*0Sstevel@tonic-gate 			skiprefchar();	/* match name to pattern */
434*0Sstevel@tonic-gate 			if (match()) {
435*0Sstevel@tonic-gate 				findcalledbysub(file);
436*0Sstevel@tonic-gate 			}
437*0Sstevel@tonic-gate 			break;
438*0Sstevel@tonic-gate 		}
439*0Sstevel@tonic-gate 	}
440*0Sstevel@tonic-gate }
441*0Sstevel@tonic-gate 
442*0Sstevel@tonic-gate static void
findcalledbysub(char * file)443*0Sstevel@tonic-gate findcalledbysub(char *file)
444*0Sstevel@tonic-gate {
445*0Sstevel@tonic-gate 	/* find the next function call or the end of this function */
446*0Sstevel@tonic-gate 	while (scanpast('\t') != NULL) {
447*0Sstevel@tonic-gate 		switch (*blockp) {
448*0Sstevel@tonic-gate 		case DEFINE:		/* #define inside a function */
449*0Sstevel@tonic-gate 			if (fileversion >= 10) {	/* skip it */
450*0Sstevel@tonic-gate 				while (scanpast('\t') != NULL &&
451*0Sstevel@tonic-gate 				    *blockp != DEFINEEND)
452*0Sstevel@tonic-gate 					;
453*0Sstevel@tonic-gate 			}
454*0Sstevel@tonic-gate 			break;
455*0Sstevel@tonic-gate 		case FCNCALL:		/* function call */
456*0Sstevel@tonic-gate 
457*0Sstevel@tonic-gate 			/* output the file name */
458*0Sstevel@tonic-gate 			(void) fprintf(refsfound, "%s ", filepath(file));
459*0Sstevel@tonic-gate 
460*0Sstevel@tonic-gate 			/* output the function name */
461*0Sstevel@tonic-gate 			skiprefchar();
462*0Sstevel@tonic-gate 			putline(refsfound);
463*0Sstevel@tonic-gate 			(void) putc(' ', refsfound);
464*0Sstevel@tonic-gate 
465*0Sstevel@tonic-gate 			/* output the source line */
466*0Sstevel@tonic-gate 			putsource(refsfound);
467*0Sstevel@tonic-gate 			break;
468*0Sstevel@tonic-gate 
469*0Sstevel@tonic-gate 		case DEFINEEND:		/* #define end */
470*0Sstevel@tonic-gate 		case FCNEND:		/* function end */
471*0Sstevel@tonic-gate 		case FCNDEF:		/* function end (pre 9.5) */
472*0Sstevel@tonic-gate 		case NEWFILE:		/* file end */
473*0Sstevel@tonic-gate 			return;
474*0Sstevel@tonic-gate 		}
475*0Sstevel@tonic-gate 	}
476*0Sstevel@tonic-gate }
477*0Sstevel@tonic-gate 
478*0Sstevel@tonic-gate /* find the functions calling this function */
479*0Sstevel@tonic-gate 
480*0Sstevel@tonic-gate void
findcalling(void)481*0Sstevel@tonic-gate findcalling(void)
482*0Sstevel@tonic-gate {
483*0Sstevel@tonic-gate 	char	file[PATHLEN + 1];	/* source file name */
484*0Sstevel@tonic-gate 	char	function[PATLEN + 1];	/* function name */
485*0Sstevel@tonic-gate 	char	macro[PATLEN + 1];	/* macro name */
486*0Sstevel@tonic-gate 
487*0Sstevel@tonic-gate 	if (invertedindex == YES) {
488*0Sstevel@tonic-gate 		POSTING	*p;
489*0Sstevel@tonic-gate 
490*0Sstevel@tonic-gate 		findterm();
491*0Sstevel@tonic-gate 		while ((p = getposting()) != NULL) {
492*0Sstevel@tonic-gate 			if (p->type == FCNCALL) {
493*0Sstevel@tonic-gate 				putpostingref(p);
494*0Sstevel@tonic-gate 			}
495*0Sstevel@tonic-gate 		}
496*0Sstevel@tonic-gate 		return;
497*0Sstevel@tonic-gate 	}
498*0Sstevel@tonic-gate 	/* find the next file name or function definition */
499*0Sstevel@tonic-gate 	/* a macro can be inside a function, but not vice versa */
500*0Sstevel@tonic-gate 	*macro = '\0';
501*0Sstevel@tonic-gate 
502*0Sstevel@tonic-gate 	while (scanpast('\t') != NULL) {
503*0Sstevel@tonic-gate 		switch (*blockp) {
504*0Sstevel@tonic-gate 		case NEWFILE:		/* save file name */
505*0Sstevel@tonic-gate 			skiprefchar();
506*0Sstevel@tonic-gate 			getstring(file);
507*0Sstevel@tonic-gate 			if (*file == '\0') {	/* if end of symbols */
508*0Sstevel@tonic-gate 				return;
509*0Sstevel@tonic-gate 			}
510*0Sstevel@tonic-gate 			fileprogress();
511*0Sstevel@tonic-gate 			/* FALLTHROUGH */
512*0Sstevel@tonic-gate 		case FCNEND:		/* function end */
513*0Sstevel@tonic-gate 			*function = '\0';
514*0Sstevel@tonic-gate 			break;
515*0Sstevel@tonic-gate 		case DEFINE:		/* could be a macro */
516*0Sstevel@tonic-gate 			if (fileversion >= 10) {
517*0Sstevel@tonic-gate 				skiprefchar();
518*0Sstevel@tonic-gate 				getstring(macro);
519*0Sstevel@tonic-gate 			}
520*0Sstevel@tonic-gate 			break;
521*0Sstevel@tonic-gate 
522*0Sstevel@tonic-gate 		case DEFINEEND:
523*0Sstevel@tonic-gate 			*macro = '\0';
524*0Sstevel@tonic-gate 			break;
525*0Sstevel@tonic-gate 
526*0Sstevel@tonic-gate 		case FCNDEF:		/* save calling function name */
527*0Sstevel@tonic-gate 			skiprefchar();
528*0Sstevel@tonic-gate 			getstring(function);
529*0Sstevel@tonic-gate 			break;
530*0Sstevel@tonic-gate 		case FCNCALL:		/* match function called to pattern */
531*0Sstevel@tonic-gate 			skiprefchar();
532*0Sstevel@tonic-gate 			if (match()) {
533*0Sstevel@tonic-gate 				/* output the file, calling function or */
534*0Sstevel@tonic-gate 				/* macro, and source */
535*0Sstevel@tonic-gate 				if (*macro != '\0') {
536*0Sstevel@tonic-gate 					putref(file, macro);
537*0Sstevel@tonic-gate 				} else {
538*0Sstevel@tonic-gate 					putref(file, function);
539*0Sstevel@tonic-gate 				}
540*0Sstevel@tonic-gate 			}
541*0Sstevel@tonic-gate 		}
542*0Sstevel@tonic-gate 	}
543*0Sstevel@tonic-gate }
544*0Sstevel@tonic-gate 
545*0Sstevel@tonic-gate /* find direct assignment to, and increment and decrement of, this variable */
546*0Sstevel@tonic-gate 
547*0Sstevel@tonic-gate void
findassignments(void)548*0Sstevel@tonic-gate findassignments(void)
549*0Sstevel@tonic-gate {
550*0Sstevel@tonic-gate 	char	file[PATHLEN + 1];	/* source file name */
551*0Sstevel@tonic-gate 	char	function[PATLEN + 1];	/* function name */
552*0Sstevel@tonic-gate 	char	macro[PATLEN + 1];	/* macro name */
553*0Sstevel@tonic-gate 
554*0Sstevel@tonic-gate 	if (fileversion < 13) {
555*0Sstevel@tonic-gate 		putmsg("Database built with cscope version < 13 does not "
556*0Sstevel@tonic-gate 		    "have assignment information");
557*0Sstevel@tonic-gate 		(void) sleep(3);
558*0Sstevel@tonic-gate 		return;
559*0Sstevel@tonic-gate 	}
560*0Sstevel@tonic-gate #if CTRACE
561*0Sstevel@tonic-gate 	ctroff();
562*0Sstevel@tonic-gate #endif
563*0Sstevel@tonic-gate 	if (invertedindex == YES) {
564*0Sstevel@tonic-gate 		POSTING	*p;
565*0Sstevel@tonic-gate 
566*0Sstevel@tonic-gate 		findterm();
567*0Sstevel@tonic-gate 		while ((p = getposting()) != NULL) {
568*0Sstevel@tonic-gate 			switch (p->type) {
569*0Sstevel@tonic-gate 			case ASSIGNMENT:
570*0Sstevel@tonic-gate 			case GLOBALDEF:		/* can have initializer */
571*0Sstevel@tonic-gate 			case LOCALDEF:		/* can have initializer */
572*0Sstevel@tonic-gate 			case PARAMETER:		/* initial value */
573*0Sstevel@tonic-gate 				putpostingref(p);
574*0Sstevel@tonic-gate 			}
575*0Sstevel@tonic-gate 		}
576*0Sstevel@tonic-gate 		return;
577*0Sstevel@tonic-gate 	}
578*0Sstevel@tonic-gate 	/* find the next file name or function definition */
579*0Sstevel@tonic-gate 	/* a macro can be inside a function, but not vice versa */
580*0Sstevel@tonic-gate 	*macro = '\0';
581*0Sstevel@tonic-gate 
582*0Sstevel@tonic-gate 	while (scanpast('\t') != NULL) {
583*0Sstevel@tonic-gate 		switch (*blockp) {
584*0Sstevel@tonic-gate 		case NEWFILE:		/* save file name */
585*0Sstevel@tonic-gate 			skiprefchar();
586*0Sstevel@tonic-gate 			getstring(file);
587*0Sstevel@tonic-gate 			if (*file == '\0') {	/* if end of symbols */
588*0Sstevel@tonic-gate 				return;
589*0Sstevel@tonic-gate 			}
590*0Sstevel@tonic-gate 			fileprogress();
591*0Sstevel@tonic-gate 			/* FALLTHROUGH */
592*0Sstevel@tonic-gate 		case FCNEND:		/* function end */
593*0Sstevel@tonic-gate 			*function = '\0';
594*0Sstevel@tonic-gate 			break;
595*0Sstevel@tonic-gate 		case DEFINE:		/* could be a macro */
596*0Sstevel@tonic-gate 			if (fileversion >= 10) {
597*0Sstevel@tonic-gate 				skiprefchar();
598*0Sstevel@tonic-gate 				getstring(macro);
599*0Sstevel@tonic-gate 			}
600*0Sstevel@tonic-gate 			break;
601*0Sstevel@tonic-gate 
602*0Sstevel@tonic-gate 		case DEFINEEND:
603*0Sstevel@tonic-gate 			*macro = '\0';
604*0Sstevel@tonic-gate 			break;
605*0Sstevel@tonic-gate 
606*0Sstevel@tonic-gate 		case FCNDEF:		/* save calling function name */
607*0Sstevel@tonic-gate 			skiprefchar();
608*0Sstevel@tonic-gate 			getstring(function);
609*0Sstevel@tonic-gate 			break;
610*0Sstevel@tonic-gate 		case ASSIGNMENT:	/* match assignment to pattern */
611*0Sstevel@tonic-gate 		case GLOBALDEF:		/* can have initializer */
612*0Sstevel@tonic-gate 		case LOCALDEF:		/* can have initializer */
613*0Sstevel@tonic-gate 		case PARAMETER:		/* initial value */
614*0Sstevel@tonic-gate 			skiprefchar();
615*0Sstevel@tonic-gate 			if (match()) {
616*0Sstevel@tonic-gate 				/* output the file, calling function or */
617*0Sstevel@tonic-gate 				/* macro, and source */
618*0Sstevel@tonic-gate 				if (*macro != '\0') {
619*0Sstevel@tonic-gate 					putref(file, macro);
620*0Sstevel@tonic-gate 				} else {
621*0Sstevel@tonic-gate 					putref(file, function);
622*0Sstevel@tonic-gate 				}
623*0Sstevel@tonic-gate 			}
624*0Sstevel@tonic-gate 		}
625*0Sstevel@tonic-gate 	}
626*0Sstevel@tonic-gate }
627*0Sstevel@tonic-gate 
628*0Sstevel@tonic-gate /* find the grep pattern in the source files */
629*0Sstevel@tonic-gate 
630*0Sstevel@tonic-gate char *
findgreppat(void)631*0Sstevel@tonic-gate findgreppat(void)
632*0Sstevel@tonic-gate {
633*0Sstevel@tonic-gate 	char	egreppat[2 * PATLEN];
634*0Sstevel@tonic-gate 	char	*cp, *pp;
635*0Sstevel@tonic-gate 
636*0Sstevel@tonic-gate 	/* translate egrep special characters in the regular expression */
637*0Sstevel@tonic-gate 	cp = egreppat;
638*0Sstevel@tonic-gate 	for (pp = pattern; *pp != '\0'; ++pp) {
639*0Sstevel@tonic-gate 		if (strchr("+?|()", *pp) != NULL) {
640*0Sstevel@tonic-gate 			*cp++ = '\\';
641*0Sstevel@tonic-gate 		}
642*0Sstevel@tonic-gate 		*cp++ = *pp;
643*0Sstevel@tonic-gate 	}
644*0Sstevel@tonic-gate 	*cp = '\0';
645*0Sstevel@tonic-gate 
646*0Sstevel@tonic-gate 	/* search the source files */
647*0Sstevel@tonic-gate 	return (findegreppat(egreppat));
648*0Sstevel@tonic-gate }
649*0Sstevel@tonic-gate 
650*0Sstevel@tonic-gate /* find this regular expression in the source files */
651*0Sstevel@tonic-gate 
652*0Sstevel@tonic-gate char *
findegreppat(char * egreppat)653*0Sstevel@tonic-gate findegreppat(char *egreppat)
654*0Sstevel@tonic-gate {
655*0Sstevel@tonic-gate 	int	i;
656*0Sstevel@tonic-gate 	char	*egreperror;
657*0Sstevel@tonic-gate 	char	msg[MSGLEN + 1];
658*0Sstevel@tonic-gate 
659*0Sstevel@tonic-gate 	/* compile the pattern */
660*0Sstevel@tonic-gate 	if ((egreperror = egrepinit(egreppat)) == NULL) {
661*0Sstevel@tonic-gate 
662*0Sstevel@tonic-gate 		/* search the files */
663*0Sstevel@tonic-gate 		for (i = 0; i < nsrcfiles; ++i) {
664*0Sstevel@tonic-gate 			char *file = filepath(srcfiles[i]);
665*0Sstevel@tonic-gate 			fileprogress();
666*0Sstevel@tonic-gate 			if (egrep(file, refsfound, "%s <unknown> %ld ") < 0) {
667*0Sstevel@tonic-gate 				(void) sprintf(msg, "Cannot open file %s",
668*0Sstevel@tonic-gate 				    file);
669*0Sstevel@tonic-gate 				putmsg2(msg);
670*0Sstevel@tonic-gate 			}
671*0Sstevel@tonic-gate 		}
672*0Sstevel@tonic-gate 	}
673*0Sstevel@tonic-gate 	return (egreperror);
674*0Sstevel@tonic-gate }
675*0Sstevel@tonic-gate 
676*0Sstevel@tonic-gate /* find matching file names */
677*0Sstevel@tonic-gate 
678*0Sstevel@tonic-gate void
findfile(void)679*0Sstevel@tonic-gate findfile(void)
680*0Sstevel@tonic-gate {
681*0Sstevel@tonic-gate 	int	i;
682*0Sstevel@tonic-gate 	char	*s;
683*0Sstevel@tonic-gate 
684*0Sstevel@tonic-gate 	for (i = 0; i < nsrcfiles; ++i) {
685*0Sstevel@tonic-gate 		s = srcfiles[i];
686*0Sstevel@tonic-gate 		if (caseless == YES) {
687*0Sstevel@tonic-gate 			s = strtolower(s);
688*0Sstevel@tonic-gate 		}
689*0Sstevel@tonic-gate 		if (regex(regexp, s) != NULL) {
690*0Sstevel@tonic-gate 			(void) fprintf(refsfound, "%s <unknown> 1 <unknown>\n",
691*0Sstevel@tonic-gate 				filepath(srcfiles[i]));
692*0Sstevel@tonic-gate 		}
693*0Sstevel@tonic-gate 	}
694*0Sstevel@tonic-gate }
695*0Sstevel@tonic-gate 
696*0Sstevel@tonic-gate /* find files #including this file */
697*0Sstevel@tonic-gate 
698*0Sstevel@tonic-gate void
findinclude(void)699*0Sstevel@tonic-gate findinclude(void)
700*0Sstevel@tonic-gate {
701*0Sstevel@tonic-gate 	char	file[PATHLEN + 1];	/* source file name */
702*0Sstevel@tonic-gate 
703*0Sstevel@tonic-gate 	if (invertedindex == YES) {
704*0Sstevel@tonic-gate 		POSTING	*p;
705*0Sstevel@tonic-gate 
706*0Sstevel@tonic-gate 		findterm();
707*0Sstevel@tonic-gate 		while ((p = getposting()) != NULL) {
708*0Sstevel@tonic-gate 			if (p->type == INCLUDE) {
709*0Sstevel@tonic-gate 				putpostingref(p);
710*0Sstevel@tonic-gate 			}
711*0Sstevel@tonic-gate 		}
712*0Sstevel@tonic-gate 		return;
713*0Sstevel@tonic-gate 	}
714*0Sstevel@tonic-gate 	/* find the next file name or function definition */
715*0Sstevel@tonic-gate 	while (scanpast('\t') != NULL) {
716*0Sstevel@tonic-gate 		switch (*blockp) {
717*0Sstevel@tonic-gate 
718*0Sstevel@tonic-gate 		case NEWFILE:		/* save file name */
719*0Sstevel@tonic-gate 			skiprefchar();
720*0Sstevel@tonic-gate 			getstring(file);
721*0Sstevel@tonic-gate 			if (*file == '\0') {	/* if end of symbols */
722*0Sstevel@tonic-gate 				return;
723*0Sstevel@tonic-gate 			}
724*0Sstevel@tonic-gate 			fileprogress();
725*0Sstevel@tonic-gate 			break;
726*0Sstevel@tonic-gate 
727*0Sstevel@tonic-gate 		case INCLUDE:		/* match function called to pattern */
728*0Sstevel@tonic-gate 			skiprefchar();
729*0Sstevel@tonic-gate 			/* skip global or local #include marker */
730*0Sstevel@tonic-gate 			skiprefchar();
731*0Sstevel@tonic-gate 			if (match()) {
732*0Sstevel@tonic-gate 				/* output the file and source line */
733*0Sstevel@tonic-gate 				putref(file, "");
734*0Sstevel@tonic-gate 			}
735*0Sstevel@tonic-gate 		}
736*0Sstevel@tonic-gate 	}
737*0Sstevel@tonic-gate }
738*0Sstevel@tonic-gate 
739*0Sstevel@tonic-gate /* initialize */
740*0Sstevel@tonic-gate 
741*0Sstevel@tonic-gate FINDINIT
findinit(void)742*0Sstevel@tonic-gate findinit(void)
743*0Sstevel@tonic-gate {
744*0Sstevel@tonic-gate 	char	buf[PATLEN + 3];
745*0Sstevel@tonic-gate 	BOOL	isregexp = NO;
746*0Sstevel@tonic-gate 	int	i;
747*0Sstevel@tonic-gate 	char	*s;
748*0Sstevel@tonic-gate 	unsigned c;
749*0Sstevel@tonic-gate 
750*0Sstevel@tonic-gate 	/* remove trailing white space */
751*0Sstevel@tonic-gate 	for (s = pattern + strlen(pattern) - 1; isspace(*s); --s) {
752*0Sstevel@tonic-gate 		*s = '\0';
753*0Sstevel@tonic-gate 	}
754*0Sstevel@tonic-gate 	/* allow a partial match for a file name */
755*0Sstevel@tonic-gate 	if (field == FILENAME || field == INCLUDES) {
756*0Sstevel@tonic-gate 		/* allow types.h to match #include <sys/types.h> */
757*0Sstevel@tonic-gate 		if (invertedindex == YES && field == INCLUDES &&
758*0Sstevel@tonic-gate 		    strncmp(pattern, ".*", 2) != 0) {
759*0Sstevel@tonic-gate 			(void) sprintf(pattern, ".*%s", strcpy(buf, pattern));
760*0Sstevel@tonic-gate 		}
761*0Sstevel@tonic-gate 		if ((regexp = regcmp(pattern, (char *)NULL)) == NULL) {
762*0Sstevel@tonic-gate 			return (REGCMPERROR);
763*0Sstevel@tonic-gate 		}
764*0Sstevel@tonic-gate 		return (NOERROR);
765*0Sstevel@tonic-gate 	}
766*0Sstevel@tonic-gate 	/* see if the pattern is a regular expression */
767*0Sstevel@tonic-gate 	if (strpbrk(pattern, "^.[{*+$") != NULL) {
768*0Sstevel@tonic-gate 		isregexp = YES;
769*0Sstevel@tonic-gate 	} else {
770*0Sstevel@tonic-gate 		/* check for a valid C symbol */
771*0Sstevel@tonic-gate 		s = pattern;
772*0Sstevel@tonic-gate 		if (!isalpha(*s) && *s != '_') {
773*0Sstevel@tonic-gate 			return (NOTSYMBOL);
774*0Sstevel@tonic-gate 		}
775*0Sstevel@tonic-gate 		while (*++s != '\0') {
776*0Sstevel@tonic-gate 			if (!isalnum(*s) && *s != '_') {
777*0Sstevel@tonic-gate 				return (NOTSYMBOL);
778*0Sstevel@tonic-gate 			}
779*0Sstevel@tonic-gate 		}
780*0Sstevel@tonic-gate 		/*
781*0Sstevel@tonic-gate 		 * look for use of the -T option (truncate symbol to 8
782*0Sstevel@tonic-gate 		 * characters) on a database not built with -T
783*0Sstevel@tonic-gate 		 */
784*0Sstevel@tonic-gate 		if (truncatesyms == YES && isuptodate == YES &&
785*0Sstevel@tonic-gate 		    dbtruncated == NO && s - pattern >= 8) {
786*0Sstevel@tonic-gate 			(void) strcpy(pattern + 8, ".*");
787*0Sstevel@tonic-gate 			isregexp = YES;
788*0Sstevel@tonic-gate 		}
789*0Sstevel@tonic-gate 	}
790*0Sstevel@tonic-gate 	/* if this is a regular expression or letter case is to be ignored */
791*0Sstevel@tonic-gate 	/* or there is an inverted index */
792*0Sstevel@tonic-gate 	if (isregexp == YES || caseless == YES || invertedindex == YES) {
793*0Sstevel@tonic-gate 
794*0Sstevel@tonic-gate 		/* remove a leading ^ */
795*0Sstevel@tonic-gate 		s = pattern;
796*0Sstevel@tonic-gate 		if (*s == '^') {
797*0Sstevel@tonic-gate 			(void) strcpy(newpat, s + 1);
798*0Sstevel@tonic-gate 			(void) strcpy(s, newpat);
799*0Sstevel@tonic-gate 		}
800*0Sstevel@tonic-gate 		/* remove a trailing $ */
801*0Sstevel@tonic-gate 		i = strlen(s) - 1;
802*0Sstevel@tonic-gate 		if (s[i] == '$') {
803*0Sstevel@tonic-gate 			s[i] = '\0';
804*0Sstevel@tonic-gate 		}
805*0Sstevel@tonic-gate 		/* if requested, try to truncate a C symbol pattern */
806*0Sstevel@tonic-gate 		if (truncatesyms == YES && strpbrk(s, "[{*+") == NULL) {
807*0Sstevel@tonic-gate 			s[8] = '\0';
808*0Sstevel@tonic-gate 		}
809*0Sstevel@tonic-gate 		/* must be an exact match */
810*0Sstevel@tonic-gate 		/*
811*0Sstevel@tonic-gate 		 * note: regcmp doesn't recognize ^*keypad$ as an syntax error
812*0Sstevel@tonic-gate 		 * unless it is given as a single arg
813*0Sstevel@tonic-gate 		 */
814*0Sstevel@tonic-gate 		(void) sprintf(buf, "^%s$", s);
815*0Sstevel@tonic-gate 		if ((regexp = regcmp(buf, (char *)NULL)) == NULL) {
816*0Sstevel@tonic-gate 			return (REGCMPERROR);
817*0Sstevel@tonic-gate 		}
818*0Sstevel@tonic-gate 	} else {
819*0Sstevel@tonic-gate 		/* if requested, truncate a C symbol pattern */
820*0Sstevel@tonic-gate 		if (truncatesyms == YES && field <= CALLING) {
821*0Sstevel@tonic-gate 			pattern[8] = '\0';
822*0Sstevel@tonic-gate 		}
823*0Sstevel@tonic-gate 		/* compress the string pattern for matching */
824*0Sstevel@tonic-gate 		s = cpattern;
825*0Sstevel@tonic-gate 		for (i = 0; (c = pattern[i]) != '\0'; ++i) {
826*0Sstevel@tonic-gate 			if (dicode1[c] && dicode2[(unsigned)pattern[i + 1]]) {
827*0Sstevel@tonic-gate 				c = (0200 - 2) + dicode1[c] +
828*0Sstevel@tonic-gate 				    dicode2[(unsigned)pattern[i + 1]];
829*0Sstevel@tonic-gate 				++i;
830*0Sstevel@tonic-gate 			}
831*0Sstevel@tonic-gate 			*s++ = (char)c;
832*0Sstevel@tonic-gate 		}
833*0Sstevel@tonic-gate 		*s = '\0';
834*0Sstevel@tonic-gate 	}
835*0Sstevel@tonic-gate 	return (NOERROR);
836*0Sstevel@tonic-gate }
837*0Sstevel@tonic-gate 
838*0Sstevel@tonic-gate void
findcleanup(void)839*0Sstevel@tonic-gate findcleanup(void)
840*0Sstevel@tonic-gate {
841*0Sstevel@tonic-gate 	/* discard any regular expression */
842*0Sstevel@tonic-gate 	if (regexp != NULL) {
843*0Sstevel@tonic-gate 		free(regexp);
844*0Sstevel@tonic-gate 		regexp = NULL;
845*0Sstevel@tonic-gate 	}
846*0Sstevel@tonic-gate }
847*0Sstevel@tonic-gate 
848*0Sstevel@tonic-gate /* find this term, which can be a regular expression */
849*0Sstevel@tonic-gate 
850*0Sstevel@tonic-gate static void
findterm(void)851*0Sstevel@tonic-gate findterm(void)
852*0Sstevel@tonic-gate {
853*0Sstevel@tonic-gate 	char	*s;
854*0Sstevel@tonic-gate 	int	len;
855*0Sstevel@tonic-gate 	char	prefix[PATLEN + 1];
856*0Sstevel@tonic-gate 	char	term[PATLEN + 1];
857*0Sstevel@tonic-gate 
858*0Sstevel@tonic-gate 	npostings = 0;		/* will be non-zero after database built */
859*0Sstevel@tonic-gate 	lastfcnoffset = 0;	/* clear the last function name found */
860*0Sstevel@tonic-gate 	boolclear();		/* clear the posting set */
861*0Sstevel@tonic-gate 
862*0Sstevel@tonic-gate 	/* get the string prefix (if any) of the regular expression */
863*0Sstevel@tonic-gate 	(void) strcpy(prefix, pattern);
864*0Sstevel@tonic-gate 	if ((s = strpbrk(prefix, ".[{*+")) != NULL) {
865*0Sstevel@tonic-gate 		*s = '\0';
866*0Sstevel@tonic-gate 	}
867*0Sstevel@tonic-gate 	/* if letter case is to be ignored */
868*0Sstevel@tonic-gate 	if (caseless == YES) {
869*0Sstevel@tonic-gate 
870*0Sstevel@tonic-gate 		/*
871*0Sstevel@tonic-gate 		 * convert the prefix to upper case because it is lexically
872*0Sstevel@tonic-gate 		 * less than lower case
873*0Sstevel@tonic-gate 		 */
874*0Sstevel@tonic-gate 		s = prefix;
875*0Sstevel@tonic-gate 		while (*s != '\0') {
876*0Sstevel@tonic-gate 			*s = toupper(*s);
877*0Sstevel@tonic-gate 			++s;
878*0Sstevel@tonic-gate 		}
879*0Sstevel@tonic-gate 	}
880*0Sstevel@tonic-gate 	/* find the term lexically >= the prefix */
881*0Sstevel@tonic-gate 	(void) invfind(&invcontrol, prefix);
882*0Sstevel@tonic-gate 	if (caseless == YES) {	/* restore lower case */
883*0Sstevel@tonic-gate 		(void) strcpy(prefix, strtolower(prefix));
884*0Sstevel@tonic-gate 	}
885*0Sstevel@tonic-gate 	/*
886*0Sstevel@tonic-gate 	 * a null prefix matches the null term in the inverted index,
887*0Sstevel@tonic-gate 	 * so move to the first real term
888*0Sstevel@tonic-gate 	 */
889*0Sstevel@tonic-gate 	if (*prefix == '\0') {
890*0Sstevel@tonic-gate 		(void) invforward(&invcontrol);
891*0Sstevel@tonic-gate 	}
892*0Sstevel@tonic-gate 	len = strlen(prefix);
893*0Sstevel@tonic-gate 	do {
894*0Sstevel@tonic-gate 		(void) invterm(&invcontrol, term);	/* get the term */
895*0Sstevel@tonic-gate 		s = term;
896*0Sstevel@tonic-gate 		if (caseless == YES) {
897*0Sstevel@tonic-gate 			s = strtolower(s);	/* make it lower case */
898*0Sstevel@tonic-gate 		}
899*0Sstevel@tonic-gate 		/* if it matches */
900*0Sstevel@tonic-gate 		if (regex(regexp, s) != NULL) {
901*0Sstevel@tonic-gate 			/* add it's postings to the set */
902*0Sstevel@tonic-gate 			if ((postingp = boolfile(&invcontrol,
903*0Sstevel@tonic-gate 			    &npostings, OR)) == NULL) {
904*0Sstevel@tonic-gate 				break;
905*0Sstevel@tonic-gate 			}
906*0Sstevel@tonic-gate 		} else if (len > 0) {
907*0Sstevel@tonic-gate 			/* if there is a prefix */
908*0Sstevel@tonic-gate 
909*0Sstevel@tonic-gate 			/*
910*0Sstevel@tonic-gate 			 * if ignoring letter case and the term is out of the
911*0Sstevel@tonic-gate 			 * range of possible matches
912*0Sstevel@tonic-gate 			 */
913*0Sstevel@tonic-gate 			if (caseless == YES) {
914*0Sstevel@tonic-gate 				if (strncmp(term, prefix, len) > 0) {
915*0Sstevel@tonic-gate 					break;	/* stop searching */
916*0Sstevel@tonic-gate 				}
917*0Sstevel@tonic-gate 			}
918*0Sstevel@tonic-gate 			/* if using letter case and the prefix doesn't match */
919*0Sstevel@tonic-gate 			else if (strncmp(term, prefix, len) != 0) {
920*0Sstevel@tonic-gate 				break;	/* stop searching */
921*0Sstevel@tonic-gate 			}
922*0Sstevel@tonic-gate 		}
923*0Sstevel@tonic-gate 		/* display progress about every three seconds */
924*0Sstevel@tonic-gate 		if (++searchcount % 50 == 0) {
925*0Sstevel@tonic-gate 			progress("%ld of %ld symbols matched",
926*0Sstevel@tonic-gate 			    searchcount, totalterms);
927*0Sstevel@tonic-gate 		}
928*0Sstevel@tonic-gate 	} while (invforward(&invcontrol));	/* while didn't wrap around */
929*0Sstevel@tonic-gate 
930*0Sstevel@tonic-gate 	/* initialize the progress message for retrieving the references */
931*0Sstevel@tonic-gate 	initprogress();
932*0Sstevel@tonic-gate 	postingsfound = npostings;
933*0Sstevel@tonic-gate }
934*0Sstevel@tonic-gate 
935*0Sstevel@tonic-gate /* display the file search progress about every three seconds */
936*0Sstevel@tonic-gate 
937*0Sstevel@tonic-gate static void
fileprogress(void)938*0Sstevel@tonic-gate fileprogress(void)
939*0Sstevel@tonic-gate {
940*0Sstevel@tonic-gate 	if (++searchcount % 10 == 0) {
941*0Sstevel@tonic-gate 		progress("%ld of %ld files searched", searchcount,
942*0Sstevel@tonic-gate 		    (long)nsrcfiles);
943*0Sstevel@tonic-gate 	}
944*0Sstevel@tonic-gate }
945*0Sstevel@tonic-gate 
946*0Sstevel@tonic-gate /* initialize the progress message */
947*0Sstevel@tonic-gate 
948*0Sstevel@tonic-gate void
initprogress(void)949*0Sstevel@tonic-gate initprogress(void)
950*0Sstevel@tonic-gate {
951*0Sstevel@tonic-gate 	searchcount = 0;
952*0Sstevel@tonic-gate 	starttime = time((long *)NULL);
953*0Sstevel@tonic-gate }
954*0Sstevel@tonic-gate 
955*0Sstevel@tonic-gate /* display the progress every three seconds */
956*0Sstevel@tonic-gate 
957*0Sstevel@tonic-gate void
progress(char * format,long n1,long n2)958*0Sstevel@tonic-gate progress(char *format, long n1, long n2)
959*0Sstevel@tonic-gate {
960*0Sstevel@tonic-gate 	char	msg[MSGLEN + 1];
961*0Sstevel@tonic-gate 	long	now;
962*0Sstevel@tonic-gate 
963*0Sstevel@tonic-gate 	/* print after 2 seconds so the average is nearer 3 seconds */
964*0Sstevel@tonic-gate 	if (linemode == NO && (now = time((long *)NULL)) - starttime >= 2) {
965*0Sstevel@tonic-gate 		starttime = now;
966*0Sstevel@tonic-gate 		(void) sprintf(msg, format, n1, n2);
967*0Sstevel@tonic-gate 		putmsg(msg);
968*0Sstevel@tonic-gate 	}
969*0Sstevel@tonic-gate }
970*0Sstevel@tonic-gate 
971*0Sstevel@tonic-gate /* match the pattern to the string */
972*0Sstevel@tonic-gate 
973*0Sstevel@tonic-gate BOOL
match(void)974*0Sstevel@tonic-gate match(void)
975*0Sstevel@tonic-gate {
976*0Sstevel@tonic-gate 	char	string[PATLEN + 1];
977*0Sstevel@tonic-gate 	char	*s;
978*0Sstevel@tonic-gate 
979*0Sstevel@tonic-gate 	/* see if this is a regular expression pattern */
980*0Sstevel@tonic-gate 	if (regexp != NULL) {
981*0Sstevel@tonic-gate 		getstring(string);
982*0Sstevel@tonic-gate 		if (*string == '\0') {
983*0Sstevel@tonic-gate 			return (NO);
984*0Sstevel@tonic-gate 		}
985*0Sstevel@tonic-gate 		s = string;
986*0Sstevel@tonic-gate 		if (caseless == YES) {
987*0Sstevel@tonic-gate 			s = strtolower(s);
988*0Sstevel@tonic-gate 		}
989*0Sstevel@tonic-gate 		return (regex(regexp, s) ? YES : NO);
990*0Sstevel@tonic-gate 	}
991*0Sstevel@tonic-gate 	/* it is a string pattern */
992*0Sstevel@tonic-gate 	return ((BOOL)(*blockp == cpattern[0] && matchrest()));
993*0Sstevel@tonic-gate }
994*0Sstevel@tonic-gate 
995*0Sstevel@tonic-gate /* match the rest of the pattern to the name */
996*0Sstevel@tonic-gate 
997*0Sstevel@tonic-gate BOOL
matchrest(void)998*0Sstevel@tonic-gate matchrest(void)
999*0Sstevel@tonic-gate {
1000*0Sstevel@tonic-gate 	int	i = 1;
1001*0Sstevel@tonic-gate 
1002*0Sstevel@tonic-gate 	skiprefchar();
1003*0Sstevel@tonic-gate 	do {
1004*0Sstevel@tonic-gate 		while (*blockp == cpattern[i]) {
1005*0Sstevel@tonic-gate 			++blockp;
1006*0Sstevel@tonic-gate 			++i;
1007*0Sstevel@tonic-gate 		}
1008*0Sstevel@tonic-gate 	} while (*(blockp + 1) == '\0' && readblock() != NULL);
1009*0Sstevel@tonic-gate 
1010*0Sstevel@tonic-gate 	if (*blockp == '\n' && cpattern[i] == '\0') {
1011*0Sstevel@tonic-gate 		return (YES);
1012*0Sstevel@tonic-gate 	}
1013*0Sstevel@tonic-gate 	return (NO);
1014*0Sstevel@tonic-gate }
1015*0Sstevel@tonic-gate 
1016*0Sstevel@tonic-gate /* get the next posting for this term */
1017*0Sstevel@tonic-gate 
1018*0Sstevel@tonic-gate static POSTING *
getposting(void)1019*0Sstevel@tonic-gate getposting(void)
1020*0Sstevel@tonic-gate {
1021*0Sstevel@tonic-gate 	if (npostings-- <= 0) {
1022*0Sstevel@tonic-gate 		return (NULL);
1023*0Sstevel@tonic-gate 	}
1024*0Sstevel@tonic-gate 	/* display progress about every three seconds */
1025*0Sstevel@tonic-gate 	if (++searchcount % 100 == 0) {
1026*0Sstevel@tonic-gate 		progress("%ld of %ld possible references retrieved",
1027*0Sstevel@tonic-gate 		    searchcount, postingsfound);
1028*0Sstevel@tonic-gate 	}
1029*0Sstevel@tonic-gate 	return (postingp++);
1030*0Sstevel@tonic-gate }
1031*0Sstevel@tonic-gate 
1032*0Sstevel@tonic-gate /* put the posting reference into the file */
1033*0Sstevel@tonic-gate 
1034*0Sstevel@tonic-gate static void
putpostingref(POSTING * p)1035*0Sstevel@tonic-gate putpostingref(POSTING *p)
1036*0Sstevel@tonic-gate {
1037*0Sstevel@tonic-gate 	static	char	function[PATLEN + 1];	/* function name */
1038*0Sstevel@tonic-gate 
1039*0Sstevel@tonic-gate 	if (p->fcnoffset == 0) {
1040*0Sstevel@tonic-gate 		*function = '\0';
1041*0Sstevel@tonic-gate 	} else if (p->fcnoffset != lastfcnoffset) {
1042*0Sstevel@tonic-gate 		if (dbseek(p->fcnoffset) != -1) {
1043*0Sstevel@tonic-gate 			getstring(function);
1044*0Sstevel@tonic-gate 			lastfcnoffset = p->fcnoffset;
1045*0Sstevel@tonic-gate 		}
1046*0Sstevel@tonic-gate 	}
1047*0Sstevel@tonic-gate 	if (dbseek(p->lineoffset) != -1) {
1048*0Sstevel@tonic-gate 		putref(srcfiles[p->fileindex], function);
1049*0Sstevel@tonic-gate 	}
1050*0Sstevel@tonic-gate }
1051*0Sstevel@tonic-gate 
1052*0Sstevel@tonic-gate /* put the reference into the file */
1053*0Sstevel@tonic-gate 
1054*0Sstevel@tonic-gate static void
putref(char * file,char * function)1055*0Sstevel@tonic-gate putref(char *file, char *function)
1056*0Sstevel@tonic-gate {
1057*0Sstevel@tonic-gate 	FILE	*output;
1058*0Sstevel@tonic-gate 
1059*0Sstevel@tonic-gate 	/* put global references first */
1060*0Sstevel@tonic-gate 	if (*function == '\0') {
1061*0Sstevel@tonic-gate 		function = "<global>";
1062*0Sstevel@tonic-gate 		output = refsfound;
1063*0Sstevel@tonic-gate 	} else {
1064*0Sstevel@tonic-gate 		output = nonglobalrefs;
1065*0Sstevel@tonic-gate 	}
1066*0Sstevel@tonic-gate 	if (fprintf(output, "%s %s ", filepath(file), function) == EOF) {
1067*0Sstevel@tonic-gate 		cannotwrite(temp1);
1068*0Sstevel@tonic-gate 		/* NOTREACHED */
1069*0Sstevel@tonic-gate 	}
1070*0Sstevel@tonic-gate 	putsource(output);
1071*0Sstevel@tonic-gate }
1072*0Sstevel@tonic-gate 
1073*0Sstevel@tonic-gate /* put the source line into the file */
1074*0Sstevel@tonic-gate 
1075*0Sstevel@tonic-gate static void
putsource(FILE * output)1076*0Sstevel@tonic-gate putsource(FILE *output)
1077*0Sstevel@tonic-gate {
1078*0Sstevel@tonic-gate 	char	*cp, nextc = '\0';
1079*0Sstevel@tonic-gate 
1080*0Sstevel@tonic-gate 	if (fileversion <= 5) {
1081*0Sstevel@tonic-gate 		(void) scanpast(' ');
1082*0Sstevel@tonic-gate 		putline(output);
1083*0Sstevel@tonic-gate 		(void) putc('\n', output);
1084*0Sstevel@tonic-gate 		return;
1085*0Sstevel@tonic-gate 	}
1086*0Sstevel@tonic-gate 	/* scan back to the beginning of the source line */
1087*0Sstevel@tonic-gate 	cp = blockp;
1088*0Sstevel@tonic-gate 	while (*cp != '\n' || nextc != '\n') {
1089*0Sstevel@tonic-gate 		nextc = *cp;
1090*0Sstevel@tonic-gate 		if (--cp < block) {
1091*0Sstevel@tonic-gate 			/* read the previous block */
1092*0Sstevel@tonic-gate 			(void) dbseek((blocknumber - 1) * BUFSIZ);
1093*0Sstevel@tonic-gate 			cp = &block[BUFSIZ - 1];
1094*0Sstevel@tonic-gate 		}
1095*0Sstevel@tonic-gate 	}
1096*0Sstevel@tonic-gate 	/* there must be a double newline followed by a line number */
1097*0Sstevel@tonic-gate 	blockp = cp;
1098*0Sstevel@tonic-gate 	setmark(' ');	/* so getrefchar doesn't skip the last block char */
1099*0Sstevel@tonic-gate 	if (*blockp != '\n' || getrefchar() != '\n' ||
1100*0Sstevel@tonic-gate 	    !isdigit(getrefchar()) && fileversion >= 12) {
1101*0Sstevel@tonic-gate 		putmsg("Internal error: cannot get source line from database");
1102*0Sstevel@tonic-gate 		myexit(1);
1103*0Sstevel@tonic-gate 	}
1104*0Sstevel@tonic-gate 	/* until a double newline is found */
1105*0Sstevel@tonic-gate 	do {
1106*0Sstevel@tonic-gate 		/* skip a symbol type */
1107*0Sstevel@tonic-gate 		if (*blockp == '\t') {
1108*0Sstevel@tonic-gate 			skiprefchar();
1109*0Sstevel@tonic-gate 			skiprefchar();
1110*0Sstevel@tonic-gate 		}
1111*0Sstevel@tonic-gate 		/* output a piece of the source line */
1112*0Sstevel@tonic-gate 		putline(output);
1113*0Sstevel@tonic-gate 	} while (blockp != NULL && getrefchar() != '\n');
1114*0Sstevel@tonic-gate 	(void) putc('\n', output);
1115*0Sstevel@tonic-gate }
1116*0Sstevel@tonic-gate 
1117*0Sstevel@tonic-gate /* put the rest of the cross-reference line into the file */
1118*0Sstevel@tonic-gate 
1119*0Sstevel@tonic-gate static void
putline(FILE * output)1120*0Sstevel@tonic-gate putline(FILE *output)
1121*0Sstevel@tonic-gate {
1122*0Sstevel@tonic-gate 	char	*cp;
1123*0Sstevel@tonic-gate 	unsigned c;
1124*0Sstevel@tonic-gate 
1125*0Sstevel@tonic-gate 	setmark('\n');
1126*0Sstevel@tonic-gate 	cp = blockp;
1127*0Sstevel@tonic-gate 	do {
1128*0Sstevel@tonic-gate 		while ((c = *cp) != '\n') {
1129*0Sstevel@tonic-gate 			/* check for a compressed digraph */
1130*0Sstevel@tonic-gate 			if (c & 0200) {
1131*0Sstevel@tonic-gate 				c &= 0177;
1132*0Sstevel@tonic-gate 				(void) putc(dichar1[c / 8], output);
1133*0Sstevel@tonic-gate 				(void) putc(dichar2[c & 7], output);
1134*0Sstevel@tonic-gate 			} else if (c < ' ') {
1135*0Sstevel@tonic-gate 				/* a compressed keyword */
1136*0Sstevel@tonic-gate 				(void) fputs(keyword[c].text, output);
1137*0Sstevel@tonic-gate 				if (keyword[c].delim != '\0') {
1138*0Sstevel@tonic-gate 					(void) putc(' ', output);
1139*0Sstevel@tonic-gate 				}
1140*0Sstevel@tonic-gate 				if (keyword[c].delim == '(') {
1141*0Sstevel@tonic-gate 					(void) putc('(', output);
1142*0Sstevel@tonic-gate 				}
1143*0Sstevel@tonic-gate 			} else {
1144*0Sstevel@tonic-gate 				(void) putc((int)c, output);
1145*0Sstevel@tonic-gate 			}
1146*0Sstevel@tonic-gate 			++cp;
1147*0Sstevel@tonic-gate 		}
1148*0Sstevel@tonic-gate 	} while (*(cp + 1) == '\0' && (cp = readblock()) != NULL);
1149*0Sstevel@tonic-gate 	blockp = cp;
1150*0Sstevel@tonic-gate }
1151*0Sstevel@tonic-gate 
1152*0Sstevel@tonic-gate /* put the rest of the cross-reference line into the string */
1153*0Sstevel@tonic-gate 
1154*0Sstevel@tonic-gate void
getstring(char * s)1155*0Sstevel@tonic-gate getstring(char *s)
1156*0Sstevel@tonic-gate {
1157*0Sstevel@tonic-gate 	char	*cp;
1158*0Sstevel@tonic-gate 	unsigned c;
1159*0Sstevel@tonic-gate 
1160*0Sstevel@tonic-gate 	setmark('\n');
1161*0Sstevel@tonic-gate 	cp = blockp;
1162*0Sstevel@tonic-gate 	do {
1163*0Sstevel@tonic-gate 		while ((c = *cp) != '\n') {
1164*0Sstevel@tonic-gate 			if (c & 0200) {
1165*0Sstevel@tonic-gate 				c &= 0177;
1166*0Sstevel@tonic-gate 				*s++ = dichar1[c / 8];
1167*0Sstevel@tonic-gate 				*s++ = dichar2[c & 7];
1168*0Sstevel@tonic-gate 			} else {
1169*0Sstevel@tonic-gate 				*s++ = (char)c;
1170*0Sstevel@tonic-gate 			}
1171*0Sstevel@tonic-gate 			++cp;
1172*0Sstevel@tonic-gate 		}
1173*0Sstevel@tonic-gate 	} while (*(cp + 1) == '\0' && (cp = readblock()) != NULL);
1174*0Sstevel@tonic-gate 	blockp = cp;
1175*0Sstevel@tonic-gate 	*s = '\0';
1176*0Sstevel@tonic-gate }
1177*0Sstevel@tonic-gate 
1178*0Sstevel@tonic-gate /* scan past the next occurence of this character in the cross-reference */
1179*0Sstevel@tonic-gate 
1180*0Sstevel@tonic-gate char *
scanpast(int c)1181*0Sstevel@tonic-gate scanpast(int c)
1182*0Sstevel@tonic-gate {
1183*0Sstevel@tonic-gate 	char	*cp;
1184*0Sstevel@tonic-gate 
1185*0Sstevel@tonic-gate 	setmark(c);
1186*0Sstevel@tonic-gate 	cp = blockp;
1187*0Sstevel@tonic-gate 	do {	/* innermost loop optimized to only one test */
1188*0Sstevel@tonic-gate 		while (*cp != c) {
1189*0Sstevel@tonic-gate 			++cp;
1190*0Sstevel@tonic-gate 		}
1191*0Sstevel@tonic-gate 	} while (*(cp + 1) == '\0' && (cp = readblock()) != NULL);
1192*0Sstevel@tonic-gate 	blockp = cp;
1193*0Sstevel@tonic-gate 	if (cp != NULL) {
1194*0Sstevel@tonic-gate 		skiprefchar();	/* skip the found character */
1195*0Sstevel@tonic-gate 	}
1196*0Sstevel@tonic-gate 	return (blockp);
1197*0Sstevel@tonic-gate }
1198*0Sstevel@tonic-gate 
1199*0Sstevel@tonic-gate /* read a block of the cross-reference */
1200*0Sstevel@tonic-gate 
1201*0Sstevel@tonic-gate char *
readblock(void)1202*0Sstevel@tonic-gate readblock(void)
1203*0Sstevel@tonic-gate {
1204*0Sstevel@tonic-gate 	/* read the next block */
1205*0Sstevel@tonic-gate 	blocklen = read(symrefs, block, BUFSIZ);
1206*0Sstevel@tonic-gate 	blockp = block;
1207*0Sstevel@tonic-gate 
1208*0Sstevel@tonic-gate 	/* add the search character and end-of-block mark */
1209*0Sstevel@tonic-gate 	block[blocklen] = blockmark;
1210*0Sstevel@tonic-gate 	block[blocklen + 1] = '\0';
1211*0Sstevel@tonic-gate 
1212*0Sstevel@tonic-gate 	/* return NULL on end-of-file */
1213*0Sstevel@tonic-gate 	if (blocklen == 0) {
1214*0Sstevel@tonic-gate 		blockp = NULL;
1215*0Sstevel@tonic-gate 	} else {
1216*0Sstevel@tonic-gate 		++blocknumber;
1217*0Sstevel@tonic-gate 	}
1218*0Sstevel@tonic-gate 	return (blockp);
1219*0Sstevel@tonic-gate }
1220*0Sstevel@tonic-gate 
1221*0Sstevel@tonic-gate /* seek to the database offset */
1222*0Sstevel@tonic-gate 
1223*0Sstevel@tonic-gate long
dbseek(long offset)1224*0Sstevel@tonic-gate dbseek(long offset)
1225*0Sstevel@tonic-gate {
1226*0Sstevel@tonic-gate 	long	n;
1227*0Sstevel@tonic-gate 	int	rc = 0;
1228*0Sstevel@tonic-gate 
1229*0Sstevel@tonic-gate 	if ((n = offset / BUFSIZ) != blocknumber) {
1230*0Sstevel@tonic-gate 		if ((rc = lseek(symrefs, n * BUFSIZ, 0)) == -1) {
1231*0Sstevel@tonic-gate 			myperror("Lseek failed");
1232*0Sstevel@tonic-gate 			(void) sleep(3);
1233*0Sstevel@tonic-gate 			return (rc);
1234*0Sstevel@tonic-gate 		}
1235*0Sstevel@tonic-gate 		(void) readblock();
1236*0Sstevel@tonic-gate 		blocknumber = n;
1237*0Sstevel@tonic-gate 	}
1238*0Sstevel@tonic-gate 	blockp = block + offset % BUFSIZ;
1239*0Sstevel@tonic-gate 	return (rc);
1240*0Sstevel@tonic-gate }
1241*0Sstevel@tonic-gate 
1242*0Sstevel@tonic-gate /* convert the string to lower case */
1243*0Sstevel@tonic-gate 
1244*0Sstevel@tonic-gate static char *
strtolower(char * s)1245*0Sstevel@tonic-gate strtolower(char *s)
1246*0Sstevel@tonic-gate {
1247*0Sstevel@tonic-gate 	static char buf[PATLEN + 1];
1248*0Sstevel@tonic-gate 	char *lp = buf;
1249*0Sstevel@tonic-gate 
1250*0Sstevel@tonic-gate 	while (*s != '\0') {
1251*0Sstevel@tonic-gate 		/*
1252*0Sstevel@tonic-gate 		 * note: s in not incremented in this line because the BSD
1253*0Sstevel@tonic-gate 		 * compatibility tolower macro evaluates its argument twice
1254*0Sstevel@tonic-gate 		 */
1255*0Sstevel@tonic-gate 		*lp++ = tolower(*s);
1256*0Sstevel@tonic-gate 		++s;
1257*0Sstevel@tonic-gate 	}
1258*0Sstevel@tonic-gate 	*lp = '\0';
1259*0Sstevel@tonic-gate 	return (buf);
1260*0Sstevel@tonic-gate }
1261*0Sstevel@tonic-gate 
1262*0Sstevel@tonic-gate /* if needed, convert a relative path to a full path */
1263*0Sstevel@tonic-gate 
1264*0Sstevel@tonic-gate static char *
filepath(char * file)1265*0Sstevel@tonic-gate filepath(char *file)
1266*0Sstevel@tonic-gate {
1267*0Sstevel@tonic-gate 	static	char	path[PATHLEN + 1];
1268*0Sstevel@tonic-gate 	int	i;
1269*0Sstevel@tonic-gate 
1270*0Sstevel@tonic-gate 	if (*file != '/') {
1271*0Sstevel@tonic-gate 
1272*0Sstevel@tonic-gate 		/* if same file as last time, return the same path */
1273*0Sstevel@tonic-gate 		if (strequal(file, lastfilepath)) {
1274*0Sstevel@tonic-gate 			return (path);
1275*0Sstevel@tonic-gate 		}
1276*0Sstevel@tonic-gate 		(void) strcpy(lastfilepath, file);
1277*0Sstevel@tonic-gate 
1278*0Sstevel@tonic-gate 		/* if requested, prepend a path to a relative file path */
1279*0Sstevel@tonic-gate 		if (prependpath != NULL) {
1280*0Sstevel@tonic-gate 			(void) sprintf(path, "%s/%s", prependpath, file);
1281*0Sstevel@tonic-gate 			return (path);
1282*0Sstevel@tonic-gate 		}
1283*0Sstevel@tonic-gate 		/*
1284*0Sstevel@tonic-gate 		 * if the database was built with a view path, return a
1285*0Sstevel@tonic-gate 		 * full path so "cscope -d -f" does not have to be called
1286*0Sstevel@tonic-gate 		 * from the build directory with the same view path
1287*0Sstevel@tonic-gate 		 */
1288*0Sstevel@tonic-gate 		if (dbvpndirs > 1) {
1289*0Sstevel@tonic-gate 			for (i = 0; i < dbvpndirs; i++) {
1290*0Sstevel@tonic-gate 				(void) sprintf(path,
1291*0Sstevel@tonic-gate 				    "%s/%s", dbvpdirs[i], file);
1292*0Sstevel@tonic-gate 				if (access(path, READ) != -1) {
1293*0Sstevel@tonic-gate 					return (path);
1294*0Sstevel@tonic-gate 				}
1295*0Sstevel@tonic-gate 			}
1296*0Sstevel@tonic-gate 		}
1297*0Sstevel@tonic-gate 		(void) strcpy(path, file);	/* for lastfilepath check */
1298*0Sstevel@tonic-gate 	}
1299*0Sstevel@tonic-gate 	return (file);
1300*0Sstevel@tonic-gate }
1301