Annotation of mandoc/dba.c, Revision 1.2
1.2 ! schwarze 1: /* $Id: dba.c,v 1.1 2016/07/19 21:31:55 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>
21: #include <errno.h>
22: #include <stdint.h>
23: #include <stdlib.h>
24: #include <string.h>
25: #include <unistd.h>
26:
27: #include "mandoc_aux.h"
28: #include "mansearch.h"
29: #include "dba_write.h"
30: #include "dba_array.h"
31: #include "dba.h"
32:
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 *);
38:
39:
40: /*** top-level functions **********************************************/
41:
42: struct dba *
43: dba_new(int32_t npages)
44: {
45: struct dba *dba;
46: int32_t im;
47:
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));
53: return dba;
54: }
55:
56: void
57: dba_free(struct dba *dba)
58: {
59: struct dba_array *page, *macro, *entry;
60:
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);
67: }
68: dba_array_free(macro);
69: }
70: dba_array_free(dba->macros);
71:
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));
79: dba_array_free(page);
80: }
81: dba_array_free(dba->pages);
82:
83: free(dba);
84: }
85:
86: /*
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.
90: * - The pages table.
91: * - The macros table.
92: * - And at the very end, the magic integer again.
93: */
94: int
95: dba_write(const char *fname, struct dba *dba)
96: {
97: int save_errno;
98: int32_t pos_end, pos_macros, pos_macros_ptr;
99:
100: if (dba_open(fname) == -1)
101: return -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) {
114: save_errno = errno;
115: unlink(fname);
116: errno = save_errno;
117: return -1;
118: }
119: return 0;
120: }
121:
122:
123: /*** functions for handling pages *************************************/
124:
125: /*
126: * Create a new page and append it to the pages table.
127: */
128: struct dba_array *
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)
131: {
132: struct dba_array *page, *entry;
133:
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);
144: } else
145: entry = NULL;
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);
152: return page;
153: }
154:
155: /*
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.
159: */
160: void
161: dba_page_add(struct dba_array *page, int32_t ie, const char *str)
162: {
163: struct dba_array *entries;
164: char *entry;
165:
166: entries = dba_array_get(page, ie);
167: if (ie == DBP_ARCH) {
168: if (entries == NULL)
169: return;
170: if (str == NULL) {
171: dba_array_free(entries);
172: dba_array_set(page, DBP_ARCH, NULL);
173: return;
174: }
175: }
176: if (*str == '\0')
177: return;
1.2 ! schwarze 178: dba_array_FOREACH(entries, entry) {
! 179: if (ie == DBP_FILE && *entry < ' ')
! 180: entry++;
1.1 schwarze 181: if (strcmp(entry, str) == 0)
182: return;
1.2 ! schwarze 183: }
1.1 schwarze 184: dba_array_add(entries, (void *)str);
185: }
186:
187: /*
188: * Add an additional name to an existing page.
189: */
190: void
191: dba_page_alias(struct dba_array *page, const char *name, uint64_t mask)
192: {
193: struct dba_array *entries;
194: char *entry;
195: char maskbyte;
196:
197: if (*name == '\0')
198: return;
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) {
203: *entry |= maskbyte;
204: return;
205: }
206: }
207: dba_array_add(entries, prepend(name, maskbyte));
208: }
209:
210: /*
211: * Return a pointer to a temporary copy of instr with inbyte prepended.
212: */
213: static void *
214: prepend(const char *instr, char inbyte)
215: {
216: static char *outstr = NULL;
217: static size_t outlen = 0;
218: size_t newlen;
219:
220: newlen = strlen(instr) + 1;
221: if (newlen > outlen) {
222: outstr = mandoc_realloc(outstr, newlen + 1);
223: outlen = newlen;
224: }
225: *outstr = inbyte;
226: memcpy(outstr + 1, instr, newlen);
227: return outstr;
228: }
229:
230: /*
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.
242: */
243: static void
244: dba_pages_write(struct dba_array *pages)
245: {
246: struct dba_array *page, *names;
247: void *entry;
248: int32_t pos_pages, pos_end;
249:
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);
256: }
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));
260: }
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);
265: } else
266: dba_array_setpos(page, DBP_ARCH, 0);
267: }
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));
271: }
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));
275: }
276: pos_end = dba_align();
277: dba_seek(pos_pages);
278: dba_array_FOREACH(pages, page)
279: dba_array_writepos(page);
280: dba_seek(pos_end);
281: }
282:
283: static int
284: compare_names(const void *vp1, const void *vp2)
285: {
286: const char *cp1, *cp2;
287: int diff;
288:
289: cp1 = *(char **)vp1;
290: cp2 = *(char **)vp2;
291: return (diff = *cp2 - *cp1) ? diff :
292: strcasecmp(cp1 + 1, cp2 + 1);
293: }
294:
295:
296: /*** functions for handling macros ************************************/
297:
298: /*
299: * Create a new macro entry and append it to one of the macro tables.
300: */
301: void
302: dba_macro_new(struct dba *dba, int32_t im, const char *value,
303: const int32_t *pp)
304: {
305: struct dba_array *entry, *pages;
306: const int32_t *ip;
307: int32_t np;
308:
309: np = 0;
310: for (ip = pp; *ip; ip++)
311: np++;
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));
316:
317: entry = dba_array_new(2, 0);
318: dba_array_add(entry, mandoc_strdup(value));
319: dba_array_add(entry, pages);
320:
321: dba_array_add(dba_array_get(dba->macros, im), entry);
322: }
323:
324: /*
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.
328: */
329: void
330: dba_macro_add(struct dba_array *macros, int32_t im, const char *value,
331: struct dba_array *page)
332: {
333: struct dba_array *macro, *entry, *pages;
334:
335: if (*value == '\0')
336: return;
337: macro = dba_array_get(macros, im);
338: dba_array_FOREACH(macro, entry)
339: if (strcmp(value, dba_array_get(entry, 0)) == 0)
340: break;
341: if (entry == NULL) {
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);
347: } else
348: pages = dba_array_get(entry, 1);
349: dba_array_add(pages, page);
350: }
351:
352: /*
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.
357: */
358: static void
359: dba_macros_write(struct dba_array *macros)
360: {
361: struct dba_array *macro;
362: int32_t im, pos_macros, pos_end;
363:
364: pos_macros = dba_array_writelen(macros, 1);
365: im = 0;
366: dba_array_FOREACH(macros, macro) {
367: dba_array_setpos(macros, im++, dba_tell());
368: dba_macro_write(macro);
369: }
370: pos_end = dba_tell();
371: dba_seek(pos_macros);
372: dba_array_writepos(macros);
373: dba_seek(pos_end);
374: }
375:
376: /*
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.
385: */
386: static void
387: dba_macro_write(struct dba_array *macro)
388: {
389: struct dba_array *entry, *pages, *page;
390: int empty;
391: int32_t addr, pos_macro, pos_end;
392:
393: dba_array_FOREACH(macro, entry) {
394: pages = dba_array_get(entry, 1);
395: empty = 1;
396: dba_array_FOREACH(pages, page)
397: if (dba_array_getpos(page))
398: empty = 0;
399: if (empty)
400: dba_array_del(macro);
401: }
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));
406: }
407: dba_align();
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)))
413: dba_int_write(addr);
414: dba_int_write(0);
415: }
416: pos_end = dba_tell();
417: dba_seek(pos_macro);
418: dba_array_FOREACH(macro, entry)
419: dba_array_writepos(entry);
420: dba_seek(pos_end);
421: }
CVSweb