2 /* Copyright (C) 1989-1992, 2000, 2001 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, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
34 extern "C" const char *Version_string;
36 static void usage(FILE *stream)
38 fprintf(stream, "usage: %s [-nv] [-p database] [-i XYZ] [-t N] keys ...\n",
42 int main(int argc, char **argv)
44 program_name = argv[0];
45 static char stderr_buf[BUFSIZ];
46 setbuf(stderr, stderr_buf);
47 int search_default = 1;
50 static const struct option long_options[] = {
51 { "help", no_argument, 0, CHAR_MAX + 1 },
52 { "version", no_argument, 0, 'v' },
55 while ((opt = getopt_long(argc, argv, "nvVi:t:p:", long_options, NULL))
65 linear_ignore_fields = optarg;
70 long n = strtol(optarg, &ptr, 10);
71 if (n == 0 && ptr == optarg) {
72 error("bad integer `%1' in `t' option", optarg);
77 linear_truncate_len = int(n);
82 printf("GNU lkbib (groff) version %s\n", Version_string);
87 list.add_file(optarg);
89 case CHAR_MAX + 1: // --help
100 if (optind >= argc) {
104 char *filename = getenv("REFER");
106 list.add_file(filename);
107 else if (search_default)
108 list.add_file(DEFAULT_INDEX, 1);
109 if (list.nfiles() == 0)
110 fatal("no databases");
113 for (i = optind; i < argc; i++)
114 total_len += strlen(argv[i]);
115 total_len += argc - optind - 1 + 1; // for spaces and '\0'
116 char *buffer = new char[total_len];
118 for (i = optind; i < argc; i++) {
121 strcpy(ptr, argv[i]);
122 ptr = strchr(ptr, '\0');
124 search_list_iterator iter(&list, buffer);
128 for (count = 0; iter.next(&start, &len); count++) {
129 if (fwrite(start, 1, len, stdout) != (size_t)len)
130 fatal("write error on stdout: %1", strerror(errno));
131 // Can happen for last reference in file.
132 if (start[len - 1] != '\n')