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. */
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. */
44 /* If non-zero do not print informational messages. */
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
[]));
98 const char *input_name
;
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. */
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
)
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
];
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. */
140 db_file
= dbopen (input_name
, O_RDONLY
, 0666, DB_BTREE
, NULL
);
142 error (EXIT_FAILURE
, 0, gettext ("cannot open database file `%s': %s"),
144 errno
== EFTYPE
? gettext ("incorrectly formatted file")
147 status
= print_database (db_file
);
149 db_file
->close (db_file
);
154 /* Open input file. */
155 if (strcmp (input_name
, "-") == 0 || strcmp (input_name
, "/dev/stdin") == 0)
159 input_file
= fopen (input_name
, "r");
160 if (input_file
== NULL
)
161 error (EXIT_FAILURE
, errno
, gettext ("cannot open input file `%s'"),
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,
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
,
177 if (input_file
!= stdin
)
179 db_file
->close (db_file
);
185 /* Handle program arguments. */
187 parse_opt (int key
, char *arg
, struct argp_state
*state
)
204 return ARGP_ERR_UNKNOWN
;
211 more_help (int key
, const char *text
, void *input
)
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"));
222 return (char *) text
;
225 /* Print the version information. */
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\
235 fprintf (stream
, gettext ("Written by %s.\n"), "Ulrich Drepper");
240 process_input (input
, inname
, output
, to_lowercase
, be_quiet
)
254 status
= EXIT_SUCCESS
;
257 while (!feof (input
))
264 n
= getline (&line
, &linelen
, input
);
266 /* This means end of file or some bug. */
269 /* Short read. Probably interrupted system call. */
274 if (line
[n
- 1] == '\n')
275 /* Remove trailing newline. */
279 while (isspace (*cp
))
283 /* First non-space character in line '#': it's a comment. */
287 while (*cp
!= '\0' && !isspace (*cp
))
295 /* It's an empty line. */
298 key
.size
= cp
- (char *) key
.data
;
300 while (isspace (*cp
))
304 val
.size
= &line
[n
] - cp
;
306 /* Store the value. */
307 status
= output
->put (output
, &key
, &val
, R_NOOVERWRITE
);
313 error_at_line (0, 0, inname
, linenr
,
314 gettext ("duplicate key"));
315 /* This is no real error. Just give a warning. */
319 error (0, errno
, gettext ("while writing data base file"));
321 status
= status
? EXIT_FAILURE
: EXIT_SUCCESS
;
330 error (0, 0, gettext ("problems while reading `%s'"));
331 status
= EXIT_FAILURE
;
346 no_more
= db
->seq (db
, &key
, &val
, R_FIRST
);
349 printf ("%.*s %.*s\n", (int) key
.size
, (char *) key
.data
, (int) val
.size
,
352 no_more
= db
->seq (db
, &key
, &val
, R_NEXT
);
357 error (0, errno
, gettext ("while reading database"));