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

Annotation of mandoc/mdoc.c, Revision 1.150

1.150   ! kristaps    1: /*     $Id: mdoc.c,v 1.149 2010/06/27 15:52:41 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;
        !           382:
1.46      kristaps  383:        return(p);
1.45      kristaps  384: }
                    385:
                    386:
1.23      kristaps  387: int
1.117     kristaps  388: mdoc_tail_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.17      kristaps  389: {
                    390:        struct mdoc_node *p;
                    391:
1.91      kristaps  392:        p = node_alloc(m, line, pos, tok, MDOC_TAIL);
1.102     kristaps  393:        if ( ! node_append(m, p))
                    394:                return(0);
                    395:        m->next = MDOC_NEXT_CHILD;
                    396:        return(1);
1.17      kristaps  397: }
                    398:
                    399:
1.23      kristaps  400: int
1.117     kristaps  401: mdoc_head_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.1       kristaps  402: {
                    403:        struct mdoc_node *p;
                    404:
1.91      kristaps  405:        assert(m->first);
                    406:        assert(m->last);
1.1       kristaps  407:
1.91      kristaps  408:        p = node_alloc(m, line, pos, tok, MDOC_HEAD);
1.102     kristaps  409:        if ( ! node_append(m, p))
                    410:                return(0);
                    411:        m->next = MDOC_NEXT_CHILD;
                    412:        return(1);
1.1       kristaps  413: }
                    414:
                    415:
1.23      kristaps  416: int
1.117     kristaps  417: mdoc_body_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.1       kristaps  418: {
                    419:        struct mdoc_node *p;
                    420:
1.91      kristaps  421:        p = node_alloc(m, line, pos, tok, MDOC_BODY);
1.102     kristaps  422:        if ( ! node_append(m, p))
                    423:                return(0);
                    424:        m->next = MDOC_NEXT_CHILD;
                    425:        return(1);
1.1       kristaps  426: }
                    427:
                    428:
1.23      kristaps  429: int
1.91      kristaps  430: mdoc_block_alloc(struct mdoc *m, int line, int pos,
1.117     kristaps  431:                enum mdoct tok, struct mdoc_arg *args)
1.1       kristaps  432: {
                    433:        struct mdoc_node *p;
                    434:
1.91      kristaps  435:        p = node_alloc(m, line, pos, tok, MDOC_BLOCK);
1.77      kristaps  436:        p->args = args;
                    437:        if (p->args)
1.53      kristaps  438:                (args->refcnt)++;
1.102     kristaps  439:        if ( ! node_append(m, p))
                    440:                return(0);
                    441:        m->next = MDOC_NEXT_CHILD;
                    442:        return(1);
1.1       kristaps  443: }
                    444:
                    445:
1.23      kristaps  446: int
1.91      kristaps  447: mdoc_elem_alloc(struct mdoc *m, int line, int pos,
1.117     kristaps  448:                enum mdoct tok, struct mdoc_arg *args)
1.1       kristaps  449: {
                    450:        struct mdoc_node *p;
                    451:
1.91      kristaps  452:        p = node_alloc(m, line, pos, tok, MDOC_ELEM);
1.77      kristaps  453:        p->args = args;
                    454:        if (p->args)
1.53      kristaps  455:                (args->refcnt)++;
1.102     kristaps  456:        if ( ! node_append(m, p))
                    457:                return(0);
                    458:        m->next = MDOC_NEXT_CHILD;
                    459:        return(1);
1.1       kristaps  460: }
                    461:
                    462:
1.124     kristaps  463: int
                    464: mdoc_word_alloc(struct mdoc *m, int line, int pos, const char *p)
1.1       kristaps  465: {
1.91      kristaps  466:        struct mdoc_node *n;
1.124     kristaps  467:        size_t            sv, len;
                    468:
                    469:        len = strlen(p);
1.1       kristaps  470:
1.125     kristaps  471:        n = node_alloc(m, line, pos, MDOC_MAX, MDOC_TEXT);
1.113     kristaps  472:        n->string = mandoc_malloc(len + 1);
1.91      kristaps  473:        sv = strlcpy(n->string, p, len + 1);
                    474:
                    475:        /* Prohibit truncation. */
                    476:        assert(sv < len + 1);
                    477:
1.101     kristaps  478:        if ( ! node_append(m, n))
                    479:                return(0);
1.124     kristaps  480:
1.101     kristaps  481:        m->next = MDOC_NEXT_SIBLING;
                    482:        return(1);
1.91      kristaps  483: }
                    484:
                    485:
1.53      kristaps  486: void
                    487: mdoc_node_free(struct mdoc_node *p)
1.1       kristaps  488: {
                    489:
1.53      kristaps  490:        if (p->string)
                    491:                free(p->string);
                    492:        if (p->args)
                    493:                mdoc_argv_free(p->args);
1.1       kristaps  494:        free(p);
                    495: }
                    496:
                    497:
1.121     kristaps  498: static void
                    499: mdoc_node_unlink(struct mdoc *m, struct mdoc_node *n)
                    500: {
                    501:
                    502:        /* Adjust siblings. */
                    503:
                    504:        if (n->prev)
                    505:                n->prev->next = n->next;
                    506:        if (n->next)
                    507:                n->next->prev = n->prev;
                    508:
                    509:        /* Adjust parent. */
                    510:
                    511:        if (n->parent) {
                    512:                n->parent->nchild--;
                    513:                if (n->parent->child == n)
                    514:                        n->parent->child = n->prev ? n->prev : n->next;
                    515:        }
                    516:
                    517:        /* Adjust parse point, if applicable. */
                    518:
                    519:        if (m && m->last == n) {
                    520:                if (n->prev) {
                    521:                        m->last = n->prev;
                    522:                        m->next = MDOC_NEXT_SIBLING;
                    523:                } else {
                    524:                        m->last = n->parent;
                    525:                        m->next = MDOC_NEXT_CHILD;
                    526:                }
                    527:        }
                    528:
                    529:        if (m && m->first == n)
                    530:                m->first = NULL;
                    531: }
                    532:
                    533:
1.53      kristaps  534: void
1.121     kristaps  535: mdoc_node_delete(struct mdoc *m, struct mdoc_node *p)
1.1       kristaps  536: {
                    537:
1.121     kristaps  538:        while (p->child) {
                    539:                assert(p->nchild);
                    540:                mdoc_node_delete(m, p->child);
                    541:        }
                    542:        assert(0 == p->nchild);
1.1       kristaps  543:
1.121     kristaps  544:        mdoc_node_unlink(m, p);
1.53      kristaps  545:        mdoc_node_free(p);
1.1       kristaps  546: }
                    547:
                    548:
1.53      kristaps  549: /*
                    550:  * Parse free-form text, that is, a line that does not begin with the
                    551:  * control character.
                    552:  */
                    553: static int
