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

Annotation of docbook2mdoc/parse.c, Revision 1.18

1.18    ! schwarze    1: /* $Id: parse.c,v 1.17 2019/04/07 14:49:26 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:  */
                     18: #include <assert.h>
                     19: #include <ctype.h>
1.6       schwarze   20: #include <stdarg.h>
1.1       schwarze   21: #include <stdio.h>
1.5       schwarze   22: #include <stdlib.h>
1.1       schwarze   23: #include <string.h>
                     24: #include <unistd.h>
                     25:
                     26: #include "node.h"
                     27: #include "parse.h"
                     28:
                     29: /*
                     30:  * The implementation of the DocBook parser.
                     31:  */
                     32:
1.14      schwarze   33: enum   pstate {
                     34:        PARSE_ELEM,
                     35:        PARSE_TAG,
                     36:        PARSE_ARG,
                     37:        PARSE_SQ,
                     38:        PARSE_DQ
                     39: };
                     40:
1.1       schwarze   41: /*
                     42:  * Global parse state.
                     43:  * Keep this as simple and small as possible.
                     44:  */
                     45: struct parse {
                     46:        const char      *fname;  /* Name of the input file. */
                     47:        struct ptree    *tree;   /* Complete parse result. */
                     48:        struct pnode    *cur;    /* Current node in the tree. */
1.5       schwarze   49:        enum nodeid      ncur;   /* Type of the current node. */
                     50:        int              line;   /* Line number in the input file. */
                     51:        int              col;    /* Column number in the input file. */
                     52:        int              nline;  /* Line number of next token. */
                     53:        int              ncol;   /* Column number of next token. */
1.4       schwarze   54:        int              del;    /* Levels of nested nodes being deleted. */
1.16      schwarze   55:        int              spc;    /* Whitespace before the next element. */
1.5       schwarze   56:        int              attr;   /* The most recent attribute is valid. */
1.1       schwarze   57:        int              warn;
                     58: };
                     59:
                     60: struct element {
                     61:        const char      *name;   /* DocBook element name. */
                     62:        enum nodeid      node;   /* Node type to generate. */
                     63: };
                     64:
                     65: static const struct element elements[] = {
1.3       schwarze   66:        { "acronym",            NODE_IGNORE },
1.1       schwarze   67:        { "affiliation",        NODE_AFFILIATION },
1.4       schwarze   68:        { "anchor",             NODE_DELETE },
1.1       schwarze   69:        { "application",        NODE_APPLICATION },
                     70:        { "arg",                NODE_ARG },
                     71:        { "author",             NODE_AUTHOR },
                     72:        { "authorgroup",        NODE_AUTHORGROUP },
                     73:        { "blockquote",         NODE_BLOCKQUOTE },
                     74:        { "book",               NODE_BOOK },
                     75:        { "bookinfo",           NODE_BOOKINFO },
                     76:        { "caution",            NODE_CAUTION },
                     77:        { "chapter",            NODE_SECTION },
                     78:        { "citerefentry",       NODE_CITEREFENTRY },
                     79:        { "citetitle",          NODE_CITETITLE },
                     80:        { "cmdsynopsis",        NODE_CMDSYNOPSIS },
1.13      schwarze   81:        { "code",               NODE_LITERAL },
1.1       schwarze   82:        { "colspec",            NODE_COLSPEC },
                     83:        { "command",            NODE_COMMAND },
                     84:        { "constant",           NODE_CONSTANT },
1.7       schwarze   85:        { "contrib",            NODE_CONTRIB },
1.1       schwarze   86:        { "copyright",          NODE_COPYRIGHT },
                     87:        { "date",               NODE_DATE },
                     88:        { "editor",             NODE_EDITOR },
                     89:        { "email",              NODE_EMAIL },
                     90:        { "emphasis",           NODE_EMPHASIS },
                     91:        { "entry",              NODE_ENTRY },
                     92:        { "envar",              NODE_ENVAR },
1.13      schwarze   93:        { "errorname",          NODE_ERRORNAME },
1.1       schwarze   94:        { "fieldsynopsis",      NODE_FIELDSYNOPSIS },
                     95:        { "filename",           NODE_FILENAME },
1.7       schwarze   96:        { "firstname",          NODE_PERSONNAME },
1.1       schwarze   97:        { "firstterm",          NODE_FIRSTTERM },
                     98:        { "footnote",           NODE_FOOTNOTE },
                     99:        { "funcdef",            NODE_FUNCDEF },
                    100:        { "funcprototype",      NODE_FUNCPROTOTYPE },
                    101:        { "funcsynopsis",       NODE_FUNCSYNOPSIS },
                    102:        { "funcsynopsisinfo",   NODE_FUNCSYNOPSISINFO },
                    103:        { "function",           NODE_FUNCTION },
                    104:        { "glossterm",          NODE_GLOSSTERM },
                    105:        { "group",              NODE_GROUP },
                    106:        { "holder",             NODE_HOLDER },
                    107:        { "index",              NODE_INDEX },
1.4       schwarze  108:        { "indexterm",          NODE_DELETE },
1.1       schwarze  109:        { "info",               NODE_INFO },
                    110:        { "informalequation",   NODE_INFORMALEQUATION },
1.11      schwarze  111:        { "informaltable",      NODE_TABLE },
1.1       schwarze  112:        { "inlineequation",     NODE_INLINEEQUATION },
                    113:        { "itemizedlist",       NODE_ITEMIZEDLIST },
                    114:        { "keysym",             NODE_KEYSYM },
                    115:        { "legalnotice",        NODE_LEGALNOTICE },
                    116:        { "link",               NODE_LINK },
                    117:        { "listitem",           NODE_LISTITEM },
                    118:        { "literal",            NODE_LITERAL },
                    119:        { "literallayout",      NODE_LITERALLAYOUT },
                    120:        { "manvolnum",          NODE_MANVOLNUM },
                    121:        { "member",             NODE_MEMBER },
                    122:        { "mml:math",           NODE_MML_MATH },
                    123:        { "mml:mfenced",        NODE_MML_MFENCED },
                    124:        { "mml:mfrac",          NODE_MML_MFRAC },
                    125:        { "mml:mi",             NODE_MML_MI },
                    126:        { "mml:mn",             NODE_MML_MN },
                    127:        { "mml:mo",             NODE_MML_MO },
                    128:        { "mml:mrow",           NODE_MML_MROW },
                    129:        { "mml:msub",           NODE_MML_MSUB },
                    130:        { "mml:msup",           NODE_MML_MSUP },
                    131:        { "modifier",           NODE_MODIFIER },
                    132:        { "note",               NODE_NOTE },
                    133:        { "option",             NODE_OPTION },
                    134:        { "orderedlist",        NODE_ORDEREDLIST },
                    135:        { "orgname",            NODE_ORGNAME },
1.7       schwarze  136:        { "othername",          NODE_PERSONNAME },
1.1       schwarze  137:        { "para",               NODE_PARA },
                    138:        { "paramdef",           NODE_PARAMDEF },
                    139:        { "parameter",          NODE_PARAMETER },
                    140:        { "part",               NODE_SECTION },
                    141:        { "personname",         NODE_PERSONNAME },
1.3       schwarze  142:        { "phrase",             NODE_IGNORE },
1.1       schwarze  143:        { "preface",            NODE_PREFACE },
1.4       schwarze  144:        { "primary",            NODE_DELETE },
1.1       schwarze  145:        { "programlisting",     NODE_PROGRAMLISTING },
                    146:        { "prompt",             NODE_PROMPT },
                    147:        { "quote",              NODE_QUOTE },
                    148:        { "refclass",           NODE_REFCLASS },
                    149:        { "refdescriptor",      NODE_REFDESCRIPTOR },
                    150:        { "refentry",           NODE_REFENTRY },
                    151:        { "refentryinfo",       NODE_REFENTRYINFO },
                    152:        { "refentrytitle",      NODE_REFENTRYTITLE },
                    153:        { "refmeta",            NODE_REFMETA },
                    154:        { "refmetainfo",        NODE_REFMETAINFO },
                    155:        { "refmiscinfo",        NODE_REFMISCINFO },
                    156:        { "refname",            NODE_REFNAME },
                    157:        { "refnamediv",         NODE_REFNAMEDIV },
                    158:        { "refpurpose",         NODE_REFPURPOSE },
                    159:        { "refsect1",           NODE_SECTION },
                    160:        { "refsect2",           NODE_SECTION },
                    161:        { "refsect3",           NODE_SECTION },
                    162:        { "refsection",         NODE_SECTION },
                    163:        { "refsynopsisdiv",     NODE_REFSYNOPSISDIV },
                    164:        { "releaseinfo",        NODE_RELEASEINFO },
                    165:        { "replaceable",        NODE_REPLACEABLE },
                    166:        { "row",                NODE_ROW },
                    167:        { "sbr",                NODE_SBR },
                    168:        { "screen",             NODE_SCREEN },
1.4       schwarze  169:        { "secondary",          NODE_DELETE },
1.1       schwarze  170:        { "sect1",              NODE_SECTION },
                    171:        { "sect2",              NODE_SECTION },
                    172:        { "section",            NODE_SECTION },
                    173:        { "sgmltag",            NODE_SGMLTAG },
1.15      schwarze  174:        { "simpara",            NODE_PARA },
1.1       schwarze  175:        { "simplelist",         NODE_SIMPLELIST },
                    176:        { "spanspec",           NODE_SPANSPEC },
1.13      schwarze  177:        { "structfield",        NODE_PARAMETER },
                    178:        { "structname",         NODE_TYPE },
1.1       schwarze  179:        { "subtitle",           NODE_SUBTITLE },
1.7       schwarze  180:        { "surname",            NODE_PERSONNAME },
1.12      schwarze  181:        { "symbol",             NODE_CONSTANT },
1.1       schwarze  182:        { "synopsis",           NODE_SYNOPSIS },
                    183:        { "table",              NODE_TABLE },
                    184:        { "tbody",              NODE_TBODY },
                    185:        { "term",               NODE_TERM },
                    186:        { "tfoot",              NODE_TFOOT },
                    187:        { "tgroup",             NODE_TGROUP },
                    188:        { "thead",              NODE_THEAD },
                    189:        { "tip",                NODE_TIP },
                    190:        { "title",              NODE_TITLE },
1.3       schwarze  191:        { "trademark",          NODE_IGNORE },
1.1       schwarze  192:        { "type",               NODE_TYPE },
1.18    ! schwarze  193:        { "ulink",              NODE_LINK },
1.13      schwarze  194:        { "userinput",          NODE_LITERAL },
1.1       schwarze  195:        { "variablelist",       NODE_VARIABLELIST },
                    196:        { "varlistentry",       NODE_VARLISTENTRY },
                    197:        { "varname",            NODE_VARNAME },
                    198:        { "warning",            NODE_WARNING },
                    199:        { "wordasword",         NODE_WORDASWORD },
1.4       schwarze  200:        { "xi:include",         NODE_DELETE_WARN },
1.1       schwarze  201:        { "year",               NODE_YEAR },
1.5       schwarze  202:        { NULL,                 NODE_IGNORE }
1.1       schwarze  203: };
                    204:
