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 cross-reference
33*7c478bd9Sstevel@tonic-gate  *
34*7c478bd9Sstevel@tonic-gate  *	build cross-reference file
35*7c478bd9Sstevel@tonic-gate  */
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #include "global.h"
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate /* convert long to a string */
40*7c478bd9Sstevel@tonic-gate #define	ltobase(value)	n = value; \
41*7c478bd9Sstevel@tonic-gate 			s = buf + (sizeof (buf) - 1); \
42*7c478bd9Sstevel@tonic-gate 			*s = '\0'; \
43*7c478bd9Sstevel@tonic-gate 			digits = 1; \
44*7c478bd9Sstevel@tonic-gate 			while (n >= BASE) { \
45*7c478bd9Sstevel@tonic-gate 				++digits; \
46*7c478bd9Sstevel@tonic-gate 				i = n; \
47*7c478bd9Sstevel@tonic-gate 				n /= BASE; \
48*7c478bd9Sstevel@tonic-gate 				*--s = i - n * BASE + '!'; \
49*7c478bd9Sstevel@tonic-gate 			} \
50*7c478bd9Sstevel@tonic-gate 			*--s = n + '!';
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate #define	SYMBOLINC	20	/* symbol list size increment */
53*7c478bd9Sstevel@tonic-gate #define	FREAD	"r"		/* fopen for reading */
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate long	dboffset;		/* new database offset */
56*7c478bd9Sstevel@tonic-gate BOOL	errorsfound;		/* prompt before clearing messages */
57*7c478bd9Sstevel@tonic-gate long	fileindex;		/* source file name index */
58*7c478bd9Sstevel@tonic-gate long	lineoffset;		/* source line database offset */
59*7c478bd9Sstevel@tonic-gate long	npostings;		/* number of postings */
60*7c478bd9Sstevel@tonic-gate int	nsrcoffset;		/* number of file name database offsets */
61*7c478bd9Sstevel@tonic-gate long	*srcoffset;		/* source file name database offsets */
62*7c478bd9Sstevel@tonic-gate int	symbols;		/* number of symbols */
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate static	char	*filename;	/* file name for warning messages */
65*7c478bd9Sstevel@tonic-gate static	long	fcnoffset;	/* function name database offset */
66*7c478bd9Sstevel@tonic-gate static	long	macrooffset;	/* macro name database offset */
67*7c478bd9Sstevel@tonic-gate static	int	msymbols = SYMBOLINC;	/* maximum number of symbols */
68*7c478bd9Sstevel@tonic-gate static	struct	symbol {	/* symbol data */
69*7c478bd9Sstevel@tonic-gate 	int	type;		/* type */
70*7c478bd9Sstevel@tonic-gate 	int	first;		/* index of first character in text */
71*7c478bd9Sstevel@tonic-gate 	int	last;		/* index of last+1 character in text */
72*7c478bd9Sstevel@tonic-gate 	int	length;		/* symbol length */
73*7c478bd9Sstevel@tonic-gate } *symbol;
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate static void putcrossref(void);
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate void
crossref(char * srcfile)78*7c478bd9Sstevel@tonic-gate crossref(char *srcfile)
79*7c478bd9Sstevel@tonic-gate {
80*7c478bd9Sstevel@tonic-gate 	int	i;
81*7c478bd9Sstevel@tonic-gate 	int	length;		/* symbol length */
82*7c478bd9Sstevel@tonic-gate 	int	token;			/* current token */
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 	/* open the source file */
85*7c478bd9Sstevel@tonic-gate 	if ((yyin = vpfopen(srcfile, FREAD)) == NULL) {
86*7c478bd9Sstevel@tonic-gate 		cannotopen(srcfile);
87*7c478bd9Sstevel@tonic-gate 		errorsfound = YES;
88*7c478bd9Sstevel@tonic-gate 		return;
89*7c478bd9Sstevel@tonic-gate 	}
90*7c478bd9Sstevel@tonic-gate 	filename = srcfile;	/* save the file name for warning messages */
91*7c478bd9Sstevel@tonic-gate 	putfilename(srcfile);	/* output the file name */
92*7c478bd9Sstevel@tonic-gate 	dbputc('\n');
93*7c478bd9Sstevel@tonic-gate 	dbputc('\n');
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate 	/* read the source file */
96*7c478bd9Sstevel@tonic-gate 	initscanner(srcfile);
97*7c478bd9Sstevel@tonic-gate 	fcnoffset = macrooffset = 0;
98*7c478bd9Sstevel@tonic-gate 	symbols = 0;
99*7c478bd9Sstevel@tonic-gate 	if (symbol == NULL) {
100*7c478bd9Sstevel@tonic-gate 		symbol = mymalloc(msymbols * sizeof (struct symbol));
101*7c478bd9Sstevel@tonic-gate 	}
102*7c478bd9Sstevel@tonic-gate 	for (;;) {
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 		/* get the next token */
105*7c478bd9Sstevel@tonic-gate 		switch (token = yylex()) {
106*7c478bd9Sstevel@tonic-gate 		default:
107*7c478bd9Sstevel@tonic-gate 			/* if requested, truncate C symbols */
108*7c478bd9Sstevel@tonic-gate 			length = last - first;
109*7c478bd9Sstevel@tonic-gate 			if (truncatesyms && length > 8 &&
110*7c478bd9Sstevel@tonic-gate 			    token != INCLUDE && token != NEWFILE) {
111*7c478bd9Sstevel@tonic-gate 				length = 8;
112*7c478bd9Sstevel@tonic-gate 				last = first + 8;
113*7c478bd9Sstevel@tonic-gate 			}
114*7c478bd9Sstevel@tonic-gate 			/* see if the token has a symbol */
115*7c478bd9Sstevel@tonic-gate 			if (length == 0) {
116*7c478bd9Sstevel@tonic-gate 				savesymbol(token);
117*7c478bd9Sstevel@tonic-gate 				break;
118*7c478bd9Sstevel@tonic-gate 			}
119*7c478bd9Sstevel@tonic-gate 			/* see if the symbol is already in the list */
120*7c478bd9Sstevel@tonic-gate 			for (i = 0; i < symbols; ++i) {
121*7c478bd9Sstevel@tonic-gate 				if (length == symbol[i].length &&
122*7c478bd9Sstevel@tonic-gate 				    strncmp(yytext + first, yytext +
123*7c478bd9Sstevel@tonic-gate 					symbol[i].first, length) == 0 &&
124*7c478bd9Sstevel@tonic-gate 				    (token == IDENT ||
125*7c478bd9Sstevel@tonic-gate 					token == symbol[i].type)) {
126*7c478bd9Sstevel@tonic-gate 					first = yyleng;
127*7c478bd9Sstevel@tonic-gate 					break;
128*7c478bd9Sstevel@tonic-gate 				}
129*7c478bd9Sstevel@tonic-gate 			}
130*7c478bd9Sstevel@tonic-gate 			if (i == symbols) {	/* if not already in list */
131*7c478bd9Sstevel@tonic-gate 				savesymbol(token);
132*7c478bd9Sstevel@tonic-gate 			}
133*7c478bd9Sstevel@tonic-gate 			break;
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 		case NEWLINE:	/* end of line containing symbols */
136*7c478bd9Sstevel@tonic-gate 			--yyleng;	/* remove the newline */
137*7c478bd9Sstevel@tonic-gate 			putcrossref();	/* output the symbols and source line */
138*7c478bd9Sstevel@tonic-gate 			lineno = yylineno; /* save the symbol line number */
139*7c478bd9Sstevel@tonic-gate 			break;
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 		case LEXEOF:	/* end of file; last line may not have \n */
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate 			/*
144*7c478bd9Sstevel@tonic-gate 			 * if there were symbols, output them and the
145*7c478bd9Sstevel@tonic-gate 			 * source line
146*7c478bd9Sstevel@tonic-gate 			 */
147*7c478bd9Sstevel@tonic-gate 			if (symbols > 0) {
148*7c478bd9Sstevel@tonic-gate 				putcrossref();
149*7c478bd9Sstevel@tonic-gate 			}
150*7c478bd9Sstevel@tonic-gate 			(void) fclose(yyin);	/* close the source file */
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 			/* output the leading tab expected by the next call */
153*7c478bd9Sstevel@tonic-gate 			dbputc('\t');
154*7c478bd9Sstevel@tonic-gate 			return;
155*7c478bd9Sstevel@tonic-gate 		}
156*7c478bd9Sstevel@tonic-gate 	}
157*7c478bd9Sstevel@tonic-gate }
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate /* save the symbol in the list */
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate void
savesymbol(int token)162*7c478bd9Sstevel@tonic-gate savesymbol(int token)
163*7c478bd9Sstevel@tonic-gate {
164*7c478bd9Sstevel@tonic-gate 	/* make sure there is room for the symbol */
165*7c478bd9Sstevel@tonic-gate 	if (symbols == msymbols) {
166*7c478bd9Sstevel@tonic-gate 		msymbols += SYMBOLINC;
167*7c478bd9Sstevel@tonic-gate 		symbol = (struct symbol *)myrealloc(symbol,
168*7c478bd9Sstevel@tonic-gate 		    msymbols * sizeof (struct symbol));
169*7c478bd9Sstevel@tonic-gate 	}
170*7c478bd9Sstevel@tonic-gate 	/* save the symbol */
171*7c478bd9Sstevel@tonic-gate 	symbol[symbols].type = token;
172*7c478bd9Sstevel@tonic-gate 	symbol[symbols].first = first;
173*7c478bd9Sstevel@tonic-gate 	symbol[symbols].last = last;
174*7c478bd9Sstevel@tonic-gate 	symbol[symbols].length = last - first;
175*7c478bd9Sstevel@tonic-gate 	++symbols;
176*7c478bd9Sstevel@tonic-gate 	first = yyleng;
177*7c478bd9Sstevel@tonic-gate }
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate /* output the file name */
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate void
putfilename(char * srcfile)182*7c478bd9Sstevel@tonic-gate putfilename(char *srcfile)
183*7c478bd9Sstevel@tonic-gate {
184*7c478bd9Sstevel@tonic-gate 	/* check for file system out of space */
185*7c478bd9Sstevel@tonic-gate 	/* note: dbputc is not used to avoid lint complaint */
186*7c478bd9Sstevel@tonic-gate 	if (putc(NEWFILE, newrefs) == EOF) {
187*7c478bd9Sstevel@tonic-gate 		cannotwrite(newreffile);
188*7c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
189*7c478bd9Sstevel@tonic-gate 	}
190*7c478bd9Sstevel@tonic-gate 	++dboffset;
191*7c478bd9Sstevel@tonic-gate 	if (invertedindex) {
192*7c478bd9Sstevel@tonic-gate 		srcoffset[nsrcoffset++] = dboffset;
193*7c478bd9Sstevel@tonic-gate 	}
194*7c478bd9Sstevel@tonic-gate 	dbfputs(srcfile);
195*7c478bd9Sstevel@tonic-gate 	fcnoffset = macrooffset = 0;
196*7c478bd9Sstevel@tonic-gate }
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate /* output the symbols and source line */
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate static void
putcrossref(void)201*7c478bd9Sstevel@tonic-gate putcrossref(void)
202*7c478bd9Sstevel@tonic-gate {
203*7c478bd9Sstevel@tonic-gate 	int	i, j;
204*7c478bd9Sstevel@tonic-gate 	unsigned c;
205*7c478bd9Sstevel@tonic-gate 	BOOL	blank = NO;	/* output blank */
206*7c478bd9Sstevel@tonic-gate 	BOOL	newline = NO;	/* output newline */
207*7c478bd9Sstevel@tonic-gate 	int	symput = 0;	/* symbols output */
208*7c478bd9Sstevel@tonic-gate 	int	type;
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate 	/* output the source line */
211*7c478bd9Sstevel@tonic-gate 	lineoffset = dboffset;
212*7c478bd9Sstevel@tonic-gate 	dbfprintf(newrefs, "%d ", lineno);
213*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < yyleng; ++i) {
214*7c478bd9Sstevel@tonic-gate 
215*7c478bd9Sstevel@tonic-gate 		/* change a tab to a blank and compress blanks */
216*7c478bd9Sstevel@tonic-gate 		if ((c = yytext[i]) == ' ' || c == '\t') {
217*7c478bd9Sstevel@tonic-gate 			blank = YES;
218*7c478bd9Sstevel@tonic-gate 		}
219*7c478bd9Sstevel@tonic-gate 		/* look for the start of a symbol */
220*7c478bd9Sstevel@tonic-gate 		else if (symput < symbols && i == symbol[symput].first) {
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate 			/* check for compressed blanks */
223*7c478bd9Sstevel@tonic-gate 			if (blank) {
224*7c478bd9Sstevel@tonic-gate 				blank = NO;
225*7c478bd9Sstevel@tonic-gate 				if (newline) {
226*7c478bd9Sstevel@tonic-gate 					dbputc('\n');
227*7c478bd9Sstevel@tonic-gate 				}
228*7c478bd9Sstevel@tonic-gate 				dbputc(' ');
229*7c478bd9Sstevel@tonic-gate 			}
230*7c478bd9Sstevel@tonic-gate 			dbputc('\n');	/* symbols start on a new line */
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate 			/* output any symbol type */
233*7c478bd9Sstevel@tonic-gate 			if ((type = symbol[symput].type) != IDENT) {
234*7c478bd9Sstevel@tonic-gate 				dbputc('\t');
235*7c478bd9Sstevel@tonic-gate 				dbputc(type);
236*7c478bd9Sstevel@tonic-gate 			} else {
237*7c478bd9Sstevel@tonic-gate 				type = ' ';
238*7c478bd9Sstevel@tonic-gate 			}
239*7c478bd9Sstevel@tonic-gate 			/* output the symbol */
240*7c478bd9Sstevel@tonic-gate 			j = symbol[symput].last;
241*7c478bd9Sstevel@tonic-gate 			c = yytext[j];
242*7c478bd9Sstevel@tonic-gate 			yytext[j] = '\0';
243*7c478bd9Sstevel@tonic-gate 			if (invertedindex) {
244*7c478bd9Sstevel@tonic-gate 				putposting(yytext + i, type);
245*7c478bd9Sstevel@tonic-gate 			}
246*7c478bd9Sstevel@tonic-gate 			putstring(yytext + i);
247*7c478bd9Sstevel@tonic-gate 			newline = YES;
248*7c478bd9Sstevel@tonic-gate 			yytext[j] = (char)c;
249*7c478bd9Sstevel@tonic-gate 			i = j - 1;
250*7c478bd9Sstevel@tonic-gate 			++symput;
251*7c478bd9Sstevel@tonic-gate 		} else {
252*7c478bd9Sstevel@tonic-gate 			if (newline) {
253*7c478bd9Sstevel@tonic-gate 				newline = NO;
254*7c478bd9Sstevel@tonic-gate 				dbputc('\n');
255*7c478bd9Sstevel@tonic-gate 			}
256*7c478bd9Sstevel@tonic-gate 			/* check for compressed blanks */
257*7c478bd9Sstevel@tonic-gate 			if (blank) {
258*7c478bd9Sstevel@tonic-gate 				if (dicode2[c]) {
259*7c478bd9Sstevel@tonic-gate 					c = (0200 - 2) + dicode1[' '] +
260*7c478bd9Sstevel@tonic-gate 					    dicode2[c];
261*7c478bd9Sstevel@tonic-gate 				} else {
262*7c478bd9Sstevel@tonic-gate 					dbputc(' ');
263*7c478bd9Sstevel@tonic-gate 				}
264*7c478bd9Sstevel@tonic-gate 			} else if (dicode1[c] &&
265*7c478bd9Sstevel@tonic-gate 			    (j = dicode2[(unsigned)yytext[i + 1]]) != 0 &&
266*7c478bd9Sstevel@tonic-gate 			    symput < symbols && i + 1 != symbol[symput].first) {
267*7c478bd9Sstevel@tonic-gate 				/* compress digraphs */
268*7c478bd9Sstevel@tonic-gate 				c = (0200 - 2) + dicode1[c] + j;
269*7c478bd9Sstevel@tonic-gate 				++i;
270*7c478bd9Sstevel@tonic-gate 			}
271*7c478bd9Sstevel@tonic-gate 			/*
272*7c478bd9Sstevel@tonic-gate 			 * if the last line of the file is a '}' without a
273*7c478bd9Sstevel@tonic-gate 			 * newline, the lex EOF code overwrites it with a 0
274*7c478bd9Sstevel@tonic-gate 			 */
275*7c478bd9Sstevel@tonic-gate 			if (c) {
276*7c478bd9Sstevel@tonic-gate 				dbputc((int)c);
277*7c478bd9Sstevel@tonic-gate 			} else {
278*7c478bd9Sstevel@tonic-gate 				dbputc(' ');
279*7c478bd9Sstevel@tonic-gate 			}
280*7c478bd9Sstevel@tonic-gate 			blank = NO;
281*7c478bd9Sstevel@tonic-gate 
282*7c478bd9Sstevel@tonic-gate 			/* skip compressed characters */
283*7c478bd9Sstevel@tonic-gate 			if (c < ' ') {
284*7c478bd9Sstevel@tonic-gate 				++i;
285*7c478bd9Sstevel@tonic-gate 
286*7c478bd9Sstevel@tonic-gate 				/* skip blanks before a preprocesor keyword */
287*7c478bd9Sstevel@tonic-gate 				/*
288*7c478bd9Sstevel@tonic-gate 				 * note: don't use isspace() because \f and \v
289*7c478bd9Sstevel@tonic-gate 				 * are used for keywords
290*7c478bd9Sstevel@tonic-gate 				 */
291*7c478bd9Sstevel@tonic-gate 				while ((j = yytext[i]) == ' ' || j == '\t') {
292*7c478bd9Sstevel@tonic-gate 					++i;
293*7c478bd9Sstevel@tonic-gate 				}
294*7c478bd9Sstevel@tonic-gate 				/* skip the rest of the keyword */
295*7c478bd9Sstevel@tonic-gate 				while (isalpha(yytext[i])) {
296*7c478bd9Sstevel@tonic-gate 					++i;
297*7c478bd9Sstevel@tonic-gate 				}
298*7c478bd9Sstevel@tonic-gate 				/* skip space after certain keywords */
299*7c478bd9Sstevel@tonic-gate 				if (keyword[c].delim != '\0') {
300*7c478bd9Sstevel@tonic-gate 					while ((j = yytext[i]) == ' ' ||
301*7c478bd9Sstevel@tonic-gate 					    j == '\t') {
302*7c478bd9Sstevel@tonic-gate 						++i;
303*7c478bd9Sstevel@tonic-gate 					}
304*7c478bd9Sstevel@tonic-gate 				}
305*7c478bd9Sstevel@tonic-gate 				/* skip a '(' after certain keywords */
306*7c478bd9Sstevel@tonic-gate 				if (keyword[c].delim == '(' &&
307*7c478bd9Sstevel@tonic-gate 				    yytext[i] == '(') {
308*7c478bd9Sstevel@tonic-gate 					++i;
309*7c478bd9Sstevel@tonic-gate 				}
310*7c478bd9Sstevel@tonic-gate 				--i;	/* compensate for ++i in for() */
311*7c478bd9Sstevel@tonic-gate 			}
312*7c478bd9Sstevel@tonic-gate 		}
313*7c478bd9Sstevel@tonic-gate 	}
314*7c478bd9Sstevel@tonic-gate 	/* ignore trailing blanks */
315*7c478bd9Sstevel@tonic-gate 	dbputc('\n');
316*7c478bd9Sstevel@tonic-gate 	dbputc('\n');
317*7c478bd9Sstevel@tonic-gate 
318*7c478bd9Sstevel@tonic-gate 	/* output any #define end marker */
319*7c478bd9Sstevel@tonic-gate 	/*
320*7c478bd9Sstevel@tonic-gate 	 * note: must not be part of #define so putsource() doesn't discard it
321*7c478bd9Sstevel@tonic-gate 	 * so findcalledbysub() can find it and return
322*7c478bd9Sstevel@tonic-gate 	 */
323*7c478bd9Sstevel@tonic-gate 	if (symput < symbols && symbol[symput].type == DEFINEEND) {
324*7c478bd9Sstevel@tonic-gate 		dbputc('\t');
325*7c478bd9Sstevel@tonic-gate 		dbputc(DEFINEEND);
326*7c478bd9Sstevel@tonic-gate 		dbputc('\n');
327*7c478bd9Sstevel@tonic-gate 		dbputc('\n');	/* mark beginning of next source line */
328*7c478bd9Sstevel@tonic-gate 		macrooffset = 0;
329*7c478bd9Sstevel@tonic-gate 	}
330*7c478bd9Sstevel@tonic-gate 	symbols = 0;
331*7c478bd9Sstevel@tonic-gate }
332*7c478bd9Sstevel@tonic-gate 
333*7c478bd9Sstevel@tonic-gate /* output the inverted index posting */
334*7c478bd9Sstevel@tonic-gate 
335*7c478bd9Sstevel@tonic-gate void
putposting(char * term,int type)336*7c478bd9Sstevel@tonic-gate putposting(char *term, int type)
337*7c478bd9Sstevel@tonic-gate {
338*7c478bd9Sstevel@tonic-gate 	long	i, n;
339*7c478bd9Sstevel@tonic-gate 	char	*s;
340*7c478bd9Sstevel@tonic-gate 	int	digits;		/* digits output */
341*7c478bd9Sstevel@tonic-gate 	long	offset;		/* function/macro database offset */
342*7c478bd9Sstevel@tonic-gate 	char	buf[11];		/* number buffer */
343*7c478bd9Sstevel@tonic-gate 
344*7c478bd9Sstevel@tonic-gate 	/* get the function or macro name offset */
345*7c478bd9Sstevel@tonic-gate 	offset = fcnoffset;
346*7c478bd9Sstevel@tonic-gate 	if (macrooffset != 0) {
347*7c478bd9Sstevel@tonic-gate 		offset = macrooffset;
348*7c478bd9Sstevel@tonic-gate 	}
349*7c478bd9Sstevel@tonic-gate 	/* then update them to avoid negative relative name offset */
350*7c478bd9Sstevel@tonic-gate 	switch (type) {
351*7c478bd9Sstevel@tonic-gate 	case DEFINE:
352*7c478bd9Sstevel@tonic-gate 		macrooffset = dboffset;
353*7c478bd9Sstevel@tonic-gate 		break;
354*7c478bd9Sstevel@tonic-gate 	case DEFINEEND:
355*7c478bd9Sstevel@tonic-gate 		macrooffset = 0;
356*7c478bd9Sstevel@tonic-gate 		return;		/* null term */
357*7c478bd9Sstevel@tonic-gate 	case FCNDEF:
358*7c478bd9Sstevel@tonic-gate 		fcnoffset = dboffset;
359*7c478bd9Sstevel@tonic-gate 		break;
360*7c478bd9Sstevel@tonic-gate 	case FCNEND:
361*7c478bd9Sstevel@tonic-gate 		fcnoffset = 0;
362*7c478bd9Sstevel@tonic-gate 		return;		/* null term */
363*7c478bd9Sstevel@tonic-gate 	}
364*7c478bd9Sstevel@tonic-gate 	/* ignore a null term caused by a enum/struct/union without a tag */
365*7c478bd9Sstevel@tonic-gate 	if (*term == '\0') {
366*7c478bd9Sstevel@tonic-gate 		return;
367*7c478bd9Sstevel@tonic-gate 	}
368*7c478bd9Sstevel@tonic-gate 	/* skip any #include secondary type char (< or ") */
369*7c478bd9Sstevel@tonic-gate 	if (type == INCLUDE) {
370*7c478bd9Sstevel@tonic-gate 		++term;
371*7c478bd9Sstevel@tonic-gate 	}
372*7c478bd9Sstevel@tonic-gate 	/*
373*7c478bd9Sstevel@tonic-gate 	 * output the posting, which should be as small as possible to reduce
374*7c478bd9Sstevel@tonic-gate 	 * the temp file size and sort time
375*7c478bd9Sstevel@tonic-gate 	 */
376*7c478bd9Sstevel@tonic-gate 	(void) fputs(term, postings);
377*7c478bd9Sstevel@tonic-gate 	(void) putc(' ', postings);
378*7c478bd9Sstevel@tonic-gate 
379*7c478bd9Sstevel@tonic-gate 	/*
380*7c478bd9Sstevel@tonic-gate 	 * the line offset is padded so postings for the same term will sort
381*7c478bd9Sstevel@tonic-gate 	 * in ascending line offset order to order the references as they
382*7c478bd9Sstevel@tonic-gate 	 * appear withing a source file
383*7c478bd9Sstevel@tonic-gate 	 */
384*7c478bd9Sstevel@tonic-gate 	ltobase(lineoffset);
385*7c478bd9Sstevel@tonic-gate 	for (i = PRECISION - digits; i > 0; --i) {
386*7c478bd9Sstevel@tonic-gate 		(void) putc('!', postings);
387*7c478bd9Sstevel@tonic-gate 	}
388*7c478bd9Sstevel@tonic-gate 	do {
389*7c478bd9Sstevel@tonic-gate 		(void) putc(*s, postings);
390*7c478bd9Sstevel@tonic-gate 	} while (*++s != '\0');
391*7c478bd9Sstevel@tonic-gate 
392*7c478bd9Sstevel@tonic-gate 	/* postings are also sorted by type */
393*7c478bd9Sstevel@tonic-gate 	(void) putc(type, postings);
394*7c478bd9Sstevel@tonic-gate 
395*7c478bd9Sstevel@tonic-gate 	/* function or macro name offset */
396*7c478bd9Sstevel@tonic-gate 	if (offset > 0) {
397*7c478bd9Sstevel@tonic-gate 		(void) putc(' ', postings);
398*7c478bd9Sstevel@tonic-gate 		ltobase(offset);
399*7c478bd9Sstevel@tonic-gate 		do {
400*7c478bd9Sstevel@tonic-gate 			(void) putc(*s, postings);
401*7c478bd9Sstevel@tonic-gate 		} while (*++s != '\0');
402*7c478bd9Sstevel@tonic-gate 	}
403*7c478bd9Sstevel@tonic-gate 	if (putc('\n', postings) == EOF) {
404*7c478bd9Sstevel@tonic-gate 		cannotwrite(temp1);
405*7c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
406*7c478bd9Sstevel@tonic-gate 	}
407*7c478bd9Sstevel@tonic-gate 	++npostings;
408*7c478bd9Sstevel@tonic-gate }
409*7c478bd9Sstevel@tonic-gate 
410*7c478bd9Sstevel@tonic-gate /* put the string into the new database */
411*7c478bd9Sstevel@tonic-gate 
412*7c478bd9Sstevel@tonic-gate void
putstring(char * s)413*7c478bd9Sstevel@tonic-gate putstring(char *s)
414*7c478bd9Sstevel@tonic-gate {
415*7c478bd9Sstevel@tonic-gate 	unsigned c;
416*7c478bd9Sstevel@tonic-gate 	int	i;
417*7c478bd9Sstevel@tonic-gate 
418*7c478bd9Sstevel@tonic-gate 	/* compress digraphs */
419*7c478bd9Sstevel@tonic-gate 	for (i = 0; (c = s[i]) != '\0'; ++i) {
420*7c478bd9Sstevel@tonic-gate 		if (dicode1[c] && dicode2[(unsigned)s[i + 1]]) {
421*7c478bd9Sstevel@tonic-gate 			c = (0200 - 2) + dicode1[c] +
422*7c478bd9Sstevel@tonic-gate 			    dicode2[(unsigned)s[i + 1]];
423*7c478bd9Sstevel@tonic-gate 			++i;
424*7c478bd9Sstevel@tonic-gate 		}
425*7c478bd9Sstevel@tonic-gate 		dbputc((int)c);
426*7c478bd9Sstevel@tonic-gate 	}
427*7c478bd9Sstevel@tonic-gate }
428*7c478bd9Sstevel@tonic-gate 
429*7c478bd9Sstevel@tonic-gate /* print a warning message with the file name and line number */
430*7c478bd9Sstevel@tonic-gate 
431*7c478bd9Sstevel@tonic-gate void
warning(text)432*7c478bd9Sstevel@tonic-gate warning(text)
433*7c478bd9Sstevel@tonic-gate char	*text;
434*7c478bd9Sstevel@tonic-gate {
435*7c478bd9Sstevel@tonic-gate 	extern	int	yylineno;
436*7c478bd9Sstevel@tonic-gate 
437*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "cscope: \"%s\", line %d: warning: %s\n",
438*7c478bd9Sstevel@tonic-gate 	    filename, yylineno, text);
439*7c478bd9Sstevel@tonic-gate 	errorsfound = YES;
440*7c478bd9Sstevel@tonic-gate }
441