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

Annotation of mandoc/man.c, Revision 1.28

1.28    ! kristaps    1: /*     $Id: man.c,v 1.27 2009/07/07 09:35:40 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.27      kristaps   38: };
                     39:
1.1       kristaps   40: const  char *const __man_macronames[MAN_MAX] = {
1.21      kristaps   41:        "br",           "TH",           "SH",           "SS",
1.1       kristaps   42:        "TP",           "LP",           "PP",           "P",
                     43:        "IP",           "HP",           "SM",           "SB",
                     44:        "BI",           "IB",           "BR",           "RB",
1.11      kristaps   45:        "R",            "B",            "I",            "IR",
1.28    ! kristaps   46:        "RI",           "na",           "i",            "sp"
1.1       kristaps   47:        };
                     48:
                     49: const  char * const *man_macronames = __man_macronames;
                     50:
1.16      kristaps   51: static struct man_node *man_node_alloc(int, int,
                     52:                                enum man_type, int);
1.1       kristaps   53: static int              man_node_append(struct man *,
                     54:                                struct man_node *);
                     55: static int              man_ptext(struct man *, int, char *);
                     56: static int              man_pmacro(struct man *, int, char *);
1.2       kristaps   57: static void             man_free1(struct man *);
1.16      kristaps   58: static int              man_alloc1(struct man *);
1.1       kristaps   59:
                     60:
                     61: const struct man_node *
1.2       kristaps   62: man_node(const struct man *m)
1.1       kristaps   63: {
                     64:
1.2       kristaps   65:        return(MAN_HALT & m->flags ? NULL : m->first);
1.1       kristaps   66: }
                     67:
                     68:
                     69: const struct man_meta *
1.2       kristaps   70: man_meta(const struct man *m)
1.1       kristaps   71: {
                     72:
1.2       kristaps   73:        return(MAN_HALT & m->flags ? NULL : &m->meta);
1.1       kristaps   74: }
                     75:
                     76:
1.15      kristaps   77: int
1.1       kristaps   78: man_reset(struct man *man)
                     79: {
                     80:
1.2       kristaps   81:        man_free1(man);
1.16      kristaps   82:        return(man_alloc1(man));
1.1       kristaps   83: }
                     84:
                     85:
                     86: void
                     87: man_free(struct man *man)
                     88: {
                     89:
1.2       kristaps   90:        man_free1(man);
                     91:
1.1       kristaps   92:        if (man->htab)
                     93:                man_hash_free(man->htab);
                     94:        free(man);
                     95: }
                     96:
                     97:
                     98: struct man *
1.7       kristaps   99: man_alloc(void *data, int pflags, const struct man_cb *cb)
1.1       kristaps  100: {
                    101:        struct man      *p;
                    102:
1.16      kristaps  103:        if (NULL == (p = calloc(1, sizeof(struct man))))
                    104:                return(NULL);
1.2       kristaps  105:
1.16      kristaps  106:        if ( ! man_alloc1(p)) {
                    107:                free(p);
                    108:                return(NULL);
                    109:        }
1.1       kristaps  110:
1.4       kristaps  111:        p->data = data;
1.7       kristaps  112:        p->pflags = pflags;
1.16      kristaps  113:        (void)memcpy(&p->cb, cb, sizeof(struct man_cb));
1.7       kristaps  114:
1.16      kristaps  115:        if (NULL == (p->htab = man_hash_alloc())) {
                    116:                free(p);
                    117:                return(NULL);
                    118:        }
1.1       kristaps  119:        return(p);
                    120: }
                    121:
                    122:
                    123: int
                    124: man_endparse(struct man *m)
                    125: {
                    126:
1.3       kristaps  127:        if (MAN_HALT & m->flags)
                    128:                return(0);
                    129:        else if (man_macroend(m))
                    130:                return(1);
                    131:        m->flags |= MAN_HALT;
                    132:        return(0);
1.1       kristaps  133: }
                    134:
                    135:
                    136: int
                    137: man_parseln(struct man *m, int ln, char *buf)
                    138: {
                    139:
                    140:        return('.' == *buf ?
                    141:                        man_pmacro(m, ln, buf) :
                    142:                        man_ptext(m, ln, buf));
                    143: }
                    144:
                    145:
1.2       kristaps  146: static void
                    147: man_free1(struct man *man)
                    148: {
                    149:
                    150:        if (man->first)
                    151:                man_node_freelist(man->first);
                    152:        if (man->meta.title)
                    153:                free(man->meta.title);
1.6       kristaps  154:        if (man->meta.source)
                    155:                free(man->meta.source);
1.2       kristaps  156:        if (man->meta.vol)
                    157:                free(man->meta.vol);
                    158: }
                    159:
                    160:
1.16      kristaps  161: static int
1.2       kristaps  162: man_alloc1(struct man *m)
                    163: {
                    164:
                    165:        bzero(&m->meta, sizeof(struct man_meta));
                    166:        m->flags = 0;
                    167:        m->last = calloc(1, sizeof(struct man_node));
                    168:        if (NULL == m->last)
1.16      kristaps  169:                return(0);
1.2       kristaps  170:        m->first = m->last;
                    171:        m->last->type = MAN_ROOT;
                    172:        m->next = MAN_NEXT_CHILD;
1.16      kristaps  173:        return(1);
1.2       kristaps  174: }
                    175:
                    176:
1.1       kristaps  177: static int
                    178: man_node_append(struct man *man, struct man_node *p)
                    179: {
                    180:
                    181:        assert(man->last);
                    182:        assert(man->first);
                    183:        assert(MAN_ROOT != p->type);
                    184:
                    185:        switch (man->next) {
                    186:        case (MAN_NEXT_SIBLING):
                    187:                man->last->next = p;
                    188:                p->prev = man->last;
                    189:                p->parent = man->last->parent;
                    190:                break;
                    191:        case (MAN_NEXT_CHILD):
                    192:                man->last->child = p;
                    193:                p->parent = man->last;
                    194:                break;
                    195:        default:
                    196:                abort();
                    197:                /* NOTREACHED */
                    198:        }
