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

Annotation of docbook2mdoc/node.c, Revision 1.25

1.25    ! schwarze    1: /* $Id: node.c,v 1.24 2019/04/28 15:03:29 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[] = {
                     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 },
1.15      schwarze   49:        { "copyright",          CLASS_LINE },
1.10      schwarze   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 },
1.17      schwarze   62:        { "footnote",           CLASS_BLOCK },
1.10      schwarze   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 },
1.18      schwarze   70:        { "imagedata",          CLASS_TEXT },
1.10      schwarze   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 },
1.21      schwarze   82:        { "literallayout",      CLASS_NOFILL },
1.10      schwarze   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 },
1.19      schwarze   97:        { "olink",              CLASS_ENCL },
1.10      schwarze   98:        { "option",             CLASS_LINE },
                     99:        { "orderedlist",        CLASS_BLOCK },
                    100:        { "para",               CLASS_BLOCK },
                    101:        { "paramdef",           CLASS_LINE },
                    102:        { "parameter",          CLASS_LINE },
                    103:        { "personname",         CLASS_TRANS },
                    104:        { "preface",            CLASS_BLOCK },
1.22      schwarze  105:        { "productname",        CLASS_LINE },
1.21      schwarze  106:        { "programlisting",     CLASS_NOFILL },
1.10      schwarze  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:        { "replaceable",        CLASS_LINE },
                    123:        { "row",                CLASS_BLOCK },
                    124:        { "sbr",                CLASS_BLOCK },
1.21      schwarze  125:        { "screen",             CLASS_NOFILL },
1.10      schwarze  126:        { "section",            CLASS_BLOCK },
                    127:        { "simplelist",         CLASS_TRANS },
1.15      schwarze  128:        { "simplesect",         CLASS_BLOCK },
1.10      schwarze  129:        { "spanspec",           CLASS_TRANS },
1.16      schwarze  130:        { "subscript",          CLASS_TEXT },
1.15      schwarze  131:        { "subtitle",           CLASS_BLOCK },
1.16      schwarze  132:        { "superscript",        CLASS_TEXT },
1.21      schwarze  133:        { "synopsis",           CLASS_NOFILL },
1.13      schwarze  134:        { "systemitem",         CLASS_LINE },
1.10      schwarze  135:        { "table",              CLASS_TRANS },
                    136:        { "tbody",              CLASS_TRANS },
                    137:        { "term",               CLASS_LINE },
                    138:        { "tfoot",              CLASS_TRANS },
                    139:        { "tgroup",             CLASS_BLOCK },
                    140:        { "thead",              CLASS_TRANS },
                    141:        { "tip",                CLASS_BLOCK },
                    142:        { "title",              CLASS_BLOCK },
                    143:        { "type",               CLASS_LINE },
                    144:        { "variablelist",       CLASS_BLOCK },
                    145:        { "varlistentry",       CLASS_BLOCK },
                    146:        { "varname",            CLASS_LINE },
1.23      schwarze  147:        { "void",               CLASS_TEXT },
1.10      schwarze  148:        { "warning",            CLASS_BLOCK },
                    149:        { "wordasword",         CLASS_TRANS },
1.20      schwarze  150:        { "xref",               CLASS_LINE },
1.10      schwarze  151:        { "[UNKNOWN]",          CLASS_VOID },
1.11      schwarze  152:        { "(t)",                CLASS_TEXT },
                    153:        { "(e)",                CLASS_TEXT }
1.10      schwarze  154: };
                    155:
1.1       schwarze  156: static const char *const attrkeys[ATTRKEY__MAX] = {
                    157:        "choice",
                    158:        "class",
                    159:        "close",
1.3       schwarze  160:        "cols",
1.5       schwarze  161:        "DEFINITION",
1.4       schwarze  162:        "endterm",
1.18      schwarze  163:        "entityref",
                    164:        "fileref",
1.6       schwarze  165:        "href",
1.1       schwarze  166:        "id",
                    167:        "linkend",
1.19      schwarze  168:        "localinfo",
1.5       schwarze  169:        "NAME",
1.1       schwarze  170:        "open",
1.5       schwarze  171:        "PUBLIC",
1.4       schwarze  172:        "rep",
1.5       schwarze  173:        "SYSTEM",
1.19      schwarze  174:        "targetdoc",
                    175:        "targetptr",
1.4       schwarze  176:        "url",
                    177:        "xlink:href"
1.1       schwarze  178: };
                    179:
                    180: static const char *const attrvals[ATTRVAL__MAX] = {
1.13      schwarze  181:        "event",
                    182:        "ipaddress",
1.1       schwarze  183:        "monospaced",
                    184:        "norepeat",
                    185:        "opt",
                    186:        "plain",
                    187:        "repeat",
1.13      schwarze  188:        "req",
                    189:        "systemname"
1.1       schwarze  190: };
                    191:
                    192: enum attrkey
                    193: attrkey_parse(const char *name)
                    194: {
                    195:        enum attrkey     key;
                    196:
                    197:        for (key = 0; key < ATTRKEY__MAX; key++)
                    198:                if (strcmp(name, attrkeys[key]) == 0)
                    199:                        break;
                    200:        return key;
                    201: }
                    202:
1.11      schwarze  203: const char *
                    204: attrkey_name(enum attrkey key)
                    205: {
                    206:        return attrkeys[key];
                    207: }
                    208:
1.1       schwarze  209: enum attrval
                    210: attrval_parse(const char *name)
                    211: {
                    212:        enum attrval     val;
                    213:
                    214:        for (val = 0; val < ATTRVAL__MAX; val++)
                    215:                if (strcmp(name, attrvals[val]) == 0)
                    216:                        break;
                    217:        return val;
1.11      schwarze  218: }
                    219:
                    220: const char *
                    221: attr_getval(const struct pattr *a)
                    222: {
                    223:        return a->val == ATTRVAL__MAX ? a->rawval : attrvals[a->val];
1.10      schwarze  224: }
                    225:
                    226: enum nodeid
                    227: pnode_parse(const char *name)
                    228: {
                    229:        enum nodeid      node;
                    230:
                    231:        for (node = 0; node < NODE_UNKNOWN; node++)
                    232:                if (strcmp(name, properties[node].name) == 0)
                    233:                        break;
                    234:        return node;
                    235: }
                    236:
                    237: const char *
                    238: pnode_name(enum nodeid node)
                    239: {
                    240:        assert(node < NODE_IGNORE);
                    241:        return properties[node].name;
                    242: }
                    243:
                    244: enum nodeclass
                    245: pnode_class(enum nodeid node)
                    246: {
                    247:        assert(node < NODE_IGNORE);
                    248:        return properties[node].class;
1.9       schwarze  249: }
                    250:
                    251: struct pnode *
                    252: pnode_alloc(struct pnode *np)
                    253: {
                    254:        struct pnode    *n;
                    255:
1.24      schwarze  256:        n = xcalloc(1, sizeof(*n));
                    257:        TAILQ_INIT(&n->childq);
                    258:        TAILQ_INIT(&n->attrq);
                    259:        if ((n->parent = np) != NULL)
                    260:                TAILQ_INSERT_TAIL(&np->childq, n, child);
1.25    ! schwarze  261:        return n;
        !           262: }
        !           263:
        !           264: struct pnode *
        !           265: pnode_alloc_text(struct pnode *np, const char *text)
        !           266: {
        !           267:        struct pnode    *n;
        !           268:
        !           269:        n = pnode_alloc(np);
        !           270:        n->node = NODE_TEXT;
        !           271:        n->b = xstrdup(text);
1.9       schwarze  272:        return n;
1.1       schwarze  273: }
                    274:
                    275: /*
                    276:  * Recursively free a node (NULL is ok).
                    277:  */
                    278: static void
