groff before CVS: release 1.06
[s-roff.git] / lkbib / lkbib.cc
blobd0946ccae831386a36a7c6d021ea7a223b754657
1 // -*- C++ -*-
2 /* Copyright (C) 1989, 1990, 1991, 1992 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
10 version.
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
15 for more details.
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, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <assert.h>
27 #include "lib.h"
28 #include "errarg.h"
29 #include "error.h"
31 #include "defs.h"
32 #include "refid.h"
33 #include "search.h"
35 static void usage()
37 fprintf(stderr, "usage: %s [-nv] [-p database] [-i XYZ] [-t N] keys ...\n",
38 program_name);
39 exit(1);
42 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;
48 search_list list;
49 int opt;
50 while ((opt = getopt(argc, argv, "nvVi:t:p:")) != EOF)
51 switch (opt) {
52 case 'V':
53 verify_flag = 1;
54 break;
55 case 'n':
56 search_default = 0;
57 break;
58 case 'i':
59 linear_ignore_fields = optarg;
60 break;
61 case 't':
63 char *ptr;
64 long n = strtol(optarg, &ptr, 10);
65 if (n == 0 && ptr == optarg) {
66 error("bad integer `%1' in `t' option", optarg);
67 break;
69 if (n < 1)
70 n = 1;
71 linear_truncate_len = int(n);
72 break;
74 case 'v':
76 extern const char *version_string;
77 fprintf(stderr, "GNU lkbib version %s\n", version_string);
78 fflush(stderr);
79 break;
81 case 'p':
82 list.add_file(optarg);
83 break;
84 case '?':
85 usage();
86 default:
87 assert(0);
89 if (optind >= argc)
90 usage();
91 char *filename = getenv("REFER");
92 if (filename)
93 list.add_file(filename);
94 else if (search_default)
95 list.add_file(DEFAULT_INDEX, 1);
96 if (list.nfiles() == 0)
97 fatal("no databases");
98 int total_len = 0;
99 for (int i = optind; i < argc; i++)
100 total_len += strlen(argv[i]);
101 total_len += argc - optind - 1 + 1; // for spaces and '\0'
102 char *buffer = new char[total_len];
103 char *ptr = buffer;
104 for (i = optind; i < argc; i++) {
105 if (i > optind)
106 *ptr++ = ' ';
107 strcpy(ptr, argv[i]);
108 ptr = strchr(ptr, '\0');
110 search_list_iterator iter(&list, buffer);
111 char *start;
112 int len;
113 for (int count = 0; iter.next(&start, &len); count++) {
114 if (fwrite(start, 1, len, stdout) != len)
115 fatal("write error on stdout: %1", strerror(errno));
116 // Can happen for last reference in file.
117 if (start[len - 1] != '\n')
118 putchar('\n');
119 putchar('\n');
121 exit(!count);