1.22      kristaps  199:
                    200:        p->parent->nchild++;
1.1       kristaps  201:
1.2       kristaps  202:        man->last = p;
                    203:
1.1       kristaps  204:        switch (p->type) {
1.2       kristaps  205:        case (MAN_TEXT):
                    206:                if ( ! man_valid_post(man))
                    207:                        return(0);
                    208:                if ( ! man_action_post(man))
                    209:                        return(0);
1.1       kristaps  210:                break;
                    211:        default:
                    212:                break;
                    213:        }
                    214:
                    215:        return(1);
                    216: }
                    217:
                    218:
                    219: static struct man_node *
1.16      kristaps  220: man_node_alloc(int line, int pos, enum man_type type, int tok)
1.1       kristaps  221: {
                    222:        struct man_node *p;
                    223:
1.16      kristaps  224:        p = calloc(1, sizeof(struct man_node));
                    225:        if (NULL == p)
                    226:                return(NULL);
                    227:
1.1       kristaps  228:        p->line = line;
                    229:        p->pos = pos;
                    230:        p->type = type;
1.16      kristaps  231:        p->tok = tok;
1.1       kristaps  232:        return(p);
                    233: }
                    234:
                    235:
                    236: int
                    237: man_elem_alloc(struct man *man, int line, int pos, int tok)
                    238: {
                    239:        struct man_node *p;
                    240:
1.16      kristaps  241:        p = man_node_alloc(line, pos, MAN_ELEM, tok);
                    242:        if (NULL == p)
                    243:                return(0);
1.1       kristaps  244:        return(man_node_append(man, p));
                    245: }
                    246:
                    247:
                    248: int
                    249: man_word_alloc(struct man *man,
                    250:                int line, int pos, const char *word)
                    251: {
                    252:        struct man_node *p;
                    253:
1.16      kristaps  254:        p = man_node_alloc(line, pos, MAN_TEXT, -1);
                    255:        if (NULL == p)
                    256:                return(0);
1.1       kristaps  257:        if (NULL == (p->string = strdup(word)))
1.16      kristaps  258:                return(0);
1.1       kristaps  259:        return(man_node_append(man, p));
                    260: }
                    261:
                    262:
                    263: void
                    264: man_node_free(struct man_node *p)
                    265: {
                    266:
                    267:        if (p->string)
                    268:                free(p->string);
1.24      kristaps  269:        if (p->parent)
                    270:                p->parent->nchild--;
1.1       kristaps  271:        free(p);
                    272: }
                    273:
                    274:
                    275: void
                    276: man_node_freelist(struct man_node *p)
                    277: {
                    278:
                    279:        if (p->child)
                    280:                man_node_freelist(p->child);
                    281:        if (p->next)
                    282:                man_node_freelist(p->next);
                    283:
1.24      kristaps  284:        assert(0 == p->nchild);
1.1       kristaps  285:        man_node_free(p);
                    286: }
                    287:
                    288:
                    289: static int
                    290: man_ptext(struct man *m, int line, char *buf)
                    291: {
                    292:
                    293:        if ( ! man_word_alloc(m, line, 0, buf))
                    294:                return(0);
                    295:        m->next = MAN_NEXT_SIBLING;
1.11      kristaps  296:
                    297:        /*
                    298:         * If this is one of the zany NLINE macros that consumes the
                    299:         * next line of input as being influenced, then close out the
                    300:         * existing macro "scope" and continue processing.
                    301:         */
                    302:
                    303:        if ( ! (MAN_NLINE & m->flags))
                    304:                return(1);
                    305:
                    306:        m->flags &= ~MAN_NLINE;
                    307:        m->last = m->last->parent;
                    308:
                    309:        assert(MAN_ROOT != m->last->type);
                    310:        if ( ! man_valid_post(m))
                    311:                return(0);
                    312:        if ( ! man_action_post(m))
                    313:                return(0);
                    314:
1.1       kristaps  315:        return(1);
                    316: }
                    317:
                    318:
                    319: int
                    320: man_pmacro(struct man *m, int ln, char *buf)
                    321: {
1.11      kristaps  322:        int               i, j, c, ppos, fl;
1.1       kristaps  323:        char              mac[5];
1.11      kristaps  324:        struct man_node  *n;
1.1       kristaps  325:
                    326:        /* Comments and empties are quickly ignored. */
                    327:
1.11      kristaps  328:        n = m->last;
                    329:        fl = MAN_NLINE & m->flags;
                    330:
1.1       kristaps  331:        if (0 == buf[1])
1.11      kristaps  332:                goto out;
1.1       kristaps  333:
1.9       kristaps  334:        i = 1;
                    335:
                    336:        if (' ' == buf[i]) {
                    337:                i++;
1.1       kristaps  338:                while (buf[i] && ' ' == buf[i])
                    339:                        i++;
                    340:                if (0 == buf[i])
1.11      kristaps  341:                        goto out;
1.1       kristaps  342:        }
                    343:
1.10      kristaps  344:        ppos = i;
                    345:
1.1       kristaps  346:        /* Copy the first word into a nil-terminated buffer. */
                    347:
1.10      kristaps  348:        for (j = 0; j < 4; j++, i++) {
                    349:                if (0 == (mac[j] = buf[i]))
1.1       kristaps  350:                        break;
1.10      kristaps  351:                else if (' ' == buf[i])
1.1       kristaps  352:                        break;
                    353:        }
                    354:
1.9       kristaps  355:        mac[j] = 0;
1.1       kristaps  356:
1.9       kristaps  357:        if (j == 4 || j < 1) {
1.7       kristaps  358:                if ( ! (MAN_IGN_MACRO & m->pflags)) {
1.10      kristaps  359:                        (void)man_verr(m, ln, ppos,
1.7       kristaps  360:                                "ill-formed macro: %s", mac);
                    361:                        goto err;
                    362:                }
1.10      kristaps  363:                if ( ! man_vwarn(m, ln, ppos,
                    364:                                "ill-formed macro: %s", mac))
1.7       kristaps  365:                        goto err;
1.12      kristaps  366:                return(1);
1.7       kristaps  367:        }
1.1       kristaps  368:
                    369:        if (MAN_MAX == (c = man_hash_find(m->htab, mac))) {
1.7       kristaps  370:                if ( ! (MAN_IGN_MACRO & m->pflags)) {
1.10      kristaps  371:                        (void)man_verr(m, ln, ppos,
1.7       kristaps  372:                                "unknown macro: %s", mac);
                    373:                        goto err;
                    374:                }
1.10      kristaps  375:                if ( ! man_vwarn(m, ln, ppos,
                    376:                                "unknown macro: %s", mac))
1.7       kristaps  377:                        goto err;
1.12      kristaps  378:                return(1);
1.1       kristaps  379:        }
                    380:
                    381:        /* The macro is sane.  Jump to the next word. */
                    382:
                    383:        while (buf[i] && ' ' == buf[i])
                    384:                i++;
                    385:
                    386:        /* Begin recursive parse sequence. */
                    387:
1.10      kristaps  388:        if ( ! man_macro(m, c, ln, ppos, &i, buf))
1.1       kristaps  389:                goto err;
                    390:
1.11      kristaps  391: out:
                    392:        if (fl) {
                    393:                /*
                    394:                 * A NLINE macro has been immediately followed with
1.17      kristaps  395:                 * another.  Close out the preceding macro's scope, and
1.11      kristaps  396:                 * continue.
                    397:                 */
                    398:                assert(MAN_ROOT != m->last->type);
                    399:                assert(m->last->parent);
                    400:                assert(MAN_ROOT != m->last->parent->type);
                    401:
                    402:                if (n != m->last)
                    403:                        m->last = m->last->parent;
                    404:
                    405:                if ( ! man_valid_post(m))
                    406:                        return(0);
                    407:                if ( ! man_action_post(m))
                    408:                        return(0);
                    409:                m->next = MAN_NEXT_SIBLING;
                    410:                m->flags &= ~MAN_NLINE;
                    411:        }
                    412:
1.1       kristaps  413:        return(1);
                    414:
                    415: err:   /* Error out. */
                    416:
1.2       kristaps  417:        m->flags |= MAN_HALT;
1.1       kristaps  418:        return(0);
                    419: }
