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

Annotation of mandoc/man.c, Revision 1.79

1.79    ! kristaps    1: /*     $Id: man.c,v 1.78 2010/06/26 16:07:08 kristaps Exp $ */
1.1       kristaps    2: /*
1.76      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.18      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.18      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.47      kristaps   17: #ifdef HAVE_CONFIG_H
                     18: #include "config.h"
                     19: #endif
                     20:
1.41      kristaps   21: #include <sys/types.h>
                     22:
1.1       kristaps   23: #include <assert.h>
                     24: #include <ctype.h>
                     25: #include <stdarg.h>
                     26: #include <stdlib.h>
                     27: #include <stdio.h>
                     28: #include <string.h>
                     29:
1.74      kristaps   30: #include "mandoc.h"
1.77      kristaps   31: #include "regs.h"
1.1       kristaps   32: #include "libman.h"
1.45      kristaps   33: #include "libmandoc.h"
1.1       kristaps   34:
                     35: const  char *const __man_macronames[MAN_MAX] = {
1.21      kristaps   36:        "br",           "TH",           "SH",           "SS",
1.1       kristaps   37:        "TP",           "LP",           "PP",           "P",
                     38:        "IP",           "HP",           "SM",           "SB",
                     39:        "BI",           "IB",           "BR",           "RB",
1.11      kristaps   40:        "R",            "B",            "I",            "IR",
1.29      kristaps   41:        "RI",           "na",           "i",            "sp",
1.30      kristaps   42:        "nf",           "fi",           "r",            "RE",
1.52      kristaps   43:        "RS",           "DT",           "UC",           "PD",
1.73      joerg      44:        "Sp",           "Vb",           "Ve",           "AT",
1.1       kristaps   45:        };
                     46:
                     47: const  char * const *man_macronames = __man_macronames;
                     48:
1.16      kristaps   49: static struct man_node *man_node_alloc(int, int,
1.53      kristaps   50:                                enum man_type, enum mant);
1.1       kristaps   51: static int              man_node_append(struct man *,
                     52:                                struct man_node *);
1.54      kristaps   53: static void             man_node_free(struct man_node *);
                     54: static void             man_node_unlink(struct man *,
                     55:                                struct man_node *);
1.72      kristaps   56: static int              man_ptext(struct man *, int, char *, int);
1.79    ! kristaps   57: static int              man_pmacro(struct man *, int, char *, int);
1.2       kristaps   58: static void             man_free1(struct man *);
1.45      kristaps   59: static void             man_alloc1(struct man *);
1.72      kristaps   60: static int              macrowarn(struct man *, int, const char *, int);
1.1       kristaps   61:
                     62:
                     63: const struct man_node *
1.2       kristaps   64: man_node(const struct man *m)
1.1       kristaps   65: {
                     66:
1.2       kristaps   67:        return(MAN_HALT & m->flags ? NULL : m->first);
1.1       kristaps   68: }
                     69:
                     70:
                     71: const struct man_meta *
1.2       kristaps   72: man_meta(const struct man *m)
1.1       kristaps   73: {
                     74:
1.2       kristaps   75:        return(MAN_HALT & m->flags ? NULL : &m->meta);
1.1       kristaps   76: }
                     77:
                     78:
1.45      kristaps   79: void
1.1       kristaps   80: man_reset(struct man *man)
                     81: {
                     82:
1.2       kristaps   83:        man_free1(man);
1.45      kristaps   84:        man_alloc1(man);
1.1       kristaps   85: }
                     86:
                     87:
                     88: void
                     89: man_free(struct man *man)
                     90: {
                     91:
1.2       kristaps   92:        man_free1(man);
1.1       kristaps   93:        free(man);
                     94: }
                     95:
                     96:
                     97: struct man *
1.79    ! kristaps   98: man_alloc(const struct regset *regs,
        !            99:                void *data, int pflags, mandocmsg msg)
1.1       kristaps  100: {
                    101:        struct man      *p;
                    102:
1.45      kristaps  103:        p = mandoc_calloc(1, sizeof(struct man));
1.2       kristaps  104:
1.40      kristaps  105:        man_hash_init();
1.4       kristaps  106:        p->data = data;
1.7       kristaps  107:        p->pflags = pflags;
1.74      kristaps  108:        p->msg = msg;
1.79    ! kristaps  109:        p->regs = regs;
1.45      kristaps  110:
                    111:        man_alloc1(p);
1.1       kristaps  112:        return(p);
                    113: }
                    114:
                    115:
                    116: int
                    117: man_endparse(struct man *m)
                    118: {
                    119:
1.3       kristaps  120:        if (MAN_HALT & m->flags)
                    121:                return(0);
                    122:        else if (man_macroend(m))
                    123:                return(1);
                    124:        m->flags |= MAN_HALT;
                    125:        return(0);
1.1       kristaps  126: }
                    127:
                    128:
                    129: int
1.79    ! kristaps  130: man_parseln(struct man *m, int ln, char *buf, int offs)
1.1       kristaps  131: {
                    132:
1.72      kristaps  133:        if (MAN_HALT & m->flags)
                    134:                return(0);
                    135:
                    136:        return(('.' == buf[offs] || '\'' == buf[offs]) ?
1.79    ! kristaps  137:                        man_pmacro(m, ln, buf, offs) :
1.72      kristaps  138:                        man_ptext(m, ln, buf, offs));
1.1       kristaps  139: }
                    140:
                    141:
1.2       kristaps  142: static void
                    143: man_free1(struct man *man)
                    144: {
                    145:
                    146:        if (man->first)
1.54      kristaps  147:                man_node_delete(man, man->first);
1.2       kristaps  148:        if (man->meta.title)
                    149:                free(man->meta.title);
1.6       kristaps  150:        if (man->meta.source)
                    151:                free(man->meta.source);
1.75      kristaps  152:        if (man->meta.rawdate)
                    153:                free(man->meta.rawdate);
1.2       kristaps  154:        if (man->meta.vol)
                    155:                free(man->meta.vol);
1.68      kristaps  156:        if (man->meta.msec)
                    157:                free(man->meta.msec);
1.2       kristaps  158: }
                    159:
                    160:
1.45      kristaps  161: static void
1.2       kristaps  162: man_alloc1(struct man *m)
                    163: {
                    164:
1.44      kristaps  165:        memset(&m->meta, 0, sizeof(struct man_meta));
1.2       kristaps  166:        m->flags = 0;
1.45      kristaps  167:        m->last = mandoc_calloc(1, sizeof(struct man_node));
1.2       kristaps  168:        m->first = m->last;
                    169:        m->last->type = MAN_ROOT;
1.54      kristaps  170:        m->last->tok = MAN_MAX;
1.2       kristaps  171:        m->next = MAN_NEXT_CHILD;
                    172: }
                    173:
                    174:
1.1       kristaps  175: static int
                    176: man_node_append(struct man *man, struct man_node *p)
                    177: {
                    178:
                    179:        assert(man->last);
                    180:        assert(man->first);
                    181:        assert(MAN_ROOT != p->type);
                    182:
                    183:        switch (man->next) {
                    184:        case (MAN_NEXT_SIBLING):
                    185:                man->last->next = p;
                    186:                p->prev = man->last;
                    187:                p->parent = man->last->parent;
                    188:                break;
                    189:        case (MAN_NEXT_CHILD):
                    190:                man->last->child = p;
                    191:                p->parent = man->last;
                    192:                break;
                    193:        default:
                    194:                abort();
                    195:                /* NOTREACHED */
                    196:        }
1.22      kristaps  197:
1.54      kristaps  198:        assert(p->parent);
1.22      kristaps  199:        p->parent->nchild++;
1.1       kristaps  200:
1.29      kristaps  201:        if ( ! man_valid_pre(man, p))
                    202:                return(0);
                    203:
                    204:        switch (p->type) {
                    205:        case (MAN_HEAD):
                    206:                assert(MAN_BLOCK == p->parent->type);
                    207:                p->parent->head = p;
                    208:                break;
                    209:        case (MAN_BODY):
                    210:                assert(MAN_BLOCK == p->parent->type);
                    211:                p->parent->body = p;
                    212:                break;
                    213:        default:
                    214:                break;
                    215:        }
                    216:
1.2       kristaps  217:        man->last = p;
                    218:
1.1       kristaps  219:        switch (p->type) {
1.2       kristaps  220:        case (MAN_TEXT):
                    221:                if ( ! man_valid_post(man))
                    222:                        return(0);
                    223:                if ( ! man_action_post(man))
                    224:                        return(0);
1.1       kristaps  225:                break;
                    226:        default:
                    227:                break;
                    228:        }
                    229:
                    230:        return(1);
                    231: }
                    232:
                    233:
                    234: static struct man_node *
1.53      kristaps  235: man_node_alloc(int line, int pos, enum man_type type, enum mant tok)
1.1       kristaps  236: {
                    237:        struct man_node *p;
                    238:
1.45      kristaps  239:        p = mandoc_calloc(1, sizeof(struct man_node));
1.1       kristaps  240:        p->line = line;
                    241:        p->pos = pos;
                    242:        p->type = type;
1.16      kristaps  243:        p->tok = tok;
1.1       kristaps  244:        return(p);
                    245: }
                    246:
                    247:
                    248: int
1.53      kristaps  249: man_elem_alloc(struct man *m, int line, int pos, enum mant tok)
1.1       kristaps  250: {
                    251:        struct man_node *p;
                    252:
1.16      kristaps  253:        p = man_node_alloc(line, pos, MAN_ELEM, tok);
1.30      kristaps  254:        if ( ! man_node_append(m, p))
                    255:                return(0);
                    256:        m->next = MAN_NEXT_CHILD;
                    257:        return(1);
1.1       kristaps  258: }
                    259:
                    260:
                    261: int
1.53      kristaps  262: man_head_alloc(struct man *m, int line, int pos, enum mant tok)
1.29      kristaps  263: {
                    264:        struct man_node *p;
                    265:
                    266:        p = man_node_alloc(line, pos, MAN_HEAD, tok);
                    267:        if ( ! man_node_append(m, p))
                    268:                return(0);
                    269:        m->next = MAN_NEXT_CHILD;
                    270:        return(1);
                    271: }
                    272:
                    273:
                    274: int
1.53      kristaps  275: man_body_alloc(struct man *m, int line, int pos, enum mant tok)
1.29      kristaps  276: {
                    277:        struct man_node *p;
                    278:
                    279:        p = man_node_alloc(line, pos, MAN_BODY, tok);
                    280:        if ( ! man_node_append(m, p))
                    281:                return(0);
                    282:        m->next = MAN_NEXT_CHILD;
                    283:        return(1);
                    284: }
                    285:
                    286:
                    287: int
1.53      kristaps  288: man_block_alloc(struct man *m, int line, int pos, enum mant tok)
1.29      kristaps  289: {
                    290:        struct man_node *p;
                    291:
                    292:        p = man_node_alloc(line, pos, MAN_BLOCK, tok);
                    293:        if ( ! man_node_append(m, p))
                    294:                return(0);
                    295:        m->next = MAN_NEXT_CHILD;
                    296:        return(1);
                    297: }
                    298:
                    299:
1.61      kristaps  300: int
                    301: man_word_alloc(struct man *m, int line, int pos, const char *word)
1.1       kristaps  302: {
1.31      kristaps  303:        struct man_node *n;
1.61      kristaps  304:        size_t           sv, len;
                    305:
                    306:        len = strlen(word);
1.1       kristaps  307:
1.53      kristaps  308:        n = man_node_alloc(line, pos, MAN_TEXT, MAN_MAX);
1.45      kristaps  309:        n->string = mandoc_malloc(len + 1);
1.61      kristaps  310:        sv = strlcpy(n->string, word, len + 1);
1.31      kristaps  311:
                    312:        /* Prohibit truncation. */
                    313:        assert(sv < len + 1);
                    314:
                    315:        if ( ! man_node_append(m, n))
1.30      kristaps  316:                return(0);
1.61      kristaps  317:
1.30      kristaps  318:        m->next = MAN_NEXT_SIBLING;
                    319:        return(1);
1.1       kristaps  320: }
                    321:
                    322:
1.54      kristaps  323: /*
                    324:  * Free all of the resources held by a node.  This does NOT unlink a
                    325:  * node from its context; for that, see man_node_unlink().
                    326:  */
                    327: static void
1.1       kristaps  328: man_node_free(struct man_node *p)
                    329: {
                    330:
                    331:        if (p->string)
                    332:                free(p->string);
                    333:        free(p);
                    334: }
                    335:
                    336:
                    337: void
1.54      kristaps  338: man_node_delete(struct man *m, struct man_node *p)
1.1       kristaps  339: {
                    340:
1.54      kristaps  341:        while (p->child)
                    342:                man_node_delete(m, p->child);
                    343:
                    344:        man_node_unlink(m, p);
1.1       kristaps  345:        man_node_free(p);
                    346: }
                    347:
                    348:
                    349: static int
1.72      kristaps  350: man_ptext(struct man *m, int line, char *buf, int offs)
1.1       kristaps  351: {
1.61      kristaps  352:        int              i;
1.60      kristaps  353:
                    354:        /* Ignore bogus comments. */
                    355:
1.72      kristaps  356:        if ('\\' == buf[offs] &&
                    357:                        '.' == buf[offs + 1] &&
                    358:                        '"' == buf[offs + 2])
1.74      kristaps  359:                return(man_pmsg(m, line, offs, MANDOCERR_BADCOMMENT));
1.31      kristaps  360:
1.35      kristaps  361:        /* Literal free-form text whitespace is preserved. */
                    362:
                    363:        if (MAN_LITERAL & m->flags) {
1.72      kristaps  364:                if ( ! man_word_alloc(m, line, offs, buf + offs))
1.35      kristaps  365:                        return(0);
                    366:                goto descope;
                    367:        }
                    368:
1.61      kristaps  369:        /* Pump blank lines directly into the backend. */
1.31      kristaps  370:
1.72      kristaps  371:        for (i = offs; ' ' == buf[i]; i++)
1.31      kristaps  372:                /* Skip leading whitespace. */ ;
1.48      kristaps  373:
1.49      kristaps  374:        if ('\0' == buf[i]) {
1.61      kristaps  375:                /* Allocate a blank entry. */
1.72      kristaps  376:                if ( ! man_word_alloc(m, line, offs, ""))
1.31      kristaps  377:                        return(0);
                    378:                goto descope;
                    379:        }
                    380:
1.63      kristaps  381:        /*
                    382:         * Warn if the last un-escaped character is whitespace. Then
                    383:         * strip away the remaining spaces (tabs stay!).
                    384:         */
1.29      kristaps  385:
1.61      kristaps  386:        i = (int)strlen(buf);
                    387:        assert(i);
1.49      kristaps  388:
1.63      kristaps  389:        if (' ' == buf[i - 1] || '\t' == buf[i - 1]) {
1.64      kristaps  390:                if (i > 1 && '\\' != buf[i - 2])
1.74      kristaps  391:                        if ( ! man_pmsg(m, line, i - 1, MANDOCERR_EOLNSPACE))
1.49      kristaps  392:                                return(0);
1.63      kristaps  393:
                    394:                for (--i; i && ' ' == buf[i]; i--)
                    395:                        /* Spin back to non-space. */ ;
                    396:
                    397:                /* Jump ahead of escaped whitespace. */
                    398:                i += '\\' == buf[i] ? 2 : 1;
                    399:
                    400:                buf[i] = '\0';
                    401:        }
1.49      kristaps  402:
1.72      kristaps  403:        if ( ! man_word_alloc(m, line, offs, buf + offs))
1.1       kristaps  404:                return(0);
1.65      kristaps  405:
                    406:        /*
                    407:         * End-of-sentence check.  If the last character is an unescaped
                    408:         * EOS character, then flag the node as being the end of a
                    409:         * sentence.  The front-end will know how to interpret this.
                    410:         */
1.67      kristaps  411:
1.65      kristaps  412:        assert(i);
1.66      kristaps  413:        if (mandoc_eos(buf, (size_t)i))
1.65      kristaps  414:                m->last->flags |= MAN_EOS;
1.31      kristaps  415:
                    416: descope:
1.11      kristaps  417:        /*
1.29      kristaps  418:         * Co-ordinate what happens with having a next-line scope open:
                    419:         * first close out the element scope (if applicable), then close
                    420:         * out the block scope (also if applicable).
1.11      kristaps  421:         */
                    422:
1.29      kristaps  423:        if (MAN_ELINE & m->flags) {
                    424:                m->flags &= ~MAN_ELINE;
1.74      kristaps  425:                if ( ! man_unscope(m, m->last->parent, MANDOCERR_MAX))
1.29      kristaps  426:                        return(0);
                    427:        }
                    428:
                    429:        if ( ! (MAN_BLINE & m->flags))
1.11      kristaps  430:                return(1);
1.29      kristaps  431:        m->flags &= ~MAN_BLINE;
1.11      kristaps  432:
1.74      kristaps  433:        if ( ! man_unscope(m, m->last->parent, MANDOCERR_MAX))
1.11      kristaps  434:                return(0);
1.72      kristaps  435:        return(man_body_alloc(m, line, offs, m->last->tok));
1.1       kristaps  436: }
                    437:
                    438:
1.43      kristaps  439: static int
1.72      kristaps  440: macrowarn(struct man *m, int ln, const char *buf, int offs)
1.43      kristaps  441: {
1.74      kristaps  442:        int              rc;
                    443:
                    444:        rc = man_vmsg(m, MANDOCERR_MACRO, ln, offs,
                    445:                        "unknown macro: %s%s",
                    446:                        buf, strlen(buf) > 3 ? "..." : "");
                    447:
                    448:        return(MAN_IGN_MACRO & m->pflags ? rc : 0);
1.43      kristaps  449: }
                    450:
                    451:
1.1       kristaps  452: int
1.79    ! kristaps  453: man_pmacro(struct man *m, int ln, char *buf, int offs)
1.1       kristaps  454: {
1.58      kristaps  455:        int              i, j, ppos;
1.53      kristaps  456:        enum mant        tok;
1.34      kristaps  457:        char             mac[5];
                    458:        struct man_node *n;
1.1       kristaps  459:
                    460:        /* Comments and empties are quickly ignored. */
                    461:
1.72      kristaps  462:        offs++;
                    463:
                    464:        if ('\0' == buf[offs])
1.46      kristaps  465:                return(1);
1.1       kristaps  466:
1.72      kristaps  467:        i = offs;
1.9       kristaps  468:
1.56      kristaps  469:        /*
                    470:         * Skip whitespace between the control character and initial
                    471:         * text.  "Whitespace" is both spaces and tabs.
                    472:         */
1.62      kristaps  473:
1.57      kristaps  474:        if (' ' == buf[i] || '\t' == buf[i]) {
1.9       kristaps  475:                i++;
1.56      kristaps  476:                while (buf[i] && (' ' == buf[i] || '\t' == buf[i]))
1.1       kristaps  477:                        i++;
1.48      kristaps  478:                if ('\0' == buf[i])
1.11      kristaps  479:                        goto out;
1.1       kristaps  480:        }
                    481:
1.10      kristaps  482:        ppos = i;
                    483:
1.1       kristaps  484:        /* Copy the first word into a nil-terminated buffer. */
                    485:
1.10      kristaps  486:        for (j = 0; j < 4; j++, i++) {
1.48      kristaps  487:                if ('\0' == (mac[j] = buf[i]))
1.1       kristaps  488:                        break;
1.10      kristaps  489:                else if (' ' == buf[i])
1.1       kristaps  490:                        break;
1.38      kristaps  491:
                    492:                /* Check for invalid characters. */
                    493:
                    494:                if (isgraph((u_char)buf[i]))
                    495:                        continue;
1.74      kristaps  496:                if ( ! man_pmsg(m, ln, i, MANDOCERR_BADCHAR))
                    497:                        return(0);
                    498:                i--;
1.1       kristaps  499:        }
                    500:
1.46      kristaps  501:        mac[j] = '\0';
1.1       kristaps  502:
1.9       kristaps  503:        if (j == 4 || j < 1) {
1.74      kristaps  504:                if ( ! macrowarn(m, ln, mac, ppos))
1.7       kristaps  505:                        goto err;
1.12      kristaps  506:                return(1);
1.7       kristaps  507:        }
1.1       kristaps  508:
1.53      kristaps  509:        if (MAN_MAX == (tok = man_hash_find(mac))) {
1.72      kristaps  510:                if ( ! macrowarn(m, ln, mac, ppos))
1.7       kristaps  511:                        goto err;
1.12      kristaps  512:                return(1);
1.1       kristaps  513:        }
                    514:
                    515:        /* The macro is sane.  Jump to the next word. */
                    516:
                    517:        while (buf[i] && ' ' == buf[i])
                    518:                i++;
                    519:
1.62      kristaps  520:        /*
                    521:         * Trailing whitespace.  Note that tabs are allowed to be passed
                    522:         * into the parser as "text", so we only warn about spaces here.
                    523:         */
1.48      kristaps  524:
                    525:        if ('\0' == buf[i] && ' ' == buf[i - 1])
1.74      kristaps  526:                if ( ! man_pmsg(m, ln, i - 1, MANDOCERR_EOLNSPACE))
1.48      kristaps  527:                        goto err;
                    528:
1.50      kristaps  529:        /*
1.51      kristaps  530:         * Remove prior ELINE macro, as it's being clobbering by a new
                    531:         * macro.  Note that NSCOPED macros do not close out ELINE
                    532:         * macros---they don't print text---so we let those slip by.
1.50      kristaps  533:         */
1.34      kristaps  534:
1.53      kristaps  535:        if ( ! (MAN_NSCOPED & man_macros[tok].flags) &&
1.51      kristaps  536:                        m->flags & MAN_ELINE) {
                    537:                assert(MAN_TEXT != m->last->type);
                    538:
                    539:                /*
                    540:                 * This occurs in the following construction:
                    541:                 *   .B
                    542:                 *   .br
                    543:                 *   .B
                    544:                 *   .br
                    545:                 *   I hate man macros.
                    546:                 * Flat-out disallow this madness.
                    547:                 */
1.74      kristaps  548:                if (MAN_NSCOPED & man_macros[m->last->tok].flags) {
                    549:                        man_pmsg(m, ln, ppos, MANDOCERR_SYNTLINESCOPE);
                    550:                        return(0);
                    551:                }
1.51      kristaps  552:
1.34      kristaps  553:                n = m->last;
1.51      kristaps  554:
                    555:                assert(n);
1.34      kristaps  556:                assert(NULL == n->child);
1.37      kristaps  557:                assert(0 == n->nchild);
1.51      kristaps  558:
1.74      kristaps  559:                if ( ! man_nmsg(m, n, MANDOCERR_LINESCOPE))
1.34      kristaps  560:                        return(0);
                    561:
1.54      kristaps  562:                man_node_delete(m, n);
1.34      kristaps  563:                m->flags &= ~MAN_ELINE;
                    564:        }
                    565:
1.59      kristaps  566:        /*
                    567:         * Save the fact that we're in the next-line for a block.  In
                    568:         * this way, embedded roff instructions can "remember" state
                    569:         * when they exit.
                    570:         */
                    571:
                    572:        if (MAN_BLINE & m->flags)
                    573:                m->flags |= MAN_BPLINE;
                    574:
                    575:        /* Call to handler... */
1.1       kristaps  576:
1.53      kristaps  577:        assert(man_macros[tok].fp);
1.79    ! kristaps  578:        if ( ! (*man_macros[tok].fp)(m, tok, ln, ppos, &i, buf))
1.1       kristaps  579:                goto err;
                    580:
1.11      kristaps  581: out:
1.50      kristaps  582:        /*
                    583:         * We weren't in a block-line scope when entering the
                    584:         * above-parsed macro, so return.
                    585:         */
                    586:
1.58      kristaps  587:        if ( ! (MAN_BPLINE & m->flags)) {
1.50      kristaps  588:                m->flags &= ~MAN_ILINE;
1.29      kristaps  589:                return(1);
1.50      kristaps  590:        }
1.58      kristaps  591:        m->flags &= ~MAN_BPLINE;
1.50      kristaps  592:
                    593:        /*
                    594:         * If we're in a block scope, then allow this macro to slip by
                    595:         * without closing scope around it.
                    596:         */
                    597:
                    598:        if (MAN_ILINE & m->flags) {
                    599:                m->flags &= ~MAN_ILINE;
                    600:                return(1);
                    601:        }
1.29      kristaps  602:
                    603:        /*
                    604:         * If we've opened a new next-line element scope, then return
                    605:         * now, as the next line will close out the block scope.
                    606:         */
                    607:
                    608:        if (MAN_ELINE & m->flags)
                    609:                return(1);
                    610:
                    611:        /* Close out the block scope opened in the prior line.  */
1.11      kristaps  612:
1.29      kristaps  613:        assert(MAN_BLINE & m->flags);
                    614:        m->flags &= ~MAN_BLINE;
1.11      kristaps  615:
1.74      kristaps  616:        if ( ! man_unscope(m, m->last->parent, MANDOCERR_MAX))
1.29      kristaps  617:                return(0);
1.72      kristaps  618:        return(man_body_alloc(m, ln, offs, m->last->tok));
1.1       kristaps  619:
                    620: err:   /* Error out. */
                    621:
1.2       kristaps  622:        m->flags |= MAN_HALT;
1.1       kristaps  623:        return(0);
                    624: }
