Update.
[glibc.git] / db2 / makedb.c
blob69a4029066352920dc42e00830d1e85c2fb13efd
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.
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_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>
33 /* Get libc version number. */
34 #include "../version.h"
36 #define PACKAGE _libc_intl_domainname
38 /* If non-zero convert key to lower case. */
39 static int to_lowercase;
41 /* If non-zero print content of input file, one entry per line. */
42 static int do_undo;
44 /* If non-zero do not print informational messages. */
45 static int be_quiet;
47 /* Name of output file. */
48 static const char *output_name;
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;
54 /* Definitions of arguments for argp functions. */
55 static const struct argp_option options[] =
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 }
66 /* Short description of program. */
67 static const char doc[] = N_("Create simple DB database from textual input.");
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");
73 /* Prototype for option handler. */
74 static error_t parse_opt __P ((int key, char *arg, struct argp_state *state));
76 /* Function to print some extra text in the help message. */
77 static char *more_help __P ((int key, const char *text, void *input));
79 /* Data structure to communicate with argp functions. */
80 static struct argp argp =
82 options, parse_opt, args_doc, doc, NULL, more_help
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[]));
93 int
94 main (argc, argv)
95 int argc;
96 char *argv[];
98 const char *input_name;
99 FILE *input_file;
100 DB *db_file;
101 int status;
102 int remaining;
104 /* Set locale via LC_ALL. */
105 setlocale (LC_ALL, "");
107 /* Set the text message domain. */
108 textdomain (_libc_intl_domainname);
110 /* Initialize local variables. */
111 input_name = NULL;
113 /* Parse and process arguments. */
114 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
116 /* Determine file names. */
117 if (do_undo || output_name != NULL)
119 if (remaining + 1 != argc)
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);
127 input_name = argv[remaining];
129 else
131 if (remaining + 2 != argc)
132 goto wrong_arguments;
134 input_name = argv[remaining++];
135 output_name = argv[remaining];
138 /* Special handling if we are asked to print the database. */
139 if (do_undo)
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));
148 status = print_database (db_file);
150 db_file->close (db_file);
152 return status;
155 /* Open input file. */
156 if (strcmp (input_name, "-") == 0 || strcmp (input_name, "/dev/stdin") == 0)
157 input_file = stdin;
158 else
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);
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)
171 error (EXIT_FAILURE, errno, gettext ("cannot open output file `%s'"),
172 output_name);
174 /* Start the real work. */
175 status = process_input (input_file, input_name, db_file, to_lowercase,
176 be_quiet);
178 /* Close files. */
179 if (input_file != stdin)
180 fclose (input_file);
181 db_file->close (db_file);
183 return status;
187 /* Handle program arguments. */
188 static error_t
189 parse_opt (int key, char *arg, struct argp_state *state)
191 switch (key)
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;
208 return 0;
212 static char *
213 more_help (int key, const char *text, void *input)
215 switch (key)
217 case ARGP_KEY_HELP_EXTRA:
218 /* We print some extra information. */
219 return strdup (gettext ("\
220 Report bugs using the `glibcbug' script to <bugs@gnu.ai.mit.edu>.\n"));
221 default:
222 break;
224 return (char *) text;
227 /* Print the version information. */
228 static void
229 print_version (FILE *stream, struct argp_state *state)
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");
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;
249 char *line;
250 size_t linelen;
251 int status;
252 size_t linenr;
254 line = NULL;
255 linelen = 0;
256 status = EXIT_SUCCESS;
257 linenr = 0;
259 while (!feof (input))
261 DBT key;
262 DBT val;
263 char *cp;
264 int n;
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;
274 ++linenr;
276 if (line[n - 1] == '\n')
277 /* Remove trailing newline. */
278 line[--n] = '\0';
280 cp = line;
281 while (isspace (*cp))
282 ++cp;
284 if (*cp == '#')
285 /* First non-space character in line '#': it's a comment. */
286 continue;
288 key.data = cp;
289 while (*cp != '\0' && !isspace (*cp))
291 if (to_lowercase)
292 *cp = tolower (*cp);
293 ++cp;
296 if (key.data == cp)
297 /* It's an empty line. */
298 continue;
300 key.size = cp - (char *) key.data;
302 while (isspace (*cp))
303 ++cp;
305 val.data = cp;
306 val.size = &line[n] - cp;
308 /* Store the value. */
309 status = output->put (output, &key, &val, R_NOOVERWRITE);
310 if (status != 0)
312 if (status == 1)
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;
320 else
321 error (0, errno, gettext ("while writing data base file"));
323 status = status ? EXIT_FAILURE : EXIT_SUCCESS;
325 clearerr (input);
326 break;
330 if (ferror (input))
332 error (0, 0, gettext ("problems while reading `%s'"), inname);
333 status = EXIT_FAILURE;
336 return status;
340 static int
341 print_database (db)
342 DB *db;
344 DBT key;
345 DBT val;
346 int no_more;
348 no_more = db->seq (db, &key, &val, R_FIRST);
349 while (!no_more)
351 printf ("%.*s %.*s\n", (int) key.size, (char *) key.data, (int) val.size,
352 (char *) val.data);
354 no_more = db->seq (db, &key, &val, R_NEXT);
357 if (no_more == -1)
359 error (0, errno, gettext ("while reading database"));
360 return EXIT_FAILURE;
363 return EXIT_SUCCESS;