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

Annotation of mandoc/man.c, Revision 1.33

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

CVSweb