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

Annotation of mandoc/man.c, Revision 1.63

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

CVSweb