1.3       kristaps  420:
1.4       kristaps  421:
                    422: int
                    423: man_verr(struct man *man, int ln, int pos, const char *fmt, ...)
                    424: {
                    425:        char             buf[256];
                    426:        va_list          ap;
                    427:
                    428:        if (NULL == man->cb.man_err)
                    429:                return(0);
                    430:
                    431:        va_start(ap, fmt);
                    432:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
                    433:        va_end(ap);
                    434:        return((*man->cb.man_err)(man->data, ln, pos, buf));
                    435: }
                    436:
                    437:
                    438: int
                    439: man_vwarn(struct man *man, int ln, int pos, const char *fmt, ...)
                    440: {
                    441:        char             buf[256];
                    442:        va_list          ap;
                    443:
                    444:        if (NULL == man->cb.man_warn)
                    445:                return(0);
                    446:
                    447:        va_start(ap, fmt);
                    448:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
                    449:        va_end(ap);
                    450:        return((*man->cb.man_warn)(man->data, ln, pos, buf));
                    451: }
                    452:
                    453:
1.23      kristaps  454: int
1.27      kristaps  455: man_err(struct man *m, int line, int pos, int iserr, enum merr type)
1.23      kristaps  456: {
                    457:        const char       *p;
                    458:
1.27      kristaps  459:        p = __man_merrnames[(int)type];
1.23      kristaps  460:        assert(p);
                    461:
                    462:        if (iserr)
                    463:                return(man_verr(m, line, pos, p));
                    464:
                    465:        return(man_vwarn(m, line, pos, p));
                    466: }

CVSweb