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

Annotation of mandoc/man.c, Revision 1.30

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

CVSweb