Fix problems with preprocessor string for `man' program.
[s-roff.git] / src / utils / lookbib / lookbib.cc
blobb742a4bdd81ce85447ec2f457990a80dfdd5ab90
1 // -*- C++ -*-
2 /* Copyright (C) 1989-1992, 2000, 2001 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <assert.h>
25 #include <errno.h>
27 #include "errarg.h"
28 #include "error.h"
29 #include "lib.h"
30 #include "cset.h"
32 #include "refid.h"
33 #include "search.h"
35 extern "C" {
36 int isatty(int);
37 const char *Version_string;
40 static void usage(FILE *stream)
42 fprintf(stream, "usage: %s [-v] [-i XYZ] [-t N] database ...\n",
43 program_name);
46 int main(int argc, char **argv)
48 program_name = argv[0];
49 static char stderr_buf[BUFSIZ];
50 setbuf(stderr, stderr_buf);
51 int opt;
52 static const struct option long_options[] = {
53 { "help", no_argument, 0, CHAR_MAX + 1 },
54 { "version", no_argument, 0, 'v' },
55 { NULL, 0, 0, 0 }
57 while ((opt = getopt_long(argc, argv, "vVi:t:", long_options, NULL)) != EOF)
58 switch (opt) {
59 case 'V':
60 verify_flag = 1;
61 break;
62 case 'i':
63 linear_ignore_fields = optarg;
64 break;
65 case 't':
67 char *ptr;
68 long n = strtol(optarg, &ptr, 10);
69 if (n == 0 && ptr == optarg) {
70 error("bad integer `%1' in `t' option", optarg);
71 break;
73 if (n < 1)
74 n = 1;
75 linear_truncate_len = int(n);
76 break;
78 case 'v':
80 printf("GNU lookbib (groff) version %s\n", Version_string);
81 exit(0);
82 break;
84 case CHAR_MAX + 1: // --help
85 usage(stdout);
86 exit(0);
87 break;
88 case '?':
89 usage(stderr);
90 exit(1);
91 break;
92 default:
93 assert(0);
95 if (optind >= argc) {
96 usage(stderr);
97 exit(1);
99 search_list list;
100 for (int i = optind; i < argc; i++)
101 list.add_file(argv[i]);
102 if (list.nfiles() == 0)
103 fatal("no databases");
104 char line[1024];
105 int interactive = isatty(fileno(stdin));
106 for (;;) {
107 if (interactive) {
108 fputs("> ", stderr);
109 fflush(stderr);
111 if (!fgets(line, sizeof(line), stdin))
112 break;
113 char *ptr = line;
114 while (csspace(*ptr))
115 ptr++;
116 if (*ptr == '\0')
117 continue;
118 search_list_iterator iter(&list, line);
119 const char *start;
120 int len;
121 int count;
122 for (count = 0; iter.next(&start, &len); count++) {
123 if (fwrite(start, 1, len, stdout) != (size_t)len)
124 fatal("write error on stdout: %1", strerror(errno));
125 // Can happen for last reference in file.
126 if (start[len - 1] != '\n')
127 putchar('\n');
128 putchar('\n');
130 fflush(stdout);
131 if (interactive) {
132 fprintf(stderr, "%d found\n", count);
133 fflush(stderr);
136 if (interactive)
137 putc('\n', stderr);
138 return 0;