1.149     kristaps  554: mdoc_ptext(struct mdoc *m, int line, char *buf, int offs)
1.1       kristaps  555: {
1.142     kristaps  556:        char             *c, *ws, *end;
                    557:        struct mdoc_node *n;
1.1       kristaps  558:
1.123     kristaps  559:        /* Ignore bogus comments. */
                    560:
1.135     kristaps  561:        if ('\\' == buf[offs] &&
                    562:                        '.' == buf[offs + 1] &&
                    563:                        '"' == buf[offs + 2])
1.136     kristaps  564:                return(mdoc_pmsg(m, line, offs, MANDOCERR_BADCOMMENT));
1.123     kristaps  565:
1.124     kristaps  566:        /* No text before an initial macro. */
                    567:
1.85      kristaps  568:        if (SEC_NONE == m->lastnamed)
1.136     kristaps  569:                return(mdoc_pmsg(m, line, offs, MANDOCERR_NOTEXT));
1.142     kristaps  570:
                    571:        assert(m->last);
                    572:        n = m->last;
                    573:
                    574:        /*
1.144     kristaps  575:         * Divert directly to list processing if we're encountering a
1.142     kristaps  576:         * columnar MDOC_BLOCK with or without a prior MDOC_BLOCK entry
1.144     kristaps  577:         * (a MDOC_BODY means it's already open, in which case we should
                    578:         * process within its context in the normal way).
1.142     kristaps  579:         */
                    580:
1.143     kristaps  581:        if (MDOC_Bl == n->tok && MDOC_BODY == n->type &&
1.146     kristaps  582:                        LIST_column == n->data.Bl.type) {
1.144     kristaps  583:                /* `Bl' is open without any children. */
1.142     kristaps  584:                m->flags |= MDOC_FREECOL;
1.149     kristaps  585:                return(mdoc_macro(m, MDOC_It, line, offs, &offs, buf));
1.142     kristaps  586:        }
                    587:
                    588:        if (MDOC_It == n->tok && MDOC_BLOCK == n->type &&
                    589:                        NULL != n->parent &&
                    590:                        MDOC_Bl == n->parent->tok &&
1.146     kristaps  591:                        LIST_column == n->parent->data.Bl.type) {
1.144     kristaps  592:                /* `Bl' has block-level `It' 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:        }
1.124     kristaps  596:
1.137     schwarze  597:        /*
                    598:         * Search for the beginning of unescaped trailing whitespace (ws)
                    599:         * and for the first character not to be output (end).
                    600:         */
1.139     kristaps  601:
                    602:        /* FIXME: replace with strcspn(). */
1.137     schwarze  603:        ws = NULL;
                    604:        for (c = end = buf + offs; *c; c++) {
                    605:                switch (*c) {
1.138     kristaps  606:                case '-':
                    607:                        if (mandoc_hyph(buf + offs, c))
                    608:                                *c = ASCII_HYPH;
1.145     kristaps  609:                        ws = NULL;
1.138     kristaps  610:                        break;
1.137     schwarze  611:                case ' ':
                    612:                        if (NULL == ws)
                    613:                                ws = c;
                    614:                        continue;
                    615:                case '\t':
                    616:                        /*
                    617:                         * Always warn about trailing tabs,
                    618:                         * even outside literal context,
                    619:                         * where they should be put on the next line.
                    620:                         */
                    621:                        if (NULL == ws)
                    622:                                ws = c;
                    623:                        /*
                    624:                         * Strip trailing tabs in literal context only;
                    625:                         * outside, they affect the next line.
                    626:                         */
                    627:                        if (MDOC_LITERAL & m->flags)
                    628:                                continue;
                    629:                        break;
                    630:                case '\\':
                    631:                        /* Skip the escaped character, too, if any. */
                    632:                        if (c[1])
                    633:                                c++;
                    634:                        /* FALLTHROUGH */
                    635:                default:
                    636:                        ws = NULL;
                    637:                        break;
                    638:                }
                    639:                end = c + 1;
                    640:        }
                    641:        *end = '\0';
1.91      kristaps  642:
1.137     schwarze  643:        if (ws)
                    644:                if ( ! mdoc_pmsg(m, line, (int)(ws-buf), MANDOCERR_EOLNSPACE))
                    645:                        return(0);
1.115     kristaps  646:
1.137     schwarze  647:        if ('\0' == buf[offs] && ! (MDOC_LITERAL & m->flags)) {
                    648:                if ( ! mdoc_pmsg(m, line, (int)(c-buf), MANDOCERR_NOBLANKLN))
1.119     kristaps  649:                        return(0);
1.124     kristaps  650:
1.119     kristaps  651:                /*
1.124     kristaps  652:                 * Insert a `Pp' in the case of a blank line.  Technically,
                    653:                 * blank lines aren't allowed, but enough manuals assume this
                    654:                 * behaviour that we want to work around it.
1.119     kristaps  655:                 */
1.135     kristaps  656:                if ( ! mdoc_elem_alloc(m, line, offs, MDOC_Pp, NULL))
1.119     kristaps  657:                        return(0);
1.124     kristaps  658:
                    659:                m->next = MDOC_NEXT_SIBLING;
                    660:                return(1);
1.119     kristaps  661:        }
1.68      kristaps  662:
1.137     schwarze  663:        if ( ! mdoc_word_alloc(m, line, offs, buf+offs))
                    664:                return(0);
1.91      kristaps  665:
1.137     schwarze  666:        if (MDOC_LITERAL & m->flags)
                    667:                return(1);
1.128     kristaps  668:
                    669:        /*
                    670:         * End-of-sentence check.  If the last character is an unescaped
                    671:         * EOS character, then flag the node as being the end of a
                    672:         * sentence.  The front-end will know how to interpret this.
                    673:         */
1.132     kristaps  674:
1.137     schwarze  675:        assert(buf < end);
                    676:
                    677:        if (mandoc_eos(buf+offs, (size_t)(end-buf-offs)))
1.128     kristaps  678:                m->last->flags |= MDOC_EOS;
                    679:
                    680:        return(1);
1.1       kristaps  681: }
                    682:
                    683:
1.58      kristaps  684: static int
1.135     kristaps  685: macrowarn(struct mdoc *m, int ln, const char *buf, int offs)
1.58      kristaps  686: {
1.136     kristaps  687:        int              rc;
                    688:
                    689:        rc = mdoc_vmsg(m, MANDOCERR_MACRO, ln, offs,
                    690:                        "unknown macro: %s%s",
                    691:                        buf, strlen(buf) > 3 ? "..." : "");
                    692:
                    693:        /* FIXME: logic should be in driver. */
1.144     kristaps  694:        /* FIXME: broken, will error out and not omit a message. */
1.136     kristaps  695:        return(MDOC_IGN_MACRO & m->pflags ? rc : 0);
1.58      kristaps  696: }
                    697:
                    698:
1.53      kristaps  699: /*
                    700:  * Parse a macro line, that is, a line beginning with the control
                    701:  * character.
                    702:  */
                    703: int
1.149     kristaps  704: mdoc_pmacro(struct mdoc *m, int ln, char *buf, int offs)
1.1       kristaps  705: {
1.144     kristaps  706:        enum mdoct        tok;
                    707:        int               i, j, sv;
                    708:        char              mac[5];
                    709:        struct mdoc_node *n;
1.1       kristaps  710:
1.81      kristaps  711:        /* Empty lines are ignored. */
1.63      kristaps  712:
1.135     kristaps  713:        offs++;
                    714:
                    715:        if ('\0' == buf[offs])
1.63      kristaps  716:                return(1);
                    717:
1.135     kristaps  718:        i = offs;
1.100     kristaps  719:
                    720:        /* Accept whitespace after the initial control char. */
                    721:
                    722:        if (' ' == buf[i]) {
                    723:                i++;
1.65      kristaps  724:                while (buf[i] && ' ' == buf[i])
1.63      kristaps  725:                        i++;
1.115     kristaps  726:                if ('\0' == buf[i])
1.63      kristaps  727:                        return(1);
                    728:        }
1.1       kristaps  729:
1.130     kristaps  730:        sv = i;
                    731:
1.53      kristaps  732:        /* Copy the first word into a nil-terminated buffer. */
1.1       kristaps  733:
1.100     kristaps  734:        for (j = 0; j < 4; j++, i++) {
1.115     kristaps  735:                if ('\0' == (mac[j] = buf[i]))
1.53      kristaps  736:                        break;
1.65      kristaps  737:                else if (' ' == buf[i])
1.53      kristaps  738:                        break;
1.104     kristaps  739:
                    740:                /* Check for invalid characters. */
                    741:
                    742:                if (isgraph((u_char)buf[i]))
                    743:                        continue;
1.136     kristaps  744:                if ( ! mdoc_pmsg(m, ln, i, MANDOCERR_BADCHAR))
                    745:                        return(0);
                    746:                i--;
1.53      kristaps  747:        }
1.1       kristaps  748:
1.135     kristaps  749:        mac[j] = '\0';
1.1       kristaps  750:
1.100     kristaps  751:        if (j == 4 || j < 2) {
1.135     kristaps  752:                if ( ! macrowarn(m, ln, mac, sv))
1.58      kristaps  753:                        goto err;
                    754:                return(1);
1.53      kristaps  755:        }
                    756:
1.125     kristaps  757:        if (MDOC_MAX == (tok = mdoc_hash_find(mac))) {
1.135     kristaps  758:                if ( ! macrowarn(m, ln, mac, sv))
1.58      kristaps  759:                        goto err;
                    760:                return(1);
1.53      kristaps  761:        }
1.1       kristaps  762:
1.53      kristaps  763:        /* The macro is sane.  Jump to the next word. */
1.1       kristaps  764:
1.65      kristaps  765:        while (buf[i] && ' ' == buf[i])
1.53      kristaps  766:                i++;
1.1       kristaps  767:
1.125     kristaps  768:        /*
                    769:         * Trailing whitespace.  Note that tabs are allowed to be passed
                    770:         * into the parser as "text", so we only warn about spaces here.
                    771:         */
1.115     kristaps  772:
                    773:        if ('\0' == buf[i] && ' ' == buf[i - 1])
1.136     kristaps  774:                if ( ! mdoc_pmsg(m, ln, i - 1, MANDOCERR_EOLNSPACE))
1.115     kristaps  775:                        goto err;
                    776:
1.144     kristaps  777:        /*
                    778:         * If an initial macro or a list invocation, divert directly
                    779:         * into macro processing.
                    780:         */
                    781:
                    782:        if (NULL == m->last || MDOC_It == tok || MDOC_El == tok) {
1.149     kristaps  783:                if ( ! mdoc_macro(m, tok, ln, sv, &i, buf))
1.144     kristaps  784:                        goto err;
                    785:                return(1);
                    786:        }
                    787:
                    788:        n = m->last;
                    789:        assert(m->last);
                    790:
                    791:        /*
                    792:         * If the first macro of a `Bl -column', open an `It' block
                    793:         * context around the parsed macro.
                    794:         */
                    795:
                    796:        if (MDOC_Bl == n->tok && MDOC_BODY == n->type &&
1.146     kristaps  797:                        LIST_column == n->data.Bl.type) {
1.144     kristaps  798:                m->flags |= MDOC_FREECOL;
1.149     kristaps  799:                if ( ! mdoc_macro(m, MDOC_It, ln, sv, &sv, buf))
1.144     kristaps  800:                        goto err;
                    801:                return(1);
                    802:        }
                    803:
                    804:        /*
                    805:         * If we're following a block-level `It' within a `Bl -column'
                    806:         * context (perhaps opened in the above block or in ptext()),
                    807:         * then open an `It' block context around the parsed macro.
1.98      kristaps  808:         */
1.144     kristaps  809:
                    810:        if (MDOC_It == n->tok && MDOC_BLOCK == n->type &&
                    811:                        NULL != n->parent &&
                    812:                        MDOC_Bl == n->parent->tok &&
1.146     kristaps  813:                        LIST_column == n->parent->data.Bl.type) {
1.144     kristaps  814:                m->flags |= MDOC_FREECOL;
1.149     kristaps  815:                if ( ! mdoc_macro(m, MDOC_It, ln, sv, &sv, buf))
1.144     kristaps  816:                        goto err;
                    817:                return(1);
                    818:        }
                    819:
                    820:        /* Normal processing of a macro. */
                    821:
1.149     kristaps  822:        if ( ! mdoc_macro(m, tok, ln, sv, &i, buf))
1.53      kristaps  823:                goto err;
1.1       kristaps  824:
1.53      kristaps  825:        return(1);
1.1       kristaps  826:
1.53      kristaps  827: err:   /* Error out. */
1.1       kristaps  828:
1.53      kristaps  829:        m->flags |= MDOC_HALT;
                    830:        return(0);
1.1       kristaps  831: }
1.100     kristaps  832:
                    833:

CVSweb