[BACK]Return to mdoc.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/mdoc.c, Revision 1.151

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

CVSweb