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