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

Annotation of mandoc/man.c, Revision 1.71

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

CVSweb