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