2 /* Copyright (C) 1989-2018 Free Software Foundation, Inc.
3 Written by James Clark (jjc@jclark.com)
5 This file is part of groff.
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 3 of the License, or
10 (at your option) any later version.
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
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
29 #include "stringclass.h"
38 extern "C" const char *Version_string;
40 #define DEFAULT_HASH_TABLE_SIZE 997
41 #define TEMP_INDEX_TEMPLATE "indxbibXXXXXX"
43 // (2^n - MALLOC_OVERHEAD) should be a good argument for malloc().
45 #define MALLOC_OVERHEAD 16
51 const int BLOCK_SIZE = ((1024 - MALLOC_OVERHEAD - sizeof(struct block *)
52 - sizeof(int)) / sizeof(int));
58 block(block *p = 0) : next(p), used(0) { }
72 word_list(const char *, int, word_list *);
75 table_entry *hash_table;
76 int hash_table_size = DEFAULT_HASH_TABLE_SIZE;
77 // We make this the same size as hash_table so we only have to do one
79 static word_list **common_words_table = 0;
85 char *temp_index_file = 0;
87 const char *ignore_fields = "XYZ";
88 const char *common_words_file = COMMON_WORDS_FILE;
89 int n_ignore_words = 100;
92 int max_keys_per_item = 100;
94 static void usage(FILE *stream);
95 static void write_hash_table();
96 static void init_hash_table();
97 static void read_common_words_file();
98 static int store_key(char *s, int len);
99 static void possibly_store_key(char *s, int len);
100 static int do_whole_file(const char *filename);
101 static int do_file(const char *filename);
102 static void store_reference(int filename_index, int pos, int len);
103 static void check_integer_arg(char opt, const char *arg, int min, int *res);
104 static void store_filename(const char *);
105 static void fwrite_or_die(const void *ptr, int size, int nitems, FILE *fp);
106 static char *get_cwd();
110 void catch_fatal_signals();
111 void ignore_fatal_signals();
114 int main(int argc, char **argv)
116 program_name = argv[0];
117 static char stderr_buf[BUFSIZ];
118 setbuf(stderr, stderr_buf);
120 const char *base_name = 0;
121 typedef int (*parser_t)(const char *);
122 parser_t parser = do_file;
123 const char *directory = 0;
124 const char *foption = 0;
126 static const struct option long_options[] = {
127 { "help", no_argument, 0, CHAR_MAX + 1 },
128 { "version", no_argument, 0, 'v' },
131 while ((opt = getopt_long(argc, argv, "c:o:h:i:k:l:t:n:c:d:f:vw",
136 common_words_file = optarg;
145 check_integer_arg('h', optarg, 1, &hash_table_size);
146 if (!is_prime(hash_table_size)) {
147 while (!is_prime(++hash_table_size))
149 warning("%1 not prime: using %2 instead", optarg, hash_table_size);
153 ignore_fields = optarg;
156 check_integer_arg('k', optarg, 1, &max_keys_per_item);
159 check_integer_arg('l', optarg, 0, &shortest_len);
162 check_integer_arg('n', optarg, 0, &n_ignore_words);
168 check_integer_arg('t', optarg, 1, &truncate_len);
171 parser = do_whole_file;
174 printf("GNU indxbib (groff) version %s\n", Version_string);
177 case CHAR_MAX + 1: // --help
189 if (optind >= argc && foption == 0)
190 fatal("no files and no -f option");
192 char *path = get_cwd();
193 store_filename(path);
197 store_filename(directory);
199 store_filename(common_words_file);
200 store_filename(ignore_fields);
201 key_buffer = new char[truncate_len];
202 read_common_words_file();
204 base_name = optind < argc ? argv[optind] : DEFAULT_INDEX_NAME;
205 const char *p = strrchr(base_name, DIR_SEPS[0]), *p1;
206 const char *sep = &DIR_SEPS[1];
208 p1 = strrchr(base_name, *sep);
209 if (p1 && (!p || p1 > p))
215 char *dir = strsave(base_name);
216 dir[p - base_name] = '\0';
217 name_max = file_name_max(dir);
221 name_max = file_name_max(".");
222 const char *filename = p ? p + 1 : base_name;
223 if (strlen(filename) + sizeof(INDEX_SUFFIX) - 1 > name_max)
224 fatal("'%1.%2' is too long for a filename", filename, INDEX_SUFFIX);
227 temp_index_file = new char[p - base_name + sizeof(TEMP_INDEX_TEMPLATE)];
228 memcpy(temp_index_file, base_name, p - base_name);
229 strcpy(temp_index_file + (p - base_name), TEMP_INDEX_TEMPLATE);
232 temp_index_file = strsave(TEMP_INDEX_TEMPLATE);
234 catch_fatal_signals();
235 int fd = mkstemp(temp_index_file);
237 fatal("can't create temporary index file: %1", strerror(errno));
238 indxfp = fdopen(fd, FOPEN_WB);
240 fatal("fdopen failed");
241 if (fseek(indxfp, sizeof(index_header), 0) < 0)
242 fatal("can't seek past index header: %1", strerror(errno));
246 if (strcmp(foption, "-") != 0) {
248 fp = fopen(foption, "r");
250 fatal("can't open '%1': %2", foption, strerror(errno));
256 for (c = getc(fp); c != '\n' && c != EOF; c = getc(fp)) {
258 error_with_file_and_line(foption, lineno,
259 "nul character in pathname ignored");
263 if (path.length() > 0) {
265 if (!(*parser)(path.contents()))
276 for (int i = optind; i < argc; i++)
277 if (!(*parser)(argv[i]))
280 if (fclose(indxfp) < 0)
281 fatal("error closing temporary index file: %1", strerror(errno));
282 char *index_file = new char[strlen(base_name) + sizeof(INDEX_SUFFIX)];
283 strcpy(index_file, base_name);
284 strcat(index_file, INDEX_SUFFIX);
287 if (access(index_file, R_OK) == 0)
290 if (rename(temp_index_file, index_file) < 0) {
292 // RENAME could fail on plain MSDOS filesystems because
293 // INDEX_FILE is an invalid filename, e.g. it has multiple dots.
294 char *fname = p ? index_file + (p - base_name) : 0;
297 // Replace the dot with an underscore and try again.
299 && (dot = strchr(fname, '.')) != 0
300 && strcmp(dot, INDEX_SUFFIX) != 0)
302 if (rename(temp_index_file, index_file) < 0)
304 fatal("can't rename temporary index file: %1", strerror(errno));
306 #else /* not HAVE_RENAME */
307 ignore_fatal_signals();
308 if (unlink(index_file) < 0) {
310 fatal("can't unlink '%1': %2", index_file, strerror(errno));
312 if (link(temp_index_file, index_file) < 0)
313 fatal("can't link temporary index file: %1", strerror(errno));
314 if (unlink(temp_index_file) < 0)
315 fatal("can't unlink temporary index file: %1", strerror(errno));
316 #endif /* not HAVE_RENAME */
321 static void usage(FILE *stream)
324 "usage: %s [-vw] [-c file] [-d dir] [-f file] [-h n] [-i XYZ] [-k n]\n"
325 " [-l n] [-n n] [-o base] [-t n] [files...]\n",
329 static void check_integer_arg(char opt, const char *arg, int min, int *res)
332 long n = strtol(arg, &ptr, 10);
333 if (n == 0 && ptr == arg)
334 error("argument to -%1 not an integer", opt);
336 error("argument to -%1 must not be less than %2", opt, min);
339 error("argument to -%1 greater than maximum integer", opt);
340 else if (*ptr != '\0')
341 error("junk after integer argument to -%1", opt);
346 static char *get_cwd()
352 buf = new char[size];
353 if (getcwd(buf, size))
356 fatal("cannot get current working directory: %1", strerror(errno));
359 fatal("current working directory longer than INT_MAX");
360 if (size > INT_MAX/2)
368 word_list::word_list(const char *s, int n, word_list *p)
375 static void read_common_words_file()
377 if (n_ignore_words <= 0)
380 FILE *fp = fopen(common_words_file, "r");
382 fatal("can't open '%1': %2", common_words_file, strerror(errno));
383 common_words_table = new word_list * [hash_table_size];
384 for (int i = 0; i < hash_table_size; i++)
385 common_words_table[i] = 0;
390 while (c != EOF && !csalnum(c))
395 if (key_len < truncate_len)
396 key_buffer[key_len++] = cmlower(c);
398 } while (c != EOF && csalnum(c));
399 if (key_len >= shortest_len) {
400 int h = hash(key_buffer, key_len) % hash_table_size;
401 common_words_table[h] = new word_list(key_buffer, key_len,
402 common_words_table[h]);
404 if (++count >= n_ignore_words)
410 n_ignore_words = count;
414 static int do_whole_file(const char *filename)
417 FILE *fp = fopen(filename, "r");
419 error("can't open '%1': %2", filename, strerror(errno));
425 while ((c = getc(fp)) != EOF) {
429 while ((c = getc(fp)) != EOF) {
432 if (key_len < truncate_len)
433 key_buffer[key_len++] = c;
435 if (store_key(key_buffer, key_len)) {
436 if (++count >= max_keys_per_item)
443 store_reference(filenames.length(), 0, 0);
444 store_filename(filename);
449 static int do_file(const char *filename)
452 // Need binary I/O for MS-DOS/MS-Windows, because indxbib relies on
453 // byte counts to be consistent with fseek.
454 FILE *fp = fopen(filename, FOPEN_RB);
456 error("can't open '%1': %2", filename, strerror(errno));
459 int filename_index = filenames.length();
460 store_filename(filename);
463 START, // at the start of the file; also in between references
464 BOL, // in the middle of a reference, at the beginning of the line
465 PERCENT, // seen a percent at the beginning of the line
466 IGNORE, // ignoring a field
467 IGNORE_BOL, // at the beginning of a line ignoring a field
468 KEY, // in the middle of a key
469 DISCARD, // after truncate_len bytes of a key
470 MIDDLE // in between keys
473 // In states START, BOL, IGNORE_BOL, space_count how many spaces at
474 // the beginning have been seen. In states PERCENT, IGNORE, KEY,
475 // MIDDLE space_count must be 0.
477 int byte_count = 0; // bytes read
479 int ref_start = -1; // position of start of current reference
484 // We opened the file in binary mode, so we need to skip
485 // every CR character before a Newline.
495 #if defined(__MSDOS__) || defined(_MSC_VER) || defined(__EMX__)
496 else if (c == 0x1a) // ^Z means EOF in text files
502 if (c == ' ' || c == '\t') {
510 ref_start = byte_count - space_count - 1;
514 else if (csalnum(c)) {
525 if (space_count > 0) {
537 store_reference(filename_index, ref_start,
538 byte_count - 1 - space_count - ref_start);
554 if (strchr(ignore_fields, c) != 0)
568 if (space_count > 0) {
580 store_reference(filename_index, ref_start,
581 byte_count - 1 - space_count - ref_start);
592 if (key_len < truncate_len)
593 key_buffer[key_len++] = c;
598 possibly_store_key(key_buffer, key_len);
608 possibly_store_key(key_buffer, key_len);
634 possibly_store_key(key_buffer, key_len);
641 store_reference(filename_index, ref_start,
642 byte_count - ref_start - space_count);
651 static void store_reference(int filename_index, int pos, int len)
654 t.filename_index = filename_index;
657 fwrite_or_die(&t, sizeof(t), 1, indxfp);
661 static void store_filename(const char *fn)
667 static void init_hash_table()
669 hash_table = new table_entry[hash_table_size];
670 for (int i = 0; i < hash_table_size; i++)
671 hash_table[i].ptr = 0;
674 static void possibly_store_key(char *s, int len)
676 static int last_tagno = -1;
677 static int key_count;
678 if (last_tagno != ntags) {
682 if (key_count < max_keys_per_item) {
683 if (store_key(s, len))
688 static int store_key(char *s, int len)
690 if (len < shortest_len)
693 for (int i = 0; i < len; i++)
694 if (!csdigit(s[i])) {
696 s[i] = cmlower(s[i]);
698 if (is_number && !(len == 4 && s[0] == '1' && s[1] == '9'))
700 int h = hash(s, len) % hash_table_size;
701 if (common_words_table) {
702 for (word_list *ptr = common_words_table[h]; ptr; ptr = ptr->next)
703 if (len == ptr->len && memcmp(s, ptr->str, len) == 0)
706 table_entry *pp = hash_table + h;
709 else if (pp->ptr->v[pp->ptr->used - 1] == ntags)
711 else if (pp->ptr->used >= BLOCK_SIZE)
712 pp->ptr = new block(pp->ptr);
713 pp->ptr->v[(pp->ptr->used)++] = ntags;
717 static void write_hash_table()
719 const int minus_one = -1;
721 for (int i = 0; i < hash_table_size; i++) {
722 block *ptr = hash_table[i].ptr;
724 hash_table[i].count = -1;
726 hash_table[i].count = li;
735 fwrite_or_die(rev->v, sizeof(int), rev->used, indxfp);
741 fwrite_or_die(&minus_one, sizeof(int), 1, indxfp);
745 if (sizeof(table_entry) == sizeof(int))
746 fwrite_or_die(hash_table, sizeof(int), hash_table_size, indxfp);
748 // write it out word by word
749 for (int i = 0; i < hash_table_size; i++)
750 fwrite_or_die(&hash_table[i].count, sizeof(int), 1, indxfp);
752 fwrite_or_die(filenames.contents(), 1, filenames.length(), indxfp);
753 if (fseek(indxfp, 0, 0) < 0)
754 fatal("error seeking on index file: %1", strerror(errno));
756 h.magic = INDEX_MAGIC;
757 h.version = INDEX_VERSION;
760 h.table_size = hash_table_size;
761 h.strings_size = filenames.length();
762 h.truncate = truncate_len;
763 h.shortest = shortest_len;
764 h.common = n_ignore_words;
765 fwrite_or_die(&h, sizeof(h), 1, indxfp);
768 static void fwrite_or_die(const void *ptr, int size, int nitems, FILE *fp)
770 if (fwrite(ptr, size, nitems, fp) != (size_t)nitems)
771 fatal("fwrite failed: %1", strerror(errno));
774 void fatal_error_exit()
785 unlink(temp_index_file);