1.9       schwarze  205: struct entity {
                    206:        const char      *name;
                    207:        const char      *roff;
                    208: };
                    209:
                    210: /*
                    211:  * XML character entity references found in the wild.
                    212:  * Those that don't have an exact mandoc_char(7) representation
                    213:  * are approximated, and the desired codepoint is given as a comment.
                    214:  * Encoding them as \\[u...] would leave -Tascii out in the cold.
                    215:  */
                    216: static const struct entity entities[] = {
                    217:        { "alpha",      "\\(*a" },
                    218:        { "amp",        "&" },
                    219:        { "apos",       "'" },
                    220:        { "auml",       "\\(:a" },
                    221:        { "beta",       "\\(*b" },
                    222:        { "circ",       "^" },      /* U+02C6 */
                    223:        { "copy",       "\\(co" },
                    224:        { "dagger",     "\\(dg" },
                    225:        { "Delta",      "\\(*D" },
                    226:        { "eacute",     "\\('e" },
                    227:        { "emsp",       "\\ " },    /* U+2003 */
                    228:        { "gt",         ">" },
                    229:        { "hairsp",     "\\^" },
                    230:        { "kappa",      "\\(*k" },
                    231:        { "larr",       "\\(<-" },
                    232:        { "ldquo",      "\\(lq" },
                    233:        { "le",         "\\(<=" },
                    234:        { "lowbar",     "_" },
                    235:        { "lsqb",       "[" },
                    236:        { "lt",         "<" },
                    237:        { "mdash",      "\\(em" },
                    238:        { "minus",      "\\-" },
                    239:        { "ndash",      "\\(en" },
                    240:        { "nbsp",       "\\ " },
                    241:        { "num",        "#" },
                    242:        { "oslash",     "\\(/o" },
                    243:        { "ouml",       "\\(:o" },
                    244:        { "percnt",     "%" },
                    245:        { "quot",       "\\(dq" },
                    246:        { "rarr",       "\\(->" },
                    247:        { "rArr",       "\\(rA" },
                    248:        { "rdquo",      "\\(rq" },
                    249:        { "reg",        "\\(rg" },
                    250:        { "rho",        "\\(*r" },
                    251:        { "rsqb",       "]" },
                    252:        { "sigma",      "\\(*s" },
                    253:        { "shy",        "\\&" },     /* U+00AD */
                    254:        { "tau",        "\\(*t" },
                    255:        { "tilde",      "\\[u02DC]" },
                    256:        { "times",      "\\[tmu]" },
                    257:        { "uuml",       "\\(:u" },
                    258:        { NULL,         NULL }
                    259: };
                    260:
1.6       schwarze  261: static void
                    262: error_msg(struct parse *p, const char *fmt, ...)
                    263: {
                    264:        va_list          ap;
                    265:
                    266:        fprintf(stderr, "%s:%d:%d: ", p->fname, p->line, p->col);
                    267:        va_start(ap, fmt);
                    268:        vfprintf(stderr, fmt, ap);
                    269:        va_end(ap);
                    270:        fputc('\n', stderr);
                    271:        p->tree->flags |= TREE_FAIL;
                    272: }
                    273:
                    274: static void
                    275: warn_msg(struct parse *p, const char *fmt, ...)
                    276: {
                    277:        va_list          ap;
                    278:
                    279:        if (p->warn == 0)
                    280:                return;
                    281:
                    282:        fprintf(stderr, "%s:%d:%d: warning: ", p->fname, p->line, p->col);
                    283:        va_start(ap, fmt);
                    284:        vfprintf(stderr, fmt, ap);
                    285:        va_end(ap);
                    286:        fputc('\n', stderr);
                    287: }
                    288:
1.1       schwarze  289: /*
                    290:  * Process a string of characters.
                    291:  * If a text node is already open, append to it.
                    292:  * Otherwise, create a new one as a child of the current node.
                    293:  */
                    294: static void
1.5       schwarze  295: xml_char(struct parse *ps, const char *p, int sz)
1.1       schwarze  296: {
                    297:        struct pnode    *dat;
1.16      schwarze  298:        size_t           newsz;
1.1       schwarze  299:
1.5       schwarze  300:        if (ps->del > 0)
1.1       schwarze  301:                return;
                    302:
1.5       schwarze  303:        if (ps->cur == NULL) {
1.6       schwarze  304:                error_msg(ps, "discarding text before document: %.*s", sz, p);
1.5       schwarze  305:                return;
                    306:        }
                    307:
1.1       schwarze  308:        if (ps->cur->node != NODE_TEXT) {
                    309:                if ((dat = calloc(1, sizeof(*dat))) == NULL) {
                    310:                        perror(NULL);
                    311:                        exit(1);
                    312:                }
                    313:                dat->node = NODE_TEXT;
1.16      schwarze  314:                dat->spc = ps->spc;
1.1       schwarze  315:                dat->parent = ps->cur;
                    316:                TAILQ_INIT(&dat->childq);
                    317:                TAILQ_INIT(&dat->attrq);
                    318:                TAILQ_INSERT_TAIL(&ps->cur->childq, dat, child);
                    319:                ps->cur = dat;
                    320:        }
                    321:
1.5       schwarze  322:        if (ps->tree->flags & TREE_CLOSED &&
1.6       schwarze  323:            ps->cur->parent == ps->tree->root)
                    324:                warn_msg(ps, "text after end of document: %.*s", sz, p);
1.5       schwarze  325:
1.1       schwarze  326:        /* Append to the current text node. */
                    327:
                    328:        assert(sz >= 0);
1.16      schwarze  329:        newsz = ps->cur->bsz + (ps->cur->bsz && ps->spc) + sz;
                    330:        ps->cur->b = realloc(ps->cur->b, newsz + 1);
1.1       schwarze  331:        if (ps->cur->b == NULL) {
                    332:                perror(NULL);
                    333:                exit(1);
                    334:        }
1.16      schwarze  335:        if (ps->cur->bsz && ps->spc)
                    336:                ps->cur->b[ps->cur->bsz++] = ' ';
1.1       schwarze  337:        memcpy(ps->cur->b + ps->cur->bsz, p, sz);
1.16      schwarze  338:        ps->cur->b[ps->cur->bsz = newsz] = '\0';
1.1       schwarze  339:        ps->cur->real = ps->cur->b;
1.16      schwarze  340:        ps->spc = 0;
1.1       schwarze  341: }
                    342:
1.16      schwarze  343: /*
                    344:  * Close out the text node and strip trailing whitespace, if one is open.
                    345:  */
1.1       schwarze  346: static void
1.16      schwarze  347: pnode_closetext(struct parse *p)
1.1       schwarze  348: {
1.16      schwarze  349:        struct pnode    *n;
                    350:
                    351:        if ((n = p->cur) == NULL || n->node != NODE_TEXT)
                    352:                return;
                    353:        p->cur = n->parent;
                    354:        while (n->bsz > 0 && isspace((unsigned char)n->b[n->bsz - 1])) {
                    355:                n->b[--n->bsz] = '\0';
                    356:                p->spc = 1;
                    357:        }
1.1       schwarze  358: }
                    359:
1.9       schwarze  360: static void
                    361: xml_entity(struct parse *p, const char *name)
                    362: {
                    363:        const struct entity     *entity;
                    364:        struct pnode            *dat;
                    365:
                    366:        if (p->del > 0)
                    367:                return;
                    368:
                    369:        if (p->cur == NULL) {
                    370:                error_msg(p, "discarding entity before document: &%s;", name);
                    371:                return;
                    372:        }
                    373:
1.16      schwarze  374:        pnode_closetext(p);
1.9       schwarze  375:
                    376:        if (p->tree->flags & TREE_CLOSED && p->cur == p->tree->root)
                    377:                warn_msg(p, "entity after end of document: &%s;", name);
                    378:
                    379:        for (entity = entities; entity->name != NULL; entity++)
                    380:                if (strcmp(name, entity->name) == 0)
                    381:                        break;
                    382:
                    383:        if (entity->roff == NULL) {
                    384:                error_msg(p, "unknown entity &%s;", name);
                    385:                return;
                    386:        }
                    387:
                    388:        /* Create, append, and close out an entity node. */
                    389:        if ((dat = calloc(1, sizeof(*dat))) == NULL ||
                    390:            (dat->b = dat->real = strdup(entity->roff)) == NULL) {
                    391:                perror(NULL);
                    392:                exit(1);
                    393:        }
                    394:        dat->node = NODE_ESCAPE;
                    395:        dat->bsz = strlen(dat->b);
1.16      schwarze  396:        dat->spc = p->spc;
1.9       schwarze  397:        dat->parent = p->cur;
                    398:        TAILQ_INIT(&dat->childq);
                    399:        TAILQ_INIT(&dat->attrq);
                    400:        TAILQ_INSERT_TAIL(&p->cur->childq, dat, child);
1.16      schwarze  401:        p->spc = 0;
1.9       schwarze  402: }
                    403:
1.1       schwarze  404: /*
                    405:  * Begin an element.
                    406:  */
                    407: static void
1.5       schwarze  408: xml_elem_start(struct parse *ps, const char *name)
1.1       schwarze  409: {
1.5       schwarze  410:        const struct element    *elem;
                    411:        struct pnode            *dat;
1.1       schwarze  412:
1.5       schwarze  413:        if (*name == '!' || *name == '?')
1.1       schwarze  414:                return;
                    415:
1.4       schwarze  416:        /*
                    417:         * An ancestor is excluded from the tree;
                    418:         * keep track of the number of levels excluded.
                    419:         */
                    420:        if (ps->del > 0) {
                    421:                ps->del++;
                    422:                return;
                    423:        }
                    424:
1.16      schwarze  425:        pnode_closetext(ps);
1.1       schwarze  426:
                    427:        for (elem = elements; elem->name != NULL; elem++)
                    428:                if (strcmp(elem->name, name) == 0)
                    429:                        break;
                    430:
1.6       schwarze  431:        if (elem->name == NULL)
                    432:                error_msg(ps, "unknown element <%s>", name);
                    433:
1.5       schwarze  434:        ps->ncur = elem->node;
1.1       schwarze  435:
1.5       schwarze  436:        switch (ps->ncur) {
1.4       schwarze  437:        case NODE_DELETE_WARN:
1.6       schwarze  438:                warn_msg(ps, "skipping element <%s>", name);
1.2       schwarze  439:                /* FALLTHROUGH */
1.4       schwarze  440:        case NODE_DELETE:
                    441:                ps->del = 1;
                    442:                /* FALLTHROUGH */
1.2       schwarze  443:        case NODE_IGNORE:
                    444:                return;
                    445:        case NODE_INLINEEQUATION:
1.1       schwarze  446:                ps->tree->flags |= TREE_EQN;
1.2       schwarze  447:                break;
                    448:        default:
                    449:                break;
                    450:        }
1.1       schwarze  451:
1.6       schwarze  452:        if (ps->tree->flags & TREE_CLOSED && ps->cur->parent == NULL)
                    453:                warn_msg(ps, "element after end of document: <%s>", name);
1.5       schwarze  454:
1.1       schwarze  455:        if ((dat = calloc(1, sizeof(*dat))) == NULL) {
                    456:                perror(NULL);
                    457:                exit(1);
                    458:        }
1.17      schwarze  459:
                    460:        /*
                    461:         * Nodes that begin a new macro or request line or start by
                    462:         * printing text always want whitespace before themselves.
                    463:         */
                    464:
                    465:        switch (dat->node = elem->node) {
                    466:        case NODE_AUTHORGROUP:
                    467:        case NODE_BOOKINFO:
                    468:        case NODE_CAUTION:
                    469:        case NODE_EDITOR:
                    470:        case NODE_ENTRY:
                    471:        case NODE_FUNCDEF:
                    472:        case NODE_FUNCPROTOTYPE:
                    473:        case NODE_INFORMALEQUATION:
                    474:        case NODE_INLINEEQUATION:
                    475:        case NODE_ITEMIZEDLIST:
                    476:        case NODE_LEGALNOTICE:
                    477:        case NODE_LITERALLAYOUT:
                    478:        case NODE_NOTE:
                    479:        case NODE_ORDEREDLIST:
                    480:        case NODE_PARA:
                    481:        case NODE_PREFACE:
                    482:        case NODE_PROGRAMLISTING:
                    483:        case NODE_REFMETA:
                    484:        case NODE_REFNAMEDIV:
                    485:        case NODE_REFSYNOPSISDIV:
                    486:        case NODE_ROW:
                    487:        case NODE_SBR:
                    488:        case NODE_SCREEN:
                    489:        case NODE_SECTION:
                    490:        case NODE_SYNOPSIS:
                    491:        case NODE_TGROUP:
                    492:        case NODE_TIP:
                    493:        case NODE_TITLE:
                    494:        case NODE_VARIABLELIST:
                    495:        case NODE_VARLISTENTRY:
                    496:        case NODE_WARNING:
                    497:                dat->spc = 1;
                    498:                break;
                    499:        default:
                    500:                dat->spc = ps->spc;
                    501:                break;
                    502:        }
1.1       schwarze  503:        dat->parent = ps->cur;
                    504:        TAILQ_INIT(&dat->childq);
                    505:        TAILQ_INIT(&dat->attrq);
                    506:
                    507:        if (ps->cur != NULL)
                    508:                TAILQ_INSERT_TAIL(&ps->cur->childq, dat, child);
                    509:
                    510:        ps->cur = dat;
                    511:        if (ps->tree->root == NULL)
                    512:                ps->tree->root = dat;
1.5       schwarze  513: }
                    514:
                    515: static void
                    516: xml_attrkey(struct parse *ps, const char *name)
                    517: {
                    518:        struct pattr    *attr;
                    519:        enum attrkey     key;
1.1       schwarze  520:
1.5       schwarze  521:        if (ps->del > 0 || *name == '\0')
                    522:                return;
                    523:        if ((key = attrkey_parse(name)) == ATTRKEY__MAX) {
                    524:                ps->attr = 0;
                    525:                return;
                    526:        }
                    527:        if ((attr = calloc(1, sizeof(*attr))) == NULL) {
                    528:                perror(NULL);
                    529:                exit(1);
                    530:        }
                    531:        attr->key = key;
                    532:        attr->val = ATTRVAL__MAX;
                    533:        attr->rawval = NULL;
                    534:        TAILQ_INSERT_TAIL(&ps->cur->attrq, attr, child);
                    535:        ps->attr = 1;
                    536: }
                    537:
                    538: static void
                    539: xml_attrval(struct parse *ps, const char *name)
                    540: {
                    541:        struct pattr    *attr;
                    542:
                    543:        if (ps->del > 0 || ps->attr == 0)
                    544:                return;
                    545:        if ((attr = TAILQ_LAST(&ps->cur->attrq, pattrq)) == NULL)
                    546:                return;
                    547:        if ((attr->val = attrval_parse(name)) == ATTRVAL__MAX &&
                    548:            (attr->rawval = strdup(name)) == NULL) {
                    549:                perror(NULL);
                    550:                exit(1);
1.1       schwarze  551:        }
                    552: }
                    553:
                    554: /*
                    555:  * Roll up the parse tree.
                    556:  * If we're at a text node, roll that one up first.
                    557:  */
                    558: static void
