[BACK]Return to mdoc.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/mdoc.c, Revision 1.16

1.16    ! schwarze    1: /*     $Id: mdoc.c,v 1.15 2009/07/12 21:08:29 schwarze Exp $ */
1.1       kristaps    2: /*
1.3       schwarze    3:  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
1.1       kristaps    4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
1.3       schwarze    6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    8:  *
1.3       schwarze    9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       kristaps   16:  */
                     17: #include <assert.h>
                     18: #include <ctype.h>
                     19: #include <stdarg.h>
                     20: #include <stdio.h>
                     21: #include <stdlib.h>
                     22: #include <string.h>
                     23:
                     24: #include "libmdoc.h"
                     25:
1.14      schwarze   26: const  char *const __mdoc_merrnames[MERRMAX] = {
                     27:        "trailing whitespace", /* ETAILWS */
                     28:        "empty last list column", /* ECOLEMPTY */
                     29:        "argument-like parameter", /* EARGVPARM */
                     30:        "unexpected quoted parameter", /* EQUOTPARM */
                     31:        "unterminated quoted parameter", /* EQUOTTERM */
                     32:        "system: malloc error", /* EMALLOC */
                     33:        "argument parameter suggested", /* EARGVAL */
                     34:        "macro not callable", /* ENOCALL */
                     35:        "macro disallowed in prologue", /* EBODYPROL */
                     36:        "macro disallowed in body", /* EPROLBODY */
                     37:        "text disallowed in prologue", /* ETEXTPROL */
                     38:        "blank line disallowed", /* ENOBLANK */
                     39:        "text parameter too long", /* ETOOLONG */
                     40:        "invalid escape sequence", /* EESCAPE */
                     41:        "invalid character", /* EPRINT */
                     42:        "document has no body", /* ENODAT */
                     43:        "document has no prologue", /* ENOPROLOGUE */
                     44:        "expected line arguments", /* ELINE */
                     45:        "invalid AT&T argument", /* EATT */
                     46:        "default name not yet set", /* ENAME */
                     47:        "missing list type", /* ELISTTYPE */
                     48:        "missing display type", /* EDISPTYPE */
                     49:        "too many display types", /* EMULTIDISP */
                     50:        "too many list types", /* EMULTILIST */
                     51:        "NAME section must be first", /* ESECNAME */
                     52:        "badly-formed NAME section", /* ENAMESECINC */
                     53:        "argument repeated", /* EARGREP */
                     54:        "expected boolean parameter", /* EBOOL */
                     55:        "inconsistent column syntax", /* ECOLMIS */
                     56:        "nested display invalid", /* ENESTDISP */
                     57:        "width argument missing", /* EMISSWIDTH */
                     58:        "invalid section for this manual section", /* EWRONGMSEC */
                     59:        "section out of conventional order", /* ESECOOO */
                     60:        "section repeated", /* ESECREP */
                     61:        "invalid standard argument", /* EBADSTAND */
                     62:        "multi-line arguments discouraged", /* ENOMULTILINE */
                     63:        "multi-line arguments suggested", /* EMULTILINE */
                     64:        "line arguments discouraged", /* ENOLINE */
                     65:        "prologue macro out of conventional order", /* EPROLOOO */
                     66:        "prologue macro repeated", /* EPROLREP */
                     67:        "invalid manual section", /* EBADMSEC */
                     68:        "invalid section", /* EBADSEC */
                     69:        "invalid font mode", /* EFONT */
                     70:        "invalid date syntax", /* EBADDATE */
                     71:        "invalid number format", /* ENUMFMT */
                     72:        "superfluous width argument", /* ENOWIDTH */
                     73:        "system: utsname error", /* EUTSNAME */
                     74:        "obsolete macro", /* EOBS */
                     75:        "macro-like parameter", /* EMACPARM */
                     76:        "end-of-line scope violation", /* EIMPBRK */
                     77:        "empty macro ignored", /* EIGNE */
                     78:        "unclosed explicit scope", /* EOPEN */
                     79:        "unterminated quoted phrase", /* EQUOTPHR */
                     80:        "closure macro without prior context", /* ENOCTX */
                     81:        "invalid whitespace after control character", /* ESPACE */
1.1       kristaps   82: };
                     83:
                     84: const  char *const __mdoc_macronames[MDOC_MAX] = {
1.7       schwarze   85:        "Ap",           "Dd",           "Dt",           "Os",
1.1       kristaps   86:        "Sh",           "Ss",           "Pp",           "D1",
                     87:        "Dl",           "Bd",           "Ed",           "Bl",
                     88:        "El",           "It",           "Ad",           "An",
                     89:        "Ar",           "Cd",           "Cm",           "Dv",
                     90:        "Er",           "Ev",           "Ex",           "Fa",
                     91:        "Fd",           "Fl",           "Fn",           "Ft",
                     92:        "Ic",           "In",           "Li",           "Nd",
                     93:        "Nm",           "Op",           "Ot",           "Pa",
                     94:        "Rv",           "St",           "Va",           "Vt",
                     95:        /* LINTED */
                     96:        "Xr",           "\%A",          "\%B",          "\%D",
                     97:        /* LINTED */
                     98:        "\%I",          "\%J",          "\%N",          "\%O",
                     99:        /* LINTED */
                    100:        "\%P",          "\%R",          "\%T",          "\%V",
                    101:        "Ac",           "Ao",           "Aq",           "At",
                    102:        "Bc",           "Bf",           "Bo",           "Bq",
                    103:        "Bsx",          "Bx",           "Db",           "Dc",
                    104:        "Do",           "Dq",           "Ec",           "Ef",
                    105:        "Em",           "Eo",           "Fx",           "Ms",
                    106:        "No",           "Ns",           "Nx",           "Ox",
                    107:        "Pc",           "Pf",           "Po",           "Pq",
                    108:        "Qc",           "Ql",           "Qo",           "Qq",
                    109:        "Re",           "Rs",           "Sc",           "So",
                    110:        "Sq",           "Sm",           "Sx",           "Sy",
                    111:        "Tn",           "Ux",           "Xc",           "Xo",
                    112:        "Fo",           "Fc",           "Oo",           "Oc",
                    113:        "Bk",           "Ek",           "Bt",           "Hf",
1.7       schwarze  114:        "Fr",           "Ud",           "Lb",           "Lp",
                    115:        "Lk",           "Mt",           "Brq",          "Bro",
1.1       kristaps  116:        /* LINTED */
1.7       schwarze  117:        "Brc",          "\%C",          "Es",           "En",
1.1       kristaps  118:        /* LINTED */
1.7       schwarze  119:        "Dx",           "\%Q"
1.1       kristaps  120:        };
                    121:
                    122: const  char *const __mdoc_argnames[MDOC_ARG_MAX] = {
                    123:        "split",                "nosplit",              "ragged",
                    124:        "unfilled",             "literal",              "file",
                    125:        "offset",               "bullet",               "dash",
                    126:        "hyphen",               "item",                 "enum",
                    127:        "tag",                  "diag",                 "hang",
                    128:        "ohang",                "inset",                "column",
                    129:        "width",                "compact",              "std",
                    130:        "filled",               "words",                "emphasis",
                    131:        "symbolic",             "nested"
                    132:        };
                    133:
                    134: const  char * const *mdoc_macronames = __mdoc_macronames;
                    135: const  char * const *mdoc_argnames = __mdoc_argnames;
                    136:
                    137: static void              mdoc_free1(struct mdoc *);
                    138: static int               mdoc_alloc1(struct mdoc *);
                    139: static struct mdoc_node *node_alloc(struct mdoc *, int, int,
                    140:                                int, enum mdoc_type);
                    141: static int               node_append(struct mdoc *,
                    142:                                struct mdoc_node *);
                    143: static int               parsetext(struct mdoc *, int, char *);
                    144: static int               parsemacro(struct mdoc *, int, char *);
                    145: static int               macrowarn(struct mdoc *, int, const char *);