1.7       schwarze  279: pnode_free(struct pnode *n)
1.1       schwarze  280: {
1.7       schwarze  281:        struct pnode    *nc;
                    282:        struct pattr    *a;
1.1       schwarze  283:
1.7       schwarze  284:        if (n == NULL)
1.1       schwarze  285:                return;
                    286:
1.7       schwarze  287:        while ((nc = TAILQ_FIRST(&n->childq)) != NULL) {
                    288:                TAILQ_REMOVE(&n->childq, nc, child);
                    289:                pnode_free(nc);
1.1       schwarze  290:        }
1.7       schwarze  291:        while ((a = TAILQ_FIRST(&n->attrq)) != NULL) {
                    292:                TAILQ_REMOVE(&n->attrq, a, child);
                    293:                free(a->rawval);
                    294:                free(a);
1.1       schwarze  295:        }
1.8       schwarze  296:        free(n->b);
1.7       schwarze  297:        free(n);
1.1       schwarze  298: }
                    299:
                    300: /*
                    301:  * Unlink a node from its parent and pnode_free() it.
                    302:  */
                    303: void
1.7       schwarze  304: pnode_unlink(struct pnode *n)
1.1       schwarze  305: {
1.7       schwarze  306:        if (n == NULL)
1.1       schwarze  307:                return;
1.7       schwarze  308:        if (n->parent != NULL)
                    309:                TAILQ_REMOVE(&n->parent->childq, n, child);
                    310:        pnode_free(n);
1.1       schwarze  311: }
                    312:
                    313: /*
                    314:  * Unlink all children of a node and pnode_free() them.
                    315:  */
                    316: void
