Annotation of docbook2mdoc/node.c, Revision 1.12
1.12 ! schwarze 1: /* $Id: node.c,v 1.11 2019/04/12 19:14:50 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:
22: #include "node.h"
23:
24: /*
25: * The implementation of the DocBook syntax tree.
26: */
27:
1.10 schwarze 28: struct nodeprop {
29: const char *name;
30: enum nodeclass class;
31: };
32:
33: static const struct nodeprop properties[] = {
34: { "affiliation", CLASS_TRANS },
35: { "appendix", CLASS_BLOCK },
36: { "application", CLASS_LINE },
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 },
50: { "copyright", CLASS_TRANS },
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 },
63: { "footnote", CLASS_TRANS },
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 },
71: { "holder", CLASS_TRANS },
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 },
83: { "literallayout", CLASS_BLOCK },
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 },
98: { "option", CLASS_LINE },
99: { "orderedlist", CLASS_BLOCK },
100: { "orgname", CLASS_TRANS },
101: { "para", CLASS_BLOCK },
102: { "paramdef", CLASS_LINE },
103: { "parameter", CLASS_LINE },
104: { "personname", CLASS_TRANS },
105: { "preface", CLASS_BLOCK },
106: { "programlisting", CLASS_BLOCK },
107: { "prompt", CLASS_TRANS },
1.12 ! schwarze 108: { "pubdate", CLASS_TRANS },
1.10 schwarze 109: { "quote", CLASS_ENCL },
110: { "refclass", CLASS_TRANS },
111: { "refdescriptor", CLASS_TRANS },
112: { "refentry", CLASS_TRANS },
113: { "refentryinfo", CLASS_VOID },
114: { "refentrytitle", CLASS_TRANS },
115: { "refmeta", CLASS_TRANS },
116: { "refmetainfo", CLASS_TRANS },
117: { "refmiscinfo", CLASS_TRANS },
118: { "refname", CLASS_LINE },
119: { "refnamediv", CLASS_BLOCK },
120: { "refpurpose", CLASS_LINE },
121: { "refsynopsisdiv", CLASS_BLOCK },
122: { "releaseinfo", CLASS_TRANS },
123: { "replaceable", CLASS_LINE },
124: { "row", CLASS_BLOCK },
125: { "sbr", CLASS_BLOCK },
126: { "screen", CLASS_BLOCK },
127: { "section", CLASS_BLOCK },
128: { "simplelist", CLASS_TRANS },
129: { "spanspec", CLASS_TRANS },
130: { "subtitle", CLASS_TRANS },
131: { "synopsis", CLASS_BLOCK },
132: { "table", CLASS_TRANS },
133: { "tbody", CLASS_TRANS },
134: { "term", CLASS_LINE },
135: { "tfoot", CLASS_TRANS },
136: { "tgroup", CLASS_BLOCK },
137: { "thead", CLASS_TRANS },
138: { "tip", CLASS_BLOCK },
139: { "title", CLASS_BLOCK },
140: { "type", CLASS_LINE },
141: { "variablelist", CLASS_BLOCK },
142: { "varlistentry", CLASS_BLOCK },
143: { "varname", CLASS_LINE },
144: { "warning", CLASS_BLOCK },
145: { "wordasword", CLASS_TRANS },
146: { "year", CLASS_TRANS },
147: { "[UNKNOWN]", CLASS_VOID },
1.11 schwarze 148: { "(t)", CLASS_TEXT },
149: { "(e)", CLASS_TEXT }
1.10 schwarze 150: };
151:
1.1 schwarze 152: static const char *const attrkeys[ATTRKEY__MAX] = {
153: "choice",
154: "class",
155: "close",
1.3 schwarze 156: "cols",
1.5 schwarze 157: "DEFINITION",
1.4 schwarze 158: "endterm",
1.6 schwarze 159: "href",
1.1 schwarze 160: "id",
161: "linkend",
1.5 schwarze 162: "NAME",
1.1 schwarze 163: "open",
1.5 schwarze 164: "PUBLIC",
1.4 schwarze 165: "rep",
1.5 schwarze 166: "SYSTEM",
1.4 schwarze 167: "url",
168: "xlink:href"
1.1 schwarze 169: };
170:
171: static const char *const attrvals[ATTRVAL__MAX] = {
172: "monospaced",
173: "norepeat",
174: "opt",
175: "plain",
176: "repeat",
177: "req"
178: };
179:
180: enum attrkey
181: attrkey_parse(const char *name)
182: {
183: enum attrkey key;
184:
185: for (key = 0; key < ATTRKEY__MAX; key++)
186: if (strcmp(name, attrkeys[key]) == 0)
187: break;
188: return key;
189: }
190:
1.11 schwarze 191: const char *
192: attrkey_name(enum attrkey key)
193: {
194: return attrkeys[key];
195: }
196:
1.1 schwarze 197: enum attrval
198: attrval_parse(const char *name)
199: {
200: enum attrval val;
201:
202: for (val = 0; val < ATTRVAL__MAX; val++)
203: if (strcmp(name, attrvals[val]) == 0)
204: break;
205: return val;
1.11 schwarze 206: }
207:
208: const char *
209: attr_getval(const struct pattr *a)
210: {
211: return a->val == ATTRVAL__MAX ? a->rawval : attrvals[a->val];
1.10 schwarze 212: }
213:
214: enum nodeid
215: pnode_parse(const char *name)
216: {
217: enum nodeid node;
218:
219: for (node = 0; node < NODE_UNKNOWN; node++)
220: if (strcmp(name, properties[node].name) == 0)
221: break;
222: return node;
223: }
224:
225: const char *
226: pnode_name(enum nodeid node)
227: {
228: assert(node < NODE_IGNORE);
229: return properties[node].name;
230: }
231:
232: enum nodeclass
233: pnode_class(enum nodeid node)
234: {
235: assert(node < NODE_IGNORE);
236: return properties[node].class;
1.9 schwarze 237: }
238:
239: struct pnode *
240: pnode_alloc(struct pnode *np)
241: {
242: struct pnode *n;
243:
244: if ((n = calloc(1, sizeof(*n))) != NULL) {
245: TAILQ_INIT(&n->childq);
246: TAILQ_INIT(&n->attrq);
247: if ((n->parent = np) != NULL)
248: TAILQ_INSERT_TAIL(&np->childq, n, child);
249: }
250: return n;
1.1 schwarze 251: }
252:
253: /*
254: * Recursively free a node (NULL is ok).
255: */
256: static void
1.7 schwarze 257: pnode_free(struct pnode *n)
1.1 schwarze 258: {
1.7 schwarze 259: struct pnode *nc;
260: struct pattr *a;
1.1 schwarze 261:
1.7 schwarze 262: if (n == NULL)
1.1 schwarze 263: return;
264:
1.7 schwarze 265: while ((nc = TAILQ_FIRST(&n->childq)) != NULL) {
266: TAILQ_REMOVE(&n->childq, nc, child);
267: pnode_free(nc);
1.1 schwarze 268: }
1.7 schwarze 269: while ((a = TAILQ_FIRST(&n->attrq)) != NULL) {
270: TAILQ_REMOVE(&n->attrq, a, child);
271: free(a->rawval);
272: free(a);
1.1 schwarze 273: }
1.8 schwarze 274: free(n->b);
1.7 schwarze 275: free(n);
1.1 schwarze 276: }
277:
278: /*
279: * Unlink a node from its parent and pnode_free() it.
280: */
281: void
1.7 schwarze 282: pnode_unlink(struct pnode *n)
1.1 schwarze 283: {
1.7 schwarze 284: if (n == NULL)
1.1 schwarze 285: return;
1.7 schwarze 286: if (n->parent != NULL)
287: TAILQ_REMOVE(&n->parent->childq, n, child);
288: pnode_free(n);
1.1 schwarze 289: }
290:
291: /*
292: * Unlink all children of a node and pnode_free() them.
293: */
294: void
1.7 schwarze 295: pnode_unlinksub(struct pnode *n)
1.1 schwarze 296: {
1.7 schwarze 297: while (TAILQ_EMPTY(&n->childq) == 0)
298: pnode_unlink(TAILQ_FIRST(&n->childq));
1.1 schwarze 299: }
300:
301: /*
302: * Retrieve an enumeration attribute from a node.
303: * Return ATTRVAL__MAX if the node has no such attribute.
304: */
305: enum attrval
1.7 schwarze 306: pnode_getattr(struct pnode *n, enum attrkey key)
1.1 schwarze 307: {
1.7 schwarze 308: struct pattr *a;
1.1 schwarze 309:
1.7 schwarze 310: if (n == NULL)
1.1 schwarze 311: return ATTRVAL__MAX;
1.7 schwarze 312: TAILQ_FOREACH(a, &n->attrq, child)
313: if (a->key == key)
314: return a->val;
1.1 schwarze 315: return ATTRVAL__MAX;
316: }
317:
318: /*
319: * Retrieve an attribute string from a node.
320: * Return defval if the node has no such attribute.
321: */
322: const char *
1.7 schwarze 323: pnode_getattr_raw(struct pnode *n, enum attrkey key, const char *defval)
1.1 schwarze 324: {
1.7 schwarze 325: struct pattr *a;
1.1 schwarze 326:
1.7 schwarze 327: if (n == NULL)
1.1 schwarze 328: return defval;
1.7 schwarze 329: TAILQ_FOREACH(a, &n->attrq, child)
330: if (a->key == key)
331: return a->val != ATTRVAL__MAX ? attrvals[a->val] :
332: a->rawval != NULL ? a->rawval : defval;
1.1 schwarze 333: return defval;
334: }
335:
336: /*
337: * Recursively search and return the first instance of "node".
338: */
339: struct pnode *
1.7 schwarze 340: pnode_findfirst(struct pnode *n, enum nodeid node)
1.1 schwarze 341: {
1.7 schwarze 342: struct pnode *nc, *res;
1.1 schwarze 343:
1.12 ! schwarze 344: if (n == NULL)
! 345: return NULL;
1.7 schwarze 346: if (n->node == node)
347: return n;
348: TAILQ_FOREACH(nc, &n->childq, child)
349: if ((res = pnode_findfirst(nc, node)) != NULL)
1.1 schwarze 350: return res;
351: return NULL;
1.12 ! schwarze 352: }
! 353:
! 354: /*
! 355: * Like pnode_findfirst(), but also take the node out of the tree.
! 356: */
! 357: struct pnode *
! 358: pnode_takefirst(struct pnode *n, enum nodeid node)
! 359: {
! 360: struct pnode *nc;
! 361:
! 362: if ((nc = pnode_findfirst(n, node)) != NULL && nc->parent != NULL)
! 363: TAILQ_REMOVE(&nc->parent->childq, nc, child);
! 364: return nc;
1.1 schwarze 365: }
CVSweb