4 /* Copyright (C) 1989-1992, 2000, 2001, 2002, 2003, 2004
5 Free Software Foundation, Inc.
6 Written by James Clark (jjc@jclark.com)
8 This file is part of groff.
10 groff is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 2, or (at your option) any later
15 groff is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 You should have received a copy of the GNU General Public License along
21 with groff; see the file COPYING. If not, write to the Free Software
22 Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
33 #include "stringclass.h"
42 extern "C" const char *Version_string
;
44 #define DEFAULT_HASH_TABLE_SIZE 997
45 #define TEMP_INDEX_TEMPLATE "indxbibXXXXXX"
47 // (2^n - MALLOC_OVERHEAD) should be a good argument for malloc().
49 #define MALLOC_OVERHEAD 16
55 const int BLOCK_SIZE
= ((1024 - MALLOC_OVERHEAD
- sizeof(struct block
*)
56 - sizeof(int)) / sizeof(int));
62 block(block
*p
= 0) : next(p
), used(0) { }
76 word_list(const char *, int, word_list
*);
79 table_entry
*hash_table
;
80 int hash_table_size
= DEFAULT_HASH_TABLE_SIZE
;
81 // We make this the same size as hash_table so we only have to do one
83 static word_list
**common_words_table
= 0;
89 char *temp_index_file
= 0;
91 const char *ignore_fields
= "XYZ";
92 const char *common_words_file
= COMMON_WORDS_FILE
;
93 int n_ignore_words
= 100;
96 int max_keys_per_item
= 100;
98 static void usage(FILE *stream
);
99 static void write_hash_table();
100 static void init_hash_table();
101 static void read_common_words_file();
102 static int store_key(char *s
, int len
);
103 static void possibly_store_key(char *s
, int len
);
104 static int do_whole_file(const char *filename
);
105 static int do_file(const char *filename
);
106 static void store_reference(int filename_index
, int pos
, int len
);
107 static void check_integer_arg(char opt
, const char *arg
, int min
, int *res
);
108 static void store_filename(const char *);
109 static void fwrite_or_die(const void *ptr
, int size
, int nitems
, FILE *fp
);
110 static char *get_cwd();
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 *base_name
= 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 static const struct option long_options
[] = {
131 { "help", no_argument
, 0, CHAR_MAX
+ 1 },
132 { "version", no_argument
, 0, 'v' },
135 while ((opt
= getopt_long(argc
, argv
, "c:o:h:i:k:l:t:n:c:d:f:vw",
140 common_words_file
= optarg
;
149 check_integer_arg('h', optarg
, 1, &hash_table_size
);
150 if (!is_prime(hash_table_size
)) {
151 while (!is_prime(++hash_table_size
))
153 warning("%1 not prime: using %2 instead", optarg
, hash_table_size
);
157 ignore_fields
= optarg
;
160 check_integer_arg('k', optarg
, 1, &max_keys_per_item
);
163 check_integer_arg('l', optarg
, 0, &shortest_len
);
166 check_integer_arg('n', optarg
, 0, &n_ignore_words
);
172 check_integer_arg('t', optarg
, 1, &truncate_len
);
175 parser
= do_whole_file
;
178 printf("GNU indxbib (groff) version %s\n", Version_string
);
181 case CHAR_MAX
+ 1: // --help
193 if (optind
>= argc
&& foption
== 0)
194 fatal("no files and no -f option");
196 char *path
= get_cwd();
197 store_filename(path
);
201 store_filename(directory
);
203 store_filename(common_words_file
);
204 store_filename(ignore_fields
);
205 key_buffer
= new char[truncate_len
];
206 read_common_words_file();
208 base_name
= optind
< argc
? argv
[optind
] : DEFAULT_INDEX_NAME
;
209 const char *p
= strrchr(base_name
, DIR_SEPS
[0]), *p1
;
210 const char *sep
= &DIR_SEPS
[1];
212 p1
= strrchr(base_name
, *sep
);
213 if (p1
&& (!p
|| p1
> p
))
219 char *dir
= strsave(base_name
);
220 dir
[p
- base_name
] = '\0';
221 name_max
= file_name_max(dir
);
225 name_max
= file_name_max(".");
226 const char *filename
= p
? p
+ 1 : base_name
;
227 if (strlen(filename
) + sizeof(INDEX_SUFFIX
) - 1 > name_max
)
228 fatal("`%1.%2' is too long for a filename", filename
, INDEX_SUFFIX
);
231 temp_index_file
= new char[p
- base_name
+ sizeof(TEMP_INDEX_TEMPLATE
)];
232 memcpy(temp_index_file
, base_name
, p
- base_name
);
233 strcpy(temp_index_file
+ (p
- base_name
), TEMP_INDEX_TEMPLATE
);
236 temp_index_file
= strsave(TEMP_INDEX_TEMPLATE
);
238 catch_fatal_signals();
239 int fd
= mkstemp(temp_index_file
);
241 fatal("can't create temporary index file: %1", strerror(errno
));
242 indxfp
= fdopen(fd
, FOPEN_WB
);
244 fatal("fdopen failed");
245 if (fseek(indxfp
, sizeof(index_header
), 0) < 0)
246 fatal("can't seek past index header: %1", strerror(errno
));
250 if (strcmp(foption
, "-") != 0) {
252 fp
= fopen(foption
, "r");
254 fatal("can't open `%1': %2", foption
, strerror(errno
));
260 for (c
= getc(fp
); c
!= '\n' && c
!= EOF
; c
= getc(fp
)) {
262 error_with_file_and_line(foption
, lineno
,
263 "nul character in pathname ignored");
267 if (path
.length() > 0) {
269 if (!(*parser
)(path
.contents()))
280 for (int i
= optind
; i
< argc
; i
++)
281 if (!(*parser
)(argv
[i
]))
284 if (fclose(indxfp
) < 0)
285 fatal("error closing temporary index file: %1", strerror(errno
));
286 char *index_file
= new char[strlen(base_name
) + sizeof(INDEX_SUFFIX
)];
287 strcpy(index_file
, base_name
);
288 strcat(index_file
, INDEX_SUFFIX
);
291 if (access(index_file
, R_OK
) == 0)
294 if (rename(temp_index_file
, index_file
) < 0) {
296 // RENAME could fail on plain MSDOS filesystems because
297 // INDEX_FILE is an invalid filename, e.g. it has multiple dots.
298 char *fname
= p
? index_file
+ (p
- base_name
) : 0;
301 // Replace the dot with an underscore and try again.
303 && (dot
= strchr(fname
, '.')) != 0
304 && strcmp(dot
, INDEX_SUFFIX
) != 0)
306 if (rename(temp_index_file
, index_file
) < 0)
308 fatal("can't rename temporary index file: %1", strerror(errno
));
310 #else /* not HAVE_RENAME */
311 ignore_fatal_signals();
312 if (unlink(index_file
) < 0) {
314 fatal("can't unlink `%1': %2", index_file
, strerror(errno
));
316 if (link(temp_index_file
, index_file
) < 0)
317 fatal("can't link temporary index file: %1", strerror(errno
));
318 if (unlink(temp_index_file
) < 0)
319 fatal("can't unlink temporary index file: %1", strerror(errno
));
320 #endif /* not HAVE_RENAME */
325 static void usage(FILE *stream
)
328 "usage: %s [-vw] [-c file] [-d dir] [-f file] [-h n] [-i XYZ] [-k n]\n"
329 " [-l n] [-n n] [-o base] [-t n] [files...]\n",
333 static void check_integer_arg(char opt
, const char *arg
, int min
, int *res
)
336 long n
= strtol(arg
, &ptr
, 10);
337 if (n
== 0 && ptr
== arg
)
338 error("argument to -%1 not an integer", opt
);
340 error("argument to -%1 must not be less than %2", opt
, min
);
343 error("argument to -%1 greater than maximum integer", opt
);
344 else if (*ptr
!= '\0')
345 error("junk after integer argument to -%1", opt
);
350 static char *get_cwd()
356 buf
= new char[size
];
357 if (getcwd(buf
, size
))
360 fatal("cannot get current working directory: %1", strerror(errno
));
363 fatal("current working directory longer than INT_MAX");
364 if (size
> INT_MAX
/2)
372 word_list::word_list(const char *s
, int n
, word_list
*p
)
379 static void read_common_words_file()
381 if (n_ignore_words
<= 0)
384 FILE *fp
= fopen(common_words_file
, "r");
386 fatal("can't open `%1': %2", common_words_file
, strerror(errno
));
387 common_words_table
= new word_list
* [hash_table_size
];
388 for (int i
= 0; i
< hash_table_size
; i
++)
389 common_words_table
[i
] = 0;
394 while (c
!= EOF
&& !csalnum(c
))
399 if (key_len
< truncate_len
)
400 key_buffer
[key_len
++] = cmlower(c
);
402 } while (c
!= EOF
&& csalnum(c
));
403 if (key_len
>= shortest_len
) {
404 int h
= hash(key_buffer
, key_len
) % hash_table_size
;
405 common_words_table
[h
] = new word_list(key_buffer
, key_len
,
406 common_words_table
[h
]);
408 if (++count
>= n_ignore_words
)
414 n_ignore_words
= count
;
418 static int do_whole_file(const char *filename
)
421 FILE *fp
= fopen(filename
, "r");
423 error("can't open `%1': %2", filename
, strerror(errno
));
429 while ((c
= getc(fp
)) != EOF
) {
433 while ((c
= getc(fp
)) != EOF
) {
436 if (key_len
< truncate_len
)
437 key_buffer
[key_len
++] = c
;
439 if (store_key(key_buffer
, key_len
)) {
440 if (++count
>= max_keys_per_item
)
447 store_reference(filenames
.length(), 0, 0);
448 store_filename(filename
);
453 static int do_file(const char *filename
)
456 // Need binary I/O for MS-DOS/MS-Windows, because indxbib relies on
457 // byte counts to be consistent with fseek.
458 FILE *fp
= fopen(filename
, FOPEN_RB
);
460 error("can't open `%1': %2", filename
, strerror(errno
));
463 int filename_index
= filenames
.length();
464 store_filename(filename
);
467 START
, // at the start of the file; also in between references
468 BOL
, // in the middle of a reference, at the beginning of the line
469 PERCENT
, // seen a percent at the beginning of the line
470 IGNORE
, // ignoring a field
471 IGNORE_BOL
, // at the beginning of a line ignoring a field
472 KEY
, // in the middle of a key
473 DISCARD
, // after truncate_len bytes of a key
474 MIDDLE
// in between keys
477 // In states START, BOL, IGNORE_BOL, space_count how many spaces at
478 // the beginning have been seen. In states PERCENT, IGNORE, KEY,
479 // MIDDLE space_count must be 0.
481 int byte_count
= 0; // bytes read
483 int ref_start
= -1; // position of start of current reference
488 // We opened the file in binary mode, so we need to skip
489 // every CR character before a Newline.
499 #if defined(__MSDOS__) || defined(_MSC_VER) || defined(__EMX__)
500 else if (c
== 0x1a) // ^Z means EOF in text files
506 if (c
== ' ' || c
== '\t') {
514 ref_start
= byte_count
- space_count
- 1;
518 else if (csalnum(c
)) {
529 if (space_count
> 0) {
541 store_reference(filename_index
, ref_start
,
542 byte_count
- 1 - space_count
- ref_start
);
558 if (strchr(ignore_fields
, c
) != 0)
572 if (space_count
> 0) {
584 store_reference(filename_index
, ref_start
,
585 byte_count
- 1 - space_count
- ref_start
);
596 if (key_len
< truncate_len
)
597 key_buffer
[key_len
++] = c
;
602 possibly_store_key(key_buffer
, key_len
);
612 possibly_store_key(key_buffer
, key_len
);
638 possibly_store_key(key_buffer
, key_len
);
645 store_reference(filename_index
, ref_start
,
646 byte_count
- ref_start
- space_count
);
655 static void store_reference(int filename_index
, int pos
, int len
)
658 t
.filename_index
= filename_index
;
661 fwrite_or_die(&t
, sizeof(t
), 1, indxfp
);
665 static void store_filename(const char *fn
)
671 static void init_hash_table()
673 hash_table
= new table_entry
[hash_table_size
];
674 for (int i
= 0; i
< hash_table_size
; i
++)
675 hash_table
[i
].ptr
= 0;
678 static void possibly_store_key(char *s
, int len
)
680 static int last_tagno
= -1;
681 static int key_count
;
682 if (last_tagno
!= ntags
) {
686 if (key_count
< max_keys_per_item
) {
687 if (store_key(s
, len
))
692 static int store_key(char *s
, int len
)
694 if (len
< shortest_len
)
697 for (int i
= 0; i
< len
; i
++)
698 if (!csdigit(s
[i
])) {
700 s
[i
] = cmlower(s
[i
]);
702 if (is_number
&& !(len
== 4 && s
[0] == '1' && s
[1] == '9'))
704 int h
= hash(s
, len
) % hash_table_size
;
705 if (common_words_table
) {
706 for (word_list
*ptr
= common_words_table
[h
]; ptr
; ptr
= ptr
->next
)
707 if (len
== ptr
->len
&& memcmp(s
, ptr
->str
, len
) == 0)
710 table_entry
*pp
= hash_table
+ h
;
713 else if (pp
->ptr
->v
[pp
->ptr
->used
- 1] == ntags
)
715 else if (pp
->ptr
->used
>= BLOCK_SIZE
)
716 pp
->ptr
= new block(pp
->ptr
);
717 pp
->ptr
->v
[(pp
->ptr
->used
)++] = ntags
;
721 static void write_hash_table()
723 const int minus_one
= -1;
725 for (int i
= 0; i
< hash_table_size
; i
++) {
726 block
*ptr
= hash_table
[i
].ptr
;
728 hash_table
[i
].count
= -1;
730 hash_table
[i
].count
= li
;
739 fwrite_or_die(rev
->v
, sizeof(int), rev
->used
, indxfp
);
745 fwrite_or_die(&minus_one
, sizeof(int), 1, indxfp
);
749 if (sizeof(table_entry
) == sizeof(int))
750 fwrite_or_die(hash_table
, sizeof(int), hash_table_size
, indxfp
);
752 // write it out word by word
753 for (int i
= 0; i
< hash_table_size
; i
++)
754 fwrite_or_die(&hash_table
[i
].count
, sizeof(int), 1, indxfp
);
756 fwrite_or_die(filenames
.contents(), 1, filenames
.length(), indxfp
);
757 if (fseek(indxfp
, 0, 0) < 0)
758 fatal("error seeking on index file: %1", strerror(errno
));
760 h
.magic
= INDEX_MAGIC
;
761 h
.version
= INDEX_VERSION
;
764 h
.table_size
= hash_table_size
;
765 h
.strings_size
= filenames
.length();
766 h
.truncate
= truncate_len
;
767 h
.shortest
= shortest_len
;
768 h
.common
= n_ignore_words
;
769 fwrite_or_die(&h
, sizeof(h
), 1, indxfp
);
772 static void fwrite_or_die(const void *ptr
, int size
, int nitems
, FILE *fp
)
774 if (fwrite(ptr
, size
, nitems
, fp
) != (size_t)nitems
)
775 fatal("fwrite failed: %1", strerror(errno
));
778 void fatal_error_exit()
789 unlink(temp_index_file
);