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

Annotation of mandoc/man.c, Revision 1.68

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

CVSweb