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