1*9508192eSchristos /* Id: dba_read.c,v 1.4 2016/08/17 20:46:56 schwarze Exp */
2*9508192eSchristos /*
3*9508192eSchristos * Copyright (c) 2016 Ingo Schwarze <schwarze@openbsd.org>
4*9508192eSchristos *
5*9508192eSchristos * Permission to use, copy, modify, and distribute this software for any
6*9508192eSchristos * purpose with or without fee is hereby granted, provided that the above
7*9508192eSchristos * copyright notice and this permission notice appear in all copies.
8*9508192eSchristos *
9*9508192eSchristos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10*9508192eSchristos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*9508192eSchristos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12*9508192eSchristos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*9508192eSchristos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*9508192eSchristos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15*9508192eSchristos * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16*9508192eSchristos *
17*9508192eSchristos * Function to read the mandoc database from disk into RAM,
18*9508192eSchristos * such that data can be added or removed.
19*9508192eSchristos * The interface is defined in "dba.h".
20*9508192eSchristos * This file is seperate from dba.c because this also uses "dbm.h".
21*9508192eSchristos */
22*9508192eSchristos #include <regex.h>
23*9508192eSchristos #include <stdint.h>
24*9508192eSchristos #include <stdlib.h>
25*9508192eSchristos #include <stdio.h>
26*9508192eSchristos #include <string.h>
27*9508192eSchristos
28*9508192eSchristos #include "mandoc_aux.h"
29*9508192eSchristos #include "mansearch.h"
30*9508192eSchristos #include "dba_array.h"
31*9508192eSchristos #include "dba.h"
32*9508192eSchristos #include "dbm.h"
33*9508192eSchristos
34*9508192eSchristos
35*9508192eSchristos struct dba *
dba_read(const char * fname)36*9508192eSchristos dba_read(const char *fname)
37*9508192eSchristos {
38*9508192eSchristos struct dba *dba;
39*9508192eSchristos struct dba_array *page;
40*9508192eSchristos struct dbm_page *pdata;
41*9508192eSchristos struct dbm_macro *mdata;
42*9508192eSchristos const char *cp;
43*9508192eSchristos int32_t im, ip, iv, npages;
44*9508192eSchristos
45*9508192eSchristos if (dbm_open(fname) == -1)
46*9508192eSchristos return NULL;
47*9508192eSchristos npages = dbm_page_count();
48*9508192eSchristos dba = dba_new(npages < 128 ? 128 : npages);
49*9508192eSchristos for (ip = 0; ip < npages; ip++) {
50*9508192eSchristos pdata = dbm_page_get(ip);
51*9508192eSchristos page = dba_page_new(dba->pages, pdata->arch,
52*9508192eSchristos pdata->desc, pdata->file + 1, *pdata->file);
53*9508192eSchristos for (cp = pdata->name; *cp != '\0'; cp = strchr(cp, '\0') + 1)
54*9508192eSchristos dba_page_add(page, DBP_NAME, cp);
55*9508192eSchristos for (cp = pdata->sect; *cp != '\0'; cp = strchr(cp, '\0') + 1)
56*9508192eSchristos dba_page_add(page, DBP_SECT, cp);
57*9508192eSchristos if ((cp = pdata->arch) != NULL)
58*9508192eSchristos while (*(cp = strchr(cp, '\0') + 1) != '\0')
59*9508192eSchristos dba_page_add(page, DBP_ARCH, cp);
60*9508192eSchristos cp = pdata->file;
61*9508192eSchristos while (*(cp = strchr(cp, '\0') + 1) != '\0')
62*9508192eSchristos dba_page_add(page, DBP_FILE, cp);
63*9508192eSchristos }
64*9508192eSchristos for (im = 0; im < MACRO_MAX; im++) {
65*9508192eSchristos for (iv = 0; iv < dbm_macro_count(im); iv++) {
66*9508192eSchristos mdata = dbm_macro_get(im, iv);
67*9508192eSchristos dba_macro_new(dba, im, mdata->value, mdata->pp);
68*9508192eSchristos }
69*9508192eSchristos }
70*9508192eSchristos dbm_close();
71*9508192eSchristos return dba;
72*9508192eSchristos }
73