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

Annotation of docbook2mdoc/parse.c, Revision 1.26

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

CVSweb