4 /* Copyright (C) 1989-1992, 2000, 2001 Free Software Foundation, Inc.
5 Written by James Clark (jjc@jclark.com)
7 This file is part of groff.
9 groff is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2, or (at your option) any later
14 groff is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License along
20 with groff; see the file COPYING. If not, write to the Free Software
21 Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
36 extern "C" const char *Version_string
;
38 static void usage(FILE *stream
)
40 fprintf(stream
, "usage: %s [-nv] [-p database] [-i XYZ] [-t N] keys ...\n",
44 int main(int argc
, char **argv
)
46 program_name
= argv
[0];
47 static char stderr_buf
[BUFSIZ
];
48 setbuf(stderr
, stderr_buf
);
49 int search_default
= 1;
52 static const struct option long_options
[] = {
53 { "help", no_argument
, 0, CHAR_MAX
+ 1 },
54 { "version", no_argument
, 0, 'v' },
57 while ((opt
= getopt_long(argc
, argv
, "nvVi:t:p:", long_options
, NULL
))
67 linear_ignore_fields
= optarg
;
72 long n
= strtol(optarg
, &ptr
, 10);
73 if (n
== 0 && ptr
== optarg
) {
74 error("bad integer `%1' in `t' option", optarg
);
79 linear_truncate_len
= int(n
);
84 printf("GNU lkbib (groff) version %s\n", Version_string
);
89 list
.add_file(optarg
);
91 case CHAR_MAX
+ 1: // --help
102 if (optind
>= argc
) {
106 char *filename
= getenv("REFER");
108 list
.add_file(filename
);
109 else if (search_default
)
110 list
.add_file(DEFAULT_INDEX
, 1);
111 if (list
.nfiles() == 0)
112 fatal("no databases");
115 for (i
= optind
; i
< argc
; i
++)
116 total_len
+= strlen(argv
[i
]);
117 total_len
+= argc
- optind
- 1 + 1; // for spaces and '\0'
118 char *buffer
= new char[total_len
];
120 for (i
= optind
; i
< argc
; i
++) {
123 strcpy(ptr
, argv
[i
]);
124 ptr
= strchr(ptr
, '\0');
126 search_list_iterator
iter(&list
, buffer
);
130 for (count
= 0; iter
.next(&start
, &len
); count
++) {
131 if (fwrite(start
, 1, len
, stdout
) != (size_t)len
)
132 fatal("write error on stdout: %1", strerror(errno
));
133 // Can happen for last reference in file.
134 if (start
[len
- 1] != '\n')