1 // -*- C++ -*-
2 /* Copyright (C) 1989, 1990, 1991, 1992, 2001 Free Software Foundation, Inc.
3 Written by James Clark (jjc@jclark.com)
4
5 This file is part of groff.
6
7 groff is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 groff is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License along
18 with groff; see the file COPYING. If not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21 #include "lib.h"
22
23 #include <stdlib.h>
24 #include <errno.h>
25
26 #include "posix.h"
27 #include "cset.h"
28 #include "cmap.h"
29 #include "errarg.h"
30 #include "error.h"
31
32 #include "refid.h"
33 #include "search.h"
34 #include "index.h"
35 #include "defs.h"
36
37 #include "nonposix.h"
38
39 // Interface to mmap.
40 extern "C" {
41 void *mapread(int fd, int len);
42 int unmap(void *, int len);
43 }
44
45 #if 0
46 const
47 #endif
48 int minus_one = -1;
49
50 int verify_flag = 0;
51
52 struct word_list;
53
54 class index_search_item : public search_item {
55 search_item *out_of_date_files;
56 index_header header;
57 char *buffer;
58 void *map_addr;
59 int map_len;
60 tag *tags;
61 int *table;
62 int *lists;
63 char *pool;
64 char *key_buffer;
65 char *filename_buffer;
66 int filename_buflen;
67 char **common_words_table;
68 int common_words_table_size;
69 const char *ignore_fields;
70 time_t mtime;
71
72 const char *do_verify();
73 const int *search1(const char **pp, const char *end);
74 const int *search(const char *ptr, int length, int **temp_listp);
75 const char *munge_filename(const char *);
76 void read_common_words_file();
77 void add_out_of_date_file(int fd, const char *filename, int fid);
78 public:
79 index_search_item(const char *, int);
80 ~index_search_item();
81 int load(int fd);
82 search_item_iterator *make_search_item_iterator(const char *);
83 int verify();
84 void check_files();
85 int next_filename_id() const;
86 friend class index_search_item_iterator;
87 };
88
89 class index_search_item_iterator : public search_item_iterator {
90 index_search_item *indx;
91 search_item_iterator *out_of_date_files_iter;
92 search_item *next_out_of_date_file;
93 const int *found_list;
94 int *temp_list;
95 char *buf;
96 int buflen;
97 linear_searcher searcher;
98 char *query;
99 int get_tag(int tagno, const linear_searcher &, const char **, int *,
100 reference_id *);
101 public:
102 index_search_item_iterator(index_search_item *, const char *);
103 ~index_search_item_iterator();
104 int next(const linear_searcher &, const char **, int *, reference_id *);
105 };
106
107
index_search_item(const char * filename,int fid)108 index_search_item::index_search_item(const char *filename, int fid)
109 : search_item(filename, fid), out_of_date_files(0), buffer(0), map_addr(0),
110 map_len(0), key_buffer(0), filename_buffer(0), filename_buflen(0),
111 common_words_table(0)
112 {
113 }
114
~index_search_item()115 index_search_item::~index_search_item()
116 {
117 if (buffer)
118 free(buffer);
119 if (map_addr) {
120 if (unmap(map_addr, map_len) < 0)
121 error("unmap: %1", strerror(errno));
122 }
123 while (out_of_date_files) {
124 search_item *tem = out_of_date_files;
125 out_of_date_files = out_of_date_files->next;
126 delete tem;
127 }
128 a_delete filename_buffer;
129 a_delete key_buffer;
130 if (common_words_table) {
131 for (int i = 0; i < common_words_table_size; i++)
132 a_delete common_words_table[i];
133 a_delete common_words_table;
134 }
135 }
136
137 class file_closer {
138 int *fdp;
139 public:
file_closer(int & fd)140 file_closer(int &fd) : fdp(&fd) { }
~file_closer()141 ~file_closer() { close(*fdp); }
142 };
143
144 // Tell the compiler that a variable is intentionally unused.
unused(void *)145 inline void unused(void *) { }
146
load(int fd)147 int index_search_item::load(int fd)
148 {
149 file_closer fd_closer(fd); // close fd on return
150 unused(&fd_closer);
151 struct stat sb;
152 if (fstat(fd, &sb) < 0) {
153 error("can't fstat `%1': %2", name, strerror(errno));
154 return 0;
155 }
156 if (!S_ISREG(sb.st_mode)) {
157 error("`%1' is not a regular file", name);
158 return 0;
159 }
160 mtime = sb.st_mtime;
161 int size = int(sb.st_size);
162 char *addr;
163 map_addr = mapread(fd, size);
164 if (map_addr) {
165 addr = (char *)map_addr;
166 map_len = size;
167 }
168 else {
169 addr = buffer = (char *)malloc(size);
170 if (buffer == 0) {
171 error("can't allocate buffer for `%1'", name);
172 return 0;
173 }
174 char *ptr = buffer;
175 int bytes_to_read = size;
176 while (bytes_to_read > 0) {
177 int nread = read(fd, ptr, bytes_to_read);
178 if (nread == 0) {
179 error("unexpected EOF on `%1'", name);
180 return 0;
181 }
182 if (nread < 0) {
183 error("read error on `%1': %2", name, strerror(errno));
184 return 0;
185 }
186 bytes_to_read -= nread;
187 ptr += nread;
188 }
189 }
190 header = *(index_header *)addr;
191 if (header.magic != INDEX_MAGIC) {
192 error("`%1' is not an index file: wrong magic number", name);
193 return 0;
194 }
195 if (header.version != INDEX_VERSION) {
196 error("version number in `%1' is wrong: was %2, should be %3",
197 name, header.version, INDEX_VERSION);
198 return 0;
199 }
200 int sz = (header.tags_size * sizeof(tag)
201 + header.lists_size * sizeof(int)
202 + header.table_size * sizeof(int)
203 + header.strings_size
204 + sizeof(header));
205 if (sz != size) {
206 error("size of `%1' is wrong: was %2, should be %3",
207 name, size, sz);
208 return 0;
209 }
210 tags = (tag *)(addr + sizeof(header));
211 lists = (int *)(tags + header.tags_size);
212 table = (int *)(lists + header.lists_size);
213 pool = (char *)(table + header.table_size);
214 ignore_fields = strchr(strchr(pool, '\0') + 1, '\0') + 1;
215 key_buffer = new char[header.truncate];
216 read_common_words_file();
217 return 1;
218 }
219
do_verify()220 const char *index_search_item::do_verify()
221 {
222 if (tags == 0)
223 return "not loaded";
224 if (lists[header.lists_size - 1] >= 0)
225 return "last list element not negative";
226 int i;
227 for (i = 0; i < header.table_size; i++) {
228 int li = table[i];
229 if (li >= header.lists_size)
230 return "bad list index";
231 if (li >= 0) {
232 for (int *ptr = lists + li; *ptr >= 0; ptr++) {
233 if (*ptr >= header.tags_size)
234 return "bad tag index";
235 if (*ptr >= ptr[1] && ptr[1] >= 0)
236 return "list not ordered";
237 }
238 }
239 }
240 for (i = 0; i < header.tags_size; i++) {
241 if (tags[i].filename_index >= header.strings_size)
242 return "bad index in tags";
243 if (tags[i].length < 0)
244 return "bad length in tags";
245 if (tags[i].start < 0)
246 return "bad start in tags";
247 }
248 if (pool[header.strings_size - 1] != '\0')
249 return "last character in pool not nul";
250 return 0;
251 }
252
verify()253 int index_search_item::verify()
254 {
255 const char *reason = do_verify();
256 if (!reason)
257 return 1;
258 error("`%1' is bad: %2", name, reason);
259 return 0;
260 }
261
next_filename_id() const262 int index_search_item::next_filename_id() const
263 {
264 return filename_id + header.strings_size + 1;
265 }
266
make_search_item_iterator(const char * query)267 search_item_iterator *index_search_item::make_search_item_iterator(
268 const char *query)
269 {
270 return new index_search_item_iterator(this, query);
271 }
272
make_index_search_item(const char * filename,int fid)273 search_item *make_index_search_item(const char *filename, int fid)
274 {
275 char *index_filename = new char[strlen(filename) + sizeof(INDEX_SUFFIX)];
276 strcpy(index_filename, filename);
277 strcat(index_filename, INDEX_SUFFIX);
278 int fd = open(index_filename, O_RDONLY | O_BINARY);
279 if (fd < 0)
280 return 0;
281 index_search_item *item = new index_search_item(index_filename, fid);
282 a_delete index_filename;
283 if (!item->load(fd)) {
284 close(fd);
285 delete item;
286 return 0;
287 }
288 else if (verify_flag && !item->verify()) {
289 delete item;
290 return 0;
291 }
292 else {
293 item->check_files();
294 return item;
295 }
296 }
297
298
index_search_item_iterator(index_search_item * ind,const char * q)299 index_search_item_iterator::index_search_item_iterator(index_search_item *ind,
300 const char *q)
301 : indx(ind), out_of_date_files_iter(0), next_out_of_date_file(0), temp_list(0),
302 buf(0), buflen(0),
303 searcher(q, strlen(q), ind->ignore_fields, ind->header.truncate),
304 query(strsave(q))
305 {
306 found_list = indx->search(q, strlen(q), &temp_list);
307 if (!found_list) {
308 found_list = &minus_one;
309 warning("all keys would have been discarded in constructing index `%1'",
310 indx->name);
311 }
312 }
313
~index_search_item_iterator()314 index_search_item_iterator::~index_search_item_iterator()
315 {
316 a_delete temp_list;
317 a_delete buf;
318 a_delete query;
319 delete out_of_date_files_iter;
320 }
321
next(const linear_searcher &,const char ** pp,int * lenp,reference_id * ridp)322 int index_search_item_iterator::next(const linear_searcher &,
323 const char **pp, int *lenp,
324 reference_id *ridp)
325 {
326 if (found_list) {
327 for (;;) {
328 int tagno = *found_list;
329 if (tagno == -1)
330 break;
331 found_list++;
332 if (get_tag(tagno, searcher, pp, lenp, ridp))
333 return 1;
334 }
335 found_list = 0;
336 next_out_of_date_file = indx->out_of_date_files;
337 }
338 while (next_out_of_date_file) {
339 if (out_of_date_files_iter == 0)
340 out_of_date_files_iter
341 = next_out_of_date_file->make_search_item_iterator(query);
342 if (out_of_date_files_iter->next(searcher, pp, lenp, ridp))
343 return 1;
344 delete out_of_date_files_iter;
345 out_of_date_files_iter = 0;
346 next_out_of_date_file = next_out_of_date_file->next;
347 }
348 return 0;
349 }
350
get_tag(int tagno,const linear_searcher & searcher,const char ** pp,int * lenp,reference_id * ridp)351 int index_search_item_iterator::get_tag(int tagno,
352 const linear_searcher &searcher,
353 const char **pp, int *lenp,
354 reference_id *ridp)
355 {
356 if (tagno < 0 || tagno >= indx->header.tags_size) {
357 error("bad tag number");
358 return 0;
359 }
360 tag *tp = indx->tags + tagno;
361 const char *filename = indx->munge_filename(indx->pool + tp->filename_index);
362 int fd = open(filename, O_RDONLY | O_BINARY);
363 if (fd < 0) {
364 error("can't open `%1': %2", filename, strerror(errno));
365 return 0;
366 }
367 struct stat sb;
368 if (fstat(fd, &sb) < 0) {
369 error("can't fstat: %1", strerror(errno));
370 close(fd);
371 return 0;
372 }
373 time_t mtime = sb.st_mtime;
374 if (mtime > indx->mtime) {
375 indx->add_out_of_date_file(fd, filename,
376 indx->filename_id + tp->filename_index);
377 return 0;
378 }
379 int res = 0;
380 FILE *fp = fdopen(fd, FOPEN_RB);
381 if (!fp) {
382 error("fdopen failed");
383 close(fd);
384 return 0;
385 }
386 if (tp->start != 0 && fseek(fp, long(tp->start), 0) < 0)
387 error("can't seek on `%1': %2", filename, strerror(errno));
388 else {
389 int length = tp->length;
390 int err = 0;
391 if (length == 0) {
392 struct stat sb;
393 if (fstat(fileno(fp), &sb) < 0) {
394 error("can't stat `%1': %2", filename, strerror(errno));
395 err = 1;
396 }
397 else if (!S_ISREG(sb.st_mode)) {
398 error("`%1' is not a regular file", filename);
399 err = 1;
400 }
401 else
402 length = int(sb.st_size);
403 }
404 if (!err) {
405 if (length + 2 > buflen) {
406 a_delete buf;
407 buflen = length + 2;
408 buf = new char[buflen];
409 }
410 if (fread(buf + 1, 1, length, fp) != (size_t)length)
411 error("fread on `%1' failed: %2", filename, strerror(errno));
412 else {
413 buf[0] = '\n';
414 // Remove the CR characters from CRLF pairs.
415 int sidx = 1, didx = 1;
416 for ( ; sidx < length + 1; sidx++, didx++)
417 {
418 if (buf[sidx] == '\r')
419 {
420 if (buf[++sidx] != '\n')
421 buf[didx++] = '\r';
422 else
423 length--;
424 }
425 if (sidx != didx)
426 buf[didx] = buf[sidx];
427 }
428 buf[length + 1] = '\n';
429 res = searcher.search(buf + 1, buf + 2 + length, pp, lenp);
430 if (res && ridp)
431 *ridp = reference_id(indx->filename_id + tp->filename_index,
432 tp->start);
433 }
434 }
435 }
436 fclose(fp);
437 return res;
438 }
439
munge_filename(const char * filename)440 const char *index_search_item::munge_filename(const char *filename)
441 {
442 if (IS_ABSOLUTE(filename))
443 return filename;
444 const char *cwd = pool;
445 int need_slash = (cwd[0] != 0
446 && strchr(DIR_SEPS, strchr(cwd, '\0')[-1]) == 0);
447 int len = strlen(cwd) + strlen(filename) + need_slash + 1;
448 if (len > filename_buflen) {
449 a_delete filename_buffer;
450 filename_buflen = len;
451 filename_buffer = new char[len];
452 }
453 strcpy(filename_buffer, cwd);
454 if (need_slash)
455 strcat(filename_buffer, "/");
456 strcat(filename_buffer, filename);
457 return filename_buffer;
458 }
459
search1(const char ** pp,const char * end)460 const int *index_search_item::search1(const char **pp, const char *end)
461 {
462 while (*pp < end && !csalnum(**pp))
463 *pp += 1;
464 if (*pp >= end)
465 return 0;
466 const char *start = *pp;
467 while (*pp < end && csalnum(**pp))
468 *pp += 1;
469 int len = *pp - start;
470 if (len < header.shortest)
471 return 0;
472 if (len > header.truncate)
473 len = header.truncate;
474 int is_number = 1;
475 for (int i = 0; i < len; i++)
476 if (csdigit(start[i]))
477 key_buffer[i] = start[i];
478 else {
479 key_buffer[i] = cmlower(start[i]);
480 is_number = 0;
481 }
482 if (is_number && !(len == 4 && start[0] == '1' && start[1] == '9'))
483 return 0;
484 unsigned hc = hash(key_buffer, len);
485 if (common_words_table) {
486 for (int h = hc % common_words_table_size;
487 common_words_table[h];
488 --h) {
489 if (strlen(common_words_table[h]) == (size_t)len
490 && memcmp(common_words_table[h], key_buffer, len) == 0)
491 return 0;
492 if (h == 0)
493 h = common_words_table_size;
494 }
495 }
496 int li = table[int(hc % header.table_size)];
497 return li < 0 ? &minus_one : lists + li;
498 }
499
merge(int * result,const int * s1,const int * s2)500 static void merge(int *result, const int *s1, const int *s2)
501 {
502 for (; *s1 >= 0; s1++) {
503 while (*s2 >= 0 && *s2 < *s1)
504 s2++;
505 if (*s2 == *s1)
506 *result++ = *s2;
507 }
508 *result++ = -1;
509 }
510
search(const char * ptr,int length,int ** temp_listp)511 const int *index_search_item::search(const char *ptr, int length,
512 int **temp_listp)
513 {
514 const char *end = ptr + length;
515 if (*temp_listp) {
516 a_delete *temp_listp;
517 *temp_listp = 0;
518 }
519 const int *first_list = 0;
520 while (ptr < end && (first_list = search1(&ptr, end)) == 0)
521 ;
522 if (!first_list)
523 return 0;
524 if (*first_list < 0)
525 return first_list;
526 const int *second_list = 0;
527 while (ptr < end && (second_list = search1(&ptr, end)) == 0)
528 ;
529 if (!second_list)
530 return first_list;
531 if (*second_list < 0)
532 return second_list;
533 const int *p;
534 for (p = first_list; *p >= 0; p++)
535 ;
536 int len = p - first_list;
537 for (p = second_list; *p >= 0; p++)
538 ;
539 if (p - second_list < len)
540 len = p - second_list;
541 int *matches = new int[len + 1];
542 merge(matches, first_list, second_list);
543 while (ptr < end) {
544 const int *list = search1(&ptr, end);
545 if (list != 0) {
546 if (*list < 0) {
547 a_delete matches;
548 return list;
549 }
550 merge(matches, matches, list);
551 if (*matches < 0) {
552 a_delete matches;
553 return &minus_one;
554 }
555 }
556 }
557 *temp_listp = matches;
558 return matches;
559 }
560
read_common_words_file()561 void index_search_item::read_common_words_file()
562 {
563 if (header.common <= 0)
564 return;
565 const char *common_words_file = munge_filename(strchr(pool, '\0') + 1);
566 errno = 0;
567 FILE *fp = fopen(common_words_file, "r");
568 if (!fp) {
569 error("can't open `%1': %2", common_words_file, strerror(errno));
570 return;
571 }
572 common_words_table_size = 2*header.common + 1;
573 while (!is_prime(common_words_table_size))
574 common_words_table_size++;
575 common_words_table = new char *[common_words_table_size];
576 for (int i = 0; i < common_words_table_size; i++)
577 common_words_table[i] = 0;
578 int count = 0;
579 int key_len = 0;
580 for (;;) {
581 int c = getc(fp);
582 while (c != EOF && !csalnum(c))
583 c = getc(fp);
584 if (c == EOF)
585 break;
586 do {
587 if (key_len < header.truncate)
588 key_buffer[key_len++] = cmlower(c);
589 c = getc(fp);
590 } while (c != EOF && csalnum(c));
591 if (key_len >= header.shortest) {
592 int h = hash(key_buffer, key_len) % common_words_table_size;
593 while (common_words_table[h]) {
594 if (h == 0)
595 h = common_words_table_size;
596 --h;
597 }
598 common_words_table[h] = new char[key_len + 1];
599 memcpy(common_words_table[h], key_buffer, key_len);
600 common_words_table[h][key_len] = '\0';
601 }
602 if (++count >= header.common)
603 break;
604 key_len = 0;
605 if (c == EOF)
606 break;
607 }
608 fclose(fp);
609 }
610
add_out_of_date_file(int fd,const char * filename,int fid)611 void index_search_item::add_out_of_date_file(int fd, const char *filename,
612 int fid)
613 {
614 search_item **pp;
615 for (pp = &out_of_date_files; *pp; pp = &(*pp)->next)
616 if ((*pp)->is_named(filename))
617 return;
618 *pp = make_linear_search_item(fd, filename, fid);
619 warning("`%1' modified since `%2' created", filename, name);
620 }
621
check_files()622 void index_search_item::check_files()
623 {
624 const char *pool_end = pool + header.strings_size;
625 for (const char *ptr = strchr(ignore_fields, '\0') + 1;
626 ptr < pool_end;
627 ptr = strchr(ptr, '\0') + 1) {
628 const char *path = munge_filename(ptr);
629 struct stat sb;
630 if (stat(path, &sb) < 0)
631 error("can't stat `%1': %2", path, strerror(errno));
632 else if (sb.st_mtime > mtime) {
633 int fd = open(path, O_RDONLY | O_BINARY);
634 if (fd < 0)
635 error("can't open `%1': %2", path, strerror(errno));
636 else
637 add_out_of_date_file(fd, path, filename_id + (ptr - pool));
638 }
639 }
640 }
641