You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

454 lines
13KB

  1. /*===========================================================================
  2. Copyright (c) 1998-2000, The Santa Cruz Operation
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. *Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. *Redistributions in binary form must reproduce the above copyright notice,
  9. this list of conditions and the following disclaimer in the documentation
  10. and/or other materials provided with the distribution.
  11. *Neither name of The Santa Cruz Operation nor the names of its contributors
  12. may be used to endorse or promote products derived from this software
  13. without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
  15. IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT falseT LIMITED TO,
  16. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  17. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
  18. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  19. CONSEQUENTIAL DAMAGES (INCLUDING, BUT falseT LIMITED TO, PROCUREMENT OF
  20. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. INTERRUPTION)
  22. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  25. DAMAGE.
  26. =========================================================================*/
  27. /* cscope - interactive C symbol cross-reference
  28. *
  29. * build cross-reference file
  30. */
  31. #include "global.h"
  32. #include "build.h"
  33. #include "scanner.h"
  34. #include <stdlib.h>
  35. #include <sys/stat.h>
  36. /* convert long to a string in base BASE notation */
  37. #define ltobase(value) \
  38. do { \
  39. n = (value); \
  40. s = buf + (sizeof(buf) - 1); \
  41. *s = '\0'; \
  42. digits = 1; \
  43. while(n >= BASE) { \
  44. ++digits; \
  45. i = n; \
  46. n /= BASE; \
  47. *--s = i - n * BASE + '!'; \
  48. } \
  49. *--s = n + '!'; \
  50. } while(0)
  51. #define SYMBOLINC 20 /* symbol list size increment */
  52. long dboffset; /* new database offset */
  53. bool errorsfound; /* prompt before clearing messages */
  54. long lineoffset; /* source line database offset */
  55. long npostings; /* number of postings */
  56. int nsrcoffset; /* number of file name database offsets */
  57. long *srcoffset; /* source file name database offsets */
  58. unsigned long symbols; /* number of symbols */
  59. static char *filename; /* file name for warning messages */
  60. static long fcnoffset; /* function name database offset */
  61. static long macrooffset; /* macro name database offset */
  62. static unsigned long msymbols = SYMBOLINC; /* maximum number of symbols */
  63. struct symbol { /* symbol data */
  64. int type; /* type */
  65. unsigned int first; /* index of first character in text */
  66. unsigned int last; /* index of last+1 character in text */
  67. unsigned int length; /* symbol length */
  68. unsigned int fcn_level; /* function level of the symbol */
  69. };
  70. static struct symbol *symbol;
  71. static void putcrossref(void);
  72. static void savesymbol(int token, int num);
  73. void crossref(char *srcfile) {
  74. unsigned int i;
  75. unsigned int length; /* symbol length */
  76. unsigned int entry_no; /* function level of the symbol */
  77. int token; /* current token */
  78. struct stat st;
  79. if(!((stat(srcfile, &st) == 0) && S_ISREG(st.st_mode))) {
  80. cannotopen(srcfile);
  81. errorsfound = true;
  82. return;
  83. }
  84. entry_no = 0;
  85. /* open the source file */
  86. if((yyin = myfopen(srcfile, "r")) == NULL) {
  87. cannotopen(srcfile);
  88. errorsfound = true;
  89. return;
  90. }
  91. filename = srcfile; /* save the file name for warning messages */
  92. putfilename(srcfile); /* output the file name */
  93. dbputc('\n');
  94. dbputc('\n');
  95. /* read the source file */
  96. initscanner(srcfile);
  97. fcnoffset = macrooffset = 0;
  98. symbols = 0;
  99. if(symbol == NULL) { symbol = malloc(msymbols * sizeof(*symbol)); }
  100. for(;;) {
  101. /* get the next token */
  102. switch(token = yylex()) {
  103. default:
  104. /* if requested, truncate C symbols */
  105. length = last - first;
  106. if(trun_syms == true && length > 8 && token != INCLUDE &&
  107. token != NEWFILE) {
  108. length = 8;
  109. last = first + 8;
  110. }
  111. /* see if the token has a symbol */
  112. if(length == 0) {
  113. savesymbol(token, entry_no);
  114. break;
  115. }
  116. /* update entry_no if see function entry */
  117. if(token == FCNDEF) { entry_no++; }
  118. /* see if the symbol is already in the list */
  119. for(i = 0; i < symbols; ++i) {
  120. if(length == symbol[i].length &&
  121. strncmp(my_yytext + first, my_yytext + symbol[i].first, length) ==
  122. 0 &&
  123. entry_no == symbol[i].fcn_level &&
  124. token == symbol[i].type) { /* could be a::a() */
  125. break;
  126. }
  127. }
  128. if(i == symbols) { /* if not already in list */
  129. savesymbol(token, entry_no);
  130. }
  131. break;
  132. case NEWLINE: /* end of line containing symbols */
  133. entry_no = 0; /* reset entry_no for each line */
  134. #ifdef USING_LEX
  135. --yyleng; /* remove the newline */
  136. #endif
  137. putcrossref(); /* output the symbols and source line */
  138. lineno = myylineno; /* save the symbol line number */
  139. #ifndef USING_LEX
  140. /* HBB 20010425: replaced yyleng-- by this chunk: */
  141. if(my_yytext) *my_yytext = '\0';
  142. my_yyleng = 0;
  143. #endif
  144. break;
  145. case LEXERR: /* Lexer error, abort further parsing of this file */
  146. case LEXEOF: /* end of file; last line may not have \n */
  147. /* if there were symbols, output them and the source line */
  148. if(symbols > 0) { putcrossref(); }
  149. (void)fclose(yyin); /* close the source file */
  150. /* output the leading tab expected by the next call */
  151. dbputc('\t');
  152. return;
  153. }
  154. }
  155. }
  156. /* save the symbol in the list */
  157. static void savesymbol(int token, int num) {
  158. /* make sure there is room for the symbol */
  159. if(symbols == msymbols) {
  160. msymbols += SYMBOLINC;
  161. symbol = realloc(symbol, msymbols * sizeof(*symbol));
  162. }
  163. /* save the symbol */
  164. symbol[symbols].type = token;
  165. symbol[symbols].first = first;
  166. symbol[symbols].last = last;
  167. symbol[symbols].length = last - first;
  168. symbol[symbols].fcn_level = num;
  169. ++symbols;
  170. }
  171. /* output the file name */
  172. void putfilename(char *srcfile) {
  173. /* check for file system out of space */
  174. /* note: dbputc is not used to avoid lint complaint */
  175. if(putc(NEWFILE, newrefs) == EOF) {
  176. cannotwrite(newreffile);
  177. /* NOTREACHED */
  178. }
  179. ++dboffset;
  180. if(invertedindex == true) { srcoffset[nsrcoffset++] = dboffset; }
  181. dbfputs(srcfile);
  182. fcnoffset = macrooffset = 0;
  183. }
  184. /* output the symbols and source line */
  185. static void putcrossref(void) {
  186. unsigned int i, j;
  187. unsigned char c;
  188. bool blank; /* blank indicator */
  189. unsigned int symput = 0; /* symbols output */
  190. int type;
  191. /* output the source line */
  192. lineoffset = dboffset;
  193. dboffset += fprintf(newrefs, "%d ", lineno);
  194. #ifdef PRINTF_RETVAL_BROKEN
  195. dboffset = ftell(newrefs); /* fprintf doesn't return chars written */
  196. #endif
  197. /* HBB 20010425: added this line: */
  198. my_yytext[my_yyleng] = '\0';
  199. blank = false;
  200. for(i = 0; i < my_yyleng; ++i) {
  201. /* change a tab to a blank and compress blanks */
  202. if((c = my_yytext[i]) == ' ' || c == '\t') {
  203. blank = true;
  204. } else if(symput < symbols && i == symbol[symput].first) {
  205. /* look for the start of a symbol */
  206. /* check for compressed blanks */
  207. if(blank == true) {
  208. blank = false;
  209. dbputc(' ');
  210. }
  211. dbputc('\n'); /* symbols start on a new line */
  212. /* output any symbol type */
  213. if((type = symbol[symput].type) != IDENT) {
  214. dbputc('\t');
  215. dbputc(type);
  216. } else {
  217. type = ' ';
  218. }
  219. /* output the symbol */
  220. j = symbol[symput].last;
  221. c = my_yytext[j];
  222. my_yytext[j] = '\0';
  223. if(invertedindex == true) { putposting(my_yytext + i, type); }
  224. writestring(my_yytext + i);
  225. dbputc('\n');
  226. my_yytext[j] = c;
  227. i = j - 1;
  228. ++symput;
  229. } else {
  230. /* HBB: try to save some time by early-out handling of
  231. * non-compressed mode */
  232. if(compress == false) {
  233. if(blank == true) {
  234. dbputc(' ');
  235. blank = false;
  236. }
  237. j = i + strcspn(my_yytext + i, "\t ");
  238. if(symput < symbols && j >= symbol[symput].first)
  239. j = symbol[symput].first;
  240. c = my_yytext[j];
  241. my_yytext[j] = '\0';
  242. writestring(my_yytext + i);
  243. my_yytext[j] = c;
  244. i = j - 1;
  245. /* finished this 'i', continue with the blank */
  246. continue;
  247. }
  248. /* check for compressed blanks */
  249. if(blank == true) {
  250. if(dicode2[c]) {
  251. c = DICODE_COMPRESS(' ', c);
  252. } else {
  253. dbputc(' ');
  254. }
  255. } else if(IS_A_DICODE(c, my_yytext[i + 1]) && symput < symbols &&
  256. i + 1 != symbol[symput].first) {
  257. /* compress digraphs */
  258. c = DICODE_COMPRESS(c, my_yytext[i + 1]);
  259. ++i;
  260. }
  261. dbputc((int)c);
  262. blank = false;
  263. /* skip compressed characters */
  264. if(c < ' ') {
  265. ++i;
  266. /* skip blanks before a preprocesor keyword */
  267. /* note: don't use isspace() because \f and \v
  268. are used for keywords */
  269. while((j = my_yytext[i]) == ' ' || j == '\t') {
  270. ++i;
  271. }
  272. /* skip the rest of the keyword */
  273. while(isalpha((unsigned char)my_yytext[i])) {
  274. ++i;
  275. }
  276. /* skip space after certain keywords */
  277. if(keyword[c].delim != '\0') {
  278. while((j = my_yytext[i]) == ' ' || j == '\t') {
  279. ++i;
  280. }
  281. }
  282. /* skip a '(' after certain keywords */
  283. if(keyword[c].delim == '(' && my_yytext[i] == '(') { ++i; }
  284. --i; /* compensate for ++i in for() */
  285. } /* if compressed char */
  286. } /* else: not a symbol */
  287. } /* for(i) */
  288. /* ignore trailing blanks */
  289. dbputc('\n');
  290. dbputc('\n');
  291. /* output any #define end marker */
  292. /* note: must not be part of #define so putsource() doesn't discard it
  293. so findcalledbysub() can find it and return */
  294. if(symput < symbols && symbol[symput].type == DEFINEEND) {
  295. dbputc('\t');
  296. dbputc(DEFINEEND);
  297. dbputc('\n');
  298. dbputc('\n'); /* mark beginning of next source line */
  299. macrooffset = 0;
  300. }
  301. symbols = 0;
  302. }
  303. /* HBB 20000421: new function, for avoiding memory leaks */
  304. /* free the cross reference symbol table */
  305. void freecrossref() {
  306. if(symbol) free(symbol);
  307. symbol = NULL;
  308. symbols = 0;
  309. }
  310. /* output the inverted index posting */
  311. void putposting(char *term, int type) {
  312. long i, n;
  313. char *s;
  314. int digits; /* digits output */
  315. long offset; /* function/macro database offset */
  316. char buf[11]; /* number buffer */
  317. /* get the function or macro name offset */
  318. offset = fcnoffset;
  319. if(macrooffset != 0) { offset = macrooffset; }
  320. /* then update them to avoid negative relative name offset */
  321. switch(type) {
  322. case DEFINE:
  323. macrooffset = dboffset;
  324. break;
  325. case DEFINEEND:
  326. macrooffset = 0;
  327. return; /* null term */
  328. case FCNDEF:
  329. fcnoffset = dboffset;
  330. break;
  331. case FCNEND:
  332. fcnoffset = 0;
  333. return; /* null term */
  334. }
  335. /* ignore a null term caused by a enum/struct/union without a tag */
  336. if(*term == '\0') { return; }
  337. /* skip any #include secondary type char (< or ") */
  338. if(type == INCLUDE) { ++term; }
  339. /* output the posting, which should be as small as possible to reduce
  340. the temp file size and sort time */
  341. (void)fputs(term, postings);
  342. (void)putc(' ', postings);
  343. /* the line offset is padded so postings for the same term will sort
  344. in ascending line offset order to order the references as they
  345. appear withing a source file */
  346. ltobase(lineoffset);
  347. for(i = PRECISION - digits; i > 0; --i) {
  348. (void)putc('!', postings);
  349. }
  350. do {
  351. (void)putc(*s, postings);
  352. } while(*++s != '\0');
  353. /* postings are also sorted by type */
  354. (void)putc(type, postings);
  355. /* function or macro name offset */
  356. if(offset > 0) {
  357. (void)putc(' ', postings);
  358. ltobase(offset);
  359. do {
  360. (void)putc(*s, postings);
  361. } while(*++s != '\0');
  362. }
  363. if(putc('\n', postings) == EOF) {
  364. cannotwrite(temp1);
  365. /* NOTREACHED */
  366. }
  367. ++npostings;
  368. }
  369. /* put the string into the new database */
  370. void writestring(char *s) {
  371. unsigned char c;
  372. int i;
  373. if(compress == false) {
  374. /* Save some I/O overhead by using puts() instead of putc(): */
  375. dbfputs(s);
  376. return;
  377. }
  378. /* compress digraphs */
  379. for(i = 0; (c = s[i]) != '\0'; ++i) {
  380. if(/* dicode1[c] && dicode2[(unsigned char) s[i + 1]] */
  381. IS_A_DICODE(c, s[i + 1])) {
  382. /* c = (0200 - 2) + dicode1[c] + dicode2[(unsigned char) s[i + 1]]; */
  383. c = DICODE_COMPRESS(c, s[i + 1]);
  384. ++i;
  385. }
  386. dbputc(c);
  387. }
  388. }
  389. /* print a warning message with the file name and line number */
  390. void warning(char *text) {
  391. (void)fprintf(stderr,
  392. PROGRAM_NAME ": \"%s\", line %d: warning: %s\n",
  393. filename,
  394. myylineno,
  395. text);
  396. errorsfound = true;
  397. }