1.16    ! schwarze  146:
1.1       kristaps  147:
                    148: const struct mdoc_node *
                    149: mdoc_node(const struct mdoc *m)
                    150: {
                    151:
                    152:        return(MDOC_HALT & m->flags ? NULL : m->first);
                    153: }
                    154:
                    155:
                    156: const struct mdoc_meta *
                    157: mdoc_meta(const struct mdoc *m)
                    158: {
                    159:
                    160:        return(MDOC_HALT & m->flags ? NULL : &m->meta);
                    161: }
                    162:
                    163:
1.8       schwarze  164: /*
                    165:  * Frees volatile resources (parse tree, meta-data, fields).
                    166:  */
1.1       kristaps  167: static void
                    168: mdoc_free1(struct mdoc *mdoc)
                    169: {
                    170:
                    171:        if (mdoc->first)
                    172:                mdoc_node_freelist(mdoc->first);
                    173:        if (mdoc->meta.title)
                    174:                free(mdoc->meta.title);
                    175:        if (mdoc->meta.os)
                    176:                free(mdoc->meta.os);
                    177:        if (mdoc->meta.name)
                    178:                free(mdoc->meta.name);
                    179:        if (mdoc->meta.arch)
                    180:                free(mdoc->meta.arch);
                    181:        if (mdoc->meta.vol)
                    182:                free(mdoc->meta.vol);
                    183: }
                    184:
                    185:
