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

Annotation of mandoc/man.c, Revision 1.35

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

CVSweb