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

Annotation of mandoc/man.c, Revision 1.2

1.2     ! kristaps    1: /* $Id: man.c,v 1.1 2009/03/23 14:22:11 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 *
                     86: man_alloc(void)
                     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:
                     96:        p->htab = man_hash_alloc();
                     97:        return(p);
                     98: }
                     99:
                    100:
                    101: int
                    102: man_endparse(struct man *m)
                    103: {
                    104:
1.2     ! kristaps  105:        /* FIXME. */
1.1       kristaps  106:        return(1);
                    107: }
                    108:
                    109:
                    110: int
                    111: man_parseln(struct man *m, int ln, char *buf)
                    112: {
                    113:
                    114:        return('.' == *buf ?
                    115:                        man_pmacro(m, ln, buf) :
                    116:                        man_ptext(m, ln, buf));
                    117: }
                    118:
                    119:
1.2     ! kristaps  120: static void
        !           121: man_free1(struct man *man)
        !           122: {
        !           123:
        !           124:        if (man->first)
        !           125:                man_node_freelist(man->first);
        !           126:        if (man->meta.title)
        !           127:                free(man->meta.title);
        !           128:        if (man->meta.os)
        !           129:                free(man->meta.os);
        !           130:        if (man->meta.vol)
        !           131:                free(man->meta.vol);
        !           132: }
        !           133:
        !           134:
        !           135: static void
        !           136: man_alloc1(struct man *m)
        !           137: {
        !           138:
        !           139:        bzero(&m->meta, sizeof(struct man_meta));
        !           140:        m->flags = 0;
        !           141:        m->last = calloc(1, sizeof(struct man_node));
        !           142:        if (NULL == m->last)
        !           143:                err(1, "calloc");
        !           144:        m->first = m->last;
        !           145:        m->last->type = MAN_ROOT;
        !           146:        m->next = MAN_NEXT_CHILD;
        !           147: }
        !           148:
        !           149:
1.1       kristaps  150: static int
                    151: man_node_append(struct man *man, struct man_node *p)
                    152: {
                    153:
                    154:        assert(man->last);
                    155:        assert(man->first);
                    156:        assert(MAN_ROOT != p->type);
                    157:
                    158:        switch (man->next) {
                    159:        case (MAN_NEXT_SIBLING):
                    160:                man->last->next = p;
                    161:                p->prev = man->last;
                    162:                p->parent = man->last->parent;
                    163:                break;
                    164:        case (MAN_NEXT_CHILD):
                    165:                man->last->child = p;
                    166:                p->parent = man->last;
                    167:                break;
                    168:        default:
                    169:                abort();
                    170:                /* NOTREACHED */
                    171:        }
                    172:
                    173: #if 0
                    174:        if ( ! man_valid_pre(man, p))
                    175:                return(0);
                    176:        if ( ! man_action_pre(man, p))
                    177:                return(0);
                    178: #endif
                    179:
1.2     ! kristaps  180:        man->last = p;
        !           181:
1.1       kristaps  182:        switch (p->type) {
1.2     ! kristaps  183:        case (MAN_TEXT):
        !           184: #if 0
        !           185:                if ( ! man_valid_post(man))
        !           186:                        return(0);
        !           187:                if ( ! man_action_post(man))
        !           188:                        return(0);
        !           189: #endif
1.1       kristaps  190:                break;
                    191:        default:
                    192:                break;
                    193:        }
                    194:
                    195:        return(1);
                    196: }
                    197:
                    198:
                    199: static struct man_node *
                    200: man_node_alloc(int line, int pos, enum man_type type)
                    201: {
                    202:        struct man_node *p;
                    203:
                    204:        if (NULL == (p = calloc(1, sizeof(struct man_node))))
                    205:                err(1, "malloc");
                    206:        p->line = line;
                    207:        p->pos = pos;
                    208:        p->type = type;
                    209:
                    210:        return(p);
                    211: }
                    212:
                    213:
                    214: int
                    215: man_elem_alloc(struct man *man, int line, int pos, int tok)
                    216: {
                    217:        struct man_node *p;
                    218:
                    219:        p = man_node_alloc(line, pos, MAN_ELEM);
                    220:        p->tok = tok;
                    221:
                    222:        return(man_node_append(man, p));
                    223: }
                    224:
                    225:
                    226: int
                    227: man_word_alloc(struct man *man,
                    228:                int line, int pos, const char *word)
                    229: {
                    230:        struct man_node *p;
                    231:
                    232:        p = man_node_alloc(line, pos, MAN_TEXT);
                    233:        if (NULL == (p->string = strdup(word)))
                    234:                err(1, "strdup");
                    235:
                    236:        return(man_node_append(man, p));
                    237: }
                    238:
                    239:
                    240: void
                    241: man_node_free(struct man_node *p)
                    242: {
                    243:
                    244:        if (p->string)
                    245:                free(p->string);
                    246:        free(p);
                    247: }
                    248:
                    249:
                    250: void
                    251: man_node_freelist(struct man_node *p)
                    252: {
                    253:
                    254:        if (p->child)
                    255:                man_node_freelist(p->child);
                    256:        if (p->next)
                    257:                man_node_freelist(p->next);
                    258:
                    259:        man_node_free(p);
                    260: }
                    261:
                    262:
                    263: static int
                    264: man_ptext(struct man *m, int line, char *buf)
                    265: {
                    266:
                    267:        if ( ! man_word_alloc(m, line, 0, buf))
                    268:                return(0);
                    269:        m->next = MAN_NEXT_SIBLING;
                    270:        return(1);
                    271: }
                    272:
                    273:
                    274: int
                    275: man_pmacro(struct man *m, int ln, char *buf)
                    276: {
                    277:        int               i, c;
                    278:        char              mac[5];
                    279:
                    280:        /* Comments and empties are quickly ignored. */
                    281:
                    282:        if (0 == buf[1])
                    283:                return(1);
                    284:
                    285:        if (' ' == buf[1]) {
                    286:                i = 2;
                    287:                while (buf[i] && ' ' == buf[i])
                    288:                        i++;
                    289:                if (0 == buf[i])
                    290:                        return(1);
                    291:                warnx("invalid syntax");
                    292:                return(0);
                    293:        }
                    294:
                    295:        if (buf[1] && '\\' == buf[1])
                    296:                if (buf[2] && '\"' == buf[2])
                    297:                        return(1);
                    298:
                    299:        /* Copy the first word into a nil-terminated buffer. */
                    300:
                    301:        for (i = 1; i < 5; i++) {
                    302:                if (0 == (mac[i - 1] = buf[i]))
                    303:                        break;
                    304:                else if (' ' == buf[i])
                    305:                        break;
                    306:        }
                    307:
                    308:        mac[i - 1] = 0;
                    309:
                    310:        if (i == 5 || i <= 1) {
                    311:                warnx("unknown macro: %s", mac);
                    312:                goto err;
                    313:        }
                    314:
                    315:        if (MAN_MAX == (c = man_hash_find(m->htab, mac))) {
                    316:                warnx("unknown macro: %s", mac);
                    317:                goto err;
                    318:        }
                    319:
                    320:        /* The macro is sane.  Jump to the next word. */
                    321:
                    322:        while (buf[i] && ' ' == buf[i])
                    323:                i++;
                    324:
                    325:        /* Begin recursive parse sequence. */
                    326:
1.2     ! kristaps  327:        if ( ! man_macro(m, c, ln, 1, &i, buf))
1.1       kristaps  328:                goto err;
                    329:
                    330:        return(1);
                    331:
                    332: err:   /* Error out. */
                    333:
1.2     ! kristaps  334:        m->flags |= MAN_HALT;
1.1       kristaps  335:        return(0);
                    336: }

CVSweb