Implement testing of inlined functions, make output nicer, update
[glibc/history.git] / db / makedb.c
blob32029fbbc621b0edea9e3406ecb0aa80b1e27803
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.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);
126 input_name = argv[remaining];
128 else
130 if (remaining + 2 != argc)
131 goto wrong_arguments;
133 input_name = argv[remaining++];
134 output_name = argv[remaining];
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,
174 be_quiet);
176 /* Close files. */
177 if (input_file != stdin)
178 fclose (input_file);
179 db_file->close (db_file);
181 return status;
185 /* Handle program arguments. */
186 static error_t
187 parse_opt (int key, char *arg, struct argp_state *state)
189 switch (key)
191 case 'f':
192 to_lowercase = 1;
193 break;
194 case 'o':
195 output_name = arg;
196 break;
197 case 'q':
198 be_quiet = 1;
199 break;
200 case 'u':
201 do_undo = 1;
202 break;
203 default:
204 return ARGP_ERR_UNKNOWN;
206 return 0;
210 static char *
211 more_help (int key, const char *text, void *input)
213 switch (key)
215 case ARGP_KEY_HELP_EXTRA:
216 /* We print some extra information. */
217 return strdup (gettext ("\
218 Report bugs using the `glibcbug' script to <bugs@gnu.ai.mit.edu>.\n"));
219 default:
220 break;
222 return (char *) text;
225 /* Print the version information. */
226 static void
227 print_version (FILE *stream, struct argp_state *state)
229 fprintf (stream, "makedb (GNU %s) %s\n", PACKAGE, VERSION);
230 fprintf (stream, gettext ("\
231 Copyright (C) %s Free Software Foundation, Inc.\n\
232 This is free software; see the source for copying conditions. There is NO\n\
233 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
234 "), "1996, 1997");
235 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
239 static int
240 process_input (input, inname, output, to_lowercase, be_quiet)
241 FILE *input;
242 const char *inname;
243 DB *output;
244 int to_lowercase;
245 int be_quiet;
247 char *line;
248 size_t linelen;
249 int status;
250 size_t linenr;
252 line = NULL;
253 linelen = 0;
254 status = EXIT_SUCCESS;
255 linenr = 0;
257 while (!feof (input))
259 DBT key;
260 DBT val;
261 char *cp;
262 int n;
264 n = getline (&line, &linelen, input);
265 if (n < 0)
266 /* This means end of file or some bug. */
267 break;
268 if (n == 0)
269 /* Short read. Probably interrupted system call. */
270 continue;
272 ++linenr;
274 if (line[n - 1] == '\n')
275 /* Remove trailing newline. */
276 line[--n] = '\0';
278 cp = line;
279 while (isspace (*cp))
280 ++cp;
282 if (*cp == '#')
283 /* First non-space character in line '#': it's a comment. */
284 continue;
286 key.data = cp;
287 while (*cp != '\0' && !isspace (*cp))
289 if (to_lowercase)
290 *cp = tolower (*cp);
291 ++cp;
294 if (key.data == cp)
295 /* It's an empty line. */
296 continue;
298 key.size = cp - (char *) key.data;
300 while (isspace (*cp))
301 ++cp;
303 val.data = cp;
304 val.size = &line[n] - cp;
306 /* Store the value. */
307 status = output->put (output, &key, &val, R_NOOVERWRITE);
308 if (status != 0)
310 if (status == 1)
312 if (!be_quiet)
313 error_at_line (0, 0, inname, linenr,
314 gettext ("duplicate key"));
315 /* This is no real error. Just give a warning. */
316 status = 0;
318 else
319 error (0, errno, gettext ("while writing data base file"));
321 status = status ? EXIT_FAILURE : EXIT_SUCCESS;
323 clearerr (input);
324 break;
328 if (ferror (input))
330 error (0, 0, gettext ("problems while reading `%s'"));
331 status = EXIT_FAILURE;
334 return status;
338 static int
339 print_database (db)
340 DB *db;
342 DBT key;
343 DBT val;
344 int no_more;
346 no_more = db->seq (db, &key, &val, R_FIRST);
347 while (!no_more)
349 printf ("%.*s %.*s\n", (int) key.size, (char *) key.data, (int) val.size,
350 (char *) val.data);
352 no_more = db->seq (db, &key, &val, R_NEXT);
355 if (no_more == -1)
357 error (0, errno, gettext ("while reading database"));
358 return EXIT_FAILURE;
361 return EXIT_SUCCESS;