1219019Sgabor/* $FreeBSD$ */ 2219019Sgabor/* $NetBSD: citrus_pivot_factory.c,v 1.7 2009/04/12 14:20:19 lukem Exp $ */ 3219019Sgabor 4219019Sgabor/*- 5219019Sgabor * Copyright (c)2003 Citrus Project, 6219019Sgabor * All rights reserved. 7219019Sgabor * 8219019Sgabor * Redistribution and use in source and binary forms, with or without 9219019Sgabor * modification, are permitted provided that the following conditions 10219019Sgabor * are met: 11219019Sgabor * 1. Redistributions of source code must retain the above copyright 12219019Sgabor * notice, this list of conditions and the following disclaimer. 13219019Sgabor * 2. Redistributions in binary form must reproduce the above copyright 14219019Sgabor * notice, this list of conditions and the following disclaimer in the 15219019Sgabor * documentation and/or other materials provided with the distribution. 16219019Sgabor * 17219019Sgabor * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18219019Sgabor * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19219019Sgabor * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20219019Sgabor * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21219019Sgabor * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22219019Sgabor * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23219019Sgabor * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24219019Sgabor * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25219019Sgabor * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26219019Sgabor * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27219019Sgabor * SUCH DAMAGE. 28219019Sgabor */ 29219019Sgabor 30219019Sgabor#include <sys/cdefs.h> 31219019Sgabor#include <sys/queue.h> 32219019Sgabor 33219019Sgabor#include <assert.h> 34219019Sgabor#include <ctype.h> 35219019Sgabor#include <errno.h> 36219019Sgabor#include <limits.h> 37219019Sgabor#include <stdio.h> 38219019Sgabor#include <stdlib.h> 39219019Sgabor#include <string.h> 40219019Sgabor 41219019Sgabor#include "citrus_namespace.h" 42219019Sgabor#include "citrus_region.h" 43219019Sgabor#include "citrus_bcs.h" 44219019Sgabor#include "citrus_db_factory.h" 45219019Sgabor#include "citrus_db_hash.h" 46219019Sgabor#include "citrus_pivot_file.h" 47219019Sgabor#include "citrus_pivot_factory.h" 48219019Sgabor 49219019Sgaborstruct src_entry { 50219019Sgabor char *se_name; 51219019Sgabor struct _citrus_db_factory *se_df; 52219019Sgabor STAILQ_ENTRY(src_entry) se_entry; 53219019Sgabor}; 54219019SgaborSTAILQ_HEAD(src_head, src_entry); 55219019Sgabor 56219019Sgaborstatic int 57219019Sgaborfind_src(struct src_head *sh, struct src_entry **rse, const char *name) 58219019Sgabor{ 59219019Sgabor int ret; 60219019Sgabor struct src_entry *se; 61219019Sgabor 62219019Sgabor STAILQ_FOREACH(se, sh, se_entry) { 63219019Sgabor if (_bcs_strcasecmp(se->se_name, name) == 0) { 64219019Sgabor *rse = se; 65219019Sgabor return (0); 66219019Sgabor } 67219019Sgabor } 68219019Sgabor se = malloc(sizeof(*se)); 69219019Sgabor if (se == NULL) 70219019Sgabor return (errno); 71219019Sgabor se->se_name = strdup(name); 72219019Sgabor if (se->se_name == NULL) { 73219019Sgabor ret = errno; 74219019Sgabor free(se); 75219019Sgabor return (ret); 76219019Sgabor } 77219019Sgabor ret = _db_factory_create(&se->se_df, &_db_hash_std, NULL); 78219019Sgabor if (ret) { 79219019Sgabor free(se->se_name); 80219019Sgabor free(se); 81219019Sgabor return (ret); 82219019Sgabor } 83219019Sgabor STAILQ_INSERT_TAIL(sh, se, se_entry); 84219019Sgabor *rse = se; 85219019Sgabor 86219019Sgabor return (0); 87219019Sgabor} 88219019Sgabor 89219019Sgaborstatic void 90219019Sgaborfree_src(struct src_head *sh) 91219019Sgabor{ 92219019Sgabor struct src_entry *se; 93219019Sgabor 94219019Sgabor while ((se = STAILQ_FIRST(sh)) != NULL) { 95219019Sgabor STAILQ_REMOVE_HEAD(sh, se_entry); 96219019Sgabor _db_factory_free(se->se_df); 97219019Sgabor free(se->se_name); 98219019Sgabor free(se); 99219019Sgabor } 100219019Sgabor} 101219019Sgabor 102219019Sgabor 103219019Sgabor#define T_COMM '#' 104219019Sgaborstatic int 105219019Sgaborconvert_line(struct src_head *sh, const char *line, size_t len) 106219019Sgabor{ 107219019Sgabor struct src_entry *se; 108219019Sgabor const char *p; 109219019Sgabor char key1[LINE_MAX], key2[LINE_MAX], data[LINE_MAX]; 110219019Sgabor char *ep; 111219019Sgabor uint32_t val; 112219019Sgabor int ret; 113219019Sgabor 114219019Sgabor se = NULL; 115219019Sgabor 116219019Sgabor /* cut off trailing comment */ 117219019Sgabor p = memchr(line, T_COMM, len); 118219019Sgabor if (p) 119219019Sgabor len = p - line; 120219019Sgabor 121219019Sgabor /* key1 */ 122219019Sgabor line = _bcs_skip_ws_len(line, &len); 123219019Sgabor if (len == 0) 124219019Sgabor return (0); 125219019Sgabor p = _bcs_skip_nonws_len(line, &len); 126219019Sgabor if (p == line) 127219019Sgabor return (0); 128219019Sgabor snprintf(key1, sizeof(key1), "%.*s", (int)(p - line), line); 129219019Sgabor 130219019Sgabor /* key2 */ 131219019Sgabor line = _bcs_skip_ws_len(p, &len); 132219019Sgabor if (len == 0) 133219019Sgabor return (0); 134219019Sgabor p = _bcs_skip_nonws_len(line, &len); 135219019Sgabor if (p == line) 136219019Sgabor return (0); 137219019Sgabor snprintf(key2, sizeof(key2), "%.*s", (int)(p - line), line); 138219019Sgabor 139219019Sgabor /* data */ 140219019Sgabor line = _bcs_skip_ws_len(p, &len); 141219019Sgabor _bcs_trunc_rws_len(line, &len); 142219019Sgabor snprintf(data, sizeof(data), "%.*s", (int)len, line); 143219019Sgabor val = strtoul(data, &ep, 0); 144219019Sgabor if (*ep != '\0') 145219019Sgabor return (EFTYPE); 146219019Sgabor 147219019Sgabor /* insert to DB */ 148219019Sgabor ret = find_src(sh, &se, key1); 149219019Sgabor if (ret) 150219019Sgabor return (ret); 151219019Sgabor 152219019Sgabor return (_db_factory_add32_by_s(se->se_df, key2, val)); 153219019Sgabor} 154219019Sgabor 155219019Sgaborstatic int 156219019Sgabordump_db(struct src_head *sh, struct _region *r) 157219019Sgabor{ 158219019Sgabor struct _db_factory *df; 159219019Sgabor struct src_entry *se; 160219019Sgabor struct _region subr; 161219019Sgabor void *ptr; 162219019Sgabor size_t size; 163219019Sgabor int ret; 164219019Sgabor 165219019Sgabor ret = _db_factory_create(&df, &_db_hash_std, NULL); 166219019Sgabor if (ret) 167219019Sgabor return (ret); 168219019Sgabor 169219019Sgabor STAILQ_FOREACH(se, sh, se_entry) { 170219019Sgabor size = _db_factory_calc_size(se->se_df); 171219019Sgabor ptr = malloc(size); 172219019Sgabor if (ptr == NULL) 173219019Sgabor goto quit; 174219019Sgabor _region_init(&subr, ptr, size); 175219019Sgabor ret = _db_factory_serialize(se->se_df, _CITRUS_PIVOT_SUB_MAGIC, 176219019Sgabor &subr); 177219019Sgabor if (ret) 178219019Sgabor goto quit; 179219019Sgabor ret = _db_factory_add_by_s(df, se->se_name, &subr, 1); 180219019Sgabor if (ret) 181219019Sgabor goto quit; 182219019Sgabor } 183219019Sgabor 184219019Sgabor size = _db_factory_calc_size(df); 185219019Sgabor ptr = malloc(size); 186219019Sgabor if (ptr == NULL) 187219019Sgabor goto quit; 188219019Sgabor _region_init(r, ptr, size); 189219019Sgabor 190219019Sgabor ret = _db_factory_serialize(df, _CITRUS_PIVOT_MAGIC, r); 191219019Sgabor ptr = NULL; 192219019Sgabor 193219019Sgaborquit: 194219019Sgabor free(ptr); 195219019Sgabor _db_factory_free(df); 196219019Sgabor return (ret); 197219019Sgabor} 198219019Sgabor 199219019Sgaborint 200219019Sgabor_citrus_pivot_factory_convert(FILE *out, FILE *in) 201219019Sgabor{ 202219019Sgabor struct src_head sh; 203219019Sgabor struct _region r; 204219019Sgabor char *line; 205219019Sgabor size_t size; 206219019Sgabor int ret; 207219019Sgabor 208219019Sgabor STAILQ_INIT(&sh); 209219019Sgabor 210219019Sgabor while ((line = fgetln(in, &size)) != NULL) 211219019Sgabor if ((ret = convert_line(&sh, line, size))) { 212219019Sgabor free_src(&sh); 213219019Sgabor return (ret); 214219019Sgabor } 215219019Sgabor 216219019Sgabor ret = dump_db(&sh, &r); 217219019Sgabor free_src(&sh); 218219019Sgabor if (ret) 219219019Sgabor return (ret); 220219019Sgabor 221219019Sgabor if (fwrite(_region_head(&r), _region_size(&r), 1, out) != 1) 222219019Sgabor return (errno); 223219019Sgabor 224219019Sgabor return (0); 225219019Sgabor} 226