1.7       schwarze  317: pnode_unlinksub(struct pnode *n)
1.1       schwarze  318: {
1.7       schwarze  319:        while (TAILQ_EMPTY(&n->childq) == 0)
                    320:                pnode_unlink(TAILQ_FIRST(&n->childq));
1.1       schwarze  321: }
                    322:
                    323: /*
                    324:  * Retrieve an enumeration attribute from a node.
                    325:  * Return ATTRVAL__MAX if the node has no such attribute.
                    326:  */
                    327: enum attrval
1.7       schwarze  328: pnode_getattr(struct pnode *n, enum attrkey key)
1.1       schwarze  329: {
1.7       schwarze  330:        struct pattr    *a;
1.1       schwarze  331:
1.7       schwarze  332:        if (n == NULL)
1.1       schwarze  333:                return ATTRVAL__MAX;
1.7       schwarze  334:        TAILQ_FOREACH(a, &n->attrq, child)
                    335:                if (a->key == key)
                    336:                        return a->val;
1.1       schwarze  337:        return ATTRVAL__MAX;
                    338: }
                    339:
                    340: /*
                    341:  * Retrieve an attribute string from a node.
                    342:  * Return defval if the node has no such attribute.
                    343:  */
                    344: const char *
1.7       schwarze  345: pnode_getattr_raw(struct pnode *n, enum attrkey key, const char *defval)
1.1       schwarze  346: {
1.7       schwarze  347:        struct pattr    *a;
1.1       schwarze  348:
1.7       schwarze  349:        if (n == NULL)
1.1       schwarze  350:                return defval;
1.7       schwarze  351:        TAILQ_FOREACH(a, &n->attrq, child)
                    352:                if (a->key == key)
                    353:                        return a->val != ATTRVAL__MAX ? attrvals[a->val] :
                    354:                            a->rawval != NULL ? a->rawval : defval;
1.1       schwarze  355:        return defval;
                    356: }
                    357:
                    358: /*
                    359:  * Recursively search and return the first instance of "node".
                    360:  */
                    361: struct pnode *
1.7       schwarze  362: pnode_findfirst(struct pnode *n, enum nodeid node)
1.1       schwarze  363: {
1.7       schwarze  364:        struct pnode    *nc, *res;
1.1       schwarze  365:
1.12      schwarze  366:        if (n == NULL)
                    367:                return NULL;
1.7       schwarze  368:        if (n->node == node)
                    369:                return n;
                    370:        TAILQ_FOREACH(nc, &n->childq, child)
                    371:                if ((res = pnode_findfirst(nc, node)) != NULL)
1.1       schwarze  372:                        return res;
                    373:        return NULL;
1.12      schwarze  374: }
                    375:
                    376: /*
                    377:  * Like pnode_findfirst(), but also take the node out of the tree.
                    378:  */
                    379: struct pnode *
                    380: pnode_takefirst(struct pnode *n, enum nodeid node)
                    381: {
                    382:        struct pnode    *nc;
                    383:
                    384:        if ((nc = pnode_findfirst(n, node)) != NULL && nc->parent != NULL)
                    385:                TAILQ_REMOVE(&nc->parent->childq, nc, child);
                    386:        return nc;
1.1       schwarze  387: }

CVSweb