1.8       schwarze  186: /*
                    187:  * Allocate all volatile resources (parse tree, meta-data, fields).
                    188:  */
1.1       kristaps  189: static int
                    190: mdoc_alloc1(struct mdoc *mdoc)
                    191: {
                    192:
                    193:        bzero(&mdoc->meta, sizeof(struct mdoc_meta));
                    194:        mdoc->flags = 0;
1.9       schwarze  195:        mdoc->lastnamed = mdoc->lastsec = SEC_NONE;
1.1       kristaps  196:        mdoc->last = calloc(1, sizeof(struct mdoc_node));
                    197:        if (NULL == mdoc->last)
                    198:                return(0);
                    199:
                    200:        mdoc->first = mdoc->last;
                    201:        mdoc->last->type = MDOC_ROOT;
                    202:        mdoc->next = MDOC_NEXT_CHILD;
                    203:        return(1);
                    204: }
                    205:
                    206:
                    207: /*
1.8       schwarze  208:  * Free up volatile resources (see mdoc_free1()) then re-initialises the
                    209:  * data with mdoc_alloc1().  After invocation, parse data has been reset
                    210:  * and the parser is ready for re-invocation on a new tree; however,
                    211:  * cross-parse non-volatile data is kept intact.
1.1       kristaps  212:  */
                    213: int
                    214: mdoc_reset(struct mdoc *mdoc)
                    215: {
                    216:
                    217:        mdoc_free1(mdoc);
                    218:        return(mdoc_alloc1(mdoc));
                    219: }
                    220:
                    221:
                    222: /*
1.8       schwarze  223:  * Completely free up all volatile and non-volatile parse resources.
                    224:  * After invocation, the pointer is no longer usable.
1.1       kristaps  225:  */
                    226: void
                    227: mdoc_free(struct mdoc *mdoc)
                    228: {
                    229:
                    230:        mdoc_free1(mdoc);
                    231:        if (mdoc->htab)
                    232:                mdoc_hash_free(mdoc->htab);
                    233:        free(mdoc);
                    234: }
                    235:
                    236:
1.8       schwarze  237: /*
                    238:  * Allocate volatile and non-volatile parse resources.
                    239:  */
