xref: /freebsd/contrib/mandoc/dba_read.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
1 /* $Id: dba_read.c,v 1.5 2020/06/22 19:20:40 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 "config.h"
23 
24 #include <regex.h>
25 #include <stdint.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 
30 #include "mandoc_aux.h"
31 #include "mansearch.h"
32 #include "dba_array.h"
33 #include "dba.h"
34 #include "dbm.h"
35 
36 
37 struct dba *
38 dba_read(const char *fname)
39 {
40 	struct dba		*dba;
41 	struct dba_array	*page;
42 	struct dbm_page		*pdata;
43 	struct dbm_macro	*mdata;
44 	const char		*cp;
45 	int32_t			 im, ip, iv, npages;
46 
47 	if (dbm_open(fname) == -1)
48 		return NULL;
49 	npages = dbm_page_count();
50 	dba = dba_new(npages < 128 ? 128 : npages);
51 	for (ip = 0; ip < npages; ip++) {
52 		pdata = dbm_page_get(ip);
53 		page = dba_page_new(dba->pages, pdata->arch,
54 		    pdata->desc, pdata->file + 1, *pdata->file);
55 		for (cp = pdata->name; *cp != '\0'; cp = strchr(cp, '\0') + 1)
56 			dba_page_add(page, DBP_NAME, cp);
57 		for (cp = pdata->sect; *cp != '\0'; cp = strchr(cp, '\0') + 1)
58 			dba_page_add(page, DBP_SECT, cp);
59 		if ((cp = pdata->arch) != NULL)
60 			while (*(cp = strchr(cp, '\0') + 1) != '\0')
61 				dba_page_add(page, DBP_ARCH, cp);
62 		cp = pdata->file;
63 		while (*(cp = strchr(cp, '\0') + 1) != '\0')
64 			dba_page_add(page, DBP_FILE, cp);
65 	}
66 	for (im = 0; im < MACRO_MAX; im++) {
67 		for (iv = 0; iv < dbm_macro_count(im); iv++) {
68 			mdata = dbm_macro_get(im, iv);
69 			dba_macro_new(dba, im, mdata->value, mdata->pp);
70 		}
71 	}
72 	dbm_close();
73 	return dba;
74 }
75