Annotation of mandoc/dba.c, Revision 1.4
1.4 ! schwarze 1: /* $Id: dba.c,v 1.3 2016/07/29 15:29:32 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.4 ! 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 *);
1.3 schwarze 37: static int compare_strings(const void *, const void *);
1.1 schwarze 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);
138: dba_array_add(entry, prepend(name, NAME_FILE & NAME_MASK));
139: dba_array_add(page, entry);
140: entry = dba_array_new(1, DBA_STR | DBA_GROW);
141: dba_array_add(entry, (void *)sect);
142: dba_array_add(page, entry);
143: if (arch != NULL && *arch != '\0') {
144: entry = dba_array_new(1, DBA_STR | DBA_GROW);
145: dba_array_add(entry, (void *)arch);
146: } else
147: entry = NULL;
148: dba_array_add(page, entry);
149: dba_array_add(page, mandoc_strdup(desc));
150: entry = dba_array_new(1, DBA_STR | DBA_GROW);
151: dba_array_add(entry, prepend(file, form));
152: dba_array_add(page, entry);
153: dba_array_add(pages, page);
154: return page;
155: }
156:
157: /*
158: * Add a section, architecture, or file name to an existing page.
159: * Passing the NULL pointer for the architecture makes the page MI.
160: * In that case, any earlier or later architectures are ignored.
161: */
162: void
163: dba_page_add(struct dba_array *page, int32_t ie, const char *str)
164: {
165: struct dba_array *entries;
166: char *entry;
167:
168: entries = dba_array_get(page, ie);
169: if (ie == DBP_ARCH) {
170: if (entries == NULL)
171: return;
172: if (str == NULL) {
173: dba_array_free(entries);
174: dba_array_set(page, DBP_ARCH, NULL);
175: return;
176: }
177: }
178: if (*str == '\0')
179: return;
1.2 schwarze 180: dba_array_FOREACH(entries, entry) {
181: if (ie == DBP_FILE && *entry < ' ')
182: entry++;
1.1 schwarze 183: if (strcmp(entry, str) == 0)
184: return;
1.2 schwarze 185: }
1.1 schwarze 186: dba_array_add(entries, (void *)str);
187: }
188:
189: /*
190: * Add an additional name to an existing page.
191: */
192: void
193: dba_page_alias(struct dba_array *page, const char *name, uint64_t mask)
194: {
195: struct dba_array *entries;
196: char *entry;
197: char maskbyte;
198:
199: if (*name == '\0')
200: return;
201: maskbyte = mask & NAME_MASK;
202: entries = dba_array_get(page, DBP_NAME);
203: dba_array_FOREACH(entries, entry) {
204: if (strcmp(entry + 1, name) == 0) {
205: *entry |= maskbyte;
206: return;
207: }
208: }
209: dba_array_add(entries, prepend(name, maskbyte));
210: }
211:
212: /*
213: * Return a pointer to a temporary copy of instr with inbyte prepended.
214: */
215: static void *
216: prepend(const char *instr, char inbyte)
217: {
218: static char *outstr = NULL;
219: static size_t outlen = 0;
220: size_t newlen;
221:
222: newlen = strlen(instr) + 1;
223: if (newlen > outlen) {
224: outstr = mandoc_realloc(outstr, newlen + 1);
225: outlen = newlen;
226: }
227: *outstr = inbyte;
228: memcpy(outstr + 1, instr, newlen);
229: return outstr;
230: }
231:
232: /*
233: * Write the pages table to disk; the format is:
234: * - One integer containing the number of pages.
235: * - For each page, five pointers to the names, sections,
236: * architectures, description, and file names of the page.
237: * MI pages write 0 instead of the architecture pointer.
238: * - One list each for names, sections, architectures, descriptions and
239: * file names. The description for each page ends with a NUL byte.
240: * For all the other lists, each string ends with a NUL byte,
241: * and the last string for a page ends with two NUL bytes.
242: * - To assure alignment of following integers,
243: * the end is padded with NUL bytes up to a multiple of four bytes.
244: */
245: static void
246: dba_pages_write(struct dba_array *pages)
247: {
1.3 schwarze 248: struct dba_array *page, *entry;
1.1 schwarze 249: int32_t pos_pages, pos_end;
250:
251: pos_pages = dba_array_writelen(pages, 5);
252: dba_array_FOREACH(pages, page) {
253: dba_array_setpos(page, DBP_NAME, dba_tell());
1.3 schwarze 254: entry = dba_array_get(page, DBP_NAME);
255: dba_array_sort(entry, compare_names);
256: dba_array_writelst(entry);
1.1 schwarze 257: }
258: dba_array_FOREACH(pages, page) {
259: dba_array_setpos(page, DBP_SECT, dba_tell());
1.3 schwarze 260: entry = dba_array_get(page, DBP_SECT);
261: dba_array_sort(entry, compare_strings);
262: dba_array_writelst(entry);
1.1 schwarze 263: }
264: dba_array_FOREACH(pages, page) {
265: if ((entry = dba_array_get(page, DBP_ARCH)) != NULL) {
266: dba_array_setpos(page, DBP_ARCH, dba_tell());
1.3 schwarze 267: dba_array_sort(entry, compare_strings);
1.1 schwarze 268: dba_array_writelst(entry);
269: } else
270: dba_array_setpos(page, DBP_ARCH, 0);
271: }
272: dba_array_FOREACH(pages, page) {
273: dba_array_setpos(page, DBP_DESC, dba_tell());
274: dba_str_write(dba_array_get(page, DBP_DESC));
275: }
276: dba_array_FOREACH(pages, page) {
277: dba_array_setpos(page, DBP_FILE, dba_tell());
278: dba_array_writelst(dba_array_get(page, DBP_FILE));
279: }
280: pos_end = dba_align();
281: dba_seek(pos_pages);
282: dba_array_FOREACH(pages, page)
283: dba_array_writepos(page);
284: dba_seek(pos_end);
285: }
286:
287: static int
288: compare_names(const void *vp1, const void *vp2)
289: {
290: const char *cp1, *cp2;
291: int diff;
292:
293: cp1 = *(char **)vp1;
294: cp2 = *(char **)vp2;
295: return (diff = *cp2 - *cp1) ? diff :
296: strcasecmp(cp1 + 1, cp2 + 1);
297: }
298:
1.3 schwarze 299: static int
300: compare_strings(const void *vp1, const void *vp2)
301: {
302: const char *cp1, *cp2;
303:
304: cp1 = *(char **)vp1;
305: cp2 = *(char **)vp2;
306: return strcmp(cp1, cp2);
307: }
1.1 schwarze 308:
309: /*** functions for handling macros ************************************/
310:
311: /*
312: * Create a new macro entry and append it to one of the macro tables.
313: */
314: void
315: dba_macro_new(struct dba *dba, int32_t im, const char *value,
316: const int32_t *pp)
317: {
318: struct dba_array *entry, *pages;
319: const int32_t *ip;
320: int32_t np;
321:
322: np = 0;
323: for (ip = pp; *ip; ip++)
324: np++;
325: pages = dba_array_new(np, DBA_GROW);
326: for (ip = pp; *ip; ip++)
327: dba_array_add(pages, dba_array_get(dba->pages,
328: be32toh(*ip) / 5 / sizeof(*ip) - 1));
329:
330: entry = dba_array_new(2, 0);
331: dba_array_add(entry, mandoc_strdup(value));
332: dba_array_add(entry, pages);
333:
334: dba_array_add(dba_array_get(dba->macros, im), entry);
335: }
336:
337: /*
338: * Look up a macro entry by value and add a reference to a new page to it.
339: * If the value does not yet exist, create a new macro entry
340: * and add it to the macro table in question.
341: */
342: void
343: dba_macro_add(struct dba_array *macros, int32_t im, const char *value,
344: struct dba_array *page)
345: {
346: struct dba_array *macro, *entry, *pages;
347:
348: if (*value == '\0')
349: return;
350: macro = dba_array_get(macros, im);
351: dba_array_FOREACH(macro, entry)
352: if (strcmp(value, dba_array_get(entry, 0)) == 0)
353: break;
354: if (entry == NULL) {
355: entry = dba_array_new(2, 0);
356: dba_array_add(entry, mandoc_strdup(value));
357: pages = dba_array_new(1, DBA_GROW);
358: dba_array_add(entry, pages);
359: dba_array_add(macro, entry);
360: } else
361: pages = dba_array_get(entry, 1);
362: dba_array_add(pages, page);
363: }
364:
365: /*
366: * Write the macros table to disk; the format is:
367: * - The number of macro tables (actually, MACRO_MAX).
368: * - That number of pointers to the individual macro tables.
369: * - The individual macro tables.
370: */
371: static void
372: dba_macros_write(struct dba_array *macros)
373: {
374: struct dba_array *macro;
375: int32_t im, pos_macros, pos_end;
376:
377: pos_macros = dba_array_writelen(macros, 1);
378: im = 0;
379: dba_array_FOREACH(macros, macro) {
380: dba_array_setpos(macros, im++, dba_tell());
381: dba_macro_write(macro);
382: }
383: pos_end = dba_tell();
384: dba_seek(pos_macros);
385: dba_array_writepos(macros);
386: dba_seek(pos_end);
387: }
388:
389: /*
390: * Write one individual macro table to disk; the format is:
391: * - The number of entries in the table.
392: * - For each entry, two pointers, the first one to the value
393: * and the second one to the list of pages.
394: * - A list of values, each ending in a NUL byte.
395: * - To assure alignment of following integers,
396: * padding with NUL bytes up to a multiple of four bytes.
397: * - A list of pointers to pages, each list ending in a 0 integer.
398: */
399: static void
400: dba_macro_write(struct dba_array *macro)
401: {
402: struct dba_array *entry, *pages, *page;
403: int empty;
404: int32_t addr, pos_macro, pos_end;
405:
406: dba_array_FOREACH(macro, entry) {
407: pages = dba_array_get(entry, 1);
408: empty = 1;
409: dba_array_FOREACH(pages, page)
410: if (dba_array_getpos(page))
411: empty = 0;
412: if (empty)
413: dba_array_del(macro);
414: }
415: pos_macro = dba_array_writelen(macro, 2);
416: dba_array_FOREACH(macro, entry) {
417: dba_array_setpos(entry, 0, dba_tell());
418: dba_str_write(dba_array_get(entry, 0));
419: }
420: dba_align();
421: dba_array_FOREACH(macro, entry) {
422: dba_array_setpos(entry, 1, dba_tell());
423: pages = dba_array_get(entry, 1);
424: dba_array_FOREACH(pages, page)
425: if ((addr = dba_array_getpos(page)))
426: dba_int_write(addr);
427: dba_int_write(0);
428: }
429: pos_end = dba_tell();
430: dba_seek(pos_macro);
431: dba_array_FOREACH(macro, entry)
432: dba_array_writepos(entry);
433: dba_seek(pos_end);
434: }
CVSweb