[BACK]Return to man.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/man.c, Revision 1.17

1.17    ! schwarze    1: /*     $Id: man.c,v 1.16 2009/12/22 23:58:00 schwarze Exp $ */
1.1       kristaps    2: /*
1.2       schwarze    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.2       schwarze    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.2       schwarze    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.14      schwarze   17: #include <sys/types.h>
                     18:
1.1       kristaps   19: #include <assert.h>
                     20: #include <ctype.h>
                     21: #include <stdarg.h>
                     22: #include <stdlib.h>
                     23: #include <stdio.h>
                     24: #include <string.h>
                     25:
                     26: #include "libman.h"
1.16      schwarze   27: #include "libmandoc.h"
1.1       kristaps   28:
1.7       schwarze   29: const  char *const __man_merrnames[WERRMAX] = {
                     30:        "invalid character", /* WNPRINT */
                     31:        "invalid manual section", /* WMSEC */
                     32:        "invalid date format", /* WDATE */
                     33:        "scope of prior line violated", /* WLNSCOPE */
                     34:        "trailing whitespace", /* WTSPACE */
                     35:        "unterminated quoted parameter", /* WTQUOTE */
                     36:        "document has no body", /* WNODATA */
                     37:        "document has no title/section", /* WNOTITLE */
                     38:        "invalid escape sequence", /* WESCAPE */
1.8       schwarze   39:        "invalid number format", /* WNUMFMT */
1.9       schwarze   40:        "expected block head arguments", /* WHEADARGS */
                     41:        "expected block body arguments", /* WBODYARGS */
                     42:        "expected empty block head", /* WNHEADARGS */
                     43:        "ill-formed macro", /* WMACROFORM */
1.10      schwarze   44:        "scope open on exit", /* WEXITSCOPE */
                     45:        "no scope context", /* WNOSCOPE */
                     46:        "literal context already open", /* WOLITERAL */
                     47:        "no literal context open" /* WNLITERAL */
1.7       schwarze   48: };
                     49:
1.1       kristaps   50: const  char *const __man_macronames[MAN_MAX] = {
1.3       schwarze   51:        "br",           "TH",           "SH",           "SS",
1.1       kristaps   52:        "TP",           "LP",           "PP",           "P",
                     53:        "IP",           "HP",           "SM",           "SB",
                     54:        "BI",           "IB",           "BR",           "RB",
                     55:        "R",            "B",            "I",            "IR",
1.9       schwarze   56:        "RI",           "na",           "i",            "sp",
1.10      schwarze   57:        "nf",           "fi",           "r",            "RE",
1.15      schwarze   58:        "RS",           "DT",           "UC",           "PD"
1.1       kristaps   59:        };
                     60:
                     61: const  char * const *man_macronames = __man_macronames;
                     62:
                     63: static struct man_node *man_node_alloc(int, int,
                     64:                                enum man_type, int);
                     65: static int              man_node_append(struct man *,
                     66:                                struct man_node *);
                     67: static int              man_ptext(struct man *, int, char *);
                     68: static int              man_pmacro(struct man *, int, char *);
                     69: static void             man_free1(struct man *);
1.16      schwarze   70: static void             man_alloc1(struct man *);
1.10      schwarze   71: static int              pstring(struct man *, int, int,
                     72:                                const char *, size_t);
1.15      schwarze   73: static int              macrowarn(struct man *, int, const char *);
1.1       kristaps   74:
                     75:
                     76: const struct man_node *
                     77: man_node(const struct man *m)
                     78: {
                     79:
                     80:        return(MAN_HALT & m->flags ? NULL : m->first);
                     81: }
                     82:
                     83:
                     84: const struct man_meta *
                     85: man_meta(const struct man *m)
                     86: {
                     87:
                     88:        return(MAN_HALT & m->flags ? NULL : &m->meta);
                     89: }
                     90:
                     91:
1.16      schwarze   92: void
1.1       kristaps   93: man_reset(struct man *man)
                     94: {
                     95:
                     96:        man_free1(man);
1.16      schwarze   97:        man_alloc1(man);
1.1       kristaps   98: }
                     99:
                    100:
                    101: void
                    102: man_free(struct man *man)
                    103: {
                    104:
                    105:        man_free1(man);
                    106:        free(man);
                    107: }
                    108:
                    109:
                    110: struct man *
                    111: man_alloc(void *data, int pflags, const struct man_cb *cb)
                    112: {
                    113:        struct man      *p;
                    114:
1.16      schwarze  115:        p = mandoc_calloc(1, sizeof(struct man));
1.1       kristaps  116:
1.16      schwarze  117:        if (cb)
                    118:                memcpy(&p->cb, cb, sizeof(struct man_cb));
1.1       kristaps  119:
1.13      schwarze  120:        man_hash_init();
1.1       kristaps  121:        p->data = data;
                    122:        p->pflags = pflags;
1.16      schwarze  123:
                    124:        man_alloc1(p);
1.1       kristaps  125:        return(p);
                    126: }
                    127:
                    128:
                    129: int
                    130: man_endparse(struct man *m)
                    131: {
                    132:
                    133:        if (MAN_HALT & m->flags)
                    134:                return(0);
                    135:        else if (man_macroend(m))
                    136:                return(1);
                    137:        m->flags |= MAN_HALT;
                    138:        return(0);
                    139: }
                    140:
                    141:
                    142: int
                    143: man_parseln(struct man *m, int ln, char *buf)
                    144: {
                    145:
                    146:        return('.' == *buf ?
                    147:                        man_pmacro(m, ln, buf) :
                    148:                        man_ptext(m, ln, buf));
                    149: }
                    150:
                    151:
                    152: static void
                    153: man_free1(struct man *man)
                    154: {
                    155:
                    156:        if (man->first)
                    157:                man_node_freelist(man->first);
                    158:        if (man->meta.title)
                    159:                free(man->meta.title);
                    160:        if (man->meta.source)
                    161:                free(man->meta.source);
                    162:        if (man->meta.vol)
                    163:                free(man->meta.vol);
                    164: }
                    165:
                    166:
1.16      schwarze  167: static void
1.1       kristaps  168: man_alloc1(struct man *m)
                    169: {
                    170:
1.16      schwarze  171:        memset(&m->meta, 0, sizeof(struct man_meta));
1.1       kristaps  172:        m->flags = 0;
1.16      schwarze  173:        m->last = mandoc_calloc(1, sizeof(struct man_node));
1.1       kristaps  174:        m->first = m->last;
                    175:        m->last->type = MAN_ROOT;
                    176:        m->next = MAN_NEXT_CHILD;
                    177: }
                    178:
                    179:
                    180: static int
                    181: man_node_append(struct man *man, struct man_node *p)
                    182: {
                    183:
                    184:        assert(man->last);
                    185:        assert(man->first);
                    186:        assert(MAN_ROOT != p->type);
                    187:
                    188:        switch (man->next) {
                    189:        case (MAN_NEXT_SIBLING):
                    190:                man->last->next = p;
                    191:                p->prev = man->last;
                    192:                p->parent = man->last->parent;
                    193:                break;
                    194:        case (MAN_NEXT_CHILD):
                    195:                man->last->child = p;
                    196:                p->parent = man->last;
                    197:                break;
                    198:        default:
                    199:                abort();
                    200:                /* NOTREACHED */
                    201:        }
1.5       schwarze  202:
                    203:        p->parent->nchild++;
1.1       kristaps  204:
1.9       schwarze  205:        if ( ! man_valid_pre(man, p))
                    206:                return(0);
                    207:
                    208:        switch (p->type) {
                    209:        case (MAN_HEAD):
                    210:                assert(MAN_BLOCK == p->parent->type);
                    211:                p->parent->head = p;
                    212:                break;
                    213:        case (MAN_BODY):
                    214:                assert(MAN_BLOCK == p->parent->type);
                    215:                p->parent->body = p;
                    216:                break;
                    217:        default:
                    218:                break;
                    219:        }
                    220:
1.1       kristaps  221:        man->last = p;
                    222:
                    223:        switch (p->type) {
                    224:        case (MAN_TEXT):
                    225:                if ( ! man_valid_post(man))
                    226:                        return(0);
                    227:                if ( ! man_action_post(man))
                    228:                        return(0);
                    229:                break;
                    230:        default:
                    231:                break;
                    232:        }
                    233:
                    234:        return(1);
                    235: }
                    236:
                    237:
                    238: static struct man_node *
                    239: man_node_alloc(int line, int pos, enum man_type type, int tok)
                    240: {
                    241:        struct man_node *p;
                    242:
1.16      schwarze  243:        p = mandoc_calloc(1, sizeof(struct man_node));
1.1       kristaps  244:        p->line = line;
                    245:        p->pos = pos;
                    246:        p->type = type;
                    247:        p->tok = tok;
                    248:        return(p);
                    249: }
                    250:
                    251:
                    252: int
