Update.
[glibc.git] / db2 / makedb.c
bloba7a085adba4a56cb37eb82c5aeda93674030108d
1 /* Create simple DB database from textual input.
2 Copyright (C) 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
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.
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.
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. */
21 #include <argp.h>
22 #include <ctype.h>
23 #include <db.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 #include <sys/stat.h>
34 /* Get libc version number. */
35 #include "../version.h"
37 #define PACKAGE _libc_intl_domainname
39 /* If non-zero convert key to lower case. */
40 static int to_lowercase;
42 /* If non-zero print content of input file, one entry per line. */
43 static int do_undo;
45 /* If non-zero do not print informational messages. */
46 static int be_quiet;
48 /* Name of output file. */
49 static const char *output_name;
51 /* Name and version of program. */
52 static void print_version (FILE *stream, struct argp_state *state);
53 void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
55 /* Definitions of arguments for argp functions. */
56 static const struct argp_option options[] =
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 }
67 /* Short description of program. */
68 static const char doc[] = N_("Create simple DB database from textual input.");
70 /* Strings for arguments in help texts. */
71 static const char args_doc[] = N_("\
72 INPUT-FILE OUTPUT-FILE\n-o OUTPUT-FILE INPUT-FILE\n-u INPUT-FILE");
74 /* Prototype for option handler. */
75 static error_t parse_opt __P ((int key, char *arg, struct argp_state *state));
77 /* Function to print some extra text in the help message. */
78 static char *more_help __P ((int key, const char *text, void *input));
80 /* Data structure to communicate with argp functions. */
81 static struct argp argp =
83 options, parse_opt, args_doc, doc, NULL, more_help
87 /* Prototypes for local functions. */
88 static int process_input __P ((FILE *input, const char *inname, DB *output,
89 int to_lowercase, int be_quiet));
90 static int print_database __P ((DB *db));
91 int main __P ((int argc, char *argv[]));
94 int
95 main (argc, argv)
96 int argc;
97 char *argv[];
99 const char *input_name;
100 FILE *input_file;
101 DB *db_file;
102 int status;
103 int remaining;
104 int mode = 0666;
106 /* Set locale via LC_ALL. */
107 setlocale (LC_ALL, "");
109 /* Set the text message domain. */
110 textdomain (_libc_intl_domainname);
112 /* Initialize local variables. */
113 input_name = NULL;
115 /* Parse and process arguments. */
116 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
118 /* Determine file names. */
119 if (do_undo || output_name != NULL)
121 if (remaining + 1 != argc)
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);
129 input_name = argv[remaining];
131 else
133 if (remaining + 2 != argc)
134 goto wrong_arguments;
136 input_name = argv[remaining++];
137 output_name = argv[remaining];
140 /* Special handling if we are asked to print the database. */
141 if (do_undo)
143 status = db_open (input_name, DB_BTREE, DB_RDONLY, 0666, NULL, NULL,
144 &db_file);
145 if (status != 0)
146 error (EXIT_FAILURE, 0, gettext ("cannot open database file `%s': %s"),
147 input_name,
148 (status == EINVAL ? gettext ("incorrectly formatted file")
149 : strerror (status)));
151 status = print_database (db_file);
153 db_file->close (db_file, 0);
155 return status;
158 /* Open input file. */
159 if (strcmp (input_name, "-") == 0 || strcmp (input_name, "/dev/stdin") == 0)
160 input_file = stdin;
161 else
163 struct stat st;
165 input_file = fopen (input_name, "r");
166 if (input_file == NULL)
167 error (EXIT_FAILURE, errno, gettext ("cannot open input file `%s'"),
168 input_name);
170 /* Get the access rights from the source file. The output file should
171 have the same. */
172 if (fstat (fileno (input_file), &st) >= 0)
173 mode = st.st_mode & ACCESSPERMS;
176 /* Open output file. This must not be standard output so we don't
177 handle "-" and "/dev/stdout" special. */
178 status = db_open (output_name, DB_BTREE, DB_CREATE | DB_TRUNCATE, mode,
179 NULL, NULL, &db_file);
180 if (status != 0)
181 error (EXIT_FAILURE, status, gettext ("cannot open output file `%s'"),
182 output_name);
184 /* Start the real work. */
185 status = process_input (input_file, input_name, db_file, to_lowercase,
186 be_quiet);
188 /* Close files. */
189 if (input_file != stdin)
190 fclose (input_file);
191 db_file->close (db_file, 0);
193 return status;
197 /* Handle program arguments. */
198 static error_t
199 parse_opt (int key, char *arg, struct argp_state *state)
201 switch (key)
203 case 'f':
204 to_lowercase = 1;
205 break;
206 case 'o':
207 output_name = arg;
208 break;
209 case 'q':
210 be_quiet = 1;
211 break;
212 case 'u':
213 do_undo = 1;
214 break;
215 default:
216 return ARGP_ERR_UNKNOWN;
218 return 0;
222 static char *
223 more_help (int key, const char *text, void *input)
225 switch (key)
227 case ARGP_KEY_HELP_EXTRA:
228 /* We print some extra information. */
229 return strdup (gettext ("\
230 Report bugs using the `glibcbug' script to <bugs@gnu.org>.\n"));
231 default:
232 break;
234 return (char *) text;
237 /* Print the version information. */
238 static void
239 print_version (FILE *stream, struct argp_state *state)
241 fprintf (stream, "makedb (GNU %s) %s\n", PACKAGE, VERSION);
242 fprintf (stream, gettext ("\
243 Copyright (C) %s Free Software Foundation, Inc.\n\
244 This is free software; see the source for copying conditions. There is NO\n\
245 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
246 "), "1999");
247 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
251 static int
252 process_input (input, inname, output, to_lowercase, be_quiet)
253 FILE *input;
254 const char *inname;
255 DB *output;
256 int to_lowercase;
257 int be_quiet;
259 char *line;
260 size_t linelen;
261 int status;
262 size_t linenr;
264 line = NULL;
265 linelen = 0;
266 status = EXIT_SUCCESS;
267 linenr = 0;
269 while (!feof (input))
271 DBT key;
272 DBT val;
273 char *cp;
274 int n;
276 n = getline (&line, &linelen, input);
277 if (n < 0)
278 /* This means end of file or some bug. */
279 break;
280 if (n == 0)
281 /* Short read. Probably interrupted system call. */
282 continue;
284 ++linenr;
286 if (line[n - 1] == '\n')
287 /* Remove trailing newline. */
288 line[--n] = '\0';
290 cp = line;
291 while (isspace (*cp))
292 ++cp;
294 if (*cp == '#')
295 /* First non-space character in line '#': it's a comment. */
296 continue;
298 key.data = cp;
299 while (*cp != '\0' && !isspace (*cp))
301 if (to_lowercase)
302 *cp = tolower (*cp);
303 ++cp;
306 if (key.data == cp)
307 /* It's an empty line. */
308 continue;
310 key.size = cp - (char *) key.data;
311 key.flags = 0;
313 while (isspace (*cp))
314 ++cp;
316 val.data = cp;
317 val.size = (&line[n] - cp) + 1;
318 val.flags = 0;
320 /* Store the value. */
321 status = output->put (output, NULL, &key, &val, DB_NOOVERWRITE);
322 if (status != 0)
324 if (status == DB_KEYEXIST)
326 if (!be_quiet)
327 error_at_line (0, 0, inname, linenr,
328 gettext ("duplicate key"));
329 /* This is no real error. Just give a warning. */
330 status = 0;
331 continue;
333 else
334 error (0, status, gettext ("while writing database file"));
336 status = EXIT_FAILURE;
338 clearerr (input);
339 break;
343 if (ferror (input))
345 error (0, 0, gettext ("problems while reading `%s'"), inname);
346 status = EXIT_FAILURE;
349 return status;
353 static int
354 print_database (db)
355 DB *db;
357 DBT key;
358 DBT val;
359 DBC *cursor;
360 int status;
362 status = db->cursor (db, NULL, &cursor);
363 if (status != 0)
365 error (0, status, gettext ("while reading database"));
366 return EXIT_FAILURE;
369 key.flags = 0;
370 val.flags = 0;
371 status = cursor->c_get (cursor, &key, &val, DB_FIRST);
372 while (status == 0)
374 printf ("%.*s %s\n", (int) key.size, (char *) key.data,
375 (char *) val.data);
377 status = cursor->c_get (cursor, &key, &val, DB_NEXT);
380 if (status != DB_NOTFOUND)
382 error (0, status, gettext ("while reading database"));
383 return EXIT_FAILURE;
386 return EXIT_SUCCESS;