Annotation of src/usr.bin/mandoc/dba.c, Revision 1.4
1.4 ! schwarze 1: /* $OpenBSD: dba.c,v 1.3 2016/08/17 18:05:40 schwarze Exp $ */
1.1 schwarze 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: * Allocation-based version of the mandoc database, for read-write access.
18: * The interface is defined in "dba.h".
19: */
20: #include <sys/types.h>
1.2 schwarze 21: #include <endian.h>
1.1 schwarze 22: #include <errno.h>
23: #include <stdint.h>
24: #include <stdlib.h>
25: #include <string.h>
26: #include <unistd.h>
27:
28: #include "mandoc_aux.h"
29: #include "mansearch.h"
30: #include "dba_write.h"
31: #include "dba_array.h"
32: #include "dba.h"
33:
34: static void *prepend(const char *, char);
35: static void dba_pages_write(struct dba_array *);
36: static int compare_names(const void *, const void *);
37: static int compare_strings(const void *, const void *);
38: static void dba_macros_write(struct dba_array *);
39: static void dba_macro_write(struct dba_array *);
40:
41:
42: /*** top-level functions **********************************************/
43:
44: struct dba *
45: dba_new(int32_t npages)
46: {
47: struct dba *dba;
48: int32_t im;
49:
50: dba = mandoc_malloc(sizeof(*dba));
51: dba->pages = dba_array_new(npages, DBA_GROW);
52: dba->macros = dba_array_new(MACRO_MAX, 0);
53: for (im = 0; im < MACRO_MAX; im++)
54: dba_array_set(dba->macros, im, dba_array_new(128, DBA_GROW));
55: return dba;
56: }
57:
58: void
59: dba_free(struct dba *dba)
60: {
61: struct dba_array *page, *macro, *entry;
62:
63: dba_array_FOREACH(dba->macros, macro) {
64: dba_array_undel(macro);
65: dba_array_FOREACH(macro, entry) {
66: free(dba_array_get(entry, 0));
67: dba_array_free(dba_array_get(entry, 1));
68: dba_array_free(entry);
69: }
70: dba_array_free(macro);
71: }
72: dba_array_free(dba->macros);
73:
74: dba_array_undel(dba->pages);
75: dba_array_FOREACH(dba->pages, page) {
76: dba_array_free(dba_array_get(page, DBP_NAME));
77: dba_array_free(dba_array_get(page, DBP_SECT));
78: dba_array_free(dba_array_get(page, DBP_ARCH));
79: free(dba_array_get(page, DBP_DESC));
80: dba_array_free(dba_array_get(page, DBP_FILE));
81: dba_array_free(page);
82: }
83: dba_array_free(dba->pages);
84:
85: free(dba);
86: }
87:
88: /*
89: * Write the complete mandoc database to disk; the format is:
90: * - One integer each for magic and version.
91: * - One pointer each to the macros table and to the final magic.
92: * - The pages table.
93: * - The macros table.
94: * - And at the very end, the magic integer again.
95: */
96: int
97: dba_write(const char *fname, struct dba *dba)
98: {
99: int save_errno;
100: int32_t pos_end, pos_macros, pos_macros_ptr;
101:
102: if (dba_open(fname) == -1)
103: return -1;
104: dba_int_write(MANDOCDB_MAGIC);
105: dba_int_write(MANDOCDB_VERSION);
106: pos_macros_ptr = dba_skip(1, 2);
107: dba_pages_write(dba->pages);
108: pos_macros = dba_tell();
109: dba_macros_write(dba->macros);
110: pos_end = dba_tell();
111: dba_int_write(MANDOCDB_MAGIC);
112: dba_seek(pos_macros_ptr);
113: dba_int_write(pos_macros);
114: dba_int_write(pos_end);
115: if (dba_close() == -1) {
116: save_errno = errno;
117: unlink(fname);
118: errno = save_errno;
119: return -1;
120: }
121: return 0;
122: }
123:
124:
125: /*** functions for handling pages *************************************/
126:
127: /*
128: * Create a new page and append it to the pages table.
129: */
130: struct dba_array *
131: dba_page_new(struct dba_array *pages, const char *name, const char *sect,
132: const char *arch, const char *desc, const char *file, enum form form)
133: {
134: struct dba_array *page, *entry;
135:
136: page = dba_array_new(DBP_MAX, 0);
137: entry = dba_array_new(1, DBA_STR | DBA_GROW);
1.4 ! schwarze 138: if (name != NULL)
! 139: dba_array_add(entry, prepend(name, NAME_FILE & NAME_MASK));
1.1 schwarze 140: dba_array_add(page, entry);
141: entry = dba_array_new(1, DBA_STR | DBA_GROW);
142: dba_array_add(entry, (void *)sect);
143: dba_array_add(page, entry);
144: if (arch != NULL && *arch != '\0') {
145: entry = dba_array_new(1, DBA_STR | DBA_GROW);
146: dba_array_add(entry, (void *)arch);
147: } else
148: entry = NULL;
149: dba_array_add(page, entry);
150: dba_array_add(page, mandoc_strdup(desc));
151: entry = dba_array_new(1, DBA_STR | DBA_GROW);
152: dba_array_add(entry, prepend(file, form));
153: dba_array_add(page, entry);
154: dba_array_add(pages, page);
155: return page;
156: }
157:
158: /*
159: * Add a section, architecture, or file name to an existing page.
160: * Passing the NULL pointer for the architecture makes the page MI.
161: * In that case, any earlier or later architectures are ignored.
162: */
163: void
164: dba_page_add(struct dba_array *page, int32_t ie, const char *str)
165: {
166: struct dba_array *entries;
167: char *entry;
168:
169: entries = dba_array_get(page, ie);
170: if (ie == DBP_ARCH) {
171: if (entries == NULL)
172: return;
1.3 schwarze 173: if (str == NULL || *str == '\0') {
1.1 schwarze 174: dba_array_free(entries);
175: dba_array_set(page, DBP_ARCH, NULL);
176: return;
177: }
178: }
179: if (*str == '\0')
180: return;
181: dba_array_FOREACH(entries, entry) {
182: if (ie == DBP_FILE && *entry < ' ')
183: entry++;
184: if (strcmp(entry, str) == 0)
185: return;
186: }
187: dba_array_add(entries, (void *)str);
188: }
189:
190: /*
191: * Add an additional name to an existing page.
192: */
193: void
194: dba_page_alias(struct dba_array *page, const char *name, uint64_t mask)
195: {
196: struct dba_array *entries;
197: char *entry;
198: char maskbyte;
199:
200: if (*name == '\0')
201: return;
202: maskbyte = mask & NAME_MASK;
203: entries = dba_array_get(page, DBP_NAME);
204: dba_array_FOREACH(entries, entry) {
205: if (strcmp(entry + 1, name) == 0) {
206: *entry |= maskbyte;
207: return;
208: }
209: }
210: dba_array_add(entries, prepend(name, maskbyte));
211: }
212:
213: /*
214: * Return a pointer to a temporary copy of instr with inbyte prepended.
215: */
216: static void *
217: prepend(const char *instr, char inbyte)
218: {
219: static char *outstr = NULL;
220: static size_t outlen = 0;
221: size_t newlen;
222:
223: newlen = strlen(instr) + 1;
224: if (newlen > outlen) {
225: outstr = mandoc_realloc(outstr, newlen + 1);
226: outlen = newlen;
227: }
228: *outstr = inbyte;
229: memcpy(outstr + 1, instr, newlen);
230: return outstr;
231: }
232:
233: /*
234: * Write the pages table to disk; the format is:
235: * - One integer containing the number of pages.
236: * - For each page, five pointers to the names, sections,
237: * architectures, description, and file names of the page.
238: * MI pages write 0 instead of the architecture pointer.
239: * - One list each for names, sections, architectures, descriptions and
240: * file names. The description for each page ends with a NUL byte.
241: * For all the other lists, each string ends with a NUL byte,
242: * and the last string for a page ends with two NUL bytes.
243: * - To assure alignment of following integers,
244: * the end is padded with NUL bytes up to a multiple of four bytes.
245: */
246: static void
247: dba_pages_write(struct dba_array *pages)
248: {
249: struct dba_array *page, *entry;
250: int32_t pos_pages, pos_end;
251:
252: pos_pages = dba_array_writelen(pages, 5);
253: dba_array_FOREACH(pages, page) {
254: dba_array_setpos(page, DBP_NAME, dba_tell());
255: entry = dba_array_get(page, DBP_NAME);
256: dba_array_sort(entry, compare_names);
257: dba_array_writelst(entry);
258: }
259: dba_array_FOREACH(pages, page) {
260: dba_array_setpos(page, DBP_SECT, dba_tell());
261: entry = dba_array_get(page, DBP_SECT);
262: dba_array_sort(entry, compare_strings);
263: dba_array_writelst(entry);
264: }
265: dba_array_FOREACH(pages, page) {
266: if ((entry = dba_array_get(page, DBP_ARCH)) != NULL) {
267: dba_array_setpos(page, DBP_ARCH, dba_tell());
268: dba_array_sort(entry, compare_strings);
269: dba_array_writelst(entry);
270: } else
271: dba_array_setpos(page, DBP_ARCH, 0);
272: }
273: dba_array_FOREACH(pages, page) {
274: dba_array_setpos(page, DBP_DESC, dba_tell());
275: dba_str_write(dba_array_get(page, DBP_DESC));
276: }
277: dba_array_FOREACH(pages, page) {
278: dba_array_setpos(page, DBP_FILE, dba_tell());
279: dba_array_writelst(dba_array_get(page, DBP_FILE));
280: }
281: pos_end = dba_align();
282: dba_seek(pos_pages);
283: dba_array_FOREACH(pages, page)
284: dba_array_writepos(page);
285: dba_seek(pos_end);
286: }
287:
288: static int
289: compare_names(const void *vp1, const void *vp2)
290: {
291: const char *cp1, *cp2;
292: int diff;
293:
294: cp1 = *(char **)vp1;
295: cp2 = *(char **)vp2;
296: return (diff = *cp2 - *cp1) ? diff :
297: strcasecmp(cp1 + 1, cp2 + 1);
298: }
299:
300: static int
301: compare_strings(const void *vp1, const void *vp2)
302: {
303: const char *cp1, *cp2;
304:
305: cp1 = *(char **)vp1;
306: cp2 = *(char **)vp2;
307: return strcmp(cp1, cp2);
308: }
309:
310: /*** functions for handling macros ************************************/
311:
312: /*
313: * Create a new macro entry and append it to one of the macro tables.
314: */
315: void
316: dba_macro_new(struct dba *dba, int32_t im, const char *value,
317: const int32_t *pp)
318: {
319: struct dba_array *entry, *pages;
320: const int32_t *ip;
321: int32_t np;
322:
323: np = 0;
324: for (ip = pp; *ip; ip++)
325: np++;
326: pages = dba_array_new(np, DBA_GROW);
327: for (ip = pp; *ip; ip++)
328: dba_array_add(pages, dba_array_get(dba->pages,
329: be32toh(*ip) / 5 / sizeof(*ip) - 1));
330:
331: entry = dba_array_new(2, 0);
332: dba_array_add(entry, mandoc_strdup(value));
333: dba_array_add(entry, pages);
334:
335: dba_array_add(dba_array_get(dba->macros, im), entry);
336: }
337:
338: /*
339: * Look up a macro entry by value and add a reference to a new page to it.
340: * If the value does not yet exist, create a new macro entry
341: * and add it to the macro table in question.
342: */
343: void
344: dba_macro_add(struct dba_array *macros, int32_t im, const char *value,
345: struct dba_array *page)
346: {
347: struct dba_array *macro, *entry, *pages;
348:
349: if (*value == '\0')
350: return;
351: macro = dba_array_get(macros, im);
352: dba_array_FOREACH(macro, entry)
353: if (strcmp(value, dba_array_get(entry, 0)) == 0)
354: break;
355: if (entry == NULL) {
356: entry = dba_array_new(2, 0);
357: dba_array_add(entry, mandoc_strdup(value));
358: pages = dba_array_new(1, DBA_GROW);
359: dba_array_add(entry, pages);
360: dba_array_add(macro, entry);
361: } else
362: pages = dba_array_get(entry, 1);
363: dba_array_add(pages, page);
364: }
365:
366: /*
367: * Write the macros table to disk; the format is:
368: * - The number of macro tables (actually, MACRO_MAX).
369: * - That number of pointers to the individual macro tables.
370: * - The individual macro tables.
371: */
372: static void
373: dba_macros_write(struct dba_array *macros)
374: {
375: struct dba_array *macro;
376: int32_t im, pos_macros, pos_end;
377:
378: pos_macros = dba_array_writelen(macros, 1);
379: im = 0;
380: dba_array_FOREACH(macros, macro) {
381: dba_array_setpos(macros, im++, dba_tell());
382: dba_macro_write(macro);
383: }
384: pos_end = dba_tell();
385: dba_seek(pos_macros);
386: dba_array_writepos(macros);
387: dba_seek(pos_end);
388: }
389:
390: /*
391: * Write one individual macro table to disk; the format is:
392: * - The number of entries in the table.
393: * - For each entry, two pointers, the first one to the value
394: * and the second one to the list of pages.
395: * - A list of values, each ending in a NUL byte.
396: * - To assure alignment of following integers,
397: * padding with NUL bytes up to a multiple of four bytes.
398: * - A list of pointers to pages, each list ending in a 0 integer.
399: */
400: static void
401: dba_macro_write(struct dba_array *macro)
402: {
403: struct dba_array *entry, *pages, *page;
404: int empty;
405: int32_t addr, pos_macro, pos_end;
406:
407: dba_array_FOREACH(macro, entry) {
408: pages = dba_array_get(entry, 1);
409: empty = 1;
410: dba_array_FOREACH(pages, page)
411: if (dba_array_getpos(page))
412: empty = 0;
413: if (empty)
414: dba_array_del(macro);
415: }
416: pos_macro = dba_array_writelen(macro, 2);
417: dba_array_FOREACH(macro, entry) {
418: dba_array_setpos(entry, 0, dba_tell());
419: dba_str_write(dba_array_get(entry, 0));
420: }
421: dba_align();
422: dba_array_FOREACH(macro, entry) {
423: dba_array_setpos(entry, 1, dba_tell());
424: pages = dba_array_get(entry, 1);
425: dba_array_FOREACH(pages, page)
426: if ((addr = dba_array_getpos(page)))
427: dba_int_write(addr);
428: dba_int_write(0);
429: }
430: pos_end = dba_tell();
431: dba_seek(pos_macro);
432: dba_array_FOREACH(macro, entry)
433: dba_array_writepos(entry);
434: dba_seek(pos_end);
435: }