1 /* Create simple DB database from textual input.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000 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 #include "nss_db/dummy-db.h"
36 /* Get libc version number. */
37 #include "../version.h"
39 #define PACKAGE _libc_intl_domainname
41 /* If non-zero convert key to lower case. */
42 static int to_lowercase
;
44 /* If non-zero print content of input file, one entry per line. */
47 /* If non-zero do not print informational messages. */
50 /* Name of output file. */
51 static const char *output_name
;
53 /* Name and version of program. */
54 static void print_version (FILE *stream
, struct argp_state
*state
);
55 void (*argp_program_version_hook
) (FILE *, struct argp_state
*) = print_version
;
57 /* Definitions of arguments for argp functions. */
58 static const struct argp_option options
[] =
60 { "fold-case", 'f', NULL
, 0, N_("Convert key to lower case") },
61 { "output", 'o', N_("NAME"), 0, N_("Write output to file NAME") },
62 { "quiet", 'q', NULL
, 0,
63 N_("Do not print messages while building database") },
64 { "undo", 'u', NULL
, 0,
65 N_("Print content of database file, one entry a line") },
66 { NULL
, 0, NULL
, 0, NULL
}
69 /* Short description of program. */
70 static const char doc
[] = N_("Create simple DB database from textual input.");
72 /* Strings for arguments in help texts. */
73 static const char args_doc
[] = N_("\
74 INPUT-FILE OUTPUT-FILE\n-o OUTPUT-FILE INPUT-FILE\n-u INPUT-FILE");
76 /* Prototype for option handler. */
77 static error_t
parse_opt (int key
, char *arg
, struct argp_state
*state
);
79 /* Function to print some extra text in the help message. */
80 static char *more_help (int key
, const char *text
, void *input
);
82 /* Data structure to communicate with argp functions. */
83 static struct argp argp
=
85 options
, parse_opt
, args_doc
, doc
, NULL
, more_help
89 /* Prototypes for local functions. */
90 static int process_input (FILE *input
, const char *inname
, NSS_DB
*output
,
91 int to_lowercase
, int be_quiet
);
92 static int print_database (NSS_DB
*db
);
96 main (int argc
, char *argv
[])
98 const char *input_name
;
105 /* Set locale via LC_ALL. */
106 setlocale (LC_ALL
, "");
108 /* Set the text message domain. */
109 textdomain (_libc_intl_domainname
);
111 /* Initialize local variables. */
114 /* Parse and process arguments. */
115 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
117 /* Determine file names. */
118 if (do_undo
|| output_name
!= NULL
)
120 if (remaining
+ 1 != argc
)
123 error (0, 0, gettext ("wrong number of arguments"));
124 argp_help (&argp
, stdout
, ARGP_HELP_SEE
,
125 program_invocation_short_name
);
128 input_name
= argv
[remaining
];
132 if (remaining
+ 2 != argc
)
133 goto wrong_arguments
;
135 input_name
= argv
[remaining
++];
136 output_name
= argv
[remaining
];
139 /* First load the shared object to initialize version dependend
141 if (load_db () != NSS_STATUS_SUCCESS
)
142 error (EXIT_FAILURE
, 0, gettext ("No usable database library found."));
144 /* Special handling if we are asked to print the database. */
147 dbopen (input_name
, db_rdonly
, 0666, &db_file
);
149 error (EXIT_FAILURE
, 0, gettext ("cannot open database file `%s': %s"),
151 (errno
== EINVAL
? gettext ("incorrectly formatted file")
152 : strerror (errno
)));
154 status
= print_database (db_file
);
156 db_file
->close (db_file
->db
, 0);
161 /* Open input file. */
162 if (strcmp (input_name
, "-") == 0 || strcmp (input_name
, "/dev/stdin") == 0)
168 input_file
= fopen (input_name
, "r");
169 if (input_file
== NULL
)
170 error (EXIT_FAILURE
, errno
, gettext ("cannot open input file `%s'"),
173 /* Get the access rights from the source file. The output file should
175 if (fstat (fileno (input_file
), &st
) >= 0)
176 mode
= st
.st_mode
& ACCESSPERMS
;
179 /* Open output file. This must not be standard output so we don't
180 handle "-" and "/dev/stdout" special. */
181 dbopen (output_name
, DB_CREATE
| db_truncate
, mode
, &db_file
);
183 error (EXIT_FAILURE
, errno
, gettext ("cannot open output file `%s'"),
186 /* Start the real work. */
187 status
= process_input (input_file
, input_name
, db_file
, to_lowercase
,
191 if (input_file
!= stdin
)
193 db_file
->close (db_file
->db
, 0);
199 /* Handle program arguments. */
201 parse_opt (int key
, char *arg
, struct argp_state
*state
)
218 return ARGP_ERR_UNKNOWN
;
225 more_help (int key
, const char *text
, void *input
)
229 case ARGP_KEY_HELP_EXTRA
:
230 /* We print some extra information. */
231 return strdup (gettext ("\
232 Report bugs using the `glibcbug' script to <bugs@gnu.org>.\n"));
236 return (char *) text
;
239 /* Print the version information. */
241 print_version (FILE *stream
, struct argp_state
*state
)
243 fprintf (stream
, "makedb (GNU %s) %s\n", PACKAGE
, VERSION
);
244 fprintf (stream
, gettext ("\
245 Copyright (C) %s Free Software Foundation, Inc.\n\
246 This is free software; see the source for copying conditions. There is NO\n\
247 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
249 fprintf (stream
, gettext ("Written by %s.\n"), "Ulrich Drepper");
254 process_input (input
, inname
, output
, to_lowercase
, be_quiet
)
268 status
= EXIT_SUCCESS
;
271 while (!feof (input
))
278 n
= getline (&line
, &linelen
, input
);
280 /* This means end of file or some bug. */
283 /* Short read. Probably interrupted system call. */
288 if (line
[n
- 1] == '\n')
289 /* Remove trailing newline. */
293 while (isspace (*cp
))
297 /* First non-space character in line '#': it's a comment. */
301 while (*cp
!= '\0' && !isspace (*cp
))
309 /* It's an empty line. */
312 key
.size
= cp
- (char *) key
.data
;
315 while (isspace (*cp
))
319 val
.size
= (&line
[n
] - cp
) + 1;
322 /* Store the value. */
323 status
= output
->put (output
->db
, NULL
, &key
, &val
, db_nooverwrite
);
326 if (status
== db_keyexist
)
329 error_at_line (0, 0, inname
, linenr
,
330 gettext ("duplicate key"));
331 /* This is no real error. Just give a warning. */
336 error (0, status
, gettext ("while writing database file"));
338 status
= EXIT_FAILURE
;
347 error (0, 0, gettext ("problems while reading `%s'"), inname
);
348 status
= EXIT_FAILURE
;
364 status
= db
->cursor (db
->db
, NULL
, &cursor
);
367 error (0, status
, gettext ("while reading database"));
373 status
= cursor
->c_get (cursor
->cursor
, &key
, &val
, db_first
);
376 printf ("%.*s %s\n", (int) key
.size
, (char *) key
.data
,
379 status
= cursor
->c_get (cursor
->cursor
, &key
, &val
, db_next
);
382 if (status
!= db_notfound
)
384 error (0, status
, gettext ("while reading database"));