xref: /onnv-gate/usr/src/tools/cscope-fast/display.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 cross-reference
35*0Sstevel@tonic-gate  *
36*0Sstevel@tonic-gate  *	display functions
37*0Sstevel@tonic-gate  */
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate #include "global.h"
40*0Sstevel@tonic-gate #include "version.h"	/* FILEVERSION and FIXVERSION */
41*0Sstevel@tonic-gate #include <curses.h>	/* COLS and LINES */
42*0Sstevel@tonic-gate #include <setjmp.h>	/* jmp_buf */
43*0Sstevel@tonic-gate #include <string.h>
44*0Sstevel@tonic-gate #include <errno.h>
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate /* see if the function column should be displayed */
47*0Sstevel@tonic-gate #define	displayfcn()	(field <= ASSIGN)
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate #define	MINCOLS	68	/* minimum columns for 3 digit Lines message numbers */
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate int	*displine;		/* screen line of displayed reference */
52*0Sstevel@tonic-gate int	disprefs;		/* displayed references */
53*0Sstevel@tonic-gate int	field;			/* input field */
54*0Sstevel@tonic-gate unsigned fldcolumn;		/* input field column */
55*0Sstevel@tonic-gate int	mdisprefs;		/* maximum displayed references */
56*0Sstevel@tonic-gate int	selectlen;		/* selection number field length */
57*0Sstevel@tonic-gate int	nextline;		/* next line to be shown */
58*0Sstevel@tonic-gate int	topline = 1;		/* top line of page */
59*0Sstevel@tonic-gate int	bottomline;		/* bottom line of page */
60*0Sstevel@tonic-gate int	totallines;		/* total reference lines */
61*0Sstevel@tonic-gate FILE	*refsfound;		/* references found file */
62*0Sstevel@tonic-gate FILE	*nonglobalrefs;		/* non-global references file */
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate static	int	fldline;		/* input field line */
65*0Sstevel@tonic-gate static	int	subsystemlen;		/* OGS subsystem name display */
66*0Sstevel@tonic-gate 					/* field length */
67*0Sstevel@tonic-gate static	int	booklen;		/* OGS book name display field length */
68*0Sstevel@tonic-gate static	int	filelen;		/* file name display field length */
69*0Sstevel@tonic-gate static	int	fcnlen;			/* function name display field length */
70*0Sstevel@tonic-gate static	jmp_buf	env;			/* setjmp/longjmp buffer */
71*0Sstevel@tonic-gate static	int	lastdispline;		/* last displayed reference line */
72*0Sstevel@tonic-gate static	char	lastmsg[MSGLEN + 1];	/* last message displayed */
73*0Sstevel@tonic-gate static	int	numlen;			/* line number display field length */
74*0Sstevel@tonic-gate static	char	depthstring[] = "Depth: ";
75*0Sstevel@tonic-gate static	char	helpstring[] = "Press the ? key for help";
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate typedef char *(*FP)();	/* pointer to function returning a character pointer */
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate static	struct	{
81*0Sstevel@tonic-gate 	char	*text1;
82*0Sstevel@tonic-gate 	char	*text2;
83*0Sstevel@tonic-gate 	FP	findfcn;
84*0Sstevel@tonic-gate 	enum {
85*0Sstevel@tonic-gate 		EGREP,
86*0Sstevel@tonic-gate 		REGCMP
87*0Sstevel@tonic-gate 	} patterntype;
88*0Sstevel@tonic-gate } fields[FIELDS + 1] = {
89*0Sstevel@tonic-gate 	/* last search is not part of the cscope display */
90*0Sstevel@tonic-gate 	{ "Find this", "C symbol",
91*0Sstevel@tonic-gate 	    (FP) findsymbol, REGCMP},
92*0Sstevel@tonic-gate 	{ "Find this", "definition",
93*0Sstevel@tonic-gate 	    (FP) finddef, REGCMP},
94*0Sstevel@tonic-gate 	{ "Find", "functions called by this function",
95*0Sstevel@tonic-gate 	    (FP) findcalledby, REGCMP},
96*0Sstevel@tonic-gate 	{ "Find", "functions calling this function",
97*0Sstevel@tonic-gate 	    (FP) findcalling, REGCMP},
98*0Sstevel@tonic-gate 	{ "Find", "assignments to",
99*0Sstevel@tonic-gate 	    (FP) findassignments, REGCMP},
100*0Sstevel@tonic-gate 	{ "Change this", "grep pattern",
101*0Sstevel@tonic-gate 	    findgreppat, EGREP},
102*0Sstevel@tonic-gate 	{ "Find this", "egrep pattern",
103*0Sstevel@tonic-gate 	    findegreppat, EGREP},
104*0Sstevel@tonic-gate 	{ "Find this", "file",
105*0Sstevel@tonic-gate 	    (FP) findfile, REGCMP},
106*0Sstevel@tonic-gate 	{ "Find", "files #including this file",
107*0Sstevel@tonic-gate 	    (FP) findinclude, REGCMP},
108*0Sstevel@tonic-gate 	{ "Find all", "function/class definitions",
109*0Sstevel@tonic-gate 	    (FP) findallfcns, REGCMP},
110*0Sstevel@tonic-gate };
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate /* initialize display parameters */
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate void
dispinit(void)115*0Sstevel@tonic-gate dispinit(void)
116*0Sstevel@tonic-gate {
117*0Sstevel@tonic-gate 	/* calculate the maximum displayed reference lines */
118*0Sstevel@tonic-gate 	lastdispline = FLDLINE - 2;
119*0Sstevel@tonic-gate 	mdisprefs = lastdispline - REFLINE + 1;
120*0Sstevel@tonic-gate 	if (mdisprefs <= 0) {
121*0Sstevel@tonic-gate 		(void) printw("cscope: window must be at least %d lines high",
122*0Sstevel@tonic-gate 		    FIELDS + 6);
123*0Sstevel@tonic-gate 		myexit(1);
124*0Sstevel@tonic-gate 	}
125*0Sstevel@tonic-gate 	if (COLS < MINCOLS) {
126*0Sstevel@tonic-gate 		(void) printw("cscope: window must be at least %d columns wide",
127*0Sstevel@tonic-gate 		    MINCOLS);
128*0Sstevel@tonic-gate 		myexit(1);
129*0Sstevel@tonic-gate 	}
130*0Sstevel@tonic-gate 	if (!mouse) {
131*0Sstevel@tonic-gate 		if (returnrequired == NO && mdisprefs > 9) {
132*0Sstevel@tonic-gate 			mdisprefs = 9;	/* single digit selection number */
133*0Sstevel@tonic-gate 		}
134*0Sstevel@tonic-gate 		/* calculate the maximum selection number width */
135*0Sstevel@tonic-gate 		(void) sprintf(newpat, "%d", mdisprefs);
136*0Sstevel@tonic-gate 		selectlen = strlen(newpat);
137*0Sstevel@tonic-gate 	}
138*0Sstevel@tonic-gate 	/* allocate the displayed line array */
139*0Sstevel@tonic-gate 	displine = (int *)mymalloc(mdisprefs * sizeof (int));
140*0Sstevel@tonic-gate }
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate /* display a page of the references */
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate void
display(void)145*0Sstevel@tonic-gate display(void)
146*0Sstevel@tonic-gate {
147*0Sstevel@tonic-gate 	char	*subsystem;		/* OGS subsystem name */
148*0Sstevel@tonic-gate 	char	*book;			/* OGS book name */
149*0Sstevel@tonic-gate 	char	file[PATHLEN + 1];	/* file name */
150*0Sstevel@tonic-gate 	char	function[PATLEN + 1];	/* function name */
151*0Sstevel@tonic-gate 	char	linenum[NUMLEN + 1];	/* line number */
152*0Sstevel@tonic-gate 	int	screenline;		/* screen line number */
153*0Sstevel@tonic-gate 	int	width;			/* source line display width */
154*0Sstevel@tonic-gate 	int	i;
155*0Sstevel@tonic-gate 	char	*s;
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate 	(void) erase();
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate 	/* if there are no references */
160*0Sstevel@tonic-gate 	if (totallines == 0) {
161*0Sstevel@tonic-gate 		if (*lastmsg != '\0') {
162*0Sstevel@tonic-gate 			(void) addstr(lastmsg);	/* redisplay any message */
163*0Sstevel@tonic-gate 		} else {
164*0Sstevel@tonic-gate 			(void) printw("Cscope version %d%s", FILEVERSION,
165*0Sstevel@tonic-gate 			    FIXVERSION);
166*0Sstevel@tonic-gate 			(void) move(0, COLS - (int)sizeof (helpstring));
167*0Sstevel@tonic-gate 			(void) addstr(helpstring);
168*0Sstevel@tonic-gate 		}
169*0Sstevel@tonic-gate 	} else {	/* display the pattern */
170*0Sstevel@tonic-gate 		if (changing == YES) {
171*0Sstevel@tonic-gate 			(void) printw("Change \"%s\" to \"%s\"",
172*0Sstevel@tonic-gate 			    pattern, newpat);
173*0Sstevel@tonic-gate 		} else {
174*0Sstevel@tonic-gate 			(void) printw("%c%s: %s",
175*0Sstevel@tonic-gate 			    toupper(fields[field].text2[0]),
176*0Sstevel@tonic-gate 			    fields[field].text2 + 1, pattern);
177*0Sstevel@tonic-gate 		}
178*0Sstevel@tonic-gate 		/* display the cscope invocation nesting depth */
179*0Sstevel@tonic-gate 		if (cscopedepth > 1) {
180*0Sstevel@tonic-gate 			(void) move(0, COLS - (int)sizeof (depthstring) - 2);
181*0Sstevel@tonic-gate 			(void) addstr(depthstring);
182*0Sstevel@tonic-gate 			(void) printw("%d", cscopedepth);
183*0Sstevel@tonic-gate 		}
184*0Sstevel@tonic-gate 		/* display the column headings */
185*0Sstevel@tonic-gate 		(void) move(2, selectlen + 1);
186*0Sstevel@tonic-gate 		if (ogs == YES && field != FILENAME) {
187*0Sstevel@tonic-gate 			(void) printw("%-*s ", subsystemlen, "Subsystem");
188*0Sstevel@tonic-gate 			(void) printw("%-*s ", booklen, "Book");
189*0Sstevel@tonic-gate 		}
190*0Sstevel@tonic-gate 		if (dispcomponents > 0) {
191*0Sstevel@tonic-gate 			(void) printw("%-*s ", filelen, "File");
192*0Sstevel@tonic-gate 		}
193*0Sstevel@tonic-gate 		if (displayfcn()) {
194*0Sstevel@tonic-gate 			(void) printw("%-*s ", fcnlen, "Function");
195*0Sstevel@tonic-gate 		}
196*0Sstevel@tonic-gate 		if (field != FILENAME) {
197*0Sstevel@tonic-gate 			(void) addstr("Line");
198*0Sstevel@tonic-gate 		}
199*0Sstevel@tonic-gate 		(void) addch('\n');
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 		/* if at end of file go back to beginning */
202*0Sstevel@tonic-gate 		if (nextline > totallines) {
203*0Sstevel@tonic-gate 			seekline(1);
204*0Sstevel@tonic-gate 		}
205*0Sstevel@tonic-gate 		/* calculate the source text column */
206*0Sstevel@tonic-gate 		width = COLS - selectlen - numlen - 2;
207*0Sstevel@tonic-gate 		if (ogs == YES) {
208*0Sstevel@tonic-gate 			width -= subsystemlen + booklen + 2;
209*0Sstevel@tonic-gate 		}
210*0Sstevel@tonic-gate 		if (dispcomponents > 0) {
211*0Sstevel@tonic-gate 			width -= filelen + 1;
212*0Sstevel@tonic-gate 		}
213*0Sstevel@tonic-gate 		if (displayfcn()) {
214*0Sstevel@tonic-gate 			width -= fcnlen + 1;
215*0Sstevel@tonic-gate 		}
216*0Sstevel@tonic-gate 		/*
217*0Sstevel@tonic-gate 		 * until the max references have been displayed or
218*0Sstevel@tonic-gate 		 * there is no more room
219*0Sstevel@tonic-gate 		 */
220*0Sstevel@tonic-gate 		topline = nextline;
221*0Sstevel@tonic-gate 		for (disprefs = 0, screenline = REFLINE;
222*0Sstevel@tonic-gate 		    disprefs < mdisprefs && screenline <= lastdispline;
223*0Sstevel@tonic-gate 		    ++disprefs, ++screenline) {
224*0Sstevel@tonic-gate 			/* read the reference line */
225*0Sstevel@tonic-gate 			if (fscanf(refsfound, "%s%s%s %[^\n]", file, function,
226*0Sstevel@tonic-gate 			    linenum, yytext) < 4) {
227*0Sstevel@tonic-gate 				break;
228*0Sstevel@tonic-gate 			}
229*0Sstevel@tonic-gate 			++nextline;
230*0Sstevel@tonic-gate 			displine[disprefs] = screenline;
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate 			/* if no mouse, display the selection number */
233*0Sstevel@tonic-gate 			if (!mouse) {
234*0Sstevel@tonic-gate 				(void) printw("%*d", selectlen, disprefs + 1);
235*0Sstevel@tonic-gate 			}
236*0Sstevel@tonic-gate 			/* display any change mark */
237*0Sstevel@tonic-gate 			if (changing == YES &&
238*0Sstevel@tonic-gate 			    change[topline + disprefs - 1] == YES) {
239*0Sstevel@tonic-gate 				(void) addch('>');
240*0Sstevel@tonic-gate 			} else {
241*0Sstevel@tonic-gate 				(void) addch(' ');
242*0Sstevel@tonic-gate 			}
243*0Sstevel@tonic-gate 			/* display the file name */
244*0Sstevel@tonic-gate 			if (field == FILENAME) {
245*0Sstevel@tonic-gate 				(void) printw("%-.*s\n", COLS - 3, file);
246*0Sstevel@tonic-gate 				continue;
247*0Sstevel@tonic-gate 			}
248*0Sstevel@tonic-gate 			/* if OGS, display the subsystem and book names */
249*0Sstevel@tonic-gate 			if (ogs == YES) {
250*0Sstevel@tonic-gate 				ogsnames(file, &subsystem, &book);
251*0Sstevel@tonic-gate 				(void) printw("%-*.*s ", subsystemlen,
252*0Sstevel@tonic-gate 				    subsystemlen, subsystem);
253*0Sstevel@tonic-gate 				(void) printw("%-*.*s ", booklen, booklen,
254*0Sstevel@tonic-gate 				    book);
255*0Sstevel@tonic-gate 			}
256*0Sstevel@tonic-gate 			/* display the requested path components */
257*0Sstevel@tonic-gate 			if (dispcomponents > 0) {
258*0Sstevel@tonic-gate 				(void) printw("%-*.*s ", filelen, filelen,
259*0Sstevel@tonic-gate 				    pathcomponents(file, dispcomponents));
260*0Sstevel@tonic-gate 			}
261*0Sstevel@tonic-gate 			/* display the function name */
262*0Sstevel@tonic-gate 			if (displayfcn()) {
263*0Sstevel@tonic-gate 				(void) printw("%-*.*s ", fcnlen, fcnlen,
264*0Sstevel@tonic-gate 				    function);
265*0Sstevel@tonic-gate 			}
266*0Sstevel@tonic-gate 			/* display the line number */
267*0Sstevel@tonic-gate 			(void) printw("%*s ", numlen, linenum);
268*0Sstevel@tonic-gate 
269*0Sstevel@tonic-gate 			/* there may be tabs in egrep output */
270*0Sstevel@tonic-gate 			while ((s = strchr(yytext, '\t')) != NULL) {
271*0Sstevel@tonic-gate 				*s = ' ';
272*0Sstevel@tonic-gate 			}
273*0Sstevel@tonic-gate 			/* display the source line */
274*0Sstevel@tonic-gate 			s = yytext;
275*0Sstevel@tonic-gate 			for (;;) {
276*0Sstevel@tonic-gate 				/* see if the source line will fit */
277*0Sstevel@tonic-gate 				if ((i = strlen(s)) > width) {
278*0Sstevel@tonic-gate 					/* find the nearest blank */
279*0Sstevel@tonic-gate 					for (i = width; s[i] != ' ' && i > 0;
280*0Sstevel@tonic-gate 					    --i) {
281*0Sstevel@tonic-gate 					}
282*0Sstevel@tonic-gate 					if (i == 0) {
283*0Sstevel@tonic-gate 						i = width;	/* no blank */
284*0Sstevel@tonic-gate 					}
285*0Sstevel@tonic-gate 				}
286*0Sstevel@tonic-gate 				/* print up to this point */
287*0Sstevel@tonic-gate 				(void) printw("%.*s", i, s);
288*0Sstevel@tonic-gate 				s += i;
289*0Sstevel@tonic-gate 
290*0Sstevel@tonic-gate 				/* if line didn't wrap around */
291*0Sstevel@tonic-gate 				if (i < width) {
292*0Sstevel@tonic-gate 					/* go to next line */
293*0Sstevel@tonic-gate 					(void) addch('\n');
294*0Sstevel@tonic-gate 				}
295*0Sstevel@tonic-gate 				/* skip blanks */
296*0Sstevel@tonic-gate 				while (*s == ' ') {
297*0Sstevel@tonic-gate 					++s;
298*0Sstevel@tonic-gate 				}
299*0Sstevel@tonic-gate 				/* see if there is more text */
300*0Sstevel@tonic-gate 				if (*s == '\0') {
301*0Sstevel@tonic-gate 					break;
302*0Sstevel@tonic-gate 				}
303*0Sstevel@tonic-gate 				/* if the source line is too long */
304*0Sstevel@tonic-gate 				if (++screenline > lastdispline) {
305*0Sstevel@tonic-gate 					/*
306*0Sstevel@tonic-gate 					 * if this is the first displayed line,
307*0Sstevel@tonic-gate 					 * display what will fit on the screen
308*0Sstevel@tonic-gate 					 */
309*0Sstevel@tonic-gate 					if (topline == nextline - 1) {
310*0Sstevel@tonic-gate 						goto endrefs;
311*0Sstevel@tonic-gate 					}
312*0Sstevel@tonic-gate 					/* erase the reference */
313*0Sstevel@tonic-gate 					while (--screenline >=
314*0Sstevel@tonic-gate 					    displine[disprefs]) {
315*0Sstevel@tonic-gate 						(void) move(screenline, 0);
316*0Sstevel@tonic-gate 						(void) clrtoeol();
317*0Sstevel@tonic-gate 					}
318*0Sstevel@tonic-gate 					++screenline;
319*0Sstevel@tonic-gate 
320*0Sstevel@tonic-gate 					/*
321*0Sstevel@tonic-gate 					 * go back to the beginning of this
322*0Sstevel@tonic-gate 					 * reference
323*0Sstevel@tonic-gate 					 */
324*0Sstevel@tonic-gate 					--nextline;
325*0Sstevel@tonic-gate 					seekline(nextline);
326*0Sstevel@tonic-gate 					goto endrefs;
327*0Sstevel@tonic-gate 				}
328*0Sstevel@tonic-gate 				/* indent the continued source line */
329*0Sstevel@tonic-gate 				(void) move(screenline, COLS - width);
330*0Sstevel@tonic-gate 			}
331*0Sstevel@tonic-gate 
332*0Sstevel@tonic-gate 		}
333*0Sstevel@tonic-gate 	endrefs:
334*0Sstevel@tonic-gate 		/* check for more references */
335*0Sstevel@tonic-gate 		bottomline = nextline;
336*0Sstevel@tonic-gate 		if (bottomline - topline < totallines) {
337*0Sstevel@tonic-gate 			(void) move(FLDLINE - 1, 0);
338*0Sstevel@tonic-gate 			(void) standout();
339*0Sstevel@tonic-gate 			(void) printw("%*s", selectlen + 1, "");
340*0Sstevel@tonic-gate 			if (bottomline - 1 == topline) {
341*0Sstevel@tonic-gate 				(void) printw("Line %d", topline);
342*0Sstevel@tonic-gate 			} else {
343*0Sstevel@tonic-gate 				(void) printw("Lines %d-%d", topline,
344*0Sstevel@tonic-gate 				    bottomline - 1);
345*0Sstevel@tonic-gate 			}
346*0Sstevel@tonic-gate 			(void) printw(" of %d, press the space bar to "
347*0Sstevel@tonic-gate 			    "display next lines", totallines);
348*0Sstevel@tonic-gate 			(void) standend();
349*0Sstevel@tonic-gate 		}
350*0Sstevel@tonic-gate 	}
351*0Sstevel@tonic-gate 	/* display the input fields */
352*0Sstevel@tonic-gate 	(void) move(FLDLINE, 0);
353*0Sstevel@tonic-gate 	for (i = 0; i < FIELDS; ++i) {
354*0Sstevel@tonic-gate 		(void) printw("%s %s:\n", fields[i].text1, fields[i].text2);
355*0Sstevel@tonic-gate 	}
356*0Sstevel@tonic-gate 	drawscrollbar(topline, nextline, totallines);
357*0Sstevel@tonic-gate }
358*0Sstevel@tonic-gate 
359*0Sstevel@tonic-gate /* set the cursor position for the field */
360*0Sstevel@tonic-gate void
setfield(void)361*0Sstevel@tonic-gate setfield(void)
362*0Sstevel@tonic-gate {
363*0Sstevel@tonic-gate 	fldline = FLDLINE + field;
364*0Sstevel@tonic-gate 	fldcolumn = strlen(fields[field].text1) +
365*0Sstevel@tonic-gate 	    strlen(fields[field].text2) + 3;
366*0Sstevel@tonic-gate }
367*0Sstevel@tonic-gate 
368*0Sstevel@tonic-gate /* move to the current input field */
369*0Sstevel@tonic-gate 
370*0Sstevel@tonic-gate void
atfield(void)371*0Sstevel@tonic-gate atfield(void)
372*0Sstevel@tonic-gate {
373*0Sstevel@tonic-gate 	(void) move(fldline, (int)fldcolumn);
374*0Sstevel@tonic-gate }
375*0Sstevel@tonic-gate 
376*0Sstevel@tonic-gate /* search for the symbol or text pattern */
377*0Sstevel@tonic-gate 
378*0Sstevel@tonic-gate /*ARGSUSED*/
379*0Sstevel@tonic-gate SIGTYPE
jumpback(int sig)380*0Sstevel@tonic-gate jumpback(int sig)
381*0Sstevel@tonic-gate {
382*0Sstevel@tonic-gate 	longjmp(env, 1);
383*0Sstevel@tonic-gate }
384*0Sstevel@tonic-gate 
385*0Sstevel@tonic-gate BOOL
search(void)386*0Sstevel@tonic-gate search(void)
387*0Sstevel@tonic-gate {
388*0Sstevel@tonic-gate 	char	*egreperror = NULL;	/* egrep error message */
389*0Sstevel@tonic-gate 	FINDINIT rc = NOERROR;		/* findinit return code */
390*0Sstevel@tonic-gate 	SIGTYPE	(*savesig)();		/* old value of signal */
391*0Sstevel@tonic-gate 	FP	f;			/* searching function */
392*0Sstevel@tonic-gate 	char	*s;
393*0Sstevel@tonic-gate 	int	c;
394*0Sstevel@tonic-gate 
395*0Sstevel@tonic-gate 	/* note: the pattern may have been a cscope argument */
396*0Sstevel@tonic-gate 	if (caseless == YES) {
397*0Sstevel@tonic-gate 		for (s = pattern; *s != '\0'; ++s) {
398*0Sstevel@tonic-gate 			*s = tolower(*s);
399*0Sstevel@tonic-gate 		}
400*0Sstevel@tonic-gate 	}
401*0Sstevel@tonic-gate 	/* open the references found file for writing */
402*0Sstevel@tonic-gate 	if (writerefsfound() == NO) {
403*0Sstevel@tonic-gate 		return (NO);
404*0Sstevel@tonic-gate 	}
405*0Sstevel@tonic-gate 	/* find the pattern - stop on an interrupt */
406*0Sstevel@tonic-gate 	if (linemode == NO) {
407*0Sstevel@tonic-gate 		putmsg("Searching");
408*0Sstevel@tonic-gate 	}
409*0Sstevel@tonic-gate 	initprogress();
410*0Sstevel@tonic-gate 	if (setjmp(env) == 0) {
411*0Sstevel@tonic-gate 		savesig = signal(SIGINT, jumpback);
412*0Sstevel@tonic-gate 		f = fields[field].findfcn;
413*0Sstevel@tonic-gate 		if (fields[field].patterntype == EGREP) {
414*0Sstevel@tonic-gate 			egreperror = (*f)(pattern);
415*0Sstevel@tonic-gate 		} else {
416*0Sstevel@tonic-gate 			if ((nonglobalrefs = fopen(temp2, "w")) == NULL) {
417*0Sstevel@tonic-gate 				cannotopen(temp2);
418*0Sstevel@tonic-gate 				return (NO);
419*0Sstevel@tonic-gate 			}
420*0Sstevel@tonic-gate 			if ((rc = findinit()) == NOERROR) {
421*0Sstevel@tonic-gate 				(void) dbseek(0L); /* goto the first block */
422*0Sstevel@tonic-gate 				(*f)();
423*0Sstevel@tonic-gate 				findcleanup();
424*0Sstevel@tonic-gate 
425*0Sstevel@tonic-gate 				/* append the non-global references */
426*0Sstevel@tonic-gate 				(void) freopen(temp2, "r", nonglobalrefs);
427*0Sstevel@tonic-gate 				while ((c = getc(nonglobalrefs)) != EOF) {
428*0Sstevel@tonic-gate 					(void) putc(c, refsfound);
429*0Sstevel@tonic-gate 				}
430*0Sstevel@tonic-gate 			}
431*0Sstevel@tonic-gate 			(void) fclose(nonglobalrefs);
432*0Sstevel@tonic-gate 		}
433*0Sstevel@tonic-gate 	}
434*0Sstevel@tonic-gate 	(void) signal(SIGINT, savesig);
435*0Sstevel@tonic-gate 	/* reopen the references found file for reading */
436*0Sstevel@tonic-gate 	(void) freopen(temp1, "r", refsfound);
437*0Sstevel@tonic-gate 	nextline = 1;
438*0Sstevel@tonic-gate 	totallines = 0;
439*0Sstevel@tonic-gate 
440*0Sstevel@tonic-gate 	/* see if it is empty */
441*0Sstevel@tonic-gate 	if ((c = getc(refsfound)) == EOF) {
442*0Sstevel@tonic-gate 		if (egreperror != NULL) {
443*0Sstevel@tonic-gate 			(void) sprintf(lastmsg, "Egrep %s in this pattern: %s",
444*0Sstevel@tonic-gate 			    egreperror, pattern);
445*0Sstevel@tonic-gate 		} else if (rc == NOTSYMBOL) {
446*0Sstevel@tonic-gate 			(void) sprintf(lastmsg, "This is not a C symbol: %s",
447*0Sstevel@tonic-gate 			    pattern);
448*0Sstevel@tonic-gate 		} else if (rc == REGCMPERROR) {
449*0Sstevel@tonic-gate 			(void) sprintf(lastmsg,
450*0Sstevel@tonic-gate 			    "Error in this regcmp(3X) regular expression: %s",
451*0Sstevel@tonic-gate 			    pattern);
452*0Sstevel@tonic-gate 		} else {
453*0Sstevel@tonic-gate 			(void) sprintf(lastmsg, "Could not find the %s: %s",
454*0Sstevel@tonic-gate 			    fields[field].text2, pattern);
455*0Sstevel@tonic-gate 		}
456*0Sstevel@tonic-gate 		return (NO);
457*0Sstevel@tonic-gate 	}
458*0Sstevel@tonic-gate 	/* put back the character read */
459*0Sstevel@tonic-gate 	(void) ungetc(c, refsfound);
460*0Sstevel@tonic-gate 
461*0Sstevel@tonic-gate 	countrefs();
462*0Sstevel@tonic-gate 	return (YES);
463*0Sstevel@tonic-gate }
464*0Sstevel@tonic-gate 
465*0Sstevel@tonic-gate /* open the references found file for writing */
466*0Sstevel@tonic-gate 
467*0Sstevel@tonic-gate BOOL
writerefsfound(void)468*0Sstevel@tonic-gate writerefsfound(void)
469*0Sstevel@tonic-gate {
470*0Sstevel@tonic-gate 	if (refsfound == NULL) {
471*0Sstevel@tonic-gate 		if ((refsfound = fopen(temp1, "w")) == NULL) {
472*0Sstevel@tonic-gate 			cannotopen(temp1);
473*0Sstevel@tonic-gate 			return (NO);
474*0Sstevel@tonic-gate 		}
475*0Sstevel@tonic-gate 	} else if (freopen(temp1, "w", refsfound) == NULL) {
476*0Sstevel@tonic-gate 		putmsg("Cannot reopen temporary file");
477*0Sstevel@tonic-gate 		return (NO);
478*0Sstevel@tonic-gate 	}
479*0Sstevel@tonic-gate 	return (YES);
480*0Sstevel@tonic-gate }
481*0Sstevel@tonic-gate 
482*0Sstevel@tonic-gate /* count the references found */
483*0Sstevel@tonic-gate 
484*0Sstevel@tonic-gate void
countrefs(void)485*0Sstevel@tonic-gate countrefs(void)
486*0Sstevel@tonic-gate {
487*0Sstevel@tonic-gate 	char	*subsystem;		/* OGS subsystem name */
488*0Sstevel@tonic-gate 	char 	*book;			/* OGS book name */
489*0Sstevel@tonic-gate 	char	file[PATHLEN + 1];	/* file name */
490*0Sstevel@tonic-gate 	char	function[PATLEN + 1];	/* function name */
491*0Sstevel@tonic-gate 	char	linenum[NUMLEN + 1];	/* line number */
492*0Sstevel@tonic-gate 	int	i;
493*0Sstevel@tonic-gate 
494*0Sstevel@tonic-gate 	/*
495*0Sstevel@tonic-gate 	 * count the references found and find the length of the file,
496*0Sstevel@tonic-gate 	 * function, and line number display fields
497*0Sstevel@tonic-gate 	 */
498*0Sstevel@tonic-gate 	subsystemlen = 9;	/* strlen("Subsystem") */
499*0Sstevel@tonic-gate 	booklen = 4;		/* strlen("Book") */
500*0Sstevel@tonic-gate 	filelen = 4;		/* strlen("File") */
501*0Sstevel@tonic-gate 	fcnlen = 8;		/* strlen("Function") */
502*0Sstevel@tonic-gate 	numlen = 0;
503*0Sstevel@tonic-gate 	while ((i = fscanf(refsfound, "%250s%250s%6s %5000[^\n]", file,
504*0Sstevel@tonic-gate 	    function, linenum, yytext)) != EOF) {
505*0Sstevel@tonic-gate 		if (i != 4 || !isgraph(*file) ||
506*0Sstevel@tonic-gate 		    !isgraph(*function) || !isdigit(*linenum)) {
507*0Sstevel@tonic-gate 			putmsg("File does not have expected format");
508*0Sstevel@tonic-gate 			totallines = 0;
509*0Sstevel@tonic-gate 			return;
510*0Sstevel@tonic-gate 		}
511*0Sstevel@tonic-gate 		if ((i = strlen(pathcomponents(file,
512*0Sstevel@tonic-gate 		    dispcomponents))) > filelen) {
513*0Sstevel@tonic-gate 			filelen = i;
514*0Sstevel@tonic-gate 		}
515*0Sstevel@tonic-gate 		if (ogs == YES) {
516*0Sstevel@tonic-gate 			ogsnames(file, &subsystem, &book);
517*0Sstevel@tonic-gate 			if ((i = strlen(subsystem)) > subsystemlen) {
518*0Sstevel@tonic-gate 				subsystemlen = i;
519*0Sstevel@tonic-gate 			}
520*0Sstevel@tonic-gate 			if ((i = strlen(book)) > booklen) {
521*0Sstevel@tonic-gate 				booklen = i;
522*0Sstevel@tonic-gate 			}
523*0Sstevel@tonic-gate 		}
524*0Sstevel@tonic-gate 		if ((i = strlen(function)) > fcnlen) {
525*0Sstevel@tonic-gate 			fcnlen = i;
526*0Sstevel@tonic-gate 		}
527*0Sstevel@tonic-gate 		if ((i = strlen(linenum)) > numlen) {
528*0Sstevel@tonic-gate 			numlen = i;
529*0Sstevel@tonic-gate 		}
530*0Sstevel@tonic-gate 		++totallines;
531*0Sstevel@tonic-gate 	}
532*0Sstevel@tonic-gate 	rewind(refsfound);
533*0Sstevel@tonic-gate 
534*0Sstevel@tonic-gate 	/* restrict the width of displayed columns */
535*0Sstevel@tonic-gate 	i = (COLS - 5) / 3;
536*0Sstevel@tonic-gate 	if (ogs == YES) {
537*0Sstevel@tonic-gate 		i = (COLS - 7) / 5;
538*0Sstevel@tonic-gate 	}
539*0Sstevel@tonic-gate 	if (filelen > i && i > 4) {
540*0Sstevel@tonic-gate 		filelen = i;
541*0Sstevel@tonic-gate 	}
542*0Sstevel@tonic-gate 	if (subsystemlen > i && i > 9) {
543*0Sstevel@tonic-gate 		subsystemlen = i;
544*0Sstevel@tonic-gate 	}
545*0Sstevel@tonic-gate 	if (booklen > i && i > 4) {
546*0Sstevel@tonic-gate 		booklen = i;
547*0Sstevel@tonic-gate 	}
548*0Sstevel@tonic-gate 	if (fcnlen > i && i > 8) {
549*0Sstevel@tonic-gate 		fcnlen = i;
550*0Sstevel@tonic-gate 	}
551*0Sstevel@tonic-gate }
552*0Sstevel@tonic-gate 
553*0Sstevel@tonic-gate /* print error message on system call failure */
554*0Sstevel@tonic-gate 
555*0Sstevel@tonic-gate void
myperror(char * text)556*0Sstevel@tonic-gate myperror(char *text)
557*0Sstevel@tonic-gate {
558*0Sstevel@tonic-gate 	char	msg[MSGLEN + 1];	/* message */
559*0Sstevel@tonic-gate 
560*0Sstevel@tonic-gate 	(void) sprintf(msg, "%s: %s", text, strerror(errno));
561*0Sstevel@tonic-gate 	putmsg(msg);
562*0Sstevel@tonic-gate }
563*0Sstevel@tonic-gate 
564*0Sstevel@tonic-gate /* putmsg clears the message line and prints the message */
565*0Sstevel@tonic-gate 
566*0Sstevel@tonic-gate void
putmsg(char * msg)567*0Sstevel@tonic-gate putmsg(char *msg)
568*0Sstevel@tonic-gate {
569*0Sstevel@tonic-gate 	if (incurses == NO) {
570*0Sstevel@tonic-gate 		*msg = tolower(*msg);
571*0Sstevel@tonic-gate 		(void) fprintf(stderr, "cscope: %s\n", msg);
572*0Sstevel@tonic-gate 	} else {
573*0Sstevel@tonic-gate 		(void) move(MSGLINE, 0);
574*0Sstevel@tonic-gate 		(void) clrtoeol();
575*0Sstevel@tonic-gate 		(void) addstr(msg);
576*0Sstevel@tonic-gate 		(void) refresh();
577*0Sstevel@tonic-gate 	}
578*0Sstevel@tonic-gate 	(void) strncpy(lastmsg, msg, sizeof (lastmsg) - 1);
579*0Sstevel@tonic-gate }
580*0Sstevel@tonic-gate 
581*0Sstevel@tonic-gate /* clearmsg2 clears the second message line */
582*0Sstevel@tonic-gate 
583*0Sstevel@tonic-gate void
clearmsg2(void)584*0Sstevel@tonic-gate clearmsg2(void)
585*0Sstevel@tonic-gate {
586*0Sstevel@tonic-gate 	if (incurses == YES) {
587*0Sstevel@tonic-gate 		(void) move(MSGLINE + 1, 0);
588*0Sstevel@tonic-gate 		(void) clrtoeol();
589*0Sstevel@tonic-gate 	}
590*0Sstevel@tonic-gate }
591*0Sstevel@tonic-gate 
592*0Sstevel@tonic-gate /* putmsg2 clears the second message line and prints the message */
593*0Sstevel@tonic-gate 
594*0Sstevel@tonic-gate void
putmsg2(char * msg)595*0Sstevel@tonic-gate putmsg2(char *msg)
596*0Sstevel@tonic-gate {
597*0Sstevel@tonic-gate 	if (incurses == NO) {
598*0Sstevel@tonic-gate 		putmsg(msg);
599*0Sstevel@tonic-gate 	} else {
600*0Sstevel@tonic-gate 		clearmsg2();
601*0Sstevel@tonic-gate 		(void) addstr(msg);
602*0Sstevel@tonic-gate 		(void) refresh();
603*0Sstevel@tonic-gate 	}
604*0Sstevel@tonic-gate }
605*0Sstevel@tonic-gate 
606*0Sstevel@tonic-gate /* position the references found file at the specified line */
607*0Sstevel@tonic-gate 
608*0Sstevel@tonic-gate void
seekline(int line)609*0Sstevel@tonic-gate seekline(int line)
610*0Sstevel@tonic-gate {
611*0Sstevel@tonic-gate 	int	c;
612*0Sstevel@tonic-gate 
613*0Sstevel@tonic-gate 	/* verify that there is a references found file */
614*0Sstevel@tonic-gate 	if (refsfound == NULL) {
615*0Sstevel@tonic-gate 		return;
616*0Sstevel@tonic-gate 	}
617*0Sstevel@tonic-gate 	/* go to the beginning of the file */
618*0Sstevel@tonic-gate 	rewind(refsfound);
619*0Sstevel@tonic-gate 
620*0Sstevel@tonic-gate 	/* find the requested line */
621*0Sstevel@tonic-gate 	nextline = 1;
622*0Sstevel@tonic-gate 	while (nextline < line && (c = getc(refsfound)) != EOF) {
623*0Sstevel@tonic-gate 		if (c == '\n') {
624*0Sstevel@tonic-gate 			nextline++;
625*0Sstevel@tonic-gate 		}
626*0Sstevel@tonic-gate 	}
627*0Sstevel@tonic-gate }
628*0Sstevel@tonic-gate 
629*0Sstevel@tonic-gate /* get the OGS subsystem and book names */
630*0Sstevel@tonic-gate 
631*0Sstevel@tonic-gate void
ogsnames(char * file,char ** subsystem,char ** book)632*0Sstevel@tonic-gate ogsnames(char *file, char **subsystem, char **book)
633*0Sstevel@tonic-gate {
634*0Sstevel@tonic-gate 	static	char	buf[PATHLEN + 1];
635*0Sstevel@tonic-gate 	char	*s, *slash;
636*0Sstevel@tonic-gate 
637*0Sstevel@tonic-gate 	*subsystem = *book = "";
638*0Sstevel@tonic-gate 	(void) strcpy(buf, file);
639*0Sstevel@tonic-gate 	s = buf;
640*0Sstevel@tonic-gate 	if (*s == '/') {
641*0Sstevel@tonic-gate 		++s;
642*0Sstevel@tonic-gate 	}
643*0Sstevel@tonic-gate 	while ((slash = strchr(s, '/')) != NULL) {
644*0Sstevel@tonic-gate 		*slash = '\0';
645*0Sstevel@tonic-gate 		if ((int)strlen(s) >= 3 && strncmp(slash - 3, ".ss", 3) == 0) {
646*0Sstevel@tonic-gate 			*subsystem = s;
647*0Sstevel@tonic-gate 			s = slash + 1;
648*0Sstevel@tonic-gate 			if ((slash = strchr(s, '/')) != NULL) {
649*0Sstevel@tonic-gate 				*book = s;
650*0Sstevel@tonic-gate 				*slash = '\0';
651*0Sstevel@tonic-gate 			}
652*0Sstevel@tonic-gate 			break;
653*0Sstevel@tonic-gate 		}
654*0Sstevel@tonic-gate 		s = slash + 1;
655*0Sstevel@tonic-gate 	}
656*0Sstevel@tonic-gate }
657*0Sstevel@tonic-gate 
658*0Sstevel@tonic-gate /* get the requested path components */
659*0Sstevel@tonic-gate 
660*0Sstevel@tonic-gate char *
pathcomponents(char * path,int components)661*0Sstevel@tonic-gate pathcomponents(char *path, int components)
662*0Sstevel@tonic-gate {
663*0Sstevel@tonic-gate 	int	i;
664*0Sstevel@tonic-gate 	char	*s;
665*0Sstevel@tonic-gate 
666*0Sstevel@tonic-gate 	s = path + strlen(path) - 1;
667*0Sstevel@tonic-gate 	for (i = 0; i < components; ++i) {
668*0Sstevel@tonic-gate 		while (s > path && *--s != '/') {
669*0Sstevel@tonic-gate 			;
670*0Sstevel@tonic-gate 		}
671*0Sstevel@tonic-gate 	}
672*0Sstevel@tonic-gate 	if (s > path && *s == '/') {
673*0Sstevel@tonic-gate 		++s;
674*0Sstevel@tonic-gate 	}
675*0Sstevel@tonic-gate 	return (s);
676*0Sstevel@tonic-gate }
677