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

Annotation of mandoc/man.c, Revision 1.31

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

CVSweb