1.1       kristaps  240: struct mdoc *
                    241: mdoc_alloc(void *data, int pflags, const struct mdoc_cb *cb)
                    242: {
                    243:        struct mdoc     *p;
                    244:
                    245:        if (NULL == (p = calloc(1, sizeof(struct mdoc))))
                    246:                return(NULL);
                    247:        if (cb)
                    248:                (void)memcpy(&p->cb, cb, sizeof(struct mdoc_cb));
                    249:
                    250:        p->data = data;
                    251:        p->pflags = pflags;
                    252:
                    253:        if (NULL == (p->htab = mdoc_hash_alloc())) {
                    254:                free(p);
                    255:                return(NULL);
                    256:        } else if (mdoc_alloc1(p))
                    257:                return(p);
                    258:
                    259:        free(p);
                    260:        return(NULL);
                    261: }
                    262:
                    263:
                    264: /*
                    265:  * Climb back up the parse tree, validating open scopes.  Mostly calls
1.8       schwarze  266:  * through to macro_end() in macro.c.
1.1       kristaps  267:  */
                    268: int
                    269: mdoc_endparse(struct mdoc *m)
                    270: {
                    271:
                    272:        if (MDOC_HALT & m->flags)
                    273:                return(0);
                    274:        else if (mdoc_macroend(m))
                    275:                return(1);
                    276:        m->flags |= MDOC_HALT;
                    277:        return(0);
                    278: }
                    279:
                    280:
                    281: /*
                    282:  * Main parse routine.  Parses a single line -- really just hands off to
1.8       schwarze  283:  * the macro (parsemacro()) or text parser (parsetext()).
1.1       kristaps  284:  */
                    285: int
                    286: mdoc_parseln(struct mdoc *m, int ln, char *buf)
                    287: {
                    288:
                    289:        if (MDOC_HALT & m->flags)
                    290:                return(0);
                    291:
                    292:        return('.' == *buf ? parsemacro(m, ln, buf) :
                    293:                        parsetext(m, ln, buf));
                    294: }
                    295:
                    296:
                    297: int
1.4       schwarze  298: mdoc_verr(struct mdoc *mdoc, int ln, int pos,
                    299:                const char *fmt, ...)
1.1       kristaps  300: {
                    301:        char             buf[256];
                    302:        va_list          ap;
                    303:
                    304:        if (NULL == mdoc->cb.mdoc_err)
                    305:                return(0);
                    306:
                    307:        va_start(ap, fmt);
                    308:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
                    309:        va_end(ap);
1.12      schwarze  310:
1.1       kristaps  311:        return((*mdoc->cb.mdoc_err)(mdoc->data, ln, pos, buf));
                    312: }
                    313:
                    314:
                    315: int
1.12      schwarze  316: mdoc_vwarn(struct mdoc *mdoc, int ln, int pos, const char *fmt, ...)
1.1       kristaps  317: {
                    318:        char             buf[256];
                    319:        va_list          ap;
                    320:
                    321:        if (NULL == mdoc->cb.mdoc_warn)
                    322:                return(0);
                    323:
                    324:        va_start(ap, fmt);
                    325:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
                    326:        va_end(ap);
1.11      schwarze  327:        return((*mdoc->cb.mdoc_warn)(mdoc->data, ln, pos, buf));
1.5       schwarze  328: }
                    329:
                    330:
                    331: int
1.8       schwarze  332: mdoc_warn(struct mdoc *mdoc, enum mdoc_warn type,
                    333:                const char *fmt, ...)
1.5       schwarze  334: {
                    335:        char             buf[256];
                    336:        va_list          ap;
                    337:
                    338:        if (NULL == mdoc->cb.mdoc_warn)
                    339:                return(0);
                    340:
                    341:        va_start(ap, fmt);
                    342:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
                    343:        va_end(ap);
                    344:        return((*mdoc->cb.mdoc_warn)(mdoc->data, mdoc->last->line,
1.11      schwarze  345:                                mdoc->last->pos, buf));
1.5       schwarze  346: }
                    347:
                    348:
                    349: int
1.14      schwarze  350: mdoc_err(struct mdoc *m, int line, int pos, int iserr, enum merr type)
1.5       schwarze  351: {
1.14      schwarze  352:        const char      *p;
1.5       schwarze  353:
1.14      schwarze  354:        p = __mdoc_merrnames[(int)type];
                    355:        assert(p);
1.5       schwarze  356:
1.14      schwarze  357:        if (iserr)
                    358:                return(mdoc_verr(m, line, pos, p));
1.16    ! schwarze  359:
1.14      schwarze  360:        return(mdoc_vwarn(m, line, pos, p));
1.5       schwarze  361: }
                    362:
                    363:
                    364: int
                    365: mdoc_pwarn(struct mdoc *mdoc, int line, int pos, enum mdoc_warn type,
                    366:                const char *fmt, ...)
                    367: {
                    368:        char             buf[256];
                    369:        va_list          ap;
                    370:
                    371:        if (NULL == mdoc->cb.mdoc_warn)
                    372:                return(0);
                    373:
                    374:        va_start(ap, fmt);
                    375:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
                    376:        va_end(ap);
1.11      schwarze  377:        return((*mdoc->cb.mdoc_warn)(mdoc->data, line, pos, buf));
1.5       schwarze  378: }
                    379:
                    380: int
