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

Annotation of mandoc/man.c, Revision 1.6

1.6     ! kristaps    1: /* $Id: man.c,v 1.5 2009/03/25 15:36:05 kristaps Exp $ */
1.1       kristaps    2: /*
                      3:  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@openbsd.org>
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the
                      7:  * above copyright notice and this permission notice appear in all
                      8:  * copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
                     11:  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
                     12:  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
                     13:  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
                     14:  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
                     15:  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
                     16:  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
                     17:  * PERFORMANCE OF THIS SOFTWARE.
                     18:  */
                     19: #include <assert.h>
                     20: #include <ctype.h>
                     21: #include <err.h>
                     22: #include <stdarg.h>
                     23: #include <stdlib.h>
                     24: #include <stdio.h>
                     25: #include <string.h>
                     26:
                     27: #include "libman.h"
                     28:
                     29: const  char *const __man_macronames[MAN_MAX] = {
                     30:        "\\\"",         "TH",           "SH",           "SS",
                     31:        "TP",           "LP",           "PP",           "P",
                     32:        "IP",           "HP",           "SM",           "SB",
                     33:        "BI",           "IB",           "BR",           "RB",
1.2       kristaps   34:        "R",            "B",            "I",            "IR"
1.1       kristaps   35:        };
                     36:
                     37: const  char * const *man_macronames = __man_macronames;
                     38:
                     39: static struct man_node *man_node_alloc(int, int, enum man_type);
                     40: static int              man_node_append(struct man *,
                     41:                                struct man_node *);
                     42: static int              man_ptext(struct man *, int, char *);
                     43: static int              man_pmacro(struct man *, int, char *);
1.2       kristaps   44: static void             man_free1(struct man *);
                     45: static void             man_alloc1(struct man *);
1.1       kristaps   46:
                     47:
                     48: const struct man_node *
1.2       kristaps   49: man_node(const struct man *m)
1.1       kristaps   50: {
                     51:
1.2       kristaps   52:        return(MAN_HALT & m->flags ? NULL : m->first);
1.1       kristaps   53: }
                     54:
                     55:
                     56: const struct man_meta *
1.2       kristaps   57: man_meta(const struct man *m)
1.1       kristaps   58: {
                     59:
1.2       kristaps   60:        return(MAN_HALT & m->flags ? NULL : &m->meta);
1.1       kristaps   61: }
                     62:
                     63:
                     64: void
                     65: man_reset(struct man *man)
                     66: {
                     67:
1.2       kristaps   68:        man_free1(man);
                     69:        man_alloc1(man);
1.1       kristaps   70: }
                     71:
                     72:
                     73: void
                     74: man_free(struct man *man)
                     75: {
                     76:
1.2       kristaps   77:        man_free1(man);
                     78:
1.1       kristaps   79:        if (man->htab)
                     80:                man_hash_free(man->htab);
                     81:        free(man);
                     82: }
                     83:
                     84:
                     85: struct man *
1.4       kristaps   86: man_alloc(void *data, const struct man_cb *cb)
1.1       kristaps   87: {
                     88:        struct man      *p;
                     89:
1.2       kristaps   90:        p = calloc(1, sizeof(struct man));
                     91:        if (NULL == p)
                     92:                err(1, "calloc");
                     93:
                     94:        man_alloc1(p);
1.1       kristaps   95:
1.4       kristaps   96:        if (cb)
                     97:                (void)memcpy(&p->cb, cb, sizeof(struct man_cb));
                     98:
1.1       kristaps   99:        p->htab = man_hash_alloc();
1.4       kristaps  100:        p->data = data;
1.1       kristaps  101:        return(p);
                    102: }
                    103:
                    104:
                    105: int
                    106: man_endparse(struct man *m)
                    107: {
                    108:
1.3       kristaps  109:        if (MAN_HALT & m->flags)
                    110:                return(0);
                    111:        else if (man_macroend(m))
                    112:                return(1);
                    113:        m->flags |= MAN_HALT;
                    114:        return(0);
1.1       kristaps  115: }
                    116:
                    117:
                    118: int
                    119: man_parseln(struct man *m, int ln, char *buf)
                    120: {
                    121:
                    122:        return('.' == *buf ?
                    123:                        man_pmacro(m, ln, buf) :
                    124:                        man_ptext(m, ln, buf));
                    125: }
                    126:
                    127:
1.2       kristaps  128: static void
                    129: man_free1(struct man *man)
                    130: {
                    131:
                    132:        if (man->first)
                    133:                man_node_freelist(man->first);
                    134:        if (man->meta.title)
                    135:                free(man->meta.title);
1.6     ! kristaps  136:        if (man->meta.source)
        !           137:                free(man->meta.source);
1.2       kristaps  138:        if (man->meta.vol)
                    139:                free(man->meta.vol);
                    140: }
                    141:
                    142:
                    143: static void
                    144: man_alloc1(struct man *m)
                    145: {
                    146:
                    147:        bzero(&m->meta, sizeof(struct man_meta));
                    148:        m->flags = 0;
                    149:        m->last = calloc(1, sizeof(struct man_node));
                    150:        if (NULL == m->last)
                    151:                err(1, "calloc");
                    152:        m->first = m->last;
                    153:        m->last->type = MAN_ROOT;
                    154:        m->next = MAN_NEXT_CHILD;
                    155: }
                    156:
                    157:
1.1       kristaps  158: static int
                    159: man_node_append(struct man *man, struct man_node *p)
                    160: {
                    161:
                    162:        assert(man->last);
                    163:        assert(man->first);
                    164:        assert(MAN_ROOT != p->type);
                    165:
                    166:        switch (man->next) {
                    167:        case (MAN_NEXT_SIBLING):
                    168:                man->last->next = p;
                    169:                p->prev = man->last;
                    170:                p->parent = man->last->parent;
                    171:                break;
                    172:        case (MAN_NEXT_CHILD):
                    173:                man->last->child = p;
                    174:                p->parent = man->last;
                    175:                break;
                    176:        default:
                    177:                abort();
                    178:                /* NOTREACHED */
                    179:        }
                    180:
1.2       kristaps  181:        man->last = p;
                    182:
1.1       kristaps  183:        switch (p->type) {
1.2       kristaps  184:        case (MAN_TEXT):
                    185:                if ( ! man_valid_post(man))
                    186:                        return(0);
                    187:                if ( ! man_action_post(man))
                    188:                        return(0);
1.1       kristaps  189:                break;
                    190:        default:
                    191:                break;
                    192:        }
                    193:
                    194:        return(1);
                    195: }
                    196:
                    197:
                    198: static struct man_node *
                    199: man_node_alloc(int line, int pos, enum man_type type)
                    200: {
                    201:        struct man_node *p;
                    202:
                    203:        if (NULL == (p = calloc(1, sizeof(struct man_node))))
                    204:                err(1, "malloc");
                    205:        p->line = line;
                    206:        p->pos = pos;
                    207:        p->type = type;
                    208:
                    209:        return(p);
                    210: }
                    211:
                    212:
                    213: int
                    214: man_elem_alloc(struct man *man, int line, int pos, int tok)
                    215: {
                    216:        struct man_node *p;
                    217:
                    218:        p = man_node_alloc(line, pos, MAN_ELEM);
                    219:        p->tok = tok;
                    220:
                    221:        return(man_node_append(man, p));
                    222: }
                    223:
                    224:
                    225: int
                    226: man_word_alloc(struct man *man,
                    227:                int line, int pos, const char *word)
                    228: {
                    229:        struct man_node *p;
                    230:
                    231:        p = man_node_alloc(line, pos, MAN_TEXT);
                    232:        if (NULL == (p->string = strdup(word)))
                    233:                err(1, "strdup");
                    234:
                    235:        return(man_node_append(man, p));
                    236: }
                    237:
                    238:
                    239: void
                    240: man_node_free(struct man_node *p)
                    241: {
                    242:
                    243:        if (p->string)
                    244:                free(p->string);
                    245:        free(p);
                    246: }
                    247:
                    248:
                    249: void
                    250: man_node_freelist(struct man_node *p)
                    251: {
                    252:
                    253:        if (p->child)
                    254:                man_node_freelist(p->child);
                    255:        if (p->next)
                    256:                man_node_freelist(p->next);
                    257:
                    258:        man_node_free(p);
                    259: }
                    260:
                    261:
                    262: static int
                    263: man_ptext(struct man *m, int line, char *buf)
                    264: {
                    265:
                    266:        if ( ! man_word_alloc(m, line, 0, buf))
                    267:                return(0);
                    268:        m->next = MAN_NEXT_SIBLING;
                    269:        return(1);
                    270: }
                    271:
                    272:
                    273: int
                    274: man_pmacro(struct man *m, int ln, char *buf)
                    275: {
                    276:        int               i, c;
                    277:        char              mac[5];
                    278:
                    279:        /* Comments and empties are quickly ignored. */
                    280:
                    281:        if (0 == buf[1])
                    282:                return(1);
                    283:
                    284:        if (' ' == buf[1]) {
                    285:                i = 2;
                    286:                while (buf[i] && ' ' == buf[i])
                    287:                        i++;
                    288:                if (0 == buf[i])
                    289:                        return(1);
                    290:                warnx("invalid syntax");
                    291:                return(0);
                    292:        }
                    293:
                    294:        if (buf[1] && '\\' == buf[1])
                    295:                if (buf[2] && '\"' == buf[2])
                    296:                        return(1);
                    297:
                    298:        /* Copy the first word into a nil-terminated buffer. */
                    299:
                    300:        for (i = 1; i < 5; i++) {
                    301:                if (0 == (mac[i - 1] = buf[i]))
                    302:                        break;
                    303:                else if (' ' == buf[i])
                    304:                        break;
                    305:        }
                    306:
                    307:        mac[i - 1] = 0;
                    308:
                    309:        if (i == 5 || i <= 1) {
                    310:                warnx("unknown macro: %s", mac);
                    311:                goto err;
                    312:        }
                    313:
                    314:        if (MAN_MAX == (c = man_hash_find(m->htab, mac))) {
                    315:                warnx("unknown macro: %s", mac);
                    316:                goto err;
                    317:        }
                    318:
                    319:        /* The macro is sane.  Jump to the next word. */
                    320:
                    321:        while (buf[i] && ' ' == buf[i])
                    322:                i++;
                    323:
                    324:        /* Begin recursive parse sequence. */
                    325:
1.2       kristaps  326:        if ( ! man_macro(m, c, ln, 1, &i, buf))
1.1       kristaps  327:                goto err;
                    328:
                    329:        return(1);
                    330:
                    331: err:   /* Error out. */
                    332:
1.2       kristaps  333:        m->flags |= MAN_HALT;
1.1       kristaps  334:        return(0);
                    335: }
1.3       kristaps  336:
1.4       kristaps  337:
                    338: int
                    339: man_verr(struct man *man, int ln, int pos, const char *fmt, ...)
                    340: {
                    341:        char             buf[256];
                    342:        va_list          ap;
                    343:
                    344:        if (NULL == man->cb.man_err)
                    345:                return(0);
                    346:
                    347:        va_start(ap, fmt);
                    348:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
                    349:        va_end(ap);
                    350:        return((*man->cb.man_err)(man->data, ln, pos, buf));
                    351: }
                    352:
                    353:
                    354: int
                    355: man_vwarn(struct man *man, int ln, int pos, const char *fmt, ...)
                    356: {
                    357:        char             buf[256];
                    358:        va_list          ap;
                    359:
                    360:        if (NULL == man->cb.man_warn)
                    361:                return(0);
                    362:
                    363:        va_start(ap, fmt);
                    364:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
                    365:        va_end(ap);
                    366:        return((*man->cb.man_warn)(man->data, ln, pos, buf));
                    367: }
                    368:
                    369:

CVSweb