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

Annotation of docbook2mdoc/node.c, Revision 1.18

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

CVSweb