[BACK]Return to node.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / docbook2mdoc

Annotation of docbook2mdoc/node.c, Revision 1.14

1.14    ! schwarze    1: /* $Id: node.c,v 1.13 2019/04/13 15:54:45 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:        { "arg",                CLASS_ENCL },
                     37:        { "author",             CLASS_LINE },
                     38:        { "authorgroup",        CLASS_BLOCK },
                     39:        { "blockquote",         CLASS_BLOCK },
                     40:        { "bookinfo",           CLASS_BLOCK },
                     41:        { "caution",            CLASS_BLOCK },
                     42:        { "citerefentry",       CLASS_LINE },
                     43:        { "citetitle",          CLASS_LINE },
                     44:        { "cmdsynopsis",        CLASS_TRANS },
                     45:        { "colspec",            CLASS_VOID },
                     46:        { "command",            CLASS_LINE },
                     47:        { "constant",           CLASS_LINE },
                     48:        { "contrib",            CLASS_TRANS },
                     49:        { "copyright",          CLASS_TRANS },
                     50:        { "date",               CLASS_TRANS },
                     51:        { "!DOCTYPE",           CLASS_VOID },
                     52:        { "editor",             CLASS_LINE },
                     53:        { "email",              CLASS_ENCL },
                     54:        { "emphasis",           CLASS_LINE },
                     55:        { "!ENTITY",            CLASS_VOID },
                     56:        { "entry",              CLASS_ENCL },
                     57:        { "envar",              CLASS_LINE },
                     58:        { "errorname",          CLASS_LINE },
                     59:        { "fieldsynopsis",      CLASS_TRANS },
                     60:        { "filename",           CLASS_LINE },
                     61:        { "firstterm",          CLASS_LINE },
                     62:        { "footnote",           CLASS_TRANS },
                     63:        { "funcdef",            CLASS_BLOCK },
                     64:        { "funcprototype",      CLASS_BLOCK },
                     65:        { "funcsynopsis",       CLASS_TRANS },
                     66:        { "funcsynopsisinfo",   CLASS_LINE },
                     67:        { "function",           CLASS_LINE },
                     68:        { "glossterm",          CLASS_LINE },
                     69:        { "group",              CLASS_ENCL },
                     70:        { "holder",             CLASS_TRANS },
                     71:        { "xi:include",         CLASS_VOID },
                     72:        { "index",              CLASS_TRANS },
                     73:        { "info",               CLASS_TRANS },
                     74:        { "informalequation",   CLASS_BLOCK },
                     75:        { "inlineequation",     CLASS_BLOCK },
                     76:        { "itemizedlist",       CLASS_BLOCK },
                     77:        { "keysym",             CLASS_LINE },
                     78:        { "legalnotice",        CLASS_BLOCK },
                     79:        { "link",               CLASS_ENCL },
                     80:        { "listitem",           CLASS_TRANS },
                     81:        { "literal",            CLASS_ENCL },
                     82:        { "literallayout",      CLASS_BLOCK },
                     83:        { "manvolnum",          CLASS_TRANS },
                     84:        { "markup",             CLASS_LINE },
                     85:        { "member",             CLASS_LINE },
                     86:        { "mml:math",           CLASS_LINE },
                     87:        { "mml:mfenced",        CLASS_LINE },
                     88:        { "mml:mfrac",          CLASS_LINE },
                     89:        { "mml:mi",             CLASS_LINE },
                     90:        { "mml:mn",             CLASS_LINE },
                     91:        { "mml:mo",             CLASS_LINE },
                     92:        { "mml:mrow",           CLASS_LINE },
                     93:        { "mml:msub",           CLASS_LINE },
                     94:        { "mml:msup",           CLASS_LINE },
                     95:        { "modifier",           CLASS_LINE },
                     96:        { "note",               CLASS_BLOCK },
                     97:        { "option",             CLASS_LINE },
                     98:        { "orderedlist",        CLASS_BLOCK },
                     99:        { "orgname",            CLASS_TRANS },
                    100:        { "para",               CLASS_BLOCK },
                    101:        { "paramdef",           CLASS_LINE },
                    102:        { "parameter",          CLASS_LINE },
                    103:        { "personname",         CLASS_TRANS },
                    104:        { "preface",            CLASS_BLOCK },
                    105:        { "programlisting",     CLASS_BLOCK },
                    106:        { "prompt",             CLASS_TRANS },
1.12      schwarze  107:        { "pubdate",            CLASS_TRANS },
1.10      schwarze  108:        { "quote",              CLASS_ENCL },
                    109:        { "refclass",           CLASS_TRANS },
                    110:        { "refdescriptor",      CLASS_TRANS },
                    111:        { "refentry",           CLASS_TRANS },
                    112:        { "refentryinfo",       CLASS_VOID },
                    113:        { "refentrytitle",      CLASS_TRANS },
                    114:        { "refmeta",            CLASS_TRANS },
                    115:        { "refmetainfo",        CLASS_TRANS },
                    116:        { "refmiscinfo",        CLASS_TRANS },
                    117:        { "refname",            CLASS_LINE },
                    118:        { "refnamediv",         CLASS_BLOCK },
                    119:        { "refpurpose",         CLASS_LINE },
                    120:        { "refsynopsisdiv",     CLASS_BLOCK },
                    121:        { "releaseinfo",        CLASS_TRANS },
                    122:        { "replaceable",        CLASS_LINE },
                    123:        { "row",                CLASS_BLOCK },
                    124:        { "sbr",                CLASS_BLOCK },
                    125:        { "screen",             CLASS_BLOCK },
                    126:        { "section",            CLASS_BLOCK },
                    127:        { "simplelist",         CLASS_TRANS },
                    128:        { "spanspec",           CLASS_TRANS },
                    129:        { "subtitle",           CLASS_TRANS },
                    130:        { "synopsis",           CLASS_BLOCK },
1.13      schwarze  131:        { "systemitem",         CLASS_LINE },
1.10      schwarze  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] = {
1.13      schwarze  172:        "event",
                    173:        "ipaddress",
1.1       schwarze  174:        "monospaced",
                    175:        "norepeat",
                    176:        "opt",
                    177:        "plain",
                    178:        "repeat",
1.13      schwarze  179:        "req",
                    180:        "systemname"
1.1       schwarze  181: };
                    182:
                    183: enum attrkey
                    184: attrkey_parse(const char *name)
                    185: {
                    186:        enum attrkey     key;
                    187:
                    188:        for (key = 0; key < ATTRKEY__MAX; key++)
                    189:                if (strcmp(name, attrkeys[key]) == 0)
                    190:                        break;
                    191:        return key;
                    192: }
                    193:
1.11      schwarze  194: const char *
                    195: attrkey_name(enum attrkey key)
                    196: {
                    197:        return attrkeys[key];
                    198: }
                    199:
1.1       schwarze  200: enum attrval
                    201: attrval_parse(const char *name)
                    202: {
                    203:        enum attrval     val;
                    204:
                    205:        for (val = 0; val < ATTRVAL__MAX; val++)
                    206:                if (strcmp(name, attrvals[val]) == 0)
                    207:                        break;
                    208:        return val;
1.11      schwarze  209: }
                    210:
                    211: const char *
                    212: attr_getval(const struct pattr *a)
                    213: {
                    214:        return a->val == ATTRVAL__MAX ? a->rawval : attrvals[a->val];
1.10      schwarze  215: }
                    216:
                    217: enum nodeid
                    218: pnode_parse(const char *name)
                    219: {
                    220:        enum nodeid      node;
                    221:
                    222:        for (node = 0; node < NODE_UNKNOWN; node++)
                    223:                if (strcmp(name, properties[node].name) == 0)
                    224:                        break;
                    225:        return node;
                    226: }
                    227:
                    228: const char *
                    229: pnode_name(enum nodeid node)
                    230: {
                    231:        assert(node < NODE_IGNORE);
                    232:        return properties[node].name;
                    233: }
                    234:
                    235: enum nodeclass
                    236: pnode_class(enum nodeid node)
                    237: {
                    238:        assert(node < NODE_IGNORE);
                    239:        return properties[node].class;
1.9       schwarze  240: }
                    241:
                    242: struct pnode *
                    243: pnode_alloc(struct pnode *np)
                    244: {
                    245:        struct pnode    *n;
                    246:
                    247:        if ((n = calloc(1, sizeof(*n))) != NULL) {
                    248:                TAILQ_INIT(&n->childq);
                    249:                TAILQ_INIT(&n->attrq);
                    250:                if ((n->parent = np) != NULL)
                    251:                        TAILQ_INSERT_TAIL(&np->childq, n, child);
                    252:        }
                    253:        return n;
1.1       schwarze  254: }
                    255:
                    256: /*
                    257:  * Recursively free a node (NULL is ok).
                    258:  */
                    259: static void