1.1       kristaps  381: mdoc_macro(struct mdoc *m, int tok,
                    382:                int ln, int pp, int *pos, char *buf)
                    383: {
                    384:
                    385:        if (MDOC_PROLOGUE & mdoc_macros[tok].flags &&
1.9       schwarze  386:                        MDOC_PBODY & m->flags)
1.16    ! schwarze  387:                return(mdoc_perr(m, ln, pp, EPROLBODY));
1.1       kristaps  388:        if ( ! (MDOC_PROLOGUE & mdoc_macros[tok].flags) &&
1.9       schwarze  389:                        ! (MDOC_PBODY & m->flags))
1.16    ! schwarze  390:                return(mdoc_perr(m, ln, pp, EBODYPROL));
1.1       kristaps  391:
                    392:        if (1 != pp && ! (MDOC_CALLABLE & mdoc_macros[tok].flags))
1.16    ! schwarze  393:                return(mdoc_perr(m, ln, pp, ENOCALL));
1.1       kristaps  394:
                    395:        return((*mdoc_macros[tok].fp)(m, tok, ln, pp, pos, buf));
                    396: }
                    397:
                    398:
                    399: static int
                    400: node_append(struct mdoc *mdoc, struct mdoc_node *p)
                    401: {
                    402:
                    403:        assert(mdoc->last);
                    404:        assert(mdoc->first);
                    405:        assert(MDOC_ROOT != p->type);
                    406:
                    407:        switch (mdoc->next) {
                    408:        case (MDOC_NEXT_SIBLING):
                    409:                mdoc->last->next = p;
                    410:                p->prev = mdoc->last;
                    411:                p->parent = mdoc->last->parent;
                    412:                break;
                    413:        case (MDOC_NEXT_CHILD):
                    414:                mdoc->last->child = p;
                    415:                p->parent = mdoc->last;
                    416:                break;
                    417:        default:
                    418:                abort();
                    419:                /* NOTREACHED */
                    420:        }
                    421:
1.10      schwarze  422:        p->parent->nchild++;
                    423:
1.1       kristaps  424:        if ( ! mdoc_valid_pre(mdoc, p))
                    425:                return(0);
                    426:        if ( ! mdoc_action_pre(mdoc, p))
                    427:                return(0);
                    428:
                    429:        switch (p->type) {
                    430:        case (MDOC_HEAD):
                    431:                assert(MDOC_BLOCK == p->parent->type);
                    432:                p->parent->head = p;
                    433:                break;
                    434:        case (MDOC_TAIL):
                    435:                assert(MDOC_BLOCK == p->parent->type);
                    436:                p->parent->tail = p;
                    437:                break;
                    438:        case (MDOC_BODY):
                    439:                assert(MDOC_BLOCK == p->parent->type);
                    440:                p->parent->body = p;
                    441:                break;
                    442:        default:
                    443:                break;
                    444:        }
                    445:
                    446:        mdoc->last = p;
                    447:
                    448:        switch (p->type) {
                    449:        case (MDOC_TEXT):
                    450:                if ( ! mdoc_valid_post(mdoc))
                    451:                        return(0);
                    452:                if ( ! mdoc_action_post(mdoc))
                    453:                        return(0);
                    454:                break;
                    455:        default:
                    456:                break;
                    457:        }
                    458:
                    459:        return(1);
                    460: }
                    461:
                    462:
                    463: static struct mdoc_node *
                    464: node_alloc(struct mdoc *mdoc, int line,
                    465:                int pos, int tok, enum mdoc_type type)
                    466: {
                    467:        struct mdoc_node *p;
                    468:
                    469:        if (NULL == (p = calloc(1, sizeof(struct mdoc_node)))) {
1.15      schwarze  470:                (void)mdoc_nerr(mdoc, mdoc->last, EMALLOC);
1.1       kristaps  471:                return(NULL);
                    472:        }
                    473:
                    474:        p->sec = mdoc->lastsec;
                    475:        p->line = line;
                    476:        p->pos = pos;
                    477:        p->tok = tok;
                    478:        if (MDOC_TEXT != (p->type = type))
                    479:                assert(p->tok >= 0);
                    480:
                    481:        return(p);
                    482: }
                    483:
                    484:
                    485: int
                    486: mdoc_tail_alloc(struct mdoc *mdoc, int line, int pos, int tok)
                    487: {
                    488:        struct mdoc_node *p;
                    489:
                    490:        p = node_alloc(mdoc, line, pos, tok, MDOC_TAIL);
                    491:        if (NULL == p)
                    492:                return(0);
                    493:        return(node_append(mdoc, p));
                    494: }
                    495:
                    496:
                    497: int
                    498: mdoc_head_alloc(struct mdoc *mdoc, int line, int pos, int tok)
                    499: {
                    500:        struct mdoc_node *p;
                    501:
                    502:        assert(mdoc->first);
                    503:        assert(mdoc->last);
                    504:
                    505:        p = node_alloc(mdoc, line, pos, tok, MDOC_HEAD);
                    506:        if (NULL == p)
                    507:                return(0);
                    508:        return(node_append(mdoc, p));
                    509: }
                    510:
                    511:
                    512: int
                    513: mdoc_body_alloc(struct mdoc *mdoc, int line, int pos, int tok)
                    514: {
                    515:        struct mdoc_node *p;
                    516:
                    517:        p = node_alloc(mdoc, line, pos, tok, MDOC_BODY);
                    518:        if (NULL == p)
                    519:                return(0);
                    520:        return(node_append(mdoc, p));
                    521: }
                    522:
                    523:
                    524: int
                    525: mdoc_block_alloc(struct mdoc *mdoc, int line, int pos,
                    526:                int tok, struct mdoc_arg *args)
                    527: {
                    528:        struct mdoc_node *p;
                    529:
                    530:        p = node_alloc(mdoc, line, pos, tok, MDOC_BLOCK);
                    531:        if (NULL == p)
                    532:                return(0);
1.4       schwarze  533:        p->args = args;
                    534:        if (p->args)
1.1       kristaps  535:                (args->refcnt)++;
                    536:        return(node_append(mdoc, p));
                    537: }
                    538:
                    539:
                    540: int
                    541: mdoc_elem_alloc(struct mdoc *mdoc, int line, int pos,
                    542:                int tok, struct mdoc_arg *args)
                    543: {
                    544:        struct mdoc_node *p;
                    545:
                    546:        p = node_alloc(mdoc, line, pos, tok, MDOC_ELEM);
                    547:        if (NULL == p)
                    548:                return(0);
1.4       schwarze  549:        p->args = args;
                    550:        if (p->args)
1.1       kristaps  551:                (args->refcnt)++;
                    552:        return(node_append(mdoc, p));
                    553: }
                    554:
                    555:
                    556: int
                    557: mdoc_word_alloc(struct mdoc *mdoc,
                    558:                int line, int pos, const char *word)
                    559: {
                    560:        struct mdoc_node *p;
                    561:
                    562:        p = node_alloc(mdoc, line, pos, -1, MDOC_TEXT);
                    563:        if (NULL == p)
                    564:                return(0);
                    565:        if (NULL == (p->string = strdup(word))) {
1.15      schwarze  566:                (void)mdoc_nerr(mdoc, mdoc->last, EMALLOC);
1.1       kristaps  567:                return(0);
                    568:        }
1.15      schwarze  569:
1.1       kristaps  570:        return(node_append(mdoc, p));
                    571: }
                    572:
                    573:
                    574: void
                    575: mdoc_node_free(struct mdoc_node *p)
                    576: {
                    577:
1.10      schwarze  578:        if (p->parent)
                    579:                p->parent->nchild--;
1.1       kristaps  580:        if (p->string)
                    581:                free(p->string);
                    582:        if (p->args)
                    583:                mdoc_argv_free(p->args);
                    584:        free(p);
                    585: }
                    586:
                    587:
                    588: void
                    589: mdoc_node_freelist(struct mdoc_node *p)
                    590: {
                    591:
                    592:        if (p->child)
                    593:                mdoc_node_freelist(p->child);
                    594:        if (p->next)
                    595:                mdoc_node_freelist(p->next);
                    596:
1.10      schwarze  597:        assert(0 == p->nchild);
1.1       kristaps  598:        mdoc_node_free(p);
                    599: }
                    600:
                    601:
                    602: /*
                    603:  * Parse free-form text, that is, a line that does not begin with the
                    604:  * control character.
                    605:  */
                    606: static int
                    607: parsetext(struct mdoc *m, int line, char *buf)
                    608: {
                    609:
1.9       schwarze  610:        if (SEC_NONE == m->lastnamed)
1.16    ! schwarze  611:                return(mdoc_perr(m, line, 0, ETEXTPROL));
1.1       kristaps  612:
                    613:        if (0 == buf[0] && ! (MDOC_LITERAL & m->flags))
1.16    ! schwarze  614:                return(mdoc_perr(m, line, 0, ENOBLANK));
1.1       kristaps  615:
                    616:        if ( ! mdoc_word_alloc(m, line, 0, buf))
                    617:                return(0);
                    618:
                    619:        m->next = MDOC_NEXT_SIBLING;
                    620:        return(1);
                    621: }
                    622:
                    623:
                    624: static int
                    625: macrowarn(struct mdoc *m, int ln, const char *buf)
                    626: {
                    627:        if ( ! (MDOC_IGN_MACRO & m->pflags))
1.13      schwarze  628:                return(mdoc_verr(m, ln, 1,
1.1       kristaps  629:                                "unknown macro: %s%s",
                    630:                                buf, strlen(buf) > 3 ? "..." : ""));
1.12      schwarze  631:        return(mdoc_vwarn(m, ln, 1, "unknown macro: %s%s",
1.1       kristaps  632:                                buf, strlen(buf) > 3 ? "..." : ""));
                    633: }
                    634:
                    635:
                    636: /*
                    637:  * Parse a macro line, that is, a line beginning with the control
                    638:  * character.
                    639:  */
                    640: int
                    641: parsemacro(struct mdoc *m, int ln, char *buf)
                    642: {
                    643:        int               i, c;
                    644:        char              mac[5];
                    645:
1.7       schwarze  646:        /* Empty lines are ignored. */
1.1       kristaps  647:
                    648:        if (0 == buf[1])
                    649:                return(1);
                    650:
                    651:        if (' ' == buf[1]) {
                    652:                i = 2;
                    653:                while (buf[i] && ' ' == buf[i])
                    654:                        i++;
                    655:                if (0 == buf[i])
                    656:                        return(1);
1.16    ! schwarze  657:                return(mdoc_perr(m, ln, 1, ESPACE));
1.1       kristaps  658:        }
                    659:
                    660:        /* Copy the first word into a nil-terminated buffer. */
                    661:
                    662:        for (i = 1; i < 5; i++) {
                    663:                if (0 == (mac[i - 1] = buf[i]))
                    664:                        break;
                    665:                else if (' ' == buf[i])
                    666:                        break;
                    667:        }
                    668:
                    669:        mac[i - 1] = 0;
                    670:
                    671:        if (i == 5 || i <= 2) {
                    672:                if ( ! macrowarn(m, ln, mac))
                    673:                        goto err;
                    674:                return(1);
                    675:        }
                    676:
                    677:        if (MDOC_MAX == (c = mdoc_hash_find(m->htab, mac))) {
                    678:                if ( ! macrowarn(m, ln, mac))
                    679:                        goto err;
                    680:                return(1);
                    681:        }
                    682:
                    683:        /* The macro is sane.  Jump to the next word. */
                    684:
                    685:        while (buf[i] && ' ' == buf[i])
                    686:                i++;
                    687:
                    688:        /* Begin recursive parse sequence. */
                    689:
                    690:        if ( ! mdoc_macro(m, c, ln, 1, &i, buf))
                    691:                goto err;
                    692:
                    693:        return(1);
                    694:
                    695: err:   /* Error out. */
                    696:
                    697:        m->flags |= MDOC_HALT;
                    698:        return(0);
                    699: }