1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * httxt2dbm.c: simple program for converting RewriteMap text files to DBM
19 * Rewrite databases for the Apache HTTP server
25 #include "apr_strings.h"
26 #include "apr_file_io.h"
27 #include "apr_file_info.h"
28 #include "apr_pools.h"
29 #include "apr_getopt.h"
34 #include <stdlib.h> /* for atexit() */
37 static const char *input
;
38 static const char *output
;
39 static const char *format
;
40 static const char *shortname
;
41 static apr_file_t
*errfile
;
42 static char errbuf
[120];
45 /* From mod_rewrite.c */
46 #ifndef REWRITE_MAX_TXT_MAP_LINE
47 #define REWRITE_MAX_TXT_MAP_LINE 1024
50 #define NL APR_EOL_STR
52 #define AVAIL "available"
53 #define UNAVAIL "unavailable"
55 static void usage(void)
57 const char *have_sdbm
;
58 const char *have_gdbm
;
59 const char *have_ndbm
;
83 apr_file_printf(errfile
,
84 "%s -- Program to Create DBM Files for use by RewriteMap" NL
85 "Usage: %s [-v] [-f format] -i SOURCE_TXT -o OUTPUT_DBM" NL
88 " -v More verbose output"NL
90 " -i Source Text File. If '-', use stdin."NL
94 " -f DBM Format. If not specified, will use the APR Default." NL
95 " GDBM for GDBM files (%s)" NL
96 " SDBM for SDBM files (%s)" NL
97 " DB for berkeley DB files (%s)" NL
98 " NDBM for NDBM files (%s)" NL
99 " default for the default DBM type" NL
110 static apr_status_t
to_dbm(apr_dbm_t
*dbm
, apr_file_t
*fp
, apr_pool_t
*pool
)
112 apr_status_t rv
= APR_SUCCESS
;
113 char line
[REWRITE_MAX_TXT_MAP_LINE
+ 1]; /* +1 for \0 */
118 apr_pool_create(&p
, pool
);
120 while (apr_file_gets(line
, sizeof(line
), fp
) == APR_SUCCESS
) {
123 if (*line
== '#' || apr_isspace(*line
)) {
129 while (*c
&& !apr_isspace(*c
)) {
134 /* no value. solid line of data. */
138 dbmkey
.dptr
= apr_pstrmemdup(p
, line
, c
- line
);
139 dbmkey
.dsize
= (c
- line
);
141 while (*c
&& apr_isspace(*c
)) {
152 while (*c
&& !apr_isspace(*c
)) {
156 dbmval
.dptr
= apr_pstrmemdup(p
, value
, c
- value
);
157 dbmval
.dsize
= (c
- line
);
160 apr_file_printf(errfile
, " '%s' -> '%s'"NL
,
161 dbmkey
.dptr
, dbmval
.dptr
);
164 rv
= apr_dbm_store(dbm
, dbmkey
, dbmval
);
168 if (rv
!= APR_SUCCESS
) {
176 int main(int argc
, const char *const argv
[])
179 apr_status_t rv
= APR_SUCCESS
;
186 apr_app_initialize(&argc
, &argv
, NULL
);
187 atexit(apr_terminate
);
194 apr_pool_create(&pool
, NULL
);
197 shortname
= apr_filepath_name_get(argv
[0]);
200 shortname
= "httxt2dbm";
203 apr_file_open_stderr(&errfile
, pool
);
204 rv
= apr_getopt_init(&opt
, pool
, argc
, argv
);
206 if (rv
!= APR_SUCCESS
) {
207 apr_file_printf(errfile
, "Error: apr_getopt_init failed."NL NL
);
216 while ((rv
= apr_getopt(opt
, "vf::i::o::", &ch
, &optarg
)) == APR_SUCCESS
) {
220 apr_file_printf(errfile
, "Error: -v can only be passed once" NL NL
);
228 apr_file_printf(errfile
, "Error: -f can only be passed once" NL NL
);
232 format
= apr_pstrdup(pool
, optarg
);
236 apr_file_printf(errfile
, "Error: -i can only be passed once" NL NL
);
240 input
= apr_pstrdup(pool
, optarg
);
244 apr_file_printf(errfile
, "Error: -o can only be passed once" NL NL
);
248 output
= apr_pstrdup(pool
, optarg
);
254 apr_file_printf(errfile
, "Error: Parsing Arguments Failed" NL NL
);
260 apr_file_printf(errfile
, "Error: No input file specified." NL NL
);
266 apr_file_printf(errfile
, "Error: No output DBM specified." NL NL
);
276 apr_file_printf(errfile
, "DBM Format: %s"NL
, format
);
279 if (!strcmp(input
, "-")) {
280 rv
= apr_file_open_stdin(&infile
, pool
);
283 rv
= apr_file_open(&infile
, input
, APR_READ
|APR_BUFFERED
,
284 APR_OS_DEFAULT
, pool
);
287 if (rv
!= APR_SUCCESS
) {
288 apr_file_printf(errfile
,
289 "Error: Cannot open input file '%s': (%d) %s" NL NL
,
290 input
, rv
, apr_strerror(rv
, errbuf
, sizeof(errbuf
)));
295 apr_file_printf(errfile
, "Input File: %s"NL
, input
);
298 rv
= apr_dbm_open_ex(&outdbm
, format
, output
, APR_DBM_RWCREATE
,
299 APR_OS_DEFAULT
, pool
);
301 if (APR_STATUS_IS_ENOTIMPL(rv
)) {
302 apr_file_printf(errfile
,
303 "Error: The requested DBM Format '%s' is not available." NL NL
,
308 if (rv
!= APR_SUCCESS
) {
309 apr_file_printf(errfile
,
310 "Error: Cannot open output DBM '%s': (%d) %s" NL NL
,
311 output
, rv
, apr_strerror(rv
, errbuf
, sizeof(errbuf
)));
316 apr_file_printf(errfile
, "DBM File: %s"NL
, output
);
319 rv
= to_dbm(outdbm
, infile
, pool
);
321 if (rv
!= APR_SUCCESS
) {
322 apr_file_printf(errfile
,
323 "Error: Converting to DBM: (%d) %s" NL NL
,
324 rv
, apr_strerror(rv
, errbuf
, sizeof(errbuf
)));
328 apr_dbm_close(outdbm
);
331 apr_file_printf(errfile
, "Conversion Complete." NL
);