2 /* Copyright (C) 1989-1992, 2000, 2001, 2002, 2003, 2004
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 2, or (at your option) any later
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 along
19 with groff; see the file COPYING. If not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
31 #include "stringclass.h"
40 extern "C" const char *Version_string
;
42 #define DEFAULT_HASH_TABLE_SIZE 997
43 #define TEMP_INDEX_TEMPLATE "indxbibXXXXXX"
45 // (2^n - MALLOC_OVERHEAD) should be a good argument for malloc().
47 #define MALLOC_OVERHEAD 16
53 const int BLOCK_SIZE
= ((1024 - MALLOC_OVERHEAD
- sizeof(struct block
*)
54 - sizeof(int)) / sizeof(int));
60 block(block
*p
= 0) : next(p
), used(0) { }
74 word_list(const char *, int, word_list
*);
77 table_entry
*hash_table
;
78 int hash_table_size
= DEFAULT_HASH_TABLE_SIZE
;
79 // We make this the same size as hash_table so we only have to do one
81 static word_list
**common_words_table
= 0;
87 char *temp_index_file
= 0;
89 const char *ignore_fields
= "XYZ";
90 const char *common_words_file
= COMMON_WORDS_FILE
;
91 int n_ignore_words
= 100;
94 int max_keys_per_item
= 100;
96 static void usage(FILE *stream
);
97 static void write_hash_table();
98 static void init_hash_table();
99 static void read_common_words_file();
100 static int store_key(char *s
, int len
);
101 static void possibly_store_key(char *s
, int len
);
102 static int do_whole_file(const char *filename
);
103 static int do_file(const char *filename
);
104 static void store_reference(int filename_index
, int pos
, int len
);
105 static void check_integer_arg(char opt
, const char *arg
, int min
, int *res
);
106 static void store_filename(const char *);
107 static void fwrite_or_die(const void *ptr
, int size
, int nitems
, FILE *fp
);
108 static char *get_cwd();
112 void catch_fatal_signals();
113 void ignore_fatal_signals();
116 int main(int argc
, char **argv
)
118 program_name
= argv
[0];
119 static char stderr_buf
[BUFSIZ
];
120 setbuf(stderr
, stderr_buf
);
122 const char *base_name
= 0;
123 typedef int (*parser_t
)(const char *);
124 parser_t parser
= do_file
;
125 const char *directory
= 0;
126 const char *foption
= 0;
128 static const struct option long_options
[] = {
129 { "help", no_argument
, 0, CHAR_MAX
+ 1 },
130 { "version", no_argument
, 0, 'v' },
133 while ((opt
= getopt_long(argc
, argv
, "c:o:h:i:k:l:t:n:c:d:f:vw",
138 common_words_file
= optarg
;
147 check_integer_arg('h', optarg
, 1, &hash_table_size
);
148 if (!is_prime(hash_table_size
)) {
149 while (!is_prime(++hash_table_size
))
151 warning("%1 not prime: using %2 instead", optarg
, hash_table_size
);
155 ignore_fields
= optarg
;
158 check_integer_arg('k', optarg
, 1, &max_keys_per_item
);
161 check_integer_arg('l', optarg
, 0, &shortest_len
);
164 check_integer_arg('n', optarg
, 0, &n_ignore_words
);
170 check_integer_arg('t', optarg
, 1, &truncate_len
);
173 parser
= do_whole_file
;
176 printf("GNU indxbib (groff) version %s\n", Version_string
);
179 case CHAR_MAX
+ 1: // --help
191 if (optind
>= argc
&& foption
== 0)
192 fatal("no files and no -f option");
194 char *path
= get_cwd();
195 store_filename(path
);
199 store_filename(directory
);
201 store_filename(common_words_file
);
202 store_filename(ignore_fields
);
203 key_buffer
= new char[truncate_len
];
204 read_common_words_file();
206 base_name
= optind
< argc
? argv
[optind
] : DEFAULT_INDEX_NAME
;
207 const char *p
= strrchr(base_name
, DIR_SEPS
[0]), *p1
;
208 const char *sep
= &DIR_SEPS
[1];
210 p1
= strrchr(base_name
, *sep
);
211 if (p1
&& (!p
|| p1
> p
))
217 char *dir
= strsave(base_name
);
218 dir
[p
- base_name
] = '\0';
219 name_max
= file_name_max(dir
);
223 name_max
= file_name_max(".");
224 const char *filename
= p
? p
+ 1 : base_name
;
225 if (strlen(filename
) + sizeof(INDEX_SUFFIX
) - 1 > name_max
)
226 fatal("`%1.%2' is too long for a filename", filename
, INDEX_SUFFIX
);
229 temp_index_file
= new char[p
- base_name
+ sizeof(TEMP_INDEX_TEMPLATE
)];
230 memcpy(temp_index_file
, base_name
, p
- base_name
);
231 strcpy(temp_index_file
+ (p
- base_name
), TEMP_INDEX_TEMPLATE
);
234 temp_index_file
= strsave(TEMP_INDEX_TEMPLATE
);
236 catch_fatal_signals();
237 int fd
= mkstemp(temp_index_file
);
239 fatal("can't create temporary index file: %1", strerror(errno
));
240 indxfp
= fdopen(fd
, FOPEN_WB
);
242 fatal("fdopen failed");
243 if (fseek(indxfp
, sizeof(index_header
), 0) < 0)
244 fatal("can't seek past index header: %1", strerror(errno
));
248 if (strcmp(foption
, "-") != 0) {
250 fp
= fopen(foption
, "r");
252 fatal("can't open `%1': %2", foption
, strerror(errno
));
258 for (c
= getc(fp
); c
!= '\n' && c
!= EOF
; c
= getc(fp
)) {
260 error_with_file_and_line(foption
, lineno
,
261 "nul character in pathname ignored");
265 if (path
.length() > 0) {
267 if (!(*parser
)(path
.contents()))
278 for (int i
= optind
; i
< argc
; i
++)
279 if (!(*parser
)(argv
[i
]))
282 if (fclose(indxfp
) < 0)
283 fatal("error closing temporary index file: %1", strerror(errno
));
284 char *index_file
= new char[strlen(base_name
) + sizeof(INDEX_SUFFIX
)];
285 strcpy(index_file
, base_name
);
286 strcat(index_file
, INDEX_SUFFIX
);
289 if (access(index_file
, R_OK
) == 0)
292 if (rename(temp_index_file
, index_file
) < 0) {
294 // RENAME could fail on plain MSDOS filesystems because
295 // INDEX_FILE is an invalid filename, e.g. it has multiple dots.
296 char *fname
= p
? index_file
+ (p
- base_name
) : 0;
299 // Replace the dot with an underscore and try again.
301 && (dot
= strchr(fname
, '.')) != 0
302 && strcmp(dot
, INDEX_SUFFIX
) != 0)
304 if (rename(temp_index_file
, index_file
) < 0)
306 fatal("can't rename temporary index file: %1", strerror(errno
));
308 #else /* not HAVE_RENAME */
309 ignore_fatal_signals();
310 if (unlink(index_file
) < 0) {
312 fatal("can't unlink `%1': %2", index_file
, strerror(errno
));
314 if (link(temp_index_file
, index_file
) < 0)
315 fatal("can't link temporary index file: %1", strerror(errno
));
316 if (unlink(temp_index_file
) < 0)
317 fatal("can't unlink temporary index file: %1", strerror(errno
));
318 #endif /* not HAVE_RENAME */
323 static void usage(FILE *stream
)
326 "usage: %s [-vw] [-c file] [-d dir] [-f file] [-h n] [-i XYZ] [-k n]\n"
327 " [-l n] [-n n] [-o base] [-t n] [files...]\n",
331 static void check_integer_arg(char opt
, const char *arg
, int min
, int *res
)
334 long n
= strtol(arg
, &ptr
, 10);
335 if (n
== 0 && ptr
== arg
)
336 error("argument to -%1 not an integer", opt
);
338 error("argument to -%1 must not be less than %2", opt
, min
);
341 error("argument to -%1 greater than maximum integer", opt
);
342 else if (*ptr
!= '\0')
343 error("junk after integer argument to -%1", opt
);
348 static char *get_cwd()
354 buf
= new char[size
];
355 if (getcwd(buf
, size
))
358 fatal("cannot get current working directory: %1", strerror(errno
));
361 fatal("current working directory longer than INT_MAX");
362 if (size
> INT_MAX
/2)
370 word_list::word_list(const char *s
, int n
, word_list
*p
)
377 static void read_common_words_file()
379 if (n_ignore_words
<= 0)
382 FILE *fp
= fopen(common_words_file
, "r");
384 fatal("can't open `%1': %2", common_words_file
, strerror(errno
));
385 common_words_table
= new word_list
* [hash_table_size
];
386 for (int i
= 0; i
< hash_table_size
; i
++)
387 common_words_table
[i
] = 0;
392 while (c
!= EOF
&& !csalnum(c
))
397 if (key_len
< truncate_len
)
398 key_buffer
[key_len
++] = cmlower(c
);
400 } while (c
!= EOF
&& csalnum(c
));
401 if (key_len
>= shortest_len
) {
402 int h
= hash(key_buffer
, key_len
) % hash_table_size
;
403 common_words_table
[h
] = new word_list(key_buffer
, key_len
,
404 common_words_table
[h
]);
406 if (++count
>= n_ignore_words
)
412 n_ignore_words
= count
;
416 static int do_whole_file(const char *filename
)
419 FILE *fp
= fopen(filename
, "r");
421 error("can't open `%1': %2", filename
, strerror(errno
));
427 while ((c
= getc(fp
)) != EOF
) {
431 while ((c
= getc(fp
)) != EOF
) {
434 if (key_len
< truncate_len
)
435 key_buffer
[key_len
++] = c
;
437 if (store_key(key_buffer
, key_len
)) {
438 if (++count
>= max_keys_per_item
)
445 store_reference(filenames
.length(), 0, 0);
446 store_filename(filename
);
451 static int do_file(const char *filename
)
454 // Need binary I/O for MS-DOS/MS-Windows, because indxbib relies on
455 // byte counts to be consistent with fseek.
456 FILE *fp
= fopen(filename
, FOPEN_RB
);
458 error("can't open `%1': %2", filename
, strerror(errno
));
461 int filename_index
= filenames
.length();
462 store_filename(filename
);
465 START
, // at the start of the file; also in between references
466 BOL
, // in the middle of a reference, at the beginning of the line
467 PERCENT
, // seen a percent at the beginning of the line
468 IGNORE
, // ignoring a field
469 IGNORE_BOL
, // at the beginning of a line ignoring a field
470 KEY
, // in the middle of a key
471 DISCARD
, // after truncate_len bytes of a key
472 MIDDLE
// in between keys
475 // In states START, BOL, IGNORE_BOL, space_count how many spaces at
476 // the beginning have been seen. In states PERCENT, IGNORE, KEY,
477 // MIDDLE space_count must be 0.
479 int byte_count
= 0; // bytes read
481 int ref_start
= -1; // position of start of current reference
486 // We opened the file in binary mode, so we need to skip
487 // every CR character before a Newline.
497 #if defined(__MSDOS__) || defined(_MSC_VER) || defined(__EMX__)
498 else if (c
== 0x1a) // ^Z means EOF in text files
504 if (c
== ' ' || c
== '\t') {
512 ref_start
= byte_count
- space_count
- 1;
516 else if (csalnum(c
)) {
527 if (space_count
> 0) {
539 store_reference(filename_index
, ref_start
,
540 byte_count
- 1 - space_count
- ref_start
);
556 if (strchr(ignore_fields
, c
) != 0)
570 if (space_count
> 0) {
582 store_reference(filename_index
, ref_start
,
583 byte_count
- 1 - space_count
- ref_start
);
594 if (key_len
< truncate_len
)
595 key_buffer
[key_len
++] = c
;
600 possibly_store_key(key_buffer
, key_len
);
610 possibly_store_key(key_buffer
, key_len
);
636 possibly_store_key(key_buffer
, key_len
);
643 store_reference(filename_index
, ref_start
,
644 byte_count
- ref_start
- space_count
);
653 static void store_reference(int filename_index
, int pos
, int len
)
656 t
.filename_index
= filename_index
;
659 fwrite_or_die(&t
, sizeof(t
), 1, indxfp
);
663 static void store_filename(const char *fn
)
669 static void init_hash_table()
671 hash_table
= new table_entry
[hash_table_size
];
672 for (int i
= 0; i
< hash_table_size
; i
++)
673 hash_table
[i
].ptr
= 0;
676 static void possibly_store_key(char *s
, int len
)
678 static int last_tagno
= -1;
679 static int key_count
;
680 if (last_tagno
!= ntags
) {
684 if (key_count
< max_keys_per_item
) {
685 if (store_key(s
, len
))
690 static int store_key(char *s
, int len
)
692 if (len
< shortest_len
)
695 for (int i
= 0; i
< len
; i
++)
696 if (!csdigit(s
[i
])) {
698 s
[i
] = cmlower(s
[i
]);
700 if (is_number
&& !(len
== 4 && s
[0] == '1' && s
[1] == '9'))
702 int h
= hash(s
, len
) % hash_table_size
;
703 if (common_words_table
) {
704 for (word_list
*ptr
= common_words_table
[h
]; ptr
; ptr
= ptr
->next
)
705 if (len
== ptr
->len
&& memcmp(s
, ptr
->str
, len
) == 0)
708 table_entry
*pp
= hash_table
+ h
;
711 else if (pp
->ptr
->v
[pp
->ptr
->used
- 1] == ntags
)
713 else if (pp
->ptr
->used
>= BLOCK_SIZE
)
714 pp
->ptr
= new block(pp
->ptr
);
715 pp
->ptr
->v
[(pp
->ptr
->used
)++] = ntags
;
719 static void write_hash_table()
721 const int minus_one
= -1;
723 for (int i
= 0; i
< hash_table_size
; i
++) {
724 block
*ptr
= hash_table
[i
].ptr
;
726 hash_table
[i
].count
= -1;
728 hash_table
[i
].count
= li
;
737 fwrite_or_die(rev
->v
, sizeof(int), rev
->used
, indxfp
);
743 fwrite_or_die(&minus_one
, sizeof(int), 1, indxfp
);
747 if (sizeof(table_entry
) == sizeof(int))
748 fwrite_or_die(hash_table
, sizeof(int), hash_table_size
, indxfp
);
750 // write it out word by word
751 for (int i
= 0; i
< hash_table_size
; i
++)
752 fwrite_or_die(&hash_table
[i
].count
, sizeof(int), 1, indxfp
);
754 fwrite_or_die(filenames
.contents(), 1, filenames
.length(), indxfp
);
755 if (fseek(indxfp
, 0, 0) < 0)
756 fatal("error seeking on index file: %1", strerror(errno
));
758 h
.magic
= INDEX_MAGIC
;
759 h
.version
= INDEX_VERSION
;
762 h
.table_size
= hash_table_size
;
763 h
.strings_size
= filenames
.length();
764 h
.truncate
= truncate_len
;
765 h
.shortest
= shortest_len
;
766 h
.common
= n_ignore_words
;
767 fwrite_or_die(&h
, sizeof(h
), 1, indxfp
);
770 static void fwrite_or_die(const void *ptr
, int size
, int nitems
, FILE *fp
)
772 if (fwrite(ptr
, size
, nitems
, fp
) != (size_t)nitems
)
773 fatal("fwrite failed: %1", strerror(errno
));
776 void fatal_error_exit()
787 unlink(temp_index_file
);