Annotation of docbook2mdoc/node.c, Revision 1.27
1.27 ! schwarze 1: /* $Id: node.c,v 1.26 2019/04/28 20:26:49 schwarze Exp $ */
1.1 schwarze 2: /*
3: * Copyright (c) 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4: * Copyright (c) 2019 Ingo Schwarze <schwarze@openbsd.org>
5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
1.10 schwarze 18: #include <assert.h>
1.1 schwarze 19: #include <stdlib.h>
20: #include <string.h>
21:
1.24 schwarze 22: #include "xmalloc.h"
1.1 schwarze 23: #include "node.h"
24:
25: /*
26: * The implementation of the DocBook syntax tree.
27: */
28:
1.10 schwarze 29: struct nodeprop {
30: const char *name;
31: enum nodeclass class;
32: };
33:
34: static const struct nodeprop properties[] = {
1.26 schwarze 35: { "abstract", CLASS_BLOCK },
1.10 schwarze 36: { "appendix", CLASS_BLOCK },
37: { "arg", CLASS_ENCL },
38: { "author", CLASS_LINE },
39: { "authorgroup", CLASS_BLOCK },
40: { "blockquote", CLASS_BLOCK },
41: { "bookinfo", CLASS_BLOCK },
42: { "caution", CLASS_BLOCK },
43: { "citerefentry", CLASS_LINE },
44: { "citetitle", CLASS_LINE },
45: { "cmdsynopsis", CLASS_TRANS },
46: { "colspec", CLASS_VOID },
47: { "command", CLASS_LINE },
48: { "constant", CLASS_LINE },
49: { "contrib", CLASS_TRANS },
1.15 schwarze 50: { "copyright", CLASS_LINE },
1.10 schwarze 51: { "date", CLASS_TRANS },
52: { "!DOCTYPE", CLASS_VOID },
53: { "editor", CLASS_LINE },
54: { "email", CLASS_ENCL },
55: { "emphasis", CLASS_LINE },
56: { "!ENTITY", CLASS_VOID },
57: { "entry", CLASS_ENCL },
58: { "envar", CLASS_LINE },
59: { "errorname", CLASS_LINE },
60: { "fieldsynopsis", CLASS_TRANS },
61: { "filename", CLASS_LINE },
62: { "firstterm", CLASS_LINE },
1.17 schwarze 63: { "footnote", CLASS_BLOCK },
1.10 schwarze 64: { "funcdef", CLASS_BLOCK },
65: { "funcprototype", CLASS_BLOCK },
66: { "funcsynopsis", CLASS_TRANS },
67: { "funcsynopsisinfo", CLASS_LINE },
68: { "function", CLASS_LINE },
69: { "glossterm", CLASS_LINE },
70: { "group", CLASS_ENCL },
1.18 schwarze 71: { "imagedata", CLASS_TEXT },
1.10 schwarze 72: { "xi:include", CLASS_VOID },
73: { "index", CLASS_TRANS },
74: { "info", CLASS_TRANS },
75: { "informalequation", CLASS_BLOCK },
76: { "inlineequation", CLASS_BLOCK },
77: { "itemizedlist", CLASS_BLOCK },
78: { "keysym", CLASS_LINE },
79: { "legalnotice", CLASS_BLOCK },
80: { "link", CLASS_ENCL },
81: { "listitem", CLASS_TRANS },
82: { "literal", CLASS_ENCL },
1.21 schwarze 83: { "literallayout", CLASS_NOFILL },
1.10 schwarze 84: { "manvolnum", CLASS_TRANS },
85: { "markup", CLASS_LINE },
86: { "member", CLASS_LINE },
87: { "mml:math", CLASS_LINE },
88: { "mml:mfenced", CLASS_LINE },
89: { "mml:mfrac", CLASS_LINE },
90: { "mml:mi", CLASS_LINE },
91: { "mml:mn", CLASS_LINE },
92: { "mml:mo", CLASS_LINE },
93: { "mml:mrow", CLASS_LINE },
94: { "mml:msub", CLASS_LINE },
95: { "mml:msup", CLASS_LINE },
96: { "modifier", CLASS_LINE },
97: { "note", CLASS_BLOCK },
1.19 schwarze 98: { "olink", CLASS_ENCL },
1.10 schwarze 99: { "option", CLASS_LINE },
100: { "orderedlist", CLASS_BLOCK },
101: { "para", CLASS_BLOCK },
102: { "paramdef", CLASS_LINE },
103: { "parameter", CLASS_LINE },
104: { "personname", CLASS_TRANS },
105: { "preface", CLASS_BLOCK },
1.22 schwarze 106: { "productname", CLASS_LINE },
1.21 schwarze 107: { "programlisting", CLASS_NOFILL },
1.10 schwarze 108: { "prompt", CLASS_TRANS },
1.12 schwarze 109: { "pubdate", CLASS_TRANS },
1.10 schwarze 110: { "quote", CLASS_ENCL },
111: { "refclass", CLASS_TRANS },
112: { "refdescriptor", CLASS_TRANS },
113: { "refentry", CLASS_TRANS },
114: { "refentryinfo", CLASS_VOID },
115: { "refentrytitle", CLASS_TRANS },
116: { "refmeta", CLASS_TRANS },
117: { "refmetainfo", CLASS_TRANS },
118: { "refmiscinfo", CLASS_TRANS },
119: { "refname", CLASS_LINE },
120: { "refnamediv", CLASS_BLOCK },
121: { "refpurpose", CLASS_LINE },
122: { "refsynopsisdiv", CLASS_BLOCK },
123: { "replaceable", CLASS_LINE },
124: { "row", CLASS_BLOCK },
125: { "sbr", CLASS_BLOCK },
1.21 schwarze 126: { "screen", CLASS_NOFILL },
1.10 schwarze 127: { "section", CLASS_BLOCK },
128: { "simplelist", CLASS_TRANS },
1.15 schwarze 129: { "simplesect", CLASS_BLOCK },
1.10 schwarze 130: { "spanspec", CLASS_TRANS },
1.16 schwarze 131: { "subscript", CLASS_TEXT },
1.15 schwarze 132: { "subtitle", CLASS_BLOCK },
1.16 schwarze 133: { "superscript", CLASS_TEXT },
1.21 schwarze 134: { "synopsis", CLASS_NOFILL },
1.13 schwarze 135: { "systemitem", CLASS_LINE },
1.10 schwarze 136: { "table", CLASS_TRANS },
137: { "tbody", CLASS_TRANS },
138: { "term", CLASS_LINE },
139: { "tfoot", CLASS_TRANS },
140: { "tgroup", CLASS_BLOCK },
141: { "thead", CLASS_TRANS },
142: { "tip", CLASS_BLOCK },
143: { "title", CLASS_BLOCK },
144: { "type", CLASS_LINE },
145: { "variablelist", CLASS_BLOCK },
146: { "varlistentry", CLASS_BLOCK },
147: { "varname", CLASS_LINE },
1.23 schwarze 148: { "void", CLASS_TEXT },
1.10 schwarze 149: { "warning", CLASS_BLOCK },
150: { "wordasword", CLASS_TRANS },
1.20 schwarze 151: { "xref", CLASS_LINE },
1.27 ! schwarze 152: { "year", CLASS_TRANS },
1.10 schwarze 153: { "[UNKNOWN]", CLASS_VOID },
1.11 schwarze 154: { "(t)", CLASS_TEXT },
155: { "(e)", CLASS_TEXT }
1.10 schwarze 156: };
157:
1.1 schwarze 158: static const char *const attrkeys[ATTRKEY__MAX] = {
159: "choice",
160: "class",
161: "close",
1.3 schwarze 162: "cols",
1.5 schwarze 163: "DEFINITION",
1.4 schwarze 164: "endterm",
1.18 schwarze 165: "entityref",
166: "fileref",
1.6 schwarze 167: "href",
1.1 schwarze 168: "id",
169: "linkend",
1.19 schwarze 170: "localinfo",
1.5 schwarze 171: "NAME",
1.1 schwarze 172: "open",
1.5 schwarze 173: "PUBLIC",
1.4 schwarze 174: "rep",
1.5 schwarze 175: "SYSTEM",
1.19 schwarze 176: "targetdoc",
177: "targetptr",
1.4 schwarze 178: "url",
179: "xlink:href"
1.1 schwarze 180: };
181:
182: static const char *const attrvals[ATTRVAL__MAX] = {
1.13 schwarze 183: "event",
184: "ipaddress",
1.1 schwarze 185: "monospaced",
186: "norepeat",
187: "opt",
188: "plain",
189: "repeat",
1.13 schwarze 190: "req",
191: "systemname"
1.1 schwarze 192: };
193:
194: enum attrkey
195: attrkey_parse(const char *name)
196: {
197: enum attrkey key;
198:
199: for (key = 0; key < ATTRKEY__MAX; key++)
200: if (strcmp(name, attrkeys[key]) == 0)
201: break;
202: return key;
203: }
204:
1.11 schwarze 205: const char *
206: attrkey_name(enum attrkey key)
207: {
208: return attrkeys[key];
209: }
210:
1.1 schwarze 211: enum attrval
212: attrval_parse(const char *name)
213: {
214: enum attrval val;
215:
216: for (val = 0; val < ATTRVAL__MAX; val++)
217: if (strcmp(name, attrvals[val]) == 0)
218: break;
219: return val;
1.11 schwarze 220: }
221:
222: const char *
223: attr_getval(const struct pattr *a)
224: {
225: return a->val == ATTRVAL__MAX ? a->rawval : attrvals[a->val];
1.10 schwarze 226: }
227:
228: enum nodeid
229: pnode_parse(const char *name)
230: {
231: enum nodeid node;
232:
233: for (node = 0; node < NODE_UNKNOWN; node++)
234: if (strcmp(name, properties[node].name) == 0)
235: break;
236: return node;
237: }
238:
239: const char *
240: pnode_name(enum nodeid node)
241: {
242: assert(node < NODE_IGNORE);
243: return properties[node].name;
244: }
245:
246: enum nodeclass
247: pnode_class(enum nodeid node)
248: {
249: assert(node < NODE_IGNORE);
250: return properties[node].class;
1.9 schwarze 251: }
252:
253: struct pnode *
254: pnode_alloc(struct pnode *np)
255: {
256: struct pnode *n;
257:
1.24 schwarze 258: n = xcalloc(1, sizeof(*n));
259: TAILQ_INIT(&n->childq);
260: TAILQ_INIT(&n->attrq);
261: if ((n->parent = np) != NULL)
262: TAILQ_INSERT_TAIL(&np->childq, n, child);
1.25 schwarze 263: return n;
264: }
265:
266: struct pnode *
267: pnode_alloc_text(struct pnode *np, const char *text)
268: {
269: struct pnode *n;
270:
271: n = pnode_alloc(np);
272: n->node = NODE_TEXT;
273: n->b = xstrdup(text);
1.9 schwarze 274: return n;
1.1 schwarze 275: }
276:
277: /*
278: * Recursively free a node (NULL is ok).
279: */
280: static void
1.7 schwarze 281: pnode_free(struct pnode *n)
1.1 schwarze 282: {
1.7 schwarze 283: struct pnode *nc;
284: struct pattr *a;
1.1 schwarze 285:
1.7 schwarze 286: if (n == NULL)
1.1 schwarze 287: return;
288:
1.7 schwarze 289: while ((nc = TAILQ_FIRST(&n->childq)) != NULL) {
290: TAILQ_REMOVE(&n->childq, nc, child);
291: pnode_free(nc);
1.1 schwarze 292: }
1.7 schwarze 293: while ((a = TAILQ_FIRST(&n->attrq)) != NULL) {
294: TAILQ_REMOVE(&n->attrq, a, child);
295: free(a->rawval);
296: free(a);
1.1 schwarze 297: }
1.8 schwarze 298: free(n->b);
1.7 schwarze 299: free(n);
1.1 schwarze 300: }
301:
302: /*
303: * Unlink a node from its parent and pnode_free() it.
304: */
305: void
1.7 schwarze 306: pnode_unlink(struct pnode *n)
1.1 schwarze 307: {
1.7 schwarze 308: if (n == NULL)
1.1 schwarze 309: return;
1.7 schwarze 310: if (n->parent != NULL)
311: TAILQ_REMOVE(&n->parent->childq, n, child);
312: pnode_free(n);
1.1 schwarze 313: }
314:
315: /*
316: * Unlink all children of a node and pnode_free() them.
317: */
318: void
1.7 schwarze 319: pnode_unlinksub(struct pnode *n)
1.1 schwarze 320: {
1.7 schwarze 321: while (TAILQ_EMPTY(&n->childq) == 0)
322: pnode_unlink(TAILQ_FIRST(&n->childq));
1.1 schwarze 323: }
324:
325: /*
326: * Retrieve an enumeration attribute from a node.
327: * Return ATTRVAL__MAX if the node has no such attribute.
328: */
329: enum attrval
1.7 schwarze 330: pnode_getattr(struct pnode *n, enum attrkey key)
1.1 schwarze 331: {
1.7 schwarze 332: struct pattr *a;
1.1 schwarze 333:
1.7 schwarze 334: if (n == NULL)
1.1 schwarze 335: return ATTRVAL__MAX;
1.7 schwarze 336: TAILQ_FOREACH(a, &n->attrq, child)
337: if (a->key == key)
338: return a->val;
1.1 schwarze 339: return ATTRVAL__MAX;
340: }
341:
342: /*
343: * Retrieve an attribute string from a node.
344: * Return defval if the node has no such attribute.
345: */
346: const char *
1.7 schwarze 347: pnode_getattr_raw(struct pnode *n, enum attrkey key, const char *defval)
1.1 schwarze 348: {
1.7 schwarze 349: struct pattr *a;
1.1 schwarze 350:
1.7 schwarze 351: if (n == NULL)
1.1 schwarze 352: return defval;
1.7 schwarze 353: TAILQ_FOREACH(a, &n->attrq, child)
354: if (a->key == key)
355: return a->val != ATTRVAL__MAX ? attrvals[a->val] :
356: a->rawval != NULL ? a->rawval : defval;
1.1 schwarze 357: return defval;
358: }
359:
360: /*
361: * Recursively search and return the first instance of "node".
362: */
363: struct pnode *
1.7 schwarze 364: pnode_findfirst(struct pnode *n, enum nodeid node)
1.1 schwarze 365: {
1.7 schwarze 366: struct pnode *nc, *res;
1.1 schwarze 367:
1.12 schwarze 368: if (n == NULL)
369: return NULL;
1.7 schwarze 370: if (n->node == node)
371: return n;
372: TAILQ_FOREACH(nc, &n->childq, child)
373: if ((res = pnode_findfirst(nc, node)) != NULL)
1.1 schwarze 374: return res;
375: return NULL;
1.12 schwarze 376: }
377:
378: /*
379: * Like pnode_findfirst(), but also take the node out of the tree.
380: */
381: struct pnode *
382: pnode_takefirst(struct pnode *n, enum nodeid node)
383: {
384: struct pnode *nc;
385:
386: if ((nc = pnode_findfirst(n, node)) != NULL && nc->parent != NULL)
387: TAILQ_REMOVE(&nc->parent->childq, nc, child);
388: return nc;
1.1 schwarze 389: }
CVSweb