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

Annotation of mandoc/man_action.c, Revision 1.3

1.3     ! kristaps    1: /* $Id: man_action.c,v 1.2 2009/03/25 16:07:36 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 <sys/utsname.h>
                     20:
                     21: #include <assert.h>
                     22: #include <err.h>
                     23: #include <errno.h>
                     24: #include <stdio.h>
                     25: #include <stdlib.h>
                     26: #include <string.h>
                     27:
                     28: #include "libman.h"
                     29:
1.3     ! kristaps   30: #ifdef __linux__
        !            31: extern char            *strptime(const char *, const char *, struct tm *);
        !            32: #endif
1.1       kristaps   33:
                     34: struct actions {
                     35:        int     (*post)(struct man *);
                     36: };
                     37:
                     38:
1.2       kristaps   39: static int       post_TH(struct man *);
                     40: static time_t    man_atotime(const char *);
                     41:
1.1       kristaps   42: const  struct actions man_actions[MAN_MAX] = {
                     43:        { NULL }, /* __ */
1.2       kristaps   44:        { post_TH }, /* TH */
1.1       kristaps   45:        { NULL }, /* SH */
                     46:        { NULL }, /* SS */
                     47:        { NULL }, /* TP */
                     48:        { NULL }, /* LP */
                     49:        { NULL }, /* PP */
                     50:        { NULL }, /* P */
                     51:        { NULL }, /* IP */
                     52:        { NULL }, /* HP */
                     53:        { NULL }, /* SM */
                     54:        { NULL }, /* SB */
                     55:        { NULL }, /* BI */
                     56:        { NULL }, /* IB */
                     57:        { NULL }, /* BR */
                     58:        { NULL }, /* RB */
                     59:        { NULL }, /* R */
                     60:        { NULL }, /* B */
                     61:        { NULL }, /* I */
                     62:        { NULL }, /* IR */
                     63: };
                     64:
                     65:
                     66: int
                     67: man_action_post(struct man *m)
                     68: {
                     69:
                     70:        if (MAN_ACTED & m->last->flags)
                     71:                return(1);
                     72:        m->last->flags |= MAN_ACTED;
                     73:
                     74:        switch (m->last->type) {
                     75:        case (MAN_TEXT):
                     76:                break;
                     77:        case (MAN_ROOT):
                     78:                break;
                     79:        default:
                     80:                if (NULL == man_actions[m->last->tok].post)
                     81:                        break;
                     82:                return((*man_actions[m->last->tok].post)(m));
                     83:        }
                     84:        return(1);
                     85: }
                     86:
1.2       kristaps   87:
1.1       kristaps   88: static int
1.2       kristaps   89: post_TH(struct man *m)
1.1       kristaps   90: {
1.2       kristaps   91:        struct man_node *n;
                     92:        char            *ep;
                     93:        long             lval;
1.1       kristaps   94:
                     95:        if (m->meta.title)
                     96:                free(m->meta.title);
                     97:        if (m->meta.vol)
                     98:                free(m->meta.vol);
1.2       kristaps   99:        if (m->meta.source)
                    100:                free(m->meta.source);
1.1       kristaps  101:
1.2       kristaps  102:        m->meta.title = m->meta.vol = m->meta.source = NULL;
1.1       kristaps  103:        m->meta.msec = 0;
1.2       kristaps  104:        m->meta.date = 0;
                    105:
                    106:        /* ->TITLE<- MSEC DATE SOURCE VOL */
                    107:
                    108:        n = m->last->child;
                    109:        assert(n);
1.1       kristaps  110:
1.2       kristaps  111:        if (NULL == (m->meta.title = strdup(n->string)))
                    112:                return(man_verr(m, n->line, n->pos, "malloc"));
                    113:
                    114:        /* TITLE ->MSEC<- DATE SOURCE VOL */
1.1       kristaps  115:
1.2       kristaps  116:        n = n->next;
                    117:        assert(n);
1.1       kristaps  118:
1.2       kristaps  119:        errno = 0;
                    120:        lval = strtol(n->string, &ep, 10);
                    121:        if (n->string[0] != '\0' && *ep == '\0')
                    122:                m->meta.msec = (int)lval;
                    123:        else if ( ! man_vwarn(m, n->line, n->pos, "invalid section"))
                    124:                return(0);
1.1       kristaps  125:
1.2       kristaps  126:        /* TITLE MSEC ->DATE<- SOURCE VOL */
1.1       kristaps  127:
                    128:        if (NULL == (n = n->next)) {
1.2       kristaps  129:                m->meta.date = time(NULL);
                    130:                return(1);
1.1       kristaps  131:        }
                    132:
1.2       kristaps  133:        if (0 == (m->meta.date = man_atotime(n->string))) {
                    134:                if ( ! man_vwarn(m, n->line, n->pos, "invalid date"))
                    135:                        return(0);
                    136:                m->meta.date = time(NULL);
                    137:        }
1.1       kristaps  138:
1.2       kristaps  139:        /* TITLE MSEC DATE ->SOURCE<- VOL */
1.1       kristaps  140:
1.2       kristaps  141:        if ((n = n->next))
                    142:                if (NULL == (m->meta.source = strdup(n->string)))
                    143:                        return(man_verr(m, n->line, n->pos, "malloc"));
1.1       kristaps  144:
1.2       kristaps  145:        /* TITLE MSEC DATE SOURCE ->VOL<- */
1.1       kristaps  146:
1.2       kristaps  147:        if ((n = n->next))
                    148:                if (NULL == (m->meta.vol = strdup(n->string)))
                    149:                        return(man_verr(m, n->line, n->pos, "malloc"));
1.1       kristaps  150:
                    151:        /*
                    152:         * The end document shouldn't have the prologue macros as part
                    153:         * of the syntax tree (they encompass only meta-data).
                    154:         */
                    155:
1.2       kristaps  156:        assert(MAN_ROOT == m->last->parent->type);
                    157:        m->last->parent->child = NULL;
                    158:        n = m->last;
                    159:        m->last = m->last->parent;
                    160:        m->next = MAN_NEXT_CHILD;
                    161:        assert(m->last == m->first);
                    162:
                    163:        man_node_freelist(n);
                    164:        return(1);
                    165: }
1.1       kristaps  166:
                    167:
1.2       kristaps  168: static time_t
                    169: man_atotime(const char *p)
                    170: {
                    171:        struct tm        tm;
                    172:        char            *pp;
                    173:
                    174:        (void)memset(&tm, 0, sizeof(struct tm));
                    175:
                    176:        if ((pp = strptime(p, "%b %d %Y", &tm)) && 0 == *pp)
                    177:                return(mktime(&tm));
                    178:        if ((pp = strptime(p, "%d %b %Y", &tm)) && 0 == *pp)
                    179:                return(mktime(&tm));
                    180:        if ((pp = strptime(p, "%b %d, %Y", &tm)) && 0 == *pp)
                    181:                return(mktime(&tm));
                    182:        if ((pp = strptime(p, "%b %Y", &tm)) && 0 == *pp)
                    183:                return(mktime(&tm));
1.1       kristaps  184:
1.2       kristaps  185:        return(0);
1.1       kristaps  186: }

CVSweb