Return-Path: Delivered-To: apmail-httpd-dev-archive@www.apache.org Received: (qmail 19361 invoked from network); 6 Jul 2005 21:51:01 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 6 Jul 2005 21:51:01 -0000 Received: (qmail 24587 invoked by uid 500); 6 Jul 2005 21:50:55 -0000 Delivered-To: apmail-httpd-dev-archive@httpd.apache.org Received: (qmail 24541 invoked by uid 500); 6 Jul 2005 21:50:55 -0000 Mailing-List: contact dev-help@httpd.apache.org; run by ezmlm Precedence: bulk Reply-To: dev@httpd.apache.org list-help: list-unsubscribe: List-Post: List-Id: Delivered-To: mailing list dev@httpd.apache.org Received: (qmail 24526 invoked by uid 99); 6 Jul 2005 21:50:54 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 06 Jul 2005 14:50:54 -0700 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests= X-Spam-Check-By: apache.org Received-SPF: pass (asf.osuosl.org: local policy) Received: from [216.255.199.145] (HELO utopia.in.force-elite.com) (216.255.199.145) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 06 Jul 2005 14:50:22 -0700 X-AuthUser: chip@force-elite.com Received: from [10.0.0.41] (10.0.0.41:4225) by utopia.in.force-elite.com with [XMail 1.17 (Linux/Ix86) ESMTP Server] id for from ; Wed, 06 Jul 2005 14:50:15 -0700 Message-ID: <42CC5218.8050105@force-elite.com> Date: Wed, 06 Jul 2005 14:50:16 -0700 From: Paul Querna User-Agent: Mozilla Thunderbird 1.0.2 (Windows/20050317) X-Accept-Language: en-us, en MIME-Version: 1.0 To: dev@httpd.apache.org Subject: new program: httxt2dbm Content-Type: multipart/mixed; boundary="------------020100040404070003080403" X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N This is a multi-part message in MIME format. --------------020100040404070003080403 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit I had to do some RewriteMaps this week, and I grew extremely warry of the txt2dbm perl program provided in the documentation. It had several big flaws, like using NDBM, which I didn't want to use. Attached is a patch adding the program that will convert a RewriteMap text format file into a DBM format. It allows you to specificy which format (GDBM, SDBM, DB, NDBM) or to just use the APR default. I will commit it to trunk tonight. -Paul --------------020100040404070003080403 Content-Type: text/plain; name="txt2dbm.patch" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="txt2dbm.patch" Index: Makefile.in =================================================================== --- Makefile.in (revision 209498) +++ Makefile.in (working copy) @@ -3,7 +3,7 @@ CLEAN_TARGETS = suexec -PROGRAMS = htpasswd htdigest rotatelogs logresolve ab checkgid htdbm htcacheclean +PROGRAMS = htpasswd htdigest rotatelogs logresolve ab checkgid htdbm htcacheclean httxt2dbm TARGETS = $(PROGRAMS) PROGRAM_LDADD = $(UTIL_LDFLAGS) $(PROGRAM_DEPENDENCIES) $(EXTRA_LIBS) $(AP_LIBS) @@ -65,3 +65,8 @@ htcacheclean_OBJECTS = htcacheclean.lo htcacheclean: $(htcacheclean_OBJECTS) $(LINK) $(htcacheclean_LTFLAGS) $(htcacheclean_OBJECTS) $(PROGRAM_LDADD) + +httxt2dbm_OBJECTS = httxt2dbm.lo +httxt2dbm: $(httxt2dbm_OBJECTS) + $(LINK) $(httxt2dbm_LTFLAGS) $(httxt2dbm_OBJECTS) $(PROGRAM_LDADD) + Index: httxt2dbm.c =================================================================== --- httxt2dbm.c (revision 0) +++ httxt2dbm.c (revision 0) @@ -0,0 +1,333 @@ +/* Copyright 2005 The Apache Software Foundation or its licensors, as + * applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * httxt2dbm.c: simple program for converting RewriteMap text files to DBM + * Rewrite databases for the Apache HTTP server + * + */ + +#include "apr.h" +#include "apr_lib.h" +#include "apr_strings.h" +#include "apr_file_io.h" +#include "apr_file_info.h" +#include "apr_pools.h" +#include "apr_getopt.h" +#include "apu.h" +#include "apr_dbm.h" + + +static const char *input; +static const char *output; +static const char *format; +static const char *shortname; +static apr_file_t *errfile; +static char errbuf[120]; +static int verbose; + +/* From mod_rewrite.c */ +#ifndef REWRITE_MAX_TXT_MAP_LINE +#define REWRITE_MAX_TXT_MAP_LINE 1024 +#endif + +#define NL APR_EOL_STR + +#define AVAIL "available" +#define UNAVAIL "unavailable" + +static void usage(void) +{ + const char* have_sdbm; + const char* have_gdbm; + const char* have_ndbm; + const char* have_db; + +#if APU_HAVE_SDBM + have_sdbm = AVAIL; +#else + have_sdbm = UNAVAIL; +#endif +#if APU_HAVE_GDBM + have_gdbm = AVAIL; +#else + have_gdbm = UNAVAIL; +#endif +#if APU_HAVE_NDBM + have_ndbm = AVAIL; +#else + have_ndbm = UNAVAIL; +#endif +#if APU_HAVE_DB + have_db = AVAIL; +#else + have_db = UNAVAIL; +#endif + + apr_file_printf(errfile, + "%s -- Program to Create DBM Files for use by RewriteMap" NL + "Usage: %s [-v] [-f format] -i SOURCE_TXT -o OUTPUT_DBM" NL + NL + "Options: " NL + " -v More verbose output"NL + NL + " -i Source Text File. If '-', use stdin."NL + NL + " -o Output DBM."NL + NL + " -f DBM Format. If not specified, will use the APR Default." NL + " GDBM for GDBM files (%s)" NL + " SDBM for SDBM files (%s)" NL + " DB for berkeley DB files (%s)" NL + " NDBM for NDBM files (%s)" NL + " default for the default DBM type" NL + NL, + shortname, + shortname, + have_gdbm, + have_sdbm, + have_db, + have_ndbm); +} + + +static apr_status_t to_dbm(apr_dbm_t *dbm, apr_file_t *fp, apr_pool_t *pool) +{ + apr_status_t rv = APR_SUCCESS; + char line[REWRITE_MAX_TXT_MAP_LINE + 1]; /* +1 for \0 */ + apr_datum_t dbmkey; + apr_datum_t dbmval; + apr_pool_t* p; + + apr_pool_create(&p, pool); + + while (apr_file_gets(line, sizeof(line), fp) == APR_SUCCESS) { + char *c, *value; + + if (*line == '#' || apr_isspace(*line)) { + continue; + } + + c = line; + + while (*c && !apr_isspace(*c)) { + ++c; + } + + if (!*c) { + /* no value. solid line of data. */ + continue; + } + + dbmkey.dptr = apr_pstrmemdup(p, line, c - line); + dbmkey.dsize = (c - line); + + while(*c && apr_isspace(*c)) { + ++c; + } + + if (!*c) { + apr_pool_clear(p); + continue; + } + + value = c; + + while(*c && !apr_isspace(*c)) { + ++c; + } + + dbmval.dptr = apr_pstrmemdup(p, value, c - value); + dbmval.dsize = (c - line); + + if (verbose) { + apr_file_printf(errfile, " '%s' -> '%s'"NL, + dbmkey.dptr, dbmval.dptr); + } + + rv = apr_dbm_store(dbm, dbmkey, dbmval); + + apr_pool_clear(p); + + if (rv != APR_SUCCESS) { + break; + } + } + + return rv; +} + +int main(int argc, const char* const argv[]) +{ + apr_pool_t *pool; + apr_status_t rv = APR_SUCCESS; + apr_getopt_t *opt; + const char *optarg; + char ch; + apr_file_t *infile; + apr_dbm_t *outdbm; + + apr_initialize(); + atexit(apr_terminate); + + verbose = 0; + format = NULL; + input = NULL; + output = NULL; + + apr_pool_create(&pool, NULL); + + if (argc) { + shortname = apr_filepath_name_get(argv[0]); + } + else { + shortname = "httxt2dbm"; + } + + apr_file_open_stderr(&errfile, pool); + rv = apr_getopt_init(&opt, pool, argc, argv); + + if (rv != APR_SUCCESS) { + apr_file_printf(errfile, "Error: apr_getopt_init failed."NL NL); + return 1; + } + + if (argc <= 1) { + usage(); + return 1; + } + + while ((rv = apr_getopt(opt, "vf::i::o::", &ch, &optarg)) == APR_SUCCESS) { + switch (ch) { + case 'v': + if (verbose) { + apr_file_printf(errfile, "Error: -v can only be passed once" NL NL); + usage(); + return 1; + } + verbose = 1; + break; + case 'f': + if (format) { + apr_file_printf(errfile, "Error: -f can only be passed once" NL NL); + usage(); + return 1; + } + format = apr_pstrdup(pool, optarg); + break; + case 'i': + if (input) { + apr_file_printf(errfile, "Error: -i can only be passed once" NL NL); + usage(); + return 1; + } + input = apr_pstrdup(pool, optarg); + break; + case 'o': + if (output) { + apr_file_printf(errfile, "Error: -o can only be passed once" NL NL); + usage(); + return 1; + } + output = apr_pstrdup(pool, optarg); + break; + } + } + + if (rv != APR_EOF) { + apr_file_printf(errfile, "Error: Parsing Arguments Failed" NL NL); + usage(); + return 1; + } + + if (!input) { + apr_file_printf(errfile, "Error: No input file specified." NL NL); + usage(); + return 1; + } + + if (!output) { + apr_file_printf(errfile, "Error: No output DBM specified." NL NL); + usage(); + return 1; + } + + if (!format) { + format = "default"; + } + + if (verbose) { + apr_file_printf(errfile, "DBM Format: %s"NL, format); + } + + if (!strcmp(input, "-")) { + rv = apr_file_open_stdin(&infile, pool); + } + else { + rv = apr_file_open(&infile, input, APR_READ|APR_BUFFERED, + APR_OS_DEFAULT, pool); + } + + if (rv != APR_SUCCESS) { + apr_file_printf(errfile, + "Error: Cannot open input file '%s': (%d) %s" NL NL, + input, rv, apr_strerror(rv, errbuf, sizeof(errbuf))); + return 1; + } + + if (verbose) { + apr_file_printf(errfile, "Input File: %s"NL, input); + } + + rv = apr_dbm_open_ex(&outdbm, format, output, APR_DBM_RWCREATE, + APR_OS_DEFAULT, pool); + + if (APR_STATUS_IS_ENOTIMPL(rv)) { + apr_file_printf(errfile, + "Error: The requested DBM Format '%s' is not available." NL NL, + format); + return 1; + } + + if (rv != APR_SUCCESS) { + apr_file_printf(errfile, + "Error: Cannot open output DBM '%s': (%d) %s" NL NL, + output, rv, apr_strerror(rv, errbuf, sizeof(errbuf))); + return 1; + } + + if (verbose) { + apr_file_printf(errfile, "DBM File: %s"NL, output); + } + + rv = to_dbm(outdbm, infile, pool); + + if (rv != APR_SUCCESS) { + apr_file_printf(errfile, + "Error: Converting to DBM: (%d) %s" NL NL, + rv, apr_strerror(rv, errbuf, sizeof(errbuf))); + return 1; + } + + apr_dbm_close(outdbm); + + if (verbose) { + apr_file_printf(errfile, "Conversion Complete." NL); + } + + return 0; +} + Property changes on: httxt2dbm.c ___________________________________________________________________ Name: svn:eol-style + native --------------020100040404070003080403--