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. */
32 /* Get libc version number. */
33 #include "../version.h"
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 { "quiet", no_argument, NULL, 'q' },
43 { "undo", no_argument, NULL, 'u' },
44 { "version", no_argument, NULL, 'V' },
48 /* Prototypes for local functions. */
49 static void usage __P ((int status)) __attribute__ ((noreturn));
50 static int process_input __P ((FILE *input, const char *inname, DB *output,
51 int to_lowercase, int be_quiet));
52 static int print_database __P ((DB *db));
60 const char *output_name;
61 const char *input_name;
72 /* Set locale via LC_ALL. */
73 setlocale (LC_ALL, "");
75 /* Set the text message domain. */
76 textdomain (_libc_intl_domainname);
78 /* Initialize local variables. */
86 while ((opt = getopt_long (argc, argv, "fho:uV", long_options, NULL)) != EOF)
89 case '\0': /* Long option. */
110 usage (EXIT_FAILURE);
113 /* Version information is requested. */
115 printf ("%s - GNU %s %s\n", program_invocation_name, "libc", VERSION);
117 /* Help is requested. */
119 usage (EXIT_SUCCESS);
123 /* Determine file names. */
124 if (do_undo || output_name != NULL)
126 if (optind + 1 != argc)
129 error (0, 0, gettext ("wrong number of arguments"));
130 usage (EXIT_FAILURE);
132 input_name = argv[optind];
136 if (optind + 2 != argc)
137 goto wrong_arguments;
139 input_name = argv[optind++];
140 output_name = argv[optind];
143 /* Special handling if we are asked to print the database. */
146 db_file = dbopen (input_name, O_RDONLY, 0666, DB_BTREE, NULL);
148 error (EXIT_FAILURE, 0, gettext ("cannot open database file `%s': %s"),
150 errno == EFTYPE ? gettext ("incorrectly formatted file")
153 status = print_database (db_file);
155 db_file->close (db_file);
160 /* Open input file. */
161 if (strcmp (input_name, "-") == 0 || strcmp (input_name, "/dev/stdin") == 0)
165 input_file = fopen (input_name, "r");
166 if (input_file == NULL)
167 error (EXIT_FAILURE, errno, gettext ("cannot open input file `%s'"),
171 /* Open output file. This must not be standard output so we don't
172 handle "-" and "/dev/stdout" special. */
173 db_file = dbopen (output_name, O_CREAT | O_RDWR | O_TRUNC, 0666,
176 error (EXIT_FAILURE, errno, gettext ("cannot open output file `%s'"));
178 /* Start the real work. */
179 status = process_input (input_file, input_name, db_file, to_lowercase,
183 if (input_file != stdin)
185 db_file->close (db_file);
195 if (status != EXIT_SUCCESS)
196 fprintf (stderr, gettext ("Try `%s --help' for more information.\n"),
197 program_invocation_name);
200 Usage: %s [OPTION]... INPUT-FILE OUTPUT-FILE\n\
201 %s [OPTION]... -o OUTPUT-FILE INPUT-FILE\n\
202 %s [OPTION]... -u INPUT-FILE\n\
203 Mandatory arguments to long options are mandatory for short options too.\n\
204 -f, --fold-case convert key to lower case\n\
205 -h, --help display this help and exit\n\
206 -o, --output=NAME write output to file NAME\n\
207 --quiet don't print messages while building database\n\
208 -u, --undo print content of database file, one entry a line\n\
209 -V, --version output version information and exit\n\
210 If INPUT-FILE is -, input is read from standard input.\n\
211 Report bugs to <bug-glibc@gnu.ai.mit.edu>.\n"),
212 program_invocation_name, program_invocation_name,
213 program_invocation_name);
220 process_input (input, inname, output, to_lowercase, be_quiet)
234 status = EXIT_SUCCESS;
237 while (!feof (input))
244 n = getline (&line, &linelen, input);
246 /* This means end of file or some bug. */
249 /* Short read. Probably interrupted system call. */
254 if (line[n - 1] == '\n')
255 /* Remove trailing newline. */
259 while (isspace (*cp))
263 /* First non-space character in line '#': it's a comment. */
267 while (*cp != '\0' && !isspace (*cp))
275 /* It's an empty line. */
278 key.size = cp - (char *) key.data;
280 while (isspace (*cp))
284 val.size = &line[n] - cp;
286 /* Store the value. */
287 status = output->put (output, &key, &val, R_NOOVERWRITE);
293 error_at_line (0, 0, inname, linenr,
294 gettext ("duplicate key"));
295 /* This is no real error. Just give a warning. */
299 error (0, errno, gettext ("while writing data base file"));
301 status = status ? EXIT_FAILURE : EXIT_SUCCESS;
310 error (0, 0, gettext ("problems while reading `%s'"));
311 status = EXIT_FAILURE;
326 no_more = db->seq (db, &key, &val, R_FIRST);
329 printf ("%.*s %.*s\n", (int) key.size, (char *) key.data, (int) val.size,
332 no_more = db->seq (db, &key, &val, R_NEXT);
337 error (0, errno, gettext ("while reading database"));