2 /* Copyright (C) 1989, 1990, 1991, 1992 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 2, or (at your option) any later
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 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. */
31 #include "stringclass.h"
39 // Sun's stdlib.h fails to declare this.
43 #define DEFAULT_HASH_TABLE_SIZE 997
44 #define TEMP_INDEX_TEMPLATE "indxbibXXXXXX"
46 // (2^n - MALLOC_OVERHEAD) should be a good argument for malloc().
48 #define MALLOC_OVERHEAD 16
54 const int BLOCK_SIZE
= ((1024 - MALLOC_OVERHEAD
- sizeof(struct block
*)
55 - sizeof(int)) / sizeof(int));
61 block(block
*p
= 0) : next(p
), used(0) { }
75 word_list(const char *, int, word_list
*);
78 table_entry
*hash_table
;
79 int hash_table_size
= DEFAULT_HASH_TABLE_SIZE
;
80 // We make this the same size as hash_table so we only have to do one
82 static word_list
**common_words_table
= 0;
88 char *temp_index_file
= 0;
90 const char *ignore_fields
= "XYZ";
91 const char *common_words_file
= COMMON_WORDS_FILE
;
92 int n_ignore_words
= 100;
95 int max_keys_per_item
= 100;
98 static void write_hash_table();
99 static void init_hash_table();
100 static void read_common_words_file();
101 static int store_key(char *s
, int len
);
102 static void possibly_store_key(char *s
, int len
);
103 static int do_whole_file(const char *filename
);
104 static int do_file(const char *filename
);
105 static void store_reference(int filename_index
, int pos
, int len
);
106 static void check_integer_arg(char opt
, const char *arg
, int min
, int *res
);
107 static void store_filename(const char *);
108 static void fwrite_or_die(const void *ptr
, int size
, int nitems
, FILE *fp
);
109 static char *get_cwd();
113 long dir_name_max(const char *);
114 void catch_fatal_signals();
115 void ignore_fatal_signals();
118 int main(int argc
, char **argv
)
120 program_name
= argv
[0];
121 static char stderr_buf
[BUFSIZ
];
122 setbuf(stderr
, stderr_buf
);
124 const char *basename
= 0;
125 typedef int (*parser_t
)(const char *);
126 parser_t parser
= do_file
;
127 const char *directory
= 0;
128 const char *foption
= 0;
130 while ((opt
= getopt(argc
, argv
, "c:o:h:i:k:l:t:n:c:d:f:vw")) != EOF
)
133 common_words_file
= optarg
;
142 check_integer_arg('h', optarg
, 1, &hash_table_size
);
143 if (!is_prime(hash_table_size
)) {
144 while (!is_prime(++hash_table_size
))
146 warning("%1 not prime: using %2 instead", optarg
, hash_table_size
);
150 ignore_fields
= optarg
;
153 check_integer_arg('k', optarg
, 1, &max_keys_per_item
);
156 check_integer_arg('l', optarg
, 0, &shortest_len
);
159 check_integer_arg('n', optarg
, 0, &n_ignore_words
);
165 check_integer_arg('t', optarg
, 1, &truncate_len
);
168 parser
= do_whole_file
;
172 extern const char *version_string
;
173 fprintf(stderr
, "GNU indxbib version %s\n", version_string
);
184 if (optind
>= argc
&& foption
== 0)
185 fatal("no files and no -f option");
187 char *path
= get_cwd();
188 store_filename(path
);
192 store_filename(directory
);
194 store_filename(common_words_file
);
195 store_filename(ignore_fields
);
196 key_buffer
= new char[truncate_len
];
197 read_common_words_file();
199 basename
= optind
< argc
? argv
[optind
] : DEFAULT_INDEX_NAME
;
200 const char *p
= strrchr(basename
, '/');
203 char *dir
= strsave(basename
);
204 dir
[p
- basename
] = '\0';
205 name_max
= dir_name_max(dir
);
209 name_max
= dir_name_max(".");
210 const char *filename
= p
? p
+ 1 : basename
;
211 if (name_max
>= 0 && strlen(filename
) + sizeof(INDEX_SUFFIX
) - 1 > name_max
)
212 fatal("`%1.%2' is too long for a filename", filename
, INDEX_SUFFIX
);
215 temp_index_file
= new char[p
- basename
+ sizeof(TEMP_INDEX_TEMPLATE
)];
216 memcpy(temp_index_file
, basename
, p
- basename
);
217 strcpy(temp_index_file
+ (p
- basename
), TEMP_INDEX_TEMPLATE
);
220 temp_index_file
= strsave(TEMP_INDEX_TEMPLATE
);
222 if (!mktemp(temp_index_file
) || !temp_index_file
[0])
223 fatal("cannot create file name for temporary file");
224 catch_fatal_signals();
225 int fd
= creat(temp_index_file
, S_IRUSR
|S_IRGRP
|S_IROTH
);
227 fatal("can't create temporary index file: %1", strerror(errno
));
228 indxfp
= fdopen(fd
, "w");
230 fatal("fdopen failed");
231 if (fseek(indxfp
, sizeof(index_header
), 0) < 0)
232 fatal("can't seek past index header: %1", strerror(errno
));
236 if (strcmp(foption
, "-") != 0) {
238 fp
= fopen(foption
, "r");
240 fatal("can't open `%1': %2", foption
, strerror(errno
));
246 for (c
= getc(fp
); c
!= '\n' && c
!= EOF
; c
= getc(fp
)) {
248 error_with_file_and_line(foption
, lineno
,
249 "nul character in pathname ignored");
253 if (path
.length() > 0) {
255 if (!(*parser
)(path
.contents()))
266 for (int i
= optind
; i
< argc
; i
++)
267 if (!(*parser
)(argv
[i
]))
270 if (fclose(indxfp
) < 0)
271 fatal("error closing temporary index file: %1", strerror(errno
));
272 char *index_file
= new char[strlen(basename
) + sizeof(INDEX_SUFFIX
)];
273 strcpy(index_file
, basename
);
274 strcat(index_file
, INDEX_SUFFIX
);
276 if (rename(temp_index_file
, index_file
) < 0)
277 fatal("can't rename temporary index file: %1", strerror(errno
));
278 #else /* not HAVE_RENAME */
279 ignore_fatal_signals();
280 if (unlink(index_file
) < 0) {
282 fatal("can't unlink `%1': %2", index_file
, strerror(errno
));
284 if (link(temp_index_file
, index_file
) < 0)
285 fatal("can't link temporary index file: %1", strerror(errno
));
286 if (unlink(temp_index_file
) < 0)
287 fatal("can't unlink temporary index file: %1", strerror(errno
));
288 #endif /* not HAVE_RENAME */
296 "usage: %s [-vw] [-c file] [-d dir] [-f file] [-h n] [-i XYZ] [-k n]\n"
297 " [-l n] [-n n] [-o base] [-t n] [files...]\n",
302 static void check_integer_arg(char opt
, const char *arg
, int min
, int *res
)
305 long n
= strtol(arg
, &ptr
, 10);
306 if (n
== 0 && ptr
== arg
)
307 error("argument to -%1 not an integer", opt
);
309 error("argument to -%1 must not be less than %2", opt
, min
);
312 error("argument to -%1 greater than maximum integer", opt
);
313 else if (*ptr
!= '\0')
314 error("junk after integer argument to -%1", opt
);
319 static char *get_cwd()
325 buf
= new char[size
];
326 if (getcwd(buf
, size
))
329 fatal("cannot get current working directory: %1", strerror(errno
));
332 fatal("current working directory longer than INT_MAX");
333 if (size
> INT_MAX
/2)
341 word_list::word_list(const char *s
, int n
, word_list
*p
)
348 static void read_common_words_file()
350 if (n_ignore_words
<= 0)
353 FILE *fp
= fopen(common_words_file
, "r");
355 fatal("can't open `%1': %2", common_words_file
, strerror(errno
));
356 common_words_table
= new word_list
* [hash_table_size
];
357 for (int i
= 0; i
< hash_table_size
; i
++)
358 common_words_table
[i
] = 0;
363 while (c
!= EOF
&& !csalnum(c
))
368 if (key_len
< truncate_len
)
369 key_buffer
[key_len
++] = cmlower(c
);
371 } while (c
!= EOF
&& csalnum(c
));
372 if (key_len
>= shortest_len
) {
373 int h
= hash(key_buffer
, key_len
) % hash_table_size
;
374 common_words_table
[h
] = new word_list(key_buffer
, key_len
,
375 common_words_table
[h
]);
377 if (++count
>= n_ignore_words
)
383 n_ignore_words
= count
;
387 static int do_whole_file(const char *filename
)
390 FILE *fp
= fopen(filename
, "r");
392 error("can't open `%1': %2", filename
, strerror(errno
));
398 while ((c
= getc(fp
)) != EOF
) {
402 while ((c
= getc(fp
)) != EOF
) {
405 if (key_len
< truncate_len
)
406 key_buffer
[key_len
++] = c
;
408 if (store_key(key_buffer
, key_len
)) {
409 if (++count
>= max_keys_per_item
)
416 store_reference(filenames
.length(), 0, 0);
417 store_filename(filename
);
422 static int do_file(const char *filename
)
425 FILE *fp
= fopen(filename
, "r");
427 error("can't open `%1': %2", filename
, strerror(errno
));
430 int filename_index
= filenames
.length();
431 store_filename(filename
);
434 START
, // at the start of the file; also in between references
435 BOL
, // in the middle of a reference, at the beginning of the line
436 PERCENT
, // seen a percent at the beginning of the line
437 IGNORE
, // ignoring a field
438 IGNORE_BOL
, // at the beginning of a line ignoring a field
439 KEY
, // in the middle of a key
440 DISCARD
, // after truncate_len bytes of a key
441 MIDDLE
// in between keys
444 // In states START, BOL, IGNORE_BOL, space_count how many spaces at
445 // the beginning have been seen. In states PERCENT, IGNORE, KEY,
446 // MIDDLE space_count must be 0.
448 int byte_count
= 0; // bytes read
450 int ref_start
= -1; // position of start of current reference
458 if (c
== ' ' || c
== '\t') {
466 ref_start
= byte_count
- space_count
- 1;
470 else if (csalnum(c
)) {
481 if (space_count
> 0) {
493 store_reference(filename_index
, ref_start
,
494 byte_count
- 1 - space_count
- ref_start
);
510 if (strchr(ignore_fields
, c
) != 0)
524 if (space_count
> 0) {
536 store_reference(filename_index
, ref_start
,
537 byte_count
- 1 - space_count
- ref_start
);
548 if (key_len
< truncate_len
)
549 key_buffer
[key_len
++] = c
;
554 possibly_store_key(key_buffer
, key_len
);
564 possibly_store_key(key_buffer
, key_len
);
590 possibly_store_key(key_buffer
, key_len
);
597 store_reference(filename_index
, ref_start
,
598 byte_count
- ref_start
- space_count
);
607 static void store_reference(int filename_index
, int pos
, int len
)
610 t
.filename_index
= filename_index
;
613 fwrite_or_die(&t
, sizeof(t
), 1, indxfp
);
617 static void store_filename(const char *fn
)
623 static void init_hash_table()
625 hash_table
= new table_entry
[hash_table_size
];
626 for (int i
= 0; i
< hash_table_size
; i
++)
627 hash_table
[i
].ptr
= 0;
630 static void possibly_store_key(char *s
, int len
)
632 static int last_tagno
= -1;
633 static int key_count
;
634 if (last_tagno
!= ntags
) {
638 if (key_count
< max_keys_per_item
) {
639 if (store_key(s
, len
))
644 static int store_key(char *s
, int len
)
646 if (len
< shortest_len
)
649 for (int i
= 0; i
< len
; i
++)
650 if (!csdigit(s
[i
])) {
652 s
[i
] = cmlower(s
[i
]);
654 if (is_number
&& !(len
== 4 && s
[0] == '1' && s
[1] == '9'))
656 int h
= hash(s
, len
) % hash_table_size
;
657 if (common_words_table
) {
658 for (word_list
*ptr
= common_words_table
[h
]; ptr
; ptr
= ptr
->next
)
659 if (len
== ptr
->len
&& memcmp(s
, ptr
->str
, len
) == 0)
662 table_entry
*pp
= hash_table
+ h
;
665 else if (pp
->ptr
->v
[pp
->ptr
->used
- 1] == ntags
)
667 else if (pp
->ptr
->used
>= BLOCK_SIZE
)
668 pp
->ptr
= new block(pp
->ptr
);
669 pp
->ptr
->v
[(pp
->ptr
->used
)++] = ntags
;
673 static void write_hash_table()
675 const int minus_one
= -1;
677 for (int i
= 0; i
< hash_table_size
; i
++) {
678 block
*ptr
= hash_table
[i
].ptr
;
680 hash_table
[i
].count
= -1;
682 hash_table
[i
].count
= li
;
691 fwrite_or_die(rev
->v
, sizeof(int), rev
->used
, indxfp
);
697 fwrite_or_die(&minus_one
, sizeof(int), 1, indxfp
);
701 if (sizeof(table_entry
) == sizeof(int))
702 fwrite_or_die(hash_table
, sizeof(int), hash_table_size
, indxfp
);
704 // write it out word by word
705 for (int i
= 0; i
< hash_table_size
; i
++)
706 fwrite_or_die(&hash_table
[i
].count
, sizeof(int), 1, indxfp
);
708 fwrite_or_die(filenames
.contents(), 1, filenames
.length(), indxfp
);
709 if (fseek(indxfp
, 0, 0) < 0)
710 fatal("error seeking on index file: %1", strerror(errno
));
712 h
.magic
= INDEX_MAGIC
;
713 h
.version
= INDEX_VERSION
;
716 h
.table_size
= hash_table_size
;
717 h
.strings_size
= filenames
.length();
718 h
.truncate
= truncate_len
;
719 h
.shortest
= shortest_len
;
720 h
.common
= n_ignore_words
;
721 fwrite_or_die(&h
, sizeof(h
), 1, indxfp
);
724 static void fwrite_or_die(const void *ptr
, int size
, int nitems
, FILE *fp
)
726 if (fwrite(ptr
, size
, nitems
, fp
) != nitems
)
727 fatal("fwrite failed: %1", strerror(errno
));
730 void fatal_error_exit()
741 unlink(temp_index_file
);