1312591Sbapt/*	$Id: dba_read.c,v 1.4 2016/08/17 20:46:56 schwarze Exp $ */
2312591Sbapt/*
3312591Sbapt * Copyright (c) 2016 Ingo Schwarze <schwarze@openbsd.org>
4312591Sbapt *
5312591Sbapt * Permission to use, copy, modify, and distribute this software for any
6312591Sbapt * purpose with or without fee is hereby granted, provided that the above
7312591Sbapt * copyright notice and this permission notice appear in all copies.
8312591Sbapt *
9312591Sbapt * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10312591Sbapt * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11312591Sbapt * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12312591Sbapt * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13312591Sbapt * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14312591Sbapt * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15312591Sbapt * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16312591Sbapt *
17312591Sbapt * Function to read the mandoc database from disk into RAM,
18312591Sbapt * such that data can be added or removed.
19312591Sbapt * The interface is defined in "dba.h".
20312591Sbapt * This file is seperate from dba.c because this also uses "dbm.h".
21312591Sbapt */
22312591Sbapt#include <regex.h>
23312591Sbapt#include <stdint.h>
24312591Sbapt#include <stdlib.h>
25312591Sbapt#include <stdio.h>
26312591Sbapt#include <string.h>
27312591Sbapt
28312591Sbapt#include "mandoc_aux.h"
29312591Sbapt#include "mansearch.h"
30312591Sbapt#include "dba_array.h"
31312591Sbapt#include "dba.h"
32312591Sbapt#include "dbm.h"
33312591Sbapt
34312591Sbapt
35312591Sbaptstruct dba *
36312591Sbaptdba_read(const char *fname)
37312591Sbapt{
38312591Sbapt	struct dba		*dba;
39312591Sbapt	struct dba_array	*page;
40312591Sbapt	struct dbm_page		*pdata;
41312591Sbapt	struct dbm_macro	*mdata;
42312591Sbapt	const char		*cp;
43312591Sbapt	int32_t			 im, ip, iv, npages;
44312591Sbapt
45312591Sbapt	if (dbm_open(fname) == -1)
46312591Sbapt		return NULL;
47312591Sbapt	npages = dbm_page_count();
48312591Sbapt	dba = dba_new(npages < 128 ? 128 : npages);
49312591Sbapt	for (ip = 0; ip < npages; ip++) {
50312591Sbapt		pdata = dbm_page_get(ip);
51312591Sbapt		page = dba_page_new(dba->pages, pdata->arch,
52312591Sbapt		    pdata->desc, pdata->file + 1, *pdata->file);
53312591Sbapt		for (cp = pdata->name; *cp != '\0'; cp = strchr(cp, '\0') + 1)
54312591Sbapt			dba_page_add(page, DBP_NAME, cp);
55312591Sbapt		for (cp = pdata->sect; *cp != '\0'; cp = strchr(cp, '\0') + 1)
56312591Sbapt			dba_page_add(page, DBP_SECT, cp);
57312591Sbapt		if ((cp = pdata->arch) != NULL)
58312591Sbapt			while (*(cp = strchr(cp, '\0') + 1) != '\0')
59312591Sbapt				dba_page_add(page, DBP_ARCH, cp);
60312591Sbapt		cp = pdata->file;
61312591Sbapt		while (*(cp = strchr(cp, '\0') + 1) != '\0')
62312591Sbapt			dba_page_add(page, DBP_FILE, cp);
63312591Sbapt	}
64312591Sbapt	for (im = 0; im < MACRO_MAX; im++) {
65312591Sbapt		for (iv = 0; iv < dbm_macro_count(im); iv++) {
66312591Sbapt			mdata = dbm_macro_get(im, iv);
67312591Sbapt			dba_macro_new(dba, im, mdata->value, mdata->pp);
68312591Sbapt		}
69312591Sbapt	}
70312591Sbapt	dbm_close();
71312591Sbapt	return dba;
72312591Sbapt}
73