]>
Commit | Line | Data |
---|---|---|
cddcfecf | 1 | /* makedb -- create simple DB database from textual input. |
df4ef2ab | 2 | Copyright (C) 1996, 1997 Free Software Foundation, Inc. |
afd4eb37 UD |
3 | This file is part of the GNU C Library. |
4 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996. | |
cddcfecf | 5 | |
afd4eb37 UD |
6 | The GNU C Library is free software; you can redistribute it and/or |
7 | modify it under the terms of the GNU Library General Public License as | |
8 | published by the Free Software Foundation; either version 2 of the | |
9 | License, or (at your option) any later version. | |
cddcfecf | 10 | |
afd4eb37 UD |
11 | The GNU C Library is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Library General Public License for more details. | |
cddcfecf | 15 | |
afd4eb37 UD |
16 | You should have received a copy of the GNU Library General Public |
17 | License along with the GNU C Library; see the file COPYING.LIB. If not, | |
18 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
19 | Boston, MA 02111-1307, USA. */ | |
cddcfecf RM |
20 | |
21 | #include <db.h> | |
22 | #include <ctype.h> | |
23 | #include <errno.h> | |
24 | #include <error.h> | |
25 | #include <fcntl.h> | |
26 | #include <getopt.h> | |
27 | #include <libintl.h> | |
da74e902 | 28 | #include <locale.h> |
cddcfecf RM |
29 | #include <stdio.h> |
30 | #include <stdlib.h> | |
31 | #include <string.h> | |
32 | ||
33 | /* Get libc version number. */ | |
34 | #include "../version.h" | |
35 | ||
8ca5c11b | 36 | #define PACKAGE _libc_intl_domainname |
cddcfecf RM |
37 | |
38 | /* Long options. */ | |
39 | static const struct option long_options[] = | |
40 | { | |
41 | { "help", no_argument, NULL, 'h' }, | |
42 | { "fold-case", no_argument, NULL, 'f' }, | |
43 | { "output", required_argument, NULL, 'o' }, | |
14bab8de | 44 | { "quiet", no_argument, NULL, 'q' }, |
cddcfecf RM |
45 | { "undo", no_argument, NULL, 'u' }, |
46 | { "version", no_argument, NULL, 'V' }, | |
14bab8de | 47 | { NULL, 0, NULL, 0} |
cddcfecf RM |
48 | }; |
49 | ||
50 | /* Prototypes for local functions. */ | |
51 | static void usage __P ((int status)) __attribute__ ((noreturn)); | |
52 | static int process_input __P ((FILE *input, const char *inname, DB *output, | |
14bab8de | 53 | int to_lowercase, int be_quiet)); |
cddcfecf | 54 | static int print_database __P ((DB *db)); |
11336c16 | 55 | int main __P ((int argc, char *argv[])); |
cddcfecf RM |
56 | |
57 | ||
58 | int | |
59 | main (argc, argv) | |
60 | int argc; | |
61 | char *argv[]; | |
62 | { | |
63 | const char *output_name; | |
64 | const char *input_name; | |
65 | FILE *input_file; | |
66 | DB *db_file; | |
67 | int do_help; | |
68 | int do_version; | |
69 | int to_lowercase; | |
70 | int do_undo; | |
14bab8de | 71 | int be_quiet; |
cddcfecf RM |
72 | int status; |
73 | int opt; | |
74 | ||
75 | /* Set locale via LC_ALL. */ | |
76 | setlocale (LC_ALL, ""); | |
77 | ||
78 | /* Set the text message domain. */ | |
79 | textdomain (_libc_intl_domainname); | |
80 | ||
81 | /* Initialize local variables. */ | |
82 | do_help = 0; | |
83 | do_version = 0; | |
84 | to_lowercase = 0; | |
85 | do_undo = 0; | |
14bab8de | 86 | be_quiet = 0; |
cddcfecf RM |
87 | output_name = NULL; |
88 | ||
1ef32c3d | 89 | while ((opt = getopt_long (argc, argv, "fho:uV", long_options, NULL)) != -1) |
cddcfecf RM |
90 | switch (opt) |
91 | { | |
92 | case '\0': /* Long option. */ | |
93 | break; | |
94 | case 'h': | |
95 | do_help = 1; | |
96 | break; | |
97 | case 'f': | |
98 | to_lowercase = 1; | |
99 | break; | |
100 | case 'o': | |
101 | output_name = optarg; | |
102 | break; | |
14bab8de UD |
103 | case 'q': |
104 | be_quiet = 1; | |
105 | break; | |
cddcfecf RM |
106 | case 'u': |
107 | do_undo = 1; | |
108 | break; | |
109 | case 'V': | |
110 | do_version = 1; | |
111 | break; | |
112 | default: | |
113 | usage (EXIT_FAILURE); | |
114 | } | |
115 | ||
116 | /* Version information is requested. */ | |
117 | if (do_version) | |
e320cbc9 | 118 | { |
a68b0d31 UD |
119 | printf ("makedb (GNU %s) %s\n", PACKAGE, VERSION); |
120 | printf (_("\ | |
e320cbc9 UD |
121 | Copyright (C) %s Free Software Foundation, Inc.\n\ |
122 | This is free software; see the source for copying conditions. There is NO\n\ | |
123 | warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\ | |
df4ef2ab | 124 | "), "1996, 1997"); |
a68b0d31 | 125 | printf (_("Written by %s.\n"), "Ulrich Drepper"); |
e320cbc9 UD |
126 | |
127 | exit (EXIT_SUCCESS); | |
128 | } | |
cddcfecf RM |
129 | |
130 | /* Help is requested. */ | |
131 | if (do_help) | |
132 | usage (EXIT_SUCCESS); | |
133 | else if (do_version) | |
134 | exit (EXIT_SUCCESS); | |
135 | ||
136 | /* Determine file names. */ | |
137 | if (do_undo || output_name != NULL) | |
138 | { | |
139 | if (optind + 1 != argc) | |
140 | { | |
141 | wrong_arguments: | |
142 | error (0, 0, gettext ("wrong number of arguments")); | |
143 | usage (EXIT_FAILURE); | |
144 | } | |
145 | input_name = argv[optind]; | |
146 | } | |
147 | else | |
148 | { | |
149 | if (optind + 2 != argc) | |
150 | goto wrong_arguments; | |
151 | ||
152 | input_name = argv[optind++]; | |
153 | output_name = argv[optind]; | |
154 | } | |
155 | ||
156 | /* Special handling if we are asked to print the database. */ | |
157 | if (do_undo) | |
158 | { | |
159 | db_file = dbopen (input_name, O_RDONLY, 0666, DB_BTREE, NULL); | |
160 | if (db_file == NULL) | |
161 | error (EXIT_FAILURE, 0, gettext ("cannot open database file `%s': %s"), | |
162 | input_name, | |
163 | errno == EFTYPE ? gettext ("incorrectly formatted file") | |
164 | : strerror (errno)); | |
165 | ||
166 | status = print_database (db_file); | |
167 | ||
168 | db_file->close (db_file); | |
169 | ||
170 | return status; | |
171 | } | |
172 | ||
173 | /* Open input file. */ | |
174 | if (strcmp (input_name, "-") == 0 || strcmp (input_name, "/dev/stdin") == 0) | |
175 | input_file = stdin; | |
176 | else | |
177 | { | |
178 | input_file = fopen (input_name, "r"); | |
179 | if (input_file == NULL) | |
180 | error (EXIT_FAILURE, errno, gettext ("cannot open input file `%s'"), | |
181 | input_name); | |
182 | } | |
183 | ||
184 | /* Open output file. This must not be standard output so we don't | |
185 | handle "-" and "/dev/stdout" special. */ | |
186 | db_file = dbopen (output_name, O_CREAT | O_RDWR | O_TRUNC, 0666, | |
187 | DB_BTREE, NULL); | |
188 | if (db_file == NULL) | |
189 | error (EXIT_FAILURE, errno, gettext ("cannot open output file `%s'")); | |
190 | ||
191 | /* Start the real work. */ | |
14bab8de UD |
192 | status = process_input (input_file, input_name, db_file, to_lowercase, |
193 | be_quiet); | |
cddcfecf RM |
194 | |
195 | /* Close files. */ | |
196 | if (input_file != stdin) | |
197 | fclose (input_file); | |
198 | db_file->close (db_file); | |
199 | ||
200 | return status; | |
201 | } | |
202 | ||
203 | ||
204 | static void | |
205 | usage (status) | |
206 | int status; | |
207 | { | |
208 | if (status != EXIT_SUCCESS) | |
209 | fprintf (stderr, gettext ("Try `%s --help' for more information.\n"), | |
210 | program_invocation_name); | |
211 | else | |
fafaa44e UD |
212 | { |
213 | printf (gettext ("\ | |
cddcfecf RM |
214 | Usage: %s [OPTION]... INPUT-FILE OUTPUT-FILE\n\ |
215 | %s [OPTION]... -o OUTPUT-FILE INPUT-FILE\n\ | |
216 | %s [OPTION]... -u INPUT-FILE\n\ | |
217 | Mandatory arguments to long options are mandatory for short options too.\n\ | |
218 | -f, --fold-case convert key to lower case\n\ | |
219 | -h, --help display this help and exit\n\ | |
220 | -o, --output=NAME write output to file NAME\n\ | |
14bab8de | 221 | --quiet don't print messages while building database\n\ |
cddcfecf RM |
222 | -u, --undo print content of database file, one entry a line\n\ |
223 | -V, --version output version information and exit\n\ | |
fafaa44e UD |
224 | If INPUT-FILE is -, input is read from standard input.\n"), |
225 | program_invocation_name, program_invocation_name, | |
226 | program_invocation_name); | |
df4ef2ab UD |
227 | fputs (gettext ("\ |
228 | Report bugs using the `glibcbug' script to <bugs@gnu.ai.mit.edu>.\n"), | |
c4029823 | 229 | stdout); |
fafaa44e | 230 | } |
cddcfecf RM |
231 | |
232 | exit (status); | |
233 | } | |
234 | ||
235 | ||
236 | static int | |
14bab8de | 237 | process_input (input, inname, output, to_lowercase, be_quiet) |
cddcfecf RM |
238 | FILE *input; |
239 | const char *inname; | |
240 | DB *output; | |
241 | int to_lowercase; | |
14bab8de | 242 | int be_quiet; |
cddcfecf RM |
243 | { |
244 | char *line; | |
245 | size_t linelen; | |
246 | int status; | |
247 | size_t linenr; | |
248 | ||
249 | line = NULL; | |
250 | linelen = 0; | |
251 | status = EXIT_SUCCESS; | |
252 | linenr = 0; | |
253 | ||
254 | while (!feof (input)) | |
255 | { | |
256 | DBT key; | |
257 | DBT val; | |
258 | char *cp; | |
259 | int n; | |
260 | ||
261 | n = getline (&line, &linelen, input); | |
262 | if (n < 0) | |
263 | /* This means end of file or some bug. */ | |
264 | break; | |
265 | if (n == 0) | |
266 | /* Short read. Probably interrupted system call. */ | |
267 | continue; | |
268 | ||
269 | ++linenr; | |
270 | ||
271 | if (line[n - 1] == '\n') | |
272 | /* Remove trailing newline. */ | |
273 | line[--n] = '\0'; | |
274 | ||
275 | cp = line; | |
276 | while (isspace (*cp)) | |
277 | ++cp; | |
278 | ||
279 | if (*cp == '#') | |
280 | /* First non-space character in line '#': it's a comment. */ | |
281 | continue; | |
282 | ||
283 | key.data = cp; | |
284 | while (*cp != '\0' && !isspace (*cp)) | |
285 | { | |
286 | if (to_lowercase) | |
287 | *cp = tolower (*cp); | |
288 | ++cp; | |
289 | } | |
290 | ||
291 | if (key.data == cp) | |
292 | /* It's an empty line. */ | |
293 | continue; | |
294 | ||
295 | key.size = cp - (char *) key.data; | |
296 | ||
297 | while (isspace (*cp)) | |
298 | ++cp; | |
299 | ||
300 | val.data = cp; | |
301 | val.size = &line[n] - cp; | |
302 | ||
303 | /* Store the value. */ | |
304 | status = output->put (output, &key, &val, R_NOOVERWRITE); | |
305 | if (status != 0) | |
306 | { | |
307 | if (status == 1) | |
14bab8de UD |
308 | { |
309 | if (!be_quiet) | |
310 | error_at_line (0, 0, inname, linenr, | |
311 | gettext ("duplicate key")); | |
312 | /* This is no real error. Just give a warning. */ | |
313 | status = 0; | |
314 | } | |
cddcfecf RM |
315 | else |
316 | error (0, errno, gettext ("while writing data base file")); | |
317 | ||
14bab8de UD |
318 | status = status ? EXIT_FAILURE : EXIT_SUCCESS; |
319 | ||
cddcfecf RM |
320 | clearerr (input); |
321 | break; | |
322 | } | |
323 | } | |
324 | ||
325 | if (ferror (input)) | |
326 | { | |
327 | error (0, 0, gettext ("problems while reading `%s'")); | |
328 | status = EXIT_FAILURE; | |
329 | } | |
330 | ||
331 | return status; | |
332 | } | |
333 | ||
334 | ||
335 | static int | |
336 | print_database (db) | |
337 | DB *db; | |
338 | { | |
339 | DBT key; | |
340 | DBT val; | |
341 | int no_more; | |
342 | ||
343 | no_more = db->seq (db, &key, &val, R_FIRST); | |
344 | while (!no_more) | |
345 | { | |
346 | printf ("%.*s %.*s\n", (int) key.size, (char *) key.data, (int) val.size, | |
347 | (char *) val.data); | |
348 | ||
349 | no_more = db->seq (db, &key, &val, R_NEXT); | |
350 | } | |
351 | ||
352 | if (no_more == -1) | |
353 | { | |
354 | error (0, errno, gettext ("while reading database")); | |
355 | return EXIT_FAILURE; | |
356 | } | |
357 | ||
358 | return EXIT_SUCCESS; | |
359 | } |