2 /* Copyright (C) 1989, 1990, 1991, 1992, 2001, 2004, 2009
3 Free Software Foundation, Inc.
4 Written by James Clark (jjc@jclark.com)
6 This file is part of groff.
8 groff is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 groff is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
41 void *mapread(int fd, int len);
42 int unmap(void *, int len);
54 class index_search_item : public search_item {
55 search_item *out_of_date_files;
65 char *filename_buffer;
67 char **common_words_table;
68 int common_words_table_size;
69 const char *ignore_fields;
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);
79 index_search_item(const char *, int);
82 search_item_iterator *make_search_item_iterator(const char *);
85 int next_filename_id() const;
86 friend class index_search_item_iterator;
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;
97 linear_searcher searcher;
99 int get_tag(int tagno, const linear_searcher &, const char **, int *,
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 *);
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)
115 index_search_item::~index_search_item()
120 if (unmap(map_addr, map_len) < 0)
121 error("unmap: %1", strerror(errno));
123 while (out_of_date_files) {
124 search_item *tem = out_of_date_files;
125 out_of_date_files = out_of_date_files->next;
128 a_delete filename_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;
140 file_closer(int &fd) : fdp(&fd) { }
141 ~file_closer() { close(*fdp); }
144 // Tell the compiler that a variable is intentionally unused.
145 inline void unused(void *) { }
147 int index_search_item::load(int fd)
149 file_closer fd_closer(fd); // close fd on return
152 if (fstat(fd, &sb) < 0) {
153 error("can't fstat `%1': %2", name, strerror(errno));
156 if (!S_ISREG(sb.st_mode)) {
157 error("`%1' is not a regular file", name);
161 int size = int(sb.st_size);
163 map_addr = mapread(fd, size);
165 addr = (char *)map_addr;
169 addr = buffer = (char *)malloc(size);
171 error("can't allocate buffer for `%1'", name);
175 int bytes_to_read = size;
176 while (bytes_to_read > 0) {
177 int nread = read(fd, ptr, bytes_to_read);
179 error("unexpected EOF on `%1'", name);
183 error("read error on `%1': %2", name, strerror(errno));
186 bytes_to_read -= nread;
190 header = *(index_header *)addr;
191 if (header.magic != INDEX_MAGIC) {
192 error("`%1' is not an index file: wrong magic number", name);
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);
200 int sz = (header.tags_size * sizeof(tag)
201 + header.lists_size * sizeof(int)
202 + header.table_size * sizeof(int)
203 + header.strings_size
206 error("size of `%1' is wrong: was %2, should be %3",
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();
220 const char *index_search_item::do_verify()
224 if (lists[header.lists_size - 1] >= 0)
225 return "last list element not negative";
227 for (i = 0; i < header.table_size; i++) {
229 if (li >= header.lists_size)
230 return "bad list index";
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";
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";
248 if (pool[header.strings_size - 1] != '\0')
249 return "last character in pool not nul";
253 int index_search_item::verify()
255 const char *reason = do_verify();
258 error("`%1' is bad: %2", name, reason);
262 int index_search_item::next_filename_id() const
264 return filename_id + header.strings_size + 1;
267 search_item_iterator *index_search_item::make_search_item_iterator(
270 return new index_search_item_iterator(this, query);
273 search_item *make_index_search_item(const char *filename, int fid)
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);
281 index_search_item *item = new index_search_item(index_filename, fid);
282 a_delete index_filename;
283 if (!item->load(fd)) {
288 else if (verify_flag && !item->verify()) {
299 index_search_item_iterator::index_search_item_iterator(index_search_item *ind,
301 : indx(ind), out_of_date_files_iter(0), next_out_of_date_file(0), temp_list(0),
303 searcher(q, strlen(q), ind->ignore_fields, ind->header.truncate),
306 found_list = indx->search(q, strlen(q), &temp_list);
308 found_list = &minus_one;
309 warning("all keys would have been discarded in constructing index `%1'",
314 index_search_item_iterator::~index_search_item_iterator()
319 delete out_of_date_files_iter;
322 int index_search_item_iterator::next(const linear_searcher &,
323 const char **pp, int *lenp,
328 int tagno = *found_list;
332 if (get_tag(tagno, searcher, pp, lenp, ridp))
336 next_out_of_date_file = indx->out_of_date_files;
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))
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;
351 int index_search_item_iterator::get_tag(int tagno,
352 const linear_searcher &searchr,
353 const char **pp, int *lenp,
356 if (tagno < 0 || tagno >= indx->header.tags_size) {
357 error("bad tag number");
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);
364 error("can't open `%1': %2", filename, strerror(errno));
368 if (fstat(fd, &sb) < 0) {
369 error("can't fstat: %1", strerror(errno));
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);
380 FILE *fp = fdopen(fd, FOPEN_RB);
382 error("fdopen failed");
386 if (tp->start != 0 && fseek(fp, long(tp->start), 0) < 0)
387 error("can't seek on `%1': %2", filename, strerror(errno));
389 int length = tp->length;
392 if (fstat(fileno(fp), &sb) < 0) {
393 error("can't stat `%1': %2", filename, strerror(errno));
396 else if (!S_ISREG(sb.st_mode)) {
397 error("`%1' is not a regular file", filename);
401 length = int(sb.st_size);
404 if (length + 2 > buflen) {
407 buf = new char[buflen];
409 if (fread(buf + 1, 1, length, fp) != (size_t)length)
410 error("fread on `%1' failed: %2", filename, strerror(errno));
413 // Remove the CR characters from CRLF pairs.
414 int sidx = 1, didx = 1;
415 for ( ; sidx < length + 1; sidx++, didx++)
417 if (buf[sidx] == '\r')
419 if (buf[++sidx] != '\n')
425 buf[didx] = buf[sidx];
427 buf[length + 1] = '\n';
428 res = searchr.search(buf + 1, buf + 2 + length, pp, lenp);
430 *ridp = reference_id(indx->filename_id + tp->filename_index,
439 const char *index_search_item::munge_filename(const char *filename)
441 if (IS_ABSOLUTE(filename))
443 const char *cwd = pool;
444 int need_slash = (cwd[0] != 0
445 && strchr(DIR_SEPS, strchr(cwd, '\0')[-1]) == 0);
446 int len = strlen(cwd) + strlen(filename) + need_slash + 1;
447 if (len > filename_buflen) {
448 a_delete filename_buffer;
449 filename_buflen = len;
450 filename_buffer = new char[len];
452 strcpy(filename_buffer, cwd);
454 strcat(filename_buffer, "/");
455 strcat(filename_buffer, filename);
456 return filename_buffer;
459 const int *index_search_item::search1(const char **pp, const char *end)
461 while (*pp < end && !csalnum(**pp))
465 const char *start = *pp;
466 while (*pp < end && csalnum(**pp))
468 int len = *pp - start;
469 if (len < header.shortest)
471 if (len > header.truncate)
472 len = header.truncate;
474 for (int i = 0; i < len; i++)
475 if (csdigit(start[i]))
476 key_buffer[i] = start[i];
478 key_buffer[i] = cmlower(start[i]);
481 if (is_number && !(len == 4 && start[0] == '1' && start[1] == '9'))
483 unsigned hc = hash(key_buffer, len);
484 if (common_words_table) {
485 for (int h = hc % common_words_table_size;
486 common_words_table[h];
488 if (strlen(common_words_table[h]) == (size_t)len
489 && memcmp(common_words_table[h], key_buffer, len) == 0)
492 h = common_words_table_size;
495 int li = table[int(hc % header.table_size)];
496 return li < 0 ? &minus_one : lists + li;
499 static void merge(int *result, const int *s1, const int *s2)
501 for (; *s1 >= 0; s1++) {
502 while (*s2 >= 0 && *s2 < *s1)
510 const int *index_search_item::search(const char *ptr, int length,
513 const char *end = ptr + length;
515 a_delete *temp_listp;
518 const int *first_list = 0;
519 while (ptr < end && (first_list = search1(&ptr, end)) == 0)
525 const int *second_list = 0;
526 while (ptr < end && (second_list = search1(&ptr, end)) == 0)
530 if (*second_list < 0)
533 for (p = first_list; *p >= 0; p++)
535 int len = p - first_list;
536 for (p = second_list; *p >= 0; p++)
538 if (p - second_list < len)
539 len = p - second_list;
540 int *matches = new int[len + 1];
541 merge(matches, first_list, second_list);
543 const int *list = search1(&ptr, end);
549 merge(matches, matches, list);
556 *temp_listp = matches;
560 void index_search_item::read_common_words_file()
562 if (header.common <= 0)
564 const char *common_words_file = munge_filename(strchr(pool, '\0') + 1);
566 FILE *fp = fopen(common_words_file, "r");
568 error("can't open `%1': %2", common_words_file, strerror(errno));
571 common_words_table_size = 2*header.common + 1;
572 while (!is_prime(common_words_table_size))
573 common_words_table_size++;
574 common_words_table = new char *[common_words_table_size];
575 for (int i = 0; i < common_words_table_size; i++)
576 common_words_table[i] = 0;
581 while (c != EOF && !csalnum(c))
586 if (key_len < header.truncate)
587 key_buffer[key_len++] = cmlower(c);
589 } while (c != EOF && csalnum(c));
590 if (key_len >= header.shortest) {
591 int h = hash(key_buffer, key_len) % common_words_table_size;
592 while (common_words_table[h]) {
594 h = common_words_table_size;
597 common_words_table[h] = new char[key_len + 1];
598 memcpy(common_words_table[h], key_buffer, key_len);
599 common_words_table[h][key_len] = '\0';
601 if (++count >= header.common)
610 void index_search_item::add_out_of_date_file(int fd, const char *filename,
614 for (pp = &out_of_date_files; *pp; pp = &(*pp)->next)
615 if ((*pp)->is_named(filename))
617 *pp = make_linear_search_item(fd, filename, fid);
618 warning("`%1' modified since `%2' created", filename, name);
621 void index_search_item::check_files()
623 const char *pool_end = pool + header.strings_size;
624 for (const char *ptr = strchr(ignore_fields, '\0') + 1;
626 ptr = strchr(ptr, '\0') + 1) {
627 const char *path = munge_filename(ptr);
629 if (stat(path, &sb) < 0)
630 error("can't stat `%1': %2", path, strerror(errno));
631 else if (sb.st_mtime > mtime) {
632 int fd = open(path, O_RDONLY | O_BINARY);
634 error("can't open `%1': %2", path, strerror(errno));
636 add_out_of_date_file(fd, path, filename_id + (ptr - pool));