update from main archive 961001
[glibc.git] / db / makedb.c
blobd30ca2cef189164f34a4a629cbf38402c14f43b0
1 /* makedb -- create simple DB database from textual input.
2 Copyright (C) 1996 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
18 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
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>
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 /* Long options. */
39 static const struct option long_options[] =
41 { "help", no_argument, NULL, 'h' },
42 { "fold-case", no_argument, NULL, 'f' },
43 { "output", required_argument, NULL, 'o' },
44 { "quiet", no_argument, NULL, 'q' },
45 { "undo", no_argument, NULL, 'u' },
46 { "version", no_argument, NULL, 'V' },
47 { NULL, 0, NULL, 0}
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,
53 int to_lowercase, int be_quiet));
54 static int print_database __P ((DB *db));
57 int
58 main (argc, argv)
59 int argc;
60 char *argv[];
62 const char *output_name;
63 const char *input_name;
64 FILE *input_file;
65 DB *db_file;
66 int do_help;
67 int do_version;
68 int to_lowercase;
69 int do_undo;
70 int be_quiet;
71 int status;
72 int opt;
74 /* Set locale via LC_ALL. */
75 setlocale (LC_ALL, "");
77 /* Set the text message domain. */
78 textdomain (_libc_intl_domainname);
80 /* Initialize local variables. */
81 do_help = 0;
82 do_version = 0;
83 to_lowercase = 0;
84 do_undo = 0;
85 be_quiet = 0;
86 output_name = NULL;
88 while ((opt = getopt_long (argc, argv, "fho:uV", long_options, NULL)) != EOF)
89 switch (opt)
91 case '\0': /* Long option. */
92 break;
93 case 'h':
94 do_help = 1;
95 break;
96 case 'f':
97 to_lowercase = 1;
98 break;
99 case 'o':
100 output_name = optarg;
101 break;
102 case 'q':
103 be_quiet = 1;
104 break;
105 case 'u':
106 do_undo = 1;
107 break;
108 case 'V':
109 do_version = 1;
110 break;
111 default:
112 usage (EXIT_FAILURE);
115 /* Version information is requested. */
116 if (do_version)
118 printf ("makedb (GNU %s) %s\n", PACKAGE, VERSION);
119 printf (_("\
120 Copyright (C) %s Free Software Foundation, Inc.\n\
121 This is free software; see the source for copying conditions. There is NO\n\
122 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
123 "), "1996");
124 printf (_("Written by %s.\n"), "Ulrich Drepper");
126 exit (EXIT_SUCCESS);
129 /* Help is requested. */
130 if (do_help)
131 usage (EXIT_SUCCESS);
132 else if (do_version)
133 exit (EXIT_SUCCESS);
135 /* Determine file names. */
136 if (do_undo || output_name != NULL)
138 if (optind + 1 != argc)
140 wrong_arguments:
141 error (0, 0, gettext ("wrong number of arguments"));
142 usage (EXIT_FAILURE);
144 input_name = argv[optind];
146 else
148 if (optind + 2 != argc)
149 goto wrong_arguments;
151 input_name = argv[optind++];
152 output_name = argv[optind];
155 /* Special handling if we are asked to print the database. */
156 if (do_undo)
158 db_file = dbopen (input_name, O_RDONLY, 0666, DB_BTREE, NULL);
159 if (db_file == NULL)
160 error (EXIT_FAILURE, 0, gettext ("cannot open database file `%s': %s"),
161 input_name,
162 errno == EFTYPE ? gettext ("incorrectly formatted file")
163 : strerror (errno));
165 status = print_database (db_file);
167 db_file->close (db_file);
169 return status;
172 /* Open input file. */
173 if (strcmp (input_name, "-") == 0 || strcmp (input_name, "/dev/stdin") == 0)
174 input_file = stdin;
175 else
177 input_file = fopen (input_name, "r");
178 if (input_file == NULL)
179 error (EXIT_FAILURE, errno, gettext ("cannot open input file `%s'"),
180 input_name);
183 /* Open output file. This must not be standard output so we don't
184 handle "-" and "/dev/stdout" special. */
185 db_file = dbopen (output_name, O_CREAT | O_RDWR | O_TRUNC, 0666,
186 DB_BTREE, NULL);
187 if (db_file == NULL)
188 error (EXIT_FAILURE, errno, gettext ("cannot open output file `%s'"));
190 /* Start the real work. */
191 status = process_input (input_file, input_name, db_file, to_lowercase,
192 be_quiet);
194 /* Close files. */
195 if (input_file != stdin)
196 fclose (input_file);
197 db_file->close (db_file);
199 return status;
203 static void
204 usage (status)
205 int status;
207 if (status != EXIT_SUCCESS)
208 fprintf (stderr, gettext ("Try `%s --help' for more information.\n"),
209 program_invocation_name);
210 else
212 printf (gettext ("\
213 Usage: %s [OPTION]... INPUT-FILE OUTPUT-FILE\n\
214 %s [OPTION]... -o OUTPUT-FILE INPUT-FILE\n\
215 %s [OPTION]... -u INPUT-FILE\n\
216 Mandatory arguments to long options are mandatory for short options too.\n\
217 -f, --fold-case convert key to lower case\n\
218 -h, --help display this help and exit\n\
219 -o, --output=NAME write output to file NAME\n\
220 --quiet don't print messages while building database\n\
221 -u, --undo print content of database file, one entry a line\n\
222 -V, --version output version information and exit\n\
223 If INPUT-FILE is -, input is read from standard input.\n"),
224 program_invocation_name, program_invocation_name,
225 program_invocation_name);
226 fputs (gettext ("Report bugs to <bug-glibc@prep.ai.mit.edu>.\n"),
227 stdout);
230 exit (status);
234 static int
235 process_input (input, inname, output, to_lowercase, be_quiet)
236 FILE *input;
237 const char *inname;
238 DB *output;
239 int to_lowercase;
240 int be_quiet;
242 char *line;
243 size_t linelen;
244 int status;
245 size_t linenr;
247 line = NULL;
248 linelen = 0;
249 status = EXIT_SUCCESS;
250 linenr = 0;
252 while (!feof (input))
254 DBT key;
255 DBT val;
256 char *cp;
257 int n;
259 n = getline (&line, &linelen, input);
260 if (n < 0)
261 /* This means end of file or some bug. */
262 break;
263 if (n == 0)
264 /* Short read. Probably interrupted system call. */
265 continue;
267 ++linenr;
269 if (line[n - 1] == '\n')
270 /* Remove trailing newline. */
271 line[--n] = '\0';
273 cp = line;
274 while (isspace (*cp))
275 ++cp;
277 if (*cp == '#')
278 /* First non-space character in line '#': it's a comment. */
279 continue;
281 key.data = cp;
282 while (*cp != '\0' && !isspace (*cp))
284 if (to_lowercase)
285 *cp = tolower (*cp);
286 ++cp;
289 if (key.data == cp)
290 /* It's an empty line. */
291 continue;
293 key.size = cp - (char *) key.data;
295 while (isspace (*cp))
296 ++cp;
298 val.data = cp;
299 val.size = &line[n] - cp;
301 /* Store the value. */
302 status = output->put (output, &key, &val, R_NOOVERWRITE);
303 if (status != 0)
305 if (status == 1)
307 if (!be_quiet)
308 error_at_line (0, 0, inname, linenr,
309 gettext ("duplicate key"));
310 /* This is no real error. Just give a warning. */
311 status = 0;
313 else
314 error (0, errno, gettext ("while writing data base file"));
316 status = status ? EXIT_FAILURE : EXIT_SUCCESS;
318 clearerr (input);
319 break;
323 if (ferror (input))
325 error (0, 0, gettext ("problems while reading `%s'"));
326 status = EXIT_FAILURE;
329 return status;
333 static int
334 print_database (db)
335 DB *db;
337 DBT key;
338 DBT val;
339 int no_more;
341 no_more = db->seq (db, &key, &val, R_FIRST);
342 while (!no_more)
344 printf ("%.*s %.*s\n", (int) key.size, (char *) key.data, (int) val.size,
345 (char *) val.data);
347 no_more = db->seq (db, &key, &val, R_NEXT);
350 if (no_more == -1)
352 error (0, errno, gettext ("while reading database"));
353 return EXIT_FAILURE;
356 return EXIT_SUCCESS;