1 /* 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. */
34 /* Get libc version number. */
35 #include "../version.h"
37 #define PACKAGE _libc_intl_domainname
39 /* If non-zero convert key to lower case. */
40 static int to_lowercase
;
42 /* If non-zero print content of input file, one entry per line. */
45 /* If non-zero do not print informational messages. */
48 /* Name of output file. */
49 static const char *output_name
;
51 /* Name and version of program. */
52 static void print_version (FILE *stream
, struct argp_state
*state
);
53 void (*argp_program_version_hook
) (FILE *, struct argp_state
*) = print_version
;
55 /* Definitions of arguments for argp functions. */
56 static const struct argp_option options
[] =
58 { "fold-case", 'f', NULL
, 0, N_("Convert key to lower case") },
59 { "output", 'o', N_("NAME"), 0, N_("Write output to file NAME") },
60 { "quiet", 'q', NULL
, 0,
61 N_("Do not print messages while building database") },
62 { "undo", 'u', NULL
, 0,
63 N_("Print content of database file, one entry a line") },
64 { NULL
, 0, NULL
, 0, NULL
}
67 /* Short description of program. */
68 static const char doc
[] = N_("Create simple DB database from textual input.");
70 /* Strings for arguments in help texts. */
71 static const char args_doc
[] = N_("\
72 INPUT-FILE OUTPUT-FILE\n-o OUTPUT-FILE INPUT-FILE\n-u INPUT-FILE");
74 /* Prototype for option handler. */
75 static error_t parse_opt
__P ((int key
, char *arg
, struct argp_state
*state
));
77 /* Function to print some extra text in the help message. */
78 static char *more_help
__P ((int key
, const char *text
, void *input
));
80 /* Data structure to communicate with argp functions. */
81 static struct argp argp
=
83 options
, parse_opt
, args_doc
, doc
, NULL
, more_help
87 /* Prototypes for local functions. */
88 static int process_input
__P ((FILE *input
, const char *inname
, DB
*output
,
89 int to_lowercase
, int be_quiet
));
90 static int print_database
__P ((DB
*db
));
91 int main
__P ((int argc
, char *argv
[]));
99 const char *input_name
;
106 /* Set locale via LC_ALL. */
107 setlocale (LC_ALL
, "");
109 /* Set the text message domain. */
110 textdomain (_libc_intl_domainname
);
112 /* Initialize local variables. */
115 /* Parse and process arguments. */
116 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
118 /* Determine file names. */
119 if (do_undo
|| output_name
!= NULL
)
121 if (remaining
+ 1 != argc
)
124 error (0, 0, gettext ("wrong number of arguments"));
125 argp_help (&argp
, stdout
, ARGP_HELP_SEE
,
126 program_invocation_short_name
);
129 input_name
= argv
[remaining
];
133 if (remaining
+ 2 != argc
)
134 goto wrong_arguments
;
136 input_name
= argv
[remaining
++];
137 output_name
= argv
[remaining
];
140 /* Special handling if we are asked to print the database. */
143 status
= db_open (input_name
, DB_BTREE
, DB_RDONLY
, 0666, NULL
, NULL
,
146 error (EXIT_FAILURE
, 0, gettext ("cannot open database file `%s': %s"),
148 (status
== EINVAL
? gettext ("incorrectly formatted file")
149 : strerror (status
)));
151 status
= print_database (db_file
);
153 db_file
->close (db_file
, 0);
158 /* Open input file. */
159 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'"),
170 /* Get the access rights from the source file. The output file should
172 if (fstat (fileno (input_file
), &st
) >= 0)
173 mode
= st
.st_mode
& ACCESSPERMS
;
176 /* Open output file. This must not be standard output so we don't
177 handle "-" and "/dev/stdout" special. */
178 status
= db_open (output_name
, DB_BTREE
, DB_CREATE
| DB_TRUNCATE
, mode
,
179 NULL
, NULL
, &db_file
);
181 error (EXIT_FAILURE
, status
, gettext ("cannot open output file `%s'"),
184 /* Start the real work. */
185 status
= process_input (input_file
, input_name
, db_file
, to_lowercase
,
189 if (input_file
!= stdin
)
191 db_file
->close (db_file
, 0);
197 /* Handle program arguments. */
199 parse_opt (int key
, char *arg
, struct argp_state
*state
)
216 return ARGP_ERR_UNKNOWN
;
223 more_help (int key
, const char *text
, void *input
)
227 case ARGP_KEY_HELP_EXTRA
:
228 /* We print some extra information. */
229 return strdup (gettext ("\
230 Report bugs using the `glibcbug' script to <bugs@gnu.org>.\n"));
234 return (char *) text
;
237 /* Print the version information. */
239 print_version (FILE *stream
, struct argp_state
*state
)
241 fprintf (stream
, "makedb (GNU %s) %s\n", PACKAGE
, VERSION
);
242 fprintf (stream
, gettext ("\
243 Copyright (C) %s Free Software Foundation, Inc.\n\
244 This is free software; see the source for copying conditions. There is NO\n\
245 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
246 "), "1996, 1997, 1998");
247 fprintf (stream
, gettext ("Written by %s.\n"), "Ulrich Drepper");
252 process_input (input
, inname
, output
, to_lowercase
, be_quiet
)
266 status
= EXIT_SUCCESS
;
269 while (!feof (input
))
276 n
= getline (&line
, &linelen
, input
);
278 /* This means end of file or some bug. */
281 /* Short read. Probably interrupted system call. */
286 if (line
[n
- 1] == '\n')
287 /* Remove trailing newline. */
291 while (isspace (*cp
))
295 /* First non-space character in line '#': it's a comment. */
299 while (*cp
!= '\0' && !isspace (*cp
))
307 /* It's an empty line. */
310 key
.size
= cp
- (char *) key
.data
;
313 while (isspace (*cp
))
317 val
.size
= (&line
[n
] - cp
) + 1;
320 /* Store the value. */
321 status
= output
->put (output
, NULL
, &key
, &val
, DB_NOOVERWRITE
);
324 if (status
== DB_KEYEXIST
)
327 error_at_line (0, 0, inname
, linenr
,
328 gettext ("duplicate key"));
329 /* This is no real error. Just give a warning. */
334 error (0, status
, gettext ("while writing database file"));
336 status
= EXIT_FAILURE
;
345 error (0, 0, gettext ("problems while reading `%s'"), inname
);
346 status
= EXIT_FAILURE
;
362 status
= db
->cursor (db
, NULL
, &cursor
);
365 error (0, status
, gettext ("while reading database"));
371 status
= cursor
->c_get (cursor
, &key
, &val
, DB_FIRST
);
374 printf ("%.*s %s\n", (int) key
.size
, (char *) key
.data
,
377 status
= cursor
->c_get (cursor
, &key
, &val
, DB_NEXT
);
380 if (status
!= DB_NOTFOUND
)
382 error (0, status
, gettext ("while reading database"));