[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.73

1.73    ! schwarze    1: /*     $Id: mdoc.c,v 1.72 2010/12/21 23:46:17 schwarze Exp $ */
1.1       kristaps    2: /*
1.61      schwarze    3:  * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
                      4:  * Copyright (c) 2010 Ingo Schwarze <schwarze@openbsd.org>
1.1       kristaps    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
1.3       schwarze    7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    9:  *
1.3       schwarze   10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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.
1.1       kristaps   17:  */
1.28      schwarze   18: #include <sys/types.h>
                     19:
1.1       kristaps   20: #include <assert.h>
                     21: #include <stdarg.h>
                     22: #include <stdio.h>
                     23: #include <stdlib.h>
                     24: #include <string.h>
1.39      schwarze   25: #include <time.h>
1.1       kristaps   26:
1.54      schwarze   27: #include "mandoc.h"
1.1       kristaps   28: #include "libmdoc.h"
1.32      schwarze   29: #include "libmandoc.h"
1.1       kristaps   30:
1.67      schwarze   31: #include "out.h"
                     32: #include "term.h"
                     33: #include "tbl.h"
                     34:
1.1       kristaps   35: const  char *const __mdoc_macronames[MDOC_MAX] = {
1.7       schwarze   36:        "Ap",           "Dd",           "Dt",           "Os",
1.1       kristaps   37:        "Sh",           "Ss",           "Pp",           "D1",
                     38:        "Dl",           "Bd",           "Ed",           "Bl",
                     39:        "El",           "It",           "Ad",           "An",
                     40:        "Ar",           "Cd",           "Cm",           "Dv",
                     41:        "Er",           "Ev",           "Ex",           "Fa",
                     42:        "Fd",           "Fl",           "Fn",           "Ft",
                     43:        "Ic",           "In",           "Li",           "Nd",
                     44:        "Nm",           "Op",           "Ot",           "Pa",
                     45:        "Rv",           "St",           "Va",           "Vt",
                     46:        /* LINTED */
1.33      schwarze   47:        "Xr",           "%A",           "%B",           "%D",
1.1       kristaps   48:        /* LINTED */
1.33      schwarze   49:        "%I",           "%J",           "%N",           "%O",
1.1       kristaps   50:        /* LINTED */
1.33      schwarze   51:        "%P",           "%R",           "%T",           "%V",
1.1       kristaps   52:        "Ac",           "Ao",           "Aq",           "At",
                     53:        "Bc",           "Bf",           "Bo",           "Bq",
                     54:        "Bsx",          "Bx",           "Db",           "Dc",
                     55:        "Do",           "Dq",           "Ec",           "Ef",
                     56:        "Em",           "Eo",           "Fx",           "Ms",
                     57:        "No",           "Ns",           "Nx",           "Ox",
                     58:        "Pc",           "Pf",           "Po",           "Pq",
                     59:        "Qc",           "Ql",           "Qo",           "Qq",
                     60:        "Re",           "Rs",           "Sc",           "So",
                     61:        "Sq",           "Sm",           "Sx",           "Sy",
                     62:        "Tn",           "Ux",           "Xc",           "Xo",
                     63:        "Fo",           "Fc",           "Oo",           "Oc",
                     64:        "Bk",           "Ek",           "Bt",           "Hf",
1.7       schwarze   65:        "Fr",           "Ud",           "Lb",           "Lp",
                     66:        "Lk",           "Mt",           "Brq",          "Bro",
1.1       kristaps   67:        /* LINTED */
1.33      schwarze   68:        "Brc",          "%C",           "Es",           "En",
1.1       kristaps   69:        /* LINTED */
1.33      schwarze   70:        "Dx",           "%Q",           "br",           "sp",
1.31      schwarze   71:        /* LINTED */
1.67      schwarze   72:        "%U",           "Ta",           "TS",           "TE"
1.1       kristaps   73:        };
                     74:
                     75: const  char *const __mdoc_argnames[MDOC_ARG_MAX] = {
                     76:        "split",                "nosplit",              "ragged",
                     77:        "unfilled",             "literal",              "file",
                     78:        "offset",               "bullet",               "dash",
                     79:        "hyphen",               "item",                 "enum",
                     80:        "tag",                  "diag",                 "hang",
                     81:        "ohang",                "inset",                "column",
                     82:        "width",                "compact",              "std",
                     83:        "filled",               "words",                "emphasis",
1.30      schwarze   84:        "symbolic",             "nested",               "centered"
1.1       kristaps   85:        };
                     86:
                     87: const  char * const *mdoc_macronames = __mdoc_macronames;
                     88: const  char * const *mdoc_argnames = __mdoc_argnames;
                     89:
1.40      schwarze   90: static void              mdoc_node_free(struct mdoc_node *);
                     91: static void              mdoc_node_unlink(struct mdoc *,
                     92:                                struct mdoc_node *);
1.1       kristaps   93: static void              mdoc_free1(struct mdoc *);
1.32      schwarze   94: static void              mdoc_alloc1(struct mdoc *);
1.1       kristaps   95: static struct mdoc_node *node_alloc(struct mdoc *, int, int,
1.37      schwarze   96:                                enum mdoct, enum mdoc_type);
1.1       kristaps   97: static int               node_append(struct mdoc *,
                     98:                                struct mdoc_node *);
1.53      schwarze   99: static int               mdoc_ptext(struct mdoc *, int, char *, int);
                    100: static int               mdoc_pmacro(struct mdoc *, int, char *, int);
1.46      schwarze  101:
1.1       kristaps  102:
                    103: const struct mdoc_node *
                    104: mdoc_node(const struct mdoc *m)
                    105: {
                    106:
                    107:        return(MDOC_HALT & m->flags ? NULL : m->first);
                    108: }
                    109:
                    110:
                    111: const struct mdoc_meta *
                    112: mdoc_meta(const struct mdoc *m)
                    113: {
                    114:
                    115:        return(MDOC_HALT & m->flags ? NULL : &m->meta);
                    116: }
                    117:
                    118:
1.8       schwarze  119: /*
                    120:  * Frees volatile resources (parse tree, meta-data, fields).
                    121:  */
1.1       kristaps  122: static void
                    123: mdoc_free1(struct mdoc *mdoc)
                    124: {
                    125:
                    126:        if (mdoc->first)
1.40      schwarze  127:                mdoc_node_delete(mdoc, mdoc->first);
1.1       kristaps  128:        if (mdoc->meta.title)
                    129:                free(mdoc->meta.title);
                    130:        if (mdoc->meta.os)
                    131:                free(mdoc->meta.os);
                    132:        if (mdoc->meta.name)
                    133:                free(mdoc->meta.name);
                    134:        if (mdoc->meta.arch)
                    135:                free(mdoc->meta.arch);
                    136:        if (mdoc->meta.vol)
                    137:                free(mdoc->meta.vol);
1.50      schwarze  138:        if (mdoc->meta.msec)
                    139:                free(mdoc->meta.msec);
1.1       kristaps  140: }
                    141:
                    142:
1.8       schwarze  143: /*
                    144:  * Allocate all volatile resources (parse tree, meta-data, fields).
                    145:  */
1.32      schwarze  146: static void
1.1       kristaps  147: mdoc_alloc1(struct mdoc *mdoc)
                    148: {
                    149:
1.32      schwarze  150:        memset(&mdoc->meta, 0, sizeof(struct mdoc_meta));
1.1       kristaps  151:        mdoc->flags = 0;
1.9       schwarze  152:        mdoc->lastnamed = mdoc->lastsec = SEC_NONE;
1.32      schwarze  153:        mdoc->last = mandoc_calloc(1, sizeof(struct mdoc_node));
1.1       kristaps  154:        mdoc->first = mdoc->last;
                    155:        mdoc->last->type = MDOC_ROOT;
                    156:        mdoc->next = MDOC_NEXT_CHILD;
                    157: }
                    158:
                    159:
                    160: /*
1.8       schwarze  161:  * Free up volatile resources (see mdoc_free1()) then re-initialises the
                    162:  * data with mdoc_alloc1().  After invocation, parse data has been reset
                    163:  * and the parser is ready for re-invocation on a new tree; however,
                    164:  * cross-parse non-volatile data is kept intact.
1.1       kristaps  165:  */
1.32      schwarze  166: void
1.1       kristaps  167: mdoc_reset(struct mdoc *mdoc)
                    168: {
                    169:
                    170:        mdoc_free1(mdoc);
1.32      schwarze  171:        mdoc_alloc1(mdoc);
1.1       kristaps  172: }
                    173:
                    174:
                    175: /*
1.8       schwarze  176:  * Completely free up all volatile and non-volatile parse resources.
                    177:  * After invocation, the pointer is no longer usable.
1.1       kristaps  178:  */
                    179: void
                    180: mdoc_free(struct mdoc *mdoc)
                    181: {
                    182:
                    183:        mdoc_free1(mdoc);
                    184:        free(mdoc);
                    185: }
                    186:
                    187:
1.8       schwarze  188: /*
                    189:  * Allocate volatile and non-volatile parse resources.
                    190:  */
1.1       kristaps  191: struct mdoc *
1.65      schwarze  192: mdoc_alloc(struct regset *regs, void *data, mandocmsg msg)
1.1       kristaps  193: {
                    194:        struct mdoc     *p;
                    195:
1.32      schwarze  196:        p = mandoc_calloc(1, sizeof(struct mdoc));
                    197:
1.54      schwarze  198:        p->msg = msg;
1.1       kristaps  199:        p->data = data;
1.58      schwarze  200:        p->regs = regs;
1.1       kristaps  201:
1.32      schwarze  202:        mdoc_hash_init();
                    203:        mdoc_alloc1(p);
                    204:        return(p);
1.1       kristaps  205: }
                    206:
                    207:
                    208: /*
                    209:  * Climb back up the parse tree, validating open scopes.  Mostly calls
1.8       schwarze  210:  * through to macro_end() in macro.c.
1.1       kristaps  211:  */
                    212: int
                    213: mdoc_endparse(struct mdoc *m)
                    214: {
                    215:
                    216:        if (MDOC_HALT & m->flags)
                    217:                return(0);
                    218:        else if (mdoc_macroend(m))
                    219:                return(1);
                    220:        m->flags |= MDOC_HALT;
                    221:        return(0);
                    222: }
                    223:
                    224:
                    225: /*
                    226:  * Main parse routine.  Parses a single line -- really just hands off to
1.45      schwarze  227:  * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()).
1.1       kristaps  228:  */
                    229: int
1.53      schwarze  230: mdoc_parseln(struct mdoc *m, int ln, char *buf, int offs)
1.1       kristaps  231: {
1.67      schwarze  232:        struct mdoc_node *n;
1.1       kristaps  233:
                    234:        if (MDOC_HALT & m->flags)
                    235:                return(0);
                    236:
1.47      schwarze  237:        m->flags |= MDOC_NEWLINE;
1.60      schwarze  238:
1.67      schwarze  239:        n = m->last;
                    240:
                    241:        if (n && MDOC_TS == n->tok && MDOC_BODY == n->type &&
                    242:            strncmp(buf+offs, ".TE", 3)) {
                    243:                n = n->parent;
1.68      schwarze  244:                if ( ! tbl_read(n->data.TS, "mdoc tbl parser",
                    245:                    ln, buf+offs, strlen(buf+offs)))
                    246:                        mdoc_nmsg(m, n, MANDOCERR_TBL);
                    247:                return(1);
1.67      schwarze  248:        }
                    249:
1.60      schwarze  250:        /*
                    251:         * Let the roff nS register switch SYNOPSIS mode early,
                    252:         * such that the parser knows at all times
                    253:         * whether this mode is on or off.
                    254:         * Note that this mode is also switched by the Sh macro.
                    255:         */
                    256:        if (m->regs->regs[(int)REG_nS].set) {
                    257:                if (m->regs->regs[(int)REG_nS].v.u)
                    258:                        m->flags |= MDOC_SYNOPSIS;
                    259:                else
                    260:                        m->flags &= ~MDOC_SYNOPSIS;
                    261:        }
                    262:
1.53      schwarze  263:        return(('.' == buf[offs] || '\'' == buf[offs]) ?
                    264:                        mdoc_pmacro(m, ln, buf, offs) :
                    265:                        mdoc_ptext(m, ln, buf, offs));
1.1       kristaps  266: }
                    267:
                    268:
                    269: int
1.54      schwarze  270: mdoc_vmsg(struct mdoc *mdoc, enum mandocerr t,
                    271:                int ln, int pos, const char *fmt, ...)
1.1       kristaps  272: {
                    273:        char             buf[256];
                    274:        va_list          ap;
                    275:
                    276:        va_start(ap, fmt);
1.54      schwarze  277:        vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
1.1       kristaps  278:        va_end(ap);
1.17      schwarze  279:
1.54      schwarze  280:        return((*mdoc->msg)(t, mdoc->data, ln, pos, buf));
1.5       schwarze  281: }
                    282:
                    283:
                    284: int
1.58      schwarze  285: mdoc_macro(MACRO_PROT_ARGS)
1.1       kristaps  286: {
1.46      schwarze  287:        assert(tok < MDOC_MAX);
                    288:
                    289:        /* If we're in the body, deny prologue calls. */
1.37      schwarze  290:
1.1       kristaps  291:        if (MDOC_PROLOGUE & mdoc_macros[tok].flags &&
1.9       schwarze  292:                        MDOC_PBODY & m->flags)
1.58      schwarze  293:                return(mdoc_pmsg(m, line, ppos, MANDOCERR_BADBODY));
1.46      schwarze  294:
                    295:        /* If we're in the prologue, deny "body" macros.  */
                    296:
1.1       kristaps  297:        if ( ! (MDOC_PROLOGUE & mdoc_macros[tok].flags) &&
1.39      schwarze  298:                        ! (MDOC_PBODY & m->flags)) {
1.58      schwarze  299:                if ( ! mdoc_pmsg(m, line, ppos, MANDOCERR_BADPROLOG))
1.39      schwarze  300:                        return(0);
1.71      schwarze  301:                if (NULL == m->meta.msec)
                    302:                        m->meta.msec = mandoc_strdup("1");
1.39      schwarze  303:                if (NULL == m->meta.title)
1.56      schwarze  304:                        m->meta.title = mandoc_strdup("UNKNOWN");
1.39      schwarze  305:                if (NULL == m->meta.vol)
1.56      schwarze  306:                        m->meta.vol = mandoc_strdup("LOCAL");
1.39      schwarze  307:                if (NULL == m->meta.os)
1.56      schwarze  308:                        m->meta.os = mandoc_strdup("LOCAL");
1.39      schwarze  309:                if (0 == m->meta.date)
                    310:                        m->meta.date = time(NULL);
                    311:                m->flags |= MDOC_PBODY;
                    312:        }
1.1       kristaps  313:
1.58      schwarze  314:        return((*mdoc_macros[tok].fp)(m, tok, line, ppos, pos, buf));
1.1       kristaps  315: }
                    316:
                    317:
                    318: static int
                    319: node_append(struct mdoc *mdoc, struct mdoc_node *p)
                    320: {
                    321:
                    322:        assert(mdoc->last);
                    323:        assert(mdoc->first);
                    324:        assert(MDOC_ROOT != p->type);
                    325:
                    326:        switch (mdoc->next) {
                    327:        case (MDOC_NEXT_SIBLING):
                    328:                mdoc->last->next = p;
                    329:                p->prev = mdoc->last;
                    330:                p->parent = mdoc->last->parent;
                    331:                break;
                    332:        case (MDOC_NEXT_CHILD):
                    333:                mdoc->last->child = p;
                    334:                p->parent = mdoc->last;
                    335:                break;
                    336:        default:
                    337:                abort();
                    338:                /* NOTREACHED */
                    339:        }
                    340:
1.10      schwarze  341:        p->parent->nchild++;
                    342:
1.1       kristaps  343:        if ( ! mdoc_valid_pre(mdoc, p))
                    344:                return(0);
                    345:
                    346:        switch (p->type) {
                    347:        case (MDOC_HEAD):
                    348:                assert(MDOC_BLOCK == p->parent->type);
                    349:                p->parent->head = p;
                    350:                break;
                    351:        case (MDOC_TAIL):
                    352:                assert(MDOC_BLOCK == p->parent->type);
                    353:                p->parent->tail = p;
                    354:                break;
                    355:        case (MDOC_BODY):
1.59      schwarze  356:                if (p->end)
                    357:                        break;
1.1       kristaps  358:                assert(MDOC_BLOCK == p->parent->type);
                    359:                p->parent->body = p;
                    360:                break;
                    361:        default:
                    362:                break;
                    363:        }
                    364:
                    365:        mdoc->last = p;
                    366:
                    367:        switch (p->type) {
                    368:        case (MDOC_TEXT):
                    369:                if ( ! mdoc_valid_post(mdoc))
                    370:                        return(0);
                    371:                break;
                    372:        default:
                    373:                break;
                    374:        }
                    375:
                    376:        return(1);
                    377: }
                    378:
                    379:
                    380: static struct mdoc_node *
1.37      schwarze  381: node_alloc(struct mdoc *m, int line, int pos,
                    382:                enum mdoct tok, enum mdoc_type type)
1.1       kristaps  383: {
                    384:        struct mdoc_node *p;
                    385:
1.32      schwarze  386:        p = mandoc_calloc(1, sizeof(struct mdoc_node));
1.19      schwarze  387:        p->sec = m->lastsec;
1.1       kristaps  388:        p->line = line;
                    389:        p->pos = pos;
                    390:        p->tok = tok;
1.37      schwarze  391:        p->type = type;
1.58      schwarze  392:
                    393:        /* Flag analysis. */
                    394:
1.60      schwarze  395:        if (MDOC_SYNOPSIS & m->flags)
                    396:                p->flags |= MDOC_SYNPRETTY;
                    397:        else
                    398:                p->flags &= ~MDOC_SYNPRETTY;
1.47      schwarze  399:        if (MDOC_NEWLINE & m->flags)
                    400:                p->flags |= MDOC_LINE;
                    401:        m->flags &= ~MDOC_NEWLINE;
1.58      schwarze  402:
1.1       kristaps  403:        return(p);
                    404: }
                    405:
                    406:
                    407: int
1.37      schwarze  408: mdoc_tail_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.1       kristaps  409: {
                    410:        struct mdoc_node *p;
                    411:
1.19      schwarze  412:        p = node_alloc(m, line, pos, tok, MDOC_TAIL);
1.25      schwarze  413:        if ( ! node_append(m, p))
                    414:                return(0);
                    415:        m->next = MDOC_NEXT_CHILD;
                    416:        return(1);
1.1       kristaps  417: }
                    418:
                    419:
                    420: int
1.37      schwarze  421: mdoc_head_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.1       kristaps  422: {
                    423:        struct mdoc_node *p;
                    424:
1.19      schwarze  425:        assert(m->first);
                    426:        assert(m->last);
1.1       kristaps  427:
1.19      schwarze  428:        p = node_alloc(m, line, pos, tok, MDOC_HEAD);
1.25      schwarze  429:        if ( ! node_append(m, p))
                    430:                return(0);
                    431:        m->next = MDOC_NEXT_CHILD;
                    432:        return(1);
1.1       kristaps  433: }
                    434:
                    435:
                    436: int
1.37      schwarze  437: mdoc_body_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.1       kristaps  438: {
                    439:        struct mdoc_node *p;
                    440:
1.19      schwarze  441:        p = node_alloc(m, line, pos, tok, MDOC_BODY);
1.25      schwarze  442:        if ( ! node_append(m, p))
                    443:                return(0);
                    444:        m->next = MDOC_NEXT_CHILD;
1.59      schwarze  445:        return(1);
                    446: }
                    447:
                    448:
                    449: int
                    450: mdoc_endbody_alloc(struct mdoc *m, int line, int pos, enum mdoct tok,
                    451:                struct mdoc_node *body, enum mdoc_endbody end)
                    452: {
                    453:        struct mdoc_node *p;
                    454:
                    455:        p = node_alloc(m, line, pos, tok, MDOC_BODY);
                    456:        p->pending = body;
                    457:        p->end = end;
                    458:        if ( ! node_append(m, p))
                    459:                return(0);
                    460:        m->next = MDOC_NEXT_SIBLING;
1.25      schwarze  461:        return(1);
1.1       kristaps  462: }
                    463:
                    464:
                    465: int
1.19      schwarze  466: mdoc_block_alloc(struct mdoc *m, int line, int pos,
1.37      schwarze  467:                enum mdoct tok, struct mdoc_arg *args)
1.1       kristaps  468: {
                    469:        struct mdoc_node *p;
                    470:
1.19      schwarze  471:        p = node_alloc(m, line, pos, tok, MDOC_BLOCK);
1.4       schwarze  472:        p->args = args;
                    473:        if (p->args)
1.1       kristaps  474:                (args->refcnt)++;
1.25      schwarze  475:        if ( ! node_append(m, p))
                    476:                return(0);
                    477:        m->next = MDOC_NEXT_CHILD;
                    478:        return(1);
1.1       kristaps  479: }
                    480:
                    481:
                    482: int
1.19      schwarze  483: mdoc_elem_alloc(struct mdoc *m, int line, int pos,
1.37      schwarze  484:                enum mdoct tok, struct mdoc_arg *args)
1.1       kristaps  485: {
                    486:        struct mdoc_node *p;
                    487:
1.19      schwarze  488:        p = node_alloc(m, line, pos, tok, MDOC_ELEM);
1.4       schwarze  489:        p->args = args;
                    490:        if (p->args)
1.1       kristaps  491:                (args->refcnt)++;
1.25      schwarze  492:        if ( ! node_append(m, p))
                    493:                return(0);
                    494:        m->next = MDOC_NEXT_CHILD;
                    495:        return(1);
1.1       kristaps  496: }
                    497:
                    498:
1.46      schwarze  499: int
                    500: mdoc_word_alloc(struct mdoc *m, int line, int pos, const char *p)
1.1       kristaps  501: {
1.19      schwarze  502:        struct mdoc_node *n;
1.46      schwarze  503:        size_t            sv, len;
                    504:
                    505:        len = strlen(p);
1.1       kristaps  506:
1.46      schwarze  507:        n = node_alloc(m, line, pos, MDOC_MAX, MDOC_TEXT);
1.32      schwarze  508:        n->string = mandoc_malloc(len + 1);
1.19      schwarze  509:        sv = strlcpy(n->string, p, len + 1);
                    510:
                    511:        /* Prohibit truncation. */
                    512:        assert(sv < len + 1);
                    513:
1.25      schwarze  514:        if ( ! node_append(m, n))
                    515:                return(0);
1.46      schwarze  516:
1.25      schwarze  517:        m->next = MDOC_NEXT_SIBLING;
                    518:        return(1);
1.19      schwarze  519: }
                    520:
                    521:
1.61      schwarze  522: static void
1.1       kristaps  523: mdoc_node_free(struct mdoc_node *p)
                    524: {
                    525:
1.61      schwarze  526:        /*
                    527:         * XXX: if these end up being problematic in terms of memory
                    528:         * management and dereferencing freed blocks, then make them
                    529:         * into reference-counted double-pointers.
                    530:         */
                    531:
                    532:        if (MDOC_Bd == p->tok && MDOC_BLOCK == p->type)
                    533:                if (p->data.Bd)
                    534:                        free(p->data.Bd);
                    535:        if (MDOC_Bl == p->tok && MDOC_BLOCK == p->type)
                    536:                if (p->data.Bl)
                    537:                        free(p->data.Bl);
                    538:        if (MDOC_Bf == p->tok && MDOC_HEAD == p->type)
                    539:                if (p->data.Bf)
                    540:                        free(p->data.Bf);
1.73    ! schwarze  541:        if (MDOC_An == p->tok)
        !           542:                if (p->data.An)
        !           543:                        free(p->data.An);
1.67      schwarze  544:        if (MDOC_TS == p->tok && MDOC_BLOCK == p->type)
                    545:                if (p->data.TS)
                    546:                        tbl_free(p->data.TS);
1.61      schwarze  547:
1.1       kristaps  548:        if (p->string)
                    549:                free(p->string);
                    550:        if (p->args)
                    551:                mdoc_argv_free(p->args);
                    552:        free(p);
                    553: }
                    554:
                    555:
1.40      schwarze  556: static void
                    557: mdoc_node_unlink(struct mdoc *m, struct mdoc_node *n)
                    558: {
                    559:
                    560:        /* Adjust siblings. */
                    561:
                    562:        if (n->prev)
                    563:                n->prev->next = n->next;
                    564:        if (n->next)
                    565:                n->next->prev = n->prev;
                    566:
                    567:        /* Adjust parent. */
                    568:
                    569:        if (n->parent) {
                    570:                n->parent->nchild--;
                    571:                if (n->parent->child == n)
                    572:                        n->parent->child = n->prev ? n->prev : n->next;
1.72      schwarze  573:                if (n->parent->last == n)
                    574:                        n->parent->last = n->prev ? n->prev : NULL;
1.40      schwarze  575:        }
                    576:
                    577:        /* Adjust parse point, if applicable. */
                    578:
                    579:        if (m && m->last == n) {
                    580:                if (n->prev) {
                    581:                        m->last = n->prev;
                    582:                        m->next = MDOC_NEXT_SIBLING;
                    583:                } else {
                    584:                        m->last = n->parent;
                    585:                        m->next = MDOC_NEXT_CHILD;
                    586:                }
                    587:        }
                    588:
                    589:        if (m && m->first == n)
                    590:                m->first = NULL;
                    591: }
                    592:
                    593:
1.1       kristaps  594: void
1.40      schwarze  595: mdoc_node_delete(struct mdoc *m, struct mdoc_node *p)
1.1       kristaps  596: {
                    597:
1.40      schwarze  598:        while (p->child) {
                    599:                assert(p->nchild);
                    600:                mdoc_node_delete(m, p->child);
                    601:        }
                    602:        assert(0 == p->nchild);
1.1       kristaps  603:
1.40      schwarze  604:        mdoc_node_unlink(m, p);
1.1       kristaps  605:        mdoc_node_free(p);
                    606: }
                    607:
                    608:
                    609: /*
                    610:  * Parse free-form text, that is, a line that does not begin with the
                    611:  * control character.
                    612:  */
                    613: static int
1.53      schwarze  614: mdoc_ptext(struct mdoc *m, int line, char *buf, int offs)
1.1       kristaps  615: {
1.56      schwarze  616:        char             *c, *ws, *end;
                    617:        struct mdoc_node *n;
1.44      schwarze  618:
                    619:        /* Ignore bogus comments. */
                    620:
1.53      schwarze  621:        if ('\\' == buf[offs] &&
                    622:                        '.' == buf[offs + 1] &&
                    623:                        '"' == buf[offs + 2])
1.54      schwarze  624:                return(mdoc_pmsg(m, line, offs, MANDOCERR_BADCOMMENT));
1.1       kristaps  625:
1.46      schwarze  626:        /* No text before an initial macro. */
                    627:
1.9       schwarze  628:        if (SEC_NONE == m->lastnamed)
1.54      schwarze  629:                return(mdoc_pmsg(m, line, offs, MANDOCERR_NOTEXT));
1.46      schwarze  630:
1.56      schwarze  631:        assert(m->last);
                    632:        n = m->last;
                    633:
                    634:        /*
                    635:         * Divert directly to list processing if we're encountering a
                    636:         * columnar MDOC_BLOCK with or without a prior MDOC_BLOCK entry
                    637:         * (a MDOC_BODY means it's already open, in which case we should
                    638:         * process within its context in the normal way).
                    639:         */
                    640:
                    641:        if (MDOC_Bl == n->tok && MDOC_BODY == n->type &&
1.61      schwarze  642:                        LIST_column == n->data.Bl->type) {
1.56      schwarze  643:                /* `Bl' is open without any children. */
                    644:                m->flags |= MDOC_FREECOL;
                    645:                return(mdoc_macro(m, MDOC_It, line, offs, &offs, buf));
                    646:        }
                    647:
                    648:        if (MDOC_It == n->tok && MDOC_BLOCK == n->type &&
                    649:                        NULL != n->parent &&
                    650:                        MDOC_Bl == n->parent->tok &&
1.61      schwarze  651:                        LIST_column == n->parent->data.Bl->type) {
1.56      schwarze  652:                /* `Bl' has block-level `It' children. */
                    653:                m->flags |= MDOC_FREECOL;
                    654:                return(mdoc_macro(m, MDOC_It, line, offs, &offs, buf));
                    655:        }
                    656:
1.52      schwarze  657:        /*
                    658:         * Search for the beginning of unescaped trailing whitespace (ws)
                    659:         * and for the first character not to be output (end).
                    660:         */
1.56      schwarze  661:
                    662:        /* FIXME: replace with strcspn(). */
1.52      schwarze  663:        ws = NULL;
1.53      schwarze  664:        for (c = end = buf + offs; *c; c++) {
1.52      schwarze  665:                switch (*c) {
1.55      schwarze  666:                case '-':
                    667:                        if (mandoc_hyph(buf + offs, c))
                    668:                                *c = ASCII_HYPH;
1.56      schwarze  669:                        ws = NULL;
1.55      schwarze  670:                        break;
1.52      schwarze  671:                case ' ':
                    672:                        if (NULL == ws)
                    673:                                ws = c;
                    674:                        continue;
                    675:                case '\t':
                    676:                        /*
                    677:                         * Always warn about trailing tabs,
                    678:                         * even outside literal context,
                    679:                         * where they should be put on the next line.
                    680:                         */
                    681:                        if (NULL == ws)
                    682:                                ws = c;
                    683:                        /*
                    684:                         * Strip trailing tabs in literal context only;
                    685:                         * outside, they affect the next line.
                    686:                         */
                    687:                        if (MDOC_LITERAL & m->flags)
                    688:                                continue;
                    689:                        break;
                    690:                case '\\':
                    691:                        /* Skip the escaped character, too, if any. */
                    692:                        if (c[1])
                    693:                                c++;
                    694:                        /* FALLTHROUGH */
                    695:                default:
                    696:                        ws = NULL;
                    697:                        break;
                    698:                }
                    699:                end = c + 1;
                    700:        }
                    701:        *end = '\0';
1.19      schwarze  702:
1.52      schwarze  703:        if (ws)
1.54      schwarze  704:                if ( ! mdoc_pmsg(m, line, (int)(ws-buf), MANDOCERR_EOLNSPACE))
1.52      schwarze  705:                        return(0);
1.34      schwarze  706:
1.53      schwarze  707:        if ('\0' == buf[offs] && ! (MDOC_LITERAL & m->flags)) {
1.54      schwarze  708:                if ( ! mdoc_pmsg(m, line, (int)(c-buf), MANDOCERR_NOBLANKLN))
1.38      schwarze  709:                        return(0);
1.46      schwarze  710:
1.40      schwarze  711:                /*
1.66      schwarze  712:                 * Insert a `sp' in the case of a blank line.  Technically,
1.46      schwarze  713:                 * blank lines aren't allowed, but enough manuals assume this
                    714:                 * behaviour that we want to work around it.
1.40      schwarze  715:                 */
1.66      schwarze  716:                if ( ! mdoc_elem_alloc(m, line, offs, MDOC_sp, NULL))
1.42      schwarze  717:                        return(0);
1.46      schwarze  718:
1.42      schwarze  719:                m->next = MDOC_NEXT_SIBLING;
                    720:                return(1);
1.38      schwarze  721:        }
1.1       kristaps  722:
1.53      schwarze  723:        if ( ! mdoc_word_alloc(m, line, offs, buf+offs))
1.1       kristaps  724:                return(0);
                    725:
1.52      schwarze  726:        if (MDOC_LITERAL & m->flags)
                    727:                return(1);
                    728:
1.35      schwarze  729:        /*
1.48      schwarze  730:         * End-of-sentence check.  If the last character is an unescaped
                    731:         * EOS character, then flag the node as being the end of a
                    732:         * sentence.  The front-end will know how to interpret this.
1.35      schwarze  733:         */
                    734:
1.52      schwarze  735:        assert(buf < end);
1.48      schwarze  736:
1.62      schwarze  737:        if (mandoc_eos(buf+offs, (size_t)(end-buf-offs), 0))
1.48      schwarze  738:                m->last->flags |= MDOC_EOS;
                    739:
1.1       kristaps  740:        return(1);
                    741: }
1.19      schwarze  742:
1.1       kristaps  743:
                    744: /*
                    745:  * Parse a macro line, that is, a line beginning with the control
                    746:  * character.
                    747:  */
1.61      schwarze  748: static int
1.53      schwarze  749: mdoc_pmacro(struct mdoc *m, int ln, char *buf, int offs)
1.1       kristaps  750: {
1.56      schwarze  751:        enum mdoct        tok;
                    752:        int               i, j, sv;
                    753:        char              mac[5];
                    754:        struct mdoc_node *n;
1.1       kristaps  755:
1.7       schwarze  756:        /* Empty lines are ignored. */
1.1       kristaps  757:
1.53      schwarze  758:        offs++;
                    759:
                    760:        if ('\0' == buf[offs])
1.1       kristaps  761:                return(1);
                    762:
1.53      schwarze  763:        i = offs;
1.24      schwarze  764:
1.63      schwarze  765:        /* Accept tabs/whitespace after the initial control char. */
1.24      schwarze  766:
1.63      schwarze  767:        if (' ' == buf[i] || '\t' == buf[i]) {
1.24      schwarze  768:                i++;
1.63      schwarze  769:                while (buf[i] && (' ' == buf[i] || '\t' == buf[i]))
1.1       kristaps  770:                        i++;
1.34      schwarze  771:                if ('\0' == buf[i])
1.1       kristaps  772:                        return(1);
                    773:        }
                    774:
1.47      schwarze  775:        sv = i;
                    776:
1.63      schwarze  777:        /*
1.64      schwarze  778:         * Copy the first word into a nil-terminated buffer.
                    779:         * Stop copying when a tab, space, or eoln is encountered.
1.63      schwarze  780:         */
1.1       kristaps  781:
1.64      schwarze  782:        j = 0;
                    783:        while (j < 4 && '\0' != buf[i] && ' ' != buf[i] && '\t' != buf[i])
                    784:                mac[j++] = buf[i++];
1.53      schwarze  785:        mac[j] = '\0';
1.1       kristaps  786:
1.65      schwarze  787:        tok = (j > 1 || j < 4) ? mdoc_hash_find(mac) : MDOC_MAX;
                    788:        if (MDOC_MAX == tok) {
1.69      schwarze  789:                mdoc_vmsg(m, MANDOCERR_MACRO, ln, sv, "%s", buf + sv - 1);
1.1       kristaps  790:                return(1);
                    791:        }
                    792:
1.63      schwarze  793:        /* Disregard the first trailing tab, if applicable. */
                    794:
                    795:        if ('\t' == buf[i])
                    796:                i++;
                    797:
                    798:        /* Jump to the next non-whitespace word. */
1.1       kristaps  799:
                    800:        while (buf[i] && ' ' == buf[i])
                    801:                i++;
1.34      schwarze  802:
1.46      schwarze  803:        /*
                    804:         * Trailing whitespace.  Note that tabs are allowed to be passed
                    805:         * into the parser as "text", so we only warn about spaces here.
                    806:         */
1.34      schwarze  807:
                    808:        if ('\0' == buf[i] && ' ' == buf[i - 1])
1.54      schwarze  809:                if ( ! mdoc_pmsg(m, ln, i - 1, MANDOCERR_EOLNSPACE))
1.34      schwarze  810:                        goto err;
1.1       kristaps  811:
1.56      schwarze  812:        /*
                    813:         * If an initial macro or a list invocation, divert directly
                    814:         * into macro processing.
                    815:         */
                    816:
                    817:        if (NULL == m->last || MDOC_It == tok || MDOC_El == tok) {
                    818:                if ( ! mdoc_macro(m, tok, ln, sv, &i, buf))
                    819:                        goto err;
                    820:                return(1);
                    821:        }
                    822:
                    823:        n = m->last;
                    824:        assert(m->last);
                    825:
                    826:        /*
                    827:         * If the first macro of a `Bl -column', open an `It' block
                    828:         * context around the parsed macro.
                    829:         */
                    830:
                    831:        if (MDOC_Bl == n->tok && MDOC_BODY == n->type &&
1.61      schwarze  832:                        LIST_column == n->data.Bl->type) {
1.56      schwarze  833:                m->flags |= MDOC_FREECOL;
1.58      schwarze  834:                if ( ! mdoc_macro(m, MDOC_It, ln, sv, &sv, buf))
1.56      schwarze  835:                        goto err;
                    836:                return(1);
                    837:        }
                    838:
                    839:        /*
                    840:         * If we're following a block-level `It' within a `Bl -column'
                    841:         * context (perhaps opened in the above block or in ptext()),
                    842:         * then open an `It' block context around the parsed macro.
1.24      schwarze  843:         */
1.56      schwarze  844:
                    845:        if (MDOC_It == n->tok && MDOC_BLOCK == n->type &&
                    846:                        NULL != n->parent &&
                    847:                        MDOC_Bl == n->parent->tok &&
1.61      schwarze  848:                        LIST_column == n->parent->data.Bl->type) {
1.56      schwarze  849:                m->flags |= MDOC_FREECOL;
                    850:                if ( ! mdoc_macro(m, MDOC_It, ln, sv, &sv, buf))
                    851:                        goto err;
                    852:                return(1);
                    853:        }
                    854:
                    855:        /* Normal processing of a macro. */
                    856:
1.47      schwarze  857:        if ( ! mdoc_macro(m, tok, ln, sv, &i, buf))
1.1       kristaps  858:                goto err;
                    859:
                    860:        return(1);
                    861:
                    862: err:   /* Error out. */
                    863:
                    864:        m->flags |= MDOC_HALT;
                    865:        return(0);
                    866: }
1.24      schwarze  867:
                    868: