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

Annotation of docbook2mdoc/parse.c, Revision 1.16

1.16    ! schwarze    1: /* $Id: parse.c,v 1.15 2019/04/06 13:45:58 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 },
                    193:        { "ulink",              NODE_ULINK },
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:        }
                    459:        dat->node = elem->node;
1.16    ! schwarze  460:        dat->spc = ps->spc;
1.1       schwarze  461:        dat->parent = ps->cur;
                    462:        TAILQ_INIT(&dat->childq);
                    463:        TAILQ_INIT(&dat->attrq);
                    464:
                    465:        if (ps->cur != NULL)
                    466:                TAILQ_INSERT_TAIL(&ps->cur->childq, dat, child);
                    467:
                    468:        ps->cur = dat;
                    469:        if (ps->tree->root == NULL)
                    470:                ps->tree->root = dat;
1.5       schwarze  471: }
                    472:
                    473: static void
                    474: xml_attrkey(struct parse *ps, const char *name)
                    475: {
                    476:        struct pattr    *attr;
                    477:        enum attrkey     key;
1.1       schwarze  478:
1.5       schwarze  479:        if (ps->del > 0 || *name == '\0')
                    480:                return;
                    481:        if ((key = attrkey_parse(name)) == ATTRKEY__MAX) {
                    482:                ps->attr = 0;
                    483:                return;
                    484:        }
                    485:        if ((attr = calloc(1, sizeof(*attr))) == NULL) {
                    486:                perror(NULL);
                    487:                exit(1);
                    488:        }
                    489:        attr->key = key;
                    490:        attr->val = ATTRVAL__MAX;
                    491:        attr->rawval = NULL;
                    492:        TAILQ_INSERT_TAIL(&ps->cur->attrq, attr, child);
                    493:        ps->attr = 1;
                    494: }
                    495:
                    496: static void
                    497: xml_attrval(struct parse *ps, const char *name)
                    498: {
                    499:        struct pattr    *attr;
                    500:
                    501:        if (ps->del > 0 || ps->attr == 0)
                    502:                return;
                    503:        if ((attr = TAILQ_LAST(&ps->cur->attrq, pattrq)) == NULL)
                    504:                return;
                    505:        if ((attr->val = attrval_parse(name)) == ATTRVAL__MAX &&
                    506:            (attr->rawval = strdup(name)) == NULL) {
                    507:                perror(NULL);
                    508:                exit(1);
1.1       schwarze  509:        }
                    510: }
                    511:
                    512: /*
                    513:  * Roll up the parse tree.
                    514:  * If we're at a text node, roll that one up first.
                    515:  */
                    516: static void