1.5       schwarze  559: xml_elem_end(struct parse *ps, const char *name)
1.1       schwarze  560: {
1.5       schwarze  561:        const struct element    *elem;
                    562:        enum nodeid              node;
1.1       schwarze  563:
1.4       schwarze  564:        /*
                    565:         * An ancestor is excluded from the tree;
                    566:         * keep track of the number of levels excluded.
                    567:         */
                    568:        if (ps->del > 1) {
                    569:                ps->del--;
                    570:                return;
                    571:        }
                    572:
1.16      schwarze  573:        if (ps->del == 0)
                    574:                pnode_closetext(ps);
1.2       schwarze  575:
1.5       schwarze  576:        if (name != NULL) {
                    577:                for (elem = elements; elem->name != NULL; elem++)
                    578:                        if (strcmp(elem->name, name) == 0)
                    579:                                break;
                    580:                node = elem->node;
                    581:        } else
                    582:                node = ps->ncur;
1.2       schwarze  583:
1.5       schwarze  584:        switch (node) {
1.4       schwarze  585:        case NODE_DELETE_WARN:
                    586:        case NODE_DELETE:
1.5       schwarze  587:                if (ps->del > 0)
                    588:                        ps->del--;
1.4       schwarze  589:                break;
1.2       schwarze  590:        case NODE_IGNORE:
                    591:                break;
                    592:        default:
1.5       schwarze  593:                if (ps->cur == NULL || node != ps->cur->node) {
1.6       schwarze  594:                        warn_msg(ps, "element not open: </%s>", name);
1.5       schwarze  595:                        break;
                    596:                }
                    597:
                    598:                /*
                    599:                 * Refrain from actually closing the document element.
                    600:                 * If no more content follows, no harm is done, but if
                    601:                 * some content still follows, simply processing it is
                    602:                 * obviously better than discarding it or crashing.
                    603:                 */
                    604:
                    605:                if (ps->cur->parent == NULL)
                    606:                        ps->tree->flags |= TREE_CLOSED;
                    607:                else
                    608:                        ps->cur = ps->cur->parent;
1.16      schwarze  609:                ps->spc = 0;
1.4       schwarze  610:                break;
1.2       schwarze  611:        }
1.4       schwarze  612:        assert(ps->del == 0);
1.1       schwarze  613: }
                    614:
                    615: struct parse *
                    616: parse_alloc(int warn)
                    617: {
                    618:        struct parse    *p;
                    619:
                    620:        if ((p = calloc(1, sizeof(*p))) == NULL)
                    621:                return NULL;
                    622:
                    623:        if ((p->tree = calloc(1, sizeof(*p->tree))) == NULL) {
                    624:                free(p);
                    625:                return NULL;
                    626:        }
                    627:        p->warn = warn;
                    628:        return p;
                    629: }
                    630:
                    631: void
                    632: parse_free(struct parse *p)
                    633: {
                    634:        if (p == NULL)
                    635:                return;
                    636:        if (p->tree != NULL) {
                    637:                pnode_unlink(p->tree->root);
                    638:                free(p->tree);
                    639:        }
                    640:        free(p);
                    641: }
                    642:
1.14      schwarze  643: static void
                    644: increment(struct parse *p, char *b, size_t *pend, int refill)
                    645: {
                    646:        if (refill) {
                    647:                if (b[*pend] == '\n') {
                    648:                        p->nline++;
                    649:                        p->ncol = 1;
                    650:                } else
                    651:                        p->ncol++;
                    652:        }
                    653:        ++*pend;
                    654: }
                    655:
1.5       schwarze  656: /*
                    657:  * Advance the pend pointer to the next character in the charset.
                    658:  * If the charset starts with a space, it stands for any whitespace.
                    659:  * Update the new input file position, used for messages.
                    660:  * Do not overrun the buffer b of length rlen.
                    661:  * When reaching the end, NUL-terminate the buffer and return 1;
                    662:  * otherwise, return 0.
                    663:  */
                    664: static int
                    665: advance(struct parse *p, char *b, size_t rlen, size_t *pend,
1.14      schwarze  666:     const char *charset, int refill)
1.5       schwarze  667: {
                    668:        int              space;
                    669:
                    670:        if (*charset == ' ') {
                    671:                space = 1;
                    672:                charset++;
                    673:        } else
                    674:                space = 0;
                    675:
1.14      schwarze  676:        if (refill) {
                    677:                p->nline = p->line;
                    678:                p->ncol = p->col;
                    679:        }
1.5       schwarze  680:        while (*pend < rlen) {
                    681:                if (space && isspace((unsigned char)b[*pend]))
                    682:                        break;
                    683:                if (strchr(charset, b[*pend]) != NULL)
                    684:                        break;
1.14      schwarze  685:                increment(p, b, pend, refill);
1.5       schwarze  686:        }
                    687:        if (*pend == rlen) {
                    688:                b[rlen] = '\0';
1.14      schwarze  689:                return refill;
1.5       schwarze  690:        } else
                    691:                return 0;
                    692: }
                    693:
1.14      schwarze  694: size_t
                    695: parse_string(struct parse *p, char *b, size_t rlen,
                    696:     enum pstate *pstate, int refill)
                    697: {
                    698:        char            *cp;
                    699:        size_t           poff;  /* Parse offset in b[]. */
                    700:        size_t           pend;  /* Offset of the end of the current word. */
                    701:        int              elem_end;
                    702:
1.16      schwarze  703:        p->spc = 0;
1.14      schwarze  704:        pend = 0;
                    705:        for (;;) {
                    706:
                    707:                /* Proceed to the next token, skipping whitespace. */
                    708:
                    709:                if (refill) {
                    710:                        p->line = p->nline;
                    711:                        p->col = p->ncol;
                    712:                }
                    713:                if ((poff = pend) == rlen)
                    714:                        break;
                    715:                if (isspace((unsigned char)b[pend])) {
1.16      schwarze  716:                        p->spc = 1;
1.14      schwarze  717:                        increment(p, b, &pend, refill);
                    718:                        continue;
                    719:                }
                    720:
                    721:                /*
                    722:                 * The following four cases (ARG, TAG, and starting an
                    723:                 * entity or a tag) all parse a word or quoted string.
                    724:                 * If that extends beyond the read buffer and the last
                    725:                 * read(2) still got data, they all break out of the
                    726:                 * token loop to request more data from the read loop.
                    727:                 *
                    728:                 * Also, three of them detect self-closing tags, those
                    729:                 * ending with "/>", setting the flag elem_end and
                    730:                 * calling xml_elem_end() at the very end, after
                    731:                 * handling the attribute value, attribute name, or
                    732:                 * tag name, respectively.
                    733:                 */
                    734:
                    735:                /* Parse an attribute value. */
                    736:
                    737:                if (*pstate >= PARSE_ARG) {
                    738:                        if (*pstate == PARSE_ARG &&
                    739:                            (b[pend] == '\'' || b[pend] == '"')) {
                    740:                                *pstate = b[pend] == '"' ?
                    741:                                    PARSE_DQ : PARSE_SQ;
                    742:                                increment(p, b, &pend, refill);
                    743:                                continue;
                    744:                        }
                    745:                        if (advance(p, b, rlen, &pend,
                    746:                            *pstate == PARSE_DQ ? "\"" :
                    747:                            *pstate == PARSE_SQ ? "'" : " >", refill))
                    748:                                break;
                    749:                        *pstate = PARSE_TAG;
                    750:                        elem_end = 0;
                    751:                        if (b[pend] == '>') {
                    752:                                *pstate = PARSE_ELEM;
                    753:                                if (pend > 0 && b[pend - 1] == '/') {
                    754:                                        b[pend - 1] = '\0';
                    755:                                        elem_end = 1;
                    756:                                }
                    757:                        }
                    758:                        b[pend] = '\0';
                    759:                        if (pend < rlen)
                    760:                                increment(p, b, &pend, refill);
                    761:                        xml_attrval(p, b + poff);
                    762:                        if (elem_end)
                    763:                                xml_elem_end(p, NULL);
                    764:
                    765:                /* Look for an attribute name. */
                    766:
                    767:                } else if (*pstate == PARSE_TAG) {
                    768:                        if (advance(p, b, rlen, &pend, " =>", refill))
                    769:                                break;
                    770:                        elem_end = 0;
                    771:                        switch (b[pend]) {
                    772:                        case '>':
                    773:                                *pstate = PARSE_ELEM;
                    774:                                if (pend > 0 && b[pend - 1] == '/') {
                    775:                                        b[pend - 1] = '\0';
                    776:                                        elem_end = 1;
                    777:                                }
                    778:                                break;
                    779:                        case '=':
                    780:                                *pstate = PARSE_ARG;
                    781:                                break;
                    782:                        default:
                    783:                                break;
                    784:                        }
                    785:                        b[pend] = '\0';
                    786:                        if (pend < rlen)
                    787:                                increment(p, b, &pend, refill);
                    788:                        xml_attrkey(p, b + poff);
                    789:                        if (elem_end)
                    790:                                xml_elem_end(p, NULL);
                    791:
                    792:                /* Begin an opening or closing tag. */
                    793:
                    794:                } else if (b[poff] == '<') {
                    795:                        if (advance(p, b, rlen, &pend, " >", refill))
                    796:                                break;
                    797:                        if (pend > poff + 3 &&
                    798:                            strncmp(b + poff, "<!--", 4) == 0) {
                    799:
                    800:                                /* Skip a comment. */
                    801:
                    802:                                cp = strstr(b + pend - 2, "-->");
                    803:                                if (cp == NULL) {
                    804:                                        if (refill)
                    805:                                                break;
                    806:                                        cp = b + rlen;
                    807:                                } else
                    808:                                        cp += 3;
                    809:                                while (b + pend < cp)
                    810:                                        increment(p, b, &pend, refill);
                    811:                                continue;
                    812:                        }
                    813:                        elem_end = 0;
                    814:                        if (b[pend] != '>')
                    815:                                *pstate = PARSE_TAG;
                    816:                        else if (pend > 0 && b[pend - 1] == '/') {
                    817:                                b[pend - 1] = '\0';
                    818:                                elem_end = 1;
                    819:                        }
                    820:                        b[pend] = '\0';
                    821:                        if (pend < rlen)
                    822:                                increment(p, b, &pend, refill);
                    823:                        if (b[++poff] == '/') {
                    824:                                elem_end = 1;
                    825:                                poff++;
                    826:                        } else
                    827:                                xml_elem_start(p, b + poff);
                    828:                        if (elem_end)
                    829:                                xml_elem_end(p, b + poff);
                    830:
                    831:                /* Process an entity. */
                    832:
                    833:                } else if (b[poff] == '&') {
                    834:                        if (advance(p, b, rlen, &pend, ";", refill))
                    835:                                break;
                    836:                        b[pend] = '\0';
                    837:                        if (pend < rlen)
                    838:                                increment(p, b, &pend, refill);
                    839:                        xml_entity(p, b + poff + 1);
                    840:
                    841:                /* Process text up to the next tag, entity, or EOL. */
                    842:
                    843:                } else {
                    844:                        advance(p, b, rlen, &pend, "<&", refill);
                    845:                        xml_char(p, b + poff, pend - poff);
                    846:                }
                    847:        }
                    848:        return poff;
                    849: }
                    850:
1.1       schwarze  851: struct ptree *
                    852: parse_file(struct parse *p, int fd, const char *fname)
                    853: {
                    854:        char             b[4096];
1.5       schwarze  855:        ssize_t          rsz;   /* Return value from read(2). */
1.14      schwarze  856:        size_t           rlen;  /* Number of bytes in b[]. */
1.5       schwarze  857:        size_t           poff;  /* Parse offset in b[]. */
1.14      schwarze  858:        enum pstate      pstate;
1.1       schwarze  859:
                    860:        p->fname = fname;
1.5       schwarze  861:        p->nline = 1;
                    862:        p->ncol = 1;
1.14      schwarze  863:        pstate = PARSE_ELEM;
1.5       schwarze  864:        rlen = 0;
                    865:
                    866:        /*
                    867:         * Read loop.
                    868:         *
1.14      schwarze  869:          * If the previous token was incomplete and asked for more
                    870:          * input, we have to enter the read loop once more even on EOF.
1.5       schwarze  871:         * Once rsz is 0, incomplete tokens will no longer ask
                    872:         * for more input but instead use whatever there is,
                    873:         * and then exit the read loop.
                    874:         * The minus one on the size limit for read(2) is needed
                    875:         * such that advance() can set b[rlen] to NUL when needed.
                    876:         */
                    877:
1.14      schwarze  878:        while ((rsz = read(fd, b + rlen, sizeof(b) - rlen - 1)) >= 0 &&
                    879:            (rlen += rsz) > 0) {
                    880:                poff = parse_string(p, b, rlen, &pstate, rsz > 0);
1.5       schwarze  881:                /* Buffer exhausted; shift left and re-fill. */
                    882:                assert(poff > 0);
                    883:                rlen -= poff;
1.14      schwarze  884:                memmove(b, b + poff, rlen);
1.5       schwarze  885:        }
                    886:        if (rsz < 0) {
                    887:                perror(fname);
                    888:                p->tree->flags |= TREE_FAIL;
                    889:        }
1.16      schwarze  890:        pnode_closetext(p);
1.6       schwarze  891:        if ((p->tree->flags & TREE_CLOSED) == 0)
                    892:                warn_msg(p, "document not closed");
1.1       schwarze  893:        return p->tree;
                    894: }

CVSweb