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

Annotation of mandoc/man_action.c, Revision 1.2

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

CVSweb