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

Annotation of mandoc/man_action.c, Revision 1.5

1.5     ! kristaps    1: /* $Id: man_action.c,v 1.4 2009/03/26 09:55:39 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 *);
1.5     ! kristaps   40: static time_t    man_atotime(const char *);
1.2       kristaps   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 */
1.5     ! kristaps   63:        { NULL }, /* RI */
1.1       kristaps   64: };
                     65:
                     66:
                     67: int
                     68: man_action_post(struct man *m)
                     69: {
                     70:
                     71:        if (MAN_ACTED & m->last->flags)
                     72:                return(1);
                     73:        m->last->flags |= MAN_ACTED;
                     74:
                     75:        switch (m->last->type) {
                     76:        case (MAN_TEXT):
                     77:                break;
                     78:        case (MAN_ROOT):
                     79:                break;
                     80:        default:
                     81:                if (NULL == man_actions[m->last->tok].post)
                     82:                        break;
                     83:                return((*man_actions[m->last->tok].post)(m));
                     84:        }
                     85:        return(1);
                     86: }
                     87:
1.2       kristaps   88:
1.1       kristaps   89: static int
1.2       kristaps   90: post_TH(struct man *m)
1.1       kristaps   91: {
1.2       kristaps   92:        struct man_node *n;
                     93:        char            *ep;
                     94:        long             lval;
1.1       kristaps   95:
                     96:        if (m->meta.title)
                     97:                free(m->meta.title);
                     98:        if (m->meta.vol)
                     99:                free(m->meta.vol);
1.2       kristaps  100:        if (m->meta.source)
                    101:                free(m->meta.source);
1.1       kristaps  102:
1.2       kristaps  103:        m->meta.title = m->meta.vol = m->meta.source = NULL;
1.1       kristaps  104:        m->meta.msec = 0;
1.2       kristaps  105:        m->meta.date = 0;
                    106:
                    107:        /* ->TITLE<- MSEC DATE SOURCE VOL */
                    108:
                    109:        n = m->last->child;
                    110:        assert(n);
1.1       kristaps  111:
1.2       kristaps  112:        if (NULL == (m->meta.title = strdup(n->string)))
                    113:                return(man_verr(m, n->line, n->pos, "malloc"));
                    114:
                    115:        /* TITLE ->MSEC<- DATE SOURCE VOL */
1.1       kristaps  116:
1.2       kristaps  117:        n = n->next;
                    118:        assert(n);
1.1       kristaps  119:
1.2       kristaps  120:        errno = 0;
                    121:        lval = strtol(n->string, &ep, 10);
                    122:        if (n->string[0] != '\0' && *ep == '\0')
                    123:                m->meta.msec = (int)lval;
                    124:        else if ( ! man_vwarn(m, n->line, n->pos, "invalid section"))
                    125:                return(0);
1.1       kristaps  126:
1.2       kristaps  127:        /* TITLE MSEC ->DATE<- SOURCE VOL */
1.1       kristaps  128:
1.5     ! kristaps  129:        if (NULL == (n = n->next))
1.2       kristaps  130:                m->meta.date = time(NULL);
1.5     ! kristaps  131:        else if (0 == (m->meta.date = man_atotime(n->string))) {
1.2       kristaps  132:                if ( ! man_vwarn(m, n->line, n->pos, "invalid date"))
                    133:                        return(0);
                    134:                m->meta.date = time(NULL);
                    135:        }
1.1       kristaps  136:
1.2       kristaps  137:        /* TITLE MSEC DATE ->SOURCE<- VOL */
1.1       kristaps  138:
1.5     ! kristaps  139:        if (n && (n = n->next))
1.2       kristaps  140:                if (NULL == (m->meta.source = strdup(n->string)))
                    141:                        return(man_verr(m, n->line, n->pos, "malloc"));
1.1       kristaps  142:
1.2       kristaps  143:        /* TITLE MSEC DATE SOURCE ->VOL<- */
1.1       kristaps  144:
1.5     ! kristaps  145:        if (n && (n = n->next))
1.2       kristaps  146:                if (NULL == (m->meta.vol = strdup(n->string)))
                    147:                        return(man_verr(m, n->line, n->pos, "malloc"));
1.1       kristaps  148:
                    149:        /*
                    150:         * The end document shouldn't have the prologue macros as part
                    151:         * of the syntax tree (they encompass only meta-data).
                    152:         */
                    153:
1.4       kristaps  154:        if (m->last->parent->child == m->last) {
                    155:                assert(MAN_ROOT == m->last->parent->type);
                    156:                m->last->parent->child = NULL;
                    157:                n = m->last;
                    158:                m->last = m->last->parent;
                    159:                m->next = MAN_NEXT_CHILD;
                    160:                assert(m->last == m->first);
                    161:        } else {
                    162:                assert(m->last->prev);
                    163:                m->last->prev->next = NULL;
                    164:                n = m->last;
                    165:                m->last = m->last->prev;
                    166:                m->next = MAN_NEXT_SIBLING;
                    167:        }
1.2       kristaps  168:
                    169:        man_node_freelist(n);
                    170:        return(1);
                    171: }
1.1       kristaps  172:
                    173:
1.2       kristaps  174: static time_t
                    175: man_atotime(const char *p)
                    176: {
                    177:        struct tm        tm;
                    178:        char            *pp;
                    179:
                    180:        (void)memset(&tm, 0, sizeof(struct tm));
                    181:
                    182:        if ((pp = strptime(p, "%b %d %Y", &tm)) && 0 == *pp)
                    183:                return(mktime(&tm));
                    184:        if ((pp = strptime(p, "%d %b %Y", &tm)) && 0 == *pp)
                    185:                return(mktime(&tm));
                    186:        if ((pp = strptime(p, "%b %d, %Y", &tm)) && 0 == *pp)
                    187:                return(mktime(&tm));
                    188:        if ((pp = strptime(p, "%b %Y", &tm)) && 0 == *pp)
                    189:                return(mktime(&tm));
1.1       kristaps  190:
1.2       kristaps  191:        return(0);
1.1       kristaps  192: }

CVSweb