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

1.67    ! schwarze    1: /*     $Id: mdoc.c,v 1.66 2010/09/27 21:25:28 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;
        !           244:                return(tbl_read(n->data.TS, "<mdoc>", ln, buf+offs,
        !           245:                    strlen(buf+offs)) ? 1 : 0);
        !           246:        }
        !           247:
1.60      schwarze  248:        /*
                    249:         * Let the roff nS register switch SYNOPSIS mode early,
                    250:         * such that the parser knows at all times
                    251:         * whether this mode is on or off.
                    252:         * Note that this mode is also switched by the Sh macro.
                    253:         */
                    254:        if (m->regs->regs[(int)REG_nS].set) {
                    255:                if (m->regs->regs[(int)REG_nS].v.u)
                    256:                        m->flags |= MDOC_SYNOPSIS;
                    257:                else
                    258:                        m->flags &= ~MDOC_SYNOPSIS;
                    259:        }
                    260:
1.53      schwarze  261:        return(('.' == buf[offs] || '\'' == buf[offs]) ?
                    262:                        mdoc_pmacro(m, ln, buf, offs) :
                    263:                        mdoc_ptext(m, ln, buf, offs));
1.1       kristaps  264: }
                    265:
                    266:
                    267: int
1.54      schwarze  268: mdoc_vmsg(struct mdoc *mdoc, enum mandocerr t,
                    269:                int ln, int pos, const char *fmt, ...)
1.1       kristaps  270: {
                    271:        char             buf[256];
                    272:        va_list          ap;
                    273:
                    274:        va_start(ap, fmt);
1.54      schwarze  275:        vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
1.1       kristaps  276:        va_end(ap);
1.17      schwarze  277:
1.54      schwarze  278:        return((*mdoc->msg)(t, mdoc->data, ln, pos, buf));
1.5       schwarze  279: }
                    280:
                    281:
                    282: int
