2 @ Copyright (c) 2014 - 2017 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
4 * Copyright (C) 1989 - 1992, 2000, 2001 Free Software Foundation, Inc.
5 * Written by James Clark (jjc@jclark.com)
7 * This 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 * This 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.
23 #include "lkbib-config.h"
36 static void usage(FILE *stream
)
39 "Synopsis: %s [-nv] [-p database] [-i XYZ] [-t N] keys ...\n",
43 int main(int argc
, char **argv
)
45 program_name
= argv
[0];
46 static char stderr_buf
[BUFSIZ
];
47 setbuf(stderr
, stderr_buf
);
48 int search_default
= 1;
51 static const struct option long_options
[] = {
52 { "help", no_argument
, 0, CHAR_MAX
+ 1 },
53 { "version", no_argument
, 0, 'v' },
56 while ((opt
= getopt_long(argc
, argv
, "nvVi:t:p:", long_options
, NULL
))
66 linear_ignore_fields
= optarg
;
71 long n
= strtol(optarg
, &ptr
, 10);
72 if (n
== 0 && ptr
== optarg
) {
73 error("bad integer `%1' in `t' option", optarg
);
78 linear_truncate_len
= int(n
);
83 printf(L_LKBIB
" (" T_ROFF
") v" VERSION
);
88 list
.add_file(optarg
);
90 case CHAR_MAX
+ 1: // --help
101 if (optind
>= argc
) {
105 char *filename
= getenv("REFER");
107 list
.add_file(filename
);
108 else if (search_default
)
109 list
.add_file(DEFAULT_INDEX
, 1);
110 if (list
.nfiles() == 0)
111 fatal("no databases");
114 for (i
= optind
; i
< argc
; i
++)
115 total_len
+= strlen(argv
[i
]);
116 total_len
+= argc
- optind
- 1 + 1; // for spaces and '\0'
117 char *buffer
= new char[total_len
];
119 for (i
= optind
; i
< argc
; i
++) {
122 strcpy(ptr
, argv
[i
]);
123 ptr
= strchr(ptr
, '\0');
125 search_list_iterator
iter(&list
, buffer
);
129 for (count
= 0; iter
.next(&start
, &len
); count
++) {
130 if (fwrite(start
, 1, len
, stdout
) != (size_t)len
)
131 fatal("write error on stdout: %1", strerror(errno
));
132 // Can happen for last reference in file.
133 if (start
[len
- 1] != '\n')