Sync-to-go: update copyright for 2015
[s-roff.git] / src / ute-lookbib / lookbib.cpp
blob2f2985d4d417bc43f748feacec075f722d4358d2
1 /*@
2 * Copyright (c) 2014 - 2015 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
4 * Copyright (C) 1989 - 1992, 2000 - 2003 Free Software Foundation, Inc.
5 * Written by James Clark (jjc@jclark.com)
7 * This 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 * This 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, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "config.h"
23 #include "lookbib-config.h"
25 #include <assert.h>
26 #include <errno.h>
27 #include <stdlib.h>
29 #include "cset.h"
30 #include "errarg.h"
31 #include "error.h"
32 #include "lib.h"
33 #include "nonposix.h"
34 #include "posix.h"
35 #include "refid.h"
36 #include "search.h"
38 static void usage(FILE *stream)
40 fprintf(stream,
41 "Synopsis: %s [-v] [-i XYZ] [-t N] database ...\n",
42 program_name);
45 int main(int argc, char **argv)
47 program_name = argv[0];
48 static char stderr_buf[BUFSIZ];
49 setbuf(stderr, stderr_buf);
50 int opt;
51 static const struct option long_options[] = {
52 { "help", no_argument, 0, CHAR_MAX + 1 },
53 { "version", no_argument, 0, 'v' },
54 { NULL, 0, 0, 0 }
56 while ((opt = getopt_long(argc, argv, "vVi:t:", long_options, NULL)) != EOF)
57 switch (opt) {
58 case 'V':
59 verify_flag = 1;
60 break;
61 case 'i':
62 linear_ignore_fields = optarg;
63 break;
64 case 't':
66 char *ptr;
67 long n = strtol(optarg, &ptr, 10);
68 if (n == 0 && ptr == optarg) {
69 error("bad integer `%1' in `t' option", optarg);
70 break;
72 if (n < 1)
73 n = 1;
74 linear_truncate_len = int(n);
75 break;
77 case 'v':
79 printf(L_LOOKBIB " (" T_ROFF ") v" VERSION);
80 exit(0);
81 break;
83 case CHAR_MAX + 1: // --help
84 usage(stdout);
85 exit(0);
86 break;
87 case '?':
88 usage(stderr);
89 exit(1);
90 break;
91 default:
92 assert(0);
94 if (optind >= argc) {
95 usage(stderr);
96 exit(1);
98 search_list list;
99 for (int i = optind; i < argc; i++)
100 list.add_file(argv[i]);
101 if (list.nfiles() == 0)
102 fatal("no databases");
103 char line[1024];
104 int interactive = isatty(fileno(stdin));
105 for (;;) {
106 if (interactive) {
107 fputs("> ", stderr);
108 fflush(stderr);
110 if (!fgets(line, sizeof(line), stdin))
111 break;
112 char *ptr = line;
113 while (csspace(*ptr))
114 ptr++;
115 if (*ptr == '\0')
116 continue;
117 search_list_iterator iter(&list, line);
118 const char *start;
119 int len;
120 int count;
121 for (count = 0; iter.next(&start, &len); count++) {
122 if (fwrite(start, 1, len, stdout) != (size_t)len)
123 fatal("write error on stdout: %1", strerror(errno));
124 // Can happen for last reference in file.
125 if (start[len - 1] != '\n')
126 putchar('\n');
127 putchar('\n');
129 fflush(stdout);
130 if (interactive) {
131 fprintf(stderr, "%d found\n", count);
132 fflush(stderr);
135 if (interactive)
136 putc('\n', stderr);
137 return 0;
140 // s-it2-mode