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

Annotation of mandoc/man.c, Revision 1.103

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

CVSweb