Correction.
[glibc.git] / db / makedb.c
blobb1ed9822758fd4e743cb751b351bfd838321384b
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 <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
32 /* Get libc version number. */
33 #include "../version.h"
36 /* Long options. */
37 static const struct option long_options[] =
39 { "help", no_argument, NULL, 'h' },
40 { "fold-case", no_argument, NULL, 'f' },
41 { "output", required_argument, NULL, 'o' },
42 { "undo", no_argument, NULL, 'u' },
43 { "version", no_argument, NULL, 'V' },
44 { NULL, }
47 /* Prototypes for local functions. */
48 static void usage __P ((int status)) __attribute__ ((noreturn));
49 static int process_input __P ((FILE *input, const char *inname, DB *output,
50 int to_lowercase));
51 static int print_database __P ((DB *db));
54 int
55 main (argc, argv)
56 int argc;
57 char *argv[];
59 const char *output_name;
60 const char *input_name;
61 FILE *input_file;
62 DB *db_file;
63 int do_help;
64 int do_version;
65 int to_lowercase;
66 int do_undo;
67 int status;
68 int opt;
70 /* Set locale via LC_ALL. */
71 setlocale (LC_ALL, "");
73 /* Set the text message domain. */
74 textdomain (_libc_intl_domainname);
76 /* Initialize local variables. */
77 do_help = 0;
78 do_version = 0;
79 to_lowercase = 0;
80 do_undo = 0;
81 output_name = NULL;
83 while ((opt = getopt_long (argc, argv, "fho:uV", long_options, NULL)) != EOF)
84 switch (opt)
86 case '\0': /* Long option. */
87 break;
88 case 'h':
89 do_help = 1;
90 break;
91 case 'f':
92 to_lowercase = 1;
93 break;
94 case 'o':
95 output_name = optarg;
96 break;
97 case 'u':
98 do_undo = 1;
99 break;
100 case 'V':
101 do_version = 1;
102 break;
103 default:
104 usage (EXIT_FAILURE);
107 /* Version information is requested. */
108 if (do_version)
109 printf ("%s - GNU %s %s\n", program_invocation_name, "libc", VERSION);
111 /* Help is requested. */
112 if (do_help)
113 usage (EXIT_SUCCESS);
114 else if (do_version)
115 exit (EXIT_SUCCESS);
117 /* Determine file names. */
118 if (do_undo || output_name != NULL)
120 if (optind + 1 != argc)
122 wrong_arguments:
123 error (0, 0, gettext ("wrong number of arguments"));
124 usage (EXIT_FAILURE);
126 input_name = argv[optind];
128 else
130 if (optind + 2 != argc)
131 goto wrong_arguments;
133 input_name = argv[optind++];
134 output_name = argv[optind];
137 /* Special handling if we are asked to print the database. */
138 if (do_undo)
140 db_file = dbopen (input_name, O_RDONLY, 0666, DB_BTREE, NULL);
141 if (db_file == NULL)
142 error (EXIT_FAILURE, 0, gettext ("cannot open database file `%s': %s"),
143 input_name,
144 errno == EFTYPE ? gettext ("incorrectly formatted file")
145 : strerror (errno));
147 status = print_database (db_file);
149 db_file->close (db_file);
151 return status;
154 /* Open input file. */
155 if (strcmp (input_name, "-") == 0 || strcmp (input_name, "/dev/stdin") == 0)
156 input_file = stdin;
157 else
159 input_file = fopen (input_name, "r");
160 if (input_file == NULL)
161 error (EXIT_FAILURE, errno, gettext ("cannot open input file `%s'"),
162 input_name);
165 /* Open output file. This must not be standard output so we don't
166 handle "-" and "/dev/stdout" special. */
167 db_file = dbopen (output_name, O_CREAT | O_RDWR | O_TRUNC, 0666,
168 DB_BTREE, NULL);
169 if (db_file == NULL)
170 error (EXIT_FAILURE, errno, gettext ("cannot open output file `%s'"));
172 /* Start the real work. */
173 status = process_input (input_file, input_name, db_file, to_lowercase);
175 /* Close files. */
176 if (input_file != stdin)
177 fclose (input_file);
178 db_file->close (db_file);
180 return status;
184 static void
185 usage (status)
186 int status;
188 if (status != EXIT_SUCCESS)
189 fprintf (stderr, gettext ("Try `%s --help' for more information.\n"),
190 program_invocation_name);
191 else
192 printf (gettext ("\
193 Usage: %s [OPTION]... INPUT-FILE OUTPUT-FILE\n\
194 %s [OPTION]... -o OUTPUT-FILE INPUT-FILE\n\
195 %s [OPTION]... -u INPUT-FILE\n\
196 Mandatory arguments to long options are mandatory for short options too.\n\
197 -f, --fold-case convert key to lower case\n\
198 -h, --help display this help and exit\n\
199 -o, --output=NAME write output to file NAME\n\
200 -u, --undo print content of database file, one entry a line\n\
201 -V, --version output version information and exit\n\
202 If INPUT-FILE is -, input is read from standard input.\n"),
203 program_invocation_name, program_invocation_name,
204 program_invocation_name);
206 exit (status);
210 static int
211 process_input (input, inname, output, to_lowercase)
212 FILE *input;
213 const char *inname;
214 DB *output;
215 int to_lowercase;
217 char *line;
218 size_t linelen;
219 int status;
220 size_t linenr;
222 line = NULL;
223 linelen = 0;
224 status = EXIT_SUCCESS;
225 linenr = 0;
227 while (!feof (input))
229 DBT key;
230 DBT val;
231 char *cp;
232 int n;
234 n = getline (&line, &linelen, input);
235 if (n < 0)
236 /* This means end of file or some bug. */
237 break;
238 if (n == 0)
239 /* Short read. Probably interrupted system call. */
240 continue;
242 ++linenr;
244 if (line[n - 1] == '\n')
245 /* Remove trailing newline. */
246 line[--n] = '\0';
248 cp = line;
249 while (isspace (*cp))
250 ++cp;
252 if (*cp == '#')
253 /* First non-space character in line '#': it's a comment. */
254 continue;
256 key.data = cp;
257 while (*cp != '\0' && !isspace (*cp))
259 if (to_lowercase)
260 *cp = tolower (*cp);
261 ++cp;
264 if (key.data == cp)
265 /* It's an empty line. */
266 continue;
268 key.size = cp - (char *) key.data;
270 while (isspace (*cp))
271 ++cp;
273 val.data = cp;
274 val.size = &line[n] - cp;
276 /* Store the value. */
277 status = output->put (output, &key, &val, R_NOOVERWRITE);
278 if (status != 0)
280 if (status == 1)
281 error_at_line (0, 0, inname, linenr, gettext ("duplicate key"));
282 else
283 error (0, errno, gettext ("while writing data base file"));
285 status = EXIT_FAILURE;
286 clearerr (input);
287 break;
291 if (ferror (input))
293 error (0, 0, gettext ("problems while reading `%s'"));
294 status = EXIT_FAILURE;
297 return status;
301 static int
302 print_database (db)
303 DB *db;
305 DBT key;
306 DBT val;
307 int no_more;
309 no_more = db->seq (db, &key, &val, R_FIRST);
310 while (!no_more)
312 printf ("%.*s %.*s\n", (int) key.size, (char *) key.data, (int) val.size,
313 (char *) val.data);
315 no_more = db->seq (db, &key, &val, R_NEXT);
318 if (no_more == -1)
320 error (0, errno, gettext ("while reading database"));
321 return EXIT_FAILURE;
324 return EXIT_SUCCESS;