1.7       schwarze  260: pnode_free(struct pnode *n)
1.1       schwarze  261: {
1.7       schwarze  262:        struct pnode    *nc;
                    263:        struct pattr    *a;
1.1       schwarze  264:
1.7       schwarze  265:        if (n == NULL)
1.1       schwarze  266:                return;
                    267:
1.7       schwarze  268:        while ((nc = TAILQ_FIRST(&n->childq)) != NULL) {
                    269:                TAILQ_REMOVE(&n->childq, nc, child);
                    270:                pnode_free(nc);
1.1       schwarze  271:        }
1.7       schwarze  272:        while ((a = TAILQ_FIRST(&n->attrq)) != NULL) {
                    273:                TAILQ_REMOVE(&n->attrq, a, child);
                    274:                free(a->rawval);
                    275:                free(a);
1.1       schwarze  276:        }
1.8       schwarze  277:        free(n->b);
1.7       schwarze  278:        free(n);
1.1       schwarze  279: }
                    280:
                    281: /*
                    282:  * Unlink a node from its parent and pnode_free() it.
                    283:  */
                    284: void
1.7       schwarze  285: pnode_unlink(struct pnode *n)
1.1       schwarze  286: {
1.7       schwarze  287:        if (n == NULL)
1.1       schwarze  288:                return;
1.7       schwarze  289:        if (n->parent != NULL)
                    290:                TAILQ_REMOVE(&n->parent->childq, n, child);
                    291:        pnode_free(n);
1.1       schwarze  292: }
                    293:
                    294: /*
                    295:  * Unlink all children of a node and pnode_free() them.
                    296:  */
                    297: void
