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

1.70    ! schwarze    1: /*     $Id: mdoc.c,v 1.69 2010/11/28 19:35:33 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);
                    301:                if (NULL == m->meta.title)
1.56      schwarze  302:                        m->meta.title = mandoc_strdup("UNKNOWN");
1.39      schwarze  303:                if (NULL == m->meta.vol)
1.56      schwarze  304:                        m->meta.vol = mandoc_strdup("LOCAL");
1.39      schwarze  305:                if (NULL == m->meta.os)
1.56      schwarze  306:                        m->meta.os = mandoc_strdup("LOCAL");
1.39      schwarze  307:                if (0 == m->meta.date)
                    308:                        m->meta.date = time(NULL);
                    309:                m->flags |= MDOC_PBODY;
                    310:        }
1.1       kristaps  311:
1.58      schwarze  312:        return((*mdoc_macros[tok].fp)(m, tok, line, ppos, pos, buf));
1.1       kristaps  313: }
                    314:
                    315:
                    316: static int
                    317: node_append(struct mdoc *mdoc, struct mdoc_node *p)
                    318: {
                    319:
                    320:        assert(mdoc->last);
                    321:        assert(mdoc->first);
                    322:        assert(MDOC_ROOT != p->type);
                    323:
                    324:        switch (mdoc->next) {
                    325:        case (MDOC_NEXT_SIBLING):
                    326:                mdoc->last->next = p;
                    327:                p->prev = mdoc->last;
                    328:                p->parent = mdoc->last->parent;
                    329:                break;
                    330:        case (MDOC_NEXT_CHILD):
                    331:                mdoc->last->child = p;
                    332:                p->parent = mdoc->last;
                    333:                break;
                    334:        default:
                    335:                abort();
                    336:                /* NOTREACHED */
                    337:        }
                    338:
1.10      schwarze  339:        p->parent->nchild++;
                    340:
1.1       kristaps  341:        if ( ! mdoc_valid_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:                break;
                    370:        default:
                    371:                break;
                    372:        }
                    373:
                    374:        return(1);
                    375: }
                    376:
                    377:
                    378: static struct mdoc_node *
1.37      schwarze  379: node_alloc(struct mdoc *m, int line, int pos,
                    380:                enum mdoct tok, enum mdoc_type type)
1.1       kristaps  381: {
                    382:        struct mdoc_node *p;
                    383:
1.32      schwarze  384:        p = mandoc_calloc(1, sizeof(struct mdoc_node));
1.19      schwarze  385:        p->sec = m->lastsec;
1.1       kristaps  386:        p->line = line;
                    387:        p->pos = pos;
                    388:        p->tok = tok;
1.37      schwarze  389:        p->type = type;
1.58      schwarze  390:
                    391:        /* Flag analysis. */
                    392:
1.60      schwarze  393:        if (MDOC_SYNOPSIS & m->flags)
                    394:                p->flags |= MDOC_SYNPRETTY;
                    395:        else
                    396:                p->flags &= ~MDOC_SYNPRETTY;
1.47      schwarze  397:        if (MDOC_NEWLINE & m->flags)
                    398:                p->flags |= MDOC_LINE;
                    399:        m->flags &= ~MDOC_NEWLINE;
1.58      schwarze  400:
1.1       kristaps  401:        return(p);
                    402: }
                    403:
                    404:
                    405: int
1.37      schwarze  406: mdoc_tail_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.1       kristaps  407: {
                    408:        struct mdoc_node *p;
                    409:
1.19      schwarze  410:        p = node_alloc(m, line, pos, tok, MDOC_TAIL);
1.25      schwarze  411:        if ( ! node_append(m, p))
                    412:                return(0);
                    413:        m->next = MDOC_NEXT_CHILD;
                    414:        return(1);
1.1       kristaps  415: }
                    416:
                    417:
                    418: int
1.37      schwarze  419: mdoc_head_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.1       kristaps  420: {
                    421:        struct mdoc_node *p;
                    422:
1.19      schwarze  423:        assert(m->first);
                    424:        assert(m->last);
1.1       kristaps  425:
1.19      schwarze  426:        p = node_alloc(m, line, pos, tok, MDOC_HEAD);
1.25      schwarze  427:        if ( ! node_append(m, p))
                    428:                return(0);
                    429:        m->next = MDOC_NEXT_CHILD;
                    430:        return(1);
1.1       kristaps  431: }
                    432:
                    433:
                    434: int
1.37      schwarze  435: mdoc_body_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.1       kristaps  436: {
                    437:        struct mdoc_node *p;
                    438:
1.19      schwarze  439:        p = node_alloc(m, line, pos, tok, MDOC_BODY);
1.25      schwarze  440:        if ( ! node_append(m, p))
                    441:                return(0);
                    442:        m->next = MDOC_NEXT_CHILD;
1.59      schwarze  443:        return(1);
                    444: }
                    445:
                    446:
                    447: int
                    448: mdoc_endbody_alloc(struct mdoc *m, int line, int pos, enum mdoct tok,
                    449:                struct mdoc_node *body, enum mdoc_endbody end)
                    450: {
                    451:        struct mdoc_node *p;
                    452:
                    453:        p = node_alloc(m, line, pos, tok, MDOC_BODY);
                    454:        p->pending = body;
                    455:        p->end = end;
                    456:        if ( ! node_append(m, p))
                    457:                return(0);
                    458:        m->next = MDOC_NEXT_SIBLING;
1.25      schwarze  459:        return(1);
1.1       kristaps  460: }
                    461:
                    462:
                    463: int
1.19      schwarze  464: mdoc_block_alloc(struct mdoc *m, int line, int pos,
1.37      schwarze  465:                enum mdoct tok, struct mdoc_arg *args)
1.1       kristaps  466: {
                    467:        struct mdoc_node *p;
                    468:
1.19      schwarze  469:        p = node_alloc(m, line, pos, tok, MDOC_BLOCK);
1.4       schwarze  470:        p->args = args;
                    471:        if (p->args)
1.1       kristaps  472:                (args->refcnt)++;
1.25      schwarze  473:        if ( ! node_append(m, p))
                    474:                return(0);
                    475:        m->next = MDOC_NEXT_CHILD;
                    476:        return(1);
1.1       kristaps  477: }
                    478:
                    479:
                    480: int
1.19      schwarze  481: mdoc_elem_alloc(struct mdoc *m, int line, int pos,
1.37      schwarze  482:                enum mdoct tok, struct mdoc_arg *args)
1.1       kristaps  483: {
                    484:        struct mdoc_node *p;
                    485:
1.19      schwarze  486:        p = node_alloc(m, line, pos, tok, MDOC_ELEM);
1.4       schwarze  487:        p->args = args;
                    488:        if (p->args)
1.1       kristaps  489:                (args->refcnt)++;
1.25      schwarze  490:        if ( ! node_append(m, p))
                    491:                return(0);
                    492:        m->next = MDOC_NEXT_CHILD;
                    493:        return(1);
1.1       kristaps  494: }
                    495:
                    496:
1.46      schwarze  497: int
                    498: mdoc_word_alloc(struct mdoc *m, int line, int pos, const char *p)
1.1       kristaps  499: {
1.19      schwarze  500:        struct mdoc_node *n;
1.46      schwarze  501:        size_t            sv, len;
                    502:
                    503:        len = strlen(p);
1.1       kristaps  504:
1.46      schwarze  505:        n = node_alloc(m, line, pos, MDOC_MAX, MDOC_TEXT);
1.32      schwarze  506:        n->string = mandoc_malloc(len + 1);
1.19      schwarze  507:        sv = strlcpy(n->string, p, len + 1);
                    508:
                    509:        /* Prohibit truncation. */
                    510:        assert(sv < len + 1);
                    511:
1.25      schwarze  512:        if ( ! node_append(m, n))
                    513:                return(0);
1.46      schwarze  514:
1.25      schwarze  515:        m->next = MDOC_NEXT_SIBLING;
                    516:        return(1);
1.19      schwarze  517: }
                    518:
                    519:
1.61      schwarze  520: static void
1.1       kristaps  521: mdoc_node_free(struct mdoc_node *p)
                    522: {
                    523:
1.61      schwarze  524:        /*
                    525:         * XXX: if these end up being problematic in terms of memory
                    526:         * management and dereferencing freed blocks, then make them
                    527:         * into reference-counted double-pointers.
                    528:         */
                    529:
                    530:        if (MDOC_Bd == p->tok && MDOC_BLOCK == p->type)
                    531:                if (p->data.Bd)
                    532:                        free(p->data.Bd);
                    533:        if (MDOC_Bl == p->tok && MDOC_BLOCK == p->type)
                    534:                if (p->data.Bl)
                    535:                        free(p->data.Bl);
                    536:        if (MDOC_Bf == p->tok && MDOC_HEAD == p->type)
                    537:                if (p->data.Bf)
                    538:                        free(p->data.Bf);
1.67      schwarze  539:        if (MDOC_TS == p->tok && MDOC_BLOCK == p->type)
                    540:                if (p->data.TS)
                    541:                        tbl_free(p->data.TS);
1.61      schwarze  542:
1.1       kristaps  543:        if (p->string)
                    544:                free(p->string);
                    545:        if (p->args)
                    546:                mdoc_argv_free(p->args);
                    547:        free(p);
                    548: }
                    549:
                    550:
1.40      schwarze  551: static void
                    552: mdoc_node_unlink(struct mdoc *m, struct mdoc_node *n)
                    553: {
                    554:
                    555:        /* Adjust siblings. */
                    556:
                    557:        if (n->prev)
                    558:                n->prev->next = n->next;
                    559:        if (n->next)
                    560:                n->next->prev = n->prev;
                    561:
                    562:        /* Adjust parent. */
                    563:
                    564:        if (n->parent) {
                    565:                n->parent->nchild--;
                    566:                if (n->parent->child == n)
                    567:                        n->parent->child = n->prev ? n->prev : n->next;
                    568:        }
                    569:
                    570:        /* Adjust parse point, if applicable. */
                    571:
                    572:        if (m && m->last == n) {
                    573:                if (n->prev) {
                    574:                        m->last = n->prev;
                    575:                        m->next = MDOC_NEXT_SIBLING;
                    576:                } else {
                    577:                        m->last = n->parent;
                    578:                        m->next = MDOC_NEXT_CHILD;
                    579:                }
                    580:        }
                    581:
                    582:        if (m && m->first == n)
                    583:                m->first = NULL;
                    584: }
                    585:
                    586:
1.1       kristaps  587: void
1.40      schwarze  588: mdoc_node_delete(struct mdoc *m, struct mdoc_node *p)
1.1       kristaps  589: {
                    590:
1.40      schwarze  591:        while (p->child) {
                    592:                assert(p->nchild);
                    593:                mdoc_node_delete(m, p->child);
                    594:        }
                    595:        assert(0 == p->nchild);
1.1       kristaps  596:
1.40      schwarze  597:        mdoc_node_unlink(m, p);
1.1       kristaps  598:        mdoc_node_free(p);
                    599: }
                    600:
                    601:
                    602: /*
                    603:  * Parse free-form text, that is, a line that does not begin with the
                    604:  * control character.
                    605:  */
                    606: static int
1.53      schwarze  607: mdoc_ptext(struct mdoc *m, int line, char *buf, int offs)
1.1       kristaps  608: {
1.56      schwarze  609:        char             *c, *ws, *end;
                    610:        struct mdoc_node *n;
1.44      schwarze  611:
                    612:        /* Ignore bogus comments. */
                    613:
1.53      schwarze  614:        if ('\\' == buf[offs] &&
                    615:                        '.' == buf[offs + 1] &&
                    616:                        '"' == buf[offs + 2])
1.54      schwarze  617:                return(mdoc_pmsg(m, line, offs, MANDOCERR_BADCOMMENT));
1.1       kristaps  618:
1.46      schwarze  619:        /* No text before an initial macro. */
                    620:
1.9       schwarze  621:        if (SEC_NONE == m->lastnamed)
1.54      schwarze  622:                return(mdoc_pmsg(m, line, offs, MANDOCERR_NOTEXT));
1.46      schwarze  623:
1.56      schwarze  624:        assert(m->last);
                    625:        n = m->last;
                    626:
                    627:        /*
                    628:         * Divert directly to list processing if we're encountering a
                    629:         * columnar MDOC_BLOCK with or without a prior MDOC_BLOCK entry
                    630:         * (a MDOC_BODY means it's already open, in which case we should
                    631:         * process within its context in the normal way).
                    632:         */
                    633:
                    634:        if (MDOC_Bl == n->tok && MDOC_BODY == n->type &&
1.61      schwarze  635:                        LIST_column == n->data.Bl->type) {
1.56      schwarze  636:                /* `Bl' is open without any children. */
                    637:                m->flags |= MDOC_FREECOL;
                    638:                return(mdoc_macro(m, MDOC_It, line, offs, &offs, buf));
                    639:        }
                    640:
                    641:        if (MDOC_It == n->tok && MDOC_BLOCK == n->type &&
                    642:                        NULL != n->parent &&
                    643:                        MDOC_Bl == n->parent->tok &&
1.61      schwarze  644:                        LIST_column == n->parent->data.Bl->type) {
1.56      schwarze  645:                /* `Bl' has block-level `It' children. */
                    646:                m->flags |= MDOC_FREECOL;
                    647:                return(mdoc_macro(m, MDOC_It, line, offs, &offs, buf));
                    648:        }
                    649:
1.52      schwarze  650:        /*
                    651:         * Search for the beginning of unescaped trailing whitespace (ws)
                    652:         * and for the first character not to be output (end).
                    653:         */
1.56      schwarze  654:
                    655:        /* FIXME: replace with strcspn(). */
1.52      schwarze  656:        ws = NULL;
1.53      schwarze  657:        for (c = end = buf + offs; *c; c++) {
1.52      schwarze  658:                switch (*c) {
1.55      schwarze  659:                case '-':
                    660:                        if (mandoc_hyph(buf + offs, c))
                    661:                                *c = ASCII_HYPH;
1.56      schwarze  662:                        ws = NULL;
1.55      schwarze  663:                        break;
1.52      schwarze  664:                case ' ':
                    665:                        if (NULL == ws)
                    666:                                ws = c;
                    667:                        continue;
                    668:                case '\t':
                    669:                        /*
                    670:                         * Always warn about trailing tabs,
                    671:                         * even outside literal context,
                    672:                         * where they should be put on the next line.
                    673:                         */
                    674:                        if (NULL == ws)
                    675:                                ws = c;
                    676:                        /*
                    677:                         * Strip trailing tabs in literal context only;
                    678:                         * outside, they affect the next line.
                    679:                         */
                    680:                        if (MDOC_LITERAL & m->flags)
                    681:                                continue;
                    682:                        break;
                    683:                case '\\':
                    684:                        /* Skip the escaped character, too, if any. */
                    685:                        if (c[1])
                    686:                                c++;
                    687:                        /* FALLTHROUGH */
                    688:                default:
                    689:                        ws = NULL;
                    690:                        break;
                    691:                }
                    692:                end = c + 1;
                    693:        }
                    694:        *end = '\0';
1.19      schwarze  695:
1.52      schwarze  696:        if (ws)
1.54      schwarze  697:                if ( ! mdoc_pmsg(m, line, (int)(ws-buf), MANDOCERR_EOLNSPACE))
1.52      schwarze  698:                        return(0);
1.34      schwarze  699:
1.53      schwarze  700:        if ('\0' == buf[offs] && ! (MDOC_LITERAL & m->flags)) {
1.54      schwarze  701:                if ( ! mdoc_pmsg(m, line, (int)(c-buf), MANDOCERR_NOBLANKLN))
1.38      schwarze  702:                        return(0);
1.46      schwarze  703:
1.40      schwarze  704:                /*
1.66      schwarze  705:                 * Insert a `sp' in the case of a blank line.  Technically,
1.46      schwarze  706:                 * blank lines aren't allowed, but enough manuals assume this
                    707:                 * behaviour that we want to work around it.
1.40      schwarze  708:                 */
1.66      schwarze  709:                if ( ! mdoc_elem_alloc(m, line, offs, MDOC_sp, NULL))
1.42      schwarze  710:                        return(0);
1.46      schwarze  711:
1.42      schwarze  712:                m->next = MDOC_NEXT_SIBLING;
                    713:                return(1);
1.38      schwarze  714:        }
1.1       kristaps  715:
1.53      schwarze  716:        if ( ! mdoc_word_alloc(m, line, offs, buf+offs))
1.1       kristaps  717:                return(0);
                    718:
1.52      schwarze  719:        if (MDOC_LITERAL & m->flags)
                    720:                return(1);
                    721:
1.35      schwarze  722:        /*
1.48      schwarze  723:         * End-of-sentence check.  If the last character is an unescaped
                    724:         * EOS character, then flag the node as being the end of a
                    725:         * sentence.  The front-end will know how to interpret this.
1.35      schwarze  726:         */
                    727:
1.52      schwarze  728:        assert(buf < end);
1.48      schwarze  729:
1.62      schwarze  730:        if (mandoc_eos(buf+offs, (size_t)(end-buf-offs), 0))
1.48      schwarze  731:                m->last->flags |= MDOC_EOS;
                    732:
1.1       kristaps  733:        return(1);
                    734: }
