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;
44 /* From mod_rewrite.c */
45 #ifndef REWRITE_MAX_TXT_MAP_LINE
46 #define REWRITE_MAX_TXT_MAP_LINE 1024
49 #define NL APR_EOL_STR
51 #define AVAIL "available"
52 #define UNAVAIL "unavailable"
54 static void usage(void)
56 const char *have_sdbm;
57 const char *have_gdbm;
58 const char *have_ndbm;
82 apr_file_printf(errfile,
83 "%s -- Program to Create DBM Files for use by RewriteMap" NL
84 "Usage: %s [-v] [-f format] -i SOURCE_TXT -o OUTPUT_DBM" NL
87 " -v More verbose output" NL
89 " -i Source Text File. If '-', use stdin." NL
93 " -f DBM Format. If not specified, will use the APR Default." NL
94 " GDBM for GDBM files (%s)" NL
95 " SDBM for SDBM files (%s)" NL
96 " DB for berkeley DB files (%s)" NL
97 " NDBM for NDBM files (%s)" NL
98 " default for the default DBM type" NL
109 static apr_status_t to_dbm(apr_dbm_t *dbm, apr_file_t *fp, apr_pool_t *pool)
111 apr_status_t rv = APR_SUCCESS;
112 char line[REWRITE_MAX_TXT_MAP_LINE + 1]; /* +1 for \0 */
117 apr_pool_create(&p, pool);
119 while (apr_file_gets(line, sizeof(line), fp) == APR_SUCCESS) {
122 if (*line == '#' || apr_isspace(*line)) {
128 while (*c && !apr_isspace(*c)) {
133 /* no value. solid line of data. */
137 dbmkey.dptr = apr_pstrmemdup(p, line, c - line);
138 dbmkey.dsize = (c - line);
140 while (apr_isspace(*c)) {
151 while (*c && !apr_isspace(*c)) {
155 dbmval.dptr = apr_pstrmemdup(p, value, c - value);
156 dbmval.dsize = (c - value);
159 apr_file_printf(errfile, " '%s' -> '%s'" NL,
160 dbmkey.dptr, dbmval.dptr);
163 rv = apr_dbm_store(dbm, dbmkey, dbmval);
167 if (rv != APR_SUCCESS) {
175 int main(int argc, const char *const argv[])
178 apr_status_t rv = APR_SUCCESS;
185 apr_app_initialize(&argc, &argv, NULL);
186 atexit(apr_terminate);
193 apr_pool_create(&pool, NULL);
196 shortname = apr_filepath_name_get(argv[0]);
199 shortname = "httxt2dbm";
202 apr_file_open_stderr(&errfile, pool);
203 rv = apr_getopt_init(&opt, pool, argc, argv);
205 if (rv != APR_SUCCESS) {
206 apr_file_printf(errfile, "Error: apr_getopt_init failed." NL NL);
215 while ((rv = apr_getopt(opt, "vf::i::o::", &ch, &opt_arg)) == APR_SUCCESS) {
219 apr_file_printf(errfile, "Error: -v can only be passed once" NL NL);
227 apr_file_printf(errfile, "Error: -f can only be passed once" NL NL);
231 format = apr_pstrdup(pool, opt_arg);
235 apr_file_printf(errfile, "Error: -i can only be passed once" NL NL);
239 input = apr_pstrdup(pool, opt_arg);
243 apr_file_printf(errfile, "Error: -o can only be passed once" NL NL);
247 output = apr_pstrdup(pool, opt_arg);
253 apr_file_printf(errfile, "Error: Parsing Arguments Failed" NL NL);
259 apr_file_printf(errfile, "Error: No input file specified." NL NL);
265 apr_file_printf(errfile, "Error: No output DBM specified." NL NL);
275 apr_file_printf(errfile, "DBM Format: %s" NL, format);
278 if (!strcmp(input, "-")) {
279 rv = apr_file_open_stdin(&infile, pool);
282 rv = apr_file_open(&infile, input, APR_READ|APR_BUFFERED,
283 APR_OS_DEFAULT, pool);
286 if (rv != APR_SUCCESS) {
287 apr_file_printf(errfile,
288 "Error: Cannot open input file '%s': (%d) %pm" NL NL,
294 apr_file_printf(errfile, "Input File: %s" NL, input);
297 rv = apr_dbm_open_ex(&outdbm, format, output, APR_DBM_RWCREATE,
298 APR_OS_DEFAULT, pool);
300 if (APR_STATUS_IS_ENOTIMPL(rv)) {
301 apr_file_printf(errfile,
302 "Error: The requested DBM Format '%s' is not available." NL NL,
307 if (rv != APR_SUCCESS) {
308 apr_file_printf(errfile,
309 "Error: Cannot open output DBM '%s': (%d) %pm" NL NL,
315 apr_file_printf(errfile, "DBM File: %s" NL, output);
318 rv = to_dbm(outdbm, infile, pool);
320 if (rv != APR_SUCCESS) {
321 apr_file_printf(errfile,
322 "Error: Converting to DBM: (%d) %pm" NL NL,
327 apr_dbm_close(outdbm);
330 apr_file_printf(errfile, "Conversion Complete." NL);