Sync-to-go: update copyright for 2015
[s-roff.git] / src / ute-lkbib / lkbib.cpp
blobefbcf866b33076b60ea935d3688ced1b63162496
1 /*@
2 @ Copyright (c) 2014 - 2015 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
4 * Copyright (C) 1989 - 1992, 2000, 2001 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 "lkbib-config.h"
25 #include <assert.h>
26 #include <errno.h>
27 #include <stdlib.h>
29 #include "defs.h"
30 #include "errarg.h"
31 #include "error.h"
32 #include "lib.h"
33 #include "refid.h"
34 #include "search.h"
36 static void usage(FILE *stream)
38 fprintf(stream,
39 "Synopsis: %s [-nv] [-p database] [-i XYZ] [-t N] keys ...\n",
40 program_name);
43 int main(int argc, char **argv)
45 program_name = argv[0];
46 static char stderr_buf[BUFSIZ];
47 setbuf(stderr, stderr_buf);
48 int search_default = 1;
49 search_list list;
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, "nvVi:t:p:", long_options, NULL))
57 != EOF)
58 switch (opt) {
59 case 'V':
60 verify_flag = 1;
61 break;
62 case 'n':
63 search_default = 0;
64 break;
65 case 'i':
66 linear_ignore_fields = optarg;
67 break;
68 case 't':
70 char *ptr;
71 long n = strtol(optarg, &ptr, 10);
72 if (n == 0 && ptr == optarg) {
73 error("bad integer `%1' in `t' option", optarg);
74 break;
76 if (n < 1)
77 n = 1;
78 linear_truncate_len = int(n);
79 break;
81 case 'v':
83 printf(L_LKBIB " (" T_ROFF ") v" VERSION);
84 exit(0);
85 break;
87 case 'p':
88 list.add_file(optarg);
89 break;
90 case CHAR_MAX + 1: // --help
91 usage(stdout);
92 exit(0);
93 break;
94 case '?':
95 usage(stderr);
96 exit(1);
97 break;
98 default:
99 assert(0);
101 if (optind >= argc) {
102 usage(stderr);
103 exit(1);
105 char *filename = getenv("REFER");
106 if (filename)
107 list.add_file(filename);
108 else if (search_default)
109 list.add_file(DEFAULT_INDEX, 1);
110 if (list.nfiles() == 0)
111 fatal("no databases");
112 int total_len = 0;
113 int i;
114 for (i = optind; i < argc; i++)
115 total_len += strlen(argv[i]);
116 total_len += argc - optind - 1 + 1; // for spaces and '\0'
117 char *buffer = new char[total_len];
118 char *ptr = buffer;
119 for (i = optind; i < argc; i++) {
120 if (i > optind)
121 *ptr++ = ' ';
122 strcpy(ptr, argv[i]);
123 ptr = strchr(ptr, '\0');
125 search_list_iterator iter(&list, buffer);
126 const char *start;
127 int len;
128 int count;
129 for (count = 0; iter.next(&start, &len); count++) {
130 if (fwrite(start, 1, len, stdout) != (size_t)len)
131 fatal("write error on stdout: %1", strerror(errno));
132 // Can happen for last reference in file.
133 if (start[len - 1] != '\n')
134 putchar('\n');
135 putchar('\n');
137 return !count;
140 // s-it2-mode