1.5       schwarze  517: xml_elem_end(struct parse *ps, const char *name)
1.1       schwarze  518: {
1.5       schwarze  519:        const struct element    *elem;
                    520:        enum nodeid              node;
1.1       schwarze  521:
1.4       schwarze  522:        /*
                    523:         * An ancestor is excluded from the tree;
                    524:         * keep track of the number of levels excluded.
                    525:         */
                    526:        if (ps->del > 1) {
                    527:                ps->del--;
                    528:                return;
                    529:        }
                    530:
1.16    ! schwarze  531:        if (ps->del == 0)
        !           532:                pnode_closetext(ps);
1.2       schwarze  533:
1.5       schwarze  534:        if (name != NULL) {
                    535:                for (elem = elements; elem->name != NULL; elem++)
                    536:                        if (strcmp(elem->name, name) == 0)
                    537:                                break;
                    538:                node = elem->node;
                    539:        } else
                    540:                node = ps->ncur;
1.2       schwarze  541:
1.5       schwarze  542:        switch (node) {
1.4       schwarze  543:        case NODE_DELETE_WARN:
                    544:        case NODE_DELETE:
1.5       schwarze  545:                if (ps->del > 0)
                    546:                        ps->del--;
1.4       schwarze  547:                break;
1.2       schwarze  548:        case NODE_IGNORE:
                    549:                break;
                    550:        default:
1.5       schwarze  551:                if (ps->cur == NULL || node != ps->cur->node) {
1.6       schwarze  552:                        warn_msg(ps, "element not open: </%s>", name);
1.5       schwarze  553:                        break;
                    554:                }
                    555:
                    556:                /*
                    557:                 * Refrain from actually closing the document element.
                    558:                 * If no more content follows, no harm is done, but if
                    559:                 * some content still follows, simply processing it is
                    560:                 * obviously better than discarding it or crashing.
                    561:                 */
                    562:
                    563:                if (ps->cur->parent == NULL)
                    564:                        ps->tree->flags |= TREE_CLOSED;
                    565:                else
                    566:                        ps->cur = ps->cur->parent;
1.16    ! schwarze  567:                ps->spc = 0;
1.4       schwarze  568:                break;
1.2       schwarze  569:        }
1.4       schwarze  570:        assert(ps->del == 0);
1.1       schwarze  571: }
                    572:
                    573: struct parse *
                    574: parse_alloc(int warn)
                    575: {
                    576:        struct parse    *p;
                    577:
                    578:        if ((p = calloc(1, sizeof(*p))) == NULL)
                    579:                return NULL;
                    580:
                    581:        if ((p->tree = calloc(1, sizeof(*p->tree))) == NULL) {
                    582:                free(p);
                    583:                return NULL;
                    584:        }
                    585:        p->warn = warn;
                    586:        return p;
                    587: }
                    588:
                    589: void
                    590: parse_free(struct parse *p)
                    591: {
                    592:        if (p == NULL)
                    593:                return;
                    594:        if (p->tree != NULL) {
                    595:                pnode_unlink(p->tree->root);
                    596:                free(p->tree);
                    597:        }
                    598:        free(p);
                    599: }
                    600:
1.14      schwarze  601: static void
                    602: increment(struct parse *p, char *b, size_t *pend, int refill)
                    603: {
                    604:        if (refill) {
                    605:                if (b[*pend] == '\n') {
                    606:                        p->nline++;
                    607:                        p->ncol = 1;
                    608:                } else
                    609:                        p->ncol++;
                    610:        }
                    611:        ++*pend;
                    612: }
                    613:
1.5       schwarze  614: /*
                    615:  * Advance the pend pointer to the next character in the charset.
                    616:  * If the charset starts with a space, it stands for any whitespace.
                    617:  * Update the new input file position, used for messages.
                    618:  * Do not overrun the buffer b of length rlen.
                    619:  * When reaching the end, NUL-terminate the buffer and return 1;
                    620:  * otherwise, return 0.
                    621:  */
                    622: static int
                    623: advance(struct parse *p, char *b, size_t rlen, size_t *pend,
1.14      schwarze  624:     const char *charset, int refill)
1.5       schwarze  625: {
                    626:        int              space;
                    627:
                    628:        if (*charset == ' ') {
                    629:                space = 1;
                    630:                charset++;
                    631:        } else
                    632:                space = 0;
                    633:
1.14      schwarze  634:        if (refill) {
                    635:                p->nline = p->line;
                    636:                p->ncol = p->col;
                    637:        }
1.5       schwarze  638:        while (*pend < rlen) {
                    639:                if (space && isspace((unsigned char)b[*pend]))
                    640:                        break;
                    641:                if (strchr(charset, b[*pend]) != NULL)
                    642:                        break;
1.14      schwarze  643:                increment(p, b, pend, refill);
1.5       schwarze  644:        }
                    645:        if (*pend == rlen) {
                    646:                b[rlen] = '\0';
1.14      schwarze  647:                return refill;
1.5       schwarze  648:        } else
                    649:                return 0;
                    650: }
                    651:
1.14      schwarze  652: size_t
                    653: parse_string(struct parse *p, char *b, size_t rlen,
                    654:     enum pstate *pstate, int refill)
                    655: {
                    656:        char            *cp;
                    657:        size_t           poff;  /* Parse offset in b[]. */
                    658:        size_t           pend;  /* Offset of the end of the current word. */
                    659:        int              elem_end;
                    660:
1.16    ! schwarze  661:        p->spc = 0;
1.14      schwarze  662:        pend = 0;
                    663:        for (;;) {
                    664:
                    665:                /* Proceed to the next token, skipping whitespace. */
                    666:
                    667:                if (refill) {
                    668:                        p->line = p->nline;
                    669:                        p->col = p->ncol;
                    670:                }
                    671:                if ((poff = pend) == rlen)
                    672:                        break;
                    673:                if (isspace((unsigned char)b[pend])) {
1.16    ! schwarze  674:                        p->spc = 1;
1.14      schwarze  675:                        increment(p, b, &pend, refill);
                    676:                        continue;
                    677:                }
                    678:
                    679:                /*
                    680:                 * The following four cases (ARG, TAG, and starting an
                    681:                 * entity or a tag) all parse a word or quoted string.
                    682:                 * If that extends beyond the read buffer and the last
                    683:                 * read(2) still got data, they all break out of the
                    684:                 * token loop to request more data from the read loop.
                    685:                 *
                    686:                 * Also, three of them detect self-closing tags, those
                    687:                 * ending with "/>", setting the flag elem_end and
                    688:                 * calling xml_elem_end() at the very end, after
                    689:                 * handling the attribute value, attribute name, or
                    690:                 * tag name, respectively.
                    691:                 */
                    692:
                    693:                /* Parse an attribute value. */
                    694:
                    695:                if (*pstate >= PARSE_ARG) {
                    696:                        if (*pstate == PARSE_ARG &&
                    697:                            (b[pend] == '\'' || b[pend] == '"')) {
                    698:                                *pstate = b[pend] == '"' ?
                    699:                                    PARSE_DQ : PARSE_SQ;
                    700:                                increment(p, b, &pend, refill);
                    701:                                continue;
                    702:                        }
                    703:                        if (advance(p, b, rlen, &pend,
                    704:                            *pstate == PARSE_DQ ? "\"" :
                    705:                            *pstate == PARSE_SQ ? "'" : " >", refill))
                    706:                                break;
                    707:                        *pstate = PARSE_TAG;
                    708:                        elem_end = 0;
                    709:                        if (b[pend] == '>') {
                    710:                                *pstate = PARSE_ELEM;
                    711:                                if (pend > 0 && b[pend - 1] == '/') {
                    712:                                        b[pend - 1] = '\0';
                    713:                                        elem_end = 1;
                    714:                                }
                    715:                        }
                    716:                        b[pend] = '\0';
                    717:                        if (pend < rlen)
                    718:                                increment(p, b, &pend, refill);
                    719:                        xml_attrval(p, b + poff);
                    720:                        if (elem_end)
                    721:                                xml_elem_end(p, NULL);
                    722:
                    723:                /* Look for an attribute name. */
                    724:
                    725:                } else if (*pstate == PARSE_TAG) {
                    726:                        if (advance(p, b, rlen, &pend, " =>", refill))
                    727:                                break;
                    728:                        elem_end = 0;
                    729:                        switch (b[pend]) {
                    730:                        case '>':
                    731:                                *pstate = PARSE_ELEM;
                    732:                                if (pend > 0 && b[pend - 1] == '/') {
                    733:                                        b[pend - 1] = '\0';
                    734:                                        elem_end = 1;
                    735:                                }
                    736:                                break;
                    737:                        case '=':
                    738:                                *pstate = PARSE_ARG;
                    739:                                break;
                    740:                        default:
                    741:                                break;
                    742:                        }
                    743:                        b[pend] = '\0';
                    744:                        if (pend < rlen)
                    745:                                increment(p, b, &pend, refill);
                    746:                        xml_attrkey(p, b + poff);
                    747:                        if (elem_end)
                    748:                                xml_elem_end(p, NULL);
                    749:
                    750:                /* Begin an opening or closing tag. */
                    751:
                    752:                } else if (b[poff] == '<') {
                    753:                        if (advance(p, b, rlen, &pend, " >", refill))
                    754:                                break;
                    755:                        if (pend > poff + 3 &&
                    756:                            strncmp(b + poff, "<!--", 4) == 0) {
                    757:
                    758:                                /* Skip a comment. */
                    759:
                    760:                                cp = strstr(b + pend - 2, "-->");
                    761:                                if (cp == NULL) {
                    762:                                        if (refill)
                    763:                                                break;
                    764:                                        cp = b + rlen;
                    765:                                } else
                    766:                                        cp += 3;
                    767:                                while (b + pend < cp)
                    768:                                        increment(p, b, &pend, refill);
                    769:                                continue;
                    770:                        }
                    771:                        elem_end = 0;
                    772:                        if (b[pend] != '>')
                    773:                                *pstate = PARSE_TAG;
                    774:                        else if (pend > 0 && b[pend - 1] == '/') {
                    775:                                b[pend - 1] = '\0';
                    776:                                elem_end = 1;
                    777:                        }
                    778:                        b[pend] = '\0';
                    779:                        if (pend < rlen)
                    780:                                increment(p, b, &pend, refill);
                    781:                        if (b[++poff] == '/') {
                    782:                                elem_end = 1;
                    783:                                poff++;
                    784:                        } else
                    785:                                xml_elem_start(p, b + poff);
                    786:                        if (elem_end)
                    787:                                xml_elem_end(p, b + poff);
                    788:
                    789:                /* Process an entity. */
                    790:
                    791:                } else if (b[poff] == '&') {
                    792:                        if (advance(p, b, rlen, &pend, ";", refill))
                    793:                                break;
                    794:                        b[pend] = '\0';
                    795:                        if (pend < rlen)
                    796:                                increment(p, b, &pend, refill);
                    797:                        xml_entity(p, b + poff + 1);
                    798:
                    799:                /* Process text up to the next tag, entity, or EOL. */
                    800:
                    801:                } else {
                    802:                        advance(p, b, rlen, &pend, "<&", refill);
                    803:                        xml_char(p, b + poff, pend - poff);
                    804:                }
                    805:        }
                    806:        return poff;
                    807: }
                    808:
1.1       schwarze  809: struct ptree *
                    810: parse_file(struct parse *p, int fd, const char *fname)
                    811: {
                    812:        char             b[4096];
1.5       schwarze  813:        ssize_t          rsz;   /* Return value from read(2). */
1.14      schwarze  814:        size_t           rlen;  /* Number of bytes in b[]. */
1.5       schwarze  815:        size_t           poff;  /* Parse offset in b[]. */
1.14      schwarze  816:        enum pstate      pstate;
1.1       schwarze  817:
                    818:        p->fname = fname;
1.5       schwarze  819:        p->nline = 1;
                    820:        p->ncol = 1;
1.14      schwarze  821:        pstate = PARSE_ELEM;
1.5       schwarze  822:        rlen = 0;
                    823:
                    824:        /*
                    825:         * Read loop.
                    826:         *
1.14      schwarze  827:          * If the previous token was incomplete and asked for more
                    828:          * input, we have to enter the read loop once more even on EOF.
1.5       schwarze  829:         * Once rsz is 0, incomplete tokens will no longer ask
                    830:         * for more input but instead use whatever there is,
                    831:         * and then exit the read loop.
                    832:         * The minus one on the size limit for read(2) is needed
                    833:         * such that advance() can set b[rlen] to NUL when needed.
                    834:         */
                    835:
1.14      schwarze  836:        while ((rsz = read(fd, b + rlen, sizeof(b) - rlen - 1)) >= 0 &&
                    837:            (rlen += rsz) > 0) {
                    838:                poff = parse_string(p, b, rlen, &pstate, rsz > 0);
1.5       schwarze  839:                /* Buffer exhausted; shift left and re-fill. */
                    840:                assert(poff > 0);
                    841:                rlen -= poff;
1.14      schwarze  842:                memmove(b, b + poff, rlen);
1.5       schwarze  843:        }
                    844:        if (rsz < 0) {
                    845:                perror(fname);
                    846:                p->tree->flags |= TREE_FAIL;
                    847:        }
1.16    ! schwarze  848:        pnode_closetext(p);
1.6       schwarze  849:        if ((p->tree->flags & TREE_CLOSED) == 0)
                    850:                warn_msg(p, "document not closed");
1.1       schwarze  851:        return p->tree;
                    852: }

CVSweb