]>
Commit | Line | Data |
---|---|---|
92f1da4d UD |
1 | /* Create simple DB database from textual input. |
2 | Copyright (C) 1996, 1997 Free Software Foundation, Inc. | |
3 | This file is part of the GNU C Library. | |
4 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996. | |
5 | ||
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. | |
10 | ||
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. | |
15 | ||
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. */ | |
20 | ||
21 | #include <argp.h> | |
22 | #include <ctype.h> | |
23 | #include <db_185.h> | |
24 | #include <errno.h> | |
25 | #include <error.h> | |
26 | #include <fcntl.h> | |
27 | #include <libintl.h> | |
28 | #include <locale.h> | |
29 | #include <stdio.h> | |
30 | #include <stdlib.h> | |
31 | #include <string.h> | |
32 | ||
33 | /* Get libc version number. */ | |
34 | #include "../version.h" | |
35 | ||
36 | #define PACKAGE _libc_intl_domainname | |
37 | ||
38 | /* If non-zero convert key to lower case. */ | |
39 | static int to_lowercase; | |
40 | ||
41 | /* If non-zero print content of input file, one entry per line. */ | |
42 | static int do_undo; | |
43 | ||
44 | /* If non-zero do not print informational messages. */ | |
45 | static int be_quiet; | |
46 | ||
47 | /* Name of output file. */ | |
48 | static const char *output_name; | |
49 | ||
50 | /* Name and version of program. */ | |
51 | static void print_version (FILE *stream, struct argp_state *state); | |
52 | void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version; | |
53 | ||
54 | /* Definitions of arguments for argp functions. */ | |
55 | static const struct argp_option options[] = | |
56 | { | |
57 | { "fold-case", 'f', NULL, 0, N_("Convert key to lower case") }, | |
58 | { "output", 'o', N_("NAME"), 0, N_("Write output to file NAME") }, | |
59 | { "quiet", 'q', NULL, 0, | |
60 | N_("Do not print messages while building database") }, | |
61 | { "undo", 'u', NULL, 0, | |
62 | N_("Print content of database file, one entry a line") }, | |
63 | { NULL, 0, NULL, 0, NULL } | |
64 | }; | |
65 | ||
66 | /* Short description of program. */ | |
67 | static const char doc[] = N_("Create simple DB database from textual input."); | |
68 | ||
69 | /* Strings for arguments in help texts. */ | |
70 | static const char args_doc[] = N_("\ | |
71 | INPUT-FILE OUTPUT-FILE\n-o OUTPUT-FILE INPUT-FILE\n-u INPUT-FILE"); | |
72 | ||
73 | /* Prototype for option handler. */ | |
74 | static error_t parse_opt __P ((int key, char *arg, struct argp_state *state)); | |
75 | ||
76 | /* Function to print some extra text in the help message. */ | |
77 | static char *more_help __P ((int key, const char *text, void *input)); | |
78 | ||
79 | /* Data structure to communicate with argp functions. */ | |
80 | static struct argp argp = | |
81 | { | |
82 | options, parse_opt, args_doc, doc, NULL, more_help | |
83 | }; | |
84 | ||
85 | ||
86 | /* Prototypes for local functions. */ | |
87 | static int process_input __P ((FILE *input, const char *inname, DB *output, | |
88 | int to_lowercase, int be_quiet)); | |
89 | static int print_database __P ((DB *db)); | |
90 | int main __P ((int argc, char *argv[])); | |
91 | ||
92 | ||
93 | int | |
94 | main (argc, argv) | |
95 | int argc; | |
96 | char *argv[]; | |
97 | { | |
98 | const char *input_name; | |
99 | FILE *input_file; | |
100 | DB *db_file; | |
101 | int status; | |
102 | int remaining; | |
103 | ||
104 | /* Set locale via LC_ALL. */ | |
105 | setlocale (LC_ALL, ""); | |
106 | ||
107 | /* Set the text message domain. */ | |
108 | textdomain (_libc_intl_domainname); | |
109 | ||
110 | /* Initialize local variables. */ | |
111 | input_name = NULL; | |
112 | ||
113 | /* Parse and process arguments. */ | |
114 | argp_parse (&argp, argc, argv, 0, &remaining, NULL); | |
115 | ||
116 | /* Determine file names. */ | |
117 | if (do_undo || output_name != NULL) | |
118 | { | |
119 | if (remaining + 1 != argc) | |
120 | { | |
121 | wrong_arguments: | |
122 | error (0, 0, gettext ("wrong number of arguments")); | |
123 | argp_help (&argp, stdout, ARGP_HELP_SEE, | |
124 | program_invocation_short_name); | |
125 | exit (1); | |
126 | } | |
127 | input_name = argv[remaining]; | |
128 | } | |
129 | else | |
130 | { | |
131 | if (remaining + 2 != argc) | |
132 | goto wrong_arguments; | |
133 | ||
134 | input_name = argv[remaining++]; | |
135 | output_name = argv[remaining]; | |
136 | } | |
137 | ||
138 | /* Special handling if we are asked to print the database. */ | |
139 | if (do_undo) | |
140 | { | |
141 | db_file = dbopen (input_name, O_RDONLY, 0666, DB_BTREE, NULL); | |
142 | if (db_file == NULL) | |
143 | error (EXIT_FAILURE, 0, gettext ("cannot open database file `%s': %s"), | |
144 | input_name, | |
145 | errno == EINVAL ? gettext ("incorrectly formatted file") | |
146 | : strerror (errno)); | |
147 | ||
148 | status = print_database (db_file); | |
149 | ||
150 | db_file->close (db_file); | |
151 | ||
152 | return status; | |
153 | } | |
154 | ||
155 | /* Open input file. */ | |
156 | if (strcmp (input_name, "-") == 0 || strcmp (input_name, "/dev/stdin") == 0) | |
157 | input_file = stdin; | |
158 | else | |
159 | { | |
160 | input_file = fopen (input_name, "r"); | |
161 | if (input_file == NULL) | |
162 | error (EXIT_FAILURE, errno, gettext ("cannot open input file `%s'"), | |
163 | input_name); | |
164 | } | |
165 | ||
166 | /* Open output file. This must not be standard output so we don't | |
167 | handle "-" and "/dev/stdout" special. */ | |
168 | db_file = dbopen (output_name, O_CREAT | O_RDWR | O_TRUNC, 0666, | |
169 | DB_BTREE, NULL); | |
170 | if (db_file == NULL) | |
dfd2257a UD |
171 | error (EXIT_FAILURE, errno, gettext ("cannot open output file `%s'"), |
172 | output_name); | |
92f1da4d UD |
173 | |
174 | /* Start the real work. */ | |
175 | status = process_input (input_file, input_name, db_file, to_lowercase, | |
176 | be_quiet); | |
177 | ||
178 | /* Close files. */ | |
179 | if (input_file != stdin) | |
180 | fclose (input_file); | |
181 | db_file->close (db_file); | |
182 | ||
183 | return status; | |
184 | } | |
185 | ||
186 | ||
187 | /* Handle program arguments. */ | |
188 | static error_t | |
189 | parse_opt (int key, char *arg, struct argp_state *state) | |
190 | { | |
191 | switch (key) | |
192 | { | |
193 | case 'f': | |
194 | to_lowercase = 1; | |
195 | break; | |
196 | case 'o': | |
197 | output_name = arg; | |
198 | break; | |
199 | case 'q': | |
200 | be_quiet = 1; | |
201 | break; | |
202 | case 'u': | |
203 | do_undo = 1; | |
204 | break; | |
205 | default: | |
206 | return ARGP_ERR_UNKNOWN; | |
207 | } | |
208 | return 0; | |
209 | } | |
210 | ||
211 | ||
212 | static char * | |
213 | more_help (int key, const char *text, void *input) | |
214 | { | |
215 | switch (key) | |
216 | { | |
217 | case ARGP_KEY_HELP_EXTRA: | |
218 | /* We print some extra information. */ | |
219 | return strdup (gettext ("\ | |
f2ea0f5b | 220 | Report bugs using the `glibcbug' script to <bugs@gnu.org>.\n")); |
92f1da4d UD |
221 | default: |
222 | break; | |
223 | } | |
224 | return (char *) text; | |
225 | } | |
226 | ||
227 | /* Print the version information. */ | |
228 | static void | |
229 | print_version (FILE *stream, struct argp_state *state) | |
230 | { | |
231 | fprintf (stream, "makedb (GNU %s) %s\n", PACKAGE, VERSION); | |
232 | fprintf (stream, gettext ("\ | |
233 | Copyright (C) %s Free Software Foundation, Inc.\n\ | |
234 | This is free software; see the source for copying conditions. There is NO\n\ | |
235 | warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\ | |
236 | "), "1996, 1997"); | |
237 | fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper"); | |
238 | } | |
239 | ||
240 | ||
241 | static int | |
242 | process_input (input, inname, output, to_lowercase, be_quiet) | |
243 | FILE *input; | |
244 | const char *inname; | |
245 | DB *output; | |
246 | int to_lowercase; | |
247 | int be_quiet; | |
248 | { | |
249 | char *line; | |
250 | size_t linelen; | |
251 | int status; | |
252 | size_t linenr; | |
253 | ||
254 | line = NULL; | |
255 | linelen = 0; | |
256 | status = EXIT_SUCCESS; | |
257 | linenr = 0; | |
258 | ||
259 | while (!feof (input)) | |
260 | { | |
261 | DBT key; | |
262 | DBT val; | |
263 | char *cp; | |
264 | int n; | |
265 | ||
266 | n = getline (&line, &linelen, input); | |
267 | if (n < 0) | |
268 | /* This means end of file or some bug. */ | |
269 | break; | |
270 | if (n == 0) | |
271 | /* Short read. Probably interrupted system call. */ | |
272 | continue; | |
273 | ||
274 | ++linenr; | |
275 | ||
276 | if (line[n - 1] == '\n') | |
277 | /* Remove trailing newline. */ | |
278 | line[--n] = '\0'; | |
279 | ||
280 | cp = line; | |
281 | while (isspace (*cp)) | |
282 | ++cp; | |
283 | ||
284 | if (*cp == '#') | |
285 | /* First non-space character in line '#': it's a comment. */ | |
286 | continue; | |
287 | ||
288 | key.data = cp; | |
289 | while (*cp != '\0' && !isspace (*cp)) | |
290 | { | |
291 | if (to_lowercase) | |
292 | *cp = tolower (*cp); | |
293 | ++cp; | |
294 | } | |
295 | ||
296 | if (key.data == cp) | |
297 | /* It's an empty line. */ | |
298 | continue; | |
299 | ||
300 | key.size = cp - (char *) key.data; | |
301 | ||
302 | while (isspace (*cp)) | |
303 | ++cp; | |
304 | ||
305 | val.data = cp; | |
306 | val.size = &line[n] - cp; | |
307 | ||
308 | /* Store the value. */ | |
309 | status = output->put (output, &key, &val, R_NOOVERWRITE); | |
310 | if (status != 0) | |
311 | { | |
312 | if (status == 1) | |
313 | { | |
314 | if (!be_quiet) | |
315 | error_at_line (0, 0, inname, linenr, | |
316 | gettext ("duplicate key")); | |
317 | /* This is no real error. Just give a warning. */ | |
318 | status = 0; | |
319 | } | |
320 | else | |
bd355af0 | 321 | error (0, errno, gettext ("while writing database file")); |
92f1da4d UD |
322 | |
323 | status = status ? EXIT_FAILURE : EXIT_SUCCESS; | |
324 | ||
325 | clearerr (input); | |
326 | break; | |
327 | } | |
328 | } | |
329 | ||
330 | if (ferror (input)) | |
331 | { | |
dfd2257a | 332 | error (0, 0, gettext ("problems while reading `%s'"), inname); |
92f1da4d UD |
333 | status = EXIT_FAILURE; |
334 | } | |
335 | ||
336 | return status; | |
337 | } | |
338 | ||
339 | ||
340 | static int | |
341 | print_database (db) | |
342 | DB *db; | |
343 | { | |
344 | DBT key; | |
345 | DBT val; | |
346 | int no_more; | |
347 | ||
348 | no_more = db->seq (db, &key, &val, R_FIRST); | |
349 | while (!no_more) | |
350 | { | |
351 | printf ("%.*s %.*s\n", (int) key.size, (char *) key.data, (int) val.size, | |
352 | (char *) val.data); | |
353 | ||
354 | no_more = db->seq (db, &key, &val, R_NEXT); | |
355 | } | |
356 | ||
357 | if (no_more == -1) | |
358 | { | |
359 | error (0, errno, gettext ("while reading database")); | |
360 | return EXIT_FAILURE; | |
361 | } | |
362 | ||
363 | return EXIT_SUCCESS; | |
364 | } |