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

Annotation of mandoc/mdoc.c, Revision 1.148

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

CVSweb