1.3       kristaps  625:
1.4       kristaps  626:
                    627: int
1.74      kristaps  628: man_vmsg(struct man *man, enum mandocerr t,
                    629:                int ln, int pos, const char *fmt, ...)
1.4       kristaps  630: {
                    631:        char             buf[256];
                    632:        va_list          ap;
                    633:
                    634:        va_start(ap, fmt);
1.74      kristaps  635:        vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
1.4       kristaps  636:        va_end(ap);
1.74      kristaps  637:        return((*man->msg)(t, man->data, ln, pos, buf));
1.51      kristaps  638: }
                    639:
                    640:
1.54      kristaps  641: /*
                    642:  * Unlink a node from its context.  If "m" is provided, the last parse
                    643:  * point will also be adjusted accordingly.
                    644:  */
                    645: static void
1.51      kristaps  646: man_node_unlink(struct man *m, struct man_node *n)
                    647: {
                    648:
1.54      kristaps  649:        /* Adjust siblings. */
                    650:
                    651:        if (n->prev)
1.51      kristaps  652:                n->prev->next = n->next;
1.54      kristaps  653:        if (n->next)
                    654:                n->next->prev = n->prev;
                    655:
                    656:        /* Adjust parent. */
                    657:
                    658:        if (n->parent) {
                    659:                n->parent->nchild--;
                    660:                if (n->parent->child == n)
                    661:                        n->parent->child = n->prev ? n->prev : n->next;
                    662:        }
                    663:
                    664:        /* Adjust parse point, if applicable. */
                    665:
                    666:        if (m && m->last == n) {
                    667:                /*XXX: this can occur when bailing from validation. */
                    668:                /*assert(NULL == n->next);*/
                    669:                if (n->prev) {
1.51      kristaps  670:                        m->last = n->prev;
                    671:                        m->next = MAN_NEXT_SIBLING;
1.54      kristaps  672:                } else {
1.51      kristaps  673:                        m->last = n->parent;
                    674:                        m->next = MAN_NEXT_CHILD;
                    675:                }
                    676:        }
                    677:
1.54      kristaps  678:        if (m && m->first == n)
                    679:                m->first = NULL;
1.23      kristaps  680: }

CVSweb