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"
41 // Solaris 2.5.1 has these functions,
42 // but its stdlib.h fails to declare them.
47 #define DEFAULT_HASH_TABLE_SIZE 997
48 #define TEMP_INDEX_TEMPLATE "indxbibXXXXXX"
50 // (2^n - MALLOC_OVERHEAD) should be a good argument for malloc().
52 #define MALLOC_OVERHEAD 16
58 const int BLOCK_SIZE
= ((1024 - MALLOC_OVERHEAD
- sizeof(struct block
*)
59 - sizeof(int)) / sizeof(int));
65 block(block
*p
= 0) : next(p
), used(0) { }
79 word_list(const char *, int, word_list
*);
82 table_entry
*hash_table
;
83 int hash_table_size
= DEFAULT_HASH_TABLE_SIZE
;
84 // We make this the same size as hash_table so we only have to do one
86 static word_list
**common_words_table
= 0;
92 char *temp_index_file
= 0;
94 const char *ignore_fields
= "XYZ";
95 const char *common_words_file
= COMMON_WORDS_FILE
;
96 int n_ignore_words
= 100;
99 int max_keys_per_item
= 100;
102 static void write_hash_table();
103 static void init_hash_table();
104 static void read_common_words_file();
105 static int store_key(char *s
, int len
);
106 static void possibly_store_key(char *s
, int len
);
107 static int do_whole_file(const char *filename
);
108 static int do_file(const char *filename
);
109 static void store_reference(int filename_index
, int pos
, int len
);
110 static void check_integer_arg(char opt
, const char *arg
, int min
, int *res
);
111 static void store_filename(const char *);
112 static void fwrite_or_die(const void *ptr
, int size
, int nitems
, FILE *fp
);
113 static char *get_cwd();
117 long dir_name_max(const char *);
118 void catch_fatal_signals();
119 void ignore_fatal_signals();
122 int main(int argc
, char **argv
)
124 program_name
= argv
[0];
125 static char stderr_buf
[BUFSIZ
];
126 setbuf(stderr
, stderr_buf
);
128 const char *basename
= 0;
129 typedef int (*parser_t
)(const char *);
130 parser_t parser
= do_file
;
131 const char *directory
= 0;
132 const char *foption
= 0;
134 while ((opt
= getopt(argc
, argv
, "c:o:h:i:k:l:t:n:c:d:f:vw")) != EOF
)
137 common_words_file
= optarg
;
146 check_integer_arg('h', optarg
, 1, &hash_table_size
);
147 if (!is_prime(hash_table_size
)) {
148 while (!is_prime(++hash_table_size
))
150 warning("%1 not prime: using %2 instead", optarg
, hash_table_size
);
154 ignore_fields
= optarg
;
157 check_integer_arg('k', optarg
, 1, &max_keys_per_item
);
160 check_integer_arg('l', optarg
, 0, &shortest_len
);
163 check_integer_arg('n', optarg
, 0, &n_ignore_words
);
169 check_integer_arg('t', optarg
, 1, &truncate_len
);
172 parser
= do_whole_file
;
176 extern const char *Version_string
;
177 fprintf(stderr
, "GNU indxbib version %s\n", Version_string
);
188 if (optind
>= argc
&& foption
== 0)
189 fatal("no files and no -f option");
191 char *path
= get_cwd();
192 store_filename(path
);
196 store_filename(directory
);
198 store_filename(common_words_file
);
199 store_filename(ignore_fields
);
200 key_buffer
= new char[truncate_len
];
201 read_common_words_file();
203 basename
= optind
< argc
? argv
[optind
] : DEFAULT_INDEX_NAME
;
204 const char *p
= strrchr(basename
, DIR_SEPS
[0]), *p1
;
205 const char *sep
= &DIR_SEPS
[1];
208 p1
= strrchr(basename
, *sep
);
209 if (p1
&& (!p
|| p1
> p
))
215 char *dir
= strsave(basename
);
216 dir
[p
- basename
] = '\0';
217 name_max
= dir_name_max(dir
);
221 name_max
= dir_name_max(".");
222 const char *filename
= p
? p
+ 1 : basename
;
223 if (name_max
>= 0 && 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
- basename
+ sizeof(TEMP_INDEX_TEMPLATE
)];
228 memcpy(temp_index_file
, basename
, p
- basename
);
229 strcpy(temp_index_file
+ (p
- basename
), TEMP_INDEX_TEMPLATE
);
232 temp_index_file
= strsave(TEMP_INDEX_TEMPLATE
);
235 if (!mktemp(temp_index_file
) || !temp_index_file
[0])
236 fatal("cannot create file name for temporary file");
238 catch_fatal_signals();
240 int fd
= mkstemp(temp_index_file
);
242 int fd
= creat(temp_index_file
, S_IRUSR
|S_IRGRP
|S_IROTH
);
245 fatal("can't create temporary index file: %1", strerror(errno
));
246 indxfp
= fdopen(fd
, FOPEN_WB
);
248 fatal("fdopen failed");
249 if (fseek(indxfp
, sizeof(index_header
), 0) < 0)
250 fatal("can't seek past index header: %1", strerror(errno
));
254 if (strcmp(foption
, "-") != 0) {
256 fp
= fopen(foption
, "r");
258 fatal("can't open `%1': %2", foption
, strerror(errno
));
264 for (c
= getc(fp
); c
!= '\n' && c
!= EOF
; c
= getc(fp
)) {
266 error_with_file_and_line(foption
, lineno
,
267 "nul character in pathname ignored");
271 if (path
.length() > 0) {
273 if (!(*parser
)(path
.contents()))
284 for (int i
= optind
; i
< argc
; i
++)
285 if (!(*parser
)(argv
[i
]))
288 if (fclose(indxfp
) < 0)
289 fatal("error closing temporary index file: %1", strerror(errno
));
290 char *index_file
= new char[strlen(basename
) + sizeof(INDEX_SUFFIX
)];
291 strcpy(index_file
, basename
);
292 strcat(index_file
, INDEX_SUFFIX
);
294 if (rename(temp_index_file
, index_file
) < 0)
297 // RENAME could fail on plain MSDOS filesystems because
298 // INDEX_FILE is an invalid filename, e.g. it has multiple dots.
299 char *fname
= p
? index_file
+ (p
- basename
) : 0;
302 // Replace the dot with an underscore and try again.
304 && (dot
= strchr(fname
, '.')) != 0
305 && strcmp(dot
, INDEX_SUFFIX
) != 0)
307 if (rename(temp_index_file
, index_file
) < 0)
309 fatal("can't rename temporary index file: %1", strerror(errno
));
311 #else /* not HAVE_RENAME */
312 ignore_fatal_signals();
313 if (unlink(index_file
) < 0) {
315 fatal("can't unlink `%1': %2", index_file
, strerror(errno
));
317 if (link(temp_index_file
, index_file
) < 0)
318 fatal("can't link temporary index file: %1", strerror(errno
));
319 if (unlink(temp_index_file
) < 0)
320 fatal("can't unlink temporary index file: %1", strerror(errno
));
321 #endif /* not HAVE_RENAME */
329 "usage: %s [-vw] [-c file] [-d dir] [-f file] [-h n] [-i XYZ] [-k n]\n"
330 " [-l n] [-n n] [-o base] [-t n] [files...]\n",
335 static void check_integer_arg(char opt
, const char *arg
, int min
, int *res
)
338 long n
= strtol(arg
, &ptr
, 10);
339 if (n
== 0 && ptr
== arg
)
340 error("argument to -%1 not an integer", opt
);
342 error("argument to -%1 must not be less than %2", opt
, min
);
345 error("argument to -%1 greater than maximum integer", opt
);
346 else if (*ptr
!= '\0')
347 error("junk after integer argument to -%1", opt
);
352 static char *get_cwd()
358 buf
= new char[size
];
359 if (getcwd(buf
, size
))
362 fatal("cannot get current working directory: %1", strerror(errno
));
365 fatal("current working directory longer than INT_MAX");
366 if (size
> INT_MAX
/2)
374 word_list::word_list(const char *s
, int n
, word_list
*p
)
381 static void read_common_words_file()
383 if (n_ignore_words
<= 0)
386 FILE *fp
= fopen(common_words_file
, "r");
388 fatal("can't open `%1': %2", common_words_file
, strerror(errno
));
389 common_words_table
= new word_list
* [hash_table_size
];
390 for (int i
= 0; i
< hash_table_size
; i
++)
391 common_words_table
[i
] = 0;
396 while (c
!= EOF
&& !csalnum(c
))
401 if (key_len
< truncate_len
)
402 key_buffer
[key_len
++] = cmlower(c
);
404 } while (c
!= EOF
&& csalnum(c
));
405 if (key_len
>= shortest_len
) {
406 int h
= hash(key_buffer
, key_len
) % hash_table_size
;
407 common_words_table
[h
] = new word_list(key_buffer
, key_len
,
408 common_words_table
[h
]);
410 if (++count
>= n_ignore_words
)
416 n_ignore_words
= count
;
420 static int do_whole_file(const char *filename
)
423 FILE *fp
= fopen(filename
, "r");
425 error("can't open `%1': %2", filename
, strerror(errno
));
431 while ((c
= getc(fp
)) != EOF
) {
435 while ((c
= getc(fp
)) != EOF
) {
438 if (key_len
< truncate_len
)
439 key_buffer
[key_len
++] = c
;
441 if (store_key(key_buffer
, key_len
)) {
442 if (++count
>= max_keys_per_item
)
449 store_reference(filenames
.length(), 0, 0);
450 store_filename(filename
);
455 static int do_file(const char *filename
)
458 // Need binary I/O for MS-DOS/MS-Windows, because indxbib relies on
459 // byte counts to be consistent with fseek.
460 FILE *fp
= fopen(filename
, FOPEN_RB
);
462 error("can't open `%1': %2", filename
, strerror(errno
));
465 int filename_index
= filenames
.length();
466 store_filename(filename
);
469 START
, // at the start of the file; also in between references
470 BOL
, // in the middle of a reference, at the beginning of the line
471 PERCENT
, // seen a percent at the beginning of the line
472 IGNORE
, // ignoring a field
473 IGNORE_BOL
, // at the beginning of a line ignoring a field
474 KEY
, // in the middle of a key
475 DISCARD
, // after truncate_len bytes of a key
476 MIDDLE
// in between keys
479 // In states START, BOL, IGNORE_BOL, space_count how many spaces at
480 // the beginning have been seen. In states PERCENT, IGNORE, KEY,
481 // MIDDLE space_count must be 0.
483 int byte_count
= 0; // bytes read
485 int ref_start
= -1; // position of start of current reference
490 // We opened the file in binary mode, so we need to skip
491 // every CR character before a Newline.
501 #if defined(__MSDOS__) || defined(_MSC_VER)
502 else if (c
== 0x1a) // ^Z means EOF in text files
508 if (c
== ' ' || c
== '\t') {
516 ref_start
= byte_count
- space_count
- 1;
520 else if (csalnum(c
)) {
531 if (space_count
> 0) {
543 store_reference(filename_index
, ref_start
,
544 byte_count
- 1 - space_count
- ref_start
);
560 if (strchr(ignore_fields
, c
) != 0)
574 if (space_count
> 0) {
586 store_reference(filename_index
, ref_start
,
587 byte_count
- 1 - space_count
- ref_start
);
598 if (key_len
< truncate_len
)
599 key_buffer
[key_len
++] = c
;
604 possibly_store_key(key_buffer
, key_len
);
614 possibly_store_key(key_buffer
, key_len
);
640 possibly_store_key(key_buffer
, key_len
);
647 store_reference(filename_index
, ref_start
,
648 byte_count
- ref_start
- space_count
);
657 static void store_reference(int filename_index
, int pos
, int len
)
660 t
.filename_index
= filename_index
;
663 fwrite_or_die(&t
, sizeof(t
), 1, indxfp
);
667 static void store_filename(const char *fn
)
673 static void init_hash_table()
675 hash_table
= new table_entry
[hash_table_size
];
676 for (int i
= 0; i
< hash_table_size
; i
++)
677 hash_table
[i
].ptr
= 0;
680 static void possibly_store_key(char *s
, int len
)
682 static int last_tagno
= -1;
683 static int key_count
;
684 if (last_tagno
!= ntags
) {
688 if (key_count
< max_keys_per_item
) {
689 if (store_key(s
, len
))
694 static int store_key(char *s
, int len
)
696 if (len
< shortest_len
)
699 for (int i
= 0; i
< len
; i
++)
700 if (!csdigit(s
[i
])) {
702 s
[i
] = cmlower(s
[i
]);
704 if (is_number
&& !(len
== 4 && s
[0] == '1' && s
[1] == '9'))
706 int h
= hash(s
, len
) % hash_table_size
;
707 if (common_words_table
) {
708 for (word_list
*ptr
= common_words_table
[h
]; ptr
; ptr
= ptr
->next
)
709 if (len
== ptr
->len
&& memcmp(s
, ptr
->str
, len
) == 0)
712 table_entry
*pp
= hash_table
+ h
;
715 else if (pp
->ptr
->v
[pp
->ptr
->used
- 1] == ntags
)
717 else if (pp
->ptr
->used
>= BLOCK_SIZE
)
718 pp
->ptr
= new block(pp
->ptr
);
719 pp
->ptr
->v
[(pp
->ptr
->used
)++] = ntags
;
723 static void write_hash_table()
725 const int minus_one
= -1;
727 for (int i
= 0; i
< hash_table_size
; i
++) {
728 block
*ptr
= hash_table
[i
].ptr
;
730 hash_table
[i
].count
= -1;
732 hash_table
[i
].count
= li
;
741 fwrite_or_die(rev
->v
, sizeof(int), rev
->used
, indxfp
);
747 fwrite_or_die(&minus_one
, sizeof(int), 1, indxfp
);
751 if (sizeof(table_entry
) == sizeof(int))
752 fwrite_or_die(hash_table
, sizeof(int), hash_table_size
, indxfp
);
754 // write it out word by word
755 for (int i
= 0; i
< hash_table_size
; i
++)
756 fwrite_or_die(&hash_table
[i
].count
, sizeof(int), 1, indxfp
);
758 fwrite_or_die(filenames
.contents(), 1, filenames
.length(), indxfp
);
759 if (fseek(indxfp
, 0, 0) < 0)
760 fatal("error seeking on index file: %1", strerror(errno
));
762 h
.magic
= INDEX_MAGIC
;
763 h
.version
= INDEX_VERSION
;
766 h
.table_size
= hash_table_size
;
767 h
.strings_size
= filenames
.length();
768 h
.truncate
= truncate_len
;
769 h
.shortest
= shortest_len
;
770 h
.common
= n_ignore_words
;
771 fwrite_or_die(&h
, sizeof(h
), 1, indxfp
);
774 static void fwrite_or_die(const void *ptr
, int size
, int nitems
, FILE *fp
)
776 if (fwrite(ptr
, size
, nitems
, fp
) != nitems
)
777 fatal("fwrite failed: %1", strerror(errno
));
780 void fatal_error_exit()
791 unlink(temp_index_file
);