search.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. /* search.c -- searching large bodies of text.
  2. $Id$
  3. Copyright 1993, 1997, 1998, 2002, 2004, 2007, 2008, 2009, 2011, 2013,
  4. 2014, 2015, 2016, 2017 Free Software Foundation, Inc.
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. Originally written by Brian Fox. */
  16. #include "info.h"
  17. #include <regex.h>
  18. #include "session.h"
  19. #include "info-utils.h"
  20. #include "search.h"
  21. /* **************************************************************** */
  22. /* */
  23. /* The Actual Searching Functions */
  24. /* */
  25. /* **************************************************************** */
  26. /* Search forwards or backwards for the text delimited by BINDING.
  27. The search is forwards if BINDING->start is greater than BINDING->end. */
  28. enum search_result
  29. search (char *string, SEARCH_BINDING *binding, long *poff)
  30. {
  31. enum search_result result;
  32. /* If the search is backwards, then search backwards, otherwise forwards. */
  33. if (binding->start > binding->end)
  34. result = search_backward (string, binding, poff);
  35. else
  36. result = search_forward (string, binding, poff);
  37. return result;
  38. }
  39. /* Expand \n and \t in regexp to newlines and tabs */
  40. static char *
  41. regexp_expand_newlines_and_tabs (char *regexp)
  42. {
  43. char *unescaped_regexp = xmalloc (1 + strlen (regexp));
  44. char *p, *q;
  45. for (p = regexp, q = unescaped_regexp; *p != '\0'; p++, q++)
  46. {
  47. if (*p == '\\')
  48. switch(*++p)
  49. {
  50. case 'n':
  51. *q = '\n';
  52. break;
  53. case 't':
  54. *q = '\t';
  55. break;
  56. case '\0':
  57. *q = '\\';
  58. p--;
  59. break;
  60. default:
  61. *q++ = '\\';
  62. *q = *p;
  63. break;
  64. }
  65. else
  66. *q = *p;
  67. }
  68. *q = '\0';
  69. return unescaped_regexp;
  70. }
  71. /* Escape any special characters in SEARCH_STRING. */
  72. static char *
  73. regexp_escape_string (char *search_string)
  74. {
  75. char *special_chars = "\\[]^$.*(){}|+?";
  76. char *p, *q;
  77. char *escaped_string = xmalloc (strlen (search_string) * 2 + 1);
  78. for (p = search_string, q = escaped_string; *p != '\0'; )
  79. {
  80. if (strchr (special_chars, *p))
  81. {
  82. *q++ = '\\';
  83. }
  84. *q++ = *p++;
  85. }
  86. *q = '\0';
  87. return escaped_string;
  88. }
  89. static void
  90. extend_matches (MATCH_STATE *state)
  91. {
  92. regmatch_t *matches = state->matches;
  93. size_t match_alloc = state->match_alloc;
  94. size_t match_count = state->match_count;
  95. char *buffer = state->buffer;
  96. size_t buflen = state->buflen;
  97. regoff_t offset = 0;
  98. char saved_char;
  99. size_t initial_match_count = match_count;
  100. if (state->finished)
  101. return;
  102. saved_char = buffer[buflen];
  103. buffer[buflen] = '\0';
  104. if (match_count > 0)
  105. {
  106. offset = matches[match_count - 1].rm_eo;
  107. /* move past zero-length match */
  108. if (offset == matches[match_count - 1].rm_so)
  109. offset++;
  110. }
  111. while (offset < buflen && match_count < initial_match_count + 5)
  112. {
  113. int result = 0;
  114. regmatch_t m;
  115. result = regexec (&state->regex, &buffer[offset], 1, &m, REG_NOTBOL);
  116. if (result == 0)
  117. {
  118. if (match_count == match_alloc)
  119. {
  120. /* The match list is full. */
  121. if (match_alloc == 0)
  122. match_alloc = 50;
  123. matches = x2nrealloc
  124. (matches, &match_alloc, sizeof matches[0]);
  125. }
  126. matches[match_count] = m;
  127. matches[match_count].rm_so += offset;
  128. matches[match_count].rm_eo += offset;
  129. offset = matches[match_count++].rm_eo;
  130. if (m.rm_eo == 0)
  131. offset++; /* Avoid finding match again for a pattern of "$". */
  132. }
  133. else
  134. {
  135. state->finished = 1;
  136. break;
  137. }
  138. }
  139. buffer[buflen] = saved_char;
  140. state->matches = matches;
  141. state->match_alloc = match_alloc;
  142. state->match_count = match_count;
  143. }
  144. /* Search BUFFER for REGEXP. Pass back the list of matches
  145. in MATCH_STATE. */
  146. enum search_result
  147. regexp_search (char *regexp, int is_literal, int is_insensitive,
  148. char *buffer, size_t buflen,
  149. MATCH_STATE *match_state)
  150. {
  151. regex_t preg; /* Compiled pattern buffer for regexp. */
  152. int result;
  153. char *regexp_str;
  154. if (!is_literal)
  155. regexp_str = regexp_expand_newlines_and_tabs (regexp);
  156. else
  157. regexp_str = regexp_escape_string (regexp);
  158. result = regcomp (&preg, regexp_str,
  159. REG_EXTENDED | REG_NEWLINE
  160. | (is_insensitive ? REG_ICASE : 0));
  161. free (regexp_str);
  162. if (result != 0)
  163. {
  164. int size = regerror (result, &preg, NULL, 0);
  165. char *buf = xmalloc (size);
  166. regerror (result, &preg, buf, size);
  167. info_error (_("regexp error: %s"), buf);
  168. return search_invalid;
  169. }
  170. match_state->matches = 0;
  171. match_state->match_count = 0;
  172. match_state->match_alloc = 0;
  173. match_state->finished = 0;
  174. match_state->regex = preg;
  175. match_state->buffer = buffer;
  176. match_state->buflen = buflen;
  177. extend_matches (match_state);
  178. if (match_state->match_count == 0)
  179. return search_not_found;
  180. else
  181. return search_success;
  182. }
  183. /* Search forwards for STRING through the text delimited in BINDING. */
  184. enum search_result
  185. search_forward (char *string, SEARCH_BINDING *binding, long *poff)
  186. {
  187. register int c, i, len;
  188. register char *buff, *end;
  189. char *alternate = NULL;
  190. len = strlen (string);
  191. /* We match characters in the search buffer against STRING and ALTERNATE.
  192. ALTERNATE is a case reversed version of STRING; this is cheaper than
  193. case folding each character before comparison. Alternate is only
  194. used if the case folding bit is turned on in the passed BINDING. */
  195. if (binding->flags & S_FoldCase)
  196. {
  197. alternate = xstrdup (string);
  198. for (i = 0; i < len; i++)
  199. {
  200. if (islower (alternate[i]))
  201. alternate[i] = toupper (alternate[i]);
  202. else if (isupper (alternate[i]))
  203. alternate[i] = tolower (alternate[i]);
  204. }
  205. }
  206. buff = binding->buffer + binding->start;
  207. end = binding->buffer + binding->end + 1;
  208. while (buff < (end - len))
  209. {
  210. for (i = 0; i < len; i++)
  211. {
  212. c = buff[i];
  213. if ((c != string[i]) && (!alternate || c != alternate[i]))
  214. break;
  215. }
  216. if (!string[i])
  217. {
  218. if (alternate)
  219. free (alternate);
  220. if (binding->flags & S_SkipDest)
  221. buff += len;
  222. *poff = buff - binding->buffer;
  223. return search_success;
  224. }
  225. buff++;
  226. }
  227. if (alternate)
  228. free (alternate);
  229. return search_not_found;
  230. }
  231. /* Search for STRING backwards through the text delimited in BINDING. */
  232. enum search_result
  233. search_backward (char *input_string, SEARCH_BINDING *binding, long *poff)
  234. {
  235. register int c, i, len;
  236. register char *buff, *end;
  237. char *string;
  238. char *alternate = NULL;
  239. len = strlen (input_string);
  240. /* Reverse the characters in the search string. */
  241. string = xmalloc (1 + len);
  242. for (c = 0, i = len - 1; input_string[c]; c++, i--)
  243. string[i] = input_string[c];
  244. string[c] = '\0';
  245. /* We match characters in the search buffer against STRING and ALTERNATE.
  246. ALTERNATE is a case reversed version of STRING; this is cheaper than
  247. case folding each character before comparison. ALTERNATE is only
  248. used if the case folding bit is turned on in the passed BINDING. */
  249. if (binding->flags & S_FoldCase)
  250. {
  251. alternate = xstrdup (string);
  252. for (i = 0; i < len; i++)
  253. {
  254. if (islower (alternate[i]))
  255. alternate[i] = toupper (alternate[i]);
  256. else if (isupper (alternate[i]))
  257. alternate[i] = tolower (alternate[i]);
  258. }
  259. }
  260. buff = binding->buffer + binding->start - 1;
  261. end = binding->buffer + binding->end;
  262. while (buff > (end + len))
  263. {
  264. for (i = 0; i < len; i++)
  265. {
  266. c = *(buff - i);
  267. if (c != string[i] && (!alternate || c != alternate[i]))
  268. break;
  269. }
  270. if (!string[i])
  271. {
  272. free (string);
  273. if (alternate)
  274. free (alternate);
  275. if (binding->flags & S_SkipDest)
  276. buff -= len;
  277. *poff = 1 + buff - binding->buffer;
  278. return search_success;
  279. }
  280. buff--;
  281. }
  282. free (string);
  283. if (alternate)
  284. free (alternate);
  285. return search_not_found;
  286. }
  287. /* Find STRING in LINE, returning the offset of the end of the string.
  288. Return an offset of -1 if STRING does not appear in LINE. The search
  289. is bound by the end of the line (i.e., either NEWLINE or 0). */
  290. int
  291. string_in_line (char *string, char *line)
  292. {
  293. register int end;
  294. SEARCH_BINDING binding;
  295. long offset;
  296. /* Find the end of the line. */
  297. for (end = 0; line[end] && line[end] != '\n'; end++);
  298. /* Search for STRING within these confines. */
  299. binding.buffer = line;
  300. binding.start = 0;
  301. binding.end = end;
  302. binding.flags = S_FoldCase | S_SkipDest;
  303. if (search_forward (string, &binding, &offset) == search_success)
  304. return offset;
  305. return -1;
  306. }
  307. /* Return non-zero if STRING is the first text to appear at BINDING. */
  308. int
  309. looking_at (char *string, SEARCH_BINDING *binding)
  310. {
  311. long search_end;
  312. if (search (string, binding, &search_end) != search_success)
  313. return 0;
  314. /* If the string was not found, SEARCH_END is -1. If the string was found,
  315. but not right away, SEARCH_END is != binding->start. Otherwise, the
  316. string was found at binding->start. */
  317. return search_end == binding->start;
  318. }
  319. /* Return non-zero if POINTER is looking at the text at STRING before an
  320. end-of-line. */
  321. int
  322. looking_at_line (char *string, char *pointer)
  323. {
  324. int len;
  325. len = strlen (string);
  326. if (strncasecmp (pointer, string, len) != 0)
  327. return 0;
  328. pointer += len;
  329. if (*pointer == '\n' || !strncmp (pointer, "\r\n", 2)
  330. || *pointer == '\0')
  331. return 1;
  332. return 0;
  333. }
  334. /* **************************************************************** */
  335. /* */
  336. /* Accessing matches */
  337. /* */
  338. /* **************************************************************** */
  339. /* Search forwards or backwards for entries in MATCHES that start within
  340. the search area. The search is forwards if DIR > 0, backward if
  341. DIR < 0. Return index of match in *MATCH_INDEX. */
  342. enum search_result
  343. match_in_match_list (MATCH_STATE *match_state,
  344. long start, long end, int dir,
  345. int *match_index)
  346. {
  347. regmatch_t *matches = match_state->matches;
  348. size_t match_count = match_state->match_count;
  349. int i;
  350. int index = -1;
  351. for (i = 0; i < match_count || !match_state->finished; i++)
  352. {
  353. /* get more matches as we need them */
  354. if (i == match_count)
  355. {
  356. extend_matches (match_state);
  357. matches = match_state->matches;
  358. match_count = match_state->match_count;
  359. if (i == match_count)
  360. break;
  361. }
  362. if (matches[i].rm_so >= end)
  363. break; /* No more matches found in search area. */
  364. if (matches[i].rm_so >= start)
  365. {
  366. index = i;
  367. if (dir > 0)
  368. {
  369. *match_index = index;
  370. return search_success;
  371. }
  372. }
  373. }
  374. if (index != -1)
  375. {
  376. *match_index = index;
  377. return search_success;
  378. }
  379. /* not found */
  380. return search_not_found;
  381. }
  382. /* Return match INDEX in STATE. INDEX must be a valid index. */
  383. regmatch_t
  384. match_by_index (MATCH_STATE *state, int index)
  385. {
  386. while (state->match_alloc <= index)
  387. extend_matches (state);
  388. return state->matches[index];
  389. }
  390. /* Free and clear all data in STATE. */
  391. void
  392. free_matches (MATCH_STATE *state)
  393. {
  394. free (state->matches);
  395. state->matches = 0;
  396. state->match_count = state->match_alloc = state->finished = 0;
  397. state->buffer = 0; /* do not free as it is kept elsewhere */
  398. state->buflen = 0;
  399. regfree (&state->regex);
  400. }
  401. int
  402. matches_ready (MATCH_STATE *state)
  403. {
  404. return state->matches ? 1 : 0;
  405. }
  406. /* Starting at index *MATCH_INDEX, decide if we are inside a match
  407. in MATCHES at offset OFF. The matches are assumed not to overlap
  408. and to be in order. */
  409. void
  410. decide_if_in_match (long off, int *in_match,
  411. MATCH_STATE *matches, size_t *match_index)
  412. {
  413. size_t i = *match_index;
  414. int m = *in_match;
  415. for (; !at_end_of_matches (matches, i); i++)
  416. {
  417. if (match_by_index (matches, i).rm_so > off)
  418. break;
  419. m = 1;
  420. if (match_by_index (matches, i).rm_eo > off)
  421. break;
  422. m = 0;
  423. }
  424. *match_index = i;
  425. *in_match = m;
  426. }
  427. /* Used for iterating through a match list. */
  428. int
  429. at_end_of_matches (MATCH_STATE *state, int index)
  430. {
  431. if (index < state->match_count)
  432. return 0;
  433. else
  434. {
  435. if (!state->finished)
  436. extend_matches (state);
  437. if (state->finished)
  438. return (state->match_count == index) ? 1 : 0;
  439. else
  440. return 0;
  441. }
  442. }
  443. /* **************************************************************** */
  444. /* */
  445. /* Small String Searches */
  446. /* */
  447. /* **************************************************************** */
  448. /* Function names that start with "skip" are passed a string, and return
  449. an offset from the start of that string. Function names that start
  450. with "find" are passed a SEARCH_BINDING, and return an absolute position
  451. marker of the item being searched for. "Find" functions return a value
  452. of -1 if the item being looked for couldn't be found. */
  453. /* Return the index of the first non-whitespace character in STRING. */
  454. int
  455. skip_whitespace (char *string)
  456. {
  457. register int i;
  458. for (i = 0; string && whitespace (string[i]); i++);
  459. return i;
  460. }
  461. /* Return the index of the first non-whitespace or newline character in
  462. STRING. */
  463. int
  464. skip_whitespace_and_newlines (char *string)
  465. {
  466. register int i;
  467. for (i = 0; string && whitespace_or_newline (string[i]); i++);
  468. return i;
  469. }
  470. /* Return the index of the first whitespace character in STRING. */
  471. int
  472. skip_non_whitespace (char *string)
  473. {
  474. register int i;
  475. for (i = 0; string && string[i] && !whitespace (string[i]); i++);
  476. return i;
  477. }
  478. /* **************************************************************** */
  479. /* */
  480. /* Searching FILE_BUFFER's */
  481. /* */
  482. /* **************************************************************** */
  483. /* Return the absolute position of the first occurence of a node separator
  484. starting in BINDING->buffer between BINDING->start and BINDING->end
  485. inclusive. Return -1 if no node separator was found. */
  486. long
  487. find_node_separator (SEARCH_BINDING *binding)
  488. {
  489. register long i;
  490. char *body;
  491. int dir;
  492. body = binding->buffer;
  493. dir = binding->start < binding->end ? 1 : -1;
  494. /* A node is started by [^L]^_[^L][\r]\n. That is to say, the C-l's are
  495. optional, but the US and NEWLINE are not. This separator holds
  496. true for all separated elements in an Info file, including the tags
  497. table (if present) and the indirect tags table (if present). */
  498. i = binding->start;
  499. while (1)
  500. {
  501. /* Note that bytes are read in order from the buffer, so if at any
  502. point a null byte is encountered signifying the end of the buffer,
  503. no more bytes will be read past that point. */
  504. if (body[i] == INFO_COOKIE)
  505. {
  506. int j = i + 1;
  507. if (body[j] == INFO_FF)
  508. j++;
  509. if (body[j] == '\r')
  510. j++;
  511. if (body[j] == '\n')
  512. return i;
  513. }
  514. if (i == binding->end)
  515. break;
  516. i += dir;
  517. }
  518. return -1;
  519. }
  520. /* Return the length of the node separator characters that BODY is currently
  521. pointing at. If it's not pointing at a node separator, return 0. */
  522. int
  523. skip_node_separator (char *body)
  524. {
  525. register int i;
  526. i = 0;
  527. if (body[i] == INFO_FF)
  528. i++;
  529. if (body[i++] != INFO_COOKIE)
  530. return 0;
  531. if (body[i] == INFO_FF)
  532. i++;
  533. if (body[i] == '\r')
  534. i++;
  535. if (body[i++] != '\n')
  536. return 0;
  537. return i;
  538. }
  539. /* Return the absolute position of the beginning of a section in this file
  540. whose first line is LABEL, starting the search at binding->start. */
  541. long
  542. find_file_section (SEARCH_BINDING *binding, char *label)
  543. {
  544. SEARCH_BINDING s;
  545. long position;
  546. int dir;
  547. s.buffer = binding->buffer;
  548. s.start = binding->start;
  549. s.end = binding->end;
  550. s.flags = S_FoldCase;
  551. dir = binding->start < binding->end ? 1 : -1;
  552. while ((position = find_node_separator (&s)) != -1 )
  553. {
  554. long offset = position;
  555. offset += skip_node_separator (s.buffer + offset);
  556. if (looking_at_line (label, s.buffer + offset))
  557. return position;
  558. if (dir > 0)
  559. {
  560. s.start = offset;
  561. if (s.start >= s.end)
  562. break;
  563. }
  564. else
  565. {
  566. s.start = position - 1;
  567. if (s.start <= s.end)
  568. break;
  569. }
  570. }
  571. return -1;
  572. }
  573. /* Return the absolute position of the node named NODENAME in BINDING.
  574. This is a brute force search, and we wish to avoid it when possible.
  575. This function is called when a tag (indirect or otherwise) doesn't
  576. really point to the right node. It returns the absolute position of
  577. the separator preceding the node. */
  578. long
  579. find_node_in_binding (char *nodename, SEARCH_BINDING *binding)
  580. {
  581. long position;
  582. int offset;
  583. SEARCH_BINDING s;
  584. s.buffer = binding->buffer;
  585. s.start = binding->start;
  586. s.end = binding->end;
  587. s.flags = 0;
  588. while ((position = find_node_separator (&s)) != -1)
  589. {
  590. char *nodename_start;
  591. char *read_nodename;
  592. int found;
  593. s.start = position;
  594. s.start += skip_node_separator (s.buffer + s.start);
  595. offset = string_in_line (INFO_NODE_LABEL, s.buffer + s.start);
  596. if (offset == -1)
  597. continue;
  598. s.start += offset;
  599. s.start += skip_whitespace (s.buffer + s.start);
  600. nodename_start = s.buffer + s.start;
  601. read_quoted_string (nodename_start, "\n\r\t,", 0, &read_nodename);
  602. if (!read_nodename)
  603. return -1;
  604. found = !strcmp (read_nodename, nodename);
  605. free (read_nodename);
  606. if (found)
  607. return position;
  608. }
  609. return -1;
  610. }