1*69495Sbostic // -*- C++ -*-
2*69495Sbostic /* Copyright (C) 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
3*69495Sbostic      Written by James Clark (jjc@jclark.com)
4*69495Sbostic 
5*69495Sbostic This file is part of groff.
6*69495Sbostic 
7*69495Sbostic groff is free software; you can redistribute it and/or modify it under
8*69495Sbostic the terms of the GNU General Public License as published by the Free
9*69495Sbostic Software Foundation; either version 2, or (at your option) any later
10*69495Sbostic version.
11*69495Sbostic 
12*69495Sbostic groff is distributed in the hope that it will be useful, but WITHOUT ANY
13*69495Sbostic WARRANTY; without even the implied warranty of MERCHANTABILITY or
14*69495Sbostic FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15*69495Sbostic for more details.
16*69495Sbostic 
17*69495Sbostic You should have received a copy of the GNU General Public License along
18*69495Sbostic with groff; see the file COPYING.  If not, write to the Free Software
19*69495Sbostic Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20*69495Sbostic 
21*69495Sbostic #include <stdlib.h>
22*69495Sbostic #include <stdio.h>
23*69495Sbostic #include <errno.h>
24*69495Sbostic #include <string.h>
25*69495Sbostic #include <assert.h>
26*69495Sbostic 
27*69495Sbostic #include "lib.h"
28*69495Sbostic #include "errarg.h"
29*69495Sbostic #include "error.h"
30*69495Sbostic 
31*69495Sbostic #include "defs.h"
32*69495Sbostic #include "refid.h"
33*69495Sbostic #include "search.h"
34*69495Sbostic 
35*69495Sbostic static void usage()
36*69495Sbostic {
37*69495Sbostic   fprintf(stderr, "usage: %s [-nv] [-p database] [-i XYZ] [-t N] keys ...\n",
38*69495Sbostic 	  program_name);
39*69495Sbostic   exit(1);
40*69495Sbostic }
41*69495Sbostic 
42*69495Sbostic main(int argc, char **argv)
43*69495Sbostic {
44*69495Sbostic   program_name = argv[0];
45*69495Sbostic   static char stderr_buf[BUFSIZ];
46*69495Sbostic   setbuf(stderr, stderr_buf);
47*69495Sbostic   int search_default = 1;
48*69495Sbostic   search_list list;
49*69495Sbostic   int opt;
50*69495Sbostic   while ((opt = getopt(argc, argv, "nvVi:t:p:")) != EOF)
51*69495Sbostic     switch (opt) {
52*69495Sbostic     case 'V':
53*69495Sbostic       verify_flag = 1;
54*69495Sbostic       break;
55*69495Sbostic     case 'n':
56*69495Sbostic       search_default = 0;
57*69495Sbostic       break;
58*69495Sbostic     case 'i':
59*69495Sbostic       linear_ignore_fields = optarg;
60*69495Sbostic       break;
61*69495Sbostic     case 't':
62*69495Sbostic       {
63*69495Sbostic 	char *ptr;
64*69495Sbostic 	long n = strtol(optarg, &ptr, 10);
65*69495Sbostic 	if (n == 0 && ptr == optarg) {
66*69495Sbostic 	  error("bad integer `%1' in `t' option", optarg);
67*69495Sbostic 	  break;
68*69495Sbostic 	}
69*69495Sbostic 	if (n < 1)
70*69495Sbostic 	  n = 1;
71*69495Sbostic 	linear_truncate_len = int(n);
72*69495Sbostic 	break;
73*69495Sbostic       }
74*69495Sbostic     case 'v':
75*69495Sbostic       {
76*69495Sbostic 	extern const char *version_string;
77*69495Sbostic 	fprintf(stderr, "GNU lkbib version %s\n", version_string);
78*69495Sbostic 	fflush(stderr);
79*69495Sbostic 	break;
80*69495Sbostic       }
81*69495Sbostic     case 'p':
82*69495Sbostic       list.add_file(optarg);
83*69495Sbostic       break;
84*69495Sbostic     case '?':
85*69495Sbostic       usage();
86*69495Sbostic     default:
87*69495Sbostic       assert(0);
88*69495Sbostic     }
89*69495Sbostic   if (optind >= argc)
90*69495Sbostic     usage();
91*69495Sbostic   char *filename = getenv("REFER");
92*69495Sbostic   if (filename)
93*69495Sbostic     list.add_file(filename);
94*69495Sbostic   else if (search_default)
95*69495Sbostic     list.add_file(DEFAULT_INDEX, 1);
96*69495Sbostic   if (list.nfiles() == 0)
97*69495Sbostic     fatal("no databases");
98*69495Sbostic   int total_len = 0;
99*69495Sbostic   for (int i = optind; i < argc; i++)
100*69495Sbostic     total_len += strlen(argv[i]);
101*69495Sbostic   total_len += argc - optind - 1 + 1; // for spaces and '\0'
102*69495Sbostic   char *buffer = new char[total_len];
103*69495Sbostic   char *ptr = buffer;
104*69495Sbostic   for (i = optind; i < argc; i++) {
105*69495Sbostic     if (i > optind)
106*69495Sbostic       *ptr++ = ' ';
107*69495Sbostic     strcpy(ptr, argv[i]);
108*69495Sbostic     ptr = strchr(ptr, '\0');
109*69495Sbostic   }
110*69495Sbostic   search_list_iterator iter(&list, buffer);
111*69495Sbostic   const char *start;
112*69495Sbostic   int len;
113*69495Sbostic   for (int count = 0; iter.next(&start, &len); count++) {
114*69495Sbostic     if (fwrite(start, 1, len, stdout) != len)
115*69495Sbostic       fatal("write error on stdout: %1", strerror(errno));
116*69495Sbostic     // Can happen for last reference in file.
117*69495Sbostic     if (start[len - 1] != '\n')
118*69495Sbostic       putchar('\n');
119*69495Sbostic     putchar('\n');
120*69495Sbostic   }
121*69495Sbostic   exit(!count);
122*69495Sbostic }
123