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

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