1.10      schwarze  253: man_elem_alloc(struct man *m, int line, int pos, int tok)
1.1       kristaps  254: {
                    255:        struct man_node *p;
                    256:
                    257:        p = man_node_alloc(line, pos, MAN_ELEM, tok);
1.10      schwarze  258:        if ( ! man_node_append(m, p))
                    259:                return(0);
                    260:        m->next = MAN_NEXT_CHILD;
                    261:        return(1);
1.1       kristaps  262: }
                    263:
                    264:
                    265: int
1.9       schwarze  266: man_head_alloc(struct man *m, int line, int pos, int tok)
                    267: {
                    268:        struct man_node *p;
                    269:
                    270:        p = man_node_alloc(line, pos, MAN_HEAD, tok);
                    271:        if ( ! man_node_append(m, p))
                    272:                return(0);
                    273:        m->next = MAN_NEXT_CHILD;
                    274:        return(1);
                    275: }
                    276:
                    277:
                    278: int
                    279: man_body_alloc(struct man *m, int line, int pos, int tok)
                    280: {
                    281:        struct man_node *p;
                    282:
                    283:        p = man_node_alloc(line, pos, MAN_BODY, tok);
                    284:        if ( ! man_node_append(m, p))
                    285:                return(0);
                    286:        m->next = MAN_NEXT_CHILD;
                    287:        return(1);
                    288: }
                    289:
                    290:
                    291: int
                    292: man_block_alloc(struct man *m, int line, int pos, int tok)
                    293: {
                    294:        struct man_node *p;
                    295:
                    296:        p = man_node_alloc(line, pos, MAN_BLOCK, tok);
                    297:        if ( ! man_node_append(m, p))
                    298:                return(0);
                    299:        m->next = MAN_NEXT_CHILD;
                    300:        return(1);
                    301: }
                    302:
                    303:
1.10      schwarze  304: static int
                    305: pstring(struct man *m, int line, int pos,
                    306:                const char *p, size_t len)
1.1       kristaps  307: {
1.10      schwarze  308:        struct man_node *n;
                    309:        size_t           sv;
1.1       kristaps  310:
1.10      schwarze  311:        n = man_node_alloc(line, pos, MAN_TEXT, -1);
1.16      schwarze  312:        n->string = mandoc_malloc(len + 1);
1.10      schwarze  313:        sv = strlcpy(n->string, p, len + 1);
                    314:
                    315:        /* Prohibit truncation. */
                    316:        assert(sv < len + 1);
                    317:
                    318:        if ( ! man_node_append(m, n))
1.1       kristaps  319:                return(0);
1.10      schwarze  320:        m->next = MAN_NEXT_SIBLING;
                    321:        return(1);
                    322: }
                    323:
                    324:
                    325: int
                    326: man_word_alloc(struct man *m, int line, int pos, const char *word)
                    327: {
                    328:
                    329:        return(pstring(m, line, pos, word, strlen(word)));
1.1       kristaps  330: }
                    331:
                    332:
                    333: void
                    334: man_node_free(struct man_node *p)
                    335: {
                    336:
                    337:        if (p->string)
                    338:                free(p->string);
1.5       schwarze  339:        if (p->parent)
                    340:                p->parent->nchild--;
1.1       kristaps  341:        free(p);
                    342: }
                    343:
                    344:
                    345: void
                    346: man_node_freelist(struct man_node *p)
                    347: {
1.10      schwarze  348:        struct man_node *n;
1.1       kristaps  349:
                    350:        if (p->child)
                    351:                man_node_freelist(p->child);
1.5       schwarze  352:        assert(0 == p->nchild);
1.10      schwarze  353:        n = p->next;
1.1       kristaps  354:        man_node_free(p);
1.10      schwarze  355:        if (n)
                    356:                man_node_freelist(n);
1.1       kristaps  357: }
                    358:
                    359:
                    360: static int
                    361: man_ptext(struct man *m, int line, char *buf)
                    362: {
1.10      schwarze  363:        int              i, j;
                    364:
                    365:        /* Literal free-form text whitespace is preserved. */
                    366:
                    367:        if (MAN_LITERAL & m->flags) {
                    368:                if ( ! man_word_alloc(m, line, 0, buf))
                    369:                        return(0);
                    370:                goto descope;
                    371:        }
                    372:
                    373:        /* First de-chunk and allocate words. */
                    374:
                    375:        for (i = 0; ' ' == buf[i]; i++)
                    376:                /* Skip leading whitespace. */ ;
                    377:        if (0 == buf[i]) {
                    378:                if ( ! pstring(m, line, 0, &buf[i], 0))
                    379:                        return(0);
                    380:                goto descope;
                    381:        }
1.1       kristaps  382:
1.10      schwarze  383:        for (j = i; buf[i]; i++) {
                    384:                if (' ' != buf[i])
                    385:                        continue;
                    386:
                    387:                /* Escaped whitespace. */
                    388:                if (i && ' ' == buf[i] && '\\' == buf[i - 1])
                    389:                        continue;
                    390:
                    391:                buf[i++] = 0;
                    392:                if ( ! pstring(m, line, j, &buf[j], (size_t)(i - j)))
                    393:                        return(0);
                    394:
                    395:                for ( ; ' ' == buf[i]; i++)
                    396:                        /* Skip trailing whitespace. */ ;
                    397:
                    398:                j = i;
                    399:                if (0 == buf[i])
                    400:                        break;
                    401:        }
1.9       schwarze  402:
1.10      schwarze  403:        if (j != i && ! pstring(m, line, j, &buf[j], (size_t)(i - j)))
1.1       kristaps  404:                return(0);
1.10      schwarze  405:
                    406: descope:
1.1       kristaps  407:
                    408:        /*
1.9       schwarze  409:         * Co-ordinate what happens with having a next-line scope open:
                    410:         * first close out the element scope (if applicable), then close
                    411:         * out the block scope (also if applicable).
1.1       kristaps  412:         */
                    413:
1.9       schwarze  414:        if (MAN_ELINE & m->flags) {
                    415:                m->flags &= ~MAN_ELINE;
                    416:                if ( ! man_unscope(m, m->last->parent))
                    417:                        return(0);
                    418:        }
                    419:
                    420:        if ( ! (MAN_BLINE & m->flags))
1.1       kristaps  421:                return(1);
1.9       schwarze  422:        m->flags &= ~MAN_BLINE;
1.1       kristaps  423:
1.9       schwarze  424:        if ( ! man_unscope(m, m->last->parent))
1.1       kristaps  425:                return(0);
1.9       schwarze  426:        return(man_body_alloc(m, line, 0, m->last->tok));
1.1       kristaps  427: }
                    428:
                    429:
