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