1.7       schwarze  298: pnode_unlinksub(struct pnode *n)
1.1       schwarze  299: {
1.7       schwarze  300:        while (TAILQ_EMPTY(&n->childq) == 0)
                    301:                pnode_unlink(TAILQ_FIRST(&n->childq));
1.1       schwarze  302: }
                    303:
                    304: /*
                    305:  * Retrieve an enumeration attribute from a node.
                    306:  * Return ATTRVAL__MAX if the node has no such attribute.
                    307:  */
                    308: enum attrval
1.7       schwarze  309: pnode_getattr(struct pnode *n, enum attrkey key)
1.1       schwarze  310: {
1.7       schwarze  311:        struct pattr    *a;
1.1       schwarze  312:
1.7       schwarze  313:        if (n == NULL)
1.1       schwarze  314:                return ATTRVAL__MAX;
1.7       schwarze  315:        TAILQ_FOREACH(a, &n->attrq, child)
                    316:                if (a->key == key)
                    317:                        return a->val;
1.1       schwarze  318:        return ATTRVAL__MAX;
                    319: }
                    320:
                    321: /*
                    322:  * Retrieve an attribute string from a node.
                    323:  * Return defval if the node has no such attribute.
                    324:  */
                    325: const char *
1.7       schwarze  326: pnode_getattr_raw(struct pnode *n, enum attrkey key, const char *defval)
1.1       schwarze  327: {
1.7       schwarze  328:        struct pattr    *a;
1.1       schwarze  329:
1.7       schwarze  330:        if (n == NULL)
1.1       schwarze  331:                return defval;
1.7       schwarze  332:        TAILQ_FOREACH(a, &n->attrq, child)
                    333:                if (a->key == key)
                    334:                        return a->val != ATTRVAL__MAX ? attrvals[a->val] :
                    335:                            a->rawval != NULL ? a->rawval : defval;
1.1       schwarze  336:        return defval;
                    337: }
                    338:
                    339: /*
                    340:  * Recursively search and return the first instance of "node".
                    341:  */
                    342: struct pnode *
1.7       schwarze  343: pnode_findfirst(struct pnode *n, enum nodeid node)
1.1       schwarze  344: {
1.7       schwarze  345:        struct pnode    *nc, *res;
1.1       schwarze  346:
1.12      schwarze  347:        if (n == NULL)
                    348:                return NULL;
1.7       schwarze  349:        if (n->node == node)
                    350:                return n;
                    351:        TAILQ_FOREACH(nc, &n->childq, child)
                    352:                if ((res = pnode_findfirst(nc, node)) != NULL)
1.1       schwarze  353:                        return res;
                    354:        return NULL;
1.12      schwarze  355: }
                    356:
                    357: /*
                    358:  * Like pnode_findfirst(), but also take the node out of the tree.
                    359:  */
                    360: struct pnode *
                    361: pnode_takefirst(struct pnode *n, enum nodeid node)
                    362: {
                    363:        struct pnode    *nc;
                    364:
                    365:        if ((nc = pnode_findfirst(n, node)) != NULL && nc->parent != NULL)
                    366:                TAILQ_REMOVE(&nc->parent->childq, nc, child);
                    367:        return nc;
1.1       schwarze  368: }

CVSweb