1.58      schwarze  283: mdoc_macro(MACRO_PROT_ARGS)
1.1       kristaps  284: {
1.46      schwarze  285:        assert(tok < MDOC_MAX);
                    286:
                    287:        /* If we're in the body, deny prologue calls. */
1.37      schwarze  288:
1.1       kristaps  289:        if (MDOC_PROLOGUE & mdoc_macros[tok].flags &&
1.9       schwarze  290:                        MDOC_PBODY & m->flags)
1.58      schwarze  291:                return(mdoc_pmsg(m, line, ppos, MANDOCERR_BADBODY));
1.46      schwarze  292:
                    293:        /* If we're in the prologue, deny "body" macros.  */
                    294:
1.1       kristaps  295:        if ( ! (MDOC_PROLOGUE & mdoc_macros[tok].flags) &&
1.39      schwarze  296:                        ! (MDOC_PBODY & m->flags)) {
1.58      schwarze  297:                if ( ! mdoc_pmsg(m, line, ppos, MANDOCERR_BADPROLOG))
1.39      schwarze  298:                        return(0);
                    299:                if (NULL == m->meta.title)
1.56      schwarze  300:                        m->meta.title = mandoc_strdup("UNKNOWN");
1.39      schwarze  301:                if (NULL == m->meta.vol)
1.56      schwarze  302:                        m->meta.vol = mandoc_strdup("LOCAL");
1.39      schwarze  303:                if (NULL == m->meta.os)
1.56      schwarze  304:                        m->meta.os = mandoc_strdup("LOCAL");
1.39      schwarze  305:                if (0 == m->meta.date)
                    306:                        m->meta.date = time(NULL);
                    307:                m->flags |= MDOC_PBODY;
                    308:        }
1.1       kristaps  309:
1.58      schwarze  310:        return((*mdoc_macros[tok].fp)(m, tok, line, ppos, pos, buf));
1.1       kristaps  311: }
                    312:
                    313:
                    314: static int
                    315: node_append(struct mdoc *mdoc, struct mdoc_node *p)
                    316: {
                    317:
                    318:        assert(mdoc->last);
                    319:        assert(mdoc->first);
                    320:        assert(MDOC_ROOT != p->type);
                    321:
                    322:        switch (mdoc->next) {
                    323:        case (MDOC_NEXT_SIBLING):
                    324:                mdoc->last->next = p;
                    325:                p->prev = mdoc->last;
                    326:                p->parent = mdoc->last->parent;
                    327:                break;
                    328:        case (MDOC_NEXT_CHILD):
                    329:                mdoc->last->child = p;
                    330:                p->parent = mdoc->last;
                    331:                break;
                    332:        default:
                    333:                abort();
                    334:                /* NOTREACHED */
                    335:        }
                    336:
1.10      schwarze  337:        p->parent->nchild++;
                    338:
1.1       kristaps  339:        if ( ! mdoc_valid_pre(mdoc, p))
                    340:                return(0);
                    341:        if ( ! mdoc_action_pre(mdoc, p))
                    342:                return(0);
                    343:
                    344:        switch (p->type) {
                    345:        case (MDOC_HEAD):
                    346:                assert(MDOC_BLOCK == p->parent->type);
                    347:                p->parent->head = p;
                    348:                break;
                    349:        case (MDOC_TAIL):
                    350:                assert(MDOC_BLOCK == p->parent->type);
                    351:                p->parent->tail = p;
                    352:                break;
                    353:        case (MDOC_BODY):
1.59      schwarze  354:                if (p->end)
                    355:                        break;
1.1       kristaps  356:                assert(MDOC_BLOCK == p->parent->type);
                    357:                p->parent->body = p;
                    358:                break;
                    359:        default:
                    360:                break;
                    361:        }
                    362:
                    363:        mdoc->last = p;
                    364:
                    365:        switch (p->type) {
                    366:        case (MDOC_TEXT):
                    367:                if ( ! mdoc_valid_post(mdoc))
                    368:                        return(0);
                    369:                if ( ! mdoc_action_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.67    ! schwarze  541:        if (MDOC_TS == p->tok && MDOC_BLOCK == p->type)
        !           542:                if (p->data.TS)
        !           543:                        tbl_free(p->data.TS);
1.61      schwarze  544:
1.1       kristaps  545:        if (p->string)
                    546:                free(p->string);
                    547:        if (p->args)
                    548:                mdoc_argv_free(p->args);
                    549:        free(p);
                    550: }
                    551:
                    552:
1.40      schwarze  553: static void
                    554: mdoc_node_unlink(struct mdoc *m, struct mdoc_node *n)
                    555: {
                    556:
                    557:        /* Adjust siblings. */
                    558:
                    559:        if (n->prev)
                    560:                n->prev->next = n->next;
                    561:        if (n->next)
                    562:                n->next->prev = n->prev;
                    563:
                    564:        /* Adjust parent. */
                    565:
                    566:        if (n->parent) {
                    567:                n->parent->nchild--;
                    568:                if (n->parent->child == n)
                    569:                        n->parent->child = n->prev ? n->prev : n->next;
                    570:        }
                    571:
                    572:        /* Adjust parse point, if applicable. */
                    573:
                    574:        if (m && m->last == n) {
                    575:                if (n->prev) {
                    576:                        m->last = n->prev;
                    577:                        m->next = MDOC_NEXT_SIBLING;
                    578:                } else {
                    579:                        m->last = n->parent;
                    580:                        m->next = MDOC_NEXT_CHILD;
                    581:                }
                    582:        }
                    583:
                    584:        if (m && m->first == n)
                    585:                m->first = NULL;
                    586: }
                    587:
                    588:
1.1       kristaps  589: void
1.40      schwarze  590: mdoc_node_delete(struct mdoc *m, struct mdoc_node *p)
1.1       kristaps  591: {
                    592:
1.40      schwarze  593:        while (p->child) {
                    594:                assert(p->nchild);
                    595:                mdoc_node_delete(m, p->child);
                    596:        }
                    597:        assert(0 == p->nchild);
1.1       kristaps  598:
1.40      schwarze  599:        mdoc_node_unlink(m, p);
1.1       kristaps  600:        mdoc_node_free(p);
                    601: }
                    602:
                    603:
                    604: /*
                    605:  * Parse free-form text, that is, a line that does not begin with the
                    606:  * control character.
                    607:  */
                    608: static int
1.53      schwarze  609: mdoc_ptext(struct mdoc *m, int line, char *buf, int offs)
1.1       kristaps  610: {
1.56      schwarze  611:        char             *c, *ws, *end;
                    612:        struct mdoc_node *n;
1.44      schwarze  613:
                    614:        /* Ignore bogus comments. */
                    615:
1.53      schwarze  616:        if ('\\' == buf[offs] &&
                    617:                        '.' == buf[offs + 1] &&
                    618:                        '"' == buf[offs + 2])
1.54      schwarze  619:                return(mdoc_pmsg(m, line, offs, MANDOCERR_BADCOMMENT));
1.1       kristaps  620:
1.46      schwarze  621:        /* No text before an initial macro. */
                    622:
1.9       schwarze  623:        if (SEC_NONE == m->lastnamed)
1.54      schwarze  624:                return(mdoc_pmsg(m, line, offs, MANDOCERR_NOTEXT));
1.46      schwarze  625:
1.56      schwarze  626:        assert(m->last);
                    627:        n = m->last;
                    628:
                    629:        /*
                    630:         * Divert directly to list processing if we're encountering a
                    631:         * columnar MDOC_BLOCK with or without a prior MDOC_BLOCK entry
                    632:         * (a MDOC_BODY means it's already open, in which case we should
                    633:         * process within its context in the normal way).
                    634:         */
                    635:
                    636:        if (MDOC_Bl == n->tok && MDOC_BODY == n->type &&
1.61      schwarze  637:                        LIST_column == n->data.Bl->type) {
1.56      schwarze  638:                /* `Bl' is open without any children. */
                    639:                m->flags |= MDOC_FREECOL;
                    640:                return(mdoc_macro(m, MDOC_It, line, offs, &offs, buf));
                    641:        }
                    642:
                    643:        if (MDOC_It == n->tok && MDOC_BLOCK == n->type &&
                    644:                        NULL != n->parent &&
                    645:                        MDOC_Bl == n->parent->tok &&
1.61      schwarze  646:                        LIST_column == n->parent->data.Bl->type) {
1.56      schwarze  647:                /* `Bl' has block-level `It' children. */
                    648:                m->flags |= MDOC_FREECOL;
                    649:                return(mdoc_macro(m, MDOC_It, line, offs, &offs, buf));
                    650:        }
                    651:
1.52      schwarze  652:        /*
                    653:         * Search for the beginning of unescaped trailing whitespace (ws)
                    654:         * and for the first character not to be output (end).
                    655:         */
1.56      schwarze  656:
                    657:        /* FIXME: replace with strcspn(). */
1.52      schwarze  658:        ws = NULL;
1.53      schwarze  659:        for (c = end = buf + offs; *c; c++) {
1.52      schwarze  660:                switch (*c) {
1.55      schwarze  661:                case '-':
                    662:                        if (mandoc_hyph(buf + offs, c))
                    663:                                *c = ASCII_HYPH;
1.56      schwarze  664:                        ws = NULL;
1.55      schwarze  665:                        break;
1.52      schwarze  666:                case ' ':
                    667:                        if (NULL == ws)
                    668:                                ws = c;
                    669:                        continue;
                    670:                case '\t':
                    671:                        /*
                    672:                         * Always warn about trailing tabs,
                    673:                         * even outside literal context,
                    674:                         * where they should be put on the next line.
                    675:                         */
                    676:                        if (NULL == ws)
                    677:                                ws = c;
                    678:                        /*
                    679:                         * Strip trailing tabs in literal context only;
                    680:                         * outside, they affect the next line.
                    681:                         */
                    682:                        if (MDOC_LITERAL & m->flags)
                    683:                                continue;
                    684:                        break;
                    685:                case '\\':
                    686:                        /* Skip the escaped character, too, if any. */
                    687:                        if (c[1])
                    688:                                c++;
                    689:                        /* FALLTHROUGH */
                    690:                default:
                    691:                        ws = NULL;
                    692:                        break;
                    693:                }
                    694:                end = c + 1;
                    695:        }
                    696:        *end = '\0';
1.19      schwarze  697:
1.52      schwarze  698:        if (ws)
1.54      schwarze  699:                if ( ! mdoc_pmsg(m, line, (int)(ws-buf), MANDOCERR_EOLNSPACE))
1.52      schwarze  700:                        return(0);
1.34      schwarze  701:
1.53      schwarze  702:        if ('\0' == buf[offs] && ! (MDOC_LITERAL & m->flags)) {
1.54      schwarze  703:                if ( ! mdoc_pmsg(m, line, (int)(c-buf), MANDOCERR_NOBLANKLN))
1.38      schwarze  704:                        return(0);
1.46      schwarze  705:
1.40      schwarze  706:                /*
1.66      schwarze  707:                 * Insert a `sp' in the case of a blank line.  Technically,
1.46      schwarze  708:                 * blank lines aren't allowed, but enough manuals assume this
                    709:                 * behaviour that we want to work around it.
1.40      schwarze  710:                 */
1.66      schwarze  711:                if ( ! mdoc_elem_alloc(m, line, offs, MDOC_sp, NULL))
1.42      schwarze  712:                        return(0);
1.46      schwarze  713:
1.42      schwarze  714:                m->next = MDOC_NEXT_SIBLING;
                    715:                return(1);
1.38      schwarze  716:        }
1.1       kristaps  717:
1.53      schwarze  718:        if ( ! mdoc_word_alloc(m, line, offs, buf+offs))
1.1       kristaps  719:                return(0);
                    720:
1.52      schwarze  721:        if (MDOC_LITERAL & m->flags)
                    722:                return(1);
                    723:
1.35      schwarze  724:        /*
1.48      schwarze  725:         * End-of-sentence check.  If the last character is an unescaped
                    726:         * EOS character, then flag the node as being the end of a
                    727:         * sentence.  The front-end will know how to interpret this.
1.35      schwarze  728:         */
                    729:
1.52      schwarze  730:        assert(buf < end);
1.48      schwarze  731:
1.62      schwarze  732:        if (mandoc_eos(buf+offs, (size_t)(end-buf-offs), 0))
1.48      schwarze  733:                m->last->flags |= MDOC_EOS;
                    734:
1.1       kristaps  735:        return(1);
                    736: }
1.19      schwarze  737:
1.1       kristaps  738:
                    739: /*
                    740:  * Parse a macro line, that is, a line beginning with the control
                    741:  * character.
                    742:  */
1.61      schwarze  743: static int
1.53      schwarze  744: mdoc_pmacro(struct mdoc *m, int ln, char *buf, int offs)
1.1       kristaps  745: {
1.56      schwarze  746:        enum mdoct        tok;
                    747:        int               i, j, sv;
                    748:        char              mac[5];
                    749:        struct mdoc_node *n;
1.1       kristaps  750:
1.7       schwarze  751:        /* Empty lines are ignored. */
1.1       kristaps  752:
1.53      schwarze  753:        offs++;
                    754:
                    755:        if ('\0' == buf[offs])
1.1       kristaps  756:                return(1);
                    757:
1.53      schwarze  758:        i = offs;
1.24      schwarze  759:
1.63      schwarze  760:        /* Accept tabs/whitespace after the initial control char. */
1.24      schwarze  761:
1.63      schwarze  762:        if (' ' == buf[i] || '\t' == buf[i]) {
1.24      schwarze  763:                i++;
1.63      schwarze  764:                while (buf[i] && (' ' == buf[i] || '\t' == buf[i]))
1.1       kristaps  765:                        i++;
1.34      schwarze  766:                if ('\0' == buf[i])
1.1       kristaps  767:                        return(1);
                    768:        }
                    769:
1.47      schwarze  770:        sv = i;
                    771:
1.63      schwarze  772:        /*
1.64      schwarze  773:         * Copy the first word into a nil-terminated buffer.
                    774:         * Stop copying when a tab, space, or eoln is encountered.
1.63      schwarze  775:         */
1.1       kristaps  776:
1.64      schwarze  777:        j = 0;
                    778:        while (j < 4 && '\0' != buf[i] && ' ' != buf[i] && '\t' != buf[i])
                    779:                mac[j++] = buf[i++];
1.53      schwarze  780:        mac[j] = '\0';
1.1       kristaps  781:
1.65      schwarze  782:        tok = (j > 1 || j < 4) ? mdoc_hash_find(mac) : MDOC_MAX;
                    783:        if (MDOC_MAX == tok) {
                    784:                mdoc_vmsg(m, MANDOCERR_MACRO, ln, sv,
                    785:                    "unknown macro: %s%s",
                    786:                    buf, strlen(buf) > 3 ? "..." : "");
1.1       kristaps  787:                return(1);
                    788:        }
                    789:
1.63      schwarze  790:        /* Disregard the first trailing tab, if applicable. */
                    791:
                    792:        if ('\t' == buf[i])
                    793:                i++;
                    794:
                    795:        /* Jump to the next non-whitespace word. */
1.1       kristaps  796:
                    797:        while (buf[i] && ' ' == buf[i])
                    798:                i++;
1.34      schwarze  799:
1.46      schwarze  800:        /*
                    801:         * Trailing whitespace.  Note that tabs are allowed to be passed
                    802:         * into the parser as "text", so we only warn about spaces here.
                    803:         */
1.34      schwarze  804:
                    805:        if ('\0' == buf[i] && ' ' == buf[i - 1])
1.54      schwarze  806:                if ( ! mdoc_pmsg(m, ln, i - 1, MANDOCERR_EOLNSPACE))
1.34      schwarze  807:                        goto err;
1.1       kristaps  808:
1.56      schwarze  809:        /*
                    810:         * If an initial macro or a list invocation, divert directly
                    811:         * into macro processing.
                    812:         */
                    813:
                    814:        if (NULL == m->last || MDOC_It == tok || MDOC_El == tok) {
                    815:                if ( ! mdoc_macro(m, tok, ln, sv, &i, buf))
                    816:                        goto err;
                    817:                return(1);
                    818:        }
                    819:
                    820:        n = m->last;
                    821:        assert(m->last);
                    822:
                    823:        /*
                    824:         * If the first macro of a `Bl -column', open an `It' block
                    825:         * context around the parsed macro.
                    826:         */
                    827:
                    828:        if (MDOC_Bl == n->tok && MDOC_BODY == n->type &&
1.61      schwarze  829:                        LIST_column == n->data.Bl->type) {
1.56      schwarze  830:                m->flags |= MDOC_FREECOL;
1.58      schwarze  831:                if ( ! mdoc_macro(m, MDOC_It, ln, sv, &sv, buf))
1.56      schwarze  832:                        goto err;
                    833:                return(1);
                    834:        }
                    835:
                    836:        /*
                    837:         * If we're following a block-level `It' within a `Bl -column'
                    838:         * context (perhaps opened in the above block or in ptext()),
                    839:         * then open an `It' block context around the parsed macro.
1.24      schwarze  840:         */
1.56      schwarze  841:
                    842:        if (MDOC_It == n->tok && MDOC_BLOCK == n->type &&
                    843:                        NULL != n->parent &&
                    844:                        MDOC_Bl == n->parent->tok &&
1.61      schwarze  845:                        LIST_column == n->parent->data.Bl->type) {
1.56      schwarze  846:                m->flags |= MDOC_FREECOL;
                    847:                if ( ! mdoc_macro(m, MDOC_It, ln, sv, &sv, buf))
                    848:                        goto err;
                    849:                return(1);
                    850:        }
                    851:
                    852:        /* Normal processing of a macro. */
                    853:
1.47      schwarze  854:        if ( ! mdoc_macro(m, tok, ln, sv, &i, buf))
1.1       kristaps  855:                goto err;
                    856:
                    857:        return(1);
                    858:
                    859: err:   /* Error out. */
                    860:
                    861:        m->flags |= MDOC_HALT;
                    862:        return(0);
                    863: }
1.24      schwarze  864:
                    865: