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

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