25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1324 lines
34KB

  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 or text cross-reference
  28. *
  29. * searching functions
  30. */
  31. #include "global.h"
  32. #include "build.h"
  33. #include "scanner.h" /* for token definitions */
  34. #include <assert.h>
  35. #include <ncurses.h>
  36. #include <regex.h>
  37. #include <setjmp.h> /* jmp_buf */
  38. /* most of these functions have been optimized so their innermost loops have
  39. * only one test for the desired character by putting the char and
  40. * an end-of-block marker (\0) at the end of the disk block buffer.
  41. * When the inner loop exits on the char, an outer loop will see if
  42. * the char is followed by a \0. If so, it will read the next block
  43. * and restart the inner loop.
  44. */
  45. char *blockp; /* pointer to current char in block */
  46. char block[BUFSIZ + 2]; /* leave room for end-of-block mark */
  47. int blocklen; /* length of disk block read */
  48. char blockmark; /* mark character to be searched for */
  49. long blocknumber; /* block number */
  50. static char global[] = "<global>"; /* dummy global function name */
  51. static char cpattern[PATLEN + 1]; /* compressed pattern */
  52. static long lastfcnoffset; /* last function name offset */
  53. static POSTING *postingp; /* retrieved posting set pointer */
  54. static long postingsfound; /* retrieved number of postings */
  55. static regex_t regexp; /* regular expression */
  56. static bool isregexp_valid = false; /* regular expression status */
  57. static bool match(void);
  58. static bool matchrest(void);
  59. static POSTING *getposting(void);
  60. static char *lcasify(const char *s);
  61. static void findcalledbysub(const char *file, bool macro);
  62. static void findterm(const char *pattern);
  63. static void putline(FILE *output);
  64. static char *find_symbol_or_assignment(const char *pattern, bool assign_flag);
  65. static bool check_for_assignment(void);
  66. static void putpostingref(POSTING *p, const char *pat);
  67. static void putref(int seemore, const char *file, const char *func);
  68. static void putsource(int seemore, FILE *output);
  69. static FILE *nonglobalrefs; /* non-global references file */
  70. static sigjmp_buf env; /* setjmp/longjmp buffer */
  71. typedef enum { /* findinit return code */
  72. NOERROR,
  73. NOTSYMBOL,
  74. REGCMPERROR
  75. } FINDINIT;
  76. typedef char *(*FP)(const char *); /* pointer to function returning a character pointer */
  77. /* Paralel array to "fields", indexed by "field" */
  78. FP field_searchers[FIELDS + 1] = {
  79. findsymbol,
  80. finddef,
  81. findcalledby,
  82. findcalling,
  83. findstring,
  84. findstring,
  85. findregexp,
  86. findfile,
  87. findinclude,
  88. findassign,
  89. findallfcns /* samuel only */
  90. };
  91. /**/
  92. struct TI {
  93. char *text1;
  94. char *text2;
  95. };
  96. extern struct TI fields[FIELDS + 1];
  97. /* Internal prototypes: */
  98. static void jumpback(int sig);
  99. static void jumpback(int sig) {
  100. signal(sig, jumpback);
  101. siglongjmp(env, 1);
  102. }
  103. /* find the symbol in the cross-reference */
  104. char *findsymbol(const char *pattern) {
  105. return find_symbol_or_assignment(pattern, false);
  106. }
  107. /* find the symbol in the cross-reference, and look for assignments */
  108. char *findassign(const char *pattern) {
  109. return find_symbol_or_assignment(pattern, true);
  110. }
  111. /* Test reference whether it's an assignment to the symbol found at
  112. * (global variable) 'blockp' */
  113. static bool check_for_assignment(void) {
  114. /* Do the extra work here to determine if this is an
  115. * assignment or not. Do this by examining the next character
  116. * or two in blockp */
  117. char *asgn_char = blockp;
  118. while(isspace((unsigned char)asgn_char[0])) {
  119. /* skip any whitespace or \n */
  120. asgn_char++;
  121. if(asgn_char[0] == '\0') {
  122. /* get the next block when we reach the end of
  123. * the current block */
  124. if(NULL == (asgn_char = read_block())) return false;
  125. }
  126. }
  127. /* check for digraph starting with = */
  128. if((asgn_char[0] & 0x80) && (dichar1[(asgn_char[0] & 0177) / 8] == '=')) {
  129. return true;
  130. }
  131. /* check for plain '=', not '==' */
  132. if((asgn_char[0] == '=') &&
  133. (((asgn_char[1] != '=') && !(asgn_char[1] & 0x80)) ||
  134. ((asgn_char[1] & 0x80) && (dichar1[(asgn_char[1] & 0177) / 8] != '=')))) {
  135. return true;
  136. }
  137. /* check for operator assignments: +=, ... ^= ? */
  138. if(((asgn_char[0] == '+') || (asgn_char[0] == '-') || (asgn_char[0] == '*') ||
  139. (asgn_char[0] == '/') || (asgn_char[0] == '%') || (asgn_char[0] == '&') ||
  140. (asgn_char[0] == '|') || (asgn_char[0] == '^')) &&
  141. ((asgn_char[1] == '=') ||
  142. ((asgn_char[1] & 0x80) && (dichar1[(asgn_char[1] & 0177) / 8] == '=')))
  143. ) {
  144. return true;
  145. }
  146. /* check for two-letter operator assignments: <<= or >>= ? */
  147. if(((asgn_char[0] == '<') || (asgn_char[0] == '>')) &&
  148. (asgn_char[1] == asgn_char[0]) &&
  149. ((asgn_char[2] == '=') ||
  150. ((asgn_char[2] & 0x80) && (dichar1[(asgn_char[2] & 0177) / 8] == '='))))
  151. return true;
  152. return false;
  153. }
  154. /* The actual routine that does the work for findsymbol() and
  155. * findassign() */
  156. static char *find_symbol_or_assignment(const char *pattern, bool assign_flag) {
  157. char file[PATHLEN + 1]; /* source file name */
  158. char function[PATLEN + 1]; /* function name */
  159. char macro[PATLEN + 1]; /* macro name */
  160. char symbol[PATLEN + 1]; /* symbol name */
  161. char *cp;
  162. char *s;
  163. size_t s_len = 0;
  164. char firstchar; /* first character of a potential symbol */
  165. bool fcndef = false;
  166. if((invertedindex == true) && (assign_flag == false)) {
  167. long lastline = 0;
  168. POSTING *p;
  169. findterm(pattern);
  170. while((p = getposting()) != NULL) {
  171. if(p->type != INCLUDE && p->lineoffset != lastline) {
  172. putpostingref(p, 0);
  173. lastline = p->lineoffset;
  174. }
  175. }
  176. return NULL;
  177. }
  178. (void)scanpast('\t'); /* find the end of the header */
  179. skiprefchar(); /* skip the file marker */
  180. fetch_string_from_dbase(file, sizeof(file));
  181. strcpy(function, global); /* set the dummy global function name */
  182. strcpy(macro, global); /* set the dummy global macro name */
  183. /* find the next symbol */
  184. /* note: this code was expanded in-line for speed */
  185. /* other macros were replaced by code using cp instead of blockp */
  186. cp = blockp;
  187. for(;;) {
  188. setmark('\n');
  189. do { /* innermost loop optimized to only one test */
  190. while(*cp != '\n') {
  191. ++cp;
  192. }
  193. } while(*(cp + 1) == '\0' && (cp = read_block()) != NULL);
  194. /* skip the found character */
  195. if(cp != NULL && *(++cp + 1) == '\0') { cp = read_block(); }
  196. if(cp == NULL) { break; }
  197. /* look for a source file, function, or macro name */
  198. if(*cp == '\t') {
  199. blockp = cp;
  200. switch(getrefchar()) {
  201. case NEWFILE: /* file name */
  202. /* save the name */
  203. skiprefchar();
  204. fetch_string_from_dbase(file, sizeof(file));
  205. /* check for the end of the symbols */
  206. if(*file == '\0') { return NULL; }
  207. progress("Search", searchcount, nsrcfiles);
  208. /* FALLTHROUGH */
  209. case FCNEND: /* function end */
  210. (void)strcpy(function, global);
  211. goto notmatched; /* don't match name */
  212. case FCNDEF: /* function name */
  213. fcndef = true;
  214. s = function;
  215. s_len = sizeof(function);
  216. break;
  217. case DEFINE: /* macro name */
  218. if(fileversion >= 10) {
  219. s = macro;
  220. s_len = sizeof(macro);
  221. } else {
  222. s = symbol;
  223. s_len = sizeof(symbol);
  224. }
  225. break;
  226. case DEFINEEND: /* macro end */
  227. (void)strcpy(macro, global);
  228. goto notmatched;
  229. case INCLUDE: /* #include file */
  230. goto notmatched; /* don't match name */
  231. default: /* other symbol */
  232. s = symbol;
  233. s_len = sizeof(symbol);
  234. }
  235. /* save the name */
  236. skiprefchar();
  237. fetch_string_from_dbase(s, s_len);
  238. /* see if this is a regular expression pattern */
  239. if(isregexp_valid == true) {
  240. if(caseless == true) { s = lcasify(s); }
  241. if(*s != '\0' && regexec(&regexp, s, (size_t)0, NULL, 0) == 0) {
  242. goto matched;
  243. }
  244. }
  245. /* match the symbol to the text pattern */
  246. else if(strequal(pattern, s)) {
  247. goto matched;
  248. }
  249. goto notmatched;
  250. }
  251. /* if this is a regular expression pattern */
  252. if(isregexp_valid == true) {
  253. /* if this is a symbol */
  254. /**************************************************
  255. * The first character may be a digraph'ed char, so
  256. * unpack it into firstchar, and then test that.
  257. *
  258. * Assume that all digraphed chars have the 8th bit
  259. * set (0200).
  260. **************************************************/
  261. if(*cp & 0200) { /* digraph char? */
  262. firstchar = dichar1[(*cp & 0177) / 8];
  263. } else {
  264. firstchar = *cp;
  265. }
  266. if(isalpha((unsigned char)firstchar) || firstchar == '_') {
  267. blockp = cp;
  268. fetch_string_from_dbase(symbol, sizeof(symbol));
  269. if(caseless == true) {
  270. s = lcasify(symbol); /* point to lower case version */
  271. } else {
  272. s = symbol;
  273. }
  274. /* match the symbol to the regular expression */
  275. if(*s != '\0' && regexec(&regexp, s, (size_t)0, NULL, 0) == 0) {
  276. goto matched;
  277. }
  278. goto notmatched;
  279. }
  280. }
  281. /* match the character to the text pattern */
  282. else if(*cp == cpattern[0]) {
  283. blockp = cp;
  284. /* match the rest of the symbol to the text pattern */
  285. if(matchrest()) {
  286. s = NULL;
  287. matched:
  288. /* if the assignment flag is set then
  289. * we are looking for assignments and
  290. * some extra filtering is needed */
  291. if(assign_flag == true && !check_for_assignment()) goto notmatched;
  292. /* output the file, function or macro, and source line */
  293. if(strcmp(macro, global) && s != macro) {
  294. putref(0, file, macro);
  295. } else if(fcndef == true || s != function) {
  296. fcndef = false;
  297. putref(0, file, function);
  298. } else {
  299. putref(0, file, global);
  300. }
  301. }
  302. notmatched:
  303. if(blockp == NULL) { return NULL; }
  304. fcndef = false;
  305. cp = blockp;
  306. }
  307. }
  308. blockp = cp;
  309. return NULL;
  310. }
  311. /* find the function definition or #define */
  312. char *finddef(const char *pattern) {
  313. char file[PATHLEN + 1]; /* source file name */
  314. if(invertedindex == true) {
  315. POSTING *p;
  316. findterm(pattern);
  317. while((p = getposting()) != NULL) {
  318. switch(p->type) {
  319. case DEFINE: /* could be a macro */
  320. case FCNDEF:
  321. case CLASSDEF:
  322. case ENUMDEF:
  323. case MEMBERDEF:
  324. case STRUCTDEF:
  325. case TYPEDEF:
  326. case UNIONDEF:
  327. case GLOBALDEF: /* other global definition */
  328. putpostingref(p, pattern);
  329. }
  330. }
  331. return NULL;
  332. }
  333. /* find the next file name or definition */
  334. while(scanpast('\t') != NULL) {
  335. switch(*blockp) {
  336. case NEWFILE:
  337. skiprefchar(); /* save file name */
  338. fetch_string_from_dbase(file, sizeof(file));
  339. if(*file == '\0') { /* if end of symbols */
  340. return NULL;
  341. }
  342. progress("Search", searchcount, nsrcfiles);
  343. break;
  344. case DEFINE: /* could be a macro */
  345. case FCNDEF:
  346. case CLASSDEF:
  347. case ENUMDEF:
  348. case MEMBERDEF:
  349. case STRUCTDEF:
  350. case TYPEDEF:
  351. case UNIONDEF:
  352. case GLOBALDEF: /* other global definition */
  353. skiprefchar(); /* match name to pattern */
  354. if(match()) {
  355. /* output the file, function and source line */
  356. putref(0, file, pattern);
  357. }
  358. break;
  359. }
  360. }
  361. return NULL;
  362. }
  363. /* find all function definitions (used by samuel only) */
  364. char *findallfcns(const char *dummy) {
  365. char file[PATHLEN + 1]; /* source file name */
  366. char function[PATLEN + 1]; /* function name */
  367. (void)dummy; /* unused argument */
  368. /* find the next file name or definition */
  369. while(scanpast('\t') != NULL) {
  370. switch(*blockp) {
  371. case NEWFILE:
  372. skiprefchar(); /* save file name */
  373. fetch_string_from_dbase(file, sizeof(file));
  374. if(*file == '\0') { /* if end of symbols */
  375. return NULL;
  376. }
  377. progress("Search", searchcount, nsrcfiles);
  378. /* FALLTHROUGH */
  379. case FCNEND: /* function end */
  380. (void)strcpy(function, global);
  381. break;
  382. case FCNDEF:
  383. case CLASSDEF:
  384. skiprefchar(); /* save function name */
  385. fetch_string_from_dbase(function, sizeof(function));
  386. /* output the file, function and source line */
  387. putref(0, file, function);
  388. break;
  389. }
  390. }
  391. return NULL;
  392. }
  393. /* find the functions calling this function */
  394. char *findcalling(const char *pattern) {
  395. char file[PATHLEN + 1]; /* source file name */
  396. char function[PATLEN + 1]; /* function name */
  397. char tmpfunc[10][PATLEN + 1]; /* 10 temporary function names */
  398. char macro[PATLEN + 1]; /* macro name */
  399. char *tmpblockp;
  400. int morefuns, i;
  401. if(invertedindex == true) {
  402. POSTING *p;
  403. findterm(pattern);
  404. while((p = getposting()) != NULL) {
  405. if(p->type == FCNCALL) { putpostingref(p, 0); }
  406. }
  407. return NULL;
  408. }
  409. /* find the next file name or function definition */
  410. *macro = '\0'; /* a macro can be inside a function, but not vice versa */
  411. tmpblockp = 0;
  412. morefuns = 0; /* one function definition is normal case */
  413. for(i = 0; i < 10; i++)
  414. *(tmpfunc[i]) = '\0';
  415. while(scanpast('\t') != NULL) {
  416. switch(*blockp) {
  417. case NEWFILE: /* save file name */
  418. skiprefchar();
  419. fetch_string_from_dbase(file, sizeof(file));
  420. if(*file == '\0') { /* if end of symbols */
  421. return NULL;
  422. }
  423. progress("Search", searchcount, nsrcfiles);
  424. (void)strcpy(function, global);
  425. break;
  426. case DEFINE: /* could be a macro */
  427. if(fileversion >= 10) {
  428. skiprefchar();
  429. fetch_string_from_dbase(macro, sizeof(macro));
  430. }
  431. break;
  432. case DEFINEEND:
  433. *macro = '\0';
  434. break;
  435. case FCNDEF: /* save calling function name */
  436. skiprefchar();
  437. fetch_string_from_dbase(function, sizeof(function));
  438. for(i = 0; i < morefuns; i++)
  439. if(!strcmp(tmpfunc[i], function)) break;
  440. if(i == morefuns) {
  441. (void)strcpy(tmpfunc[morefuns], function);
  442. if(++morefuns >= 10) morefuns = 9;
  443. }
  444. break;
  445. case FCNEND:
  446. for(i = 0; i < morefuns; i++)
  447. *(tmpfunc[i]) = '\0';
  448. morefuns = 0;
  449. break;
  450. case FCNCALL: /* match function called to pattern */
  451. skiprefchar();
  452. if(match()) {
  453. /* output the file, calling function or macro, and source */
  454. if(*macro != '\0') {
  455. putref(1, file, macro);
  456. } else {
  457. tmpblockp = blockp;
  458. for(i = 0; i < morefuns; i++) {
  459. blockp = tmpblockp;
  460. putref(1, file, tmpfunc[i]);
  461. }
  462. }
  463. }
  464. }
  465. }
  466. return NULL;
  467. }
  468. /* find the text in the source files */
  469. char *findstring(const char *pattern) {
  470. char egreppat[2 * PATLEN];
  471. char *cp = egreppat;
  472. const char *pp;
  473. /* translate special characters in the regular expression */
  474. for(pp = pattern; *pp != '\0'; ++pp) {
  475. if(strchr(".*[\\^$+?|()", *pp) != NULL) { *cp++ = '\\'; }
  476. *cp++ = *pp;
  477. }
  478. *cp = '\0';
  479. /* search the source files */
  480. return (findregexp(egreppat));
  481. }
  482. /* find this regular expression in the source files */
  483. char *findregexp(const char *egreppat) {
  484. unsigned int i;
  485. char *egreperror;
  486. /* compile the pattern */
  487. if((egreperror = egrepinit(egreppat)) == NULL) {
  488. /* search the files */
  489. for(i = 0; i < nsrcfiles; ++i) {
  490. char *file = filepath(srcfiles[i]);
  491. progress("Search", searchcount, nsrcfiles);
  492. if(egrep(file, refsfound, "%s <unknown> %ld ") < 0) {
  493. posterr("Cannot open file %s", file);
  494. }
  495. }
  496. }
  497. return (egreperror);
  498. }
  499. /* find matching file names */
  500. char *findfile(const char *dummy) {
  501. unsigned int i;
  502. (void)dummy; /* unused argument */
  503. for(i = 0; i < nsrcfiles; ++i) {
  504. char *s;
  505. if(caseless == true) {
  506. s = lcasify(srcfiles[i]);
  507. } else {
  508. s = srcfiles[i];
  509. }
  510. if(regexec(&regexp, s, (size_t)0, NULL, 0) == 0) {
  511. (void)fprintf(refsfound, "%s <unknown> 1 <unknown>\n", srcfiles[i]);
  512. }
  513. }
  514. return NULL;
  515. }
  516. /* find files #including this file */
  517. char *findinclude(const char *pattern) {
  518. char file[PATHLEN + 1]; /* source file name */
  519. if(invertedindex == true) {
  520. POSTING *p;
  521. findterm(pattern);
  522. while((p = getposting()) != NULL) {
  523. if(p->type == INCLUDE) { putpostingref(p, 0); }
  524. }
  525. return NULL;
  526. }
  527. /* find the next file name or function definition */
  528. while(scanpast('\t') != NULL) {
  529. switch(*blockp) {
  530. case NEWFILE: /* save file name */
  531. skiprefchar();
  532. fetch_string_from_dbase(file, sizeof(file));
  533. if(*file == '\0') { /* if end of symbols */
  534. return NULL;
  535. }
  536. progress("Search", searchcount, nsrcfiles);
  537. break;
  538. case INCLUDE: /* match function called to pattern */
  539. skiprefchar();
  540. skiprefchar(); /* skip global or local #include marker */
  541. if(match()) {
  542. /* output the file and source line */
  543. putref(0, file, global);
  544. }
  545. }
  546. }
  547. return NULL;
  548. }
  549. /* initialize */
  550. int findinit(const char *pattern_) {
  551. char *pattern = strdup(pattern_);
  552. int r = NOERROR;
  553. char buf[PATLEN + 3];
  554. bool isregexp = false;
  555. int i;
  556. char *s;
  557. unsigned char c;
  558. /* HBB: be nice: free regexp before allocating a new one */
  559. if(isregexp_valid == true) regfree(&regexp);
  560. isregexp_valid = false;
  561. /* remove trailing white space */
  562. for(s = pattern + strlen(pattern) - 1; isspace((unsigned char)*s); --s) {
  563. *s = '\0';
  564. }
  565. /* Make sure pattern is lowercased. Curses
  566. * mode gets this right all on its own, but at least -L mode
  567. * doesn't */
  568. if(caseless == true) { pattern = lcasify(pattern); }
  569. /* allow a partial match for a file name */
  570. if(field == FILENAME || field == INCLUDES) {
  571. if(regcomp(&regexp, pattern, REG_EXTENDED | REG_NOSUB) != 0) {
  572. r = REGCMPERROR;
  573. } else {
  574. isregexp_valid = true;
  575. }
  576. goto end;
  577. }
  578. /* see if the pattern is a regular expression */
  579. if(strpbrk(pattern, "^.[{*+$|(") != NULL) {
  580. isregexp = true;
  581. } else {
  582. /* check for a valid C symbol */
  583. s = pattern;
  584. if(!isalpha((unsigned char)*s) && *s != '_') {
  585. r = NOTSYMBOL;
  586. goto end;
  587. }
  588. while(*++s != '\0') {
  589. if(!isalnum((unsigned char)*s) && *s != '_') {
  590. r = NOTSYMBOL;
  591. goto end;
  592. }
  593. }
  594. /* look for use of the -T option (truncate symbol to 8
  595. characters) on a database not built with -T */
  596. if(trun_syms == true && isuptodate == true && dbtruncated == false &&
  597. s - pattern >= 8) {
  598. (void)strcpy(pattern + 8, ".*");
  599. isregexp = true;
  600. }
  601. }
  602. /* if this is a regular expression or letter case is to be ignored */
  603. /* or there is an inverted index */
  604. if(isregexp == true || caseless == true || invertedindex == true) {
  605. /* remove a leading ^ */
  606. s = pattern;
  607. if(*s == '^') {
  608. (void)strcpy(newpat, s + 1);
  609. (void)strcpy(s, newpat);
  610. }
  611. /* remove a trailing $ */
  612. i = strlen(s) - 1;
  613. if(s[i] == '$') {
  614. if(i > 0 && s[i - 1] == '\\') { s[i - 1] = '$'; }
  615. s[i] = '\0';
  616. }
  617. /* if requested, try to truncate a C symbol pattern */
  618. if(trun_syms == true && strpbrk(s, "[{*+") == NULL) { s[8] = '\0'; }
  619. /* must be an exact match */
  620. /* note: regcomp doesn't recognize ^*keypad$ as a syntax error
  621. unless it is given as a single arg */
  622. (void)snprintf(buf, sizeof(buf), "^%s$", s);
  623. if(regcomp(&regexp, buf, REG_EXTENDED | REG_NOSUB) != 0) {
  624. r = REGCMPERROR;
  625. goto end;
  626. } else {
  627. isregexp_valid = true;
  628. }
  629. } else {
  630. /* if requested, truncate a C symbol pattern */
  631. if(trun_syms == true && field <= CALLING) { pattern[8] = '\0'; }
  632. /* compress the string pattern for matching */
  633. s = cpattern;
  634. for(i = 0; (c = pattern[i]) != '\0'; ++i) {
  635. if(IS_A_DICODE(c, pattern[i + 1])) {
  636. c = DICODE_COMPRESS(c, pattern[i + 1]);
  637. ++i;
  638. }
  639. *s++ = c;
  640. }
  641. *s = '\0';
  642. }
  643. end:
  644. return r;
  645. }
  646. void findcleanup(void) {
  647. /* discard any regular expression */
  648. }
  649. /* match the pattern to the string */
  650. static bool match(void) {
  651. char string[PATLEN + 1];
  652. /* see if this is a regular expression pattern */
  653. if(isregexp_valid == true) {
  654. fetch_string_from_dbase(string, sizeof(string));
  655. if(*string == '\0') { return (false); }
  656. if(caseless == true) {
  657. return (regexec(&regexp, lcasify(string), (size_t)0, NULL, 0) ? false : true);
  658. } else {
  659. return (regexec(&regexp, string, (size_t)0, NULL, 0) ? false : true);
  660. }
  661. }
  662. /* it is a string pattern */
  663. return ((bool)(*blockp == cpattern[0] && matchrest()));
  664. }
  665. /* match the rest of the pattern to the name */
  666. static bool matchrest(void) {
  667. int i = 1;
  668. skiprefchar();
  669. do {
  670. while(*blockp == cpattern[i]) {
  671. ++blockp;
  672. ++i;
  673. }
  674. } while(*(blockp + 1) == '\0' && read_block() != NULL);
  675. if(*blockp == '\n' && cpattern[i] == '\0') { return (true); }
  676. return (false);
  677. }
  678. /* put the reference into the file */
  679. static void putref(int seemore, const char *file, const char *func) {
  680. FILE *output;
  681. if(strcmp(func, global) == 0) {
  682. output = refsfound;
  683. } else {
  684. output = nonglobalrefs;
  685. }
  686. (void)fprintf(output, "%s %s ", file, func);
  687. putsource(seemore, output);
  688. }
  689. /* put the source line into the file */
  690. static void putsource(int seemore, FILE *output) {
  691. char *tmpblockp;
  692. char *cp, nextc = '\0';
  693. bool Change = false, retreat = false;
  694. if(fileversion <= 5) {
  695. (void)scanpast(' ');
  696. putline(output);
  697. (void)putc('\n', output);
  698. return;
  699. }
  700. /* scan back to the beginning of the source line */
  701. cp = tmpblockp = blockp;
  702. while(*cp != '\n' || nextc != '\n') {
  703. nextc = *cp;
  704. if(--cp < block) {
  705. retreat = true;
  706. /* read the previous block */
  707. (void)dbseek((blocknumber - 1) * BUFSIZ);
  708. cp = block + (BUFSIZ - 1);
  709. }
  710. }
  711. blockp = cp;
  712. if(*blockp != '\n' || getrefchar() != '\n' ||
  713. (!isdigit(getrefchar()) && fileversion >= 12)) {
  714. postfatal("Internal error: cannot get source line from database");
  715. /* NOTREACHED */
  716. }
  717. /* until a double newline is found */
  718. do {
  719. /* skip a symbol type */
  720. if(*blockp == '\t') {
  721. /* if retreat == true, that means tmpblockp and blockp
  722. * point to different blocks. Offset comparison should
  723. * falseT be performed until they point to the same block.
  724. */
  725. if(seemore && Change == false && retreat == false && blockp > tmpblockp) {
  726. Change = true;
  727. cp = blockp;
  728. }
  729. skiprefchar();
  730. skiprefchar();
  731. }
  732. /* output a piece of the source line */
  733. putline(output);
  734. if(retreat == true) retreat = false;
  735. } while(blockp != NULL && getrefchar() != '\n');
  736. (void)putc('\n', output);
  737. if(Change == true) blockp = cp;
  738. }
  739. /* put the rest of the cross-reference line into the file */
  740. static void putline(FILE *output) {
  741. char *cp;
  742. unsigned c;
  743. setmark('\n');
  744. cp = blockp;
  745. do {
  746. while((c = (unsigned)(*cp)) != '\n') {
  747. /* check for a compressed digraph */
  748. if(c > '\177') {
  749. c &= 0177;
  750. (void)putc(dichar1[c / 8], output);
  751. (void)putc(dichar2[c & 7], output);
  752. }
  753. /* check for a compressed keyword */
  754. else if(c < ' ') {
  755. (void)fputs(keyword[c].text, output);
  756. if(keyword[c].delim != '\0') { (void)putc(' ', output); }
  757. if(keyword[c].delim == '(') { (void)putc('(', output); }
  758. } else {
  759. (void)putc((int)c, output);
  760. }
  761. ++cp;
  762. }
  763. } while(*(cp + 1) == '\0' && (cp = read_block()) != NULL);
  764. blockp = cp;
  765. }
  766. /* put the rest of the cross-reference line into the string */
  767. void fetch_string_from_dbase(char *s, size_t length) {
  768. char *cp;
  769. unsigned int c;
  770. assert(length > sizeof(char *));
  771. setmark('\n');
  772. cp = blockp;
  773. do {
  774. while(length > 1 && (c = (unsigned int)(*cp)) != '\n') {
  775. if(c >= 0x80 && length > 2) {
  776. c &= 0x7f;
  777. *s++ = dichar1[c / 8];
  778. *s++ = dichar2[c & 7];
  779. length -= 2;
  780. } else {
  781. *s++ = c;
  782. length--;
  783. }
  784. ++cp;
  785. }
  786. } while(length > 0 && cp[1] == '\0' && (cp = read_block()) != NULL);
  787. blockp = cp;
  788. *s = '\0';
  789. }
  790. /* scan past the next occurence of this character in the cross-reference */
  791. char *scanpast(char c) {
  792. char *cp;
  793. setmark(c);
  794. cp = blockp;
  795. do { /* innermost loop optimized to only one test */
  796. while(*cp != c) {
  797. ++cp;
  798. }
  799. } while(*(cp + 1) == '\0' && (cp = read_block()) != NULL);
  800. blockp = cp;
  801. if(cp != NULL) { skiprefchar(); /* skip the found character */ }
  802. return (blockp);
  803. }
  804. /* read a block of the cross-reference */
  805. /* HBB 20040430: renamed from readblock(), to avoid name clash on QNX */
  806. char *read_block(void) {
  807. /* read the next block */
  808. blocklen = read(symrefs, block, BUFSIZ);
  809. blockp = block;
  810. /* add the search character and end-of-block mark */
  811. block[blocklen] = blockmark;
  812. block[blocklen + 1] = '\0';
  813. /* return NULL on end-of-file */
  814. if(blocklen == 0) {
  815. blockp = NULL;
  816. } else {
  817. ++blocknumber;
  818. }
  819. return (blockp);
  820. }
  821. static char *lcasify(const char *s) {
  822. static char ls[PATLEN + 1]; /* largest possible match string */
  823. char *lptr = ls;
  824. while(*s) {
  825. *lptr = tolower((unsigned char)*s);
  826. lptr++;
  827. s++;
  828. }
  829. *lptr = '\0';
  830. return ls;
  831. }
  832. /* find the functions called by this function */
  833. /* HBB 2000/05/05: for consitency of calling interface between the
  834. * different 'find...()' functions, this now returns a char pointer,
  835. * too. Implemented as a pointer to static storage containing 'y' or
  836. * 'n', for the boolean result values true and false */
  837. char *findcalledby(const char *pattern) {
  838. char file[PATHLEN + 1]; /* source file name */
  839. static char found_caller = 'n'; /* seen calling function? */
  840. bool macro = false;
  841. if(invertedindex == true) {
  842. POSTING *p;
  843. findterm(pattern);
  844. while((p = getposting()) != NULL) {
  845. switch(p->type) {
  846. case DEFINE: /* could be a macro */
  847. case FCNDEF:
  848. if(dbseek(p->lineoffset) != -1 &&
  849. scanpast('\t') != NULL) { /* skip def */
  850. found_caller = 'y';
  851. findcalledbysub(srcfiles[p->fileindex], macro);
  852. }
  853. }
  854. }
  855. return (&found_caller);
  856. }
  857. /* find the function definition(s) */
  858. while(scanpast('\t') != NULL) {
  859. switch(*blockp) {
  860. case NEWFILE:
  861. skiprefchar(); /* save file name */
  862. fetch_string_from_dbase(file, sizeof(file));
  863. if(*file == '\0') { /* if end of symbols */
  864. return (&found_caller);
  865. }
  866. progress("Search", searchcount, nsrcfiles);
  867. break;
  868. case DEFINE: /* could be a macro */
  869. if(fileversion < 10) { break; }
  870. macro = true;
  871. /* FALLTHROUGH */
  872. case FCNDEF:
  873. skiprefchar(); /* match name to pattern */
  874. if(match()) {
  875. found_caller = 'y';
  876. findcalledbysub(file, macro);
  877. }
  878. break;
  879. }
  880. }
  881. return (&found_caller);
  882. }
  883. /* find this term, which can be a regular expression */
  884. static void findterm(const char *pattern) {
  885. char *s;
  886. int len;
  887. char prefix[PATLEN + 1];
  888. char term[PATLEN + 1];
  889. npostings = 0; /* will be non-zero after database built */
  890. lastfcnoffset = 0; /* clear the last function name found */
  891. boolclear(); /* clear the posting set */
  892. /* get the string prefix (if any) of the regular expression */
  893. (void)strcpy(prefix, pattern);
  894. if((s = strpbrk(prefix, ".[{*+")) != NULL) { *s = '\0'; }
  895. /* if letter case is to be ignored */
  896. if(caseless == true) {
  897. /* convert the prefix to upper case because it is lexically
  898. less than lower case */
  899. s = prefix;
  900. while(*s != '\0') {
  901. *s = toupper((unsigned char)*s);
  902. ++s;
  903. }
  904. }
  905. /* find the term lexically >= the prefix */
  906. (void)invfind(&invcontrol, prefix);
  907. if(caseless == true) { /* restore lower case */
  908. (void)strcpy(prefix, lcasify(prefix));
  909. }
  910. /* a null prefix matches the null term in the inverted index,
  911. so move to the first real term */
  912. if(*prefix == '\0') { (void)invforward(&invcontrol); }
  913. len = strlen(prefix);
  914. do {
  915. (void)invterm(&invcontrol, term); /* get the term */
  916. s = term;
  917. if(caseless == true) { s = lcasify(s); /* make it lower case */ }
  918. /* if it matches */
  919. if(regexec(&regexp, s, (size_t)0, NULL, 0) == 0) {
  920. /* add its postings to the set */
  921. if((postingp = boolfile(&invcontrol, &npostings, bool_OR)) == NULL) { break; }
  922. }
  923. /* if there is a prefix */
  924. else if(len > 0) {
  925. /* if ignoring letter case and the term is out of the
  926. range of possible matches */
  927. if(caseless == true) {
  928. if(strncmp(term, prefix, len) > 0) { break; /* stop searching */ }
  929. }
  930. /* if using letter case and the prefix doesn't match */
  931. else if(strncmp(term, prefix, len) != 0) {
  932. break; /* stop searching */
  933. }
  934. }
  935. /* display progress about every three seconds */
  936. if(++searchcount % 50 == 0) {
  937. progress("Symbols matched", searchcount, totalterms);
  938. }
  939. } while(invforward(&invcontrol)); /* while didn't wrap around */
  940. /* initialize the progress message for retrieving the references */
  941. searchcount = 0;
  942. postingsfound = npostings;
  943. }
  944. /* get the next posting for this term */
  945. static POSTING *getposting(void) {
  946. if(npostings-- <= 0) { return (NULL); }
  947. /* display progress about every three seconds */
  948. if(++searchcount % 100 == 0) {
  949. progress("Possible references retrieved", searchcount, postingsfound);
  950. }
  951. return (postingp++);
  952. }
  953. /* put the posting reference into the file */
  954. static void putpostingref(POSTING *p, const char *pat) {
  955. // initialize function to "unknown" so that the first line of temp1
  956. // is properly formed if symbol matches a header file entry first time
  957. static char function[PATLEN + 1] = "unknown"; /* function name */
  958. if(p->fcnoffset == 0) {
  959. if(p->type == FCNDEF) { /* need to find the function name */
  960. if(dbseek(p->lineoffset) != -1) {
  961. scanpast(FCNDEF);
  962. fetch_string_from_dbase(function, sizeof(function));
  963. }
  964. } else if(p->type != FCNCALL) {
  965. strcpy(function, global);
  966. }
  967. } else if(p->fcnoffset != lastfcnoffset) {
  968. if(dbseek(p->fcnoffset) != -1) {
  969. fetch_string_from_dbase(function, sizeof(function));
  970. lastfcnoffset = p->fcnoffset;
  971. }
  972. }
  973. if(dbseek(p->lineoffset) != -1) {
  974. if(pat)
  975. putref(0, srcfiles[p->fileindex], pat);
  976. else
  977. putref(0, srcfiles[p->fileindex], function);
  978. }
  979. }
  980. /* seek to the database offset */
  981. long dbseek(long offset) {
  982. long n;
  983. int rc = 0;
  984. if((n = offset / BUFSIZ) != blocknumber) {
  985. if((rc = lseek(symrefs, n * BUFSIZ, 0)) == -1) {
  986. myperror("Lseek failed");
  987. (void)sleep(3);
  988. return (rc);
  989. }
  990. (void)read_block();
  991. blocknumber = n;
  992. }
  993. blockp = block + offset % BUFSIZ;
  994. return (rc);
  995. }
  996. static void findcalledbysub(const char *file, bool macro) {
  997. /* find the next function call or the end of this function */
  998. while(scanpast('\t') != NULL) {
  999. switch(*blockp) {
  1000. case DEFINE: /* #define inside a function */
  1001. if(fileversion >= 10) { /* skip it */
  1002. while(scanpast('\t') != NULL && *blockp != DEFINEEND)
  1003. ;
  1004. }
  1005. break;
  1006. case FCNCALL: /* function call */
  1007. /* output the file name */
  1008. (void)fprintf(refsfound, "%s ", file);
  1009. /* output the function name */
  1010. skiprefchar();
  1011. putline(refsfound);
  1012. (void)putc(' ', refsfound);
  1013. /* output the source line */
  1014. putsource(1, refsfound);
  1015. break;
  1016. case DEFINEEND: /* #define end */
  1017. if(invertedindex == false) {
  1018. if(macro == true) { return; }
  1019. break; /* inside a function */
  1020. }
  1021. /* FALLTHROUGH */
  1022. case FCNDEF: /* function end (pre 9.5) */
  1023. if(invertedindex == false) break;
  1024. /* FALLTHROUGH */
  1025. case FCNEND: /* function end */
  1026. case NEWFILE: /* file end */
  1027. return;
  1028. }
  1029. }
  1030. }
  1031. /* open the references found file for writing */
  1032. bool writerefsfound(void) {
  1033. if(refsfound == NULL) {
  1034. if((refsfound = myfopen(temp1, "wb")) == NULL) {
  1035. cannotopen(temp1);
  1036. return (false);
  1037. }
  1038. } else {
  1039. (void)fclose(refsfound);
  1040. if((refsfound = myfopen(temp1, "wb")) == NULL) {
  1041. postmsg("Cannot reopen temporary file");
  1042. return (false);
  1043. }
  1044. }
  1045. return (true);
  1046. }
  1047. /* Perform token search based on "field" */
  1048. bool search(const char *query) {
  1049. char msg[MSGLEN + 1];
  1050. char *findresult = NULL; /* find function output */
  1051. bool funcexist = true; /* find "function" error */
  1052. FINDINIT rc = NOERROR; /* findinit return code */
  1053. sighandler_t savesig; /* old value of signal */
  1054. FP f; /* searching function */
  1055. int c;
  1056. /* open the references found file for writing */
  1057. if(writerefsfound() == false) { return (false); }
  1058. /* find the pattern - stop on an interrupt */
  1059. if(linemode == false) { postmsg("Searching"); }
  1060. searchcount = 0;
  1061. savesig = signal(SIGINT, jumpback);
  1062. if(sigsetjmp(env, 1) == 0) {
  1063. f = field_searchers[field];
  1064. if(f == findregexp || f == findstring) {
  1065. findresult = (*f)(query);
  1066. } else {
  1067. if((nonglobalrefs = myfopen(temp2, "wb")) == NULL) {
  1068. cannotopen(temp2);
  1069. return (false);
  1070. }
  1071. if((rc = findinit(query)) == NOERROR) {
  1072. (void)dbseek(0L); /* read the first block */
  1073. findresult = (*f)(query);
  1074. if(f == findcalledby) funcexist = (*findresult == 'y');
  1075. findcleanup();
  1076. /* append the non-global references */
  1077. (void)fclose(nonglobalrefs);
  1078. if((nonglobalrefs = myfopen(temp2, "rb")) == NULL) {
  1079. cannotopen(temp2);
  1080. return (false);
  1081. }
  1082. while((c = getc(nonglobalrefs)) != EOF) {
  1083. (void)putc(c, refsfound);
  1084. }
  1085. }
  1086. (void)fclose(nonglobalrefs);
  1087. }
  1088. }
  1089. signal(SIGINT, savesig);
  1090. /* rewind the cross-reference file */
  1091. (void)lseek(symrefs, (long)0, 0);
  1092. /* reopen the references found file for reading */
  1093. (void)fclose(refsfound);
  1094. if((refsfound = myfopen(temp1, "rb")) == NULL) {
  1095. cannotopen(temp1);
  1096. return (false);
  1097. }
  1098. totallines = 0;
  1099. disprefs = 0;
  1100. /* see if it is empty */
  1101. if((c = getc(refsfound)) == EOF) {
  1102. if(findresult != NULL) {
  1103. (void)snprintf(msg,
  1104. sizeof(msg),
  1105. "Egrep %s in this pattern: %s",
  1106. findresult,
  1107. query);
  1108. } else if(rc == NOTSYMBOL) {
  1109. (void)snprintf(msg, sizeof(msg), "This is not a C symbol: %s", query);
  1110. } else if(rc == REGCMPERROR) {
  1111. (void)snprintf(msg,
  1112. sizeof(msg),
  1113. "Error in this regcomp(3) regular expression: %s",
  1114. query);
  1115. } else if(funcexist == false) {
  1116. (void)snprintf(msg,
  1117. sizeof(msg),
  1118. "Function definition does not exist: %s",
  1119. query);
  1120. } else {
  1121. (void)snprintf(msg,
  1122. sizeof(msg),
  1123. "Could not find the %s: %s",
  1124. fields[field].text2,
  1125. query);
  1126. }
  1127. postmsg(msg);
  1128. return (false);
  1129. }
  1130. /* put back the character read */
  1131. (void)ungetc(c, refsfound);
  1132. countrefs();
  1133. window_change |= CH_RESULT;
  1134. return (true);
  1135. }