1.19      schwarze  735:
1.1       kristaps  736:
                    737: /*
                    738:  * Parse a macro line, that is, a line beginning with the control
                    739:  * character.
                    740:  */
1.61      schwarze  741: static int
1.53      schwarze  742: mdoc_pmacro(struct mdoc *m, int ln, char *buf, int offs)
1.1       kristaps  743: {
1.56      schwarze  744:        enum mdoct        tok;
                    745:        int               i, j, sv;
                    746:        char              mac[5];
                    747:        struct mdoc_node *n;
1.1       kristaps  748:
1.7       schwarze  749:        /* Empty lines are ignored. */
1.1       kristaps  750:
1.53      schwarze  751:        offs++;
                    752:
                    753:        if ('\0' == buf[offs])
1.1       kristaps  754:                return(1);
                    755:
1.53      schwarze  756:        i = offs;
1.24      schwarze  757:
1.63      schwarze  758:        /* Accept tabs/whitespace after the initial control char. */
1.24      schwarze  759:
1.63      schwarze  760:        if (' ' == buf[i] || '\t' == buf[i]) {
1.24      schwarze  761:                i++;
1.63      schwarze  762:                while (buf[i] && (' ' == buf[i] || '\t' == buf[i]))
1.1       kristaps  763:                        i++;
1.34      schwarze  764:                if ('\0' == buf[i])
1.1       kristaps  765:                        return(1);
                    766:        }
                    767:
1.47      schwarze  768:        sv = i;
                    769:
1.63      schwarze  770:        /*
1.64      schwarze  771:         * Copy the first word into a nil-terminated buffer.
                    772:         * Stop copying when a tab, space, or eoln is encountered.
1.63      schwarze  773:         */
1.1       kristaps  774:
1.64      schwarze  775:        j = 0;
                    776:        while (j < 4 && '\0' != buf[i] && ' ' != buf[i] && '\t' != buf[i])
                    777:                mac[j++] = buf[i++];
1.53      schwarze  778:        mac[j] = '\0';
1.1       kristaps  779:
1.65      schwarze  780:        tok = (j > 1 || j < 4) ? mdoc_hash_find(mac) : MDOC_MAX;
                    781:        if (MDOC_MAX == tok) {
1.69      schwarze  782:                mdoc_vmsg(m, MANDOCERR_MACRO, ln, sv, "%s", buf + sv - 1);
1.1       kristaps  783:                return(1);
                    784:        }
                    785:
1.63      schwarze  786:        /* Disregard the first trailing tab, if applicable. */
                    787:
                    788:        if ('\t' == buf[i])
                    789:                i++;
                    790:
                    791:        /* Jump to the next non-whitespace word. */
1.1       kristaps  792:
                    793:        while (buf[i] && ' ' == buf[i])
                    794:                i++;
1.34      schwarze  795:
1.46      schwarze  796:        /*
                    797:         * Trailing whitespace.  Note that tabs are allowed to be passed
                    798:         * into the parser as "text", so we only warn about spaces here.
                    799:         */
1.34      schwarze  800:
                    801:        if ('\0' == buf[i] && ' ' == buf[i - 1])
1.54      schwarze  802:                if ( ! mdoc_pmsg(m, ln, i - 1, MANDOCERR_EOLNSPACE))
1.34      schwarze  803:                        goto err;
1.1       kristaps  804:
1.56      schwarze  805:        /*
                    806:         * If an initial macro or a list invocation, divert directly
                    807:         * into macro processing.
                    808:         */
                    809:
                    810:        if (NULL == m->last || MDOC_It == tok || MDOC_El == tok) {
                    811:                if ( ! mdoc_macro(m, tok, ln, sv, &i, buf))
                    812:                        goto err;
                    813:                return(1);
                    814:        }
                    815:
                    816:        n = m->last;
                    817:        assert(m->last);
                    818:
                    819:        /*
                    820:         * If the first macro of a `Bl -column', open an `It' block
                    821:         * context around the parsed macro.
                    822:         */
                    823:
                    824:        if (MDOC_Bl == n->tok && MDOC_BODY == n->type &&
1.61      schwarze  825:                        LIST_column == n->data.Bl->type) {
1.56      schwarze  826:                m->flags |= MDOC_FREECOL;
1.58      schwarze  827:                if ( ! mdoc_macro(m, MDOC_It, ln, sv, &sv, buf))
1.56      schwarze  828:                        goto err;
                    829:                return(1);
                    830:        }
                    831:
                    832:        /*
                    833:         * If we're following a block-level `It' within a `Bl -column'
                    834:         * context (perhaps opened in the above block or in ptext()),
                    835:         * then open an `It' block context around the parsed macro.
1.24      schwarze  836:         */
1.56      schwarze  837:
                    838:        if (MDOC_It == n->tok && MDOC_BLOCK == n->type &&
                    839:                        NULL != n->parent &&
                    840:                        MDOC_Bl == n->parent->tok &&
1.61      schwarze  841:                        LIST_column == n->parent->data.Bl->type) {
1.56      schwarze  842:                m->flags |= MDOC_FREECOL;
                    843:                if ( ! mdoc_macro(m, MDOC_It, ln, sv, &sv, buf))
                    844:                        goto err;
                    845:                return(1);
                    846:        }
                    847:
                    848:        /* Normal processing of a macro. */
                    849:
1.47      schwarze  850:        if ( ! mdoc_macro(m, tok, ln, sv, &i, buf))
1.1       kristaps  851:                goto err;
                    852:
                    853:        return(1);
                    854:
                    855: err:   /* Error out. */
                    856:
                    857:        m->flags |= MDOC_HALT;
                    858:        return(0);
                    859: }
1.24      schwarze  860:
                    861: