1 /* makedb -- create simple DB database from textual input.
2 Copyright (C) 1996, 1997, 1998 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. */
33 /* Get libc version number. */
34 #include "../version.h"
36 #define PACKAGE _libc_intl_domainname
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' },
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
));
55 int main
__P ((int argc
, char *argv
[]));
63 const char *output_name
;
64 const char *input_name
;
75 /* Set locale via LC_ALL. */
76 setlocale (LC_ALL
, "");
78 /* Set the text message domain. */
79 textdomain (_libc_intl_domainname
);
81 /* Initialize local variables. */
89 while ((opt
= getopt_long (argc
, argv
, "fho:uV", long_options
, NULL
)) != -1)
92 case '\0': /* Long option. */
101 output_name
= optarg
;
113 usage (EXIT_FAILURE
);
116 /* Version information is requested. */
119 printf ("makedb (GNU %s) %s\n", PACKAGE
, VERSION
);
121 Copyright (C) %s Free Software Foundation, Inc.\n\
122 This is free software; see the source for copying conditions. There is NO\n\
123 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
124 "), "1996, 1997, 1998");
125 printf (_("Written by %s.\n"), "Ulrich Drepper");
130 /* Help is requested. */
132 usage (EXIT_SUCCESS
);
136 /* Determine file names. */
137 if (do_undo
|| output_name
!= NULL
)
139 if (optind
+ 1 != argc
)
142 error (0, 0, gettext ("wrong number of arguments"));
143 usage (EXIT_FAILURE
);
145 input_name
= argv
[optind
];
149 if (optind
+ 2 != argc
)
150 goto wrong_arguments
;
152 input_name
= argv
[optind
++];
153 output_name
= argv
[optind
];
156 /* Special handling if we are asked to print the database. */
159 db_file
= dbopen (input_name
, O_RDONLY
, 0666, DB_BTREE
, NULL
);
161 error (EXIT_FAILURE
, 0, gettext ("cannot open database file `%s': %s"),
163 errno
== EFTYPE
? gettext ("incorrectly formatted file")
166 status
= print_database (db_file
);
168 db_file
->close (db_file
);
173 /* Open input file. */
174 if (strcmp (input_name
, "-") == 0 || strcmp (input_name
, "/dev/stdin") == 0)
178 input_file
= fopen (input_name
, "r");
179 if (input_file
== NULL
)
180 error (EXIT_FAILURE
, errno
, gettext ("cannot open input file `%s'"),
184 /* Open output file. This must not be standard output so we don't
185 handle "-" and "/dev/stdout" special. */
186 db_file
= dbopen (output_name
, O_CREAT
| O_RDWR
| O_TRUNC
, 0666,
189 error (EXIT_FAILURE
, errno
, gettext ("cannot open output file `%s'"),
192 /* Start the real work. */
193 status
= process_input (input_file
, input_name
, db_file
, to_lowercase
,
197 if (input_file
!= stdin
)
199 db_file
->close (db_file
);
209 if (status
!= EXIT_SUCCESS
)
210 fprintf (stderr
, gettext ("Try `%s --help' for more information.\n"),
211 program_invocation_name
);
215 Usage: %s [OPTION]... INPUT-FILE OUTPUT-FILE\n\
216 %s [OPTION]... -o OUTPUT-FILE INPUT-FILE\n\
217 %s [OPTION]... -u INPUT-FILE\n\
218 Mandatory arguments to long options are mandatory for short options too.\n\
219 -f, --fold-case convert key to lower case\n\
220 -h, --help display this help and exit\n\
221 -o, --output=NAME write output to file NAME\n\
222 --quiet don't print messages while building database\n\
223 -u, --undo print content of database file, one entry a line\n\
224 -V, --version output version information and exit\n\
225 If INPUT-FILE is -, input is read from standard input.\n"),
226 program_invocation_name
, program_invocation_name
,
227 program_invocation_name
);
229 Report bugs using the `glibcbug' script to <bugs@gnu.ai.mit.edu>.\n"),
238 process_input (input
, inname
, output
, to_lowercase
, be_quiet
)
252 status
= EXIT_SUCCESS
;
255 while (!feof (input
))
262 n
= getline (&line
, &linelen
, input
);
264 /* This means end of file or some bug. */
267 /* Short read. Probably interrupted system call. */
272 if (line
[n
- 1] == '\n')
273 /* Remove trailing newline. */
277 while (isspace (*cp
))
281 /* First non-space character in line '#': it's a comment. */
285 while (*cp
!= '\0' && !isspace (*cp
))
293 /* It's an empty line. */
296 key
.size
= cp
- (char *) key
.data
;
298 while (isspace (*cp
))
302 val
.size
= (&line
[n
] - cp
) + 1;
304 /* Store the value. */
305 status
= output
->put (output
, &key
, &val
, R_NOOVERWRITE
);
311 error_at_line (0, 0, inname
, linenr
,
312 gettext ("duplicate key"));
313 /* This is no real error. Just give a warning. */
318 error (0, errno
, gettext ("while writing database file"));
320 status
= EXIT_FAILURE
;
329 error (0, 0, gettext ("problems while reading `%s'"), inname
);
330 status
= EXIT_FAILURE
;
345 no_more
= db
->seq (db
, &key
, &val
, R_FIRST
);
348 printf ("%.*s %s\n", (int) key
.size
, (char *) key
.data
,
351 no_more
= db
->seq (db
, &key
, &val
, R_NEXT
);
356 error (0, errno
, gettext ("while reading database"));