]>
git.cameronkatri.com Git - mandoc.git/blob - dba.c
2fb2ab64782709f3969cbde6e4fd73cb0380802f
1 /* $Id: dba.c,v 1.2 2016/07/29 15:23:57 schwarze Exp $ */
3 * Copyright (c) 2016 Ingo Schwarze <schwarze@openbsd.org>
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.
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.
17 * Allocation-based version of the mandoc database, for read-write access.
18 * The interface is defined in "dba.h".
20 #include <sys/types.h>
27 #include "mandoc_aux.h"
28 #include "mansearch.h"
29 #include "dba_write.h"
30 #include "dba_array.h"
33 static void *prepend(const char *, char);
34 static void dba_pages_write(struct dba_array
*);
35 static int compare_names(const void *, const void *);
36 static void dba_macros_write(struct dba_array
*);
37 static void dba_macro_write(struct dba_array
*);
40 /*** top-level functions **********************************************/
43 dba_new(int32_t npages
)
48 dba
= mandoc_malloc(sizeof(*dba
));
49 dba
->pages
= dba_array_new(npages
, DBA_GROW
);
50 dba
->macros
= dba_array_new(MACRO_MAX
, 0);
51 for (im
= 0; im
< MACRO_MAX
; im
++)
52 dba_array_set(dba
->macros
, im
, dba_array_new(128, DBA_GROW
));
57 dba_free(struct dba
*dba
)
59 struct dba_array
*page
, *macro
, *entry
;
61 dba_array_FOREACH(dba
->macros
, macro
) {
62 dba_array_undel(macro
);
63 dba_array_FOREACH(macro
, entry
) {
64 free(dba_array_get(entry
, 0));
65 dba_array_free(dba_array_get(entry
, 1));
66 dba_array_free(entry
);
68 dba_array_free(macro
);
70 dba_array_free(dba
->macros
);
72 dba_array_undel(dba
->pages
);
73 dba_array_FOREACH(dba
->pages
, page
) {
74 dba_array_free(dba_array_get(page
, DBP_NAME
));
75 dba_array_free(dba_array_get(page
, DBP_SECT
));
76 dba_array_free(dba_array_get(page
, DBP_ARCH
));
77 free(dba_array_get(page
, DBP_DESC
));
78 dba_array_free(dba_array_get(page
, DBP_FILE
));
81 dba_array_free(dba
->pages
);
87 * Write the complete mandoc database to disk; the format is:
88 * - One integer each for magic and version.
89 * - One pointer each to the macros table and to the final magic.
92 * - And at the very end, the magic integer again.
95 dba_write(const char *fname
, struct dba
*dba
)
98 int32_t pos_end
, pos_macros
, pos_macros_ptr
;
100 if (dba_open(fname
) == -1)
102 dba_int_write(MANDOCDB_MAGIC
);
103 dba_int_write(MANDOCDB_VERSION
);
104 pos_macros_ptr
= dba_skip(1, 2);
105 dba_pages_write(dba
->pages
);
106 pos_macros
= dba_tell();
107 dba_macros_write(dba
->macros
);
108 pos_end
= dba_tell();
109 dba_int_write(MANDOCDB_MAGIC
);
110 dba_seek(pos_macros_ptr
);
111 dba_int_write(pos_macros
);
112 dba_int_write(pos_end
);
113 if (dba_close() == -1) {
123 /*** functions for handling pages *************************************/
126 * Create a new page and append it to the pages table.
129 dba_page_new(struct dba_array
*pages
, const char *name
, const char *sect
,
130 const char *arch
, const char *desc
, const char *file
, enum form form
)
132 struct dba_array
*page
, *entry
;
134 page
= dba_array_new(DBP_MAX
, 0);
135 entry
= dba_array_new(1, DBA_STR
| DBA_GROW
);
136 dba_array_add(entry
, prepend(name
, NAME_FILE
& NAME_MASK
));
137 dba_array_add(page
, entry
);
138 entry
= dba_array_new(1, DBA_STR
| DBA_GROW
);
139 dba_array_add(entry
, (void *)sect
);
140 dba_array_add(page
, entry
);
141 if (arch
!= NULL
&& *arch
!= '\0') {
142 entry
= dba_array_new(1, DBA_STR
| DBA_GROW
);
143 dba_array_add(entry
, (void *)arch
);
146 dba_array_add(page
, entry
);
147 dba_array_add(page
, mandoc_strdup(desc
));
148 entry
= dba_array_new(1, DBA_STR
| DBA_GROW
);
149 dba_array_add(entry
, prepend(file
, form
));
150 dba_array_add(page
, entry
);
151 dba_array_add(pages
, page
);
156 * Add a section, architecture, or file name to an existing page.
157 * Passing the NULL pointer for the architecture makes the page MI.
158 * In that case, any earlier or later architectures are ignored.
161 dba_page_add(struct dba_array
*page
, int32_t ie
, const char *str
)
163 struct dba_array
*entries
;
166 entries
= dba_array_get(page
, ie
);
167 if (ie
== DBP_ARCH
) {
171 dba_array_free(entries
);
172 dba_array_set(page
, DBP_ARCH
, NULL
);
178 dba_array_FOREACH(entries
, entry
) {
179 if (ie
== DBP_FILE
&& *entry
< ' ')
181 if (strcmp(entry
, str
) == 0)
184 dba_array_add(entries
, (void *)str
);
188 * Add an additional name to an existing page.
191 dba_page_alias(struct dba_array
*page
, const char *name
, uint64_t mask
)
193 struct dba_array
*entries
;
199 maskbyte
= mask
& NAME_MASK
;
200 entries
= dba_array_get(page
, DBP_NAME
);
201 dba_array_FOREACH(entries
, entry
) {
202 if (strcmp(entry
+ 1, name
) == 0) {
207 dba_array_add(entries
, prepend(name
, maskbyte
));
211 * Return a pointer to a temporary copy of instr with inbyte prepended.
214 prepend(const char *instr
, char inbyte
)
216 static char *outstr
= NULL
;
217 static size_t outlen
= 0;
220 newlen
= strlen(instr
) + 1;
221 if (newlen
> outlen
) {
222 outstr
= mandoc_realloc(outstr
, newlen
+ 1);
226 memcpy(outstr
+ 1, instr
, newlen
);
231 * Write the pages table to disk; the format is:
232 * - One integer containing the number of pages.
233 * - For each page, five pointers to the names, sections,
234 * architectures, description, and file names of the page.
235 * MI pages write 0 instead of the architecture pointer.
236 * - One list each for names, sections, architectures, descriptions and
237 * file names. The description for each page ends with a NUL byte.
238 * For all the other lists, each string ends with a NUL byte,
239 * and the last string for a page ends with two NUL bytes.
240 * - To assure alignment of following integers,
241 * the end is padded with NUL bytes up to a multiple of four bytes.
244 dba_pages_write(struct dba_array
*pages
)
246 struct dba_array
*page
, *names
;
248 int32_t pos_pages
, pos_end
;
250 pos_pages
= dba_array_writelen(pages
, 5);
251 dba_array_FOREACH(pages
, page
) {
252 dba_array_setpos(page
, DBP_NAME
, dba_tell());
253 names
= dba_array_get(page
, DBP_NAME
);
254 dba_array_sort(names
, compare_names
);
255 dba_array_writelst(names
);
257 dba_array_FOREACH(pages
, page
) {
258 dba_array_setpos(page
, DBP_SECT
, dba_tell());
259 dba_array_writelst(dba_array_get(page
, DBP_SECT
));
261 dba_array_FOREACH(pages
, page
) {
262 if ((entry
= dba_array_get(page
, DBP_ARCH
)) != NULL
) {
263 dba_array_setpos(page
, DBP_ARCH
, dba_tell());
264 dba_array_writelst(entry
);
266 dba_array_setpos(page
, DBP_ARCH
, 0);
268 dba_array_FOREACH(pages
, page
) {
269 dba_array_setpos(page
, DBP_DESC
, dba_tell());
270 dba_str_write(dba_array_get(page
, DBP_DESC
));
272 dba_array_FOREACH(pages
, page
) {
273 dba_array_setpos(page
, DBP_FILE
, dba_tell());
274 dba_array_writelst(dba_array_get(page
, DBP_FILE
));
276 pos_end
= dba_align();
278 dba_array_FOREACH(pages
, page
)
279 dba_array_writepos(page
);
284 compare_names(const void *vp1
, const void *vp2
)
286 const char *cp1
, *cp2
;
291 return (diff
= *cp2
- *cp1
) ? diff
:
292 strcasecmp(cp1
+ 1, cp2
+ 1);
296 /*** functions for handling macros ************************************/
299 * Create a new macro entry and append it to one of the macro tables.
302 dba_macro_new(struct dba
*dba
, int32_t im
, const char *value
,
305 struct dba_array
*entry
, *pages
;
310 for (ip
= pp
; *ip
; ip
++)
312 pages
= dba_array_new(np
, DBA_GROW
);
313 for (ip
= pp
; *ip
; ip
++)
314 dba_array_add(pages
, dba_array_get(dba
->pages
,
315 be32toh(*ip
) / 5 / sizeof(*ip
) - 1));
317 entry
= dba_array_new(2, 0);
318 dba_array_add(entry
, mandoc_strdup(value
));
319 dba_array_add(entry
, pages
);
321 dba_array_add(dba_array_get(dba
->macros
, im
), entry
);
325 * Look up a macro entry by value and add a reference to a new page to it.
326 * If the value does not yet exist, create a new macro entry
327 * and add it to the macro table in question.
330 dba_macro_add(struct dba_array
*macros
, int32_t im
, const char *value
,
331 struct dba_array
*page
)
333 struct dba_array
*macro
, *entry
, *pages
;
337 macro
= dba_array_get(macros
, im
);
338 dba_array_FOREACH(macro
, entry
)
339 if (strcmp(value
, dba_array_get(entry
, 0)) == 0)
342 entry
= dba_array_new(2, 0);
343 dba_array_add(entry
, mandoc_strdup(value
));
344 pages
= dba_array_new(1, DBA_GROW
);
345 dba_array_add(entry
, pages
);
346 dba_array_add(macro
, entry
);
348 pages
= dba_array_get(entry
, 1);
349 dba_array_add(pages
, page
);
353 * Write the macros table to disk; the format is:
354 * - The number of macro tables (actually, MACRO_MAX).
355 * - That number of pointers to the individual macro tables.
356 * - The individual macro tables.
359 dba_macros_write(struct dba_array
*macros
)
361 struct dba_array
*macro
;
362 int32_t im
, pos_macros
, pos_end
;
364 pos_macros
= dba_array_writelen(macros
, 1);
366 dba_array_FOREACH(macros
, macro
) {
367 dba_array_setpos(macros
, im
++, dba_tell());
368 dba_macro_write(macro
);
370 pos_end
= dba_tell();
371 dba_seek(pos_macros
);
372 dba_array_writepos(macros
);
377 * Write one individual macro table to disk; the format is:
378 * - The number of entries in the table.
379 * - For each entry, two pointers, the first one to the value
380 * and the second one to the list of pages.
381 * - A list of values, each ending in a NUL byte.
382 * - To assure alignment of following integers,
383 * padding with NUL bytes up to a multiple of four bytes.
384 * - A list of pointers to pages, each list ending in a 0 integer.
387 dba_macro_write(struct dba_array
*macro
)
389 struct dba_array
*entry
, *pages
, *page
;
391 int32_t addr
, pos_macro
, pos_end
;
393 dba_array_FOREACH(macro
, entry
) {
394 pages
= dba_array_get(entry
, 1);
396 dba_array_FOREACH(pages
, page
)
397 if (dba_array_getpos(page
))
400 dba_array_del(macro
);
402 pos_macro
= dba_array_writelen(macro
, 2);
403 dba_array_FOREACH(macro
, entry
) {
404 dba_array_setpos(entry
, 0, dba_tell());
405 dba_str_write(dba_array_get(entry
, 0));
408 dba_array_FOREACH(macro
, entry
) {
409 dba_array_setpos(entry
, 1, dba_tell());
410 pages
= dba_array_get(entry
, 1);
411 dba_array_FOREACH(pages
, page
)
412 if ((addr
= dba_array_getpos(page
)))
416 pos_end
= dba_tell();
418 dba_array_FOREACH(macro
, entry
)
419 dba_array_writepos(entry
);