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