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