Annotation of docbook2mdoc/node.c, Revision 1.26
1.26 ! schwarze 1: /* $Id: node.c,v 1.25 2019/04/28 15:32:05 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.10 schwarze 152: { "[UNKNOWN]", CLASS_VOID },
1.11 schwarze 153: { "(t)", CLASS_TEXT },
154: { "(e)", CLASS_TEXT }
1.10 schwarze 155: };
156:
1.1 schwarze 157: static const char *const attrkeys[ATTRKEY__MAX] = {
158: "choice",
159: "class",
160: "close",
1.3 schwarze 161: "cols",
1.5 schwarze 162: "DEFINITION",
1.4 schwarze 163: "endterm",
1.18 schwarze 164: "entityref",
165: "fileref",
1.6 schwarze 166: "href",
1.1 schwarze 167: "id",
168: "linkend",
1.19 schwarze 169: "localinfo",
1.5 schwarze 170: "NAME",
1.1 schwarze 171: "open",
1.5 schwarze 172: "PUBLIC",
1.4 schwarze 173: "rep",
1.5 schwarze 174: "SYSTEM",
1.19 schwarze 175: "targetdoc",
176: "targetptr",
1.4 schwarze 177: "url",
178: "xlink:href"
1.1 schwarze 179: };
180:
181: static const char *const attrvals[ATTRVAL__MAX] = {
1.13 schwarze 182: "event",
183: "ipaddress",
1.1 schwarze 184: "monospaced",
185: "norepeat",
186: "opt",
187: "plain",
188: "repeat",
1.13 schwarze 189: "req",
190: "systemname"
1.1 schwarze 191: };
192:
193: enum attrkey
194: attrkey_parse(const char *name)
195: {
196: enum attrkey key;
197:
198: for (key = 0; key < ATTRKEY__MAX; key++)
199: if (strcmp(name, attrkeys[key]) == 0)
200: break;
201: return key;
202: }
203:
1.11 schwarze 204: const char *
205: attrkey_name(enum attrkey key)
206: {
207: return attrkeys[key];
208: }
209:
1.1 schwarze 210: enum attrval
211: attrval_parse(const char *name)
212: {
213: enum attrval val;
214:
215: for (val = 0; val < ATTRVAL__MAX; val++)
216: if (strcmp(name, attrvals[val]) == 0)
217: break;
218: return val;
1.11 schwarze 219: }
220:
221: const char *
222: attr_getval(const struct pattr *a)
223: {
224: return a->val == ATTRVAL__MAX ? a->rawval : attrvals[a->val];
1.10 schwarze 225: }
226:
227: enum nodeid
228: pnode_parse(const char *name)
229: {
230: enum nodeid node;
231:
232: for (node = 0; node < NODE_UNKNOWN; node++)
233: if (strcmp(name, properties[node].name) == 0)
234: break;
235: return node;
236: }
237:
238: const char *
239: pnode_name(enum nodeid node)
240: {
241: assert(node < NODE_IGNORE);
242: return properties[node].name;
243: }
244:
245: enum nodeclass
246: pnode_class(enum nodeid node)
247: {
248: assert(node < NODE_IGNORE);
249: return properties[node].class;
1.9 schwarze 250: }
251:
252: struct pnode *
253: pnode_alloc(struct pnode *np)
254: {
255: struct pnode *n;
256:
1.24 schwarze 257: n = xcalloc(1, sizeof(*n));
258: TAILQ_INIT(&n->childq);
259: TAILQ_INIT(&n->attrq);
260: if ((n->parent = np) != NULL)
261: TAILQ_INSERT_TAIL(&np->childq, n, child);
1.25 schwarze 262: return n;
263: }
264:
265: struct pnode *
266: pnode_alloc_text(struct pnode *np, const char *text)
267: {
268: struct pnode *n;
269:
270: n = pnode_alloc(np);
271: n->node = NODE_TEXT;
272: n->b = xstrdup(text);
1.9 schwarze 273: return n;
1.1 schwarze 274: }
275:
276: /*
277: * Recursively free a node (NULL is ok).
278: */
279: static void
1.7 schwarze 280: pnode_free(struct pnode *n)
1.1 schwarze 281: {
1.7 schwarze 282: struct pnode *nc;
283: struct pattr *a;
1.1 schwarze 284:
1.7 schwarze 285: if (n == NULL)
1.1 schwarze 286: return;
287:
1.7 schwarze 288: while ((nc = TAILQ_FIRST(&n->childq)) != NULL) {
289: TAILQ_REMOVE(&n->childq, nc, child);
290: pnode_free(nc);
1.1 schwarze 291: }
1.7 schwarze 292: while ((a = TAILQ_FIRST(&n->attrq)) != NULL) {
293: TAILQ_REMOVE(&n->attrq, a, child);
294: free(a->rawval);
295: free(a);
1.1 schwarze 296: }
1.8 schwarze 297: free(n->b);
1.7 schwarze 298: free(n);
1.1 schwarze 299: }
300:
301: /*
302: * Unlink a node from its parent and pnode_free() it.
303: */
304: void
1.7 schwarze 305: pnode_unlink(struct pnode *n)
1.1 schwarze 306: {
1.7 schwarze 307: if (n == NULL)
1.1 schwarze 308: return;
1.7 schwarze 309: if (n->parent != NULL)
310: TAILQ_REMOVE(&n->parent->childq, n, child);
311: pnode_free(n);
1.1 schwarze 312: }
313:
314: /*
315: * Unlink all children of a node and pnode_free() them.
316: */
317: void
1.7 schwarze 318: pnode_unlinksub(struct pnode *n)
1.1 schwarze 319: {
1.7 schwarze 320: while (TAILQ_EMPTY(&n->childq) == 0)
321: pnode_unlink(TAILQ_FIRST(&n->childq));
1.1 schwarze 322: }
323:
324: /*
325: * Retrieve an enumeration attribute from a node.
326: * Return ATTRVAL__MAX if the node has no such attribute.
327: */
328: enum attrval
1.7 schwarze 329: pnode_getattr(struct pnode *n, enum attrkey key)
1.1 schwarze 330: {
1.7 schwarze 331: struct pattr *a;
1.1 schwarze 332:
1.7 schwarze 333: if (n == NULL)
1.1 schwarze 334: return ATTRVAL__MAX;
1.7 schwarze 335: TAILQ_FOREACH(a, &n->attrq, child)
336: if (a->key == key)
337: return a->val;
1.1 schwarze 338: return ATTRVAL__MAX;
339: }
340:
341: /*
342: * Retrieve an attribute string from a node.
343: * Return defval if the node has no such attribute.
344: */
345: const char *
1.7 schwarze 346: pnode_getattr_raw(struct pnode *n, enum attrkey key, const char *defval)
1.1 schwarze 347: {
1.7 schwarze 348: struct pattr *a;
1.1 schwarze 349:
1.7 schwarze 350: if (n == NULL)
1.1 schwarze 351: return defval;
1.7 schwarze 352: TAILQ_FOREACH(a, &n->attrq, child)
353: if (a->key == key)
354: return a->val != ATTRVAL__MAX ? attrvals[a->val] :
355: a->rawval != NULL ? a->rawval : defval;
1.1 schwarze 356: return defval;
357: }
358:
359: /*
360: * Recursively search and return the first instance of "node".
361: */
362: struct pnode *
1.7 schwarze 363: pnode_findfirst(struct pnode *n, enum nodeid node)
1.1 schwarze 364: {
1.7 schwarze 365: struct pnode *nc, *res;
1.1 schwarze 366:
1.12 schwarze 367: if (n == NULL)
368: return NULL;
1.7 schwarze 369: if (n->node == node)
370: return n;
371: TAILQ_FOREACH(nc, &n->childq, child)
372: if ((res = pnode_findfirst(nc, node)) != NULL)
1.1 schwarze 373: return res;
374: return NULL;
1.12 schwarze 375: }
376:
377: /*
378: * Like pnode_findfirst(), but also take the node out of the tree.
379: */
380: struct pnode *
381: pnode_takefirst(struct pnode *n, enum nodeid node)
382: {
383: struct pnode *nc;
384:
385: if ((nc = pnode_findfirst(n, node)) != NULL && nc->parent != NULL)
386: TAILQ_REMOVE(&nc->parent->childq, nc, child);
387: return nc;
1.1 schwarze 388: }
CVSweb