1.15      schwarze  430: static int
                    431: macrowarn(struct man *m, int ln, const char *buf)
                    432: {
                    433:        if ( ! (MAN_IGN_MACRO & m->pflags))
                    434:                return(man_verr(m, ln, 0,
                    435:                                "unknown macro: %s%s",
                    436:                                buf, strlen(buf) > 3 ? "..." : ""));
                    437:        return(man_vwarn(m, ln, 0, "unknown macro: %s%s",
                    438:                                buf, strlen(buf) > 3 ? "..." : ""));
                    439: }
                    440:
                    441:
1.1       kristaps  442: int
                    443: man_pmacro(struct man *m, int ln, char *buf)
                    444: {
1.10      schwarze  445:        int              i, j, c, ppos, fl;
                    446:        char             mac[5];
                    447:        struct man_node *n;
1.1       kristaps  448:
                    449:        /* Comments and empties are quickly ignored. */
                    450:
1.9       schwarze  451:        fl = m->flags;
1.1       kristaps  452:
1.17    ! schwarze  453:        if ('\0' == buf[1])
        !           454:                return(1);
1.1       kristaps  455:
                    456:        i = 1;
                    457:
                    458:        if (' ' == buf[i]) {
                    459:                i++;
                    460:                while (buf[i] && ' ' == buf[i])
                    461:                        i++;
                    462:                if (0 == buf[i])
                    463:                        goto out;
                    464:        }
                    465:
                    466:        ppos = i;
                    467:
                    468:        /* Copy the first word into a nil-terminated buffer. */
                    469:
                    470:        for (j = 0; j < 4; j++, i++) {
                    471:                if (0 == (mac[j] = buf[i]))
                    472:                        break;
                    473:                else if (' ' == buf[i])
                    474:                        break;
1.11      schwarze  475:
                    476:                /* Check for invalid characters. */
                    477:
                    478:                if (isgraph((u_char)buf[i]))
                    479:                        continue;
                    480:                return(man_perr(m, ln, i, WNPRINT));
1.1       kristaps  481:        }
                    482:
1.17    ! schwarze  483:        mac[j] = '\0';
1.1       kristaps  484:
                    485:        if (j == 4 || j < 1) {
                    486:                if ( ! (MAN_IGN_MACRO & m->pflags)) {
1.9       schwarze  487:                        (void)man_perr(m, ln, ppos, WMACROFORM);
1.1       kristaps  488:                        goto err;
                    489:                }
1.9       schwarze  490:                if ( ! man_pwarn(m, ln, ppos, WMACROFORM))
1.1       kristaps  491:                        goto err;
                    492:                return(1);
                    493:        }
                    494:
1.13      schwarze  495:        if (MAN_MAX == (c = man_hash_find(mac))) {
1.15      schwarze  496:                if ( ! macrowarn(m, ln, mac))
1.1       kristaps  497:                        goto err;
                    498:                return(1);
                    499:        }
                    500:
                    501:        /* The macro is sane.  Jump to the next word. */
                    502:
                    503:        while (buf[i] && ' ' == buf[i])
                    504:                i++;
                    505:
1.10      schwarze  506:        /* Remove prior ELINE macro, if applicable. */
                    507:
                    508:        if (m->flags & MAN_ELINE) {
                    509:                n = m->last;
                    510:                assert(NULL == n->child);
                    511:                assert(0 == n->nchild);
                    512:                if ( ! man_nwarn(m, n, WLNSCOPE))
                    513:                        return(0);
                    514:
                    515:                if (n->prev) {
                    516:                        assert(n != n->parent->child);
                    517:                        assert(n == n->prev->next);
                    518:                        n->prev->next = NULL;
                    519:                        m->last = n->prev;
                    520:                        m->next = MAN_NEXT_SIBLING;
                    521:                } else {
                    522:                        assert(n == n->parent->child);
                    523:                        n->parent->child = NULL;
                    524:                        m->last = n->parent;
                    525:                        m->next = MAN_NEXT_CHILD;
                    526:                }
                    527:
                    528:                man_node_free(n);
                    529:                m->flags &= ~MAN_ELINE;
                    530:        }
                    531:
1.1       kristaps  532:        /* Begin recursive parse sequence. */
                    533:
1.9       schwarze  534:        assert(man_macros[c].fp);
                    535:
                    536:        if ( ! (*man_macros[c].fp)(m, c, ln, ppos, &i, buf))
1.1       kristaps  537:                goto err;
                    538:
                    539: out:
1.9       schwarze  540:        if ( ! (MAN_BLINE & fl))
                    541:                return(1);
                    542:
                    543:        /*
                    544:         * If we've opened a new next-line element scope, then return
                    545:         * now, as the next line will close out the block scope.
                    546:         */
                    547:
                    548:        if (MAN_ELINE & m->flags)
                    549:                return(1);
                    550:
                    551:        /* Close out the block scope opened in the prior line.  */
1.1       kristaps  552:
1.9       schwarze  553:        assert(MAN_BLINE & m->flags);
                    554:        m->flags &= ~MAN_BLINE;
1.1       kristaps  555:
1.9       schwarze  556:        if ( ! man_unscope(m, m->last->parent))
                    557:                return(0);
                    558:        return(man_body_alloc(m, ln, 0, m->last->tok));
1.1       kristaps  559:
                    560: err:   /* Error out. */
                    561:
                    562:        m->flags |= MAN_HALT;
                    563:        return(0);
                    564: }
                    565:
                    566:
                    567: int
                    568: man_verr(struct man *man, int ln, int pos, const char *fmt, ...)
                    569: {
                    570:        char             buf[256];
                    571:        va_list          ap;
                    572:
                    573:        if (NULL == man->cb.man_err)
                    574:                return(0);
                    575:
                    576:        va_start(ap, fmt);
                    577:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
                    578:        va_end(ap);
                    579:        return((*man->cb.man_err)(man->data, ln, pos, buf));
                    580: }
                    581:
                    582:
                    583: int
                    584: man_vwarn(struct man *man, int ln, int pos, const char *fmt, ...)
                    585: {
                    586:        char             buf[256];
                    587:        va_list          ap;
                    588:
                    589:        if (NULL == man->cb.man_warn)
                    590:                return(0);
                    591:
                    592:        va_start(ap, fmt);
                    593:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
                    594:        va_end(ap);
                    595:        return((*man->cb.man_warn)(man->data, ln, pos, buf));
                    596: }
                    597:
                    598:
1.4       schwarze  599: int
1.7       schwarze  600: man_err(struct man *m, int line, int pos, int iserr, enum merr type)
1.4       schwarze  601: {
                    602:        const char       *p;
                    603:
1.7       schwarze  604:        p = __man_merrnames[(int)type];
1.4       schwarze  605:        assert(p);
                    606:
                    607:        if (iserr)
                    608:                return(man_verr(m, line, pos, p));
                    609:
                    610:        return(man_vwarn(m, line, pos, p));
                    611: }